blob: c8da31706b11223a6c431ac3f11af6177086104a [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
14#include "clang/AST/ASTConsumer.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000015#include "RAIIObjectsForParser.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000016#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000017#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000018#include "clang/Parse/Parser.h"
19#include "clang/Sema/Scope.h"
20#include "llvm/ADT/PointerIntPair.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// OpenMP declarative directives.
25//===----------------------------------------------------------------------===//
26
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000027/// \brief Parsing of declarative OpenMP directives.
28///
29/// threadprivate-directive:
30/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataeva769e072013-03-22 06:34:35 +000031///
32Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
33 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000034 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +000035
36 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000037 SmallVector<Expr *, 5> Identifiers;
38 OpenMPDirectiveKind DKind = Tok.isAnnotation() ?
39 OMPD_unknown :
40 getOpenMPDirectiveKind(PP.getSpelling(Tok));
41
42 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +000043 case OMPD_threadprivate:
44 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000045 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +000046 // The last seen token is annot_pragma_openmp_end - need to check for
47 // extra tokens.
48 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
49 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
50 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +000051 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +000052 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000053 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +000054 ConsumeToken();
55 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
Alexey Bataeva769e072013-03-22 06:34:35 +000056 Identifiers);
57 }
58 break;
59 case OMPD_unknown:
60 Diag(Tok, diag::err_omp_unknown_directive);
61 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000062 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +000063 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000064 case OMPD_task:
65 case NUM_OPENMP_DIRECTIVES:
Alexey Bataeva769e072013-03-22 06:34:35 +000066 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000067 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +000068 break;
69 }
Alp Tokerd751fa72013-12-18 19:10:49 +000070 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +000071 return DeclGroupPtrTy();
72}
73
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000074/// \brief Parsing of declarative or executable OpenMP directives.
75///
76/// threadprivate-directive:
77/// annot_pragma_openmp 'threadprivate' simple-variable-list
78/// annot_pragma_openmp_end
79///
80/// parallel-directive:
81/// annot_pragma_openmp 'parallel' {clause} annot_pragma_openmp_end
82///
83StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective() {
84 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000085 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000086 SmallVector<Expr *, 5> Identifiers;
87 SmallVector<OMPClause *, 5> Clauses;
88 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, NUM_OPENMP_CLAUSES>
89 FirstClauses(NUM_OPENMP_CLAUSES);
Alexey Bataev758e55e2013-09-06 18:03:48 +000090 const unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
91 Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000092 SourceLocation Loc = ConsumeToken(), EndLoc;
93 OpenMPDirectiveKind DKind = Tok.isAnnotation() ?
94 OMPD_unknown :
95 getOpenMPDirectiveKind(PP.getSpelling(Tok));
Alexey Bataev758e55e2013-09-06 18:03:48 +000096 // Name of critical directive.
97 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000098 StmtResult Directive = StmtError();
99
100 switch (DKind) {
101 case OMPD_threadprivate:
102 ConsumeToken();
103 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
104 // The last seen token is annot_pragma_openmp_end - need to check for
105 // extra tokens.
106 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
107 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
108 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000109 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000110 }
111 DeclGroupPtrTy Res =
112 Actions.ActOnOpenMPThreadprivateDirective(Loc,
113 Identifiers);
114 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
115 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000116 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000117 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000118 case OMPD_parallel:
119 case OMPD_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000120 ConsumeToken();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000121
122 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope());
123
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000124 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
125 OpenMPClauseKind CKind = Tok.isAnnotation() ?
126 OMPC_unknown :
127 getOpenMPClauseKind(PP.getSpelling(Tok));
128 OMPClause *Clause = ParseOpenMPClause(DKind, CKind,
129 !FirstClauses[CKind].getInt());
130 FirstClauses[CKind].setInt(true);
131 if (Clause) {
132 FirstClauses[CKind].setPointer(Clause);
133 Clauses.push_back(Clause);
134 }
135
136 // Skip ',' if any.
137 if (Tok.is(tok::comma))
138 ConsumeToken();
139 }
140 // End location of the directive.
141 EndLoc = Tok.getLocation();
142 // Consume final annot_pragma_openmp_end.
143 ConsumeToken();
144
145 StmtResult AssociatedStmt;
146 bool CreateDirective = true;
147 ParseScope OMPDirectiveScope(this, ScopeFlags);
148 {
149 // The body is a block scope like in Lambdas and Blocks.
150 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000151 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_OpenMP, 1);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000152 Actions.ActOnStartOfCompoundStmt();
153 // Parse statement
154 AssociatedStmt = ParseStatement();
155 Actions.ActOnFinishOfCompoundStmt();
156 if (!AssociatedStmt.isUsable()) {
157 Actions.ActOnCapturedRegionError();
158 CreateDirective = false;
159 } else {
160 AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.take());
161 CreateDirective = AssociatedStmt.isUsable();
162 }
163 }
164 if (CreateDirective)
165 Directive = Actions.ActOnOpenMPExecutableDirective(DKind, Clauses,
166 AssociatedStmt.take(),
167 Loc, EndLoc);
168
169 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000170 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000171 OMPDirectiveScope.Exit();
172 }
173 break;
174 case OMPD_unknown:
175 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000176 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000177 break;
178 case OMPD_task:
179 case NUM_OPENMP_DIRECTIVES:
180 Diag(Tok, diag::err_omp_unexpected_directive)
181 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000182 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000183 break;
184 }
185 return Directive;
186}
187
Alexey Bataeva769e072013-03-22 06:34:35 +0000188/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000189/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000190///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000191/// simple-variable-list:
192/// '(' id-expression {, id-expression} ')'
193///
194bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
195 SmallVectorImpl<Expr *> &VarList,
196 bool AllowScopeSpecifier) {
197 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000198 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000199 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000200 if (T.expectAndConsume(diag::err_expected_lparen_after,
201 getOpenMPDirectiveName(Kind)))
202 return true;
203 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000204 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000205
206 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000207 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000208 CXXScopeSpec SS;
209 SourceLocation TemplateKWLoc;
210 UnqualifiedId Name;
211 // Read var name.
212 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000213 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000214
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000215 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
216 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000217 IsCorrect = false;
218 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000219 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000220 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
221 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000222 IsCorrect = false;
223 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000224 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000225 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
226 Tok.isNot(tok::annot_pragma_openmp_end)) {
227 IsCorrect = false;
228 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000229 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000230 Diag(PrevTok.getLocation(), diag::err_expected)
231 << tok::identifier
232 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000233 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000234 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
235 ExprResult Res = Actions.ActOnOpenMPIdExpression(getCurScope(), SS,
236 NameInfo);
237 if (Res.isUsable())
238 VarList.push_back(Res.take());
Alexey Bataeva769e072013-03-22 06:34:35 +0000239 }
240 // Consume ','.
241 if (Tok.is(tok::comma)) {
242 ConsumeToken();
243 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000244 }
245
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000246 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000247 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000248 IsCorrect = false;
249 }
250
251 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000252 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000253
254 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000255}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000256
257/// \brief Parsing of OpenMP clauses.
258///
259/// clause:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000260/// if-clause | num_threads-clause | safelen-clause | default-clause |
Alexander Musman8dba6642014-04-22 13:09:42 +0000261/// private-clause | firstprivate-clause | shared-clause | linear-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000262///
263OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
264 OpenMPClauseKind CKind, bool FirstClause) {
265 OMPClause *Clause = 0;
266 bool ErrorFound = false;
267 // Check if clause is allowed for the given directive.
268 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
269 Diag(Tok, diag::err_omp_unexpected_clause)
270 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
271 ErrorFound = true;
272 }
273
274 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000275 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +0000276 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000277 case OMPC_safelen:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000278 // OpenMP [2.5, Restrictions]
279 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000280 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000281 // OpenMP [2.8.1, simd construct, Restrictions]
282 // Only one safelen clause can appear on a simd directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000283 if (!FirstClause) {
284 Diag(Tok, diag::err_omp_more_one_clause)
285 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind);
286 }
287
288 Clause = ParseOpenMPSingleExprClause(CKind);
289 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000290 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000291 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000292 // OpenMP [2.14.3.1, Restrictions]
293 // Only a single default clause may be specified on a parallel, task or
294 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000295 // OpenMP [2.5, parallel Construct, Restrictions]
296 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000297 if (!FirstClause) {
298 Diag(Tok, diag::err_omp_more_one_clause)
299 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind);
300 }
301
302 Clause = ParseOpenMPSimpleClause(CKind);
303 break;
304 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000305 case OMPC_firstprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000306 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +0000307 case OMPC_linear:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000308 case OMPC_copyin:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000309 Clause = ParseOpenMPVarListClause(CKind);
310 break;
311 case OMPC_unknown:
312 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
313 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000314 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000315 break;
316 case OMPC_threadprivate:
317 case NUM_OPENMP_CLAUSES:
318 Diag(Tok, diag::err_omp_unexpected_clause)
319 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000320 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000321 break;
322 }
323 return ErrorFound ? 0 : Clause;
324}
325
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000326/// \brief Parsing of OpenMP clauses with single expressions like 'if',
327/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
328/// 'thread_limit'.
329///
330/// if-clause:
331/// 'if' '(' expression ')'
332///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000333/// num_threads-clause:
334/// 'num_threads' '(' expression ')'
335///
336/// safelen-clause:
337/// 'safelen' '(' expression ')'
338///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000339OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
340 SourceLocation Loc = ConsumeToken();
341
342 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
343 if (T.expectAndConsume(diag::err_expected_lparen_after,
344 getOpenMPClauseName(Kind)))
345 return 0;
346
347 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
348 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
349
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000350 // Parse ')'.
351 T.consumeClose();
352
353 if (Val.isInvalid())
354 return 0;
355
356 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.take(), Loc,
357 T.getOpenLocation(),
358 T.getCloseLocation());
359}
360
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000361/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000362///
363/// default-clause:
364/// 'default' '(' 'none' | 'shared' ')
365///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000366/// proc_bind-clause:
367/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
368///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000369OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
370 SourceLocation Loc = Tok.getLocation();
371 SourceLocation LOpen = ConsumeToken();
372 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000373 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000374 if (T.expectAndConsume(diag::err_expected_lparen_after,
375 getOpenMPClauseName(Kind)))
376 return 0;
377
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000378 unsigned Type =
379 getOpenMPSimpleClauseType(Kind,
380 Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000381 SourceLocation TypeLoc = Tok.getLocation();
382 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
383 Tok.isNot(tok::annot_pragma_openmp_end))
384 ConsumeAnyToken();
385
386 // Parse ')'.
387 T.consumeClose();
388
389 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
390 Tok.getLocation());
391}
392
393/// \brief Parsing of OpenMP clause 'private', 'firstprivate',
394/// 'shared', 'copyin', or 'reduction'.
395///
396/// private-clause:
397/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000398/// firstprivate-clause:
399/// 'firstprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000400/// shared-clause:
401/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000402/// linear-clause:
403/// 'linear' '(' list [ ':' linear-step ] ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000404///
405OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
406 SourceLocation Loc = Tok.getLocation();
407 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000408 SourceLocation ColonLoc = SourceLocation();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000409 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000410 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000411 if (T.expectAndConsume(diag::err_expected_lparen_after,
412 getOpenMPClauseName(Kind)))
413 return 0;
414
415 SmallVector<Expr *, 5> Vars;
416 bool IsComma = true;
Alexander Musman8dba6642014-04-22 13:09:42 +0000417 const bool MayHaveTail = (Kind == OMPC_linear);
418 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000419 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000420 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000421 // Parse variable
422 ExprResult VarExpr = ParseAssignmentExpression();
423 if (VarExpr.isUsable()) {
424 Vars.push_back(VarExpr.take());
425 } else {
426 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000427 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000428 }
429 // Skip ',' if any
430 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000431 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000432 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000433 else if (Tok.isNot(tok::r_paren) &&
434 Tok.isNot(tok::annot_pragma_openmp_end) &&
435 (!MayHaveTail || Tok.isNot(tok::colon)))
436 Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
437 }
438
439 // Parse ':' linear-step
440 Expr *TailExpr = 0;
441 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
442 if (MustHaveTail) {
443 ColonLoc = Tok.getLocation();
444 ConsumeToken();
445 ExprResult Tail = ParseAssignmentExpression();
446 if (Tail.isUsable())
447 TailExpr = Tail.take();
448 else
449 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
450 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000451 }
452
453 // Parse ')'.
454 T.consumeClose();
Alexander Musman8dba6642014-04-22 13:09:42 +0000455 if (Vars.empty() || (MustHaveTail && !TailExpr))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000456 return 0;
457
Alexander Musman8dba6642014-04-22 13:09:42 +0000458 return Actions.ActOnOpenMPVarListClause(Kind, Vars, TailExpr, Loc, LOpen,
459 ColonLoc, Tok.getLocation());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000460}
461