blob: dfd8ca7c2c94defbaf9ae6a0ac3bcda852598adc [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:
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;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +000088 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
89 FirstClauses(OMPC_unknown + 1);
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 Bataev9959db52014-05-06 10:08:46 +0000151 Actions.ActOnOpenMPRegionStart(DKind, Loc, getCurScope());
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:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000179 Diag(Tok, diag::err_omp_unexpected_directive)
180 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000181 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000182 break;
183 }
184 return Directive;
185}
186
Alexey Bataeva769e072013-03-22 06:34:35 +0000187/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000188/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000189///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000190/// simple-variable-list:
191/// '(' id-expression {, id-expression} ')'
192///
193bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
194 SmallVectorImpl<Expr *> &VarList,
195 bool AllowScopeSpecifier) {
196 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000197 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000198 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000199 if (T.expectAndConsume(diag::err_expected_lparen_after,
200 getOpenMPDirectiveName(Kind)))
201 return true;
202 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000203 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000204
205 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000206 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000207 CXXScopeSpec SS;
208 SourceLocation TemplateKWLoc;
209 UnqualifiedId Name;
210 // Read var name.
211 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000212 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000213
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000214 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
215 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000216 IsCorrect = false;
217 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000218 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000219 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
220 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000221 IsCorrect = false;
222 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000223 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000224 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
225 Tok.isNot(tok::annot_pragma_openmp_end)) {
226 IsCorrect = false;
227 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000228 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000229 Diag(PrevTok.getLocation(), diag::err_expected)
230 << tok::identifier
231 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000232 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000233 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
234 ExprResult Res = Actions.ActOnOpenMPIdExpression(getCurScope(), SS,
235 NameInfo);
236 if (Res.isUsable())
237 VarList.push_back(Res.take());
Alexey Bataeva769e072013-03-22 06:34:35 +0000238 }
239 // Consume ','.
240 if (Tok.is(tok::comma)) {
241 ConsumeToken();
242 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000243 }
244
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000245 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000246 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000247 IsCorrect = false;
248 }
249
250 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000251 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000252
253 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000254}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000255
256/// \brief Parsing of OpenMP clauses.
257///
258/// clause:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000259/// if-clause | num_threads-clause | safelen-clause | default-clause |
Alexander Musman8dba6642014-04-22 13:09:42 +0000260/// private-clause | firstprivate-clause | shared-clause | linear-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000261///
262OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
263 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000264 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000265 bool ErrorFound = false;
266 // Check if clause is allowed for the given directive.
267 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
268 Diag(Tok, diag::err_omp_unexpected_clause)
269 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
270 ErrorFound = true;
271 }
272
273 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000274 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +0000275 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000276 case OMPC_safelen:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000277 // OpenMP [2.5, Restrictions]
278 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000279 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000280 // OpenMP [2.8.1, simd construct, Restrictions]
281 // Only one safelen clause can appear on a simd directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000282 if (!FirstClause) {
283 Diag(Tok, diag::err_omp_more_one_clause)
284 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind);
285 }
286
287 Clause = ParseOpenMPSingleExprClause(CKind);
288 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000289 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000290 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000291 // OpenMP [2.14.3.1, Restrictions]
292 // Only a single default clause may be specified on a parallel, task or
293 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000294 // OpenMP [2.5, parallel Construct, Restrictions]
295 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000296 if (!FirstClause) {
297 Diag(Tok, diag::err_omp_more_one_clause)
298 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind);
299 }
300
301 Clause = ParseOpenMPSimpleClause(CKind);
302 break;
303 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000304 case OMPC_firstprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000305 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +0000306 case OMPC_linear:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000307 case OMPC_copyin:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000308 Clause = ParseOpenMPVarListClause(CKind);
309 break;
310 case OMPC_unknown:
311 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
312 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000313 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000314 break;
315 case OMPC_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000316 Diag(Tok, diag::err_omp_unexpected_clause)
317 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000318 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000319 break;
320 }
Craig Topper161e4db2014-05-21 06:02:52 +0000321 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000322}
323
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000324/// \brief Parsing of OpenMP clauses with single expressions like 'if',
325/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
326/// 'thread_limit'.
327///
328/// if-clause:
329/// 'if' '(' expression ')'
330///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000331/// num_threads-clause:
332/// 'num_threads' '(' expression ')'
333///
334/// safelen-clause:
335/// 'safelen' '(' expression ')'
336///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000337OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
338 SourceLocation Loc = ConsumeToken();
339
340 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
341 if (T.expectAndConsume(diag::err_expected_lparen_after,
342 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000343 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000344
345 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
346 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
347
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000348 // Parse ')'.
349 T.consumeClose();
350
351 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000352 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000353
354 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.take(), Loc,
355 T.getOpenLocation(),
356 T.getCloseLocation());
357}
358
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000359/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000360///
361/// default-clause:
362/// 'default' '(' 'none' | 'shared' ')
363///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000364/// proc_bind-clause:
365/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
366///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000367OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
368 SourceLocation Loc = Tok.getLocation();
369 SourceLocation LOpen = ConsumeToken();
370 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000371 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000372 if (T.expectAndConsume(diag::err_expected_lparen_after,
373 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000374 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000375
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000376 unsigned Type =
377 getOpenMPSimpleClauseType(Kind,
378 Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000379 SourceLocation TypeLoc = Tok.getLocation();
380 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
381 Tok.isNot(tok::annot_pragma_openmp_end))
382 ConsumeAnyToken();
383
384 // Parse ')'.
385 T.consumeClose();
386
387 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
388 Tok.getLocation());
389}
390
391/// \brief Parsing of OpenMP clause 'private', 'firstprivate',
392/// 'shared', 'copyin', or 'reduction'.
393///
394/// private-clause:
395/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000396/// firstprivate-clause:
397/// 'firstprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000398/// shared-clause:
399/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000400/// linear-clause:
401/// 'linear' '(' list [ ':' linear-step ] ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000402///
403OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
404 SourceLocation Loc = Tok.getLocation();
405 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000406 SourceLocation ColonLoc = SourceLocation();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000407 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000408 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000409 if (T.expectAndConsume(diag::err_expected_lparen_after,
410 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000411 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000412
413 SmallVector<Expr *, 5> Vars;
414 bool IsComma = true;
Alexander Musman8dba6642014-04-22 13:09:42 +0000415 const bool MayHaveTail = (Kind == OMPC_linear);
416 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000417 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000418 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000419 // Parse variable
420 ExprResult VarExpr = ParseAssignmentExpression();
421 if (VarExpr.isUsable()) {
422 Vars.push_back(VarExpr.take());
423 } else {
424 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000425 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000426 }
427 // Skip ',' if any
428 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000429 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000430 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000431 else if (Tok.isNot(tok::r_paren) &&
432 Tok.isNot(tok::annot_pragma_openmp_end) &&
433 (!MayHaveTail || Tok.isNot(tok::colon)))
434 Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
435 }
436
437 // Parse ':' linear-step
Craig Topper161e4db2014-05-21 06:02:52 +0000438 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000439 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
440 if (MustHaveTail) {
441 ColonLoc = Tok.getLocation();
442 ConsumeToken();
443 ExprResult Tail = ParseAssignmentExpression();
444 if (Tail.isUsable())
445 TailExpr = Tail.take();
446 else
447 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
448 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000449 }
450
451 // Parse ')'.
452 T.consumeClose();
Alexander Musman8dba6642014-04-22 13:09:42 +0000453 if (Vars.empty() || (MustHaveTail && !TailExpr))
Craig Topper161e4db2014-05-21 06:02:52 +0000454 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000455
Alexander Musman8dba6642014-04-22 13:09:42 +0000456 return Actions.ActOnOpenMPVarListClause(Kind, Vars, TailExpr, Loc, LOpen,
457 ColonLoc, Tok.getLocation());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000458}
459