1 //===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// \file 10 /// This file implements parsing of all OpenMP directives and clauses. 11 /// 12 //===----------------------------------------------------------------------===// 13 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" 21 22 using namespace clang; 23 24 //===----------------------------------------------------------------------===// 25 // OpenMP declarative directives. 26 //===----------------------------------------------------------------------===// 27 28 namespace { 29 enum OpenMPDirectiveKindEx { 30 OMPD_cancellation = OMPD_unknown + 1, 31 OMPD_data, 32 OMPD_declare, 33 OMPD_end, 34 OMPD_end_declare, 35 OMPD_enter, 36 OMPD_exit, 37 OMPD_point, 38 OMPD_reduction, 39 OMPD_target_enter, 40 OMPD_target_exit, 41 OMPD_update, 42 OMPD_distribute_parallel, 43 OMPD_teams_distribute_parallel, 44 OMPD_target_teams_distribute_parallel 45 }; 46 47 class ThreadprivateListParserHelper final { 48 SmallVector<Expr *, 4> Identifiers; 49 Parser *P; 50 51 public: 52 ThreadprivateListParserHelper(Parser *P) : P(P) {} 53 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) { 54 ExprResult Res = 55 P->getActions().ActOnOpenMPIdExpression(P->getCurScope(), SS, NameInfo); 56 if (Res.isUsable()) 57 Identifiers.push_back(Res.get()); 58 } 59 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; } 60 }; 61 } // namespace 62 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) 68 return DKind; 69 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); 81 } 82 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(); 133 unsigned DKind = 134 Tok.isAnnotation() 135 ? static_cast<unsigned>(OMPD_unknown) 136 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok)); 137 if (DKind == OMPD_unknown) 138 return OMPD_unknown; 139 140 for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) { 141 if (DKind != F[I][0]) 142 continue; 143 144 Tok = P.getPreprocessor().LookAhead(0); 145 unsigned SDKind = 146 Tok.isAnnotation() 147 ? static_cast<unsigned>(OMPD_unknown) 148 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok)); 149 if (SDKind == OMPD_unknown) 150 continue; 151 152 if (SDKind == F[I][1]) { 153 P.ConsumeToken(); 154 DKind = F[I][2]; 155 } 156 } 157 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind) 158 : OMPD_unknown; 159 } 160 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)) { 168 P.ConsumeToken(); 169 Tok = P.getCurToken(); 170 WithOperator = true; 171 } 172 switch (Tok.getKind()) { 173 case tok::plus: // '+' 174 OOK = OO_Plus; 175 break; 176 case tok::minus: // '-' 177 OOK = OO_Minus; 178 break; 179 case tok::star: // '*' 180 OOK = OO_Star; 181 break; 182 case tok::amp: // '&' 183 OOK = OO_Amp; 184 break; 185 case tok::pipe: // '|' 186 OOK = OO_Pipe; 187 break; 188 case tok::caret: // '^' 189 OOK = OO_Caret; 190 break; 191 case tok::ampamp: // '&&' 192 OOK = OO_AmpAmp; 193 break; 194 case tok::pipepipe: // '||' 195 OOK = OO_PipePipe; 196 break; 197 case tok::identifier: // identifier 198 if (!WithOperator) 199 break; 200 LLVM_FALLTHROUGH; 201 default: 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(); 206 } 207 P.ConsumeToken(); 208 auto &DeclNames = Actions.getASTContext().DeclarationNames; 209 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo()) 210 : DeclNames.getCXXOperatorName(OOK); 211 } 212 213 /// Parse 'omp declare reduction' construct. 214 /// 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 '||'. 222 /// 223 Parser::DeclGroupPtrTy 224 Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) { 225 // Parse '('. 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(); 231 } 232 233 DeclarationName Name = parseOpenMPReductionId(*this); 234 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end)) 235 return DeclGroupPtrTy(); 236 237 // Consume ':'. 238 bool IsCorrect = !ExpectAndConsume(tok::colon); 239 240 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end)) 241 return DeclGroupPtrTy(); 242 243 IsCorrect = IsCorrect && !Name.isEmpty(); 244 245 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) { 246 Diag(Tok.getLocation(), diag::err_expected_type); 247 IsCorrect = false; 248 } 249 250 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end)) 251 return DeclGroupPtrTy(); 252 253 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes; 254 // Parse list of types until ':' token. 255 do { 256 ColonProtectionRAIIObject ColonRAII(*this); 257 SourceRange Range; 258 TypeResult TR = 259 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS); 260 if (TR.isUsable()) { 261 QualType ReductionType = 262 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR); 263 if (!ReductionType.isNull()) { 264 ReductionTypes.push_back( 265 std::make_pair(ReductionType, Range.getBegin())); 266 } 267 } else { 268 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end, 269 StopBeforeMatch); 270 } 271 272 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) 273 break; 274 275 // Consume ','. 276 if (ExpectAndConsume(tok::comma)) { 277 IsCorrect = false; 278 if (Tok.is(tok::annot_pragma_openmp_end)) { 279 Diag(Tok.getLocation(), diag::err_expected_type); 280 return DeclGroupPtrTy(); 281 } 282 } 283 } while (Tok.isNot(tok::annot_pragma_openmp_end)); 284 285 if (ReductionTypes.empty()) { 286 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); 287 return DeclGroupPtrTy(); 288 } 289 290 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end)) 291 return DeclGroupPtrTy(); 292 293 // Consume ':'. 294 if (ExpectAndConsume(tok::colon)) 295 IsCorrect = false; 296 297 if (Tok.is(tok::annot_pragma_openmp_end)) { 298 Diag(Tok.getLocation(), diag::err_expected_expression); 299 return DeclGroupPtrTy(); 300 } 301 302 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart( 303 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS); 304 305 // Parse <combiner> expression and then parse initializer if any for each 306 // correct type. 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()); 319 320 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) && 321 Tok.isNot(tok::annot_pragma_openmp_end)) { 322 TPA.Commit(); 323 IsCorrect = false; 324 break; 325 } 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")) { 332 ConsumeToken(); 333 } else { 334 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'"; 335 TPA.Commit(); 336 IsCorrect = false; 337 break; 338 } 339 // Parse '('. 340 BalancedDelimiterTracker T(*this, tok::l_paren, 341 tok::annot_pragma_openmp_end); 342 IsCorrect = 343 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") && 344 IsCorrect; 345 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 346 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope | 347 Scope::CompoundStmtScope | 348 Scope::OpenMPDirectiveScope); 349 // Parse expression. 350 VarDecl *OmpPrivParm = 351 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), 352 D); 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 ConsumeToken(); 357 ParseOpenMPReductionInitializerForDecl(OmpPrivParm); 358 } else { 359 InitializerResult = Actions.ActOnFinishFullExpr( 360 ParseAssignmentExpression().get(), D->getLocation(), 361 /*DiscardedValue=*/true); 362 } 363 Actions.ActOnOpenMPDeclareReductionInitializerEnd( 364 D, InitializerResult.get(), OmpPrivParm); 365 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) && 366 Tok.isNot(tok::annot_pragma_openmp_end)) { 367 TPA.Commit(); 368 IsCorrect = false; 369 break; 370 } 371 IsCorrect = 372 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid(); 373 } 374 } 375 376 ++I; 377 // Revert parsing if not the last type, otherwise accept it, we're done with 378 // parsing. 379 if (I != E) 380 TPA.Revert(); 381 else 382 TPA.Commit(); 383 } 384 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD, 385 IsCorrect); 386 } 387 388 void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) { 389 // Parse declarator '=' initializer. 390 // If a '==' or '+=' is found, suggest a fixit to '='. 391 if (isTokenEqualOrEqualTypo()) { 392 ConsumeToken(); 393 394 if (Tok.is(tok::code_completion)) { 395 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm); 396 Actions.FinalizeDeclaration(OmpPrivParm); 397 cutOffParsing(); 398 return; 399 } 400 401 ExprResult Init(ParseInitializer()); 402 403 if (Init.isInvalid()) { 404 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch); 405 Actions.ActOnInitializerError(OmpPrivParm); 406 } else { 407 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(), 408 /*DirectInit=*/false); 409 } 410 } else if (Tok.is(tok::l_paren)) { 411 // Parse C++ direct initializer: '(' expression-list ')' 412 BalancedDelimiterTracker T(*this, tok::l_paren); 413 T.consumeOpen(); 414 415 ExprVector Exprs; 416 CommaLocsTy CommaLocs; 417 418 if (ParseExpressionList(Exprs, CommaLocs, [this, OmpPrivParm, &Exprs] { 419 Actions.CodeCompleteConstructor( 420 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(), 421 OmpPrivParm->getLocation(), Exprs); 422 })) { 423 Actions.ActOnInitializerError(OmpPrivParm); 424 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch); 425 } else { 426 // Match the ')'. 427 T.consumeClose(); 428 429 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() && 430 "Unexpected number of commas!"); 431 432 ExprResult Initializer = Actions.ActOnParenListExpr( 433 T.getOpenLocation(), T.getCloseLocation(), Exprs); 434 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(), 435 /*DirectInit=*/true); 436 } 437 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { 438 // Parse C++0x braced-init-list. 439 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); 440 441 ExprResult Init(ParseBraceInitializer()); 442 443 if (Init.isInvalid()) { 444 Actions.ActOnInitializerError(OmpPrivParm); 445 } else { 446 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(), 447 /*DirectInit=*/true); 448 } 449 } else { 450 Actions.ActOnUninitializedDecl(OmpPrivParm); 451 } 452 } 453 454 namespace { 455 /// RAII that recreates function context for correct parsing of clauses of 456 /// 'declare simd' construct. 457 /// OpenMP, 2.8.2 declare simd Construct 458 /// The expressions appearing in the clauses of this directive are evaluated in 459 /// the scope of the arguments of the function declaration or definition. 460 class FNContextRAII final { 461 Parser &P; 462 Sema::CXXThisScopeRAII *ThisScope; 463 Parser::ParseScope *TempScope; 464 Parser::ParseScope *FnScope; 465 bool HasTemplateScope = false; 466 bool HasFunScope = false; 467 FNContextRAII() = delete; 468 FNContextRAII(const FNContextRAII &) = delete; 469 FNContextRAII &operator=(const FNContextRAII &) = delete; 470 471 public: 472 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) { 473 Decl *D = *Ptr.get().begin(); 474 NamedDecl *ND = dyn_cast<NamedDecl>(D); 475 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); 476 Sema &Actions = P.getActions(); 477 478 // Allow 'this' within late-parsed attributes. 479 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0, 480 ND && ND->isCXXInstanceMember()); 481 482 // If the Decl is templatized, add template parameters to scope. 483 HasTemplateScope = D->isTemplateDecl(); 484 TempScope = 485 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope); 486 if (HasTemplateScope) 487 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D); 488 489 // If the Decl is on a function, add function parameters to the scope. 490 HasFunScope = D->isFunctionOrFunctionTemplate(); 491 FnScope = new Parser::ParseScope( 492 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope, 493 HasFunScope); 494 if (HasFunScope) 495 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D); 496 } 497 ~FNContextRAII() { 498 if (HasFunScope) { 499 P.getActions().ActOnExitFunctionContext(); 500 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver 501 } 502 if (HasTemplateScope) 503 TempScope->Exit(); 504 delete FnScope; 505 delete TempScope; 506 delete ThisScope; 507 } 508 }; 509 } // namespace 510 511 /// Parses clauses for 'declare simd' directive. 512 /// clause: 513 /// 'inbranch' | 'notinbranch' 514 /// 'simdlen' '(' <expr> ')' 515 /// { 'uniform' '(' <argument_list> ')' } 516 /// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' } 517 /// { 'linear '(' <argument_list> [ ':' <step> ] ')' } 518 static bool parseDeclareSimdClauses( 519 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen, 520 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds, 521 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears, 522 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) { 523 SourceRange BSRange; 524 const Token &Tok = P.getCurToken(); 525 bool IsError = false; 526 while (Tok.isNot(tok::annot_pragma_openmp_end)) { 527 if (Tok.isNot(tok::identifier)) 528 break; 529 OMPDeclareSimdDeclAttr::BranchStateTy Out; 530 IdentifierInfo *II = Tok.getIdentifierInfo(); 531 StringRef ClauseName = II->getName(); 532 // Parse 'inranch|notinbranch' clauses. 533 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) { 534 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) { 535 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch) 536 << ClauseName 537 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange; 538 IsError = true; 539 } 540 BS = Out; 541 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc()); 542 P.ConsumeToken(); 543 } else if (ClauseName.equals("simdlen")) { 544 if (SimdLen.isUsable()) { 545 P.Diag(Tok, diag::err_omp_more_one_clause) 546 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0; 547 IsError = true; 548 } 549 P.ConsumeToken(); 550 SourceLocation RLoc; 551 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc); 552 if (SimdLen.isInvalid()) 553 IsError = true; 554 } else { 555 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName); 556 if (CKind == OMPC_uniform || CKind == OMPC_aligned || 557 CKind == OMPC_linear) { 558 Parser::OpenMPVarListDataTy Data; 559 SmallVectorImpl<Expr *> *Vars = &Uniforms; 560 if (CKind == OMPC_aligned) 561 Vars = &Aligneds; 562 else if (CKind == OMPC_linear) 563 Vars = &Linears; 564 565 P.ConsumeToken(); 566 if (P.ParseOpenMPVarList(OMPD_declare_simd, 567 getOpenMPClauseKind(ClauseName), *Vars, Data)) 568 IsError = true; 569 if (CKind == OMPC_aligned) { 570 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr); 571 } else if (CKind == OMPC_linear) { 572 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind, 573 Data.DepLinMapLoc)) 574 Data.LinKind = OMPC_LINEAR_val; 575 LinModifiers.append(Linears.size() - LinModifiers.size(), 576 Data.LinKind); 577 Steps.append(Linears.size() - Steps.size(), Data.TailExpr); 578 } 579 } else 580 // TODO: add parsing of other clauses. 581 break; 582 } 583 // Skip ',' if any. 584 if (Tok.is(tok::comma)) 585 P.ConsumeToken(); 586 } 587 return IsError; 588 } 589 590 /// Parse clauses for '#pragma omp declare simd'. 591 Parser::DeclGroupPtrTy 592 Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr, 593 CachedTokens &Toks, SourceLocation Loc) { 594 PP.EnterToken(Tok); 595 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true); 596 // Consume the previously pushed token. 597 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); 598 599 FNContextRAII FnContext(*this, Ptr); 600 OMPDeclareSimdDeclAttr::BranchStateTy BS = 601 OMPDeclareSimdDeclAttr::BS_Undefined; 602 ExprResult Simdlen; 603 SmallVector<Expr *, 4> Uniforms; 604 SmallVector<Expr *, 4> Aligneds; 605 SmallVector<Expr *, 4> Alignments; 606 SmallVector<Expr *, 4> Linears; 607 SmallVector<unsigned, 4> LinModifiers; 608 SmallVector<Expr *, 4> Steps; 609 bool IsError = 610 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds, 611 Alignments, Linears, LinModifiers, Steps); 612 // Need to check for extra tokens. 613 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 614 Diag(Tok, diag::warn_omp_extra_tokens_at_eol) 615 << getOpenMPDirectiveName(OMPD_declare_simd); 616 while (Tok.isNot(tok::annot_pragma_openmp_end)) 617 ConsumeAnyToken(); 618 } 619 // Skip the last annot_pragma_openmp_end. 620 SourceLocation EndLoc = ConsumeAnnotationToken(); 621 if (IsError) 622 return Ptr; 623 return Actions.ActOnOpenMPDeclareSimdDirective( 624 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears, 625 LinModifiers, Steps, SourceRange(Loc, EndLoc)); 626 } 627 628 /// Parsing of declarative OpenMP directives. 629 /// 630 /// threadprivate-directive: 631 /// annot_pragma_openmp 'threadprivate' simple-variable-list 632 /// annot_pragma_openmp_end 633 /// 634 /// declare-reduction-directive: 635 /// annot_pragma_openmp 'declare' 'reduction' [...] 636 /// annot_pragma_openmp_end 637 /// 638 /// declare-simd-directive: 639 /// annot_pragma_openmp 'declare simd' {<clause> [,]} 640 /// annot_pragma_openmp_end 641 /// <function declaration/definition> 642 /// 643 Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl( 644 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs, 645 DeclSpec::TST TagType, Decl *Tag) { 646 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!"); 647 ParenBraceBracketBalancer BalancerRAIIObj(*this); 648 649 SourceLocation Loc = ConsumeAnnotationToken(); 650 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this); 651 652 switch (DKind) { 653 case OMPD_threadprivate: { 654 ConsumeToken(); 655 ThreadprivateListParserHelper Helper(this); 656 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) { 657 // The last seen token is annot_pragma_openmp_end - need to check for 658 // extra tokens. 659 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 660 Diag(Tok, diag::warn_omp_extra_tokens_at_eol) 661 << getOpenMPDirectiveName(OMPD_threadprivate); 662 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); 663 } 664 // Skip the last annot_pragma_openmp_end. 665 ConsumeAnnotationToken(); 666 return Actions.ActOnOpenMPThreadprivateDirective(Loc, 667 Helper.getIdentifiers()); 668 } 669 break; 670 } 671 case OMPD_declare_reduction: 672 ConsumeToken(); 673 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) { 674 // The last seen token is annot_pragma_openmp_end - need to check for 675 // extra tokens. 676 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 677 Diag(Tok, diag::warn_omp_extra_tokens_at_eol) 678 << getOpenMPDirectiveName(OMPD_declare_reduction); 679 while (Tok.isNot(tok::annot_pragma_openmp_end)) 680 ConsumeAnyToken(); 681 } 682 // Skip the last annot_pragma_openmp_end. 683 ConsumeAnnotationToken(); 684 return Res; 685 } 686 break; 687 case OMPD_declare_simd: { 688 // The syntax is: 689 // { #pragma omp declare simd } 690 // <function-declaration-or-definition> 691 // 692 ConsumeToken(); 693 CachedTokens Toks; 694 while(Tok.isNot(tok::annot_pragma_openmp_end)) { 695 Toks.push_back(Tok); 696 ConsumeAnyToken(); 697 } 698 Toks.push_back(Tok); 699 ConsumeAnyToken(); 700 701 DeclGroupPtrTy Ptr; 702 if (Tok.is(tok::annot_pragma_openmp)) { 703 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag); 704 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) { 705 // Here we expect to see some function declaration. 706 if (AS == AS_none) { 707 assert(TagType == DeclSpec::TST_unspecified); 708 MaybeParseCXX11Attributes(Attrs); 709 ParsingDeclSpec PDS(*this); 710 Ptr = ParseExternalDeclaration(Attrs, &PDS); 711 } else { 712 Ptr = 713 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag); 714 } 715 } 716 if (!Ptr) { 717 Diag(Loc, diag::err_omp_decl_in_declare_simd); 718 return DeclGroupPtrTy(); 719 } 720 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc); 721 } 722 case OMPD_declare_target: { 723 SourceLocation DTLoc = ConsumeAnyToken(); 724 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 725 // OpenMP 4.5 syntax with list of entities. 726 Sema::NamedDeclSetType SameDirectiveDecls; 727 while (Tok.isNot(tok::annot_pragma_openmp_end)) { 728 OMPDeclareTargetDeclAttr::MapTypeTy MT = 729 OMPDeclareTargetDeclAttr::MT_To; 730 if (Tok.is(tok::identifier)) { 731 IdentifierInfo *II = Tok.getIdentifierInfo(); 732 StringRef ClauseName = II->getName(); 733 // Parse 'to|link' clauses. 734 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName, 735 MT)) { 736 Diag(Tok, diag::err_omp_declare_target_unexpected_clause) 737 << ClauseName; 738 break; 739 } 740 ConsumeToken(); 741 } 742 auto &&Callback = [this, MT, &SameDirectiveDecls]( 743 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) { 744 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT, 745 SameDirectiveDecls); 746 }; 747 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, 748 /*AllowScopeSpecifier=*/true)) 749 break; 750 751 // Consume optional ','. 752 if (Tok.is(tok::comma)) 753 ConsumeToken(); 754 } 755 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); 756 ConsumeAnyToken(); 757 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(), 758 SameDirectiveDecls.end()); 759 if (Decls.empty()) 760 return DeclGroupPtrTy(); 761 return Actions.BuildDeclaratorGroup(Decls); 762 } 763 764 // Skip the last annot_pragma_openmp_end. 765 ConsumeAnyToken(); 766 767 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc)) 768 return DeclGroupPtrTy(); 769 770 llvm::SmallVector<Decl *, 4> Decls; 771 DKind = parseOpenMPDirectiveKind(*this); 772 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target && 773 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) { 774 DeclGroupPtrTy Ptr; 775 // Here we expect to see some function declaration. 776 if (AS == AS_none) { 777 assert(TagType == DeclSpec::TST_unspecified); 778 MaybeParseCXX11Attributes(Attrs); 779 ParsingDeclSpec PDS(*this); 780 Ptr = ParseExternalDeclaration(Attrs, &PDS); 781 } else { 782 Ptr = 783 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag); 784 } 785 if (Ptr) { 786 DeclGroupRef Ref = Ptr.get(); 787 Decls.append(Ref.begin(), Ref.end()); 788 } 789 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) { 790 TentativeParsingAction TPA(*this); 791 ConsumeAnnotationToken(); 792 DKind = parseOpenMPDirectiveKind(*this); 793 if (DKind != OMPD_end_declare_target) 794 TPA.Revert(); 795 else 796 TPA.Commit(); 797 } 798 } 799 800 if (DKind == OMPD_end_declare_target) { 801 ConsumeAnyToken(); 802 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 803 Diag(Tok, diag::warn_omp_extra_tokens_at_eol) 804 << getOpenMPDirectiveName(OMPD_end_declare_target); 805 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); 806 } 807 // Skip the last annot_pragma_openmp_end. 808 ConsumeAnyToken(); 809 } else { 810 Diag(Tok, diag::err_expected_end_declare_target); 811 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'"; 812 } 813 Actions.ActOnFinishOpenMPDeclareTargetDirective(); 814 return Actions.BuildDeclaratorGroup(Decls); 815 } 816 case OMPD_unknown: 817 Diag(Tok, diag::err_omp_unknown_directive); 818 break; 819 case OMPD_parallel: 820 case OMPD_simd: 821 case OMPD_task: 822 case OMPD_taskyield: 823 case OMPD_barrier: 824 case OMPD_taskwait: 825 case OMPD_taskgroup: 826 case OMPD_flush: 827 case OMPD_for: 828 case OMPD_for_simd: 829 case OMPD_sections: 830 case OMPD_section: 831 case OMPD_single: 832 case OMPD_master: 833 case OMPD_ordered: 834 case OMPD_critical: 835 case OMPD_parallel_for: 836 case OMPD_parallel_for_simd: 837 case OMPD_parallel_sections: 838 case OMPD_atomic: 839 case OMPD_target: 840 case OMPD_teams: 841 case OMPD_cancellation_point: 842 case OMPD_cancel: 843 case OMPD_target_data: 844 case OMPD_target_enter_data: 845 case OMPD_target_exit_data: 846 case OMPD_target_parallel: 847 case OMPD_target_parallel_for: 848 case OMPD_taskloop: 849 case OMPD_taskloop_simd: 850 case OMPD_distribute: 851 case OMPD_end_declare_target: 852 case OMPD_target_update: 853 case OMPD_distribute_parallel_for: 854 case OMPD_distribute_parallel_for_simd: 855 case OMPD_distribute_simd: 856 case OMPD_target_parallel_for_simd: 857 case OMPD_target_simd: 858 case OMPD_teams_distribute: 859 case OMPD_teams_distribute_simd: 860 case OMPD_teams_distribute_parallel_for_simd: 861 case OMPD_teams_distribute_parallel_for: 862 case OMPD_target_teams: 863 case OMPD_target_teams_distribute: 864 case OMPD_target_teams_distribute_parallel_for: 865 case OMPD_target_teams_distribute_parallel_for_simd: 866 case OMPD_target_teams_distribute_simd: 867 Diag(Tok, diag::err_omp_unexpected_directive) 868 << 1 << getOpenMPDirectiveName(DKind); 869 break; 870 } 871 while (Tok.isNot(tok::annot_pragma_openmp_end)) 872 ConsumeAnyToken(); 873 ConsumeAnyToken(); 874 return nullptr; 875 } 876 877 /// Parsing of declarative or executable OpenMP directives. 878 /// 879 /// threadprivate-directive: 880 /// annot_pragma_openmp 'threadprivate' simple-variable-list 881 /// annot_pragma_openmp_end 882 /// 883 /// declare-reduction-directive: 884 /// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':' 885 /// <type> {',' <type>} ':' <expression> ')' ['initializer' '(' 886 /// ('omp_priv' '=' <expression>|<function_call>) ')'] 887 /// annot_pragma_openmp_end 888 /// 889 /// executable-directive: 890 /// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' | 891 /// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] | 892 /// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' | 893 /// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' | 894 /// 'for simd' | 'parallel for simd' | 'target' | 'target data' | 895 /// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' | 896 /// 'distribute' | 'target enter data' | 'target exit data' | 897 /// 'target parallel' | 'target parallel for' | 898 /// 'target update' | 'distribute parallel for' | 899 /// 'distribute paralle for simd' | 'distribute simd' | 900 /// 'target parallel for simd' | 'target simd' | 901 /// 'teams distribute' | 'teams distribute simd' | 902 /// 'teams distribute parallel for simd' | 903 /// 'teams distribute parallel for' | 'target teams' | 904 /// 'target teams distribute' | 905 /// 'target teams distribute parallel for' | 906 /// 'target teams distribute parallel for simd' | 907 /// 'target teams distribute simd' {clause} 908 /// annot_pragma_openmp_end 909 /// 910 StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective( 911 AllowedConstructsKind Allowed) { 912 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!"); 913 ParenBraceBracketBalancer BalancerRAIIObj(*this); 914 SmallVector<OMPClause *, 5> Clauses; 915 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1> 916 FirstClauses(OMPC_unknown + 1); 917 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope | 918 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope; 919 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc; 920 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this); 921 OpenMPDirectiveKind CancelRegion = OMPD_unknown; 922 // Name of critical directive. 923 DeclarationNameInfo DirName; 924 StmtResult Directive = StmtError(); 925 bool HasAssociatedStatement = true; 926 bool FlushHasClause = false; 927 928 switch (DKind) { 929 case OMPD_threadprivate: { 930 if (Allowed != ACK_Any) { 931 Diag(Tok, diag::err_omp_immediate_directive) 932 << getOpenMPDirectiveName(DKind) << 0; 933 } 934 ConsumeToken(); 935 ThreadprivateListParserHelper Helper(this); 936 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) { 937 // The last seen token is annot_pragma_openmp_end - need to check for 938 // extra tokens. 939 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 940 Diag(Tok, diag::warn_omp_extra_tokens_at_eol) 941 << getOpenMPDirectiveName(OMPD_threadprivate); 942 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); 943 } 944 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective( 945 Loc, Helper.getIdentifiers()); 946 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation()); 947 } 948 SkipUntil(tok::annot_pragma_openmp_end); 949 break; 950 } 951 case OMPD_declare_reduction: 952 ConsumeToken(); 953 if (DeclGroupPtrTy Res = 954 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) { 955 // The last seen token is annot_pragma_openmp_end - need to check for 956 // extra tokens. 957 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 958 Diag(Tok, diag::warn_omp_extra_tokens_at_eol) 959 << getOpenMPDirectiveName(OMPD_declare_reduction); 960 while (Tok.isNot(tok::annot_pragma_openmp_end)) 961 ConsumeAnyToken(); 962 } 963 ConsumeAnyToken(); 964 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation()); 965 } else { 966 SkipUntil(tok::annot_pragma_openmp_end); 967 } 968 break; 969 case OMPD_flush: 970 if (PP.LookAhead(0).is(tok::l_paren)) { 971 FlushHasClause = true; 972 // Push copy of the current token back to stream to properly parse 973 // pseudo-clause OMPFlushClause. 974 PP.EnterToken(Tok); 975 } 976 LLVM_FALLTHROUGH; 977 case OMPD_taskyield: 978 case OMPD_barrier: 979 case OMPD_taskwait: 980 case OMPD_cancellation_point: 981 case OMPD_cancel: 982 case OMPD_target_enter_data: 983 case OMPD_target_exit_data: 984 case OMPD_target_update: 985 if (Allowed == ACK_StatementsOpenMPNonStandalone) { 986 Diag(Tok, diag::err_omp_immediate_directive) 987 << getOpenMPDirectiveName(DKind) << 0; 988 } 989 HasAssociatedStatement = false; 990 // Fall through for further analysis. 991 LLVM_FALLTHROUGH; 992 case OMPD_parallel: 993 case OMPD_simd: 994 case OMPD_for: 995 case OMPD_for_simd: 996 case OMPD_sections: 997 case OMPD_single: 998 case OMPD_section: 999 case OMPD_master: 1000 case OMPD_critical: 1001 case OMPD_parallel_for: 1002 case OMPD_parallel_for_simd: 1003 case OMPD_parallel_sections: 1004 case OMPD_task: 1005 case OMPD_ordered: 1006 case OMPD_atomic: 1007 case OMPD_target: 1008 case OMPD_teams: 1009 case OMPD_taskgroup: 1010 case OMPD_target_data: 1011 case OMPD_target_parallel: 1012 case OMPD_target_parallel_for: 1013 case OMPD_taskloop: 1014 case OMPD_taskloop_simd: 1015 case OMPD_distribute: 1016 case OMPD_distribute_parallel_for: 1017 case OMPD_distribute_parallel_for_simd: 1018 case OMPD_distribute_simd: 1019 case OMPD_target_parallel_for_simd: 1020 case OMPD_target_simd: 1021 case OMPD_teams_distribute: 1022 case OMPD_teams_distribute_simd: 1023 case OMPD_teams_distribute_parallel_for_simd: 1024 case OMPD_teams_distribute_parallel_for: 1025 case OMPD_target_teams: 1026 case OMPD_target_teams_distribute: 1027 case OMPD_target_teams_distribute_parallel_for: 1028 case OMPD_target_teams_distribute_parallel_for_simd: 1029 case OMPD_target_teams_distribute_simd: { 1030 ConsumeToken(); 1031 // Parse directive name of the 'critical' directive if any. 1032 if (DKind == OMPD_critical) { 1033 BalancedDelimiterTracker T(*this, tok::l_paren, 1034 tok::annot_pragma_openmp_end); 1035 if (!T.consumeOpen()) { 1036 if (Tok.isAnyIdentifier()) { 1037 DirName = 1038 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation()); 1039 ConsumeAnyToken(); 1040 } else { 1041 Diag(Tok, diag::err_omp_expected_identifier_for_critical); 1042 } 1043 T.consumeClose(); 1044 } 1045 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) { 1046 CancelRegion = parseOpenMPDirectiveKind(*this); 1047 if (Tok.isNot(tok::annot_pragma_openmp_end)) 1048 ConsumeToken(); 1049 } 1050 1051 if (isOpenMPLoopDirective(DKind)) 1052 ScopeFlags |= Scope::OpenMPLoopDirectiveScope; 1053 if (isOpenMPSimdDirective(DKind)) 1054 ScopeFlags |= Scope::OpenMPSimdDirectiveScope; 1055 ParseScope OMPDirectiveScope(this, ScopeFlags); 1056 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc); 1057 1058 while (Tok.isNot(tok::annot_pragma_openmp_end)) { 1059 OpenMPClauseKind CKind = 1060 Tok.isAnnotation() 1061 ? OMPC_unknown 1062 : FlushHasClause ? OMPC_flush 1063 : getOpenMPClauseKind(PP.getSpelling(Tok)); 1064 Actions.StartOpenMPClause(CKind); 1065 FlushHasClause = false; 1066 OMPClause *Clause = 1067 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt()); 1068 FirstClauses[CKind].setInt(true); 1069 if (Clause) { 1070 FirstClauses[CKind].setPointer(Clause); 1071 Clauses.push_back(Clause); 1072 } 1073 1074 // Skip ',' if any. 1075 if (Tok.is(tok::comma)) 1076 ConsumeToken(); 1077 Actions.EndOpenMPClause(); 1078 } 1079 // End location of the directive. 1080 EndLoc = Tok.getLocation(); 1081 // Consume final annot_pragma_openmp_end. 1082 ConsumeAnnotationToken(); 1083 1084 // OpenMP [2.13.8, ordered Construct, Syntax] 1085 // If the depend clause is specified, the ordered construct is a stand-alone 1086 // directive. 1087 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) { 1088 if (Allowed == ACK_StatementsOpenMPNonStandalone) { 1089 Diag(Loc, diag::err_omp_immediate_directive) 1090 << getOpenMPDirectiveName(DKind) << 1 1091 << getOpenMPClauseName(OMPC_depend); 1092 } 1093 HasAssociatedStatement = false; 1094 } 1095 1096 StmtResult AssociatedStmt; 1097 if (HasAssociatedStatement) { 1098 // The body is a block scope like in Lambdas and Blocks. 1099 Actions.ActOnOpenMPRegionStart(DKind, getCurScope()); 1100 // FIXME: We create a bogus CompoundStmt scope to hold the contents of 1101 // the captured region. Code elsewhere assumes that any FunctionScopeInfo 1102 // should have at least one compound statement scope within it. 1103 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement()); 1104 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses); 1105 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data || 1106 DKind == OMPD_target_exit_data) { 1107 Actions.ActOnOpenMPRegionStart(DKind, getCurScope()); 1108 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), 1109 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None, 1110 /*isStmtExpr=*/false)); 1111 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses); 1112 } 1113 Directive = Actions.ActOnOpenMPExecutableDirective( 1114 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc, 1115 EndLoc); 1116 1117 // Exit scope. 1118 Actions.EndOpenMPDSABlock(Directive.get()); 1119 OMPDirectiveScope.Exit(); 1120 break; 1121 } 1122 case OMPD_declare_simd: 1123 case OMPD_declare_target: 1124 case OMPD_end_declare_target: 1125 Diag(Tok, diag::err_omp_unexpected_directive) 1126 << 1 << getOpenMPDirectiveName(DKind); 1127 SkipUntil(tok::annot_pragma_openmp_end); 1128 break; 1129 case OMPD_unknown: 1130 Diag(Tok, diag::err_omp_unknown_directive); 1131 SkipUntil(tok::annot_pragma_openmp_end); 1132 break; 1133 } 1134 return Directive; 1135 } 1136 1137 // Parses simple list: 1138 // simple-variable-list: 1139 // '(' id-expression {, id-expression} ')' 1140 // 1141 bool Parser::ParseOpenMPSimpleVarList( 1142 OpenMPDirectiveKind Kind, 1143 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> & 1144 Callback, 1145 bool AllowScopeSpecifier) { 1146 // Parse '('. 1147 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); 1148 if (T.expectAndConsume(diag::err_expected_lparen_after, 1149 getOpenMPDirectiveName(Kind))) 1150 return true; 1151 bool IsCorrect = true; 1152 bool NoIdentIsFound = true; 1153 1154 // Read tokens while ')' or annot_pragma_openmp_end is not found. 1155 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) { 1156 CXXScopeSpec SS; 1157 UnqualifiedId Name; 1158 // Read var name. 1159 Token PrevTok = Tok; 1160 NoIdentIsFound = false; 1161 1162 if (AllowScopeSpecifier && getLangOpts().CPlusPlus && 1163 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) { 1164 IsCorrect = false; 1165 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, 1166 StopBeforeMatch); 1167 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr, 1168 nullptr, Name)) { 1169 IsCorrect = false; 1170 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, 1171 StopBeforeMatch); 1172 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) && 1173 Tok.isNot(tok::annot_pragma_openmp_end)) { 1174 IsCorrect = false; 1175 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, 1176 StopBeforeMatch); 1177 Diag(PrevTok.getLocation(), diag::err_expected) 1178 << tok::identifier 1179 << SourceRange(PrevTok.getLocation(), PrevTokLocation); 1180 } else { 1181 Callback(SS, Actions.GetNameFromUnqualifiedId(Name)); 1182 } 1183 // Consume ','. 1184 if (Tok.is(tok::comma)) { 1185 ConsumeToken(); 1186 } 1187 } 1188 1189 if (NoIdentIsFound) { 1190 Diag(Tok, diag::err_expected) << tok::identifier; 1191 IsCorrect = false; 1192 } 1193 1194 // Parse ')'. 1195 IsCorrect = !T.consumeClose() && IsCorrect; 1196 1197 return !IsCorrect; 1198 } 1199 1200 /// Parsing of OpenMP clauses. 1201 /// 1202 /// clause: 1203 /// if-clause | final-clause | num_threads-clause | safelen-clause | 1204 /// default-clause | private-clause | firstprivate-clause | shared-clause 1205 /// | linear-clause | aligned-clause | collapse-clause | 1206 /// lastprivate-clause | reduction-clause | proc_bind-clause | 1207 /// schedule-clause | copyin-clause | copyprivate-clause | untied-clause | 1208 /// mergeable-clause | flush-clause | read-clause | write-clause | 1209 /// update-clause | capture-clause | seq_cst-clause | device-clause | 1210 /// simdlen-clause | threads-clause | simd-clause | num_teams-clause | 1211 /// thread_limit-clause | priority-clause | grainsize-clause | 1212 /// nogroup-clause | num_tasks-clause | hint-clause | to-clause | 1213 /// from-clause | is_device_ptr-clause | task_reduction-clause | 1214 /// in_reduction-clause 1215 /// 1216 OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind, 1217 OpenMPClauseKind CKind, bool FirstClause) { 1218 OMPClause *Clause = nullptr; 1219 bool ErrorFound = false; 1220 bool WrongDirective = false; 1221 // Check if clause is allowed for the given directive. 1222 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) { 1223 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind) 1224 << getOpenMPDirectiveName(DKind); 1225 ErrorFound = true; 1226 WrongDirective = true; 1227 } 1228 1229 switch (CKind) { 1230 case OMPC_final: 1231 case OMPC_num_threads: 1232 case OMPC_safelen: 1233 case OMPC_simdlen: 1234 case OMPC_collapse: 1235 case OMPC_ordered: 1236 case OMPC_device: 1237 case OMPC_num_teams: 1238 case OMPC_thread_limit: 1239 case OMPC_priority: 1240 case OMPC_grainsize: 1241 case OMPC_num_tasks: 1242 case OMPC_hint: 1243 // OpenMP [2.5, Restrictions] 1244 // At most one num_threads clause can appear on the directive. 1245 // OpenMP [2.8.1, simd construct, Restrictions] 1246 // Only one safelen clause can appear on a simd directive. 1247 // Only one simdlen clause can appear on a simd directive. 1248 // Only one collapse clause can appear on a simd directive. 1249 // OpenMP [2.9.1, target data construct, Restrictions] 1250 // At most one device clause can appear on the directive. 1251 // OpenMP [2.11.1, task Construct, Restrictions] 1252 // At most one if clause can appear on the directive. 1253 // At most one final clause can appear on the directive. 1254 // OpenMP [teams Construct, Restrictions] 1255 // At most one num_teams clause can appear on the directive. 1256 // At most one thread_limit clause can appear on the directive. 1257 // OpenMP [2.9.1, task Construct, Restrictions] 1258 // At most one priority clause can appear on the directive. 1259 // OpenMP [2.9.2, taskloop Construct, Restrictions] 1260 // At most one grainsize clause can appear on the directive. 1261 // OpenMP [2.9.2, taskloop Construct, Restrictions] 1262 // At most one num_tasks clause can appear on the directive. 1263 if (!FirstClause) { 1264 Diag(Tok, diag::err_omp_more_one_clause) 1265 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; 1266 ErrorFound = true; 1267 } 1268 1269 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren)) 1270 Clause = ParseOpenMPClause(CKind, WrongDirective); 1271 else 1272 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective); 1273 break; 1274 case OMPC_default: 1275 case OMPC_proc_bind: 1276 // OpenMP [2.14.3.1, Restrictions] 1277 // Only a single default clause may be specified on a parallel, task or 1278 // teams directive. 1279 // OpenMP [2.5, parallel Construct, Restrictions] 1280 // At most one proc_bind clause can appear on the directive. 1281 if (!FirstClause) { 1282 Diag(Tok, diag::err_omp_more_one_clause) 1283 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; 1284 ErrorFound = true; 1285 } 1286 1287 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective); 1288 break; 1289 case OMPC_schedule: 1290 case OMPC_dist_schedule: 1291 case OMPC_defaultmap: 1292 // OpenMP [2.7.1, Restrictions, p. 3] 1293 // Only one schedule clause can appear on a loop directive. 1294 // OpenMP [2.10.4, Restrictions, p. 106] 1295 // At most one defaultmap clause can appear on the directive. 1296 if (!FirstClause) { 1297 Diag(Tok, diag::err_omp_more_one_clause) 1298 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; 1299 ErrorFound = true; 1300 } 1301 LLVM_FALLTHROUGH; 1302 1303 case OMPC_if: 1304 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective); 1305 break; 1306 case OMPC_nowait: 1307 case OMPC_untied: 1308 case OMPC_mergeable: 1309 case OMPC_read: 1310 case OMPC_write: 1311 case OMPC_update: 1312 case OMPC_capture: 1313 case OMPC_seq_cst: 1314 case OMPC_threads: 1315 case OMPC_simd: 1316 case OMPC_nogroup: 1317 // OpenMP [2.7.1, Restrictions, p. 9] 1318 // Only one ordered clause can appear on a loop directive. 1319 // OpenMP [2.7.1, Restrictions, C/C++, p. 4] 1320 // Only one nowait clause can appear on a for directive. 1321 if (!FirstClause) { 1322 Diag(Tok, diag::err_omp_more_one_clause) 1323 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; 1324 ErrorFound = true; 1325 } 1326 1327 Clause = ParseOpenMPClause(CKind, WrongDirective); 1328 break; 1329 case OMPC_private: 1330 case OMPC_firstprivate: 1331 case OMPC_lastprivate: 1332 case OMPC_shared: 1333 case OMPC_reduction: 1334 case OMPC_task_reduction: 1335 case OMPC_in_reduction: 1336 case OMPC_linear: 1337 case OMPC_aligned: 1338 case OMPC_copyin: 1339 case OMPC_copyprivate: 1340 case OMPC_flush: 1341 case OMPC_depend: 1342 case OMPC_map: 1343 case OMPC_to: 1344 case OMPC_from: 1345 case OMPC_use_device_ptr: 1346 case OMPC_is_device_ptr: 1347 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective); 1348 break; 1349 case OMPC_unknown: 1350 Diag(Tok, diag::warn_omp_extra_tokens_at_eol) 1351 << getOpenMPDirectiveName(DKind); 1352 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); 1353 break; 1354 case OMPC_threadprivate: 1355 case OMPC_uniform: 1356 if (!WrongDirective) 1357 Diag(Tok, diag::err_omp_unexpected_clause) 1358 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind); 1359 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch); 1360 break; 1361 } 1362 return ErrorFound ? nullptr : Clause; 1363 } 1364 1365 /// Parses simple expression in parens for single-expression clauses of OpenMP 1366 /// constructs. 1367 /// \param RLoc Returned location of right paren. 1368 ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName, 1369 SourceLocation &RLoc) { 1370 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); 1371 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data())) 1372 return ExprError(); 1373 1374 SourceLocation ELoc = Tok.getLocation(); 1375 ExprResult LHS(ParseCastExpression( 1376 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast)); 1377 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional)); 1378 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc); 1379 1380 // Parse ')'. 1381 T.consumeClose(); 1382 1383 RLoc = T.getCloseLocation(); 1384 return Val; 1385 } 1386 1387 /// Parsing of OpenMP clauses with single expressions like 'final', 1388 /// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams', 1389 /// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'. 1390 /// 1391 /// final-clause: 1392 /// 'final' '(' expression ')' 1393 /// 1394 /// num_threads-clause: 1395 /// 'num_threads' '(' expression ')' 1396 /// 1397 /// safelen-clause: 1398 /// 'safelen' '(' expression ')' 1399 /// 1400 /// simdlen-clause: 1401 /// 'simdlen' '(' expression ')' 1402 /// 1403 /// collapse-clause: 1404 /// 'collapse' '(' expression ')' 1405 /// 1406 /// priority-clause: 1407 /// 'priority' '(' expression ')' 1408 /// 1409 /// grainsize-clause: 1410 /// 'grainsize' '(' expression ')' 1411 /// 1412 /// num_tasks-clause: 1413 /// 'num_tasks' '(' expression ')' 1414 /// 1415 /// hint-clause: 1416 /// 'hint' '(' expression ')' 1417 /// 1418 OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind, 1419 bool ParseOnly) { 1420 SourceLocation Loc = ConsumeToken(); 1421 SourceLocation LLoc = Tok.getLocation(); 1422 SourceLocation RLoc; 1423 1424 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc); 1425 1426 if (Val.isInvalid()) 1427 return nullptr; 1428 1429 if (ParseOnly) 1430 return nullptr; 1431 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc); 1432 } 1433 1434 /// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'. 1435 /// 1436 /// default-clause: 1437 /// 'default' '(' 'none' | 'shared' ') 1438 /// 1439 /// proc_bind-clause: 1440 /// 'proc_bind' '(' 'master' | 'close' | 'spread' ') 1441 /// 1442 OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind, 1443 bool ParseOnly) { 1444 SourceLocation Loc = Tok.getLocation(); 1445 SourceLocation LOpen = ConsumeToken(); 1446 // Parse '('. 1447 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); 1448 if (T.expectAndConsume(diag::err_expected_lparen_after, 1449 getOpenMPClauseName(Kind))) 1450 return nullptr; 1451 1452 unsigned Type = getOpenMPSimpleClauseType( 1453 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)); 1454 SourceLocation TypeLoc = Tok.getLocation(); 1455 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 1456 Tok.isNot(tok::annot_pragma_openmp_end)) 1457 ConsumeAnyToken(); 1458 1459 // Parse ')'. 1460 T.consumeClose(); 1461 1462 if (ParseOnly) 1463 return nullptr; 1464 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc, 1465 Tok.getLocation()); 1466 } 1467 1468 /// Parsing of OpenMP clauses like 'ordered'. 1469 /// 1470 /// ordered-clause: 1471 /// 'ordered' 1472 /// 1473 /// nowait-clause: 1474 /// 'nowait' 1475 /// 1476 /// untied-clause: 1477 /// 'untied' 1478 /// 1479 /// mergeable-clause: 1480 /// 'mergeable' 1481 /// 1482 /// read-clause: 1483 /// 'read' 1484 /// 1485 /// threads-clause: 1486 /// 'threads' 1487 /// 1488 /// simd-clause: 1489 /// 'simd' 1490 /// 1491 /// nogroup-clause: 1492 /// 'nogroup' 1493 /// 1494 OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) { 1495 SourceLocation Loc = Tok.getLocation(); 1496 ConsumeAnyToken(); 1497 1498 if (ParseOnly) 1499 return nullptr; 1500 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation()); 1501 } 1502 1503 1504 /// Parsing of OpenMP clauses with single expressions and some additional 1505 /// argument like 'schedule' or 'dist_schedule'. 1506 /// 1507 /// schedule-clause: 1508 /// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ] 1509 /// ')' 1510 /// 1511 /// if-clause: 1512 /// 'if' '(' [ directive-name-modifier ':' ] expression ')' 1513 /// 1514 /// defaultmap: 1515 /// 'defaultmap' '(' modifier ':' kind ')' 1516 /// 1517 OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind, 1518 bool ParseOnly) { 1519 SourceLocation Loc = ConsumeToken(); 1520 SourceLocation DelimLoc; 1521 // Parse '('. 1522 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); 1523 if (T.expectAndConsume(diag::err_expected_lparen_after, 1524 getOpenMPClauseName(Kind))) 1525 return nullptr; 1526 1527 ExprResult Val; 1528 SmallVector<unsigned, 4> Arg; 1529 SmallVector<SourceLocation, 4> KLoc; 1530 if (Kind == OMPC_schedule) { 1531 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 1532 Arg.resize(NumberOfElements); 1533 KLoc.resize(NumberOfElements); 1534 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown; 1535 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown; 1536 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown; 1537 unsigned KindModifier = getOpenMPSimpleClauseType( 1538 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)); 1539 if (KindModifier > OMPC_SCHEDULE_unknown) { 1540 // Parse 'modifier' 1541 Arg[Modifier1] = KindModifier; 1542 KLoc[Modifier1] = Tok.getLocation(); 1543 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 1544 Tok.isNot(tok::annot_pragma_openmp_end)) 1545 ConsumeAnyToken(); 1546 if (Tok.is(tok::comma)) { 1547 // Parse ',' 'modifier' 1548 ConsumeAnyToken(); 1549 KindModifier = getOpenMPSimpleClauseType( 1550 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)); 1551 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown 1552 ? KindModifier 1553 : (unsigned)OMPC_SCHEDULE_unknown; 1554 KLoc[Modifier2] = Tok.getLocation(); 1555 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 1556 Tok.isNot(tok::annot_pragma_openmp_end)) 1557 ConsumeAnyToken(); 1558 } 1559 // Parse ':' 1560 if (Tok.is(tok::colon)) 1561 ConsumeAnyToken(); 1562 else 1563 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier"; 1564 KindModifier = getOpenMPSimpleClauseType( 1565 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)); 1566 } 1567 Arg[ScheduleKind] = KindModifier; 1568 KLoc[ScheduleKind] = Tok.getLocation(); 1569 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 1570 Tok.isNot(tok::annot_pragma_openmp_end)) 1571 ConsumeAnyToken(); 1572 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static || 1573 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic || 1574 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) && 1575 Tok.is(tok::comma)) 1576 DelimLoc = ConsumeAnyToken(); 1577 } else if (Kind == OMPC_dist_schedule) { 1578 Arg.push_back(getOpenMPSimpleClauseType( 1579 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok))); 1580 KLoc.push_back(Tok.getLocation()); 1581 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 1582 Tok.isNot(tok::annot_pragma_openmp_end)) 1583 ConsumeAnyToken(); 1584 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma)) 1585 DelimLoc = ConsumeAnyToken(); 1586 } else if (Kind == OMPC_defaultmap) { 1587 // Get a defaultmap modifier 1588 Arg.push_back(getOpenMPSimpleClauseType( 1589 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok))); 1590 KLoc.push_back(Tok.getLocation()); 1591 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 1592 Tok.isNot(tok::annot_pragma_openmp_end)) 1593 ConsumeAnyToken(); 1594 // Parse ':' 1595 if (Tok.is(tok::colon)) 1596 ConsumeAnyToken(); 1597 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown) 1598 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier"; 1599 // Get a defaultmap kind 1600 Arg.push_back(getOpenMPSimpleClauseType( 1601 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok))); 1602 KLoc.push_back(Tok.getLocation()); 1603 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 1604 Tok.isNot(tok::annot_pragma_openmp_end)) 1605 ConsumeAnyToken(); 1606 } else { 1607 assert(Kind == OMPC_if); 1608 KLoc.push_back(Tok.getLocation()); 1609 TentativeParsingAction TPA(*this); 1610 Arg.push_back(parseOpenMPDirectiveKind(*this)); 1611 if (Arg.back() != OMPD_unknown) { 1612 ConsumeToken(); 1613 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) { 1614 TPA.Commit(); 1615 DelimLoc = ConsumeToken(); 1616 } else { 1617 TPA.Revert(); 1618 Arg.back() = OMPD_unknown; 1619 } 1620 } else { 1621 TPA.Revert(); 1622 } 1623 } 1624 1625 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) || 1626 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) || 1627 Kind == OMPC_if; 1628 if (NeedAnExpression) { 1629 SourceLocation ELoc = Tok.getLocation(); 1630 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast)); 1631 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional); 1632 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc); 1633 } 1634 1635 // Parse ')'. 1636 T.consumeClose(); 1637 1638 if (NeedAnExpression && Val.isInvalid()) 1639 return nullptr; 1640 1641 if (ParseOnly) 1642 return nullptr; 1643 return Actions.ActOnOpenMPSingleExprWithArgClause( 1644 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, 1645 T.getCloseLocation()); 1646 } 1647 1648 static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec, 1649 UnqualifiedId &ReductionId) { 1650 if (ReductionIdScopeSpec.isEmpty()) { 1651 auto OOK = OO_None; 1652 switch (P.getCurToken().getKind()) { 1653 case tok::plus: 1654 OOK = OO_Plus; 1655 break; 1656 case tok::minus: 1657 OOK = OO_Minus; 1658 break; 1659 case tok::star: 1660 OOK = OO_Star; 1661 break; 1662 case tok::amp: 1663 OOK = OO_Amp; 1664 break; 1665 case tok::pipe: 1666 OOK = OO_Pipe; 1667 break; 1668 case tok::caret: 1669 OOK = OO_Caret; 1670 break; 1671 case tok::ampamp: 1672 OOK = OO_AmpAmp; 1673 break; 1674 case tok::pipepipe: 1675 OOK = OO_PipePipe; 1676 break; 1677 default: 1678 break; 1679 } 1680 if (OOK != OO_None) { 1681 SourceLocation OpLoc = P.ConsumeToken(); 1682 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()}; 1683 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations); 1684 return false; 1685 } 1686 } 1687 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false, 1688 /*AllowDestructorName*/ false, 1689 /*AllowConstructorName*/ false, 1690 /*AllowDeductionGuide*/ false, 1691 nullptr, nullptr, ReductionId); 1692 } 1693 1694 /// Parses clauses with list. 1695 bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind, 1696 OpenMPClauseKind Kind, 1697 SmallVectorImpl<Expr *> &Vars, 1698 OpenMPVarListDataTy &Data) { 1699 UnqualifiedId UnqualifiedReductionId; 1700 bool InvalidReductionId = false; 1701 bool MapTypeModifierSpecified = false; 1702 1703 // Parse '('. 1704 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); 1705 if (T.expectAndConsume(diag::err_expected_lparen_after, 1706 getOpenMPClauseName(Kind))) 1707 return true; 1708 1709 bool NeedRParenForLinear = false; 1710 BalancedDelimiterTracker LinearT(*this, tok::l_paren, 1711 tok::annot_pragma_openmp_end); 1712 // Handle reduction-identifier for reduction clause. 1713 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction || 1714 Kind == OMPC_in_reduction) { 1715 ColonProtectionRAIIObject ColonRAII(*this); 1716 if (getLangOpts().CPlusPlus) 1717 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec, 1718 /*ObjectType=*/nullptr, 1719 /*EnteringContext=*/false); 1720 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec, 1721 UnqualifiedReductionId); 1722 if (InvalidReductionId) { 1723 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, 1724 StopBeforeMatch); 1725 } 1726 if (Tok.is(tok::colon)) 1727 Data.ColonLoc = ConsumeToken(); 1728 else 1729 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier"; 1730 if (!InvalidReductionId) 1731 Data.ReductionId = 1732 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId); 1733 } else if (Kind == OMPC_depend) { 1734 // Handle dependency type for depend clause. 1735 ColonProtectionRAIIObject ColonRAII(*this); 1736 Data.DepKind = 1737 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType( 1738 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : "")); 1739 Data.DepLinMapLoc = Tok.getLocation(); 1740 1741 if (Data.DepKind == OMPC_DEPEND_unknown) { 1742 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, 1743 StopBeforeMatch); 1744 } else { 1745 ConsumeToken(); 1746 // Special processing for depend(source) clause. 1747 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) { 1748 // Parse ')'. 1749 T.consumeClose(); 1750 return false; 1751 } 1752 } 1753 if (Tok.is(tok::colon)) { 1754 Data.ColonLoc = ConsumeToken(); 1755 } else { 1756 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren 1757 : diag::warn_pragma_expected_colon) 1758 << "dependency type"; 1759 } 1760 } else if (Kind == OMPC_linear) { 1761 // Try to parse modifier if any. 1762 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) { 1763 Data.LinKind = static_cast<OpenMPLinearClauseKind>( 1764 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok))); 1765 Data.DepLinMapLoc = ConsumeToken(); 1766 LinearT.consumeOpen(); 1767 NeedRParenForLinear = true; 1768 } 1769 } else if (Kind == OMPC_map) { 1770 // Handle map type for map clause. 1771 ColonProtectionRAIIObject ColonRAII(*this); 1772 1773 /// The map clause modifier token can be either a identifier or the C++ 1774 /// delete keyword. 1775 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool { 1776 return Tok.isOneOf(tok::identifier, tok::kw_delete); 1777 }; 1778 1779 // The first identifier may be a list item, a map-type or a 1780 // map-type-modifier. The map modifier can also be delete which has the same 1781 // spelling of the C++ delete keyword. 1782 Data.MapType = 1783 IsMapClauseModifierToken(Tok) 1784 ? static_cast<OpenMPMapClauseKind>( 1785 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok))) 1786 : OMPC_MAP_unknown; 1787 Data.DepLinMapLoc = Tok.getLocation(); 1788 bool ColonExpected = false; 1789 1790 if (IsMapClauseModifierToken(Tok)) { 1791 if (PP.LookAhead(0).is(tok::colon)) { 1792 if (Data.MapType == OMPC_MAP_unknown) 1793 Diag(Tok, diag::err_omp_unknown_map_type); 1794 else if (Data.MapType == OMPC_MAP_always) 1795 Diag(Tok, diag::err_omp_map_type_missing); 1796 ConsumeToken(); 1797 } else if (PP.LookAhead(0).is(tok::comma)) { 1798 if (IsMapClauseModifierToken(PP.LookAhead(1)) && 1799 PP.LookAhead(2).is(tok::colon)) { 1800 Data.MapTypeModifier = Data.MapType; 1801 if (Data.MapTypeModifier != OMPC_MAP_always) { 1802 Diag(Tok, diag::err_omp_unknown_map_type_modifier); 1803 Data.MapTypeModifier = OMPC_MAP_unknown; 1804 } else { 1805 MapTypeModifierSpecified = true; 1806 } 1807 1808 ConsumeToken(); 1809 ConsumeToken(); 1810 1811 Data.MapType = 1812 IsMapClauseModifierToken(Tok) 1813 ? static_cast<OpenMPMapClauseKind>( 1814 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok))) 1815 : OMPC_MAP_unknown; 1816 if (Data.MapType == OMPC_MAP_unknown || 1817 Data.MapType == OMPC_MAP_always) 1818 Diag(Tok, diag::err_omp_unknown_map_type); 1819 ConsumeToken(); 1820 } else { 1821 Data.MapType = OMPC_MAP_tofrom; 1822 Data.IsMapTypeImplicit = true; 1823 } 1824 } else if (IsMapClauseModifierToken(PP.LookAhead(0))) { 1825 if (PP.LookAhead(1).is(tok::colon)) { 1826 Data.MapTypeModifier = Data.MapType; 1827 if (Data.MapTypeModifier != OMPC_MAP_always) { 1828 Diag(Tok, diag::err_omp_unknown_map_type_modifier); 1829 Data.MapTypeModifier = OMPC_MAP_unknown; 1830 } else { 1831 MapTypeModifierSpecified = true; 1832 } 1833 1834 ConsumeToken(); 1835 1836 Data.MapType = 1837 IsMapClauseModifierToken(Tok) 1838 ? static_cast<OpenMPMapClauseKind>( 1839 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok))) 1840 : OMPC_MAP_unknown; 1841 if (Data.MapType == OMPC_MAP_unknown || 1842 Data.MapType == OMPC_MAP_always) 1843 Diag(Tok, diag::err_omp_unknown_map_type); 1844 ConsumeToken(); 1845 } else { 1846 Data.MapType = OMPC_MAP_tofrom; 1847 Data.IsMapTypeImplicit = true; 1848 } 1849 } else { 1850 Data.MapType = OMPC_MAP_tofrom; 1851 Data.IsMapTypeImplicit = true; 1852 } 1853 } else { 1854 Data.MapType = OMPC_MAP_tofrom; 1855 Data.IsMapTypeImplicit = true; 1856 } 1857 1858 if (Tok.is(tok::colon)) 1859 Data.ColonLoc = ConsumeToken(); 1860 else if (ColonExpected) 1861 Diag(Tok, diag::warn_pragma_expected_colon) << "map type"; 1862 } 1863 1864 bool IsComma = 1865 (Kind != OMPC_reduction && Kind != OMPC_task_reduction && 1866 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) || 1867 (Kind == OMPC_reduction && !InvalidReductionId) || 1868 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown && 1869 (!MapTypeModifierSpecified || 1870 Data.MapTypeModifier == OMPC_MAP_always)) || 1871 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown); 1872 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned); 1873 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) && 1874 Tok.isNot(tok::annot_pragma_openmp_end))) { 1875 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail); 1876 // Parse variable 1877 ExprResult VarExpr = 1878 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()); 1879 if (VarExpr.isUsable()) { 1880 Vars.push_back(VarExpr.get()); 1881 } else { 1882 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, 1883 StopBeforeMatch); 1884 } 1885 // Skip ',' if any 1886 IsComma = Tok.is(tok::comma); 1887 if (IsComma) 1888 ConsumeToken(); 1889 else if (Tok.isNot(tok::r_paren) && 1890 Tok.isNot(tok::annot_pragma_openmp_end) && 1891 (!MayHaveTail || Tok.isNot(tok::colon))) 1892 Diag(Tok, diag::err_omp_expected_punc) 1893 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush) 1894 : getOpenMPClauseName(Kind)) 1895 << (Kind == OMPC_flush); 1896 } 1897 1898 // Parse ')' for linear clause with modifier. 1899 if (NeedRParenForLinear) 1900 LinearT.consumeClose(); 1901 1902 // Parse ':' linear-step (or ':' alignment). 1903 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon); 1904 if (MustHaveTail) { 1905 Data.ColonLoc = Tok.getLocation(); 1906 SourceLocation ELoc = ConsumeToken(); 1907 ExprResult Tail = ParseAssignmentExpression(); 1908 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc); 1909 if (Tail.isUsable()) 1910 Data.TailExpr = Tail.get(); 1911 else 1912 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, 1913 StopBeforeMatch); 1914 } 1915 1916 // Parse ')'. 1917 T.consumeClose(); 1918 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown && 1919 Vars.empty()) || 1920 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) || 1921 (MustHaveTail && !Data.TailExpr) || InvalidReductionId; 1922 } 1923 1924 /// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate', 1925 /// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or 1926 /// 'in_reduction'. 1927 /// 1928 /// private-clause: 1929 /// 'private' '(' list ')' 1930 /// firstprivate-clause: 1931 /// 'firstprivate' '(' list ')' 1932 /// lastprivate-clause: 1933 /// 'lastprivate' '(' list ')' 1934 /// shared-clause: 1935 /// 'shared' '(' list ')' 1936 /// linear-clause: 1937 /// 'linear' '(' linear-list [ ':' linear-step ] ')' 1938 /// aligned-clause: 1939 /// 'aligned' '(' list [ ':' alignment ] ')' 1940 /// reduction-clause: 1941 /// 'reduction' '(' reduction-identifier ':' list ')' 1942 /// task_reduction-clause: 1943 /// 'task_reduction' '(' reduction-identifier ':' list ')' 1944 /// in_reduction-clause: 1945 /// 'in_reduction' '(' reduction-identifier ':' list ')' 1946 /// copyprivate-clause: 1947 /// 'copyprivate' '(' list ')' 1948 /// flush-clause: 1949 /// 'flush' '(' list ')' 1950 /// depend-clause: 1951 /// 'depend' '(' in | out | inout : list | source ')' 1952 /// map-clause: 1953 /// 'map' '(' [ [ always , ] 1954 /// to | from | tofrom | alloc | release | delete ':' ] list ')'; 1955 /// to-clause: 1956 /// 'to' '(' list ')' 1957 /// from-clause: 1958 /// 'from' '(' list ')' 1959 /// use_device_ptr-clause: 1960 /// 'use_device_ptr' '(' list ')' 1961 /// is_device_ptr-clause: 1962 /// 'is_device_ptr' '(' list ')' 1963 /// 1964 /// For 'linear' clause linear-list may have the following forms: 1965 /// list 1966 /// modifier(list) 1967 /// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++). 1968 OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind, 1969 OpenMPClauseKind Kind, 1970 bool ParseOnly) { 1971 SourceLocation Loc = Tok.getLocation(); 1972 SourceLocation LOpen = ConsumeToken(); 1973 SmallVector<Expr *, 4> Vars; 1974 OpenMPVarListDataTy Data; 1975 1976 if (ParseOpenMPVarList(DKind, Kind, Vars, Data)) 1977 return nullptr; 1978 1979 if (ParseOnly) 1980 return nullptr; 1981 return Actions.ActOnOpenMPVarListClause( 1982 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(), 1983 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind, 1984 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit, 1985 Data.DepLinMapLoc); 1986 } 1987 1988