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