blob: 1266e447fca3184ee2eaecd1c9457ad0cbf6b5a9 [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;
291 bool CreateDirective = true;
Alexey Bataev68446b72014-07-18 07:47:19 +0000292 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000293 // The body is a block scope like in Lambdas and Blocks.
294 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000295 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000296 Actions.ActOnStartOfCompoundStmt();
297 // Parse statement
298 AssociatedStmt = ParseStatement();
299 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000300 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
301 CreateDirective = AssociatedStmt.isUsable();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000302 }
303 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000304 Directive = Actions.ActOnOpenMPExecutableDirective(
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000305 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
306 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000307
308 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000309 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000310 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000311 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000312 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000313 case OMPD_unknown:
314 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000315 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000316 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000317 }
318 return Directive;
319}
320
Alexey Bataeva769e072013-03-22 06:34:35 +0000321/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000322/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000323///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000324/// simple-variable-list:
325/// '(' id-expression {, id-expression} ')'
326///
327bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
328 SmallVectorImpl<Expr *> &VarList,
329 bool AllowScopeSpecifier) {
330 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000331 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000332 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000333 if (T.expectAndConsume(diag::err_expected_lparen_after,
334 getOpenMPDirectiveName(Kind)))
335 return true;
336 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000337 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000338
339 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000340 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000341 CXXScopeSpec SS;
342 SourceLocation TemplateKWLoc;
343 UnqualifiedId Name;
344 // Read var name.
345 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000346 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000347
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000348 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
349 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000350 IsCorrect = false;
351 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000352 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000353 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
354 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000355 IsCorrect = false;
356 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000357 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000358 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
359 Tok.isNot(tok::annot_pragma_openmp_end)) {
360 IsCorrect = false;
361 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000362 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000363 Diag(PrevTok.getLocation(), diag::err_expected)
364 << tok::identifier
365 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000366 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000367 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000368 ExprResult Res =
369 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000370 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000371 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000372 }
373 // Consume ','.
374 if (Tok.is(tok::comma)) {
375 ConsumeToken();
376 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000377 }
378
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000379 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000380 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000381 IsCorrect = false;
382 }
383
384 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000385 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000386
387 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000388}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000389
390/// \brief Parsing of OpenMP clauses.
391///
392/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000393/// if-clause | final-clause | num_threads-clause | safelen-clause |
394/// default-clause | private-clause | firstprivate-clause | shared-clause
395/// | linear-clause | aligned-clause | collapse-clause |
396/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000397/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000398/// mergeable-clause | flush-clause | read-clause | write-clause |
Michael Wong65f367f2015-07-21 13:44:28 +0000399/// update-clause | capture-clause | seq_cst-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000400///
401OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
402 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000403 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000404 bool ErrorFound = false;
405 // Check if clause is allowed for the given directive.
406 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000407 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
408 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000409 ErrorFound = true;
410 }
411
412 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000413 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +0000414 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000415 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000416 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000417 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +0000418 case OMPC_ordered:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000419 // OpenMP [2.5, Restrictions]
420 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000421 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000422 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000423 // Only one safelen clause can appear on a simd directive.
424 // Only one collapse clause can appear on a simd directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000425 // OpenMP [2.11.1, task Construct, Restrictions]
426 // At most one if clause can appear on the directive.
427 // At most one final clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000428 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000429 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
430 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000431 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000432 }
433
Alexey Bataev10e775f2015-07-30 11:36:16 +0000434 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
435 Clause = ParseOpenMPClause(CKind);
436 else
437 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000438 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000439 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000440 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000441 // OpenMP [2.14.3.1, Restrictions]
442 // Only a single default clause may be specified on a parallel, task or
443 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000444 // OpenMP [2.5, parallel Construct, Restrictions]
445 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000446 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000447 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
448 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000449 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000450 }
451
452 Clause = ParseOpenMPSimpleClause(CKind);
453 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000454 case OMPC_schedule:
455 // OpenMP [2.7.1, Restrictions, p. 3]
456 // Only one schedule clause can appear on a loop directive.
457 if (!FirstClause) {
458 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
459 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000460 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000461 }
462
463 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
464 break;
Alexey Bataev236070f2014-06-20 11:19:47 +0000465 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000466 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000467 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000468 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000469 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000470 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000471 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000472 case OMPC_seq_cst:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000473 // OpenMP [2.7.1, Restrictions, p. 9]
474 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000475 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
476 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000477 if (!FirstClause) {
478 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
479 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000480 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000481 }
482
483 Clause = ParseOpenMPClause(CKind);
484 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000485 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000486 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000487 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000488 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000489 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000490 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000491 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000492 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000493 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000494 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000495 case OMPC_depend:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000496 Clause = ParseOpenMPVarListClause(CKind);
497 break;
498 case OMPC_unknown:
499 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000500 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000501 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000502 break;
503 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000504 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
505 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000506 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000507 break;
508 }
Craig Topper161e4db2014-05-21 06:02:52 +0000509 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000510}
511
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000512/// \brief Parsing of OpenMP clauses with single expressions like 'if',
Alexey Bataev3778b602014-07-17 07:32:53 +0000513/// 'final', 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000514/// 'thread_limit'.
515///
516/// if-clause:
517/// 'if' '(' expression ')'
518///
Alexey Bataev3778b602014-07-17 07:32:53 +0000519/// final-clause:
520/// 'final' '(' expression ')'
521///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000522/// num_threads-clause:
523/// 'num_threads' '(' expression ')'
524///
525/// safelen-clause:
526/// 'safelen' '(' expression ')'
527///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000528/// collapse-clause:
529/// 'collapse' '(' expression ')'
530///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000531OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
532 SourceLocation Loc = ConsumeToken();
533
534 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
535 if (T.expectAndConsume(diag::err_expected_lparen_after,
536 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000537 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000538
539 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
540 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
541
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000542 // Parse ')'.
543 T.consumeClose();
544
545 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000546 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000547
Alexey Bataeva55ed262014-05-28 06:15:33 +0000548 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000549 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000550}
551
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000552/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000553///
554/// default-clause:
555/// 'default' '(' 'none' | 'shared' ')
556///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000557/// proc_bind-clause:
558/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
559///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000560OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
561 SourceLocation Loc = Tok.getLocation();
562 SourceLocation LOpen = ConsumeToken();
563 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000564 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000565 if (T.expectAndConsume(diag::err_expected_lparen_after,
566 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000567 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000568
Alexey Bataeva55ed262014-05-28 06:15:33 +0000569 unsigned Type = getOpenMPSimpleClauseType(
570 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000571 SourceLocation TypeLoc = Tok.getLocation();
572 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
573 Tok.isNot(tok::annot_pragma_openmp_end))
574 ConsumeAnyToken();
575
576 // Parse ')'.
577 T.consumeClose();
578
579 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
580 Tok.getLocation());
581}
582
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000583/// \brief Parsing of OpenMP clauses like 'ordered'.
584///
585/// ordered-clause:
586/// 'ordered'
587///
Alexey Bataev236070f2014-06-20 11:19:47 +0000588/// nowait-clause:
589/// 'nowait'
590///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000591/// untied-clause:
592/// 'untied'
593///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000594/// mergeable-clause:
595/// 'mergeable'
596///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000597/// read-clause:
598/// 'read'
599///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000600OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
601 SourceLocation Loc = Tok.getLocation();
602 ConsumeAnyToken();
603
604 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
605}
606
607
Alexey Bataev56dafe82014-06-20 07:16:17 +0000608/// \brief Parsing of OpenMP clauses with single expressions and some additional
609/// argument like 'schedule' or 'dist_schedule'.
610///
611/// schedule-clause:
612/// 'schedule' '(' kind [',' expression ] ')'
613///
614OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
615 SourceLocation Loc = ConsumeToken();
616 SourceLocation CommaLoc;
617 // Parse '('.
618 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
619 if (T.expectAndConsume(diag::err_expected_lparen_after,
620 getOpenMPClauseName(Kind)))
621 return nullptr;
622
623 ExprResult Val;
624 unsigned Type = getOpenMPSimpleClauseType(
625 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
626 SourceLocation KLoc = Tok.getLocation();
627 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
628 Tok.isNot(tok::annot_pragma_openmp_end))
629 ConsumeAnyToken();
630
631 if (Kind == OMPC_schedule &&
632 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
633 Type == OMPC_SCHEDULE_guided) &&
634 Tok.is(tok::comma)) {
635 CommaLoc = ConsumeAnyToken();
636 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
637 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
638 if (Val.isInvalid())
639 return nullptr;
640 }
641
642 // Parse ')'.
643 T.consumeClose();
644
645 return Actions.ActOnOpenMPSingleExprWithArgClause(
646 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
647 T.getCloseLocation());
648}
649
Alexey Bataevc5e02582014-06-16 07:08:35 +0000650static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
651 UnqualifiedId &ReductionId) {
652 SourceLocation TemplateKWLoc;
653 if (ReductionIdScopeSpec.isEmpty()) {
654 auto OOK = OO_None;
655 switch (P.getCurToken().getKind()) {
656 case tok::plus:
657 OOK = OO_Plus;
658 break;
659 case tok::minus:
660 OOK = OO_Minus;
661 break;
662 case tok::star:
663 OOK = OO_Star;
664 break;
665 case tok::amp:
666 OOK = OO_Amp;
667 break;
668 case tok::pipe:
669 OOK = OO_Pipe;
670 break;
671 case tok::caret:
672 OOK = OO_Caret;
673 break;
674 case tok::ampamp:
675 OOK = OO_AmpAmp;
676 break;
677 case tok::pipepipe:
678 OOK = OO_PipePipe;
679 break;
680 default:
681 break;
682 }
683 if (OOK != OO_None) {
684 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000685 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000686 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
687 return false;
688 }
689 }
690 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
691 /*AllowDestructorName*/ false,
692 /*AllowConstructorName*/ false, ParsedType(),
693 TemplateKWLoc, ReductionId);
694}
695
Alexander Musman1bb328c2014-06-04 13:06:39 +0000696/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000697/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000698///
699/// private-clause:
700/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000701/// firstprivate-clause:
702/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000703/// lastprivate-clause:
704/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000705/// shared-clause:
706/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000707/// linear-clause:
708/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000709/// aligned-clause:
710/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000711/// reduction-clause:
712/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000713/// copyprivate-clause:
714/// 'copyprivate' '(' list ')'
715/// flush-clause:
716/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000717/// depend-clause:
718/// 'depend' '(' in | out | inout : list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000719///
720OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
721 SourceLocation Loc = Tok.getLocation();
722 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000723 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000724 // Optional scope specifier and unqualified id for reduction identifier.
725 CXXScopeSpec ReductionIdScopeSpec;
726 UnqualifiedId ReductionId;
727 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000728 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
729 SourceLocation DepLoc;
730
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000731 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000732 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000733 if (T.expectAndConsume(diag::err_expected_lparen_after,
734 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000735 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000736
Alexey Bataevc5e02582014-06-16 07:08:35 +0000737 // Handle reduction-identifier for reduction clause.
738 if (Kind == OMPC_reduction) {
739 ColonProtectionRAIIObject ColonRAII(*this);
740 if (getLangOpts().CPlusPlus) {
741 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
742 }
743 InvalidReductionId =
744 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
745 if (InvalidReductionId) {
746 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
747 StopBeforeMatch);
748 }
749 if (Tok.is(tok::colon)) {
750 ColonLoc = ConsumeToken();
751 } else {
752 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
753 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000754 } else if (Kind == OMPC_depend) {
755 // Handle dependency type for depend clause.
756 ColonProtectionRAIIObject ColonRAII(*this);
757 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
758 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
759 DepLoc = Tok.getLocation();
760
761 if (DepKind == OMPC_DEPEND_unknown) {
762 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
763 StopBeforeMatch);
764 } else {
765 ConsumeToken();
766 }
767 if (Tok.is(tok::colon)) {
768 ColonLoc = ConsumeToken();
769 } else {
770 Diag(Tok, diag::warn_pragma_expected_colon) << "dependency type";
771 }
Alexey Bataevc5e02582014-06-16 07:08:35 +0000772 }
773
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000774 SmallVector<Expr *, 5> Vars;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000775 bool IsComma = ((Kind != OMPC_reduction) && (Kind != OMPC_depend)) ||
776 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
777 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000778 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000779 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000780 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000781 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000782 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +0000783 ExprResult VarExpr =
784 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000785 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000786 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000787 } else {
788 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000789 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000790 }
791 // Skip ',' if any
792 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000793 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000794 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000795 else if (Tok.isNot(tok::r_paren) &&
796 Tok.isNot(tok::annot_pragma_openmp_end) &&
797 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +0000798 Diag(Tok, diag::err_omp_expected_punc)
799 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
800 : getOpenMPClauseName(Kind))
801 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +0000802 }
803
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000804 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000805 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000806 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
807 if (MustHaveTail) {
808 ColonLoc = Tok.getLocation();
809 ConsumeToken();
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000810 ExprResult Tail =
811 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexander Musman8dba6642014-04-22 13:09:42 +0000812 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000813 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000814 else
815 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
816 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000817 }
818
819 // Parse ')'.
820 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000821 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
822 (Kind != OMPC_depend && Vars.empty()) || (MustHaveTail && !TailExpr) ||
823 InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000824 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000825
Alexey Bataevc5e02582014-06-16 07:08:35 +0000826 return Actions.ActOnOpenMPVarListClause(
827 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
828 ReductionIdScopeSpec,
829 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000830 : DeclarationNameInfo(),
831 DepKind, DepLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000832}
833