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 /// \brief 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 /// \brief 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 /// \brief 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 /// \brief 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 SourceLocation TemplateKWLoc; 1158 UnqualifiedId Name; 1159 // Read var name. 1160 Token PrevTok = Tok; 1161 NoIdentIsFound = false; 1162 1163 if (AllowScopeSpecifier && getLangOpts().CPlusPlus && 1164 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) { 1165 IsCorrect = false; 1166 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, 1167 StopBeforeMatch); 1168 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr, 1169 TemplateKWLoc, Name)) { 1170 IsCorrect = false; 1171 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, 1172 StopBeforeMatch); 1173 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) && 1174 Tok.isNot(tok::annot_pragma_openmp_end)) { 1175 IsCorrect = false; 1176 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, 1177 StopBeforeMatch); 1178 Diag(PrevTok.getLocation(), diag::err_expected) 1179 << tok::identifier 1180 << SourceRange(PrevTok.getLocation(), PrevTokLocation); 1181 } else { 1182 Callback(SS, Actions.GetNameFromUnqualifiedId(Name)); 1183 } 1184 // Consume ','. 1185 if (Tok.is(tok::comma)) { 1186 ConsumeToken(); 1187 } 1188 } 1189 1190 if (NoIdentIsFound) { 1191 Diag(Tok, diag::err_expected) << tok::identifier; 1192 IsCorrect = false; 1193 } 1194 1195 // Parse ')'. 1196 IsCorrect = !T.consumeClose() && IsCorrect; 1197 1198 return !IsCorrect; 1199 } 1200 1201 /// \brief Parsing of OpenMP clauses. 1202 /// 1203 /// clause: 1204 /// if-clause | final-clause | num_threads-clause | safelen-clause | 1205 /// default-clause | private-clause | firstprivate-clause | shared-clause 1206 /// | linear-clause | aligned-clause | collapse-clause | 1207 /// lastprivate-clause | reduction-clause | proc_bind-clause | 1208 /// schedule-clause | copyin-clause | copyprivate-clause | untied-clause | 1209 /// mergeable-clause | flush-clause | read-clause | write-clause | 1210 /// update-clause | capture-clause | seq_cst-clause | device-clause | 1211 /// simdlen-clause | threads-clause | simd-clause | num_teams-clause | 1212 /// thread_limit-clause | priority-clause | grainsize-clause | 1213 /// nogroup-clause | num_tasks-clause | hint-clause | to-clause | 1214 /// from-clause | is_device_ptr-clause | task_reduction-clause | 1215 /// in_reduction-clause 1216 /// 1217 OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind, 1218 OpenMPClauseKind CKind, bool FirstClause) { 1219 OMPClause *Clause = nullptr; 1220 bool ErrorFound = false; 1221 bool WrongDirective = false; 1222 // Check if clause is allowed for the given directive. 1223 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) { 1224 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind) 1225 << getOpenMPDirectiveName(DKind); 1226 ErrorFound = true; 1227 WrongDirective = true; 1228 } 1229 1230 switch (CKind) { 1231 case OMPC_final: 1232 case OMPC_num_threads: 1233 case OMPC_safelen: 1234 case OMPC_simdlen: 1235 case OMPC_collapse: 1236 case OMPC_ordered: 1237 case OMPC_device: 1238 case OMPC_num_teams: 1239 case OMPC_thread_limit: 1240 case OMPC_priority: 1241 case OMPC_grainsize: 1242 case OMPC_num_tasks: 1243 case OMPC_hint: 1244 // OpenMP [2.5, Restrictions] 1245 // At most one num_threads clause can appear on the directive. 1246 // OpenMP [2.8.1, simd construct, Restrictions] 1247 // Only one safelen clause can appear on a simd directive. 1248 // Only one simdlen clause can appear on a simd directive. 1249 // Only one collapse clause can appear on a simd directive. 1250 // OpenMP [2.9.1, target data construct, Restrictions] 1251 // At most one device clause can appear on the directive. 1252 // OpenMP [2.11.1, task Construct, Restrictions] 1253 // At most one if clause can appear on the directive. 1254 // At most one final clause can appear on the directive. 1255 // OpenMP [teams Construct, Restrictions] 1256 // At most one num_teams clause can appear on the directive. 1257 // At most one thread_limit clause can appear on the directive. 1258 // OpenMP [2.9.1, task Construct, Restrictions] 1259 // At most one priority clause can appear on the directive. 1260 // OpenMP [2.9.2, taskloop Construct, Restrictions] 1261 // At most one grainsize clause can appear on the directive. 1262 // OpenMP [2.9.2, taskloop Construct, Restrictions] 1263 // At most one num_tasks clause can appear on the directive. 1264 if (!FirstClause) { 1265 Diag(Tok, diag::err_omp_more_one_clause) 1266 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; 1267 ErrorFound = true; 1268 } 1269 1270 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren)) 1271 Clause = ParseOpenMPClause(CKind, WrongDirective); 1272 else 1273 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective); 1274 break; 1275 case OMPC_default: 1276 case OMPC_proc_bind: 1277 // OpenMP [2.14.3.1, Restrictions] 1278 // Only a single default clause may be specified on a parallel, task or 1279 // teams directive. 1280 // OpenMP [2.5, parallel Construct, Restrictions] 1281 // At most one proc_bind clause can appear on the directive. 1282 if (!FirstClause) { 1283 Diag(Tok, diag::err_omp_more_one_clause) 1284 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; 1285 ErrorFound = true; 1286 } 1287 1288 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective); 1289 break; 1290 case OMPC_schedule: 1291 case OMPC_dist_schedule: 1292 case OMPC_defaultmap: 1293 // OpenMP [2.7.1, Restrictions, p. 3] 1294 // Only one schedule clause can appear on a loop directive. 1295 // OpenMP [2.10.4, Restrictions, p. 106] 1296 // At most one defaultmap clause can appear on the directive. 1297 if (!FirstClause) { 1298 Diag(Tok, diag::err_omp_more_one_clause) 1299 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; 1300 ErrorFound = true; 1301 } 1302 LLVM_FALLTHROUGH; 1303 1304 case OMPC_if: 1305 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective); 1306 break; 1307 case OMPC_nowait: 1308 case OMPC_untied: 1309 case OMPC_mergeable: 1310 case OMPC_read: 1311 case OMPC_write: 1312 case OMPC_update: 1313 case OMPC_capture: 1314 case OMPC_seq_cst: 1315 case OMPC_threads: 1316 case OMPC_simd: 1317 case OMPC_nogroup: 1318 // OpenMP [2.7.1, Restrictions, p. 9] 1319 // Only one ordered clause can appear on a loop directive. 1320 // OpenMP [2.7.1, Restrictions, C/C++, p. 4] 1321 // Only one nowait clause can appear on a for directive. 1322 if (!FirstClause) { 1323 Diag(Tok, diag::err_omp_more_one_clause) 1324 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; 1325 ErrorFound = true; 1326 } 1327 1328 Clause = ParseOpenMPClause(CKind, WrongDirective); 1329 break; 1330 case OMPC_private: 1331 case OMPC_firstprivate: 1332 case OMPC_lastprivate: 1333 case OMPC_shared: 1334 case OMPC_reduction: 1335 case OMPC_task_reduction: 1336 case OMPC_in_reduction: 1337 case OMPC_linear: 1338 case OMPC_aligned: 1339 case OMPC_copyin: 1340 case OMPC_copyprivate: 1341 case OMPC_flush: 1342 case OMPC_depend: 1343 case OMPC_map: 1344 case OMPC_to: 1345 case OMPC_from: 1346 case OMPC_use_device_ptr: 1347 case OMPC_is_device_ptr: 1348 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective); 1349 break; 1350 case OMPC_unknown: 1351 Diag(Tok, diag::warn_omp_extra_tokens_at_eol) 1352 << getOpenMPDirectiveName(DKind); 1353 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); 1354 break; 1355 case OMPC_threadprivate: 1356 case OMPC_uniform: 1357 if (!WrongDirective) 1358 Diag(Tok, diag::err_omp_unexpected_clause) 1359 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind); 1360 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch); 1361 break; 1362 } 1363 return ErrorFound ? nullptr : Clause; 1364 } 1365 1366 /// Parses simple expression in parens for single-expression clauses of OpenMP 1367 /// constructs. 1368 /// \param RLoc Returned location of right paren. 1369 ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName, 1370 SourceLocation &RLoc) { 1371 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); 1372 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data())) 1373 return ExprError(); 1374 1375 SourceLocation ELoc = Tok.getLocation(); 1376 ExprResult LHS(ParseCastExpression( 1377 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast)); 1378 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional)); 1379 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc); 1380 1381 // Parse ')'. 1382 T.consumeClose(); 1383 1384 RLoc = T.getCloseLocation(); 1385 return Val; 1386 } 1387 1388 /// \brief Parsing of OpenMP clauses with single expressions like 'final', 1389 /// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams', 1390 /// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'. 1391 /// 1392 /// final-clause: 1393 /// 'final' '(' expression ')' 1394 /// 1395 /// num_threads-clause: 1396 /// 'num_threads' '(' expression ')' 1397 /// 1398 /// safelen-clause: 1399 /// 'safelen' '(' expression ')' 1400 /// 1401 /// simdlen-clause: 1402 /// 'simdlen' '(' expression ')' 1403 /// 1404 /// collapse-clause: 1405 /// 'collapse' '(' expression ')' 1406 /// 1407 /// priority-clause: 1408 /// 'priority' '(' expression ')' 1409 /// 1410 /// grainsize-clause: 1411 /// 'grainsize' '(' expression ')' 1412 /// 1413 /// num_tasks-clause: 1414 /// 'num_tasks' '(' expression ')' 1415 /// 1416 /// hint-clause: 1417 /// 'hint' '(' expression ')' 1418 /// 1419 OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind, 1420 bool ParseOnly) { 1421 SourceLocation Loc = ConsumeToken(); 1422 SourceLocation LLoc = Tok.getLocation(); 1423 SourceLocation RLoc; 1424 1425 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc); 1426 1427 if (Val.isInvalid()) 1428 return nullptr; 1429 1430 if (ParseOnly) 1431 return nullptr; 1432 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc); 1433 } 1434 1435 /// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'. 1436 /// 1437 /// default-clause: 1438 /// 'default' '(' 'none' | 'shared' ') 1439 /// 1440 /// proc_bind-clause: 1441 /// 'proc_bind' '(' 'master' | 'close' | 'spread' ') 1442 /// 1443 OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind, 1444 bool ParseOnly) { 1445 SourceLocation Loc = Tok.getLocation(); 1446 SourceLocation LOpen = ConsumeToken(); 1447 // Parse '('. 1448 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); 1449 if (T.expectAndConsume(diag::err_expected_lparen_after, 1450 getOpenMPClauseName(Kind))) 1451 return nullptr; 1452 1453 unsigned Type = getOpenMPSimpleClauseType( 1454 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)); 1455 SourceLocation TypeLoc = Tok.getLocation(); 1456 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 1457 Tok.isNot(tok::annot_pragma_openmp_end)) 1458 ConsumeAnyToken(); 1459 1460 // Parse ')'. 1461 T.consumeClose(); 1462 1463 if (ParseOnly) 1464 return nullptr; 1465 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc, 1466 Tok.getLocation()); 1467 } 1468 1469 /// \brief Parsing of OpenMP clauses like 'ordered'. 1470 /// 1471 /// ordered-clause: 1472 /// 'ordered' 1473 /// 1474 /// nowait-clause: 1475 /// 'nowait' 1476 /// 1477 /// untied-clause: 1478 /// 'untied' 1479 /// 1480 /// mergeable-clause: 1481 /// 'mergeable' 1482 /// 1483 /// read-clause: 1484 /// 'read' 1485 /// 1486 /// threads-clause: 1487 /// 'threads' 1488 /// 1489 /// simd-clause: 1490 /// 'simd' 1491 /// 1492 /// nogroup-clause: 1493 /// 'nogroup' 1494 /// 1495 OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) { 1496 SourceLocation Loc = Tok.getLocation(); 1497 ConsumeAnyToken(); 1498 1499 if (ParseOnly) 1500 return nullptr; 1501 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation()); 1502 } 1503 1504 1505 /// \brief Parsing of OpenMP clauses with single expressions and some additional 1506 /// argument like 'schedule' or 'dist_schedule'. 1507 /// 1508 /// schedule-clause: 1509 /// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ] 1510 /// ')' 1511 /// 1512 /// if-clause: 1513 /// 'if' '(' [ directive-name-modifier ':' ] expression ')' 1514 /// 1515 /// defaultmap: 1516 /// 'defaultmap' '(' modifier ':' kind ')' 1517 /// 1518 OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind, 1519 bool ParseOnly) { 1520 SourceLocation Loc = ConsumeToken(); 1521 SourceLocation DelimLoc; 1522 // Parse '('. 1523 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); 1524 if (T.expectAndConsume(diag::err_expected_lparen_after, 1525 getOpenMPClauseName(Kind))) 1526 return nullptr; 1527 1528 ExprResult Val; 1529 SmallVector<unsigned, 4> Arg; 1530 SmallVector<SourceLocation, 4> KLoc; 1531 if (Kind == OMPC_schedule) { 1532 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 1533 Arg.resize(NumberOfElements); 1534 KLoc.resize(NumberOfElements); 1535 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown; 1536 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown; 1537 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown; 1538 unsigned KindModifier = getOpenMPSimpleClauseType( 1539 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)); 1540 if (KindModifier > OMPC_SCHEDULE_unknown) { 1541 // Parse 'modifier' 1542 Arg[Modifier1] = KindModifier; 1543 KLoc[Modifier1] = Tok.getLocation(); 1544 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 1545 Tok.isNot(tok::annot_pragma_openmp_end)) 1546 ConsumeAnyToken(); 1547 if (Tok.is(tok::comma)) { 1548 // Parse ',' 'modifier' 1549 ConsumeAnyToken(); 1550 KindModifier = getOpenMPSimpleClauseType( 1551 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)); 1552 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown 1553 ? KindModifier 1554 : (unsigned)OMPC_SCHEDULE_unknown; 1555 KLoc[Modifier2] = Tok.getLocation(); 1556 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 1557 Tok.isNot(tok::annot_pragma_openmp_end)) 1558 ConsumeAnyToken(); 1559 } 1560 // Parse ':' 1561 if (Tok.is(tok::colon)) 1562 ConsumeAnyToken(); 1563 else 1564 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier"; 1565 KindModifier = getOpenMPSimpleClauseType( 1566 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)); 1567 } 1568 Arg[ScheduleKind] = KindModifier; 1569 KLoc[ScheduleKind] = Tok.getLocation(); 1570 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 1571 Tok.isNot(tok::annot_pragma_openmp_end)) 1572 ConsumeAnyToken(); 1573 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static || 1574 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic || 1575 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) && 1576 Tok.is(tok::comma)) 1577 DelimLoc = ConsumeAnyToken(); 1578 } else if (Kind == OMPC_dist_schedule) { 1579 Arg.push_back(getOpenMPSimpleClauseType( 1580 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok))); 1581 KLoc.push_back(Tok.getLocation()); 1582 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 1583 Tok.isNot(tok::annot_pragma_openmp_end)) 1584 ConsumeAnyToken(); 1585 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma)) 1586 DelimLoc = ConsumeAnyToken(); 1587 } else if (Kind == OMPC_defaultmap) { 1588 // Get a defaultmap modifier 1589 Arg.push_back(getOpenMPSimpleClauseType( 1590 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok))); 1591 KLoc.push_back(Tok.getLocation()); 1592 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 1593 Tok.isNot(tok::annot_pragma_openmp_end)) 1594 ConsumeAnyToken(); 1595 // Parse ':' 1596 if (Tok.is(tok::colon)) 1597 ConsumeAnyToken(); 1598 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown) 1599 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier"; 1600 // Get a defaultmap kind 1601 Arg.push_back(getOpenMPSimpleClauseType( 1602 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok))); 1603 KLoc.push_back(Tok.getLocation()); 1604 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 1605 Tok.isNot(tok::annot_pragma_openmp_end)) 1606 ConsumeAnyToken(); 1607 } else { 1608 assert(Kind == OMPC_if); 1609 KLoc.push_back(Tok.getLocation()); 1610 TentativeParsingAction TPA(*this); 1611 Arg.push_back(parseOpenMPDirectiveKind(*this)); 1612 if (Arg.back() != OMPD_unknown) { 1613 ConsumeToken(); 1614 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) { 1615 TPA.Commit(); 1616 DelimLoc = ConsumeToken(); 1617 } else { 1618 TPA.Revert(); 1619 Arg.back() = OMPD_unknown; 1620 } 1621 } else { 1622 TPA.Revert(); 1623 } 1624 } 1625 1626 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) || 1627 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) || 1628 Kind == OMPC_if; 1629 if (NeedAnExpression) { 1630 SourceLocation ELoc = Tok.getLocation(); 1631 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast)); 1632 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional); 1633 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc); 1634 } 1635 1636 // Parse ')'. 1637 T.consumeClose(); 1638 1639 if (NeedAnExpression && Val.isInvalid()) 1640 return nullptr; 1641 1642 if (ParseOnly) 1643 return nullptr; 1644 return Actions.ActOnOpenMPSingleExprWithArgClause( 1645 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, 1646 T.getCloseLocation()); 1647 } 1648 1649 static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec, 1650 UnqualifiedId &ReductionId) { 1651 SourceLocation TemplateKWLoc; 1652 if (ReductionIdScopeSpec.isEmpty()) { 1653 auto OOK = OO_None; 1654 switch (P.getCurToken().getKind()) { 1655 case tok::plus: 1656 OOK = OO_Plus; 1657 break; 1658 case tok::minus: 1659 OOK = OO_Minus; 1660 break; 1661 case tok::star: 1662 OOK = OO_Star; 1663 break; 1664 case tok::amp: 1665 OOK = OO_Amp; 1666 break; 1667 case tok::pipe: 1668 OOK = OO_Pipe; 1669 break; 1670 case tok::caret: 1671 OOK = OO_Caret; 1672 break; 1673 case tok::ampamp: 1674 OOK = OO_AmpAmp; 1675 break; 1676 case tok::pipepipe: 1677 OOK = OO_PipePipe; 1678 break; 1679 default: 1680 break; 1681 } 1682 if (OOK != OO_None) { 1683 SourceLocation OpLoc = P.ConsumeToken(); 1684 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()}; 1685 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations); 1686 return false; 1687 } 1688 } 1689 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false, 1690 /*AllowDestructorName*/ false, 1691 /*AllowConstructorName*/ false, 1692 /*AllowDeductionGuide*/ false, 1693 nullptr, TemplateKWLoc, ReductionId); 1694 } 1695 1696 /// Parses clauses with list. 1697 bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind, 1698 OpenMPClauseKind Kind, 1699 SmallVectorImpl<Expr *> &Vars, 1700 OpenMPVarListDataTy &Data) { 1701 UnqualifiedId UnqualifiedReductionId; 1702 bool InvalidReductionId = false; 1703 bool MapTypeModifierSpecified = false; 1704 1705 // Parse '('. 1706 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); 1707 if (T.expectAndConsume(diag::err_expected_lparen_after, 1708 getOpenMPClauseName(Kind))) 1709 return true; 1710 1711 bool NeedRParenForLinear = false; 1712 BalancedDelimiterTracker LinearT(*this, tok::l_paren, 1713 tok::annot_pragma_openmp_end); 1714 // Handle reduction-identifier for reduction clause. 1715 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction || 1716 Kind == OMPC_in_reduction) { 1717 ColonProtectionRAIIObject ColonRAII(*this); 1718 if (getLangOpts().CPlusPlus) 1719 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec, 1720 /*ObjectType=*/nullptr, 1721 /*EnteringContext=*/false); 1722 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec, 1723 UnqualifiedReductionId); 1724 if (InvalidReductionId) { 1725 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, 1726 StopBeforeMatch); 1727 } 1728 if (Tok.is(tok::colon)) 1729 Data.ColonLoc = ConsumeToken(); 1730 else 1731 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier"; 1732 if (!InvalidReductionId) 1733 Data.ReductionId = 1734 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId); 1735 } else if (Kind == OMPC_depend) { 1736 // Handle dependency type for depend clause. 1737 ColonProtectionRAIIObject ColonRAII(*this); 1738 Data.DepKind = 1739 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType( 1740 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : "")); 1741 Data.DepLinMapLoc = Tok.getLocation(); 1742 1743 if (Data.DepKind == OMPC_DEPEND_unknown) { 1744 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, 1745 StopBeforeMatch); 1746 } else { 1747 ConsumeToken(); 1748 // Special processing for depend(source) clause. 1749 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) { 1750 // Parse ')'. 1751 T.consumeClose(); 1752 return false; 1753 } 1754 } 1755 if (Tok.is(tok::colon)) { 1756 Data.ColonLoc = ConsumeToken(); 1757 } else { 1758 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren 1759 : diag::warn_pragma_expected_colon) 1760 << "dependency type"; 1761 } 1762 } else if (Kind == OMPC_linear) { 1763 // Try to parse modifier if any. 1764 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) { 1765 Data.LinKind = static_cast<OpenMPLinearClauseKind>( 1766 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok))); 1767 Data.DepLinMapLoc = ConsumeToken(); 1768 LinearT.consumeOpen(); 1769 NeedRParenForLinear = true; 1770 } 1771 } else if (Kind == OMPC_map) { 1772 // Handle map type for map clause. 1773 ColonProtectionRAIIObject ColonRAII(*this); 1774 1775 /// The map clause modifier token can be either a identifier or the C++ 1776 /// delete keyword. 1777 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool { 1778 return Tok.isOneOf(tok::identifier, tok::kw_delete); 1779 }; 1780 1781 // The first identifier may be a list item, a map-type or a 1782 // map-type-modifier. The map modifier can also be delete which has the same 1783 // spelling of the C++ delete keyword. 1784 Data.MapType = 1785 IsMapClauseModifierToken(Tok) 1786 ? static_cast<OpenMPMapClauseKind>( 1787 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok))) 1788 : OMPC_MAP_unknown; 1789 Data.DepLinMapLoc = Tok.getLocation(); 1790 bool ColonExpected = false; 1791 1792 if (IsMapClauseModifierToken(Tok)) { 1793 if (PP.LookAhead(0).is(tok::colon)) { 1794 if (Data.MapType == OMPC_MAP_unknown) 1795 Diag(Tok, diag::err_omp_unknown_map_type); 1796 else if (Data.MapType == OMPC_MAP_always) 1797 Diag(Tok, diag::err_omp_map_type_missing); 1798 ConsumeToken(); 1799 } else if (PP.LookAhead(0).is(tok::comma)) { 1800 if (IsMapClauseModifierToken(PP.LookAhead(1)) && 1801 PP.LookAhead(2).is(tok::colon)) { 1802 Data.MapTypeModifier = Data.MapType; 1803 if (Data.MapTypeModifier != OMPC_MAP_always) { 1804 Diag(Tok, diag::err_omp_unknown_map_type_modifier); 1805 Data.MapTypeModifier = OMPC_MAP_unknown; 1806 } else { 1807 MapTypeModifierSpecified = true; 1808 } 1809 1810 ConsumeToken(); 1811 ConsumeToken(); 1812 1813 Data.MapType = 1814 IsMapClauseModifierToken(Tok) 1815 ? static_cast<OpenMPMapClauseKind>( 1816 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok))) 1817 : OMPC_MAP_unknown; 1818 if (Data.MapType == OMPC_MAP_unknown || 1819 Data.MapType == OMPC_MAP_always) 1820 Diag(Tok, diag::err_omp_unknown_map_type); 1821 ConsumeToken(); 1822 } else { 1823 Data.MapType = OMPC_MAP_tofrom; 1824 Data.IsMapTypeImplicit = true; 1825 } 1826 } else if (IsMapClauseModifierToken(PP.LookAhead(0))) { 1827 if (PP.LookAhead(1).is(tok::colon)) { 1828 Data.MapTypeModifier = Data.MapType; 1829 if (Data.MapTypeModifier != OMPC_MAP_always) { 1830 Diag(Tok, diag::err_omp_unknown_map_type_modifier); 1831 Data.MapTypeModifier = OMPC_MAP_unknown; 1832 } else { 1833 MapTypeModifierSpecified = true; 1834 } 1835 1836 ConsumeToken(); 1837 1838 Data.MapType = 1839 IsMapClauseModifierToken(Tok) 1840 ? static_cast<OpenMPMapClauseKind>( 1841 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok))) 1842 : OMPC_MAP_unknown; 1843 if (Data.MapType == OMPC_MAP_unknown || 1844 Data.MapType == OMPC_MAP_always) 1845 Diag(Tok, diag::err_omp_unknown_map_type); 1846 ConsumeToken(); 1847 } else { 1848 Data.MapType = OMPC_MAP_tofrom; 1849 Data.IsMapTypeImplicit = true; 1850 } 1851 } else { 1852 Data.MapType = OMPC_MAP_tofrom; 1853 Data.IsMapTypeImplicit = true; 1854 } 1855 } else { 1856 Data.MapType = OMPC_MAP_tofrom; 1857 Data.IsMapTypeImplicit = true; 1858 } 1859 1860 if (Tok.is(tok::colon)) 1861 Data.ColonLoc = ConsumeToken(); 1862 else if (ColonExpected) 1863 Diag(Tok, diag::warn_pragma_expected_colon) << "map type"; 1864 } 1865 1866 bool IsComma = 1867 (Kind != OMPC_reduction && Kind != OMPC_task_reduction && 1868 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) || 1869 (Kind == OMPC_reduction && !InvalidReductionId) || 1870 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown && 1871 (!MapTypeModifierSpecified || 1872 Data.MapTypeModifier == OMPC_MAP_always)) || 1873 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown); 1874 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned); 1875 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) && 1876 Tok.isNot(tok::annot_pragma_openmp_end))) { 1877 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail); 1878 // Parse variable 1879 ExprResult VarExpr = 1880 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()); 1881 if (VarExpr.isUsable()) { 1882 Vars.push_back(VarExpr.get()); 1883 } else { 1884 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, 1885 StopBeforeMatch); 1886 } 1887 // Skip ',' if any 1888 IsComma = Tok.is(tok::comma); 1889 if (IsComma) 1890 ConsumeToken(); 1891 else if (Tok.isNot(tok::r_paren) && 1892 Tok.isNot(tok::annot_pragma_openmp_end) && 1893 (!MayHaveTail || Tok.isNot(tok::colon))) 1894 Diag(Tok, diag::err_omp_expected_punc) 1895 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush) 1896 : getOpenMPClauseName(Kind)) 1897 << (Kind == OMPC_flush); 1898 } 1899 1900 // Parse ')' for linear clause with modifier. 1901 if (NeedRParenForLinear) 1902 LinearT.consumeClose(); 1903 1904 // Parse ':' linear-step (or ':' alignment). 1905 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon); 1906 if (MustHaveTail) { 1907 Data.ColonLoc = Tok.getLocation(); 1908 SourceLocation ELoc = ConsumeToken(); 1909 ExprResult Tail = ParseAssignmentExpression(); 1910 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc); 1911 if (Tail.isUsable()) 1912 Data.TailExpr = Tail.get(); 1913 else 1914 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, 1915 StopBeforeMatch); 1916 } 1917 1918 // Parse ')'. 1919 T.consumeClose(); 1920 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown && 1921 Vars.empty()) || 1922 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) || 1923 (MustHaveTail && !Data.TailExpr) || InvalidReductionId; 1924 } 1925 1926 /// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate', 1927 /// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or 1928 /// 'in_reduction'. 1929 /// 1930 /// private-clause: 1931 /// 'private' '(' list ')' 1932 /// firstprivate-clause: 1933 /// 'firstprivate' '(' list ')' 1934 /// lastprivate-clause: 1935 /// 'lastprivate' '(' list ')' 1936 /// shared-clause: 1937 /// 'shared' '(' list ')' 1938 /// linear-clause: 1939 /// 'linear' '(' linear-list [ ':' linear-step ] ')' 1940 /// aligned-clause: 1941 /// 'aligned' '(' list [ ':' alignment ] ')' 1942 /// reduction-clause: 1943 /// 'reduction' '(' reduction-identifier ':' list ')' 1944 /// task_reduction-clause: 1945 /// 'task_reduction' '(' reduction-identifier ':' list ')' 1946 /// in_reduction-clause: 1947 /// 'in_reduction' '(' reduction-identifier ':' list ')' 1948 /// copyprivate-clause: 1949 /// 'copyprivate' '(' list ')' 1950 /// flush-clause: 1951 /// 'flush' '(' list ')' 1952 /// depend-clause: 1953 /// 'depend' '(' in | out | inout : list | source ')' 1954 /// map-clause: 1955 /// 'map' '(' [ [ always , ] 1956 /// to | from | tofrom | alloc | release | delete ':' ] list ')'; 1957 /// to-clause: 1958 /// 'to' '(' list ')' 1959 /// from-clause: 1960 /// 'from' '(' list ')' 1961 /// use_device_ptr-clause: 1962 /// 'use_device_ptr' '(' list ')' 1963 /// is_device_ptr-clause: 1964 /// 'is_device_ptr' '(' list ')' 1965 /// 1966 /// For 'linear' clause linear-list may have the following forms: 1967 /// list 1968 /// modifier(list) 1969 /// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++). 1970 OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind, 1971 OpenMPClauseKind Kind, 1972 bool ParseOnly) { 1973 SourceLocation Loc = Tok.getLocation(); 1974 SourceLocation LOpen = ConsumeToken(); 1975 SmallVector<Expr *, 4> Vars; 1976 OpenMPVarListDataTy Data; 1977 1978 if (ParseOpenMPVarList(DKind, Kind, Vars, Data)) 1979 return nullptr; 1980 1981 if (ParseOnly) 1982 return nullptr; 1983 return Actions.ActOnOpenMPVarListClause( 1984 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(), 1985 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind, 1986 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit, 1987 Data.DepLinMapLoc); 1988 } 1989 1990