blob: 37eeabae10a7f1a44c9031154ecf1bfdbda2dd38 [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements parsing of all OpenMP directives and clauses.
11///
12//===----------------------------------------------------------------------===//
13
Chandler Carruth5553d0d2014-01-07 11:51:46 +000014#include "RAIIObjectsForParser.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTConsumer.h"
16#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000017#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000019#include "clang/Parse/Parser.h"
20#include "clang/Sema/Scope.h"
21#include "llvm/ADT/PointerIntPair.h"
Michael Wong65f367f2015-07-21 13:44:28 +000022
Alexey Bataeva769e072013-03-22 06:34:35 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// OpenMP declarative directives.
27//===----------------------------------------------------------------------===//
28
Alexey Bataev4acb8592014-07-07 13:01:15 +000029static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000030 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
31 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
32 // TODO: add other combined directives in topological order.
33 const OpenMPDirectiveKind F[][3] = {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000034 {OMPD_unknown /*cancellation*/, OMPD_unknown /*point*/,
35 OMPD_cancellation_point},
Michael Wong65f367f2015-07-21 13:44:28 +000036 {OMPD_target, OMPD_unknown /*data*/, OMPD_target_data},
Alexey Bataev6d4ed052015-07-01 06:57:41 +000037 {OMPD_for, OMPD_simd, OMPD_for_simd},
38 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
39 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
40 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections}};
Alexey Bataev4acb8592014-07-07 13:01:15 +000041 auto Tok = P.getCurToken();
42 auto DKind =
43 Tok.isAnnotation()
44 ? OMPD_unknown
45 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Michael Wong65f367f2015-07-21 13:44:28 +000046
Alexey Bataev6d4ed052015-07-01 06:57:41 +000047 bool TokenMatched = false;
Alexander Musmanf82886e2014-09-18 05:12:34 +000048 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000049 if (!Tok.isAnnotation() && DKind == OMPD_unknown) {
50 TokenMatched =
51 (i == 0) &&
52 !P.getPreprocessor().getSpelling(Tok).compare("cancellation");
53 } else {
54 TokenMatched = DKind == F[i][0] && DKind != OMPD_unknown;
55 }
Michael Wong65f367f2015-07-21 13:44:28 +000056
Alexey Bataev6d4ed052015-07-01 06:57:41 +000057 if (TokenMatched) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000058 Tok = P.getPreprocessor().LookAhead(0);
Michael Wong65f367f2015-07-21 13:44:28 +000059 auto TokenIsAnnotation = Tok.isAnnotation();
Alexander Musmanf82886e2014-09-18 05:12:34 +000060 auto SDKind =
Michael Wong65f367f2015-07-21 13:44:28 +000061 TokenIsAnnotation
Alexander Musmanf82886e2014-09-18 05:12:34 +000062 ? OMPD_unknown
63 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Michael Wong65f367f2015-07-21 13:44:28 +000064
65 if (!TokenIsAnnotation && SDKind == OMPD_unknown) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000066 TokenMatched =
Daniel Jasper9aea8602015-07-21 16:18:51 +000067 ((i == 0) &&
68 !P.getPreprocessor().getSpelling(Tok).compare("point")) ||
69 ((i == 1) && !P.getPreprocessor().getSpelling(Tok).compare("data"));
Alexey Bataev6d4ed052015-07-01 06:57:41 +000070 } else {
71 TokenMatched = SDKind == F[i][1] && SDKind != OMPD_unknown;
72 }
Michael Wong65f367f2015-07-21 13:44:28 +000073
Alexey Bataev6d4ed052015-07-01 06:57:41 +000074 if (TokenMatched) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000075 P.ConsumeToken();
76 DKind = F[i][2];
77 }
Alexey Bataev4acb8592014-07-07 13:01:15 +000078 }
79 }
80 return DKind;
81}
82
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000083/// \brief Parsing of declarative OpenMP directives.
84///
85/// threadprivate-directive:
86/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataeva769e072013-03-22 06:34:35 +000087///
88Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
89 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000090 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +000091
92 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000093 SmallVector<Expr *, 5> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +000094 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000095
96 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +000097 case OMPD_threadprivate:
98 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000099 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000100 // The last seen token is annot_pragma_openmp_end - need to check for
101 // extra tokens.
102 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
103 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000104 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000105 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000106 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000107 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000108 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +0000109 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +0000110 }
111 break;
112 case OMPD_unknown:
113 Diag(Tok, diag::err_omp_unknown_directive);
114 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000115 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000116 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000117 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000118 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000119 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000120 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000121 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000122 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000123 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000124 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000125 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000126 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000127 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000128 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000129 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000130 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000131 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000132 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000133 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000134 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000135 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000136 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000137 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000138 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000139 case OMPD_target_data:
Alexey Bataeva769e072013-03-22 06:34:35 +0000140 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000141 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000142 break;
143 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000144 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +0000145 return DeclGroupPtrTy();
146}
147
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000148/// \brief Parsing of declarative or executable OpenMP directives.
149///
150/// threadprivate-directive:
151/// annot_pragma_openmp 'threadprivate' simple-variable-list
152/// annot_pragma_openmp_end
153///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000154/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000155/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000156/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
157/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000158/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000159/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
160/// 'taskgroup' | 'teams' {clause}
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000161/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000162///
Alexey Bataev68446b72014-07-18 07:47:19 +0000163StmtResult
164Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000165 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000166 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000167 SmallVector<Expr *, 5> Identifiers;
168 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000169 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000170 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000171 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000172 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000173 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000174 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000175 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000176 // Name of critical directive.
177 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000178 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000179 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000180 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000181
182 switch (DKind) {
183 case OMPD_threadprivate:
184 ConsumeToken();
185 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
186 // The last seen token is annot_pragma_openmp_end - need to check for
187 // extra tokens.
188 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
189 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000190 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000191 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000192 }
193 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000194 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000195 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
196 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000197 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000198 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000199 case OMPD_flush:
200 if (PP.LookAhead(0).is(tok::l_paren)) {
201 FlushHasClause = true;
202 // Push copy of the current token back to stream to properly parse
203 // pseudo-clause OMPFlushClause.
204 PP.EnterToken(Tok);
205 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000206 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000207 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000208 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000209 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000210 case OMPD_cancel:
Alexey Bataev68446b72014-07-18 07:47:19 +0000211 if (!StandAloneAllowed) {
212 Diag(Tok, diag::err_omp_immediate_directive)
213 << getOpenMPDirectiveName(DKind);
214 }
215 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000216 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000217 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000218 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000219 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000220 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000221 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000222 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000223 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000224 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000225 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000226 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000227 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000228 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000229 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000230 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000231 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000232 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000233 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000234 case OMPD_taskgroup:
235 case OMPD_target_data: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000236 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000237 // Parse directive name of the 'critical' directive if any.
238 if (DKind == OMPD_critical) {
239 BalancedDelimiterTracker T(*this, tok::l_paren,
240 tok::annot_pragma_openmp_end);
241 if (!T.consumeOpen()) {
242 if (Tok.isAnyIdentifier()) {
243 DirName =
244 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
245 ConsumeAnyToken();
246 } else {
247 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
248 }
249 T.consumeClose();
250 }
Alexey Bataev80909872015-07-02 11:25:17 +0000251 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000252 CancelRegion = ParseOpenMPDirectiveKind(*this);
253 if (Tok.isNot(tok::annot_pragma_openmp_end))
254 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000255 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000256
Alexey Bataevf29276e2014-06-18 04:14:57 +0000257 if (isOpenMPLoopDirective(DKind))
258 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
259 if (isOpenMPSimdDirective(DKind))
260 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
261 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000262 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000263
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000264 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000265 OpenMPClauseKind CKind =
266 Tok.isAnnotation()
267 ? OMPC_unknown
268 : FlushHasClause ? OMPC_flush
269 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000270 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000271 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000272 OMPClause *Clause =
273 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000274 FirstClauses[CKind].setInt(true);
275 if (Clause) {
276 FirstClauses[CKind].setPointer(Clause);
277 Clauses.push_back(Clause);
278 }
279
280 // Skip ',' if any.
281 if (Tok.is(tok::comma))
282 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000283 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000284 }
285 // End location of the directive.
286 EndLoc = Tok.getLocation();
287 // Consume final annot_pragma_openmp_end.
288 ConsumeToken();
289
290 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000291 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000292 // The body is a block scope like in Lambdas and Blocks.
293 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000294 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000295 Actions.ActOnStartOfCompoundStmt();
296 // Parse statement
297 AssociatedStmt = ParseStatement();
298 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000299 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000300 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000301 Directive = Actions.ActOnOpenMPExecutableDirective(
302 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
303 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000304
305 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000306 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000307 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000308 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000309 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000310 case OMPD_unknown:
311 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000312 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000313 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000314 }
315 return Directive;
316}
317
Alexey Bataeva769e072013-03-22 06:34:35 +0000318/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000319/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000320///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000321/// simple-variable-list:
322/// '(' id-expression {, id-expression} ')'
323///
324bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
325 SmallVectorImpl<Expr *> &VarList,
326 bool AllowScopeSpecifier) {
327 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000328 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000329 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000330 if (T.expectAndConsume(diag::err_expected_lparen_after,
331 getOpenMPDirectiveName(Kind)))
332 return true;
333 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000334 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000335
336 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000337 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000338 CXXScopeSpec SS;
339 SourceLocation TemplateKWLoc;
340 UnqualifiedId Name;
341 // Read var name.
342 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000343 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000344
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000345 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
346 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000347 IsCorrect = false;
348 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000349 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000350 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
351 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000352 IsCorrect = false;
353 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000354 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000355 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
356 Tok.isNot(tok::annot_pragma_openmp_end)) {
357 IsCorrect = false;
358 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000359 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000360 Diag(PrevTok.getLocation(), diag::err_expected)
361 << tok::identifier
362 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000363 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000364 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000365 ExprResult Res =
366 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000367 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000368 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000369 }
370 // Consume ','.
371 if (Tok.is(tok::comma)) {
372 ConsumeToken();
373 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000374 }
375
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000376 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000377 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000378 IsCorrect = false;
379 }
380
381 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000382 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000383
384 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000385}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000386
387/// \brief Parsing of OpenMP clauses.
388///
389/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000390/// if-clause | final-clause | num_threads-clause | safelen-clause |
391/// default-clause | private-clause | firstprivate-clause | shared-clause
392/// | linear-clause | aligned-clause | collapse-clause |
393/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000394/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000395/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +0000396/// update-clause | capture-clause | seq_cst-clause | device-clause |
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000397/// simdlen-clause | threads-clause | simd-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000398///
399OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
400 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000401 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000402 bool ErrorFound = false;
403 // Check if clause is allowed for the given directive.
404 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000405 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
406 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000407 ErrorFound = true;
408 }
409
410 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +0000411 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000412 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000413 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +0000414 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000415 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +0000416 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +0000417 case OMPC_device:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000418 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +0000419 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000420 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000421 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +0000422 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +0000423 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +0000424 // OpenMP [2.9.1, target data construct, Restrictions]
425 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000426 // OpenMP [2.11.1, task Construct, Restrictions]
427 // At most one if clause can appear on the directive.
428 // At most one final clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000429 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000430 Diag(Tok, diag::err_omp_more_one_clause)
431 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000432 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000433 }
434
Alexey Bataev10e775f2015-07-30 11:36:16 +0000435 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
436 Clause = ParseOpenMPClause(CKind);
437 else
438 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000439 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000440 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000441 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000442 // OpenMP [2.14.3.1, Restrictions]
443 // Only a single default clause may be specified on a parallel, task or
444 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000445 // OpenMP [2.5, parallel Construct, Restrictions]
446 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000447 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000448 Diag(Tok, diag::err_omp_more_one_clause)
449 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000450 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000451 }
452
453 Clause = ParseOpenMPSimpleClause(CKind);
454 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000455 case OMPC_schedule:
456 // OpenMP [2.7.1, Restrictions, p. 3]
457 // Only one schedule clause can appear on a loop directive.
458 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000459 Diag(Tok, diag::err_omp_more_one_clause)
460 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000461 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000462 }
463
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000464 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000465 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
466 break;
Alexey Bataev236070f2014-06-20 11:19:47 +0000467 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000468 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000469 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000470 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000471 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000472 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000473 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000474 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +0000475 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000476 case OMPC_simd:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000477 // OpenMP [2.7.1, Restrictions, p. 9]
478 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000479 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
480 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000481 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000482 Diag(Tok, diag::err_omp_more_one_clause)
483 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000484 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000485 }
486
487 Clause = ParseOpenMPClause(CKind);
488 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000489 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000490 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000491 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000492 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000493 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000494 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000495 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000496 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000497 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000498 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000499 case OMPC_depend:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000500 Clause = ParseOpenMPVarListClause(CKind);
501 break;
502 case OMPC_unknown:
503 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000504 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000505 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000506 break;
507 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000508 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
509 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000510 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000511 break;
512 }
Craig Topper161e4db2014-05-21 06:02:52 +0000513 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000514}
515
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000516/// \brief Parsing of OpenMP clauses with single expressions like 'final',
517/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams', 'thread_limit'
518/// or 'simdlen'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000519///
Alexey Bataev3778b602014-07-17 07:32:53 +0000520/// final-clause:
521/// 'final' '(' expression ')'
522///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000523/// num_threads-clause:
524/// 'num_threads' '(' expression ')'
525///
526/// safelen-clause:
527/// 'safelen' '(' expression ')'
528///
Alexey Bataev66b15b52015-08-21 11:14:16 +0000529/// simdlen-clause:
530/// 'simdlen' '(' expression ')'
531///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000532/// collapse-clause:
533/// 'collapse' '(' expression ')'
534///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000535OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
536 SourceLocation Loc = ConsumeToken();
537
538 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
539 if (T.expectAndConsume(diag::err_expected_lparen_after,
540 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000541 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000542
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000543 SourceLocation ELoc = Tok.getLocation();
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000544 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
545 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000546 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000547
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000548 // Parse ')'.
549 T.consumeClose();
550
551 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000552 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000553
Alexey Bataeva55ed262014-05-28 06:15:33 +0000554 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000555 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000556}
557
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000558/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000559///
560/// default-clause:
561/// 'default' '(' 'none' | 'shared' ')
562///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000563/// proc_bind-clause:
564/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
565///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000566OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
567 SourceLocation Loc = Tok.getLocation();
568 SourceLocation LOpen = ConsumeToken();
569 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000570 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000571 if (T.expectAndConsume(diag::err_expected_lparen_after,
572 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000573 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000574
Alexey Bataeva55ed262014-05-28 06:15:33 +0000575 unsigned Type = getOpenMPSimpleClauseType(
576 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000577 SourceLocation TypeLoc = Tok.getLocation();
578 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
579 Tok.isNot(tok::annot_pragma_openmp_end))
580 ConsumeAnyToken();
581
582 // Parse ')'.
583 T.consumeClose();
584
585 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
586 Tok.getLocation());
587}
588
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000589/// \brief Parsing of OpenMP clauses like 'ordered'.
590///
591/// ordered-clause:
592/// 'ordered'
593///
Alexey Bataev236070f2014-06-20 11:19:47 +0000594/// nowait-clause:
595/// 'nowait'
596///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000597/// untied-clause:
598/// 'untied'
599///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000600/// mergeable-clause:
601/// 'mergeable'
602///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000603/// read-clause:
604/// 'read'
605///
Alexey Bataev346265e2015-09-25 10:37:12 +0000606/// threads-clause:
607/// 'threads'
608///
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000609/// simd-clause:
610/// 'simd'
611///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000612OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
613 SourceLocation Loc = Tok.getLocation();
614 ConsumeAnyToken();
615
616 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
617}
618
619
Alexey Bataev56dafe82014-06-20 07:16:17 +0000620/// \brief Parsing of OpenMP clauses with single expressions and some additional
621/// argument like 'schedule' or 'dist_schedule'.
622///
623/// schedule-clause:
624/// 'schedule' '(' kind [',' expression ] ')'
625///
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000626/// if-clause:
627/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
628///
Alexey Bataev56dafe82014-06-20 07:16:17 +0000629OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
630 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000631 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000632 // Parse '('.
633 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
634 if (T.expectAndConsume(diag::err_expected_lparen_after,
635 getOpenMPClauseName(Kind)))
636 return nullptr;
637
638 ExprResult Val;
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000639 unsigned Arg;
640 SourceLocation KLoc;
641 if (Kind == OMPC_schedule) {
642 Arg = getOpenMPSimpleClauseType(
643 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
644 KLoc = Tok.getLocation();
645 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
646 Tok.isNot(tok::annot_pragma_openmp_end))
647 ConsumeAnyToken();
648 if ((Arg == OMPC_SCHEDULE_static || Arg == OMPC_SCHEDULE_dynamic ||
649 Arg == OMPC_SCHEDULE_guided) &&
650 Tok.is(tok::comma))
651 DelimLoc = ConsumeAnyToken();
652 } else {
653 assert(Kind == OMPC_if);
654 KLoc = Tok.getLocation();
655 Arg = ParseOpenMPDirectiveKind(*this);
656 if (Arg != OMPD_unknown) {
657 ConsumeToken();
658 if (Tok.is(tok::colon))
659 DelimLoc = ConsumeToken();
660 else
661 Diag(Tok, diag::warn_pragma_expected_colon)
662 << "directive name modifier";
663 }
664 }
Alexey Bataev56dafe82014-06-20 07:16:17 +0000665
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000666 bool NeedAnExpression =
667 (Kind == OMPC_schedule && DelimLoc.isValid()) || Kind == OMPC_if;
668 if (NeedAnExpression) {
669 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +0000670 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
671 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000672 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +0000673 }
674
675 // Parse ')'.
676 T.consumeClose();
677
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000678 if (NeedAnExpression && Val.isInvalid())
679 return nullptr;
680
Alexey Bataev56dafe82014-06-20 07:16:17 +0000681 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000682 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +0000683 T.getCloseLocation());
684}
685
Alexey Bataevc5e02582014-06-16 07:08:35 +0000686static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
687 UnqualifiedId &ReductionId) {
688 SourceLocation TemplateKWLoc;
689 if (ReductionIdScopeSpec.isEmpty()) {
690 auto OOK = OO_None;
691 switch (P.getCurToken().getKind()) {
692 case tok::plus:
693 OOK = OO_Plus;
694 break;
695 case tok::minus:
696 OOK = OO_Minus;
697 break;
698 case tok::star:
699 OOK = OO_Star;
700 break;
701 case tok::amp:
702 OOK = OO_Amp;
703 break;
704 case tok::pipe:
705 OOK = OO_Pipe;
706 break;
707 case tok::caret:
708 OOK = OO_Caret;
709 break;
710 case tok::ampamp:
711 OOK = OO_AmpAmp;
712 break;
713 case tok::pipepipe:
714 OOK = OO_PipePipe;
715 break;
716 default:
717 break;
718 }
719 if (OOK != OO_None) {
720 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000721 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000722 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
723 return false;
724 }
725 }
726 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
727 /*AllowDestructorName*/ false,
728 /*AllowConstructorName*/ false, ParsedType(),
729 TemplateKWLoc, ReductionId);
730}
731
Alexander Musman1bb328c2014-06-04 13:06:39 +0000732/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000733/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000734///
735/// private-clause:
736/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000737/// firstprivate-clause:
738/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000739/// lastprivate-clause:
740/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000741/// shared-clause:
742/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000743/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +0000744/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000745/// aligned-clause:
746/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000747/// reduction-clause:
748/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000749/// copyprivate-clause:
750/// 'copyprivate' '(' list ')'
751/// flush-clause:
752/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000753/// depend-clause:
754/// 'depend' '(' in | out | inout : list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000755///
Alexey Bataev182227b2015-08-20 10:54:39 +0000756/// For 'linear' clause linear-list may have the following forms:
757/// list
758/// modifier(list)
759/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000760OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
761 SourceLocation Loc = Tok.getLocation();
762 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000763 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000764 // Optional scope specifier and unqualified id for reduction identifier.
765 CXXScopeSpec ReductionIdScopeSpec;
766 UnqualifiedId ReductionId;
767 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000768 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
Alexey Bataev182227b2015-08-20 10:54:39 +0000769 // OpenMP 4.1 [2.15.3.7, linear Clause]
770 // If no modifier is specified it is assumed to be val.
771 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
772 SourceLocation DepLinLoc;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000773
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000774 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000775 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000776 if (T.expectAndConsume(diag::err_expected_lparen_after,
777 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000778 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000779
Alexey Bataev182227b2015-08-20 10:54:39 +0000780 bool NeedRParenForLinear = false;
781 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
782 tok::annot_pragma_openmp_end);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000783 // Handle reduction-identifier for reduction clause.
784 if (Kind == OMPC_reduction) {
785 ColonProtectionRAIIObject ColonRAII(*this);
786 if (getLangOpts().CPlusPlus) {
787 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
788 }
789 InvalidReductionId =
790 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
791 if (InvalidReductionId) {
792 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
793 StopBeforeMatch);
794 }
795 if (Tok.is(tok::colon)) {
796 ColonLoc = ConsumeToken();
797 } else {
798 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
799 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000800 } else if (Kind == OMPC_depend) {
801 // Handle dependency type for depend clause.
802 ColonProtectionRAIIObject ColonRAII(*this);
803 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
804 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
Alexey Bataev182227b2015-08-20 10:54:39 +0000805 DepLinLoc = Tok.getLocation();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000806
807 if (DepKind == OMPC_DEPEND_unknown) {
808 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
809 StopBeforeMatch);
810 } else {
811 ConsumeToken();
812 }
813 if (Tok.is(tok::colon)) {
814 ColonLoc = ConsumeToken();
815 } else {
816 Diag(Tok, diag::warn_pragma_expected_colon) << "dependency type";
817 }
Alexey Bataev182227b2015-08-20 10:54:39 +0000818 } else if (Kind == OMPC_linear) {
819 // Try to parse modifier if any.
820 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev182227b2015-08-20 10:54:39 +0000821 LinearModifier = static_cast<OpenMPLinearClauseKind>(
Alexey Bataev1185e192015-08-20 12:15:57 +0000822 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
Alexey Bataev182227b2015-08-20 10:54:39 +0000823 DepLinLoc = ConsumeToken();
824 LinearT.consumeOpen();
825 NeedRParenForLinear = true;
826 }
Alexey Bataevc5e02582014-06-16 07:08:35 +0000827 }
828
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000829 SmallVector<Expr *, 5> Vars;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000830 bool IsComma = ((Kind != OMPC_reduction) && (Kind != OMPC_depend)) ||
831 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
832 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000833 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000834 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000835 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000836 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000837 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +0000838 ExprResult VarExpr =
839 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000840 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000841 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000842 } else {
843 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000844 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000845 }
846 // Skip ',' if any
847 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000848 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000849 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000850 else if (Tok.isNot(tok::r_paren) &&
851 Tok.isNot(tok::annot_pragma_openmp_end) &&
852 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +0000853 Diag(Tok, diag::err_omp_expected_punc)
854 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
855 : getOpenMPClauseName(Kind))
856 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +0000857 }
858
Alexey Bataev182227b2015-08-20 10:54:39 +0000859 // Parse ')' for linear clause with modifier.
860 if (NeedRParenForLinear)
861 LinearT.consumeClose();
862
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000863 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000864 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000865 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
866 if (MustHaveTail) {
867 ColonLoc = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000868 SourceLocation ELoc = ConsumeToken();
869 ExprResult Tail = ParseAssignmentExpression();
870 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
Alexander Musman8dba6642014-04-22 13:09:42 +0000871 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000872 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000873 else
874 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
875 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000876 }
877
878 // Parse ')'.
879 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000880 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
881 (Kind != OMPC_depend && Vars.empty()) || (MustHaveTail && !TailExpr) ||
882 InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000883 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000884
Alexey Bataevc5e02582014-06-16 07:08:35 +0000885 return Actions.ActOnOpenMPVarListClause(
886 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
887 ReductionIdScopeSpec,
888 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000889 : DeclarationNameInfo(),
Alexey Bataev182227b2015-08-20 10:54:39 +0000890 DepKind, LinearModifier, DepLinLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000891}
892