blob: 572863c164fe435bcfa4a0deeca4c650999d4c0d [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexey Bataeva769e072013-03-22 06:34:35 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009/// This file implements parsing of all OpenMP directives and clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000010///
11//===----------------------------------------------------------------------===//
12
Alexey Bataev9959db52014-05-06 10:08:46 +000013#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000014#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000015#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000016#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000017#include "clang/Parse/RAIIObjectsForParser.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000018#include "clang/Sema/Scope.h"
19#include "llvm/ADT/PointerIntPair.h"
Michael Wong65f367f2015-07-21 13:44:28 +000020
Alexey Bataeva769e072013-03-22 06:34:35 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// OpenMP declarative directives.
25//===----------------------------------------------------------------------===//
26
Dmitry Polukhin82478332016-02-13 06:53:38 +000027namespace {
28enum OpenMPDirectiveKindEx {
29 OMPD_cancellation = OMPD_unknown + 1,
30 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000031 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000032 OMPD_end,
33 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000034 OMPD_enter,
35 OMPD_exit,
36 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000037 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000038 OMPD_target_enter,
Samuel Antao686c70c2016-05-26 17:30:50 +000039 OMPD_target_exit,
40 OMPD_update,
Kelvin Li579e41c2016-11-30 23:51:03 +000041 OMPD_distribute_parallel,
Kelvin Li80e8f562016-12-29 22:16:30 +000042 OMPD_teams_distribute_parallel,
Michael Kruse251e1482019-02-01 20:25:04 +000043 OMPD_target_teams_distribute_parallel,
44 OMPD_mapper,
Dmitry Polukhin82478332016-02-13 06:53:38 +000045};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000046
Alexey Bataev25ed0c02019-03-07 17:54:44 +000047class DeclDirectiveListParserHelper final {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000048 SmallVector<Expr *, 4> Identifiers;
49 Parser *P;
Alexey Bataev25ed0c02019-03-07 17:54:44 +000050 OpenMPDirectiveKind Kind;
Dmitry Polukhind69b5052016-05-09 14:59:13 +000051
52public:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000053 DeclDirectiveListParserHelper(Parser *P, OpenMPDirectiveKind Kind)
54 : P(P), Kind(Kind) {}
Dmitry Polukhind69b5052016-05-09 14:59:13 +000055 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
Alexey Bataev25ed0c02019-03-07 17:54:44 +000056 ExprResult Res = P->getActions().ActOnOpenMPIdExpression(
57 P->getCurScope(), SS, NameInfo, Kind);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000058 if (Res.isUsable())
59 Identifiers.push_back(Res.get());
60 }
61 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
62};
Dmitry Polukhin82478332016-02-13 06:53:38 +000063} // namespace
64
65// Map token string to extended OMP token kind that are
66// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
67static unsigned getOpenMPDirectiveKindEx(StringRef S) {
68 auto DKind = getOpenMPDirectiveKind(S);
69 if (DKind != OMPD_unknown)
70 return DKind;
71
72 return llvm::StringSwitch<unsigned>(S)
73 .Case("cancellation", OMPD_cancellation)
74 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000075 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000076 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000077 .Case("enter", OMPD_enter)
78 .Case("exit", OMPD_exit)
79 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000080 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000081 .Case("update", OMPD_update)
Michael Kruse251e1482019-02-01 20:25:04 +000082 .Case("mapper", OMPD_mapper)
Dmitry Polukhin82478332016-02-13 06:53:38 +000083 .Default(OMPD_unknown);
84}
85
Alexey Bataev61908f652018-04-23 19:53:05 +000086static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000087 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
88 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
89 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000090 static const unsigned F[][3] = {
Alexey Bataev61908f652018-04-23 19:53:05 +000091 {OMPD_cancellation, OMPD_point, OMPD_cancellation_point},
92 {OMPD_declare, OMPD_reduction, OMPD_declare_reduction},
Michael Kruse251e1482019-02-01 20:25:04 +000093 {OMPD_declare, OMPD_mapper, OMPD_declare_mapper},
Alexey Bataev61908f652018-04-23 19:53:05 +000094 {OMPD_declare, OMPD_simd, OMPD_declare_simd},
95 {OMPD_declare, OMPD_target, OMPD_declare_target},
96 {OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel},
97 {OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for},
98 {OMPD_distribute_parallel_for, OMPD_simd,
99 OMPD_distribute_parallel_for_simd},
100 {OMPD_distribute, OMPD_simd, OMPD_distribute_simd},
101 {OMPD_end, OMPD_declare, OMPD_end_declare},
102 {OMPD_end_declare, OMPD_target, OMPD_end_declare_target},
103 {OMPD_target, OMPD_data, OMPD_target_data},
104 {OMPD_target, OMPD_enter, OMPD_target_enter},
105 {OMPD_target, OMPD_exit, OMPD_target_exit},
106 {OMPD_target, OMPD_update, OMPD_target_update},
107 {OMPD_target_enter, OMPD_data, OMPD_target_enter_data},
108 {OMPD_target_exit, OMPD_data, OMPD_target_exit_data},
109 {OMPD_for, OMPD_simd, OMPD_for_simd},
110 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
111 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
112 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
113 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd},
114 {OMPD_target, OMPD_parallel, OMPD_target_parallel},
115 {OMPD_target, OMPD_simd, OMPD_target_simd},
116 {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for},
117 {OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd},
118 {OMPD_teams, OMPD_distribute, OMPD_teams_distribute},
119 {OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd},
120 {OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel},
121 {OMPD_teams_distribute_parallel, OMPD_for,
122 OMPD_teams_distribute_parallel_for},
123 {OMPD_teams_distribute_parallel_for, OMPD_simd,
124 OMPD_teams_distribute_parallel_for_simd},
125 {OMPD_target, OMPD_teams, OMPD_target_teams},
126 {OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute},
127 {OMPD_target_teams_distribute, OMPD_parallel,
128 OMPD_target_teams_distribute_parallel},
129 {OMPD_target_teams_distribute, OMPD_simd,
130 OMPD_target_teams_distribute_simd},
131 {OMPD_target_teams_distribute_parallel, OMPD_for,
132 OMPD_target_teams_distribute_parallel_for},
133 {OMPD_target_teams_distribute_parallel_for, OMPD_simd,
134 OMPD_target_teams_distribute_parallel_for_simd}};
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000135 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev61908f652018-04-23 19:53:05 +0000136 Token Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000137 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000138 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000139 ? static_cast<unsigned>(OMPD_unknown)
140 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
141 if (DKind == OMPD_unknown)
142 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000143
Alexey Bataev61908f652018-04-23 19:53:05 +0000144 for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) {
145 if (DKind != F[I][0])
Dmitry Polukhin82478332016-02-13 06:53:38 +0000146 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000147
Dmitry Polukhin82478332016-02-13 06:53:38 +0000148 Tok = P.getPreprocessor().LookAhead(0);
149 unsigned SDKind =
150 Tok.isAnnotation()
151 ? static_cast<unsigned>(OMPD_unknown)
152 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
153 if (SDKind == OMPD_unknown)
154 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000155
Alexey Bataev61908f652018-04-23 19:53:05 +0000156 if (SDKind == F[I][1]) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000157 P.ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000158 DKind = F[I][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000159 }
160 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000161 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
162 : OMPD_unknown;
163}
164
165static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000166 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000167 Sema &Actions = P.getActions();
168 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000169 // Allow to use 'operator' keyword for C++ operators
170 bool WithOperator = false;
171 if (Tok.is(tok::kw_operator)) {
172 P.ConsumeToken();
173 Tok = P.getCurToken();
174 WithOperator = true;
175 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000176 switch (Tok.getKind()) {
177 case tok::plus: // '+'
178 OOK = OO_Plus;
179 break;
180 case tok::minus: // '-'
181 OOK = OO_Minus;
182 break;
183 case tok::star: // '*'
184 OOK = OO_Star;
185 break;
186 case tok::amp: // '&'
187 OOK = OO_Amp;
188 break;
189 case tok::pipe: // '|'
190 OOK = OO_Pipe;
191 break;
192 case tok::caret: // '^'
193 OOK = OO_Caret;
194 break;
195 case tok::ampamp: // '&&'
196 OOK = OO_AmpAmp;
197 break;
198 case tok::pipepipe: // '||'
199 OOK = OO_PipePipe;
200 break;
201 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000202 if (!WithOperator)
203 break;
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000204 LLVM_FALLTHROUGH;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000205 default:
206 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
207 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
208 Parser::StopBeforeMatch);
209 return DeclarationName();
210 }
211 P.ConsumeToken();
212 auto &DeclNames = Actions.getASTContext().DeclarationNames;
213 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
214 : DeclNames.getCXXOperatorName(OOK);
215}
216
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000217/// Parse 'omp declare reduction' construct.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000218///
219/// declare-reduction-directive:
220/// annot_pragma_openmp 'declare' 'reduction'
221/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
222/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
223/// annot_pragma_openmp_end
224/// <reduction_id> is either a base language identifier or one of the following
225/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
226///
227Parser::DeclGroupPtrTy
228Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
229 // Parse '('.
230 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
231 if (T.expectAndConsume(diag::err_expected_lparen_after,
232 getOpenMPDirectiveName(OMPD_declare_reduction))) {
233 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
234 return DeclGroupPtrTy();
235 }
236
237 DeclarationName Name = parseOpenMPReductionId(*this);
238 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
239 return DeclGroupPtrTy();
240
241 // Consume ':'.
242 bool IsCorrect = !ExpectAndConsume(tok::colon);
243
244 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
245 return DeclGroupPtrTy();
246
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000247 IsCorrect = IsCorrect && !Name.isEmpty();
248
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000249 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
250 Diag(Tok.getLocation(), diag::err_expected_type);
251 IsCorrect = false;
252 }
253
254 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
255 return DeclGroupPtrTy();
256
257 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
258 // Parse list of types until ':' token.
259 do {
260 ColonProtectionRAIIObject ColonRAII(*this);
261 SourceRange Range;
Faisal Vali421b2d12017-12-29 05:41:00 +0000262 TypeResult TR =
263 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000264 if (TR.isUsable()) {
Alexey Bataev61908f652018-04-23 19:53:05 +0000265 QualType ReductionType =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000266 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
267 if (!ReductionType.isNull()) {
268 ReductionTypes.push_back(
269 std::make_pair(ReductionType, Range.getBegin()));
270 }
271 } else {
272 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
273 StopBeforeMatch);
274 }
275
276 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
277 break;
278
279 // Consume ','.
280 if (ExpectAndConsume(tok::comma)) {
281 IsCorrect = false;
282 if (Tok.is(tok::annot_pragma_openmp_end)) {
283 Diag(Tok.getLocation(), diag::err_expected_type);
284 return DeclGroupPtrTy();
285 }
286 }
287 } while (Tok.isNot(tok::annot_pragma_openmp_end));
288
289 if (ReductionTypes.empty()) {
290 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
291 return DeclGroupPtrTy();
292 }
293
294 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
295 return DeclGroupPtrTy();
296
297 // Consume ':'.
298 if (ExpectAndConsume(tok::colon))
299 IsCorrect = false;
300
301 if (Tok.is(tok::annot_pragma_openmp_end)) {
302 Diag(Tok.getLocation(), diag::err_expected_expression);
303 return DeclGroupPtrTy();
304 }
305
306 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
307 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
308
309 // Parse <combiner> expression and then parse initializer if any for each
310 // correct type.
311 unsigned I = 0, E = ReductionTypes.size();
Alexey Bataev61908f652018-04-23 19:53:05 +0000312 for (Decl *D : DRD.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000313 TentativeParsingAction TPA(*this);
314 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000315 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000316 Scope::OpenMPDirectiveScope);
317 // Parse <combiner> expression.
318 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
319 ExprResult CombinerResult =
320 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000321 D->getLocation(), /*DiscardedValue*/ false);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000322 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
323
324 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
325 Tok.isNot(tok::annot_pragma_openmp_end)) {
326 TPA.Commit();
327 IsCorrect = false;
328 break;
329 }
330 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
331 ExprResult InitializerResult;
332 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
333 // Parse <initializer> expression.
334 if (Tok.is(tok::identifier) &&
Alexey Bataev61908f652018-04-23 19:53:05 +0000335 Tok.getIdentifierInfo()->isStr("initializer")) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000336 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000337 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000338 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
339 TPA.Commit();
340 IsCorrect = false;
341 break;
342 }
343 // Parse '('.
344 BalancedDelimiterTracker T(*this, tok::l_paren,
345 tok::annot_pragma_openmp_end);
346 IsCorrect =
347 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
348 IsCorrect;
349 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
350 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000351 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000352 Scope::OpenMPDirectiveScope);
353 // Parse expression.
Alexey Bataev070f43a2017-09-06 14:49:58 +0000354 VarDecl *OmpPrivParm =
355 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
356 D);
357 // Check if initializer is omp_priv <init_expr> or something else.
358 if (Tok.is(tok::identifier) &&
359 Tok.getIdentifierInfo()->isStr("omp_priv")) {
Alexey Bataeve6aa4692018-09-13 16:54:05 +0000360 if (Actions.getLangOpts().CPlusPlus) {
361 InitializerResult = Actions.ActOnFinishFullExpr(
362 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000363 /*DiscardedValue*/ false);
Alexey Bataeve6aa4692018-09-13 16:54:05 +0000364 } else {
365 ConsumeToken();
366 ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
367 }
Alexey Bataev070f43a2017-09-06 14:49:58 +0000368 } else {
369 InitializerResult = Actions.ActOnFinishFullExpr(
370 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000371 /*DiscardedValue*/ false);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000372 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000373 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
Alexey Bataev070f43a2017-09-06 14:49:58 +0000374 D, InitializerResult.get(), OmpPrivParm);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000375 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
376 Tok.isNot(tok::annot_pragma_openmp_end)) {
377 TPA.Commit();
378 IsCorrect = false;
379 break;
380 }
381 IsCorrect =
382 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
383 }
384 }
385
386 ++I;
387 // Revert parsing if not the last type, otherwise accept it, we're done with
388 // parsing.
389 if (I != E)
390 TPA.Revert();
391 else
392 TPA.Commit();
393 }
394 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
395 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000396}
397
Alexey Bataev070f43a2017-09-06 14:49:58 +0000398void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
399 // Parse declarator '=' initializer.
400 // If a '==' or '+=' is found, suggest a fixit to '='.
401 if (isTokenEqualOrEqualTypo()) {
402 ConsumeToken();
403
404 if (Tok.is(tok::code_completion)) {
405 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
406 Actions.FinalizeDeclaration(OmpPrivParm);
407 cutOffParsing();
408 return;
409 }
410
411 ExprResult Init(ParseInitializer());
412
413 if (Init.isInvalid()) {
414 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
415 Actions.ActOnInitializerError(OmpPrivParm);
416 } else {
417 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
418 /*DirectInit=*/false);
419 }
420 } else if (Tok.is(tok::l_paren)) {
421 // Parse C++ direct initializer: '(' expression-list ')'
422 BalancedDelimiterTracker T(*this, tok::l_paren);
423 T.consumeOpen();
424
425 ExprVector Exprs;
426 CommaLocsTy CommaLocs;
427
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000428 SourceLocation LParLoc = T.getOpenLocation();
Ilya Biryukovff2a9972019-02-26 11:01:50 +0000429 auto RunSignatureHelp = [this, OmpPrivParm, LParLoc, &Exprs]() {
430 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
431 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
432 OmpPrivParm->getLocation(), Exprs, LParLoc);
433 CalledSignatureHelp = true;
434 return PreferredType;
435 };
436 if (ParseExpressionList(Exprs, CommaLocs, [&] {
437 PreferredType.enterFunctionArgument(Tok.getLocation(),
438 RunSignatureHelp);
439 })) {
440 if (PP.isCodeCompletionReached() && !CalledSignatureHelp)
441 RunSignatureHelp();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000442 Actions.ActOnInitializerError(OmpPrivParm);
443 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
444 } else {
445 // Match the ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000446 SourceLocation RLoc = Tok.getLocation();
447 if (!T.consumeClose())
448 RLoc = T.getCloseLocation();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000449
450 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
451 "Unexpected number of commas!");
452
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000453 ExprResult Initializer =
454 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000455 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
456 /*DirectInit=*/true);
457 }
458 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
459 // Parse C++0x braced-init-list.
460 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
461
462 ExprResult Init(ParseBraceInitializer());
463
464 if (Init.isInvalid()) {
465 Actions.ActOnInitializerError(OmpPrivParm);
466 } else {
467 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
468 /*DirectInit=*/true);
469 }
470 } else {
471 Actions.ActOnUninitializedDecl(OmpPrivParm);
472 }
473}
474
Michael Kruse251e1482019-02-01 20:25:04 +0000475/// Parses 'omp declare mapper' directive.
476///
477/// declare-mapper-directive:
478/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifier> ':']
479/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
480/// annot_pragma_openmp_end
481/// <mapper-identifier> and <var> are base language identifiers.
482///
483Parser::DeclGroupPtrTy
484Parser::ParseOpenMPDeclareMapperDirective(AccessSpecifier AS) {
485 bool IsCorrect = true;
486 // Parse '('
487 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
488 if (T.expectAndConsume(diag::err_expected_lparen_after,
489 getOpenMPDirectiveName(OMPD_declare_mapper))) {
490 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
491 return DeclGroupPtrTy();
492 }
493
494 // Parse <mapper-identifier>
495 auto &DeclNames = Actions.getASTContext().DeclarationNames;
496 DeclarationName MapperId;
497 if (PP.LookAhead(0).is(tok::colon)) {
498 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
499 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
500 IsCorrect = false;
501 } else {
502 MapperId = DeclNames.getIdentifier(Tok.getIdentifierInfo());
503 }
504 ConsumeToken();
505 // Consume ':'.
506 ExpectAndConsume(tok::colon);
507 } else {
508 // If no mapper identifier is provided, its name is "default" by default
509 MapperId =
510 DeclNames.getIdentifier(&Actions.getASTContext().Idents.get("default"));
511 }
512
513 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
514 return DeclGroupPtrTy();
515
516 // Parse <type> <var>
517 DeclarationName VName;
518 QualType MapperType;
519 SourceRange Range;
520 TypeResult ParsedType = parseOpenMPDeclareMapperVarDecl(Range, VName, AS);
521 if (ParsedType.isUsable())
522 MapperType =
523 Actions.ActOnOpenMPDeclareMapperType(Range.getBegin(), ParsedType);
524 if (MapperType.isNull())
525 IsCorrect = false;
526 if (!IsCorrect) {
527 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
528 return DeclGroupPtrTy();
529 }
530
531 // Consume ')'.
532 IsCorrect &= !T.consumeClose();
533 if (!IsCorrect) {
534 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
535 return DeclGroupPtrTy();
536 }
537
538 // Enter scope.
539 OMPDeclareMapperDecl *DMD = Actions.ActOnOpenMPDeclareMapperDirectiveStart(
540 getCurScope(), Actions.getCurLexicalContext(), MapperId, MapperType,
541 Range.getBegin(), VName, AS);
542 DeclarationNameInfo DirName;
543 SourceLocation Loc = Tok.getLocation();
544 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
545 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
546 ParseScope OMPDirectiveScope(this, ScopeFlags);
547 Actions.StartOpenMPDSABlock(OMPD_declare_mapper, DirName, getCurScope(), Loc);
548
549 // Add the mapper variable declaration.
550 Actions.ActOnOpenMPDeclareMapperDirectiveVarDecl(
551 DMD, getCurScope(), MapperType, Range.getBegin(), VName);
552
553 // Parse map clauses.
554 SmallVector<OMPClause *, 6> Clauses;
555 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
556 OpenMPClauseKind CKind = Tok.isAnnotation()
557 ? OMPC_unknown
558 : getOpenMPClauseKind(PP.getSpelling(Tok));
559 Actions.StartOpenMPClause(CKind);
560 OMPClause *Clause =
561 ParseOpenMPClause(OMPD_declare_mapper, CKind, Clauses.size() == 0);
562 if (Clause)
563 Clauses.push_back(Clause);
564 else
565 IsCorrect = false;
566 // Skip ',' if any.
567 if (Tok.is(tok::comma))
568 ConsumeToken();
569 Actions.EndOpenMPClause();
570 }
571 if (Clauses.empty()) {
572 Diag(Tok, diag::err_omp_expected_clause)
573 << getOpenMPDirectiveName(OMPD_declare_mapper);
574 IsCorrect = false;
575 }
576
577 // Exit scope.
578 Actions.EndOpenMPDSABlock(nullptr);
579 OMPDirectiveScope.Exit();
580
581 DeclGroupPtrTy DGP =
582 Actions.ActOnOpenMPDeclareMapperDirectiveEnd(DMD, getCurScope(), Clauses);
583 if (!IsCorrect)
584 return DeclGroupPtrTy();
585 return DGP;
586}
587
588TypeResult Parser::parseOpenMPDeclareMapperVarDecl(SourceRange &Range,
589 DeclarationName &Name,
590 AccessSpecifier AS) {
591 // Parse the common declaration-specifiers piece.
592 Parser::DeclSpecContext DSC = Parser::DeclSpecContext::DSC_type_specifier;
593 DeclSpec DS(AttrFactory);
594 ParseSpecifierQualifierList(DS, AS, DSC);
595
596 // Parse the declarator.
597 DeclaratorContext Context = DeclaratorContext::PrototypeContext;
598 Declarator DeclaratorInfo(DS, Context);
599 ParseDeclarator(DeclaratorInfo);
600 Range = DeclaratorInfo.getSourceRange();
601 if (DeclaratorInfo.getIdentifier() == nullptr) {
602 Diag(Tok.getLocation(), diag::err_omp_mapper_expected_declarator);
603 return true;
604 }
605 Name = Actions.GetNameForDeclarator(DeclaratorInfo).getName();
606
607 return Actions.ActOnOpenMPDeclareMapperVarDecl(getCurScope(), DeclaratorInfo);
608}
609
Alexey Bataev2af33e32016-04-07 12:45:37 +0000610namespace {
611/// RAII that recreates function context for correct parsing of clauses of
612/// 'declare simd' construct.
613/// OpenMP, 2.8.2 declare simd Construct
614/// The expressions appearing in the clauses of this directive are evaluated in
615/// the scope of the arguments of the function declaration or definition.
616class FNContextRAII final {
617 Parser &P;
618 Sema::CXXThisScopeRAII *ThisScope;
619 Parser::ParseScope *TempScope;
620 Parser::ParseScope *FnScope;
621 bool HasTemplateScope = false;
622 bool HasFunScope = false;
623 FNContextRAII() = delete;
624 FNContextRAII(const FNContextRAII &) = delete;
625 FNContextRAII &operator=(const FNContextRAII &) = delete;
626
627public:
628 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
629 Decl *D = *Ptr.get().begin();
630 NamedDecl *ND = dyn_cast<NamedDecl>(D);
631 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
632 Sema &Actions = P.getActions();
633
634 // Allow 'this' within late-parsed attributes.
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000635 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, Qualifiers(),
Alexey Bataev2af33e32016-04-07 12:45:37 +0000636 ND && ND->isCXXInstanceMember());
637
638 // If the Decl is templatized, add template parameters to scope.
639 HasTemplateScope = D->isTemplateDecl();
640 TempScope =
641 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
642 if (HasTemplateScope)
643 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
644
645 // If the Decl is on a function, add function parameters to the scope.
646 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000647 FnScope = new Parser::ParseScope(
648 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
649 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000650 if (HasFunScope)
651 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
652 }
653 ~FNContextRAII() {
654 if (HasFunScope) {
655 P.getActions().ActOnExitFunctionContext();
656 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
657 }
658 if (HasTemplateScope)
659 TempScope->Exit();
660 delete FnScope;
661 delete TempScope;
662 delete ThisScope;
663 }
664};
665} // namespace
666
Alexey Bataevd93d3762016-04-12 09:35:56 +0000667/// Parses clauses for 'declare simd' directive.
668/// clause:
669/// 'inbranch' | 'notinbranch'
670/// 'simdlen' '(' <expr> ')'
671/// { 'uniform' '(' <argument_list> ')' }
672/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000673/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
674static bool parseDeclareSimdClauses(
675 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
676 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
677 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
678 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000679 SourceRange BSRange;
680 const Token &Tok = P.getCurToken();
681 bool IsError = false;
682 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
683 if (Tok.isNot(tok::identifier))
684 break;
685 OMPDeclareSimdDeclAttr::BranchStateTy Out;
686 IdentifierInfo *II = Tok.getIdentifierInfo();
687 StringRef ClauseName = II->getName();
688 // Parse 'inranch|notinbranch' clauses.
689 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
690 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
691 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
692 << ClauseName
693 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
694 IsError = true;
695 }
696 BS = Out;
697 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
698 P.ConsumeToken();
699 } else if (ClauseName.equals("simdlen")) {
700 if (SimdLen.isUsable()) {
701 P.Diag(Tok, diag::err_omp_more_one_clause)
702 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
703 IsError = true;
704 }
705 P.ConsumeToken();
706 SourceLocation RLoc;
707 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
708 if (SimdLen.isInvalid())
709 IsError = true;
710 } else {
711 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000712 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
713 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000714 Parser::OpenMPVarListDataTy Data;
Alexey Bataev61908f652018-04-23 19:53:05 +0000715 SmallVectorImpl<Expr *> *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000716 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000717 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000718 else if (CKind == OMPC_linear)
719 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000720
721 P.ConsumeToken();
722 if (P.ParseOpenMPVarList(OMPD_declare_simd,
723 getOpenMPClauseKind(ClauseName), *Vars, Data))
724 IsError = true;
Alexey Bataev61908f652018-04-23 19:53:05 +0000725 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000726 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataev61908f652018-04-23 19:53:05 +0000727 } else if (CKind == OMPC_linear) {
Alexey Bataevecba70f2016-04-12 11:02:11 +0000728 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
729 Data.DepLinMapLoc))
730 Data.LinKind = OMPC_LINEAR_val;
731 LinModifiers.append(Linears.size() - LinModifiers.size(),
732 Data.LinKind);
733 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
734 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000735 } else
736 // TODO: add parsing of other clauses.
737 break;
738 }
739 // Skip ',' if any.
740 if (Tok.is(tok::comma))
741 P.ConsumeToken();
742 }
743 return IsError;
744}
745
Alexey Bataev2af33e32016-04-07 12:45:37 +0000746/// Parse clauses for '#pragma omp declare simd'.
747Parser::DeclGroupPtrTy
748Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
749 CachedTokens &Toks, SourceLocation Loc) {
750 PP.EnterToken(Tok);
751 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
752 // Consume the previously pushed token.
753 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
754
755 FNContextRAII FnContext(*this, Ptr);
756 OMPDeclareSimdDeclAttr::BranchStateTy BS =
757 OMPDeclareSimdDeclAttr::BS_Undefined;
758 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000759 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000760 SmallVector<Expr *, 4> Aligneds;
761 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000762 SmallVector<Expr *, 4> Linears;
763 SmallVector<unsigned, 4> LinModifiers;
764 SmallVector<Expr *, 4> Steps;
765 bool IsError =
766 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
767 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000768 // Need to check for extra tokens.
769 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
770 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
771 << getOpenMPDirectiveName(OMPD_declare_simd);
772 while (Tok.isNot(tok::annot_pragma_openmp_end))
773 ConsumeAnyToken();
774 }
775 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000776 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000777 if (IsError)
778 return Ptr;
779 return Actions.ActOnOpenMPDeclareSimdDirective(
780 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
781 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataev20dfd772016-04-04 10:12:15 +0000782}
783
Kelvin Lie0502752018-11-21 20:15:57 +0000784Parser::DeclGroupPtrTy Parser::ParseOMPDeclareTargetClauses() {
785 // OpenMP 4.5 syntax with list of entities.
786 Sema::NamedDeclSetType SameDirectiveDecls;
787 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
788 OMPDeclareTargetDeclAttr::MapTypeTy MT = OMPDeclareTargetDeclAttr::MT_To;
789 if (Tok.is(tok::identifier)) {
790 IdentifierInfo *II = Tok.getIdentifierInfo();
791 StringRef ClauseName = II->getName();
792 // Parse 'to|link' clauses.
793 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName, MT)) {
794 Diag(Tok, diag::err_omp_declare_target_unexpected_clause) << ClauseName;
795 break;
796 }
797 ConsumeToken();
798 }
799 auto &&Callback = [this, MT, &SameDirectiveDecls](
800 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
801 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
802 SameDirectiveDecls);
803 };
804 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
805 /*AllowScopeSpecifier=*/true))
806 break;
807
808 // Consume optional ','.
809 if (Tok.is(tok::comma))
810 ConsumeToken();
811 }
812 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
813 ConsumeAnyToken();
814 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
815 SameDirectiveDecls.end());
816 if (Decls.empty())
817 return DeclGroupPtrTy();
818 return Actions.BuildDeclaratorGroup(Decls);
819}
820
821void Parser::ParseOMPEndDeclareTargetDirective(OpenMPDirectiveKind DKind,
822 SourceLocation DTLoc) {
823 if (DKind != OMPD_end_declare_target) {
824 Diag(Tok, diag::err_expected_end_declare_target);
825 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
826 return;
827 }
828 ConsumeAnyToken();
829 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
830 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
831 << getOpenMPDirectiveName(OMPD_end_declare_target);
832 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
833 }
834 // Skip the last annot_pragma_openmp_end.
835 ConsumeAnyToken();
836}
837
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000838/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000839///
840/// threadprivate-directive:
841/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000842/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000843///
Alexey Bataev25ed0c02019-03-07 17:54:44 +0000844/// allocate-directive:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +0000845/// annot_pragma_openmp 'allocate' simple-variable-list [<clause>]
Alexey Bataev25ed0c02019-03-07 17:54:44 +0000846/// annot_pragma_openmp_end
847///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000848/// declare-reduction-directive:
849/// annot_pragma_openmp 'declare' 'reduction' [...]
850/// annot_pragma_openmp_end
851///
Michael Kruse251e1482019-02-01 20:25:04 +0000852/// declare-mapper-directive:
853/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
854/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
855/// annot_pragma_openmp_end
856///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000857/// declare-simd-directive:
858/// annot_pragma_openmp 'declare simd' {<clause> [,]}
859/// annot_pragma_openmp_end
860/// <function declaration/definition>
861///
Kelvin Li1408f912018-09-26 04:28:39 +0000862/// requires directive:
863/// annot_pragma_openmp 'requires' <clause> [[[,] <clause>] ... ]
864/// annot_pragma_openmp_end
865///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000866Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
867 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
868 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000869 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000870 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000871
Richard Smithaf3b3252017-05-18 19:21:48 +0000872 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000873 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000874
875 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000876 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000877 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +0000878 DeclDirectiveListParserHelper Helper(this, DKind);
879 if (!ParseOpenMPSimpleVarList(DKind, Helper,
880 /*AllowScopeSpecifier=*/true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000881 // The last seen token is annot_pragma_openmp_end - need to check for
882 // extra tokens.
883 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
884 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +0000885 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000886 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000887 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000888 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000889 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000890 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
891 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000892 }
893 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000894 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +0000895 case OMPD_allocate: {
896 ConsumeToken();
897 DeclDirectiveListParserHelper Helper(this, DKind);
898 if (!ParseOpenMPSimpleVarList(DKind, Helper,
899 /*AllowScopeSpecifier=*/true)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +0000900 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +0000901 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +0000902 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
903 OMPC_unknown + 1>
904 FirstClauses(OMPC_unknown + 1);
905 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
906 OpenMPClauseKind CKind =
907 Tok.isAnnotation() ? OMPC_unknown
908 : getOpenMPClauseKind(PP.getSpelling(Tok));
909 Actions.StartOpenMPClause(CKind);
910 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
911 !FirstClauses[CKind].getInt());
912 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
913 StopBeforeMatch);
914 FirstClauses[CKind].setInt(true);
915 if (Clause != nullptr)
916 Clauses.push_back(Clause);
917 if (Tok.is(tok::annot_pragma_openmp_end)) {
918 Actions.EndOpenMPClause();
919 break;
920 }
921 // Skip ',' if any.
922 if (Tok.is(tok::comma))
923 ConsumeToken();
924 Actions.EndOpenMPClause();
925 }
926 // The last seen token is annot_pragma_openmp_end - need to check for
927 // extra tokens.
928 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
929 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
930 << getOpenMPDirectiveName(DKind);
931 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
932 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +0000933 }
934 // Skip the last annot_pragma_openmp_end.
935 ConsumeAnnotationToken();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +0000936 return Actions.ActOnOpenMPAllocateDirective(Loc, Helper.getIdentifiers(),
937 Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +0000938 }
939 break;
940 }
Kelvin Li1408f912018-09-26 04:28:39 +0000941 case OMPD_requires: {
942 SourceLocation StartLoc = ConsumeToken();
943 SmallVector<OMPClause *, 5> Clauses;
944 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
945 FirstClauses(OMPC_unknown + 1);
946 if (Tok.is(tok::annot_pragma_openmp_end)) {
Ilya Biryukovff2a9972019-02-26 11:01:50 +0000947 Diag(Tok, diag::err_omp_expected_clause)
Kelvin Li1408f912018-09-26 04:28:39 +0000948 << getOpenMPDirectiveName(OMPD_requires);
949 break;
950 }
951 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
952 OpenMPClauseKind CKind = Tok.isAnnotation()
953 ? OMPC_unknown
954 : getOpenMPClauseKind(PP.getSpelling(Tok));
955 Actions.StartOpenMPClause(CKind);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +0000956 OMPClause *Clause = ParseOpenMPClause(OMPD_requires, CKind,
957 !FirstClauses[CKind].getInt());
958 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
959 StopBeforeMatch);
Kelvin Li1408f912018-09-26 04:28:39 +0000960 FirstClauses[CKind].setInt(true);
961 if (Clause != nullptr)
962 Clauses.push_back(Clause);
963 if (Tok.is(tok::annot_pragma_openmp_end)) {
964 Actions.EndOpenMPClause();
965 break;
966 }
967 // Skip ',' if any.
968 if (Tok.is(tok::comma))
969 ConsumeToken();
970 Actions.EndOpenMPClause();
971 }
972 // Consume final annot_pragma_openmp_end
973 if (Clauses.size() == 0) {
974 Diag(Tok, diag::err_omp_expected_clause)
975 << getOpenMPDirectiveName(OMPD_requires);
976 ConsumeAnnotationToken();
977 return nullptr;
978 }
979 ConsumeAnnotationToken();
980 return Actions.ActOnOpenMPRequiresDirective(StartLoc, Clauses);
981 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000982 case OMPD_declare_reduction:
983 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000984 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000985 // The last seen token is annot_pragma_openmp_end - need to check for
986 // extra tokens.
987 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
988 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
989 << getOpenMPDirectiveName(OMPD_declare_reduction);
990 while (Tok.isNot(tok::annot_pragma_openmp_end))
991 ConsumeAnyToken();
992 }
993 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000994 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000995 return Res;
996 }
997 break;
Michael Kruse251e1482019-02-01 20:25:04 +0000998 case OMPD_declare_mapper: {
999 ConsumeToken();
1000 if (DeclGroupPtrTy Res = ParseOpenMPDeclareMapperDirective(AS)) {
1001 // Skip the last annot_pragma_openmp_end.
1002 ConsumeAnnotationToken();
1003 return Res;
1004 }
1005 break;
1006 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001007 case OMPD_declare_simd: {
1008 // The syntax is:
1009 // { #pragma omp declare simd }
1010 // <function-declaration-or-definition>
1011 //
Alexey Bataev587e1de2016-03-30 10:43:55 +00001012 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001013 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001014 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
1015 Toks.push_back(Tok);
1016 ConsumeAnyToken();
1017 }
1018 Toks.push_back(Tok);
1019 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +00001020
1021 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +00001022 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001023 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +00001024 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001025 // Here we expect to see some function declaration.
1026 if (AS == AS_none) {
1027 assert(TagType == DeclSpec::TST_unspecified);
1028 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001029 ParsingDeclSpec PDS(*this);
1030 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1031 } else {
1032 Ptr =
1033 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1034 }
1035 }
1036 if (!Ptr) {
1037 Diag(Loc, diag::err_omp_decl_in_declare_simd);
1038 return DeclGroupPtrTy();
1039 }
Alexey Bataev2af33e32016-04-07 12:45:37 +00001040 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001041 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001042 case OMPD_declare_target: {
1043 SourceLocation DTLoc = ConsumeAnyToken();
1044 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Kelvin Lie0502752018-11-21 20:15:57 +00001045 return ParseOMPDeclareTargetClauses();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001046 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001047
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001048 // Skip the last annot_pragma_openmp_end.
1049 ConsumeAnyToken();
1050
1051 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
1052 return DeclGroupPtrTy();
1053
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001054 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +00001055 DKind = parseOpenMPDirectiveKind(*this);
Kelvin Libc38e632018-09-10 02:07:09 +00001056 while (DKind != OMPD_end_declare_target && Tok.isNot(tok::eof) &&
1057 Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +00001058 DeclGroupPtrTy Ptr;
1059 // Here we expect to see some function declaration.
1060 if (AS == AS_none) {
1061 assert(TagType == DeclSpec::TST_unspecified);
1062 MaybeParseCXX11Attributes(Attrs);
1063 ParsingDeclSpec PDS(*this);
1064 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1065 } else {
1066 Ptr =
1067 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1068 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001069 if (Ptr) {
1070 DeclGroupRef Ref = Ptr.get();
1071 Decls.append(Ref.begin(), Ref.end());
1072 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001073 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
1074 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +00001075 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001076 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001077 if (DKind != OMPD_end_declare_target)
1078 TPA.Revert();
1079 else
1080 TPA.Commit();
1081 }
1082 }
1083
Kelvin Lie0502752018-11-21 20:15:57 +00001084 ParseOMPEndDeclareTargetDirective(DKind, DTLoc);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001085 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +00001086 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001087 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001088 case OMPD_unknown:
1089 Diag(Tok, diag::err_omp_unknown_directive);
1090 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001091 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001092 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001093 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +00001094 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001095 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001096 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001097 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +00001098 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001099 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001100 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001101 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001102 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001103 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +00001104 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001105 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001106 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001107 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001108 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001109 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +00001110 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001111 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001112 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001113 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001114 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +00001115 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001116 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001117 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001118 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001119 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001120 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001121 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001122 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001123 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001124 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001125 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +00001126 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001127 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001128 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001129 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001130 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +00001131 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +00001132 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001133 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +00001134 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +00001135 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +00001136 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +00001137 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +00001138 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +00001139 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001140 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +00001141 break;
1142 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001143 while (Tok.isNot(tok::annot_pragma_openmp_end))
1144 ConsumeAnyToken();
1145 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +00001146 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001147}
1148
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001149/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001150///
1151/// threadprivate-directive:
1152/// annot_pragma_openmp 'threadprivate' simple-variable-list
1153/// annot_pragma_openmp_end
1154///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001155/// allocate-directive:
1156/// annot_pragma_openmp 'allocate' simple-variable-list
1157/// annot_pragma_openmp_end
1158///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001159/// declare-reduction-directive:
1160/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
1161/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
1162/// ('omp_priv' '=' <expression>|<function_call>) ')']
1163/// annot_pragma_openmp_end
1164///
Michael Kruse251e1482019-02-01 20:25:04 +00001165/// declare-mapper-directive:
1166/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1167/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1168/// annot_pragma_openmp_end
1169///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001170/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001171/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001172/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
1173/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001174/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +00001175/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001176/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001177/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +00001178/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +00001179/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +00001180/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +00001181/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +00001182/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +00001183/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +00001184/// 'teams distribute parallel for' | 'target teams' |
1185/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +00001186/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +00001187/// 'target teams distribute parallel for simd' |
1188/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +00001189/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001190///
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001191StmtResult
1192Parser::ParseOpenMPDeclarativeOrExecutableDirective(ParsedStmtContext StmtCtx) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001193 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +00001194 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001195 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001196 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +00001197 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +00001198 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
1199 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +00001200 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +00001201 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001202 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001203 // Name of critical directive.
1204 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001205 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +00001206 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +00001207 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001208
1209 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001210 case OMPD_threadprivate: {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001211 // FIXME: Should this be permitted in C++?
1212 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1213 ParsedStmtContext()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001214 Diag(Tok, diag::err_omp_immediate_directive)
1215 << getOpenMPDirectiveName(DKind) << 0;
1216 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001217 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001218 DeclDirectiveListParserHelper Helper(this, DKind);
1219 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1220 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001221 // The last seen token is annot_pragma_openmp_end - need to check for
1222 // extra tokens.
1223 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1224 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001225 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001226 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001227 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001228 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
1229 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001230 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1231 }
Alp Tokerd751fa72013-12-18 19:10:49 +00001232 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001233 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001234 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001235 case OMPD_allocate: {
1236 // FIXME: Should this be permitted in C++?
1237 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1238 ParsedStmtContext()) {
1239 Diag(Tok, diag::err_omp_immediate_directive)
1240 << getOpenMPDirectiveName(DKind) << 0;
1241 }
1242 ConsumeToken();
1243 DeclDirectiveListParserHelper Helper(this, DKind);
1244 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1245 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001246 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001247 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001248 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1249 OMPC_unknown + 1>
1250 FirstClauses(OMPC_unknown + 1);
1251 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1252 OpenMPClauseKind CKind =
1253 Tok.isAnnotation() ? OMPC_unknown
1254 : getOpenMPClauseKind(PP.getSpelling(Tok));
1255 Actions.StartOpenMPClause(CKind);
1256 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1257 !FirstClauses[CKind].getInt());
1258 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1259 StopBeforeMatch);
1260 FirstClauses[CKind].setInt(true);
1261 if (Clause != nullptr)
1262 Clauses.push_back(Clause);
1263 if (Tok.is(tok::annot_pragma_openmp_end)) {
1264 Actions.EndOpenMPClause();
1265 break;
1266 }
1267 // Skip ',' if any.
1268 if (Tok.is(tok::comma))
1269 ConsumeToken();
1270 Actions.EndOpenMPClause();
1271 }
1272 // The last seen token is annot_pragma_openmp_end - need to check for
1273 // extra tokens.
1274 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1275 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1276 << getOpenMPDirectiveName(DKind);
1277 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1278 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001279 }
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001280 DeclGroupPtrTy Res = Actions.ActOnOpenMPAllocateDirective(
1281 Loc, Helper.getIdentifiers(), Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001282 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1283 }
1284 SkipUntil(tok::annot_pragma_openmp_end);
1285 break;
1286 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001287 case OMPD_declare_reduction:
1288 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001289 if (DeclGroupPtrTy Res =
1290 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001291 // The last seen token is annot_pragma_openmp_end - need to check for
1292 // extra tokens.
1293 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1294 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1295 << getOpenMPDirectiveName(OMPD_declare_reduction);
1296 while (Tok.isNot(tok::annot_pragma_openmp_end))
1297 ConsumeAnyToken();
1298 }
1299 ConsumeAnyToken();
1300 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +00001301 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001302 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +00001303 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001304 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001305 case OMPD_declare_mapper: {
1306 ConsumeToken();
1307 if (DeclGroupPtrTy Res =
1308 ParseOpenMPDeclareMapperDirective(/*AS=*/AS_none)) {
1309 // Skip the last annot_pragma_openmp_end.
1310 ConsumeAnnotationToken();
1311 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1312 } else {
1313 SkipUntil(tok::annot_pragma_openmp_end);
1314 }
1315 break;
1316 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001317 case OMPD_flush:
1318 if (PP.LookAhead(0).is(tok::l_paren)) {
1319 FlushHasClause = true;
1320 // Push copy of the current token back to stream to properly parse
1321 // pseudo-clause OMPFlushClause.
1322 PP.EnterToken(Tok);
1323 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001324 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +00001325 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001326 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001327 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001328 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001329 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001330 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001331 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +00001332 case OMPD_target_update:
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001333 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1334 ParsedStmtContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00001335 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +00001336 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +00001337 }
1338 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001339 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001340 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001341 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001342 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001343 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001344 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001345 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001346 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001347 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001348 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001349 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001350 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001351 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001352 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001353 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001354 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001355 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001356 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001357 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001358 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001359 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001360 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001361 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001362 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001363 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001364 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001365 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001366 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001367 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001368 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001369 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001370 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001371 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001372 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001373 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001374 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001375 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001376 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001377 case OMPD_target_teams_distribute_parallel_for_simd:
1378 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001379 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001380 // Parse directive name of the 'critical' directive if any.
1381 if (DKind == OMPD_critical) {
1382 BalancedDelimiterTracker T(*this, tok::l_paren,
1383 tok::annot_pragma_openmp_end);
1384 if (!T.consumeOpen()) {
1385 if (Tok.isAnyIdentifier()) {
1386 DirName =
1387 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1388 ConsumeAnyToken();
1389 } else {
1390 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1391 }
1392 T.consumeClose();
1393 }
Alexey Bataev80909872015-07-02 11:25:17 +00001394 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001395 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001396 if (Tok.isNot(tok::annot_pragma_openmp_end))
1397 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001398 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001399
Alexey Bataevf29276e2014-06-18 04:14:57 +00001400 if (isOpenMPLoopDirective(DKind))
1401 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1402 if (isOpenMPSimdDirective(DKind))
1403 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1404 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001405 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001406
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001407 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001408 OpenMPClauseKind CKind =
1409 Tok.isAnnotation()
1410 ? OMPC_unknown
1411 : FlushHasClause ? OMPC_flush
1412 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001413 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001414 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001415 OMPClause *Clause =
1416 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001417 FirstClauses[CKind].setInt(true);
1418 if (Clause) {
1419 FirstClauses[CKind].setPointer(Clause);
1420 Clauses.push_back(Clause);
1421 }
1422
1423 // Skip ',' if any.
1424 if (Tok.is(tok::comma))
1425 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001426 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001427 }
1428 // End location of the directive.
1429 EndLoc = Tok.getLocation();
1430 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001431 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001432
Alexey Bataeveb482352015-12-18 05:05:56 +00001433 // OpenMP [2.13.8, ordered Construct, Syntax]
1434 // If the depend clause is specified, the ordered construct is a stand-alone
1435 // directive.
1436 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001437 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1438 ParsedStmtContext()) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001439 Diag(Loc, diag::err_omp_immediate_directive)
1440 << getOpenMPDirectiveName(DKind) << 1
1441 << getOpenMPClauseName(OMPC_depend);
1442 }
1443 HasAssociatedStatement = false;
1444 }
1445
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001446 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001447 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001448 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001449 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001450 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1451 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1452 // should have at least one compound statement scope within it.
1453 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001454 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001455 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1456 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001457 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001458 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1459 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1460 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001461 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001462 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001463 Directive = Actions.ActOnOpenMPExecutableDirective(
1464 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1465 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001466
1467 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001468 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001469 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001470 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001471 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001472 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001473 case OMPD_declare_target:
1474 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00001475 case OMPD_requires:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001476 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001477 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001478 SkipUntil(tok::annot_pragma_openmp_end);
1479 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001480 case OMPD_unknown:
1481 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001482 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001483 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001484 }
1485 return Directive;
1486}
1487
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001488// Parses simple list:
1489// simple-variable-list:
1490// '(' id-expression {, id-expression} ')'
1491//
1492bool Parser::ParseOpenMPSimpleVarList(
1493 OpenMPDirectiveKind Kind,
1494 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1495 Callback,
1496 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001497 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001498 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001499 if (T.expectAndConsume(diag::err_expected_lparen_after,
1500 getOpenMPDirectiveName(Kind)))
1501 return true;
1502 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001503 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001504
1505 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001506 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001507 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001508 UnqualifiedId Name;
1509 // Read var name.
1510 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001511 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001512
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001513 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001514 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001515 IsCorrect = false;
1516 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001517 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001518 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00001519 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001520 IsCorrect = false;
1521 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001522 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001523 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1524 Tok.isNot(tok::annot_pragma_openmp_end)) {
1525 IsCorrect = false;
1526 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001527 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001528 Diag(PrevTok.getLocation(), diag::err_expected)
1529 << tok::identifier
1530 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001531 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001532 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001533 }
1534 // Consume ','.
1535 if (Tok.is(tok::comma)) {
1536 ConsumeToken();
1537 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001538 }
1539
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001540 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001541 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001542 IsCorrect = false;
1543 }
1544
1545 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001546 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001547
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001548 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001549}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001550
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001551/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001552///
1553/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001554/// if-clause | final-clause | num_threads-clause | safelen-clause |
1555/// default-clause | private-clause | firstprivate-clause | shared-clause
1556/// | linear-clause | aligned-clause | collapse-clause |
1557/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001558/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001559/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001560/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001561/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001562/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001563/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001564/// from-clause | is_device_ptr-clause | task_reduction-clause |
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001565/// in_reduction-clause | allocator-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001566///
1567OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1568 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001569 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001570 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001571 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001572 // Check if clause is allowed for the given directive.
1573 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001574 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1575 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001576 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001577 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001578 }
1579
1580 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001581 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001582 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001583 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001584 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001585 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001586 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001587 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001588 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001589 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001590 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001591 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001592 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001593 case OMPC_hint:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001594 case OMPC_allocator:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001595 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001596 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001597 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001598 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001599 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001600 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001601 // OpenMP [2.9.1, target data construct, Restrictions]
1602 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001603 // OpenMP [2.11.1, task Construct, Restrictions]
1604 // At most one if clause can appear on the directive.
1605 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001606 // OpenMP [teams Construct, Restrictions]
1607 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001608 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001609 // OpenMP [2.9.1, task Construct, Restrictions]
1610 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001611 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1612 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001613 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1614 // At most one num_tasks clause can appear on the directive.
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001615 // OpenMP [2.11.3, allocate Directive, Restrictions]
1616 // At most one allocator clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001617 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001618 Diag(Tok, diag::err_omp_more_one_clause)
1619 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001620 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001621 }
1622
Alexey Bataev10e775f2015-07-30 11:36:16 +00001623 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001624 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001625 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001626 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001627 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001628 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001629 case OMPC_proc_bind:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001630 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001631 // OpenMP [2.14.3.1, Restrictions]
1632 // Only a single default clause may be specified on a parallel, task or
1633 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001634 // OpenMP [2.5, parallel Construct, Restrictions]
1635 // At most one proc_bind clause can appear on the directive.
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001636 // OpenMP [5.0, Requires directive, Restrictions]
1637 // At most one atomic_default_mem_order clause can appear
1638 // on the directive
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001639 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001640 Diag(Tok, diag::err_omp_more_one_clause)
1641 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001642 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001643 }
1644
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001645 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001646 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001647 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001648 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001649 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001650 // OpenMP [2.7.1, Restrictions, p. 3]
1651 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001652 // OpenMP [2.10.4, Restrictions, p. 106]
1653 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001654 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001655 Diag(Tok, diag::err_omp_more_one_clause)
1656 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001657 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001658 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001659 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001660
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001661 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001662 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001663 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001664 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001665 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001666 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001667 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001668 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001669 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001670 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001671 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001672 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001673 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001674 case OMPC_nogroup:
Kelvin Li1408f912018-09-26 04:28:39 +00001675 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00001676 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00001677 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00001678 case OMPC_dynamic_allocators:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001679 // OpenMP [2.7.1, Restrictions, p. 9]
1680 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001681 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1682 // Only one nowait clause can appear on a for directive.
Kelvin Li1408f912018-09-26 04:28:39 +00001683 // OpenMP [5.0, Requires directive, Restrictions]
1684 // Each of the requires clauses can appear at most once on the directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001685 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001686 Diag(Tok, diag::err_omp_more_one_clause)
1687 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001688 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001689 }
1690
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001691 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001692 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001693 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001694 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001695 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001696 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001697 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00001698 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00001699 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001700 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001701 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001702 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001703 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001704 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001705 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001706 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001707 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001708 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001709 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001710 case OMPC_is_device_ptr:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001711 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001712 break;
1713 case OMPC_unknown:
1714 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001715 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001716 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001717 break;
1718 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001719 case OMPC_allocate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001720 case OMPC_uniform:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001721 if (!WrongDirective)
1722 Diag(Tok, diag::err_omp_unexpected_clause)
1723 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001724 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001725 break;
1726 }
Craig Topper161e4db2014-05-21 06:02:52 +00001727 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001728}
1729
Alexey Bataev2af33e32016-04-07 12:45:37 +00001730/// Parses simple expression in parens for single-expression clauses of OpenMP
1731/// constructs.
1732/// \param RLoc Returned location of right paren.
1733ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1734 SourceLocation &RLoc) {
1735 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1736 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1737 return ExprError();
1738
1739 SourceLocation ELoc = Tok.getLocation();
1740 ExprResult LHS(ParseCastExpression(
1741 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1742 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00001743 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev2af33e32016-04-07 12:45:37 +00001744
1745 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001746 RLoc = Tok.getLocation();
1747 if (!T.consumeClose())
1748 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001749
Alexey Bataev2af33e32016-04-07 12:45:37 +00001750 return Val;
1751}
1752
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001753/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001754/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001755/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001756///
Alexey Bataev3778b602014-07-17 07:32:53 +00001757/// final-clause:
1758/// 'final' '(' expression ')'
1759///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001760/// num_threads-clause:
1761/// 'num_threads' '(' expression ')'
1762///
1763/// safelen-clause:
1764/// 'safelen' '(' expression ')'
1765///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001766/// simdlen-clause:
1767/// 'simdlen' '(' expression ')'
1768///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001769/// collapse-clause:
1770/// 'collapse' '(' expression ')'
1771///
Alexey Bataeva0569352015-12-01 10:17:31 +00001772/// priority-clause:
1773/// 'priority' '(' expression ')'
1774///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001775/// grainsize-clause:
1776/// 'grainsize' '(' expression ')'
1777///
Alexey Bataev382967a2015-12-08 12:06:20 +00001778/// num_tasks-clause:
1779/// 'num_tasks' '(' expression ')'
1780///
Alexey Bataev28c75412015-12-15 08:19:24 +00001781/// hint-clause:
1782/// 'hint' '(' expression ')'
1783///
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001784/// allocator-clause:
1785/// 'allocator' '(' expression ')'
1786///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001787OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
1788 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001789 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001790 SourceLocation LLoc = Tok.getLocation();
1791 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001792
Alexey Bataev2af33e32016-04-07 12:45:37 +00001793 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001794
1795 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001796 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001797
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001798 if (ParseOnly)
1799 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00001800 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001801}
1802
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001803/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001804///
1805/// default-clause:
1806/// 'default' '(' 'none' | 'shared' ')
1807///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001808/// proc_bind-clause:
1809/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1810///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001811OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
1812 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001813 SourceLocation Loc = Tok.getLocation();
1814 SourceLocation LOpen = ConsumeToken();
1815 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001816 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001817 if (T.expectAndConsume(diag::err_expected_lparen_after,
1818 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001819 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001820
Alexey Bataeva55ed262014-05-28 06:15:33 +00001821 unsigned Type = getOpenMPSimpleClauseType(
1822 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001823 SourceLocation TypeLoc = Tok.getLocation();
1824 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1825 Tok.isNot(tok::annot_pragma_openmp_end))
1826 ConsumeAnyToken();
1827
1828 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001829 SourceLocation RLoc = Tok.getLocation();
1830 if (!T.consumeClose())
1831 RLoc = T.getCloseLocation();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001832
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001833 if (ParseOnly)
1834 return nullptr;
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001835 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc, RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001836}
1837
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001838/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001839///
1840/// ordered-clause:
1841/// 'ordered'
1842///
Alexey Bataev236070f2014-06-20 11:19:47 +00001843/// nowait-clause:
1844/// 'nowait'
1845///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001846/// untied-clause:
1847/// 'untied'
1848///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001849/// mergeable-clause:
1850/// 'mergeable'
1851///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001852/// read-clause:
1853/// 'read'
1854///
Alexey Bataev346265e2015-09-25 10:37:12 +00001855/// threads-clause:
1856/// 'threads'
1857///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001858/// simd-clause:
1859/// 'simd'
1860///
Alexey Bataevb825de12015-12-07 10:51:44 +00001861/// nogroup-clause:
1862/// 'nogroup'
1863///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001864OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001865 SourceLocation Loc = Tok.getLocation();
1866 ConsumeAnyToken();
1867
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001868 if (ParseOnly)
1869 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001870 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1871}
1872
1873
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001874/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00001875/// argument like 'schedule' or 'dist_schedule'.
1876///
1877/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001878/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1879/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001880///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001881/// if-clause:
1882/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1883///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001884/// defaultmap:
1885/// 'defaultmap' '(' modifier ':' kind ')'
1886///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001887OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
1888 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00001889 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001890 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001891 // Parse '('.
1892 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1893 if (T.expectAndConsume(diag::err_expected_lparen_after,
1894 getOpenMPClauseName(Kind)))
1895 return nullptr;
1896
1897 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001898 SmallVector<unsigned, 4> Arg;
1899 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001900 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001901 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1902 Arg.resize(NumberOfElements);
1903 KLoc.resize(NumberOfElements);
1904 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1905 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1906 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001907 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001908 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001909 if (KindModifier > OMPC_SCHEDULE_unknown) {
1910 // Parse 'modifier'
1911 Arg[Modifier1] = KindModifier;
1912 KLoc[Modifier1] = Tok.getLocation();
1913 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1914 Tok.isNot(tok::annot_pragma_openmp_end))
1915 ConsumeAnyToken();
1916 if (Tok.is(tok::comma)) {
1917 // Parse ',' 'modifier'
1918 ConsumeAnyToken();
1919 KindModifier = getOpenMPSimpleClauseType(
1920 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1921 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1922 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001923 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001924 KLoc[Modifier2] = Tok.getLocation();
1925 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1926 Tok.isNot(tok::annot_pragma_openmp_end))
1927 ConsumeAnyToken();
1928 }
1929 // Parse ':'
1930 if (Tok.is(tok::colon))
1931 ConsumeAnyToken();
1932 else
1933 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1934 KindModifier = getOpenMPSimpleClauseType(
1935 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1936 }
1937 Arg[ScheduleKind] = KindModifier;
1938 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001939 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1940 Tok.isNot(tok::annot_pragma_openmp_end))
1941 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001942 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1943 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1944 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001945 Tok.is(tok::comma))
1946 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001947 } else if (Kind == OMPC_dist_schedule) {
1948 Arg.push_back(getOpenMPSimpleClauseType(
1949 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1950 KLoc.push_back(Tok.getLocation());
1951 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1952 Tok.isNot(tok::annot_pragma_openmp_end))
1953 ConsumeAnyToken();
1954 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1955 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001956 } else if (Kind == OMPC_defaultmap) {
1957 // Get a defaultmap modifier
1958 Arg.push_back(getOpenMPSimpleClauseType(
1959 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1960 KLoc.push_back(Tok.getLocation());
1961 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1962 Tok.isNot(tok::annot_pragma_openmp_end))
1963 ConsumeAnyToken();
1964 // Parse ':'
1965 if (Tok.is(tok::colon))
1966 ConsumeAnyToken();
1967 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1968 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1969 // Get a defaultmap kind
1970 Arg.push_back(getOpenMPSimpleClauseType(
1971 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1972 KLoc.push_back(Tok.getLocation());
1973 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1974 Tok.isNot(tok::annot_pragma_openmp_end))
1975 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001976 } else {
1977 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001978 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001979 TentativeParsingAction TPA(*this);
Alexey Bataev61908f652018-04-23 19:53:05 +00001980 Arg.push_back(parseOpenMPDirectiveKind(*this));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001981 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001982 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001983 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1984 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001985 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001986 } else {
1987 TPA.Revert();
1988 Arg.back() = OMPD_unknown;
1989 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001990 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001991 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00001992 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001993 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001994
Carlo Bertollib4adf552016-01-15 18:50:31 +00001995 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1996 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1997 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001998 if (NeedAnExpression) {
1999 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002000 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
2001 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002002 Val =
2003 Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002004 }
2005
2006 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002007 SourceLocation RLoc = Tok.getLocation();
2008 if (!T.consumeClose())
2009 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002010
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002011 if (NeedAnExpression && Val.isInvalid())
2012 return nullptr;
2013
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002014 if (ParseOnly)
2015 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002016 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002017 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002018}
2019
Alexey Bataevc5e02582014-06-16 07:08:35 +00002020static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
2021 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002022 if (ReductionIdScopeSpec.isEmpty()) {
2023 auto OOK = OO_None;
2024 switch (P.getCurToken().getKind()) {
2025 case tok::plus:
2026 OOK = OO_Plus;
2027 break;
2028 case tok::minus:
2029 OOK = OO_Minus;
2030 break;
2031 case tok::star:
2032 OOK = OO_Star;
2033 break;
2034 case tok::amp:
2035 OOK = OO_Amp;
2036 break;
2037 case tok::pipe:
2038 OOK = OO_Pipe;
2039 break;
2040 case tok::caret:
2041 OOK = OO_Caret;
2042 break;
2043 case tok::ampamp:
2044 OOK = OO_AmpAmp;
2045 break;
2046 case tok::pipepipe:
2047 OOK = OO_PipePipe;
2048 break;
2049 default:
2050 break;
2051 }
2052 if (OOK != OO_None) {
2053 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00002054 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00002055 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
2056 return false;
2057 }
2058 }
2059 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
2060 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00002061 /*AllowConstructorName*/ false,
2062 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00002063 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002064}
2065
Kelvin Lief579432018-12-18 22:18:41 +00002066/// Checks if the token is a valid map-type-modifier.
2067static OpenMPMapModifierKind isMapModifier(Parser &P) {
2068 Token Tok = P.getCurToken();
2069 if (!Tok.is(tok::identifier))
2070 return OMPC_MAP_MODIFIER_unknown;
2071
2072 Preprocessor &PP = P.getPreprocessor();
2073 OpenMPMapModifierKind TypeModifier = static_cast<OpenMPMapModifierKind>(
2074 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2075 return TypeModifier;
2076}
2077
Michael Kruse01f670d2019-02-22 22:29:42 +00002078/// Parse the mapper modifier in map, to, and from clauses.
2079bool Parser::parseMapperModifier(OpenMPVarListDataTy &Data) {
2080 // Parse '('.
2081 BalancedDelimiterTracker T(*this, tok::l_paren, tok::colon);
2082 if (T.expectAndConsume(diag::err_expected_lparen_after, "mapper")) {
2083 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2084 StopBeforeMatch);
2085 return true;
2086 }
2087 // Parse mapper-identifier
2088 if (getLangOpts().CPlusPlus)
2089 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
2090 /*ObjectType=*/nullptr,
2091 /*EnteringContext=*/false);
2092 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
2093 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
2094 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2095 StopBeforeMatch);
2096 return true;
2097 }
2098 auto &DeclNames = Actions.getASTContext().DeclarationNames;
2099 Data.ReductionOrMapperId = DeclarationNameInfo(
2100 DeclNames.getIdentifier(Tok.getIdentifierInfo()), Tok.getLocation());
2101 ConsumeToken();
2102 // Parse ')'.
2103 return T.consumeClose();
2104}
2105
Kelvin Lief579432018-12-18 22:18:41 +00002106/// Parse map-type-modifiers in map clause.
2107/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002108/// where, map-type-modifier ::= always | close | mapper(mapper-identifier)
2109bool Parser::parseMapTypeModifiers(OpenMPVarListDataTy &Data) {
2110 while (getCurToken().isNot(tok::colon)) {
2111 OpenMPMapModifierKind TypeModifier = isMapModifier(*this);
Kelvin Lief579432018-12-18 22:18:41 +00002112 if (TypeModifier == OMPC_MAP_MODIFIER_always ||
2113 TypeModifier == OMPC_MAP_MODIFIER_close) {
2114 Data.MapTypeModifiers.push_back(TypeModifier);
2115 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
Michael Kruse4304e9d2019-02-19 16:38:20 +00002116 ConsumeToken();
2117 } else if (TypeModifier == OMPC_MAP_MODIFIER_mapper) {
2118 Data.MapTypeModifiers.push_back(TypeModifier);
2119 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
2120 ConsumeToken();
Michael Kruse01f670d2019-02-22 22:29:42 +00002121 if (parseMapperModifier(Data))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002122 return true;
Kelvin Lief579432018-12-18 22:18:41 +00002123 } else {
2124 // For the case of unknown map-type-modifier or a map-type.
2125 // Map-type is followed by a colon; the function returns when it
2126 // encounters a token followed by a colon.
2127 if (Tok.is(tok::comma)) {
Michael Kruse4304e9d2019-02-19 16:38:20 +00002128 Diag(Tok, diag::err_omp_map_type_modifier_missing);
2129 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002130 continue;
2131 }
2132 // Potential map-type token as it is followed by a colon.
2133 if (PP.LookAhead(0).is(tok::colon))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002134 return false;
2135 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
2136 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002137 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002138 if (getCurToken().is(tok::comma))
2139 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002140 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002141 return false;
Kelvin Lief579432018-12-18 22:18:41 +00002142}
2143
2144/// Checks if the token is a valid map-type.
2145static OpenMPMapClauseKind isMapType(Parser &P) {
2146 Token Tok = P.getCurToken();
2147 // The map-type token can be either an identifier or the C++ delete keyword.
2148 if (!Tok.isOneOf(tok::identifier, tok::kw_delete))
2149 return OMPC_MAP_unknown;
2150 Preprocessor &PP = P.getPreprocessor();
2151 OpenMPMapClauseKind MapType = static_cast<OpenMPMapClauseKind>(
2152 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2153 return MapType;
2154}
2155
2156/// Parse map-type in map clause.
2157/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002158/// where, map-type ::= to | from | tofrom | alloc | release | delete
Kelvin Lief579432018-12-18 22:18:41 +00002159static void parseMapType(Parser &P, Parser::OpenMPVarListDataTy &Data) {
2160 Token Tok = P.getCurToken();
2161 if (Tok.is(tok::colon)) {
2162 P.Diag(Tok, diag::err_omp_map_type_missing);
2163 return;
2164 }
2165 Data.MapType = isMapType(P);
2166 if (Data.MapType == OMPC_MAP_unknown)
2167 P.Diag(Tok, diag::err_omp_unknown_map_type);
2168 P.ConsumeToken();
2169}
2170
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002171/// Parses clauses with list.
2172bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
2173 OpenMPClauseKind Kind,
2174 SmallVectorImpl<Expr *> &Vars,
2175 OpenMPVarListDataTy &Data) {
2176 UnqualifiedId UnqualifiedReductionId;
2177 bool InvalidReductionId = false;
Michael Kruse01f670d2019-02-22 22:29:42 +00002178 bool IsInvalidMapperModifier = false;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002179
2180 // Parse '('.
2181 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2182 if (T.expectAndConsume(diag::err_expected_lparen_after,
2183 getOpenMPClauseName(Kind)))
2184 return true;
2185
2186 bool NeedRParenForLinear = false;
2187 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
2188 tok::annot_pragma_openmp_end);
2189 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00002190 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
2191 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002192 ColonProtectionRAIIObject ColonRAII(*this);
2193 if (getLangOpts().CPlusPlus)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002194 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002195 /*ObjectType=*/nullptr,
2196 /*EnteringContext=*/false);
Michael Kruse4304e9d2019-02-19 16:38:20 +00002197 InvalidReductionId = ParseReductionId(
2198 *this, Data.ReductionOrMapperIdScopeSpec, UnqualifiedReductionId);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002199 if (InvalidReductionId) {
2200 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2201 StopBeforeMatch);
2202 }
2203 if (Tok.is(tok::colon))
2204 Data.ColonLoc = ConsumeToken();
2205 else
2206 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
2207 if (!InvalidReductionId)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002208 Data.ReductionOrMapperId =
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002209 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
2210 } else if (Kind == OMPC_depend) {
2211 // Handle dependency type for depend clause.
2212 ColonProtectionRAIIObject ColonRAII(*this);
2213 Data.DepKind =
2214 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
2215 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
2216 Data.DepLinMapLoc = Tok.getLocation();
2217
2218 if (Data.DepKind == OMPC_DEPEND_unknown) {
2219 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2220 StopBeforeMatch);
2221 } else {
2222 ConsumeToken();
2223 // Special processing for depend(source) clause.
2224 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
2225 // Parse ')'.
2226 T.consumeClose();
2227 return false;
2228 }
2229 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002230 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002231 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00002232 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002233 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
2234 : diag::warn_pragma_expected_colon)
2235 << "dependency type";
2236 }
2237 } else if (Kind == OMPC_linear) {
2238 // Try to parse modifier if any.
2239 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
2240 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
2241 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2242 Data.DepLinMapLoc = ConsumeToken();
2243 LinearT.consumeOpen();
2244 NeedRParenForLinear = true;
2245 }
2246 } else if (Kind == OMPC_map) {
2247 // Handle map type for map clause.
2248 ColonProtectionRAIIObject ColonRAII(*this);
2249
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002250 // The first identifier may be a list item, a map-type or a
Kelvin Lief579432018-12-18 22:18:41 +00002251 // map-type-modifier. The map-type can also be delete which has the same
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002252 // spelling of the C++ delete keyword.
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002253 Data.DepLinMapLoc = Tok.getLocation();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002254
Kelvin Lief579432018-12-18 22:18:41 +00002255 // Check for presence of a colon in the map clause.
2256 TentativeParsingAction TPA(*this);
2257 bool ColonPresent = false;
2258 if (SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2259 StopBeforeMatch)) {
2260 if (Tok.is(tok::colon))
2261 ColonPresent = true;
2262 }
2263 TPA.Revert();
2264 // Only parse map-type-modifier[s] and map-type if a colon is present in
2265 // the map clause.
2266 if (ColonPresent) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002267 IsInvalidMapperModifier = parseMapTypeModifiers(Data);
2268 if (!IsInvalidMapperModifier)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002269 parseMapType(*this, Data);
Michael Kruse01f670d2019-02-22 22:29:42 +00002270 else
2271 SkipUntil(tok::colon, tok::annot_pragma_openmp_end, StopBeforeMatch);
Kelvin Lief579432018-12-18 22:18:41 +00002272 }
2273 if (Data.MapType == OMPC_MAP_unknown) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002274 Data.MapType = OMPC_MAP_tofrom;
2275 Data.IsMapTypeImplicit = true;
2276 }
2277
2278 if (Tok.is(tok::colon))
2279 Data.ColonLoc = ConsumeToken();
Michael Kruse0336c752019-02-25 20:34:15 +00002280 } else if (Kind == OMPC_to || Kind == OMPC_from) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002281 if (Tok.is(tok::identifier)) {
2282 bool IsMapperModifier = false;
Michael Kruse0336c752019-02-25 20:34:15 +00002283 if (Kind == OMPC_to) {
2284 auto Modifier = static_cast<OpenMPToModifierKind>(
2285 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2286 if (Modifier == OMPC_TO_MODIFIER_mapper)
2287 IsMapperModifier = true;
2288 } else {
2289 auto Modifier = static_cast<OpenMPFromModifierKind>(
2290 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2291 if (Modifier == OMPC_FROM_MODIFIER_mapper)
2292 IsMapperModifier = true;
2293 }
Michael Kruse01f670d2019-02-22 22:29:42 +00002294 if (IsMapperModifier) {
2295 // Parse the mapper modifier.
2296 ConsumeToken();
2297 IsInvalidMapperModifier = parseMapperModifier(Data);
2298 if (Tok.isNot(tok::colon)) {
2299 if (!IsInvalidMapperModifier)
2300 Diag(Tok, diag::warn_pragma_expected_colon) << ")";
2301 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2302 StopBeforeMatch);
2303 }
2304 // Consume ':'.
2305 if (Tok.is(tok::colon))
2306 ConsumeToken();
2307 }
2308 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002309 }
2310
Alexey Bataevfa312f32017-07-21 18:48:21 +00002311 bool IsComma =
2312 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
2313 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
2314 (Kind == OMPC_reduction && !InvalidReductionId) ||
Kelvin Lida6bc702018-11-21 19:38:53 +00002315 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown) ||
Alexey Bataevfa312f32017-07-21 18:48:21 +00002316 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002317 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
2318 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
2319 Tok.isNot(tok::annot_pragma_openmp_end))) {
2320 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
2321 // Parse variable
2322 ExprResult VarExpr =
2323 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00002324 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002325 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00002326 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002327 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2328 StopBeforeMatch);
2329 }
2330 // Skip ',' if any
2331 IsComma = Tok.is(tok::comma);
2332 if (IsComma)
2333 ConsumeToken();
2334 else if (Tok.isNot(tok::r_paren) &&
2335 Tok.isNot(tok::annot_pragma_openmp_end) &&
2336 (!MayHaveTail || Tok.isNot(tok::colon)))
2337 Diag(Tok, diag::err_omp_expected_punc)
2338 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
2339 : getOpenMPClauseName(Kind))
2340 << (Kind == OMPC_flush);
2341 }
2342
2343 // Parse ')' for linear clause with modifier.
2344 if (NeedRParenForLinear)
2345 LinearT.consumeClose();
2346
2347 // Parse ':' linear-step (or ':' alignment).
2348 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
2349 if (MustHaveTail) {
2350 Data.ColonLoc = Tok.getLocation();
2351 SourceLocation ELoc = ConsumeToken();
2352 ExprResult Tail = ParseAssignmentExpression();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002353 Tail =
2354 Actions.ActOnFinishFullExpr(Tail.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002355 if (Tail.isUsable())
2356 Data.TailExpr = Tail.get();
2357 else
2358 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2359 StopBeforeMatch);
2360 }
2361
2362 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002363 Data.RLoc = Tok.getLocation();
2364 if (!T.consumeClose())
2365 Data.RLoc = T.getCloseLocation();
Alexey Bataev61908f652018-04-23 19:53:05 +00002366 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
2367 Vars.empty()) ||
2368 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
Michael Kruse4304e9d2019-02-19 16:38:20 +00002369 (MustHaveTail && !Data.TailExpr) || InvalidReductionId ||
Michael Kruse01f670d2019-02-22 22:29:42 +00002370 IsInvalidMapperModifier;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002371}
2372
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002373/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00002374/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
2375/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002376///
2377/// private-clause:
2378/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002379/// firstprivate-clause:
2380/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00002381/// lastprivate-clause:
2382/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00002383/// shared-clause:
2384/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00002385/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00002386/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002387/// aligned-clause:
2388/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00002389/// reduction-clause:
2390/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00002391/// task_reduction-clause:
2392/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00002393/// in_reduction-clause:
2394/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00002395/// copyprivate-clause:
2396/// 'copyprivate' '(' list ')'
2397/// flush-clause:
2398/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002399/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00002400/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00002401/// map-clause:
Kelvin Lief579432018-12-18 22:18:41 +00002402/// 'map' '(' [ [ always [,] ] [ close [,] ]
Michael Kruse01f670d2019-02-22 22:29:42 +00002403/// [ mapper '(' mapper-identifier ')' [,] ]
Kelvin Li0bff7af2015-11-23 05:32:03 +00002404/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00002405/// to-clause:
Michael Kruse01f670d2019-02-22 22:29:42 +00002406/// 'to' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00002407/// from-clause:
Michael Kruse0336c752019-02-25 20:34:15 +00002408/// 'from' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00002409/// use_device_ptr-clause:
2410/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00002411/// is_device_ptr-clause:
2412/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002413///
Alexey Bataev182227b2015-08-20 10:54:39 +00002414/// For 'linear' clause linear-list may have the following forms:
2415/// list
2416/// modifier(list)
2417/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00002418OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002419 OpenMPClauseKind Kind,
2420 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002421 SourceLocation Loc = Tok.getLocation();
2422 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002423 SmallVector<Expr *, 4> Vars;
2424 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002425
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002426 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00002427 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002428
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002429 if (ParseOnly)
2430 return nullptr;
Michael Kruse4304e9d2019-02-19 16:38:20 +00002431 OMPVarListLocTy Locs(Loc, LOpen, Data.RLoc);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002432 return Actions.ActOnOpenMPVarListClause(
Michael Kruse4304e9d2019-02-19 16:38:20 +00002433 Kind, Vars, Data.TailExpr, Locs, Data.ColonLoc,
2434 Data.ReductionOrMapperIdScopeSpec, Data.ReductionOrMapperId, Data.DepKind,
2435 Data.LinKind, Data.MapTypeModifiers, Data.MapTypeModifiersLoc,
2436 Data.MapType, Data.IsMapTypeImplicit, Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002437}
2438