blob: e293148b0afe61ddb13b7412e93776ea472f6e4d [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;
Alexey Bataeva55ed262014-05-28 06:15:33 +000039 OpenMPDirectiveKind DKind = Tok.isAnnotation()
40 ? OMPD_unknown
41 : getOpenMPDirectiveKind(PP.getSpelling(Tok));
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000042
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)
Alexey Bataeva55ed262014-05-28 06:15:33 +000051 << 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();
Alexey Bataeva55ed262014-05-28 06:15:33 +000056 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +000057 }
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:
Alexey Bataeva769e072013-03-22 06:34:35 +000065 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +000066 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +000067 break;
68 }
Alp Tokerd751fa72013-12-18 19:10:49 +000069 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +000070 return DeclGroupPtrTy();
71}
72
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000073/// \brief Parsing of declarative or executable OpenMP directives.
74///
75/// threadprivate-directive:
76/// annot_pragma_openmp 'threadprivate' simple-variable-list
77/// annot_pragma_openmp_end
78///
79/// parallel-directive:
80/// annot_pragma_openmp 'parallel' {clause} annot_pragma_openmp_end
81///
82StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective() {
83 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000084 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000085 SmallVector<Expr *, 5> Identifiers;
86 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +000087 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +000088 FirstClauses(OMPC_unknown + 1);
89 const unsigned ScopeFlags =
90 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000091 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataeva55ed262014-05-28 06:15:33 +000092 OpenMPDirectiveKind DKind = Tok.isAnnotation()
93 ? OMPD_unknown
94 : getOpenMPDirectiveKind(PP.getSpelling(Tok));
Alexey Bataev758e55e2013-09-06 18:03:48 +000095 // Name of critical directive.
96 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000097 StmtResult Directive = StmtError();
98
99 switch (DKind) {
100 case OMPD_threadprivate:
101 ConsumeToken();
102 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
103 // The last seen token is annot_pragma_openmp_end - need to check for
104 // extra tokens.
105 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
106 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000107 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000108 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000109 }
110 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000111 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000112 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
113 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000114 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000115 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000116 case OMPD_parallel:
117 case OMPD_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000118 ConsumeToken();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000119
120 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope());
121
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000122 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000123 OpenMPClauseKind CKind = Tok.isAnnotation()
124 ? OMPC_unknown
125 : getOpenMPClauseKind(PP.getSpelling(Tok));
126 OMPClause *Clause =
127 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000128 FirstClauses[CKind].setInt(true);
129 if (Clause) {
130 FirstClauses[CKind].setPointer(Clause);
131 Clauses.push_back(Clause);
132 }
133
134 // Skip ',' if any.
135 if (Tok.is(tok::comma))
136 ConsumeToken();
137 }
138 // End location of the directive.
139 EndLoc = Tok.getLocation();
140 // Consume final annot_pragma_openmp_end.
141 ConsumeToken();
142
143 StmtResult AssociatedStmt;
144 bool CreateDirective = true;
145 ParseScope OMPDirectiveScope(this, ScopeFlags);
146 {
147 // The body is a block scope like in Lambdas and Blocks.
148 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataev9959db52014-05-06 10:08:46 +0000149 Actions.ActOnOpenMPRegionStart(DKind, Loc, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000150 Actions.ActOnStartOfCompoundStmt();
151 // Parse statement
152 AssociatedStmt = ParseStatement();
153 Actions.ActOnFinishOfCompoundStmt();
154 if (!AssociatedStmt.isUsable()) {
155 Actions.ActOnCapturedRegionError();
156 CreateDirective = false;
157 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000158 AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000159 CreateDirective = AssociatedStmt.isUsable();
160 }
161 }
162 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000163 Directive = Actions.ActOnOpenMPExecutableDirective(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000164 DKind, Clauses, AssociatedStmt.get(), Loc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000165
166 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000167 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000168 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000169 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000170 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000171 case OMPD_unknown:
172 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000173 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000174 break;
175 case OMPD_task:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000176 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000177 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000178 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000179 break;
180 }
181 return Directive;
182}
183
Alexey Bataeva769e072013-03-22 06:34:35 +0000184/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000185/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000186///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000187/// simple-variable-list:
188/// '(' id-expression {, id-expression} ')'
189///
190bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
191 SmallVectorImpl<Expr *> &VarList,
192 bool AllowScopeSpecifier) {
193 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000194 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000195 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000196 if (T.expectAndConsume(diag::err_expected_lparen_after,
197 getOpenMPDirectiveName(Kind)))
198 return true;
199 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000200 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000201
202 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000203 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000204 CXXScopeSpec SS;
205 SourceLocation TemplateKWLoc;
206 UnqualifiedId Name;
207 // Read var name.
208 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000209 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000210
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000211 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
212 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000213 IsCorrect = false;
214 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000215 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000216 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
217 TemplateKWLoc, Name)) {
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 (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
222 Tok.isNot(tok::annot_pragma_openmp_end)) {
223 IsCorrect = false;
224 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000225 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000226 Diag(PrevTok.getLocation(), diag::err_expected)
227 << tok::identifier
228 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000229 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000230 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000231 ExprResult Res =
232 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000233 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000234 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000235 }
236 // Consume ','.
237 if (Tok.is(tok::comma)) {
238 ConsumeToken();
239 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000240 }
241
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000242 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000243 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000244 IsCorrect = false;
245 }
246
247 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000248 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000249
250 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000251}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000252
253/// \brief Parsing of OpenMP clauses.
254///
255/// clause:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000256/// if-clause | num_threads-clause | safelen-clause | default-clause |
Alexander Musman8bd31e62014-05-27 15:12:19 +0000257/// private-clause | firstprivate-clause | shared-clause | linear-clause |
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000258/// aligned-clause | collapse-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000259///
260OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
261 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000262 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000263 bool ErrorFound = false;
264 // Check if clause is allowed for the given directive.
265 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000266 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
267 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000268 ErrorFound = true;
269 }
270
271 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000272 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +0000273 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000274 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000275 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000276 // OpenMP [2.5, Restrictions]
277 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000278 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000279 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000280 // Only one safelen clause can appear on a simd directive.
281 // Only one collapse clause can appear on a simd directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000282 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000283 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
284 << getOpenMPClauseName(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000285 }
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) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000297 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
298 << getOpenMPClauseName(CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000299 }
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:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000307 case OMPC_aligned:
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)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000313 << 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:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000317 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
318 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000319 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000320 break;
321 }
Craig Topper161e4db2014-05-21 06:02:52 +0000322 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000323}
324
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000325/// \brief Parsing of OpenMP clauses with single expressions like 'if',
326/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
327/// 'thread_limit'.
328///
329/// if-clause:
330/// 'if' '(' expression ')'
331///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000332/// num_threads-clause:
333/// 'num_threads' '(' expression ')'
334///
335/// safelen-clause:
336/// 'safelen' '(' expression ')'
337///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000338/// collapse-clause:
339/// 'collapse' '(' expression ')'
340///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000341OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
342 SourceLocation Loc = ConsumeToken();
343
344 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
345 if (T.expectAndConsume(diag::err_expected_lparen_after,
346 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000347 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000348
349 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
350 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
351
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000352 // Parse ')'.
353 T.consumeClose();
354
355 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000356 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000357
Alexey Bataeva55ed262014-05-28 06:15:33 +0000358 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000359 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000360}
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)))
Craig Topper161e4db2014-05-21 06:02:52 +0000377 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000378
Alexey Bataeva55ed262014-05-28 06:15:33 +0000379 unsigned Type = getOpenMPSimpleClauseType(
380 Kind, 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 ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000404/// aligned-clause:
405/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000406///
407OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
408 SourceLocation Loc = Tok.getLocation();
409 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000410 SourceLocation ColonLoc = SourceLocation();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000411 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000412 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000413 if (T.expectAndConsume(diag::err_expected_lparen_after,
414 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000415 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000416
417 SmallVector<Expr *, 5> Vars;
418 bool IsComma = true;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000419 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000420 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000421 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000422 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000423 // Parse variable
424 ExprResult VarExpr = ParseAssignmentExpression();
425 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000426 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000427 } else {
428 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000429 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000430 }
431 // Skip ',' if any
432 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000433 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000434 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000435 else if (Tok.isNot(tok::r_paren) &&
436 Tok.isNot(tok::annot_pragma_openmp_end) &&
437 (!MayHaveTail || Tok.isNot(tok::colon)))
438 Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
439 }
440
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000441 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000442 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000443 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
444 if (MustHaveTail) {
445 ColonLoc = Tok.getLocation();
446 ConsumeToken();
447 ExprResult Tail = ParseAssignmentExpression();
448 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000449 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000450 else
451 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
452 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000453 }
454
455 // Parse ')'.
456 T.consumeClose();
Alexander Musman8dba6642014-04-22 13:09:42 +0000457 if (Vars.empty() || (MustHaveTail && !TailExpr))
Craig Topper161e4db2014-05-21 06:02:52 +0000458 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000459
Alexander Musman8dba6642014-04-22 13:09:42 +0000460 return Actions.ActOnOpenMPVarListClause(Kind, Vars, TailExpr, Loc, LOpen,
461 ColonLoc, Tok.getLocation());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000462}
463