blob: 570f246d19b1d7cf253237bdb74cb5792d68cedd [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:
Alexey Bataevd158cf62019-09-13 20:18:17 +0000791/// <selector_set_name> '=' '{' <context_selectors> '}'
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000792/// [ ',' <selector_set_name> '=' '{' <context_selectors> '}' ]
793bool Parser::parseOpenMPContextSelectors(
794 SourceLocation Loc, llvm::function_ref<void(SourceRange)> Callback) {
795 do {
796 // Parse inner context selector set name.
797 if (!Tok.is(tok::identifier)) {
798 Diag(Tok.getLocation(), diag::err_omp_declare_variant_no_ctx_selector)
Alexey Bataevdba792c2019-09-23 18:13:31 +0000799 << getOpenMPClauseName(OMPC_match);
Alexey Bataevd158cf62019-09-13 20:18:17 +0000800 return true;
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000801 }
802 SmallString<16> Buffer;
803 StringRef CtxSelectorName = PP.getSpelling(Tok, Buffer);
804 // Parse '='.
805 (void)ConsumeToken();
806 if (Tok.isNot(tok::equal)) {
807 Diag(Tok.getLocation(), diag::err_omp_declare_variant_equal_expected)
808 << CtxSelectorName;
809 return true;
810 }
811 (void)ConsumeToken();
812 // TBD: add parsing of known context selectors.
813 // Unknown selector - just ignore it completely.
814 {
815 // Parse '{'.
816 BalancedDelimiterTracker TBr(*this, tok::l_brace,
817 tok::annot_pragma_openmp_end);
818 if (TBr.expectAndConsume(diag::err_expected_lbrace_after, "="))
819 return true;
820 while (!SkipUntil(tok::r_brace, tok::r_paren,
821 tok::annot_pragma_openmp_end, StopBeforeMatch))
822 ;
823 // Parse '}'.
824 (void)TBr.consumeClose();
825 }
826 Callback(SourceRange(Loc, Tok.getLocation()));
827 // Consume ','
828 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end))
829 (void)ExpectAndConsume(tok::comma);
830 } while (Tok.isAnyIdentifier());
Alexey Bataevd158cf62019-09-13 20:18:17 +0000831 return false;
832}
833
834/// Parse clauses for '#pragma omp declare variant ( variant-func-id ) clause'.
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000835void Parser::ParseOMPDeclareVariantClauses(Parser::DeclGroupPtrTy Ptr,
836 CachedTokens &Toks,
837 SourceLocation Loc) {
Alexey Bataevd158cf62019-09-13 20:18:17 +0000838 PP.EnterToken(Tok, /*IsReinject*/ true);
839 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
840 /*IsReinject*/ true);
841 // Consume the previously pushed token.
842 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
843 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
844
845 FNContextRAII FnContext(*this, Ptr);
846 // Parse function declaration id.
847 SourceLocation RLoc;
848 // Parse with IsAddressOfOperand set to true to parse methods as DeclRefExprs
849 // instead of MemberExprs.
850 ExprResult AssociatedFunction =
851 ParseOpenMPParensExpr(getOpenMPDirectiveName(OMPD_declare_variant), RLoc,
852 /*IsAddressOfOperand=*/true);
853 if (!AssociatedFunction.isUsable()) {
854 if (!Tok.is(tok::annot_pragma_openmp_end))
855 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
856 ;
857 // Skip the last annot_pragma_openmp_end.
858 (void)ConsumeAnnotationToken();
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000859 return;
860 }
861 Optional<std::pair<FunctionDecl *, Expr *>> DeclVarData =
862 Actions.checkOpenMPDeclareVariantFunction(
863 Ptr, AssociatedFunction.get(), SourceRange(Loc, Tok.getLocation()));
864
865 // Parse 'match'.
Alexey Bataevdba792c2019-09-23 18:13:31 +0000866 OpenMPClauseKind CKind = Tok.isAnnotation()
867 ? OMPC_unknown
868 : getOpenMPClauseKind(PP.getSpelling(Tok));
869 if (CKind != OMPC_match) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000870 Diag(Tok.getLocation(), diag::err_omp_declare_variant_wrong_clause)
Alexey Bataevdba792c2019-09-23 18:13:31 +0000871 << getOpenMPClauseName(OMPC_match);
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000872 while (!SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
873 ;
874 // Skip the last annot_pragma_openmp_end.
875 (void)ConsumeAnnotationToken();
876 return;
877 }
878 (void)ConsumeToken();
879 // Parse '('.
880 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataevdba792c2019-09-23 18:13:31 +0000881 if (T.expectAndConsume(diag::err_expected_lparen_after,
882 getOpenMPClauseName(OMPC_match))) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000883 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
884 ;
885 // Skip the last annot_pragma_openmp_end.
886 (void)ConsumeAnnotationToken();
887 return;
Alexey Bataevd158cf62019-09-13 20:18:17 +0000888 }
889
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000890 // Parse inner context selectors.
891 if (!parseOpenMPContextSelectors(Loc, [this, &DeclVarData](SourceRange SR) {
892 if (DeclVarData.hasValue())
893 Actions.ActOnOpenMPDeclareVariantDirective(
894 DeclVarData.getValue().first, DeclVarData.getValue().second, SR);
895 })) {
896 // Parse ')'.
897 (void)T.consumeClose();
898 // Need to check for extra tokens.
899 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
900 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
901 << getOpenMPDirectiveName(OMPD_declare_variant);
902 }
Alexey Bataevd158cf62019-09-13 20:18:17 +0000903 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000904
905 // Skip last tokens.
906 while (Tok.isNot(tok::annot_pragma_openmp_end))
907 ConsumeAnyToken();
Alexey Bataevd158cf62019-09-13 20:18:17 +0000908 // Skip the last annot_pragma_openmp_end.
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000909 (void)ConsumeAnnotationToken();
Alexey Bataevd158cf62019-09-13 20:18:17 +0000910}
911
Alexey Bataev729e2422019-08-23 16:11:14 +0000912/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
913///
914/// default-clause:
915/// 'default' '(' 'none' | 'shared' ')
916///
917/// proc_bind-clause:
918/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
919///
920/// device_type-clause:
921/// 'device_type' '(' 'host' | 'nohost' | 'any' )'
922namespace {
923 struct SimpleClauseData {
924 unsigned Type;
925 SourceLocation Loc;
926 SourceLocation LOpen;
927 SourceLocation TypeLoc;
928 SourceLocation RLoc;
929 SimpleClauseData(unsigned Type, SourceLocation Loc, SourceLocation LOpen,
930 SourceLocation TypeLoc, SourceLocation RLoc)
931 : Type(Type), Loc(Loc), LOpen(LOpen), TypeLoc(TypeLoc), RLoc(RLoc) {}
932 };
933} // anonymous namespace
934
935static Optional<SimpleClauseData>
936parseOpenMPSimpleClause(Parser &P, OpenMPClauseKind Kind) {
937 const Token &Tok = P.getCurToken();
938 SourceLocation Loc = Tok.getLocation();
939 SourceLocation LOpen = P.ConsumeToken();
940 // Parse '('.
941 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
942 if (T.expectAndConsume(diag::err_expected_lparen_after,
943 getOpenMPClauseName(Kind)))
944 return llvm::None;
945
946 unsigned Type = getOpenMPSimpleClauseType(
947 Kind, Tok.isAnnotation() ? "" : P.getPreprocessor().getSpelling(Tok));
948 SourceLocation TypeLoc = Tok.getLocation();
949 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
950 Tok.isNot(tok::annot_pragma_openmp_end))
951 P.ConsumeAnyToken();
952
953 // Parse ')'.
954 SourceLocation RLoc = Tok.getLocation();
955 if (!T.consumeClose())
956 RLoc = T.getCloseLocation();
957
958 return SimpleClauseData(Type, Loc, LOpen, TypeLoc, RLoc);
959}
960
Kelvin Lie0502752018-11-21 20:15:57 +0000961Parser::DeclGroupPtrTy Parser::ParseOMPDeclareTargetClauses() {
962 // OpenMP 4.5 syntax with list of entities.
963 Sema::NamedDeclSetType SameDirectiveDecls;
Alexey Bataev729e2422019-08-23 16:11:14 +0000964 SmallVector<std::tuple<OMPDeclareTargetDeclAttr::MapTypeTy, SourceLocation,
965 NamedDecl *>,
966 4>
967 DeclareTargetDecls;
968 OMPDeclareTargetDeclAttr::DevTypeTy DT = OMPDeclareTargetDeclAttr::DT_Any;
969 SourceLocation DeviceTypeLoc;
Kelvin Lie0502752018-11-21 20:15:57 +0000970 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
971 OMPDeclareTargetDeclAttr::MapTypeTy MT = OMPDeclareTargetDeclAttr::MT_To;
972 if (Tok.is(tok::identifier)) {
973 IdentifierInfo *II = Tok.getIdentifierInfo();
974 StringRef ClauseName = II->getName();
Alexey Bataev729e2422019-08-23 16:11:14 +0000975 bool IsDeviceTypeClause =
976 getLangOpts().OpenMP >= 50 &&
977 getOpenMPClauseKind(ClauseName) == OMPC_device_type;
978 // Parse 'to|link|device_type' clauses.
979 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName, MT) &&
980 !IsDeviceTypeClause) {
981 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
982 << ClauseName << (getLangOpts().OpenMP >= 50 ? 1 : 0);
Kelvin Lie0502752018-11-21 20:15:57 +0000983 break;
984 }
Alexey Bataev729e2422019-08-23 16:11:14 +0000985 // Parse 'device_type' clause and go to next clause if any.
986 if (IsDeviceTypeClause) {
987 Optional<SimpleClauseData> DevTypeData =
988 parseOpenMPSimpleClause(*this, OMPC_device_type);
989 if (DevTypeData.hasValue()) {
990 if (DeviceTypeLoc.isValid()) {
991 // We already saw another device_type clause, diagnose it.
992 Diag(DevTypeData.getValue().Loc,
993 diag::warn_omp_more_one_device_type_clause);
994 }
995 switch(static_cast<OpenMPDeviceType>(DevTypeData.getValue().Type)) {
996 case OMPC_DEVICE_TYPE_any:
997 DT = OMPDeclareTargetDeclAttr::DT_Any;
998 break;
999 case OMPC_DEVICE_TYPE_host:
1000 DT = OMPDeclareTargetDeclAttr::DT_Host;
1001 break;
1002 case OMPC_DEVICE_TYPE_nohost:
1003 DT = OMPDeclareTargetDeclAttr::DT_NoHost;
1004 break;
1005 case OMPC_DEVICE_TYPE_unknown:
1006 llvm_unreachable("Unexpected device_type");
1007 }
1008 DeviceTypeLoc = DevTypeData.getValue().Loc;
1009 }
1010 continue;
1011 }
Kelvin Lie0502752018-11-21 20:15:57 +00001012 ConsumeToken();
1013 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001014 auto &&Callback = [this, MT, &DeclareTargetDecls, &SameDirectiveDecls](
1015 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
1016 NamedDecl *ND = Actions.lookupOpenMPDeclareTargetName(
1017 getCurScope(), SS, NameInfo, SameDirectiveDecls);
1018 if (ND)
1019 DeclareTargetDecls.emplace_back(MT, NameInfo.getLoc(), ND);
Kelvin Lie0502752018-11-21 20:15:57 +00001020 };
1021 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
1022 /*AllowScopeSpecifier=*/true))
1023 break;
1024
1025 // Consume optional ','.
1026 if (Tok.is(tok::comma))
1027 ConsumeToken();
1028 }
1029 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1030 ConsumeAnyToken();
Alexey Bataev729e2422019-08-23 16:11:14 +00001031 for (auto &MTLocDecl : DeclareTargetDecls) {
1032 OMPDeclareTargetDeclAttr::MapTypeTy MT;
1033 SourceLocation Loc;
1034 NamedDecl *ND;
1035 std::tie(MT, Loc, ND) = MTLocDecl;
1036 // device_type clause is applied only to functions.
1037 Actions.ActOnOpenMPDeclareTargetName(
1038 ND, Loc, MT, isa<VarDecl>(ND) ? OMPDeclareTargetDeclAttr::DT_Any : DT);
1039 }
Kelvin Lie0502752018-11-21 20:15:57 +00001040 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
1041 SameDirectiveDecls.end());
1042 if (Decls.empty())
1043 return DeclGroupPtrTy();
1044 return Actions.BuildDeclaratorGroup(Decls);
1045}
1046
1047void Parser::ParseOMPEndDeclareTargetDirective(OpenMPDirectiveKind DKind,
1048 SourceLocation DTLoc) {
1049 if (DKind != OMPD_end_declare_target) {
1050 Diag(Tok, diag::err_expected_end_declare_target);
1051 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
1052 return;
1053 }
1054 ConsumeAnyToken();
1055 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1056 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1057 << getOpenMPDirectiveName(OMPD_end_declare_target);
1058 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1059 }
1060 // Skip the last annot_pragma_openmp_end.
1061 ConsumeAnyToken();
1062}
1063
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001064/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001065///
1066/// threadprivate-directive:
1067/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001068/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +00001069///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001070/// allocate-directive:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001071/// annot_pragma_openmp 'allocate' simple-variable-list [<clause>]
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001072/// annot_pragma_openmp_end
1073///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001074/// declare-reduction-directive:
1075/// annot_pragma_openmp 'declare' 'reduction' [...]
1076/// annot_pragma_openmp_end
1077///
Michael Kruse251e1482019-02-01 20:25:04 +00001078/// declare-mapper-directive:
1079/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1080/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1081/// annot_pragma_openmp_end
1082///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001083/// declare-simd-directive:
1084/// annot_pragma_openmp 'declare simd' {<clause> [,]}
1085/// annot_pragma_openmp_end
1086/// <function declaration/definition>
1087///
Kelvin Li1408f912018-09-26 04:28:39 +00001088/// requires directive:
1089/// annot_pragma_openmp 'requires' <clause> [[[,] <clause>] ... ]
1090/// annot_pragma_openmp_end
1091///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001092Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
1093 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
1094 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001095 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +00001096 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +00001097
Richard Smithaf3b3252017-05-18 19:21:48 +00001098 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001099 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001100
1101 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001102 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +00001103 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001104 DeclDirectiveListParserHelper Helper(this, DKind);
1105 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1106 /*AllowScopeSpecifier=*/true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001107 // The last seen token is annot_pragma_openmp_end - need to check for
1108 // extra tokens.
1109 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1110 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001111 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001112 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +00001113 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001114 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001115 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001116 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
1117 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +00001118 }
1119 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001120 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001121 case OMPD_allocate: {
1122 ConsumeToken();
1123 DeclDirectiveListParserHelper Helper(this, DKind);
1124 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1125 /*AllowScopeSpecifier=*/true)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001126 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001127 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001128 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1129 OMPC_unknown + 1>
1130 FirstClauses(OMPC_unknown + 1);
1131 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1132 OpenMPClauseKind CKind =
1133 Tok.isAnnotation() ? OMPC_unknown
1134 : getOpenMPClauseKind(PP.getSpelling(Tok));
1135 Actions.StartOpenMPClause(CKind);
1136 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1137 !FirstClauses[CKind].getInt());
1138 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1139 StopBeforeMatch);
1140 FirstClauses[CKind].setInt(true);
1141 if (Clause != nullptr)
1142 Clauses.push_back(Clause);
1143 if (Tok.is(tok::annot_pragma_openmp_end)) {
1144 Actions.EndOpenMPClause();
1145 break;
1146 }
1147 // Skip ',' if any.
1148 if (Tok.is(tok::comma))
1149 ConsumeToken();
1150 Actions.EndOpenMPClause();
1151 }
1152 // The last seen token is annot_pragma_openmp_end - need to check for
1153 // extra tokens.
1154 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1155 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1156 << getOpenMPDirectiveName(DKind);
1157 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1158 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001159 }
1160 // Skip the last annot_pragma_openmp_end.
1161 ConsumeAnnotationToken();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001162 return Actions.ActOnOpenMPAllocateDirective(Loc, Helper.getIdentifiers(),
1163 Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001164 }
1165 break;
1166 }
Kelvin Li1408f912018-09-26 04:28:39 +00001167 case OMPD_requires: {
1168 SourceLocation StartLoc = ConsumeToken();
1169 SmallVector<OMPClause *, 5> Clauses;
1170 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
1171 FirstClauses(OMPC_unknown + 1);
1172 if (Tok.is(tok::annot_pragma_openmp_end)) {
Ilya Biryukovff2a9972019-02-26 11:01:50 +00001173 Diag(Tok, diag::err_omp_expected_clause)
Kelvin Li1408f912018-09-26 04:28:39 +00001174 << getOpenMPDirectiveName(OMPD_requires);
1175 break;
1176 }
1177 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1178 OpenMPClauseKind CKind = Tok.isAnnotation()
1179 ? OMPC_unknown
1180 : getOpenMPClauseKind(PP.getSpelling(Tok));
1181 Actions.StartOpenMPClause(CKind);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001182 OMPClause *Clause = ParseOpenMPClause(OMPD_requires, CKind,
1183 !FirstClauses[CKind].getInt());
1184 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1185 StopBeforeMatch);
Kelvin Li1408f912018-09-26 04:28:39 +00001186 FirstClauses[CKind].setInt(true);
1187 if (Clause != nullptr)
1188 Clauses.push_back(Clause);
1189 if (Tok.is(tok::annot_pragma_openmp_end)) {
1190 Actions.EndOpenMPClause();
1191 break;
1192 }
1193 // Skip ',' if any.
1194 if (Tok.is(tok::comma))
1195 ConsumeToken();
1196 Actions.EndOpenMPClause();
1197 }
1198 // Consume final annot_pragma_openmp_end
1199 if (Clauses.size() == 0) {
1200 Diag(Tok, diag::err_omp_expected_clause)
1201 << getOpenMPDirectiveName(OMPD_requires);
1202 ConsumeAnnotationToken();
1203 return nullptr;
1204 }
1205 ConsumeAnnotationToken();
1206 return Actions.ActOnOpenMPRequiresDirective(StartLoc, Clauses);
1207 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001208 case OMPD_declare_reduction:
1209 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001210 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001211 // The last seen token is annot_pragma_openmp_end - need to check for
1212 // extra tokens.
1213 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1214 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1215 << getOpenMPDirectiveName(OMPD_declare_reduction);
1216 while (Tok.isNot(tok::annot_pragma_openmp_end))
1217 ConsumeAnyToken();
1218 }
1219 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001220 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001221 return Res;
1222 }
1223 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001224 case OMPD_declare_mapper: {
1225 ConsumeToken();
1226 if (DeclGroupPtrTy Res = ParseOpenMPDeclareMapperDirective(AS)) {
1227 // Skip the last annot_pragma_openmp_end.
1228 ConsumeAnnotationToken();
1229 return Res;
1230 }
1231 break;
1232 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001233 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001234 case OMPD_declare_simd: {
1235 // The syntax is:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001236 // { #pragma omp declare {simd|variant} }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001237 // <function-declaration-or-definition>
1238 //
Alexey Bataev2af33e32016-04-07 12:45:37 +00001239 CachedTokens Toks;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001240 Toks.push_back(Tok);
1241 ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001242 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
1243 Toks.push_back(Tok);
1244 ConsumeAnyToken();
1245 }
1246 Toks.push_back(Tok);
1247 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +00001248
1249 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +00001250 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001251 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +00001252 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001253 // Here we expect to see some function declaration.
1254 if (AS == AS_none) {
1255 assert(TagType == DeclSpec::TST_unspecified);
1256 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001257 ParsingDeclSpec PDS(*this);
1258 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1259 } else {
1260 Ptr =
1261 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1262 }
1263 }
1264 if (!Ptr) {
Alexey Bataevd158cf62019-09-13 20:18:17 +00001265 Diag(Loc, diag::err_omp_decl_in_declare_simd_variant)
1266 << (DKind == OMPD_declare_simd ? 0 : 1);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001267 return DeclGroupPtrTy();
1268 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001269 if (DKind == OMPD_declare_simd)
1270 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
1271 assert(DKind == OMPD_declare_variant &&
1272 "Expected declare variant directive only");
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001273 ParseOMPDeclareVariantClauses(Ptr, Toks, Loc);
1274 return Ptr;
Alexey Bataev587e1de2016-03-30 10:43:55 +00001275 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001276 case OMPD_declare_target: {
1277 SourceLocation DTLoc = ConsumeAnyToken();
1278 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Kelvin Lie0502752018-11-21 20:15:57 +00001279 return ParseOMPDeclareTargetClauses();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001280 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001281
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001282 // Skip the last annot_pragma_openmp_end.
1283 ConsumeAnyToken();
1284
1285 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
1286 return DeclGroupPtrTy();
1287
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001288 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +00001289 DKind = parseOpenMPDirectiveKind(*this);
Kelvin Libc38e632018-09-10 02:07:09 +00001290 while (DKind != OMPD_end_declare_target && Tok.isNot(tok::eof) &&
1291 Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +00001292 DeclGroupPtrTy Ptr;
1293 // Here we expect to see some function declaration.
1294 if (AS == AS_none) {
1295 assert(TagType == DeclSpec::TST_unspecified);
1296 MaybeParseCXX11Attributes(Attrs);
1297 ParsingDeclSpec PDS(*this);
1298 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1299 } else {
1300 Ptr =
1301 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1302 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001303 if (Ptr) {
1304 DeclGroupRef Ref = Ptr.get();
1305 Decls.append(Ref.begin(), Ref.end());
1306 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001307 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
1308 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +00001309 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001310 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001311 if (DKind != OMPD_end_declare_target)
1312 TPA.Revert();
1313 else
1314 TPA.Commit();
1315 }
1316 }
1317
Kelvin Lie0502752018-11-21 20:15:57 +00001318 ParseOMPEndDeclareTargetDirective(DKind, DTLoc);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001319 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +00001320 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001321 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001322 case OMPD_unknown:
1323 Diag(Tok, diag::err_omp_unknown_directive);
1324 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001325 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001326 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001327 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +00001328 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001329 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001330 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001331 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +00001332 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001333 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001334 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001335 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001336 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001337 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +00001338 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001339 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001340 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001341 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001342 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001343 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +00001344 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001345 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001346 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001347 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001348 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +00001349 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001350 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001351 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001352 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001353 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001354 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001355 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001356 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001357 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001358 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001359 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +00001360 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001361 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001362 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001363 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001364 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +00001365 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +00001366 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001367 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +00001368 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +00001369 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +00001370 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +00001371 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +00001372 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +00001373 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001374 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +00001375 break;
1376 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001377 while (Tok.isNot(tok::annot_pragma_openmp_end))
1378 ConsumeAnyToken();
1379 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +00001380 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001381}
1382
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001383/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001384///
1385/// threadprivate-directive:
1386/// annot_pragma_openmp 'threadprivate' simple-variable-list
1387/// annot_pragma_openmp_end
1388///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001389/// allocate-directive:
1390/// annot_pragma_openmp 'allocate' simple-variable-list
1391/// annot_pragma_openmp_end
1392///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001393/// declare-reduction-directive:
1394/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
1395/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
1396/// ('omp_priv' '=' <expression>|<function_call>) ')']
1397/// annot_pragma_openmp_end
1398///
Michael Kruse251e1482019-02-01 20:25:04 +00001399/// declare-mapper-directive:
1400/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1401/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1402/// annot_pragma_openmp_end
1403///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001404/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001405/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001406/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
1407/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001408/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +00001409/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001410/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001411/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +00001412/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +00001413/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +00001414/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +00001415/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +00001416/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +00001417/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +00001418/// 'teams distribute parallel for' | 'target teams' |
1419/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +00001420/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +00001421/// 'target teams distribute parallel for simd' |
1422/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +00001423/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001424///
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001425StmtResult
1426Parser::ParseOpenMPDeclarativeOrExecutableDirective(ParsedStmtContext StmtCtx) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001427 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +00001428 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001429 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001430 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +00001431 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +00001432 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
1433 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +00001434 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +00001435 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001436 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001437 // Name of critical directive.
1438 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001439 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +00001440 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +00001441 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001442
1443 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001444 case OMPD_threadprivate: {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001445 // FIXME: Should this be permitted in C++?
1446 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1447 ParsedStmtContext()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001448 Diag(Tok, diag::err_omp_immediate_directive)
1449 << getOpenMPDirectiveName(DKind) << 0;
1450 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001451 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001452 DeclDirectiveListParserHelper Helper(this, DKind);
1453 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1454 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001455 // The last seen token is annot_pragma_openmp_end - need to check for
1456 // extra tokens.
1457 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1458 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001459 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001460 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001461 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001462 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
1463 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001464 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1465 }
Alp Tokerd751fa72013-12-18 19:10:49 +00001466 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001467 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001468 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001469 case OMPD_allocate: {
1470 // FIXME: Should this be permitted in C++?
1471 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1472 ParsedStmtContext()) {
1473 Diag(Tok, diag::err_omp_immediate_directive)
1474 << getOpenMPDirectiveName(DKind) << 0;
1475 }
1476 ConsumeToken();
1477 DeclDirectiveListParserHelper Helper(this, DKind);
1478 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1479 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001480 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001481 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001482 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1483 OMPC_unknown + 1>
1484 FirstClauses(OMPC_unknown + 1);
1485 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1486 OpenMPClauseKind CKind =
1487 Tok.isAnnotation() ? OMPC_unknown
1488 : getOpenMPClauseKind(PP.getSpelling(Tok));
1489 Actions.StartOpenMPClause(CKind);
1490 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1491 !FirstClauses[CKind].getInt());
1492 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1493 StopBeforeMatch);
1494 FirstClauses[CKind].setInt(true);
1495 if (Clause != nullptr)
1496 Clauses.push_back(Clause);
1497 if (Tok.is(tok::annot_pragma_openmp_end)) {
1498 Actions.EndOpenMPClause();
1499 break;
1500 }
1501 // Skip ',' if any.
1502 if (Tok.is(tok::comma))
1503 ConsumeToken();
1504 Actions.EndOpenMPClause();
1505 }
1506 // The last seen token is annot_pragma_openmp_end - need to check for
1507 // extra tokens.
1508 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1509 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1510 << getOpenMPDirectiveName(DKind);
1511 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1512 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001513 }
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001514 DeclGroupPtrTy Res = Actions.ActOnOpenMPAllocateDirective(
1515 Loc, Helper.getIdentifiers(), Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001516 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1517 }
1518 SkipUntil(tok::annot_pragma_openmp_end);
1519 break;
1520 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001521 case OMPD_declare_reduction:
1522 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001523 if (DeclGroupPtrTy Res =
1524 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001525 // The last seen token is annot_pragma_openmp_end - need to check for
1526 // extra tokens.
1527 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1528 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1529 << getOpenMPDirectiveName(OMPD_declare_reduction);
1530 while (Tok.isNot(tok::annot_pragma_openmp_end))
1531 ConsumeAnyToken();
1532 }
1533 ConsumeAnyToken();
1534 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +00001535 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001536 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +00001537 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001538 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001539 case OMPD_declare_mapper: {
1540 ConsumeToken();
1541 if (DeclGroupPtrTy Res =
1542 ParseOpenMPDeclareMapperDirective(/*AS=*/AS_none)) {
1543 // Skip the last annot_pragma_openmp_end.
1544 ConsumeAnnotationToken();
1545 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1546 } else {
1547 SkipUntil(tok::annot_pragma_openmp_end);
1548 }
1549 break;
1550 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001551 case OMPD_flush:
1552 if (PP.LookAhead(0).is(tok::l_paren)) {
1553 FlushHasClause = true;
1554 // Push copy of the current token back to stream to properly parse
1555 // pseudo-clause OMPFlushClause.
Ilya Biryukov929af672019-05-17 09:32:05 +00001556 PP.EnterToken(Tok, /*IsReinject*/ true);
Alexey Bataev6125da92014-07-21 11:26:11 +00001557 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001558 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +00001559 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001560 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001561 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001562 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001563 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001564 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001565 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +00001566 case OMPD_target_update:
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001567 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1568 ParsedStmtContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00001569 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +00001570 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +00001571 }
1572 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001573 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001574 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001575 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001576 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001577 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001578 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001579 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001580 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001581 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001582 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001583 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001584 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001585 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001586 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001587 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001588 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001589 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001590 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001591 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001592 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001593 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001594 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001595 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001596 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001597 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001598 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001599 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001600 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001601 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001602 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001603 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001604 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001605 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001606 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001607 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001608 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001609 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001610 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001611 case OMPD_target_teams_distribute_parallel_for_simd:
1612 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001613 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001614 // Parse directive name of the 'critical' directive if any.
1615 if (DKind == OMPD_critical) {
1616 BalancedDelimiterTracker T(*this, tok::l_paren,
1617 tok::annot_pragma_openmp_end);
1618 if (!T.consumeOpen()) {
1619 if (Tok.isAnyIdentifier()) {
1620 DirName =
1621 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1622 ConsumeAnyToken();
1623 } else {
1624 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1625 }
1626 T.consumeClose();
1627 }
Alexey Bataev80909872015-07-02 11:25:17 +00001628 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001629 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001630 if (Tok.isNot(tok::annot_pragma_openmp_end))
1631 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001632 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001633
Alexey Bataevf29276e2014-06-18 04:14:57 +00001634 if (isOpenMPLoopDirective(DKind))
1635 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1636 if (isOpenMPSimdDirective(DKind))
1637 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1638 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001639 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001640
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001641 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001642 OpenMPClauseKind CKind =
1643 Tok.isAnnotation()
1644 ? OMPC_unknown
1645 : FlushHasClause ? OMPC_flush
1646 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001647 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001648 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001649 OMPClause *Clause =
1650 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001651 FirstClauses[CKind].setInt(true);
1652 if (Clause) {
1653 FirstClauses[CKind].setPointer(Clause);
1654 Clauses.push_back(Clause);
1655 }
1656
1657 // Skip ',' if any.
1658 if (Tok.is(tok::comma))
1659 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001660 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001661 }
1662 // End location of the directive.
1663 EndLoc = Tok.getLocation();
1664 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001665 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001666
Alexey Bataeveb482352015-12-18 05:05:56 +00001667 // OpenMP [2.13.8, ordered Construct, Syntax]
1668 // If the depend clause is specified, the ordered construct is a stand-alone
1669 // directive.
1670 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001671 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1672 ParsedStmtContext()) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001673 Diag(Loc, diag::err_omp_immediate_directive)
1674 << getOpenMPDirectiveName(DKind) << 1
1675 << getOpenMPClauseName(OMPC_depend);
1676 }
1677 HasAssociatedStatement = false;
1678 }
1679
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001680 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001681 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001682 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001683 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001684 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1685 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1686 // should have at least one compound statement scope within it.
1687 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001688 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001689 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1690 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001691 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001692 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1693 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1694 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001695 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001696 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001697 Directive = Actions.ActOnOpenMPExecutableDirective(
1698 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1699 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001700
1701 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001702 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001703 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001704 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001705 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001706 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001707 case OMPD_declare_target:
1708 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00001709 case OMPD_requires:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001710 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001711 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001712 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001713 SkipUntil(tok::annot_pragma_openmp_end);
1714 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001715 case OMPD_unknown:
1716 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001717 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001718 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001719 }
1720 return Directive;
1721}
1722
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001723// Parses simple list:
1724// simple-variable-list:
1725// '(' id-expression {, id-expression} ')'
1726//
1727bool Parser::ParseOpenMPSimpleVarList(
1728 OpenMPDirectiveKind Kind,
1729 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1730 Callback,
1731 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001732 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001733 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001734 if (T.expectAndConsume(diag::err_expected_lparen_after,
1735 getOpenMPDirectiveName(Kind)))
1736 return true;
1737 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001738 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001739
1740 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001741 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001742 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001743 UnqualifiedId Name;
1744 // Read var name.
1745 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001746 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001747
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001748 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001749 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001750 IsCorrect = false;
1751 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001752 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001753 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00001754 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001755 IsCorrect = false;
1756 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001757 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001758 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1759 Tok.isNot(tok::annot_pragma_openmp_end)) {
1760 IsCorrect = false;
1761 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001762 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001763 Diag(PrevTok.getLocation(), diag::err_expected)
1764 << tok::identifier
1765 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001766 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001767 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001768 }
1769 // Consume ','.
1770 if (Tok.is(tok::comma)) {
1771 ConsumeToken();
1772 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001773 }
1774
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001775 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001776 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001777 IsCorrect = false;
1778 }
1779
1780 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001781 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001782
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001783 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001784}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001785
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001786/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001787///
1788/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001789/// if-clause | final-clause | num_threads-clause | safelen-clause |
1790/// default-clause | private-clause | firstprivate-clause | shared-clause
1791/// | linear-clause | aligned-clause | collapse-clause |
1792/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001793/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001794/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001795/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001796/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001797/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001798/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001799/// from-clause | is_device_ptr-clause | task_reduction-clause |
Alexey Bataeve04483e2019-03-27 14:14:31 +00001800/// in_reduction-clause | allocator-clause | allocate-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001801///
1802OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1803 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001804 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001805 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001806 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001807 // Check if clause is allowed for the given directive.
1808 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001809 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1810 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001811 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001812 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001813 }
1814
1815 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001816 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001817 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001818 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001819 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001820 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001821 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001822 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001823 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001824 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001825 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001826 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001827 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001828 case OMPC_hint:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001829 case OMPC_allocator:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001830 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001831 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001832 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001833 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001834 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001835 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001836 // OpenMP [2.9.1, target data construct, Restrictions]
1837 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001838 // OpenMP [2.11.1, task Construct, Restrictions]
1839 // At most one if clause can appear on the directive.
1840 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001841 // OpenMP [teams Construct, Restrictions]
1842 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001843 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001844 // OpenMP [2.9.1, task Construct, Restrictions]
1845 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001846 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1847 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001848 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1849 // At most one num_tasks clause can appear on the directive.
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001850 // OpenMP [2.11.3, allocate Directive, Restrictions]
1851 // At most one allocator clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001852 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001853 Diag(Tok, diag::err_omp_more_one_clause)
1854 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001855 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001856 }
1857
Alexey Bataev10e775f2015-07-30 11:36:16 +00001858 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001859 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001860 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001861 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001862 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001863 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001864 case OMPC_proc_bind:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001865 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001866 // OpenMP [2.14.3.1, Restrictions]
1867 // Only a single default clause may be specified on a parallel, task or
1868 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001869 // OpenMP [2.5, parallel Construct, Restrictions]
1870 // At most one proc_bind clause can appear on the directive.
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001871 // OpenMP [5.0, Requires directive, Restrictions]
1872 // At most one atomic_default_mem_order clause can appear
1873 // on the directive
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001874 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001875 Diag(Tok, diag::err_omp_more_one_clause)
1876 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001877 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001878 }
1879
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001880 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001881 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001882 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001883 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001884 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001885 // OpenMP [2.7.1, Restrictions, p. 3]
1886 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001887 // OpenMP [2.10.4, Restrictions, p. 106]
1888 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001889 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001890 Diag(Tok, diag::err_omp_more_one_clause)
1891 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001892 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001893 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001894 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001895
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001896 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001897 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001898 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001899 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001900 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001901 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001902 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001903 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001904 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001905 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001906 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001907 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001908 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001909 case OMPC_nogroup:
Kelvin Li1408f912018-09-26 04:28:39 +00001910 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00001911 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00001912 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00001913 case OMPC_dynamic_allocators:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001914 // OpenMP [2.7.1, Restrictions, p. 9]
1915 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001916 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1917 // Only one nowait clause can appear on a for directive.
Kelvin Li1408f912018-09-26 04:28:39 +00001918 // OpenMP [5.0, Requires directive, Restrictions]
1919 // Each of the requires clauses can appear at most once on the directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001920 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001921 Diag(Tok, diag::err_omp_more_one_clause)
1922 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001923 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001924 }
1925
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001926 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001927 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001928 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001929 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001930 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001931 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001932 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00001933 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00001934 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001935 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001936 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001937 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001938 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001939 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001940 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001941 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001942 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001943 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001944 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001945 case OMPC_is_device_ptr:
Alexey Bataeve04483e2019-03-27 14:14:31 +00001946 case OMPC_allocate:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001947 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001948 break;
Alexey Bataev729e2422019-08-23 16:11:14 +00001949 case OMPC_device_type:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001950 case OMPC_unknown:
1951 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001952 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001953 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001954 break;
1955 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001956 case OMPC_uniform:
Alexey Bataevdba792c2019-09-23 18:13:31 +00001957 case OMPC_match:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001958 if (!WrongDirective)
1959 Diag(Tok, diag::err_omp_unexpected_clause)
1960 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001961 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001962 break;
1963 }
Craig Topper161e4db2014-05-21 06:02:52 +00001964 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001965}
1966
Alexey Bataev2af33e32016-04-07 12:45:37 +00001967/// Parses simple expression in parens for single-expression clauses of OpenMP
1968/// constructs.
1969/// \param RLoc Returned location of right paren.
1970ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
Alexey Bataevd158cf62019-09-13 20:18:17 +00001971 SourceLocation &RLoc,
1972 bool IsAddressOfOperand) {
Alexey Bataev2af33e32016-04-07 12:45:37 +00001973 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1974 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1975 return ExprError();
1976
1977 SourceLocation ELoc = Tok.getLocation();
1978 ExprResult LHS(ParseCastExpression(
Alexey Bataevd158cf62019-09-13 20:18:17 +00001979 /*isUnaryExpression=*/false, IsAddressOfOperand, NotTypeCast));
Alexey Bataev2af33e32016-04-07 12:45:37 +00001980 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00001981 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev2af33e32016-04-07 12:45:37 +00001982
1983 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001984 RLoc = Tok.getLocation();
1985 if (!T.consumeClose())
1986 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001987
Alexey Bataev2af33e32016-04-07 12:45:37 +00001988 return Val;
1989}
1990
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001991/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001992/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001993/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001994///
Alexey Bataev3778b602014-07-17 07:32:53 +00001995/// final-clause:
1996/// 'final' '(' expression ')'
1997///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001998/// num_threads-clause:
1999/// 'num_threads' '(' expression ')'
2000///
2001/// safelen-clause:
2002/// 'safelen' '(' expression ')'
2003///
Alexey Bataev66b15b52015-08-21 11:14:16 +00002004/// simdlen-clause:
2005/// 'simdlen' '(' expression ')'
2006///
Alexander Musman8bd31e62014-05-27 15:12:19 +00002007/// collapse-clause:
2008/// 'collapse' '(' expression ')'
2009///
Alexey Bataeva0569352015-12-01 10:17:31 +00002010/// priority-clause:
2011/// 'priority' '(' expression ')'
2012///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002013/// grainsize-clause:
2014/// 'grainsize' '(' expression ')'
2015///
Alexey Bataev382967a2015-12-08 12:06:20 +00002016/// num_tasks-clause:
2017/// 'num_tasks' '(' expression ')'
2018///
Alexey Bataev28c75412015-12-15 08:19:24 +00002019/// hint-clause:
2020/// 'hint' '(' expression ')'
2021///
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002022/// allocator-clause:
2023/// 'allocator' '(' expression ')'
2024///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002025OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
2026 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002027 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00002028 SourceLocation LLoc = Tok.getLocation();
2029 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002030
Alexey Bataev2af33e32016-04-07 12:45:37 +00002031 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002032
2033 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00002034 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002035
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002036 if (ParseOnly)
2037 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00002038 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002039}
2040
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002041/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002042///
2043/// default-clause:
2044/// 'default' '(' 'none' | 'shared' ')
2045///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002046/// proc_bind-clause:
2047/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
2048///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002049OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
2050 bool ParseOnly) {
Alexey Bataev729e2422019-08-23 16:11:14 +00002051 llvm::Optional<SimpleClauseData> Val = parseOpenMPSimpleClause(*this, Kind);
2052 if (!Val || ParseOnly)
Craig Topper161e4db2014-05-21 06:02:52 +00002053 return nullptr;
Alexey Bataev729e2422019-08-23 16:11:14 +00002054 return Actions.ActOnOpenMPSimpleClause(
2055 Kind, Val.getValue().Type, Val.getValue().TypeLoc, Val.getValue().LOpen,
2056 Val.getValue().Loc, Val.getValue().RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002057}
2058
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002059/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002060///
2061/// ordered-clause:
2062/// 'ordered'
2063///
Alexey Bataev236070f2014-06-20 11:19:47 +00002064/// nowait-clause:
2065/// 'nowait'
2066///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002067/// untied-clause:
2068/// 'untied'
2069///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002070/// mergeable-clause:
2071/// 'mergeable'
2072///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002073/// read-clause:
2074/// 'read'
2075///
Alexey Bataev346265e2015-09-25 10:37:12 +00002076/// threads-clause:
2077/// 'threads'
2078///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002079/// simd-clause:
2080/// 'simd'
2081///
Alexey Bataevb825de12015-12-07 10:51:44 +00002082/// nogroup-clause:
2083/// 'nogroup'
2084///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002085OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002086 SourceLocation Loc = Tok.getLocation();
2087 ConsumeAnyToken();
2088
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002089 if (ParseOnly)
2090 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002091 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
2092}
2093
2094
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002095/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00002096/// argument like 'schedule' or 'dist_schedule'.
2097///
2098/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00002099/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
2100/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00002101///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002102/// if-clause:
2103/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
2104///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002105/// defaultmap:
2106/// 'defaultmap' '(' modifier ':' kind ')'
2107///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002108OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
2109 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00002110 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002111 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002112 // Parse '('.
2113 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2114 if (T.expectAndConsume(diag::err_expected_lparen_after,
2115 getOpenMPClauseName(Kind)))
2116 return nullptr;
2117
2118 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002119 SmallVector<unsigned, 4> Arg;
2120 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002121 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00002122 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
2123 Arg.resize(NumberOfElements);
2124 KLoc.resize(NumberOfElements);
2125 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
2126 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
2127 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00002128 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002129 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00002130 if (KindModifier > OMPC_SCHEDULE_unknown) {
2131 // Parse 'modifier'
2132 Arg[Modifier1] = KindModifier;
2133 KLoc[Modifier1] = Tok.getLocation();
2134 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2135 Tok.isNot(tok::annot_pragma_openmp_end))
2136 ConsumeAnyToken();
2137 if (Tok.is(tok::comma)) {
2138 // Parse ',' 'modifier'
2139 ConsumeAnyToken();
2140 KindModifier = getOpenMPSimpleClauseType(
2141 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2142 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
2143 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00002144 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002145 KLoc[Modifier2] = Tok.getLocation();
2146 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2147 Tok.isNot(tok::annot_pragma_openmp_end))
2148 ConsumeAnyToken();
2149 }
2150 // Parse ':'
2151 if (Tok.is(tok::colon))
2152 ConsumeAnyToken();
2153 else
2154 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
2155 KindModifier = getOpenMPSimpleClauseType(
2156 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2157 }
2158 Arg[ScheduleKind] = KindModifier;
2159 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002160 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2161 Tok.isNot(tok::annot_pragma_openmp_end))
2162 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00002163 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
2164 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
2165 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002166 Tok.is(tok::comma))
2167 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00002168 } else if (Kind == OMPC_dist_schedule) {
2169 Arg.push_back(getOpenMPSimpleClauseType(
2170 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2171 KLoc.push_back(Tok.getLocation());
2172 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2173 Tok.isNot(tok::annot_pragma_openmp_end))
2174 ConsumeAnyToken();
2175 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
2176 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002177 } else if (Kind == OMPC_defaultmap) {
2178 // Get a defaultmap modifier
2179 Arg.push_back(getOpenMPSimpleClauseType(
2180 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2181 KLoc.push_back(Tok.getLocation());
2182 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2183 Tok.isNot(tok::annot_pragma_openmp_end))
2184 ConsumeAnyToken();
2185 // Parse ':'
2186 if (Tok.is(tok::colon))
2187 ConsumeAnyToken();
2188 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
2189 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
2190 // Get a defaultmap kind
2191 Arg.push_back(getOpenMPSimpleClauseType(
2192 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2193 KLoc.push_back(Tok.getLocation());
2194 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2195 Tok.isNot(tok::annot_pragma_openmp_end))
2196 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002197 } else {
2198 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00002199 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002200 TentativeParsingAction TPA(*this);
Alexey Bataev61908f652018-04-23 19:53:05 +00002201 Arg.push_back(parseOpenMPDirectiveKind(*this));
Alexey Bataev6402bca2015-12-28 07:25:51 +00002202 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002203 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002204 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
2205 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002206 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002207 } else {
2208 TPA.Revert();
2209 Arg.back() = OMPD_unknown;
2210 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002211 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002212 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00002213 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002214 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00002215
Carlo Bertollib4adf552016-01-15 18:50:31 +00002216 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
2217 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
2218 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002219 if (NeedAnExpression) {
2220 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002221 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
2222 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002223 Val =
2224 Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002225 }
2226
2227 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002228 SourceLocation RLoc = Tok.getLocation();
2229 if (!T.consumeClose())
2230 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002231
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002232 if (NeedAnExpression && Val.isInvalid())
2233 return nullptr;
2234
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002235 if (ParseOnly)
2236 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002237 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002238 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002239}
2240
Alexey Bataevc5e02582014-06-16 07:08:35 +00002241static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
2242 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002243 if (ReductionIdScopeSpec.isEmpty()) {
2244 auto OOK = OO_None;
2245 switch (P.getCurToken().getKind()) {
2246 case tok::plus:
2247 OOK = OO_Plus;
2248 break;
2249 case tok::minus:
2250 OOK = OO_Minus;
2251 break;
2252 case tok::star:
2253 OOK = OO_Star;
2254 break;
2255 case tok::amp:
2256 OOK = OO_Amp;
2257 break;
2258 case tok::pipe:
2259 OOK = OO_Pipe;
2260 break;
2261 case tok::caret:
2262 OOK = OO_Caret;
2263 break;
2264 case tok::ampamp:
2265 OOK = OO_AmpAmp;
2266 break;
2267 case tok::pipepipe:
2268 OOK = OO_PipePipe;
2269 break;
2270 default:
2271 break;
2272 }
2273 if (OOK != OO_None) {
2274 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00002275 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00002276 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
2277 return false;
2278 }
2279 }
2280 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
2281 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00002282 /*AllowConstructorName*/ false,
2283 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00002284 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002285}
2286
Kelvin Lief579432018-12-18 22:18:41 +00002287/// Checks if the token is a valid map-type-modifier.
2288static OpenMPMapModifierKind isMapModifier(Parser &P) {
2289 Token Tok = P.getCurToken();
2290 if (!Tok.is(tok::identifier))
2291 return OMPC_MAP_MODIFIER_unknown;
2292
2293 Preprocessor &PP = P.getPreprocessor();
2294 OpenMPMapModifierKind TypeModifier = static_cast<OpenMPMapModifierKind>(
2295 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2296 return TypeModifier;
2297}
2298
Michael Kruse01f670d2019-02-22 22:29:42 +00002299/// Parse the mapper modifier in map, to, and from clauses.
2300bool Parser::parseMapperModifier(OpenMPVarListDataTy &Data) {
2301 // Parse '('.
2302 BalancedDelimiterTracker T(*this, tok::l_paren, tok::colon);
2303 if (T.expectAndConsume(diag::err_expected_lparen_after, "mapper")) {
2304 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2305 StopBeforeMatch);
2306 return true;
2307 }
2308 // Parse mapper-identifier
2309 if (getLangOpts().CPlusPlus)
2310 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
2311 /*ObjectType=*/nullptr,
2312 /*EnteringContext=*/false);
2313 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
2314 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
2315 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2316 StopBeforeMatch);
2317 return true;
2318 }
2319 auto &DeclNames = Actions.getASTContext().DeclarationNames;
2320 Data.ReductionOrMapperId = DeclarationNameInfo(
2321 DeclNames.getIdentifier(Tok.getIdentifierInfo()), Tok.getLocation());
2322 ConsumeToken();
2323 // Parse ')'.
2324 return T.consumeClose();
2325}
2326
Kelvin Lief579432018-12-18 22:18:41 +00002327/// Parse map-type-modifiers in map clause.
2328/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002329/// where, map-type-modifier ::= always | close | mapper(mapper-identifier)
2330bool Parser::parseMapTypeModifiers(OpenMPVarListDataTy &Data) {
2331 while (getCurToken().isNot(tok::colon)) {
2332 OpenMPMapModifierKind TypeModifier = isMapModifier(*this);
Kelvin Lief579432018-12-18 22:18:41 +00002333 if (TypeModifier == OMPC_MAP_MODIFIER_always ||
2334 TypeModifier == OMPC_MAP_MODIFIER_close) {
2335 Data.MapTypeModifiers.push_back(TypeModifier);
2336 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
Michael Kruse4304e9d2019-02-19 16:38:20 +00002337 ConsumeToken();
2338 } else if (TypeModifier == OMPC_MAP_MODIFIER_mapper) {
2339 Data.MapTypeModifiers.push_back(TypeModifier);
2340 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
2341 ConsumeToken();
Michael Kruse01f670d2019-02-22 22:29:42 +00002342 if (parseMapperModifier(Data))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002343 return true;
Kelvin Lief579432018-12-18 22:18:41 +00002344 } else {
2345 // For the case of unknown map-type-modifier or a map-type.
2346 // Map-type is followed by a colon; the function returns when it
2347 // encounters a token followed by a colon.
2348 if (Tok.is(tok::comma)) {
Michael Kruse4304e9d2019-02-19 16:38:20 +00002349 Diag(Tok, diag::err_omp_map_type_modifier_missing);
2350 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002351 continue;
2352 }
2353 // Potential map-type token as it is followed by a colon.
2354 if (PP.LookAhead(0).is(tok::colon))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002355 return false;
2356 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
2357 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002358 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002359 if (getCurToken().is(tok::comma))
2360 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002361 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002362 return false;
Kelvin Lief579432018-12-18 22:18:41 +00002363}
2364
2365/// Checks if the token is a valid map-type.
2366static OpenMPMapClauseKind isMapType(Parser &P) {
2367 Token Tok = P.getCurToken();
2368 // The map-type token can be either an identifier or the C++ delete keyword.
2369 if (!Tok.isOneOf(tok::identifier, tok::kw_delete))
2370 return OMPC_MAP_unknown;
2371 Preprocessor &PP = P.getPreprocessor();
2372 OpenMPMapClauseKind MapType = static_cast<OpenMPMapClauseKind>(
2373 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2374 return MapType;
2375}
2376
2377/// Parse map-type in map clause.
2378/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002379/// where, map-type ::= to | from | tofrom | alloc | release | delete
Kelvin Lief579432018-12-18 22:18:41 +00002380static void parseMapType(Parser &P, Parser::OpenMPVarListDataTy &Data) {
2381 Token Tok = P.getCurToken();
2382 if (Tok.is(tok::colon)) {
2383 P.Diag(Tok, diag::err_omp_map_type_missing);
2384 return;
2385 }
2386 Data.MapType = isMapType(P);
2387 if (Data.MapType == OMPC_MAP_unknown)
2388 P.Diag(Tok, diag::err_omp_unknown_map_type);
2389 P.ConsumeToken();
2390}
2391
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002392/// Parses clauses with list.
2393bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
2394 OpenMPClauseKind Kind,
2395 SmallVectorImpl<Expr *> &Vars,
2396 OpenMPVarListDataTy &Data) {
2397 UnqualifiedId UnqualifiedReductionId;
2398 bool InvalidReductionId = false;
Michael Kruse01f670d2019-02-22 22:29:42 +00002399 bool IsInvalidMapperModifier = false;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002400
2401 // Parse '('.
2402 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2403 if (T.expectAndConsume(diag::err_expected_lparen_after,
2404 getOpenMPClauseName(Kind)))
2405 return true;
2406
2407 bool NeedRParenForLinear = false;
2408 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
2409 tok::annot_pragma_openmp_end);
2410 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00002411 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
2412 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002413 ColonProtectionRAIIObject ColonRAII(*this);
2414 if (getLangOpts().CPlusPlus)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002415 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002416 /*ObjectType=*/nullptr,
2417 /*EnteringContext=*/false);
Michael Kruse4304e9d2019-02-19 16:38:20 +00002418 InvalidReductionId = ParseReductionId(
2419 *this, Data.ReductionOrMapperIdScopeSpec, UnqualifiedReductionId);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002420 if (InvalidReductionId) {
2421 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2422 StopBeforeMatch);
2423 }
2424 if (Tok.is(tok::colon))
2425 Data.ColonLoc = ConsumeToken();
2426 else
2427 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
2428 if (!InvalidReductionId)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002429 Data.ReductionOrMapperId =
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002430 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
2431 } else if (Kind == OMPC_depend) {
2432 // Handle dependency type for depend clause.
2433 ColonProtectionRAIIObject ColonRAII(*this);
2434 Data.DepKind =
2435 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
2436 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
2437 Data.DepLinMapLoc = Tok.getLocation();
2438
2439 if (Data.DepKind == OMPC_DEPEND_unknown) {
2440 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2441 StopBeforeMatch);
2442 } else {
2443 ConsumeToken();
2444 // Special processing for depend(source) clause.
2445 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
2446 // Parse ')'.
2447 T.consumeClose();
2448 return false;
2449 }
2450 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002451 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002452 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00002453 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002454 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
2455 : diag::warn_pragma_expected_colon)
2456 << "dependency type";
2457 }
2458 } else if (Kind == OMPC_linear) {
2459 // Try to parse modifier if any.
2460 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
2461 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
2462 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2463 Data.DepLinMapLoc = ConsumeToken();
2464 LinearT.consumeOpen();
2465 NeedRParenForLinear = true;
2466 }
2467 } else if (Kind == OMPC_map) {
2468 // Handle map type for map clause.
2469 ColonProtectionRAIIObject ColonRAII(*this);
2470
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002471 // The first identifier may be a list item, a map-type or a
Kelvin Lief579432018-12-18 22:18:41 +00002472 // map-type-modifier. The map-type can also be delete which has the same
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002473 // spelling of the C++ delete keyword.
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002474 Data.DepLinMapLoc = Tok.getLocation();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002475
Kelvin Lief579432018-12-18 22:18:41 +00002476 // Check for presence of a colon in the map clause.
2477 TentativeParsingAction TPA(*this);
2478 bool ColonPresent = false;
2479 if (SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2480 StopBeforeMatch)) {
2481 if (Tok.is(tok::colon))
2482 ColonPresent = true;
2483 }
2484 TPA.Revert();
2485 // Only parse map-type-modifier[s] and map-type if a colon is present in
2486 // the map clause.
2487 if (ColonPresent) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002488 IsInvalidMapperModifier = parseMapTypeModifiers(Data);
2489 if (!IsInvalidMapperModifier)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002490 parseMapType(*this, Data);
Michael Kruse01f670d2019-02-22 22:29:42 +00002491 else
2492 SkipUntil(tok::colon, tok::annot_pragma_openmp_end, StopBeforeMatch);
Kelvin Lief579432018-12-18 22:18:41 +00002493 }
2494 if (Data.MapType == OMPC_MAP_unknown) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002495 Data.MapType = OMPC_MAP_tofrom;
2496 Data.IsMapTypeImplicit = true;
2497 }
2498
2499 if (Tok.is(tok::colon))
2500 Data.ColonLoc = ConsumeToken();
Michael Kruse0336c752019-02-25 20:34:15 +00002501 } else if (Kind == OMPC_to || Kind == OMPC_from) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002502 if (Tok.is(tok::identifier)) {
2503 bool IsMapperModifier = false;
Michael Kruse0336c752019-02-25 20:34:15 +00002504 if (Kind == OMPC_to) {
2505 auto Modifier = static_cast<OpenMPToModifierKind>(
2506 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2507 if (Modifier == OMPC_TO_MODIFIER_mapper)
2508 IsMapperModifier = true;
2509 } else {
2510 auto Modifier = static_cast<OpenMPFromModifierKind>(
2511 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2512 if (Modifier == OMPC_FROM_MODIFIER_mapper)
2513 IsMapperModifier = true;
2514 }
Michael Kruse01f670d2019-02-22 22:29:42 +00002515 if (IsMapperModifier) {
2516 // Parse the mapper modifier.
2517 ConsumeToken();
2518 IsInvalidMapperModifier = parseMapperModifier(Data);
2519 if (Tok.isNot(tok::colon)) {
2520 if (!IsInvalidMapperModifier)
2521 Diag(Tok, diag::warn_pragma_expected_colon) << ")";
2522 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2523 StopBeforeMatch);
2524 }
2525 // Consume ':'.
2526 if (Tok.is(tok::colon))
2527 ConsumeToken();
2528 }
2529 }
Alexey Bataeve04483e2019-03-27 14:14:31 +00002530 } else if (Kind == OMPC_allocate) {
2531 // Handle optional allocator expression followed by colon delimiter.
2532 ColonProtectionRAIIObject ColonRAII(*this);
2533 TentativeParsingAction TPA(*this);
2534 ExprResult Tail =
2535 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
2536 Tail = Actions.ActOnFinishFullExpr(Tail.get(), T.getOpenLocation(),
2537 /*DiscardedValue=*/false);
2538 if (Tail.isUsable()) {
2539 if (Tok.is(tok::colon)) {
2540 Data.TailExpr = Tail.get();
2541 Data.ColonLoc = ConsumeToken();
2542 TPA.Commit();
2543 } else {
2544 // colon not found, no allocator specified, parse only list of
2545 // variables.
2546 TPA.Revert();
2547 }
2548 } else {
2549 // Parsing was unsuccessfull, revert and skip to the end of clause or
2550 // directive.
2551 TPA.Revert();
2552 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2553 StopBeforeMatch);
2554 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002555 }
2556
Alexey Bataevfa312f32017-07-21 18:48:21 +00002557 bool IsComma =
2558 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
2559 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
2560 (Kind == OMPC_reduction && !InvalidReductionId) ||
Kelvin Lida6bc702018-11-21 19:38:53 +00002561 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown) ||
Alexey Bataevfa312f32017-07-21 18:48:21 +00002562 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002563 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
2564 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
2565 Tok.isNot(tok::annot_pragma_openmp_end))) {
2566 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
2567 // Parse variable
2568 ExprResult VarExpr =
2569 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00002570 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002571 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00002572 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002573 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2574 StopBeforeMatch);
2575 }
2576 // Skip ',' if any
2577 IsComma = Tok.is(tok::comma);
2578 if (IsComma)
2579 ConsumeToken();
2580 else if (Tok.isNot(tok::r_paren) &&
2581 Tok.isNot(tok::annot_pragma_openmp_end) &&
2582 (!MayHaveTail || Tok.isNot(tok::colon)))
2583 Diag(Tok, diag::err_omp_expected_punc)
2584 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
2585 : getOpenMPClauseName(Kind))
2586 << (Kind == OMPC_flush);
2587 }
2588
2589 // Parse ')' for linear clause with modifier.
2590 if (NeedRParenForLinear)
2591 LinearT.consumeClose();
2592
2593 // Parse ':' linear-step (or ':' alignment).
2594 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
2595 if (MustHaveTail) {
2596 Data.ColonLoc = Tok.getLocation();
2597 SourceLocation ELoc = ConsumeToken();
2598 ExprResult Tail = ParseAssignmentExpression();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002599 Tail =
2600 Actions.ActOnFinishFullExpr(Tail.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002601 if (Tail.isUsable())
2602 Data.TailExpr = Tail.get();
2603 else
2604 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2605 StopBeforeMatch);
2606 }
2607
2608 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002609 Data.RLoc = Tok.getLocation();
2610 if (!T.consumeClose())
2611 Data.RLoc = T.getCloseLocation();
Alexey Bataev61908f652018-04-23 19:53:05 +00002612 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
2613 Vars.empty()) ||
2614 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
Michael Kruse4304e9d2019-02-19 16:38:20 +00002615 (MustHaveTail && !Data.TailExpr) || InvalidReductionId ||
Michael Kruse01f670d2019-02-22 22:29:42 +00002616 IsInvalidMapperModifier;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002617}
2618
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002619/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00002620/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
2621/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002622///
2623/// private-clause:
2624/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002625/// firstprivate-clause:
2626/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00002627/// lastprivate-clause:
2628/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00002629/// shared-clause:
2630/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00002631/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00002632/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002633/// aligned-clause:
2634/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00002635/// reduction-clause:
2636/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00002637/// task_reduction-clause:
2638/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00002639/// in_reduction-clause:
2640/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00002641/// copyprivate-clause:
2642/// 'copyprivate' '(' list ')'
2643/// flush-clause:
2644/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002645/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00002646/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00002647/// map-clause:
Kelvin Lief579432018-12-18 22:18:41 +00002648/// 'map' '(' [ [ always [,] ] [ close [,] ]
Michael Kruse01f670d2019-02-22 22:29:42 +00002649/// [ mapper '(' mapper-identifier ')' [,] ]
Kelvin Li0bff7af2015-11-23 05:32:03 +00002650/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00002651/// to-clause:
Michael Kruse01f670d2019-02-22 22:29:42 +00002652/// 'to' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00002653/// from-clause:
Michael Kruse0336c752019-02-25 20:34:15 +00002654/// 'from' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00002655/// use_device_ptr-clause:
2656/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00002657/// is_device_ptr-clause:
2658/// 'is_device_ptr' '(' list ')'
Alexey Bataeve04483e2019-03-27 14:14:31 +00002659/// allocate-clause:
2660/// 'allocate' '(' [ allocator ':' ] list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002661///
Alexey Bataev182227b2015-08-20 10:54:39 +00002662/// For 'linear' clause linear-list may have the following forms:
2663/// list
2664/// modifier(list)
2665/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00002666OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002667 OpenMPClauseKind Kind,
2668 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002669 SourceLocation Loc = Tok.getLocation();
2670 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002671 SmallVector<Expr *, 4> Vars;
2672 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002673
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002674 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00002675 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002676
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002677 if (ParseOnly)
2678 return nullptr;
Michael Kruse4304e9d2019-02-19 16:38:20 +00002679 OMPVarListLocTy Locs(Loc, LOpen, Data.RLoc);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002680 return Actions.ActOnOpenMPVarListClause(
Michael Kruse4304e9d2019-02-19 16:38:20 +00002681 Kind, Vars, Data.TailExpr, Locs, Data.ColonLoc,
2682 Data.ReductionOrMapperIdScopeSpec, Data.ReductionOrMapperId, Data.DepKind,
2683 Data.LinKind, Data.MapTypeModifiers, Data.MapTypeModifiersLoc,
2684 Data.MapType, Data.IsMapTypeImplicit, Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002685}
2686