1 //===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// \file 9 /// This file implements parsing of all OpenMP directives and clauses. 10 /// 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/AST/ASTContext.h" 14 #include "clang/AST/StmtOpenMP.h" 15 #include "clang/Parse/ParseDiagnostic.h" 16 #include "clang/Parse/Parser.h" 17 #include "clang/Parse/RAIIObjectsForParser.h" 18 #include "clang/Sema/Scope.h" 19 #include "llvm/ADT/PointerIntPair.h" 20 #include "llvm/ADT/UniqueVector.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 OMPD_mapper, 46 OMPD_variant, 47 OMPD_parallel_master, 48 }; 49 50 class DeclDirectiveListParserHelper final { 51 SmallVector<Expr *, 4> Identifiers; 52 Parser *P; 53 OpenMPDirectiveKind Kind; 54 55 public: 56 DeclDirectiveListParserHelper(Parser *P, OpenMPDirectiveKind Kind) 57 : P(P), Kind(Kind) {} 58 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) { 59 ExprResult Res = P->getActions().ActOnOpenMPIdExpression( 60 P->getCurScope(), SS, NameInfo, Kind); 61 if (Res.isUsable()) 62 Identifiers.push_back(Res.get()); 63 } 64 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; } 65 }; 66 } // namespace 67 68 // Map token string to extended OMP token kind that are 69 // OpenMPDirectiveKind + OpenMPDirectiveKindEx. 70 static unsigned getOpenMPDirectiveKindEx(StringRef S) { 71 auto DKind = getOpenMPDirectiveKind(S); 72 if (DKind != OMPD_unknown) 73 return DKind; 74 75 return llvm::StringSwitch<unsigned>(S) 76 .Case("cancellation", OMPD_cancellation) 77 .Case("data", OMPD_data) 78 .Case("declare", OMPD_declare) 79 .Case("end", OMPD_end) 80 .Case("enter", OMPD_enter) 81 .Case("exit", OMPD_exit) 82 .Case("point", OMPD_point) 83 .Case("reduction", OMPD_reduction) 84 .Case("update", OMPD_update) 85 .Case("mapper", OMPD_mapper) 86 .Case("variant", OMPD_variant) 87 .Default(OMPD_unknown); 88 } 89 90 static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P) { 91 // Array of foldings: F[i][0] F[i][1] ===> F[i][2]. 92 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd 93 // TODO: add other combined directives in topological order. 94 static const unsigned F[][3] = { 95 {OMPD_cancellation, OMPD_point, OMPD_cancellation_point}, 96 {OMPD_declare, OMPD_reduction, OMPD_declare_reduction}, 97 {OMPD_declare, OMPD_mapper, OMPD_declare_mapper}, 98 {OMPD_declare, OMPD_simd, OMPD_declare_simd}, 99 {OMPD_declare, OMPD_target, OMPD_declare_target}, 100 {OMPD_declare, OMPD_variant, OMPD_declare_variant}, 101 {OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel}, 102 {OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for}, 103 {OMPD_distribute_parallel_for, OMPD_simd, 104 OMPD_distribute_parallel_for_simd}, 105 {OMPD_distribute, OMPD_simd, OMPD_distribute_simd}, 106 {OMPD_end, OMPD_declare, OMPD_end_declare}, 107 {OMPD_end_declare, OMPD_target, OMPD_end_declare_target}, 108 {OMPD_target, OMPD_data, OMPD_target_data}, 109 {OMPD_target, OMPD_enter, OMPD_target_enter}, 110 {OMPD_target, OMPD_exit, OMPD_target_exit}, 111 {OMPD_target, OMPD_update, OMPD_target_update}, 112 {OMPD_target_enter, OMPD_data, OMPD_target_enter_data}, 113 {OMPD_target_exit, OMPD_data, OMPD_target_exit_data}, 114 {OMPD_for, OMPD_simd, OMPD_for_simd}, 115 {OMPD_parallel, OMPD_for, OMPD_parallel_for}, 116 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd}, 117 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections}, 118 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd}, 119 {OMPD_target, OMPD_parallel, OMPD_target_parallel}, 120 {OMPD_target, OMPD_simd, OMPD_target_simd}, 121 {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for}, 122 {OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd}, 123 {OMPD_teams, OMPD_distribute, OMPD_teams_distribute}, 124 {OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd}, 125 {OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel}, 126 {OMPD_teams_distribute_parallel, OMPD_for, 127 OMPD_teams_distribute_parallel_for}, 128 {OMPD_teams_distribute_parallel_for, OMPD_simd, 129 OMPD_teams_distribute_parallel_for_simd}, 130 {OMPD_target, OMPD_teams, OMPD_target_teams}, 131 {OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute}, 132 {OMPD_target_teams_distribute, OMPD_parallel, 133 OMPD_target_teams_distribute_parallel}, 134 {OMPD_target_teams_distribute, OMPD_simd, 135 OMPD_target_teams_distribute_simd}, 136 {OMPD_target_teams_distribute_parallel, OMPD_for, 137 OMPD_target_teams_distribute_parallel_for}, 138 {OMPD_target_teams_distribute_parallel_for, OMPD_simd, 139 OMPD_target_teams_distribute_parallel_for_simd}, 140 {OMPD_master, OMPD_taskloop, OMPD_master_taskloop}, 141 {OMPD_master_taskloop, OMPD_simd, OMPD_master_taskloop_simd}, 142 {OMPD_parallel, OMPD_master, OMPD_parallel_master}, 143 {OMPD_parallel_master, OMPD_taskloop, OMPD_parallel_master_taskloop}, 144 {OMPD_parallel_master_taskloop, OMPD_simd, 145 OMPD_parallel_master_taskloop_simd}}; 146 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 }; 147 Token Tok = P.getCurToken(); 148 unsigned DKind = 149 Tok.isAnnotation() 150 ? static_cast<unsigned>(OMPD_unknown) 151 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok)); 152 if (DKind == OMPD_unknown) 153 return OMPD_unknown; 154 155 for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) { 156 if (DKind != F[I][0]) 157 continue; 158 159 Tok = P.getPreprocessor().LookAhead(0); 160 unsigned SDKind = 161 Tok.isAnnotation() 162 ? static_cast<unsigned>(OMPD_unknown) 163 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok)); 164 if (SDKind == OMPD_unknown) 165 continue; 166 167 if (SDKind == F[I][1]) { 168 P.ConsumeToken(); 169 DKind = F[I][2]; 170 } 171 } 172 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind) 173 : OMPD_unknown; 174 } 175 176 static DeclarationName parseOpenMPReductionId(Parser &P) { 177 Token Tok = P.getCurToken(); 178 Sema &Actions = P.getActions(); 179 OverloadedOperatorKind OOK = OO_None; 180 // Allow to use 'operator' keyword for C++ operators 181 bool WithOperator = false; 182 if (Tok.is(tok::kw_operator)) { 183 P.ConsumeToken(); 184 Tok = P.getCurToken(); 185 WithOperator = true; 186 } 187 switch (Tok.getKind()) { 188 case tok::plus: // '+' 189 OOK = OO_Plus; 190 break; 191 case tok::minus: // '-' 192 OOK = OO_Minus; 193 break; 194 case tok::star: // '*' 195 OOK = OO_Star; 196 break; 197 case tok::amp: // '&' 198 OOK = OO_Amp; 199 break; 200 case tok::pipe: // '|' 201 OOK = OO_Pipe; 202 break; 203 case tok::caret: // '^' 204 OOK = OO_Caret; 205 break; 206 case tok::ampamp: // '&&' 207 OOK = OO_AmpAmp; 208 break; 209 case tok::pipepipe: // '||' 210 OOK = OO_PipePipe; 211 break; 212 case tok::identifier: // identifier 213 if (!WithOperator) 214 break; 215 LLVM_FALLTHROUGH; 216 default: 217 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier); 218 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, 219 Parser::StopBeforeMatch); 220 return DeclarationName(); 221 } 222 P.ConsumeToken(); 223 auto &DeclNames = Actions.getASTContext().DeclarationNames; 224 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo()) 225 : DeclNames.getCXXOperatorName(OOK); 226 } 227 228 /// Parse 'omp declare reduction' construct. 229 /// 230 /// declare-reduction-directive: 231 /// annot_pragma_openmp 'declare' 'reduction' 232 /// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')' 233 /// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')'] 234 /// annot_pragma_openmp_end 235 /// <reduction_id> is either a base language identifier or one of the following 236 /// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'. 237 /// 238 Parser::DeclGroupPtrTy 239 Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) { 240 // Parse '('. 241 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); 242 if (T.expectAndConsume(diag::err_expected_lparen_after, 243 getOpenMPDirectiveName(OMPD_declare_reduction))) { 244 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); 245 return DeclGroupPtrTy(); 246 } 247 248 DeclarationName Name = parseOpenMPReductionId(*this); 249 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end)) 250 return DeclGroupPtrTy(); 251 252 // Consume ':'. 253 bool IsCorrect = !ExpectAndConsume(tok::colon); 254 255 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end)) 256 return DeclGroupPtrTy(); 257 258 IsCorrect = IsCorrect && !Name.isEmpty(); 259 260 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) { 261 Diag(Tok.getLocation(), diag::err_expected_type); 262 IsCorrect = false; 263 } 264 265 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end)) 266 return DeclGroupPtrTy(); 267 268 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes; 269 // Parse list of types until ':' token. 270 do { 271 ColonProtectionRAIIObject ColonRAII(*this); 272 SourceRange Range; 273 TypeResult TR = 274 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS); 275 if (TR.isUsable()) { 276 QualType ReductionType = 277 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR); 278 if (!ReductionType.isNull()) { 279 ReductionTypes.push_back( 280 std::make_pair(ReductionType, Range.getBegin())); 281 } 282 } else { 283 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end, 284 StopBeforeMatch); 285 } 286 287 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) 288 break; 289 290 // Consume ','. 291 if (ExpectAndConsume(tok::comma)) { 292 IsCorrect = false; 293 if (Tok.is(tok::annot_pragma_openmp_end)) { 294 Diag(Tok.getLocation(), diag::err_expected_type); 295 return DeclGroupPtrTy(); 296 } 297 } 298 } while (Tok.isNot(tok::annot_pragma_openmp_end)); 299 300 if (ReductionTypes.empty()) { 301 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); 302 return DeclGroupPtrTy(); 303 } 304 305 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end)) 306 return DeclGroupPtrTy(); 307 308 // Consume ':'. 309 if (ExpectAndConsume(tok::colon)) 310 IsCorrect = false; 311 312 if (Tok.is(tok::annot_pragma_openmp_end)) { 313 Diag(Tok.getLocation(), diag::err_expected_expression); 314 return DeclGroupPtrTy(); 315 } 316 317 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart( 318 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS); 319 320 // Parse <combiner> expression and then parse initializer if any for each 321 // correct type. 322 unsigned I = 0, E = ReductionTypes.size(); 323 for (Decl *D : DRD.get()) { 324 TentativeParsingAction TPA(*this); 325 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope | 326 Scope::CompoundStmtScope | 327 Scope::OpenMPDirectiveScope); 328 // Parse <combiner> expression. 329 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D); 330 ExprResult CombinerResult = 331 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(), 332 D->getLocation(), /*DiscardedValue*/ false); 333 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get()); 334 335 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) && 336 Tok.isNot(tok::annot_pragma_openmp_end)) { 337 TPA.Commit(); 338 IsCorrect = false; 339 break; 340 } 341 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable(); 342 ExprResult InitializerResult; 343 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 344 // Parse <initializer> expression. 345 if (Tok.is(tok::identifier) && 346 Tok.getIdentifierInfo()->isStr("initializer")) { 347 ConsumeToken(); 348 } else { 349 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'"; 350 TPA.Commit(); 351 IsCorrect = false; 352 break; 353 } 354 // Parse '('. 355 BalancedDelimiterTracker T(*this, tok::l_paren, 356 tok::annot_pragma_openmp_end); 357 IsCorrect = 358 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") && 359 IsCorrect; 360 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 361 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope | 362 Scope::CompoundStmtScope | 363 Scope::OpenMPDirectiveScope); 364 // Parse expression. 365 VarDecl *OmpPrivParm = 366 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), 367 D); 368 // Check if initializer is omp_priv <init_expr> or something else. 369 if (Tok.is(tok::identifier) && 370 Tok.getIdentifierInfo()->isStr("omp_priv")) { 371 if (Actions.getLangOpts().CPlusPlus) { 372 InitializerResult = Actions.ActOnFinishFullExpr( 373 ParseAssignmentExpression().get(), D->getLocation(), 374 /*DiscardedValue*/ false); 375 } else { 376 ConsumeToken(); 377 ParseOpenMPReductionInitializerForDecl(OmpPrivParm); 378 } 379 } else { 380 InitializerResult = Actions.ActOnFinishFullExpr( 381 ParseAssignmentExpression().get(), D->getLocation(), 382 /*DiscardedValue*/ false); 383 } 384 Actions.ActOnOpenMPDeclareReductionInitializerEnd( 385 D, InitializerResult.get(), OmpPrivParm); 386 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) && 387 Tok.isNot(tok::annot_pragma_openmp_end)) { 388 TPA.Commit(); 389 IsCorrect = false; 390 break; 391 } 392 IsCorrect = 393 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid(); 394 } 395 } 396 397 ++I; 398 // Revert parsing if not the last type, otherwise accept it, we're done with 399 // parsing. 400 if (I != E) 401 TPA.Revert(); 402 else 403 TPA.Commit(); 404 } 405 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD, 406 IsCorrect); 407 } 408 409 void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) { 410 // Parse declarator '=' initializer. 411 // If a '==' or '+=' is found, suggest a fixit to '='. 412 if (isTokenEqualOrEqualTypo()) { 413 ConsumeToken(); 414 415 if (Tok.is(tok::code_completion)) { 416 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm); 417 Actions.FinalizeDeclaration(OmpPrivParm); 418 cutOffParsing(); 419 return; 420 } 421 422 ExprResult Init(ParseInitializer()); 423 424 if (Init.isInvalid()) { 425 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch); 426 Actions.ActOnInitializerError(OmpPrivParm); 427 } else { 428 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(), 429 /*DirectInit=*/false); 430 } 431 } else if (Tok.is(tok::l_paren)) { 432 // Parse C++ direct initializer: '(' expression-list ')' 433 BalancedDelimiterTracker T(*this, tok::l_paren); 434 T.consumeOpen(); 435 436 ExprVector Exprs; 437 CommaLocsTy CommaLocs; 438 439 SourceLocation LParLoc = T.getOpenLocation(); 440 auto RunSignatureHelp = [this, OmpPrivParm, LParLoc, &Exprs]() { 441 QualType PreferredType = Actions.ProduceConstructorSignatureHelp( 442 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(), 443 OmpPrivParm->getLocation(), Exprs, LParLoc); 444 CalledSignatureHelp = true; 445 return PreferredType; 446 }; 447 if (ParseExpressionList(Exprs, CommaLocs, [&] { 448 PreferredType.enterFunctionArgument(Tok.getLocation(), 449 RunSignatureHelp); 450 })) { 451 if (PP.isCodeCompletionReached() && !CalledSignatureHelp) 452 RunSignatureHelp(); 453 Actions.ActOnInitializerError(OmpPrivParm); 454 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch); 455 } else { 456 // Match the ')'. 457 SourceLocation RLoc = Tok.getLocation(); 458 if (!T.consumeClose()) 459 RLoc = T.getCloseLocation(); 460 461 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() && 462 "Unexpected number of commas!"); 463 464 ExprResult Initializer = 465 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs); 466 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(), 467 /*DirectInit=*/true); 468 } 469 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { 470 // Parse C++0x braced-init-list. 471 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); 472 473 ExprResult Init(ParseBraceInitializer()); 474 475 if (Init.isInvalid()) { 476 Actions.ActOnInitializerError(OmpPrivParm); 477 } else { 478 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(), 479 /*DirectInit=*/true); 480 } 481 } else { 482 Actions.ActOnUninitializedDecl(OmpPrivParm); 483 } 484 } 485 486 /// Parses 'omp declare mapper' directive. 487 /// 488 /// declare-mapper-directive: 489 /// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifier> ':'] 490 /// <type> <var> ')' [<clause>[[,] <clause>] ... ] 491 /// annot_pragma_openmp_end 492 /// <mapper-identifier> and <var> are base language identifiers. 493 /// 494 Parser::DeclGroupPtrTy 495 Parser::ParseOpenMPDeclareMapperDirective(AccessSpecifier AS) { 496 bool IsCorrect = true; 497 // Parse '(' 498 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); 499 if (T.expectAndConsume(diag::err_expected_lparen_after, 500 getOpenMPDirectiveName(OMPD_declare_mapper))) { 501 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); 502 return DeclGroupPtrTy(); 503 } 504 505 // Parse <mapper-identifier> 506 auto &DeclNames = Actions.getASTContext().DeclarationNames; 507 DeclarationName MapperId; 508 if (PP.LookAhead(0).is(tok::colon)) { 509 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) { 510 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier); 511 IsCorrect = false; 512 } else { 513 MapperId = DeclNames.getIdentifier(Tok.getIdentifierInfo()); 514 } 515 ConsumeToken(); 516 // Consume ':'. 517 ExpectAndConsume(tok::colon); 518 } else { 519 // If no mapper identifier is provided, its name is "default" by default 520 MapperId = 521 DeclNames.getIdentifier(&Actions.getASTContext().Idents.get("default")); 522 } 523 524 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end)) 525 return DeclGroupPtrTy(); 526 527 // Parse <type> <var> 528 DeclarationName VName; 529 QualType MapperType; 530 SourceRange Range; 531 TypeResult ParsedType = parseOpenMPDeclareMapperVarDecl(Range, VName, AS); 532 if (ParsedType.isUsable()) 533 MapperType = 534 Actions.ActOnOpenMPDeclareMapperType(Range.getBegin(), ParsedType); 535 if (MapperType.isNull()) 536 IsCorrect = false; 537 if (!IsCorrect) { 538 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch); 539 return DeclGroupPtrTy(); 540 } 541 542 // Consume ')'. 543 IsCorrect &= !T.consumeClose(); 544 if (!IsCorrect) { 545 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch); 546 return DeclGroupPtrTy(); 547 } 548 549 // Enter scope. 550 OMPDeclareMapperDecl *DMD = Actions.ActOnOpenMPDeclareMapperDirectiveStart( 551 getCurScope(), Actions.getCurLexicalContext(), MapperId, MapperType, 552 Range.getBegin(), VName, AS); 553 DeclarationNameInfo DirName; 554 SourceLocation Loc = Tok.getLocation(); 555 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope | 556 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope; 557 ParseScope OMPDirectiveScope(this, ScopeFlags); 558 Actions.StartOpenMPDSABlock(OMPD_declare_mapper, DirName, getCurScope(), Loc); 559 560 // Add the mapper variable declaration. 561 Actions.ActOnOpenMPDeclareMapperDirectiveVarDecl( 562 DMD, getCurScope(), MapperType, Range.getBegin(), VName); 563 564 // Parse map clauses. 565 SmallVector<OMPClause *, 6> Clauses; 566 while (Tok.isNot(tok::annot_pragma_openmp_end)) { 567 OpenMPClauseKind CKind = Tok.isAnnotation() 568 ? OMPC_unknown 569 : getOpenMPClauseKind(PP.getSpelling(Tok)); 570 Actions.StartOpenMPClause(CKind); 571 OMPClause *Clause = 572 ParseOpenMPClause(OMPD_declare_mapper, CKind, Clauses.size() == 0); 573 if (Clause) 574 Clauses.push_back(Clause); 575 else 576 IsCorrect = false; 577 // Skip ',' if any. 578 if (Tok.is(tok::comma)) 579 ConsumeToken(); 580 Actions.EndOpenMPClause(); 581 } 582 if (Clauses.empty()) { 583 Diag(Tok, diag::err_omp_expected_clause) 584 << getOpenMPDirectiveName(OMPD_declare_mapper); 585 IsCorrect = false; 586 } 587 588 // Exit scope. 589 Actions.EndOpenMPDSABlock(nullptr); 590 OMPDirectiveScope.Exit(); 591 592 DeclGroupPtrTy DGP = 593 Actions.ActOnOpenMPDeclareMapperDirectiveEnd(DMD, getCurScope(), Clauses); 594 if (!IsCorrect) 595 return DeclGroupPtrTy(); 596 return DGP; 597 } 598 599 TypeResult Parser::parseOpenMPDeclareMapperVarDecl(SourceRange &Range, 600 DeclarationName &Name, 601 AccessSpecifier AS) { 602 // Parse the common declaration-specifiers piece. 603 Parser::DeclSpecContext DSC = Parser::DeclSpecContext::DSC_type_specifier; 604 DeclSpec DS(AttrFactory); 605 ParseSpecifierQualifierList(DS, AS, DSC); 606 607 // Parse the declarator. 608 DeclaratorContext Context = DeclaratorContext::PrototypeContext; 609 Declarator DeclaratorInfo(DS, Context); 610 ParseDeclarator(DeclaratorInfo); 611 Range = DeclaratorInfo.getSourceRange(); 612 if (DeclaratorInfo.getIdentifier() == nullptr) { 613 Diag(Tok.getLocation(), diag::err_omp_mapper_expected_declarator); 614 return true; 615 } 616 Name = Actions.GetNameForDeclarator(DeclaratorInfo).getName(); 617 618 return Actions.ActOnOpenMPDeclareMapperVarDecl(getCurScope(), DeclaratorInfo); 619 } 620 621 namespace { 622 /// RAII that recreates function context for correct parsing of clauses of 623 /// 'declare simd' construct. 624 /// OpenMP, 2.8.2 declare simd Construct 625 /// The expressions appearing in the clauses of this directive are evaluated in 626 /// the scope of the arguments of the function declaration or definition. 627 class FNContextRAII final { 628 Parser &P; 629 Sema::CXXThisScopeRAII *ThisScope; 630 Parser::ParseScope *TempScope; 631 Parser::ParseScope *FnScope; 632 bool HasTemplateScope = false; 633 bool HasFunScope = false; 634 FNContextRAII() = delete; 635 FNContextRAII(const FNContextRAII &) = delete; 636 FNContextRAII &operator=(const FNContextRAII &) = delete; 637 638 public: 639 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) { 640 Decl *D = *Ptr.get().begin(); 641 NamedDecl *ND = dyn_cast<NamedDecl>(D); 642 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); 643 Sema &Actions = P.getActions(); 644 645 // Allow 'this' within late-parsed attributes. 646 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, Qualifiers(), 647 ND && ND->isCXXInstanceMember()); 648 649 // If the Decl is templatized, add template parameters to scope. 650 HasTemplateScope = D->isTemplateDecl(); 651 TempScope = 652 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope); 653 if (HasTemplateScope) 654 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D); 655 656 // If the Decl is on a function, add function parameters to the scope. 657 HasFunScope = D->isFunctionOrFunctionTemplate(); 658 FnScope = new Parser::ParseScope( 659 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope, 660 HasFunScope); 661 if (HasFunScope) 662 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D); 663 } 664 ~FNContextRAII() { 665 if (HasFunScope) { 666 P.getActions().ActOnExitFunctionContext(); 667 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver 668 } 669 if (HasTemplateScope) 670 TempScope->Exit(); 671 delete FnScope; 672 delete TempScope; 673 delete ThisScope; 674 } 675 }; 676 } // namespace 677 678 /// Parses clauses for 'declare simd' directive. 679 /// clause: 680 /// 'inbranch' | 'notinbranch' 681 /// 'simdlen' '(' <expr> ')' 682 /// { 'uniform' '(' <argument_list> ')' } 683 /// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' } 684 /// { 'linear '(' <argument_list> [ ':' <step> ] ')' } 685 static bool parseDeclareSimdClauses( 686 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen, 687 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds, 688 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears, 689 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) { 690 SourceRange BSRange; 691 const Token &Tok = P.getCurToken(); 692 bool IsError = false; 693 while (Tok.isNot(tok::annot_pragma_openmp_end)) { 694 if (Tok.isNot(tok::identifier)) 695 break; 696 OMPDeclareSimdDeclAttr::BranchStateTy Out; 697 IdentifierInfo *II = Tok.getIdentifierInfo(); 698 StringRef ClauseName = II->getName(); 699 // Parse 'inranch|notinbranch' clauses. 700 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) { 701 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) { 702 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch) 703 << ClauseName 704 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange; 705 IsError = true; 706 } 707 BS = Out; 708 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc()); 709 P.ConsumeToken(); 710 } else if (ClauseName.equals("simdlen")) { 711 if (SimdLen.isUsable()) { 712 P.Diag(Tok, diag::err_omp_more_one_clause) 713 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0; 714 IsError = true; 715 } 716 P.ConsumeToken(); 717 SourceLocation RLoc; 718 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc); 719 if (SimdLen.isInvalid()) 720 IsError = true; 721 } else { 722 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName); 723 if (CKind == OMPC_uniform || CKind == OMPC_aligned || 724 CKind == OMPC_linear) { 725 Parser::OpenMPVarListDataTy Data; 726 SmallVectorImpl<Expr *> *Vars = &Uniforms; 727 if (CKind == OMPC_aligned) 728 Vars = &Aligneds; 729 else if (CKind == OMPC_linear) 730 Vars = &Linears; 731 732 P.ConsumeToken(); 733 if (P.ParseOpenMPVarList(OMPD_declare_simd, 734 getOpenMPClauseKind(ClauseName), *Vars, Data)) 735 IsError = true; 736 if (CKind == OMPC_aligned) { 737 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr); 738 } else if (CKind == OMPC_linear) { 739 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind, 740 Data.DepLinMapLoc)) 741 Data.LinKind = OMPC_LINEAR_val; 742 LinModifiers.append(Linears.size() - LinModifiers.size(), 743 Data.LinKind); 744 Steps.append(Linears.size() - Steps.size(), Data.TailExpr); 745 } 746 } else 747 // TODO: add parsing of other clauses. 748 break; 749 } 750 // Skip ',' if any. 751 if (Tok.is(tok::comma)) 752 P.ConsumeToken(); 753 } 754 return IsError; 755 } 756 757 /// Parse clauses for '#pragma omp declare simd'. 758 Parser::DeclGroupPtrTy 759 Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr, 760 CachedTokens &Toks, SourceLocation Loc) { 761 PP.EnterToken(Tok, /*IsReinject*/ true); 762 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true, 763 /*IsReinject*/ true); 764 // Consume the previously pushed token. 765 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); 766 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); 767 768 FNContextRAII FnContext(*this, Ptr); 769 OMPDeclareSimdDeclAttr::BranchStateTy BS = 770 OMPDeclareSimdDeclAttr::BS_Undefined; 771 ExprResult Simdlen; 772 SmallVector<Expr *, 4> Uniforms; 773 SmallVector<Expr *, 4> Aligneds; 774 SmallVector<Expr *, 4> Alignments; 775 SmallVector<Expr *, 4> Linears; 776 SmallVector<unsigned, 4> LinModifiers; 777 SmallVector<Expr *, 4> Steps; 778 bool IsError = 779 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds, 780 Alignments, Linears, LinModifiers, Steps); 781 // Need to check for extra tokens. 782 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 783 Diag(Tok, diag::warn_omp_extra_tokens_at_eol) 784 << getOpenMPDirectiveName(OMPD_declare_simd); 785 while (Tok.isNot(tok::annot_pragma_openmp_end)) 786 ConsumeAnyToken(); 787 } 788 // Skip the last annot_pragma_openmp_end. 789 SourceLocation EndLoc = ConsumeAnnotationToken(); 790 if (IsError) 791 return Ptr; 792 return Actions.ActOnOpenMPDeclareSimdDirective( 793 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears, 794 LinModifiers, Steps, SourceRange(Loc, EndLoc)); 795 } 796 797 /// Parse optional 'score' '(' <expr> ')' ':'. 798 static ExprResult parseContextScore(Parser &P) { 799 ExprResult ScoreExpr; 800 SmallString<16> Buffer; 801 StringRef SelectorName = 802 P.getPreprocessor().getSpelling(P.getCurToken(), Buffer); 803 if (!SelectorName.equals("score")) 804 return ScoreExpr; 805 (void)P.ConsumeToken(); 806 SourceLocation RLoc; 807 ScoreExpr = P.ParseOpenMPParensExpr(SelectorName, RLoc); 808 // Parse ':' 809 if (P.getCurToken().is(tok::colon)) 810 (void)P.ConsumeAnyToken(); 811 else 812 P.Diag(P.getCurToken(), diag::warn_pragma_expected_colon) 813 << "context selector score clause"; 814 return ScoreExpr; 815 } 816 817 /// Parse context selector for 'implementation' selector set: 818 /// 'vendor' '(' [ 'score' '(' <score _expr> ')' ':' ] <vendor> { ',' <vendor> } 819 /// ')' 820 static void parseImplementationSelector( 821 Parser &P, SourceLocation Loc, llvm::StringMap<SourceLocation> &UsedCtx, 822 llvm::function_ref<void(SourceRange, 823 const Sema::OpenMPDeclareVariantCtsSelectorData &)> 824 Callback) { 825 const Token &Tok = P.getCurToken(); 826 // Parse inner context selector set name, if any. 827 if (!Tok.is(tok::identifier)) { 828 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected) 829 << "implementation"; 830 // Skip until either '}', ')', or end of directive. 831 while (!P.SkipUntil(tok::r_brace, tok::r_paren, 832 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch)) 833 ; 834 return; 835 } 836 SmallString<16> Buffer; 837 StringRef CtxSelectorName = P.getPreprocessor().getSpelling(Tok, Buffer); 838 auto Res = UsedCtx.try_emplace(CtxSelectorName, Tok.getLocation()); 839 if (!Res.second) { 840 // OpenMP 5.0, 2.3.2 Context Selectors, Restrictions. 841 // Each trait-selector-name can only be specified once. 842 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_ctx_mutiple_use) 843 << CtxSelectorName << "implementation"; 844 P.Diag(Res.first->getValue(), diag::note_omp_declare_variant_ctx_used_here) 845 << CtxSelectorName; 846 } 847 OMPDeclareVariantAttr::CtxSelectorType CSKind = 848 OMPDeclareVariantAttr::CtxUnknown; 849 (void)OMPDeclareVariantAttr::ConvertStrToCtxSelectorType(CtxSelectorName, 850 CSKind); 851 (void)P.ConsumeToken(); 852 switch (CSKind) { 853 case OMPDeclareVariantAttr::CtxVendor: { 854 // Parse '('. 855 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end); 856 (void)T.expectAndConsume(diag::err_expected_lparen_after, 857 CtxSelectorName.data()); 858 const ExprResult Score = parseContextScore(P); 859 llvm::UniqueVector<llvm::SmallString<16>> Vendors; 860 do { 861 // Parse <vendor>. 862 StringRef VendorName; 863 if (Tok.is(tok::identifier)) { 864 Buffer.clear(); 865 VendorName = P.getPreprocessor().getSpelling(P.getCurToken(), Buffer); 866 (void)P.ConsumeToken(); 867 if (!VendorName.empty()) 868 Vendors.insert(VendorName); 869 } else { 870 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_item_expected) 871 << "vendor identifier" 872 << "vendor" 873 << "implementation"; 874 } 875 if (!P.TryConsumeToken(tok::comma) && Tok.isNot(tok::r_paren)) { 876 P.Diag(Tok, diag::err_expected_punc) 877 << (VendorName.empty() ? "vendor name" : VendorName); 878 } 879 } while (Tok.is(tok::identifier)); 880 // Parse ')'. 881 (void)T.consumeClose(); 882 if (!Vendors.empty()) { 883 SmallVector<StringRef, 4> ImplVendors(Vendors.size()); 884 llvm::copy(Vendors, ImplVendors.begin()); 885 Sema::OpenMPDeclareVariantCtsSelectorData Data( 886 OMPDeclareVariantAttr::CtxSetImplementation, CSKind, 887 llvm::makeMutableArrayRef(ImplVendors.begin(), ImplVendors.size()), 888 Score); 889 Callback(SourceRange(Loc, Tok.getLocation()), Data); 890 } 891 break; 892 } 893 case OMPDeclareVariantAttr::CtxUnknown: 894 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected) 895 << "implementation"; 896 // Skip until either '}', ')', or end of directive. 897 while (!P.SkipUntil(tok::r_brace, tok::r_paren, 898 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch)) 899 ; 900 return; 901 } 902 } 903 904 /// Parses clauses for 'declare variant' directive. 905 /// clause: 906 /// <selector_set_name> '=' '{' <context_selectors> '}' 907 /// [ ',' <selector_set_name> '=' '{' <context_selectors> '}' ] 908 bool Parser::parseOpenMPContextSelectors( 909 SourceLocation Loc, 910 llvm::function_ref<void(SourceRange, 911 const Sema::OpenMPDeclareVariantCtsSelectorData &)> 912 Callback) { 913 llvm::StringMap<SourceLocation> UsedCtxSets; 914 do { 915 // Parse inner context selector set name. 916 if (!Tok.is(tok::identifier)) { 917 Diag(Tok.getLocation(), diag::err_omp_declare_variant_no_ctx_selector) 918 << getOpenMPClauseName(OMPC_match); 919 return true; 920 } 921 SmallString<16> Buffer; 922 StringRef CtxSelectorSetName = PP.getSpelling(Tok, Buffer); 923 auto Res = UsedCtxSets.try_emplace(CtxSelectorSetName, Tok.getLocation()); 924 if (!Res.second) { 925 // OpenMP 5.0, 2.3.2 Context Selectors, Restrictions. 926 // Each trait-set-selector-name can only be specified once. 927 Diag(Tok.getLocation(), diag::err_omp_declare_variant_ctx_set_mutiple_use) 928 << CtxSelectorSetName; 929 Diag(Res.first->getValue(), 930 diag::note_omp_declare_variant_ctx_set_used_here) 931 << CtxSelectorSetName; 932 } 933 // Parse '='. 934 (void)ConsumeToken(); 935 if (Tok.isNot(tok::equal)) { 936 Diag(Tok.getLocation(), diag::err_omp_declare_variant_equal_expected) 937 << CtxSelectorSetName; 938 return true; 939 } 940 (void)ConsumeToken(); 941 // TBD: add parsing of known context selectors. 942 // Unknown selector - just ignore it completely. 943 { 944 // Parse '{'. 945 BalancedDelimiterTracker TBr(*this, tok::l_brace, 946 tok::annot_pragma_openmp_end); 947 if (TBr.expectAndConsume(diag::err_expected_lbrace_after, "=")) 948 return true; 949 OMPDeclareVariantAttr::CtxSelectorSetType CSSKind = 950 OMPDeclareVariantAttr::CtxSetUnknown; 951 (void)OMPDeclareVariantAttr::ConvertStrToCtxSelectorSetType( 952 CtxSelectorSetName, CSSKind); 953 llvm::StringMap<SourceLocation> UsedCtx; 954 do { 955 switch (CSSKind) { 956 case OMPDeclareVariantAttr::CtxSetImplementation: 957 parseImplementationSelector(*this, Loc, UsedCtx, Callback); 958 break; 959 case OMPDeclareVariantAttr::CtxSetUnknown: 960 // Skip until either '}', ')', or end of directive. 961 while (!SkipUntil(tok::r_brace, tok::r_paren, 962 tok::annot_pragma_openmp_end, StopBeforeMatch)) 963 ; 964 break; 965 } 966 const Token PrevTok = Tok; 967 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace)) 968 Diag(Tok, diag::err_omp_expected_comma_brace) 969 << (PrevTok.isAnnotation() ? "context selector trait" 970 : PP.getSpelling(PrevTok)); 971 } while (Tok.is(tok::identifier)); 972 // Parse '}'. 973 (void)TBr.consumeClose(); 974 } 975 // Consume ',' 976 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) 977 (void)ExpectAndConsume(tok::comma); 978 } while (Tok.isAnyIdentifier()); 979 return false; 980 } 981 982 /// Parse clauses for '#pragma omp declare variant ( variant-func-id ) clause'. 983 void Parser::ParseOMPDeclareVariantClauses(Parser::DeclGroupPtrTy Ptr, 984 CachedTokens &Toks, 985 SourceLocation Loc) { 986 PP.EnterToken(Tok, /*IsReinject*/ true); 987 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true, 988 /*IsReinject*/ true); 989 // Consume the previously pushed token. 990 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); 991 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); 992 993 FNContextRAII FnContext(*this, Ptr); 994 // Parse function declaration id. 995 SourceLocation RLoc; 996 // Parse with IsAddressOfOperand set to true to parse methods as DeclRefExprs 997 // instead of MemberExprs. 998 ExprResult AssociatedFunction = 999 ParseOpenMPParensExpr(getOpenMPDirectiveName(OMPD_declare_variant), RLoc, 1000 /*IsAddressOfOperand=*/true); 1001 if (!AssociatedFunction.isUsable()) { 1002 if (!Tok.is(tok::annot_pragma_openmp_end)) 1003 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch)) 1004 ; 1005 // Skip the last annot_pragma_openmp_end. 1006 (void)ConsumeAnnotationToken(); 1007 return; 1008 } 1009 Optional<std::pair<FunctionDecl *, Expr *>> DeclVarData = 1010 Actions.checkOpenMPDeclareVariantFunction( 1011 Ptr, AssociatedFunction.get(), SourceRange(Loc, Tok.getLocation())); 1012 1013 // Parse 'match'. 1014 OpenMPClauseKind CKind = Tok.isAnnotation() 1015 ? OMPC_unknown 1016 : getOpenMPClauseKind(PP.getSpelling(Tok)); 1017 if (CKind != OMPC_match) { 1018 Diag(Tok.getLocation(), diag::err_omp_declare_variant_wrong_clause) 1019 << getOpenMPClauseName(OMPC_match); 1020 while (!SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch)) 1021 ; 1022 // Skip the last annot_pragma_openmp_end. 1023 (void)ConsumeAnnotationToken(); 1024 return; 1025 } 1026 (void)ConsumeToken(); 1027 // Parse '('. 1028 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); 1029 if (T.expectAndConsume(diag::err_expected_lparen_after, 1030 getOpenMPClauseName(OMPC_match))) { 1031 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch)) 1032 ; 1033 // Skip the last annot_pragma_openmp_end. 1034 (void)ConsumeAnnotationToken(); 1035 return; 1036 } 1037 1038 // Parse inner context selectors. 1039 if (!parseOpenMPContextSelectors( 1040 Loc, [this, &DeclVarData]( 1041 SourceRange SR, 1042 const Sema::OpenMPDeclareVariantCtsSelectorData &Data) { 1043 if (DeclVarData.hasValue()) 1044 Actions.ActOnOpenMPDeclareVariantDirective( 1045 DeclVarData.getValue().first, DeclVarData.getValue().second, 1046 SR, Data); 1047 })) { 1048 // Parse ')'. 1049 (void)T.consumeClose(); 1050 // Need to check for extra tokens. 1051 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 1052 Diag(Tok, diag::warn_omp_extra_tokens_at_eol) 1053 << getOpenMPDirectiveName(OMPD_declare_variant); 1054 } 1055 } 1056 1057 // Skip last tokens. 1058 while (Tok.isNot(tok::annot_pragma_openmp_end)) 1059 ConsumeAnyToken(); 1060 // Skip the last annot_pragma_openmp_end. 1061 (void)ConsumeAnnotationToken(); 1062 } 1063 1064 /// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'. 1065 /// 1066 /// default-clause: 1067 /// 'default' '(' 'none' | 'shared' ') 1068 /// 1069 /// proc_bind-clause: 1070 /// 'proc_bind' '(' 'master' | 'close' | 'spread' ') 1071 /// 1072 /// device_type-clause: 1073 /// 'device_type' '(' 'host' | 'nohost' | 'any' )' 1074 namespace { 1075 struct SimpleClauseData { 1076 unsigned Type; 1077 SourceLocation Loc; 1078 SourceLocation LOpen; 1079 SourceLocation TypeLoc; 1080 SourceLocation RLoc; 1081 SimpleClauseData(unsigned Type, SourceLocation Loc, SourceLocation LOpen, 1082 SourceLocation TypeLoc, SourceLocation RLoc) 1083 : Type(Type), Loc(Loc), LOpen(LOpen), TypeLoc(TypeLoc), RLoc(RLoc) {} 1084 }; 1085 } // anonymous namespace 1086 1087 static Optional<SimpleClauseData> 1088 parseOpenMPSimpleClause(Parser &P, OpenMPClauseKind Kind) { 1089 const Token &Tok = P.getCurToken(); 1090 SourceLocation Loc = Tok.getLocation(); 1091 SourceLocation LOpen = P.ConsumeToken(); 1092 // Parse '('. 1093 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end); 1094 if (T.expectAndConsume(diag::err_expected_lparen_after, 1095 getOpenMPClauseName(Kind))) 1096 return llvm::None; 1097 1098 unsigned Type = getOpenMPSimpleClauseType( 1099 Kind, Tok.isAnnotation() ? "" : P.getPreprocessor().getSpelling(Tok)); 1100 SourceLocation TypeLoc = Tok.getLocation(); 1101 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 1102 Tok.isNot(tok::annot_pragma_openmp_end)) 1103 P.ConsumeAnyToken(); 1104 1105 // Parse ')'. 1106 SourceLocation RLoc = Tok.getLocation(); 1107 if (!T.consumeClose()) 1108 RLoc = T.getCloseLocation(); 1109 1110 return SimpleClauseData(Type, Loc, LOpen, TypeLoc, RLoc); 1111 } 1112 1113 Parser::DeclGroupPtrTy Parser::ParseOMPDeclareTargetClauses() { 1114 // OpenMP 4.5 syntax with list of entities. 1115 Sema::NamedDeclSetType SameDirectiveDecls; 1116 SmallVector<std::tuple<OMPDeclareTargetDeclAttr::MapTypeTy, SourceLocation, 1117 NamedDecl *>, 1118 4> 1119 DeclareTargetDecls; 1120 OMPDeclareTargetDeclAttr::DevTypeTy DT = OMPDeclareTargetDeclAttr::DT_Any; 1121 SourceLocation DeviceTypeLoc; 1122 while (Tok.isNot(tok::annot_pragma_openmp_end)) { 1123 OMPDeclareTargetDeclAttr::MapTypeTy MT = OMPDeclareTargetDeclAttr::MT_To; 1124 if (Tok.is(tok::identifier)) { 1125 IdentifierInfo *II = Tok.getIdentifierInfo(); 1126 StringRef ClauseName = II->getName(); 1127 bool IsDeviceTypeClause = 1128 getLangOpts().OpenMP >= 50 && 1129 getOpenMPClauseKind(ClauseName) == OMPC_device_type; 1130 // Parse 'to|link|device_type' clauses. 1131 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName, MT) && 1132 !IsDeviceTypeClause) { 1133 Diag(Tok, diag::err_omp_declare_target_unexpected_clause) 1134 << ClauseName << (getLangOpts().OpenMP >= 50 ? 1 : 0); 1135 break; 1136 } 1137 // Parse 'device_type' clause and go to next clause if any. 1138 if (IsDeviceTypeClause) { 1139 Optional<SimpleClauseData> DevTypeData = 1140 parseOpenMPSimpleClause(*this, OMPC_device_type); 1141 if (DevTypeData.hasValue()) { 1142 if (DeviceTypeLoc.isValid()) { 1143 // We already saw another device_type clause, diagnose it. 1144 Diag(DevTypeData.getValue().Loc, 1145 diag::warn_omp_more_one_device_type_clause); 1146 } 1147 switch(static_cast<OpenMPDeviceType>(DevTypeData.getValue().Type)) { 1148 case OMPC_DEVICE_TYPE_any: 1149 DT = OMPDeclareTargetDeclAttr::DT_Any; 1150 break; 1151 case OMPC_DEVICE_TYPE_host: 1152 DT = OMPDeclareTargetDeclAttr::DT_Host; 1153 break; 1154 case OMPC_DEVICE_TYPE_nohost: 1155 DT = OMPDeclareTargetDeclAttr::DT_NoHost; 1156 break; 1157 case OMPC_DEVICE_TYPE_unknown: 1158 llvm_unreachable("Unexpected device_type"); 1159 } 1160 DeviceTypeLoc = DevTypeData.getValue().Loc; 1161 } 1162 continue; 1163 } 1164 ConsumeToken(); 1165 } 1166 auto &&Callback = [this, MT, &DeclareTargetDecls, &SameDirectiveDecls]( 1167 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) { 1168 NamedDecl *ND = Actions.lookupOpenMPDeclareTargetName( 1169 getCurScope(), SS, NameInfo, SameDirectiveDecls); 1170 if (ND) 1171 DeclareTargetDecls.emplace_back(MT, NameInfo.getLoc(), ND); 1172 }; 1173 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, 1174 /*AllowScopeSpecifier=*/true)) 1175 break; 1176 1177 // Consume optional ','. 1178 if (Tok.is(tok::comma)) 1179 ConsumeToken(); 1180 } 1181 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); 1182 ConsumeAnyToken(); 1183 for (auto &MTLocDecl : DeclareTargetDecls) { 1184 OMPDeclareTargetDeclAttr::MapTypeTy MT; 1185 SourceLocation Loc; 1186 NamedDecl *ND; 1187 std::tie(MT, Loc, ND) = MTLocDecl; 1188 // device_type clause is applied only to functions. 1189 Actions.ActOnOpenMPDeclareTargetName( 1190 ND, Loc, MT, isa<VarDecl>(ND) ? OMPDeclareTargetDeclAttr::DT_Any : DT); 1191 } 1192 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(), 1193 SameDirectiveDecls.end()); 1194 if (Decls.empty()) 1195 return DeclGroupPtrTy(); 1196 return Actions.BuildDeclaratorGroup(Decls); 1197 } 1198 1199 void Parser::ParseOMPEndDeclareTargetDirective(OpenMPDirectiveKind DKind, 1200 SourceLocation DTLoc) { 1201 if (DKind != OMPD_end_declare_target) { 1202 Diag(Tok, diag::err_expected_end_declare_target); 1203 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'"; 1204 return; 1205 } 1206 ConsumeAnyToken(); 1207 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 1208 Diag(Tok, diag::warn_omp_extra_tokens_at_eol) 1209 << getOpenMPDirectiveName(OMPD_end_declare_target); 1210 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); 1211 } 1212 // Skip the last annot_pragma_openmp_end. 1213 ConsumeAnyToken(); 1214 } 1215 1216 /// Parsing of declarative OpenMP directives. 1217 /// 1218 /// threadprivate-directive: 1219 /// annot_pragma_openmp 'threadprivate' simple-variable-list 1220 /// annot_pragma_openmp_end 1221 /// 1222 /// allocate-directive: 1223 /// annot_pragma_openmp 'allocate' simple-variable-list [<clause>] 1224 /// annot_pragma_openmp_end 1225 /// 1226 /// declare-reduction-directive: 1227 /// annot_pragma_openmp 'declare' 'reduction' [...] 1228 /// annot_pragma_openmp_end 1229 /// 1230 /// declare-mapper-directive: 1231 /// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':'] 1232 /// <type> <var> ')' [<clause>[[,] <clause>] ... ] 1233 /// annot_pragma_openmp_end 1234 /// 1235 /// declare-simd-directive: 1236 /// annot_pragma_openmp 'declare simd' {<clause> [,]} 1237 /// annot_pragma_openmp_end 1238 /// <function declaration/definition> 1239 /// 1240 /// requires directive: 1241 /// annot_pragma_openmp 'requires' <clause> [[[,] <clause>] ... ] 1242 /// annot_pragma_openmp_end 1243 /// 1244 Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl( 1245 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs, 1246 DeclSpec::TST TagType, Decl *Tag) { 1247 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!"); 1248 ParenBraceBracketBalancer BalancerRAIIObj(*this); 1249 1250 SourceLocation Loc = ConsumeAnnotationToken(); 1251 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this); 1252 1253 switch (DKind) { 1254 case OMPD_threadprivate: { 1255 ConsumeToken(); 1256 DeclDirectiveListParserHelper Helper(this, DKind); 1257 if (!ParseOpenMPSimpleVarList(DKind, Helper, 1258 /*AllowScopeSpecifier=*/true)) { 1259 // The last seen token is annot_pragma_openmp_end - need to check for 1260 // extra tokens. 1261 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 1262 Diag(Tok, diag::warn_omp_extra_tokens_at_eol) 1263 << getOpenMPDirectiveName(DKind); 1264 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); 1265 } 1266 // Skip the last annot_pragma_openmp_end. 1267 ConsumeAnnotationToken(); 1268 return Actions.ActOnOpenMPThreadprivateDirective(Loc, 1269 Helper.getIdentifiers()); 1270 } 1271 break; 1272 } 1273 case OMPD_allocate: { 1274 ConsumeToken(); 1275 DeclDirectiveListParserHelper Helper(this, DKind); 1276 if (!ParseOpenMPSimpleVarList(DKind, Helper, 1277 /*AllowScopeSpecifier=*/true)) { 1278 SmallVector<OMPClause *, 1> Clauses; 1279 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 1280 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, 1281 OMPC_unknown + 1> 1282 FirstClauses(OMPC_unknown + 1); 1283 while (Tok.isNot(tok::annot_pragma_openmp_end)) { 1284 OpenMPClauseKind CKind = 1285 Tok.isAnnotation() ? OMPC_unknown 1286 : getOpenMPClauseKind(PP.getSpelling(Tok)); 1287 Actions.StartOpenMPClause(CKind); 1288 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind, 1289 !FirstClauses[CKind].getInt()); 1290 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end, 1291 StopBeforeMatch); 1292 FirstClauses[CKind].setInt(true); 1293 if (Clause != nullptr) 1294 Clauses.push_back(Clause); 1295 if (Tok.is(tok::annot_pragma_openmp_end)) { 1296 Actions.EndOpenMPClause(); 1297 break; 1298 } 1299 // Skip ',' if any. 1300 if (Tok.is(tok::comma)) 1301 ConsumeToken(); 1302 Actions.EndOpenMPClause(); 1303 } 1304 // The last seen token is annot_pragma_openmp_end - need to check for 1305 // extra tokens. 1306 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 1307 Diag(Tok, diag::warn_omp_extra_tokens_at_eol) 1308 << getOpenMPDirectiveName(DKind); 1309 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); 1310 } 1311 } 1312 // Skip the last annot_pragma_openmp_end. 1313 ConsumeAnnotationToken(); 1314 return Actions.ActOnOpenMPAllocateDirective(Loc, Helper.getIdentifiers(), 1315 Clauses); 1316 } 1317 break; 1318 } 1319 case OMPD_requires: { 1320 SourceLocation StartLoc = ConsumeToken(); 1321 SmallVector<OMPClause *, 5> Clauses; 1322 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1> 1323 FirstClauses(OMPC_unknown + 1); 1324 if (Tok.is(tok::annot_pragma_openmp_end)) { 1325 Diag(Tok, diag::err_omp_expected_clause) 1326 << getOpenMPDirectiveName(OMPD_requires); 1327 break; 1328 } 1329 while (Tok.isNot(tok::annot_pragma_openmp_end)) { 1330 OpenMPClauseKind CKind = Tok.isAnnotation() 1331 ? OMPC_unknown 1332 : getOpenMPClauseKind(PP.getSpelling(Tok)); 1333 Actions.StartOpenMPClause(CKind); 1334 OMPClause *Clause = ParseOpenMPClause(OMPD_requires, CKind, 1335 !FirstClauses[CKind].getInt()); 1336 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end, 1337 StopBeforeMatch); 1338 FirstClauses[CKind].setInt(true); 1339 if (Clause != nullptr) 1340 Clauses.push_back(Clause); 1341 if (Tok.is(tok::annot_pragma_openmp_end)) { 1342 Actions.EndOpenMPClause(); 1343 break; 1344 } 1345 // Skip ',' if any. 1346 if (Tok.is(tok::comma)) 1347 ConsumeToken(); 1348 Actions.EndOpenMPClause(); 1349 } 1350 // Consume final annot_pragma_openmp_end 1351 if (Clauses.size() == 0) { 1352 Diag(Tok, diag::err_omp_expected_clause) 1353 << getOpenMPDirectiveName(OMPD_requires); 1354 ConsumeAnnotationToken(); 1355 return nullptr; 1356 } 1357 ConsumeAnnotationToken(); 1358 return Actions.ActOnOpenMPRequiresDirective(StartLoc, Clauses); 1359 } 1360 case OMPD_declare_reduction: 1361 ConsumeToken(); 1362 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) { 1363 // The last seen token is annot_pragma_openmp_end - need to check for 1364 // extra tokens. 1365 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 1366 Diag(Tok, diag::warn_omp_extra_tokens_at_eol) 1367 << getOpenMPDirectiveName(OMPD_declare_reduction); 1368 while (Tok.isNot(tok::annot_pragma_openmp_end)) 1369 ConsumeAnyToken(); 1370 } 1371 // Skip the last annot_pragma_openmp_end. 1372 ConsumeAnnotationToken(); 1373 return Res; 1374 } 1375 break; 1376 case OMPD_declare_mapper: { 1377 ConsumeToken(); 1378 if (DeclGroupPtrTy Res = ParseOpenMPDeclareMapperDirective(AS)) { 1379 // Skip the last annot_pragma_openmp_end. 1380 ConsumeAnnotationToken(); 1381 return Res; 1382 } 1383 break; 1384 } 1385 case OMPD_declare_variant: 1386 case OMPD_declare_simd: { 1387 // The syntax is: 1388 // { #pragma omp declare {simd|variant} } 1389 // <function-declaration-or-definition> 1390 // 1391 CachedTokens Toks; 1392 Toks.push_back(Tok); 1393 ConsumeToken(); 1394 while(Tok.isNot(tok::annot_pragma_openmp_end)) { 1395 Toks.push_back(Tok); 1396 ConsumeAnyToken(); 1397 } 1398 Toks.push_back(Tok); 1399 ConsumeAnyToken(); 1400 1401 DeclGroupPtrTy Ptr; 1402 if (Tok.is(tok::annot_pragma_openmp)) { 1403 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag); 1404 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) { 1405 // Here we expect to see some function declaration. 1406 if (AS == AS_none) { 1407 assert(TagType == DeclSpec::TST_unspecified); 1408 MaybeParseCXX11Attributes(Attrs); 1409 ParsingDeclSpec PDS(*this); 1410 Ptr = ParseExternalDeclaration(Attrs, &PDS); 1411 } else { 1412 Ptr = 1413 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag); 1414 } 1415 } 1416 if (!Ptr) { 1417 Diag(Loc, diag::err_omp_decl_in_declare_simd_variant) 1418 << (DKind == OMPD_declare_simd ? 0 : 1); 1419 return DeclGroupPtrTy(); 1420 } 1421 if (DKind == OMPD_declare_simd) 1422 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc); 1423 assert(DKind == OMPD_declare_variant && 1424 "Expected declare variant directive only"); 1425 ParseOMPDeclareVariantClauses(Ptr, Toks, Loc); 1426 return Ptr; 1427 } 1428 case OMPD_declare_target: { 1429 SourceLocation DTLoc = ConsumeAnyToken(); 1430 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 1431 return ParseOMPDeclareTargetClauses(); 1432 } 1433 1434 // Skip the last annot_pragma_openmp_end. 1435 ConsumeAnyToken(); 1436 1437 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc)) 1438 return DeclGroupPtrTy(); 1439 1440 llvm::SmallVector<Decl *, 4> Decls; 1441 DKind = parseOpenMPDirectiveKind(*this); 1442 while (DKind != OMPD_end_declare_target && Tok.isNot(tok::eof) && 1443 Tok.isNot(tok::r_brace)) { 1444 DeclGroupPtrTy Ptr; 1445 // Here we expect to see some function declaration. 1446 if (AS == AS_none) { 1447 assert(TagType == DeclSpec::TST_unspecified); 1448 MaybeParseCXX11Attributes(Attrs); 1449 ParsingDeclSpec PDS(*this); 1450 Ptr = ParseExternalDeclaration(Attrs, &PDS); 1451 } else { 1452 Ptr = 1453 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag); 1454 } 1455 if (Ptr) { 1456 DeclGroupRef Ref = Ptr.get(); 1457 Decls.append(Ref.begin(), Ref.end()); 1458 } 1459 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) { 1460 TentativeParsingAction TPA(*this); 1461 ConsumeAnnotationToken(); 1462 DKind = parseOpenMPDirectiveKind(*this); 1463 if (DKind != OMPD_end_declare_target) 1464 TPA.Revert(); 1465 else 1466 TPA.Commit(); 1467 } 1468 } 1469 1470 ParseOMPEndDeclareTargetDirective(DKind, DTLoc); 1471 Actions.ActOnFinishOpenMPDeclareTargetDirective(); 1472 return Actions.BuildDeclaratorGroup(Decls); 1473 } 1474 case OMPD_unknown: 1475 Diag(Tok, diag::err_omp_unknown_directive); 1476 break; 1477 case OMPD_parallel: 1478 case OMPD_simd: 1479 case OMPD_task: 1480 case OMPD_taskyield: 1481 case OMPD_barrier: 1482 case OMPD_taskwait: 1483 case OMPD_taskgroup: 1484 case OMPD_flush: 1485 case OMPD_for: 1486 case OMPD_for_simd: 1487 case OMPD_sections: 1488 case OMPD_section: 1489 case OMPD_single: 1490 case OMPD_master: 1491 case OMPD_ordered: 1492 case OMPD_critical: 1493 case OMPD_parallel_for: 1494 case OMPD_parallel_for_simd: 1495 case OMPD_parallel_sections: 1496 case OMPD_atomic: 1497 case OMPD_target: 1498 case OMPD_teams: 1499 case OMPD_cancellation_point: 1500 case OMPD_cancel: 1501 case OMPD_target_data: 1502 case OMPD_target_enter_data: 1503 case OMPD_target_exit_data: 1504 case OMPD_target_parallel: 1505 case OMPD_target_parallel_for: 1506 case OMPD_taskloop: 1507 case OMPD_taskloop_simd: 1508 case OMPD_master_taskloop: 1509 case OMPD_master_taskloop_simd: 1510 case OMPD_parallel_master_taskloop: 1511 case OMPD_parallel_master_taskloop_simd: 1512 case OMPD_distribute: 1513 case OMPD_end_declare_target: 1514 case OMPD_target_update: 1515 case OMPD_distribute_parallel_for: 1516 case OMPD_distribute_parallel_for_simd: 1517 case OMPD_distribute_simd: 1518 case OMPD_target_parallel_for_simd: 1519 case OMPD_target_simd: 1520 case OMPD_teams_distribute: 1521 case OMPD_teams_distribute_simd: 1522 case OMPD_teams_distribute_parallel_for_simd: 1523 case OMPD_teams_distribute_parallel_for: 1524 case OMPD_target_teams: 1525 case OMPD_target_teams_distribute: 1526 case OMPD_target_teams_distribute_parallel_for: 1527 case OMPD_target_teams_distribute_parallel_for_simd: 1528 case OMPD_target_teams_distribute_simd: 1529 Diag(Tok, diag::err_omp_unexpected_directive) 1530 << 1 << getOpenMPDirectiveName(DKind); 1531 break; 1532 } 1533 while (Tok.isNot(tok::annot_pragma_openmp_end)) 1534 ConsumeAnyToken(); 1535 ConsumeAnyToken(); 1536 return nullptr; 1537 } 1538 1539 /// Parsing of declarative or executable OpenMP directives. 1540 /// 1541 /// threadprivate-directive: 1542 /// annot_pragma_openmp 'threadprivate' simple-variable-list 1543 /// annot_pragma_openmp_end 1544 /// 1545 /// allocate-directive: 1546 /// annot_pragma_openmp 'allocate' simple-variable-list 1547 /// annot_pragma_openmp_end 1548 /// 1549 /// declare-reduction-directive: 1550 /// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':' 1551 /// <type> {',' <type>} ':' <expression> ')' ['initializer' '(' 1552 /// ('omp_priv' '=' <expression>|<function_call>) ')'] 1553 /// annot_pragma_openmp_end 1554 /// 1555 /// declare-mapper-directive: 1556 /// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':'] 1557 /// <type> <var> ')' [<clause>[[,] <clause>] ... ] 1558 /// annot_pragma_openmp_end 1559 /// 1560 /// executable-directive: 1561 /// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' | 1562 /// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] | 1563 /// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' | 1564 /// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' | 1565 /// 'for simd' | 'parallel for simd' | 'target' | 'target data' | 1566 /// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' | 'master 1567 /// taskloop' | 'master taskloop simd' | 'parallel master taskloop' | 1568 /// 'parallel master taskloop simd' | 'distribute' | 'target enter data' 1569 /// | 'target exit data' | 'target parallel' | 'target parallel for' | 1570 /// 'target update' | 'distribute parallel for' | 'distribute paralle 1571 /// for simd' | 'distribute simd' | 'target parallel for simd' | 'target 1572 /// simd' | 'teams distribute' | 'teams distribute simd' | 'teams 1573 /// distribute parallel for simd' | 'teams distribute parallel for' | 1574 /// 'target teams' | 'target teams distribute' | 'target teams 1575 /// distribute parallel for' | 'target teams distribute parallel for 1576 /// simd' | 'target teams distribute simd' {clause} 1577 /// annot_pragma_openmp_end 1578 /// 1579 StmtResult 1580 Parser::ParseOpenMPDeclarativeOrExecutableDirective(ParsedStmtContext StmtCtx) { 1581 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!"); 1582 ParenBraceBracketBalancer BalancerRAIIObj(*this); 1583 SmallVector<OMPClause *, 5> Clauses; 1584 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1> 1585 FirstClauses(OMPC_unknown + 1); 1586 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope | 1587 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope; 1588 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc; 1589 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this); 1590 OpenMPDirectiveKind CancelRegion = OMPD_unknown; 1591 // Name of critical directive. 1592 DeclarationNameInfo DirName; 1593 StmtResult Directive = StmtError(); 1594 bool HasAssociatedStatement = true; 1595 bool FlushHasClause = false; 1596 1597 switch (DKind) { 1598 case OMPD_threadprivate: { 1599 // FIXME: Should this be permitted in C++? 1600 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) == 1601 ParsedStmtContext()) { 1602 Diag(Tok, diag::err_omp_immediate_directive) 1603 << getOpenMPDirectiveName(DKind) << 0; 1604 } 1605 ConsumeToken(); 1606 DeclDirectiveListParserHelper Helper(this, DKind); 1607 if (!ParseOpenMPSimpleVarList(DKind, Helper, 1608 /*AllowScopeSpecifier=*/false)) { 1609 // The last seen token is annot_pragma_openmp_end - need to check for 1610 // extra tokens. 1611 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 1612 Diag(Tok, diag::warn_omp_extra_tokens_at_eol) 1613 << getOpenMPDirectiveName(DKind); 1614 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); 1615 } 1616 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective( 1617 Loc, Helper.getIdentifiers()); 1618 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation()); 1619 } 1620 SkipUntil(tok::annot_pragma_openmp_end); 1621 break; 1622 } 1623 case OMPD_allocate: { 1624 // FIXME: Should this be permitted in C++? 1625 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) == 1626 ParsedStmtContext()) { 1627 Diag(Tok, diag::err_omp_immediate_directive) 1628 << getOpenMPDirectiveName(DKind) << 0; 1629 } 1630 ConsumeToken(); 1631 DeclDirectiveListParserHelper Helper(this, DKind); 1632 if (!ParseOpenMPSimpleVarList(DKind, Helper, 1633 /*AllowScopeSpecifier=*/false)) { 1634 SmallVector<OMPClause *, 1> Clauses; 1635 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 1636 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, 1637 OMPC_unknown + 1> 1638 FirstClauses(OMPC_unknown + 1); 1639 while (Tok.isNot(tok::annot_pragma_openmp_end)) { 1640 OpenMPClauseKind CKind = 1641 Tok.isAnnotation() ? OMPC_unknown 1642 : getOpenMPClauseKind(PP.getSpelling(Tok)); 1643 Actions.StartOpenMPClause(CKind); 1644 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind, 1645 !FirstClauses[CKind].getInt()); 1646 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end, 1647 StopBeforeMatch); 1648 FirstClauses[CKind].setInt(true); 1649 if (Clause != nullptr) 1650 Clauses.push_back(Clause); 1651 if (Tok.is(tok::annot_pragma_openmp_end)) { 1652 Actions.EndOpenMPClause(); 1653 break; 1654 } 1655 // Skip ',' if any. 1656 if (Tok.is(tok::comma)) 1657 ConsumeToken(); 1658 Actions.EndOpenMPClause(); 1659 } 1660 // The last seen token is annot_pragma_openmp_end - need to check for 1661 // extra tokens. 1662 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 1663 Diag(Tok, diag::warn_omp_extra_tokens_at_eol) 1664 << getOpenMPDirectiveName(DKind); 1665 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); 1666 } 1667 } 1668 DeclGroupPtrTy Res = Actions.ActOnOpenMPAllocateDirective( 1669 Loc, Helper.getIdentifiers(), Clauses); 1670 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation()); 1671 } 1672 SkipUntil(tok::annot_pragma_openmp_end); 1673 break; 1674 } 1675 case OMPD_declare_reduction: 1676 ConsumeToken(); 1677 if (DeclGroupPtrTy Res = 1678 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) { 1679 // The last seen token is annot_pragma_openmp_end - need to check for 1680 // extra tokens. 1681 if (Tok.isNot(tok::annot_pragma_openmp_end)) { 1682 Diag(Tok, diag::warn_omp_extra_tokens_at_eol) 1683 << getOpenMPDirectiveName(OMPD_declare_reduction); 1684 while (Tok.isNot(tok::annot_pragma_openmp_end)) 1685 ConsumeAnyToken(); 1686 } 1687 ConsumeAnyToken(); 1688 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation()); 1689 } else { 1690 SkipUntil(tok::annot_pragma_openmp_end); 1691 } 1692 break; 1693 case OMPD_declare_mapper: { 1694 ConsumeToken(); 1695 if (DeclGroupPtrTy Res = 1696 ParseOpenMPDeclareMapperDirective(/*AS=*/AS_none)) { 1697 // Skip the last annot_pragma_openmp_end. 1698 ConsumeAnnotationToken(); 1699 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation()); 1700 } else { 1701 SkipUntil(tok::annot_pragma_openmp_end); 1702 } 1703 break; 1704 } 1705 case OMPD_flush: 1706 if (PP.LookAhead(0).is(tok::l_paren)) { 1707 FlushHasClause = true; 1708 // Push copy of the current token back to stream to properly parse 1709 // pseudo-clause OMPFlushClause. 1710 PP.EnterToken(Tok, /*IsReinject*/ true); 1711 } 1712 LLVM_FALLTHROUGH; 1713 case OMPD_taskyield: 1714 case OMPD_barrier: 1715 case OMPD_taskwait: 1716 case OMPD_cancellation_point: 1717 case OMPD_cancel: 1718 case OMPD_target_enter_data: 1719 case OMPD_target_exit_data: 1720 case OMPD_target_update: 1721 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) == 1722 ParsedStmtContext()) { 1723 Diag(Tok, diag::err_omp_immediate_directive) 1724 << getOpenMPDirectiveName(DKind) << 0; 1725 } 1726 HasAssociatedStatement = false; 1727 // Fall through for further analysis. 1728 LLVM_FALLTHROUGH; 1729 case OMPD_parallel: 1730 case OMPD_simd: 1731 case OMPD_for: 1732 case OMPD_for_simd: 1733 case OMPD_sections: 1734 case OMPD_single: 1735 case OMPD_section: 1736 case OMPD_master: 1737 case OMPD_critical: 1738 case OMPD_parallel_for: 1739 case OMPD_parallel_for_simd: 1740 case OMPD_parallel_sections: 1741 case OMPD_task: 1742 case OMPD_ordered: 1743 case OMPD_atomic: 1744 case OMPD_target: 1745 case OMPD_teams: 1746 case OMPD_taskgroup: 1747 case OMPD_target_data: 1748 case OMPD_target_parallel: 1749 case OMPD_target_parallel_for: 1750 case OMPD_taskloop: 1751 case OMPD_taskloop_simd: 1752 case OMPD_master_taskloop: 1753 case OMPD_master_taskloop_simd: 1754 case OMPD_parallel_master_taskloop: 1755 case OMPD_parallel_master_taskloop_simd: 1756 case OMPD_distribute: 1757 case OMPD_distribute_parallel_for: 1758 case OMPD_distribute_parallel_for_simd: 1759 case OMPD_distribute_simd: 1760 case OMPD_target_parallel_for_simd: 1761 case OMPD_target_simd: 1762 case OMPD_teams_distribute: 1763 case OMPD_teams_distribute_simd: 1764 case OMPD_teams_distribute_parallel_for_simd: 1765 case OMPD_teams_distribute_parallel_for: 1766 case OMPD_target_teams: 1767 case OMPD_target_teams_distribute: 1768 case OMPD_target_teams_distribute_parallel_for: 1769 case OMPD_target_teams_distribute_parallel_for_simd: 1770 case OMPD_target_teams_distribute_simd: { 1771 ConsumeToken(); 1772 // Parse directive name of the 'critical' directive if any. 1773 if (DKind == OMPD_critical) { 1774 BalancedDelimiterTracker T(*this, tok::l_paren, 1775 tok::annot_pragma_openmp_end); 1776 if (!T.consumeOpen()) { 1777 if (Tok.isAnyIdentifier()) { 1778 DirName = 1779 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation()); 1780 ConsumeAnyToken(); 1781 } else { 1782 Diag(Tok, diag::err_omp_expected_identifier_for_critical); 1783 } 1784 T.consumeClose(); 1785 } 1786 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) { 1787 CancelRegion = parseOpenMPDirectiveKind(*this); 1788 if (Tok.isNot(tok::annot_pragma_openmp_end)) 1789 ConsumeToken(); 1790 } 1791 1792 if (isOpenMPLoopDirective(DKind)) 1793 ScopeFlags |= Scope::OpenMPLoopDirectiveScope; 1794 if (isOpenMPSimdDirective(DKind)) 1795 ScopeFlags |= Scope::OpenMPSimdDirectiveScope; 1796 ParseScope OMPDirectiveScope(this, ScopeFlags); 1797 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc); 1798 1799 while (Tok.isNot(tok::annot_pragma_openmp_end)) { 1800 OpenMPClauseKind CKind = 1801 Tok.isAnnotation() 1802 ? OMPC_unknown 1803 : FlushHasClause ? OMPC_flush 1804 : getOpenMPClauseKind(PP.getSpelling(Tok)); 1805 Actions.StartOpenMPClause(CKind); 1806 FlushHasClause = false; 1807 OMPClause *Clause = 1808 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt()); 1809 FirstClauses[CKind].setInt(true); 1810 if (Clause) { 1811 FirstClauses[CKind].setPointer(Clause); 1812 Clauses.push_back(Clause); 1813 } 1814 1815 // Skip ',' if any. 1816 if (Tok.is(tok::comma)) 1817 ConsumeToken(); 1818 Actions.EndOpenMPClause(); 1819 } 1820 // End location of the directive. 1821 EndLoc = Tok.getLocation(); 1822 // Consume final annot_pragma_openmp_end. 1823 ConsumeAnnotationToken(); 1824 1825 // OpenMP [2.13.8, ordered Construct, Syntax] 1826 // If the depend clause is specified, the ordered construct is a stand-alone 1827 // directive. 1828 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) { 1829 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) == 1830 ParsedStmtContext()) { 1831 Diag(Loc, diag::err_omp_immediate_directive) 1832 << getOpenMPDirectiveName(DKind) << 1 1833 << getOpenMPClauseName(OMPC_depend); 1834 } 1835 HasAssociatedStatement = false; 1836 } 1837 1838 StmtResult AssociatedStmt; 1839 if (HasAssociatedStatement) { 1840 // The body is a block scope like in Lambdas and Blocks. 1841 Actions.ActOnOpenMPRegionStart(DKind, getCurScope()); 1842 // FIXME: We create a bogus CompoundStmt scope to hold the contents of 1843 // the captured region. Code elsewhere assumes that any FunctionScopeInfo 1844 // should have at least one compound statement scope within it. 1845 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement()); 1846 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses); 1847 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data || 1848 DKind == OMPD_target_exit_data) { 1849 Actions.ActOnOpenMPRegionStart(DKind, getCurScope()); 1850 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), 1851 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None, 1852 /*isStmtExpr=*/false)); 1853 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses); 1854 } 1855 Directive = Actions.ActOnOpenMPExecutableDirective( 1856 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc, 1857 EndLoc); 1858 1859 // Exit scope. 1860 Actions.EndOpenMPDSABlock(Directive.get()); 1861 OMPDirectiveScope.Exit(); 1862 break; 1863 } 1864 case OMPD_declare_simd: 1865 case OMPD_declare_target: 1866 case OMPD_end_declare_target: 1867 case OMPD_requires: 1868 case OMPD_declare_variant: 1869 Diag(Tok, diag::err_omp_unexpected_directive) 1870 << 1 << getOpenMPDirectiveName(DKind); 1871 SkipUntil(tok::annot_pragma_openmp_end); 1872 break; 1873 case OMPD_unknown: 1874 Diag(Tok, diag::err_omp_unknown_directive); 1875 SkipUntil(tok::annot_pragma_openmp_end); 1876 break; 1877 } 1878 return Directive; 1879 } 1880 1881 // Parses simple list: 1882 // simple-variable-list: 1883 // '(' id-expression {, id-expression} ')' 1884 // 1885 bool Parser::ParseOpenMPSimpleVarList( 1886 OpenMPDirectiveKind Kind, 1887 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> & 1888 Callback, 1889 bool AllowScopeSpecifier) { 1890 // Parse '('. 1891 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); 1892 if (T.expectAndConsume(diag::err_expected_lparen_after, 1893 getOpenMPDirectiveName(Kind))) 1894 return true; 1895 bool IsCorrect = true; 1896 bool NoIdentIsFound = true; 1897 1898 // Read tokens while ')' or annot_pragma_openmp_end is not found. 1899 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) { 1900 CXXScopeSpec SS; 1901 UnqualifiedId Name; 1902 // Read var name. 1903 Token PrevTok = Tok; 1904 NoIdentIsFound = false; 1905 1906 if (AllowScopeSpecifier && getLangOpts().CPlusPlus && 1907 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) { 1908 IsCorrect = false; 1909 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, 1910 StopBeforeMatch); 1911 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr, 1912 nullptr, Name)) { 1913 IsCorrect = false; 1914 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, 1915 StopBeforeMatch); 1916 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) && 1917 Tok.isNot(tok::annot_pragma_openmp_end)) { 1918 IsCorrect = false; 1919 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, 1920 StopBeforeMatch); 1921 Diag(PrevTok.getLocation(), diag::err_expected) 1922 << tok::identifier 1923 << SourceRange(PrevTok.getLocation(), PrevTokLocation); 1924 } else { 1925 Callback(SS, Actions.GetNameFromUnqualifiedId(Name)); 1926 } 1927 // Consume ','. 1928 if (Tok.is(tok::comma)) { 1929 ConsumeToken(); 1930 } 1931 } 1932 1933 if (NoIdentIsFound) { 1934 Diag(Tok, diag::err_expected) << tok::identifier; 1935 IsCorrect = false; 1936 } 1937 1938 // Parse ')'. 1939 IsCorrect = !T.consumeClose() && IsCorrect; 1940 1941 return !IsCorrect; 1942 } 1943 1944 /// Parsing of OpenMP clauses. 1945 /// 1946 /// clause: 1947 /// if-clause | final-clause | num_threads-clause | safelen-clause | 1948 /// default-clause | private-clause | firstprivate-clause | shared-clause 1949 /// | linear-clause | aligned-clause | collapse-clause | 1950 /// lastprivate-clause | reduction-clause | proc_bind-clause | 1951 /// schedule-clause | copyin-clause | copyprivate-clause | untied-clause | 1952 /// mergeable-clause | flush-clause | read-clause | write-clause | 1953 /// update-clause | capture-clause | seq_cst-clause | device-clause | 1954 /// simdlen-clause | threads-clause | simd-clause | num_teams-clause | 1955 /// thread_limit-clause | priority-clause | grainsize-clause | 1956 /// nogroup-clause | num_tasks-clause | hint-clause | to-clause | 1957 /// from-clause | is_device_ptr-clause | task_reduction-clause | 1958 /// in_reduction-clause | allocator-clause | allocate-clause 1959 /// 1960 OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind, 1961 OpenMPClauseKind CKind, bool FirstClause) { 1962 OMPClause *Clause = nullptr; 1963 bool ErrorFound = false; 1964 bool WrongDirective = false; 1965 // Check if clause is allowed for the given directive. 1966 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) { 1967 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind) 1968 << getOpenMPDirectiveName(DKind); 1969 ErrorFound = true; 1970 WrongDirective = true; 1971 } 1972 1973 switch (CKind) { 1974 case OMPC_final: 1975 case OMPC_num_threads: 1976 case OMPC_safelen: 1977 case OMPC_simdlen: 1978 case OMPC_collapse: 1979 case OMPC_ordered: 1980 case OMPC_device: 1981 case OMPC_num_teams: 1982 case OMPC_thread_limit: 1983 case OMPC_priority: 1984 case OMPC_grainsize: 1985 case OMPC_num_tasks: 1986 case OMPC_hint: 1987 case OMPC_allocator: 1988 // OpenMP [2.5, Restrictions] 1989 // At most one num_threads clause can appear on the directive. 1990 // OpenMP [2.8.1, simd construct, Restrictions] 1991 // Only one safelen clause can appear on a simd directive. 1992 // Only one simdlen clause can appear on a simd directive. 1993 // Only one collapse clause can appear on a simd directive. 1994 // OpenMP [2.9.1, target data construct, Restrictions] 1995 // At most one device clause can appear on the directive. 1996 // OpenMP [2.11.1, task Construct, Restrictions] 1997 // At most one if clause can appear on the directive. 1998 // At most one final clause can appear on the directive. 1999 // OpenMP [teams Construct, Restrictions] 2000 // At most one num_teams clause can appear on the directive. 2001 // At most one thread_limit clause can appear on the directive. 2002 // OpenMP [2.9.1, task Construct, Restrictions] 2003 // At most one priority clause can appear on the directive. 2004 // OpenMP [2.9.2, taskloop Construct, Restrictions] 2005 // At most one grainsize clause can appear on the directive. 2006 // OpenMP [2.9.2, taskloop Construct, Restrictions] 2007 // At most one num_tasks clause can appear on the directive. 2008 // OpenMP [2.11.3, allocate Directive, Restrictions] 2009 // At most one allocator clause can appear on the directive. 2010 if (!FirstClause) { 2011 Diag(Tok, diag::err_omp_more_one_clause) 2012 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; 2013 ErrorFound = true; 2014 } 2015 2016 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren)) 2017 Clause = ParseOpenMPClause(CKind, WrongDirective); 2018 else 2019 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective); 2020 break; 2021 case OMPC_default: 2022 case OMPC_proc_bind: 2023 case OMPC_atomic_default_mem_order: 2024 // OpenMP [2.14.3.1, Restrictions] 2025 // Only a single default clause may be specified on a parallel, task or 2026 // teams directive. 2027 // OpenMP [2.5, parallel Construct, Restrictions] 2028 // At most one proc_bind clause can appear on the directive. 2029 // OpenMP [5.0, Requires directive, Restrictions] 2030 // At most one atomic_default_mem_order clause can appear 2031 // on the directive 2032 if (!FirstClause) { 2033 Diag(Tok, diag::err_omp_more_one_clause) 2034 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; 2035 ErrorFound = true; 2036 } 2037 2038 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective); 2039 break; 2040 case OMPC_schedule: 2041 case OMPC_dist_schedule: 2042 case OMPC_defaultmap: 2043 // OpenMP [2.7.1, Restrictions, p. 3] 2044 // Only one schedule clause can appear on a loop directive. 2045 // OpenMP [2.10.4, Restrictions, p. 106] 2046 // At most one defaultmap clause can appear on the directive. 2047 if (!FirstClause) { 2048 Diag(Tok, diag::err_omp_more_one_clause) 2049 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; 2050 ErrorFound = true; 2051 } 2052 LLVM_FALLTHROUGH; 2053 2054 case OMPC_if: 2055 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective); 2056 break; 2057 case OMPC_nowait: 2058 case OMPC_untied: 2059 case OMPC_mergeable: 2060 case OMPC_read: 2061 case OMPC_write: 2062 case OMPC_update: 2063 case OMPC_capture: 2064 case OMPC_seq_cst: 2065 case OMPC_threads: 2066 case OMPC_simd: 2067 case OMPC_nogroup: 2068 case OMPC_unified_address: 2069 case OMPC_unified_shared_memory: 2070 case OMPC_reverse_offload: 2071 case OMPC_dynamic_allocators: 2072 // OpenMP [2.7.1, Restrictions, p. 9] 2073 // Only one ordered clause can appear on a loop directive. 2074 // OpenMP [2.7.1, Restrictions, C/C++, p. 4] 2075 // Only one nowait clause can appear on a for directive. 2076 // OpenMP [5.0, Requires directive, Restrictions] 2077 // Each of the requires clauses can appear at most once on the directive. 2078 if (!FirstClause) { 2079 Diag(Tok, diag::err_omp_more_one_clause) 2080 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; 2081 ErrorFound = true; 2082 } 2083 2084 Clause = ParseOpenMPClause(CKind, WrongDirective); 2085 break; 2086 case OMPC_private: 2087 case OMPC_firstprivate: 2088 case OMPC_lastprivate: 2089 case OMPC_shared: 2090 case OMPC_reduction: 2091 case OMPC_task_reduction: 2092 case OMPC_in_reduction: 2093 case OMPC_linear: 2094 case OMPC_aligned: 2095 case OMPC_copyin: 2096 case OMPC_copyprivate: 2097 case OMPC_flush: 2098 case OMPC_depend: 2099 case OMPC_map: 2100 case OMPC_to: 2101 case OMPC_from: 2102 case OMPC_use_device_ptr: 2103 case OMPC_is_device_ptr: 2104 case OMPC_allocate: 2105 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective); 2106 break; 2107 case OMPC_device_type: 2108 case OMPC_unknown: 2109 Diag(Tok, diag::warn_omp_extra_tokens_at_eol) 2110 << getOpenMPDirectiveName(DKind); 2111 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); 2112 break; 2113 case OMPC_threadprivate: 2114 case OMPC_uniform: 2115 case OMPC_match: 2116 if (!WrongDirective) 2117 Diag(Tok, diag::err_omp_unexpected_clause) 2118 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind); 2119 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch); 2120 break; 2121 } 2122 return ErrorFound ? nullptr : Clause; 2123 } 2124 2125 /// Parses simple expression in parens for single-expression clauses of OpenMP 2126 /// constructs. 2127 /// \param RLoc Returned location of right paren. 2128 ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName, 2129 SourceLocation &RLoc, 2130 bool IsAddressOfOperand) { 2131 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); 2132 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data())) 2133 return ExprError(); 2134 2135 SourceLocation ELoc = Tok.getLocation(); 2136 ExprResult LHS(ParseCastExpression( 2137 /*isUnaryExpression=*/false, IsAddressOfOperand, NotTypeCast)); 2138 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional)); 2139 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false); 2140 2141 // Parse ')'. 2142 RLoc = Tok.getLocation(); 2143 if (!T.consumeClose()) 2144 RLoc = T.getCloseLocation(); 2145 2146 return Val; 2147 } 2148 2149 /// Parsing of OpenMP clauses with single expressions like 'final', 2150 /// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams', 2151 /// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'. 2152 /// 2153 /// final-clause: 2154 /// 'final' '(' expression ')' 2155 /// 2156 /// num_threads-clause: 2157 /// 'num_threads' '(' expression ')' 2158 /// 2159 /// safelen-clause: 2160 /// 'safelen' '(' expression ')' 2161 /// 2162 /// simdlen-clause: 2163 /// 'simdlen' '(' expression ')' 2164 /// 2165 /// collapse-clause: 2166 /// 'collapse' '(' expression ')' 2167 /// 2168 /// priority-clause: 2169 /// 'priority' '(' expression ')' 2170 /// 2171 /// grainsize-clause: 2172 /// 'grainsize' '(' expression ')' 2173 /// 2174 /// num_tasks-clause: 2175 /// 'num_tasks' '(' expression ')' 2176 /// 2177 /// hint-clause: 2178 /// 'hint' '(' expression ')' 2179 /// 2180 /// allocator-clause: 2181 /// 'allocator' '(' expression ')' 2182 /// 2183 OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind, 2184 bool ParseOnly) { 2185 SourceLocation Loc = ConsumeToken(); 2186 SourceLocation LLoc = Tok.getLocation(); 2187 SourceLocation RLoc; 2188 2189 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc); 2190 2191 if (Val.isInvalid()) 2192 return nullptr; 2193 2194 if (ParseOnly) 2195 return nullptr; 2196 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc); 2197 } 2198 2199 /// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'. 2200 /// 2201 /// default-clause: 2202 /// 'default' '(' 'none' | 'shared' ') 2203 /// 2204 /// proc_bind-clause: 2205 /// 'proc_bind' '(' 'master' | 'close' | 'spread' ') 2206 /// 2207 OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind, 2208 bool ParseOnly) { 2209 llvm::Optional<SimpleClauseData> Val = parseOpenMPSimpleClause(*this, Kind); 2210 if (!Val || ParseOnly) 2211 return nullptr; 2212 return Actions.ActOnOpenMPSimpleClause( 2213 Kind, Val.getValue().Type, Val.getValue().TypeLoc, Val.getValue().LOpen, 2214 Val.getValue().Loc, Val.getValue().RLoc); 2215 } 2216 2217 /// Parsing of OpenMP clauses like 'ordered'. 2218 /// 2219 /// ordered-clause: 2220 /// 'ordered' 2221 /// 2222 /// nowait-clause: 2223 /// 'nowait' 2224 /// 2225 /// untied-clause: 2226 /// 'untied' 2227 /// 2228 /// mergeable-clause: 2229 /// 'mergeable' 2230 /// 2231 /// read-clause: 2232 /// 'read' 2233 /// 2234 /// threads-clause: 2235 /// 'threads' 2236 /// 2237 /// simd-clause: 2238 /// 'simd' 2239 /// 2240 /// nogroup-clause: 2241 /// 'nogroup' 2242 /// 2243 OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) { 2244 SourceLocation Loc = Tok.getLocation(); 2245 ConsumeAnyToken(); 2246 2247 if (ParseOnly) 2248 return nullptr; 2249 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation()); 2250 } 2251 2252 2253 /// Parsing of OpenMP clauses with single expressions and some additional 2254 /// argument like 'schedule' or 'dist_schedule'. 2255 /// 2256 /// schedule-clause: 2257 /// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ] 2258 /// ')' 2259 /// 2260 /// if-clause: 2261 /// 'if' '(' [ directive-name-modifier ':' ] expression ')' 2262 /// 2263 /// defaultmap: 2264 /// 'defaultmap' '(' modifier ':' kind ')' 2265 /// 2266 OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind, 2267 bool ParseOnly) { 2268 SourceLocation Loc = ConsumeToken(); 2269 SourceLocation DelimLoc; 2270 // Parse '('. 2271 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); 2272 if (T.expectAndConsume(diag::err_expected_lparen_after, 2273 getOpenMPClauseName(Kind))) 2274 return nullptr; 2275 2276 ExprResult Val; 2277 SmallVector<unsigned, 4> Arg; 2278 SmallVector<SourceLocation, 4> KLoc; 2279 if (Kind == OMPC_schedule) { 2280 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 2281 Arg.resize(NumberOfElements); 2282 KLoc.resize(NumberOfElements); 2283 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown; 2284 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown; 2285 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown; 2286 unsigned KindModifier = getOpenMPSimpleClauseType( 2287 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)); 2288 if (KindModifier > OMPC_SCHEDULE_unknown) { 2289 // Parse 'modifier' 2290 Arg[Modifier1] = KindModifier; 2291 KLoc[Modifier1] = Tok.getLocation(); 2292 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 2293 Tok.isNot(tok::annot_pragma_openmp_end)) 2294 ConsumeAnyToken(); 2295 if (Tok.is(tok::comma)) { 2296 // Parse ',' 'modifier' 2297 ConsumeAnyToken(); 2298 KindModifier = getOpenMPSimpleClauseType( 2299 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)); 2300 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown 2301 ? KindModifier 2302 : (unsigned)OMPC_SCHEDULE_unknown; 2303 KLoc[Modifier2] = Tok.getLocation(); 2304 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 2305 Tok.isNot(tok::annot_pragma_openmp_end)) 2306 ConsumeAnyToken(); 2307 } 2308 // Parse ':' 2309 if (Tok.is(tok::colon)) 2310 ConsumeAnyToken(); 2311 else 2312 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier"; 2313 KindModifier = getOpenMPSimpleClauseType( 2314 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)); 2315 } 2316 Arg[ScheduleKind] = KindModifier; 2317 KLoc[ScheduleKind] = Tok.getLocation(); 2318 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 2319 Tok.isNot(tok::annot_pragma_openmp_end)) 2320 ConsumeAnyToken(); 2321 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static || 2322 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic || 2323 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) && 2324 Tok.is(tok::comma)) 2325 DelimLoc = ConsumeAnyToken(); 2326 } else if (Kind == OMPC_dist_schedule) { 2327 Arg.push_back(getOpenMPSimpleClauseType( 2328 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok))); 2329 KLoc.push_back(Tok.getLocation()); 2330 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 2331 Tok.isNot(tok::annot_pragma_openmp_end)) 2332 ConsumeAnyToken(); 2333 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma)) 2334 DelimLoc = ConsumeAnyToken(); 2335 } else if (Kind == OMPC_defaultmap) { 2336 // Get a defaultmap modifier 2337 Arg.push_back(getOpenMPSimpleClauseType( 2338 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok))); 2339 KLoc.push_back(Tok.getLocation()); 2340 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 2341 Tok.isNot(tok::annot_pragma_openmp_end)) 2342 ConsumeAnyToken(); 2343 // Parse ':' 2344 if (Tok.is(tok::colon)) 2345 ConsumeAnyToken(); 2346 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown) 2347 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier"; 2348 // Get a defaultmap kind 2349 Arg.push_back(getOpenMPSimpleClauseType( 2350 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok))); 2351 KLoc.push_back(Tok.getLocation()); 2352 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && 2353 Tok.isNot(tok::annot_pragma_openmp_end)) 2354 ConsumeAnyToken(); 2355 } else { 2356 assert(Kind == OMPC_if); 2357 KLoc.push_back(Tok.getLocation()); 2358 TentativeParsingAction TPA(*this); 2359 Arg.push_back(parseOpenMPDirectiveKind(*this)); 2360 if (Arg.back() != OMPD_unknown) { 2361 ConsumeToken(); 2362 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) { 2363 TPA.Commit(); 2364 DelimLoc = ConsumeToken(); 2365 } else { 2366 TPA.Revert(); 2367 Arg.back() = OMPD_unknown; 2368 } 2369 } else { 2370 TPA.Revert(); 2371 } 2372 } 2373 2374 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) || 2375 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) || 2376 Kind == OMPC_if; 2377 if (NeedAnExpression) { 2378 SourceLocation ELoc = Tok.getLocation(); 2379 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast)); 2380 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional); 2381 Val = 2382 Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false); 2383 } 2384 2385 // Parse ')'. 2386 SourceLocation RLoc = Tok.getLocation(); 2387 if (!T.consumeClose()) 2388 RLoc = T.getCloseLocation(); 2389 2390 if (NeedAnExpression && Val.isInvalid()) 2391 return nullptr; 2392 2393 if (ParseOnly) 2394 return nullptr; 2395 return Actions.ActOnOpenMPSingleExprWithArgClause( 2396 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc); 2397 } 2398 2399 static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec, 2400 UnqualifiedId &ReductionId) { 2401 if (ReductionIdScopeSpec.isEmpty()) { 2402 auto OOK = OO_None; 2403 switch (P.getCurToken().getKind()) { 2404 case tok::plus: 2405 OOK = OO_Plus; 2406 break; 2407 case tok::minus: 2408 OOK = OO_Minus; 2409 break; 2410 case tok::star: 2411 OOK = OO_Star; 2412 break; 2413 case tok::amp: 2414 OOK = OO_Amp; 2415 break; 2416 case tok::pipe: 2417 OOK = OO_Pipe; 2418 break; 2419 case tok::caret: 2420 OOK = OO_Caret; 2421 break; 2422 case tok::ampamp: 2423 OOK = OO_AmpAmp; 2424 break; 2425 case tok::pipepipe: 2426 OOK = OO_PipePipe; 2427 break; 2428 default: 2429 break; 2430 } 2431 if (OOK != OO_None) { 2432 SourceLocation OpLoc = P.ConsumeToken(); 2433 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()}; 2434 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations); 2435 return false; 2436 } 2437 } 2438 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false, 2439 /*AllowDestructorName*/ false, 2440 /*AllowConstructorName*/ false, 2441 /*AllowDeductionGuide*/ false, 2442 nullptr, nullptr, ReductionId); 2443 } 2444 2445 /// Checks if the token is a valid map-type-modifier. 2446 static OpenMPMapModifierKind isMapModifier(Parser &P) { 2447 Token Tok = P.getCurToken(); 2448 if (!Tok.is(tok::identifier)) 2449 return OMPC_MAP_MODIFIER_unknown; 2450 2451 Preprocessor &PP = P.getPreprocessor(); 2452 OpenMPMapModifierKind TypeModifier = static_cast<OpenMPMapModifierKind>( 2453 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok))); 2454 return TypeModifier; 2455 } 2456 2457 /// Parse the mapper modifier in map, to, and from clauses. 2458 bool Parser::parseMapperModifier(OpenMPVarListDataTy &Data) { 2459 // Parse '('. 2460 BalancedDelimiterTracker T(*this, tok::l_paren, tok::colon); 2461 if (T.expectAndConsume(diag::err_expected_lparen_after, "mapper")) { 2462 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, 2463 StopBeforeMatch); 2464 return true; 2465 } 2466 // Parse mapper-identifier 2467 if (getLangOpts().CPlusPlus) 2468 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec, 2469 /*ObjectType=*/nullptr, 2470 /*EnteringContext=*/false); 2471 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) { 2472 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier); 2473 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, 2474 StopBeforeMatch); 2475 return true; 2476 } 2477 auto &DeclNames = Actions.getASTContext().DeclarationNames; 2478 Data.ReductionOrMapperId = DeclarationNameInfo( 2479 DeclNames.getIdentifier(Tok.getIdentifierInfo()), Tok.getLocation()); 2480 ConsumeToken(); 2481 // Parse ')'. 2482 return T.consumeClose(); 2483 } 2484 2485 /// Parse map-type-modifiers in map clause. 2486 /// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list) 2487 /// where, map-type-modifier ::= always | close | mapper(mapper-identifier) 2488 bool Parser::parseMapTypeModifiers(OpenMPVarListDataTy &Data) { 2489 while (getCurToken().isNot(tok::colon)) { 2490 OpenMPMapModifierKind TypeModifier = isMapModifier(*this); 2491 if (TypeModifier == OMPC_MAP_MODIFIER_always || 2492 TypeModifier == OMPC_MAP_MODIFIER_close) { 2493 Data.MapTypeModifiers.push_back(TypeModifier); 2494 Data.MapTypeModifiersLoc.push_back(Tok.getLocation()); 2495 ConsumeToken(); 2496 } else if (TypeModifier == OMPC_MAP_MODIFIER_mapper) { 2497 Data.MapTypeModifiers.push_back(TypeModifier); 2498 Data.MapTypeModifiersLoc.push_back(Tok.getLocation()); 2499 ConsumeToken(); 2500 if (parseMapperModifier(Data)) 2501 return true; 2502 } else { 2503 // For the case of unknown map-type-modifier or a map-type. 2504 // Map-type is followed by a colon; the function returns when it 2505 // encounters a token followed by a colon. 2506 if (Tok.is(tok::comma)) { 2507 Diag(Tok, diag::err_omp_map_type_modifier_missing); 2508 ConsumeToken(); 2509 continue; 2510 } 2511 // Potential map-type token as it is followed by a colon. 2512 if (PP.LookAhead(0).is(tok::colon)) 2513 return false; 2514 Diag(Tok, diag::err_omp_unknown_map_type_modifier); 2515 ConsumeToken(); 2516 } 2517 if (getCurToken().is(tok::comma)) 2518 ConsumeToken(); 2519 } 2520 return false; 2521 } 2522 2523 /// Checks if the token is a valid map-type. 2524 static OpenMPMapClauseKind isMapType(Parser &P) { 2525 Token Tok = P.getCurToken(); 2526 // The map-type token can be either an identifier or the C++ delete keyword. 2527 if (!Tok.isOneOf(tok::identifier, tok::kw_delete)) 2528 return OMPC_MAP_unknown; 2529 Preprocessor &PP = P.getPreprocessor(); 2530 OpenMPMapClauseKind MapType = static_cast<OpenMPMapClauseKind>( 2531 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok))); 2532 return MapType; 2533 } 2534 2535 /// Parse map-type in map clause. 2536 /// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list) 2537 /// where, map-type ::= to | from | tofrom | alloc | release | delete 2538 static void parseMapType(Parser &P, Parser::OpenMPVarListDataTy &Data) { 2539 Token Tok = P.getCurToken(); 2540 if (Tok.is(tok::colon)) { 2541 P.Diag(Tok, diag::err_omp_map_type_missing); 2542 return; 2543 } 2544 Data.MapType = isMapType(P); 2545 if (Data.MapType == OMPC_MAP_unknown) 2546 P.Diag(Tok, diag::err_omp_unknown_map_type); 2547 P.ConsumeToken(); 2548 } 2549 2550 /// Parses clauses with list. 2551 bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind, 2552 OpenMPClauseKind Kind, 2553 SmallVectorImpl<Expr *> &Vars, 2554 OpenMPVarListDataTy &Data) { 2555 UnqualifiedId UnqualifiedReductionId; 2556 bool InvalidReductionId = false; 2557 bool IsInvalidMapperModifier = false; 2558 2559 // Parse '('. 2560 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); 2561 if (T.expectAndConsume(diag::err_expected_lparen_after, 2562 getOpenMPClauseName(Kind))) 2563 return true; 2564 2565 bool NeedRParenForLinear = false; 2566 BalancedDelimiterTracker LinearT(*this, tok::l_paren, 2567 tok::annot_pragma_openmp_end); 2568 // Handle reduction-identifier for reduction clause. 2569 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction || 2570 Kind == OMPC_in_reduction) { 2571 ColonProtectionRAIIObject ColonRAII(*this); 2572 if (getLangOpts().CPlusPlus) 2573 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec, 2574 /*ObjectType=*/nullptr, 2575 /*EnteringContext=*/false); 2576 InvalidReductionId = ParseReductionId( 2577 *this, Data.ReductionOrMapperIdScopeSpec, UnqualifiedReductionId); 2578 if (InvalidReductionId) { 2579 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, 2580 StopBeforeMatch); 2581 } 2582 if (Tok.is(tok::colon)) 2583 Data.ColonLoc = ConsumeToken(); 2584 else 2585 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier"; 2586 if (!InvalidReductionId) 2587 Data.ReductionOrMapperId = 2588 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId); 2589 } else if (Kind == OMPC_depend) { 2590 // Handle dependency type for depend clause. 2591 ColonProtectionRAIIObject ColonRAII(*this); 2592 Data.DepKind = 2593 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType( 2594 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : "")); 2595 Data.DepLinMapLoc = Tok.getLocation(); 2596 2597 if (Data.DepKind == OMPC_DEPEND_unknown) { 2598 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, 2599 StopBeforeMatch); 2600 } else { 2601 ConsumeToken(); 2602 // Special processing for depend(source) clause. 2603 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) { 2604 // Parse ')'. 2605 T.consumeClose(); 2606 return false; 2607 } 2608 } 2609 if (Tok.is(tok::colon)) { 2610 Data.ColonLoc = ConsumeToken(); 2611 } else { 2612 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren 2613 : diag::warn_pragma_expected_colon) 2614 << "dependency type"; 2615 } 2616 } else if (Kind == OMPC_linear) { 2617 // Try to parse modifier if any. 2618 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) { 2619 Data.LinKind = static_cast<OpenMPLinearClauseKind>( 2620 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok))); 2621 Data.DepLinMapLoc = ConsumeToken(); 2622 LinearT.consumeOpen(); 2623 NeedRParenForLinear = true; 2624 } 2625 } else if (Kind == OMPC_map) { 2626 // Handle map type for map clause. 2627 ColonProtectionRAIIObject ColonRAII(*this); 2628 2629 // The first identifier may be a list item, a map-type or a 2630 // map-type-modifier. The map-type can also be delete which has the same 2631 // spelling of the C++ delete keyword. 2632 Data.DepLinMapLoc = Tok.getLocation(); 2633 2634 // Check for presence of a colon in the map clause. 2635 TentativeParsingAction TPA(*this); 2636 bool ColonPresent = false; 2637 if (SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, 2638 StopBeforeMatch)) { 2639 if (Tok.is(tok::colon)) 2640 ColonPresent = true; 2641 } 2642 TPA.Revert(); 2643 // Only parse map-type-modifier[s] and map-type if a colon is present in 2644 // the map clause. 2645 if (ColonPresent) { 2646 IsInvalidMapperModifier = parseMapTypeModifiers(Data); 2647 if (!IsInvalidMapperModifier) 2648 parseMapType(*this, Data); 2649 else 2650 SkipUntil(tok::colon, tok::annot_pragma_openmp_end, StopBeforeMatch); 2651 } 2652 if (Data.MapType == OMPC_MAP_unknown) { 2653 Data.MapType = OMPC_MAP_tofrom; 2654 Data.IsMapTypeImplicit = true; 2655 } 2656 2657 if (Tok.is(tok::colon)) 2658 Data.ColonLoc = ConsumeToken(); 2659 } else if (Kind == OMPC_to || Kind == OMPC_from) { 2660 if (Tok.is(tok::identifier)) { 2661 bool IsMapperModifier = false; 2662 if (Kind == OMPC_to) { 2663 auto Modifier = static_cast<OpenMPToModifierKind>( 2664 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok))); 2665 if (Modifier == OMPC_TO_MODIFIER_mapper) 2666 IsMapperModifier = true; 2667 } else { 2668 auto Modifier = static_cast<OpenMPFromModifierKind>( 2669 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok))); 2670 if (Modifier == OMPC_FROM_MODIFIER_mapper) 2671 IsMapperModifier = true; 2672 } 2673 if (IsMapperModifier) { 2674 // Parse the mapper modifier. 2675 ConsumeToken(); 2676 IsInvalidMapperModifier = parseMapperModifier(Data); 2677 if (Tok.isNot(tok::colon)) { 2678 if (!IsInvalidMapperModifier) 2679 Diag(Tok, diag::warn_pragma_expected_colon) << ")"; 2680 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, 2681 StopBeforeMatch); 2682 } 2683 // Consume ':'. 2684 if (Tok.is(tok::colon)) 2685 ConsumeToken(); 2686 } 2687 } 2688 } else if (Kind == OMPC_allocate) { 2689 // Handle optional allocator expression followed by colon delimiter. 2690 ColonProtectionRAIIObject ColonRAII(*this); 2691 TentativeParsingAction TPA(*this); 2692 ExprResult Tail = 2693 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()); 2694 Tail = Actions.ActOnFinishFullExpr(Tail.get(), T.getOpenLocation(), 2695 /*DiscardedValue=*/false); 2696 if (Tail.isUsable()) { 2697 if (Tok.is(tok::colon)) { 2698 Data.TailExpr = Tail.get(); 2699 Data.ColonLoc = ConsumeToken(); 2700 TPA.Commit(); 2701 } else { 2702 // colon not found, no allocator specified, parse only list of 2703 // variables. 2704 TPA.Revert(); 2705 } 2706 } else { 2707 // Parsing was unsuccessfull, revert and skip to the end of clause or 2708 // directive. 2709 TPA.Revert(); 2710 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, 2711 StopBeforeMatch); 2712 } 2713 } 2714 2715 bool IsComma = 2716 (Kind != OMPC_reduction && Kind != OMPC_task_reduction && 2717 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) || 2718 (Kind == OMPC_reduction && !InvalidReductionId) || 2719 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown) || 2720 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown); 2721 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned); 2722 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) && 2723 Tok.isNot(tok::annot_pragma_openmp_end))) { 2724 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail); 2725 // Parse variable 2726 ExprResult VarExpr = 2727 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()); 2728 if (VarExpr.isUsable()) { 2729 Vars.push_back(VarExpr.get()); 2730 } else { 2731 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, 2732 StopBeforeMatch); 2733 } 2734 // Skip ',' if any 2735 IsComma = Tok.is(tok::comma); 2736 if (IsComma) 2737 ConsumeToken(); 2738 else if (Tok.isNot(tok::r_paren) && 2739 Tok.isNot(tok::annot_pragma_openmp_end) && 2740 (!MayHaveTail || Tok.isNot(tok::colon))) 2741 Diag(Tok, diag::err_omp_expected_punc) 2742 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush) 2743 : getOpenMPClauseName(Kind)) 2744 << (Kind == OMPC_flush); 2745 } 2746 2747 // Parse ')' for linear clause with modifier. 2748 if (NeedRParenForLinear) 2749 LinearT.consumeClose(); 2750 2751 // Parse ':' linear-step (or ':' alignment). 2752 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon); 2753 if (MustHaveTail) { 2754 Data.ColonLoc = Tok.getLocation(); 2755 SourceLocation ELoc = ConsumeToken(); 2756 ExprResult Tail = ParseAssignmentExpression(); 2757 Tail = 2758 Actions.ActOnFinishFullExpr(Tail.get(), ELoc, /*DiscardedValue*/ false); 2759 if (Tail.isUsable()) 2760 Data.TailExpr = Tail.get(); 2761 else 2762 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, 2763 StopBeforeMatch); 2764 } 2765 2766 // Parse ')'. 2767 Data.RLoc = Tok.getLocation(); 2768 if (!T.consumeClose()) 2769 Data.RLoc = T.getCloseLocation(); 2770 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown && 2771 Vars.empty()) || 2772 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) || 2773 (MustHaveTail && !Data.TailExpr) || InvalidReductionId || 2774 IsInvalidMapperModifier; 2775 } 2776 2777 /// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate', 2778 /// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or 2779 /// 'in_reduction'. 2780 /// 2781 /// private-clause: 2782 /// 'private' '(' list ')' 2783 /// firstprivate-clause: 2784 /// 'firstprivate' '(' list ')' 2785 /// lastprivate-clause: 2786 /// 'lastprivate' '(' list ')' 2787 /// shared-clause: 2788 /// 'shared' '(' list ')' 2789 /// linear-clause: 2790 /// 'linear' '(' linear-list [ ':' linear-step ] ')' 2791 /// aligned-clause: 2792 /// 'aligned' '(' list [ ':' alignment ] ')' 2793 /// reduction-clause: 2794 /// 'reduction' '(' reduction-identifier ':' list ')' 2795 /// task_reduction-clause: 2796 /// 'task_reduction' '(' reduction-identifier ':' list ')' 2797 /// in_reduction-clause: 2798 /// 'in_reduction' '(' reduction-identifier ':' list ')' 2799 /// copyprivate-clause: 2800 /// 'copyprivate' '(' list ')' 2801 /// flush-clause: 2802 /// 'flush' '(' list ')' 2803 /// depend-clause: 2804 /// 'depend' '(' in | out | inout : list | source ')' 2805 /// map-clause: 2806 /// 'map' '(' [ [ always [,] ] [ close [,] ] 2807 /// [ mapper '(' mapper-identifier ')' [,] ] 2808 /// to | from | tofrom | alloc | release | delete ':' ] list ')'; 2809 /// to-clause: 2810 /// 'to' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')' 2811 /// from-clause: 2812 /// 'from' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')' 2813 /// use_device_ptr-clause: 2814 /// 'use_device_ptr' '(' list ')' 2815 /// is_device_ptr-clause: 2816 /// 'is_device_ptr' '(' list ')' 2817 /// allocate-clause: 2818 /// 'allocate' '(' [ allocator ':' ] list ')' 2819 /// 2820 /// For 'linear' clause linear-list may have the following forms: 2821 /// list 2822 /// modifier(list) 2823 /// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++). 2824 OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind, 2825 OpenMPClauseKind Kind, 2826 bool ParseOnly) { 2827 SourceLocation Loc = Tok.getLocation(); 2828 SourceLocation LOpen = ConsumeToken(); 2829 SmallVector<Expr *, 4> Vars; 2830 OpenMPVarListDataTy Data; 2831 2832 if (ParseOpenMPVarList(DKind, Kind, Vars, Data)) 2833 return nullptr; 2834 2835 if (ParseOnly) 2836 return nullptr; 2837 OMPVarListLocTy Locs(Loc, LOpen, Data.RLoc); 2838 return Actions.ActOnOpenMPVarListClause( 2839 Kind, Vars, Data.TailExpr, Locs, Data.ColonLoc, 2840 Data.ReductionOrMapperIdScopeSpec, Data.ReductionOrMapperId, Data.DepKind, 2841 Data.LinKind, Data.MapTypeModifiers, Data.MapTypeModifiersLoc, 2842 Data.MapType, Data.IsMapTypeImplicit, Data.DepLinMapLoc); 2843 } 2844 2845