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