blob: 8379b8b687f7a772233594240cb3657a94c53b25 [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 |
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000397/// simdlen-clause | threads-clause | simd-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:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000418 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +0000419 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000420 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000421 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +0000422 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +0000423 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +0000424 // OpenMP [2.9.1, target data construct, Restrictions]
425 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000426 // OpenMP [2.11.1, task Construct, Restrictions]
427 // At most one if clause can appear on the directive.
428 // At most one final clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000429 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000430 Diag(Tok, diag::err_omp_more_one_clause)
431 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000432 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000433 }
434
Alexey Bataev10e775f2015-07-30 11:36:16 +0000435 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
436 Clause = ParseOpenMPClause(CKind);
437 else
438 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000439 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000440 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000441 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000442 // OpenMP [2.14.3.1, Restrictions]
443 // Only a single default clause may be specified on a parallel, task or
444 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000445 // OpenMP [2.5, parallel Construct, Restrictions]
446 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000447 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000448 Diag(Tok, diag::err_omp_more_one_clause)
449 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000450 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000451 }
452
453 Clause = ParseOpenMPSimpleClause(CKind);
454 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000455 case OMPC_schedule:
456 // OpenMP [2.7.1, Restrictions, p. 3]
457 // Only one schedule clause can appear on a loop directive.
458 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000459 Diag(Tok, diag::err_omp_more_one_clause)
460 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000461 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000462 }
463
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000464 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000465 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
466 break;
Alexey Bataev236070f2014-06-20 11:19:47 +0000467 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000468 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000469 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000470 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000471 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000472 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000473 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000474 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +0000475 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000476 case OMPC_simd:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000477 // OpenMP [2.7.1, Restrictions, p. 9]
478 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000479 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
480 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000481 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000482 Diag(Tok, diag::err_omp_more_one_clause)
483 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000484 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000485 }
486
487 Clause = ParseOpenMPClause(CKind);
488 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000489 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000490 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000491 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000492 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000493 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000494 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000495 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000496 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000497 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000498 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000499 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +0000500 case OMPC_map:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000501 Clause = ParseOpenMPVarListClause(CKind);
502 break;
503 case OMPC_unknown:
504 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000505 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000506 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000507 break;
508 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000509 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
510 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000511 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000512 break;
513 }
Craig Topper161e4db2014-05-21 06:02:52 +0000514 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000515}
516
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000517/// \brief Parsing of OpenMP clauses with single expressions like 'final',
518/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams', 'thread_limit'
519/// or 'simdlen'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000520///
Alexey Bataev3778b602014-07-17 07:32:53 +0000521/// final-clause:
522/// 'final' '(' expression ')'
523///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000524/// num_threads-clause:
525/// 'num_threads' '(' expression ')'
526///
527/// safelen-clause:
528/// 'safelen' '(' expression ')'
529///
Alexey Bataev66b15b52015-08-21 11:14:16 +0000530/// simdlen-clause:
531/// 'simdlen' '(' expression ')'
532///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000533/// collapse-clause:
534/// 'collapse' '(' expression ')'
535///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000536OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
537 SourceLocation Loc = ConsumeToken();
538
539 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
540 if (T.expectAndConsume(diag::err_expected_lparen_after,
541 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000542 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000543
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000544 SourceLocation ELoc = Tok.getLocation();
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000545 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
546 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000547 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000548
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000549 // Parse ')'.
550 T.consumeClose();
551
552 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000553 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000554
Alexey Bataeva55ed262014-05-28 06:15:33 +0000555 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000556 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000557}
558
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000559/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000560///
561/// default-clause:
562/// 'default' '(' 'none' | 'shared' ')
563///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000564/// proc_bind-clause:
565/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
566///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000567OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
568 SourceLocation Loc = Tok.getLocation();
569 SourceLocation LOpen = ConsumeToken();
570 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000571 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000572 if (T.expectAndConsume(diag::err_expected_lparen_after,
573 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000574 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000575
Alexey Bataeva55ed262014-05-28 06:15:33 +0000576 unsigned Type = getOpenMPSimpleClauseType(
577 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000578 SourceLocation TypeLoc = Tok.getLocation();
579 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
580 Tok.isNot(tok::annot_pragma_openmp_end))
581 ConsumeAnyToken();
582
583 // Parse ')'.
584 T.consumeClose();
585
586 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
587 Tok.getLocation());
588}
589
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000590/// \brief Parsing of OpenMP clauses like 'ordered'.
591///
592/// ordered-clause:
593/// 'ordered'
594///
Alexey Bataev236070f2014-06-20 11:19:47 +0000595/// nowait-clause:
596/// 'nowait'
597///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000598/// untied-clause:
599/// 'untied'
600///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000601/// mergeable-clause:
602/// 'mergeable'
603///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000604/// read-clause:
605/// 'read'
606///
Alexey Bataev346265e2015-09-25 10:37:12 +0000607/// threads-clause:
608/// 'threads'
609///
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000610/// simd-clause:
611/// 'simd'
612///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000613OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
614 SourceLocation Loc = Tok.getLocation();
615 ConsumeAnyToken();
616
617 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
618}
619
620
Alexey Bataev56dafe82014-06-20 07:16:17 +0000621/// \brief Parsing of OpenMP clauses with single expressions and some additional
622/// argument like 'schedule' or 'dist_schedule'.
623///
624/// schedule-clause:
625/// 'schedule' '(' kind [',' expression ] ')'
626///
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000627/// if-clause:
628/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
629///
Alexey Bataev56dafe82014-06-20 07:16:17 +0000630OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
631 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000632 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000633 // Parse '('.
634 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
635 if (T.expectAndConsume(diag::err_expected_lparen_after,
636 getOpenMPClauseName(Kind)))
637 return nullptr;
638
639 ExprResult Val;
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000640 unsigned Arg;
641 SourceLocation KLoc;
642 if (Kind == OMPC_schedule) {
643 Arg = getOpenMPSimpleClauseType(
644 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
645 KLoc = Tok.getLocation();
646 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
647 Tok.isNot(tok::annot_pragma_openmp_end))
648 ConsumeAnyToken();
649 if ((Arg == OMPC_SCHEDULE_static || Arg == OMPC_SCHEDULE_dynamic ||
650 Arg == OMPC_SCHEDULE_guided) &&
651 Tok.is(tok::comma))
652 DelimLoc = ConsumeAnyToken();
653 } else {
654 assert(Kind == OMPC_if);
655 KLoc = Tok.getLocation();
656 Arg = ParseOpenMPDirectiveKind(*this);
657 if (Arg != OMPD_unknown) {
658 ConsumeToken();
659 if (Tok.is(tok::colon))
660 DelimLoc = ConsumeToken();
661 else
662 Diag(Tok, diag::warn_pragma_expected_colon)
663 << "directive name modifier";
664 }
665 }
Alexey Bataev56dafe82014-06-20 07:16:17 +0000666
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000667 bool NeedAnExpression =
668 (Kind == OMPC_schedule && DelimLoc.isValid()) || Kind == OMPC_if;
669 if (NeedAnExpression) {
670 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +0000671 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
672 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000673 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +0000674 }
675
676 // Parse ')'.
677 T.consumeClose();
678
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000679 if (NeedAnExpression && Val.isInvalid())
680 return nullptr;
681
Alexey Bataev56dafe82014-06-20 07:16:17 +0000682 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000683 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +0000684 T.getCloseLocation());
685}
686
Alexey Bataevc5e02582014-06-16 07:08:35 +0000687static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
688 UnqualifiedId &ReductionId) {
689 SourceLocation TemplateKWLoc;
690 if (ReductionIdScopeSpec.isEmpty()) {
691 auto OOK = OO_None;
692 switch (P.getCurToken().getKind()) {
693 case tok::plus:
694 OOK = OO_Plus;
695 break;
696 case tok::minus:
697 OOK = OO_Minus;
698 break;
699 case tok::star:
700 OOK = OO_Star;
701 break;
702 case tok::amp:
703 OOK = OO_Amp;
704 break;
705 case tok::pipe:
706 OOK = OO_Pipe;
707 break;
708 case tok::caret:
709 OOK = OO_Caret;
710 break;
711 case tok::ampamp:
712 OOK = OO_AmpAmp;
713 break;
714 case tok::pipepipe:
715 OOK = OO_PipePipe;
716 break;
717 default:
718 break;
719 }
720 if (OOK != OO_None) {
721 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000722 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000723 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
724 return false;
725 }
726 }
727 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
728 /*AllowDestructorName*/ false,
729 /*AllowConstructorName*/ false, ParsedType(),
730 TemplateKWLoc, ReductionId);
731}
732
Alexander Musman1bb328c2014-06-04 13:06:39 +0000733/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000734/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000735///
736/// private-clause:
737/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000738/// firstprivate-clause:
739/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000740/// lastprivate-clause:
741/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000742/// shared-clause:
743/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000744/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +0000745/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000746/// aligned-clause:
747/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000748/// reduction-clause:
749/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000750/// copyprivate-clause:
751/// 'copyprivate' '(' list ')'
752/// flush-clause:
753/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000754/// depend-clause:
755/// 'depend' '(' in | out | inout : list ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +0000756/// map-clause:
757/// 'map' '(' [ [ always , ]
758/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000759///
Alexey Bataev182227b2015-08-20 10:54:39 +0000760/// For 'linear' clause linear-list may have the following forms:
761/// list
762/// modifier(list)
763/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000764OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
765 SourceLocation Loc = Tok.getLocation();
766 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000767 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000768 // Optional scope specifier and unqualified id for reduction identifier.
769 CXXScopeSpec ReductionIdScopeSpec;
770 UnqualifiedId ReductionId;
771 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000772 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
Alexey Bataev182227b2015-08-20 10:54:39 +0000773 // OpenMP 4.1 [2.15.3.7, linear Clause]
774 // If no modifier is specified it is assumed to be val.
775 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000776 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
777 OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown;
778 bool MapTypeModifierSpecified = false;
779 bool UnexpectedId = false;
780 SourceLocation DepLinMapLoc;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000781
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000782 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000783 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000784 if (T.expectAndConsume(diag::err_expected_lparen_after,
785 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000786 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000787
Alexey Bataev182227b2015-08-20 10:54:39 +0000788 bool NeedRParenForLinear = false;
789 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
790 tok::annot_pragma_openmp_end);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000791 // Handle reduction-identifier for reduction clause.
792 if (Kind == OMPC_reduction) {
793 ColonProtectionRAIIObject ColonRAII(*this);
794 if (getLangOpts().CPlusPlus) {
795 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
796 }
797 InvalidReductionId =
798 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
799 if (InvalidReductionId) {
800 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
801 StopBeforeMatch);
802 }
803 if (Tok.is(tok::colon)) {
804 ColonLoc = ConsumeToken();
805 } else {
806 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
807 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000808 } else if (Kind == OMPC_depend) {
809 // Handle dependency type for depend clause.
810 ColonProtectionRAIIObject ColonRAII(*this);
811 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
812 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000813 DepLinMapLoc = Tok.getLocation();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000814
815 if (DepKind == OMPC_DEPEND_unknown) {
816 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
817 StopBeforeMatch);
818 } else {
819 ConsumeToken();
820 }
821 if (Tok.is(tok::colon)) {
822 ColonLoc = ConsumeToken();
823 } else {
824 Diag(Tok, diag::warn_pragma_expected_colon) << "dependency type";
825 }
Alexey Bataev182227b2015-08-20 10:54:39 +0000826 } else if (Kind == OMPC_linear) {
827 // Try to parse modifier if any.
828 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev182227b2015-08-20 10:54:39 +0000829 LinearModifier = static_cast<OpenMPLinearClauseKind>(
Alexey Bataev1185e192015-08-20 12:15:57 +0000830 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000831 DepLinMapLoc = ConsumeToken();
Alexey Bataev182227b2015-08-20 10:54:39 +0000832 LinearT.consumeOpen();
833 NeedRParenForLinear = true;
834 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000835 } else if (Kind == OMPC_map) {
836 // Handle map type for map clause.
837 ColonProtectionRAIIObject ColonRAII(*this);
838
839 // the first identifier may be a list item, a map-type or
840 // a map-type-modifier
841 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
842 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
843 DepLinMapLoc = Tok.getLocation();
844 bool ColonExpected = false;
845
846 if (Tok.is(tok::identifier)) {
847 if (PP.LookAhead(0).is(tok::colon)) {
848 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
849 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
850 if (MapType == OMPC_MAP_unknown) {
851 Diag(Tok, diag::err_omp_unknown_map_type);
852 } else if (MapType == OMPC_MAP_always) {
853 Diag(Tok, diag::err_omp_map_type_missing);
854 }
855 ConsumeToken();
856 } else if (PP.LookAhead(0).is(tok::comma)) {
857 if (PP.LookAhead(1).is(tok::identifier) &&
858 PP.LookAhead(2).is(tok::colon)) {
859 MapTypeModifier =
860 static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
861 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
862 if (MapTypeModifier != OMPC_MAP_always) {
863 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
864 MapTypeModifier = OMPC_MAP_unknown;
865 } else {
866 MapTypeModifierSpecified = true;
867 }
868
869 ConsumeToken();
870 ConsumeToken();
871
872 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
873 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
874 if (MapType == OMPC_MAP_unknown || MapType == OMPC_MAP_always) {
875 Diag(Tok, diag::err_omp_unknown_map_type);
876 }
877 ConsumeToken();
878 } else {
879 MapType = OMPC_MAP_tofrom;
880 }
881 } else {
882 MapType = OMPC_MAP_tofrom;
883 }
884 } else {
885 UnexpectedId = true;
886 }
887
888 if (Tok.is(tok::colon)) {
889 ColonLoc = ConsumeToken();
890 } else if (ColonExpected) {
891 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
892 }
Alexey Bataevc5e02582014-06-16 07:08:35 +0000893 }
894
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000895 SmallVector<Expr *, 5> Vars;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000896 bool IsComma =
897 ((Kind != OMPC_reduction) && (Kind != OMPC_depend) &&
898 (Kind != OMPC_map)) ||
899 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
900 ((Kind == OMPC_map) && (UnexpectedId || MapType != OMPC_MAP_unknown) &&
901 (!MapTypeModifierSpecified ||
902 (MapTypeModifierSpecified && MapTypeModifier == OMPC_MAP_always))) ||
903 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000904 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000905 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000906 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000907 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000908 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +0000909 ExprResult VarExpr =
910 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000911 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000912 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000913 } else {
914 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000915 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000916 }
917 // Skip ',' if any
918 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000919 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000920 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000921 else if (Tok.isNot(tok::r_paren) &&
922 Tok.isNot(tok::annot_pragma_openmp_end) &&
923 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +0000924 Diag(Tok, diag::err_omp_expected_punc)
925 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
926 : getOpenMPClauseName(Kind))
927 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +0000928 }
929
Alexey Bataev182227b2015-08-20 10:54:39 +0000930 // Parse ')' for linear clause with modifier.
931 if (NeedRParenForLinear)
932 LinearT.consumeClose();
933
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000934 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000935 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000936 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
937 if (MustHaveTail) {
938 ColonLoc = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000939 SourceLocation ELoc = ConsumeToken();
940 ExprResult Tail = ParseAssignmentExpression();
941 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
Alexander Musman8dba6642014-04-22 13:09:42 +0000942 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000943 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000944 else
945 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
946 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000947 }
948
949 // Parse ')'.
950 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000951 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
952 (Kind != OMPC_depend && Vars.empty()) || (MustHaveTail && !TailExpr) ||
Kelvin Li0bff7af2015-11-23 05:32:03 +0000953 (Kind == OMPC_map && MapType == OMPC_MAP_unknown) ||
954 InvalidReductionId) {
Craig Topper161e4db2014-05-21 06:02:52 +0000955 return nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000956 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000957
Alexey Bataevc5e02582014-06-16 07:08:35 +0000958 return Actions.ActOnOpenMPVarListClause(
959 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
960 ReductionIdScopeSpec,
961 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000962 : DeclarationNameInfo(),
Kelvin Li0bff7af2015-11-23 05:32:03 +0000963 DepKind, LinearModifier, MapTypeModifier, MapType, DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000964}
965