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