blob: 91fe10e667db47d9bf0f9520c4530c2214f85f3c [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"
Alexey Bataev4513e93f2019-10-10 15:15:26 +000020#include "llvm/ADT/UniqueVector.h"
Michael Wong65f367f2015-07-21 13:44:28 +000021
Alexey Bataeva769e072013-03-22 06:34:35 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// OpenMP declarative directives.
26//===----------------------------------------------------------------------===//
27
Dmitry Polukhin82478332016-02-13 06:53:38 +000028namespace {
29enum OpenMPDirectiveKindEx {
30 OMPD_cancellation = OMPD_unknown + 1,
31 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000032 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000033 OMPD_end,
34 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000035 OMPD_enter,
36 OMPD_exit,
37 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000038 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000039 OMPD_target_enter,
Samuel Antao686c70c2016-05-26 17:30:50 +000040 OMPD_target_exit,
41 OMPD_update,
Kelvin Li579e41c2016-11-30 23:51:03 +000042 OMPD_distribute_parallel,
Kelvin Li80e8f562016-12-29 22:16:30 +000043 OMPD_teams_distribute_parallel,
Michael Kruse251e1482019-02-01 20:25:04 +000044 OMPD_target_teams_distribute_parallel,
45 OMPD_mapper,
Alexey Bataevd158cf62019-09-13 20:18:17 +000046 OMPD_variant,
Alexey Bataev5bbcead2019-10-14 17:17:41 +000047 OMPD_parallel_master,
Dmitry Polukhin82478332016-02-13 06:53:38 +000048};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000049
Alexey Bataev25ed0c02019-03-07 17:54:44 +000050class DeclDirectiveListParserHelper final {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000051 SmallVector<Expr *, 4> Identifiers;
52 Parser *P;
Alexey Bataev25ed0c02019-03-07 17:54:44 +000053 OpenMPDirectiveKind Kind;
Dmitry Polukhind69b5052016-05-09 14:59:13 +000054
55public:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000056 DeclDirectiveListParserHelper(Parser *P, OpenMPDirectiveKind Kind)
57 : P(P), Kind(Kind) {}
Dmitry Polukhind69b5052016-05-09 14:59:13 +000058 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
Alexey Bataev25ed0c02019-03-07 17:54:44 +000059 ExprResult Res = P->getActions().ActOnOpenMPIdExpression(
60 P->getCurScope(), SS, NameInfo, Kind);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000061 if (Res.isUsable())
62 Identifiers.push_back(Res.get());
63 }
64 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
65};
Dmitry Polukhin82478332016-02-13 06:53:38 +000066} // namespace
67
68// Map token string to extended OMP token kind that are
69// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
70static unsigned getOpenMPDirectiveKindEx(StringRef S) {
71 auto DKind = getOpenMPDirectiveKind(S);
72 if (DKind != OMPD_unknown)
73 return DKind;
74
75 return llvm::StringSwitch<unsigned>(S)
76 .Case("cancellation", OMPD_cancellation)
77 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000078 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000079 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000080 .Case("enter", OMPD_enter)
81 .Case("exit", OMPD_exit)
82 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000083 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000084 .Case("update", OMPD_update)
Michael Kruse251e1482019-02-01 20:25:04 +000085 .Case("mapper", OMPD_mapper)
Alexey Bataevd158cf62019-09-13 20:18:17 +000086 .Case("variant", OMPD_variant)
Dmitry Polukhin82478332016-02-13 06:53:38 +000087 .Default(OMPD_unknown);
88}
89
Alexey Bataev61908f652018-04-23 19:53:05 +000090static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000091 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
92 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
93 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000094 static const unsigned F[][3] = {
Alexey Bataev61908f652018-04-23 19:53:05 +000095 {OMPD_cancellation, OMPD_point, OMPD_cancellation_point},
96 {OMPD_declare, OMPD_reduction, OMPD_declare_reduction},
Michael Kruse251e1482019-02-01 20:25:04 +000097 {OMPD_declare, OMPD_mapper, OMPD_declare_mapper},
Alexey Bataev61908f652018-04-23 19:53:05 +000098 {OMPD_declare, OMPD_simd, OMPD_declare_simd},
99 {OMPD_declare, OMPD_target, OMPD_declare_target},
Alexey Bataevd158cf62019-09-13 20:18:17 +0000100 {OMPD_declare, OMPD_variant, OMPD_declare_variant},
Alexey Bataev61908f652018-04-23 19:53:05 +0000101 {OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel},
102 {OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for},
103 {OMPD_distribute_parallel_for, OMPD_simd,
104 OMPD_distribute_parallel_for_simd},
105 {OMPD_distribute, OMPD_simd, OMPD_distribute_simd},
106 {OMPD_end, OMPD_declare, OMPD_end_declare},
107 {OMPD_end_declare, OMPD_target, OMPD_end_declare_target},
108 {OMPD_target, OMPD_data, OMPD_target_data},
109 {OMPD_target, OMPD_enter, OMPD_target_enter},
110 {OMPD_target, OMPD_exit, OMPD_target_exit},
111 {OMPD_target, OMPD_update, OMPD_target_update},
112 {OMPD_target_enter, OMPD_data, OMPD_target_enter_data},
113 {OMPD_target_exit, OMPD_data, OMPD_target_exit_data},
114 {OMPD_for, OMPD_simd, OMPD_for_simd},
115 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
116 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
117 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
118 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd},
119 {OMPD_target, OMPD_parallel, OMPD_target_parallel},
120 {OMPD_target, OMPD_simd, OMPD_target_simd},
121 {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for},
122 {OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd},
123 {OMPD_teams, OMPD_distribute, OMPD_teams_distribute},
124 {OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd},
125 {OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel},
126 {OMPD_teams_distribute_parallel, OMPD_for,
127 OMPD_teams_distribute_parallel_for},
128 {OMPD_teams_distribute_parallel_for, OMPD_simd,
129 OMPD_teams_distribute_parallel_for_simd},
130 {OMPD_target, OMPD_teams, OMPD_target_teams},
131 {OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute},
132 {OMPD_target_teams_distribute, OMPD_parallel,
133 OMPD_target_teams_distribute_parallel},
134 {OMPD_target_teams_distribute, OMPD_simd,
135 OMPD_target_teams_distribute_simd},
136 {OMPD_target_teams_distribute_parallel, OMPD_for,
137 OMPD_target_teams_distribute_parallel_for},
138 {OMPD_target_teams_distribute_parallel_for, OMPD_simd,
Alexey Bataev60e51c42019-10-10 20:13:02 +0000139 OMPD_target_teams_distribute_parallel_for_simd},
Alexey Bataev5bbcead2019-10-14 17:17:41 +0000140 {OMPD_master, OMPD_taskloop, OMPD_master_taskloop},
Alexey Bataevb8552ab2019-10-18 16:47:35 +0000141 {OMPD_master_taskloop, OMPD_simd, OMPD_master_taskloop_simd},
Alexey Bataev5bbcead2019-10-14 17:17:41 +0000142 {OMPD_parallel, OMPD_master, OMPD_parallel_master},
143 {OMPD_parallel_master, OMPD_taskloop, OMPD_parallel_master_taskloop}};
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000144 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev61908f652018-04-23 19:53:05 +0000145 Token Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000146 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000147 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000148 ? static_cast<unsigned>(OMPD_unknown)
149 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
150 if (DKind == OMPD_unknown)
151 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000152
Alexey Bataev61908f652018-04-23 19:53:05 +0000153 for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) {
154 if (DKind != F[I][0])
Dmitry Polukhin82478332016-02-13 06:53:38 +0000155 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000156
Dmitry Polukhin82478332016-02-13 06:53:38 +0000157 Tok = P.getPreprocessor().LookAhead(0);
158 unsigned SDKind =
159 Tok.isAnnotation()
160 ? static_cast<unsigned>(OMPD_unknown)
161 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
162 if (SDKind == OMPD_unknown)
163 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000164
Alexey Bataev61908f652018-04-23 19:53:05 +0000165 if (SDKind == F[I][1]) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000166 P.ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000167 DKind = F[I][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000168 }
169 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000170 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
171 : OMPD_unknown;
172}
173
174static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000175 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000176 Sema &Actions = P.getActions();
177 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000178 // Allow to use 'operator' keyword for C++ operators
179 bool WithOperator = false;
180 if (Tok.is(tok::kw_operator)) {
181 P.ConsumeToken();
182 Tok = P.getCurToken();
183 WithOperator = true;
184 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000185 switch (Tok.getKind()) {
186 case tok::plus: // '+'
187 OOK = OO_Plus;
188 break;
189 case tok::minus: // '-'
190 OOK = OO_Minus;
191 break;
192 case tok::star: // '*'
193 OOK = OO_Star;
194 break;
195 case tok::amp: // '&'
196 OOK = OO_Amp;
197 break;
198 case tok::pipe: // '|'
199 OOK = OO_Pipe;
200 break;
201 case tok::caret: // '^'
202 OOK = OO_Caret;
203 break;
204 case tok::ampamp: // '&&'
205 OOK = OO_AmpAmp;
206 break;
207 case tok::pipepipe: // '||'
208 OOK = OO_PipePipe;
209 break;
210 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000211 if (!WithOperator)
212 break;
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000213 LLVM_FALLTHROUGH;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000214 default:
215 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
216 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
217 Parser::StopBeforeMatch);
218 return DeclarationName();
219 }
220 P.ConsumeToken();
221 auto &DeclNames = Actions.getASTContext().DeclarationNames;
222 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
223 : DeclNames.getCXXOperatorName(OOK);
224}
225
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000226/// Parse 'omp declare reduction' construct.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000227///
228/// declare-reduction-directive:
229/// annot_pragma_openmp 'declare' 'reduction'
230/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
231/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
232/// annot_pragma_openmp_end
233/// <reduction_id> is either a base language identifier or one of the following
234/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
235///
236Parser::DeclGroupPtrTy
237Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
238 // Parse '('.
239 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
240 if (T.expectAndConsume(diag::err_expected_lparen_after,
241 getOpenMPDirectiveName(OMPD_declare_reduction))) {
242 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
243 return DeclGroupPtrTy();
244 }
245
246 DeclarationName Name = parseOpenMPReductionId(*this);
247 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
248 return DeclGroupPtrTy();
249
250 // Consume ':'.
251 bool IsCorrect = !ExpectAndConsume(tok::colon);
252
253 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
254 return DeclGroupPtrTy();
255
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000256 IsCorrect = IsCorrect && !Name.isEmpty();
257
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000258 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
259 Diag(Tok.getLocation(), diag::err_expected_type);
260 IsCorrect = false;
261 }
262
263 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
264 return DeclGroupPtrTy();
265
266 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
267 // Parse list of types until ':' token.
268 do {
269 ColonProtectionRAIIObject ColonRAII(*this);
270 SourceRange Range;
Faisal Vali421b2d12017-12-29 05:41:00 +0000271 TypeResult TR =
272 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000273 if (TR.isUsable()) {
Alexey Bataev61908f652018-04-23 19:53:05 +0000274 QualType ReductionType =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000275 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
276 if (!ReductionType.isNull()) {
277 ReductionTypes.push_back(
278 std::make_pair(ReductionType, Range.getBegin()));
279 }
280 } else {
281 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
282 StopBeforeMatch);
283 }
284
285 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
286 break;
287
288 // Consume ','.
289 if (ExpectAndConsume(tok::comma)) {
290 IsCorrect = false;
291 if (Tok.is(tok::annot_pragma_openmp_end)) {
292 Diag(Tok.getLocation(), diag::err_expected_type);
293 return DeclGroupPtrTy();
294 }
295 }
296 } while (Tok.isNot(tok::annot_pragma_openmp_end));
297
298 if (ReductionTypes.empty()) {
299 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
300 return DeclGroupPtrTy();
301 }
302
303 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
304 return DeclGroupPtrTy();
305
306 // Consume ':'.
307 if (ExpectAndConsume(tok::colon))
308 IsCorrect = false;
309
310 if (Tok.is(tok::annot_pragma_openmp_end)) {
311 Diag(Tok.getLocation(), diag::err_expected_expression);
312 return DeclGroupPtrTy();
313 }
314
315 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
316 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
317
318 // Parse <combiner> expression and then parse initializer if any for each
319 // correct type.
320 unsigned I = 0, E = ReductionTypes.size();
Alexey Bataev61908f652018-04-23 19:53:05 +0000321 for (Decl *D : DRD.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000322 TentativeParsingAction TPA(*this);
323 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000324 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000325 Scope::OpenMPDirectiveScope);
326 // Parse <combiner> expression.
327 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
328 ExprResult CombinerResult =
329 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000330 D->getLocation(), /*DiscardedValue*/ false);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000331 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
332
333 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
334 Tok.isNot(tok::annot_pragma_openmp_end)) {
335 TPA.Commit();
336 IsCorrect = false;
337 break;
338 }
339 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
340 ExprResult InitializerResult;
341 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
342 // Parse <initializer> expression.
343 if (Tok.is(tok::identifier) &&
Alexey Bataev61908f652018-04-23 19:53:05 +0000344 Tok.getIdentifierInfo()->isStr("initializer")) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000345 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000346 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000347 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
348 TPA.Commit();
349 IsCorrect = false;
350 break;
351 }
352 // Parse '('.
353 BalancedDelimiterTracker T(*this, tok::l_paren,
354 tok::annot_pragma_openmp_end);
355 IsCorrect =
356 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
357 IsCorrect;
358 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
359 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000360 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000361 Scope::OpenMPDirectiveScope);
362 // Parse expression.
Alexey Bataev070f43a2017-09-06 14:49:58 +0000363 VarDecl *OmpPrivParm =
364 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
365 D);
366 // Check if initializer is omp_priv <init_expr> or something else.
367 if (Tok.is(tok::identifier) &&
368 Tok.getIdentifierInfo()->isStr("omp_priv")) {
Alexey Bataeve6aa4692018-09-13 16:54:05 +0000369 if (Actions.getLangOpts().CPlusPlus) {
370 InitializerResult = Actions.ActOnFinishFullExpr(
371 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000372 /*DiscardedValue*/ false);
Alexey Bataeve6aa4692018-09-13 16:54:05 +0000373 } else {
374 ConsumeToken();
375 ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
376 }
Alexey Bataev070f43a2017-09-06 14:49:58 +0000377 } else {
378 InitializerResult = Actions.ActOnFinishFullExpr(
379 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000380 /*DiscardedValue*/ false);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000381 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000382 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
Alexey Bataev070f43a2017-09-06 14:49:58 +0000383 D, InitializerResult.get(), OmpPrivParm);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000384 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
385 Tok.isNot(tok::annot_pragma_openmp_end)) {
386 TPA.Commit();
387 IsCorrect = false;
388 break;
389 }
390 IsCorrect =
391 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
392 }
393 }
394
395 ++I;
396 // Revert parsing if not the last type, otherwise accept it, we're done with
397 // parsing.
398 if (I != E)
399 TPA.Revert();
400 else
401 TPA.Commit();
402 }
403 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
404 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000405}
406
Alexey Bataev070f43a2017-09-06 14:49:58 +0000407void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
408 // Parse declarator '=' initializer.
409 // If a '==' or '+=' is found, suggest a fixit to '='.
410 if (isTokenEqualOrEqualTypo()) {
411 ConsumeToken();
412
413 if (Tok.is(tok::code_completion)) {
414 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
415 Actions.FinalizeDeclaration(OmpPrivParm);
416 cutOffParsing();
417 return;
418 }
419
420 ExprResult Init(ParseInitializer());
421
422 if (Init.isInvalid()) {
423 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
424 Actions.ActOnInitializerError(OmpPrivParm);
425 } else {
426 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
427 /*DirectInit=*/false);
428 }
429 } else if (Tok.is(tok::l_paren)) {
430 // Parse C++ direct initializer: '(' expression-list ')'
431 BalancedDelimiterTracker T(*this, tok::l_paren);
432 T.consumeOpen();
433
434 ExprVector Exprs;
435 CommaLocsTy CommaLocs;
436
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000437 SourceLocation LParLoc = T.getOpenLocation();
Ilya Biryukovff2a9972019-02-26 11:01:50 +0000438 auto RunSignatureHelp = [this, OmpPrivParm, LParLoc, &Exprs]() {
439 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
440 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
441 OmpPrivParm->getLocation(), Exprs, LParLoc);
442 CalledSignatureHelp = true;
443 return PreferredType;
444 };
445 if (ParseExpressionList(Exprs, CommaLocs, [&] {
446 PreferredType.enterFunctionArgument(Tok.getLocation(),
447 RunSignatureHelp);
448 })) {
449 if (PP.isCodeCompletionReached() && !CalledSignatureHelp)
450 RunSignatureHelp();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000451 Actions.ActOnInitializerError(OmpPrivParm);
452 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
453 } else {
454 // Match the ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000455 SourceLocation RLoc = Tok.getLocation();
456 if (!T.consumeClose())
457 RLoc = T.getCloseLocation();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000458
459 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
460 "Unexpected number of commas!");
461
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000462 ExprResult Initializer =
463 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000464 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
465 /*DirectInit=*/true);
466 }
467 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
468 // Parse C++0x braced-init-list.
469 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
470
471 ExprResult Init(ParseBraceInitializer());
472
473 if (Init.isInvalid()) {
474 Actions.ActOnInitializerError(OmpPrivParm);
475 } else {
476 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
477 /*DirectInit=*/true);
478 }
479 } else {
480 Actions.ActOnUninitializedDecl(OmpPrivParm);
481 }
482}
483
Michael Kruse251e1482019-02-01 20:25:04 +0000484/// Parses 'omp declare mapper' directive.
485///
486/// declare-mapper-directive:
487/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifier> ':']
488/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
489/// annot_pragma_openmp_end
490/// <mapper-identifier> and <var> are base language identifiers.
491///
492Parser::DeclGroupPtrTy
493Parser::ParseOpenMPDeclareMapperDirective(AccessSpecifier AS) {
494 bool IsCorrect = true;
495 // Parse '('
496 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
497 if (T.expectAndConsume(diag::err_expected_lparen_after,
498 getOpenMPDirectiveName(OMPD_declare_mapper))) {
499 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
500 return DeclGroupPtrTy();
501 }
502
503 // Parse <mapper-identifier>
504 auto &DeclNames = Actions.getASTContext().DeclarationNames;
505 DeclarationName MapperId;
506 if (PP.LookAhead(0).is(tok::colon)) {
507 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
508 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
509 IsCorrect = false;
510 } else {
511 MapperId = DeclNames.getIdentifier(Tok.getIdentifierInfo());
512 }
513 ConsumeToken();
514 // Consume ':'.
515 ExpectAndConsume(tok::colon);
516 } else {
517 // If no mapper identifier is provided, its name is "default" by default
518 MapperId =
519 DeclNames.getIdentifier(&Actions.getASTContext().Idents.get("default"));
520 }
521
522 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
523 return DeclGroupPtrTy();
524
525 // Parse <type> <var>
526 DeclarationName VName;
527 QualType MapperType;
528 SourceRange Range;
529 TypeResult ParsedType = parseOpenMPDeclareMapperVarDecl(Range, VName, AS);
530 if (ParsedType.isUsable())
531 MapperType =
532 Actions.ActOnOpenMPDeclareMapperType(Range.getBegin(), ParsedType);
533 if (MapperType.isNull())
534 IsCorrect = false;
535 if (!IsCorrect) {
536 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
537 return DeclGroupPtrTy();
538 }
539
540 // Consume ')'.
541 IsCorrect &= !T.consumeClose();
542 if (!IsCorrect) {
543 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
544 return DeclGroupPtrTy();
545 }
546
547 // Enter scope.
548 OMPDeclareMapperDecl *DMD = Actions.ActOnOpenMPDeclareMapperDirectiveStart(
549 getCurScope(), Actions.getCurLexicalContext(), MapperId, MapperType,
550 Range.getBegin(), VName, AS);
551 DeclarationNameInfo DirName;
552 SourceLocation Loc = Tok.getLocation();
553 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
554 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
555 ParseScope OMPDirectiveScope(this, ScopeFlags);
556 Actions.StartOpenMPDSABlock(OMPD_declare_mapper, DirName, getCurScope(), Loc);
557
558 // Add the mapper variable declaration.
559 Actions.ActOnOpenMPDeclareMapperDirectiveVarDecl(
560 DMD, getCurScope(), MapperType, Range.getBegin(), VName);
561
562 // Parse map clauses.
563 SmallVector<OMPClause *, 6> Clauses;
564 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
565 OpenMPClauseKind CKind = Tok.isAnnotation()
566 ? OMPC_unknown
567 : getOpenMPClauseKind(PP.getSpelling(Tok));
568 Actions.StartOpenMPClause(CKind);
569 OMPClause *Clause =
570 ParseOpenMPClause(OMPD_declare_mapper, CKind, Clauses.size() == 0);
571 if (Clause)
572 Clauses.push_back(Clause);
573 else
574 IsCorrect = false;
575 // Skip ',' if any.
576 if (Tok.is(tok::comma))
577 ConsumeToken();
578 Actions.EndOpenMPClause();
579 }
580 if (Clauses.empty()) {
581 Diag(Tok, diag::err_omp_expected_clause)
582 << getOpenMPDirectiveName(OMPD_declare_mapper);
583 IsCorrect = false;
584 }
585
586 // Exit scope.
587 Actions.EndOpenMPDSABlock(nullptr);
588 OMPDirectiveScope.Exit();
589
590 DeclGroupPtrTy DGP =
591 Actions.ActOnOpenMPDeclareMapperDirectiveEnd(DMD, getCurScope(), Clauses);
592 if (!IsCorrect)
593 return DeclGroupPtrTy();
594 return DGP;
595}
596
597TypeResult Parser::parseOpenMPDeclareMapperVarDecl(SourceRange &Range,
598 DeclarationName &Name,
599 AccessSpecifier AS) {
600 // Parse the common declaration-specifiers piece.
601 Parser::DeclSpecContext DSC = Parser::DeclSpecContext::DSC_type_specifier;
602 DeclSpec DS(AttrFactory);
603 ParseSpecifierQualifierList(DS, AS, DSC);
604
605 // Parse the declarator.
606 DeclaratorContext Context = DeclaratorContext::PrototypeContext;
607 Declarator DeclaratorInfo(DS, Context);
608 ParseDeclarator(DeclaratorInfo);
609 Range = DeclaratorInfo.getSourceRange();
610 if (DeclaratorInfo.getIdentifier() == nullptr) {
611 Diag(Tok.getLocation(), diag::err_omp_mapper_expected_declarator);
612 return true;
613 }
614 Name = Actions.GetNameForDeclarator(DeclaratorInfo).getName();
615
616 return Actions.ActOnOpenMPDeclareMapperVarDecl(getCurScope(), DeclaratorInfo);
617}
618
Alexey Bataev2af33e32016-04-07 12:45:37 +0000619namespace {
620/// RAII that recreates function context for correct parsing of clauses of
621/// 'declare simd' construct.
622/// OpenMP, 2.8.2 declare simd Construct
623/// The expressions appearing in the clauses of this directive are evaluated in
624/// the scope of the arguments of the function declaration or definition.
625class FNContextRAII final {
626 Parser &P;
627 Sema::CXXThisScopeRAII *ThisScope;
628 Parser::ParseScope *TempScope;
629 Parser::ParseScope *FnScope;
630 bool HasTemplateScope = false;
631 bool HasFunScope = false;
632 FNContextRAII() = delete;
633 FNContextRAII(const FNContextRAII &) = delete;
634 FNContextRAII &operator=(const FNContextRAII &) = delete;
635
636public:
637 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
638 Decl *D = *Ptr.get().begin();
639 NamedDecl *ND = dyn_cast<NamedDecl>(D);
640 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
641 Sema &Actions = P.getActions();
642
643 // Allow 'this' within late-parsed attributes.
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000644 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, Qualifiers(),
Alexey Bataev2af33e32016-04-07 12:45:37 +0000645 ND && ND->isCXXInstanceMember());
646
647 // If the Decl is templatized, add template parameters to scope.
648 HasTemplateScope = D->isTemplateDecl();
649 TempScope =
650 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
651 if (HasTemplateScope)
652 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
653
654 // If the Decl is on a function, add function parameters to the scope.
655 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000656 FnScope = new Parser::ParseScope(
657 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
658 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000659 if (HasFunScope)
660 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
661 }
662 ~FNContextRAII() {
663 if (HasFunScope) {
664 P.getActions().ActOnExitFunctionContext();
665 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
666 }
667 if (HasTemplateScope)
668 TempScope->Exit();
669 delete FnScope;
670 delete TempScope;
671 delete ThisScope;
672 }
673};
674} // namespace
675
Alexey Bataevd93d3762016-04-12 09:35:56 +0000676/// Parses clauses for 'declare simd' directive.
677/// clause:
678/// 'inbranch' | 'notinbranch'
679/// 'simdlen' '(' <expr> ')'
680/// { 'uniform' '(' <argument_list> ')' }
681/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000682/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
683static bool parseDeclareSimdClauses(
684 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
685 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
686 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
687 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000688 SourceRange BSRange;
689 const Token &Tok = P.getCurToken();
690 bool IsError = false;
691 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
692 if (Tok.isNot(tok::identifier))
693 break;
694 OMPDeclareSimdDeclAttr::BranchStateTy Out;
695 IdentifierInfo *II = Tok.getIdentifierInfo();
696 StringRef ClauseName = II->getName();
697 // Parse 'inranch|notinbranch' clauses.
698 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
699 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
700 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
701 << ClauseName
702 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
703 IsError = true;
704 }
705 BS = Out;
706 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
707 P.ConsumeToken();
708 } else if (ClauseName.equals("simdlen")) {
709 if (SimdLen.isUsable()) {
710 P.Diag(Tok, diag::err_omp_more_one_clause)
711 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
712 IsError = true;
713 }
714 P.ConsumeToken();
715 SourceLocation RLoc;
716 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
717 if (SimdLen.isInvalid())
718 IsError = true;
719 } else {
720 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000721 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
722 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000723 Parser::OpenMPVarListDataTy Data;
Alexey Bataev61908f652018-04-23 19:53:05 +0000724 SmallVectorImpl<Expr *> *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000725 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000726 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000727 else if (CKind == OMPC_linear)
728 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000729
730 P.ConsumeToken();
731 if (P.ParseOpenMPVarList(OMPD_declare_simd,
732 getOpenMPClauseKind(ClauseName), *Vars, Data))
733 IsError = true;
Alexey Bataev61908f652018-04-23 19:53:05 +0000734 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000735 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataev61908f652018-04-23 19:53:05 +0000736 } else if (CKind == OMPC_linear) {
Alexey Bataevecba70f2016-04-12 11:02:11 +0000737 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
738 Data.DepLinMapLoc))
739 Data.LinKind = OMPC_LINEAR_val;
740 LinModifiers.append(Linears.size() - LinModifiers.size(),
741 Data.LinKind);
742 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
743 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000744 } else
745 // TODO: add parsing of other clauses.
746 break;
747 }
748 // Skip ',' if any.
749 if (Tok.is(tok::comma))
750 P.ConsumeToken();
751 }
752 return IsError;
753}
754
Alexey Bataev2af33e32016-04-07 12:45:37 +0000755/// Parse clauses for '#pragma omp declare simd'.
756Parser::DeclGroupPtrTy
757Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
758 CachedTokens &Toks, SourceLocation Loc) {
Ilya Biryukov929af672019-05-17 09:32:05 +0000759 PP.EnterToken(Tok, /*IsReinject*/ true);
760 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
761 /*IsReinject*/ true);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000762 // Consume the previously pushed token.
763 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Alexey Bataevd158cf62019-09-13 20:18:17 +0000764 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000765
766 FNContextRAII FnContext(*this, Ptr);
767 OMPDeclareSimdDeclAttr::BranchStateTy BS =
768 OMPDeclareSimdDeclAttr::BS_Undefined;
769 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000770 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000771 SmallVector<Expr *, 4> Aligneds;
772 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000773 SmallVector<Expr *, 4> Linears;
774 SmallVector<unsigned, 4> LinModifiers;
775 SmallVector<Expr *, 4> Steps;
776 bool IsError =
777 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
778 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000779 // Need to check for extra tokens.
780 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
781 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
782 << getOpenMPDirectiveName(OMPD_declare_simd);
783 while (Tok.isNot(tok::annot_pragma_openmp_end))
784 ConsumeAnyToken();
785 }
786 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000787 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000788 if (IsError)
789 return Ptr;
790 return Actions.ActOnOpenMPDeclareSimdDirective(
791 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
792 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataev20dfd772016-04-04 10:12:15 +0000793}
794
Alexey Bataeva15a1412019-10-02 18:19:02 +0000795/// Parse optional 'score' '(' <expr> ')' ':'.
796static ExprResult parseContextScore(Parser &P) {
797 ExprResult ScoreExpr;
798 SmallString<16> Buffer;
799 StringRef SelectorName =
800 P.getPreprocessor().getSpelling(P.getCurToken(), Buffer);
801 OMPDeclareVariantAttr::ScoreType ScoreKind =
802 OMPDeclareVariantAttr::ScoreUnknown;
803 (void)OMPDeclareVariantAttr::ConvertStrToScoreType(SelectorName, ScoreKind);
804 if (ScoreKind == OMPDeclareVariantAttr::ScoreUnknown)
805 return ScoreExpr;
806 assert(ScoreKind == OMPDeclareVariantAttr::ScoreSpecified &&
807 "Expected \"score\" clause.");
808 (void)P.ConsumeToken();
809 SourceLocation RLoc;
810 ScoreExpr = P.ParseOpenMPParensExpr(SelectorName, RLoc);
811 // Parse ':'
812 if (P.getCurToken().is(tok::colon))
813 (void)P.ConsumeAnyToken();
814 else
815 P.Diag(P.getCurToken(), diag::warn_pragma_expected_colon)
816 << "context selector score clause";
817 return ScoreExpr;
818}
819
Alexey Bataev9ff34742019-09-25 19:43:37 +0000820/// Parse context selector for 'implementation' selector set:
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000821/// 'vendor' '(' [ 'score' '(' <score _expr> ')' ':' ] <vendor> { ',' <vendor> }
822/// ')'
823static void parseImplementationSelector(
Alexey Bataev70d2e542019-10-08 17:47:52 +0000824 Parser &P, SourceLocation Loc, llvm::StringMap<SourceLocation> &UsedCtx,
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000825 llvm::function_ref<void(SourceRange,
826 const Sema::OpenMPDeclareVariantCtsSelectorData &)>
827 Callback) {
Alexey Bataev9ff34742019-09-25 19:43:37 +0000828 const Token &Tok = P.getCurToken();
829 // Parse inner context selector set name, if any.
830 if (!Tok.is(tok::identifier)) {
831 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
832 << "implementation";
833 // Skip until either '}', ')', or end of directive.
834 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
835 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
836 ;
837 return;
838 }
839 SmallString<16> Buffer;
840 StringRef CtxSelectorName = P.getPreprocessor().getSpelling(Tok, Buffer);
Alexey Bataev70d2e542019-10-08 17:47:52 +0000841 auto Res = UsedCtx.try_emplace(CtxSelectorName, Tok.getLocation());
842 if (!Res.second) {
843 // OpenMP 5.0, 2.3.2 Context Selectors, Restrictions.
844 // Each trait-selector-name can only be specified once.
845 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_ctx_mutiple_use)
846 << CtxSelectorName << "implementation";
847 P.Diag(Res.first->getValue(), diag::note_omp_declare_variant_ctx_used_here)
848 << CtxSelectorName;
849 }
Alexey Bataev9ff34742019-09-25 19:43:37 +0000850 OMPDeclareVariantAttr::CtxSelectorType CSKind =
851 OMPDeclareVariantAttr::CtxUnknown;
852 (void)OMPDeclareVariantAttr::ConvertStrToCtxSelectorType(CtxSelectorName,
853 CSKind);
854 (void)P.ConsumeToken();
855 switch (CSKind) {
856 case OMPDeclareVariantAttr::CtxVendor: {
857 // Parse '('.
858 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
859 (void)T.expectAndConsume(diag::err_expected_lparen_after,
860 CtxSelectorName.data());
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000861 const ExprResult Score = parseContextScore(P);
Alexey Bataev4513e93f2019-10-10 15:15:26 +0000862 llvm::UniqueVector<llvm::SmallString<16>> Vendors;
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000863 do {
864 // Parse <vendor>.
865 StringRef VendorName;
866 if (Tok.is(tok::identifier)) {
867 Buffer.clear();
868 VendorName = P.getPreprocessor().getSpelling(P.getCurToken(), Buffer);
869 (void)P.ConsumeToken();
Alexey Bataev303657a2019-10-08 19:44:16 +0000870 if (!VendorName.empty())
Alexey Bataev4513e93f2019-10-10 15:15:26 +0000871 Vendors.insert(VendorName);
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000872 } else {
873 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_item_expected)
874 << "vendor identifier"
875 << "vendor"
876 << "implementation";
877 }
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000878 if (!P.TryConsumeToken(tok::comma) && Tok.isNot(tok::r_paren)) {
879 P.Diag(Tok, diag::err_expected_punc)
880 << (VendorName.empty() ? "vendor name" : VendorName);
881 }
882 } while (Tok.is(tok::identifier));
Alexey Bataev9ff34742019-09-25 19:43:37 +0000883 // Parse ')'.
884 (void)T.consumeClose();
Alexey Bataev303657a2019-10-08 19:44:16 +0000885 if (!Vendors.empty()) {
886 SmallVector<StringRef, 4> ImplVendors(Vendors.size());
Alexey Bataev4513e93f2019-10-10 15:15:26 +0000887 llvm::copy(Vendors, ImplVendors.begin());
Alexey Bataev303657a2019-10-08 19:44:16 +0000888 Sema::OpenMPDeclareVariantCtsSelectorData Data(
Alexey Bataev4513e93f2019-10-10 15:15:26 +0000889 OMPDeclareVariantAttr::CtxSetImplementation, CSKind,
890 llvm::makeMutableArrayRef(ImplVendors.begin(), ImplVendors.size()),
Alexey Bataev303657a2019-10-08 19:44:16 +0000891 Score);
892 Callback(SourceRange(Loc, Tok.getLocation()), Data);
893 }
Alexey Bataev9ff34742019-09-25 19:43:37 +0000894 break;
895 }
896 case OMPDeclareVariantAttr::CtxUnknown:
897 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
898 << "implementation";
899 // Skip until either '}', ')', or end of directive.
900 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
901 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
902 ;
903 return;
904 }
Alexey Bataev9ff34742019-09-25 19:43:37 +0000905}
906
Alexey Bataevd158cf62019-09-13 20:18:17 +0000907/// Parses clauses for 'declare variant' directive.
908/// clause:
Alexey Bataevd158cf62019-09-13 20:18:17 +0000909/// <selector_set_name> '=' '{' <context_selectors> '}'
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000910/// [ ',' <selector_set_name> '=' '{' <context_selectors> '}' ]
911bool Parser::parseOpenMPContextSelectors(
Alexey Bataev9ff34742019-09-25 19:43:37 +0000912 SourceLocation Loc,
913 llvm::function_ref<void(SourceRange,
914 const Sema::OpenMPDeclareVariantCtsSelectorData &)>
915 Callback) {
Alexey Bataev5d154c32019-10-08 15:56:43 +0000916 llvm::StringMap<SourceLocation> UsedCtxSets;
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000917 do {
918 // Parse inner context selector set name.
919 if (!Tok.is(tok::identifier)) {
920 Diag(Tok.getLocation(), diag::err_omp_declare_variant_no_ctx_selector)
Alexey Bataevdba792c2019-09-23 18:13:31 +0000921 << getOpenMPClauseName(OMPC_match);
Alexey Bataevd158cf62019-09-13 20:18:17 +0000922 return true;
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000923 }
924 SmallString<16> Buffer;
Alexey Bataev9ff34742019-09-25 19:43:37 +0000925 StringRef CtxSelectorSetName = PP.getSpelling(Tok, Buffer);
Alexey Bataev5d154c32019-10-08 15:56:43 +0000926 auto Res = UsedCtxSets.try_emplace(CtxSelectorSetName, Tok.getLocation());
927 if (!Res.second) {
928 // OpenMP 5.0, 2.3.2 Context Selectors, Restrictions.
929 // Each trait-set-selector-name can only be specified once.
930 Diag(Tok.getLocation(), diag::err_omp_declare_variant_ctx_set_mutiple_use)
931 << CtxSelectorSetName;
932 Diag(Res.first->getValue(),
933 diag::note_omp_declare_variant_ctx_set_used_here)
934 << CtxSelectorSetName;
935 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000936 // Parse '='.
937 (void)ConsumeToken();
938 if (Tok.isNot(tok::equal)) {
939 Diag(Tok.getLocation(), diag::err_omp_declare_variant_equal_expected)
Alexey Bataev9ff34742019-09-25 19:43:37 +0000940 << CtxSelectorSetName;
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000941 return true;
942 }
943 (void)ConsumeToken();
944 // TBD: add parsing of known context selectors.
945 // Unknown selector - just ignore it completely.
946 {
947 // Parse '{'.
948 BalancedDelimiterTracker TBr(*this, tok::l_brace,
949 tok::annot_pragma_openmp_end);
950 if (TBr.expectAndConsume(diag::err_expected_lbrace_after, "="))
951 return true;
Alexey Bataev9ff34742019-09-25 19:43:37 +0000952 OMPDeclareVariantAttr::CtxSelectorSetType CSSKind =
953 OMPDeclareVariantAttr::CtxSetUnknown;
954 (void)OMPDeclareVariantAttr::ConvertStrToCtxSelectorSetType(
955 CtxSelectorSetName, CSSKind);
Alexey Bataev70d2e542019-10-08 17:47:52 +0000956 llvm::StringMap<SourceLocation> UsedCtx;
957 do {
958 switch (CSSKind) {
959 case OMPDeclareVariantAttr::CtxSetImplementation:
960 parseImplementationSelector(*this, Loc, UsedCtx, Callback);
961 break;
962 case OMPDeclareVariantAttr::CtxSetUnknown:
963 // Skip until either '}', ')', or end of directive.
964 while (!SkipUntil(tok::r_brace, tok::r_paren,
965 tok::annot_pragma_openmp_end, StopBeforeMatch))
966 ;
967 break;
968 }
969 const Token PrevTok = Tok;
970 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
971 Diag(Tok, diag::err_omp_expected_comma_brace)
972 << (PrevTok.isAnnotation() ? "context selector trait"
973 : PP.getSpelling(PrevTok));
974 } while (Tok.is(tok::identifier));
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000975 // Parse '}'.
976 (void)TBr.consumeClose();
977 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000978 // Consume ','
979 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end))
980 (void)ExpectAndConsume(tok::comma);
981 } while (Tok.isAnyIdentifier());
Alexey Bataevd158cf62019-09-13 20:18:17 +0000982 return false;
983}
984
985/// Parse clauses for '#pragma omp declare variant ( variant-func-id ) clause'.
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000986void Parser::ParseOMPDeclareVariantClauses(Parser::DeclGroupPtrTy Ptr,
987 CachedTokens &Toks,
988 SourceLocation Loc) {
Alexey Bataevd158cf62019-09-13 20:18:17 +0000989 PP.EnterToken(Tok, /*IsReinject*/ true);
990 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
991 /*IsReinject*/ true);
992 // Consume the previously pushed token.
993 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
994 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
995
996 FNContextRAII FnContext(*this, Ptr);
997 // Parse function declaration id.
998 SourceLocation RLoc;
999 // Parse with IsAddressOfOperand set to true to parse methods as DeclRefExprs
1000 // instead of MemberExprs.
1001 ExprResult AssociatedFunction =
1002 ParseOpenMPParensExpr(getOpenMPDirectiveName(OMPD_declare_variant), RLoc,
1003 /*IsAddressOfOperand=*/true);
1004 if (!AssociatedFunction.isUsable()) {
1005 if (!Tok.is(tok::annot_pragma_openmp_end))
1006 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
1007 ;
1008 // Skip the last annot_pragma_openmp_end.
1009 (void)ConsumeAnnotationToken();
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001010 return;
1011 }
1012 Optional<std::pair<FunctionDecl *, Expr *>> DeclVarData =
1013 Actions.checkOpenMPDeclareVariantFunction(
1014 Ptr, AssociatedFunction.get(), SourceRange(Loc, Tok.getLocation()));
1015
1016 // Parse 'match'.
Alexey Bataevdba792c2019-09-23 18:13:31 +00001017 OpenMPClauseKind CKind = Tok.isAnnotation()
1018 ? OMPC_unknown
1019 : getOpenMPClauseKind(PP.getSpelling(Tok));
1020 if (CKind != OMPC_match) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001021 Diag(Tok.getLocation(), diag::err_omp_declare_variant_wrong_clause)
Alexey Bataevdba792c2019-09-23 18:13:31 +00001022 << getOpenMPClauseName(OMPC_match);
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001023 while (!SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
1024 ;
1025 // Skip the last annot_pragma_openmp_end.
1026 (void)ConsumeAnnotationToken();
1027 return;
1028 }
1029 (void)ConsumeToken();
1030 // Parse '('.
1031 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataevdba792c2019-09-23 18:13:31 +00001032 if (T.expectAndConsume(diag::err_expected_lparen_after,
1033 getOpenMPClauseName(OMPC_match))) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001034 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
1035 ;
1036 // Skip the last annot_pragma_openmp_end.
1037 (void)ConsumeAnnotationToken();
1038 return;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001039 }
1040
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001041 // Parse inner context selectors.
Alexey Bataev9ff34742019-09-25 19:43:37 +00001042 if (!parseOpenMPContextSelectors(
1043 Loc, [this, &DeclVarData](
1044 SourceRange SR,
1045 const Sema::OpenMPDeclareVariantCtsSelectorData &Data) {
1046 if (DeclVarData.hasValue())
1047 Actions.ActOnOpenMPDeclareVariantDirective(
1048 DeclVarData.getValue().first, DeclVarData.getValue().second,
1049 SR, Data);
1050 })) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001051 // Parse ')'.
1052 (void)T.consumeClose();
1053 // Need to check for extra tokens.
1054 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1055 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1056 << getOpenMPDirectiveName(OMPD_declare_variant);
1057 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001058 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001059
1060 // Skip last tokens.
1061 while (Tok.isNot(tok::annot_pragma_openmp_end))
1062 ConsumeAnyToken();
Alexey Bataevd158cf62019-09-13 20:18:17 +00001063 // Skip the last annot_pragma_openmp_end.
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001064 (void)ConsumeAnnotationToken();
Alexey Bataevd158cf62019-09-13 20:18:17 +00001065}
1066
Alexey Bataev729e2422019-08-23 16:11:14 +00001067/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
1068///
1069/// default-clause:
1070/// 'default' '(' 'none' | 'shared' ')
1071///
1072/// proc_bind-clause:
1073/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1074///
1075/// device_type-clause:
1076/// 'device_type' '(' 'host' | 'nohost' | 'any' )'
1077namespace {
1078 struct SimpleClauseData {
1079 unsigned Type;
1080 SourceLocation Loc;
1081 SourceLocation LOpen;
1082 SourceLocation TypeLoc;
1083 SourceLocation RLoc;
1084 SimpleClauseData(unsigned Type, SourceLocation Loc, SourceLocation LOpen,
1085 SourceLocation TypeLoc, SourceLocation RLoc)
1086 : Type(Type), Loc(Loc), LOpen(LOpen), TypeLoc(TypeLoc), RLoc(RLoc) {}
1087 };
1088} // anonymous namespace
1089
1090static Optional<SimpleClauseData>
1091parseOpenMPSimpleClause(Parser &P, OpenMPClauseKind Kind) {
1092 const Token &Tok = P.getCurToken();
1093 SourceLocation Loc = Tok.getLocation();
1094 SourceLocation LOpen = P.ConsumeToken();
1095 // Parse '('.
1096 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
1097 if (T.expectAndConsume(diag::err_expected_lparen_after,
1098 getOpenMPClauseName(Kind)))
1099 return llvm::None;
1100
1101 unsigned Type = getOpenMPSimpleClauseType(
1102 Kind, Tok.isAnnotation() ? "" : P.getPreprocessor().getSpelling(Tok));
1103 SourceLocation TypeLoc = Tok.getLocation();
1104 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1105 Tok.isNot(tok::annot_pragma_openmp_end))
1106 P.ConsumeAnyToken();
1107
1108 // Parse ')'.
1109 SourceLocation RLoc = Tok.getLocation();
1110 if (!T.consumeClose())
1111 RLoc = T.getCloseLocation();
1112
1113 return SimpleClauseData(Type, Loc, LOpen, TypeLoc, RLoc);
1114}
1115
Kelvin Lie0502752018-11-21 20:15:57 +00001116Parser::DeclGroupPtrTy Parser::ParseOMPDeclareTargetClauses() {
1117 // OpenMP 4.5 syntax with list of entities.
1118 Sema::NamedDeclSetType SameDirectiveDecls;
Alexey Bataev729e2422019-08-23 16:11:14 +00001119 SmallVector<std::tuple<OMPDeclareTargetDeclAttr::MapTypeTy, SourceLocation,
1120 NamedDecl *>,
1121 4>
1122 DeclareTargetDecls;
1123 OMPDeclareTargetDeclAttr::DevTypeTy DT = OMPDeclareTargetDeclAttr::DT_Any;
1124 SourceLocation DeviceTypeLoc;
Kelvin Lie0502752018-11-21 20:15:57 +00001125 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1126 OMPDeclareTargetDeclAttr::MapTypeTy MT = OMPDeclareTargetDeclAttr::MT_To;
1127 if (Tok.is(tok::identifier)) {
1128 IdentifierInfo *II = Tok.getIdentifierInfo();
1129 StringRef ClauseName = II->getName();
Alexey Bataev729e2422019-08-23 16:11:14 +00001130 bool IsDeviceTypeClause =
1131 getLangOpts().OpenMP >= 50 &&
1132 getOpenMPClauseKind(ClauseName) == OMPC_device_type;
1133 // Parse 'to|link|device_type' clauses.
1134 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName, MT) &&
1135 !IsDeviceTypeClause) {
1136 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
1137 << ClauseName << (getLangOpts().OpenMP >= 50 ? 1 : 0);
Kelvin Lie0502752018-11-21 20:15:57 +00001138 break;
1139 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001140 // Parse 'device_type' clause and go to next clause if any.
1141 if (IsDeviceTypeClause) {
1142 Optional<SimpleClauseData> DevTypeData =
1143 parseOpenMPSimpleClause(*this, OMPC_device_type);
1144 if (DevTypeData.hasValue()) {
1145 if (DeviceTypeLoc.isValid()) {
1146 // We already saw another device_type clause, diagnose it.
1147 Diag(DevTypeData.getValue().Loc,
1148 diag::warn_omp_more_one_device_type_clause);
1149 }
1150 switch(static_cast<OpenMPDeviceType>(DevTypeData.getValue().Type)) {
1151 case OMPC_DEVICE_TYPE_any:
1152 DT = OMPDeclareTargetDeclAttr::DT_Any;
1153 break;
1154 case OMPC_DEVICE_TYPE_host:
1155 DT = OMPDeclareTargetDeclAttr::DT_Host;
1156 break;
1157 case OMPC_DEVICE_TYPE_nohost:
1158 DT = OMPDeclareTargetDeclAttr::DT_NoHost;
1159 break;
1160 case OMPC_DEVICE_TYPE_unknown:
1161 llvm_unreachable("Unexpected device_type");
1162 }
1163 DeviceTypeLoc = DevTypeData.getValue().Loc;
1164 }
1165 continue;
1166 }
Kelvin Lie0502752018-11-21 20:15:57 +00001167 ConsumeToken();
1168 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001169 auto &&Callback = [this, MT, &DeclareTargetDecls, &SameDirectiveDecls](
1170 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
1171 NamedDecl *ND = Actions.lookupOpenMPDeclareTargetName(
1172 getCurScope(), SS, NameInfo, SameDirectiveDecls);
1173 if (ND)
1174 DeclareTargetDecls.emplace_back(MT, NameInfo.getLoc(), ND);
Kelvin Lie0502752018-11-21 20:15:57 +00001175 };
1176 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
1177 /*AllowScopeSpecifier=*/true))
1178 break;
1179
1180 // Consume optional ','.
1181 if (Tok.is(tok::comma))
1182 ConsumeToken();
1183 }
1184 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1185 ConsumeAnyToken();
Alexey Bataev729e2422019-08-23 16:11:14 +00001186 for (auto &MTLocDecl : DeclareTargetDecls) {
1187 OMPDeclareTargetDeclAttr::MapTypeTy MT;
1188 SourceLocation Loc;
1189 NamedDecl *ND;
1190 std::tie(MT, Loc, ND) = MTLocDecl;
1191 // device_type clause is applied only to functions.
1192 Actions.ActOnOpenMPDeclareTargetName(
1193 ND, Loc, MT, isa<VarDecl>(ND) ? OMPDeclareTargetDeclAttr::DT_Any : DT);
1194 }
Kelvin Lie0502752018-11-21 20:15:57 +00001195 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
1196 SameDirectiveDecls.end());
1197 if (Decls.empty())
1198 return DeclGroupPtrTy();
1199 return Actions.BuildDeclaratorGroup(Decls);
1200}
1201
1202void Parser::ParseOMPEndDeclareTargetDirective(OpenMPDirectiveKind DKind,
1203 SourceLocation DTLoc) {
1204 if (DKind != OMPD_end_declare_target) {
1205 Diag(Tok, diag::err_expected_end_declare_target);
1206 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
1207 return;
1208 }
1209 ConsumeAnyToken();
1210 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1211 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1212 << getOpenMPDirectiveName(OMPD_end_declare_target);
1213 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1214 }
1215 // Skip the last annot_pragma_openmp_end.
1216 ConsumeAnyToken();
1217}
1218
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001219/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001220///
1221/// threadprivate-directive:
1222/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001223/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +00001224///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001225/// allocate-directive:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001226/// annot_pragma_openmp 'allocate' simple-variable-list [<clause>]
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001227/// annot_pragma_openmp_end
1228///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001229/// declare-reduction-directive:
1230/// annot_pragma_openmp 'declare' 'reduction' [...]
1231/// annot_pragma_openmp_end
1232///
Michael Kruse251e1482019-02-01 20:25:04 +00001233/// declare-mapper-directive:
1234/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1235/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1236/// annot_pragma_openmp_end
1237///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001238/// declare-simd-directive:
1239/// annot_pragma_openmp 'declare simd' {<clause> [,]}
1240/// annot_pragma_openmp_end
1241/// <function declaration/definition>
1242///
Kelvin Li1408f912018-09-26 04:28:39 +00001243/// requires directive:
1244/// annot_pragma_openmp 'requires' <clause> [[[,] <clause>] ... ]
1245/// annot_pragma_openmp_end
1246///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001247Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
1248 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
1249 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001250 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +00001251 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +00001252
Richard Smithaf3b3252017-05-18 19:21:48 +00001253 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001254 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001255
1256 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001257 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +00001258 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001259 DeclDirectiveListParserHelper Helper(this, DKind);
1260 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1261 /*AllowScopeSpecifier=*/true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001262 // The last seen token is annot_pragma_openmp_end - need to check for
1263 // extra tokens.
1264 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1265 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001266 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001267 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +00001268 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001269 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001270 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001271 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
1272 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +00001273 }
1274 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001275 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001276 case OMPD_allocate: {
1277 ConsumeToken();
1278 DeclDirectiveListParserHelper Helper(this, DKind);
1279 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1280 /*AllowScopeSpecifier=*/true)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001281 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001282 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001283 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1284 OMPC_unknown + 1>
1285 FirstClauses(OMPC_unknown + 1);
1286 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1287 OpenMPClauseKind CKind =
1288 Tok.isAnnotation() ? OMPC_unknown
1289 : getOpenMPClauseKind(PP.getSpelling(Tok));
1290 Actions.StartOpenMPClause(CKind);
1291 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1292 !FirstClauses[CKind].getInt());
1293 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1294 StopBeforeMatch);
1295 FirstClauses[CKind].setInt(true);
1296 if (Clause != nullptr)
1297 Clauses.push_back(Clause);
1298 if (Tok.is(tok::annot_pragma_openmp_end)) {
1299 Actions.EndOpenMPClause();
1300 break;
1301 }
1302 // Skip ',' if any.
1303 if (Tok.is(tok::comma))
1304 ConsumeToken();
1305 Actions.EndOpenMPClause();
1306 }
1307 // The last seen token is annot_pragma_openmp_end - need to check for
1308 // extra tokens.
1309 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1310 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1311 << getOpenMPDirectiveName(DKind);
1312 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1313 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001314 }
1315 // Skip the last annot_pragma_openmp_end.
1316 ConsumeAnnotationToken();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001317 return Actions.ActOnOpenMPAllocateDirective(Loc, Helper.getIdentifiers(),
1318 Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001319 }
1320 break;
1321 }
Kelvin Li1408f912018-09-26 04:28:39 +00001322 case OMPD_requires: {
1323 SourceLocation StartLoc = ConsumeToken();
1324 SmallVector<OMPClause *, 5> Clauses;
1325 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
1326 FirstClauses(OMPC_unknown + 1);
1327 if (Tok.is(tok::annot_pragma_openmp_end)) {
Ilya Biryukovff2a9972019-02-26 11:01:50 +00001328 Diag(Tok, diag::err_omp_expected_clause)
Kelvin Li1408f912018-09-26 04:28:39 +00001329 << getOpenMPDirectiveName(OMPD_requires);
1330 break;
1331 }
1332 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1333 OpenMPClauseKind CKind = Tok.isAnnotation()
1334 ? OMPC_unknown
1335 : getOpenMPClauseKind(PP.getSpelling(Tok));
1336 Actions.StartOpenMPClause(CKind);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001337 OMPClause *Clause = ParseOpenMPClause(OMPD_requires, CKind,
1338 !FirstClauses[CKind].getInt());
1339 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1340 StopBeforeMatch);
Kelvin Li1408f912018-09-26 04:28:39 +00001341 FirstClauses[CKind].setInt(true);
1342 if (Clause != nullptr)
1343 Clauses.push_back(Clause);
1344 if (Tok.is(tok::annot_pragma_openmp_end)) {
1345 Actions.EndOpenMPClause();
1346 break;
1347 }
1348 // Skip ',' if any.
1349 if (Tok.is(tok::comma))
1350 ConsumeToken();
1351 Actions.EndOpenMPClause();
1352 }
1353 // Consume final annot_pragma_openmp_end
1354 if (Clauses.size() == 0) {
1355 Diag(Tok, diag::err_omp_expected_clause)
1356 << getOpenMPDirectiveName(OMPD_requires);
1357 ConsumeAnnotationToken();
1358 return nullptr;
1359 }
1360 ConsumeAnnotationToken();
1361 return Actions.ActOnOpenMPRequiresDirective(StartLoc, Clauses);
1362 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001363 case OMPD_declare_reduction:
1364 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001365 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001366 // The last seen token is annot_pragma_openmp_end - need to check for
1367 // extra tokens.
1368 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1369 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1370 << getOpenMPDirectiveName(OMPD_declare_reduction);
1371 while (Tok.isNot(tok::annot_pragma_openmp_end))
1372 ConsumeAnyToken();
1373 }
1374 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001375 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001376 return Res;
1377 }
1378 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001379 case OMPD_declare_mapper: {
1380 ConsumeToken();
1381 if (DeclGroupPtrTy Res = ParseOpenMPDeclareMapperDirective(AS)) {
1382 // Skip the last annot_pragma_openmp_end.
1383 ConsumeAnnotationToken();
1384 return Res;
1385 }
1386 break;
1387 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001388 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001389 case OMPD_declare_simd: {
1390 // The syntax is:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001391 // { #pragma omp declare {simd|variant} }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001392 // <function-declaration-or-definition>
1393 //
Alexey Bataev2af33e32016-04-07 12:45:37 +00001394 CachedTokens Toks;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001395 Toks.push_back(Tok);
1396 ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001397 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
1398 Toks.push_back(Tok);
1399 ConsumeAnyToken();
1400 }
1401 Toks.push_back(Tok);
1402 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +00001403
1404 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +00001405 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001406 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +00001407 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001408 // Here we expect to see some function declaration.
1409 if (AS == AS_none) {
1410 assert(TagType == DeclSpec::TST_unspecified);
1411 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001412 ParsingDeclSpec PDS(*this);
1413 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1414 } else {
1415 Ptr =
1416 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1417 }
1418 }
1419 if (!Ptr) {
Alexey Bataevd158cf62019-09-13 20:18:17 +00001420 Diag(Loc, diag::err_omp_decl_in_declare_simd_variant)
1421 << (DKind == OMPD_declare_simd ? 0 : 1);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001422 return DeclGroupPtrTy();
1423 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001424 if (DKind == OMPD_declare_simd)
1425 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
1426 assert(DKind == OMPD_declare_variant &&
1427 "Expected declare variant directive only");
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001428 ParseOMPDeclareVariantClauses(Ptr, Toks, Loc);
1429 return Ptr;
Alexey Bataev587e1de2016-03-30 10:43:55 +00001430 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001431 case OMPD_declare_target: {
1432 SourceLocation DTLoc = ConsumeAnyToken();
1433 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Kelvin Lie0502752018-11-21 20:15:57 +00001434 return ParseOMPDeclareTargetClauses();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001435 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001436
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001437 // Skip the last annot_pragma_openmp_end.
1438 ConsumeAnyToken();
1439
1440 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
1441 return DeclGroupPtrTy();
1442
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001443 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +00001444 DKind = parseOpenMPDirectiveKind(*this);
Kelvin Libc38e632018-09-10 02:07:09 +00001445 while (DKind != OMPD_end_declare_target && Tok.isNot(tok::eof) &&
1446 Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +00001447 DeclGroupPtrTy Ptr;
1448 // Here we expect to see some function declaration.
1449 if (AS == AS_none) {
1450 assert(TagType == DeclSpec::TST_unspecified);
1451 MaybeParseCXX11Attributes(Attrs);
1452 ParsingDeclSpec PDS(*this);
1453 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1454 } else {
1455 Ptr =
1456 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1457 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001458 if (Ptr) {
1459 DeclGroupRef Ref = Ptr.get();
1460 Decls.append(Ref.begin(), Ref.end());
1461 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001462 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
1463 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +00001464 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001465 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001466 if (DKind != OMPD_end_declare_target)
1467 TPA.Revert();
1468 else
1469 TPA.Commit();
1470 }
1471 }
1472
Kelvin Lie0502752018-11-21 20:15:57 +00001473 ParseOMPEndDeclareTargetDirective(DKind, DTLoc);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001474 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +00001475 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001476 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001477 case OMPD_unknown:
1478 Diag(Tok, diag::err_omp_unknown_directive);
1479 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001480 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001481 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001482 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +00001483 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001484 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001485 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001486 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +00001487 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001488 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001489 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001490 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001491 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001492 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +00001493 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001494 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001495 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001496 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001497 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001498 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +00001499 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001500 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001501 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001502 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001503 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +00001504 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001505 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001506 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001507 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001508 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001509 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001510 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +00001511 case OMPD_master_taskloop:
Alexey Bataevb8552ab2019-10-18 16:47:35 +00001512 case OMPD_master_taskloop_simd:
Alexey Bataev5bbcead2019-10-14 17:17:41 +00001513 case OMPD_parallel_master_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001514 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001515 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001516 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001517 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +00001518 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001519 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001520 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001521 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001522 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +00001523 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +00001524 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001525 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +00001526 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +00001527 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +00001528 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +00001529 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +00001530 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +00001531 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001532 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +00001533 break;
1534 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001535 while (Tok.isNot(tok::annot_pragma_openmp_end))
1536 ConsumeAnyToken();
1537 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +00001538 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001539}
1540
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001541/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001542///
1543/// threadprivate-directive:
1544/// annot_pragma_openmp 'threadprivate' simple-variable-list
1545/// annot_pragma_openmp_end
1546///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001547/// allocate-directive:
1548/// annot_pragma_openmp 'allocate' simple-variable-list
1549/// annot_pragma_openmp_end
1550///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001551/// declare-reduction-directive:
1552/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
1553/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
1554/// ('omp_priv' '=' <expression>|<function_call>) ')']
1555/// annot_pragma_openmp_end
1556///
Michael Kruse251e1482019-02-01 20:25:04 +00001557/// declare-mapper-directive:
1558/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1559/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1560/// annot_pragma_openmp_end
1561///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001562/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001563/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001564/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
1565/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001566/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +00001567/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Alexey Bataev60e51c42019-10-10 20:13:02 +00001568/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' | 'master
Alexey Bataevb8552ab2019-10-18 16:47:35 +00001569/// taskloop' | 'master taskloop simd' | 'parallel master taskloop' |
1570/// 'distribute' | 'target enter data' | 'target exit data' | 'target
1571/// parallel' | 'target parallel for' | 'target update' | 'distribute
1572/// parallel for' | 'distribute paralle for simd' | 'distribute simd' |
1573/// 'target parallel for simd' | 'target simd' | 'teams distribute' |
1574/// 'teams distribute simd' | 'teams distribute parallel for simd' |
1575/// 'teams distribute parallel for' | 'target teams' | 'target teams
1576/// distribute' | 'target teams distribute parallel for' | 'target teams
1577/// distribute parallel for simd' | 'target teams distribute simd'
1578/// {clause} annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001579///
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001580StmtResult
1581Parser::ParseOpenMPDeclarativeOrExecutableDirective(ParsedStmtContext StmtCtx) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001582 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +00001583 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001584 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001585 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +00001586 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +00001587 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
1588 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +00001589 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +00001590 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001591 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001592 // Name of critical directive.
1593 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001594 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +00001595 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +00001596 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001597
1598 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001599 case OMPD_threadprivate: {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001600 // FIXME: Should this be permitted in C++?
1601 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1602 ParsedStmtContext()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001603 Diag(Tok, diag::err_omp_immediate_directive)
1604 << getOpenMPDirectiveName(DKind) << 0;
1605 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001606 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001607 DeclDirectiveListParserHelper Helper(this, DKind);
1608 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1609 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001610 // The last seen token is annot_pragma_openmp_end - need to check for
1611 // extra tokens.
1612 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1613 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001614 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001615 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001616 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001617 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
1618 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001619 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1620 }
Alp Tokerd751fa72013-12-18 19:10:49 +00001621 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001622 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001623 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001624 case OMPD_allocate: {
1625 // FIXME: Should this be permitted in C++?
1626 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1627 ParsedStmtContext()) {
1628 Diag(Tok, diag::err_omp_immediate_directive)
1629 << getOpenMPDirectiveName(DKind) << 0;
1630 }
1631 ConsumeToken();
1632 DeclDirectiveListParserHelper Helper(this, DKind);
1633 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1634 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001635 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001636 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001637 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1638 OMPC_unknown + 1>
1639 FirstClauses(OMPC_unknown + 1);
1640 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1641 OpenMPClauseKind CKind =
1642 Tok.isAnnotation() ? OMPC_unknown
1643 : getOpenMPClauseKind(PP.getSpelling(Tok));
1644 Actions.StartOpenMPClause(CKind);
1645 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1646 !FirstClauses[CKind].getInt());
1647 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1648 StopBeforeMatch);
1649 FirstClauses[CKind].setInt(true);
1650 if (Clause != nullptr)
1651 Clauses.push_back(Clause);
1652 if (Tok.is(tok::annot_pragma_openmp_end)) {
1653 Actions.EndOpenMPClause();
1654 break;
1655 }
1656 // Skip ',' if any.
1657 if (Tok.is(tok::comma))
1658 ConsumeToken();
1659 Actions.EndOpenMPClause();
1660 }
1661 // The last seen token is annot_pragma_openmp_end - need to check for
1662 // extra tokens.
1663 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1664 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1665 << getOpenMPDirectiveName(DKind);
1666 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1667 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001668 }
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001669 DeclGroupPtrTy Res = Actions.ActOnOpenMPAllocateDirective(
1670 Loc, Helper.getIdentifiers(), Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001671 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1672 }
1673 SkipUntil(tok::annot_pragma_openmp_end);
1674 break;
1675 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001676 case OMPD_declare_reduction:
1677 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001678 if (DeclGroupPtrTy Res =
1679 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001680 // The last seen token is annot_pragma_openmp_end - need to check for
1681 // extra tokens.
1682 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1683 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1684 << getOpenMPDirectiveName(OMPD_declare_reduction);
1685 while (Tok.isNot(tok::annot_pragma_openmp_end))
1686 ConsumeAnyToken();
1687 }
1688 ConsumeAnyToken();
1689 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +00001690 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001691 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +00001692 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001693 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001694 case OMPD_declare_mapper: {
1695 ConsumeToken();
1696 if (DeclGroupPtrTy Res =
1697 ParseOpenMPDeclareMapperDirective(/*AS=*/AS_none)) {
1698 // Skip the last annot_pragma_openmp_end.
1699 ConsumeAnnotationToken();
1700 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1701 } else {
1702 SkipUntil(tok::annot_pragma_openmp_end);
1703 }
1704 break;
1705 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001706 case OMPD_flush:
1707 if (PP.LookAhead(0).is(tok::l_paren)) {
1708 FlushHasClause = true;
1709 // Push copy of the current token back to stream to properly parse
1710 // pseudo-clause OMPFlushClause.
Ilya Biryukov929af672019-05-17 09:32:05 +00001711 PP.EnterToken(Tok, /*IsReinject*/ true);
Alexey Bataev6125da92014-07-21 11:26:11 +00001712 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001713 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +00001714 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001715 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001716 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001717 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001718 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001719 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001720 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +00001721 case OMPD_target_update:
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001722 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1723 ParsedStmtContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00001724 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +00001725 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +00001726 }
1727 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001728 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001729 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001730 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001731 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001732 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001733 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001734 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001735 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001736 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001737 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001738 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001739 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001740 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001741 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001742 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001743 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001744 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001745 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001746 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001747 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001748 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001749 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001750 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001751 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001752 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +00001753 case OMPD_master_taskloop:
Alexey Bataevb8552ab2019-10-18 16:47:35 +00001754 case OMPD_master_taskloop_simd:
Alexey Bataev5bbcead2019-10-14 17:17:41 +00001755 case OMPD_parallel_master_taskloop:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001756 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001757 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001758 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001759 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001760 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001761 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001762 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001763 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001764 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001765 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001766 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001767 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001768 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001769 case OMPD_target_teams_distribute_parallel_for_simd:
1770 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001771 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001772 // Parse directive name of the 'critical' directive if any.
1773 if (DKind == OMPD_critical) {
1774 BalancedDelimiterTracker T(*this, tok::l_paren,
1775 tok::annot_pragma_openmp_end);
1776 if (!T.consumeOpen()) {
1777 if (Tok.isAnyIdentifier()) {
1778 DirName =
1779 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1780 ConsumeAnyToken();
1781 } else {
1782 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1783 }
1784 T.consumeClose();
1785 }
Alexey Bataev80909872015-07-02 11:25:17 +00001786 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001787 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001788 if (Tok.isNot(tok::annot_pragma_openmp_end))
1789 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001790 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001791
Alexey Bataevf29276e2014-06-18 04:14:57 +00001792 if (isOpenMPLoopDirective(DKind))
1793 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1794 if (isOpenMPSimdDirective(DKind))
1795 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1796 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001797 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001798
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001799 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001800 OpenMPClauseKind CKind =
1801 Tok.isAnnotation()
1802 ? OMPC_unknown
1803 : FlushHasClause ? OMPC_flush
1804 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001805 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001806 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001807 OMPClause *Clause =
1808 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001809 FirstClauses[CKind].setInt(true);
1810 if (Clause) {
1811 FirstClauses[CKind].setPointer(Clause);
1812 Clauses.push_back(Clause);
1813 }
1814
1815 // Skip ',' if any.
1816 if (Tok.is(tok::comma))
1817 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001818 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001819 }
1820 // End location of the directive.
1821 EndLoc = Tok.getLocation();
1822 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001823 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001824
Alexey Bataeveb482352015-12-18 05:05:56 +00001825 // OpenMP [2.13.8, ordered Construct, Syntax]
1826 // If the depend clause is specified, the ordered construct is a stand-alone
1827 // directive.
1828 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001829 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1830 ParsedStmtContext()) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001831 Diag(Loc, diag::err_omp_immediate_directive)
1832 << getOpenMPDirectiveName(DKind) << 1
1833 << getOpenMPClauseName(OMPC_depend);
1834 }
1835 HasAssociatedStatement = false;
1836 }
1837
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001838 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001839 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001840 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001841 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001842 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1843 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1844 // should have at least one compound statement scope within it.
1845 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001846 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001847 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1848 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001849 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001850 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1851 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1852 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001853 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001854 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001855 Directive = Actions.ActOnOpenMPExecutableDirective(
1856 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1857 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001858
1859 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001860 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001861 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001862 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001863 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001864 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001865 case OMPD_declare_target:
1866 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00001867 case OMPD_requires:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001868 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001869 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001870 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001871 SkipUntil(tok::annot_pragma_openmp_end);
1872 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001873 case OMPD_unknown:
1874 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001875 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001876 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001877 }
1878 return Directive;
1879}
1880
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001881// Parses simple list:
1882// simple-variable-list:
1883// '(' id-expression {, id-expression} ')'
1884//
1885bool Parser::ParseOpenMPSimpleVarList(
1886 OpenMPDirectiveKind Kind,
1887 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1888 Callback,
1889 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001890 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001891 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001892 if (T.expectAndConsume(diag::err_expected_lparen_after,
1893 getOpenMPDirectiveName(Kind)))
1894 return true;
1895 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001896 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001897
1898 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001899 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001900 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001901 UnqualifiedId Name;
1902 // Read var name.
1903 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001904 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001905
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001906 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001907 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001908 IsCorrect = false;
1909 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001910 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001911 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00001912 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001913 IsCorrect = false;
1914 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001915 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001916 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1917 Tok.isNot(tok::annot_pragma_openmp_end)) {
1918 IsCorrect = false;
1919 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001920 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001921 Diag(PrevTok.getLocation(), diag::err_expected)
1922 << tok::identifier
1923 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001924 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001925 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001926 }
1927 // Consume ','.
1928 if (Tok.is(tok::comma)) {
1929 ConsumeToken();
1930 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001931 }
1932
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001933 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001934 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001935 IsCorrect = false;
1936 }
1937
1938 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001939 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001940
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001941 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001942}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001943
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001944/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001945///
1946/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001947/// if-clause | final-clause | num_threads-clause | safelen-clause |
1948/// default-clause | private-clause | firstprivate-clause | shared-clause
1949/// | linear-clause | aligned-clause | collapse-clause |
1950/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001951/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001952/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001953/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001954/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001955/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001956/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001957/// from-clause | is_device_ptr-clause | task_reduction-clause |
Alexey Bataeve04483e2019-03-27 14:14:31 +00001958/// in_reduction-clause | allocator-clause | allocate-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001959///
1960OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1961 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001962 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001963 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001964 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001965 // Check if clause is allowed for the given directive.
1966 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001967 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1968 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001969 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001970 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001971 }
1972
1973 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001974 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001975 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001976 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001977 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001978 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001979 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001980 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001981 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001982 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001983 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001984 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001985 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001986 case OMPC_hint:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001987 case OMPC_allocator:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001988 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001989 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001990 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001991 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001992 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001993 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001994 // OpenMP [2.9.1, target data construct, Restrictions]
1995 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001996 // OpenMP [2.11.1, task Construct, Restrictions]
1997 // At most one if clause can appear on the directive.
1998 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001999 // OpenMP [teams Construct, Restrictions]
2000 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00002001 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00002002 // OpenMP [2.9.1, task Construct, Restrictions]
2003 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002004 // OpenMP [2.9.2, taskloop Construct, Restrictions]
2005 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00002006 // OpenMP [2.9.2, taskloop Construct, Restrictions]
2007 // At most one num_tasks clause can appear on the directive.
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002008 // OpenMP [2.11.3, allocate Directive, Restrictions]
2009 // At most one allocator clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002010 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002011 Diag(Tok, diag::err_omp_more_one_clause)
2012 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002013 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002014 }
2015
Alexey Bataev10e775f2015-07-30 11:36:16 +00002016 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002017 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00002018 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002019 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002020 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002021 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002022 case OMPC_proc_bind:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00002023 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002024 // OpenMP [2.14.3.1, Restrictions]
2025 // Only a single default clause may be specified on a parallel, task or
2026 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002027 // OpenMP [2.5, parallel Construct, Restrictions]
2028 // At most one proc_bind clause can appear on the directive.
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00002029 // OpenMP [5.0, Requires directive, Restrictions]
2030 // At most one atomic_default_mem_order clause can appear
2031 // on the directive
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002032 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002033 Diag(Tok, diag::err_omp_more_one_clause)
2034 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002035 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002036 }
2037
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002038 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002039 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002040 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00002041 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002042 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002043 // OpenMP [2.7.1, Restrictions, p. 3]
2044 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002045 // OpenMP [2.10.4, Restrictions, p. 106]
2046 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00002047 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002048 Diag(Tok, diag::err_omp_more_one_clause)
2049 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002050 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002051 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00002052 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002053
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002054 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002055 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002056 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002057 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002058 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002059 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002060 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002061 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00002062 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00002063 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002064 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00002065 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002066 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00002067 case OMPC_nogroup:
Kelvin Li1408f912018-09-26 04:28:39 +00002068 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00002069 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00002070 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00002071 case OMPC_dynamic_allocators:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002072 // OpenMP [2.7.1, Restrictions, p. 9]
2073 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00002074 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
2075 // Only one nowait clause can appear on a for directive.
Kelvin Li1408f912018-09-26 04:28:39 +00002076 // OpenMP [5.0, Requires directive, Restrictions]
2077 // Each of the requires clauses can appear at most once on the directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002078 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002079 Diag(Tok, diag::err_omp_more_one_clause)
2080 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002081 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002082 }
2083
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002084 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002085 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002086 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002087 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002088 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002089 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002090 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00002091 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00002092 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002093 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002094 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002095 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002096 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002097 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002098 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00002099 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00002100 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00002101 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00002102 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00002103 case OMPC_is_device_ptr:
Alexey Bataeve04483e2019-03-27 14:14:31 +00002104 case OMPC_allocate:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002105 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002106 break;
Alexey Bataev729e2422019-08-23 16:11:14 +00002107 case OMPC_device_type:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002108 case OMPC_unknown:
2109 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00002110 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00002111 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002112 break;
2113 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002114 case OMPC_uniform:
Alexey Bataevdba792c2019-09-23 18:13:31 +00002115 case OMPC_match:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002116 if (!WrongDirective)
2117 Diag(Tok, diag::err_omp_unexpected_clause)
2118 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00002119 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002120 break;
2121 }
Craig Topper161e4db2014-05-21 06:02:52 +00002122 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002123}
2124
Alexey Bataev2af33e32016-04-07 12:45:37 +00002125/// Parses simple expression in parens for single-expression clauses of OpenMP
2126/// constructs.
2127/// \param RLoc Returned location of right paren.
2128ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
Alexey Bataevd158cf62019-09-13 20:18:17 +00002129 SourceLocation &RLoc,
2130 bool IsAddressOfOperand) {
Alexey Bataev2af33e32016-04-07 12:45:37 +00002131 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2132 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
2133 return ExprError();
2134
2135 SourceLocation ELoc = Tok.getLocation();
2136 ExprResult LHS(ParseCastExpression(
Alexey Bataevd158cf62019-09-13 20:18:17 +00002137 /*isUnaryExpression=*/false, IsAddressOfOperand, NotTypeCast));
Alexey Bataev2af33e32016-04-07 12:45:37 +00002138 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002139 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev2af33e32016-04-07 12:45:37 +00002140
2141 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002142 RLoc = Tok.getLocation();
2143 if (!T.consumeClose())
2144 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00002145
Alexey Bataev2af33e32016-04-07 12:45:37 +00002146 return Val;
2147}
2148
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002149/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00002150/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00002151/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002152///
Alexey Bataev3778b602014-07-17 07:32:53 +00002153/// final-clause:
2154/// 'final' '(' expression ')'
2155///
Alexey Bataev62c87d22014-03-21 04:51:18 +00002156/// num_threads-clause:
2157/// 'num_threads' '(' expression ')'
2158///
2159/// safelen-clause:
2160/// 'safelen' '(' expression ')'
2161///
Alexey Bataev66b15b52015-08-21 11:14:16 +00002162/// simdlen-clause:
2163/// 'simdlen' '(' expression ')'
2164///
Alexander Musman8bd31e62014-05-27 15:12:19 +00002165/// collapse-clause:
2166/// 'collapse' '(' expression ')'
2167///
Alexey Bataeva0569352015-12-01 10:17:31 +00002168/// priority-clause:
2169/// 'priority' '(' expression ')'
2170///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002171/// grainsize-clause:
2172/// 'grainsize' '(' expression ')'
2173///
Alexey Bataev382967a2015-12-08 12:06:20 +00002174/// num_tasks-clause:
2175/// 'num_tasks' '(' expression ')'
2176///
Alexey Bataev28c75412015-12-15 08:19:24 +00002177/// hint-clause:
2178/// 'hint' '(' expression ')'
2179///
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002180/// allocator-clause:
2181/// 'allocator' '(' expression ')'
2182///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002183OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
2184 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002185 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00002186 SourceLocation LLoc = Tok.getLocation();
2187 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002188
Alexey Bataev2af33e32016-04-07 12:45:37 +00002189 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002190
2191 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00002192 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002193
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002194 if (ParseOnly)
2195 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00002196 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002197}
2198
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002199/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002200///
2201/// default-clause:
2202/// 'default' '(' 'none' | 'shared' ')
2203///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002204/// proc_bind-clause:
2205/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
2206///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002207OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
2208 bool ParseOnly) {
Alexey Bataev729e2422019-08-23 16:11:14 +00002209 llvm::Optional<SimpleClauseData> Val = parseOpenMPSimpleClause(*this, Kind);
2210 if (!Val || ParseOnly)
Craig Topper161e4db2014-05-21 06:02:52 +00002211 return nullptr;
Alexey Bataev729e2422019-08-23 16:11:14 +00002212 return Actions.ActOnOpenMPSimpleClause(
2213 Kind, Val.getValue().Type, Val.getValue().TypeLoc, Val.getValue().LOpen,
2214 Val.getValue().Loc, Val.getValue().RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002215}
2216
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002217/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002218///
2219/// ordered-clause:
2220/// 'ordered'
2221///
Alexey Bataev236070f2014-06-20 11:19:47 +00002222/// nowait-clause:
2223/// 'nowait'
2224///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002225/// untied-clause:
2226/// 'untied'
2227///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002228/// mergeable-clause:
2229/// 'mergeable'
2230///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002231/// read-clause:
2232/// 'read'
2233///
Alexey Bataev346265e2015-09-25 10:37:12 +00002234/// threads-clause:
2235/// 'threads'
2236///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002237/// simd-clause:
2238/// 'simd'
2239///
Alexey Bataevb825de12015-12-07 10:51:44 +00002240/// nogroup-clause:
2241/// 'nogroup'
2242///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002243OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002244 SourceLocation Loc = Tok.getLocation();
2245 ConsumeAnyToken();
2246
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002247 if (ParseOnly)
2248 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002249 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
2250}
2251
2252
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002253/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00002254/// argument like 'schedule' or 'dist_schedule'.
2255///
2256/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00002257/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
2258/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00002259///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002260/// if-clause:
2261/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
2262///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002263/// defaultmap:
2264/// 'defaultmap' '(' modifier ':' kind ')'
2265///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002266OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
2267 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00002268 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002269 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002270 // Parse '('.
2271 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2272 if (T.expectAndConsume(diag::err_expected_lparen_after,
2273 getOpenMPClauseName(Kind)))
2274 return nullptr;
2275
2276 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002277 SmallVector<unsigned, 4> Arg;
2278 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002279 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00002280 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
2281 Arg.resize(NumberOfElements);
2282 KLoc.resize(NumberOfElements);
2283 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
2284 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
2285 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00002286 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002287 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00002288 if (KindModifier > OMPC_SCHEDULE_unknown) {
2289 // Parse 'modifier'
2290 Arg[Modifier1] = KindModifier;
2291 KLoc[Modifier1] = Tok.getLocation();
2292 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2293 Tok.isNot(tok::annot_pragma_openmp_end))
2294 ConsumeAnyToken();
2295 if (Tok.is(tok::comma)) {
2296 // Parse ',' 'modifier'
2297 ConsumeAnyToken();
2298 KindModifier = getOpenMPSimpleClauseType(
2299 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2300 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
2301 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00002302 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002303 KLoc[Modifier2] = Tok.getLocation();
2304 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2305 Tok.isNot(tok::annot_pragma_openmp_end))
2306 ConsumeAnyToken();
2307 }
2308 // Parse ':'
2309 if (Tok.is(tok::colon))
2310 ConsumeAnyToken();
2311 else
2312 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
2313 KindModifier = getOpenMPSimpleClauseType(
2314 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2315 }
2316 Arg[ScheduleKind] = KindModifier;
2317 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002318 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2319 Tok.isNot(tok::annot_pragma_openmp_end))
2320 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00002321 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
2322 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
2323 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002324 Tok.is(tok::comma))
2325 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00002326 } else if (Kind == OMPC_dist_schedule) {
2327 Arg.push_back(getOpenMPSimpleClauseType(
2328 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2329 KLoc.push_back(Tok.getLocation());
2330 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2331 Tok.isNot(tok::annot_pragma_openmp_end))
2332 ConsumeAnyToken();
2333 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
2334 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002335 } else if (Kind == OMPC_defaultmap) {
2336 // Get a defaultmap modifier
2337 Arg.push_back(getOpenMPSimpleClauseType(
2338 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2339 KLoc.push_back(Tok.getLocation());
2340 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2341 Tok.isNot(tok::annot_pragma_openmp_end))
2342 ConsumeAnyToken();
2343 // Parse ':'
2344 if (Tok.is(tok::colon))
2345 ConsumeAnyToken();
2346 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
2347 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
2348 // Get a defaultmap kind
2349 Arg.push_back(getOpenMPSimpleClauseType(
2350 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2351 KLoc.push_back(Tok.getLocation());
2352 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2353 Tok.isNot(tok::annot_pragma_openmp_end))
2354 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002355 } else {
2356 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00002357 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002358 TentativeParsingAction TPA(*this);
Alexey Bataev61908f652018-04-23 19:53:05 +00002359 Arg.push_back(parseOpenMPDirectiveKind(*this));
Alexey Bataev6402bca2015-12-28 07:25:51 +00002360 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002361 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002362 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
2363 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002364 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002365 } else {
2366 TPA.Revert();
2367 Arg.back() = OMPD_unknown;
2368 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002369 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002370 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00002371 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002372 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00002373
Carlo Bertollib4adf552016-01-15 18:50:31 +00002374 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
2375 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
2376 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002377 if (NeedAnExpression) {
2378 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002379 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
2380 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002381 Val =
2382 Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002383 }
2384
2385 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002386 SourceLocation RLoc = Tok.getLocation();
2387 if (!T.consumeClose())
2388 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002389
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002390 if (NeedAnExpression && Val.isInvalid())
2391 return nullptr;
2392
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002393 if (ParseOnly)
2394 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002395 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002396 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002397}
2398
Alexey Bataevc5e02582014-06-16 07:08:35 +00002399static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
2400 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002401 if (ReductionIdScopeSpec.isEmpty()) {
2402 auto OOK = OO_None;
2403 switch (P.getCurToken().getKind()) {
2404 case tok::plus:
2405 OOK = OO_Plus;
2406 break;
2407 case tok::minus:
2408 OOK = OO_Minus;
2409 break;
2410 case tok::star:
2411 OOK = OO_Star;
2412 break;
2413 case tok::amp:
2414 OOK = OO_Amp;
2415 break;
2416 case tok::pipe:
2417 OOK = OO_Pipe;
2418 break;
2419 case tok::caret:
2420 OOK = OO_Caret;
2421 break;
2422 case tok::ampamp:
2423 OOK = OO_AmpAmp;
2424 break;
2425 case tok::pipepipe:
2426 OOK = OO_PipePipe;
2427 break;
2428 default:
2429 break;
2430 }
2431 if (OOK != OO_None) {
2432 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00002433 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00002434 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
2435 return false;
2436 }
2437 }
2438 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
2439 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00002440 /*AllowConstructorName*/ false,
2441 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00002442 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002443}
2444
Kelvin Lief579432018-12-18 22:18:41 +00002445/// Checks if the token is a valid map-type-modifier.
2446static OpenMPMapModifierKind isMapModifier(Parser &P) {
2447 Token Tok = P.getCurToken();
2448 if (!Tok.is(tok::identifier))
2449 return OMPC_MAP_MODIFIER_unknown;
2450
2451 Preprocessor &PP = P.getPreprocessor();
2452 OpenMPMapModifierKind TypeModifier = static_cast<OpenMPMapModifierKind>(
2453 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2454 return TypeModifier;
2455}
2456
Michael Kruse01f670d2019-02-22 22:29:42 +00002457/// Parse the mapper modifier in map, to, and from clauses.
2458bool Parser::parseMapperModifier(OpenMPVarListDataTy &Data) {
2459 // Parse '('.
2460 BalancedDelimiterTracker T(*this, tok::l_paren, tok::colon);
2461 if (T.expectAndConsume(diag::err_expected_lparen_after, "mapper")) {
2462 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2463 StopBeforeMatch);
2464 return true;
2465 }
2466 // Parse mapper-identifier
2467 if (getLangOpts().CPlusPlus)
2468 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
2469 /*ObjectType=*/nullptr,
2470 /*EnteringContext=*/false);
2471 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
2472 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
2473 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2474 StopBeforeMatch);
2475 return true;
2476 }
2477 auto &DeclNames = Actions.getASTContext().DeclarationNames;
2478 Data.ReductionOrMapperId = DeclarationNameInfo(
2479 DeclNames.getIdentifier(Tok.getIdentifierInfo()), Tok.getLocation());
2480 ConsumeToken();
2481 // Parse ')'.
2482 return T.consumeClose();
2483}
2484
Kelvin Lief579432018-12-18 22:18:41 +00002485/// Parse map-type-modifiers in map clause.
2486/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002487/// where, map-type-modifier ::= always | close | mapper(mapper-identifier)
2488bool Parser::parseMapTypeModifiers(OpenMPVarListDataTy &Data) {
2489 while (getCurToken().isNot(tok::colon)) {
2490 OpenMPMapModifierKind TypeModifier = isMapModifier(*this);
Kelvin Lief579432018-12-18 22:18:41 +00002491 if (TypeModifier == OMPC_MAP_MODIFIER_always ||
2492 TypeModifier == OMPC_MAP_MODIFIER_close) {
2493 Data.MapTypeModifiers.push_back(TypeModifier);
2494 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
Michael Kruse4304e9d2019-02-19 16:38:20 +00002495 ConsumeToken();
2496 } else if (TypeModifier == OMPC_MAP_MODIFIER_mapper) {
2497 Data.MapTypeModifiers.push_back(TypeModifier);
2498 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
2499 ConsumeToken();
Michael Kruse01f670d2019-02-22 22:29:42 +00002500 if (parseMapperModifier(Data))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002501 return true;
Kelvin Lief579432018-12-18 22:18:41 +00002502 } else {
2503 // For the case of unknown map-type-modifier or a map-type.
2504 // Map-type is followed by a colon; the function returns when it
2505 // encounters a token followed by a colon.
2506 if (Tok.is(tok::comma)) {
Michael Kruse4304e9d2019-02-19 16:38:20 +00002507 Diag(Tok, diag::err_omp_map_type_modifier_missing);
2508 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002509 continue;
2510 }
2511 // Potential map-type token as it is followed by a colon.
2512 if (PP.LookAhead(0).is(tok::colon))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002513 return false;
2514 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
2515 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002516 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002517 if (getCurToken().is(tok::comma))
2518 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002519 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002520 return false;
Kelvin Lief579432018-12-18 22:18:41 +00002521}
2522
2523/// Checks if the token is a valid map-type.
2524static OpenMPMapClauseKind isMapType(Parser &P) {
2525 Token Tok = P.getCurToken();
2526 // The map-type token can be either an identifier or the C++ delete keyword.
2527 if (!Tok.isOneOf(tok::identifier, tok::kw_delete))
2528 return OMPC_MAP_unknown;
2529 Preprocessor &PP = P.getPreprocessor();
2530 OpenMPMapClauseKind MapType = static_cast<OpenMPMapClauseKind>(
2531 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2532 return MapType;
2533}
2534
2535/// Parse map-type in map clause.
2536/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002537/// where, map-type ::= to | from | tofrom | alloc | release | delete
Kelvin Lief579432018-12-18 22:18:41 +00002538static void parseMapType(Parser &P, Parser::OpenMPVarListDataTy &Data) {
2539 Token Tok = P.getCurToken();
2540 if (Tok.is(tok::colon)) {
2541 P.Diag(Tok, diag::err_omp_map_type_missing);
2542 return;
2543 }
2544 Data.MapType = isMapType(P);
2545 if (Data.MapType == OMPC_MAP_unknown)
2546 P.Diag(Tok, diag::err_omp_unknown_map_type);
2547 P.ConsumeToken();
2548}
2549
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002550/// Parses clauses with list.
2551bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
2552 OpenMPClauseKind Kind,
2553 SmallVectorImpl<Expr *> &Vars,
2554 OpenMPVarListDataTy &Data) {
2555 UnqualifiedId UnqualifiedReductionId;
2556 bool InvalidReductionId = false;
Michael Kruse01f670d2019-02-22 22:29:42 +00002557 bool IsInvalidMapperModifier = false;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002558
2559 // Parse '('.
2560 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2561 if (T.expectAndConsume(diag::err_expected_lparen_after,
2562 getOpenMPClauseName(Kind)))
2563 return true;
2564
2565 bool NeedRParenForLinear = false;
2566 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
2567 tok::annot_pragma_openmp_end);
2568 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00002569 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
2570 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002571 ColonProtectionRAIIObject ColonRAII(*this);
2572 if (getLangOpts().CPlusPlus)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002573 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002574 /*ObjectType=*/nullptr,
2575 /*EnteringContext=*/false);
Michael Kruse4304e9d2019-02-19 16:38:20 +00002576 InvalidReductionId = ParseReductionId(
2577 *this, Data.ReductionOrMapperIdScopeSpec, UnqualifiedReductionId);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002578 if (InvalidReductionId) {
2579 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2580 StopBeforeMatch);
2581 }
2582 if (Tok.is(tok::colon))
2583 Data.ColonLoc = ConsumeToken();
2584 else
2585 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
2586 if (!InvalidReductionId)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002587 Data.ReductionOrMapperId =
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002588 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
2589 } else if (Kind == OMPC_depend) {
2590 // Handle dependency type for depend clause.
2591 ColonProtectionRAIIObject ColonRAII(*this);
2592 Data.DepKind =
2593 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
2594 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
2595 Data.DepLinMapLoc = Tok.getLocation();
2596
2597 if (Data.DepKind == OMPC_DEPEND_unknown) {
2598 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2599 StopBeforeMatch);
2600 } else {
2601 ConsumeToken();
2602 // Special processing for depend(source) clause.
2603 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
2604 // Parse ')'.
2605 T.consumeClose();
2606 return false;
2607 }
2608 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002609 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002610 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00002611 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002612 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
2613 : diag::warn_pragma_expected_colon)
2614 << "dependency type";
2615 }
2616 } else if (Kind == OMPC_linear) {
2617 // Try to parse modifier if any.
2618 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
2619 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
2620 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2621 Data.DepLinMapLoc = ConsumeToken();
2622 LinearT.consumeOpen();
2623 NeedRParenForLinear = true;
2624 }
2625 } else if (Kind == OMPC_map) {
2626 // Handle map type for map clause.
2627 ColonProtectionRAIIObject ColonRAII(*this);
2628
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002629 // The first identifier may be a list item, a map-type or a
Kelvin Lief579432018-12-18 22:18:41 +00002630 // map-type-modifier. The map-type can also be delete which has the same
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002631 // spelling of the C++ delete keyword.
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002632 Data.DepLinMapLoc = Tok.getLocation();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002633
Kelvin Lief579432018-12-18 22:18:41 +00002634 // Check for presence of a colon in the map clause.
2635 TentativeParsingAction TPA(*this);
2636 bool ColonPresent = false;
2637 if (SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2638 StopBeforeMatch)) {
2639 if (Tok.is(tok::colon))
2640 ColonPresent = true;
2641 }
2642 TPA.Revert();
2643 // Only parse map-type-modifier[s] and map-type if a colon is present in
2644 // the map clause.
2645 if (ColonPresent) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002646 IsInvalidMapperModifier = parseMapTypeModifiers(Data);
2647 if (!IsInvalidMapperModifier)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002648 parseMapType(*this, Data);
Michael Kruse01f670d2019-02-22 22:29:42 +00002649 else
2650 SkipUntil(tok::colon, tok::annot_pragma_openmp_end, StopBeforeMatch);
Kelvin Lief579432018-12-18 22:18:41 +00002651 }
2652 if (Data.MapType == OMPC_MAP_unknown) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002653 Data.MapType = OMPC_MAP_tofrom;
2654 Data.IsMapTypeImplicit = true;
2655 }
2656
2657 if (Tok.is(tok::colon))
2658 Data.ColonLoc = ConsumeToken();
Michael Kruse0336c752019-02-25 20:34:15 +00002659 } else if (Kind == OMPC_to || Kind == OMPC_from) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002660 if (Tok.is(tok::identifier)) {
2661 bool IsMapperModifier = false;
Michael Kruse0336c752019-02-25 20:34:15 +00002662 if (Kind == OMPC_to) {
2663 auto Modifier = static_cast<OpenMPToModifierKind>(
2664 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2665 if (Modifier == OMPC_TO_MODIFIER_mapper)
2666 IsMapperModifier = true;
2667 } else {
2668 auto Modifier = static_cast<OpenMPFromModifierKind>(
2669 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2670 if (Modifier == OMPC_FROM_MODIFIER_mapper)
2671 IsMapperModifier = true;
2672 }
Michael Kruse01f670d2019-02-22 22:29:42 +00002673 if (IsMapperModifier) {
2674 // Parse the mapper modifier.
2675 ConsumeToken();
2676 IsInvalidMapperModifier = parseMapperModifier(Data);
2677 if (Tok.isNot(tok::colon)) {
2678 if (!IsInvalidMapperModifier)
2679 Diag(Tok, diag::warn_pragma_expected_colon) << ")";
2680 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2681 StopBeforeMatch);
2682 }
2683 // Consume ':'.
2684 if (Tok.is(tok::colon))
2685 ConsumeToken();
2686 }
2687 }
Alexey Bataeve04483e2019-03-27 14:14:31 +00002688 } else if (Kind == OMPC_allocate) {
2689 // Handle optional allocator expression followed by colon delimiter.
2690 ColonProtectionRAIIObject ColonRAII(*this);
2691 TentativeParsingAction TPA(*this);
2692 ExprResult Tail =
2693 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
2694 Tail = Actions.ActOnFinishFullExpr(Tail.get(), T.getOpenLocation(),
2695 /*DiscardedValue=*/false);
2696 if (Tail.isUsable()) {
2697 if (Tok.is(tok::colon)) {
2698 Data.TailExpr = Tail.get();
2699 Data.ColonLoc = ConsumeToken();
2700 TPA.Commit();
2701 } else {
2702 // colon not found, no allocator specified, parse only list of
2703 // variables.
2704 TPA.Revert();
2705 }
2706 } else {
2707 // Parsing was unsuccessfull, revert and skip to the end of clause or
2708 // directive.
2709 TPA.Revert();
2710 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2711 StopBeforeMatch);
2712 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002713 }
2714
Alexey Bataevfa312f32017-07-21 18:48:21 +00002715 bool IsComma =
2716 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
2717 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
2718 (Kind == OMPC_reduction && !InvalidReductionId) ||
Kelvin Lida6bc702018-11-21 19:38:53 +00002719 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown) ||
Alexey Bataevfa312f32017-07-21 18:48:21 +00002720 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002721 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
2722 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
2723 Tok.isNot(tok::annot_pragma_openmp_end))) {
2724 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
2725 // Parse variable
2726 ExprResult VarExpr =
2727 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00002728 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002729 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00002730 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002731 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2732 StopBeforeMatch);
2733 }
2734 // Skip ',' if any
2735 IsComma = Tok.is(tok::comma);
2736 if (IsComma)
2737 ConsumeToken();
2738 else if (Tok.isNot(tok::r_paren) &&
2739 Tok.isNot(tok::annot_pragma_openmp_end) &&
2740 (!MayHaveTail || Tok.isNot(tok::colon)))
2741 Diag(Tok, diag::err_omp_expected_punc)
2742 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
2743 : getOpenMPClauseName(Kind))
2744 << (Kind == OMPC_flush);
2745 }
2746
2747 // Parse ')' for linear clause with modifier.
2748 if (NeedRParenForLinear)
2749 LinearT.consumeClose();
2750
2751 // Parse ':' linear-step (or ':' alignment).
2752 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
2753 if (MustHaveTail) {
2754 Data.ColonLoc = Tok.getLocation();
2755 SourceLocation ELoc = ConsumeToken();
2756 ExprResult Tail = ParseAssignmentExpression();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002757 Tail =
2758 Actions.ActOnFinishFullExpr(Tail.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002759 if (Tail.isUsable())
2760 Data.TailExpr = Tail.get();
2761 else
2762 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2763 StopBeforeMatch);
2764 }
2765
2766 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002767 Data.RLoc = Tok.getLocation();
2768 if (!T.consumeClose())
2769 Data.RLoc = T.getCloseLocation();
Alexey Bataev61908f652018-04-23 19:53:05 +00002770 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
2771 Vars.empty()) ||
2772 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
Michael Kruse4304e9d2019-02-19 16:38:20 +00002773 (MustHaveTail && !Data.TailExpr) || InvalidReductionId ||
Michael Kruse01f670d2019-02-22 22:29:42 +00002774 IsInvalidMapperModifier;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002775}
2776
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002777/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00002778/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
2779/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002780///
2781/// private-clause:
2782/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002783/// firstprivate-clause:
2784/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00002785/// lastprivate-clause:
2786/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00002787/// shared-clause:
2788/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00002789/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00002790/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002791/// aligned-clause:
2792/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00002793/// reduction-clause:
2794/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00002795/// task_reduction-clause:
2796/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00002797/// in_reduction-clause:
2798/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00002799/// copyprivate-clause:
2800/// 'copyprivate' '(' list ')'
2801/// flush-clause:
2802/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002803/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00002804/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00002805/// map-clause:
Kelvin Lief579432018-12-18 22:18:41 +00002806/// 'map' '(' [ [ always [,] ] [ close [,] ]
Michael Kruse01f670d2019-02-22 22:29:42 +00002807/// [ mapper '(' mapper-identifier ')' [,] ]
Kelvin Li0bff7af2015-11-23 05:32:03 +00002808/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00002809/// to-clause:
Michael Kruse01f670d2019-02-22 22:29:42 +00002810/// 'to' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00002811/// from-clause:
Michael Kruse0336c752019-02-25 20:34:15 +00002812/// 'from' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00002813/// use_device_ptr-clause:
2814/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00002815/// is_device_ptr-clause:
2816/// 'is_device_ptr' '(' list ')'
Alexey Bataeve04483e2019-03-27 14:14:31 +00002817/// allocate-clause:
2818/// 'allocate' '(' [ allocator ':' ] list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002819///
Alexey Bataev182227b2015-08-20 10:54:39 +00002820/// For 'linear' clause linear-list may have the following forms:
2821/// list
2822/// modifier(list)
2823/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00002824OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002825 OpenMPClauseKind Kind,
2826 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002827 SourceLocation Loc = Tok.getLocation();
2828 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002829 SmallVector<Expr *, 4> Vars;
2830 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002831
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002832 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00002833 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002834
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002835 if (ParseOnly)
2836 return nullptr;
Michael Kruse4304e9d2019-02-19 16:38:20 +00002837 OMPVarListLocTy Locs(Loc, LOpen, Data.RLoc);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002838 return Actions.ActOnOpenMPVarListClause(
Michael Kruse4304e9d2019-02-19 16:38:20 +00002839 Kind, Vars, Data.TailExpr, Locs, Data.ColonLoc,
2840 Data.ReductionOrMapperIdScopeSpec, Data.ReductionOrMapperId, Data.DepKind,
2841 Data.LinKind, Data.MapTypeModifiers, Data.MapTypeModifiersLoc,
2842 Data.MapType, Data.IsMapTypeImplicit, Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002843}
2844