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