blob: 5b8670e928b156b8680a1613b804bc4264d78882 [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexey Bataeva769e072013-03-22 06:34:35 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009/// This file implements parsing of all OpenMP directives and clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000010///
11//===----------------------------------------------------------------------===//
12
Alexey Bataev9959db52014-05-06 10:08:46 +000013#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000014#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000015#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000016#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000017#include "clang/Parse/RAIIObjectsForParser.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000018#include "clang/Sema/Scope.h"
19#include "llvm/ADT/PointerIntPair.h"
Michael Wong65f367f2015-07-21 13:44:28 +000020
Alexey Bataeva769e072013-03-22 06:34:35 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// OpenMP declarative directives.
25//===----------------------------------------------------------------------===//
26
Dmitry Polukhin82478332016-02-13 06:53:38 +000027namespace {
28enum OpenMPDirectiveKindEx {
29 OMPD_cancellation = OMPD_unknown + 1,
30 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000031 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000032 OMPD_end,
33 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000034 OMPD_enter,
35 OMPD_exit,
36 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000037 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000038 OMPD_target_enter,
Samuel Antao686c70c2016-05-26 17:30:50 +000039 OMPD_target_exit,
40 OMPD_update,
Kelvin Li579e41c2016-11-30 23:51:03 +000041 OMPD_distribute_parallel,
Kelvin Li80e8f562016-12-29 22:16:30 +000042 OMPD_teams_distribute_parallel,
Michael Kruse251e1482019-02-01 20:25:04 +000043 OMPD_target_teams_distribute_parallel,
44 OMPD_mapper,
Dmitry Polukhin82478332016-02-13 06:53:38 +000045};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000046
47class ThreadprivateListParserHelper final {
48 SmallVector<Expr *, 4> Identifiers;
49 Parser *P;
50
51public:
52 ThreadprivateListParserHelper(Parser *P) : P(P) {}
53 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
54 ExprResult Res =
55 P->getActions().ActOnOpenMPIdExpression(P->getCurScope(), SS, NameInfo);
56 if (Res.isUsable())
57 Identifiers.push_back(Res.get());
58 }
59 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
60};
Dmitry Polukhin82478332016-02-13 06:53:38 +000061} // namespace
62
63// Map token string to extended OMP token kind that are
64// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
65static unsigned getOpenMPDirectiveKindEx(StringRef S) {
66 auto DKind = getOpenMPDirectiveKind(S);
67 if (DKind != OMPD_unknown)
68 return DKind;
69
70 return llvm::StringSwitch<unsigned>(S)
71 .Case("cancellation", OMPD_cancellation)
72 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000073 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000074 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000075 .Case("enter", OMPD_enter)
76 .Case("exit", OMPD_exit)
77 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000078 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000079 .Case("update", OMPD_update)
Michael Kruse251e1482019-02-01 20:25:04 +000080 .Case("mapper", OMPD_mapper)
Dmitry Polukhin82478332016-02-13 06:53:38 +000081 .Default(OMPD_unknown);
82}
83
Alexey Bataev61908f652018-04-23 19:53:05 +000084static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000085 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
86 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
87 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000088 static const unsigned F[][3] = {
Alexey Bataev61908f652018-04-23 19:53:05 +000089 {OMPD_cancellation, OMPD_point, OMPD_cancellation_point},
90 {OMPD_declare, OMPD_reduction, OMPD_declare_reduction},
Michael Kruse251e1482019-02-01 20:25:04 +000091 {OMPD_declare, OMPD_mapper, OMPD_declare_mapper},
Alexey Bataev61908f652018-04-23 19:53:05 +000092 {OMPD_declare, OMPD_simd, OMPD_declare_simd},
93 {OMPD_declare, OMPD_target, OMPD_declare_target},
94 {OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel},
95 {OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for},
96 {OMPD_distribute_parallel_for, OMPD_simd,
97 OMPD_distribute_parallel_for_simd},
98 {OMPD_distribute, OMPD_simd, OMPD_distribute_simd},
99 {OMPD_end, OMPD_declare, OMPD_end_declare},
100 {OMPD_end_declare, OMPD_target, OMPD_end_declare_target},
101 {OMPD_target, OMPD_data, OMPD_target_data},
102 {OMPD_target, OMPD_enter, OMPD_target_enter},
103 {OMPD_target, OMPD_exit, OMPD_target_exit},
104 {OMPD_target, OMPD_update, OMPD_target_update},
105 {OMPD_target_enter, OMPD_data, OMPD_target_enter_data},
106 {OMPD_target_exit, OMPD_data, OMPD_target_exit_data},
107 {OMPD_for, OMPD_simd, OMPD_for_simd},
108 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
109 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
110 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
111 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd},
112 {OMPD_target, OMPD_parallel, OMPD_target_parallel},
113 {OMPD_target, OMPD_simd, OMPD_target_simd},
114 {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for},
115 {OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd},
116 {OMPD_teams, OMPD_distribute, OMPD_teams_distribute},
117 {OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd},
118 {OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel},
119 {OMPD_teams_distribute_parallel, OMPD_for,
120 OMPD_teams_distribute_parallel_for},
121 {OMPD_teams_distribute_parallel_for, OMPD_simd,
122 OMPD_teams_distribute_parallel_for_simd},
123 {OMPD_target, OMPD_teams, OMPD_target_teams},
124 {OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute},
125 {OMPD_target_teams_distribute, OMPD_parallel,
126 OMPD_target_teams_distribute_parallel},
127 {OMPD_target_teams_distribute, OMPD_simd,
128 OMPD_target_teams_distribute_simd},
129 {OMPD_target_teams_distribute_parallel, OMPD_for,
130 OMPD_target_teams_distribute_parallel_for},
131 {OMPD_target_teams_distribute_parallel_for, OMPD_simd,
132 OMPD_target_teams_distribute_parallel_for_simd}};
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000133 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev61908f652018-04-23 19:53:05 +0000134 Token Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000135 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000136 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000137 ? static_cast<unsigned>(OMPD_unknown)
138 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
139 if (DKind == OMPD_unknown)
140 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000141
Alexey Bataev61908f652018-04-23 19:53:05 +0000142 for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) {
143 if (DKind != F[I][0])
Dmitry Polukhin82478332016-02-13 06:53:38 +0000144 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000145
Dmitry Polukhin82478332016-02-13 06:53:38 +0000146 Tok = P.getPreprocessor().LookAhead(0);
147 unsigned SDKind =
148 Tok.isAnnotation()
149 ? static_cast<unsigned>(OMPD_unknown)
150 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
151 if (SDKind == OMPD_unknown)
152 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000153
Alexey Bataev61908f652018-04-23 19:53:05 +0000154 if (SDKind == F[I][1]) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000155 P.ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000156 DKind = F[I][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000157 }
158 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000159 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
160 : OMPD_unknown;
161}
162
163static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000164 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000165 Sema &Actions = P.getActions();
166 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000167 // Allow to use 'operator' keyword for C++ operators
168 bool WithOperator = false;
169 if (Tok.is(tok::kw_operator)) {
170 P.ConsumeToken();
171 Tok = P.getCurToken();
172 WithOperator = true;
173 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000174 switch (Tok.getKind()) {
175 case tok::plus: // '+'
176 OOK = OO_Plus;
177 break;
178 case tok::minus: // '-'
179 OOK = OO_Minus;
180 break;
181 case tok::star: // '*'
182 OOK = OO_Star;
183 break;
184 case tok::amp: // '&'
185 OOK = OO_Amp;
186 break;
187 case tok::pipe: // '|'
188 OOK = OO_Pipe;
189 break;
190 case tok::caret: // '^'
191 OOK = OO_Caret;
192 break;
193 case tok::ampamp: // '&&'
194 OOK = OO_AmpAmp;
195 break;
196 case tok::pipepipe: // '||'
197 OOK = OO_PipePipe;
198 break;
199 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000200 if (!WithOperator)
201 break;
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000202 LLVM_FALLTHROUGH;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000203 default:
204 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
205 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
206 Parser::StopBeforeMatch);
207 return DeclarationName();
208 }
209 P.ConsumeToken();
210 auto &DeclNames = Actions.getASTContext().DeclarationNames;
211 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
212 : DeclNames.getCXXOperatorName(OOK);
213}
214
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000215/// Parse 'omp declare reduction' construct.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000216///
217/// declare-reduction-directive:
218/// annot_pragma_openmp 'declare' 'reduction'
219/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
220/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
221/// annot_pragma_openmp_end
222/// <reduction_id> is either a base language identifier or one of the following
223/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
224///
225Parser::DeclGroupPtrTy
226Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
227 // Parse '('.
228 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
229 if (T.expectAndConsume(diag::err_expected_lparen_after,
230 getOpenMPDirectiveName(OMPD_declare_reduction))) {
231 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
232 return DeclGroupPtrTy();
233 }
234
235 DeclarationName Name = parseOpenMPReductionId(*this);
236 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
237 return DeclGroupPtrTy();
238
239 // Consume ':'.
240 bool IsCorrect = !ExpectAndConsume(tok::colon);
241
242 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
243 return DeclGroupPtrTy();
244
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000245 IsCorrect = IsCorrect && !Name.isEmpty();
246
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000247 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
248 Diag(Tok.getLocation(), diag::err_expected_type);
249 IsCorrect = false;
250 }
251
252 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
253 return DeclGroupPtrTy();
254
255 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
256 // Parse list of types until ':' token.
257 do {
258 ColonProtectionRAIIObject ColonRAII(*this);
259 SourceRange Range;
Faisal Vali421b2d12017-12-29 05:41:00 +0000260 TypeResult TR =
261 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000262 if (TR.isUsable()) {
Alexey Bataev61908f652018-04-23 19:53:05 +0000263 QualType ReductionType =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000264 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
265 if (!ReductionType.isNull()) {
266 ReductionTypes.push_back(
267 std::make_pair(ReductionType, Range.getBegin()));
268 }
269 } else {
270 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
271 StopBeforeMatch);
272 }
273
274 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
275 break;
276
277 // Consume ','.
278 if (ExpectAndConsume(tok::comma)) {
279 IsCorrect = false;
280 if (Tok.is(tok::annot_pragma_openmp_end)) {
281 Diag(Tok.getLocation(), diag::err_expected_type);
282 return DeclGroupPtrTy();
283 }
284 }
285 } while (Tok.isNot(tok::annot_pragma_openmp_end));
286
287 if (ReductionTypes.empty()) {
288 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
289 return DeclGroupPtrTy();
290 }
291
292 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
293 return DeclGroupPtrTy();
294
295 // Consume ':'.
296 if (ExpectAndConsume(tok::colon))
297 IsCorrect = false;
298
299 if (Tok.is(tok::annot_pragma_openmp_end)) {
300 Diag(Tok.getLocation(), diag::err_expected_expression);
301 return DeclGroupPtrTy();
302 }
303
304 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
305 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
306
307 // Parse <combiner> expression and then parse initializer if any for each
308 // correct type.
309 unsigned I = 0, E = ReductionTypes.size();
Alexey Bataev61908f652018-04-23 19:53:05 +0000310 for (Decl *D : DRD.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000311 TentativeParsingAction TPA(*this);
312 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000313 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000314 Scope::OpenMPDirectiveScope);
315 // Parse <combiner> expression.
316 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
317 ExprResult CombinerResult =
318 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000319 D->getLocation(), /*DiscardedValue*/ false);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000320 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
321
322 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
323 Tok.isNot(tok::annot_pragma_openmp_end)) {
324 TPA.Commit();
325 IsCorrect = false;
326 break;
327 }
328 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
329 ExprResult InitializerResult;
330 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
331 // Parse <initializer> expression.
332 if (Tok.is(tok::identifier) &&
Alexey Bataev61908f652018-04-23 19:53:05 +0000333 Tok.getIdentifierInfo()->isStr("initializer")) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000334 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000335 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000336 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
337 TPA.Commit();
338 IsCorrect = false;
339 break;
340 }
341 // Parse '('.
342 BalancedDelimiterTracker T(*this, tok::l_paren,
343 tok::annot_pragma_openmp_end);
344 IsCorrect =
345 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
346 IsCorrect;
347 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
348 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000349 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000350 Scope::OpenMPDirectiveScope);
351 // Parse expression.
Alexey Bataev070f43a2017-09-06 14:49:58 +0000352 VarDecl *OmpPrivParm =
353 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
354 D);
355 // Check if initializer is omp_priv <init_expr> or something else.
356 if (Tok.is(tok::identifier) &&
357 Tok.getIdentifierInfo()->isStr("omp_priv")) {
Alexey Bataeve6aa4692018-09-13 16:54:05 +0000358 if (Actions.getLangOpts().CPlusPlus) {
359 InitializerResult = Actions.ActOnFinishFullExpr(
360 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000361 /*DiscardedValue*/ false);
Alexey Bataeve6aa4692018-09-13 16:54:05 +0000362 } else {
363 ConsumeToken();
364 ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
365 }
Alexey Bataev070f43a2017-09-06 14:49:58 +0000366 } else {
367 InitializerResult = Actions.ActOnFinishFullExpr(
368 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000369 /*DiscardedValue*/ false);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000370 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000371 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
Alexey Bataev070f43a2017-09-06 14:49:58 +0000372 D, InitializerResult.get(), OmpPrivParm);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000373 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
374 Tok.isNot(tok::annot_pragma_openmp_end)) {
375 TPA.Commit();
376 IsCorrect = false;
377 break;
378 }
379 IsCorrect =
380 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
381 }
382 }
383
384 ++I;
385 // Revert parsing if not the last type, otherwise accept it, we're done with
386 // parsing.
387 if (I != E)
388 TPA.Revert();
389 else
390 TPA.Commit();
391 }
392 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
393 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000394}
395
Alexey Bataev070f43a2017-09-06 14:49:58 +0000396void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
397 // Parse declarator '=' initializer.
398 // If a '==' or '+=' is found, suggest a fixit to '='.
399 if (isTokenEqualOrEqualTypo()) {
400 ConsumeToken();
401
402 if (Tok.is(tok::code_completion)) {
403 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
404 Actions.FinalizeDeclaration(OmpPrivParm);
405 cutOffParsing();
406 return;
407 }
408
409 ExprResult Init(ParseInitializer());
410
411 if (Init.isInvalid()) {
412 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
413 Actions.ActOnInitializerError(OmpPrivParm);
414 } else {
415 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
416 /*DirectInit=*/false);
417 }
418 } else if (Tok.is(tok::l_paren)) {
419 // Parse C++ direct initializer: '(' expression-list ')'
420 BalancedDelimiterTracker T(*this, tok::l_paren);
421 T.consumeOpen();
422
423 ExprVector Exprs;
424 CommaLocsTy CommaLocs;
425
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000426 SourceLocation LParLoc = T.getOpenLocation();
427 if (ParseExpressionList(
428 Exprs, CommaLocs, [this, OmpPrivParm, LParLoc, &Exprs] {
Ilya Biryukov832c4af2018-09-07 14:04:39 +0000429 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000430 getCurScope(),
431 OmpPrivParm->getType()->getCanonicalTypeInternal(),
432 OmpPrivParm->getLocation(), Exprs, LParLoc);
Kadir Cetinkayaa32d2532018-09-10 13:46:28 +0000433 CalledSignatureHelp = true;
Ilya Biryukov832c4af2018-09-07 14:04:39 +0000434 Actions.CodeCompleteExpression(getCurScope(), PreferredType);
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000435 })) {
Kadir Cetinkayaa32d2532018-09-10 13:46:28 +0000436 if (PP.isCodeCompletionReached() && !CalledSignatureHelp) {
437 Actions.ProduceConstructorSignatureHelp(
438 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
439 OmpPrivParm->getLocation(), Exprs, LParLoc);
440 CalledSignatureHelp = true;
441 }
Alexey Bataev070f43a2017-09-06 14:49:58 +0000442 Actions.ActOnInitializerError(OmpPrivParm);
443 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
444 } else {
445 // Match the ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000446 SourceLocation RLoc = Tok.getLocation();
447 if (!T.consumeClose())
448 RLoc = T.getCloseLocation();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000449
450 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
451 "Unexpected number of commas!");
452
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000453 ExprResult Initializer =
454 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000455 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
456 /*DirectInit=*/true);
457 }
458 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
459 // Parse C++0x braced-init-list.
460 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
461
462 ExprResult Init(ParseBraceInitializer());
463
464 if (Init.isInvalid()) {
465 Actions.ActOnInitializerError(OmpPrivParm);
466 } else {
467 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
468 /*DirectInit=*/true);
469 }
470 } else {
471 Actions.ActOnUninitializedDecl(OmpPrivParm);
472 }
473}
474
Michael Kruse251e1482019-02-01 20:25:04 +0000475/// Parses 'omp declare mapper' directive.
476///
477/// declare-mapper-directive:
478/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifier> ':']
479/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
480/// annot_pragma_openmp_end
481/// <mapper-identifier> and <var> are base language identifiers.
482///
483Parser::DeclGroupPtrTy
484Parser::ParseOpenMPDeclareMapperDirective(AccessSpecifier AS) {
485 bool IsCorrect = true;
486 // Parse '('
487 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
488 if (T.expectAndConsume(diag::err_expected_lparen_after,
489 getOpenMPDirectiveName(OMPD_declare_mapper))) {
490 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
491 return DeclGroupPtrTy();
492 }
493
494 // Parse <mapper-identifier>
495 auto &DeclNames = Actions.getASTContext().DeclarationNames;
496 DeclarationName MapperId;
497 if (PP.LookAhead(0).is(tok::colon)) {
498 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
499 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
500 IsCorrect = false;
501 } else {
502 MapperId = DeclNames.getIdentifier(Tok.getIdentifierInfo());
503 }
504 ConsumeToken();
505 // Consume ':'.
506 ExpectAndConsume(tok::colon);
507 } else {
508 // If no mapper identifier is provided, its name is "default" by default
509 MapperId =
510 DeclNames.getIdentifier(&Actions.getASTContext().Idents.get("default"));
511 }
512
513 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
514 return DeclGroupPtrTy();
515
516 // Parse <type> <var>
517 DeclarationName VName;
518 QualType MapperType;
519 SourceRange Range;
520 TypeResult ParsedType = parseOpenMPDeclareMapperVarDecl(Range, VName, AS);
521 if (ParsedType.isUsable())
522 MapperType =
523 Actions.ActOnOpenMPDeclareMapperType(Range.getBegin(), ParsedType);
524 if (MapperType.isNull())
525 IsCorrect = false;
526 if (!IsCorrect) {
527 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
528 return DeclGroupPtrTy();
529 }
530
531 // Consume ')'.
532 IsCorrect &= !T.consumeClose();
533 if (!IsCorrect) {
534 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
535 return DeclGroupPtrTy();
536 }
537
538 // Enter scope.
539 OMPDeclareMapperDecl *DMD = Actions.ActOnOpenMPDeclareMapperDirectiveStart(
540 getCurScope(), Actions.getCurLexicalContext(), MapperId, MapperType,
541 Range.getBegin(), VName, AS);
542 DeclarationNameInfo DirName;
543 SourceLocation Loc = Tok.getLocation();
544 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
545 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
546 ParseScope OMPDirectiveScope(this, ScopeFlags);
547 Actions.StartOpenMPDSABlock(OMPD_declare_mapper, DirName, getCurScope(), Loc);
548
549 // Add the mapper variable declaration.
550 Actions.ActOnOpenMPDeclareMapperDirectiveVarDecl(
551 DMD, getCurScope(), MapperType, Range.getBegin(), VName);
552
553 // Parse map clauses.
554 SmallVector<OMPClause *, 6> Clauses;
555 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
556 OpenMPClauseKind CKind = Tok.isAnnotation()
557 ? OMPC_unknown
558 : getOpenMPClauseKind(PP.getSpelling(Tok));
559 Actions.StartOpenMPClause(CKind);
560 OMPClause *Clause =
561 ParseOpenMPClause(OMPD_declare_mapper, CKind, Clauses.size() == 0);
562 if (Clause)
563 Clauses.push_back(Clause);
564 else
565 IsCorrect = false;
566 // Skip ',' if any.
567 if (Tok.is(tok::comma))
568 ConsumeToken();
569 Actions.EndOpenMPClause();
570 }
571 if (Clauses.empty()) {
572 Diag(Tok, diag::err_omp_expected_clause)
573 << getOpenMPDirectiveName(OMPD_declare_mapper);
574 IsCorrect = false;
575 }
576
577 // Exit scope.
578 Actions.EndOpenMPDSABlock(nullptr);
579 OMPDirectiveScope.Exit();
580
581 DeclGroupPtrTy DGP =
582 Actions.ActOnOpenMPDeclareMapperDirectiveEnd(DMD, getCurScope(), Clauses);
583 if (!IsCorrect)
584 return DeclGroupPtrTy();
585 return DGP;
586}
587
588TypeResult Parser::parseOpenMPDeclareMapperVarDecl(SourceRange &Range,
589 DeclarationName &Name,
590 AccessSpecifier AS) {
591 // Parse the common declaration-specifiers piece.
592 Parser::DeclSpecContext DSC = Parser::DeclSpecContext::DSC_type_specifier;
593 DeclSpec DS(AttrFactory);
594 ParseSpecifierQualifierList(DS, AS, DSC);
595
596 // Parse the declarator.
597 DeclaratorContext Context = DeclaratorContext::PrototypeContext;
598 Declarator DeclaratorInfo(DS, Context);
599 ParseDeclarator(DeclaratorInfo);
600 Range = DeclaratorInfo.getSourceRange();
601 if (DeclaratorInfo.getIdentifier() == nullptr) {
602 Diag(Tok.getLocation(), diag::err_omp_mapper_expected_declarator);
603 return true;
604 }
605 Name = Actions.GetNameForDeclarator(DeclaratorInfo).getName();
606
607 return Actions.ActOnOpenMPDeclareMapperVarDecl(getCurScope(), DeclaratorInfo);
608}
609
Alexey Bataev2af33e32016-04-07 12:45:37 +0000610namespace {
611/// RAII that recreates function context for correct parsing of clauses of
612/// 'declare simd' construct.
613/// OpenMP, 2.8.2 declare simd Construct
614/// The expressions appearing in the clauses of this directive are evaluated in
615/// the scope of the arguments of the function declaration or definition.
616class FNContextRAII final {
617 Parser &P;
618 Sema::CXXThisScopeRAII *ThisScope;
619 Parser::ParseScope *TempScope;
620 Parser::ParseScope *FnScope;
621 bool HasTemplateScope = false;
622 bool HasFunScope = false;
623 FNContextRAII() = delete;
624 FNContextRAII(const FNContextRAII &) = delete;
625 FNContextRAII &operator=(const FNContextRAII &) = delete;
626
627public:
628 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
629 Decl *D = *Ptr.get().begin();
630 NamedDecl *ND = dyn_cast<NamedDecl>(D);
631 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
632 Sema &Actions = P.getActions();
633
634 // Allow 'this' within late-parsed attributes.
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000635 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, Qualifiers(),
Alexey Bataev2af33e32016-04-07 12:45:37 +0000636 ND && ND->isCXXInstanceMember());
637
638 // If the Decl is templatized, add template parameters to scope.
639 HasTemplateScope = D->isTemplateDecl();
640 TempScope =
641 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
642 if (HasTemplateScope)
643 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
644
645 // If the Decl is on a function, add function parameters to the scope.
646 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000647 FnScope = new Parser::ParseScope(
648 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
649 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000650 if (HasFunScope)
651 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
652 }
653 ~FNContextRAII() {
654 if (HasFunScope) {
655 P.getActions().ActOnExitFunctionContext();
656 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
657 }
658 if (HasTemplateScope)
659 TempScope->Exit();
660 delete FnScope;
661 delete TempScope;
662 delete ThisScope;
663 }
664};
665} // namespace
666
Alexey Bataevd93d3762016-04-12 09:35:56 +0000667/// Parses clauses for 'declare simd' directive.
668/// clause:
669/// 'inbranch' | 'notinbranch'
670/// 'simdlen' '(' <expr> ')'
671/// { 'uniform' '(' <argument_list> ')' }
672/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000673/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
674static bool parseDeclareSimdClauses(
675 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
676 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
677 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
678 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000679 SourceRange BSRange;
680 const Token &Tok = P.getCurToken();
681 bool IsError = false;
682 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
683 if (Tok.isNot(tok::identifier))
684 break;
685 OMPDeclareSimdDeclAttr::BranchStateTy Out;
686 IdentifierInfo *II = Tok.getIdentifierInfo();
687 StringRef ClauseName = II->getName();
688 // Parse 'inranch|notinbranch' clauses.
689 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
690 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
691 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
692 << ClauseName
693 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
694 IsError = true;
695 }
696 BS = Out;
697 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
698 P.ConsumeToken();
699 } else if (ClauseName.equals("simdlen")) {
700 if (SimdLen.isUsable()) {
701 P.Diag(Tok, diag::err_omp_more_one_clause)
702 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
703 IsError = true;
704 }
705 P.ConsumeToken();
706 SourceLocation RLoc;
707 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
708 if (SimdLen.isInvalid())
709 IsError = true;
710 } else {
711 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000712 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
713 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000714 Parser::OpenMPVarListDataTy Data;
Alexey Bataev61908f652018-04-23 19:53:05 +0000715 SmallVectorImpl<Expr *> *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000716 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000717 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000718 else if (CKind == OMPC_linear)
719 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000720
721 P.ConsumeToken();
722 if (P.ParseOpenMPVarList(OMPD_declare_simd,
723 getOpenMPClauseKind(ClauseName), *Vars, Data))
724 IsError = true;
Alexey Bataev61908f652018-04-23 19:53:05 +0000725 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000726 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataev61908f652018-04-23 19:53:05 +0000727 } else if (CKind == OMPC_linear) {
Alexey Bataevecba70f2016-04-12 11:02:11 +0000728 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
729 Data.DepLinMapLoc))
730 Data.LinKind = OMPC_LINEAR_val;
731 LinModifiers.append(Linears.size() - LinModifiers.size(),
732 Data.LinKind);
733 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
734 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000735 } else
736 // TODO: add parsing of other clauses.
737 break;
738 }
739 // Skip ',' if any.
740 if (Tok.is(tok::comma))
741 P.ConsumeToken();
742 }
743 return IsError;
744}
745
Alexey Bataev2af33e32016-04-07 12:45:37 +0000746/// Parse clauses for '#pragma omp declare simd'.
747Parser::DeclGroupPtrTy
748Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
749 CachedTokens &Toks, SourceLocation Loc) {
750 PP.EnterToken(Tok);
751 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
752 // Consume the previously pushed token.
753 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
754
755 FNContextRAII FnContext(*this, Ptr);
756 OMPDeclareSimdDeclAttr::BranchStateTy BS =
757 OMPDeclareSimdDeclAttr::BS_Undefined;
758 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000759 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000760 SmallVector<Expr *, 4> Aligneds;
761 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000762 SmallVector<Expr *, 4> Linears;
763 SmallVector<unsigned, 4> LinModifiers;
764 SmallVector<Expr *, 4> Steps;
765 bool IsError =
766 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
767 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000768 // Need to check for extra tokens.
769 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
770 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
771 << getOpenMPDirectiveName(OMPD_declare_simd);
772 while (Tok.isNot(tok::annot_pragma_openmp_end))
773 ConsumeAnyToken();
774 }
775 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000776 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000777 if (IsError)
778 return Ptr;
779 return Actions.ActOnOpenMPDeclareSimdDirective(
780 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
781 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataev20dfd772016-04-04 10:12:15 +0000782}
783
Kelvin Lie0502752018-11-21 20:15:57 +0000784Parser::DeclGroupPtrTy Parser::ParseOMPDeclareTargetClauses() {
785 // OpenMP 4.5 syntax with list of entities.
786 Sema::NamedDeclSetType SameDirectiveDecls;
787 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
788 OMPDeclareTargetDeclAttr::MapTypeTy MT = OMPDeclareTargetDeclAttr::MT_To;
789 if (Tok.is(tok::identifier)) {
790 IdentifierInfo *II = Tok.getIdentifierInfo();
791 StringRef ClauseName = II->getName();
792 // Parse 'to|link' clauses.
793 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName, MT)) {
794 Diag(Tok, diag::err_omp_declare_target_unexpected_clause) << ClauseName;
795 break;
796 }
797 ConsumeToken();
798 }
799 auto &&Callback = [this, MT, &SameDirectiveDecls](
800 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
801 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
802 SameDirectiveDecls);
803 };
804 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
805 /*AllowScopeSpecifier=*/true))
806 break;
807
808 // Consume optional ','.
809 if (Tok.is(tok::comma))
810 ConsumeToken();
811 }
812 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
813 ConsumeAnyToken();
814 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
815 SameDirectiveDecls.end());
816 if (Decls.empty())
817 return DeclGroupPtrTy();
818 return Actions.BuildDeclaratorGroup(Decls);
819}
820
821void Parser::ParseOMPEndDeclareTargetDirective(OpenMPDirectiveKind DKind,
822 SourceLocation DTLoc) {
823 if (DKind != OMPD_end_declare_target) {
824 Diag(Tok, diag::err_expected_end_declare_target);
825 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
826 return;
827 }
828 ConsumeAnyToken();
829 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
830 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
831 << getOpenMPDirectiveName(OMPD_end_declare_target);
832 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
833 }
834 // Skip the last annot_pragma_openmp_end.
835 ConsumeAnyToken();
836}
837
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000838/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000839///
840/// threadprivate-directive:
841/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000842/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000843///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000844/// declare-reduction-directive:
845/// annot_pragma_openmp 'declare' 'reduction' [...]
846/// annot_pragma_openmp_end
847///
Michael Kruse251e1482019-02-01 20:25:04 +0000848/// declare-mapper-directive:
849/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
850/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
851/// annot_pragma_openmp_end
852///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000853/// declare-simd-directive:
854/// annot_pragma_openmp 'declare simd' {<clause> [,]}
855/// annot_pragma_openmp_end
856/// <function declaration/definition>
857///
Kelvin Li1408f912018-09-26 04:28:39 +0000858/// requires directive:
859/// annot_pragma_openmp 'requires' <clause> [[[,] <clause>] ... ]
860/// annot_pragma_openmp_end
861///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000862Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
863 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
864 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000865 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000866 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000867
Richard Smithaf3b3252017-05-18 19:21:48 +0000868 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000869 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000870
871 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000872 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000873 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000874 ThreadprivateListParserHelper Helper(this);
875 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000876 // The last seen token is annot_pragma_openmp_end - need to check for
877 // extra tokens.
878 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
879 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000880 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000881 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000882 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000883 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000884 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000885 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
886 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000887 }
888 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000889 }
Kelvin Li1408f912018-09-26 04:28:39 +0000890 case OMPD_requires: {
891 SourceLocation StartLoc = ConsumeToken();
892 SmallVector<OMPClause *, 5> Clauses;
893 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
894 FirstClauses(OMPC_unknown + 1);
895 if (Tok.is(tok::annot_pragma_openmp_end)) {
896 Diag(Tok, diag::err_omp_expected_clause)
897 << getOpenMPDirectiveName(OMPD_requires);
898 break;
899 }
900 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
901 OpenMPClauseKind CKind = Tok.isAnnotation()
902 ? OMPC_unknown
903 : getOpenMPClauseKind(PP.getSpelling(Tok));
904 Actions.StartOpenMPClause(CKind);
905 OMPClause *Clause =
906 ParseOpenMPClause(OMPD_requires, CKind, !FirstClauses[CKind].getInt());
907 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end, StopBeforeMatch);
908 FirstClauses[CKind].setInt(true);
909 if (Clause != nullptr)
910 Clauses.push_back(Clause);
911 if (Tok.is(tok::annot_pragma_openmp_end)) {
912 Actions.EndOpenMPClause();
913 break;
914 }
915 // Skip ',' if any.
916 if (Tok.is(tok::comma))
917 ConsumeToken();
918 Actions.EndOpenMPClause();
919 }
920 // Consume final annot_pragma_openmp_end
921 if (Clauses.size() == 0) {
922 Diag(Tok, diag::err_omp_expected_clause)
923 << getOpenMPDirectiveName(OMPD_requires);
924 ConsumeAnnotationToken();
925 return nullptr;
926 }
927 ConsumeAnnotationToken();
928 return Actions.ActOnOpenMPRequiresDirective(StartLoc, Clauses);
929 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000930 case OMPD_declare_reduction:
931 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000932 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000933 // The last seen token is annot_pragma_openmp_end - need to check for
934 // extra tokens.
935 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
936 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
937 << getOpenMPDirectiveName(OMPD_declare_reduction);
938 while (Tok.isNot(tok::annot_pragma_openmp_end))
939 ConsumeAnyToken();
940 }
941 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000942 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000943 return Res;
944 }
945 break;
Michael Kruse251e1482019-02-01 20:25:04 +0000946 case OMPD_declare_mapper: {
947 ConsumeToken();
948 if (DeclGroupPtrTy Res = ParseOpenMPDeclareMapperDirective(AS)) {
949 // Skip the last annot_pragma_openmp_end.
950 ConsumeAnnotationToken();
951 return Res;
952 }
953 break;
954 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000955 case OMPD_declare_simd: {
956 // The syntax is:
957 // { #pragma omp declare simd }
958 // <function-declaration-or-definition>
959 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000960 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000961 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000962 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
963 Toks.push_back(Tok);
964 ConsumeAnyToken();
965 }
966 Toks.push_back(Tok);
967 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000968
969 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +0000970 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000971 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +0000972 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000973 // Here we expect to see some function declaration.
974 if (AS == AS_none) {
975 assert(TagType == DeclSpec::TST_unspecified);
976 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000977 ParsingDeclSpec PDS(*this);
978 Ptr = ParseExternalDeclaration(Attrs, &PDS);
979 } else {
980 Ptr =
981 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
982 }
983 }
984 if (!Ptr) {
985 Diag(Loc, diag::err_omp_decl_in_declare_simd);
986 return DeclGroupPtrTy();
987 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000988 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000989 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000990 case OMPD_declare_target: {
991 SourceLocation DTLoc = ConsumeAnyToken();
992 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Kelvin Lie0502752018-11-21 20:15:57 +0000993 return ParseOMPDeclareTargetClauses();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000994 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000995
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000996 // Skip the last annot_pragma_openmp_end.
997 ConsumeAnyToken();
998
999 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
1000 return DeclGroupPtrTy();
1001
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001002 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +00001003 DKind = parseOpenMPDirectiveKind(*this);
Kelvin Libc38e632018-09-10 02:07:09 +00001004 while (DKind != OMPD_end_declare_target && Tok.isNot(tok::eof) &&
1005 Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +00001006 DeclGroupPtrTy Ptr;
1007 // Here we expect to see some function declaration.
1008 if (AS == AS_none) {
1009 assert(TagType == DeclSpec::TST_unspecified);
1010 MaybeParseCXX11Attributes(Attrs);
1011 ParsingDeclSpec PDS(*this);
1012 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1013 } else {
1014 Ptr =
1015 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1016 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001017 if (Ptr) {
1018 DeclGroupRef Ref = Ptr.get();
1019 Decls.append(Ref.begin(), Ref.end());
1020 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001021 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
1022 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +00001023 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001024 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001025 if (DKind != OMPD_end_declare_target)
1026 TPA.Revert();
1027 else
1028 TPA.Commit();
1029 }
1030 }
1031
Kelvin Lie0502752018-11-21 20:15:57 +00001032 ParseOMPEndDeclareTargetDirective(DKind, DTLoc);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001033 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +00001034 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001035 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001036 case OMPD_unknown:
1037 Diag(Tok, diag::err_omp_unknown_directive);
1038 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001039 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001040 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001041 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +00001042 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001043 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001044 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001045 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +00001046 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001047 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001048 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001049 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001050 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001051 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +00001052 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001053 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001054 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001055 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001056 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001057 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +00001058 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001059 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001060 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001061 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001062 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +00001063 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001064 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001065 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001066 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001067 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001068 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001069 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001070 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001071 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001072 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001073 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +00001074 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001075 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001076 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001077 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001078 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +00001079 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +00001080 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001081 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +00001082 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +00001083 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +00001084 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +00001085 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +00001086 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +00001087 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001088 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +00001089 break;
1090 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001091 while (Tok.isNot(tok::annot_pragma_openmp_end))
1092 ConsumeAnyToken();
1093 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +00001094 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001095}
1096
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001097/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001098///
1099/// threadprivate-directive:
1100/// annot_pragma_openmp 'threadprivate' simple-variable-list
1101/// annot_pragma_openmp_end
1102///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001103/// declare-reduction-directive:
1104/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
1105/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
1106/// ('omp_priv' '=' <expression>|<function_call>) ')']
1107/// annot_pragma_openmp_end
1108///
Michael Kruse251e1482019-02-01 20:25:04 +00001109/// declare-mapper-directive:
1110/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1111/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1112/// annot_pragma_openmp_end
1113///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001114/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001115/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001116/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
1117/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001118/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +00001119/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001120/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001121/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +00001122/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +00001123/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +00001124/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +00001125/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +00001126/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +00001127/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +00001128/// 'teams distribute parallel for' | 'target teams' |
1129/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +00001130/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +00001131/// 'target teams distribute parallel for simd' |
1132/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +00001133/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001134///
Alexey Bataevc4fad652016-01-13 11:18:54 +00001135StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +00001136 AllowedConstructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001137 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +00001138 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001139 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001140 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +00001141 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +00001142 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
1143 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +00001144 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +00001145 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001146 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001147 // Name of critical directive.
1148 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001149 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +00001150 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +00001151 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001152
1153 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001154 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001155 if (Allowed != ACK_Any) {
1156 Diag(Tok, diag::err_omp_immediate_directive)
1157 << getOpenMPDirectiveName(DKind) << 0;
1158 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001159 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001160 ThreadprivateListParserHelper Helper(this);
1161 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001162 // The last seen token is annot_pragma_openmp_end - need to check for
1163 // extra tokens.
1164 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1165 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001166 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +00001167 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001168 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001169 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
1170 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001171 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1172 }
Alp Tokerd751fa72013-12-18 19:10:49 +00001173 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001174 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001175 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001176 case OMPD_declare_reduction:
1177 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001178 if (DeclGroupPtrTy Res =
1179 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001180 // The last seen token is annot_pragma_openmp_end - need to check for
1181 // extra tokens.
1182 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1183 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1184 << getOpenMPDirectiveName(OMPD_declare_reduction);
1185 while (Tok.isNot(tok::annot_pragma_openmp_end))
1186 ConsumeAnyToken();
1187 }
1188 ConsumeAnyToken();
1189 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +00001190 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001191 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +00001192 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001193 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001194 case OMPD_declare_mapper: {
1195 ConsumeToken();
1196 if (DeclGroupPtrTy Res =
1197 ParseOpenMPDeclareMapperDirective(/*AS=*/AS_none)) {
1198 // Skip the last annot_pragma_openmp_end.
1199 ConsumeAnnotationToken();
1200 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1201 } else {
1202 SkipUntil(tok::annot_pragma_openmp_end);
1203 }
1204 break;
1205 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001206 case OMPD_flush:
1207 if (PP.LookAhead(0).is(tok::l_paren)) {
1208 FlushHasClause = true;
1209 // Push copy of the current token back to stream to properly parse
1210 // pseudo-clause OMPFlushClause.
1211 PP.EnterToken(Tok);
1212 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001213 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +00001214 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001215 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001216 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001217 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001218 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001219 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001220 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +00001221 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +00001222 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +00001223 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +00001224 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +00001225 }
1226 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001227 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001228 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001229 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001230 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001231 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001232 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001233 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001234 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001235 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001236 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001237 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001238 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001239 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001240 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001241 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001242 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001243 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001244 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001245 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001246 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001247 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001248 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001249 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001250 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001251 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001252 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001253 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001254 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001255 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001256 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001257 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001258 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001259 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001260 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001261 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001262 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001263 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001264 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001265 case OMPD_target_teams_distribute_parallel_for_simd:
1266 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001267 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001268 // Parse directive name of the 'critical' directive if any.
1269 if (DKind == OMPD_critical) {
1270 BalancedDelimiterTracker T(*this, tok::l_paren,
1271 tok::annot_pragma_openmp_end);
1272 if (!T.consumeOpen()) {
1273 if (Tok.isAnyIdentifier()) {
1274 DirName =
1275 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1276 ConsumeAnyToken();
1277 } else {
1278 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1279 }
1280 T.consumeClose();
1281 }
Alexey Bataev80909872015-07-02 11:25:17 +00001282 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001283 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001284 if (Tok.isNot(tok::annot_pragma_openmp_end))
1285 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001286 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001287
Alexey Bataevf29276e2014-06-18 04:14:57 +00001288 if (isOpenMPLoopDirective(DKind))
1289 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1290 if (isOpenMPSimdDirective(DKind))
1291 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1292 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001293 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001294
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001295 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001296 OpenMPClauseKind CKind =
1297 Tok.isAnnotation()
1298 ? OMPC_unknown
1299 : FlushHasClause ? OMPC_flush
1300 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001301 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001302 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001303 OMPClause *Clause =
1304 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001305 FirstClauses[CKind].setInt(true);
1306 if (Clause) {
1307 FirstClauses[CKind].setPointer(Clause);
1308 Clauses.push_back(Clause);
1309 }
1310
1311 // Skip ',' if any.
1312 if (Tok.is(tok::comma))
1313 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001314 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001315 }
1316 // End location of the directive.
1317 EndLoc = Tok.getLocation();
1318 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001319 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001320
Alexey Bataeveb482352015-12-18 05:05:56 +00001321 // OpenMP [2.13.8, ordered Construct, Syntax]
1322 // If the depend clause is specified, the ordered construct is a stand-alone
1323 // directive.
1324 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001325 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001326 Diag(Loc, diag::err_omp_immediate_directive)
1327 << getOpenMPDirectiveName(DKind) << 1
1328 << getOpenMPClauseName(OMPC_depend);
1329 }
1330 HasAssociatedStatement = false;
1331 }
1332
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001333 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001334 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001335 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001336 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001337 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1338 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1339 // should have at least one compound statement scope within it.
1340 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001341 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001342 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1343 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001344 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001345 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1346 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1347 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001348 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001349 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001350 Directive = Actions.ActOnOpenMPExecutableDirective(
1351 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1352 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001353
1354 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001355 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001356 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001357 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001358 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001359 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001360 case OMPD_declare_target:
1361 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00001362 case OMPD_requires:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001363 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001364 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001365 SkipUntil(tok::annot_pragma_openmp_end);
1366 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001367 case OMPD_unknown:
1368 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001369 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001370 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001371 }
1372 return Directive;
1373}
1374
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001375// Parses simple list:
1376// simple-variable-list:
1377// '(' id-expression {, id-expression} ')'
1378//
1379bool Parser::ParseOpenMPSimpleVarList(
1380 OpenMPDirectiveKind Kind,
1381 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1382 Callback,
1383 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001384 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001385 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001386 if (T.expectAndConsume(diag::err_expected_lparen_after,
1387 getOpenMPDirectiveName(Kind)))
1388 return true;
1389 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001390 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001391
1392 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001393 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001394 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001395 UnqualifiedId Name;
1396 // Read var name.
1397 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001398 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001399
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001400 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001401 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001402 IsCorrect = false;
1403 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001404 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001405 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00001406 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001407 IsCorrect = false;
1408 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001409 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001410 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1411 Tok.isNot(tok::annot_pragma_openmp_end)) {
1412 IsCorrect = false;
1413 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001414 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001415 Diag(PrevTok.getLocation(), diag::err_expected)
1416 << tok::identifier
1417 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001418 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001419 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001420 }
1421 // Consume ','.
1422 if (Tok.is(tok::comma)) {
1423 ConsumeToken();
1424 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001425 }
1426
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001427 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001428 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001429 IsCorrect = false;
1430 }
1431
1432 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001433 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001434
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001435 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001436}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001437
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001438/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001439///
1440/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001441/// if-clause | final-clause | num_threads-clause | safelen-clause |
1442/// default-clause | private-clause | firstprivate-clause | shared-clause
1443/// | linear-clause | aligned-clause | collapse-clause |
1444/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001445/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001446/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001447/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001448/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001449/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001450/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001451/// from-clause | is_device_ptr-clause | task_reduction-clause |
1452/// in_reduction-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001453///
1454OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1455 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001456 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001457 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001458 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001459 // Check if clause is allowed for the given directive.
1460 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001461 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1462 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001463 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001464 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001465 }
1466
1467 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001468 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001469 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001470 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001471 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001472 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001473 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001474 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001475 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001476 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001477 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001478 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001479 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001480 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001481 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001482 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001483 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001484 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001485 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001486 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001487 // OpenMP [2.9.1, target data construct, Restrictions]
1488 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001489 // OpenMP [2.11.1, task Construct, Restrictions]
1490 // At most one if clause can appear on the directive.
1491 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001492 // OpenMP [teams Construct, Restrictions]
1493 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001494 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001495 // OpenMP [2.9.1, task Construct, Restrictions]
1496 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001497 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1498 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001499 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1500 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001501 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001502 Diag(Tok, diag::err_omp_more_one_clause)
1503 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001504 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001505 }
1506
Alexey Bataev10e775f2015-07-30 11:36:16 +00001507 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001508 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001509 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001510 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001511 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001512 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001513 case OMPC_proc_bind:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001514 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001515 // OpenMP [2.14.3.1, Restrictions]
1516 // Only a single default clause may be specified on a parallel, task or
1517 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001518 // OpenMP [2.5, parallel Construct, Restrictions]
1519 // At most one proc_bind clause can appear on the directive.
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001520 // OpenMP [5.0, Requires directive, Restrictions]
1521 // At most one atomic_default_mem_order clause can appear
1522 // on the directive
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001523 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001524 Diag(Tok, diag::err_omp_more_one_clause)
1525 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001526 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001527 }
1528
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001529 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001530 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001531 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001532 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001533 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001534 // OpenMP [2.7.1, Restrictions, p. 3]
1535 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001536 // OpenMP [2.10.4, Restrictions, p. 106]
1537 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001538 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001539 Diag(Tok, diag::err_omp_more_one_clause)
1540 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001541 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001542 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001543 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001544
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001545 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001546 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001547 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001548 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001549 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001550 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001551 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001552 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001553 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001554 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001555 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001556 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001557 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001558 case OMPC_nogroup:
Kelvin Li1408f912018-09-26 04:28:39 +00001559 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00001560 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00001561 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00001562 case OMPC_dynamic_allocators:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001563 // OpenMP [2.7.1, Restrictions, p. 9]
1564 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001565 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1566 // Only one nowait clause can appear on a for directive.
Kelvin Li1408f912018-09-26 04:28:39 +00001567 // OpenMP [5.0, Requires directive, Restrictions]
1568 // Each of the requires clauses can appear at most once on the directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001569 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001570 Diag(Tok, diag::err_omp_more_one_clause)
1571 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001572 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001573 }
1574
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001575 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001576 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001577 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001578 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001579 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001580 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001581 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00001582 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00001583 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001584 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001585 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001586 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001587 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001588 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001589 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001590 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001591 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001592 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001593 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001594 case OMPC_is_device_ptr:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001595 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001596 break;
1597 case OMPC_unknown:
1598 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001599 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001600 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001601 break;
1602 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001603 case OMPC_uniform:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001604 if (!WrongDirective)
1605 Diag(Tok, diag::err_omp_unexpected_clause)
1606 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001607 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001608 break;
1609 }
Craig Topper161e4db2014-05-21 06:02:52 +00001610 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001611}
1612
Alexey Bataev2af33e32016-04-07 12:45:37 +00001613/// Parses simple expression in parens for single-expression clauses of OpenMP
1614/// constructs.
1615/// \param RLoc Returned location of right paren.
1616ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1617 SourceLocation &RLoc) {
1618 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1619 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1620 return ExprError();
1621
1622 SourceLocation ELoc = Tok.getLocation();
1623 ExprResult LHS(ParseCastExpression(
1624 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1625 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00001626 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev2af33e32016-04-07 12:45:37 +00001627
1628 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001629 RLoc = Tok.getLocation();
1630 if (!T.consumeClose())
1631 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001632
Alexey Bataev2af33e32016-04-07 12:45:37 +00001633 return Val;
1634}
1635
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001636/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001637/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001638/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001639///
Alexey Bataev3778b602014-07-17 07:32:53 +00001640/// final-clause:
1641/// 'final' '(' expression ')'
1642///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001643/// num_threads-clause:
1644/// 'num_threads' '(' expression ')'
1645///
1646/// safelen-clause:
1647/// 'safelen' '(' expression ')'
1648///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001649/// simdlen-clause:
1650/// 'simdlen' '(' expression ')'
1651///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001652/// collapse-clause:
1653/// 'collapse' '(' expression ')'
1654///
Alexey Bataeva0569352015-12-01 10:17:31 +00001655/// priority-clause:
1656/// 'priority' '(' expression ')'
1657///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001658/// grainsize-clause:
1659/// 'grainsize' '(' expression ')'
1660///
Alexey Bataev382967a2015-12-08 12:06:20 +00001661/// num_tasks-clause:
1662/// 'num_tasks' '(' expression ')'
1663///
Alexey Bataev28c75412015-12-15 08:19:24 +00001664/// hint-clause:
1665/// 'hint' '(' expression ')'
1666///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001667OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
1668 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001669 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001670 SourceLocation LLoc = Tok.getLocation();
1671 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001672
Alexey Bataev2af33e32016-04-07 12:45:37 +00001673 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001674
1675 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001676 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001677
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001678 if (ParseOnly)
1679 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00001680 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001681}
1682
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001683/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001684///
1685/// default-clause:
1686/// 'default' '(' 'none' | 'shared' ')
1687///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001688/// proc_bind-clause:
1689/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1690///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001691OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
1692 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001693 SourceLocation Loc = Tok.getLocation();
1694 SourceLocation LOpen = ConsumeToken();
1695 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001696 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001697 if (T.expectAndConsume(diag::err_expected_lparen_after,
1698 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001699 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001700
Alexey Bataeva55ed262014-05-28 06:15:33 +00001701 unsigned Type = getOpenMPSimpleClauseType(
1702 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001703 SourceLocation TypeLoc = Tok.getLocation();
1704 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1705 Tok.isNot(tok::annot_pragma_openmp_end))
1706 ConsumeAnyToken();
1707
1708 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001709 SourceLocation RLoc = Tok.getLocation();
1710 if (!T.consumeClose())
1711 RLoc = T.getCloseLocation();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001712
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001713 if (ParseOnly)
1714 return nullptr;
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001715 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc, RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001716}
1717
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001718/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001719///
1720/// ordered-clause:
1721/// 'ordered'
1722///
Alexey Bataev236070f2014-06-20 11:19:47 +00001723/// nowait-clause:
1724/// 'nowait'
1725///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001726/// untied-clause:
1727/// 'untied'
1728///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001729/// mergeable-clause:
1730/// 'mergeable'
1731///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001732/// read-clause:
1733/// 'read'
1734///
Alexey Bataev346265e2015-09-25 10:37:12 +00001735/// threads-clause:
1736/// 'threads'
1737///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001738/// simd-clause:
1739/// 'simd'
1740///
Alexey Bataevb825de12015-12-07 10:51:44 +00001741/// nogroup-clause:
1742/// 'nogroup'
1743///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001744OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001745 SourceLocation Loc = Tok.getLocation();
1746 ConsumeAnyToken();
1747
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001748 if (ParseOnly)
1749 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001750 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1751}
1752
1753
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001754/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00001755/// argument like 'schedule' or 'dist_schedule'.
1756///
1757/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001758/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1759/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001760///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001761/// if-clause:
1762/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1763///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001764/// defaultmap:
1765/// 'defaultmap' '(' modifier ':' kind ')'
1766///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001767OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
1768 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00001769 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001770 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001771 // Parse '('.
1772 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1773 if (T.expectAndConsume(diag::err_expected_lparen_after,
1774 getOpenMPClauseName(Kind)))
1775 return nullptr;
1776
1777 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001778 SmallVector<unsigned, 4> Arg;
1779 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001780 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001781 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1782 Arg.resize(NumberOfElements);
1783 KLoc.resize(NumberOfElements);
1784 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1785 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1786 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001787 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001788 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001789 if (KindModifier > OMPC_SCHEDULE_unknown) {
1790 // Parse 'modifier'
1791 Arg[Modifier1] = KindModifier;
1792 KLoc[Modifier1] = Tok.getLocation();
1793 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1794 Tok.isNot(tok::annot_pragma_openmp_end))
1795 ConsumeAnyToken();
1796 if (Tok.is(tok::comma)) {
1797 // Parse ',' 'modifier'
1798 ConsumeAnyToken();
1799 KindModifier = getOpenMPSimpleClauseType(
1800 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1801 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1802 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001803 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001804 KLoc[Modifier2] = Tok.getLocation();
1805 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1806 Tok.isNot(tok::annot_pragma_openmp_end))
1807 ConsumeAnyToken();
1808 }
1809 // Parse ':'
1810 if (Tok.is(tok::colon))
1811 ConsumeAnyToken();
1812 else
1813 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1814 KindModifier = getOpenMPSimpleClauseType(
1815 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1816 }
1817 Arg[ScheduleKind] = KindModifier;
1818 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001819 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1820 Tok.isNot(tok::annot_pragma_openmp_end))
1821 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001822 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1823 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1824 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001825 Tok.is(tok::comma))
1826 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001827 } else if (Kind == OMPC_dist_schedule) {
1828 Arg.push_back(getOpenMPSimpleClauseType(
1829 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1830 KLoc.push_back(Tok.getLocation());
1831 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1832 Tok.isNot(tok::annot_pragma_openmp_end))
1833 ConsumeAnyToken();
1834 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1835 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001836 } else if (Kind == OMPC_defaultmap) {
1837 // Get a defaultmap modifier
1838 Arg.push_back(getOpenMPSimpleClauseType(
1839 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1840 KLoc.push_back(Tok.getLocation());
1841 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1842 Tok.isNot(tok::annot_pragma_openmp_end))
1843 ConsumeAnyToken();
1844 // Parse ':'
1845 if (Tok.is(tok::colon))
1846 ConsumeAnyToken();
1847 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1848 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1849 // Get a defaultmap kind
1850 Arg.push_back(getOpenMPSimpleClauseType(
1851 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1852 KLoc.push_back(Tok.getLocation());
1853 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1854 Tok.isNot(tok::annot_pragma_openmp_end))
1855 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001856 } else {
1857 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001858 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001859 TentativeParsingAction TPA(*this);
Alexey Bataev61908f652018-04-23 19:53:05 +00001860 Arg.push_back(parseOpenMPDirectiveKind(*this));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001861 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001862 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001863 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1864 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001865 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001866 } else {
1867 TPA.Revert();
1868 Arg.back() = OMPD_unknown;
1869 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001870 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001871 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00001872 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001873 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001874
Carlo Bertollib4adf552016-01-15 18:50:31 +00001875 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1876 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1877 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001878 if (NeedAnExpression) {
1879 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001880 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1881 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00001882 Val =
1883 Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001884 }
1885
1886 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001887 SourceLocation RLoc = Tok.getLocation();
1888 if (!T.consumeClose())
1889 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001890
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001891 if (NeedAnExpression && Val.isInvalid())
1892 return nullptr;
1893
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001894 if (ParseOnly)
1895 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001896 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001897 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001898}
1899
Alexey Bataevc5e02582014-06-16 07:08:35 +00001900static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1901 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001902 if (ReductionIdScopeSpec.isEmpty()) {
1903 auto OOK = OO_None;
1904 switch (P.getCurToken().getKind()) {
1905 case tok::plus:
1906 OOK = OO_Plus;
1907 break;
1908 case tok::minus:
1909 OOK = OO_Minus;
1910 break;
1911 case tok::star:
1912 OOK = OO_Star;
1913 break;
1914 case tok::amp:
1915 OOK = OO_Amp;
1916 break;
1917 case tok::pipe:
1918 OOK = OO_Pipe;
1919 break;
1920 case tok::caret:
1921 OOK = OO_Caret;
1922 break;
1923 case tok::ampamp:
1924 OOK = OO_AmpAmp;
1925 break;
1926 case tok::pipepipe:
1927 OOK = OO_PipePipe;
1928 break;
1929 default:
1930 break;
1931 }
1932 if (OOK != OO_None) {
1933 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001934 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001935 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1936 return false;
1937 }
1938 }
1939 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1940 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00001941 /*AllowConstructorName*/ false,
1942 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00001943 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001944}
1945
Kelvin Lief579432018-12-18 22:18:41 +00001946/// Checks if the token is a valid map-type-modifier.
1947static OpenMPMapModifierKind isMapModifier(Parser &P) {
1948 Token Tok = P.getCurToken();
1949 if (!Tok.is(tok::identifier))
1950 return OMPC_MAP_MODIFIER_unknown;
1951
1952 Preprocessor &PP = P.getPreprocessor();
1953 OpenMPMapModifierKind TypeModifier = static_cast<OpenMPMapModifierKind>(
1954 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
1955 return TypeModifier;
1956}
1957
1958/// Parse map-type-modifiers in map clause.
1959/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
1960/// where, map-type-modifier ::= always | close
1961static void parseMapTypeModifiers(Parser &P,
1962 Parser::OpenMPVarListDataTy &Data) {
1963 Preprocessor &PP = P.getPreprocessor();
1964 while (P.getCurToken().isNot(tok::colon)) {
1965 Token Tok = P.getCurToken();
1966 OpenMPMapModifierKind TypeModifier = isMapModifier(P);
1967 if (TypeModifier == OMPC_MAP_MODIFIER_always ||
1968 TypeModifier == OMPC_MAP_MODIFIER_close) {
1969 Data.MapTypeModifiers.push_back(TypeModifier);
1970 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
1971 P.ConsumeToken();
1972 } else {
1973 // For the case of unknown map-type-modifier or a map-type.
1974 // Map-type is followed by a colon; the function returns when it
1975 // encounters a token followed by a colon.
1976 if (Tok.is(tok::comma)) {
1977 P.Diag(Tok, diag::err_omp_map_type_modifier_missing);
1978 P.ConsumeToken();
1979 continue;
1980 }
1981 // Potential map-type token as it is followed by a colon.
1982 if (PP.LookAhead(0).is(tok::colon))
1983 return;
1984 P.Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1985 P.ConsumeToken();
1986 }
1987 if (P.getCurToken().is(tok::comma))
1988 P.ConsumeToken();
1989 }
1990}
1991
1992/// Checks if the token is a valid map-type.
1993static OpenMPMapClauseKind isMapType(Parser &P) {
1994 Token Tok = P.getCurToken();
1995 // The map-type token can be either an identifier or the C++ delete keyword.
1996 if (!Tok.isOneOf(tok::identifier, tok::kw_delete))
1997 return OMPC_MAP_unknown;
1998 Preprocessor &PP = P.getPreprocessor();
1999 OpenMPMapClauseKind MapType = static_cast<OpenMPMapClauseKind>(
2000 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2001 return MapType;
2002}
2003
2004/// Parse map-type in map clause.
2005/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
2006/// where, map-type ::= to | from | tofrom | alloc | release | delete
2007static void parseMapType(Parser &P, Parser::OpenMPVarListDataTy &Data) {
2008 Token Tok = P.getCurToken();
2009 if (Tok.is(tok::colon)) {
2010 P.Diag(Tok, diag::err_omp_map_type_missing);
2011 return;
2012 }
2013 Data.MapType = isMapType(P);
2014 if (Data.MapType == OMPC_MAP_unknown)
2015 P.Diag(Tok, diag::err_omp_unknown_map_type);
2016 P.ConsumeToken();
2017}
2018
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002019/// Parses clauses with list.
2020bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
2021 OpenMPClauseKind Kind,
2022 SmallVectorImpl<Expr *> &Vars,
2023 OpenMPVarListDataTy &Data) {
2024 UnqualifiedId UnqualifiedReductionId;
2025 bool InvalidReductionId = false;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002026
2027 // Parse '('.
2028 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2029 if (T.expectAndConsume(diag::err_expected_lparen_after,
2030 getOpenMPClauseName(Kind)))
2031 return true;
2032
2033 bool NeedRParenForLinear = false;
2034 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
2035 tok::annot_pragma_openmp_end);
2036 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00002037 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
2038 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002039 ColonProtectionRAIIObject ColonRAII(*this);
2040 if (getLangOpts().CPlusPlus)
2041 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
2042 /*ObjectType=*/nullptr,
2043 /*EnteringContext=*/false);
2044 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
2045 UnqualifiedReductionId);
2046 if (InvalidReductionId) {
2047 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2048 StopBeforeMatch);
2049 }
2050 if (Tok.is(tok::colon))
2051 Data.ColonLoc = ConsumeToken();
2052 else
2053 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
2054 if (!InvalidReductionId)
2055 Data.ReductionId =
2056 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
2057 } else if (Kind == OMPC_depend) {
2058 // Handle dependency type for depend clause.
2059 ColonProtectionRAIIObject ColonRAII(*this);
2060 Data.DepKind =
2061 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
2062 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
2063 Data.DepLinMapLoc = Tok.getLocation();
2064
2065 if (Data.DepKind == OMPC_DEPEND_unknown) {
2066 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2067 StopBeforeMatch);
2068 } else {
2069 ConsumeToken();
2070 // Special processing for depend(source) clause.
2071 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
2072 // Parse ')'.
2073 T.consumeClose();
2074 return false;
2075 }
2076 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002077 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002078 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00002079 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002080 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
2081 : diag::warn_pragma_expected_colon)
2082 << "dependency type";
2083 }
2084 } else if (Kind == OMPC_linear) {
2085 // Try to parse modifier if any.
2086 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
2087 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
2088 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2089 Data.DepLinMapLoc = ConsumeToken();
2090 LinearT.consumeOpen();
2091 NeedRParenForLinear = true;
2092 }
2093 } else if (Kind == OMPC_map) {
2094 // Handle map type for map clause.
2095 ColonProtectionRAIIObject ColonRAII(*this);
2096
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002097 // The first identifier may be a list item, a map-type or a
Kelvin Lief579432018-12-18 22:18:41 +00002098 // map-type-modifier. The map-type can also be delete which has the same
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002099 // spelling of the C++ delete keyword.
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002100 Data.DepLinMapLoc = Tok.getLocation();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002101
Kelvin Lief579432018-12-18 22:18:41 +00002102 // Check for presence of a colon in the map clause.
2103 TentativeParsingAction TPA(*this);
2104 bool ColonPresent = false;
2105 if (SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2106 StopBeforeMatch)) {
2107 if (Tok.is(tok::colon))
2108 ColonPresent = true;
2109 }
2110 TPA.Revert();
2111 // Only parse map-type-modifier[s] and map-type if a colon is present in
2112 // the map clause.
2113 if (ColonPresent) {
2114 parseMapTypeModifiers(*this, Data);
2115 parseMapType(*this, Data);
2116 }
2117 if (Data.MapType == OMPC_MAP_unknown) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002118 Data.MapType = OMPC_MAP_tofrom;
2119 Data.IsMapTypeImplicit = true;
2120 }
2121
2122 if (Tok.is(tok::colon))
2123 Data.ColonLoc = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002124 }
2125
Alexey Bataevfa312f32017-07-21 18:48:21 +00002126 bool IsComma =
2127 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
2128 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
2129 (Kind == OMPC_reduction && !InvalidReductionId) ||
Kelvin Lida6bc702018-11-21 19:38:53 +00002130 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown) ||
Alexey Bataevfa312f32017-07-21 18:48:21 +00002131 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002132 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
2133 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
2134 Tok.isNot(tok::annot_pragma_openmp_end))) {
2135 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
2136 // Parse variable
2137 ExprResult VarExpr =
2138 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00002139 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002140 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00002141 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002142 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2143 StopBeforeMatch);
2144 }
2145 // Skip ',' if any
2146 IsComma = Tok.is(tok::comma);
2147 if (IsComma)
2148 ConsumeToken();
2149 else if (Tok.isNot(tok::r_paren) &&
2150 Tok.isNot(tok::annot_pragma_openmp_end) &&
2151 (!MayHaveTail || Tok.isNot(tok::colon)))
2152 Diag(Tok, diag::err_omp_expected_punc)
2153 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
2154 : getOpenMPClauseName(Kind))
2155 << (Kind == OMPC_flush);
2156 }
2157
2158 // Parse ')' for linear clause with modifier.
2159 if (NeedRParenForLinear)
2160 LinearT.consumeClose();
2161
2162 // Parse ':' linear-step (or ':' alignment).
2163 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
2164 if (MustHaveTail) {
2165 Data.ColonLoc = Tok.getLocation();
2166 SourceLocation ELoc = ConsumeToken();
2167 ExprResult Tail = ParseAssignmentExpression();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002168 Tail =
2169 Actions.ActOnFinishFullExpr(Tail.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002170 if (Tail.isUsable())
2171 Data.TailExpr = Tail.get();
2172 else
2173 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2174 StopBeforeMatch);
2175 }
2176
2177 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002178 Data.RLoc = Tok.getLocation();
2179 if (!T.consumeClose())
2180 Data.RLoc = T.getCloseLocation();
Alexey Bataev61908f652018-04-23 19:53:05 +00002181 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
2182 Vars.empty()) ||
2183 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
2184 (MustHaveTail && !Data.TailExpr) || InvalidReductionId;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002185}
2186
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002187/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00002188/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
2189/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002190///
2191/// private-clause:
2192/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002193/// firstprivate-clause:
2194/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00002195/// lastprivate-clause:
2196/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00002197/// shared-clause:
2198/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00002199/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00002200/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002201/// aligned-clause:
2202/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00002203/// reduction-clause:
2204/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00002205/// task_reduction-clause:
2206/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00002207/// in_reduction-clause:
2208/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00002209/// copyprivate-clause:
2210/// 'copyprivate' '(' list ')'
2211/// flush-clause:
2212/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002213/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00002214/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00002215/// map-clause:
Kelvin Lief579432018-12-18 22:18:41 +00002216/// 'map' '(' [ [ always [,] ] [ close [,] ]
Kelvin Li0bff7af2015-11-23 05:32:03 +00002217/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00002218/// to-clause:
2219/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00002220/// from-clause:
2221/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00002222/// use_device_ptr-clause:
2223/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00002224/// is_device_ptr-clause:
2225/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002226///
Alexey Bataev182227b2015-08-20 10:54:39 +00002227/// For 'linear' clause linear-list may have the following forms:
2228/// list
2229/// modifier(list)
2230/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00002231OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002232 OpenMPClauseKind Kind,
2233 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002234 SourceLocation Loc = Tok.getLocation();
2235 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002236 SmallVector<Expr *, 4> Vars;
2237 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002238
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002239 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00002240 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002241
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002242 if (ParseOnly)
2243 return nullptr;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002244 return Actions.ActOnOpenMPVarListClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002245 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Data.RLoc,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002246 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
Kelvin Lief579432018-12-18 22:18:41 +00002247 Data.MapTypeModifiers, Data.MapTypeModifiersLoc, Data.MapType,
2248 Data.IsMapTypeImplicit, Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002249}
2250