blob: e487e0ab652ef737c7aaec0f917ef439ccb85b92 [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexey Bataeva769e072013-03-22 06:34:35 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009/// This file implements parsing of all OpenMP directives and clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000010///
11//===----------------------------------------------------------------------===//
12
Alexey Bataev9959db52014-05-06 10:08:46 +000013#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000014#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000015#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000016#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000017#include "clang/Parse/RAIIObjectsForParser.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000018#include "clang/Sema/Scope.h"
19#include "llvm/ADT/PointerIntPair.h"
Michael Wong65f367f2015-07-21 13:44:28 +000020
Alexey Bataeva769e072013-03-22 06:34:35 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// OpenMP declarative directives.
25//===----------------------------------------------------------------------===//
26
Dmitry Polukhin82478332016-02-13 06:53:38 +000027namespace {
28enum OpenMPDirectiveKindEx {
29 OMPD_cancellation = OMPD_unknown + 1,
30 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000031 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000032 OMPD_end,
33 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000034 OMPD_enter,
35 OMPD_exit,
36 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000037 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000038 OMPD_target_enter,
Samuel Antao686c70c2016-05-26 17:30:50 +000039 OMPD_target_exit,
40 OMPD_update,
Kelvin Li579e41c2016-11-30 23:51:03 +000041 OMPD_distribute_parallel,
Kelvin Li80e8f562016-12-29 22:16:30 +000042 OMPD_teams_distribute_parallel,
Michael Kruse251e1482019-02-01 20:25:04 +000043 OMPD_target_teams_distribute_parallel,
44 OMPD_mapper,
Alexey Bataevd158cf62019-09-13 20:18:17 +000045 OMPD_variant,
Dmitry Polukhin82478332016-02-13 06:53:38 +000046};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000047
Alexey Bataev25ed0c02019-03-07 17:54:44 +000048class DeclDirectiveListParserHelper final {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000049 SmallVector<Expr *, 4> Identifiers;
50 Parser *P;
Alexey Bataev25ed0c02019-03-07 17:54:44 +000051 OpenMPDirectiveKind Kind;
Dmitry Polukhind69b5052016-05-09 14:59:13 +000052
53public:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000054 DeclDirectiveListParserHelper(Parser *P, OpenMPDirectiveKind Kind)
55 : P(P), Kind(Kind) {}
Dmitry Polukhind69b5052016-05-09 14:59:13 +000056 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
Alexey Bataev25ed0c02019-03-07 17:54:44 +000057 ExprResult Res = P->getActions().ActOnOpenMPIdExpression(
58 P->getCurScope(), SS, NameInfo, Kind);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000059 if (Res.isUsable())
60 Identifiers.push_back(Res.get());
61 }
62 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
63};
Dmitry Polukhin82478332016-02-13 06:53:38 +000064} // namespace
65
66// Map token string to extended OMP token kind that are
67// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
68static unsigned getOpenMPDirectiveKindEx(StringRef S) {
69 auto DKind = getOpenMPDirectiveKind(S);
70 if (DKind != OMPD_unknown)
71 return DKind;
72
73 return llvm::StringSwitch<unsigned>(S)
74 .Case("cancellation", OMPD_cancellation)
75 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000076 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000077 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000078 .Case("enter", OMPD_enter)
79 .Case("exit", OMPD_exit)
80 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000081 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000082 .Case("update", OMPD_update)
Michael Kruse251e1482019-02-01 20:25:04 +000083 .Case("mapper", OMPD_mapper)
Alexey Bataevd158cf62019-09-13 20:18:17 +000084 .Case("variant", OMPD_variant)
Dmitry Polukhin82478332016-02-13 06:53:38 +000085 .Default(OMPD_unknown);
86}
87
Alexey Bataev61908f652018-04-23 19:53:05 +000088static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000089 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
90 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
91 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000092 static const unsigned F[][3] = {
Alexey Bataev61908f652018-04-23 19:53:05 +000093 {OMPD_cancellation, OMPD_point, OMPD_cancellation_point},
94 {OMPD_declare, OMPD_reduction, OMPD_declare_reduction},
Michael Kruse251e1482019-02-01 20:25:04 +000095 {OMPD_declare, OMPD_mapper, OMPD_declare_mapper},
Alexey Bataev61908f652018-04-23 19:53:05 +000096 {OMPD_declare, OMPD_simd, OMPD_declare_simd},
97 {OMPD_declare, OMPD_target, OMPD_declare_target},
Alexey Bataevd158cf62019-09-13 20:18:17 +000098 {OMPD_declare, OMPD_variant, OMPD_declare_variant},
Alexey Bataev61908f652018-04-23 19:53:05 +000099 {OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel},
100 {OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for},
101 {OMPD_distribute_parallel_for, OMPD_simd,
102 OMPD_distribute_parallel_for_simd},
103 {OMPD_distribute, OMPD_simd, OMPD_distribute_simd},
104 {OMPD_end, OMPD_declare, OMPD_end_declare},
105 {OMPD_end_declare, OMPD_target, OMPD_end_declare_target},
106 {OMPD_target, OMPD_data, OMPD_target_data},
107 {OMPD_target, OMPD_enter, OMPD_target_enter},
108 {OMPD_target, OMPD_exit, OMPD_target_exit},
109 {OMPD_target, OMPD_update, OMPD_target_update},
110 {OMPD_target_enter, OMPD_data, OMPD_target_enter_data},
111 {OMPD_target_exit, OMPD_data, OMPD_target_exit_data},
112 {OMPD_for, OMPD_simd, OMPD_for_simd},
113 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
114 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
115 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
116 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd},
117 {OMPD_target, OMPD_parallel, OMPD_target_parallel},
118 {OMPD_target, OMPD_simd, OMPD_target_simd},
119 {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for},
120 {OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd},
121 {OMPD_teams, OMPD_distribute, OMPD_teams_distribute},
122 {OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd},
123 {OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel},
124 {OMPD_teams_distribute_parallel, OMPD_for,
125 OMPD_teams_distribute_parallel_for},
126 {OMPD_teams_distribute_parallel_for, OMPD_simd,
127 OMPD_teams_distribute_parallel_for_simd},
128 {OMPD_target, OMPD_teams, OMPD_target_teams},
129 {OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute},
130 {OMPD_target_teams_distribute, OMPD_parallel,
131 OMPD_target_teams_distribute_parallel},
132 {OMPD_target_teams_distribute, OMPD_simd,
133 OMPD_target_teams_distribute_simd},
134 {OMPD_target_teams_distribute_parallel, OMPD_for,
135 OMPD_target_teams_distribute_parallel_for},
136 {OMPD_target_teams_distribute_parallel_for, OMPD_simd,
137 OMPD_target_teams_distribute_parallel_for_simd}};
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000138 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev61908f652018-04-23 19:53:05 +0000139 Token Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000140 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000141 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000142 ? static_cast<unsigned>(OMPD_unknown)
143 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
144 if (DKind == OMPD_unknown)
145 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000146
Alexey Bataev61908f652018-04-23 19:53:05 +0000147 for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) {
148 if (DKind != F[I][0])
Dmitry Polukhin82478332016-02-13 06:53:38 +0000149 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000150
Dmitry Polukhin82478332016-02-13 06:53:38 +0000151 Tok = P.getPreprocessor().LookAhead(0);
152 unsigned SDKind =
153 Tok.isAnnotation()
154 ? static_cast<unsigned>(OMPD_unknown)
155 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
156 if (SDKind == OMPD_unknown)
157 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000158
Alexey Bataev61908f652018-04-23 19:53:05 +0000159 if (SDKind == F[I][1]) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000160 P.ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000161 DKind = F[I][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000162 }
163 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000164 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
165 : OMPD_unknown;
166}
167
168static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000169 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000170 Sema &Actions = P.getActions();
171 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000172 // Allow to use 'operator' keyword for C++ operators
173 bool WithOperator = false;
174 if (Tok.is(tok::kw_operator)) {
175 P.ConsumeToken();
176 Tok = P.getCurToken();
177 WithOperator = true;
178 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000179 switch (Tok.getKind()) {
180 case tok::plus: // '+'
181 OOK = OO_Plus;
182 break;
183 case tok::minus: // '-'
184 OOK = OO_Minus;
185 break;
186 case tok::star: // '*'
187 OOK = OO_Star;
188 break;
189 case tok::amp: // '&'
190 OOK = OO_Amp;
191 break;
192 case tok::pipe: // '|'
193 OOK = OO_Pipe;
194 break;
195 case tok::caret: // '^'
196 OOK = OO_Caret;
197 break;
198 case tok::ampamp: // '&&'
199 OOK = OO_AmpAmp;
200 break;
201 case tok::pipepipe: // '||'
202 OOK = OO_PipePipe;
203 break;
204 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000205 if (!WithOperator)
206 break;
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000207 LLVM_FALLTHROUGH;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000208 default:
209 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
210 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
211 Parser::StopBeforeMatch);
212 return DeclarationName();
213 }
214 P.ConsumeToken();
215 auto &DeclNames = Actions.getASTContext().DeclarationNames;
216 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
217 : DeclNames.getCXXOperatorName(OOK);
218}
219
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000220/// Parse 'omp declare reduction' construct.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000221///
222/// declare-reduction-directive:
223/// annot_pragma_openmp 'declare' 'reduction'
224/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
225/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
226/// annot_pragma_openmp_end
227/// <reduction_id> is either a base language identifier or one of the following
228/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
229///
230Parser::DeclGroupPtrTy
231Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
232 // Parse '('.
233 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
234 if (T.expectAndConsume(diag::err_expected_lparen_after,
235 getOpenMPDirectiveName(OMPD_declare_reduction))) {
236 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
237 return DeclGroupPtrTy();
238 }
239
240 DeclarationName Name = parseOpenMPReductionId(*this);
241 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
242 return DeclGroupPtrTy();
243
244 // Consume ':'.
245 bool IsCorrect = !ExpectAndConsume(tok::colon);
246
247 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
248 return DeclGroupPtrTy();
249
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000250 IsCorrect = IsCorrect && !Name.isEmpty();
251
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000252 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
253 Diag(Tok.getLocation(), diag::err_expected_type);
254 IsCorrect = false;
255 }
256
257 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
258 return DeclGroupPtrTy();
259
260 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
261 // Parse list of types until ':' token.
262 do {
263 ColonProtectionRAIIObject ColonRAII(*this);
264 SourceRange Range;
Faisal Vali421b2d12017-12-29 05:41:00 +0000265 TypeResult TR =
266 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000267 if (TR.isUsable()) {
Alexey Bataev61908f652018-04-23 19:53:05 +0000268 QualType ReductionType =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000269 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
270 if (!ReductionType.isNull()) {
271 ReductionTypes.push_back(
272 std::make_pair(ReductionType, Range.getBegin()));
273 }
274 } else {
275 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
276 StopBeforeMatch);
277 }
278
279 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
280 break;
281
282 // Consume ','.
283 if (ExpectAndConsume(tok::comma)) {
284 IsCorrect = false;
285 if (Tok.is(tok::annot_pragma_openmp_end)) {
286 Diag(Tok.getLocation(), diag::err_expected_type);
287 return DeclGroupPtrTy();
288 }
289 }
290 } while (Tok.isNot(tok::annot_pragma_openmp_end));
291
292 if (ReductionTypes.empty()) {
293 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
294 return DeclGroupPtrTy();
295 }
296
297 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
298 return DeclGroupPtrTy();
299
300 // Consume ':'.
301 if (ExpectAndConsume(tok::colon))
302 IsCorrect = false;
303
304 if (Tok.is(tok::annot_pragma_openmp_end)) {
305 Diag(Tok.getLocation(), diag::err_expected_expression);
306 return DeclGroupPtrTy();
307 }
308
309 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
310 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
311
312 // Parse <combiner> expression and then parse initializer if any for each
313 // correct type.
314 unsigned I = 0, E = ReductionTypes.size();
Alexey Bataev61908f652018-04-23 19:53:05 +0000315 for (Decl *D : DRD.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000316 TentativeParsingAction TPA(*this);
317 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000318 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000319 Scope::OpenMPDirectiveScope);
320 // Parse <combiner> expression.
321 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
322 ExprResult CombinerResult =
323 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000324 D->getLocation(), /*DiscardedValue*/ false);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000325 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
326
327 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
328 Tok.isNot(tok::annot_pragma_openmp_end)) {
329 TPA.Commit();
330 IsCorrect = false;
331 break;
332 }
333 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
334 ExprResult InitializerResult;
335 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
336 // Parse <initializer> expression.
337 if (Tok.is(tok::identifier) &&
Alexey Bataev61908f652018-04-23 19:53:05 +0000338 Tok.getIdentifierInfo()->isStr("initializer")) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000339 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000340 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000341 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
342 TPA.Commit();
343 IsCorrect = false;
344 break;
345 }
346 // Parse '('.
347 BalancedDelimiterTracker T(*this, tok::l_paren,
348 tok::annot_pragma_openmp_end);
349 IsCorrect =
350 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
351 IsCorrect;
352 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
353 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000354 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000355 Scope::OpenMPDirectiveScope);
356 // Parse expression.
Alexey Bataev070f43a2017-09-06 14:49:58 +0000357 VarDecl *OmpPrivParm =
358 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
359 D);
360 // Check if initializer is omp_priv <init_expr> or something else.
361 if (Tok.is(tok::identifier) &&
362 Tok.getIdentifierInfo()->isStr("omp_priv")) {
Alexey Bataeve6aa4692018-09-13 16:54:05 +0000363 if (Actions.getLangOpts().CPlusPlus) {
364 InitializerResult = Actions.ActOnFinishFullExpr(
365 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000366 /*DiscardedValue*/ false);
Alexey Bataeve6aa4692018-09-13 16:54:05 +0000367 } else {
368 ConsumeToken();
369 ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
370 }
Alexey Bataev070f43a2017-09-06 14:49:58 +0000371 } else {
372 InitializerResult = Actions.ActOnFinishFullExpr(
373 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000374 /*DiscardedValue*/ false);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000375 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000376 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
Alexey Bataev070f43a2017-09-06 14:49:58 +0000377 D, InitializerResult.get(), OmpPrivParm);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000378 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
379 Tok.isNot(tok::annot_pragma_openmp_end)) {
380 TPA.Commit();
381 IsCorrect = false;
382 break;
383 }
384 IsCorrect =
385 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
386 }
387 }
388
389 ++I;
390 // Revert parsing if not the last type, otherwise accept it, we're done with
391 // parsing.
392 if (I != E)
393 TPA.Revert();
394 else
395 TPA.Commit();
396 }
397 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
398 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000399}
400
Alexey Bataev070f43a2017-09-06 14:49:58 +0000401void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
402 // Parse declarator '=' initializer.
403 // If a '==' or '+=' is found, suggest a fixit to '='.
404 if (isTokenEqualOrEqualTypo()) {
405 ConsumeToken();
406
407 if (Tok.is(tok::code_completion)) {
408 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
409 Actions.FinalizeDeclaration(OmpPrivParm);
410 cutOffParsing();
411 return;
412 }
413
414 ExprResult Init(ParseInitializer());
415
416 if (Init.isInvalid()) {
417 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
418 Actions.ActOnInitializerError(OmpPrivParm);
419 } else {
420 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
421 /*DirectInit=*/false);
422 }
423 } else if (Tok.is(tok::l_paren)) {
424 // Parse C++ direct initializer: '(' expression-list ')'
425 BalancedDelimiterTracker T(*this, tok::l_paren);
426 T.consumeOpen();
427
428 ExprVector Exprs;
429 CommaLocsTy CommaLocs;
430
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000431 SourceLocation LParLoc = T.getOpenLocation();
Ilya Biryukovff2a9972019-02-26 11:01:50 +0000432 auto RunSignatureHelp = [this, OmpPrivParm, LParLoc, &Exprs]() {
433 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
434 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
435 OmpPrivParm->getLocation(), Exprs, LParLoc);
436 CalledSignatureHelp = true;
437 return PreferredType;
438 };
439 if (ParseExpressionList(Exprs, CommaLocs, [&] {
440 PreferredType.enterFunctionArgument(Tok.getLocation(),
441 RunSignatureHelp);
442 })) {
443 if (PP.isCodeCompletionReached() && !CalledSignatureHelp)
444 RunSignatureHelp();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000445 Actions.ActOnInitializerError(OmpPrivParm);
446 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
447 } else {
448 // Match the ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000449 SourceLocation RLoc = Tok.getLocation();
450 if (!T.consumeClose())
451 RLoc = T.getCloseLocation();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000452
453 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
454 "Unexpected number of commas!");
455
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000456 ExprResult Initializer =
457 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000458 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
459 /*DirectInit=*/true);
460 }
461 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
462 // Parse C++0x braced-init-list.
463 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
464
465 ExprResult Init(ParseBraceInitializer());
466
467 if (Init.isInvalid()) {
468 Actions.ActOnInitializerError(OmpPrivParm);
469 } else {
470 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
471 /*DirectInit=*/true);
472 }
473 } else {
474 Actions.ActOnUninitializedDecl(OmpPrivParm);
475 }
476}
477
Michael Kruse251e1482019-02-01 20:25:04 +0000478/// Parses 'omp declare mapper' directive.
479///
480/// declare-mapper-directive:
481/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifier> ':']
482/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
483/// annot_pragma_openmp_end
484/// <mapper-identifier> and <var> are base language identifiers.
485///
486Parser::DeclGroupPtrTy
487Parser::ParseOpenMPDeclareMapperDirective(AccessSpecifier AS) {
488 bool IsCorrect = true;
489 // Parse '('
490 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
491 if (T.expectAndConsume(diag::err_expected_lparen_after,
492 getOpenMPDirectiveName(OMPD_declare_mapper))) {
493 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
494 return DeclGroupPtrTy();
495 }
496
497 // Parse <mapper-identifier>
498 auto &DeclNames = Actions.getASTContext().DeclarationNames;
499 DeclarationName MapperId;
500 if (PP.LookAhead(0).is(tok::colon)) {
501 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
502 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
503 IsCorrect = false;
504 } else {
505 MapperId = DeclNames.getIdentifier(Tok.getIdentifierInfo());
506 }
507 ConsumeToken();
508 // Consume ':'.
509 ExpectAndConsume(tok::colon);
510 } else {
511 // If no mapper identifier is provided, its name is "default" by default
512 MapperId =
513 DeclNames.getIdentifier(&Actions.getASTContext().Idents.get("default"));
514 }
515
516 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
517 return DeclGroupPtrTy();
518
519 // Parse <type> <var>
520 DeclarationName VName;
521 QualType MapperType;
522 SourceRange Range;
523 TypeResult ParsedType = parseOpenMPDeclareMapperVarDecl(Range, VName, AS);
524 if (ParsedType.isUsable())
525 MapperType =
526 Actions.ActOnOpenMPDeclareMapperType(Range.getBegin(), ParsedType);
527 if (MapperType.isNull())
528 IsCorrect = false;
529 if (!IsCorrect) {
530 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
531 return DeclGroupPtrTy();
532 }
533
534 // Consume ')'.
535 IsCorrect &= !T.consumeClose();
536 if (!IsCorrect) {
537 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
538 return DeclGroupPtrTy();
539 }
540
541 // Enter scope.
542 OMPDeclareMapperDecl *DMD = Actions.ActOnOpenMPDeclareMapperDirectiveStart(
543 getCurScope(), Actions.getCurLexicalContext(), MapperId, MapperType,
544 Range.getBegin(), VName, AS);
545 DeclarationNameInfo DirName;
546 SourceLocation Loc = Tok.getLocation();
547 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
548 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
549 ParseScope OMPDirectiveScope(this, ScopeFlags);
550 Actions.StartOpenMPDSABlock(OMPD_declare_mapper, DirName, getCurScope(), Loc);
551
552 // Add the mapper variable declaration.
553 Actions.ActOnOpenMPDeclareMapperDirectiveVarDecl(
554 DMD, getCurScope(), MapperType, Range.getBegin(), VName);
555
556 // Parse map clauses.
557 SmallVector<OMPClause *, 6> Clauses;
558 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
559 OpenMPClauseKind CKind = Tok.isAnnotation()
560 ? OMPC_unknown
561 : getOpenMPClauseKind(PP.getSpelling(Tok));
562 Actions.StartOpenMPClause(CKind);
563 OMPClause *Clause =
564 ParseOpenMPClause(OMPD_declare_mapper, CKind, Clauses.size() == 0);
565 if (Clause)
566 Clauses.push_back(Clause);
567 else
568 IsCorrect = false;
569 // Skip ',' if any.
570 if (Tok.is(tok::comma))
571 ConsumeToken();
572 Actions.EndOpenMPClause();
573 }
574 if (Clauses.empty()) {
575 Diag(Tok, diag::err_omp_expected_clause)
576 << getOpenMPDirectiveName(OMPD_declare_mapper);
577 IsCorrect = false;
578 }
579
580 // Exit scope.
581 Actions.EndOpenMPDSABlock(nullptr);
582 OMPDirectiveScope.Exit();
583
584 DeclGroupPtrTy DGP =
585 Actions.ActOnOpenMPDeclareMapperDirectiveEnd(DMD, getCurScope(), Clauses);
586 if (!IsCorrect)
587 return DeclGroupPtrTy();
588 return DGP;
589}
590
591TypeResult Parser::parseOpenMPDeclareMapperVarDecl(SourceRange &Range,
592 DeclarationName &Name,
593 AccessSpecifier AS) {
594 // Parse the common declaration-specifiers piece.
595 Parser::DeclSpecContext DSC = Parser::DeclSpecContext::DSC_type_specifier;
596 DeclSpec DS(AttrFactory);
597 ParseSpecifierQualifierList(DS, AS, DSC);
598
599 // Parse the declarator.
600 DeclaratorContext Context = DeclaratorContext::PrototypeContext;
601 Declarator DeclaratorInfo(DS, Context);
602 ParseDeclarator(DeclaratorInfo);
603 Range = DeclaratorInfo.getSourceRange();
604 if (DeclaratorInfo.getIdentifier() == nullptr) {
605 Diag(Tok.getLocation(), diag::err_omp_mapper_expected_declarator);
606 return true;
607 }
608 Name = Actions.GetNameForDeclarator(DeclaratorInfo).getName();
609
610 return Actions.ActOnOpenMPDeclareMapperVarDecl(getCurScope(), DeclaratorInfo);
611}
612
Alexey Bataev2af33e32016-04-07 12:45:37 +0000613namespace {
614/// RAII that recreates function context for correct parsing of clauses of
615/// 'declare simd' construct.
616/// OpenMP, 2.8.2 declare simd Construct
617/// The expressions appearing in the clauses of this directive are evaluated in
618/// the scope of the arguments of the function declaration or definition.
619class FNContextRAII final {
620 Parser &P;
621 Sema::CXXThisScopeRAII *ThisScope;
622 Parser::ParseScope *TempScope;
623 Parser::ParseScope *FnScope;
624 bool HasTemplateScope = false;
625 bool HasFunScope = false;
626 FNContextRAII() = delete;
627 FNContextRAII(const FNContextRAII &) = delete;
628 FNContextRAII &operator=(const FNContextRAII &) = delete;
629
630public:
631 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
632 Decl *D = *Ptr.get().begin();
633 NamedDecl *ND = dyn_cast<NamedDecl>(D);
634 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
635 Sema &Actions = P.getActions();
636
637 // Allow 'this' within late-parsed attributes.
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000638 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, Qualifiers(),
Alexey Bataev2af33e32016-04-07 12:45:37 +0000639 ND && ND->isCXXInstanceMember());
640
641 // If the Decl is templatized, add template parameters to scope.
642 HasTemplateScope = D->isTemplateDecl();
643 TempScope =
644 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
645 if (HasTemplateScope)
646 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
647
648 // If the Decl is on a function, add function parameters to the scope.
649 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000650 FnScope = new Parser::ParseScope(
651 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
652 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000653 if (HasFunScope)
654 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
655 }
656 ~FNContextRAII() {
657 if (HasFunScope) {
658 P.getActions().ActOnExitFunctionContext();
659 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
660 }
661 if (HasTemplateScope)
662 TempScope->Exit();
663 delete FnScope;
664 delete TempScope;
665 delete ThisScope;
666 }
667};
668} // namespace
669
Alexey Bataevd93d3762016-04-12 09:35:56 +0000670/// Parses clauses for 'declare simd' directive.
671/// clause:
672/// 'inbranch' | 'notinbranch'
673/// 'simdlen' '(' <expr> ')'
674/// { 'uniform' '(' <argument_list> ')' }
675/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000676/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
677static bool parseDeclareSimdClauses(
678 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
679 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
680 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
681 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000682 SourceRange BSRange;
683 const Token &Tok = P.getCurToken();
684 bool IsError = false;
685 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
686 if (Tok.isNot(tok::identifier))
687 break;
688 OMPDeclareSimdDeclAttr::BranchStateTy Out;
689 IdentifierInfo *II = Tok.getIdentifierInfo();
690 StringRef ClauseName = II->getName();
691 // Parse 'inranch|notinbranch' clauses.
692 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
693 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
694 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
695 << ClauseName
696 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
697 IsError = true;
698 }
699 BS = Out;
700 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
701 P.ConsumeToken();
702 } else if (ClauseName.equals("simdlen")) {
703 if (SimdLen.isUsable()) {
704 P.Diag(Tok, diag::err_omp_more_one_clause)
705 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
706 IsError = true;
707 }
708 P.ConsumeToken();
709 SourceLocation RLoc;
710 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
711 if (SimdLen.isInvalid())
712 IsError = true;
713 } else {
714 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000715 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
716 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000717 Parser::OpenMPVarListDataTy Data;
Alexey Bataev61908f652018-04-23 19:53:05 +0000718 SmallVectorImpl<Expr *> *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000719 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000720 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000721 else if (CKind == OMPC_linear)
722 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000723
724 P.ConsumeToken();
725 if (P.ParseOpenMPVarList(OMPD_declare_simd,
726 getOpenMPClauseKind(ClauseName), *Vars, Data))
727 IsError = true;
Alexey Bataev61908f652018-04-23 19:53:05 +0000728 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000729 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataev61908f652018-04-23 19:53:05 +0000730 } else if (CKind == OMPC_linear) {
Alexey Bataevecba70f2016-04-12 11:02:11 +0000731 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
732 Data.DepLinMapLoc))
733 Data.LinKind = OMPC_LINEAR_val;
734 LinModifiers.append(Linears.size() - LinModifiers.size(),
735 Data.LinKind);
736 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
737 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000738 } else
739 // TODO: add parsing of other clauses.
740 break;
741 }
742 // Skip ',' if any.
743 if (Tok.is(tok::comma))
744 P.ConsumeToken();
745 }
746 return IsError;
747}
748
Alexey Bataev2af33e32016-04-07 12:45:37 +0000749/// Parse clauses for '#pragma omp declare simd'.
750Parser::DeclGroupPtrTy
751Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
752 CachedTokens &Toks, SourceLocation Loc) {
Ilya Biryukov929af672019-05-17 09:32:05 +0000753 PP.EnterToken(Tok, /*IsReinject*/ true);
754 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
755 /*IsReinject*/ true);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000756 // Consume the previously pushed token.
757 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Alexey Bataevd158cf62019-09-13 20:18:17 +0000758 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000759
760 FNContextRAII FnContext(*this, Ptr);
761 OMPDeclareSimdDeclAttr::BranchStateTy BS =
762 OMPDeclareSimdDeclAttr::BS_Undefined;
763 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000764 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000765 SmallVector<Expr *, 4> Aligneds;
766 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000767 SmallVector<Expr *, 4> Linears;
768 SmallVector<unsigned, 4> LinModifiers;
769 SmallVector<Expr *, 4> Steps;
770 bool IsError =
771 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
772 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000773 // Need to check for extra tokens.
774 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
775 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
776 << getOpenMPDirectiveName(OMPD_declare_simd);
777 while (Tok.isNot(tok::annot_pragma_openmp_end))
778 ConsumeAnyToken();
779 }
780 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000781 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000782 if (IsError)
783 return Ptr;
784 return Actions.ActOnOpenMPDeclareSimdDirective(
785 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
786 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataev20dfd772016-04-04 10:12:15 +0000787}
788
Alexey Bataeva15a1412019-10-02 18:19:02 +0000789/// Parse optional 'score' '(' <expr> ')' ':'.
790static ExprResult parseContextScore(Parser &P) {
791 ExprResult ScoreExpr;
792 SmallString<16> Buffer;
793 StringRef SelectorName =
794 P.getPreprocessor().getSpelling(P.getCurToken(), Buffer);
795 OMPDeclareVariantAttr::ScoreType ScoreKind =
796 OMPDeclareVariantAttr::ScoreUnknown;
797 (void)OMPDeclareVariantAttr::ConvertStrToScoreType(SelectorName, ScoreKind);
798 if (ScoreKind == OMPDeclareVariantAttr::ScoreUnknown)
799 return ScoreExpr;
800 assert(ScoreKind == OMPDeclareVariantAttr::ScoreSpecified &&
801 "Expected \"score\" clause.");
802 (void)P.ConsumeToken();
803 SourceLocation RLoc;
804 ScoreExpr = P.ParseOpenMPParensExpr(SelectorName, RLoc);
805 // Parse ':'
806 if (P.getCurToken().is(tok::colon))
807 (void)P.ConsumeAnyToken();
808 else
809 P.Diag(P.getCurToken(), diag::warn_pragma_expected_colon)
810 << "context selector score clause";
811 return ScoreExpr;
812}
813
Alexey Bataev9ff34742019-09-25 19:43:37 +0000814/// Parse context selector for 'implementation' selector set:
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000815/// 'vendor' '(' [ 'score' '(' <score _expr> ')' ':' ] <vendor> { ',' <vendor> }
816/// ')'
817static void parseImplementationSelector(
818 Parser &P, SourceLocation Loc,
819 llvm::function_ref<void(SourceRange,
820 const Sema::OpenMPDeclareVariantCtsSelectorData &)>
821 Callback) {
Alexey Bataev9ff34742019-09-25 19:43:37 +0000822 const Token &Tok = P.getCurToken();
823 // Parse inner context selector set name, if any.
824 if (!Tok.is(tok::identifier)) {
825 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
826 << "implementation";
827 // Skip until either '}', ')', or end of directive.
828 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
829 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
830 ;
831 return;
832 }
833 SmallString<16> Buffer;
834 StringRef CtxSelectorName = P.getPreprocessor().getSpelling(Tok, Buffer);
835 OMPDeclareVariantAttr::CtxSelectorType CSKind =
836 OMPDeclareVariantAttr::CtxUnknown;
837 (void)OMPDeclareVariantAttr::ConvertStrToCtxSelectorType(CtxSelectorName,
838 CSKind);
839 (void)P.ConsumeToken();
840 switch (CSKind) {
841 case OMPDeclareVariantAttr::CtxVendor: {
842 // Parse '('.
843 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
844 (void)T.expectAndConsume(diag::err_expected_lparen_after,
845 CtxSelectorName.data());
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000846 const ExprResult Score = parseContextScore(P);
847 do {
848 // Parse <vendor>.
849 StringRef VendorName;
850 if (Tok.is(tok::identifier)) {
851 Buffer.clear();
852 VendorName = P.getPreprocessor().getSpelling(P.getCurToken(), Buffer);
853 (void)P.ConsumeToken();
854 } else {
855 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_item_expected)
856 << "vendor identifier"
857 << "vendor"
858 << "implementation";
859 }
860 if (!VendorName.empty()) {
861 Sema::OpenMPDeclareVariantCtsSelectorData Data(
862 OMPDeclareVariantAttr::CtxSetImplementation, CSKind, VendorName,
863 Score);
864 Callback(SourceRange(Loc, Tok.getLocation()), Data);
865 }
866 if (!P.TryConsumeToken(tok::comma) && Tok.isNot(tok::r_paren)) {
867 P.Diag(Tok, diag::err_expected_punc)
868 << (VendorName.empty() ? "vendor name" : VendorName);
869 }
870 } while (Tok.is(tok::identifier));
Alexey Bataev9ff34742019-09-25 19:43:37 +0000871 // Parse ')'.
872 (void)T.consumeClose();
Alexey Bataev9ff34742019-09-25 19:43:37 +0000873 break;
874 }
875 case OMPDeclareVariantAttr::CtxUnknown:
876 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
877 << "implementation";
878 // Skip until either '}', ')', or end of directive.
879 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
880 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
881 ;
882 return;
883 }
Alexey Bataev9ff34742019-09-25 19:43:37 +0000884}
885
Alexey Bataevd158cf62019-09-13 20:18:17 +0000886/// Parses clauses for 'declare variant' directive.
887/// clause:
Alexey Bataevd158cf62019-09-13 20:18:17 +0000888/// <selector_set_name> '=' '{' <context_selectors> '}'
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000889/// [ ',' <selector_set_name> '=' '{' <context_selectors> '}' ]
890bool Parser::parseOpenMPContextSelectors(
Alexey Bataev9ff34742019-09-25 19:43:37 +0000891 SourceLocation Loc,
892 llvm::function_ref<void(SourceRange,
893 const Sema::OpenMPDeclareVariantCtsSelectorData &)>
894 Callback) {
Alexey Bataev5d154c32019-10-08 15:56:43 +0000895 llvm::StringMap<SourceLocation> UsedCtxSets;
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000896 do {
897 // Parse inner context selector set name.
898 if (!Tok.is(tok::identifier)) {
899 Diag(Tok.getLocation(), diag::err_omp_declare_variant_no_ctx_selector)
Alexey Bataevdba792c2019-09-23 18:13:31 +0000900 << getOpenMPClauseName(OMPC_match);
Alexey Bataevd158cf62019-09-13 20:18:17 +0000901 return true;
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000902 }
903 SmallString<16> Buffer;
Alexey Bataev9ff34742019-09-25 19:43:37 +0000904 StringRef CtxSelectorSetName = PP.getSpelling(Tok, Buffer);
Alexey Bataev5d154c32019-10-08 15:56:43 +0000905 auto Res = UsedCtxSets.try_emplace(CtxSelectorSetName, Tok.getLocation());
906 if (!Res.second) {
907 // OpenMP 5.0, 2.3.2 Context Selectors, Restrictions.
908 // Each trait-set-selector-name can only be specified once.
909 Diag(Tok.getLocation(), diag::err_omp_declare_variant_ctx_set_mutiple_use)
910 << CtxSelectorSetName;
911 Diag(Res.first->getValue(),
912 diag::note_omp_declare_variant_ctx_set_used_here)
913 << CtxSelectorSetName;
914 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000915 // Parse '='.
916 (void)ConsumeToken();
917 if (Tok.isNot(tok::equal)) {
918 Diag(Tok.getLocation(), diag::err_omp_declare_variant_equal_expected)
Alexey Bataev9ff34742019-09-25 19:43:37 +0000919 << CtxSelectorSetName;
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000920 return true;
921 }
922 (void)ConsumeToken();
923 // TBD: add parsing of known context selectors.
924 // Unknown selector - just ignore it completely.
925 {
926 // Parse '{'.
927 BalancedDelimiterTracker TBr(*this, tok::l_brace,
928 tok::annot_pragma_openmp_end);
929 if (TBr.expectAndConsume(diag::err_expected_lbrace_after, "="))
930 return true;
Alexey Bataev9ff34742019-09-25 19:43:37 +0000931 OMPDeclareVariantAttr::CtxSelectorSetType CSSKind =
932 OMPDeclareVariantAttr::CtxSetUnknown;
933 (void)OMPDeclareVariantAttr::ConvertStrToCtxSelectorSetType(
934 CtxSelectorSetName, CSSKind);
935 switch (CSSKind) {
936 case OMPDeclareVariantAttr::CtxSetImplementation:
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000937 parseImplementationSelector(*this, Loc, Callback);
Alexey Bataev9ff34742019-09-25 19:43:37 +0000938 break;
939 case OMPDeclareVariantAttr::CtxSetUnknown:
940 // Skip until either '}', ')', or end of directive.
941 while (!SkipUntil(tok::r_brace, tok::r_paren,
942 tok::annot_pragma_openmp_end, StopBeforeMatch))
943 ;
944 break;
945 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000946 // Parse '}'.
947 (void)TBr.consumeClose();
948 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000949 // Consume ','
950 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end))
951 (void)ExpectAndConsume(tok::comma);
952 } while (Tok.isAnyIdentifier());
Alexey Bataevd158cf62019-09-13 20:18:17 +0000953 return false;
954}
955
956/// Parse clauses for '#pragma omp declare variant ( variant-func-id ) clause'.
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000957void Parser::ParseOMPDeclareVariantClauses(Parser::DeclGroupPtrTy Ptr,
958 CachedTokens &Toks,
959 SourceLocation Loc) {
Alexey Bataevd158cf62019-09-13 20:18:17 +0000960 PP.EnterToken(Tok, /*IsReinject*/ true);
961 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
962 /*IsReinject*/ true);
963 // Consume the previously pushed token.
964 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
965 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
966
967 FNContextRAII FnContext(*this, Ptr);
968 // Parse function declaration id.
969 SourceLocation RLoc;
970 // Parse with IsAddressOfOperand set to true to parse methods as DeclRefExprs
971 // instead of MemberExprs.
972 ExprResult AssociatedFunction =
973 ParseOpenMPParensExpr(getOpenMPDirectiveName(OMPD_declare_variant), RLoc,
974 /*IsAddressOfOperand=*/true);
975 if (!AssociatedFunction.isUsable()) {
976 if (!Tok.is(tok::annot_pragma_openmp_end))
977 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
978 ;
979 // Skip the last annot_pragma_openmp_end.
980 (void)ConsumeAnnotationToken();
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000981 return;
982 }
983 Optional<std::pair<FunctionDecl *, Expr *>> DeclVarData =
984 Actions.checkOpenMPDeclareVariantFunction(
985 Ptr, AssociatedFunction.get(), SourceRange(Loc, Tok.getLocation()));
986
987 // Parse 'match'.
Alexey Bataevdba792c2019-09-23 18:13:31 +0000988 OpenMPClauseKind CKind = Tok.isAnnotation()
989 ? OMPC_unknown
990 : getOpenMPClauseKind(PP.getSpelling(Tok));
991 if (CKind != OMPC_match) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000992 Diag(Tok.getLocation(), diag::err_omp_declare_variant_wrong_clause)
Alexey Bataevdba792c2019-09-23 18:13:31 +0000993 << getOpenMPClauseName(OMPC_match);
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000994 while (!SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
995 ;
996 // Skip the last annot_pragma_openmp_end.
997 (void)ConsumeAnnotationToken();
998 return;
999 }
1000 (void)ConsumeToken();
1001 // Parse '('.
1002 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataevdba792c2019-09-23 18:13:31 +00001003 if (T.expectAndConsume(diag::err_expected_lparen_after,
1004 getOpenMPClauseName(OMPC_match))) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001005 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
1006 ;
1007 // Skip the last annot_pragma_openmp_end.
1008 (void)ConsumeAnnotationToken();
1009 return;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001010 }
1011
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001012 // Parse inner context selectors.
Alexey Bataev9ff34742019-09-25 19:43:37 +00001013 if (!parseOpenMPContextSelectors(
1014 Loc, [this, &DeclVarData](
1015 SourceRange SR,
1016 const Sema::OpenMPDeclareVariantCtsSelectorData &Data) {
1017 if (DeclVarData.hasValue())
1018 Actions.ActOnOpenMPDeclareVariantDirective(
1019 DeclVarData.getValue().first, DeclVarData.getValue().second,
1020 SR, Data);
1021 })) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001022 // Parse ')'.
1023 (void)T.consumeClose();
1024 // Need to check for extra tokens.
1025 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1026 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1027 << getOpenMPDirectiveName(OMPD_declare_variant);
1028 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001029 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001030
1031 // Skip last tokens.
1032 while (Tok.isNot(tok::annot_pragma_openmp_end))
1033 ConsumeAnyToken();
Alexey Bataevd158cf62019-09-13 20:18:17 +00001034 // Skip the last annot_pragma_openmp_end.
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001035 (void)ConsumeAnnotationToken();
Alexey Bataevd158cf62019-09-13 20:18:17 +00001036}
1037
Alexey Bataev729e2422019-08-23 16:11:14 +00001038/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
1039///
1040/// default-clause:
1041/// 'default' '(' 'none' | 'shared' ')
1042///
1043/// proc_bind-clause:
1044/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1045///
1046/// device_type-clause:
1047/// 'device_type' '(' 'host' | 'nohost' | 'any' )'
1048namespace {
1049 struct SimpleClauseData {
1050 unsigned Type;
1051 SourceLocation Loc;
1052 SourceLocation LOpen;
1053 SourceLocation TypeLoc;
1054 SourceLocation RLoc;
1055 SimpleClauseData(unsigned Type, SourceLocation Loc, SourceLocation LOpen,
1056 SourceLocation TypeLoc, SourceLocation RLoc)
1057 : Type(Type), Loc(Loc), LOpen(LOpen), TypeLoc(TypeLoc), RLoc(RLoc) {}
1058 };
1059} // anonymous namespace
1060
1061static Optional<SimpleClauseData>
1062parseOpenMPSimpleClause(Parser &P, OpenMPClauseKind Kind) {
1063 const Token &Tok = P.getCurToken();
1064 SourceLocation Loc = Tok.getLocation();
1065 SourceLocation LOpen = P.ConsumeToken();
1066 // Parse '('.
1067 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
1068 if (T.expectAndConsume(diag::err_expected_lparen_after,
1069 getOpenMPClauseName(Kind)))
1070 return llvm::None;
1071
1072 unsigned Type = getOpenMPSimpleClauseType(
1073 Kind, Tok.isAnnotation() ? "" : P.getPreprocessor().getSpelling(Tok));
1074 SourceLocation TypeLoc = Tok.getLocation();
1075 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1076 Tok.isNot(tok::annot_pragma_openmp_end))
1077 P.ConsumeAnyToken();
1078
1079 // Parse ')'.
1080 SourceLocation RLoc = Tok.getLocation();
1081 if (!T.consumeClose())
1082 RLoc = T.getCloseLocation();
1083
1084 return SimpleClauseData(Type, Loc, LOpen, TypeLoc, RLoc);
1085}
1086
Kelvin Lie0502752018-11-21 20:15:57 +00001087Parser::DeclGroupPtrTy Parser::ParseOMPDeclareTargetClauses() {
1088 // OpenMP 4.5 syntax with list of entities.
1089 Sema::NamedDeclSetType SameDirectiveDecls;
Alexey Bataev729e2422019-08-23 16:11:14 +00001090 SmallVector<std::tuple<OMPDeclareTargetDeclAttr::MapTypeTy, SourceLocation,
1091 NamedDecl *>,
1092 4>
1093 DeclareTargetDecls;
1094 OMPDeclareTargetDeclAttr::DevTypeTy DT = OMPDeclareTargetDeclAttr::DT_Any;
1095 SourceLocation DeviceTypeLoc;
Kelvin Lie0502752018-11-21 20:15:57 +00001096 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1097 OMPDeclareTargetDeclAttr::MapTypeTy MT = OMPDeclareTargetDeclAttr::MT_To;
1098 if (Tok.is(tok::identifier)) {
1099 IdentifierInfo *II = Tok.getIdentifierInfo();
1100 StringRef ClauseName = II->getName();
Alexey Bataev729e2422019-08-23 16:11:14 +00001101 bool IsDeviceTypeClause =
1102 getLangOpts().OpenMP >= 50 &&
1103 getOpenMPClauseKind(ClauseName) == OMPC_device_type;
1104 // Parse 'to|link|device_type' clauses.
1105 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName, MT) &&
1106 !IsDeviceTypeClause) {
1107 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
1108 << ClauseName << (getLangOpts().OpenMP >= 50 ? 1 : 0);
Kelvin Lie0502752018-11-21 20:15:57 +00001109 break;
1110 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001111 // Parse 'device_type' clause and go to next clause if any.
1112 if (IsDeviceTypeClause) {
1113 Optional<SimpleClauseData> DevTypeData =
1114 parseOpenMPSimpleClause(*this, OMPC_device_type);
1115 if (DevTypeData.hasValue()) {
1116 if (DeviceTypeLoc.isValid()) {
1117 // We already saw another device_type clause, diagnose it.
1118 Diag(DevTypeData.getValue().Loc,
1119 diag::warn_omp_more_one_device_type_clause);
1120 }
1121 switch(static_cast<OpenMPDeviceType>(DevTypeData.getValue().Type)) {
1122 case OMPC_DEVICE_TYPE_any:
1123 DT = OMPDeclareTargetDeclAttr::DT_Any;
1124 break;
1125 case OMPC_DEVICE_TYPE_host:
1126 DT = OMPDeclareTargetDeclAttr::DT_Host;
1127 break;
1128 case OMPC_DEVICE_TYPE_nohost:
1129 DT = OMPDeclareTargetDeclAttr::DT_NoHost;
1130 break;
1131 case OMPC_DEVICE_TYPE_unknown:
1132 llvm_unreachable("Unexpected device_type");
1133 }
1134 DeviceTypeLoc = DevTypeData.getValue().Loc;
1135 }
1136 continue;
1137 }
Kelvin Lie0502752018-11-21 20:15:57 +00001138 ConsumeToken();
1139 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001140 auto &&Callback = [this, MT, &DeclareTargetDecls, &SameDirectiveDecls](
1141 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
1142 NamedDecl *ND = Actions.lookupOpenMPDeclareTargetName(
1143 getCurScope(), SS, NameInfo, SameDirectiveDecls);
1144 if (ND)
1145 DeclareTargetDecls.emplace_back(MT, NameInfo.getLoc(), ND);
Kelvin Lie0502752018-11-21 20:15:57 +00001146 };
1147 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
1148 /*AllowScopeSpecifier=*/true))
1149 break;
1150
1151 // Consume optional ','.
1152 if (Tok.is(tok::comma))
1153 ConsumeToken();
1154 }
1155 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1156 ConsumeAnyToken();
Alexey Bataev729e2422019-08-23 16:11:14 +00001157 for (auto &MTLocDecl : DeclareTargetDecls) {
1158 OMPDeclareTargetDeclAttr::MapTypeTy MT;
1159 SourceLocation Loc;
1160 NamedDecl *ND;
1161 std::tie(MT, Loc, ND) = MTLocDecl;
1162 // device_type clause is applied only to functions.
1163 Actions.ActOnOpenMPDeclareTargetName(
1164 ND, Loc, MT, isa<VarDecl>(ND) ? OMPDeclareTargetDeclAttr::DT_Any : DT);
1165 }
Kelvin Lie0502752018-11-21 20:15:57 +00001166 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
1167 SameDirectiveDecls.end());
1168 if (Decls.empty())
1169 return DeclGroupPtrTy();
1170 return Actions.BuildDeclaratorGroup(Decls);
1171}
1172
1173void Parser::ParseOMPEndDeclareTargetDirective(OpenMPDirectiveKind DKind,
1174 SourceLocation DTLoc) {
1175 if (DKind != OMPD_end_declare_target) {
1176 Diag(Tok, diag::err_expected_end_declare_target);
1177 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
1178 return;
1179 }
1180 ConsumeAnyToken();
1181 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1182 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1183 << getOpenMPDirectiveName(OMPD_end_declare_target);
1184 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1185 }
1186 // Skip the last annot_pragma_openmp_end.
1187 ConsumeAnyToken();
1188}
1189
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001190/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001191///
1192/// threadprivate-directive:
1193/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001194/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +00001195///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001196/// allocate-directive:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001197/// annot_pragma_openmp 'allocate' simple-variable-list [<clause>]
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001198/// annot_pragma_openmp_end
1199///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001200/// declare-reduction-directive:
1201/// annot_pragma_openmp 'declare' 'reduction' [...]
1202/// annot_pragma_openmp_end
1203///
Michael Kruse251e1482019-02-01 20:25:04 +00001204/// declare-mapper-directive:
1205/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1206/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1207/// annot_pragma_openmp_end
1208///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001209/// declare-simd-directive:
1210/// annot_pragma_openmp 'declare simd' {<clause> [,]}
1211/// annot_pragma_openmp_end
1212/// <function declaration/definition>
1213///
Kelvin Li1408f912018-09-26 04:28:39 +00001214/// requires directive:
1215/// annot_pragma_openmp 'requires' <clause> [[[,] <clause>] ... ]
1216/// annot_pragma_openmp_end
1217///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001218Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
1219 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
1220 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001221 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +00001222 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +00001223
Richard Smithaf3b3252017-05-18 19:21:48 +00001224 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001225 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001226
1227 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001228 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +00001229 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001230 DeclDirectiveListParserHelper Helper(this, DKind);
1231 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1232 /*AllowScopeSpecifier=*/true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001233 // The last seen token is annot_pragma_openmp_end - need to check for
1234 // extra tokens.
1235 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1236 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001237 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001238 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +00001239 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001240 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001241 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001242 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
1243 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +00001244 }
1245 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001246 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001247 case OMPD_allocate: {
1248 ConsumeToken();
1249 DeclDirectiveListParserHelper Helper(this, DKind);
1250 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1251 /*AllowScopeSpecifier=*/true)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001252 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001253 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001254 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1255 OMPC_unknown + 1>
1256 FirstClauses(OMPC_unknown + 1);
1257 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1258 OpenMPClauseKind CKind =
1259 Tok.isAnnotation() ? OMPC_unknown
1260 : getOpenMPClauseKind(PP.getSpelling(Tok));
1261 Actions.StartOpenMPClause(CKind);
1262 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1263 !FirstClauses[CKind].getInt());
1264 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1265 StopBeforeMatch);
1266 FirstClauses[CKind].setInt(true);
1267 if (Clause != nullptr)
1268 Clauses.push_back(Clause);
1269 if (Tok.is(tok::annot_pragma_openmp_end)) {
1270 Actions.EndOpenMPClause();
1271 break;
1272 }
1273 // Skip ',' if any.
1274 if (Tok.is(tok::comma))
1275 ConsumeToken();
1276 Actions.EndOpenMPClause();
1277 }
1278 // The last seen token is annot_pragma_openmp_end - need to check for
1279 // extra tokens.
1280 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1281 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1282 << getOpenMPDirectiveName(DKind);
1283 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1284 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001285 }
1286 // Skip the last annot_pragma_openmp_end.
1287 ConsumeAnnotationToken();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001288 return Actions.ActOnOpenMPAllocateDirective(Loc, Helper.getIdentifiers(),
1289 Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001290 }
1291 break;
1292 }
Kelvin Li1408f912018-09-26 04:28:39 +00001293 case OMPD_requires: {
1294 SourceLocation StartLoc = ConsumeToken();
1295 SmallVector<OMPClause *, 5> Clauses;
1296 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
1297 FirstClauses(OMPC_unknown + 1);
1298 if (Tok.is(tok::annot_pragma_openmp_end)) {
Ilya Biryukovff2a9972019-02-26 11:01:50 +00001299 Diag(Tok, diag::err_omp_expected_clause)
Kelvin Li1408f912018-09-26 04:28:39 +00001300 << getOpenMPDirectiveName(OMPD_requires);
1301 break;
1302 }
1303 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1304 OpenMPClauseKind CKind = Tok.isAnnotation()
1305 ? OMPC_unknown
1306 : getOpenMPClauseKind(PP.getSpelling(Tok));
1307 Actions.StartOpenMPClause(CKind);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001308 OMPClause *Clause = ParseOpenMPClause(OMPD_requires, CKind,
1309 !FirstClauses[CKind].getInt());
1310 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1311 StopBeforeMatch);
Kelvin Li1408f912018-09-26 04:28:39 +00001312 FirstClauses[CKind].setInt(true);
1313 if (Clause != nullptr)
1314 Clauses.push_back(Clause);
1315 if (Tok.is(tok::annot_pragma_openmp_end)) {
1316 Actions.EndOpenMPClause();
1317 break;
1318 }
1319 // Skip ',' if any.
1320 if (Tok.is(tok::comma))
1321 ConsumeToken();
1322 Actions.EndOpenMPClause();
1323 }
1324 // Consume final annot_pragma_openmp_end
1325 if (Clauses.size() == 0) {
1326 Diag(Tok, diag::err_omp_expected_clause)
1327 << getOpenMPDirectiveName(OMPD_requires);
1328 ConsumeAnnotationToken();
1329 return nullptr;
1330 }
1331 ConsumeAnnotationToken();
1332 return Actions.ActOnOpenMPRequiresDirective(StartLoc, Clauses);
1333 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001334 case OMPD_declare_reduction:
1335 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001336 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001337 // The last seen token is annot_pragma_openmp_end - need to check for
1338 // extra tokens.
1339 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1340 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1341 << getOpenMPDirectiveName(OMPD_declare_reduction);
1342 while (Tok.isNot(tok::annot_pragma_openmp_end))
1343 ConsumeAnyToken();
1344 }
1345 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001346 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001347 return Res;
1348 }
1349 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001350 case OMPD_declare_mapper: {
1351 ConsumeToken();
1352 if (DeclGroupPtrTy Res = ParseOpenMPDeclareMapperDirective(AS)) {
1353 // Skip the last annot_pragma_openmp_end.
1354 ConsumeAnnotationToken();
1355 return Res;
1356 }
1357 break;
1358 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001359 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001360 case OMPD_declare_simd: {
1361 // The syntax is:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001362 // { #pragma omp declare {simd|variant} }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001363 // <function-declaration-or-definition>
1364 //
Alexey Bataev2af33e32016-04-07 12:45:37 +00001365 CachedTokens Toks;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001366 Toks.push_back(Tok);
1367 ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001368 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
1369 Toks.push_back(Tok);
1370 ConsumeAnyToken();
1371 }
1372 Toks.push_back(Tok);
1373 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +00001374
1375 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +00001376 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001377 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +00001378 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001379 // Here we expect to see some function declaration.
1380 if (AS == AS_none) {
1381 assert(TagType == DeclSpec::TST_unspecified);
1382 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001383 ParsingDeclSpec PDS(*this);
1384 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1385 } else {
1386 Ptr =
1387 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1388 }
1389 }
1390 if (!Ptr) {
Alexey Bataevd158cf62019-09-13 20:18:17 +00001391 Diag(Loc, diag::err_omp_decl_in_declare_simd_variant)
1392 << (DKind == OMPD_declare_simd ? 0 : 1);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001393 return DeclGroupPtrTy();
1394 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001395 if (DKind == OMPD_declare_simd)
1396 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
1397 assert(DKind == OMPD_declare_variant &&
1398 "Expected declare variant directive only");
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001399 ParseOMPDeclareVariantClauses(Ptr, Toks, Loc);
1400 return Ptr;
Alexey Bataev587e1de2016-03-30 10:43:55 +00001401 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001402 case OMPD_declare_target: {
1403 SourceLocation DTLoc = ConsumeAnyToken();
1404 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Kelvin Lie0502752018-11-21 20:15:57 +00001405 return ParseOMPDeclareTargetClauses();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001406 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001407
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001408 // Skip the last annot_pragma_openmp_end.
1409 ConsumeAnyToken();
1410
1411 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
1412 return DeclGroupPtrTy();
1413
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001414 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +00001415 DKind = parseOpenMPDirectiveKind(*this);
Kelvin Libc38e632018-09-10 02:07:09 +00001416 while (DKind != OMPD_end_declare_target && Tok.isNot(tok::eof) &&
1417 Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +00001418 DeclGroupPtrTy Ptr;
1419 // Here we expect to see some function declaration.
1420 if (AS == AS_none) {
1421 assert(TagType == DeclSpec::TST_unspecified);
1422 MaybeParseCXX11Attributes(Attrs);
1423 ParsingDeclSpec PDS(*this);
1424 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1425 } else {
1426 Ptr =
1427 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1428 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001429 if (Ptr) {
1430 DeclGroupRef Ref = Ptr.get();
1431 Decls.append(Ref.begin(), Ref.end());
1432 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001433 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
1434 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +00001435 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001436 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001437 if (DKind != OMPD_end_declare_target)
1438 TPA.Revert();
1439 else
1440 TPA.Commit();
1441 }
1442 }
1443
Kelvin Lie0502752018-11-21 20:15:57 +00001444 ParseOMPEndDeclareTargetDirective(DKind, DTLoc);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001445 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +00001446 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001447 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001448 case OMPD_unknown:
1449 Diag(Tok, diag::err_omp_unknown_directive);
1450 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001451 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001452 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001453 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +00001454 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001455 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001456 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001457 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +00001458 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001459 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001460 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001461 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001462 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001463 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +00001464 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001465 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001466 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001467 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001468 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001469 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +00001470 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001471 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001472 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001473 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001474 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +00001475 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001476 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001477 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001478 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001479 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001480 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001481 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001482 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001483 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001484 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001485 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +00001486 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001487 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001488 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001489 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001490 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +00001491 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +00001492 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001493 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +00001494 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +00001495 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +00001496 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +00001497 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +00001498 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +00001499 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001500 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +00001501 break;
1502 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001503 while (Tok.isNot(tok::annot_pragma_openmp_end))
1504 ConsumeAnyToken();
1505 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +00001506 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001507}
1508
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001509/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001510///
1511/// threadprivate-directive:
1512/// annot_pragma_openmp 'threadprivate' simple-variable-list
1513/// annot_pragma_openmp_end
1514///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001515/// allocate-directive:
1516/// annot_pragma_openmp 'allocate' simple-variable-list
1517/// annot_pragma_openmp_end
1518///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001519/// declare-reduction-directive:
1520/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
1521/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
1522/// ('omp_priv' '=' <expression>|<function_call>) ')']
1523/// annot_pragma_openmp_end
1524///
Michael Kruse251e1482019-02-01 20:25:04 +00001525/// declare-mapper-directive:
1526/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1527/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1528/// annot_pragma_openmp_end
1529///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001530/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001531/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001532/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
1533/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001534/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +00001535/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001536/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001537/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +00001538/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +00001539/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +00001540/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +00001541/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +00001542/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +00001543/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +00001544/// 'teams distribute parallel for' | 'target teams' |
1545/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +00001546/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +00001547/// 'target teams distribute parallel for simd' |
1548/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +00001549/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001550///
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001551StmtResult
1552Parser::ParseOpenMPDeclarativeOrExecutableDirective(ParsedStmtContext StmtCtx) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001553 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +00001554 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001555 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001556 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +00001557 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +00001558 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
1559 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +00001560 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +00001561 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001562 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001563 // Name of critical directive.
1564 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001565 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +00001566 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +00001567 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001568
1569 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001570 case OMPD_threadprivate: {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001571 // FIXME: Should this be permitted in C++?
1572 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1573 ParsedStmtContext()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001574 Diag(Tok, diag::err_omp_immediate_directive)
1575 << getOpenMPDirectiveName(DKind) << 0;
1576 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001577 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001578 DeclDirectiveListParserHelper Helper(this, DKind);
1579 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1580 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001581 // The last seen token is annot_pragma_openmp_end - need to check for
1582 // extra tokens.
1583 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1584 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001585 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001586 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001587 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001588 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
1589 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001590 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1591 }
Alp Tokerd751fa72013-12-18 19:10:49 +00001592 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001593 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001594 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001595 case OMPD_allocate: {
1596 // FIXME: Should this be permitted in C++?
1597 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1598 ParsedStmtContext()) {
1599 Diag(Tok, diag::err_omp_immediate_directive)
1600 << getOpenMPDirectiveName(DKind) << 0;
1601 }
1602 ConsumeToken();
1603 DeclDirectiveListParserHelper Helper(this, DKind);
1604 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1605 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001606 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001607 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001608 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1609 OMPC_unknown + 1>
1610 FirstClauses(OMPC_unknown + 1);
1611 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1612 OpenMPClauseKind CKind =
1613 Tok.isAnnotation() ? OMPC_unknown
1614 : getOpenMPClauseKind(PP.getSpelling(Tok));
1615 Actions.StartOpenMPClause(CKind);
1616 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1617 !FirstClauses[CKind].getInt());
1618 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1619 StopBeforeMatch);
1620 FirstClauses[CKind].setInt(true);
1621 if (Clause != nullptr)
1622 Clauses.push_back(Clause);
1623 if (Tok.is(tok::annot_pragma_openmp_end)) {
1624 Actions.EndOpenMPClause();
1625 break;
1626 }
1627 // Skip ',' if any.
1628 if (Tok.is(tok::comma))
1629 ConsumeToken();
1630 Actions.EndOpenMPClause();
1631 }
1632 // The last seen token is annot_pragma_openmp_end - need to check for
1633 // extra tokens.
1634 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1635 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1636 << getOpenMPDirectiveName(DKind);
1637 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1638 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001639 }
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001640 DeclGroupPtrTy Res = Actions.ActOnOpenMPAllocateDirective(
1641 Loc, Helper.getIdentifiers(), Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001642 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1643 }
1644 SkipUntil(tok::annot_pragma_openmp_end);
1645 break;
1646 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001647 case OMPD_declare_reduction:
1648 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001649 if (DeclGroupPtrTy Res =
1650 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001651 // The last seen token is annot_pragma_openmp_end - need to check for
1652 // extra tokens.
1653 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1654 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1655 << getOpenMPDirectiveName(OMPD_declare_reduction);
1656 while (Tok.isNot(tok::annot_pragma_openmp_end))
1657 ConsumeAnyToken();
1658 }
1659 ConsumeAnyToken();
1660 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +00001661 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001662 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +00001663 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001664 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001665 case OMPD_declare_mapper: {
1666 ConsumeToken();
1667 if (DeclGroupPtrTy Res =
1668 ParseOpenMPDeclareMapperDirective(/*AS=*/AS_none)) {
1669 // Skip the last annot_pragma_openmp_end.
1670 ConsumeAnnotationToken();
1671 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1672 } else {
1673 SkipUntil(tok::annot_pragma_openmp_end);
1674 }
1675 break;
1676 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001677 case OMPD_flush:
1678 if (PP.LookAhead(0).is(tok::l_paren)) {
1679 FlushHasClause = true;
1680 // Push copy of the current token back to stream to properly parse
1681 // pseudo-clause OMPFlushClause.
Ilya Biryukov929af672019-05-17 09:32:05 +00001682 PP.EnterToken(Tok, /*IsReinject*/ true);
Alexey Bataev6125da92014-07-21 11:26:11 +00001683 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001684 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +00001685 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001686 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001687 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001688 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001689 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001690 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001691 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +00001692 case OMPD_target_update:
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001693 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1694 ParsedStmtContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00001695 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +00001696 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +00001697 }
1698 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001699 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001700 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001701 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001702 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001703 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001704 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001705 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001706 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001707 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001708 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001709 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001710 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001711 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001712 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001713 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001714 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001715 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001716 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001717 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001718 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001719 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001720 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001721 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001722 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001723 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001724 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001725 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001726 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001727 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001728 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001729 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001730 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001731 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001732 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001733 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001734 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001735 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001736 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001737 case OMPD_target_teams_distribute_parallel_for_simd:
1738 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001739 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001740 // Parse directive name of the 'critical' directive if any.
1741 if (DKind == OMPD_critical) {
1742 BalancedDelimiterTracker T(*this, tok::l_paren,
1743 tok::annot_pragma_openmp_end);
1744 if (!T.consumeOpen()) {
1745 if (Tok.isAnyIdentifier()) {
1746 DirName =
1747 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1748 ConsumeAnyToken();
1749 } else {
1750 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1751 }
1752 T.consumeClose();
1753 }
Alexey Bataev80909872015-07-02 11:25:17 +00001754 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001755 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001756 if (Tok.isNot(tok::annot_pragma_openmp_end))
1757 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001758 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001759
Alexey Bataevf29276e2014-06-18 04:14:57 +00001760 if (isOpenMPLoopDirective(DKind))
1761 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1762 if (isOpenMPSimdDirective(DKind))
1763 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1764 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001765 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001766
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001767 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001768 OpenMPClauseKind CKind =
1769 Tok.isAnnotation()
1770 ? OMPC_unknown
1771 : FlushHasClause ? OMPC_flush
1772 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001773 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001774 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001775 OMPClause *Clause =
1776 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001777 FirstClauses[CKind].setInt(true);
1778 if (Clause) {
1779 FirstClauses[CKind].setPointer(Clause);
1780 Clauses.push_back(Clause);
1781 }
1782
1783 // Skip ',' if any.
1784 if (Tok.is(tok::comma))
1785 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001786 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001787 }
1788 // End location of the directive.
1789 EndLoc = Tok.getLocation();
1790 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001791 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001792
Alexey Bataeveb482352015-12-18 05:05:56 +00001793 // OpenMP [2.13.8, ordered Construct, Syntax]
1794 // If the depend clause is specified, the ordered construct is a stand-alone
1795 // directive.
1796 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001797 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1798 ParsedStmtContext()) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001799 Diag(Loc, diag::err_omp_immediate_directive)
1800 << getOpenMPDirectiveName(DKind) << 1
1801 << getOpenMPClauseName(OMPC_depend);
1802 }
1803 HasAssociatedStatement = false;
1804 }
1805
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001806 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001807 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001808 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001809 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001810 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1811 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1812 // should have at least one compound statement scope within it.
1813 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001814 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001815 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1816 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001817 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001818 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1819 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1820 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001821 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001822 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001823 Directive = Actions.ActOnOpenMPExecutableDirective(
1824 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1825 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001826
1827 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001828 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001829 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001830 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001831 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001832 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001833 case OMPD_declare_target:
1834 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00001835 case OMPD_requires:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001836 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001837 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001838 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001839 SkipUntil(tok::annot_pragma_openmp_end);
1840 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001841 case OMPD_unknown:
1842 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001843 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001844 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001845 }
1846 return Directive;
1847}
1848
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001849// Parses simple list:
1850// simple-variable-list:
1851// '(' id-expression {, id-expression} ')'
1852//
1853bool Parser::ParseOpenMPSimpleVarList(
1854 OpenMPDirectiveKind Kind,
1855 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1856 Callback,
1857 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001858 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001859 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001860 if (T.expectAndConsume(diag::err_expected_lparen_after,
1861 getOpenMPDirectiveName(Kind)))
1862 return true;
1863 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001864 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001865
1866 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001867 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001868 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001869 UnqualifiedId Name;
1870 // Read var name.
1871 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001872 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001873
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001874 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001875 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001876 IsCorrect = false;
1877 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001878 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001879 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00001880 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001881 IsCorrect = false;
1882 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001883 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001884 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1885 Tok.isNot(tok::annot_pragma_openmp_end)) {
1886 IsCorrect = false;
1887 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001888 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001889 Diag(PrevTok.getLocation(), diag::err_expected)
1890 << tok::identifier
1891 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001892 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001893 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001894 }
1895 // Consume ','.
1896 if (Tok.is(tok::comma)) {
1897 ConsumeToken();
1898 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001899 }
1900
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001901 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001902 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001903 IsCorrect = false;
1904 }
1905
1906 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001907 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001908
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001909 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001910}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001911
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001912/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001913///
1914/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001915/// if-clause | final-clause | num_threads-clause | safelen-clause |
1916/// default-clause | private-clause | firstprivate-clause | shared-clause
1917/// | linear-clause | aligned-clause | collapse-clause |
1918/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001919/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001920/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001921/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001922/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001923/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001924/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001925/// from-clause | is_device_ptr-clause | task_reduction-clause |
Alexey Bataeve04483e2019-03-27 14:14:31 +00001926/// in_reduction-clause | allocator-clause | allocate-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001927///
1928OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1929 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001930 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001931 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001932 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001933 // Check if clause is allowed for the given directive.
1934 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001935 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1936 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001937 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001938 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001939 }
1940
1941 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001942 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001943 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001944 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001945 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001946 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001947 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001948 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001949 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001950 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001951 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001952 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001953 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001954 case OMPC_hint:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001955 case OMPC_allocator:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001956 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001957 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001958 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001959 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001960 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001961 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001962 // OpenMP [2.9.1, target data construct, Restrictions]
1963 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001964 // OpenMP [2.11.1, task Construct, Restrictions]
1965 // At most one if clause can appear on the directive.
1966 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001967 // OpenMP [teams Construct, Restrictions]
1968 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001969 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001970 // OpenMP [2.9.1, task Construct, Restrictions]
1971 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001972 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1973 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001974 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1975 // At most one num_tasks clause can appear on the directive.
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001976 // OpenMP [2.11.3, allocate Directive, Restrictions]
1977 // At most one allocator clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001978 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001979 Diag(Tok, diag::err_omp_more_one_clause)
1980 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001981 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001982 }
1983
Alexey Bataev10e775f2015-07-30 11:36:16 +00001984 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001985 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001986 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001987 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001988 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001989 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001990 case OMPC_proc_bind:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001991 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001992 // OpenMP [2.14.3.1, Restrictions]
1993 // Only a single default clause may be specified on a parallel, task or
1994 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001995 // OpenMP [2.5, parallel Construct, Restrictions]
1996 // At most one proc_bind clause can appear on the directive.
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001997 // OpenMP [5.0, Requires directive, Restrictions]
1998 // At most one atomic_default_mem_order clause can appear
1999 // on the directive
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002000 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002001 Diag(Tok, diag::err_omp_more_one_clause)
2002 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002003 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002004 }
2005
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002006 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002007 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002008 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00002009 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002010 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002011 // OpenMP [2.7.1, Restrictions, p. 3]
2012 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002013 // OpenMP [2.10.4, Restrictions, p. 106]
2014 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00002015 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002016 Diag(Tok, diag::err_omp_more_one_clause)
2017 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002018 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002019 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00002020 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002021
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002022 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002023 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002024 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002025 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002026 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002027 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002028 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002029 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00002030 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00002031 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002032 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00002033 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002034 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00002035 case OMPC_nogroup:
Kelvin Li1408f912018-09-26 04:28:39 +00002036 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00002037 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00002038 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00002039 case OMPC_dynamic_allocators:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002040 // OpenMP [2.7.1, Restrictions, p. 9]
2041 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00002042 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
2043 // Only one nowait clause can appear on a for directive.
Kelvin Li1408f912018-09-26 04:28:39 +00002044 // OpenMP [5.0, Requires directive, Restrictions]
2045 // Each of the requires clauses can appear at most once on the directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002046 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002047 Diag(Tok, diag::err_omp_more_one_clause)
2048 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002049 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002050 }
2051
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002052 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002053 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002054 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002055 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002056 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002057 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002058 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00002059 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00002060 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002061 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002062 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002063 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002064 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002065 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002066 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00002067 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00002068 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00002069 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00002070 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00002071 case OMPC_is_device_ptr:
Alexey Bataeve04483e2019-03-27 14:14:31 +00002072 case OMPC_allocate:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002073 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002074 break;
Alexey Bataev729e2422019-08-23 16:11:14 +00002075 case OMPC_device_type:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002076 case OMPC_unknown:
2077 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00002078 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00002079 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002080 break;
2081 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002082 case OMPC_uniform:
Alexey Bataevdba792c2019-09-23 18:13:31 +00002083 case OMPC_match:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002084 if (!WrongDirective)
2085 Diag(Tok, diag::err_omp_unexpected_clause)
2086 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00002087 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002088 break;
2089 }
Craig Topper161e4db2014-05-21 06:02:52 +00002090 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002091}
2092
Alexey Bataev2af33e32016-04-07 12:45:37 +00002093/// Parses simple expression in parens for single-expression clauses of OpenMP
2094/// constructs.
2095/// \param RLoc Returned location of right paren.
2096ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
Alexey Bataevd158cf62019-09-13 20:18:17 +00002097 SourceLocation &RLoc,
2098 bool IsAddressOfOperand) {
Alexey Bataev2af33e32016-04-07 12:45:37 +00002099 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2100 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
2101 return ExprError();
2102
2103 SourceLocation ELoc = Tok.getLocation();
2104 ExprResult LHS(ParseCastExpression(
Alexey Bataevd158cf62019-09-13 20:18:17 +00002105 /*isUnaryExpression=*/false, IsAddressOfOperand, NotTypeCast));
Alexey Bataev2af33e32016-04-07 12:45:37 +00002106 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002107 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev2af33e32016-04-07 12:45:37 +00002108
2109 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002110 RLoc = Tok.getLocation();
2111 if (!T.consumeClose())
2112 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00002113
Alexey Bataev2af33e32016-04-07 12:45:37 +00002114 return Val;
2115}
2116
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002117/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00002118/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00002119/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002120///
Alexey Bataev3778b602014-07-17 07:32:53 +00002121/// final-clause:
2122/// 'final' '(' expression ')'
2123///
Alexey Bataev62c87d22014-03-21 04:51:18 +00002124/// num_threads-clause:
2125/// 'num_threads' '(' expression ')'
2126///
2127/// safelen-clause:
2128/// 'safelen' '(' expression ')'
2129///
Alexey Bataev66b15b52015-08-21 11:14:16 +00002130/// simdlen-clause:
2131/// 'simdlen' '(' expression ')'
2132///
Alexander Musman8bd31e62014-05-27 15:12:19 +00002133/// collapse-clause:
2134/// 'collapse' '(' expression ')'
2135///
Alexey Bataeva0569352015-12-01 10:17:31 +00002136/// priority-clause:
2137/// 'priority' '(' expression ')'
2138///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002139/// grainsize-clause:
2140/// 'grainsize' '(' expression ')'
2141///
Alexey Bataev382967a2015-12-08 12:06:20 +00002142/// num_tasks-clause:
2143/// 'num_tasks' '(' expression ')'
2144///
Alexey Bataev28c75412015-12-15 08:19:24 +00002145/// hint-clause:
2146/// 'hint' '(' expression ')'
2147///
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002148/// allocator-clause:
2149/// 'allocator' '(' expression ')'
2150///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002151OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
2152 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002153 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00002154 SourceLocation LLoc = Tok.getLocation();
2155 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002156
Alexey Bataev2af33e32016-04-07 12:45:37 +00002157 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002158
2159 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00002160 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002161
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002162 if (ParseOnly)
2163 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00002164 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002165}
2166
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002167/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002168///
2169/// default-clause:
2170/// 'default' '(' 'none' | 'shared' ')
2171///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002172/// proc_bind-clause:
2173/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
2174///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002175OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
2176 bool ParseOnly) {
Alexey Bataev729e2422019-08-23 16:11:14 +00002177 llvm::Optional<SimpleClauseData> Val = parseOpenMPSimpleClause(*this, Kind);
2178 if (!Val || ParseOnly)
Craig Topper161e4db2014-05-21 06:02:52 +00002179 return nullptr;
Alexey Bataev729e2422019-08-23 16:11:14 +00002180 return Actions.ActOnOpenMPSimpleClause(
2181 Kind, Val.getValue().Type, Val.getValue().TypeLoc, Val.getValue().LOpen,
2182 Val.getValue().Loc, Val.getValue().RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002183}
2184
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002185/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002186///
2187/// ordered-clause:
2188/// 'ordered'
2189///
Alexey Bataev236070f2014-06-20 11:19:47 +00002190/// nowait-clause:
2191/// 'nowait'
2192///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002193/// untied-clause:
2194/// 'untied'
2195///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002196/// mergeable-clause:
2197/// 'mergeable'
2198///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002199/// read-clause:
2200/// 'read'
2201///
Alexey Bataev346265e2015-09-25 10:37:12 +00002202/// threads-clause:
2203/// 'threads'
2204///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002205/// simd-clause:
2206/// 'simd'
2207///
Alexey Bataevb825de12015-12-07 10:51:44 +00002208/// nogroup-clause:
2209/// 'nogroup'
2210///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002211OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002212 SourceLocation Loc = Tok.getLocation();
2213 ConsumeAnyToken();
2214
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002215 if (ParseOnly)
2216 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002217 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
2218}
2219
2220
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002221/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00002222/// argument like 'schedule' or 'dist_schedule'.
2223///
2224/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00002225/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
2226/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00002227///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002228/// if-clause:
2229/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
2230///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002231/// defaultmap:
2232/// 'defaultmap' '(' modifier ':' kind ')'
2233///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002234OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
2235 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00002236 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002237 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002238 // Parse '('.
2239 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2240 if (T.expectAndConsume(diag::err_expected_lparen_after,
2241 getOpenMPClauseName(Kind)))
2242 return nullptr;
2243
2244 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002245 SmallVector<unsigned, 4> Arg;
2246 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002247 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00002248 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
2249 Arg.resize(NumberOfElements);
2250 KLoc.resize(NumberOfElements);
2251 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
2252 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
2253 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00002254 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002255 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00002256 if (KindModifier > OMPC_SCHEDULE_unknown) {
2257 // Parse 'modifier'
2258 Arg[Modifier1] = KindModifier;
2259 KLoc[Modifier1] = Tok.getLocation();
2260 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2261 Tok.isNot(tok::annot_pragma_openmp_end))
2262 ConsumeAnyToken();
2263 if (Tok.is(tok::comma)) {
2264 // Parse ',' 'modifier'
2265 ConsumeAnyToken();
2266 KindModifier = getOpenMPSimpleClauseType(
2267 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2268 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
2269 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00002270 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002271 KLoc[Modifier2] = Tok.getLocation();
2272 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2273 Tok.isNot(tok::annot_pragma_openmp_end))
2274 ConsumeAnyToken();
2275 }
2276 // Parse ':'
2277 if (Tok.is(tok::colon))
2278 ConsumeAnyToken();
2279 else
2280 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
2281 KindModifier = getOpenMPSimpleClauseType(
2282 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2283 }
2284 Arg[ScheduleKind] = KindModifier;
2285 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002286 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2287 Tok.isNot(tok::annot_pragma_openmp_end))
2288 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00002289 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
2290 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
2291 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002292 Tok.is(tok::comma))
2293 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00002294 } else if (Kind == OMPC_dist_schedule) {
2295 Arg.push_back(getOpenMPSimpleClauseType(
2296 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2297 KLoc.push_back(Tok.getLocation());
2298 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2299 Tok.isNot(tok::annot_pragma_openmp_end))
2300 ConsumeAnyToken();
2301 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
2302 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002303 } else if (Kind == OMPC_defaultmap) {
2304 // Get a defaultmap modifier
2305 Arg.push_back(getOpenMPSimpleClauseType(
2306 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2307 KLoc.push_back(Tok.getLocation());
2308 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2309 Tok.isNot(tok::annot_pragma_openmp_end))
2310 ConsumeAnyToken();
2311 // Parse ':'
2312 if (Tok.is(tok::colon))
2313 ConsumeAnyToken();
2314 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
2315 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
2316 // Get a defaultmap kind
2317 Arg.push_back(getOpenMPSimpleClauseType(
2318 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2319 KLoc.push_back(Tok.getLocation());
2320 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2321 Tok.isNot(tok::annot_pragma_openmp_end))
2322 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002323 } else {
2324 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00002325 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002326 TentativeParsingAction TPA(*this);
Alexey Bataev61908f652018-04-23 19:53:05 +00002327 Arg.push_back(parseOpenMPDirectiveKind(*this));
Alexey Bataev6402bca2015-12-28 07:25:51 +00002328 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002329 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002330 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
2331 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002332 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002333 } else {
2334 TPA.Revert();
2335 Arg.back() = OMPD_unknown;
2336 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002337 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002338 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00002339 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002340 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00002341
Carlo Bertollib4adf552016-01-15 18:50:31 +00002342 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
2343 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
2344 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002345 if (NeedAnExpression) {
2346 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002347 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
2348 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002349 Val =
2350 Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002351 }
2352
2353 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002354 SourceLocation RLoc = Tok.getLocation();
2355 if (!T.consumeClose())
2356 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002357
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002358 if (NeedAnExpression && Val.isInvalid())
2359 return nullptr;
2360
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002361 if (ParseOnly)
2362 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002363 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002364 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002365}
2366
Alexey Bataevc5e02582014-06-16 07:08:35 +00002367static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
2368 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002369 if (ReductionIdScopeSpec.isEmpty()) {
2370 auto OOK = OO_None;
2371 switch (P.getCurToken().getKind()) {
2372 case tok::plus:
2373 OOK = OO_Plus;
2374 break;
2375 case tok::minus:
2376 OOK = OO_Minus;
2377 break;
2378 case tok::star:
2379 OOK = OO_Star;
2380 break;
2381 case tok::amp:
2382 OOK = OO_Amp;
2383 break;
2384 case tok::pipe:
2385 OOK = OO_Pipe;
2386 break;
2387 case tok::caret:
2388 OOK = OO_Caret;
2389 break;
2390 case tok::ampamp:
2391 OOK = OO_AmpAmp;
2392 break;
2393 case tok::pipepipe:
2394 OOK = OO_PipePipe;
2395 break;
2396 default:
2397 break;
2398 }
2399 if (OOK != OO_None) {
2400 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00002401 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00002402 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
2403 return false;
2404 }
2405 }
2406 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
2407 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00002408 /*AllowConstructorName*/ false,
2409 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00002410 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002411}
2412
Kelvin Lief579432018-12-18 22:18:41 +00002413/// Checks if the token is a valid map-type-modifier.
2414static OpenMPMapModifierKind isMapModifier(Parser &P) {
2415 Token Tok = P.getCurToken();
2416 if (!Tok.is(tok::identifier))
2417 return OMPC_MAP_MODIFIER_unknown;
2418
2419 Preprocessor &PP = P.getPreprocessor();
2420 OpenMPMapModifierKind TypeModifier = static_cast<OpenMPMapModifierKind>(
2421 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2422 return TypeModifier;
2423}
2424
Michael Kruse01f670d2019-02-22 22:29:42 +00002425/// Parse the mapper modifier in map, to, and from clauses.
2426bool Parser::parseMapperModifier(OpenMPVarListDataTy &Data) {
2427 // Parse '('.
2428 BalancedDelimiterTracker T(*this, tok::l_paren, tok::colon);
2429 if (T.expectAndConsume(diag::err_expected_lparen_after, "mapper")) {
2430 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2431 StopBeforeMatch);
2432 return true;
2433 }
2434 // Parse mapper-identifier
2435 if (getLangOpts().CPlusPlus)
2436 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
2437 /*ObjectType=*/nullptr,
2438 /*EnteringContext=*/false);
2439 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
2440 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
2441 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2442 StopBeforeMatch);
2443 return true;
2444 }
2445 auto &DeclNames = Actions.getASTContext().DeclarationNames;
2446 Data.ReductionOrMapperId = DeclarationNameInfo(
2447 DeclNames.getIdentifier(Tok.getIdentifierInfo()), Tok.getLocation());
2448 ConsumeToken();
2449 // Parse ')'.
2450 return T.consumeClose();
2451}
2452
Kelvin Lief579432018-12-18 22:18:41 +00002453/// Parse map-type-modifiers in map clause.
2454/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002455/// where, map-type-modifier ::= always | close | mapper(mapper-identifier)
2456bool Parser::parseMapTypeModifiers(OpenMPVarListDataTy &Data) {
2457 while (getCurToken().isNot(tok::colon)) {
2458 OpenMPMapModifierKind TypeModifier = isMapModifier(*this);
Kelvin Lief579432018-12-18 22:18:41 +00002459 if (TypeModifier == OMPC_MAP_MODIFIER_always ||
2460 TypeModifier == OMPC_MAP_MODIFIER_close) {
2461 Data.MapTypeModifiers.push_back(TypeModifier);
2462 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
Michael Kruse4304e9d2019-02-19 16:38:20 +00002463 ConsumeToken();
2464 } else if (TypeModifier == OMPC_MAP_MODIFIER_mapper) {
2465 Data.MapTypeModifiers.push_back(TypeModifier);
2466 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
2467 ConsumeToken();
Michael Kruse01f670d2019-02-22 22:29:42 +00002468 if (parseMapperModifier(Data))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002469 return true;
Kelvin Lief579432018-12-18 22:18:41 +00002470 } else {
2471 // For the case of unknown map-type-modifier or a map-type.
2472 // Map-type is followed by a colon; the function returns when it
2473 // encounters a token followed by a colon.
2474 if (Tok.is(tok::comma)) {
Michael Kruse4304e9d2019-02-19 16:38:20 +00002475 Diag(Tok, diag::err_omp_map_type_modifier_missing);
2476 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002477 continue;
2478 }
2479 // Potential map-type token as it is followed by a colon.
2480 if (PP.LookAhead(0).is(tok::colon))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002481 return false;
2482 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
2483 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002484 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002485 if (getCurToken().is(tok::comma))
2486 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002487 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002488 return false;
Kelvin Lief579432018-12-18 22:18:41 +00002489}
2490
2491/// Checks if the token is a valid map-type.
2492static OpenMPMapClauseKind isMapType(Parser &P) {
2493 Token Tok = P.getCurToken();
2494 // The map-type token can be either an identifier or the C++ delete keyword.
2495 if (!Tok.isOneOf(tok::identifier, tok::kw_delete))
2496 return OMPC_MAP_unknown;
2497 Preprocessor &PP = P.getPreprocessor();
2498 OpenMPMapClauseKind MapType = static_cast<OpenMPMapClauseKind>(
2499 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2500 return MapType;
2501}
2502
2503/// Parse map-type in map clause.
2504/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002505/// where, map-type ::= to | from | tofrom | alloc | release | delete
Kelvin Lief579432018-12-18 22:18:41 +00002506static void parseMapType(Parser &P, Parser::OpenMPVarListDataTy &Data) {
2507 Token Tok = P.getCurToken();
2508 if (Tok.is(tok::colon)) {
2509 P.Diag(Tok, diag::err_omp_map_type_missing);
2510 return;
2511 }
2512 Data.MapType = isMapType(P);
2513 if (Data.MapType == OMPC_MAP_unknown)
2514 P.Diag(Tok, diag::err_omp_unknown_map_type);
2515 P.ConsumeToken();
2516}
2517
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002518/// Parses clauses with list.
2519bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
2520 OpenMPClauseKind Kind,
2521 SmallVectorImpl<Expr *> &Vars,
2522 OpenMPVarListDataTy &Data) {
2523 UnqualifiedId UnqualifiedReductionId;
2524 bool InvalidReductionId = false;
Michael Kruse01f670d2019-02-22 22:29:42 +00002525 bool IsInvalidMapperModifier = false;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002526
2527 // Parse '('.
2528 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2529 if (T.expectAndConsume(diag::err_expected_lparen_after,
2530 getOpenMPClauseName(Kind)))
2531 return true;
2532
2533 bool NeedRParenForLinear = false;
2534 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
2535 tok::annot_pragma_openmp_end);
2536 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00002537 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
2538 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002539 ColonProtectionRAIIObject ColonRAII(*this);
2540 if (getLangOpts().CPlusPlus)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002541 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002542 /*ObjectType=*/nullptr,
2543 /*EnteringContext=*/false);
Michael Kruse4304e9d2019-02-19 16:38:20 +00002544 InvalidReductionId = ParseReductionId(
2545 *this, Data.ReductionOrMapperIdScopeSpec, UnqualifiedReductionId);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002546 if (InvalidReductionId) {
2547 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2548 StopBeforeMatch);
2549 }
2550 if (Tok.is(tok::colon))
2551 Data.ColonLoc = ConsumeToken();
2552 else
2553 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
2554 if (!InvalidReductionId)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002555 Data.ReductionOrMapperId =
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002556 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
2557 } else if (Kind == OMPC_depend) {
2558 // Handle dependency type for depend clause.
2559 ColonProtectionRAIIObject ColonRAII(*this);
2560 Data.DepKind =
2561 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
2562 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
2563 Data.DepLinMapLoc = Tok.getLocation();
2564
2565 if (Data.DepKind == OMPC_DEPEND_unknown) {
2566 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2567 StopBeforeMatch);
2568 } else {
2569 ConsumeToken();
2570 // Special processing for depend(source) clause.
2571 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
2572 // Parse ')'.
2573 T.consumeClose();
2574 return false;
2575 }
2576 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002577 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002578 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00002579 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002580 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
2581 : diag::warn_pragma_expected_colon)
2582 << "dependency type";
2583 }
2584 } else if (Kind == OMPC_linear) {
2585 // Try to parse modifier if any.
2586 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
2587 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
2588 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2589 Data.DepLinMapLoc = ConsumeToken();
2590 LinearT.consumeOpen();
2591 NeedRParenForLinear = true;
2592 }
2593 } else if (Kind == OMPC_map) {
2594 // Handle map type for map clause.
2595 ColonProtectionRAIIObject ColonRAII(*this);
2596
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002597 // The first identifier may be a list item, a map-type or a
Kelvin Lief579432018-12-18 22:18:41 +00002598 // map-type-modifier. The map-type can also be delete which has the same
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002599 // spelling of the C++ delete keyword.
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002600 Data.DepLinMapLoc = Tok.getLocation();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002601
Kelvin Lief579432018-12-18 22:18:41 +00002602 // Check for presence of a colon in the map clause.
2603 TentativeParsingAction TPA(*this);
2604 bool ColonPresent = false;
2605 if (SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2606 StopBeforeMatch)) {
2607 if (Tok.is(tok::colon))
2608 ColonPresent = true;
2609 }
2610 TPA.Revert();
2611 // Only parse map-type-modifier[s] and map-type if a colon is present in
2612 // the map clause.
2613 if (ColonPresent) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002614 IsInvalidMapperModifier = parseMapTypeModifiers(Data);
2615 if (!IsInvalidMapperModifier)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002616 parseMapType(*this, Data);
Michael Kruse01f670d2019-02-22 22:29:42 +00002617 else
2618 SkipUntil(tok::colon, tok::annot_pragma_openmp_end, StopBeforeMatch);
Kelvin Lief579432018-12-18 22:18:41 +00002619 }
2620 if (Data.MapType == OMPC_MAP_unknown) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002621 Data.MapType = OMPC_MAP_tofrom;
2622 Data.IsMapTypeImplicit = true;
2623 }
2624
2625 if (Tok.is(tok::colon))
2626 Data.ColonLoc = ConsumeToken();
Michael Kruse0336c752019-02-25 20:34:15 +00002627 } else if (Kind == OMPC_to || Kind == OMPC_from) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002628 if (Tok.is(tok::identifier)) {
2629 bool IsMapperModifier = false;
Michael Kruse0336c752019-02-25 20:34:15 +00002630 if (Kind == OMPC_to) {
2631 auto Modifier = static_cast<OpenMPToModifierKind>(
2632 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2633 if (Modifier == OMPC_TO_MODIFIER_mapper)
2634 IsMapperModifier = true;
2635 } else {
2636 auto Modifier = static_cast<OpenMPFromModifierKind>(
2637 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2638 if (Modifier == OMPC_FROM_MODIFIER_mapper)
2639 IsMapperModifier = true;
2640 }
Michael Kruse01f670d2019-02-22 22:29:42 +00002641 if (IsMapperModifier) {
2642 // Parse the mapper modifier.
2643 ConsumeToken();
2644 IsInvalidMapperModifier = parseMapperModifier(Data);
2645 if (Tok.isNot(tok::colon)) {
2646 if (!IsInvalidMapperModifier)
2647 Diag(Tok, diag::warn_pragma_expected_colon) << ")";
2648 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2649 StopBeforeMatch);
2650 }
2651 // Consume ':'.
2652 if (Tok.is(tok::colon))
2653 ConsumeToken();
2654 }
2655 }
Alexey Bataeve04483e2019-03-27 14:14:31 +00002656 } else if (Kind == OMPC_allocate) {
2657 // Handle optional allocator expression followed by colon delimiter.
2658 ColonProtectionRAIIObject ColonRAII(*this);
2659 TentativeParsingAction TPA(*this);
2660 ExprResult Tail =
2661 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
2662 Tail = Actions.ActOnFinishFullExpr(Tail.get(), T.getOpenLocation(),
2663 /*DiscardedValue=*/false);
2664 if (Tail.isUsable()) {
2665 if (Tok.is(tok::colon)) {
2666 Data.TailExpr = Tail.get();
2667 Data.ColonLoc = ConsumeToken();
2668 TPA.Commit();
2669 } else {
2670 // colon not found, no allocator specified, parse only list of
2671 // variables.
2672 TPA.Revert();
2673 }
2674 } else {
2675 // Parsing was unsuccessfull, revert and skip to the end of clause or
2676 // directive.
2677 TPA.Revert();
2678 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2679 StopBeforeMatch);
2680 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002681 }
2682
Alexey Bataevfa312f32017-07-21 18:48:21 +00002683 bool IsComma =
2684 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
2685 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
2686 (Kind == OMPC_reduction && !InvalidReductionId) ||
Kelvin Lida6bc702018-11-21 19:38:53 +00002687 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown) ||
Alexey Bataevfa312f32017-07-21 18:48:21 +00002688 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002689 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
2690 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
2691 Tok.isNot(tok::annot_pragma_openmp_end))) {
2692 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
2693 // Parse variable
2694 ExprResult VarExpr =
2695 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00002696 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002697 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00002698 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002699 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2700 StopBeforeMatch);
2701 }
2702 // Skip ',' if any
2703 IsComma = Tok.is(tok::comma);
2704 if (IsComma)
2705 ConsumeToken();
2706 else if (Tok.isNot(tok::r_paren) &&
2707 Tok.isNot(tok::annot_pragma_openmp_end) &&
2708 (!MayHaveTail || Tok.isNot(tok::colon)))
2709 Diag(Tok, diag::err_omp_expected_punc)
2710 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
2711 : getOpenMPClauseName(Kind))
2712 << (Kind == OMPC_flush);
2713 }
2714
2715 // Parse ')' for linear clause with modifier.
2716 if (NeedRParenForLinear)
2717 LinearT.consumeClose();
2718
2719 // Parse ':' linear-step (or ':' alignment).
2720 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
2721 if (MustHaveTail) {
2722 Data.ColonLoc = Tok.getLocation();
2723 SourceLocation ELoc = ConsumeToken();
2724 ExprResult Tail = ParseAssignmentExpression();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002725 Tail =
2726 Actions.ActOnFinishFullExpr(Tail.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002727 if (Tail.isUsable())
2728 Data.TailExpr = Tail.get();
2729 else
2730 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2731 StopBeforeMatch);
2732 }
2733
2734 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002735 Data.RLoc = Tok.getLocation();
2736 if (!T.consumeClose())
2737 Data.RLoc = T.getCloseLocation();
Alexey Bataev61908f652018-04-23 19:53:05 +00002738 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
2739 Vars.empty()) ||
2740 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
Michael Kruse4304e9d2019-02-19 16:38:20 +00002741 (MustHaveTail && !Data.TailExpr) || InvalidReductionId ||
Michael Kruse01f670d2019-02-22 22:29:42 +00002742 IsInvalidMapperModifier;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002743}
2744
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002745/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00002746/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
2747/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002748///
2749/// private-clause:
2750/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002751/// firstprivate-clause:
2752/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00002753/// lastprivate-clause:
2754/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00002755/// shared-clause:
2756/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00002757/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00002758/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002759/// aligned-clause:
2760/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00002761/// reduction-clause:
2762/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00002763/// task_reduction-clause:
2764/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00002765/// in_reduction-clause:
2766/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00002767/// copyprivate-clause:
2768/// 'copyprivate' '(' list ')'
2769/// flush-clause:
2770/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002771/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00002772/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00002773/// map-clause:
Kelvin Lief579432018-12-18 22:18:41 +00002774/// 'map' '(' [ [ always [,] ] [ close [,] ]
Michael Kruse01f670d2019-02-22 22:29:42 +00002775/// [ mapper '(' mapper-identifier ')' [,] ]
Kelvin Li0bff7af2015-11-23 05:32:03 +00002776/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00002777/// to-clause:
Michael Kruse01f670d2019-02-22 22:29:42 +00002778/// 'to' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00002779/// from-clause:
Michael Kruse0336c752019-02-25 20:34:15 +00002780/// 'from' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00002781/// use_device_ptr-clause:
2782/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00002783/// is_device_ptr-clause:
2784/// 'is_device_ptr' '(' list ')'
Alexey Bataeve04483e2019-03-27 14:14:31 +00002785/// allocate-clause:
2786/// 'allocate' '(' [ allocator ':' ] list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002787///
Alexey Bataev182227b2015-08-20 10:54:39 +00002788/// For 'linear' clause linear-list may have the following forms:
2789/// list
2790/// modifier(list)
2791/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00002792OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002793 OpenMPClauseKind Kind,
2794 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002795 SourceLocation Loc = Tok.getLocation();
2796 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002797 SmallVector<Expr *, 4> Vars;
2798 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002799
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002800 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00002801 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002802
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002803 if (ParseOnly)
2804 return nullptr;
Michael Kruse4304e9d2019-02-19 16:38:20 +00002805 OMPVarListLocTy Locs(Loc, LOpen, Data.RLoc);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002806 return Actions.ActOnOpenMPVarListClause(
Michael Kruse4304e9d2019-02-19 16:38:20 +00002807 Kind, Vars, Data.TailExpr, Locs, Data.ColonLoc,
2808 Data.ReductionOrMapperIdScopeSpec, Data.ReductionOrMapperId, Data.DepKind,
2809 Data.LinKind, Data.MapTypeModifiers, Data.MapTypeModifiersLoc,
2810 Data.MapType, Data.IsMapTypeImplicit, Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002811}
2812