blob: a7b7d696aa94c4f4f47066a6440bf60cc1f458e0 [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 Bataev6f6f3b42013-05-13 04:18:18 +000028/// \brief Parsing of declarative OpenMP directives.
29///
30/// threadprivate-directive:
31/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataeva769e072013-03-22 06:34:35 +000032///
33Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
34 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000035 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +000036
37 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000038 SmallVector<Expr *, 5> Identifiers;
39 OpenMPDirectiveKind DKind = Tok.isAnnotation() ?
40 OMPD_unknown :
41 getOpenMPDirectiveKind(PP.getSpelling(Tok));
42
43 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +000044 case OMPD_threadprivate:
45 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000046 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +000047 // The last seen token is annot_pragma_openmp_end - need to check for
48 // extra tokens.
49 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
50 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
51 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +000052 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +000053 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000054 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +000055 ConsumeToken();
56 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
Alexey Bataeva769e072013-03-22 06:34:35 +000057 Identifiers);
58 }
59 break;
60 case OMPD_unknown:
61 Diag(Tok, diag::err_omp_unknown_directive);
62 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000063 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +000064 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000065 case OMPD_task:
66 case NUM_OPENMP_DIRECTIVES:
Alexey Bataeva769e072013-03-22 06:34:35 +000067 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000068 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +000069 break;
70 }
Alp Tokerd751fa72013-12-18 19:10:49 +000071 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +000072 return DeclGroupPtrTy();
73}
74
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000075/// \brief Parsing of declarative or executable OpenMP directives.
76///
77/// threadprivate-directive:
78/// annot_pragma_openmp 'threadprivate' simple-variable-list
79/// annot_pragma_openmp_end
80///
81/// parallel-directive:
82/// annot_pragma_openmp 'parallel' {clause} annot_pragma_openmp_end
83///
84StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective() {
85 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000086 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000087 SmallVector<Expr *, 5> Identifiers;
88 SmallVector<OMPClause *, 5> Clauses;
89 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, NUM_OPENMP_CLAUSES>
90 FirstClauses(NUM_OPENMP_CLAUSES);
Alexey Bataev758e55e2013-09-06 18:03:48 +000091 const unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
92 Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000093 SourceLocation Loc = ConsumeToken(), EndLoc;
94 OpenMPDirectiveKind DKind = Tok.isAnnotation() ?
95 OMPD_unknown :
96 getOpenMPDirectiveKind(PP.getSpelling(Tok));
Alexey Bataev758e55e2013-09-06 18:03:48 +000097 // Name of critical directive.
98 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000099 StmtResult Directive = StmtError();
100
101 switch (DKind) {
102 case OMPD_threadprivate:
103 ConsumeToken();
104 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
105 // The last seen token is annot_pragma_openmp_end - need to check for
106 // extra tokens.
107 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
108 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
109 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000110 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000111 }
112 DeclGroupPtrTy Res =
113 Actions.ActOnOpenMPThreadprivateDirective(Loc,
114 Identifiers);
115 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
116 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000117 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000118 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000119 case OMPD_parallel:
120 case OMPD_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000121 ConsumeToken();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000122
123 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope());
124
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000125 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
126 OpenMPClauseKind CKind = Tok.isAnnotation() ?
127 OMPC_unknown :
128 getOpenMPClauseKind(PP.getSpelling(Tok));
129 OMPClause *Clause = ParseOpenMPClause(DKind, CKind,
130 !FirstClauses[CKind].getInt());
131 FirstClauses[CKind].setInt(true);
132 if (Clause) {
133 FirstClauses[CKind].setPointer(Clause);
134 Clauses.push_back(Clause);
135 }
136
137 // Skip ',' if any.
138 if (Tok.is(tok::comma))
139 ConsumeToken();
140 }
141 // End location of the directive.
142 EndLoc = Tok.getLocation();
143 // Consume final annot_pragma_openmp_end.
144 ConsumeToken();
145
146 StmtResult AssociatedStmt;
147 bool CreateDirective = true;
148 ParseScope OMPDirectiveScope(this, ScopeFlags);
149 {
150 // The body is a block scope like in Lambdas and Blocks.
151 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataev9959db52014-05-06 10:08:46 +0000152 Actions.ActOnOpenMPRegionStart(DKind, Loc, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000153 Actions.ActOnStartOfCompoundStmt();
154 // Parse statement
155 AssociatedStmt = ParseStatement();
156 Actions.ActOnFinishOfCompoundStmt();
157 if (!AssociatedStmt.isUsable()) {
158 Actions.ActOnCapturedRegionError();
159 CreateDirective = false;
160 } else {
161 AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.take());
162 CreateDirective = AssociatedStmt.isUsable();
163 }
164 }
165 if (CreateDirective)
166 Directive = Actions.ActOnOpenMPExecutableDirective(DKind, Clauses,
167 AssociatedStmt.take(),
168 Loc, EndLoc);
169
170 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000171 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000172 OMPDirectiveScope.Exit();
173 }
174 break;
175 case OMPD_unknown:
176 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000177 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000178 break;
179 case OMPD_task:
180 case NUM_OPENMP_DIRECTIVES:
181 Diag(Tok, diag::err_omp_unexpected_directive)
182 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000183 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000184 break;
185 }
186 return Directive;
187}
188
Alexey Bataeva769e072013-03-22 06:34:35 +0000189/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000190/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000191///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000192/// simple-variable-list:
193/// '(' id-expression {, id-expression} ')'
194///
195bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
196 SmallVectorImpl<Expr *> &VarList,
197 bool AllowScopeSpecifier) {
198 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000199 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000200 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000201 if (T.expectAndConsume(diag::err_expected_lparen_after,
202 getOpenMPDirectiveName(Kind)))
203 return true;
204 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000205 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000206
207 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000208 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000209 CXXScopeSpec SS;
210 SourceLocation TemplateKWLoc;
211 UnqualifiedId Name;
212 // Read var name.
213 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000214 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000215
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000216 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
217 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000218 IsCorrect = false;
219 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000220 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000221 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
222 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000223 IsCorrect = false;
224 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000225 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000226 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
227 Tok.isNot(tok::annot_pragma_openmp_end)) {
228 IsCorrect = false;
229 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000230 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000231 Diag(PrevTok.getLocation(), diag::err_expected)
232 << tok::identifier
233 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000234 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000235 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
236 ExprResult Res = Actions.ActOnOpenMPIdExpression(getCurScope(), SS,
237 NameInfo);
238 if (Res.isUsable())
239 VarList.push_back(Res.take());
Alexey Bataeva769e072013-03-22 06:34:35 +0000240 }
241 // Consume ','.
242 if (Tok.is(tok::comma)) {
243 ConsumeToken();
244 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000245 }
246
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000247 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000248 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000249 IsCorrect = false;
250 }
251
252 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000253 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000254
255 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000256}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000257
258/// \brief Parsing of OpenMP clauses.
259///
260/// clause:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000261/// if-clause | num_threads-clause | safelen-clause | default-clause |
Alexander Musman8dba6642014-04-22 13:09:42 +0000262/// private-clause | firstprivate-clause | shared-clause | linear-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000263///
264OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
265 OpenMPClauseKind CKind, bool FirstClause) {
266 OMPClause *Clause = 0;
267 bool ErrorFound = false;
268 // Check if clause is allowed for the given directive.
269 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
270 Diag(Tok, diag::err_omp_unexpected_clause)
271 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
272 ErrorFound = true;
273 }
274
275 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000276 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +0000277 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000278 case OMPC_safelen:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000279 // OpenMP [2.5, Restrictions]
280 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000281 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000282 // OpenMP [2.8.1, simd construct, Restrictions]
283 // Only one safelen clause can appear on a simd directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000284 if (!FirstClause) {
285 Diag(Tok, diag::err_omp_more_one_clause)
286 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind);
287 }
288
289 Clause = ParseOpenMPSingleExprClause(CKind);
290 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000291 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000292 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000293 // OpenMP [2.14.3.1, Restrictions]
294 // Only a single default clause may be specified on a parallel, task or
295 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000296 // OpenMP [2.5, parallel Construct, Restrictions]
297 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000298 if (!FirstClause) {
299 Diag(Tok, diag::err_omp_more_one_clause)
300 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind);
301 }
302
303 Clause = ParseOpenMPSimpleClause(CKind);
304 break;
305 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000306 case OMPC_firstprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000307 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +0000308 case OMPC_linear:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000309 case OMPC_copyin:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000310 Clause = ParseOpenMPVarListClause(CKind);
311 break;
312 case OMPC_unknown:
313 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
314 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000315 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000316 break;
317 case OMPC_threadprivate:
318 case NUM_OPENMP_CLAUSES:
319 Diag(Tok, diag::err_omp_unexpected_clause)
320 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000321 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000322 break;
323 }
324 return ErrorFound ? 0 : Clause;
325}
326
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000327/// \brief Parsing of OpenMP clauses with single expressions like 'if',
328/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
329/// 'thread_limit'.
330///
331/// if-clause:
332/// 'if' '(' expression ')'
333///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000334/// num_threads-clause:
335/// 'num_threads' '(' expression ')'
336///
337/// safelen-clause:
338/// 'safelen' '(' expression ')'
339///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000340OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
341 SourceLocation Loc = ConsumeToken();
342
343 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
344 if (T.expectAndConsume(diag::err_expected_lparen_after,
345 getOpenMPClauseName(Kind)))
346 return 0;
347
348 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
349 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
350
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000351 // Parse ')'.
352 T.consumeClose();
353
354 if (Val.isInvalid())
355 return 0;
356
357 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.take(), Loc,
358 T.getOpenLocation(),
359 T.getCloseLocation());
360}
361
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000362/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000363///
364/// default-clause:
365/// 'default' '(' 'none' | 'shared' ')
366///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000367/// proc_bind-clause:
368/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
369///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000370OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
371 SourceLocation Loc = Tok.getLocation();
372 SourceLocation LOpen = ConsumeToken();
373 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000374 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000375 if (T.expectAndConsume(diag::err_expected_lparen_after,
376 getOpenMPClauseName(Kind)))
377 return 0;
378
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000379 unsigned Type =
380 getOpenMPSimpleClauseType(Kind,
381 Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000382 SourceLocation TypeLoc = Tok.getLocation();
383 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
384 Tok.isNot(tok::annot_pragma_openmp_end))
385 ConsumeAnyToken();
386
387 // Parse ')'.
388 T.consumeClose();
389
390 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
391 Tok.getLocation());
392}
393
394/// \brief Parsing of OpenMP clause 'private', 'firstprivate',
395/// 'shared', 'copyin', or 'reduction'.
396///
397/// private-clause:
398/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000399/// firstprivate-clause:
400/// 'firstprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000401/// shared-clause:
402/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000403/// linear-clause:
404/// 'linear' '(' list [ ':' linear-step ] ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000405///
406OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
407 SourceLocation Loc = Tok.getLocation();
408 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000409 SourceLocation ColonLoc = SourceLocation();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000410 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000411 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000412 if (T.expectAndConsume(diag::err_expected_lparen_after,
413 getOpenMPClauseName(Kind)))
414 return 0;
415
416 SmallVector<Expr *, 5> Vars;
417 bool IsComma = true;
Alexander Musman8dba6642014-04-22 13:09:42 +0000418 const bool MayHaveTail = (Kind == OMPC_linear);
419 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000420 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000421 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000422 // Parse variable
423 ExprResult VarExpr = ParseAssignmentExpression();
424 if (VarExpr.isUsable()) {
425 Vars.push_back(VarExpr.take());
426 } else {
427 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000428 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000429 }
430 // Skip ',' if any
431 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000432 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000433 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000434 else if (Tok.isNot(tok::r_paren) &&
435 Tok.isNot(tok::annot_pragma_openmp_end) &&
436 (!MayHaveTail || Tok.isNot(tok::colon)))
437 Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
438 }
439
440 // Parse ':' linear-step
441 Expr *TailExpr = 0;
442 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
443 if (MustHaveTail) {
444 ColonLoc = Tok.getLocation();
445 ConsumeToken();
446 ExprResult Tail = ParseAssignmentExpression();
447 if (Tail.isUsable())
448 TailExpr = Tail.take();
449 else
450 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
451 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000452 }
453
454 // Parse ')'.
455 T.consumeClose();
Alexander Musman8dba6642014-04-22 13:09:42 +0000456 if (Vars.empty() || (MustHaveTail && !TailExpr))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000457 return 0;
458
Alexander Musman8dba6642014-04-22 13:09:42 +0000459 return Actions.ActOnOpenMPVarListClause(Kind, Vars, TailExpr, Loc, LOpen,
460 ColonLoc, Tok.getLocation());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000461}
462