blob: 68843f0c65620c6b8c50606005c3677e2e31de23 [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///
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001135StmtResult
1136Parser::ParseOpenMPDeclarativeOrExecutableDirective(ParsedStmtContext StmtCtx) {
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: {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001155 // FIXME: Should this be permitted in C++?
1156 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1157 ParsedStmtContext()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001158 Diag(Tok, diag::err_omp_immediate_directive)
1159 << getOpenMPDirectiveName(DKind) << 0;
1160 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001161 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001162 ThreadprivateListParserHelper Helper(this);
1163 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001164 // The last seen token is annot_pragma_openmp_end - need to check for
1165 // extra tokens.
1166 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1167 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001168 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +00001169 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001170 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001171 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
1172 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001173 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1174 }
Alp Tokerd751fa72013-12-18 19:10:49 +00001175 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001176 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001177 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001178 case OMPD_declare_reduction:
1179 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001180 if (DeclGroupPtrTy Res =
1181 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001182 // The last seen token is annot_pragma_openmp_end - need to check for
1183 // extra tokens.
1184 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1185 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1186 << getOpenMPDirectiveName(OMPD_declare_reduction);
1187 while (Tok.isNot(tok::annot_pragma_openmp_end))
1188 ConsumeAnyToken();
1189 }
1190 ConsumeAnyToken();
1191 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +00001192 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001193 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +00001194 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001195 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001196 case OMPD_declare_mapper: {
1197 ConsumeToken();
1198 if (DeclGroupPtrTy Res =
1199 ParseOpenMPDeclareMapperDirective(/*AS=*/AS_none)) {
1200 // Skip the last annot_pragma_openmp_end.
1201 ConsumeAnnotationToken();
1202 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1203 } else {
1204 SkipUntil(tok::annot_pragma_openmp_end);
1205 }
1206 break;
1207 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001208 case OMPD_flush:
1209 if (PP.LookAhead(0).is(tok::l_paren)) {
1210 FlushHasClause = true;
1211 // Push copy of the current token back to stream to properly parse
1212 // pseudo-clause OMPFlushClause.
1213 PP.EnterToken(Tok);
1214 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001215 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +00001216 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001217 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001218 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001219 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001220 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001221 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001222 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +00001223 case OMPD_target_update:
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001224 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1225 ParsedStmtContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00001226 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +00001227 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +00001228 }
1229 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001230 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001231 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001232 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001233 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001234 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001235 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001236 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001237 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001238 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001239 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001240 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001241 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001242 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001243 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001244 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001245 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001246 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001247 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001248 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001249 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001250 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001251 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001252 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001253 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001254 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001255 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001256 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001257 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001258 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001259 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001260 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001261 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001262 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001263 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001264 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001265 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001266 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001267 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001268 case OMPD_target_teams_distribute_parallel_for_simd:
1269 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001270 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001271 // Parse directive name of the 'critical' directive if any.
1272 if (DKind == OMPD_critical) {
1273 BalancedDelimiterTracker T(*this, tok::l_paren,
1274 tok::annot_pragma_openmp_end);
1275 if (!T.consumeOpen()) {
1276 if (Tok.isAnyIdentifier()) {
1277 DirName =
1278 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1279 ConsumeAnyToken();
1280 } else {
1281 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1282 }
1283 T.consumeClose();
1284 }
Alexey Bataev80909872015-07-02 11:25:17 +00001285 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001286 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001287 if (Tok.isNot(tok::annot_pragma_openmp_end))
1288 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001289 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001290
Alexey Bataevf29276e2014-06-18 04:14:57 +00001291 if (isOpenMPLoopDirective(DKind))
1292 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1293 if (isOpenMPSimdDirective(DKind))
1294 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1295 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001296 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001297
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001298 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001299 OpenMPClauseKind CKind =
1300 Tok.isAnnotation()
1301 ? OMPC_unknown
1302 : FlushHasClause ? OMPC_flush
1303 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001304 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001305 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001306 OMPClause *Clause =
1307 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001308 FirstClauses[CKind].setInt(true);
1309 if (Clause) {
1310 FirstClauses[CKind].setPointer(Clause);
1311 Clauses.push_back(Clause);
1312 }
1313
1314 // Skip ',' if any.
1315 if (Tok.is(tok::comma))
1316 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001317 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001318 }
1319 // End location of the directive.
1320 EndLoc = Tok.getLocation();
1321 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001322 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001323
Alexey Bataeveb482352015-12-18 05:05:56 +00001324 // OpenMP [2.13.8, ordered Construct, Syntax]
1325 // If the depend clause is specified, the ordered construct is a stand-alone
1326 // directive.
1327 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001328 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1329 ParsedStmtContext()) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001330 Diag(Loc, diag::err_omp_immediate_directive)
1331 << getOpenMPDirectiveName(DKind) << 1
1332 << getOpenMPClauseName(OMPC_depend);
1333 }
1334 HasAssociatedStatement = false;
1335 }
1336
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001337 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001338 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001339 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001340 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001341 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1342 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1343 // should have at least one compound statement scope within it.
1344 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001345 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001346 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1347 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001348 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001349 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1350 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1351 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001352 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001353 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001354 Directive = Actions.ActOnOpenMPExecutableDirective(
1355 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1356 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001357
1358 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001359 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001360 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001361 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001362 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001363 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001364 case OMPD_declare_target:
1365 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00001366 case OMPD_requires:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001367 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001368 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001369 SkipUntil(tok::annot_pragma_openmp_end);
1370 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001371 case OMPD_unknown:
1372 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001373 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001374 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001375 }
1376 return Directive;
1377}
1378
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001379// Parses simple list:
1380// simple-variable-list:
1381// '(' id-expression {, id-expression} ')'
1382//
1383bool Parser::ParseOpenMPSimpleVarList(
1384 OpenMPDirectiveKind Kind,
1385 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1386 Callback,
1387 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001388 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001389 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001390 if (T.expectAndConsume(diag::err_expected_lparen_after,
1391 getOpenMPDirectiveName(Kind)))
1392 return true;
1393 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001394 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001395
1396 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001397 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001398 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001399 UnqualifiedId Name;
1400 // Read var name.
1401 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001402 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001403
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001404 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001405 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001406 IsCorrect = false;
1407 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001408 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001409 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00001410 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001411 IsCorrect = false;
1412 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001413 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001414 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1415 Tok.isNot(tok::annot_pragma_openmp_end)) {
1416 IsCorrect = false;
1417 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001418 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001419 Diag(PrevTok.getLocation(), diag::err_expected)
1420 << tok::identifier
1421 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001422 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001423 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001424 }
1425 // Consume ','.
1426 if (Tok.is(tok::comma)) {
1427 ConsumeToken();
1428 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001429 }
1430
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001431 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001432 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001433 IsCorrect = false;
1434 }
1435
1436 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001437 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001438
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001439 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001440}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001441
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001442/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001443///
1444/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001445/// if-clause | final-clause | num_threads-clause | safelen-clause |
1446/// default-clause | private-clause | firstprivate-clause | shared-clause
1447/// | linear-clause | aligned-clause | collapse-clause |
1448/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001449/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001450/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001451/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001452/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001453/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001454/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001455/// from-clause | is_device_ptr-clause | task_reduction-clause |
1456/// in_reduction-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001457///
1458OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1459 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001460 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001461 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001462 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001463 // Check if clause is allowed for the given directive.
1464 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001465 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1466 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001467 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001468 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001469 }
1470
1471 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001472 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001473 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001474 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001475 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001476 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001477 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001478 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001479 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001480 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001481 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001482 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001483 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001484 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001485 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001486 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001487 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001488 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001489 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001490 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001491 // OpenMP [2.9.1, target data construct, Restrictions]
1492 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001493 // OpenMP [2.11.1, task Construct, Restrictions]
1494 // At most one if clause can appear on the directive.
1495 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001496 // OpenMP [teams Construct, Restrictions]
1497 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001498 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001499 // OpenMP [2.9.1, task Construct, Restrictions]
1500 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001501 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1502 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001503 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1504 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001505 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001506 Diag(Tok, diag::err_omp_more_one_clause)
1507 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001508 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001509 }
1510
Alexey Bataev10e775f2015-07-30 11:36:16 +00001511 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001512 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001513 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001514 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001515 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001516 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001517 case OMPC_proc_bind:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001518 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001519 // OpenMP [2.14.3.1, Restrictions]
1520 // Only a single default clause may be specified on a parallel, task or
1521 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001522 // OpenMP [2.5, parallel Construct, Restrictions]
1523 // At most one proc_bind clause can appear on the directive.
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001524 // OpenMP [5.0, Requires directive, Restrictions]
1525 // At most one atomic_default_mem_order clause can appear
1526 // on the directive
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001527 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001528 Diag(Tok, diag::err_omp_more_one_clause)
1529 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001530 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001531 }
1532
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001533 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001534 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001535 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001536 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001537 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001538 // OpenMP [2.7.1, Restrictions, p. 3]
1539 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001540 // OpenMP [2.10.4, Restrictions, p. 106]
1541 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001542 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001543 Diag(Tok, diag::err_omp_more_one_clause)
1544 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001545 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001546 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001547 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001548
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001549 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001550 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001551 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001552 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001553 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001554 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001555 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001556 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001557 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001558 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001559 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001560 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001561 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001562 case OMPC_nogroup:
Kelvin Li1408f912018-09-26 04:28:39 +00001563 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00001564 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00001565 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00001566 case OMPC_dynamic_allocators:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001567 // OpenMP [2.7.1, Restrictions, p. 9]
1568 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001569 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1570 // Only one nowait clause can appear on a for directive.
Kelvin Li1408f912018-09-26 04:28:39 +00001571 // OpenMP [5.0, Requires directive, Restrictions]
1572 // Each of the requires clauses can appear at most once on the directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001573 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001574 Diag(Tok, diag::err_omp_more_one_clause)
1575 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001576 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001577 }
1578
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001579 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001580 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001581 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001582 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001583 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001584 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001585 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00001586 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00001587 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001588 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001589 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001590 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001591 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001592 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001593 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001594 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001595 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001596 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001597 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001598 case OMPC_is_device_ptr:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001599 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001600 break;
1601 case OMPC_unknown:
1602 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001603 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001604 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001605 break;
1606 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001607 case OMPC_uniform:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001608 if (!WrongDirective)
1609 Diag(Tok, diag::err_omp_unexpected_clause)
1610 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001611 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001612 break;
1613 }
Craig Topper161e4db2014-05-21 06:02:52 +00001614 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001615}
1616
Alexey Bataev2af33e32016-04-07 12:45:37 +00001617/// Parses simple expression in parens for single-expression clauses of OpenMP
1618/// constructs.
1619/// \param RLoc Returned location of right paren.
1620ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1621 SourceLocation &RLoc) {
1622 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1623 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1624 return ExprError();
1625
1626 SourceLocation ELoc = Tok.getLocation();
1627 ExprResult LHS(ParseCastExpression(
1628 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1629 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00001630 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev2af33e32016-04-07 12:45:37 +00001631
1632 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001633 RLoc = Tok.getLocation();
1634 if (!T.consumeClose())
1635 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001636
Alexey Bataev2af33e32016-04-07 12:45:37 +00001637 return Val;
1638}
1639
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001640/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001641/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001642/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001643///
Alexey Bataev3778b602014-07-17 07:32:53 +00001644/// final-clause:
1645/// 'final' '(' expression ')'
1646///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001647/// num_threads-clause:
1648/// 'num_threads' '(' expression ')'
1649///
1650/// safelen-clause:
1651/// 'safelen' '(' expression ')'
1652///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001653/// simdlen-clause:
1654/// 'simdlen' '(' expression ')'
1655///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001656/// collapse-clause:
1657/// 'collapse' '(' expression ')'
1658///
Alexey Bataeva0569352015-12-01 10:17:31 +00001659/// priority-clause:
1660/// 'priority' '(' expression ')'
1661///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001662/// grainsize-clause:
1663/// 'grainsize' '(' expression ')'
1664///
Alexey Bataev382967a2015-12-08 12:06:20 +00001665/// num_tasks-clause:
1666/// 'num_tasks' '(' expression ')'
1667///
Alexey Bataev28c75412015-12-15 08:19:24 +00001668/// hint-clause:
1669/// 'hint' '(' expression ')'
1670///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001671OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
1672 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001673 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001674 SourceLocation LLoc = Tok.getLocation();
1675 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001676
Alexey Bataev2af33e32016-04-07 12:45:37 +00001677 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001678
1679 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001680 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001681
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001682 if (ParseOnly)
1683 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00001684 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001685}
1686
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001687/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001688///
1689/// default-clause:
1690/// 'default' '(' 'none' | 'shared' ')
1691///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001692/// proc_bind-clause:
1693/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1694///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001695OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
1696 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001697 SourceLocation Loc = Tok.getLocation();
1698 SourceLocation LOpen = ConsumeToken();
1699 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001700 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001701 if (T.expectAndConsume(diag::err_expected_lparen_after,
1702 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001703 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001704
Alexey Bataeva55ed262014-05-28 06:15:33 +00001705 unsigned Type = getOpenMPSimpleClauseType(
1706 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001707 SourceLocation TypeLoc = Tok.getLocation();
1708 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1709 Tok.isNot(tok::annot_pragma_openmp_end))
1710 ConsumeAnyToken();
1711
1712 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001713 SourceLocation RLoc = Tok.getLocation();
1714 if (!T.consumeClose())
1715 RLoc = T.getCloseLocation();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001716
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001717 if (ParseOnly)
1718 return nullptr;
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001719 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc, RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001720}
1721
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001722/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001723///
1724/// ordered-clause:
1725/// 'ordered'
1726///
Alexey Bataev236070f2014-06-20 11:19:47 +00001727/// nowait-clause:
1728/// 'nowait'
1729///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001730/// untied-clause:
1731/// 'untied'
1732///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001733/// mergeable-clause:
1734/// 'mergeable'
1735///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001736/// read-clause:
1737/// 'read'
1738///
Alexey Bataev346265e2015-09-25 10:37:12 +00001739/// threads-clause:
1740/// 'threads'
1741///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001742/// simd-clause:
1743/// 'simd'
1744///
Alexey Bataevb825de12015-12-07 10:51:44 +00001745/// nogroup-clause:
1746/// 'nogroup'
1747///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001748OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001749 SourceLocation Loc = Tok.getLocation();
1750 ConsumeAnyToken();
1751
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001752 if (ParseOnly)
1753 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001754 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1755}
1756
1757
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001758/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00001759/// argument like 'schedule' or 'dist_schedule'.
1760///
1761/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001762/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1763/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001764///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001765/// if-clause:
1766/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1767///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001768/// defaultmap:
1769/// 'defaultmap' '(' modifier ':' kind ')'
1770///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001771OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
1772 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00001773 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001774 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001775 // Parse '('.
1776 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1777 if (T.expectAndConsume(diag::err_expected_lparen_after,
1778 getOpenMPClauseName(Kind)))
1779 return nullptr;
1780
1781 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001782 SmallVector<unsigned, 4> Arg;
1783 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001784 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001785 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1786 Arg.resize(NumberOfElements);
1787 KLoc.resize(NumberOfElements);
1788 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1789 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1790 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001791 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001792 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001793 if (KindModifier > OMPC_SCHEDULE_unknown) {
1794 // Parse 'modifier'
1795 Arg[Modifier1] = KindModifier;
1796 KLoc[Modifier1] = Tok.getLocation();
1797 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1798 Tok.isNot(tok::annot_pragma_openmp_end))
1799 ConsumeAnyToken();
1800 if (Tok.is(tok::comma)) {
1801 // Parse ',' 'modifier'
1802 ConsumeAnyToken();
1803 KindModifier = getOpenMPSimpleClauseType(
1804 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1805 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1806 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001807 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001808 KLoc[Modifier2] = Tok.getLocation();
1809 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1810 Tok.isNot(tok::annot_pragma_openmp_end))
1811 ConsumeAnyToken();
1812 }
1813 // Parse ':'
1814 if (Tok.is(tok::colon))
1815 ConsumeAnyToken();
1816 else
1817 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1818 KindModifier = getOpenMPSimpleClauseType(
1819 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1820 }
1821 Arg[ScheduleKind] = KindModifier;
1822 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001823 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1824 Tok.isNot(tok::annot_pragma_openmp_end))
1825 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001826 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1827 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1828 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001829 Tok.is(tok::comma))
1830 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001831 } else if (Kind == OMPC_dist_schedule) {
1832 Arg.push_back(getOpenMPSimpleClauseType(
1833 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1834 KLoc.push_back(Tok.getLocation());
1835 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1836 Tok.isNot(tok::annot_pragma_openmp_end))
1837 ConsumeAnyToken();
1838 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1839 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001840 } else if (Kind == OMPC_defaultmap) {
1841 // Get a defaultmap modifier
1842 Arg.push_back(getOpenMPSimpleClauseType(
1843 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1844 KLoc.push_back(Tok.getLocation());
1845 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1846 Tok.isNot(tok::annot_pragma_openmp_end))
1847 ConsumeAnyToken();
1848 // Parse ':'
1849 if (Tok.is(tok::colon))
1850 ConsumeAnyToken();
1851 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1852 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1853 // Get a defaultmap kind
1854 Arg.push_back(getOpenMPSimpleClauseType(
1855 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1856 KLoc.push_back(Tok.getLocation());
1857 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1858 Tok.isNot(tok::annot_pragma_openmp_end))
1859 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001860 } else {
1861 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001862 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001863 TentativeParsingAction TPA(*this);
Alexey Bataev61908f652018-04-23 19:53:05 +00001864 Arg.push_back(parseOpenMPDirectiveKind(*this));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001865 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001866 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001867 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1868 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001869 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001870 } else {
1871 TPA.Revert();
1872 Arg.back() = OMPD_unknown;
1873 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001874 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001875 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00001876 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001877 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001878
Carlo Bertollib4adf552016-01-15 18:50:31 +00001879 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1880 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1881 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001882 if (NeedAnExpression) {
1883 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001884 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1885 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00001886 Val =
1887 Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001888 }
1889
1890 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001891 SourceLocation RLoc = Tok.getLocation();
1892 if (!T.consumeClose())
1893 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001894
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001895 if (NeedAnExpression && Val.isInvalid())
1896 return nullptr;
1897
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001898 if (ParseOnly)
1899 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001900 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001901 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001902}
1903
Alexey Bataevc5e02582014-06-16 07:08:35 +00001904static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1905 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001906 if (ReductionIdScopeSpec.isEmpty()) {
1907 auto OOK = OO_None;
1908 switch (P.getCurToken().getKind()) {
1909 case tok::plus:
1910 OOK = OO_Plus;
1911 break;
1912 case tok::minus:
1913 OOK = OO_Minus;
1914 break;
1915 case tok::star:
1916 OOK = OO_Star;
1917 break;
1918 case tok::amp:
1919 OOK = OO_Amp;
1920 break;
1921 case tok::pipe:
1922 OOK = OO_Pipe;
1923 break;
1924 case tok::caret:
1925 OOK = OO_Caret;
1926 break;
1927 case tok::ampamp:
1928 OOK = OO_AmpAmp;
1929 break;
1930 case tok::pipepipe:
1931 OOK = OO_PipePipe;
1932 break;
1933 default:
1934 break;
1935 }
1936 if (OOK != OO_None) {
1937 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001938 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001939 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1940 return false;
1941 }
1942 }
1943 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1944 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00001945 /*AllowConstructorName*/ false,
1946 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00001947 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001948}
1949
Kelvin Lief579432018-12-18 22:18:41 +00001950/// Checks if the token is a valid map-type-modifier.
1951static OpenMPMapModifierKind isMapModifier(Parser &P) {
1952 Token Tok = P.getCurToken();
1953 if (!Tok.is(tok::identifier))
1954 return OMPC_MAP_MODIFIER_unknown;
1955
1956 Preprocessor &PP = P.getPreprocessor();
1957 OpenMPMapModifierKind TypeModifier = static_cast<OpenMPMapModifierKind>(
1958 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
1959 return TypeModifier;
1960}
1961
Michael Kruse01f670d2019-02-22 22:29:42 +00001962/// Parse the mapper modifier in map, to, and from clauses.
1963bool Parser::parseMapperModifier(OpenMPVarListDataTy &Data) {
1964 // Parse '('.
1965 BalancedDelimiterTracker T(*this, tok::l_paren, tok::colon);
1966 if (T.expectAndConsume(diag::err_expected_lparen_after, "mapper")) {
1967 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1968 StopBeforeMatch);
1969 return true;
1970 }
1971 // Parse mapper-identifier
1972 if (getLangOpts().CPlusPlus)
1973 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
1974 /*ObjectType=*/nullptr,
1975 /*EnteringContext=*/false);
1976 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
1977 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
1978 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1979 StopBeforeMatch);
1980 return true;
1981 }
1982 auto &DeclNames = Actions.getASTContext().DeclarationNames;
1983 Data.ReductionOrMapperId = DeclarationNameInfo(
1984 DeclNames.getIdentifier(Tok.getIdentifierInfo()), Tok.getLocation());
1985 ConsumeToken();
1986 // Parse ')'.
1987 return T.consumeClose();
1988}
1989
Kelvin Lief579432018-12-18 22:18:41 +00001990/// Parse map-type-modifiers in map clause.
1991/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Michael Kruse4304e9d2019-02-19 16:38:20 +00001992/// where, map-type-modifier ::= always | close | mapper(mapper-identifier)
1993bool Parser::parseMapTypeModifiers(OpenMPVarListDataTy &Data) {
1994 while (getCurToken().isNot(tok::colon)) {
1995 OpenMPMapModifierKind TypeModifier = isMapModifier(*this);
Kelvin Lief579432018-12-18 22:18:41 +00001996 if (TypeModifier == OMPC_MAP_MODIFIER_always ||
1997 TypeModifier == OMPC_MAP_MODIFIER_close) {
1998 Data.MapTypeModifiers.push_back(TypeModifier);
1999 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
Michael Kruse4304e9d2019-02-19 16:38:20 +00002000 ConsumeToken();
2001 } else if (TypeModifier == OMPC_MAP_MODIFIER_mapper) {
2002 Data.MapTypeModifiers.push_back(TypeModifier);
2003 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
2004 ConsumeToken();
Michael Kruse01f670d2019-02-22 22:29:42 +00002005 if (parseMapperModifier(Data))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002006 return true;
Kelvin Lief579432018-12-18 22:18:41 +00002007 } else {
2008 // For the case of unknown map-type-modifier or a map-type.
2009 // Map-type is followed by a colon; the function returns when it
2010 // encounters a token followed by a colon.
2011 if (Tok.is(tok::comma)) {
Michael Kruse4304e9d2019-02-19 16:38:20 +00002012 Diag(Tok, diag::err_omp_map_type_modifier_missing);
2013 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002014 continue;
2015 }
2016 // Potential map-type token as it is followed by a colon.
2017 if (PP.LookAhead(0).is(tok::colon))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002018 return false;
2019 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
2020 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002021 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002022 if (getCurToken().is(tok::comma))
2023 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002024 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002025 return false;
Kelvin Lief579432018-12-18 22:18:41 +00002026}
2027
2028/// Checks if the token is a valid map-type.
2029static OpenMPMapClauseKind isMapType(Parser &P) {
2030 Token Tok = P.getCurToken();
2031 // The map-type token can be either an identifier or the C++ delete keyword.
2032 if (!Tok.isOneOf(tok::identifier, tok::kw_delete))
2033 return OMPC_MAP_unknown;
2034 Preprocessor &PP = P.getPreprocessor();
2035 OpenMPMapClauseKind MapType = static_cast<OpenMPMapClauseKind>(
2036 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2037 return MapType;
2038}
2039
2040/// Parse map-type in map clause.
2041/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
2042/// where, map-type ::= to | from | tofrom | alloc | release | delete
2043static void parseMapType(Parser &P, Parser::OpenMPVarListDataTy &Data) {
2044 Token Tok = P.getCurToken();
2045 if (Tok.is(tok::colon)) {
2046 P.Diag(Tok, diag::err_omp_map_type_missing);
2047 return;
2048 }
2049 Data.MapType = isMapType(P);
2050 if (Data.MapType == OMPC_MAP_unknown)
2051 P.Diag(Tok, diag::err_omp_unknown_map_type);
2052 P.ConsumeToken();
2053}
2054
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002055/// Parses clauses with list.
2056bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
2057 OpenMPClauseKind Kind,
2058 SmallVectorImpl<Expr *> &Vars,
2059 OpenMPVarListDataTy &Data) {
2060 UnqualifiedId UnqualifiedReductionId;
2061 bool InvalidReductionId = false;
Michael Kruse01f670d2019-02-22 22:29:42 +00002062 bool IsInvalidMapperModifier = false;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002063
2064 // Parse '('.
2065 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2066 if (T.expectAndConsume(diag::err_expected_lparen_after,
2067 getOpenMPClauseName(Kind)))
2068 return true;
2069
2070 bool NeedRParenForLinear = false;
2071 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
2072 tok::annot_pragma_openmp_end);
2073 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00002074 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
2075 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002076 ColonProtectionRAIIObject ColonRAII(*this);
2077 if (getLangOpts().CPlusPlus)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002078 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002079 /*ObjectType=*/nullptr,
2080 /*EnteringContext=*/false);
Michael Kruse4304e9d2019-02-19 16:38:20 +00002081 InvalidReductionId = ParseReductionId(
2082 *this, Data.ReductionOrMapperIdScopeSpec, UnqualifiedReductionId);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002083 if (InvalidReductionId) {
2084 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2085 StopBeforeMatch);
2086 }
2087 if (Tok.is(tok::colon))
2088 Data.ColonLoc = ConsumeToken();
2089 else
2090 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
2091 if (!InvalidReductionId)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002092 Data.ReductionOrMapperId =
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002093 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
2094 } else if (Kind == OMPC_depend) {
2095 // Handle dependency type for depend clause.
2096 ColonProtectionRAIIObject ColonRAII(*this);
2097 Data.DepKind =
2098 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
2099 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
2100 Data.DepLinMapLoc = Tok.getLocation();
2101
2102 if (Data.DepKind == OMPC_DEPEND_unknown) {
2103 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2104 StopBeforeMatch);
2105 } else {
2106 ConsumeToken();
2107 // Special processing for depend(source) clause.
2108 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
2109 // Parse ')'.
2110 T.consumeClose();
2111 return false;
2112 }
2113 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002114 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002115 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00002116 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002117 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
2118 : diag::warn_pragma_expected_colon)
2119 << "dependency type";
2120 }
2121 } else if (Kind == OMPC_linear) {
2122 // Try to parse modifier if any.
2123 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
2124 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
2125 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2126 Data.DepLinMapLoc = ConsumeToken();
2127 LinearT.consumeOpen();
2128 NeedRParenForLinear = true;
2129 }
2130 } else if (Kind == OMPC_map) {
2131 // Handle map type for map clause.
2132 ColonProtectionRAIIObject ColonRAII(*this);
2133
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002134 // The first identifier may be a list item, a map-type or a
Kelvin Lief579432018-12-18 22:18:41 +00002135 // map-type-modifier. The map-type can also be delete which has the same
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002136 // spelling of the C++ delete keyword.
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002137 Data.DepLinMapLoc = Tok.getLocation();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002138
Kelvin Lief579432018-12-18 22:18:41 +00002139 // Check for presence of a colon in the map clause.
2140 TentativeParsingAction TPA(*this);
2141 bool ColonPresent = false;
2142 if (SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2143 StopBeforeMatch)) {
2144 if (Tok.is(tok::colon))
2145 ColonPresent = true;
2146 }
2147 TPA.Revert();
2148 // Only parse map-type-modifier[s] and map-type if a colon is present in
2149 // the map clause.
2150 if (ColonPresent) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002151 IsInvalidMapperModifier = parseMapTypeModifiers(Data);
2152 if (!IsInvalidMapperModifier)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002153 parseMapType(*this, Data);
Michael Kruse01f670d2019-02-22 22:29:42 +00002154 else
2155 SkipUntil(tok::colon, tok::annot_pragma_openmp_end, StopBeforeMatch);
Kelvin Lief579432018-12-18 22:18:41 +00002156 }
2157 if (Data.MapType == OMPC_MAP_unknown) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002158 Data.MapType = OMPC_MAP_tofrom;
2159 Data.IsMapTypeImplicit = true;
2160 }
2161
2162 if (Tok.is(tok::colon))
2163 Data.ColonLoc = ConsumeToken();
Michael Kruse01f670d2019-02-22 22:29:42 +00002164 } else if (Kind == OMPC_to) {
2165 if (Tok.is(tok::identifier)) {
2166 bool IsMapperModifier = false;
2167 auto Modifier = static_cast<OpenMPToModifierKind>(
2168 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2169 if (Modifier == OMPC_TO_MODIFIER_mapper)
2170 IsMapperModifier = true;
2171 if (IsMapperModifier) {
2172 // Parse the mapper modifier.
2173 ConsumeToken();
2174 IsInvalidMapperModifier = parseMapperModifier(Data);
2175 if (Tok.isNot(tok::colon)) {
2176 if (!IsInvalidMapperModifier)
2177 Diag(Tok, diag::warn_pragma_expected_colon) << ")";
2178 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2179 StopBeforeMatch);
2180 }
2181 // Consume ':'.
2182 if (Tok.is(tok::colon))
2183 ConsumeToken();
2184 }
2185 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002186 }
2187
Alexey Bataevfa312f32017-07-21 18:48:21 +00002188 bool IsComma =
2189 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
2190 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
2191 (Kind == OMPC_reduction && !InvalidReductionId) ||
Kelvin Lida6bc702018-11-21 19:38:53 +00002192 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown) ||
Alexey Bataevfa312f32017-07-21 18:48:21 +00002193 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002194 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
2195 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
2196 Tok.isNot(tok::annot_pragma_openmp_end))) {
2197 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
2198 // Parse variable
2199 ExprResult VarExpr =
2200 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00002201 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002202 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00002203 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002204 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2205 StopBeforeMatch);
2206 }
2207 // Skip ',' if any
2208 IsComma = Tok.is(tok::comma);
2209 if (IsComma)
2210 ConsumeToken();
2211 else if (Tok.isNot(tok::r_paren) &&
2212 Tok.isNot(tok::annot_pragma_openmp_end) &&
2213 (!MayHaveTail || Tok.isNot(tok::colon)))
2214 Diag(Tok, diag::err_omp_expected_punc)
2215 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
2216 : getOpenMPClauseName(Kind))
2217 << (Kind == OMPC_flush);
2218 }
2219
2220 // Parse ')' for linear clause with modifier.
2221 if (NeedRParenForLinear)
2222 LinearT.consumeClose();
2223
2224 // Parse ':' linear-step (or ':' alignment).
2225 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
2226 if (MustHaveTail) {
2227 Data.ColonLoc = Tok.getLocation();
2228 SourceLocation ELoc = ConsumeToken();
2229 ExprResult Tail = ParseAssignmentExpression();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002230 Tail =
2231 Actions.ActOnFinishFullExpr(Tail.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002232 if (Tail.isUsable())
2233 Data.TailExpr = Tail.get();
2234 else
2235 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2236 StopBeforeMatch);
2237 }
2238
2239 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002240 Data.RLoc = Tok.getLocation();
2241 if (!T.consumeClose())
2242 Data.RLoc = T.getCloseLocation();
Alexey Bataev61908f652018-04-23 19:53:05 +00002243 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
2244 Vars.empty()) ||
2245 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
Michael Kruse4304e9d2019-02-19 16:38:20 +00002246 (MustHaveTail && !Data.TailExpr) || InvalidReductionId ||
Michael Kruse01f670d2019-02-22 22:29:42 +00002247 IsInvalidMapperModifier;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002248}
2249
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002250/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00002251/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
2252/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002253///
2254/// private-clause:
2255/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002256/// firstprivate-clause:
2257/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00002258/// lastprivate-clause:
2259/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00002260/// shared-clause:
2261/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00002262/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00002263/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002264/// aligned-clause:
2265/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00002266/// reduction-clause:
2267/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00002268/// task_reduction-clause:
2269/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00002270/// in_reduction-clause:
2271/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00002272/// copyprivate-clause:
2273/// 'copyprivate' '(' list ')'
2274/// flush-clause:
2275/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002276/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00002277/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00002278/// map-clause:
Kelvin Lief579432018-12-18 22:18:41 +00002279/// 'map' '(' [ [ always [,] ] [ close [,] ]
Michael Kruse01f670d2019-02-22 22:29:42 +00002280/// [ mapper '(' mapper-identifier ')' [,] ]
Kelvin Li0bff7af2015-11-23 05:32:03 +00002281/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00002282/// to-clause:
Michael Kruse01f670d2019-02-22 22:29:42 +00002283/// 'to' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00002284/// from-clause:
2285/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00002286/// use_device_ptr-clause:
2287/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00002288/// is_device_ptr-clause:
2289/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002290///
Alexey Bataev182227b2015-08-20 10:54:39 +00002291/// For 'linear' clause linear-list may have the following forms:
2292/// list
2293/// modifier(list)
2294/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00002295OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002296 OpenMPClauseKind Kind,
2297 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002298 SourceLocation Loc = Tok.getLocation();
2299 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002300 SmallVector<Expr *, 4> Vars;
2301 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002302
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002303 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00002304 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002305
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002306 if (ParseOnly)
2307 return nullptr;
Michael Kruse4304e9d2019-02-19 16:38:20 +00002308 OMPVarListLocTy Locs(Loc, LOpen, Data.RLoc);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002309 return Actions.ActOnOpenMPVarListClause(
Michael Kruse4304e9d2019-02-19 16:38:20 +00002310 Kind, Vars, Data.TailExpr, Locs, Data.ColonLoc,
2311 Data.ReductionOrMapperIdScopeSpec, Data.ReductionOrMapperId, Data.DepKind,
2312 Data.LinKind, Data.MapTypeModifiers, Data.MapTypeModifiersLoc,
2313 Data.MapType, Data.IsMapTypeImplicit, Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002314}
2315