blob: 6c9c88a49c2d22b8d60d125da9326352f074d2a4 [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 |
397/// simdlen-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 Bataev142e1fc2014-06-20 09:44:06 +0000475 // OpenMP [2.7.1, Restrictions, p. 9]
476 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000477 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
478 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000479 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000480 Diag(Tok, diag::err_omp_more_one_clause)
481 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000482 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000483 }
484
485 Clause = ParseOpenMPClause(CKind);
486 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000487 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000488 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000489 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000490 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000491 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000492 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000493 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000494 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000495 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000496 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000497 case OMPC_depend:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000498 Clause = ParseOpenMPVarListClause(CKind);
499 break;
500 case OMPC_unknown:
501 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000502 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000503 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000504 break;
505 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000506 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
507 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000508 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000509 break;
510 }
Craig Topper161e4db2014-05-21 06:02:52 +0000511 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000512}
513
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000514/// \brief Parsing of OpenMP clauses with single expressions like 'final',
515/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams', 'thread_limit'
516/// or 'simdlen'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000517///
Alexey Bataev3778b602014-07-17 07:32:53 +0000518/// final-clause:
519/// 'final' '(' expression ')'
520///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000521/// num_threads-clause:
522/// 'num_threads' '(' expression ')'
523///
524/// safelen-clause:
525/// 'safelen' '(' expression ')'
526///
Alexey Bataev66b15b52015-08-21 11:14:16 +0000527/// simdlen-clause:
528/// 'simdlen' '(' expression ')'
529///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000530/// collapse-clause:
531/// 'collapse' '(' expression ')'
532///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000533OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
534 SourceLocation Loc = ConsumeToken();
535
536 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
537 if (T.expectAndConsume(diag::err_expected_lparen_after,
538 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000539 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000540
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000541 SourceLocation ELoc = Tok.getLocation();
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000542 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
543 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000544 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000545
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000546 // Parse ')'.
547 T.consumeClose();
548
549 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000550 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000551
Alexey Bataeva55ed262014-05-28 06:15:33 +0000552 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000553 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000554}
555
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000556/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000557///
558/// default-clause:
559/// 'default' '(' 'none' | 'shared' ')
560///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000561/// proc_bind-clause:
562/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
563///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000564OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
565 SourceLocation Loc = Tok.getLocation();
566 SourceLocation LOpen = ConsumeToken();
567 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000568 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000569 if (T.expectAndConsume(diag::err_expected_lparen_after,
570 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000571 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000572
Alexey Bataeva55ed262014-05-28 06:15:33 +0000573 unsigned Type = getOpenMPSimpleClauseType(
574 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000575 SourceLocation TypeLoc = Tok.getLocation();
576 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
577 Tok.isNot(tok::annot_pragma_openmp_end))
578 ConsumeAnyToken();
579
580 // Parse ')'.
581 T.consumeClose();
582
583 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
584 Tok.getLocation());
585}
586
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000587/// \brief Parsing of OpenMP clauses like 'ordered'.
588///
589/// ordered-clause:
590/// 'ordered'
591///
Alexey Bataev236070f2014-06-20 11:19:47 +0000592/// nowait-clause:
593/// 'nowait'
594///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000595/// untied-clause:
596/// 'untied'
597///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000598/// mergeable-clause:
599/// 'mergeable'
600///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000601/// read-clause:
602/// 'read'
603///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000604OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
605 SourceLocation Loc = Tok.getLocation();
606 ConsumeAnyToken();
607
608 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
609}
610
611
Alexey Bataev56dafe82014-06-20 07:16:17 +0000612/// \brief Parsing of OpenMP clauses with single expressions and some additional
613/// argument like 'schedule' or 'dist_schedule'.
614///
615/// schedule-clause:
616/// 'schedule' '(' kind [',' expression ] ')'
617///
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000618/// if-clause:
619/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
620///
Alexey Bataev56dafe82014-06-20 07:16:17 +0000621OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
622 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000623 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000624 // Parse '('.
625 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
626 if (T.expectAndConsume(diag::err_expected_lparen_after,
627 getOpenMPClauseName(Kind)))
628 return nullptr;
629
630 ExprResult Val;
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000631 unsigned Arg;
632 SourceLocation KLoc;
633 if (Kind == OMPC_schedule) {
634 Arg = getOpenMPSimpleClauseType(
635 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
636 KLoc = Tok.getLocation();
637 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
638 Tok.isNot(tok::annot_pragma_openmp_end))
639 ConsumeAnyToken();
640 if ((Arg == OMPC_SCHEDULE_static || Arg == OMPC_SCHEDULE_dynamic ||
641 Arg == OMPC_SCHEDULE_guided) &&
642 Tok.is(tok::comma))
643 DelimLoc = ConsumeAnyToken();
644 } else {
645 assert(Kind == OMPC_if);
646 KLoc = Tok.getLocation();
647 Arg = ParseOpenMPDirectiveKind(*this);
648 if (Arg != OMPD_unknown) {
649 ConsumeToken();
650 if (Tok.is(tok::colon))
651 DelimLoc = ConsumeToken();
652 else
653 Diag(Tok, diag::warn_pragma_expected_colon)
654 << "directive name modifier";
655 }
656 }
Alexey Bataev56dafe82014-06-20 07:16:17 +0000657
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000658 bool NeedAnExpression =
659 (Kind == OMPC_schedule && DelimLoc.isValid()) || Kind == OMPC_if;
660 if (NeedAnExpression) {
661 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +0000662 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
663 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000664 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +0000665 }
666
667 // Parse ')'.
668 T.consumeClose();
669
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000670 if (NeedAnExpression && Val.isInvalid())
671 return nullptr;
672
Alexey Bataev56dafe82014-06-20 07:16:17 +0000673 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000674 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +0000675 T.getCloseLocation());
676}
677
Alexey Bataevc5e02582014-06-16 07:08:35 +0000678static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
679 UnqualifiedId &ReductionId) {
680 SourceLocation TemplateKWLoc;
681 if (ReductionIdScopeSpec.isEmpty()) {
682 auto OOK = OO_None;
683 switch (P.getCurToken().getKind()) {
684 case tok::plus:
685 OOK = OO_Plus;
686 break;
687 case tok::minus:
688 OOK = OO_Minus;
689 break;
690 case tok::star:
691 OOK = OO_Star;
692 break;
693 case tok::amp:
694 OOK = OO_Amp;
695 break;
696 case tok::pipe:
697 OOK = OO_Pipe;
698 break;
699 case tok::caret:
700 OOK = OO_Caret;
701 break;
702 case tok::ampamp:
703 OOK = OO_AmpAmp;
704 break;
705 case tok::pipepipe:
706 OOK = OO_PipePipe;
707 break;
708 default:
709 break;
710 }
711 if (OOK != OO_None) {
712 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000713 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000714 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
715 return false;
716 }
717 }
718 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
719 /*AllowDestructorName*/ false,
720 /*AllowConstructorName*/ false, ParsedType(),
721 TemplateKWLoc, ReductionId);
722}
723
Alexander Musman1bb328c2014-06-04 13:06:39 +0000724/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000725/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000726///
727/// private-clause:
728/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000729/// firstprivate-clause:
730/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000731/// lastprivate-clause:
732/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000733/// shared-clause:
734/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000735/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +0000736/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000737/// aligned-clause:
738/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000739/// reduction-clause:
740/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000741/// copyprivate-clause:
742/// 'copyprivate' '(' list ')'
743/// flush-clause:
744/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000745/// depend-clause:
746/// 'depend' '(' in | out | inout : list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000747///
Alexey Bataev182227b2015-08-20 10:54:39 +0000748/// For 'linear' clause linear-list may have the following forms:
749/// list
750/// modifier(list)
751/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000752OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
753 SourceLocation Loc = Tok.getLocation();
754 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000755 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000756 // Optional scope specifier and unqualified id for reduction identifier.
757 CXXScopeSpec ReductionIdScopeSpec;
758 UnqualifiedId ReductionId;
759 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000760 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
Alexey Bataev182227b2015-08-20 10:54:39 +0000761 // OpenMP 4.1 [2.15.3.7, linear Clause]
762 // If no modifier is specified it is assumed to be val.
763 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
764 SourceLocation DepLinLoc;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000765
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000766 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000767 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000768 if (T.expectAndConsume(diag::err_expected_lparen_after,
769 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000770 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000771
Alexey Bataev182227b2015-08-20 10:54:39 +0000772 bool NeedRParenForLinear = false;
773 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
774 tok::annot_pragma_openmp_end);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000775 // Handle reduction-identifier for reduction clause.
776 if (Kind == OMPC_reduction) {
777 ColonProtectionRAIIObject ColonRAII(*this);
778 if (getLangOpts().CPlusPlus) {
779 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
780 }
781 InvalidReductionId =
782 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
783 if (InvalidReductionId) {
784 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
785 StopBeforeMatch);
786 }
787 if (Tok.is(tok::colon)) {
788 ColonLoc = ConsumeToken();
789 } else {
790 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
791 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000792 } else if (Kind == OMPC_depend) {
793 // Handle dependency type for depend clause.
794 ColonProtectionRAIIObject ColonRAII(*this);
795 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
796 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
Alexey Bataev182227b2015-08-20 10:54:39 +0000797 DepLinLoc = Tok.getLocation();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000798
799 if (DepKind == OMPC_DEPEND_unknown) {
800 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
801 StopBeforeMatch);
802 } else {
803 ConsumeToken();
804 }
805 if (Tok.is(tok::colon)) {
806 ColonLoc = ConsumeToken();
807 } else {
808 Diag(Tok, diag::warn_pragma_expected_colon) << "dependency type";
809 }
Alexey Bataev182227b2015-08-20 10:54:39 +0000810 } else if (Kind == OMPC_linear) {
811 // Try to parse modifier if any.
812 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev182227b2015-08-20 10:54:39 +0000813 LinearModifier = static_cast<OpenMPLinearClauseKind>(
Alexey Bataev1185e192015-08-20 12:15:57 +0000814 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
Alexey Bataev182227b2015-08-20 10:54:39 +0000815 DepLinLoc = ConsumeToken();
816 LinearT.consumeOpen();
817 NeedRParenForLinear = true;
818 }
Alexey Bataevc5e02582014-06-16 07:08:35 +0000819 }
820
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000821 SmallVector<Expr *, 5> Vars;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000822 bool IsComma = ((Kind != OMPC_reduction) && (Kind != OMPC_depend)) ||
823 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
824 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000825 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000826 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000827 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000828 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000829 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +0000830 ExprResult VarExpr =
831 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000832 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000833 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000834 } else {
835 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000836 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000837 }
838 // Skip ',' if any
839 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000840 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000841 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000842 else if (Tok.isNot(tok::r_paren) &&
843 Tok.isNot(tok::annot_pragma_openmp_end) &&
844 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +0000845 Diag(Tok, diag::err_omp_expected_punc)
846 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
847 : getOpenMPClauseName(Kind))
848 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +0000849 }
850
Alexey Bataev182227b2015-08-20 10:54:39 +0000851 // Parse ')' for linear clause with modifier.
852 if (NeedRParenForLinear)
853 LinearT.consumeClose();
854
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000855 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000856 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000857 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
858 if (MustHaveTail) {
859 ColonLoc = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000860 SourceLocation ELoc = ConsumeToken();
861 ExprResult Tail = ParseAssignmentExpression();
862 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
Alexander Musman8dba6642014-04-22 13:09:42 +0000863 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000864 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000865 else
866 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
867 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000868 }
869
870 // Parse ')'.
871 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000872 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
873 (Kind != OMPC_depend && Vars.empty()) || (MustHaveTail && !TailExpr) ||
874 InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000875 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000876
Alexey Bataevc5e02582014-06-16 07:08:35 +0000877 return Actions.ActOnOpenMPVarListClause(
878 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
879 ReductionIdScopeSpec,
880 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000881 : DeclarationNameInfo(),
Alexey Bataev182227b2015-08-20 10:54:39 +0000882 DepKind, LinearModifier, DepLinLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000883}
884