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