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