blob: 9c52d645cfdf28ea7d756b599c7231e7c9f9d319 [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);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000089 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +000090 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;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000145 if (DKind == OMPD_simd)
146 ScopeFlags |=
147 Scope::OpenMPLoopDirectiveScope | Scope::OpenMPSimdDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000148 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 {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000161 AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000162 CreateDirective = AssociatedStmt.isUsable();
163 }
164 }
165 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000166 Directive = Actions.ActOnOpenMPExecutableDirective(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000167 DKind, Clauses, AssociatedStmt.get(), Loc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000168
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();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000172 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000173 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000174 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)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000180 << 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);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000234 ExprResult Res =
235 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000236 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000237 VarList.push_back(Res.get());
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 Musman8bd31e62014-05-27 15:12:19 +0000260/// private-clause | firstprivate-clause | shared-clause | linear-clause |
Alexey Bataevc5e02582014-06-16 07:08:35 +0000261/// aligned-clause | collapse-clause | lastprivate-clause |
262/// reduction-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000263///
264OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
265 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000266 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000267 bool ErrorFound = false;
268 // Check if clause is allowed for the given directive.
269 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000270 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
271 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000272 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:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000279 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000280 // OpenMP [2.5, Restrictions]
281 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000282 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000283 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000284 // Only one safelen clause can appear on a simd directive.
285 // Only one collapse clause can appear on a simd directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000286 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000287 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
288 << getOpenMPClauseName(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000289 }
290
291 Clause = ParseOpenMPSingleExprClause(CKind);
292 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000293 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000294 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000295 // OpenMP [2.14.3.1, Restrictions]
296 // Only a single default clause may be specified on a parallel, task or
297 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000298 // OpenMP [2.5, parallel Construct, Restrictions]
299 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000300 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000301 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
302 << getOpenMPClauseName(CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000303 }
304
305 Clause = ParseOpenMPSimpleClause(CKind);
306 break;
307 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000308 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000309 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000310 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000311 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000312 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000313 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000314 case OMPC_copyin:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000315 Clause = ParseOpenMPVarListClause(CKind);
316 break;
317 case OMPC_unknown:
318 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000319 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000320 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000321 break;
322 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000323 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
324 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000325 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000326 break;
327 }
Craig Topper161e4db2014-05-21 06:02:52 +0000328 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000329}
330
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000331/// \brief Parsing of OpenMP clauses with single expressions like 'if',
332/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
333/// 'thread_limit'.
334///
335/// if-clause:
336/// 'if' '(' expression ')'
337///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000338/// num_threads-clause:
339/// 'num_threads' '(' expression ')'
340///
341/// safelen-clause:
342/// 'safelen' '(' expression ')'
343///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000344/// collapse-clause:
345/// 'collapse' '(' expression ')'
346///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000347OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
348 SourceLocation Loc = ConsumeToken();
349
350 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
351 if (T.expectAndConsume(diag::err_expected_lparen_after,
352 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000353 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000354
355 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
356 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
357
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000358 // Parse ')'.
359 T.consumeClose();
360
361 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000362 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000363
Alexey Bataeva55ed262014-05-28 06:15:33 +0000364 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000365 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000366}
367
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000368/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000369///
370/// default-clause:
371/// 'default' '(' 'none' | 'shared' ')
372///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000373/// proc_bind-clause:
374/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
375///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000376OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
377 SourceLocation Loc = Tok.getLocation();
378 SourceLocation LOpen = ConsumeToken();
379 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000380 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000381 if (T.expectAndConsume(diag::err_expected_lparen_after,
382 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000383 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000384
Alexey Bataeva55ed262014-05-28 06:15:33 +0000385 unsigned Type = getOpenMPSimpleClauseType(
386 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000387 SourceLocation TypeLoc = Tok.getLocation();
388 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
389 Tok.isNot(tok::annot_pragma_openmp_end))
390 ConsumeAnyToken();
391
392 // Parse ')'.
393 T.consumeClose();
394
395 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
396 Tok.getLocation());
397}
398
Alexey Bataevc5e02582014-06-16 07:08:35 +0000399static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
400 UnqualifiedId &ReductionId) {
401 SourceLocation TemplateKWLoc;
402 if (ReductionIdScopeSpec.isEmpty()) {
403 auto OOK = OO_None;
404 switch (P.getCurToken().getKind()) {
405 case tok::plus:
406 OOK = OO_Plus;
407 break;
408 case tok::minus:
409 OOK = OO_Minus;
410 break;
411 case tok::star:
412 OOK = OO_Star;
413 break;
414 case tok::amp:
415 OOK = OO_Amp;
416 break;
417 case tok::pipe:
418 OOK = OO_Pipe;
419 break;
420 case tok::caret:
421 OOK = OO_Caret;
422 break;
423 case tok::ampamp:
424 OOK = OO_AmpAmp;
425 break;
426 case tok::pipepipe:
427 OOK = OO_PipePipe;
428 break;
429 default:
430 break;
431 }
432 if (OOK != OO_None) {
433 SourceLocation OpLoc = P.ConsumeToken();
434 SourceLocation SymbolLocations[] = { OpLoc, OpLoc, SourceLocation() };
435 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
436 return false;
437 }
438 }
439 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
440 /*AllowDestructorName*/ false,
441 /*AllowConstructorName*/ false, ParsedType(),
442 TemplateKWLoc, ReductionId);
443}
444
Alexander Musman1bb328c2014-06-04 13:06:39 +0000445/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000446/// 'shared', 'copyin', or 'reduction'.
447///
448/// private-clause:
449/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000450/// firstprivate-clause:
451/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000452/// lastprivate-clause:
453/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000454/// shared-clause:
455/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000456/// linear-clause:
457/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000458/// aligned-clause:
459/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000460/// reduction-clause:
461/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000462///
463OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
464 SourceLocation Loc = Tok.getLocation();
465 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000466 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000467 // Optional scope specifier and unqualified id for reduction identifier.
468 CXXScopeSpec ReductionIdScopeSpec;
469 UnqualifiedId ReductionId;
470 bool InvalidReductionId = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000471 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000472 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000473 if (T.expectAndConsume(diag::err_expected_lparen_after,
474 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000475 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000476
Alexey Bataevc5e02582014-06-16 07:08:35 +0000477 // Handle reduction-identifier for reduction clause.
478 if (Kind == OMPC_reduction) {
479 ColonProtectionRAIIObject ColonRAII(*this);
480 if (getLangOpts().CPlusPlus) {
481 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
482 }
483 InvalidReductionId =
484 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
485 if (InvalidReductionId) {
486 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
487 StopBeforeMatch);
488 }
489 if (Tok.is(tok::colon)) {
490 ColonLoc = ConsumeToken();
491 } else {
492 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
493 }
494 }
495
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000496 SmallVector<Expr *, 5> Vars;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000497 bool IsComma = !InvalidReductionId;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000498 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000499 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000500 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000501 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000502 // Parse variable
503 ExprResult VarExpr = ParseAssignmentExpression();
504 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000505 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000506 } else {
507 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000508 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000509 }
510 // Skip ',' if any
511 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000512 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000513 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000514 else if (Tok.isNot(tok::r_paren) &&
515 Tok.isNot(tok::annot_pragma_openmp_end) &&
516 (!MayHaveTail || Tok.isNot(tok::colon)))
517 Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
518 }
519
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000520 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000521 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000522 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
523 if (MustHaveTail) {
524 ColonLoc = Tok.getLocation();
525 ConsumeToken();
526 ExprResult Tail = ParseAssignmentExpression();
527 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000528 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000529 else
530 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
531 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000532 }
533
534 // Parse ')'.
535 T.consumeClose();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000536 if (Vars.empty() || (MustHaveTail && !TailExpr) || InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000537 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000538
Alexey Bataevc5e02582014-06-16 07:08:35 +0000539 return Actions.ActOnOpenMPVarListClause(
540 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
541 ReductionIdScopeSpec,
542 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
543 : DeclarationNameInfo());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000544}
545