blob: f9ca1185d949ccb488ab57b174cec32360b5e369 [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"
Michael Wong65f367f2015-07-21 13:44:28 +000022
Alexey Bataeva769e072013-03-22 06:34:35 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// OpenMP declarative directives.
27//===----------------------------------------------------------------------===//
28
Alexey Bataev4acb8592014-07-07 13:01:15 +000029static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000030 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
31 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
32 // TODO: add other combined directives in topological order.
33 const OpenMPDirectiveKind F[][3] = {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000034 {OMPD_unknown /*cancellation*/, OMPD_unknown /*point*/,
35 OMPD_cancellation_point},
Michael Wong65f367f2015-07-21 13:44:28 +000036 {OMPD_target, OMPD_unknown /*data*/, OMPD_target_data},
Alexey Bataev6d4ed052015-07-01 06:57:41 +000037 {OMPD_for, OMPD_simd, OMPD_for_simd},
38 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
39 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
40 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections}};
Alexey Bataev4acb8592014-07-07 13:01:15 +000041 auto Tok = P.getCurToken();
42 auto DKind =
43 Tok.isAnnotation()
44 ? OMPD_unknown
45 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Michael Wong65f367f2015-07-21 13:44:28 +000046
Alexey Bataev6d4ed052015-07-01 06:57:41 +000047 bool TokenMatched = false;
Alexander Musmanf82886e2014-09-18 05:12:34 +000048 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000049 if (!Tok.isAnnotation() && DKind == OMPD_unknown) {
50 TokenMatched =
51 (i == 0) &&
52 !P.getPreprocessor().getSpelling(Tok).compare("cancellation");
53 } else {
54 TokenMatched = DKind == F[i][0] && DKind != OMPD_unknown;
55 }
Michael Wong65f367f2015-07-21 13:44:28 +000056
Alexey Bataev6d4ed052015-07-01 06:57:41 +000057 if (TokenMatched) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000058 Tok = P.getPreprocessor().LookAhead(0);
Michael Wong65f367f2015-07-21 13:44:28 +000059 auto TokenIsAnnotation = Tok.isAnnotation();
Alexander Musmanf82886e2014-09-18 05:12:34 +000060 auto SDKind =
Michael Wong65f367f2015-07-21 13:44:28 +000061 TokenIsAnnotation
Alexander Musmanf82886e2014-09-18 05:12:34 +000062 ? OMPD_unknown
63 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Michael Wong65f367f2015-07-21 13:44:28 +000064
65 if (!TokenIsAnnotation && SDKind == OMPD_unknown) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000066 TokenMatched =
Daniel Jasper9aea8602015-07-21 16:18:51 +000067 ((i == 0) &&
68 !P.getPreprocessor().getSpelling(Tok).compare("point")) ||
69 ((i == 1) && !P.getPreprocessor().getSpelling(Tok).compare("data"));
Alexey Bataev6d4ed052015-07-01 06:57:41 +000070 } else {
71 TokenMatched = SDKind == F[i][1] && SDKind != OMPD_unknown;
72 }
Michael Wong65f367f2015-07-21 13:44:28 +000073
Alexey Bataev6d4ed052015-07-01 06:57:41 +000074 if (TokenMatched) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000075 P.ConsumeToken();
76 DKind = F[i][2];
77 }
Alexey Bataev4acb8592014-07-07 13:01:15 +000078 }
79 }
80 return DKind;
81}
82
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000083/// \brief Parsing of declarative OpenMP directives.
84///
85/// threadprivate-directive:
86/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataeva769e072013-03-22 06:34:35 +000087///
88Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
89 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000090 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +000091
92 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000093 SmallVector<Expr *, 5> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +000094 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000095
96 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +000097 case OMPD_threadprivate:
98 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000099 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000100 // The last seen token is annot_pragma_openmp_end - need to check for
101 // extra tokens.
102 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
103 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000104 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000105 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000106 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000107 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000108 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +0000109 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +0000110 }
111 break;
112 case OMPD_unknown:
113 Diag(Tok, diag::err_omp_unknown_directive);
114 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000115 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000116 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000117 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000118 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000119 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000120 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000121 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000122 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000123 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000124 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000125 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000126 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000127 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000128 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000129 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000130 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000131 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000132 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000133 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000134 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000135 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000136 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000137 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000138 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000139 case OMPD_target_data:
Alexey Bataeva769e072013-03-22 06:34:35 +0000140 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000141 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000142 break;
143 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000144 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +0000145 return DeclGroupPtrTy();
146}
147
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000148/// \brief Parsing of declarative or executable OpenMP directives.
149///
150/// threadprivate-directive:
151/// annot_pragma_openmp 'threadprivate' simple-variable-list
152/// annot_pragma_openmp_end
153///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000154/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000155/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000156/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
157/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000158/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000159/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
160/// 'taskgroup' | 'teams' {clause}
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000161/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000162///
Alexey Bataev68446b72014-07-18 07:47:19 +0000163StmtResult
164Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000165 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000166 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000167 SmallVector<Expr *, 5> Identifiers;
168 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000169 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000170 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000171 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000172 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000173 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000174 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000175 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000176 // Name of critical directive.
177 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000178 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000179 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000180 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000181
182 switch (DKind) {
183 case OMPD_threadprivate:
184 ConsumeToken();
185 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
186 // The last seen token is annot_pragma_openmp_end - need to check for
187 // extra tokens.
188 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
189 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000190 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000191 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000192 }
193 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000194 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000195 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
196 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000197 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000198 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000199 case OMPD_flush:
200 if (PP.LookAhead(0).is(tok::l_paren)) {
201 FlushHasClause = true;
202 // Push copy of the current token back to stream to properly parse
203 // pseudo-clause OMPFlushClause.
204 PP.EnterToken(Tok);
205 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000206 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000207 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000208 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000209 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000210 case OMPD_cancel:
Alexey Bataev68446b72014-07-18 07:47:19 +0000211 if (!StandAloneAllowed) {
212 Diag(Tok, diag::err_omp_immediate_directive)
213 << getOpenMPDirectiveName(DKind);
214 }
215 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000216 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000217 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000218 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000219 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000220 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000221 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000222 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000223 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000224 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000225 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000226 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000227 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000228 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000229 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000230 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000231 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000232 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000233 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000234 case OMPD_taskgroup:
235 case OMPD_target_data: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000236 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000237 // Parse directive name of the 'critical' directive if any.
238 if (DKind == OMPD_critical) {
239 BalancedDelimiterTracker T(*this, tok::l_paren,
240 tok::annot_pragma_openmp_end);
241 if (!T.consumeOpen()) {
242 if (Tok.isAnyIdentifier()) {
243 DirName =
244 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
245 ConsumeAnyToken();
246 } else {
247 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
248 }
249 T.consumeClose();
250 }
Alexey Bataev80909872015-07-02 11:25:17 +0000251 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000252 CancelRegion = ParseOpenMPDirectiveKind(*this);
253 if (Tok.isNot(tok::annot_pragma_openmp_end))
254 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000255 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000256
Alexey Bataevf29276e2014-06-18 04:14:57 +0000257 if (isOpenMPLoopDirective(DKind))
258 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
259 if (isOpenMPSimdDirective(DKind))
260 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
261 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000262 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000263
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000264 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000265 OpenMPClauseKind CKind =
266 Tok.isAnnotation()
267 ? OMPC_unknown
268 : FlushHasClause ? OMPC_flush
269 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000270 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000271 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000272 OMPClause *Clause =
273 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000274 FirstClauses[CKind].setInt(true);
275 if (Clause) {
276 FirstClauses[CKind].setPointer(Clause);
277 Clauses.push_back(Clause);
278 }
279
280 // Skip ',' if any.
281 if (Tok.is(tok::comma))
282 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000283 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000284 }
285 // End location of the directive.
286 EndLoc = Tok.getLocation();
287 // Consume final annot_pragma_openmp_end.
288 ConsumeToken();
289
290 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000291 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000292 // The body is a block scope like in Lambdas and Blocks.
293 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000294 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000295 Actions.ActOnStartOfCompoundStmt();
296 // Parse statement
297 AssociatedStmt = ParseStatement();
298 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000299 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000300 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000301 Directive = Actions.ActOnOpenMPExecutableDirective(
302 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
303 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000304
305 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000306 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000307 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000308 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000309 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000310 case OMPD_unknown:
311 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000312 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000313 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000314 }
315 return Directive;
316}
317
Alexey Bataeva769e072013-03-22 06:34:35 +0000318/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000319/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000320///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000321/// simple-variable-list:
322/// '(' id-expression {, id-expression} ')'
323///
324bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
325 SmallVectorImpl<Expr *> &VarList,
326 bool AllowScopeSpecifier) {
327 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000328 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000329 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000330 if (T.expectAndConsume(diag::err_expected_lparen_after,
331 getOpenMPDirectiveName(Kind)))
332 return true;
333 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000334 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000335
336 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000337 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000338 CXXScopeSpec SS;
339 SourceLocation TemplateKWLoc;
340 UnqualifiedId Name;
341 // Read var name.
342 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000343 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000344
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000345 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
346 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000347 IsCorrect = false;
348 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000349 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000350 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
351 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000352 IsCorrect = false;
353 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000354 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000355 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
356 Tok.isNot(tok::annot_pragma_openmp_end)) {
357 IsCorrect = false;
358 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000359 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000360 Diag(PrevTok.getLocation(), diag::err_expected)
361 << tok::identifier
362 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000363 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000364 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000365 ExprResult Res =
366 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000367 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000368 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000369 }
370 // Consume ','.
371 if (Tok.is(tok::comma)) {
372 ConsumeToken();
373 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000374 }
375
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000376 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000377 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000378 IsCorrect = false;
379 }
380
381 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000382 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000383
384 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000385}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000386
387/// \brief Parsing of OpenMP clauses.
388///
389/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000390/// if-clause | final-clause | num_threads-clause | safelen-clause |
391/// default-clause | private-clause | firstprivate-clause | shared-clause
392/// | linear-clause | aligned-clause | collapse-clause |
393/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000394/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000395/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +0000396/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Li099bb8c2015-11-24 20:50:12 +0000397/// simdlen-clause | threads-clause | simd-clause | num_teams-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000398///
399OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
400 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000401 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000402 bool ErrorFound = false;
403 // Check if clause is allowed for the given directive.
404 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000405 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
406 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000407 ErrorFound = true;
408 }
409
410 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +0000411 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000412 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000413 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +0000414 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000415 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +0000416 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +0000417 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +0000418 case OMPC_num_teams:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000419 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +0000420 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000421 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000422 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +0000423 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +0000424 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +0000425 // OpenMP [2.9.1, target data construct, Restrictions]
426 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000427 // OpenMP [2.11.1, task Construct, Restrictions]
428 // At most one if clause can appear on the directive.
429 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +0000430 // OpenMP [teams Construct, Restrictions]
431 // At most one num_teams clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000432 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000433 Diag(Tok, diag::err_omp_more_one_clause)
434 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000435 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000436 }
437
Alexey Bataev10e775f2015-07-30 11:36:16 +0000438 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
439 Clause = ParseOpenMPClause(CKind);
440 else
441 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000442 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000443 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000444 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000445 // OpenMP [2.14.3.1, Restrictions]
446 // Only a single default clause may be specified on a parallel, task or
447 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000448 // OpenMP [2.5, parallel Construct, Restrictions]
449 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000450 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000451 Diag(Tok, diag::err_omp_more_one_clause)
452 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000453 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000454 }
455
456 Clause = ParseOpenMPSimpleClause(CKind);
457 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000458 case OMPC_schedule:
459 // OpenMP [2.7.1, Restrictions, p. 3]
460 // Only one schedule clause can appear on a loop directive.
461 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000462 Diag(Tok, diag::err_omp_more_one_clause)
463 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000464 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000465 }
466
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000467 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000468 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
469 break;
Alexey Bataev236070f2014-06-20 11:19:47 +0000470 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000471 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000472 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000473 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000474 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000475 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000476 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000477 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +0000478 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000479 case OMPC_simd:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000480 // OpenMP [2.7.1, Restrictions, p. 9]
481 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000482 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
483 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000484 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000485 Diag(Tok, diag::err_omp_more_one_clause)
486 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000487 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000488 }
489
490 Clause = ParseOpenMPClause(CKind);
491 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000492 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000493 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000494 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000495 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000496 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000497 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000498 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000499 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000500 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000501 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000502 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +0000503 case OMPC_map:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000504 Clause = ParseOpenMPVarListClause(CKind);
505 break;
506 case OMPC_unknown:
507 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000508 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000509 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000510 break;
511 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000512 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
513 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000514 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000515 break;
516 }
Craig Topper161e4db2014-05-21 06:02:52 +0000517 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000518}
519
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000520/// \brief Parsing of OpenMP clauses with single expressions like 'final',
521/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams', 'thread_limit'
522/// or 'simdlen'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000523///
Alexey Bataev3778b602014-07-17 07:32:53 +0000524/// final-clause:
525/// 'final' '(' expression ')'
526///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000527/// num_threads-clause:
528/// 'num_threads' '(' expression ')'
529///
530/// safelen-clause:
531/// 'safelen' '(' expression ')'
532///
Alexey Bataev66b15b52015-08-21 11:14:16 +0000533/// simdlen-clause:
534/// 'simdlen' '(' expression ')'
535///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000536/// collapse-clause:
537/// 'collapse' '(' expression ')'
538///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000539OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
540 SourceLocation Loc = ConsumeToken();
541
542 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
543 if (T.expectAndConsume(diag::err_expected_lparen_after,
544 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000545 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000546
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000547 SourceLocation ELoc = Tok.getLocation();
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000548 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
549 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000550 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000551
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000552 // Parse ')'.
553 T.consumeClose();
554
555 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000556 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000557
Alexey Bataeva55ed262014-05-28 06:15:33 +0000558 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000559 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000560}
561
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000562/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000563///
564/// default-clause:
565/// 'default' '(' 'none' | 'shared' ')
566///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000567/// proc_bind-clause:
568/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
569///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000570OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
571 SourceLocation Loc = Tok.getLocation();
572 SourceLocation LOpen = ConsumeToken();
573 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000574 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000575 if (T.expectAndConsume(diag::err_expected_lparen_after,
576 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000577 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000578
Alexey Bataeva55ed262014-05-28 06:15:33 +0000579 unsigned Type = getOpenMPSimpleClauseType(
580 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000581 SourceLocation TypeLoc = Tok.getLocation();
582 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
583 Tok.isNot(tok::annot_pragma_openmp_end))
584 ConsumeAnyToken();
585
586 // Parse ')'.
587 T.consumeClose();
588
589 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
590 Tok.getLocation());
591}
592
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000593/// \brief Parsing of OpenMP clauses like 'ordered'.
594///
595/// ordered-clause:
596/// 'ordered'
597///
Alexey Bataev236070f2014-06-20 11:19:47 +0000598/// nowait-clause:
599/// 'nowait'
600///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000601/// untied-clause:
602/// 'untied'
603///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000604/// mergeable-clause:
605/// 'mergeable'
606///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000607/// read-clause:
608/// 'read'
609///
Alexey Bataev346265e2015-09-25 10:37:12 +0000610/// threads-clause:
611/// 'threads'
612///
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000613/// simd-clause:
614/// 'simd'
615///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000616OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
617 SourceLocation Loc = Tok.getLocation();
618 ConsumeAnyToken();
619
620 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
621}
622
623
Alexey Bataev56dafe82014-06-20 07:16:17 +0000624/// \brief Parsing of OpenMP clauses with single expressions and some additional
625/// argument like 'schedule' or 'dist_schedule'.
626///
627/// schedule-clause:
628/// 'schedule' '(' kind [',' expression ] ')'
629///
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000630/// if-clause:
631/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
632///
Alexey Bataev56dafe82014-06-20 07:16:17 +0000633OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
634 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000635 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000636 // Parse '('.
637 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
638 if (T.expectAndConsume(diag::err_expected_lparen_after,
639 getOpenMPClauseName(Kind)))
640 return nullptr;
641
642 ExprResult Val;
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000643 unsigned Arg;
644 SourceLocation KLoc;
645 if (Kind == OMPC_schedule) {
646 Arg = getOpenMPSimpleClauseType(
647 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
648 KLoc = Tok.getLocation();
649 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
650 Tok.isNot(tok::annot_pragma_openmp_end))
651 ConsumeAnyToken();
652 if ((Arg == OMPC_SCHEDULE_static || Arg == OMPC_SCHEDULE_dynamic ||
653 Arg == OMPC_SCHEDULE_guided) &&
654 Tok.is(tok::comma))
655 DelimLoc = ConsumeAnyToken();
656 } else {
657 assert(Kind == OMPC_if);
658 KLoc = Tok.getLocation();
659 Arg = ParseOpenMPDirectiveKind(*this);
660 if (Arg != OMPD_unknown) {
661 ConsumeToken();
662 if (Tok.is(tok::colon))
663 DelimLoc = ConsumeToken();
664 else
665 Diag(Tok, diag::warn_pragma_expected_colon)
666 << "directive name modifier";
667 }
668 }
Alexey Bataev56dafe82014-06-20 07:16:17 +0000669
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000670 bool NeedAnExpression =
671 (Kind == OMPC_schedule && DelimLoc.isValid()) || Kind == OMPC_if;
672 if (NeedAnExpression) {
673 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +0000674 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
675 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000676 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +0000677 }
678
679 // Parse ')'.
680 T.consumeClose();
681
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000682 if (NeedAnExpression && Val.isInvalid())
683 return nullptr;
684
Alexey Bataev56dafe82014-06-20 07:16:17 +0000685 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000686 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +0000687 T.getCloseLocation());
688}
689
Alexey Bataevc5e02582014-06-16 07:08:35 +0000690static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
691 UnqualifiedId &ReductionId) {
692 SourceLocation TemplateKWLoc;
693 if (ReductionIdScopeSpec.isEmpty()) {
694 auto OOK = OO_None;
695 switch (P.getCurToken().getKind()) {
696 case tok::plus:
697 OOK = OO_Plus;
698 break;
699 case tok::minus:
700 OOK = OO_Minus;
701 break;
702 case tok::star:
703 OOK = OO_Star;
704 break;
705 case tok::amp:
706 OOK = OO_Amp;
707 break;
708 case tok::pipe:
709 OOK = OO_Pipe;
710 break;
711 case tok::caret:
712 OOK = OO_Caret;
713 break;
714 case tok::ampamp:
715 OOK = OO_AmpAmp;
716 break;
717 case tok::pipepipe:
718 OOK = OO_PipePipe;
719 break;
720 default:
721 break;
722 }
723 if (OOK != OO_None) {
724 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000725 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000726 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
727 return false;
728 }
729 }
730 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
731 /*AllowDestructorName*/ false,
732 /*AllowConstructorName*/ false, ParsedType(),
733 TemplateKWLoc, ReductionId);
734}
735
Alexander Musman1bb328c2014-06-04 13:06:39 +0000736/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000737/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000738///
739/// private-clause:
740/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000741/// firstprivate-clause:
742/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000743/// lastprivate-clause:
744/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000745/// shared-clause:
746/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000747/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +0000748/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000749/// aligned-clause:
750/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000751/// reduction-clause:
752/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000753/// copyprivate-clause:
754/// 'copyprivate' '(' list ')'
755/// flush-clause:
756/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000757/// depend-clause:
758/// 'depend' '(' in | out | inout : list ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +0000759/// map-clause:
760/// 'map' '(' [ [ always , ]
761/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000762///
Alexey Bataev182227b2015-08-20 10:54:39 +0000763/// For 'linear' clause linear-list may have the following forms:
764/// list
765/// modifier(list)
766/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000767OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
768 SourceLocation Loc = Tok.getLocation();
769 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000770 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000771 // Optional scope specifier and unqualified id for reduction identifier.
772 CXXScopeSpec ReductionIdScopeSpec;
773 UnqualifiedId ReductionId;
774 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000775 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
Alexey Bataev182227b2015-08-20 10:54:39 +0000776 // OpenMP 4.1 [2.15.3.7, linear Clause]
777 // If no modifier is specified it is assumed to be val.
778 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000779 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
780 OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown;
781 bool MapTypeModifierSpecified = false;
782 bool UnexpectedId = false;
783 SourceLocation DepLinMapLoc;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000784
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000785 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000786 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000787 if (T.expectAndConsume(diag::err_expected_lparen_after,
788 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000789 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000790
Alexey Bataev182227b2015-08-20 10:54:39 +0000791 bool NeedRParenForLinear = false;
792 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
793 tok::annot_pragma_openmp_end);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000794 // Handle reduction-identifier for reduction clause.
795 if (Kind == OMPC_reduction) {
796 ColonProtectionRAIIObject ColonRAII(*this);
797 if (getLangOpts().CPlusPlus) {
798 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
799 }
800 InvalidReductionId =
801 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
802 if (InvalidReductionId) {
803 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
804 StopBeforeMatch);
805 }
806 if (Tok.is(tok::colon)) {
807 ColonLoc = ConsumeToken();
808 } else {
809 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
810 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000811 } else if (Kind == OMPC_depend) {
812 // Handle dependency type for depend clause.
813 ColonProtectionRAIIObject ColonRAII(*this);
814 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
815 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000816 DepLinMapLoc = Tok.getLocation();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000817
818 if (DepKind == OMPC_DEPEND_unknown) {
819 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
820 StopBeforeMatch);
821 } else {
822 ConsumeToken();
823 }
824 if (Tok.is(tok::colon)) {
825 ColonLoc = ConsumeToken();
826 } else {
827 Diag(Tok, diag::warn_pragma_expected_colon) << "dependency type";
828 }
Alexey Bataev182227b2015-08-20 10:54:39 +0000829 } else if (Kind == OMPC_linear) {
830 // Try to parse modifier if any.
831 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev182227b2015-08-20 10:54:39 +0000832 LinearModifier = static_cast<OpenMPLinearClauseKind>(
Alexey Bataev1185e192015-08-20 12:15:57 +0000833 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000834 DepLinMapLoc = ConsumeToken();
Alexey Bataev182227b2015-08-20 10:54:39 +0000835 LinearT.consumeOpen();
836 NeedRParenForLinear = true;
837 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000838 } else if (Kind == OMPC_map) {
839 // Handle map type for map clause.
840 ColonProtectionRAIIObject ColonRAII(*this);
841
842 // the first identifier may be a list item, a map-type or
843 // a map-type-modifier
844 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
845 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
846 DepLinMapLoc = Tok.getLocation();
847 bool ColonExpected = false;
848
849 if (Tok.is(tok::identifier)) {
850 if (PP.LookAhead(0).is(tok::colon)) {
851 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
852 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
853 if (MapType == OMPC_MAP_unknown) {
854 Diag(Tok, diag::err_omp_unknown_map_type);
855 } else if (MapType == OMPC_MAP_always) {
856 Diag(Tok, diag::err_omp_map_type_missing);
857 }
858 ConsumeToken();
859 } else if (PP.LookAhead(0).is(tok::comma)) {
860 if (PP.LookAhead(1).is(tok::identifier) &&
861 PP.LookAhead(2).is(tok::colon)) {
862 MapTypeModifier =
863 static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
864 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
865 if (MapTypeModifier != OMPC_MAP_always) {
866 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
867 MapTypeModifier = OMPC_MAP_unknown;
868 } else {
869 MapTypeModifierSpecified = true;
870 }
871
872 ConsumeToken();
873 ConsumeToken();
874
875 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
876 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
877 if (MapType == OMPC_MAP_unknown || MapType == OMPC_MAP_always) {
878 Diag(Tok, diag::err_omp_unknown_map_type);
879 }
880 ConsumeToken();
881 } else {
882 MapType = OMPC_MAP_tofrom;
883 }
884 } else {
885 MapType = OMPC_MAP_tofrom;
886 }
887 } else {
888 UnexpectedId = true;
889 }
890
891 if (Tok.is(tok::colon)) {
892 ColonLoc = ConsumeToken();
893 } else if (ColonExpected) {
894 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
895 }
Alexey Bataevc5e02582014-06-16 07:08:35 +0000896 }
897
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000898 SmallVector<Expr *, 5> Vars;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000899 bool IsComma =
900 ((Kind != OMPC_reduction) && (Kind != OMPC_depend) &&
901 (Kind != OMPC_map)) ||
902 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
903 ((Kind == OMPC_map) && (UnexpectedId || MapType != OMPC_MAP_unknown) &&
904 (!MapTypeModifierSpecified ||
905 (MapTypeModifierSpecified && MapTypeModifier == OMPC_MAP_always))) ||
906 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000907 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000908 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000909 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000910 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000911 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +0000912 ExprResult VarExpr =
913 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000914 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000915 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000916 } else {
917 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000918 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000919 }
920 // Skip ',' if any
921 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000922 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000923 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000924 else if (Tok.isNot(tok::r_paren) &&
925 Tok.isNot(tok::annot_pragma_openmp_end) &&
926 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +0000927 Diag(Tok, diag::err_omp_expected_punc)
928 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
929 : getOpenMPClauseName(Kind))
930 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +0000931 }
932
Alexey Bataev182227b2015-08-20 10:54:39 +0000933 // Parse ')' for linear clause with modifier.
934 if (NeedRParenForLinear)
935 LinearT.consumeClose();
936
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000937 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000938 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000939 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
940 if (MustHaveTail) {
941 ColonLoc = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000942 SourceLocation ELoc = ConsumeToken();
943 ExprResult Tail = ParseAssignmentExpression();
944 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
Alexander Musman8dba6642014-04-22 13:09:42 +0000945 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000946 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000947 else
948 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
949 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000950 }
951
952 // Parse ')'.
953 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000954 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
955 (Kind != OMPC_depend && Vars.empty()) || (MustHaveTail && !TailExpr) ||
Kelvin Li0bff7af2015-11-23 05:32:03 +0000956 (Kind == OMPC_map && MapType == OMPC_MAP_unknown) ||
957 InvalidReductionId) {
Craig Topper161e4db2014-05-21 06:02:52 +0000958 return nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000959 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000960
Alexey Bataevc5e02582014-06-16 07:08:35 +0000961 return Actions.ActOnOpenMPVarListClause(
962 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
963 ReductionIdScopeSpec,
964 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000965 : DeclarationNameInfo(),
Kelvin Li0bff7af2015-11-23 05:32:03 +0000966 DepKind, LinearModifier, MapTypeModifier, MapType, DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000967}
968