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