blob: 1682c83bab1fe7c77b2eab00195667cbbea7d4fb [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- 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
Chandler Carruth5553d0d2014-01-07 11:51:46 +000014#include "RAIIObjectsForParser.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTConsumer.h"
16#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000017#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000019#include "clang/Parse/Parser.h"
20#include "clang/Sema/Scope.h"
21#include "llvm/ADT/PointerIntPair.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// OpenMP declarative directives.
26//===----------------------------------------------------------------------===//
27
Alexey Bataev4acb8592014-07-07 13:01:15 +000028static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000029 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
30 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
31 // TODO: add other combined directives in topological order.
32 const OpenMPDirectiveKind F[][3] = {
33 { OMPD_for, OMPD_simd, OMPD_for_simd },
34 { OMPD_parallel, OMPD_for, OMPD_parallel_for },
Alexander Musmane4e893b2014-09-23 09:33:00 +000035 { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
Alexander Musmanf82886e2014-09-18 05:12:34 +000036 { OMPD_parallel, OMPD_sections, OMPD_parallel_sections }
37 };
Alexey Bataev4acb8592014-07-07 13:01:15 +000038 auto Tok = P.getCurToken();
39 auto DKind =
40 Tok.isAnnotation()
41 ? OMPD_unknown
42 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Alexander Musmanf82886e2014-09-18 05:12:34 +000043 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
44 if (DKind == F[i][0]) {
45 Tok = P.getPreprocessor().LookAhead(0);
46 auto SDKind =
47 Tok.isAnnotation()
48 ? OMPD_unknown
49 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
50 if (SDKind == F[i][1]) {
51 P.ConsumeToken();
52 DKind = F[i][2];
53 }
Alexey Bataev4acb8592014-07-07 13:01:15 +000054 }
55 }
56 return DKind;
57}
58
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000059/// \brief Parsing of declarative OpenMP directives.
60///
61/// threadprivate-directive:
62/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataeva769e072013-03-22 06:34:35 +000063///
64Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
65 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000066 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +000067
68 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000069 SmallVector<Expr *, 5> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +000070 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000071
72 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +000073 case OMPD_threadprivate:
74 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000075 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +000076 // The last seen token is annot_pragma_openmp_end - need to check for
77 // extra tokens.
78 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
79 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +000080 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +000081 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +000082 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000083 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +000084 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +000085 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +000086 }
87 break;
88 case OMPD_unknown:
89 Diag(Tok, diag::err_omp_unknown_directive);
90 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000091 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +000092 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000093 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +000094 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +000095 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +000096 case OMPD_taskwait:
Alexey Bataev6125da92014-07-21 11:26:11 +000097 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +000098 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +000099 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000100 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000101 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000102 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000103 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000104 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000105 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000106 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000107 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000108 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000109 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000110 case OMPD_target:
Alexey Bataeva769e072013-03-22 06:34:35 +0000111 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000112 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000113 break;
114 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000115 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +0000116 return DeclGroupPtrTy();
117}
118
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000119/// \brief Parsing of declarative or executable OpenMP directives.
120///
121/// threadprivate-directive:
122/// annot_pragma_openmp 'threadprivate' simple-variable-list
123/// annot_pragma_openmp_end
124///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000125/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000126/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000127/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
128/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000129/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Alexander Musmane4e893b2014-09-23 09:33:00 +0000130/// 'for simd' | 'parallel for simd' | 'target' {clause}
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000131/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000132///
Alexey Bataev68446b72014-07-18 07:47:19 +0000133StmtResult
134Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000135 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000136 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000137 SmallVector<Expr *, 5> Identifiers;
138 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000139 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000140 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000141 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000142 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000143 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000144 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000145 // Name of critical directive.
146 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000147 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000148 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000149 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000150
151 switch (DKind) {
152 case OMPD_threadprivate:
153 ConsumeToken();
154 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
155 // The last seen token is annot_pragma_openmp_end - need to check for
156 // extra tokens.
157 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
158 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000159 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000160 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000161 }
162 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000163 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000164 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
165 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000166 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000167 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000168 case OMPD_flush:
169 if (PP.LookAhead(0).is(tok::l_paren)) {
170 FlushHasClause = true;
171 // Push copy of the current token back to stream to properly parse
172 // pseudo-clause OMPFlushClause.
173 PP.EnterToken(Tok);
174 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000175 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000176 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000177 case OMPD_taskwait:
Alexey Bataev68446b72014-07-18 07:47:19 +0000178 if (!StandAloneAllowed) {
179 Diag(Tok, diag::err_omp_immediate_directive)
180 << getOpenMPDirectiveName(DKind);
181 }
182 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000183 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000184 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000185 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000186 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000187 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000188 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000189 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000190 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000191 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000192 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000193 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000194 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000195 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000196 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000197 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000198 case OMPD_atomic:
199 case OMPD_target: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000200 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000201 // Parse directive name of the 'critical' directive if any.
202 if (DKind == OMPD_critical) {
203 BalancedDelimiterTracker T(*this, tok::l_paren,
204 tok::annot_pragma_openmp_end);
205 if (!T.consumeOpen()) {
206 if (Tok.isAnyIdentifier()) {
207 DirName =
208 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
209 ConsumeAnyToken();
210 } else {
211 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
212 }
213 T.consumeClose();
214 }
215 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000216
Alexey Bataevf29276e2014-06-18 04:14:57 +0000217 if (isOpenMPLoopDirective(DKind))
218 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
219 if (isOpenMPSimdDirective(DKind))
220 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
221 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000222 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000223
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000224 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000225 OpenMPClauseKind CKind =
226 Tok.isAnnotation()
227 ? OMPC_unknown
228 : FlushHasClause ? OMPC_flush
229 : getOpenMPClauseKind(PP.getSpelling(Tok));
230 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000231 OMPClause *Clause =
232 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000233 FirstClauses[CKind].setInt(true);
234 if (Clause) {
235 FirstClauses[CKind].setPointer(Clause);
236 Clauses.push_back(Clause);
237 }
238
239 // Skip ',' if any.
240 if (Tok.is(tok::comma))
241 ConsumeToken();
242 }
243 // End location of the directive.
244 EndLoc = Tok.getLocation();
245 // Consume final annot_pragma_openmp_end.
246 ConsumeToken();
247
248 StmtResult AssociatedStmt;
249 bool CreateDirective = true;
Alexey Bataev68446b72014-07-18 07:47:19 +0000250 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000251 // The body is a block scope like in Lambdas and Blocks.
252 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000253 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000254 Actions.ActOnStartOfCompoundStmt();
255 // Parse statement
256 AssociatedStmt = ParseStatement();
257 Actions.ActOnFinishOfCompoundStmt();
258 if (!AssociatedStmt.isUsable()) {
259 Actions.ActOnCapturedRegionError();
260 CreateDirective = false;
261 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000262 AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000263 CreateDirective = AssociatedStmt.isUsable();
264 }
265 }
266 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000267 Directive = Actions.ActOnOpenMPExecutableDirective(
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000268 DKind, DirName, Clauses, AssociatedStmt.get(), Loc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000269
270 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000271 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000272 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000273 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000274 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000275 case OMPD_unknown:
276 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000277 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000278 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000279 }
280 return Directive;
281}
282
Alexey Bataeva769e072013-03-22 06:34:35 +0000283/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000284/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000285///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000286/// simple-variable-list:
287/// '(' id-expression {, id-expression} ')'
288///
289bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
290 SmallVectorImpl<Expr *> &VarList,
291 bool AllowScopeSpecifier) {
292 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000293 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000294 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000295 if (T.expectAndConsume(diag::err_expected_lparen_after,
296 getOpenMPDirectiveName(Kind)))
297 return true;
298 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000299 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000300
301 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000302 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000303 CXXScopeSpec SS;
304 SourceLocation TemplateKWLoc;
305 UnqualifiedId Name;
306 // Read var name.
307 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000308 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000309
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000310 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
311 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000312 IsCorrect = false;
313 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000314 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000315 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
316 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000317 IsCorrect = false;
318 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000319 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000320 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
321 Tok.isNot(tok::annot_pragma_openmp_end)) {
322 IsCorrect = false;
323 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000324 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000325 Diag(PrevTok.getLocation(), diag::err_expected)
326 << tok::identifier
327 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000328 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000329 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000330 ExprResult Res =
331 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000332 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000333 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000334 }
335 // Consume ','.
336 if (Tok.is(tok::comma)) {
337 ConsumeToken();
338 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000339 }
340
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000341 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000342 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000343 IsCorrect = false;
344 }
345
346 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000347 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000348
349 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000350}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000351
352/// \brief Parsing of OpenMP clauses.
353///
354/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000355/// if-clause | final-clause | num_threads-clause | safelen-clause |
356/// default-clause | private-clause | firstprivate-clause | shared-clause
357/// | linear-clause | aligned-clause | collapse-clause |
358/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000359/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000360/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000361/// update-clause | capture-clause | seq_cst-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000362///
363OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
364 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000365 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000366 bool ErrorFound = false;
367 // Check if clause is allowed for the given directive.
368 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000369 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
370 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000371 ErrorFound = true;
372 }
373
374 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000375 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +0000376 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000377 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000378 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000379 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000380 // OpenMP [2.5, Restrictions]
381 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000382 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000383 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000384 // Only one safelen clause can appear on a simd directive.
385 // Only one collapse clause can appear on a simd directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000386 // OpenMP [2.11.1, task Construct, Restrictions]
387 // At most one if clause can appear on the directive.
388 // At most one final clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000389 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000390 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
391 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000392 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000393 }
394
395 Clause = ParseOpenMPSingleExprClause(CKind);
396 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000397 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000398 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000399 // OpenMP [2.14.3.1, Restrictions]
400 // Only a single default clause may be specified on a parallel, task or
401 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000402 // OpenMP [2.5, parallel Construct, Restrictions]
403 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000404 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000405 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
406 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000407 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000408 }
409
410 Clause = ParseOpenMPSimpleClause(CKind);
411 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000412 case OMPC_schedule:
413 // OpenMP [2.7.1, Restrictions, p. 3]
414 // Only one schedule clause can appear on a loop directive.
415 if (!FirstClause) {
416 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
417 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000418 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000419 }
420
421 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
422 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000423 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +0000424 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000425 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000426 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000427 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000428 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000429 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000430 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000431 case OMPC_seq_cst:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000432 // OpenMP [2.7.1, Restrictions, p. 9]
433 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000434 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
435 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000436 if (!FirstClause) {
437 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
438 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000439 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000440 }
441
442 Clause = ParseOpenMPClause(CKind);
443 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000444 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000445 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000446 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000447 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000448 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000449 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000450 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000451 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000452 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000453 case OMPC_flush:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000454 Clause = ParseOpenMPVarListClause(CKind);
455 break;
456 case OMPC_unknown:
457 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000458 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000459 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000460 break;
461 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000462 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
463 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000464 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000465 break;
466 }
Craig Topper161e4db2014-05-21 06:02:52 +0000467 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000468}
469
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000470/// \brief Parsing of OpenMP clauses with single expressions like 'if',
Alexey Bataev3778b602014-07-17 07:32:53 +0000471/// 'final', 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000472/// 'thread_limit'.
473///
474/// if-clause:
475/// 'if' '(' expression ')'
476///
Alexey Bataev3778b602014-07-17 07:32:53 +0000477/// final-clause:
478/// 'final' '(' expression ')'
479///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000480/// num_threads-clause:
481/// 'num_threads' '(' expression ')'
482///
483/// safelen-clause:
484/// 'safelen' '(' expression ')'
485///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000486/// collapse-clause:
487/// 'collapse' '(' expression ')'
488///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000489OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
490 SourceLocation Loc = ConsumeToken();
491
492 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
493 if (T.expectAndConsume(diag::err_expected_lparen_after,
494 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000495 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000496
497 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
498 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
499
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000500 // Parse ')'.
501 T.consumeClose();
502
503 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000504 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000505
Alexey Bataeva55ed262014-05-28 06:15:33 +0000506 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000507 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000508}
509
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000510/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000511///
512/// default-clause:
513/// 'default' '(' 'none' | 'shared' ')
514///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000515/// proc_bind-clause:
516/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
517///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000518OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
519 SourceLocation Loc = Tok.getLocation();
520 SourceLocation LOpen = ConsumeToken();
521 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000522 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000523 if (T.expectAndConsume(diag::err_expected_lparen_after,
524 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000525 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000526
Alexey Bataeva55ed262014-05-28 06:15:33 +0000527 unsigned Type = getOpenMPSimpleClauseType(
528 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000529 SourceLocation TypeLoc = Tok.getLocation();
530 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
531 Tok.isNot(tok::annot_pragma_openmp_end))
532 ConsumeAnyToken();
533
534 // Parse ')'.
535 T.consumeClose();
536
537 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
538 Tok.getLocation());
539}
540
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000541/// \brief Parsing of OpenMP clauses like 'ordered'.
542///
543/// ordered-clause:
544/// 'ordered'
545///
Alexey Bataev236070f2014-06-20 11:19:47 +0000546/// nowait-clause:
547/// 'nowait'
548///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000549/// untied-clause:
550/// 'untied'
551///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000552/// mergeable-clause:
553/// 'mergeable'
554///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000555/// read-clause:
556/// 'read'
557///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000558OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
559 SourceLocation Loc = Tok.getLocation();
560 ConsumeAnyToken();
561
562 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
563}
564
565
Alexey Bataev56dafe82014-06-20 07:16:17 +0000566/// \brief Parsing of OpenMP clauses with single expressions and some additional
567/// argument like 'schedule' or 'dist_schedule'.
568///
569/// schedule-clause:
570/// 'schedule' '(' kind [',' expression ] ')'
571///
572OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
573 SourceLocation Loc = ConsumeToken();
574 SourceLocation CommaLoc;
575 // Parse '('.
576 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
577 if (T.expectAndConsume(diag::err_expected_lparen_after,
578 getOpenMPClauseName(Kind)))
579 return nullptr;
580
581 ExprResult Val;
582 unsigned Type = getOpenMPSimpleClauseType(
583 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
584 SourceLocation KLoc = Tok.getLocation();
585 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
586 Tok.isNot(tok::annot_pragma_openmp_end))
587 ConsumeAnyToken();
588
589 if (Kind == OMPC_schedule &&
590 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
591 Type == OMPC_SCHEDULE_guided) &&
592 Tok.is(tok::comma)) {
593 CommaLoc = ConsumeAnyToken();
594 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
595 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
596 if (Val.isInvalid())
597 return nullptr;
598 }
599
600 // Parse ')'.
601 T.consumeClose();
602
603 return Actions.ActOnOpenMPSingleExprWithArgClause(
604 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
605 T.getCloseLocation());
606}
607
Alexey Bataevc5e02582014-06-16 07:08:35 +0000608static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
609 UnqualifiedId &ReductionId) {
610 SourceLocation TemplateKWLoc;
611 if (ReductionIdScopeSpec.isEmpty()) {
612 auto OOK = OO_None;
613 switch (P.getCurToken().getKind()) {
614 case tok::plus:
615 OOK = OO_Plus;
616 break;
617 case tok::minus:
618 OOK = OO_Minus;
619 break;
620 case tok::star:
621 OOK = OO_Star;
622 break;
623 case tok::amp:
624 OOK = OO_Amp;
625 break;
626 case tok::pipe:
627 OOK = OO_Pipe;
628 break;
629 case tok::caret:
630 OOK = OO_Caret;
631 break;
632 case tok::ampamp:
633 OOK = OO_AmpAmp;
634 break;
635 case tok::pipepipe:
636 OOK = OO_PipePipe;
637 break;
638 default:
639 break;
640 }
641 if (OOK != OO_None) {
642 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000643 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000644 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
645 return false;
646 }
647 }
648 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
649 /*AllowDestructorName*/ false,
650 /*AllowConstructorName*/ false, ParsedType(),
651 TemplateKWLoc, ReductionId);
652}
653
Alexander Musman1bb328c2014-06-04 13:06:39 +0000654/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000655/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000656///
657/// private-clause:
658/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000659/// firstprivate-clause:
660/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000661/// lastprivate-clause:
662/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000663/// shared-clause:
664/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000665/// linear-clause:
666/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000667/// aligned-clause:
668/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000669/// reduction-clause:
670/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000671/// copyprivate-clause:
672/// 'copyprivate' '(' list ')'
673/// flush-clause:
674/// 'flush' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000675///
676OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
677 SourceLocation Loc = Tok.getLocation();
678 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000679 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000680 // Optional scope specifier and unqualified id for reduction identifier.
681 CXXScopeSpec ReductionIdScopeSpec;
682 UnqualifiedId ReductionId;
683 bool InvalidReductionId = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000684 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000685 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000686 if (T.expectAndConsume(diag::err_expected_lparen_after,
687 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000688 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000689
Alexey Bataevc5e02582014-06-16 07:08:35 +0000690 // Handle reduction-identifier for reduction clause.
691 if (Kind == OMPC_reduction) {
692 ColonProtectionRAIIObject ColonRAII(*this);
693 if (getLangOpts().CPlusPlus) {
694 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
695 }
696 InvalidReductionId =
697 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
698 if (InvalidReductionId) {
699 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
700 StopBeforeMatch);
701 }
702 if (Tok.is(tok::colon)) {
703 ColonLoc = ConsumeToken();
704 } else {
705 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
706 }
707 }
708
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000709 SmallVector<Expr *, 5> Vars;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000710 bool IsComma = !InvalidReductionId;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000711 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000712 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000713 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000714 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000715 // Parse variable
716 ExprResult VarExpr = ParseAssignmentExpression();
717 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000718 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000719 } else {
720 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000721 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000722 }
723 // Skip ',' if any
724 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000725 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000726 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000727 else if (Tok.isNot(tok::r_paren) &&
728 Tok.isNot(tok::annot_pragma_openmp_end) &&
729 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +0000730 Diag(Tok, diag::err_omp_expected_punc)
731 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
732 : getOpenMPClauseName(Kind))
733 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +0000734 }
735
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000736 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000737 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000738 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
739 if (MustHaveTail) {
740 ColonLoc = Tok.getLocation();
741 ConsumeToken();
742 ExprResult Tail = ParseAssignmentExpression();
743 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000744 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000745 else
746 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
747 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000748 }
749
750 // Parse ')'.
751 T.consumeClose();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000752 if (Vars.empty() || (MustHaveTail && !TailExpr) || InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000753 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000754
Alexey Bataevc5e02582014-06-16 07:08:35 +0000755 return Actions.ActOnOpenMPVarListClause(
756 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
757 ReductionIdScopeSpec,
758 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
759 : DeclarationNameInfo());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000760}
761