blob: 293660ec4cd8c20d179cf8c86a66a470047e6625 [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,
Alexey Bataevd158cf62019-09-13 20:18:17 +000045 OMPD_variant,
Dmitry Polukhin82478332016-02-13 06:53:38 +000046};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000047
Alexey Bataev25ed0c02019-03-07 17:54:44 +000048class DeclDirectiveListParserHelper final {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000049 SmallVector<Expr *, 4> Identifiers;
50 Parser *P;
Alexey Bataev25ed0c02019-03-07 17:54:44 +000051 OpenMPDirectiveKind Kind;
Dmitry Polukhind69b5052016-05-09 14:59:13 +000052
53public:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000054 DeclDirectiveListParserHelper(Parser *P, OpenMPDirectiveKind Kind)
55 : P(P), Kind(Kind) {}
Dmitry Polukhind69b5052016-05-09 14:59:13 +000056 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
Alexey Bataev25ed0c02019-03-07 17:54:44 +000057 ExprResult Res = P->getActions().ActOnOpenMPIdExpression(
58 P->getCurScope(), SS, NameInfo, Kind);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000059 if (Res.isUsable())
60 Identifiers.push_back(Res.get());
61 }
62 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
63};
Dmitry Polukhin82478332016-02-13 06:53:38 +000064} // namespace
65
66// Map token string to extended OMP token kind that are
67// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
68static unsigned getOpenMPDirectiveKindEx(StringRef S) {
69 auto DKind = getOpenMPDirectiveKind(S);
70 if (DKind != OMPD_unknown)
71 return DKind;
72
73 return llvm::StringSwitch<unsigned>(S)
74 .Case("cancellation", OMPD_cancellation)
75 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000076 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000077 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000078 .Case("enter", OMPD_enter)
79 .Case("exit", OMPD_exit)
80 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000081 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000082 .Case("update", OMPD_update)
Michael Kruse251e1482019-02-01 20:25:04 +000083 .Case("mapper", OMPD_mapper)
Alexey Bataevd158cf62019-09-13 20:18:17 +000084 .Case("variant", OMPD_variant)
Dmitry Polukhin82478332016-02-13 06:53:38 +000085 .Default(OMPD_unknown);
86}
87
Alexey Bataev61908f652018-04-23 19:53:05 +000088static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000089 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
90 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
91 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000092 static const unsigned F[][3] = {
Alexey Bataev61908f652018-04-23 19:53:05 +000093 {OMPD_cancellation, OMPD_point, OMPD_cancellation_point},
94 {OMPD_declare, OMPD_reduction, OMPD_declare_reduction},
Michael Kruse251e1482019-02-01 20:25:04 +000095 {OMPD_declare, OMPD_mapper, OMPD_declare_mapper},
Alexey Bataev61908f652018-04-23 19:53:05 +000096 {OMPD_declare, OMPD_simd, OMPD_declare_simd},
97 {OMPD_declare, OMPD_target, OMPD_declare_target},
Alexey Bataevd158cf62019-09-13 20:18:17 +000098 {OMPD_declare, OMPD_variant, OMPD_declare_variant},
Alexey Bataev61908f652018-04-23 19:53:05 +000099 {OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel},
100 {OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for},
101 {OMPD_distribute_parallel_for, OMPD_simd,
102 OMPD_distribute_parallel_for_simd},
103 {OMPD_distribute, OMPD_simd, OMPD_distribute_simd},
104 {OMPD_end, OMPD_declare, OMPD_end_declare},
105 {OMPD_end_declare, OMPD_target, OMPD_end_declare_target},
106 {OMPD_target, OMPD_data, OMPD_target_data},
107 {OMPD_target, OMPD_enter, OMPD_target_enter},
108 {OMPD_target, OMPD_exit, OMPD_target_exit},
109 {OMPD_target, OMPD_update, OMPD_target_update},
110 {OMPD_target_enter, OMPD_data, OMPD_target_enter_data},
111 {OMPD_target_exit, OMPD_data, OMPD_target_exit_data},
112 {OMPD_for, OMPD_simd, OMPD_for_simd},
113 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
114 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
115 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
116 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd},
117 {OMPD_target, OMPD_parallel, OMPD_target_parallel},
118 {OMPD_target, OMPD_simd, OMPD_target_simd},
119 {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for},
120 {OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd},
121 {OMPD_teams, OMPD_distribute, OMPD_teams_distribute},
122 {OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd},
123 {OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel},
124 {OMPD_teams_distribute_parallel, OMPD_for,
125 OMPD_teams_distribute_parallel_for},
126 {OMPD_teams_distribute_parallel_for, OMPD_simd,
127 OMPD_teams_distribute_parallel_for_simd},
128 {OMPD_target, OMPD_teams, OMPD_target_teams},
129 {OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute},
130 {OMPD_target_teams_distribute, OMPD_parallel,
131 OMPD_target_teams_distribute_parallel},
132 {OMPD_target_teams_distribute, OMPD_simd,
133 OMPD_target_teams_distribute_simd},
134 {OMPD_target_teams_distribute_parallel, OMPD_for,
135 OMPD_target_teams_distribute_parallel_for},
136 {OMPD_target_teams_distribute_parallel_for, OMPD_simd,
137 OMPD_target_teams_distribute_parallel_for_simd}};
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000138 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev61908f652018-04-23 19:53:05 +0000139 Token Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000140 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000141 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000142 ? static_cast<unsigned>(OMPD_unknown)
143 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
144 if (DKind == OMPD_unknown)
145 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000146
Alexey Bataev61908f652018-04-23 19:53:05 +0000147 for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) {
148 if (DKind != F[I][0])
Dmitry Polukhin82478332016-02-13 06:53:38 +0000149 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000150
Dmitry Polukhin82478332016-02-13 06:53:38 +0000151 Tok = P.getPreprocessor().LookAhead(0);
152 unsigned SDKind =
153 Tok.isAnnotation()
154 ? static_cast<unsigned>(OMPD_unknown)
155 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
156 if (SDKind == OMPD_unknown)
157 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000158
Alexey Bataev61908f652018-04-23 19:53:05 +0000159 if (SDKind == F[I][1]) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000160 P.ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000161 DKind = F[I][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000162 }
163 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000164 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
165 : OMPD_unknown;
166}
167
168static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000169 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000170 Sema &Actions = P.getActions();
171 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000172 // Allow to use 'operator' keyword for C++ operators
173 bool WithOperator = false;
174 if (Tok.is(tok::kw_operator)) {
175 P.ConsumeToken();
176 Tok = P.getCurToken();
177 WithOperator = true;
178 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000179 switch (Tok.getKind()) {
180 case tok::plus: // '+'
181 OOK = OO_Plus;
182 break;
183 case tok::minus: // '-'
184 OOK = OO_Minus;
185 break;
186 case tok::star: // '*'
187 OOK = OO_Star;
188 break;
189 case tok::amp: // '&'
190 OOK = OO_Amp;
191 break;
192 case tok::pipe: // '|'
193 OOK = OO_Pipe;
194 break;
195 case tok::caret: // '^'
196 OOK = OO_Caret;
197 break;
198 case tok::ampamp: // '&&'
199 OOK = OO_AmpAmp;
200 break;
201 case tok::pipepipe: // '||'
202 OOK = OO_PipePipe;
203 break;
204 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000205 if (!WithOperator)
206 break;
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000207 LLVM_FALLTHROUGH;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000208 default:
209 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
210 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
211 Parser::StopBeforeMatch);
212 return DeclarationName();
213 }
214 P.ConsumeToken();
215 auto &DeclNames = Actions.getASTContext().DeclarationNames;
216 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
217 : DeclNames.getCXXOperatorName(OOK);
218}
219
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000220/// Parse 'omp declare reduction' construct.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000221///
222/// declare-reduction-directive:
223/// annot_pragma_openmp 'declare' 'reduction'
224/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
225/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
226/// annot_pragma_openmp_end
227/// <reduction_id> is either a base language identifier or one of the following
228/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
229///
230Parser::DeclGroupPtrTy
231Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
232 // Parse '('.
233 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
234 if (T.expectAndConsume(diag::err_expected_lparen_after,
235 getOpenMPDirectiveName(OMPD_declare_reduction))) {
236 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
237 return DeclGroupPtrTy();
238 }
239
240 DeclarationName Name = parseOpenMPReductionId(*this);
241 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
242 return DeclGroupPtrTy();
243
244 // Consume ':'.
245 bool IsCorrect = !ExpectAndConsume(tok::colon);
246
247 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
248 return DeclGroupPtrTy();
249
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000250 IsCorrect = IsCorrect && !Name.isEmpty();
251
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000252 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
253 Diag(Tok.getLocation(), diag::err_expected_type);
254 IsCorrect = false;
255 }
256
257 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
258 return DeclGroupPtrTy();
259
260 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
261 // Parse list of types until ':' token.
262 do {
263 ColonProtectionRAIIObject ColonRAII(*this);
264 SourceRange Range;
Faisal Vali421b2d12017-12-29 05:41:00 +0000265 TypeResult TR =
266 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000267 if (TR.isUsable()) {
Alexey Bataev61908f652018-04-23 19:53:05 +0000268 QualType ReductionType =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000269 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
270 if (!ReductionType.isNull()) {
271 ReductionTypes.push_back(
272 std::make_pair(ReductionType, Range.getBegin()));
273 }
274 } else {
275 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
276 StopBeforeMatch);
277 }
278
279 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
280 break;
281
282 // Consume ','.
283 if (ExpectAndConsume(tok::comma)) {
284 IsCorrect = false;
285 if (Tok.is(tok::annot_pragma_openmp_end)) {
286 Diag(Tok.getLocation(), diag::err_expected_type);
287 return DeclGroupPtrTy();
288 }
289 }
290 } while (Tok.isNot(tok::annot_pragma_openmp_end));
291
292 if (ReductionTypes.empty()) {
293 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
294 return DeclGroupPtrTy();
295 }
296
297 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
298 return DeclGroupPtrTy();
299
300 // Consume ':'.
301 if (ExpectAndConsume(tok::colon))
302 IsCorrect = false;
303
304 if (Tok.is(tok::annot_pragma_openmp_end)) {
305 Diag(Tok.getLocation(), diag::err_expected_expression);
306 return DeclGroupPtrTy();
307 }
308
309 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
310 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
311
312 // Parse <combiner> expression and then parse initializer if any for each
313 // correct type.
314 unsigned I = 0, E = ReductionTypes.size();
Alexey Bataev61908f652018-04-23 19:53:05 +0000315 for (Decl *D : DRD.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000316 TentativeParsingAction TPA(*this);
317 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000318 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000319 Scope::OpenMPDirectiveScope);
320 // Parse <combiner> expression.
321 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
322 ExprResult CombinerResult =
323 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000324 D->getLocation(), /*DiscardedValue*/ false);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000325 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
326
327 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
328 Tok.isNot(tok::annot_pragma_openmp_end)) {
329 TPA.Commit();
330 IsCorrect = false;
331 break;
332 }
333 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
334 ExprResult InitializerResult;
335 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
336 // Parse <initializer> expression.
337 if (Tok.is(tok::identifier) &&
Alexey Bataev61908f652018-04-23 19:53:05 +0000338 Tok.getIdentifierInfo()->isStr("initializer")) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000339 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000340 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000341 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
342 TPA.Commit();
343 IsCorrect = false;
344 break;
345 }
346 // Parse '('.
347 BalancedDelimiterTracker T(*this, tok::l_paren,
348 tok::annot_pragma_openmp_end);
349 IsCorrect =
350 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
351 IsCorrect;
352 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
353 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000354 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000355 Scope::OpenMPDirectiveScope);
356 // Parse expression.
Alexey Bataev070f43a2017-09-06 14:49:58 +0000357 VarDecl *OmpPrivParm =
358 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
359 D);
360 // Check if initializer is omp_priv <init_expr> or something else.
361 if (Tok.is(tok::identifier) &&
362 Tok.getIdentifierInfo()->isStr("omp_priv")) {
Alexey Bataeve6aa4692018-09-13 16:54:05 +0000363 if (Actions.getLangOpts().CPlusPlus) {
364 InitializerResult = Actions.ActOnFinishFullExpr(
365 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000366 /*DiscardedValue*/ false);
Alexey Bataeve6aa4692018-09-13 16:54:05 +0000367 } else {
368 ConsumeToken();
369 ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
370 }
Alexey Bataev070f43a2017-09-06 14:49:58 +0000371 } else {
372 InitializerResult = Actions.ActOnFinishFullExpr(
373 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000374 /*DiscardedValue*/ false);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000375 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000376 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
Alexey Bataev070f43a2017-09-06 14:49:58 +0000377 D, InitializerResult.get(), OmpPrivParm);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000378 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
379 Tok.isNot(tok::annot_pragma_openmp_end)) {
380 TPA.Commit();
381 IsCorrect = false;
382 break;
383 }
384 IsCorrect =
385 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
386 }
387 }
388
389 ++I;
390 // Revert parsing if not the last type, otherwise accept it, we're done with
391 // parsing.
392 if (I != E)
393 TPA.Revert();
394 else
395 TPA.Commit();
396 }
397 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
398 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000399}
400
Alexey Bataev070f43a2017-09-06 14:49:58 +0000401void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
402 // Parse declarator '=' initializer.
403 // If a '==' or '+=' is found, suggest a fixit to '='.
404 if (isTokenEqualOrEqualTypo()) {
405 ConsumeToken();
406
407 if (Tok.is(tok::code_completion)) {
408 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
409 Actions.FinalizeDeclaration(OmpPrivParm);
410 cutOffParsing();
411 return;
412 }
413
414 ExprResult Init(ParseInitializer());
415
416 if (Init.isInvalid()) {
417 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
418 Actions.ActOnInitializerError(OmpPrivParm);
419 } else {
420 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
421 /*DirectInit=*/false);
422 }
423 } else if (Tok.is(tok::l_paren)) {
424 // Parse C++ direct initializer: '(' expression-list ')'
425 BalancedDelimiterTracker T(*this, tok::l_paren);
426 T.consumeOpen();
427
428 ExprVector Exprs;
429 CommaLocsTy CommaLocs;
430
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000431 SourceLocation LParLoc = T.getOpenLocation();
Ilya Biryukovff2a9972019-02-26 11:01:50 +0000432 auto RunSignatureHelp = [this, OmpPrivParm, LParLoc, &Exprs]() {
433 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
434 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
435 OmpPrivParm->getLocation(), Exprs, LParLoc);
436 CalledSignatureHelp = true;
437 return PreferredType;
438 };
439 if (ParseExpressionList(Exprs, CommaLocs, [&] {
440 PreferredType.enterFunctionArgument(Tok.getLocation(),
441 RunSignatureHelp);
442 })) {
443 if (PP.isCodeCompletionReached() && !CalledSignatureHelp)
444 RunSignatureHelp();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000445 Actions.ActOnInitializerError(OmpPrivParm);
446 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
447 } else {
448 // Match the ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000449 SourceLocation RLoc = Tok.getLocation();
450 if (!T.consumeClose())
451 RLoc = T.getCloseLocation();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000452
453 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
454 "Unexpected number of commas!");
455
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000456 ExprResult Initializer =
457 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000458 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
459 /*DirectInit=*/true);
460 }
461 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
462 // Parse C++0x braced-init-list.
463 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
464
465 ExprResult Init(ParseBraceInitializer());
466
467 if (Init.isInvalid()) {
468 Actions.ActOnInitializerError(OmpPrivParm);
469 } else {
470 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
471 /*DirectInit=*/true);
472 }
473 } else {
474 Actions.ActOnUninitializedDecl(OmpPrivParm);
475 }
476}
477
Michael Kruse251e1482019-02-01 20:25:04 +0000478/// Parses 'omp declare mapper' directive.
479///
480/// declare-mapper-directive:
481/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifier> ':']
482/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
483/// annot_pragma_openmp_end
484/// <mapper-identifier> and <var> are base language identifiers.
485///
486Parser::DeclGroupPtrTy
487Parser::ParseOpenMPDeclareMapperDirective(AccessSpecifier AS) {
488 bool IsCorrect = true;
489 // Parse '('
490 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
491 if (T.expectAndConsume(diag::err_expected_lparen_after,
492 getOpenMPDirectiveName(OMPD_declare_mapper))) {
493 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
494 return DeclGroupPtrTy();
495 }
496
497 // Parse <mapper-identifier>
498 auto &DeclNames = Actions.getASTContext().DeclarationNames;
499 DeclarationName MapperId;
500 if (PP.LookAhead(0).is(tok::colon)) {
501 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
502 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
503 IsCorrect = false;
504 } else {
505 MapperId = DeclNames.getIdentifier(Tok.getIdentifierInfo());
506 }
507 ConsumeToken();
508 // Consume ':'.
509 ExpectAndConsume(tok::colon);
510 } else {
511 // If no mapper identifier is provided, its name is "default" by default
512 MapperId =
513 DeclNames.getIdentifier(&Actions.getASTContext().Idents.get("default"));
514 }
515
516 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
517 return DeclGroupPtrTy();
518
519 // Parse <type> <var>
520 DeclarationName VName;
521 QualType MapperType;
522 SourceRange Range;
523 TypeResult ParsedType = parseOpenMPDeclareMapperVarDecl(Range, VName, AS);
524 if (ParsedType.isUsable())
525 MapperType =
526 Actions.ActOnOpenMPDeclareMapperType(Range.getBegin(), ParsedType);
527 if (MapperType.isNull())
528 IsCorrect = false;
529 if (!IsCorrect) {
530 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
531 return DeclGroupPtrTy();
532 }
533
534 // Consume ')'.
535 IsCorrect &= !T.consumeClose();
536 if (!IsCorrect) {
537 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
538 return DeclGroupPtrTy();
539 }
540
541 // Enter scope.
542 OMPDeclareMapperDecl *DMD = Actions.ActOnOpenMPDeclareMapperDirectiveStart(
543 getCurScope(), Actions.getCurLexicalContext(), MapperId, MapperType,
544 Range.getBegin(), VName, AS);
545 DeclarationNameInfo DirName;
546 SourceLocation Loc = Tok.getLocation();
547 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
548 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
549 ParseScope OMPDirectiveScope(this, ScopeFlags);
550 Actions.StartOpenMPDSABlock(OMPD_declare_mapper, DirName, getCurScope(), Loc);
551
552 // Add the mapper variable declaration.
553 Actions.ActOnOpenMPDeclareMapperDirectiveVarDecl(
554 DMD, getCurScope(), MapperType, Range.getBegin(), VName);
555
556 // Parse map clauses.
557 SmallVector<OMPClause *, 6> Clauses;
558 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
559 OpenMPClauseKind CKind = Tok.isAnnotation()
560 ? OMPC_unknown
561 : getOpenMPClauseKind(PP.getSpelling(Tok));
562 Actions.StartOpenMPClause(CKind);
563 OMPClause *Clause =
564 ParseOpenMPClause(OMPD_declare_mapper, CKind, Clauses.size() == 0);
565 if (Clause)
566 Clauses.push_back(Clause);
567 else
568 IsCorrect = false;
569 // Skip ',' if any.
570 if (Tok.is(tok::comma))
571 ConsumeToken();
572 Actions.EndOpenMPClause();
573 }
574 if (Clauses.empty()) {
575 Diag(Tok, diag::err_omp_expected_clause)
576 << getOpenMPDirectiveName(OMPD_declare_mapper);
577 IsCorrect = false;
578 }
579
580 // Exit scope.
581 Actions.EndOpenMPDSABlock(nullptr);
582 OMPDirectiveScope.Exit();
583
584 DeclGroupPtrTy DGP =
585 Actions.ActOnOpenMPDeclareMapperDirectiveEnd(DMD, getCurScope(), Clauses);
586 if (!IsCorrect)
587 return DeclGroupPtrTy();
588 return DGP;
589}
590
591TypeResult Parser::parseOpenMPDeclareMapperVarDecl(SourceRange &Range,
592 DeclarationName &Name,
593 AccessSpecifier AS) {
594 // Parse the common declaration-specifiers piece.
595 Parser::DeclSpecContext DSC = Parser::DeclSpecContext::DSC_type_specifier;
596 DeclSpec DS(AttrFactory);
597 ParseSpecifierQualifierList(DS, AS, DSC);
598
599 // Parse the declarator.
600 DeclaratorContext Context = DeclaratorContext::PrototypeContext;
601 Declarator DeclaratorInfo(DS, Context);
602 ParseDeclarator(DeclaratorInfo);
603 Range = DeclaratorInfo.getSourceRange();
604 if (DeclaratorInfo.getIdentifier() == nullptr) {
605 Diag(Tok.getLocation(), diag::err_omp_mapper_expected_declarator);
606 return true;
607 }
608 Name = Actions.GetNameForDeclarator(DeclaratorInfo).getName();
609
610 return Actions.ActOnOpenMPDeclareMapperVarDecl(getCurScope(), DeclaratorInfo);
611}
612
Alexey Bataev2af33e32016-04-07 12:45:37 +0000613namespace {
614/// RAII that recreates function context for correct parsing of clauses of
615/// 'declare simd' construct.
616/// OpenMP, 2.8.2 declare simd Construct
617/// The expressions appearing in the clauses of this directive are evaluated in
618/// the scope of the arguments of the function declaration or definition.
619class FNContextRAII final {
620 Parser &P;
621 Sema::CXXThisScopeRAII *ThisScope;
622 Parser::ParseScope *TempScope;
623 Parser::ParseScope *FnScope;
624 bool HasTemplateScope = false;
625 bool HasFunScope = false;
626 FNContextRAII() = delete;
627 FNContextRAII(const FNContextRAII &) = delete;
628 FNContextRAII &operator=(const FNContextRAII &) = delete;
629
630public:
631 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
632 Decl *D = *Ptr.get().begin();
633 NamedDecl *ND = dyn_cast<NamedDecl>(D);
634 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
635 Sema &Actions = P.getActions();
636
637 // Allow 'this' within late-parsed attributes.
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000638 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, Qualifiers(),
Alexey Bataev2af33e32016-04-07 12:45:37 +0000639 ND && ND->isCXXInstanceMember());
640
641 // If the Decl is templatized, add template parameters to scope.
642 HasTemplateScope = D->isTemplateDecl();
643 TempScope =
644 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
645 if (HasTemplateScope)
646 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
647
648 // If the Decl is on a function, add function parameters to the scope.
649 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000650 FnScope = new Parser::ParseScope(
651 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
652 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000653 if (HasFunScope)
654 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
655 }
656 ~FNContextRAII() {
657 if (HasFunScope) {
658 P.getActions().ActOnExitFunctionContext();
659 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
660 }
661 if (HasTemplateScope)
662 TempScope->Exit();
663 delete FnScope;
664 delete TempScope;
665 delete ThisScope;
666 }
667};
668} // namespace
669
Alexey Bataevd93d3762016-04-12 09:35:56 +0000670/// Parses clauses for 'declare simd' directive.
671/// clause:
672/// 'inbranch' | 'notinbranch'
673/// 'simdlen' '(' <expr> ')'
674/// { 'uniform' '(' <argument_list> ')' }
675/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000676/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
677static bool parseDeclareSimdClauses(
678 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
679 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
680 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
681 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000682 SourceRange BSRange;
683 const Token &Tok = P.getCurToken();
684 bool IsError = false;
685 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
686 if (Tok.isNot(tok::identifier))
687 break;
688 OMPDeclareSimdDeclAttr::BranchStateTy Out;
689 IdentifierInfo *II = Tok.getIdentifierInfo();
690 StringRef ClauseName = II->getName();
691 // Parse 'inranch|notinbranch' clauses.
692 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
693 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
694 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
695 << ClauseName
696 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
697 IsError = true;
698 }
699 BS = Out;
700 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
701 P.ConsumeToken();
702 } else if (ClauseName.equals("simdlen")) {
703 if (SimdLen.isUsable()) {
704 P.Diag(Tok, diag::err_omp_more_one_clause)
705 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
706 IsError = true;
707 }
708 P.ConsumeToken();
709 SourceLocation RLoc;
710 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
711 if (SimdLen.isInvalid())
712 IsError = true;
713 } else {
714 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000715 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
716 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000717 Parser::OpenMPVarListDataTy Data;
Alexey Bataev61908f652018-04-23 19:53:05 +0000718 SmallVectorImpl<Expr *> *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000719 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000720 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000721 else if (CKind == OMPC_linear)
722 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000723
724 P.ConsumeToken();
725 if (P.ParseOpenMPVarList(OMPD_declare_simd,
726 getOpenMPClauseKind(ClauseName), *Vars, Data))
727 IsError = true;
Alexey Bataev61908f652018-04-23 19:53:05 +0000728 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000729 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataev61908f652018-04-23 19:53:05 +0000730 } else if (CKind == OMPC_linear) {
Alexey Bataevecba70f2016-04-12 11:02:11 +0000731 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
732 Data.DepLinMapLoc))
733 Data.LinKind = OMPC_LINEAR_val;
734 LinModifiers.append(Linears.size() - LinModifiers.size(),
735 Data.LinKind);
736 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
737 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000738 } else
739 // TODO: add parsing of other clauses.
740 break;
741 }
742 // Skip ',' if any.
743 if (Tok.is(tok::comma))
744 P.ConsumeToken();
745 }
746 return IsError;
747}
748
Alexey Bataev2af33e32016-04-07 12:45:37 +0000749/// Parse clauses for '#pragma omp declare simd'.
750Parser::DeclGroupPtrTy
751Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
752 CachedTokens &Toks, SourceLocation Loc) {
Ilya Biryukov929af672019-05-17 09:32:05 +0000753 PP.EnterToken(Tok, /*IsReinject*/ true);
754 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
755 /*IsReinject*/ true);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000756 // Consume the previously pushed token.
757 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Alexey Bataevd158cf62019-09-13 20:18:17 +0000758 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000759
760 FNContextRAII FnContext(*this, Ptr);
761 OMPDeclareSimdDeclAttr::BranchStateTy BS =
762 OMPDeclareSimdDeclAttr::BS_Undefined;
763 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000764 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000765 SmallVector<Expr *, 4> Aligneds;
766 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000767 SmallVector<Expr *, 4> Linears;
768 SmallVector<unsigned, 4> LinModifiers;
769 SmallVector<Expr *, 4> Steps;
770 bool IsError =
771 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
772 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000773 // Need to check for extra tokens.
774 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
775 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
776 << getOpenMPDirectiveName(OMPD_declare_simd);
777 while (Tok.isNot(tok::annot_pragma_openmp_end))
778 ConsumeAnyToken();
779 }
780 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000781 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000782 if (IsError)
783 return Ptr;
784 return Actions.ActOnOpenMPDeclareSimdDirective(
785 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
786 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataev20dfd772016-04-04 10:12:15 +0000787}
788
Alexey Bataevd158cf62019-09-13 20:18:17 +0000789/// Parses clauses for 'declare variant' directive.
790/// clause:
791/// 'match' '('
792/// <selector_set_name> '=' '{' <context_selectors> '}'
793/// ')'
794static bool parseDeclareVariantClause(Parser &P) {
795 Token Tok = P.getCurToken();
796 // Parse 'match'.
797 if (!Tok.is(tok::identifier) ||
798 P.getPreprocessor().getSpelling(Tok).compare("match")) {
799 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_wrong_clause)
800 << "match";
801 while (!P.SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
802 ;
803 return true;
804 }
805 (void)P.ConsumeToken();
806 // Parse '('.
807 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
808 if (T.expectAndConsume(diag::err_expected_lparen_after, "match"))
809 return true;
810 // Parse inner context selector.
811 Tok = P.getCurToken();
812 if (!Tok.is(tok::identifier)) {
813 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_no_ctx_selector)
814 << "match";
815 return true;
816 }
817 SmallString<16> Buffer;
818 StringRef CtxSelectorName = P.getPreprocessor().getSpelling(Tok, Buffer);
819 // Parse '='.
820 (void)P.ConsumeToken();
821 Tok = P.getCurToken();
822 if (Tok.isNot(tok::equal)) {
823 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_equal_expected)
824 << CtxSelectorName;
825 return true;
826 }
827 (void)P.ConsumeToken();
828 // Unknown selector - just ignore it completely.
829 {
830 // Parse '{'.
831 BalancedDelimiterTracker TBr(P, tok::l_brace, tok::annot_pragma_openmp_end);
832 if (TBr.expectAndConsume(diag::err_expected_lbrace_after, "="))
833 return true;
834 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
835 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
836 ;
837 // Parse '}'.
838 (void)TBr.consumeClose();
839 }
840 // Parse ')'.
841 (void)T.consumeClose();
842 // TBD: add parsing of known context selectors.
843 return false;
844}
845
846/// Parse clauses for '#pragma omp declare variant ( variant-func-id ) clause'.
847Parser::DeclGroupPtrTy
848Parser::ParseOMPDeclareVariantClauses(Parser::DeclGroupPtrTy Ptr,
849 CachedTokens &Toks, SourceLocation Loc) {
850 PP.EnterToken(Tok, /*IsReinject*/ true);
851 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
852 /*IsReinject*/ true);
853 // Consume the previously pushed token.
854 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
855 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
856
857 FNContextRAII FnContext(*this, Ptr);
858 // Parse function declaration id.
859 SourceLocation RLoc;
860 // Parse with IsAddressOfOperand set to true to parse methods as DeclRefExprs
861 // instead of MemberExprs.
862 ExprResult AssociatedFunction =
863 ParseOpenMPParensExpr(getOpenMPDirectiveName(OMPD_declare_variant), RLoc,
864 /*IsAddressOfOperand=*/true);
865 if (!AssociatedFunction.isUsable()) {
866 if (!Tok.is(tok::annot_pragma_openmp_end))
867 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
868 ;
869 // Skip the last annot_pragma_openmp_end.
870 (void)ConsumeAnnotationToken();
871 return Ptr;
872 }
873
874 bool IsError = parseDeclareVariantClause(*this);
875 // Need to check for extra tokens.
876 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
877 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
878 << getOpenMPDirectiveName(OMPD_declare_variant);
879 while (Tok.isNot(tok::annot_pragma_openmp_end))
880 ConsumeAnyToken();
881 }
882 // Skip the last annot_pragma_openmp_end.
883 SourceLocation EndLoc = ConsumeAnnotationToken();
884 if (IsError)
885 return Ptr;
886 return Actions.ActOnOpenMPDeclareVariantDirective(
887 Ptr, AssociatedFunction.get(), SourceRange(Loc, EndLoc));
888}
889
Alexey Bataev729e2422019-08-23 16:11:14 +0000890/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
891///
892/// default-clause:
893/// 'default' '(' 'none' | 'shared' ')
894///
895/// proc_bind-clause:
896/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
897///
898/// device_type-clause:
899/// 'device_type' '(' 'host' | 'nohost' | 'any' )'
900namespace {
901 struct SimpleClauseData {
902 unsigned Type;
903 SourceLocation Loc;
904 SourceLocation LOpen;
905 SourceLocation TypeLoc;
906 SourceLocation RLoc;
907 SimpleClauseData(unsigned Type, SourceLocation Loc, SourceLocation LOpen,
908 SourceLocation TypeLoc, SourceLocation RLoc)
909 : Type(Type), Loc(Loc), LOpen(LOpen), TypeLoc(TypeLoc), RLoc(RLoc) {}
910 };
911} // anonymous namespace
912
913static Optional<SimpleClauseData>
914parseOpenMPSimpleClause(Parser &P, OpenMPClauseKind Kind) {
915 const Token &Tok = P.getCurToken();
916 SourceLocation Loc = Tok.getLocation();
917 SourceLocation LOpen = P.ConsumeToken();
918 // Parse '('.
919 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
920 if (T.expectAndConsume(diag::err_expected_lparen_after,
921 getOpenMPClauseName(Kind)))
922 return llvm::None;
923
924 unsigned Type = getOpenMPSimpleClauseType(
925 Kind, Tok.isAnnotation() ? "" : P.getPreprocessor().getSpelling(Tok));
926 SourceLocation TypeLoc = Tok.getLocation();
927 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
928 Tok.isNot(tok::annot_pragma_openmp_end))
929 P.ConsumeAnyToken();
930
931 // Parse ')'.
932 SourceLocation RLoc = Tok.getLocation();
933 if (!T.consumeClose())
934 RLoc = T.getCloseLocation();
935
936 return SimpleClauseData(Type, Loc, LOpen, TypeLoc, RLoc);
937}
938
Kelvin Lie0502752018-11-21 20:15:57 +0000939Parser::DeclGroupPtrTy Parser::ParseOMPDeclareTargetClauses() {
940 // OpenMP 4.5 syntax with list of entities.
941 Sema::NamedDeclSetType SameDirectiveDecls;
Alexey Bataev729e2422019-08-23 16:11:14 +0000942 SmallVector<std::tuple<OMPDeclareTargetDeclAttr::MapTypeTy, SourceLocation,
943 NamedDecl *>,
944 4>
945 DeclareTargetDecls;
946 OMPDeclareTargetDeclAttr::DevTypeTy DT = OMPDeclareTargetDeclAttr::DT_Any;
947 SourceLocation DeviceTypeLoc;
Kelvin Lie0502752018-11-21 20:15:57 +0000948 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
949 OMPDeclareTargetDeclAttr::MapTypeTy MT = OMPDeclareTargetDeclAttr::MT_To;
950 if (Tok.is(tok::identifier)) {
951 IdentifierInfo *II = Tok.getIdentifierInfo();
952 StringRef ClauseName = II->getName();
Alexey Bataev729e2422019-08-23 16:11:14 +0000953 bool IsDeviceTypeClause =
954 getLangOpts().OpenMP >= 50 &&
955 getOpenMPClauseKind(ClauseName) == OMPC_device_type;
956 // Parse 'to|link|device_type' clauses.
957 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName, MT) &&
958 !IsDeviceTypeClause) {
959 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
960 << ClauseName << (getLangOpts().OpenMP >= 50 ? 1 : 0);
Kelvin Lie0502752018-11-21 20:15:57 +0000961 break;
962 }
Alexey Bataev729e2422019-08-23 16:11:14 +0000963 // Parse 'device_type' clause and go to next clause if any.
964 if (IsDeviceTypeClause) {
965 Optional<SimpleClauseData> DevTypeData =
966 parseOpenMPSimpleClause(*this, OMPC_device_type);
967 if (DevTypeData.hasValue()) {
968 if (DeviceTypeLoc.isValid()) {
969 // We already saw another device_type clause, diagnose it.
970 Diag(DevTypeData.getValue().Loc,
971 diag::warn_omp_more_one_device_type_clause);
972 }
973 switch(static_cast<OpenMPDeviceType>(DevTypeData.getValue().Type)) {
974 case OMPC_DEVICE_TYPE_any:
975 DT = OMPDeclareTargetDeclAttr::DT_Any;
976 break;
977 case OMPC_DEVICE_TYPE_host:
978 DT = OMPDeclareTargetDeclAttr::DT_Host;
979 break;
980 case OMPC_DEVICE_TYPE_nohost:
981 DT = OMPDeclareTargetDeclAttr::DT_NoHost;
982 break;
983 case OMPC_DEVICE_TYPE_unknown:
984 llvm_unreachable("Unexpected device_type");
985 }
986 DeviceTypeLoc = DevTypeData.getValue().Loc;
987 }
988 continue;
989 }
Kelvin Lie0502752018-11-21 20:15:57 +0000990 ConsumeToken();
991 }
Alexey Bataev729e2422019-08-23 16:11:14 +0000992 auto &&Callback = [this, MT, &DeclareTargetDecls, &SameDirectiveDecls](
993 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
994 NamedDecl *ND = Actions.lookupOpenMPDeclareTargetName(
995 getCurScope(), SS, NameInfo, SameDirectiveDecls);
996 if (ND)
997 DeclareTargetDecls.emplace_back(MT, NameInfo.getLoc(), ND);
Kelvin Lie0502752018-11-21 20:15:57 +0000998 };
999 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
1000 /*AllowScopeSpecifier=*/true))
1001 break;
1002
1003 // Consume optional ','.
1004 if (Tok.is(tok::comma))
1005 ConsumeToken();
1006 }
1007 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1008 ConsumeAnyToken();
Alexey Bataev729e2422019-08-23 16:11:14 +00001009 for (auto &MTLocDecl : DeclareTargetDecls) {
1010 OMPDeclareTargetDeclAttr::MapTypeTy MT;
1011 SourceLocation Loc;
1012 NamedDecl *ND;
1013 std::tie(MT, Loc, ND) = MTLocDecl;
1014 // device_type clause is applied only to functions.
1015 Actions.ActOnOpenMPDeclareTargetName(
1016 ND, Loc, MT, isa<VarDecl>(ND) ? OMPDeclareTargetDeclAttr::DT_Any : DT);
1017 }
Kelvin Lie0502752018-11-21 20:15:57 +00001018 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
1019 SameDirectiveDecls.end());
1020 if (Decls.empty())
1021 return DeclGroupPtrTy();
1022 return Actions.BuildDeclaratorGroup(Decls);
1023}
1024
1025void Parser::ParseOMPEndDeclareTargetDirective(OpenMPDirectiveKind DKind,
1026 SourceLocation DTLoc) {
1027 if (DKind != OMPD_end_declare_target) {
1028 Diag(Tok, diag::err_expected_end_declare_target);
1029 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
1030 return;
1031 }
1032 ConsumeAnyToken();
1033 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1034 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1035 << getOpenMPDirectiveName(OMPD_end_declare_target);
1036 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1037 }
1038 // Skip the last annot_pragma_openmp_end.
1039 ConsumeAnyToken();
1040}
1041
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001042/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001043///
1044/// threadprivate-directive:
1045/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001046/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +00001047///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001048/// allocate-directive:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001049/// annot_pragma_openmp 'allocate' simple-variable-list [<clause>]
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001050/// annot_pragma_openmp_end
1051///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001052/// declare-reduction-directive:
1053/// annot_pragma_openmp 'declare' 'reduction' [...]
1054/// annot_pragma_openmp_end
1055///
Michael Kruse251e1482019-02-01 20:25:04 +00001056/// declare-mapper-directive:
1057/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1058/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1059/// annot_pragma_openmp_end
1060///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001061/// declare-simd-directive:
1062/// annot_pragma_openmp 'declare simd' {<clause> [,]}
1063/// annot_pragma_openmp_end
1064/// <function declaration/definition>
1065///
Kelvin Li1408f912018-09-26 04:28:39 +00001066/// requires directive:
1067/// annot_pragma_openmp 'requires' <clause> [[[,] <clause>] ... ]
1068/// annot_pragma_openmp_end
1069///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001070Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
1071 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
1072 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001073 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +00001074 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +00001075
Richard Smithaf3b3252017-05-18 19:21:48 +00001076 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001077 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001078
1079 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001080 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +00001081 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001082 DeclDirectiveListParserHelper Helper(this, DKind);
1083 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1084 /*AllowScopeSpecifier=*/true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001085 // The last seen token is annot_pragma_openmp_end - need to check for
1086 // extra tokens.
1087 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1088 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001089 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001090 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +00001091 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001092 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001093 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001094 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
1095 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +00001096 }
1097 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001098 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001099 case OMPD_allocate: {
1100 ConsumeToken();
1101 DeclDirectiveListParserHelper Helper(this, DKind);
1102 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1103 /*AllowScopeSpecifier=*/true)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001104 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001105 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001106 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1107 OMPC_unknown + 1>
1108 FirstClauses(OMPC_unknown + 1);
1109 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1110 OpenMPClauseKind CKind =
1111 Tok.isAnnotation() ? OMPC_unknown
1112 : getOpenMPClauseKind(PP.getSpelling(Tok));
1113 Actions.StartOpenMPClause(CKind);
1114 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1115 !FirstClauses[CKind].getInt());
1116 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1117 StopBeforeMatch);
1118 FirstClauses[CKind].setInt(true);
1119 if (Clause != nullptr)
1120 Clauses.push_back(Clause);
1121 if (Tok.is(tok::annot_pragma_openmp_end)) {
1122 Actions.EndOpenMPClause();
1123 break;
1124 }
1125 // Skip ',' if any.
1126 if (Tok.is(tok::comma))
1127 ConsumeToken();
1128 Actions.EndOpenMPClause();
1129 }
1130 // The last seen token is annot_pragma_openmp_end - need to check for
1131 // extra tokens.
1132 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1133 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1134 << getOpenMPDirectiveName(DKind);
1135 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1136 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001137 }
1138 // Skip the last annot_pragma_openmp_end.
1139 ConsumeAnnotationToken();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001140 return Actions.ActOnOpenMPAllocateDirective(Loc, Helper.getIdentifiers(),
1141 Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001142 }
1143 break;
1144 }
Kelvin Li1408f912018-09-26 04:28:39 +00001145 case OMPD_requires: {
1146 SourceLocation StartLoc = ConsumeToken();
1147 SmallVector<OMPClause *, 5> Clauses;
1148 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
1149 FirstClauses(OMPC_unknown + 1);
1150 if (Tok.is(tok::annot_pragma_openmp_end)) {
Ilya Biryukovff2a9972019-02-26 11:01:50 +00001151 Diag(Tok, diag::err_omp_expected_clause)
Kelvin Li1408f912018-09-26 04:28:39 +00001152 << getOpenMPDirectiveName(OMPD_requires);
1153 break;
1154 }
1155 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1156 OpenMPClauseKind CKind = Tok.isAnnotation()
1157 ? OMPC_unknown
1158 : getOpenMPClauseKind(PP.getSpelling(Tok));
1159 Actions.StartOpenMPClause(CKind);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001160 OMPClause *Clause = ParseOpenMPClause(OMPD_requires, CKind,
1161 !FirstClauses[CKind].getInt());
1162 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1163 StopBeforeMatch);
Kelvin Li1408f912018-09-26 04:28:39 +00001164 FirstClauses[CKind].setInt(true);
1165 if (Clause != nullptr)
1166 Clauses.push_back(Clause);
1167 if (Tok.is(tok::annot_pragma_openmp_end)) {
1168 Actions.EndOpenMPClause();
1169 break;
1170 }
1171 // Skip ',' if any.
1172 if (Tok.is(tok::comma))
1173 ConsumeToken();
1174 Actions.EndOpenMPClause();
1175 }
1176 // Consume final annot_pragma_openmp_end
1177 if (Clauses.size() == 0) {
1178 Diag(Tok, diag::err_omp_expected_clause)
1179 << getOpenMPDirectiveName(OMPD_requires);
1180 ConsumeAnnotationToken();
1181 return nullptr;
1182 }
1183 ConsumeAnnotationToken();
1184 return Actions.ActOnOpenMPRequiresDirective(StartLoc, Clauses);
1185 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001186 case OMPD_declare_reduction:
1187 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001188 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001189 // The last seen token is annot_pragma_openmp_end - need to check for
1190 // extra tokens.
1191 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1192 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1193 << getOpenMPDirectiveName(OMPD_declare_reduction);
1194 while (Tok.isNot(tok::annot_pragma_openmp_end))
1195 ConsumeAnyToken();
1196 }
1197 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001198 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001199 return Res;
1200 }
1201 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001202 case OMPD_declare_mapper: {
1203 ConsumeToken();
1204 if (DeclGroupPtrTy Res = ParseOpenMPDeclareMapperDirective(AS)) {
1205 // Skip the last annot_pragma_openmp_end.
1206 ConsumeAnnotationToken();
1207 return Res;
1208 }
1209 break;
1210 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001211 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001212 case OMPD_declare_simd: {
1213 // The syntax is:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001214 // { #pragma omp declare {simd|variant} }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001215 // <function-declaration-or-definition>
1216 //
Alexey Bataev2af33e32016-04-07 12:45:37 +00001217 CachedTokens Toks;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001218 Toks.push_back(Tok);
1219 ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001220 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
1221 Toks.push_back(Tok);
1222 ConsumeAnyToken();
1223 }
1224 Toks.push_back(Tok);
1225 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +00001226
1227 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +00001228 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001229 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +00001230 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001231 // Here we expect to see some function declaration.
1232 if (AS == AS_none) {
1233 assert(TagType == DeclSpec::TST_unspecified);
1234 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001235 ParsingDeclSpec PDS(*this);
1236 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1237 } else {
1238 Ptr =
1239 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1240 }
1241 }
1242 if (!Ptr) {
Alexey Bataevd158cf62019-09-13 20:18:17 +00001243 Diag(Loc, diag::err_omp_decl_in_declare_simd_variant)
1244 << (DKind == OMPD_declare_simd ? 0 : 1);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001245 return DeclGroupPtrTy();
1246 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001247 if (DKind == OMPD_declare_simd)
1248 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
1249 assert(DKind == OMPD_declare_variant &&
1250 "Expected declare variant directive only");
1251 return ParseOMPDeclareVariantClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001252 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001253 case OMPD_declare_target: {
1254 SourceLocation DTLoc = ConsumeAnyToken();
1255 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Kelvin Lie0502752018-11-21 20:15:57 +00001256 return ParseOMPDeclareTargetClauses();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001257 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001258
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001259 // Skip the last annot_pragma_openmp_end.
1260 ConsumeAnyToken();
1261
1262 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
1263 return DeclGroupPtrTy();
1264
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001265 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +00001266 DKind = parseOpenMPDirectiveKind(*this);
Kelvin Libc38e632018-09-10 02:07:09 +00001267 while (DKind != OMPD_end_declare_target && Tok.isNot(tok::eof) &&
1268 Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +00001269 DeclGroupPtrTy Ptr;
1270 // Here we expect to see some function declaration.
1271 if (AS == AS_none) {
1272 assert(TagType == DeclSpec::TST_unspecified);
1273 MaybeParseCXX11Attributes(Attrs);
1274 ParsingDeclSpec PDS(*this);
1275 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1276 } else {
1277 Ptr =
1278 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1279 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001280 if (Ptr) {
1281 DeclGroupRef Ref = Ptr.get();
1282 Decls.append(Ref.begin(), Ref.end());
1283 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001284 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
1285 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +00001286 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001287 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001288 if (DKind != OMPD_end_declare_target)
1289 TPA.Revert();
1290 else
1291 TPA.Commit();
1292 }
1293 }
1294
Kelvin Lie0502752018-11-21 20:15:57 +00001295 ParseOMPEndDeclareTargetDirective(DKind, DTLoc);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001296 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +00001297 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001298 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001299 case OMPD_unknown:
1300 Diag(Tok, diag::err_omp_unknown_directive);
1301 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001302 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001303 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001304 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +00001305 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001306 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001307 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001308 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +00001309 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001310 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001311 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001312 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001313 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001314 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +00001315 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001316 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001317 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001318 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001319 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001320 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +00001321 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001322 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001323 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001324 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001325 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +00001326 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001327 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001328 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001329 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001330 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001331 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001332 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001333 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001334 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001335 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001336 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +00001337 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001338 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001339 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001340 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001341 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +00001342 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +00001343 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001344 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +00001345 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +00001346 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +00001347 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +00001348 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +00001349 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +00001350 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001351 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +00001352 break;
1353 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001354 while (Tok.isNot(tok::annot_pragma_openmp_end))
1355 ConsumeAnyToken();
1356 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +00001357 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001358}
1359
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001360/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001361///
1362/// threadprivate-directive:
1363/// annot_pragma_openmp 'threadprivate' simple-variable-list
1364/// annot_pragma_openmp_end
1365///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001366/// allocate-directive:
1367/// annot_pragma_openmp 'allocate' simple-variable-list
1368/// annot_pragma_openmp_end
1369///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001370/// declare-reduction-directive:
1371/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
1372/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
1373/// ('omp_priv' '=' <expression>|<function_call>) ')']
1374/// annot_pragma_openmp_end
1375///
Michael Kruse251e1482019-02-01 20:25:04 +00001376/// declare-mapper-directive:
1377/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1378/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1379/// annot_pragma_openmp_end
1380///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001381/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001382/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001383/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
1384/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001385/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +00001386/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001387/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001388/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +00001389/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +00001390/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +00001391/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +00001392/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +00001393/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +00001394/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +00001395/// 'teams distribute parallel for' | 'target teams' |
1396/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +00001397/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +00001398/// 'target teams distribute parallel for simd' |
1399/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +00001400/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001401///
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001402StmtResult
1403Parser::ParseOpenMPDeclarativeOrExecutableDirective(ParsedStmtContext StmtCtx) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001404 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +00001405 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001406 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001407 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +00001408 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +00001409 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
1410 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +00001411 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +00001412 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001413 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001414 // Name of critical directive.
1415 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001416 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +00001417 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +00001418 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001419
1420 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001421 case OMPD_threadprivate: {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001422 // FIXME: Should this be permitted in C++?
1423 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1424 ParsedStmtContext()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001425 Diag(Tok, diag::err_omp_immediate_directive)
1426 << getOpenMPDirectiveName(DKind) << 0;
1427 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001428 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001429 DeclDirectiveListParserHelper Helper(this, DKind);
1430 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1431 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001432 // The last seen token is annot_pragma_openmp_end - need to check for
1433 // extra tokens.
1434 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1435 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001436 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001437 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001438 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001439 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
1440 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001441 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1442 }
Alp Tokerd751fa72013-12-18 19:10:49 +00001443 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001444 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001445 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001446 case OMPD_allocate: {
1447 // FIXME: Should this be permitted in C++?
1448 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1449 ParsedStmtContext()) {
1450 Diag(Tok, diag::err_omp_immediate_directive)
1451 << getOpenMPDirectiveName(DKind) << 0;
1452 }
1453 ConsumeToken();
1454 DeclDirectiveListParserHelper Helper(this, DKind);
1455 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1456 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001457 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001458 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001459 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1460 OMPC_unknown + 1>
1461 FirstClauses(OMPC_unknown + 1);
1462 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1463 OpenMPClauseKind CKind =
1464 Tok.isAnnotation() ? OMPC_unknown
1465 : getOpenMPClauseKind(PP.getSpelling(Tok));
1466 Actions.StartOpenMPClause(CKind);
1467 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1468 !FirstClauses[CKind].getInt());
1469 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1470 StopBeforeMatch);
1471 FirstClauses[CKind].setInt(true);
1472 if (Clause != nullptr)
1473 Clauses.push_back(Clause);
1474 if (Tok.is(tok::annot_pragma_openmp_end)) {
1475 Actions.EndOpenMPClause();
1476 break;
1477 }
1478 // Skip ',' if any.
1479 if (Tok.is(tok::comma))
1480 ConsumeToken();
1481 Actions.EndOpenMPClause();
1482 }
1483 // The last seen token is annot_pragma_openmp_end - need to check for
1484 // extra tokens.
1485 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1486 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1487 << getOpenMPDirectiveName(DKind);
1488 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1489 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001490 }
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001491 DeclGroupPtrTy Res = Actions.ActOnOpenMPAllocateDirective(
1492 Loc, Helper.getIdentifiers(), Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001493 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1494 }
1495 SkipUntil(tok::annot_pragma_openmp_end);
1496 break;
1497 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001498 case OMPD_declare_reduction:
1499 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001500 if (DeclGroupPtrTy Res =
1501 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001502 // The last seen token is annot_pragma_openmp_end - need to check for
1503 // extra tokens.
1504 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1505 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1506 << getOpenMPDirectiveName(OMPD_declare_reduction);
1507 while (Tok.isNot(tok::annot_pragma_openmp_end))
1508 ConsumeAnyToken();
1509 }
1510 ConsumeAnyToken();
1511 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +00001512 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001513 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +00001514 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001515 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001516 case OMPD_declare_mapper: {
1517 ConsumeToken();
1518 if (DeclGroupPtrTy Res =
1519 ParseOpenMPDeclareMapperDirective(/*AS=*/AS_none)) {
1520 // Skip the last annot_pragma_openmp_end.
1521 ConsumeAnnotationToken();
1522 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1523 } else {
1524 SkipUntil(tok::annot_pragma_openmp_end);
1525 }
1526 break;
1527 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001528 case OMPD_flush:
1529 if (PP.LookAhead(0).is(tok::l_paren)) {
1530 FlushHasClause = true;
1531 // Push copy of the current token back to stream to properly parse
1532 // pseudo-clause OMPFlushClause.
Ilya Biryukov929af672019-05-17 09:32:05 +00001533 PP.EnterToken(Tok, /*IsReinject*/ true);
Alexey Bataev6125da92014-07-21 11:26:11 +00001534 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001535 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +00001536 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001537 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001538 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001539 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001540 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001541 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001542 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +00001543 case OMPD_target_update:
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001544 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1545 ParsedStmtContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00001546 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +00001547 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +00001548 }
1549 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001550 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001551 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001552 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001553 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001554 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001555 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001556 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001557 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001558 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001559 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001560 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001561 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001562 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001563 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001564 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001565 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001566 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001567 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001568 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001569 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001570 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001571 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001572 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001573 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001574 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001575 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001576 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001577 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001578 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001579 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001580 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001581 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001582 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001583 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001584 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001585 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001586 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001587 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001588 case OMPD_target_teams_distribute_parallel_for_simd:
1589 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001590 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001591 // Parse directive name of the 'critical' directive if any.
1592 if (DKind == OMPD_critical) {
1593 BalancedDelimiterTracker T(*this, tok::l_paren,
1594 tok::annot_pragma_openmp_end);
1595 if (!T.consumeOpen()) {
1596 if (Tok.isAnyIdentifier()) {
1597 DirName =
1598 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1599 ConsumeAnyToken();
1600 } else {
1601 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1602 }
1603 T.consumeClose();
1604 }
Alexey Bataev80909872015-07-02 11:25:17 +00001605 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001606 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001607 if (Tok.isNot(tok::annot_pragma_openmp_end))
1608 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001609 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001610
Alexey Bataevf29276e2014-06-18 04:14:57 +00001611 if (isOpenMPLoopDirective(DKind))
1612 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1613 if (isOpenMPSimdDirective(DKind))
1614 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1615 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001616 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001617
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001618 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001619 OpenMPClauseKind CKind =
1620 Tok.isAnnotation()
1621 ? OMPC_unknown
1622 : FlushHasClause ? OMPC_flush
1623 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001624 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001625 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001626 OMPClause *Clause =
1627 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001628 FirstClauses[CKind].setInt(true);
1629 if (Clause) {
1630 FirstClauses[CKind].setPointer(Clause);
1631 Clauses.push_back(Clause);
1632 }
1633
1634 // Skip ',' if any.
1635 if (Tok.is(tok::comma))
1636 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001637 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001638 }
1639 // End location of the directive.
1640 EndLoc = Tok.getLocation();
1641 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001642 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001643
Alexey Bataeveb482352015-12-18 05:05:56 +00001644 // OpenMP [2.13.8, ordered Construct, Syntax]
1645 // If the depend clause is specified, the ordered construct is a stand-alone
1646 // directive.
1647 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001648 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1649 ParsedStmtContext()) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001650 Diag(Loc, diag::err_omp_immediate_directive)
1651 << getOpenMPDirectiveName(DKind) << 1
1652 << getOpenMPClauseName(OMPC_depend);
1653 }
1654 HasAssociatedStatement = false;
1655 }
1656
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001657 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001658 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001659 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001660 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001661 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1662 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1663 // should have at least one compound statement scope within it.
1664 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001665 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001666 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1667 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001668 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001669 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1670 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1671 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001672 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001673 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001674 Directive = Actions.ActOnOpenMPExecutableDirective(
1675 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1676 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001677
1678 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001679 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001680 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001681 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001682 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001683 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001684 case OMPD_declare_target:
1685 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00001686 case OMPD_requires:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001687 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001688 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001689 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001690 SkipUntil(tok::annot_pragma_openmp_end);
1691 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001692 case OMPD_unknown:
1693 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001694 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001695 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001696 }
1697 return Directive;
1698}
1699
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001700// Parses simple list:
1701// simple-variable-list:
1702// '(' id-expression {, id-expression} ')'
1703//
1704bool Parser::ParseOpenMPSimpleVarList(
1705 OpenMPDirectiveKind Kind,
1706 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1707 Callback,
1708 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001709 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001710 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001711 if (T.expectAndConsume(diag::err_expected_lparen_after,
1712 getOpenMPDirectiveName(Kind)))
1713 return true;
1714 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001715 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001716
1717 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001718 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001719 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001720 UnqualifiedId Name;
1721 // Read var name.
1722 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001723 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001724
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001725 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001726 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001727 IsCorrect = false;
1728 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001729 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001730 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00001731 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001732 IsCorrect = false;
1733 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001734 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001735 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1736 Tok.isNot(tok::annot_pragma_openmp_end)) {
1737 IsCorrect = false;
1738 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001739 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001740 Diag(PrevTok.getLocation(), diag::err_expected)
1741 << tok::identifier
1742 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001743 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001744 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001745 }
1746 // Consume ','.
1747 if (Tok.is(tok::comma)) {
1748 ConsumeToken();
1749 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001750 }
1751
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001752 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001753 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001754 IsCorrect = false;
1755 }
1756
1757 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001758 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001759
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001760 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001761}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001762
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001763/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001764///
1765/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001766/// if-clause | final-clause | num_threads-clause | safelen-clause |
1767/// default-clause | private-clause | firstprivate-clause | shared-clause
1768/// | linear-clause | aligned-clause | collapse-clause |
1769/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001770/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001771/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001772/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001773/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001774/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001775/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001776/// from-clause | is_device_ptr-clause | task_reduction-clause |
Alexey Bataeve04483e2019-03-27 14:14:31 +00001777/// in_reduction-clause | allocator-clause | allocate-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001778///
1779OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1780 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001781 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001782 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001783 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001784 // Check if clause is allowed for the given directive.
1785 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001786 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1787 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001788 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001789 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001790 }
1791
1792 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001793 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001794 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001795 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001796 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001797 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001798 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001799 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001800 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001801 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001802 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001803 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001804 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001805 case OMPC_hint:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001806 case OMPC_allocator:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001807 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001808 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001809 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001810 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001811 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001812 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001813 // OpenMP [2.9.1, target data construct, Restrictions]
1814 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001815 // OpenMP [2.11.1, task Construct, Restrictions]
1816 // At most one if clause can appear on the directive.
1817 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001818 // OpenMP [teams Construct, Restrictions]
1819 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001820 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001821 // OpenMP [2.9.1, task Construct, Restrictions]
1822 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001823 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1824 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001825 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1826 // At most one num_tasks clause can appear on the directive.
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001827 // OpenMP [2.11.3, allocate Directive, Restrictions]
1828 // At most one allocator clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001829 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001830 Diag(Tok, diag::err_omp_more_one_clause)
1831 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001832 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001833 }
1834
Alexey Bataev10e775f2015-07-30 11:36:16 +00001835 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001836 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001837 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001838 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001839 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001840 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001841 case OMPC_proc_bind:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001842 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001843 // OpenMP [2.14.3.1, Restrictions]
1844 // Only a single default clause may be specified on a parallel, task or
1845 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001846 // OpenMP [2.5, parallel Construct, Restrictions]
1847 // At most one proc_bind clause can appear on the directive.
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001848 // OpenMP [5.0, Requires directive, Restrictions]
1849 // At most one atomic_default_mem_order clause can appear
1850 // on the directive
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001851 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001852 Diag(Tok, diag::err_omp_more_one_clause)
1853 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001854 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001855 }
1856
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001857 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001858 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001859 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001860 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001861 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001862 // OpenMP [2.7.1, Restrictions, p. 3]
1863 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001864 // OpenMP [2.10.4, Restrictions, p. 106]
1865 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001866 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001867 Diag(Tok, diag::err_omp_more_one_clause)
1868 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001869 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001870 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001871 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001872
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001873 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001874 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001875 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001876 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001877 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001878 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001879 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001880 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001881 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001882 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001883 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001884 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001885 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001886 case OMPC_nogroup:
Kelvin Li1408f912018-09-26 04:28:39 +00001887 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00001888 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00001889 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00001890 case OMPC_dynamic_allocators:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001891 // OpenMP [2.7.1, Restrictions, p. 9]
1892 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001893 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1894 // Only one nowait clause can appear on a for directive.
Kelvin Li1408f912018-09-26 04:28:39 +00001895 // OpenMP [5.0, Requires directive, Restrictions]
1896 // Each of the requires clauses can appear at most once on the directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001897 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001898 Diag(Tok, diag::err_omp_more_one_clause)
1899 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001900 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001901 }
1902
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001903 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001904 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001905 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001906 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001907 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001908 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001909 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00001910 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00001911 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001912 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001913 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001914 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001915 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001916 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001917 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001918 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001919 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001920 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001921 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001922 case OMPC_is_device_ptr:
Alexey Bataeve04483e2019-03-27 14:14:31 +00001923 case OMPC_allocate:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001924 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001925 break;
Alexey Bataev729e2422019-08-23 16:11:14 +00001926 case OMPC_device_type:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001927 case OMPC_unknown:
1928 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001929 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001930 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001931 break;
1932 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001933 case OMPC_uniform:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001934 if (!WrongDirective)
1935 Diag(Tok, diag::err_omp_unexpected_clause)
1936 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001937 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001938 break;
1939 }
Craig Topper161e4db2014-05-21 06:02:52 +00001940 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001941}
1942
Alexey Bataev2af33e32016-04-07 12:45:37 +00001943/// Parses simple expression in parens for single-expression clauses of OpenMP
1944/// constructs.
1945/// \param RLoc Returned location of right paren.
1946ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
Alexey Bataevd158cf62019-09-13 20:18:17 +00001947 SourceLocation &RLoc,
1948 bool IsAddressOfOperand) {
Alexey Bataev2af33e32016-04-07 12:45:37 +00001949 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1950 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1951 return ExprError();
1952
1953 SourceLocation ELoc = Tok.getLocation();
1954 ExprResult LHS(ParseCastExpression(
Alexey Bataevd158cf62019-09-13 20:18:17 +00001955 /*isUnaryExpression=*/false, IsAddressOfOperand, NotTypeCast));
Alexey Bataev2af33e32016-04-07 12:45:37 +00001956 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00001957 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev2af33e32016-04-07 12:45:37 +00001958
1959 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001960 RLoc = Tok.getLocation();
1961 if (!T.consumeClose())
1962 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001963
Alexey Bataev2af33e32016-04-07 12:45:37 +00001964 return Val;
1965}
1966
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001967/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001968/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001969/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001970///
Alexey Bataev3778b602014-07-17 07:32:53 +00001971/// final-clause:
1972/// 'final' '(' expression ')'
1973///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001974/// num_threads-clause:
1975/// 'num_threads' '(' expression ')'
1976///
1977/// safelen-clause:
1978/// 'safelen' '(' expression ')'
1979///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001980/// simdlen-clause:
1981/// 'simdlen' '(' expression ')'
1982///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001983/// collapse-clause:
1984/// 'collapse' '(' expression ')'
1985///
Alexey Bataeva0569352015-12-01 10:17:31 +00001986/// priority-clause:
1987/// 'priority' '(' expression ')'
1988///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001989/// grainsize-clause:
1990/// 'grainsize' '(' expression ')'
1991///
Alexey Bataev382967a2015-12-08 12:06:20 +00001992/// num_tasks-clause:
1993/// 'num_tasks' '(' expression ')'
1994///
Alexey Bataev28c75412015-12-15 08:19:24 +00001995/// hint-clause:
1996/// 'hint' '(' expression ')'
1997///
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001998/// allocator-clause:
1999/// 'allocator' '(' expression ')'
2000///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002001OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
2002 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002003 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00002004 SourceLocation LLoc = Tok.getLocation();
2005 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002006
Alexey Bataev2af33e32016-04-07 12:45:37 +00002007 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002008
2009 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00002010 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002011
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002012 if (ParseOnly)
2013 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00002014 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002015}
2016
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002017/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002018///
2019/// default-clause:
2020/// 'default' '(' 'none' | 'shared' ')
2021///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002022/// proc_bind-clause:
2023/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
2024///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002025OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
2026 bool ParseOnly) {
Alexey Bataev729e2422019-08-23 16:11:14 +00002027 llvm::Optional<SimpleClauseData> Val = parseOpenMPSimpleClause(*this, Kind);
2028 if (!Val || ParseOnly)
Craig Topper161e4db2014-05-21 06:02:52 +00002029 return nullptr;
Alexey Bataev729e2422019-08-23 16:11:14 +00002030 return Actions.ActOnOpenMPSimpleClause(
2031 Kind, Val.getValue().Type, Val.getValue().TypeLoc, Val.getValue().LOpen,
2032 Val.getValue().Loc, Val.getValue().RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002033}
2034
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002035/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002036///
2037/// ordered-clause:
2038/// 'ordered'
2039///
Alexey Bataev236070f2014-06-20 11:19:47 +00002040/// nowait-clause:
2041/// 'nowait'
2042///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002043/// untied-clause:
2044/// 'untied'
2045///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002046/// mergeable-clause:
2047/// 'mergeable'
2048///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002049/// read-clause:
2050/// 'read'
2051///
Alexey Bataev346265e2015-09-25 10:37:12 +00002052/// threads-clause:
2053/// 'threads'
2054///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002055/// simd-clause:
2056/// 'simd'
2057///
Alexey Bataevb825de12015-12-07 10:51:44 +00002058/// nogroup-clause:
2059/// 'nogroup'
2060///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002061OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002062 SourceLocation Loc = Tok.getLocation();
2063 ConsumeAnyToken();
2064
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002065 if (ParseOnly)
2066 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002067 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
2068}
2069
2070
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002071/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00002072/// argument like 'schedule' or 'dist_schedule'.
2073///
2074/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00002075/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
2076/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00002077///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002078/// if-clause:
2079/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
2080///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002081/// defaultmap:
2082/// 'defaultmap' '(' modifier ':' kind ')'
2083///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002084OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
2085 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00002086 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002087 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002088 // Parse '('.
2089 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2090 if (T.expectAndConsume(diag::err_expected_lparen_after,
2091 getOpenMPClauseName(Kind)))
2092 return nullptr;
2093
2094 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002095 SmallVector<unsigned, 4> Arg;
2096 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002097 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00002098 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
2099 Arg.resize(NumberOfElements);
2100 KLoc.resize(NumberOfElements);
2101 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
2102 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
2103 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00002104 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002105 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00002106 if (KindModifier > OMPC_SCHEDULE_unknown) {
2107 // Parse 'modifier'
2108 Arg[Modifier1] = KindModifier;
2109 KLoc[Modifier1] = Tok.getLocation();
2110 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2111 Tok.isNot(tok::annot_pragma_openmp_end))
2112 ConsumeAnyToken();
2113 if (Tok.is(tok::comma)) {
2114 // Parse ',' 'modifier'
2115 ConsumeAnyToken();
2116 KindModifier = getOpenMPSimpleClauseType(
2117 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2118 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
2119 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00002120 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002121 KLoc[Modifier2] = Tok.getLocation();
2122 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2123 Tok.isNot(tok::annot_pragma_openmp_end))
2124 ConsumeAnyToken();
2125 }
2126 // Parse ':'
2127 if (Tok.is(tok::colon))
2128 ConsumeAnyToken();
2129 else
2130 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
2131 KindModifier = getOpenMPSimpleClauseType(
2132 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2133 }
2134 Arg[ScheduleKind] = KindModifier;
2135 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002136 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2137 Tok.isNot(tok::annot_pragma_openmp_end))
2138 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00002139 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
2140 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
2141 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002142 Tok.is(tok::comma))
2143 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00002144 } else if (Kind == OMPC_dist_schedule) {
2145 Arg.push_back(getOpenMPSimpleClauseType(
2146 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2147 KLoc.push_back(Tok.getLocation());
2148 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2149 Tok.isNot(tok::annot_pragma_openmp_end))
2150 ConsumeAnyToken();
2151 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
2152 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002153 } else if (Kind == OMPC_defaultmap) {
2154 // Get a defaultmap modifier
2155 Arg.push_back(getOpenMPSimpleClauseType(
2156 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2157 KLoc.push_back(Tok.getLocation());
2158 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2159 Tok.isNot(tok::annot_pragma_openmp_end))
2160 ConsumeAnyToken();
2161 // Parse ':'
2162 if (Tok.is(tok::colon))
2163 ConsumeAnyToken();
2164 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
2165 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
2166 // Get a defaultmap kind
2167 Arg.push_back(getOpenMPSimpleClauseType(
2168 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2169 KLoc.push_back(Tok.getLocation());
2170 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2171 Tok.isNot(tok::annot_pragma_openmp_end))
2172 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002173 } else {
2174 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00002175 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002176 TentativeParsingAction TPA(*this);
Alexey Bataev61908f652018-04-23 19:53:05 +00002177 Arg.push_back(parseOpenMPDirectiveKind(*this));
Alexey Bataev6402bca2015-12-28 07:25:51 +00002178 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002179 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002180 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
2181 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002182 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002183 } else {
2184 TPA.Revert();
2185 Arg.back() = OMPD_unknown;
2186 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002187 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002188 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00002189 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002190 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00002191
Carlo Bertollib4adf552016-01-15 18:50:31 +00002192 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
2193 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
2194 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002195 if (NeedAnExpression) {
2196 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002197 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
2198 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002199 Val =
2200 Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002201 }
2202
2203 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002204 SourceLocation RLoc = Tok.getLocation();
2205 if (!T.consumeClose())
2206 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002207
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002208 if (NeedAnExpression && Val.isInvalid())
2209 return nullptr;
2210
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002211 if (ParseOnly)
2212 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002213 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002214 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002215}
2216
Alexey Bataevc5e02582014-06-16 07:08:35 +00002217static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
2218 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002219 if (ReductionIdScopeSpec.isEmpty()) {
2220 auto OOK = OO_None;
2221 switch (P.getCurToken().getKind()) {
2222 case tok::plus:
2223 OOK = OO_Plus;
2224 break;
2225 case tok::minus:
2226 OOK = OO_Minus;
2227 break;
2228 case tok::star:
2229 OOK = OO_Star;
2230 break;
2231 case tok::amp:
2232 OOK = OO_Amp;
2233 break;
2234 case tok::pipe:
2235 OOK = OO_Pipe;
2236 break;
2237 case tok::caret:
2238 OOK = OO_Caret;
2239 break;
2240 case tok::ampamp:
2241 OOK = OO_AmpAmp;
2242 break;
2243 case tok::pipepipe:
2244 OOK = OO_PipePipe;
2245 break;
2246 default:
2247 break;
2248 }
2249 if (OOK != OO_None) {
2250 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00002251 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00002252 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
2253 return false;
2254 }
2255 }
2256 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
2257 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00002258 /*AllowConstructorName*/ false,
2259 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00002260 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002261}
2262
Kelvin Lief579432018-12-18 22:18:41 +00002263/// Checks if the token is a valid map-type-modifier.
2264static OpenMPMapModifierKind isMapModifier(Parser &P) {
2265 Token Tok = P.getCurToken();
2266 if (!Tok.is(tok::identifier))
2267 return OMPC_MAP_MODIFIER_unknown;
2268
2269 Preprocessor &PP = P.getPreprocessor();
2270 OpenMPMapModifierKind TypeModifier = static_cast<OpenMPMapModifierKind>(
2271 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2272 return TypeModifier;
2273}
2274
Michael Kruse01f670d2019-02-22 22:29:42 +00002275/// Parse the mapper modifier in map, to, and from clauses.
2276bool Parser::parseMapperModifier(OpenMPVarListDataTy &Data) {
2277 // Parse '('.
2278 BalancedDelimiterTracker T(*this, tok::l_paren, tok::colon);
2279 if (T.expectAndConsume(diag::err_expected_lparen_after, "mapper")) {
2280 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2281 StopBeforeMatch);
2282 return true;
2283 }
2284 // Parse mapper-identifier
2285 if (getLangOpts().CPlusPlus)
2286 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
2287 /*ObjectType=*/nullptr,
2288 /*EnteringContext=*/false);
2289 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
2290 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
2291 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2292 StopBeforeMatch);
2293 return true;
2294 }
2295 auto &DeclNames = Actions.getASTContext().DeclarationNames;
2296 Data.ReductionOrMapperId = DeclarationNameInfo(
2297 DeclNames.getIdentifier(Tok.getIdentifierInfo()), Tok.getLocation());
2298 ConsumeToken();
2299 // Parse ')'.
2300 return T.consumeClose();
2301}
2302
Kelvin Lief579432018-12-18 22:18:41 +00002303/// Parse map-type-modifiers in map clause.
2304/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002305/// where, map-type-modifier ::= always | close | mapper(mapper-identifier)
2306bool Parser::parseMapTypeModifiers(OpenMPVarListDataTy &Data) {
2307 while (getCurToken().isNot(tok::colon)) {
2308 OpenMPMapModifierKind TypeModifier = isMapModifier(*this);
Kelvin Lief579432018-12-18 22:18:41 +00002309 if (TypeModifier == OMPC_MAP_MODIFIER_always ||
2310 TypeModifier == OMPC_MAP_MODIFIER_close) {
2311 Data.MapTypeModifiers.push_back(TypeModifier);
2312 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
Michael Kruse4304e9d2019-02-19 16:38:20 +00002313 ConsumeToken();
2314 } else if (TypeModifier == OMPC_MAP_MODIFIER_mapper) {
2315 Data.MapTypeModifiers.push_back(TypeModifier);
2316 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
2317 ConsumeToken();
Michael Kruse01f670d2019-02-22 22:29:42 +00002318 if (parseMapperModifier(Data))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002319 return true;
Kelvin Lief579432018-12-18 22:18:41 +00002320 } else {
2321 // For the case of unknown map-type-modifier or a map-type.
2322 // Map-type is followed by a colon; the function returns when it
2323 // encounters a token followed by a colon.
2324 if (Tok.is(tok::comma)) {
Michael Kruse4304e9d2019-02-19 16:38:20 +00002325 Diag(Tok, diag::err_omp_map_type_modifier_missing);
2326 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002327 continue;
2328 }
2329 // Potential map-type token as it is followed by a colon.
2330 if (PP.LookAhead(0).is(tok::colon))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002331 return false;
2332 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
2333 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002334 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002335 if (getCurToken().is(tok::comma))
2336 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002337 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002338 return false;
Kelvin Lief579432018-12-18 22:18:41 +00002339}
2340
2341/// Checks if the token is a valid map-type.
2342static OpenMPMapClauseKind isMapType(Parser &P) {
2343 Token Tok = P.getCurToken();
2344 // The map-type token can be either an identifier or the C++ delete keyword.
2345 if (!Tok.isOneOf(tok::identifier, tok::kw_delete))
2346 return OMPC_MAP_unknown;
2347 Preprocessor &PP = P.getPreprocessor();
2348 OpenMPMapClauseKind MapType = static_cast<OpenMPMapClauseKind>(
2349 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2350 return MapType;
2351}
2352
2353/// Parse map-type in map clause.
2354/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002355/// where, map-type ::= to | from | tofrom | alloc | release | delete
Kelvin Lief579432018-12-18 22:18:41 +00002356static void parseMapType(Parser &P, Parser::OpenMPVarListDataTy &Data) {
2357 Token Tok = P.getCurToken();
2358 if (Tok.is(tok::colon)) {
2359 P.Diag(Tok, diag::err_omp_map_type_missing);
2360 return;
2361 }
2362 Data.MapType = isMapType(P);
2363 if (Data.MapType == OMPC_MAP_unknown)
2364 P.Diag(Tok, diag::err_omp_unknown_map_type);
2365 P.ConsumeToken();
2366}
2367
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002368/// Parses clauses with list.
2369bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
2370 OpenMPClauseKind Kind,
2371 SmallVectorImpl<Expr *> &Vars,
2372 OpenMPVarListDataTy &Data) {
2373 UnqualifiedId UnqualifiedReductionId;
2374 bool InvalidReductionId = false;
Michael Kruse01f670d2019-02-22 22:29:42 +00002375 bool IsInvalidMapperModifier = false;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002376
2377 // Parse '('.
2378 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2379 if (T.expectAndConsume(diag::err_expected_lparen_after,
2380 getOpenMPClauseName(Kind)))
2381 return true;
2382
2383 bool NeedRParenForLinear = false;
2384 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
2385 tok::annot_pragma_openmp_end);
2386 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00002387 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
2388 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002389 ColonProtectionRAIIObject ColonRAII(*this);
2390 if (getLangOpts().CPlusPlus)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002391 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002392 /*ObjectType=*/nullptr,
2393 /*EnteringContext=*/false);
Michael Kruse4304e9d2019-02-19 16:38:20 +00002394 InvalidReductionId = ParseReductionId(
2395 *this, Data.ReductionOrMapperIdScopeSpec, UnqualifiedReductionId);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002396 if (InvalidReductionId) {
2397 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2398 StopBeforeMatch);
2399 }
2400 if (Tok.is(tok::colon))
2401 Data.ColonLoc = ConsumeToken();
2402 else
2403 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
2404 if (!InvalidReductionId)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002405 Data.ReductionOrMapperId =
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002406 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
2407 } else if (Kind == OMPC_depend) {
2408 // Handle dependency type for depend clause.
2409 ColonProtectionRAIIObject ColonRAII(*this);
2410 Data.DepKind =
2411 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
2412 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
2413 Data.DepLinMapLoc = Tok.getLocation();
2414
2415 if (Data.DepKind == OMPC_DEPEND_unknown) {
2416 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2417 StopBeforeMatch);
2418 } else {
2419 ConsumeToken();
2420 // Special processing for depend(source) clause.
2421 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
2422 // Parse ')'.
2423 T.consumeClose();
2424 return false;
2425 }
2426 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002427 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002428 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00002429 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002430 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
2431 : diag::warn_pragma_expected_colon)
2432 << "dependency type";
2433 }
2434 } else if (Kind == OMPC_linear) {
2435 // Try to parse modifier if any.
2436 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
2437 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
2438 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2439 Data.DepLinMapLoc = ConsumeToken();
2440 LinearT.consumeOpen();
2441 NeedRParenForLinear = true;
2442 }
2443 } else if (Kind == OMPC_map) {
2444 // Handle map type for map clause.
2445 ColonProtectionRAIIObject ColonRAII(*this);
2446
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002447 // The first identifier may be a list item, a map-type or a
Kelvin Lief579432018-12-18 22:18:41 +00002448 // map-type-modifier. The map-type can also be delete which has the same
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002449 // spelling of the C++ delete keyword.
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002450 Data.DepLinMapLoc = Tok.getLocation();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002451
Kelvin Lief579432018-12-18 22:18:41 +00002452 // Check for presence of a colon in the map clause.
2453 TentativeParsingAction TPA(*this);
2454 bool ColonPresent = false;
2455 if (SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2456 StopBeforeMatch)) {
2457 if (Tok.is(tok::colon))
2458 ColonPresent = true;
2459 }
2460 TPA.Revert();
2461 // Only parse map-type-modifier[s] and map-type if a colon is present in
2462 // the map clause.
2463 if (ColonPresent) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002464 IsInvalidMapperModifier = parseMapTypeModifiers(Data);
2465 if (!IsInvalidMapperModifier)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002466 parseMapType(*this, Data);
Michael Kruse01f670d2019-02-22 22:29:42 +00002467 else
2468 SkipUntil(tok::colon, tok::annot_pragma_openmp_end, StopBeforeMatch);
Kelvin Lief579432018-12-18 22:18:41 +00002469 }
2470 if (Data.MapType == OMPC_MAP_unknown) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002471 Data.MapType = OMPC_MAP_tofrom;
2472 Data.IsMapTypeImplicit = true;
2473 }
2474
2475 if (Tok.is(tok::colon))
2476 Data.ColonLoc = ConsumeToken();
Michael Kruse0336c752019-02-25 20:34:15 +00002477 } else if (Kind == OMPC_to || Kind == OMPC_from) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002478 if (Tok.is(tok::identifier)) {
2479 bool IsMapperModifier = false;
Michael Kruse0336c752019-02-25 20:34:15 +00002480 if (Kind == OMPC_to) {
2481 auto Modifier = static_cast<OpenMPToModifierKind>(
2482 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2483 if (Modifier == OMPC_TO_MODIFIER_mapper)
2484 IsMapperModifier = true;
2485 } else {
2486 auto Modifier = static_cast<OpenMPFromModifierKind>(
2487 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2488 if (Modifier == OMPC_FROM_MODIFIER_mapper)
2489 IsMapperModifier = true;
2490 }
Michael Kruse01f670d2019-02-22 22:29:42 +00002491 if (IsMapperModifier) {
2492 // Parse the mapper modifier.
2493 ConsumeToken();
2494 IsInvalidMapperModifier = parseMapperModifier(Data);
2495 if (Tok.isNot(tok::colon)) {
2496 if (!IsInvalidMapperModifier)
2497 Diag(Tok, diag::warn_pragma_expected_colon) << ")";
2498 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2499 StopBeforeMatch);
2500 }
2501 // Consume ':'.
2502 if (Tok.is(tok::colon))
2503 ConsumeToken();
2504 }
2505 }
Alexey Bataeve04483e2019-03-27 14:14:31 +00002506 } else if (Kind == OMPC_allocate) {
2507 // Handle optional allocator expression followed by colon delimiter.
2508 ColonProtectionRAIIObject ColonRAII(*this);
2509 TentativeParsingAction TPA(*this);
2510 ExprResult Tail =
2511 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
2512 Tail = Actions.ActOnFinishFullExpr(Tail.get(), T.getOpenLocation(),
2513 /*DiscardedValue=*/false);
2514 if (Tail.isUsable()) {
2515 if (Tok.is(tok::colon)) {
2516 Data.TailExpr = Tail.get();
2517 Data.ColonLoc = ConsumeToken();
2518 TPA.Commit();
2519 } else {
2520 // colon not found, no allocator specified, parse only list of
2521 // variables.
2522 TPA.Revert();
2523 }
2524 } else {
2525 // Parsing was unsuccessfull, revert and skip to the end of clause or
2526 // directive.
2527 TPA.Revert();
2528 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2529 StopBeforeMatch);
2530 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002531 }
2532
Alexey Bataevfa312f32017-07-21 18:48:21 +00002533 bool IsComma =
2534 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
2535 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
2536 (Kind == OMPC_reduction && !InvalidReductionId) ||
Kelvin Lida6bc702018-11-21 19:38:53 +00002537 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown) ||
Alexey Bataevfa312f32017-07-21 18:48:21 +00002538 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002539 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
2540 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
2541 Tok.isNot(tok::annot_pragma_openmp_end))) {
2542 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
2543 // Parse variable
2544 ExprResult VarExpr =
2545 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00002546 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002547 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00002548 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002549 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2550 StopBeforeMatch);
2551 }
2552 // Skip ',' if any
2553 IsComma = Tok.is(tok::comma);
2554 if (IsComma)
2555 ConsumeToken();
2556 else if (Tok.isNot(tok::r_paren) &&
2557 Tok.isNot(tok::annot_pragma_openmp_end) &&
2558 (!MayHaveTail || Tok.isNot(tok::colon)))
2559 Diag(Tok, diag::err_omp_expected_punc)
2560 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
2561 : getOpenMPClauseName(Kind))
2562 << (Kind == OMPC_flush);
2563 }
2564
2565 // Parse ')' for linear clause with modifier.
2566 if (NeedRParenForLinear)
2567 LinearT.consumeClose();
2568
2569 // Parse ':' linear-step (or ':' alignment).
2570 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
2571 if (MustHaveTail) {
2572 Data.ColonLoc = Tok.getLocation();
2573 SourceLocation ELoc = ConsumeToken();
2574 ExprResult Tail = ParseAssignmentExpression();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002575 Tail =
2576 Actions.ActOnFinishFullExpr(Tail.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002577 if (Tail.isUsable())
2578 Data.TailExpr = Tail.get();
2579 else
2580 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2581 StopBeforeMatch);
2582 }
2583
2584 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002585 Data.RLoc = Tok.getLocation();
2586 if (!T.consumeClose())
2587 Data.RLoc = T.getCloseLocation();
Alexey Bataev61908f652018-04-23 19:53:05 +00002588 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
2589 Vars.empty()) ||
2590 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
Michael Kruse4304e9d2019-02-19 16:38:20 +00002591 (MustHaveTail && !Data.TailExpr) || InvalidReductionId ||
Michael Kruse01f670d2019-02-22 22:29:42 +00002592 IsInvalidMapperModifier;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002593}
2594
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002595/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00002596/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
2597/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002598///
2599/// private-clause:
2600/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002601/// firstprivate-clause:
2602/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00002603/// lastprivate-clause:
2604/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00002605/// shared-clause:
2606/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00002607/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00002608/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002609/// aligned-clause:
2610/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00002611/// reduction-clause:
2612/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00002613/// task_reduction-clause:
2614/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00002615/// in_reduction-clause:
2616/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00002617/// copyprivate-clause:
2618/// 'copyprivate' '(' list ')'
2619/// flush-clause:
2620/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002621/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00002622/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00002623/// map-clause:
Kelvin Lief579432018-12-18 22:18:41 +00002624/// 'map' '(' [ [ always [,] ] [ close [,] ]
Michael Kruse01f670d2019-02-22 22:29:42 +00002625/// [ mapper '(' mapper-identifier ')' [,] ]
Kelvin Li0bff7af2015-11-23 05:32:03 +00002626/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00002627/// to-clause:
Michael Kruse01f670d2019-02-22 22:29:42 +00002628/// 'to' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00002629/// from-clause:
Michael Kruse0336c752019-02-25 20:34:15 +00002630/// 'from' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00002631/// use_device_ptr-clause:
2632/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00002633/// is_device_ptr-clause:
2634/// 'is_device_ptr' '(' list ')'
Alexey Bataeve04483e2019-03-27 14:14:31 +00002635/// allocate-clause:
2636/// 'allocate' '(' [ allocator ':' ] list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002637///
Alexey Bataev182227b2015-08-20 10:54:39 +00002638/// For 'linear' clause linear-list may have the following forms:
2639/// list
2640/// modifier(list)
2641/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00002642OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002643 OpenMPClauseKind Kind,
2644 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002645 SourceLocation Loc = Tok.getLocation();
2646 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002647 SmallVector<Expr *, 4> Vars;
2648 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002649
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002650 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00002651 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002652
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002653 if (ParseOnly)
2654 return nullptr;
Michael Kruse4304e9d2019-02-19 16:38:20 +00002655 OMPVarListLocTy Locs(Loc, LOpen, Data.RLoc);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002656 return Actions.ActOnOpenMPVarListClause(
Michael Kruse4304e9d2019-02-19 16:38:20 +00002657 Kind, Vars, Data.TailExpr, Locs, Data.ColonLoc,
2658 Data.ReductionOrMapperIdScopeSpec, Data.ReductionOrMapperId, Data.DepKind,
2659 Data.LinKind, Data.MapTypeModifiers, Data.MapTypeModifiersLoc,
2660 Data.MapType, Data.IsMapTypeImplicit, Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002661}
2662