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/ASTConsumer.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/StmtOpenMP.h"
18 #include "clang/Parse/ParseDiagnostic.h"
19 #include "clang/Parse/Parser.h"
20 #include "clang/Sema/Scope.h"
21 #include "llvm/ADT/PointerIntPair.h"
22 using namespace clang;
23 
24 //===----------------------------------------------------------------------===//
25 // OpenMP declarative directives.
26 //===----------------------------------------------------------------------===//
27 
28 static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
29   auto Tok = P.getCurToken();
30   auto DKind =
31       Tok.isAnnotation()
32           ? OMPD_unknown
33           : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
34   if (DKind == OMPD_parallel) {
35     Tok = P.getPreprocessor().LookAhead(0);
36     auto SDKind =
37         Tok.isAnnotation()
38             ? OMPD_unknown
39             : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
40     if (SDKind == OMPD_for) {
41       P.ConsumeToken();
42       DKind = OMPD_parallel_for;
43     } else if (SDKind == OMPD_sections) {
44       P.ConsumeToken();
45       DKind = OMPD_parallel_sections;
46     }
47   }
48   return DKind;
49 }
50 
51 /// \brief Parsing of declarative OpenMP directives.
52 ///
53 ///       threadprivate-directive:
54 ///         annot_pragma_openmp 'threadprivate' simple-variable-list
55 ///
56 Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
57   assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
58   ParenBraceBracketBalancer BalancerRAIIObj(*this);
59 
60   SourceLocation Loc = ConsumeToken();
61   SmallVector<Expr *, 5> Identifiers;
62   auto DKind = ParseOpenMPDirectiveKind(*this);
63 
64   switch (DKind) {
65   case OMPD_threadprivate:
66     ConsumeToken();
67     if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
68       // The last seen token is annot_pragma_openmp_end - need to check for
69       // extra tokens.
70       if (Tok.isNot(tok::annot_pragma_openmp_end)) {
71         Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
72             << getOpenMPDirectiveName(OMPD_threadprivate);
73         SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
74       }
75       // Skip the last annot_pragma_openmp_end.
76       ConsumeToken();
77       return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
78     }
79     break;
80   case OMPD_unknown:
81     Diag(Tok, diag::err_omp_unknown_directive);
82     break;
83   case OMPD_parallel:
84   case OMPD_simd:
85   case OMPD_task:
86   case OMPD_taskyield:
87   case OMPD_barrier:
88   case OMPD_taskwait:
89   case OMPD_for:
90   case OMPD_sections:
91   case OMPD_section:
92   case OMPD_single:
93   case OMPD_master:
94   case OMPD_parallel_for:
95   case OMPD_parallel_sections:
96     Diag(Tok, diag::err_omp_unexpected_directive)
97         << getOpenMPDirectiveName(DKind);
98     break;
99   }
100   SkipUntil(tok::annot_pragma_openmp_end);
101   return DeclGroupPtrTy();
102 }
103 
104 /// \brief Parsing of declarative or executable OpenMP directives.
105 ///
106 ///       threadprivate-directive:
107 ///         annot_pragma_openmp 'threadprivate' simple-variable-list
108 ///         annot_pragma_openmp_end
109 ///
110 ///       executable-directive:
111 ///         annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
112 ///         'section' | 'single' | 'master' | 'parallel for' |
113 ///         'parallel sections' | 'task' | 'taskyield' | 'barrier' | 'taskwait'
114 ///         {clause} annot_pragma_openmp_end
115 ///
116 StmtResult
117 Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) {
118   assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
119   ParenBraceBracketBalancer BalancerRAIIObj(*this);
120   SmallVector<Expr *, 5> Identifiers;
121   SmallVector<OMPClause *, 5> Clauses;
122   SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
123   FirstClauses(OMPC_unknown + 1);
124   unsigned ScopeFlags =
125       Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
126   SourceLocation Loc = ConsumeToken(), EndLoc;
127   auto DKind = ParseOpenMPDirectiveKind(*this);
128   // Name of critical directive.
129   DeclarationNameInfo DirName;
130   StmtResult Directive = StmtError();
131   bool HasAssociatedStatement = true;
132 
133   switch (DKind) {
134   case OMPD_threadprivate:
135     ConsumeToken();
136     if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
137       // The last seen token is annot_pragma_openmp_end - need to check for
138       // extra tokens.
139       if (Tok.isNot(tok::annot_pragma_openmp_end)) {
140         Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
141             << getOpenMPDirectiveName(OMPD_threadprivate);
142         SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
143       }
144       DeclGroupPtrTy Res =
145           Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
146       Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
147     }
148     SkipUntil(tok::annot_pragma_openmp_end);
149     break;
150   case OMPD_taskyield:
151   case OMPD_barrier:
152   case OMPD_taskwait:
153     if (!StandAloneAllowed) {
154       Diag(Tok, diag::err_omp_immediate_directive)
155           << getOpenMPDirectiveName(DKind);
156     }
157     HasAssociatedStatement = false;
158   case OMPD_parallel:
159   case OMPD_simd:
160   case OMPD_for:
161   case OMPD_sections:
162   case OMPD_single:
163   case OMPD_section:
164   case OMPD_master:
165   case OMPD_parallel_for:
166   case OMPD_parallel_sections:
167   case OMPD_task: {
168     ConsumeToken();
169 
170     if (isOpenMPLoopDirective(DKind))
171       ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
172     if (isOpenMPSimdDirective(DKind))
173       ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
174     ParseScope OMPDirectiveScope(this, ScopeFlags);
175     Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
176 
177     while (Tok.isNot(tok::annot_pragma_openmp_end)) {
178       OpenMPClauseKind CKind = Tok.isAnnotation()
179                                    ? OMPC_unknown
180                                    : getOpenMPClauseKind(PP.getSpelling(Tok));
181       OMPClause *Clause =
182           ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
183       FirstClauses[CKind].setInt(true);
184       if (Clause) {
185         FirstClauses[CKind].setPointer(Clause);
186         Clauses.push_back(Clause);
187       }
188 
189       // Skip ',' if any.
190       if (Tok.is(tok::comma))
191         ConsumeToken();
192     }
193     // End location of the directive.
194     EndLoc = Tok.getLocation();
195     // Consume final annot_pragma_openmp_end.
196     ConsumeToken();
197 
198     StmtResult AssociatedStmt;
199     bool CreateDirective = true;
200     if (HasAssociatedStatement) {
201       // The body is a block scope like in Lambdas and Blocks.
202       Sema::CompoundScopeRAII CompoundScope(Actions);
203       Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
204       Actions.ActOnStartOfCompoundStmt();
205       // Parse statement
206       AssociatedStmt = ParseStatement();
207       Actions.ActOnFinishOfCompoundStmt();
208       if (!AssociatedStmt.isUsable()) {
209         Actions.ActOnCapturedRegionError();
210         CreateDirective = false;
211       } else {
212         AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.get());
213         CreateDirective = AssociatedStmt.isUsable();
214       }
215     }
216     if (CreateDirective)
217       Directive = Actions.ActOnOpenMPExecutableDirective(
218           DKind, Clauses, AssociatedStmt.get(), Loc, EndLoc);
219 
220     // Exit scope.
221     Actions.EndOpenMPDSABlock(Directive.get());
222     OMPDirectiveScope.Exit();
223     break;
224   }
225   case OMPD_unknown:
226     Diag(Tok, diag::err_omp_unknown_directive);
227     SkipUntil(tok::annot_pragma_openmp_end);
228     break;
229   }
230   return Directive;
231 }
232 
233 /// \brief Parses list of simple variables for '#pragma omp threadprivate'
234 /// directive.
235 ///
236 ///   simple-variable-list:
237 ///         '(' id-expression {, id-expression} ')'
238 ///
239 bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
240                                       SmallVectorImpl<Expr *> &VarList,
241                                       bool AllowScopeSpecifier) {
242   VarList.clear();
243   // Parse '('.
244   BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
245   if (T.expectAndConsume(diag::err_expected_lparen_after,
246                          getOpenMPDirectiveName(Kind)))
247     return true;
248   bool IsCorrect = true;
249   bool NoIdentIsFound = true;
250 
251   // Read tokens while ')' or annot_pragma_openmp_end is not found.
252   while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
253     CXXScopeSpec SS;
254     SourceLocation TemplateKWLoc;
255     UnqualifiedId Name;
256     // Read var name.
257     Token PrevTok = Tok;
258     NoIdentIsFound = false;
259 
260     if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
261         ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
262       IsCorrect = false;
263       SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
264                 StopBeforeMatch);
265     } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
266                                   TemplateKWLoc, Name)) {
267       IsCorrect = false;
268       SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
269                 StopBeforeMatch);
270     } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
271                Tok.isNot(tok::annot_pragma_openmp_end)) {
272       IsCorrect = false;
273       SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
274                 StopBeforeMatch);
275       Diag(PrevTok.getLocation(), diag::err_expected)
276           << tok::identifier
277           << SourceRange(PrevTok.getLocation(), PrevTokLocation);
278     } else {
279       DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
280       ExprResult Res =
281           Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
282       if (Res.isUsable())
283         VarList.push_back(Res.get());
284     }
285     // Consume ','.
286     if (Tok.is(tok::comma)) {
287       ConsumeToken();
288     }
289   }
290 
291   if (NoIdentIsFound) {
292     Diag(Tok, diag::err_expected) << tok::identifier;
293     IsCorrect = false;
294   }
295 
296   // Parse ')'.
297   IsCorrect = !T.consumeClose() && IsCorrect;
298 
299   return !IsCorrect && VarList.empty();
300 }
301 
302 /// \brief Parsing of OpenMP clauses.
303 ///
304 ///    clause:
305 ///       if-clause | final-clause | num_threads-clause | safelen-clause |
306 ///       default-clause | private-clause | firstprivate-clause | shared-clause
307 ///       | linear-clause | aligned-clause | collapse-clause |
308 ///       lastprivate-clause | reduction-clause | proc_bind-clause |
309 ///       schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
310 ///       mergeable-clause
311 ///
312 OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
313                                      OpenMPClauseKind CKind, bool FirstClause) {
314   OMPClause *Clause = nullptr;
315   bool ErrorFound = false;
316   // Check if clause is allowed for the given directive.
317   if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
318     Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
319                                                << getOpenMPDirectiveName(DKind);
320     ErrorFound = true;
321   }
322 
323   switch (CKind) {
324   case OMPC_if:
325   case OMPC_final:
326   case OMPC_num_threads:
327   case OMPC_safelen:
328   case OMPC_collapse:
329     // OpenMP [2.5, Restrictions]
330     //  At most one if clause can appear on the directive.
331     //  At most one num_threads clause can appear on the directive.
332     // OpenMP [2.8.1, simd construct, Restrictions]
333     //  Only one safelen  clause can appear on a simd directive.
334     //  Only one collapse clause can appear on a simd directive.
335     // OpenMP [2.11.1, task Construct, Restrictions]
336     //  At most one if clause can appear on the directive.
337     //  At most one final clause can appear on the directive.
338     if (!FirstClause) {
339       Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
340                                                << getOpenMPClauseName(CKind);
341     }
342 
343     Clause = ParseOpenMPSingleExprClause(CKind);
344     break;
345   case OMPC_default:
346   case OMPC_proc_bind:
347     // OpenMP [2.14.3.1, Restrictions]
348     //  Only a single default clause may be specified on a parallel, task or
349     //  teams directive.
350     // OpenMP [2.5, parallel Construct, Restrictions]
351     //  At most one proc_bind clause can appear on the directive.
352     if (!FirstClause) {
353       Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
354                                                << getOpenMPClauseName(CKind);
355     }
356 
357     Clause = ParseOpenMPSimpleClause(CKind);
358     break;
359   case OMPC_schedule:
360     // OpenMP [2.7.1, Restrictions, p. 3]
361     //  Only one schedule clause can appear on a loop directive.
362     if (!FirstClause) {
363       Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
364                                                << getOpenMPClauseName(CKind);
365     }
366 
367     Clause = ParseOpenMPSingleExprWithArgClause(CKind);
368     break;
369   case OMPC_ordered:
370   case OMPC_nowait:
371   case OMPC_untied:
372   case OMPC_mergeable:
373     // OpenMP [2.7.1, Restrictions, p. 9]
374     //  Only one ordered clause can appear on a loop directive.
375     // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
376     //  Only one nowait clause can appear on a for directive.
377     if (!FirstClause) {
378       Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
379                                                << getOpenMPClauseName(CKind);
380     }
381 
382     Clause = ParseOpenMPClause(CKind);
383     break;
384   case OMPC_private:
385   case OMPC_firstprivate:
386   case OMPC_lastprivate:
387   case OMPC_shared:
388   case OMPC_reduction:
389   case OMPC_linear:
390   case OMPC_aligned:
391   case OMPC_copyin:
392   case OMPC_copyprivate:
393     Clause = ParseOpenMPVarListClause(CKind);
394     break;
395   case OMPC_unknown:
396     Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
397         << getOpenMPDirectiveName(DKind);
398     SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
399     break;
400   case OMPC_threadprivate:
401     Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
402                                                << getOpenMPDirectiveName(DKind);
403     SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
404     break;
405   }
406   return ErrorFound ? nullptr : Clause;
407 }
408 
409 /// \brief Parsing of OpenMP clauses with single expressions like 'if',
410 /// 'final', 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
411 /// 'thread_limit'.
412 ///
413 ///    if-clause:
414 ///      'if' '(' expression ')'
415 ///
416 ///    final-clause:
417 ///      'final' '(' expression ')'
418 ///
419 ///    num_threads-clause:
420 ///      'num_threads' '(' expression ')'
421 ///
422 ///    safelen-clause:
423 ///      'safelen' '(' expression ')'
424 ///
425 ///    collapse-clause:
426 ///      'collapse' '(' expression ')'
427 ///
428 OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
429   SourceLocation Loc = ConsumeToken();
430 
431   BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
432   if (T.expectAndConsume(diag::err_expected_lparen_after,
433                          getOpenMPClauseName(Kind)))
434     return nullptr;
435 
436   ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
437   ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
438 
439   // Parse ')'.
440   T.consumeClose();
441 
442   if (Val.isInvalid())
443     return nullptr;
444 
445   return Actions.ActOnOpenMPSingleExprClause(
446       Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
447 }
448 
449 /// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
450 ///
451 ///    default-clause:
452 ///         'default' '(' 'none' | 'shared' ')
453 ///
454 ///    proc_bind-clause:
455 ///         'proc_bind' '(' 'master' | 'close' | 'spread' ')
456 ///
457 OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
458   SourceLocation Loc = Tok.getLocation();
459   SourceLocation LOpen = ConsumeToken();
460   // Parse '('.
461   BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
462   if (T.expectAndConsume(diag::err_expected_lparen_after,
463                          getOpenMPClauseName(Kind)))
464     return nullptr;
465 
466   unsigned Type = getOpenMPSimpleClauseType(
467       Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
468   SourceLocation TypeLoc = Tok.getLocation();
469   if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
470       Tok.isNot(tok::annot_pragma_openmp_end))
471     ConsumeAnyToken();
472 
473   // Parse ')'.
474   T.consumeClose();
475 
476   return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
477                                          Tok.getLocation());
478 }
479 
480 /// \brief Parsing of OpenMP clauses like 'ordered'.
481 ///
482 ///    ordered-clause:
483 ///         'ordered'
484 ///
485 ///    nowait-clause:
486 ///         'nowait'
487 ///
488 ///    untied-clause:
489 ///         'untied'
490 ///
491 ///    mergeable-clause:
492 ///         'mergeable'
493 ///
494 OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
495   SourceLocation Loc = Tok.getLocation();
496   ConsumeAnyToken();
497 
498   return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
499 }
500 
501 
502 /// \brief Parsing of OpenMP clauses with single expressions and some additional
503 /// argument like 'schedule' or 'dist_schedule'.
504 ///
505 ///    schedule-clause:
506 ///      'schedule' '(' kind [',' expression ] ')'
507 ///
508 OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
509   SourceLocation Loc = ConsumeToken();
510   SourceLocation CommaLoc;
511   // Parse '('.
512   BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
513   if (T.expectAndConsume(diag::err_expected_lparen_after,
514                          getOpenMPClauseName(Kind)))
515     return nullptr;
516 
517   ExprResult Val;
518   unsigned Type = getOpenMPSimpleClauseType(
519       Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
520   SourceLocation KLoc = Tok.getLocation();
521   if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
522       Tok.isNot(tok::annot_pragma_openmp_end))
523     ConsumeAnyToken();
524 
525   if (Kind == OMPC_schedule &&
526       (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
527        Type == OMPC_SCHEDULE_guided) &&
528       Tok.is(tok::comma)) {
529     CommaLoc = ConsumeAnyToken();
530     ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
531     Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
532     if (Val.isInvalid())
533       return nullptr;
534   }
535 
536   // Parse ')'.
537   T.consumeClose();
538 
539   return Actions.ActOnOpenMPSingleExprWithArgClause(
540       Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
541       T.getCloseLocation());
542 }
543 
544 static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
545                              UnqualifiedId &ReductionId) {
546   SourceLocation TemplateKWLoc;
547   if (ReductionIdScopeSpec.isEmpty()) {
548     auto OOK = OO_None;
549     switch (P.getCurToken().getKind()) {
550     case tok::plus:
551       OOK = OO_Plus;
552       break;
553     case tok::minus:
554       OOK = OO_Minus;
555       break;
556     case tok::star:
557       OOK = OO_Star;
558       break;
559     case tok::amp:
560       OOK = OO_Amp;
561       break;
562     case tok::pipe:
563       OOK = OO_Pipe;
564       break;
565     case tok::caret:
566       OOK = OO_Caret;
567       break;
568     case tok::ampamp:
569       OOK = OO_AmpAmp;
570       break;
571     case tok::pipepipe:
572       OOK = OO_PipePipe;
573       break;
574     default:
575       break;
576     }
577     if (OOK != OO_None) {
578       SourceLocation OpLoc = P.ConsumeToken();
579       SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
580       ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
581       return false;
582     }
583   }
584   return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
585                               /*AllowDestructorName*/ false,
586                               /*AllowConstructorName*/ false, ParsedType(),
587                               TemplateKWLoc, ReductionId);
588 }
589 
590 /// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
591 /// 'shared', 'copyin', or 'reduction'.
592 ///
593 ///    private-clause:
594 ///       'private' '(' list ')'
595 ///    firstprivate-clause:
596 ///       'firstprivate' '(' list ')'
597 ///    lastprivate-clause:
598 ///       'lastprivate' '(' list ')'
599 ///    shared-clause:
600 ///       'shared' '(' list ')'
601 ///    linear-clause:
602 ///       'linear' '(' list [ ':' linear-step ] ')'
603 ///    aligned-clause:
604 ///       'aligned' '(' list [ ':' alignment ] ')'
605 ///    reduction-clause:
606 ///       'reduction' '(' reduction-identifier ':' list ')'
607 ///
608 OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
609   SourceLocation Loc = Tok.getLocation();
610   SourceLocation LOpen = ConsumeToken();
611   SourceLocation ColonLoc = SourceLocation();
612   // Optional scope specifier and unqualified id for reduction identifier.
613   CXXScopeSpec ReductionIdScopeSpec;
614   UnqualifiedId ReductionId;
615   bool InvalidReductionId = false;
616   // Parse '('.
617   BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
618   if (T.expectAndConsume(diag::err_expected_lparen_after,
619                          getOpenMPClauseName(Kind)))
620     return nullptr;
621 
622   // Handle reduction-identifier for reduction clause.
623   if (Kind == OMPC_reduction) {
624     ColonProtectionRAIIObject ColonRAII(*this);
625     if (getLangOpts().CPlusPlus) {
626       ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
627     }
628     InvalidReductionId =
629         ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
630     if (InvalidReductionId) {
631       SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
632                 StopBeforeMatch);
633     }
634     if (Tok.is(tok::colon)) {
635       ColonLoc = ConsumeToken();
636     } else {
637       Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
638     }
639   }
640 
641   SmallVector<Expr *, 5> Vars;
642   bool IsComma = !InvalidReductionId;
643   const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
644   while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
645                      Tok.isNot(tok::annot_pragma_openmp_end))) {
646     ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
647     // Parse variable
648     ExprResult VarExpr = ParseAssignmentExpression();
649     if (VarExpr.isUsable()) {
650       Vars.push_back(VarExpr.get());
651     } else {
652       SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
653                 StopBeforeMatch);
654     }
655     // Skip ',' if any
656     IsComma = Tok.is(tok::comma);
657     if (IsComma)
658       ConsumeToken();
659     else if (Tok.isNot(tok::r_paren) &&
660              Tok.isNot(tok::annot_pragma_openmp_end) &&
661              (!MayHaveTail || Tok.isNot(tok::colon)))
662       Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
663   }
664 
665   // Parse ':' linear-step (or ':' alignment).
666   Expr *TailExpr = nullptr;
667   const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
668   if (MustHaveTail) {
669     ColonLoc = Tok.getLocation();
670     ConsumeToken();
671     ExprResult Tail = ParseAssignmentExpression();
672     if (Tail.isUsable())
673       TailExpr = Tail.get();
674     else
675       SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
676                 StopBeforeMatch);
677   }
678 
679   // Parse ')'.
680   T.consumeClose();
681   if (Vars.empty() || (MustHaveTail && !TailExpr) || InvalidReductionId)
682     return nullptr;
683 
684   return Actions.ActOnOpenMPVarListClause(
685       Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
686       ReductionIdScopeSpec,
687       ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
688                             : DeclarationNameInfo());
689 }
690 
691