blob: 1eca15496ee9119cbeb6a864decc614e58bb3f77 [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 Bataev93dc40d2019-12-20 11:04:57 -050015#include "clang/Basic/OpenMPKinds.h"
Alexey Bataev1236eb62020-03-23 17:30:38 -040016#include "clang/Basic/TokenKinds.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000017#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000018#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000019#include "clang/Parse/RAIIObjectsForParser.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000020#include "clang/Sema/Scope.h"
21#include "llvm/ADT/PointerIntPair.h"
Alexey Bataev4513e93f2019-10-10 15:15:26 +000022#include "llvm/ADT/UniqueVector.h"
Johannes Doerfert1228d422019-12-19 20:42:12 -060023#include "llvm/Frontend/OpenMP/OMPContext.h"
Michael Wong65f367f2015-07-21 13:44:28 +000024
Alexey Bataeva769e072013-03-22 06:34:35 +000025using namespace clang;
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060026using namespace llvm::omp;
Alexey Bataeva769e072013-03-22 06:34:35 +000027
28//===----------------------------------------------------------------------===//
29// OpenMP declarative directives.
30//===----------------------------------------------------------------------===//
31
Dmitry Polukhin82478332016-02-13 06:53:38 +000032namespace {
33enum OpenMPDirectiveKindEx {
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060034 OMPD_cancellation = unsigned(OMPD_unknown) + 1,
Dmitry Polukhin82478332016-02-13 06:53:38 +000035 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000036 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000037 OMPD_end,
38 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000039 OMPD_enter,
40 OMPD_exit,
41 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000042 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000043 OMPD_target_enter,
Samuel Antao686c70c2016-05-26 17:30:50 +000044 OMPD_target_exit,
45 OMPD_update,
Kelvin Li579e41c2016-11-30 23:51:03 +000046 OMPD_distribute_parallel,
Kelvin Li80e8f562016-12-29 22:16:30 +000047 OMPD_teams_distribute_parallel,
Michael Kruse251e1482019-02-01 20:25:04 +000048 OMPD_target_teams_distribute_parallel,
49 OMPD_mapper,
Alexey Bataevd158cf62019-09-13 20:18:17 +000050 OMPD_variant,
Johannes Doerfert095cecb2020-02-20 19:50:47 -060051 OMPD_begin,
52 OMPD_begin_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000053};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000054
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060055// Helper to unify the enum class OpenMPDirectiveKind with its extension
56// the OpenMPDirectiveKindEx enum which allows to use them together as if they
57// are unsigned values.
58struct OpenMPDirectiveKindExWrapper {
59 OpenMPDirectiveKindExWrapper(unsigned Value) : Value(Value) {}
60 OpenMPDirectiveKindExWrapper(OpenMPDirectiveKind DK) : Value(unsigned(DK)) {}
61 bool operator==(OpenMPDirectiveKind V) const { return Value == unsigned(V); }
62 bool operator!=(OpenMPDirectiveKind V) const { return Value != unsigned(V); }
63 bool operator<(OpenMPDirectiveKind V) const { return Value < unsigned(V); }
64 operator unsigned() const { return Value; }
65 operator OpenMPDirectiveKind() const { return OpenMPDirectiveKind(Value); }
66 unsigned Value;
67};
68
Alexey Bataev25ed0c02019-03-07 17:54:44 +000069class DeclDirectiveListParserHelper final {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000070 SmallVector<Expr *, 4> Identifiers;
71 Parser *P;
Alexey Bataev25ed0c02019-03-07 17:54:44 +000072 OpenMPDirectiveKind Kind;
Dmitry Polukhind69b5052016-05-09 14:59:13 +000073
74public:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000075 DeclDirectiveListParserHelper(Parser *P, OpenMPDirectiveKind Kind)
76 : P(P), Kind(Kind) {}
Dmitry Polukhind69b5052016-05-09 14:59:13 +000077 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
Alexey Bataev25ed0c02019-03-07 17:54:44 +000078 ExprResult Res = P->getActions().ActOnOpenMPIdExpression(
79 P->getCurScope(), SS, NameInfo, Kind);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000080 if (Res.isUsable())
81 Identifiers.push_back(Res.get());
82 }
83 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
84};
Dmitry Polukhin82478332016-02-13 06:53:38 +000085} // namespace
86
87// Map token string to extended OMP token kind that are
88// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
89static unsigned getOpenMPDirectiveKindEx(StringRef S) {
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060090 OpenMPDirectiveKindExWrapper DKind = getOpenMPDirectiveKind(S);
Dmitry Polukhin82478332016-02-13 06:53:38 +000091 if (DKind != OMPD_unknown)
92 return DKind;
93
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060094 return llvm::StringSwitch<OpenMPDirectiveKindExWrapper>(S)
Dmitry Polukhin82478332016-02-13 06:53:38 +000095 .Case("cancellation", OMPD_cancellation)
96 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000097 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000098 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000099 .Case("enter", OMPD_enter)
100 .Case("exit", OMPD_exit)
101 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000102 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +0000103 .Case("update", OMPD_update)
Michael Kruse251e1482019-02-01 20:25:04 +0000104 .Case("mapper", OMPD_mapper)
Alexey Bataevd158cf62019-09-13 20:18:17 +0000105 .Case("variant", OMPD_variant)
Johannes Doerfert095cecb2020-02-20 19:50:47 -0600106 .Case("begin", OMPD_begin)
Dmitry Polukhin82478332016-02-13 06:53:38 +0000107 .Default(OMPD_unknown);
108}
109
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600110static OpenMPDirectiveKindExWrapper parseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +0000111 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
112 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
113 // TODO: add other combined directives in topological order.
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600114 static const OpenMPDirectiveKindExWrapper F[][3] = {
Johannes Doerfert095cecb2020-02-20 19:50:47 -0600115 {OMPD_begin, OMPD_declare, OMPD_begin_declare},
116 {OMPD_end, OMPD_declare, OMPD_end_declare},
Alexey Bataev61908f652018-04-23 19:53:05 +0000117 {OMPD_cancellation, OMPD_point, OMPD_cancellation_point},
118 {OMPD_declare, OMPD_reduction, OMPD_declare_reduction},
Michael Kruse251e1482019-02-01 20:25:04 +0000119 {OMPD_declare, OMPD_mapper, OMPD_declare_mapper},
Alexey Bataev61908f652018-04-23 19:53:05 +0000120 {OMPD_declare, OMPD_simd, OMPD_declare_simd},
121 {OMPD_declare, OMPD_target, OMPD_declare_target},
Alexey Bataevd158cf62019-09-13 20:18:17 +0000122 {OMPD_declare, OMPD_variant, OMPD_declare_variant},
Johannes Doerfert095cecb2020-02-20 19:50:47 -0600123 {OMPD_begin_declare, OMPD_variant, OMPD_begin_declare_variant},
124 {OMPD_end_declare, OMPD_variant, OMPD_end_declare_variant},
Alexey Bataev61908f652018-04-23 19:53:05 +0000125 {OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel},
126 {OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for},
127 {OMPD_distribute_parallel_for, OMPD_simd,
128 OMPD_distribute_parallel_for_simd},
129 {OMPD_distribute, OMPD_simd, OMPD_distribute_simd},
Alexey Bataev61908f652018-04-23 19:53:05 +0000130 {OMPD_end_declare, OMPD_target, OMPD_end_declare_target},
131 {OMPD_target, OMPD_data, OMPD_target_data},
132 {OMPD_target, OMPD_enter, OMPD_target_enter},
133 {OMPD_target, OMPD_exit, OMPD_target_exit},
134 {OMPD_target, OMPD_update, OMPD_target_update},
135 {OMPD_target_enter, OMPD_data, OMPD_target_enter_data},
136 {OMPD_target_exit, OMPD_data, OMPD_target_exit_data},
137 {OMPD_for, OMPD_simd, OMPD_for_simd},
138 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
139 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
140 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
141 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd},
142 {OMPD_target, OMPD_parallel, OMPD_target_parallel},
143 {OMPD_target, OMPD_simd, OMPD_target_simd},
144 {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for},
145 {OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd},
146 {OMPD_teams, OMPD_distribute, OMPD_teams_distribute},
147 {OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd},
148 {OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel},
149 {OMPD_teams_distribute_parallel, OMPD_for,
150 OMPD_teams_distribute_parallel_for},
151 {OMPD_teams_distribute_parallel_for, OMPD_simd,
152 OMPD_teams_distribute_parallel_for_simd},
153 {OMPD_target, OMPD_teams, OMPD_target_teams},
154 {OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute},
155 {OMPD_target_teams_distribute, OMPD_parallel,
156 OMPD_target_teams_distribute_parallel},
157 {OMPD_target_teams_distribute, OMPD_simd,
158 OMPD_target_teams_distribute_simd},
159 {OMPD_target_teams_distribute_parallel, OMPD_for,
160 OMPD_target_teams_distribute_parallel_for},
161 {OMPD_target_teams_distribute_parallel_for, OMPD_simd,
Alexey Bataev60e51c42019-10-10 20:13:02 +0000162 OMPD_target_teams_distribute_parallel_for_simd},
Alexey Bataev5bbcead2019-10-14 17:17:41 +0000163 {OMPD_master, OMPD_taskloop, OMPD_master_taskloop},
Alexey Bataevb8552ab2019-10-18 16:47:35 +0000164 {OMPD_master_taskloop, OMPD_simd, OMPD_master_taskloop_simd},
Alexey Bataev5bbcead2019-10-14 17:17:41 +0000165 {OMPD_parallel, OMPD_master, OMPD_parallel_master},
Alexey Bataev14a388f2019-10-25 10:27:13 -0400166 {OMPD_parallel_master, OMPD_taskloop, OMPD_parallel_master_taskloop},
167 {OMPD_parallel_master_taskloop, OMPD_simd,
168 OMPD_parallel_master_taskloop_simd}};
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000169 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev61908f652018-04-23 19:53:05 +0000170 Token Tok = P.getCurToken();
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600171 OpenMPDirectiveKindExWrapper DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000172 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000173 ? static_cast<unsigned>(OMPD_unknown)
174 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
175 if (DKind == OMPD_unknown)
176 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000177
Alexey Bataev61908f652018-04-23 19:53:05 +0000178 for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) {
179 if (DKind != F[I][0])
Dmitry Polukhin82478332016-02-13 06:53:38 +0000180 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000181
Dmitry Polukhin82478332016-02-13 06:53:38 +0000182 Tok = P.getPreprocessor().LookAhead(0);
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600183 OpenMPDirectiveKindExWrapper SDKind =
Dmitry Polukhin82478332016-02-13 06:53:38 +0000184 Tok.isAnnotation()
185 ? static_cast<unsigned>(OMPD_unknown)
186 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
187 if (SDKind == OMPD_unknown)
188 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000189
Alexey Bataev61908f652018-04-23 19:53:05 +0000190 if (SDKind == F[I][1]) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000191 P.ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000192 DKind = F[I][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000193 }
194 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000195 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
196 : OMPD_unknown;
197}
198
199static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000200 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000201 Sema &Actions = P.getActions();
202 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000203 // Allow to use 'operator' keyword for C++ operators
204 bool WithOperator = false;
205 if (Tok.is(tok::kw_operator)) {
206 P.ConsumeToken();
207 Tok = P.getCurToken();
208 WithOperator = true;
209 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000210 switch (Tok.getKind()) {
211 case tok::plus: // '+'
212 OOK = OO_Plus;
213 break;
214 case tok::minus: // '-'
215 OOK = OO_Minus;
216 break;
217 case tok::star: // '*'
218 OOK = OO_Star;
219 break;
220 case tok::amp: // '&'
221 OOK = OO_Amp;
222 break;
223 case tok::pipe: // '|'
224 OOK = OO_Pipe;
225 break;
226 case tok::caret: // '^'
227 OOK = OO_Caret;
228 break;
229 case tok::ampamp: // '&&'
230 OOK = OO_AmpAmp;
231 break;
232 case tok::pipepipe: // '||'
233 OOK = OO_PipePipe;
234 break;
235 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000236 if (!WithOperator)
237 break;
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000238 LLVM_FALLTHROUGH;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000239 default:
240 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
241 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
242 Parser::StopBeforeMatch);
243 return DeclarationName();
244 }
245 P.ConsumeToken();
246 auto &DeclNames = Actions.getASTContext().DeclarationNames;
247 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
248 : DeclNames.getCXXOperatorName(OOK);
249}
250
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000251/// Parse 'omp declare reduction' construct.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000252///
253/// declare-reduction-directive:
254/// annot_pragma_openmp 'declare' 'reduction'
255/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
256/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
257/// annot_pragma_openmp_end
258/// <reduction_id> is either a base language identifier or one of the following
259/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
260///
261Parser::DeclGroupPtrTy
262Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
263 // Parse '('.
264 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600265 if (T.expectAndConsume(
266 diag::err_expected_lparen_after,
267 getOpenMPDirectiveName(OMPD_declare_reduction).data())) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000268 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
269 return DeclGroupPtrTy();
270 }
271
272 DeclarationName Name = parseOpenMPReductionId(*this);
273 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
274 return DeclGroupPtrTy();
275
276 // Consume ':'.
277 bool IsCorrect = !ExpectAndConsume(tok::colon);
278
279 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
280 return DeclGroupPtrTy();
281
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000282 IsCorrect = IsCorrect && !Name.isEmpty();
283
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000284 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
285 Diag(Tok.getLocation(), diag::err_expected_type);
286 IsCorrect = false;
287 }
288
289 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
290 return DeclGroupPtrTy();
291
292 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
293 // Parse list of types until ':' token.
294 do {
295 ColonProtectionRAIIObject ColonRAII(*this);
296 SourceRange Range;
Faisal Vali421b2d12017-12-29 05:41:00 +0000297 TypeResult TR =
298 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000299 if (TR.isUsable()) {
Alexey Bataev61908f652018-04-23 19:53:05 +0000300 QualType ReductionType =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000301 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
302 if (!ReductionType.isNull()) {
303 ReductionTypes.push_back(
304 std::make_pair(ReductionType, Range.getBegin()));
305 }
306 } else {
307 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
308 StopBeforeMatch);
309 }
310
311 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
312 break;
313
314 // Consume ','.
315 if (ExpectAndConsume(tok::comma)) {
316 IsCorrect = false;
317 if (Tok.is(tok::annot_pragma_openmp_end)) {
318 Diag(Tok.getLocation(), diag::err_expected_type);
319 return DeclGroupPtrTy();
320 }
321 }
322 } while (Tok.isNot(tok::annot_pragma_openmp_end));
323
324 if (ReductionTypes.empty()) {
325 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
326 return DeclGroupPtrTy();
327 }
328
329 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
330 return DeclGroupPtrTy();
331
332 // Consume ':'.
333 if (ExpectAndConsume(tok::colon))
334 IsCorrect = false;
335
336 if (Tok.is(tok::annot_pragma_openmp_end)) {
337 Diag(Tok.getLocation(), diag::err_expected_expression);
338 return DeclGroupPtrTy();
339 }
340
341 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
342 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
343
344 // Parse <combiner> expression and then parse initializer if any for each
345 // correct type.
346 unsigned I = 0, E = ReductionTypes.size();
Alexey Bataev61908f652018-04-23 19:53:05 +0000347 for (Decl *D : DRD.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000348 TentativeParsingAction TPA(*this);
349 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000350 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000351 Scope::OpenMPDirectiveScope);
352 // Parse <combiner> expression.
353 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
Alexey Bataevc74a8ad2020-01-08 09:39:44 -0500354 ExprResult CombinerResult = Actions.ActOnFinishFullExpr(
355 ParseExpression().get(), D->getLocation(), /*DiscardedValue*/ false);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000356 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
357
358 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
359 Tok.isNot(tok::annot_pragma_openmp_end)) {
360 TPA.Commit();
361 IsCorrect = false;
362 break;
363 }
364 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
365 ExprResult InitializerResult;
366 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
367 // Parse <initializer> expression.
368 if (Tok.is(tok::identifier) &&
Alexey Bataev61908f652018-04-23 19:53:05 +0000369 Tok.getIdentifierInfo()->isStr("initializer")) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000370 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000371 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000372 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
373 TPA.Commit();
374 IsCorrect = false;
375 break;
376 }
377 // Parse '('.
378 BalancedDelimiterTracker T(*this, tok::l_paren,
379 tok::annot_pragma_openmp_end);
380 IsCorrect =
381 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
382 IsCorrect;
383 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
384 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000385 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000386 Scope::OpenMPDirectiveScope);
387 // Parse expression.
Alexey Bataev070f43a2017-09-06 14:49:58 +0000388 VarDecl *OmpPrivParm =
389 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
390 D);
391 // Check if initializer is omp_priv <init_expr> or something else.
392 if (Tok.is(tok::identifier) &&
393 Tok.getIdentifierInfo()->isStr("omp_priv")) {
Alexey Bataev3c676e32019-11-12 11:19:26 -0500394 ConsumeToken();
395 ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000396 } else {
397 InitializerResult = Actions.ActOnFinishFullExpr(
398 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000399 /*DiscardedValue*/ false);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000400 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000401 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
Alexey Bataev070f43a2017-09-06 14:49:58 +0000402 D, InitializerResult.get(), OmpPrivParm);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000403 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
404 Tok.isNot(tok::annot_pragma_openmp_end)) {
405 TPA.Commit();
406 IsCorrect = false;
407 break;
408 }
409 IsCorrect =
410 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
411 }
412 }
413
414 ++I;
415 // Revert parsing if not the last type, otherwise accept it, we're done with
416 // parsing.
417 if (I != E)
418 TPA.Revert();
419 else
420 TPA.Commit();
421 }
422 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
423 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000424}
425
Alexey Bataev070f43a2017-09-06 14:49:58 +0000426void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
427 // Parse declarator '=' initializer.
428 // If a '==' or '+=' is found, suggest a fixit to '='.
429 if (isTokenEqualOrEqualTypo()) {
430 ConsumeToken();
431
432 if (Tok.is(tok::code_completion)) {
433 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
434 Actions.FinalizeDeclaration(OmpPrivParm);
435 cutOffParsing();
436 return;
437 }
438
Alexey Bataev3c676e32019-11-12 11:19:26 -0500439 PreferredType.enterVariableInit(Tok.getLocation(), OmpPrivParm);
Alexey Bataev1fcc9b62020-01-02 15:49:08 -0500440 ExprResult Init = ParseInitializer();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000441
442 if (Init.isInvalid()) {
443 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
444 Actions.ActOnInitializerError(OmpPrivParm);
445 } else {
446 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
447 /*DirectInit=*/false);
448 }
449 } else if (Tok.is(tok::l_paren)) {
450 // Parse C++ direct initializer: '(' expression-list ')'
451 BalancedDelimiterTracker T(*this, tok::l_paren);
452 T.consumeOpen();
453
454 ExprVector Exprs;
455 CommaLocsTy CommaLocs;
456
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000457 SourceLocation LParLoc = T.getOpenLocation();
Ilya Biryukovff2a9972019-02-26 11:01:50 +0000458 auto RunSignatureHelp = [this, OmpPrivParm, LParLoc, &Exprs]() {
459 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
460 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
461 OmpPrivParm->getLocation(), Exprs, LParLoc);
462 CalledSignatureHelp = true;
463 return PreferredType;
464 };
465 if (ParseExpressionList(Exprs, CommaLocs, [&] {
466 PreferredType.enterFunctionArgument(Tok.getLocation(),
467 RunSignatureHelp);
468 })) {
469 if (PP.isCodeCompletionReached() && !CalledSignatureHelp)
470 RunSignatureHelp();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000471 Actions.ActOnInitializerError(OmpPrivParm);
472 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
473 } else {
474 // Match the ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000475 SourceLocation RLoc = Tok.getLocation();
476 if (!T.consumeClose())
477 RLoc = T.getCloseLocation();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000478
479 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
480 "Unexpected number of commas!");
481
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000482 ExprResult Initializer =
483 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000484 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
485 /*DirectInit=*/true);
486 }
487 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
488 // Parse C++0x braced-init-list.
489 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
490
491 ExprResult Init(ParseBraceInitializer());
492
493 if (Init.isInvalid()) {
494 Actions.ActOnInitializerError(OmpPrivParm);
495 } else {
496 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
497 /*DirectInit=*/true);
498 }
499 } else {
500 Actions.ActOnUninitializedDecl(OmpPrivParm);
501 }
502}
503
Michael Kruse251e1482019-02-01 20:25:04 +0000504/// Parses 'omp declare mapper' directive.
505///
506/// declare-mapper-directive:
507/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifier> ':']
508/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
509/// annot_pragma_openmp_end
510/// <mapper-identifier> and <var> are base language identifiers.
511///
512Parser::DeclGroupPtrTy
513Parser::ParseOpenMPDeclareMapperDirective(AccessSpecifier AS) {
514 bool IsCorrect = true;
515 // Parse '('
516 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
517 if (T.expectAndConsume(diag::err_expected_lparen_after,
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600518 getOpenMPDirectiveName(OMPD_declare_mapper).data())) {
Michael Kruse251e1482019-02-01 20:25:04 +0000519 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
520 return DeclGroupPtrTy();
521 }
522
523 // Parse <mapper-identifier>
524 auto &DeclNames = Actions.getASTContext().DeclarationNames;
525 DeclarationName MapperId;
526 if (PP.LookAhead(0).is(tok::colon)) {
527 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
528 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
529 IsCorrect = false;
530 } else {
531 MapperId = DeclNames.getIdentifier(Tok.getIdentifierInfo());
532 }
533 ConsumeToken();
534 // Consume ':'.
535 ExpectAndConsume(tok::colon);
536 } else {
537 // If no mapper identifier is provided, its name is "default" by default
538 MapperId =
539 DeclNames.getIdentifier(&Actions.getASTContext().Idents.get("default"));
540 }
541
542 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
543 return DeclGroupPtrTy();
544
545 // Parse <type> <var>
546 DeclarationName VName;
547 QualType MapperType;
548 SourceRange Range;
549 TypeResult ParsedType = parseOpenMPDeclareMapperVarDecl(Range, VName, AS);
550 if (ParsedType.isUsable())
551 MapperType =
552 Actions.ActOnOpenMPDeclareMapperType(Range.getBegin(), ParsedType);
553 if (MapperType.isNull())
554 IsCorrect = false;
555 if (!IsCorrect) {
556 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
557 return DeclGroupPtrTy();
558 }
559
560 // Consume ')'.
561 IsCorrect &= !T.consumeClose();
562 if (!IsCorrect) {
563 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
564 return DeclGroupPtrTy();
565 }
566
567 // Enter scope.
568 OMPDeclareMapperDecl *DMD = Actions.ActOnOpenMPDeclareMapperDirectiveStart(
569 getCurScope(), Actions.getCurLexicalContext(), MapperId, MapperType,
570 Range.getBegin(), VName, AS);
571 DeclarationNameInfo DirName;
572 SourceLocation Loc = Tok.getLocation();
573 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
574 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
575 ParseScope OMPDirectiveScope(this, ScopeFlags);
576 Actions.StartOpenMPDSABlock(OMPD_declare_mapper, DirName, getCurScope(), Loc);
577
578 // Add the mapper variable declaration.
579 Actions.ActOnOpenMPDeclareMapperDirectiveVarDecl(
580 DMD, getCurScope(), MapperType, Range.getBegin(), VName);
581
582 // Parse map clauses.
583 SmallVector<OMPClause *, 6> Clauses;
584 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
585 OpenMPClauseKind CKind = Tok.isAnnotation()
586 ? OMPC_unknown
587 : getOpenMPClauseKind(PP.getSpelling(Tok));
588 Actions.StartOpenMPClause(CKind);
589 OMPClause *Clause =
590 ParseOpenMPClause(OMPD_declare_mapper, CKind, Clauses.size() == 0);
591 if (Clause)
592 Clauses.push_back(Clause);
593 else
594 IsCorrect = false;
595 // Skip ',' if any.
596 if (Tok.is(tok::comma))
597 ConsumeToken();
598 Actions.EndOpenMPClause();
599 }
600 if (Clauses.empty()) {
601 Diag(Tok, diag::err_omp_expected_clause)
602 << getOpenMPDirectiveName(OMPD_declare_mapper);
603 IsCorrect = false;
604 }
605
606 // Exit scope.
607 Actions.EndOpenMPDSABlock(nullptr);
608 OMPDirectiveScope.Exit();
609
610 DeclGroupPtrTy DGP =
611 Actions.ActOnOpenMPDeclareMapperDirectiveEnd(DMD, getCurScope(), Clauses);
612 if (!IsCorrect)
613 return DeclGroupPtrTy();
614 return DGP;
615}
616
617TypeResult Parser::parseOpenMPDeclareMapperVarDecl(SourceRange &Range,
618 DeclarationName &Name,
619 AccessSpecifier AS) {
620 // Parse the common declaration-specifiers piece.
621 Parser::DeclSpecContext DSC = Parser::DeclSpecContext::DSC_type_specifier;
622 DeclSpec DS(AttrFactory);
623 ParseSpecifierQualifierList(DS, AS, DSC);
624
625 // Parse the declarator.
626 DeclaratorContext Context = DeclaratorContext::PrototypeContext;
627 Declarator DeclaratorInfo(DS, Context);
628 ParseDeclarator(DeclaratorInfo);
629 Range = DeclaratorInfo.getSourceRange();
630 if (DeclaratorInfo.getIdentifier() == nullptr) {
631 Diag(Tok.getLocation(), diag::err_omp_mapper_expected_declarator);
632 return true;
633 }
634 Name = Actions.GetNameForDeclarator(DeclaratorInfo).getName();
635
636 return Actions.ActOnOpenMPDeclareMapperVarDecl(getCurScope(), DeclaratorInfo);
637}
638
Alexey Bataev2af33e32016-04-07 12:45:37 +0000639namespace {
640/// RAII that recreates function context for correct parsing of clauses of
641/// 'declare simd' construct.
642/// OpenMP, 2.8.2 declare simd Construct
643/// The expressions appearing in the clauses of this directive are evaluated in
644/// the scope of the arguments of the function declaration or definition.
645class FNContextRAII final {
646 Parser &P;
647 Sema::CXXThisScopeRAII *ThisScope;
648 Parser::ParseScope *TempScope;
649 Parser::ParseScope *FnScope;
650 bool HasTemplateScope = false;
651 bool HasFunScope = false;
652 FNContextRAII() = delete;
653 FNContextRAII(const FNContextRAII &) = delete;
654 FNContextRAII &operator=(const FNContextRAII &) = delete;
655
656public:
657 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
658 Decl *D = *Ptr.get().begin();
659 NamedDecl *ND = dyn_cast<NamedDecl>(D);
660 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
661 Sema &Actions = P.getActions();
662
663 // Allow 'this' within late-parsed attributes.
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000664 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, Qualifiers(),
Alexey Bataev2af33e32016-04-07 12:45:37 +0000665 ND && ND->isCXXInstanceMember());
666
667 // If the Decl is templatized, add template parameters to scope.
668 HasTemplateScope = D->isTemplateDecl();
669 TempScope =
670 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
671 if (HasTemplateScope)
672 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
673
674 // If the Decl is on a function, add function parameters to the scope.
675 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000676 FnScope = new Parser::ParseScope(
677 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
678 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000679 if (HasFunScope)
680 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
681 }
682 ~FNContextRAII() {
683 if (HasFunScope) {
684 P.getActions().ActOnExitFunctionContext();
685 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
686 }
687 if (HasTemplateScope)
688 TempScope->Exit();
689 delete FnScope;
690 delete TempScope;
691 delete ThisScope;
692 }
693};
694} // namespace
695
Alexey Bataevd93d3762016-04-12 09:35:56 +0000696/// Parses clauses for 'declare simd' directive.
697/// clause:
698/// 'inbranch' | 'notinbranch'
699/// 'simdlen' '(' <expr> ')'
700/// { 'uniform' '(' <argument_list> ')' }
701/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000702/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
703static bool parseDeclareSimdClauses(
704 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
705 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
706 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
707 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000708 SourceRange BSRange;
709 const Token &Tok = P.getCurToken();
710 bool IsError = false;
711 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
712 if (Tok.isNot(tok::identifier))
713 break;
714 OMPDeclareSimdDeclAttr::BranchStateTy Out;
715 IdentifierInfo *II = Tok.getIdentifierInfo();
716 StringRef ClauseName = II->getName();
717 // Parse 'inranch|notinbranch' clauses.
718 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
719 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
720 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
721 << ClauseName
722 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
723 IsError = true;
724 }
725 BS = Out;
726 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
727 P.ConsumeToken();
728 } else if (ClauseName.equals("simdlen")) {
729 if (SimdLen.isUsable()) {
730 P.Diag(Tok, diag::err_omp_more_one_clause)
731 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
732 IsError = true;
733 }
734 P.ConsumeToken();
735 SourceLocation RLoc;
736 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
737 if (SimdLen.isInvalid())
738 IsError = true;
739 } else {
740 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000741 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
742 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000743 Parser::OpenMPVarListDataTy Data;
Alexey Bataev61908f652018-04-23 19:53:05 +0000744 SmallVectorImpl<Expr *> *Vars = &Uniforms;
Alexey Bataev3732f4e2019-12-24 16:02:58 -0500745 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000746 Vars = &Aligneds;
Alexey Bataev3732f4e2019-12-24 16:02:58 -0500747 } else if (CKind == OMPC_linear) {
748 Data.ExtraModifier = OMPC_LINEAR_val;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000749 Vars = &Linears;
Alexey Bataev3732f4e2019-12-24 16:02:58 -0500750 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000751
752 P.ConsumeToken();
753 if (P.ParseOpenMPVarList(OMPD_declare_simd,
754 getOpenMPClauseKind(ClauseName), *Vars, Data))
755 IsError = true;
Alexey Bataev61908f652018-04-23 19:53:05 +0000756 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000757 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataev61908f652018-04-23 19:53:05 +0000758 } else if (CKind == OMPC_linear) {
Alexey Bataev3732f4e2019-12-24 16:02:58 -0500759 assert(0 <= Data.ExtraModifier &&
760 Data.ExtraModifier <= OMPC_LINEAR_unknown &&
761 "Unexpected linear modifier.");
Alexey Bataev93dc40d2019-12-20 11:04:57 -0500762 if (P.getActions().CheckOpenMPLinearModifier(
763 static_cast<OpenMPLinearClauseKind>(Data.ExtraModifier),
Alexey Bataev1236eb62020-03-23 17:30:38 -0400764 Data.ExtraModifierLoc))
Alexey Bataev93dc40d2019-12-20 11:04:57 -0500765 Data.ExtraModifier = OMPC_LINEAR_val;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000766 LinModifiers.append(Linears.size() - LinModifiers.size(),
Alexey Bataev93dc40d2019-12-20 11:04:57 -0500767 Data.ExtraModifier);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000768 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
769 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000770 } else
771 // TODO: add parsing of other clauses.
772 break;
773 }
774 // Skip ',' if any.
775 if (Tok.is(tok::comma))
776 P.ConsumeToken();
777 }
778 return IsError;
779}
780
Alexey Bataev2af33e32016-04-07 12:45:37 +0000781/// Parse clauses for '#pragma omp declare simd'.
782Parser::DeclGroupPtrTy
783Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
784 CachedTokens &Toks, SourceLocation Loc) {
Ilya Biryukov929af672019-05-17 09:32:05 +0000785 PP.EnterToken(Tok, /*IsReinject*/ true);
786 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
787 /*IsReinject*/ true);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000788 // Consume the previously pushed token.
789 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Alexey Bataevd158cf62019-09-13 20:18:17 +0000790 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000791
792 FNContextRAII FnContext(*this, Ptr);
793 OMPDeclareSimdDeclAttr::BranchStateTy BS =
794 OMPDeclareSimdDeclAttr::BS_Undefined;
795 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000796 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000797 SmallVector<Expr *, 4> Aligneds;
798 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000799 SmallVector<Expr *, 4> Linears;
800 SmallVector<unsigned, 4> LinModifiers;
801 SmallVector<Expr *, 4> Steps;
802 bool IsError =
803 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
804 Alignments, Linears, LinModifiers, Steps);
Johannes Doerfert56d15532020-02-21 13:48:56 -0600805 skipUntilPragmaOpenMPEnd(OMPD_declare_simd);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000806 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000807 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000808 if (IsError)
809 return Ptr;
810 return Actions.ActOnOpenMPDeclareSimdDirective(
811 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
812 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataev20dfd772016-04-04 10:12:15 +0000813}
814
Johannes Doerfert1228d422019-12-19 20:42:12 -0600815namespace {
816/// Constant used in the diagnostics to distinguish the levels in an OpenMP
817/// contexts: selector-set={selector(trait, ...), ...}, ....
818enum OMPContextLvl {
819 CONTEXT_SELECTOR_SET_LVL = 0,
820 CONTEXT_SELECTOR_LVL = 1,
821 CONTEXT_TRAIT_LVL = 2,
822};
823
824static StringRef stringLiteralParser(Parser &P) {
825 ExprResult Res = P.ParseStringLiteralExpression(true);
826 return Res.isUsable() ? Res.getAs<StringLiteral>()->getString() : "";
827}
828
829static StringRef getNameFromIdOrString(Parser &P, Token &Tok,
830 OMPContextLvl Lvl) {
831 if (Tok.is(tok::identifier)) {
832 llvm::SmallString<16> Buffer;
833 StringRef Name = P.getPreprocessor().getSpelling(Tok, Buffer);
834 (void)P.ConsumeToken();
835 return Name;
836 }
837
838 if (tok::isStringLiteral(Tok.getKind()))
839 return stringLiteralParser(P);
840
841 P.Diag(Tok.getLocation(),
842 diag::warn_omp_declare_variant_string_literal_or_identifier)
843 << Lvl;
844 return "";
845}
846
847static bool checkForDuplicates(Parser &P, StringRef Name,
848 SourceLocation NameLoc,
849 llvm::StringMap<SourceLocation> &Seen,
850 OMPContextLvl Lvl) {
851 auto Res = Seen.try_emplace(Name, NameLoc);
852 if (Res.second)
853 return false;
854
855 // Each trait-set-selector-name, trait-selector-name and trait-name can
856 // only be specified once.
857 P.Diag(NameLoc, diag::warn_omp_declare_variant_ctx_mutiple_use)
858 << Lvl << Name;
859 P.Diag(Res.first->getValue(), diag::note_omp_declare_variant_ctx_used_here)
860 << Lvl << Name;
861 return true;
862}
863} // namespace
864
865void Parser::parseOMPTraitPropertyKind(
866 OMPTraitInfo::OMPTraitProperty &TIProperty, llvm::omp::TraitSet Set,
867 llvm::omp::TraitSelector Selector, llvm::StringMap<SourceLocation> &Seen) {
868 TIProperty.Kind = TraitProperty::invalid;
869
870 SourceLocation NameLoc = Tok.getLocation();
871 StringRef Name =
872 getNameFromIdOrString(*this, Tok, CONTEXT_TRAIT_LVL);
873 if (Name.empty()) {
874 Diag(Tok.getLocation(), diag::note_omp_declare_variant_ctx_options)
875 << CONTEXT_TRAIT_LVL << listOpenMPContextTraitProperties(Set, Selector);
876 return;
877 }
878
879 TIProperty.Kind = getOpenMPContextTraitPropertyKind(Set, Name);
880 if (TIProperty.Kind != TraitProperty::invalid) {
881 if (checkForDuplicates(*this, Name, NameLoc, Seen, CONTEXT_TRAIT_LVL))
882 TIProperty.Kind = TraitProperty::invalid;
883 return;
884 }
885
886 // It follows diagnosis and helping notes.
887 // FIXME: We should move the diagnosis string generation into libFrontend.
888 Diag(NameLoc, diag::warn_omp_declare_variant_ctx_not_a_property)
889 << Name << getOpenMPContextTraitSelectorName(Selector)
890 << getOpenMPContextTraitSetName(Set);
891
892 TraitSet SetForName = getOpenMPContextTraitSetKind(Name);
893 if (SetForName != TraitSet::invalid) {
894 Diag(NameLoc, diag::note_omp_declare_variant_ctx_is_a)
895 << Name << CONTEXT_SELECTOR_SET_LVL << CONTEXT_TRAIT_LVL;
896 Diag(NameLoc, diag::note_omp_declare_variant_ctx_try)
897 << Name << "<selector-name>"
898 << "(<property-name>)";
899 return;
900 }
901 TraitSelector SelectorForName = getOpenMPContextTraitSelectorKind(Name);
902 if (SelectorForName != TraitSelector::invalid) {
903 Diag(NameLoc, diag::note_omp_declare_variant_ctx_is_a)
904 << Name << CONTEXT_SELECTOR_LVL << CONTEXT_TRAIT_LVL;
905 bool AllowsTraitScore = false;
906 bool RequiresProperty = false;
907 isValidTraitSelectorForTraitSet(
908 SelectorForName, getOpenMPContextTraitSetForSelector(SelectorForName),
909 AllowsTraitScore, RequiresProperty);
910 Diag(NameLoc, diag::note_omp_declare_variant_ctx_try)
911 << getOpenMPContextTraitSetName(
912 getOpenMPContextTraitSetForSelector(SelectorForName))
913 << Name << (RequiresProperty ? "(<property-name>)" : "");
914 return;
915 }
916 for (const auto &PotentialSet :
917 {TraitSet::construct, TraitSet::user, TraitSet::implementation,
918 TraitSet::device}) {
919 TraitProperty PropertyForName =
920 getOpenMPContextTraitPropertyKind(PotentialSet, Name);
921 if (PropertyForName == TraitProperty::invalid)
922 continue;
923 Diag(NameLoc, diag::note_omp_declare_variant_ctx_try)
924 << getOpenMPContextTraitSetName(
925 getOpenMPContextTraitSetForProperty(PropertyForName))
926 << getOpenMPContextTraitSelectorName(
927 getOpenMPContextTraitSelectorForProperty(PropertyForName))
928 << ("(" + Name + ")").str();
929 return;
930 }
931 Diag(NameLoc, diag::note_omp_declare_variant_ctx_options)
932 << CONTEXT_TRAIT_LVL << listOpenMPContextTraitProperties(Set, Selector);
933}
934
935void Parser::parseOMPContextProperty(OMPTraitInfo::OMPTraitSelector &TISelector,
936 llvm::omp::TraitSet Set,
937 llvm::StringMap<SourceLocation> &Seen) {
938 assert(TISelector.Kind != TraitSelector::user_condition &&
939 "User conditions are special properties not handled here!");
940
941 SourceLocation PropertyLoc = Tok.getLocation();
942 OMPTraitInfo::OMPTraitProperty TIProperty;
943 parseOMPTraitPropertyKind(TIProperty, Set, TISelector.Kind, Seen);
944
945 // If we have an invalid property here we already issued a warning.
946 if (TIProperty.Kind == TraitProperty::invalid) {
947 if (PropertyLoc != Tok.getLocation())
948 Diag(Tok.getLocation(), diag::note_omp_declare_variant_ctx_continue_here)
949 << CONTEXT_TRAIT_LVL;
950 return;
951 }
952
953 if (isValidTraitPropertyForTraitSetAndSelector(TIProperty.Kind,
954 TISelector.Kind, Set)) {
955 // If we make it here the property, selector, set, score, condition, ... are
956 // all valid (or have been corrected). Thus we can record the property.
957 TISelector.Properties.push_back(TIProperty);
958 return;
959 }
960
961 Diag(PropertyLoc, diag::warn_omp_ctx_incompatible_property_for_selector)
962 << getOpenMPContextTraitPropertyName(TIProperty.Kind)
963 << getOpenMPContextTraitSelectorName(TISelector.Kind)
964 << getOpenMPContextTraitSetName(Set);
965 Diag(PropertyLoc, diag::note_omp_ctx_compatible_set_and_selector_for_property)
966 << getOpenMPContextTraitPropertyName(TIProperty.Kind)
967 << getOpenMPContextTraitSelectorName(
968 getOpenMPContextTraitSelectorForProperty(TIProperty.Kind))
969 << getOpenMPContextTraitSetName(
970 getOpenMPContextTraitSetForProperty(TIProperty.Kind));
971 Diag(Tok.getLocation(), diag::note_omp_declare_variant_ctx_continue_here)
972 << CONTEXT_TRAIT_LVL;
973}
974
975void Parser::parseOMPTraitSelectorKind(
976 OMPTraitInfo::OMPTraitSelector &TISelector, llvm::omp::TraitSet Set,
977 llvm::StringMap<SourceLocation> &Seen) {
978 TISelector.Kind = TraitSelector::invalid;
979
980 SourceLocation NameLoc = Tok.getLocation();
981 StringRef Name = getNameFromIdOrString(*this, Tok, CONTEXT_SELECTOR_LVL
982 );
983 if (Name.empty()) {
984 Diag(Tok.getLocation(), diag::note_omp_declare_variant_ctx_options)
985 << CONTEXT_SELECTOR_LVL << listOpenMPContextTraitSelectors(Set);
986 return;
987 }
988
989 TISelector.Kind = getOpenMPContextTraitSelectorKind(Name);
990 if (TISelector.Kind != TraitSelector::invalid) {
991 if (checkForDuplicates(*this, Name, NameLoc, Seen, CONTEXT_SELECTOR_LVL))
992 TISelector.Kind = TraitSelector::invalid;
993 return;
994 }
995
996 // It follows diagnosis and helping notes.
997 Diag(NameLoc, diag::warn_omp_declare_variant_ctx_not_a_selector)
998 << Name << getOpenMPContextTraitSetName(Set);
999
1000 TraitSet SetForName = getOpenMPContextTraitSetKind(Name);
1001 if (SetForName != TraitSet::invalid) {
1002 Diag(NameLoc, diag::note_omp_declare_variant_ctx_is_a)
1003 << Name << CONTEXT_SELECTOR_SET_LVL << CONTEXT_SELECTOR_LVL;
1004 Diag(NameLoc, diag::note_omp_declare_variant_ctx_try)
1005 << Name << "<selector-name>"
1006 << "<property-name>";
1007 return;
1008 }
1009 for (const auto &PotentialSet :
1010 {TraitSet::construct, TraitSet::user, TraitSet::implementation,
1011 TraitSet::device}) {
1012 TraitProperty PropertyForName =
1013 getOpenMPContextTraitPropertyKind(PotentialSet, Name);
1014 if (PropertyForName == TraitProperty::invalid)
1015 continue;
1016 Diag(NameLoc, diag::note_omp_declare_variant_ctx_is_a)
1017 << Name << CONTEXT_TRAIT_LVL << CONTEXT_SELECTOR_LVL;
1018 Diag(NameLoc, diag::note_omp_declare_variant_ctx_try)
1019 << getOpenMPContextTraitSetName(
1020 getOpenMPContextTraitSetForProperty(PropertyForName))
1021 << getOpenMPContextTraitSelectorName(
1022 getOpenMPContextTraitSelectorForProperty(PropertyForName))
1023 << ("(" + Name + ")").str();
1024 return;
1025 }
1026 Diag(NameLoc, diag::note_omp_declare_variant_ctx_options)
1027 << CONTEXT_SELECTOR_LVL << listOpenMPContextTraitSelectors(Set);
1028}
1029
Alexey Bataeva15a1412019-10-02 18:19:02 +00001030/// Parse optional 'score' '(' <expr> ')' ':'.
1031static ExprResult parseContextScore(Parser &P) {
1032 ExprResult ScoreExpr;
Johannes Doerfert1228d422019-12-19 20:42:12 -06001033 llvm::SmallString<16> Buffer;
Alexey Bataeva15a1412019-10-02 18:19:02 +00001034 StringRef SelectorName =
1035 P.getPreprocessor().getSpelling(P.getCurToken(), Buffer);
Alexey Bataevdcec2ac2019-11-05 15:33:18 -05001036 if (!SelectorName.equals("score"))
Alexey Bataeva15a1412019-10-02 18:19:02 +00001037 return ScoreExpr;
Alexey Bataeva15a1412019-10-02 18:19:02 +00001038 (void)P.ConsumeToken();
1039 SourceLocation RLoc;
1040 ScoreExpr = P.ParseOpenMPParensExpr(SelectorName, RLoc);
1041 // Parse ':'
1042 if (P.getCurToken().is(tok::colon))
1043 (void)P.ConsumeAnyToken();
1044 else
Johannes Doerfert1228d422019-12-19 20:42:12 -06001045 P.Diag(P.getCurToken(), diag::warn_omp_declare_variant_expected)
1046 << "':'"
1047 << "score expression";
Alexey Bataeva15a1412019-10-02 18:19:02 +00001048 return ScoreExpr;
1049}
1050
Johannes Doerfert1228d422019-12-19 20:42:12 -06001051/// Parses an OpenMP context selector.
1052///
1053/// <trait-selector-name> ['('[<trait-score>] <trait-property> [, <t-p>]* ')']
1054void Parser::parseOMPContextSelector(
1055 OMPTraitInfo::OMPTraitSelector &TISelector, llvm::omp::TraitSet Set,
1056 llvm::StringMap<SourceLocation> &SeenSelectors) {
1057 unsigned short OuterPC = ParenCount;
Alexey Bataev9ff34742019-09-25 19:43:37 +00001058
Johannes Doerfert1228d422019-12-19 20:42:12 -06001059 // If anything went wrong we issue an error or warning and then skip the rest
1060 // of the selector. However, commas are ambiguous so we look for the nesting
1061 // of parentheses here as well.
1062 auto FinishSelector = [OuterPC, this]() -> void {
1063 bool Done = false;
1064 while (!Done) {
1065 while (!SkipUntil({tok::r_brace, tok::r_paren, tok::comma,
1066 tok::annot_pragma_openmp_end},
1067 StopBeforeMatch))
1068 ;
1069 if (Tok.is(tok::r_paren) && OuterPC > ParenCount)
1070 (void)ConsumeParen();
1071 if (OuterPC <= ParenCount) {
1072 Done = true;
1073 break;
Alexey Bataev4e8231b2019-11-05 15:13:30 -05001074 }
Johannes Doerfert1228d422019-12-19 20:42:12 -06001075 if (!Tok.is(tok::comma) && !Tok.is(tok::r_paren)) {
1076 Done = true;
1077 break;
Alexey Bataev4e8231b2019-11-05 15:13:30 -05001078 }
Johannes Doerfert1228d422019-12-19 20:42:12 -06001079 (void)ConsumeAnyToken();
1080 }
1081 Diag(Tok.getLocation(), diag::note_omp_declare_variant_ctx_continue_here)
1082 << CONTEXT_SELECTOR_LVL;
1083 };
Alexey Bataev4e8231b2019-11-05 15:13:30 -05001084
Johannes Doerfert1228d422019-12-19 20:42:12 -06001085 SourceLocation SelectorLoc = Tok.getLocation();
1086 parseOMPTraitSelectorKind(TISelector, Set, SeenSelectors);
1087 if (TISelector.Kind == TraitSelector::invalid)
1088 return FinishSelector();
1089
1090 bool AllowsTraitScore = false;
1091 bool RequiresProperty = false;
1092 if (!isValidTraitSelectorForTraitSet(TISelector.Kind, Set, AllowsTraitScore,
1093 RequiresProperty)) {
1094 Diag(SelectorLoc, diag::warn_omp_ctx_incompatible_selector_for_set)
1095 << getOpenMPContextTraitSelectorName(TISelector.Kind)
1096 << getOpenMPContextTraitSetName(Set);
1097 Diag(SelectorLoc, diag::note_omp_ctx_compatible_set_for_selector)
1098 << getOpenMPContextTraitSelectorName(TISelector.Kind)
1099 << getOpenMPContextTraitSetName(
1100 getOpenMPContextTraitSetForSelector(TISelector.Kind))
1101 << RequiresProperty;
1102 return FinishSelector();
1103 }
1104
1105 if (!RequiresProperty) {
1106 TISelector.Properties.push_back(
1107 {getOpenMPContextTraitPropertyForSelector(TISelector.Kind)});
1108 return;
1109 }
1110
1111 if (!Tok.is(tok::l_paren)) {
1112 Diag(SelectorLoc, diag::warn_omp_ctx_selector_without_properties)
1113 << getOpenMPContextTraitSelectorName(TISelector.Kind)
1114 << getOpenMPContextTraitSetName(Set);
1115 return FinishSelector();
1116 }
1117
1118 if (TISelector.Kind == TraitSelector::user_condition) {
1119 SourceLocation RLoc;
1120 ExprResult Condition = ParseOpenMPParensExpr("user condition", RLoc);
1121 if (!Condition.isUsable())
1122 return FinishSelector();
1123 TISelector.ScoreOrCondition = Condition.get();
1124 TISelector.Properties.push_back({TraitProperty::user_condition_unknown});
1125 return;
1126 }
1127
1128 BalancedDelimiterTracker BDT(*this, tok::l_paren,
1129 tok::annot_pragma_openmp_end);
1130 // Parse '('.
1131 (void)BDT.consumeOpen();
1132
Johannes Doerfert095cecb2020-02-20 19:50:47 -06001133 SourceLocation ScoreLoc = Tok.getLocation();
Johannes Doerfert1228d422019-12-19 20:42:12 -06001134 ExprResult Score = parseContextScore(*this);
1135
Johannes Doerfert095cecb2020-02-20 19:50:47 -06001136 if (!AllowsTraitScore && !Score.isUnset()) {
1137 if (Score.isUsable()) {
1138 Diag(ScoreLoc, diag::warn_omp_ctx_incompatible_score_for_property)
1139 << getOpenMPContextTraitSelectorName(TISelector.Kind)
1140 << getOpenMPContextTraitSetName(Set) << Score.get();
1141 } else {
1142 Diag(ScoreLoc, diag::warn_omp_ctx_incompatible_score_for_property)
1143 << getOpenMPContextTraitSelectorName(TISelector.Kind)
1144 << getOpenMPContextTraitSetName(Set) << "<invalid>";
1145 }
Johannes Doerfert1228d422019-12-19 20:42:12 -06001146 Score = ExprResult();
1147 }
1148
1149 if (Score.isUsable())
1150 TISelector.ScoreOrCondition = Score.get();
1151
1152 llvm::StringMap<SourceLocation> SeenProperties;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001153 do {
Johannes Doerfert1228d422019-12-19 20:42:12 -06001154 parseOMPContextProperty(TISelector, Set, SeenProperties);
1155 } while (TryConsumeToken(tok::comma));
1156
1157 // Parse ')'.
1158 BDT.consumeClose();
1159}
1160
1161void Parser::parseOMPTraitSetKind(OMPTraitInfo::OMPTraitSet &TISet,
1162 llvm::StringMap<SourceLocation> &Seen) {
1163 TISet.Kind = TraitSet::invalid;
1164
1165 SourceLocation NameLoc = Tok.getLocation();
1166 StringRef Name = getNameFromIdOrString(*this, Tok, CONTEXT_SELECTOR_SET_LVL
1167 );
1168 if (Name.empty()) {
1169 Diag(Tok.getLocation(), diag::note_omp_declare_variant_ctx_options)
1170 << CONTEXT_SELECTOR_SET_LVL << listOpenMPContextTraitSets();
1171 return;
1172 }
1173
1174 TISet.Kind = getOpenMPContextTraitSetKind(Name);
1175 if (TISet.Kind != TraitSet::invalid) {
1176 if (checkForDuplicates(*this, Name, NameLoc, Seen,
1177 CONTEXT_SELECTOR_SET_LVL))
1178 TISet.Kind = TraitSet::invalid;
1179 return;
1180 }
1181
1182 // It follows diagnosis and helping notes.
1183 Diag(NameLoc, diag::warn_omp_declare_variant_ctx_not_a_set) << Name;
1184
1185 TraitSelector SelectorForName = getOpenMPContextTraitSelectorKind(Name);
1186 if (SelectorForName != TraitSelector::invalid) {
1187 Diag(NameLoc, diag::note_omp_declare_variant_ctx_is_a)
1188 << Name << CONTEXT_SELECTOR_LVL << CONTEXT_SELECTOR_SET_LVL;
1189 bool AllowsTraitScore = false;
1190 bool RequiresProperty = false;
1191 isValidTraitSelectorForTraitSet(
1192 SelectorForName, getOpenMPContextTraitSetForSelector(SelectorForName),
1193 AllowsTraitScore, RequiresProperty);
1194 Diag(NameLoc, diag::note_omp_declare_variant_ctx_try)
1195 << getOpenMPContextTraitSetName(
1196 getOpenMPContextTraitSetForSelector(SelectorForName))
1197 << Name << (RequiresProperty ? "(<property-name>)" : "");
1198 return;
1199 }
1200 for (const auto &PotentialSet :
1201 {TraitSet::construct, TraitSet::user, TraitSet::implementation,
1202 TraitSet::device}) {
1203 TraitProperty PropertyForName =
1204 getOpenMPContextTraitPropertyKind(PotentialSet, Name);
1205 if (PropertyForName == TraitProperty::invalid)
1206 continue;
1207 Diag(NameLoc, diag::note_omp_declare_variant_ctx_is_a)
1208 << Name << CONTEXT_TRAIT_LVL << CONTEXT_SELECTOR_SET_LVL;
1209 Diag(NameLoc, diag::note_omp_declare_variant_ctx_try)
1210 << getOpenMPContextTraitSetName(
1211 getOpenMPContextTraitSetForProperty(PropertyForName))
1212 << getOpenMPContextTraitSelectorName(
1213 getOpenMPContextTraitSelectorForProperty(PropertyForName))
1214 << ("(" + Name + ")").str();
1215 return;
1216 }
1217 Diag(NameLoc, diag::note_omp_declare_variant_ctx_options)
1218 << CONTEXT_SELECTOR_SET_LVL << listOpenMPContextTraitSets();
1219}
1220
1221/// Parses an OpenMP context selector set.
1222///
1223/// <trait-set-selector-name> '=' '{' <trait-selector> [, <trait-selector>]* '}'
1224void Parser::parseOMPContextSelectorSet(
1225 OMPTraitInfo::OMPTraitSet &TISet,
1226 llvm::StringMap<SourceLocation> &SeenSets) {
1227 auto OuterBC = BraceCount;
1228
1229 // If anything went wrong we issue an error or warning and then skip the rest
1230 // of the set. However, commas are ambiguous so we look for the nesting
1231 // of braces here as well.
1232 auto FinishSelectorSet = [this, OuterBC]() -> void {
1233 bool Done = false;
1234 while (!Done) {
1235 while (!SkipUntil({tok::comma, tok::r_brace, tok::r_paren,
1236 tok::annot_pragma_openmp_end},
1237 StopBeforeMatch))
1238 ;
1239 if (Tok.is(tok::r_brace) && OuterBC > BraceCount)
1240 (void)ConsumeBrace();
1241 if (OuterBC <= BraceCount) {
1242 Done = true;
1243 break;
1244 }
1245 if (!Tok.is(tok::comma) && !Tok.is(tok::r_brace)) {
1246 Done = true;
1247 break;
1248 }
1249 (void)ConsumeAnyToken();
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001250 }
Johannes Doerfert1228d422019-12-19 20:42:12 -06001251 Diag(Tok.getLocation(), diag::note_omp_declare_variant_ctx_continue_here)
1252 << CONTEXT_SELECTOR_SET_LVL;
1253 };
1254
1255 parseOMPTraitSetKind(TISet, SeenSets);
1256 if (TISet.Kind == TraitSet::invalid)
1257 return FinishSelectorSet();
1258
1259 // Parse '='.
1260 if (!TryConsumeToken(tok::equal))
1261 Diag(Tok.getLocation(), diag::warn_omp_declare_variant_expected)
1262 << "="
1263 << ("context set name \"" + getOpenMPContextTraitSetName(TISet.Kind) +
1264 "\"")
1265 .str();
1266
1267 // Parse '{'.
1268 if (Tok.is(tok::l_brace)) {
1269 (void)ConsumeBrace();
1270 } else {
1271 Diag(Tok.getLocation(), diag::warn_omp_declare_variant_expected)
1272 << "{"
1273 << ("'=' that follows the context set name \"" +
1274 getOpenMPContextTraitSetName(TISet.Kind) + "\"")
1275 .str();
1276 }
1277
1278 llvm::StringMap<SourceLocation> SeenSelectors;
1279 do {
1280 OMPTraitInfo::OMPTraitSelector TISelector;
1281 parseOMPContextSelector(TISelector, TISet.Kind, SeenSelectors);
1282 if (TISelector.Kind != TraitSelector::invalid &&
1283 !TISelector.Properties.empty())
1284 TISet.Selectors.push_back(TISelector);
1285 } while (TryConsumeToken(tok::comma));
1286
1287 // Parse '}'.
1288 if (Tok.is(tok::r_brace)) {
1289 (void)ConsumeBrace();
1290 } else {
1291 Diag(Tok.getLocation(), diag::warn_omp_declare_variant_expected)
1292 << "}"
1293 << ("context selectors for the context set \"" +
1294 getOpenMPContextTraitSetName(TISet.Kind) + "\"")
1295 .str();
1296 }
1297}
1298
1299/// Parse OpenMP context selectors:
1300///
1301/// <trait-set-selector> [, <trait-set-selector>]*
1302bool Parser::parseOMPContextSelectors(SourceLocation Loc, OMPTraitInfo &TI) {
1303 llvm::StringMap<SourceLocation> SeenSets;
1304 do {
1305 OMPTraitInfo::OMPTraitSet TISet;
1306 parseOMPContextSelectorSet(TISet, SeenSets);
1307 if (TISet.Kind != TraitSet::invalid && !TISet.Selectors.empty())
1308 TI.Sets.push_back(TISet);
1309 } while (TryConsumeToken(tok::comma));
1310
Alexey Bataevd158cf62019-09-13 20:18:17 +00001311 return false;
1312}
1313
1314/// Parse clauses for '#pragma omp declare variant ( variant-func-id ) clause'.
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001315void Parser::ParseOMPDeclareVariantClauses(Parser::DeclGroupPtrTy Ptr,
1316 CachedTokens &Toks,
1317 SourceLocation Loc) {
Alexey Bataevd158cf62019-09-13 20:18:17 +00001318 PP.EnterToken(Tok, /*IsReinject*/ true);
1319 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
1320 /*IsReinject*/ true);
1321 // Consume the previously pushed token.
1322 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
1323 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
1324
1325 FNContextRAII FnContext(*this, Ptr);
1326 // Parse function declaration id.
1327 SourceLocation RLoc;
1328 // Parse with IsAddressOfOperand set to true to parse methods as DeclRefExprs
1329 // instead of MemberExprs.
Alexey Bataev02d04d52019-12-10 16:12:53 -05001330 ExprResult AssociatedFunction;
1331 {
1332 // Do not mark function as is used to prevent its emission if this is the
1333 // only place where it is used.
1334 EnterExpressionEvaluationContext Unevaluated(
1335 Actions, Sema::ExpressionEvaluationContext::Unevaluated);
1336 AssociatedFunction = ParseOpenMPParensExpr(
1337 getOpenMPDirectiveName(OMPD_declare_variant), RLoc,
1338 /*IsAddressOfOperand=*/true);
1339 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001340 if (!AssociatedFunction.isUsable()) {
1341 if (!Tok.is(tok::annot_pragma_openmp_end))
1342 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
1343 ;
1344 // Skip the last annot_pragma_openmp_end.
1345 (void)ConsumeAnnotationToken();
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001346 return;
1347 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001348
Johannes Doerfert095cecb2020-02-20 19:50:47 -06001349 OMPTraitInfo TI;
1350 if (parseOMPDeclareVariantMatchClause(Loc, TI))
1351 return;
1352
1353 Optional<std::pair<FunctionDecl *, Expr *>> DeclVarData =
1354 Actions.checkOpenMPDeclareVariantFunction(
1355 Ptr, AssociatedFunction.get(), TI,
1356 SourceRange(Loc, Tok.getLocation()));
1357
1358 // Skip last tokens.
1359 while (Tok.isNot(tok::annot_pragma_openmp_end))
1360 ConsumeAnyToken();
1361 if (DeclVarData && !TI.Sets.empty())
1362 Actions.ActOnOpenMPDeclareVariantDirective(
1363 DeclVarData->first, DeclVarData->second, TI,
1364 SourceRange(Loc, Tok.getLocation()));
1365
1366 // Skip the last annot_pragma_openmp_end.
1367 (void)ConsumeAnnotationToken();
1368}
1369
1370bool Parser::parseOMPDeclareVariantMatchClause(SourceLocation Loc,
1371 OMPTraitInfo &TI) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001372 // Parse 'match'.
Alexey Bataevdba792c2019-09-23 18:13:31 +00001373 OpenMPClauseKind CKind = Tok.isAnnotation()
1374 ? OMPC_unknown
1375 : getOpenMPClauseKind(PP.getSpelling(Tok));
1376 if (CKind != OMPC_match) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001377 Diag(Tok.getLocation(), diag::err_omp_declare_variant_wrong_clause)
Alexey Bataevdba792c2019-09-23 18:13:31 +00001378 << getOpenMPClauseName(OMPC_match);
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001379 while (!SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
1380 ;
1381 // Skip the last annot_pragma_openmp_end.
1382 (void)ConsumeAnnotationToken();
Johannes Doerfert095cecb2020-02-20 19:50:47 -06001383 return true;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001384 }
1385 (void)ConsumeToken();
1386 // Parse '('.
1387 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataevdba792c2019-09-23 18:13:31 +00001388 if (T.expectAndConsume(diag::err_expected_lparen_after,
1389 getOpenMPClauseName(OMPC_match))) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001390 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
1391 ;
1392 // Skip the last annot_pragma_openmp_end.
1393 (void)ConsumeAnnotationToken();
Johannes Doerfert095cecb2020-02-20 19:50:47 -06001394 return true;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001395 }
1396
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001397 // Parse inner context selectors.
Johannes Doerfertb86bf832020-02-15 18:07:42 -06001398 parseOMPContextSelectors(Loc, TI);
Johannes Doerfert1228d422019-12-19 20:42:12 -06001399
1400 // Parse ')'
1401 (void)T.consumeClose();
Johannes Doerfert095cecb2020-02-20 19:50:47 -06001402 return false;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001403}
1404
Alexey Bataev729e2422019-08-23 16:11:14 +00001405/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
1406///
1407/// default-clause:
1408/// 'default' '(' 'none' | 'shared' ')
1409///
1410/// proc_bind-clause:
1411/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1412///
1413/// device_type-clause:
1414/// 'device_type' '(' 'host' | 'nohost' | 'any' )'
1415namespace {
1416 struct SimpleClauseData {
1417 unsigned Type;
1418 SourceLocation Loc;
1419 SourceLocation LOpen;
1420 SourceLocation TypeLoc;
1421 SourceLocation RLoc;
1422 SimpleClauseData(unsigned Type, SourceLocation Loc, SourceLocation LOpen,
1423 SourceLocation TypeLoc, SourceLocation RLoc)
1424 : Type(Type), Loc(Loc), LOpen(LOpen), TypeLoc(TypeLoc), RLoc(RLoc) {}
1425 };
1426} // anonymous namespace
1427
1428static Optional<SimpleClauseData>
1429parseOpenMPSimpleClause(Parser &P, OpenMPClauseKind Kind) {
1430 const Token &Tok = P.getCurToken();
1431 SourceLocation Loc = Tok.getLocation();
1432 SourceLocation LOpen = P.ConsumeToken();
1433 // Parse '('.
1434 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
1435 if (T.expectAndConsume(diag::err_expected_lparen_after,
1436 getOpenMPClauseName(Kind)))
1437 return llvm::None;
1438
1439 unsigned Type = getOpenMPSimpleClauseType(
1440 Kind, Tok.isAnnotation() ? "" : P.getPreprocessor().getSpelling(Tok));
1441 SourceLocation TypeLoc = Tok.getLocation();
1442 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1443 Tok.isNot(tok::annot_pragma_openmp_end))
1444 P.ConsumeAnyToken();
1445
1446 // Parse ')'.
1447 SourceLocation RLoc = Tok.getLocation();
1448 if (!T.consumeClose())
1449 RLoc = T.getCloseLocation();
1450
1451 return SimpleClauseData(Type, Loc, LOpen, TypeLoc, RLoc);
1452}
1453
Kelvin Lie0502752018-11-21 20:15:57 +00001454Parser::DeclGroupPtrTy Parser::ParseOMPDeclareTargetClauses() {
1455 // OpenMP 4.5 syntax with list of entities.
1456 Sema::NamedDeclSetType SameDirectiveDecls;
Alexey Bataev729e2422019-08-23 16:11:14 +00001457 SmallVector<std::tuple<OMPDeclareTargetDeclAttr::MapTypeTy, SourceLocation,
1458 NamedDecl *>,
1459 4>
1460 DeclareTargetDecls;
1461 OMPDeclareTargetDeclAttr::DevTypeTy DT = OMPDeclareTargetDeclAttr::DT_Any;
1462 SourceLocation DeviceTypeLoc;
Kelvin Lie0502752018-11-21 20:15:57 +00001463 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1464 OMPDeclareTargetDeclAttr::MapTypeTy MT = OMPDeclareTargetDeclAttr::MT_To;
1465 if (Tok.is(tok::identifier)) {
1466 IdentifierInfo *II = Tok.getIdentifierInfo();
1467 StringRef ClauseName = II->getName();
Alexey Bataev729e2422019-08-23 16:11:14 +00001468 bool IsDeviceTypeClause =
1469 getLangOpts().OpenMP >= 50 &&
1470 getOpenMPClauseKind(ClauseName) == OMPC_device_type;
1471 // Parse 'to|link|device_type' clauses.
1472 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName, MT) &&
1473 !IsDeviceTypeClause) {
1474 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
1475 << ClauseName << (getLangOpts().OpenMP >= 50 ? 1 : 0);
Kelvin Lie0502752018-11-21 20:15:57 +00001476 break;
1477 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001478 // Parse 'device_type' clause and go to next clause if any.
1479 if (IsDeviceTypeClause) {
1480 Optional<SimpleClauseData> DevTypeData =
1481 parseOpenMPSimpleClause(*this, OMPC_device_type);
1482 if (DevTypeData.hasValue()) {
1483 if (DeviceTypeLoc.isValid()) {
1484 // We already saw another device_type clause, diagnose it.
1485 Diag(DevTypeData.getValue().Loc,
1486 diag::warn_omp_more_one_device_type_clause);
1487 }
1488 switch(static_cast<OpenMPDeviceType>(DevTypeData.getValue().Type)) {
1489 case OMPC_DEVICE_TYPE_any:
1490 DT = OMPDeclareTargetDeclAttr::DT_Any;
1491 break;
1492 case OMPC_DEVICE_TYPE_host:
1493 DT = OMPDeclareTargetDeclAttr::DT_Host;
1494 break;
1495 case OMPC_DEVICE_TYPE_nohost:
1496 DT = OMPDeclareTargetDeclAttr::DT_NoHost;
1497 break;
1498 case OMPC_DEVICE_TYPE_unknown:
1499 llvm_unreachable("Unexpected device_type");
1500 }
1501 DeviceTypeLoc = DevTypeData.getValue().Loc;
1502 }
1503 continue;
1504 }
Kelvin Lie0502752018-11-21 20:15:57 +00001505 ConsumeToken();
1506 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001507 auto &&Callback = [this, MT, &DeclareTargetDecls, &SameDirectiveDecls](
1508 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
1509 NamedDecl *ND = Actions.lookupOpenMPDeclareTargetName(
1510 getCurScope(), SS, NameInfo, SameDirectiveDecls);
1511 if (ND)
1512 DeclareTargetDecls.emplace_back(MT, NameInfo.getLoc(), ND);
Kelvin Lie0502752018-11-21 20:15:57 +00001513 };
1514 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
1515 /*AllowScopeSpecifier=*/true))
1516 break;
1517
1518 // Consume optional ','.
1519 if (Tok.is(tok::comma))
1520 ConsumeToken();
1521 }
1522 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1523 ConsumeAnyToken();
Alexey Bataev729e2422019-08-23 16:11:14 +00001524 for (auto &MTLocDecl : DeclareTargetDecls) {
1525 OMPDeclareTargetDeclAttr::MapTypeTy MT;
1526 SourceLocation Loc;
1527 NamedDecl *ND;
1528 std::tie(MT, Loc, ND) = MTLocDecl;
1529 // device_type clause is applied only to functions.
1530 Actions.ActOnOpenMPDeclareTargetName(
1531 ND, Loc, MT, isa<VarDecl>(ND) ? OMPDeclareTargetDeclAttr::DT_Any : DT);
1532 }
Kelvin Lie0502752018-11-21 20:15:57 +00001533 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
1534 SameDirectiveDecls.end());
1535 if (Decls.empty())
1536 return DeclGroupPtrTy();
1537 return Actions.BuildDeclaratorGroup(Decls);
1538}
1539
Johannes Doerfert56d15532020-02-21 13:48:56 -06001540void Parser::skipUntilPragmaOpenMPEnd(OpenMPDirectiveKind DKind) {
1541 // The last seen token is annot_pragma_openmp_end - need to check for
1542 // extra tokens.
1543 if (Tok.is(tok::annot_pragma_openmp_end))
1544 return;
1545
1546 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1547 << getOpenMPDirectiveName(DKind);
1548 while (Tok.isNot(tok::annot_pragma_openmp_end))
1549 ConsumeAnyToken();
1550}
1551
Johannes Doerfert095cecb2020-02-20 19:50:47 -06001552void Parser::parseOMPEndDirective(OpenMPDirectiveKind BeginKind,
1553 OpenMPDirectiveKind ExpectedKind,
1554 OpenMPDirectiveKind FoundKind,
1555 SourceLocation BeginLoc,
1556 SourceLocation FoundLoc,
1557 bool SkipUntilOpenMPEnd) {
1558 int DiagSelection = ExpectedKind == OMPD_end_declare_target ? 0 : 1;
1559
1560 if (FoundKind == ExpectedKind) {
1561 ConsumeAnyToken();
1562 skipUntilPragmaOpenMPEnd(ExpectedKind);
Kelvin Lie0502752018-11-21 20:15:57 +00001563 return;
1564 }
Johannes Doerfert095cecb2020-02-20 19:50:47 -06001565
1566 Diag(FoundLoc, diag::err_expected_end_declare_target_or_variant)
1567 << DiagSelection;
1568 Diag(BeginLoc, diag::note_matching)
1569 << ("'#pragma omp " + getOpenMPDirectiveName(BeginKind) + "'").str();
1570 if (SkipUntilOpenMPEnd)
1571 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1572}
1573
1574void Parser::ParseOMPEndDeclareTargetDirective(OpenMPDirectiveKind DKind,
1575 SourceLocation DKLoc) {
1576 parseOMPEndDirective(OMPD_declare_target, OMPD_end_declare_target, DKind,
1577 DKLoc, Tok.getLocation(),
1578 /* SkipUntilOpenMPEnd */ false);
Kelvin Lie0502752018-11-21 20:15:57 +00001579 // Skip the last annot_pragma_openmp_end.
Johannes Doerfert095cecb2020-02-20 19:50:47 -06001580 if (Tok.is(tok::annot_pragma_openmp_end))
1581 ConsumeAnnotationToken();
Kelvin Lie0502752018-11-21 20:15:57 +00001582}
1583
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001584/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001585///
1586/// threadprivate-directive:
1587/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001588/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +00001589///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001590/// allocate-directive:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001591/// annot_pragma_openmp 'allocate' simple-variable-list [<clause>]
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001592/// annot_pragma_openmp_end
1593///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001594/// declare-reduction-directive:
1595/// annot_pragma_openmp 'declare' 'reduction' [...]
1596/// annot_pragma_openmp_end
1597///
Michael Kruse251e1482019-02-01 20:25:04 +00001598/// declare-mapper-directive:
1599/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1600/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1601/// annot_pragma_openmp_end
1602///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001603/// declare-simd-directive:
1604/// annot_pragma_openmp 'declare simd' {<clause> [,]}
1605/// annot_pragma_openmp_end
1606/// <function declaration/definition>
1607///
Kelvin Li1408f912018-09-26 04:28:39 +00001608/// requires directive:
1609/// annot_pragma_openmp 'requires' <clause> [[[,] <clause>] ... ]
1610/// annot_pragma_openmp_end
1611///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001612Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
Alexey Bataevc972f6f2020-01-07 13:39:18 -05001613 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs, bool Delayed,
Alexey Bataev587e1de2016-03-30 10:43:55 +00001614 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001615 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataev8035bb42019-12-13 16:05:30 -05001616 ParsingOpenMPDirectiveRAII DirScope(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001617 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +00001618
Alexey Bataevc972f6f2020-01-07 13:39:18 -05001619 SourceLocation Loc;
1620 OpenMPDirectiveKind DKind;
1621 if (Delayed) {
1622 TentativeParsingAction TPA(*this);
1623 Loc = ConsumeAnnotationToken();
1624 DKind = parseOpenMPDirectiveKind(*this);
1625 if (DKind == OMPD_declare_reduction || DKind == OMPD_declare_mapper) {
1626 // Need to delay parsing until completion of the parent class.
1627 TPA.Revert();
1628 CachedTokens Toks;
1629 unsigned Cnt = 1;
1630 Toks.push_back(Tok);
1631 while (Cnt && Tok.isNot(tok::eof)) {
1632 (void)ConsumeAnyToken();
1633 if (Tok.is(tok::annot_pragma_openmp))
1634 ++Cnt;
1635 else if (Tok.is(tok::annot_pragma_openmp_end))
1636 --Cnt;
1637 Toks.push_back(Tok);
1638 }
1639 // Skip last annot_pragma_openmp_end.
1640 if (Cnt == 0)
1641 (void)ConsumeAnyToken();
1642 auto *LP = new LateParsedPragma(this, AS);
1643 LP->takeToks(Toks);
1644 getCurrentClass().LateParsedDeclarations.push_back(LP);
1645 return nullptr;
1646 }
1647 TPA.Commit();
1648 } else {
1649 Loc = ConsumeAnnotationToken();
1650 DKind = parseOpenMPDirectiveKind(*this);
1651 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001652
1653 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001654 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +00001655 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001656 DeclDirectiveListParserHelper Helper(this, DKind);
1657 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1658 /*AllowScopeSpecifier=*/true)) {
Johannes Doerfert56d15532020-02-21 13:48:56 -06001659 skipUntilPragmaOpenMPEnd(DKind);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001660 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001661 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001662 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
1663 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +00001664 }
1665 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001666 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001667 case OMPD_allocate: {
1668 ConsumeToken();
1669 DeclDirectiveListParserHelper Helper(this, DKind);
1670 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1671 /*AllowScopeSpecifier=*/true)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001672 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001673 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001674 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1675 OMPC_unknown + 1>
1676 FirstClauses(OMPC_unknown + 1);
1677 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1678 OpenMPClauseKind CKind =
1679 Tok.isAnnotation() ? OMPC_unknown
1680 : getOpenMPClauseKind(PP.getSpelling(Tok));
1681 Actions.StartOpenMPClause(CKind);
1682 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1683 !FirstClauses[CKind].getInt());
1684 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1685 StopBeforeMatch);
1686 FirstClauses[CKind].setInt(true);
1687 if (Clause != nullptr)
1688 Clauses.push_back(Clause);
1689 if (Tok.is(tok::annot_pragma_openmp_end)) {
1690 Actions.EndOpenMPClause();
1691 break;
1692 }
1693 // Skip ',' if any.
1694 if (Tok.is(tok::comma))
1695 ConsumeToken();
1696 Actions.EndOpenMPClause();
1697 }
Johannes Doerfert56d15532020-02-21 13:48:56 -06001698 skipUntilPragmaOpenMPEnd(DKind);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001699 }
1700 // Skip the last annot_pragma_openmp_end.
1701 ConsumeAnnotationToken();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001702 return Actions.ActOnOpenMPAllocateDirective(Loc, Helper.getIdentifiers(),
1703 Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001704 }
1705 break;
1706 }
Kelvin Li1408f912018-09-26 04:28:39 +00001707 case OMPD_requires: {
1708 SourceLocation StartLoc = ConsumeToken();
1709 SmallVector<OMPClause *, 5> Clauses;
1710 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
1711 FirstClauses(OMPC_unknown + 1);
1712 if (Tok.is(tok::annot_pragma_openmp_end)) {
Ilya Biryukovff2a9972019-02-26 11:01:50 +00001713 Diag(Tok, diag::err_omp_expected_clause)
Kelvin Li1408f912018-09-26 04:28:39 +00001714 << getOpenMPDirectiveName(OMPD_requires);
1715 break;
1716 }
1717 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1718 OpenMPClauseKind CKind = Tok.isAnnotation()
1719 ? OMPC_unknown
1720 : getOpenMPClauseKind(PP.getSpelling(Tok));
1721 Actions.StartOpenMPClause(CKind);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001722 OMPClause *Clause = ParseOpenMPClause(OMPD_requires, CKind,
1723 !FirstClauses[CKind].getInt());
1724 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1725 StopBeforeMatch);
Kelvin Li1408f912018-09-26 04:28:39 +00001726 FirstClauses[CKind].setInt(true);
1727 if (Clause != nullptr)
1728 Clauses.push_back(Clause);
1729 if (Tok.is(tok::annot_pragma_openmp_end)) {
1730 Actions.EndOpenMPClause();
1731 break;
1732 }
1733 // Skip ',' if any.
1734 if (Tok.is(tok::comma))
1735 ConsumeToken();
1736 Actions.EndOpenMPClause();
1737 }
1738 // Consume final annot_pragma_openmp_end
Alexey Bataev2d4f80f2020-02-11 15:15:21 -05001739 if (Clauses.empty()) {
Kelvin Li1408f912018-09-26 04:28:39 +00001740 Diag(Tok, diag::err_omp_expected_clause)
1741 << getOpenMPDirectiveName(OMPD_requires);
1742 ConsumeAnnotationToken();
1743 return nullptr;
1744 }
1745 ConsumeAnnotationToken();
1746 return Actions.ActOnOpenMPRequiresDirective(StartLoc, Clauses);
1747 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001748 case OMPD_declare_reduction:
1749 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001750 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Johannes Doerfert56d15532020-02-21 13:48:56 -06001751 skipUntilPragmaOpenMPEnd(OMPD_declare_reduction);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001752 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001753 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001754 return Res;
1755 }
1756 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001757 case OMPD_declare_mapper: {
1758 ConsumeToken();
1759 if (DeclGroupPtrTy Res = ParseOpenMPDeclareMapperDirective(AS)) {
1760 // Skip the last annot_pragma_openmp_end.
1761 ConsumeAnnotationToken();
1762 return Res;
1763 }
1764 break;
1765 }
Johannes Doerfert095cecb2020-02-20 19:50:47 -06001766 case OMPD_begin_declare_variant: {
1767 // The syntax is:
1768 // { #pragma omp begin declare variant clause }
1769 // <function-declaration-or-definition-sequence>
1770 // { #pragma omp end declare variant }
1771 //
1772 ConsumeToken();
1773 OMPTraitInfo &TI = Actions.getASTContext().getNewOMPTraitInfo();
1774 if (parseOMPDeclareVariantMatchClause(Loc, TI))
1775 break;
1776
1777 // Skip last tokens.
1778 skipUntilPragmaOpenMPEnd(OMPD_begin_declare_variant);
1779
1780 VariantMatchInfo VMI;
1781 ASTContext &ASTCtx = Actions.getASTContext();
1782 TI.getAsVariantMatchInfo(ASTCtx, VMI, /* DeviceSetOnly */ true);
1783 OMPContext OMPCtx(ASTCtx.getLangOpts().OpenMPIsDevice,
1784 ASTCtx.getTargetInfo().getTriple());
1785
1786 if (isVariantApplicableInContext(VMI, OMPCtx))
1787 break;
1788
1789 // Elide all the code till the matching end declare variant was found.
1790 unsigned Nesting = 1;
1791 SourceLocation DKLoc;
1792 OpenMPDirectiveKind DK = OMPD_unknown;
1793 do {
1794 DKLoc = Tok.getLocation();
1795 DK = parseOpenMPDirectiveKind(*this);
1796 if (DK == OMPD_end_declare_variant)
1797 --Nesting;
1798 else if (DK == OMPD_begin_declare_variant)
1799 ++Nesting;
1800 if (!Nesting || isEofOrEom())
1801 break;
1802 ConsumeAnyToken();
1803 } while (true);
1804
1805 parseOMPEndDirective(OMPD_begin_declare_variant, OMPD_end_declare_variant,
1806 DK, Loc, DKLoc, /* SkipUntilOpenMPEnd */ true);
1807 if (isEofOrEom())
1808 return nullptr;
1809 break;
1810 }
1811 case OMPD_end_declare_variant:
1812 // FIXME: With the sema changes we will keep track of nesting and be able to
1813 // diagnose unmatchend OMPD_end_declare_variant.
1814 ConsumeToken();
1815 break;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001816 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001817 case OMPD_declare_simd: {
1818 // The syntax is:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001819 // { #pragma omp declare {simd|variant} }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001820 // <function-declaration-or-definition>
1821 //
Alexey Bataev2af33e32016-04-07 12:45:37 +00001822 CachedTokens Toks;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001823 Toks.push_back(Tok);
1824 ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001825 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
1826 Toks.push_back(Tok);
1827 ConsumeAnyToken();
1828 }
1829 Toks.push_back(Tok);
1830 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +00001831
1832 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +00001833 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataevc972f6f2020-01-07 13:39:18 -05001834 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, Delayed,
1835 TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +00001836 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001837 // Here we expect to see some function declaration.
1838 if (AS == AS_none) {
1839 assert(TagType == DeclSpec::TST_unspecified);
1840 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001841 ParsingDeclSpec PDS(*this);
1842 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1843 } else {
1844 Ptr =
1845 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1846 }
1847 }
1848 if (!Ptr) {
Alexey Bataevd158cf62019-09-13 20:18:17 +00001849 Diag(Loc, diag::err_omp_decl_in_declare_simd_variant)
1850 << (DKind == OMPD_declare_simd ? 0 : 1);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001851 return DeclGroupPtrTy();
1852 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001853 if (DKind == OMPD_declare_simd)
1854 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
1855 assert(DKind == OMPD_declare_variant &&
1856 "Expected declare variant directive only");
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001857 ParseOMPDeclareVariantClauses(Ptr, Toks, Loc);
1858 return Ptr;
Alexey Bataev587e1de2016-03-30 10:43:55 +00001859 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001860 case OMPD_declare_target: {
1861 SourceLocation DTLoc = ConsumeAnyToken();
1862 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Kelvin Lie0502752018-11-21 20:15:57 +00001863 return ParseOMPDeclareTargetClauses();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001864 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001865
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001866 // Skip the last annot_pragma_openmp_end.
1867 ConsumeAnyToken();
1868
1869 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
1870 return DeclGroupPtrTy();
1871
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001872 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +00001873 DKind = parseOpenMPDirectiveKind(*this);
Kelvin Libc38e632018-09-10 02:07:09 +00001874 while (DKind != OMPD_end_declare_target && Tok.isNot(tok::eof) &&
1875 Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +00001876 DeclGroupPtrTy Ptr;
1877 // Here we expect to see some function declaration.
1878 if (AS == AS_none) {
1879 assert(TagType == DeclSpec::TST_unspecified);
1880 MaybeParseCXX11Attributes(Attrs);
1881 ParsingDeclSpec PDS(*this);
1882 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1883 } else {
1884 Ptr =
1885 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1886 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001887 if (Ptr) {
1888 DeclGroupRef Ref = Ptr.get();
1889 Decls.append(Ref.begin(), Ref.end());
1890 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001891 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
1892 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +00001893 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001894 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001895 if (DKind != OMPD_end_declare_target)
1896 TPA.Revert();
1897 else
1898 TPA.Commit();
1899 }
1900 }
1901
Kelvin Lie0502752018-11-21 20:15:57 +00001902 ParseOMPEndDeclareTargetDirective(DKind, DTLoc);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001903 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +00001904 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001905 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001906 case OMPD_unknown:
1907 Diag(Tok, diag::err_omp_unknown_directive);
1908 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001909 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001910 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001911 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +00001912 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001913 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001914 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001915 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +00001916 case OMPD_flush:
Alexey Bataevc112e942020-02-28 09:52:15 -05001917 case OMPD_depobj:
Alexey Bataevfcba7c32020-03-20 07:03:01 -04001918 case OMPD_scan:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001919 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001920 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001921 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001922 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001923 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +00001924 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001925 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001926 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001927 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001928 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001929 case OMPD_parallel_sections:
cchen47d60942019-12-05 13:43:48 -05001930 case OMPD_parallel_master:
Alexey Bataev0162e452014-07-22 10:10:35 +00001931 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001932 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001933 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001934 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001935 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +00001936 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001937 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001938 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001939 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001940 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001941 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001942 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +00001943 case OMPD_master_taskloop:
Alexey Bataevb8552ab2019-10-18 16:47:35 +00001944 case OMPD_master_taskloop_simd:
Alexey Bataev5bbcead2019-10-14 17:17:41 +00001945 case OMPD_parallel_master_taskloop:
Alexey Bataev14a388f2019-10-25 10:27:13 -04001946 case OMPD_parallel_master_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001947 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001948 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001949 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001950 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +00001951 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001952 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001953 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001954 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001955 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +00001956 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +00001957 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001958 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +00001959 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +00001960 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +00001961 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +00001962 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +00001963 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +00001964 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001965 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +00001966 break;
1967 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001968 while (Tok.isNot(tok::annot_pragma_openmp_end))
1969 ConsumeAnyToken();
1970 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +00001971 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001972}
1973
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001974/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001975///
1976/// threadprivate-directive:
1977/// annot_pragma_openmp 'threadprivate' simple-variable-list
1978/// annot_pragma_openmp_end
1979///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001980/// allocate-directive:
1981/// annot_pragma_openmp 'allocate' simple-variable-list
1982/// annot_pragma_openmp_end
1983///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001984/// declare-reduction-directive:
1985/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
1986/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
1987/// ('omp_priv' '=' <expression>|<function_call>) ')']
1988/// annot_pragma_openmp_end
1989///
Michael Kruse251e1482019-02-01 20:25:04 +00001990/// declare-mapper-directive:
1991/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1992/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1993/// annot_pragma_openmp_end
1994///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001995/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001996/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001997/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
cchen47d60942019-12-05 13:43:48 -05001998/// 'parallel for' | 'parallel sections' | 'parallel master' | 'task' |
1999/// 'taskyield' | 'barrier' | 'taskwait' | 'flush' | 'ordered' |
2000/// 'atomic' | 'for simd' | 'parallel for simd' | 'target' | 'target
2001/// data' | 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
2002/// 'master taskloop' | 'master taskloop simd' | 'parallel master
2003/// taskloop' | 'parallel master taskloop simd' | 'distribute' | 'target
2004/// enter data' | 'target exit data' | 'target parallel' | 'target
2005/// parallel for' | 'target update' | 'distribute parallel for' |
2006/// 'distribute paralle for simd' | 'distribute simd' | 'target parallel
2007/// for simd' | 'target simd' | 'teams distribute' | 'teams distribute
2008/// simd' | 'teams distribute parallel for simd' | 'teams distribute
2009/// parallel for' | 'target teams' | 'target teams distribute' | 'target
2010/// teams distribute parallel for' | 'target teams distribute parallel
2011/// for simd' | 'target teams distribute simd' {clause}
Alexey Bataev14a388f2019-10-25 10:27:13 -04002012/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002013///
Richard Smitha6e8d5e2019-02-15 00:27:53 +00002014StmtResult
2015Parser::ParseOpenMPDeclarativeOrExecutableDirective(ParsedStmtContext StmtCtx) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002016 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataev8035bb42019-12-13 16:05:30 -05002017 ParsingOpenMPDirectiveRAII DirScope(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002018 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002019 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002020 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +00002021 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +00002022 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
2023 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +00002024 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +00002025 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002026 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002027 // Name of critical directive.
2028 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002029 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +00002030 bool HasAssociatedStatement = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002031
2032 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002033 case OMPD_threadprivate: {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00002034 // FIXME: Should this be permitted in C++?
2035 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
2036 ParsedStmtContext()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00002037 Diag(Tok, diag::err_omp_immediate_directive)
2038 << getOpenMPDirectiveName(DKind) << 0;
2039 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002040 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002041 DeclDirectiveListParserHelper Helper(this, DKind);
2042 if (!ParseOpenMPSimpleVarList(DKind, Helper,
2043 /*AllowScopeSpecifier=*/false)) {
Johannes Doerfert56d15532020-02-21 13:48:56 -06002044 skipUntilPragmaOpenMPEnd(DKind);
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002045 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
2046 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002047 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
2048 }
Alp Tokerd751fa72013-12-18 19:10:49 +00002049 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002050 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002051 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002052 case OMPD_allocate: {
2053 // FIXME: Should this be permitted in C++?
2054 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
2055 ParsedStmtContext()) {
2056 Diag(Tok, diag::err_omp_immediate_directive)
2057 << getOpenMPDirectiveName(DKind) << 0;
2058 }
2059 ConsumeToken();
2060 DeclDirectiveListParserHelper Helper(this, DKind);
2061 if (!ParseOpenMPSimpleVarList(DKind, Helper,
2062 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002063 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002064 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002065 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
2066 OMPC_unknown + 1>
2067 FirstClauses(OMPC_unknown + 1);
2068 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
2069 OpenMPClauseKind CKind =
2070 Tok.isAnnotation() ? OMPC_unknown
2071 : getOpenMPClauseKind(PP.getSpelling(Tok));
2072 Actions.StartOpenMPClause(CKind);
2073 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
2074 !FirstClauses[CKind].getInt());
2075 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
2076 StopBeforeMatch);
2077 FirstClauses[CKind].setInt(true);
2078 if (Clause != nullptr)
2079 Clauses.push_back(Clause);
2080 if (Tok.is(tok::annot_pragma_openmp_end)) {
2081 Actions.EndOpenMPClause();
2082 break;
2083 }
2084 // Skip ',' if any.
2085 if (Tok.is(tok::comma))
2086 ConsumeToken();
2087 Actions.EndOpenMPClause();
2088 }
Johannes Doerfert56d15532020-02-21 13:48:56 -06002089 skipUntilPragmaOpenMPEnd(DKind);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002090 }
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002091 DeclGroupPtrTy Res = Actions.ActOnOpenMPAllocateDirective(
2092 Loc, Helper.getIdentifiers(), Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002093 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
2094 }
2095 SkipUntil(tok::annot_pragma_openmp_end);
2096 break;
2097 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002098 case OMPD_declare_reduction:
2099 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00002100 if (DeclGroupPtrTy Res =
2101 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Johannes Doerfert56d15532020-02-21 13:48:56 -06002102 skipUntilPragmaOpenMPEnd(OMPD_declare_reduction);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002103 ConsumeAnyToken();
2104 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +00002105 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002106 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +00002107 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002108 break;
Michael Kruse251e1482019-02-01 20:25:04 +00002109 case OMPD_declare_mapper: {
2110 ConsumeToken();
2111 if (DeclGroupPtrTy Res =
2112 ParseOpenMPDeclareMapperDirective(/*AS=*/AS_none)) {
2113 // Skip the last annot_pragma_openmp_end.
2114 ConsumeAnnotationToken();
2115 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
2116 } else {
2117 SkipUntil(tok::annot_pragma_openmp_end);
2118 }
2119 break;
2120 }
Alexey Bataev6125da92014-07-21 11:26:11 +00002121 case OMPD_flush:
Alexey Bataevc112e942020-02-28 09:52:15 -05002122 case OMPD_depobj:
Alexey Bataevfcba7c32020-03-20 07:03:01 -04002123 case OMPD_scan:
Alexey Bataev68446b72014-07-18 07:47:19 +00002124 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002125 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00002126 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002127 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00002128 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +00002129 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00002130 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +00002131 case OMPD_target_update:
Richard Smitha6e8d5e2019-02-15 00:27:53 +00002132 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
2133 ParsedStmtContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00002134 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +00002135 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +00002136 }
2137 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002138 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +00002139 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002140 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +00002141 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002142 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00002143 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002144 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002145 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002146 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00002147 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002148 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002149 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00002150 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002151 case OMPD_parallel_sections:
cchen47d60942019-12-05 13:43:48 -05002152 case OMPD_parallel_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002153 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00002154 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002155 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00002156 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002157 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00002158 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00002159 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002160 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002161 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002162 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002163 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +00002164 case OMPD_master_taskloop:
Alexey Bataevb8552ab2019-10-18 16:47:35 +00002165 case OMPD_master_taskloop_simd:
Alexey Bataev5bbcead2019-10-14 17:17:41 +00002166 case OMPD_parallel_master_taskloop:
Alexey Bataev14a388f2019-10-25 10:27:13 -04002167 case OMPD_parallel_master_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00002168 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00002169 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00002170 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00002171 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00002172 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00002173 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00002174 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00002175 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00002176 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00002177 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00002178 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00002179 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00002180 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00002181 case OMPD_target_teams_distribute_parallel_for_simd:
2182 case OMPD_target_teams_distribute_simd: {
Alexey Bataevc112e942020-02-28 09:52:15 -05002183 // Special processing for flush and depobj clauses.
2184 Token ImplicitTok;
2185 bool ImplicitClauseAllowed = false;
2186 if (DKind == OMPD_flush || DKind == OMPD_depobj) {
2187 ImplicitTok = Tok;
2188 ImplicitClauseAllowed = true;
2189 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002190 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002191 // Parse directive name of the 'critical' directive if any.
2192 if (DKind == OMPD_critical) {
2193 BalancedDelimiterTracker T(*this, tok::l_paren,
2194 tok::annot_pragma_openmp_end);
2195 if (!T.consumeOpen()) {
2196 if (Tok.isAnyIdentifier()) {
2197 DirName =
2198 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
2199 ConsumeAnyToken();
2200 } else {
2201 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
2202 }
2203 T.consumeClose();
2204 }
Alexey Bataev80909872015-07-02 11:25:17 +00002205 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00002206 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002207 if (Tok.isNot(tok::annot_pragma_openmp_end))
2208 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002209 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002210
Alexey Bataevf29276e2014-06-18 04:14:57 +00002211 if (isOpenMPLoopDirective(DKind))
2212 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
2213 if (isOpenMPSimdDirective(DKind))
2214 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
2215 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00002216 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002217
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002218 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataevc112e942020-02-28 09:52:15 -05002219 bool HasImplicitClause = false;
2220 if (ImplicitClauseAllowed && Tok.is(tok::l_paren)) {
2221 HasImplicitClause = true;
Alexey Bataevea9166b2020-02-06 16:30:23 -05002222 // Push copy of the current token back to stream to properly parse
Alexey Bataevc112e942020-02-28 09:52:15 -05002223 // pseudo-clause OMPFlushClause or OMPDepobjClause.
Alexey Bataevea9166b2020-02-06 16:30:23 -05002224 PP.EnterToken(Tok, /*IsReinject*/ true);
Alexey Bataevc112e942020-02-28 09:52:15 -05002225 PP.EnterToken(ImplicitTok, /*IsReinject*/ true);
Alexey Bataevea9166b2020-02-06 16:30:23 -05002226 ConsumeAnyToken();
2227 }
Alexey Bataevc112e942020-02-28 09:52:15 -05002228 OpenMPClauseKind CKind = Tok.isAnnotation()
2229 ? OMPC_unknown
2230 : getOpenMPClauseKind(PP.getSpelling(Tok));
2231 if (HasImplicitClause) {
2232 assert(CKind == OMPC_unknown && "Must be unknown implicit clause.");
2233 if (DKind == OMPD_flush) {
2234 CKind = OMPC_flush;
2235 } else {
2236 assert(DKind == OMPD_depobj &&
2237 "Expected flush or depobj directives.");
2238 CKind = OMPC_depobj;
2239 }
2240 }
2241 // No more implicit clauses allowed.
2242 ImplicitClauseAllowed = false;
Alexey Bataevaac108a2015-06-23 04:51:00 +00002243 Actions.StartOpenMPClause(CKind);
Alexey Bataevc112e942020-02-28 09:52:15 -05002244 HasImplicitClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00002245 OMPClause *Clause =
2246 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002247 FirstClauses[CKind].setInt(true);
2248 if (Clause) {
2249 FirstClauses[CKind].setPointer(Clause);
2250 Clauses.push_back(Clause);
2251 }
2252
2253 // Skip ',' if any.
2254 if (Tok.is(tok::comma))
2255 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00002256 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002257 }
2258 // End location of the directive.
2259 EndLoc = Tok.getLocation();
2260 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00002261 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002262
Alexey Bataeveb482352015-12-18 05:05:56 +00002263 // OpenMP [2.13.8, ordered Construct, Syntax]
2264 // If the depend clause is specified, the ordered construct is a stand-alone
2265 // directive.
2266 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00002267 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
2268 ParsedStmtContext()) {
Alexey Bataeveb482352015-12-18 05:05:56 +00002269 Diag(Loc, diag::err_omp_immediate_directive)
2270 << getOpenMPDirectiveName(DKind) << 1
2271 << getOpenMPClauseName(OMPC_depend);
2272 }
2273 HasAssociatedStatement = false;
2274 }
2275
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002276 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00002277 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002278 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00002279 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00002280 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
2281 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
2282 // should have at least one compound statement scope within it.
2283 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002284 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00002285 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
2286 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00002287 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00002288 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
2289 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
2290 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00002291 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002292 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002293 Directive = Actions.ActOnOpenMPExecutableDirective(
2294 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
2295 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002296
2297 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00002298 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002299 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002300 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00002301 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00002302 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00002303 case OMPD_declare_target:
2304 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00002305 case OMPD_requires:
Johannes Doerfert095cecb2020-02-20 19:50:47 -06002306 case OMPD_begin_declare_variant:
2307 case OMPD_end_declare_variant:
Alexey Bataevd158cf62019-09-13 20:18:17 +00002308 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00002309 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00002310 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00002311 SkipUntil(tok::annot_pragma_openmp_end);
2312 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002313 case OMPD_unknown:
2314 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00002315 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002316 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002317 }
2318 return Directive;
2319}
2320
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002321// Parses simple list:
2322// simple-variable-list:
2323// '(' id-expression {, id-expression} ')'
2324//
2325bool Parser::ParseOpenMPSimpleVarList(
2326 OpenMPDirectiveKind Kind,
2327 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
2328 Callback,
2329 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002330 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00002331 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002332 if (T.expectAndConsume(diag::err_expected_lparen_after,
Johannes Doerferteb3e81f2019-11-04 22:00:49 -06002333 getOpenMPDirectiveName(Kind).data()))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002334 return true;
2335 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002336 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00002337
2338 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002339 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002340 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00002341 UnqualifiedId Name;
2342 // Read var name.
2343 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002344 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00002345
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002346 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
Haojian Wu0dd0b102020-03-19 09:12:29 +01002347 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
2348 /*ObjectHadErrors=*/false, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002349 IsCorrect = false;
2350 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00002351 StopBeforeMatch);
Haojian Wu0dd0b102020-03-19 09:12:29 +01002352 } else if (ParseUnqualifiedId(SS, /*ObjectType=*/nullptr,
2353 /*ObjectHadErrors=*/false, false, false,
2354 false, false, nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002355 IsCorrect = false;
2356 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00002357 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002358 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
2359 Tok.isNot(tok::annot_pragma_openmp_end)) {
2360 IsCorrect = false;
2361 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00002362 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00002363 Diag(PrevTok.getLocation(), diag::err_expected)
2364 << tok::identifier
2365 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00002366 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002367 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00002368 }
2369 // Consume ','.
2370 if (Tok.is(tok::comma)) {
2371 ConsumeToken();
2372 }
Alexey Bataeva769e072013-03-22 06:34:35 +00002373 }
2374
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002375 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00002376 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002377 IsCorrect = false;
2378 }
2379
2380 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002381 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002382
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002383 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00002384}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002385
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002386/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002387///
2388/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00002389/// if-clause | final-clause | num_threads-clause | safelen-clause |
2390/// default-clause | private-clause | firstprivate-clause | shared-clause
2391/// | linear-clause | aligned-clause | collapse-clause |
2392/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002393/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00002394/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00002395/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00002396/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002397/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00002398/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00002399/// from-clause | is_device_ptr-clause | task_reduction-clause |
Alexey Bataevea9166b2020-02-06 16:30:23 -05002400/// in_reduction-clause | allocator-clause | allocate-clause |
Alexey Bataevc112e942020-02-28 09:52:15 -05002401/// acq_rel-clause | acquire-clause | release-clause | relaxed-clause |
Alexey Bataev63828a32020-03-23 10:41:08 -04002402/// depobj-clause | destroy-clause | detach-clause | inclusive-clause |
2403/// exclusive-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002404///
2405OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
2406 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00002407 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002408 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002409 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002410 // Check if clause is allowed for the given directive.
Alexey Bataevd08c0562019-11-19 12:07:54 -05002411 if (CKind != OMPC_unknown &&
2412 !isAllowedClauseForDirective(DKind, CKind, getLangOpts().OpenMP)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00002413 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
2414 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002415 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002416 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002417 }
2418
2419 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00002420 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002421 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002422 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00002423 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002424 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00002425 case OMPC_ordered:
Kelvin Li099bb8c2015-11-24 20:50:12 +00002426 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00002427 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00002428 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002429 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00002430 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00002431 case OMPC_hint:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002432 case OMPC_allocator:
Alexey Bataevc112e942020-02-28 09:52:15 -05002433 case OMPC_depobj:
Alexey Bataev0f0564b2020-03-17 09:17:42 -04002434 case OMPC_detach:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002435 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00002436 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00002437 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002438 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00002439 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00002440 // Only one collapse clause can appear on a simd directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00002441 // OpenMP [2.11.1, task Construct, Restrictions]
2442 // At most one if clause can appear on the directive.
2443 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00002444 // OpenMP [teams Construct, Restrictions]
2445 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00002446 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00002447 // OpenMP [2.9.1, task Construct, Restrictions]
2448 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002449 // OpenMP [2.9.2, taskloop Construct, Restrictions]
2450 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00002451 // OpenMP [2.9.2, taskloop Construct, Restrictions]
2452 // At most one num_tasks clause can appear on the directive.
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002453 // OpenMP [2.11.3, allocate Directive, Restrictions]
2454 // At most one allocator clause can appear on the directive.
Alexey Bataev0f0564b2020-03-17 09:17:42 -04002455 // OpenMP 5.0, 2.10.1 task Construct, Restrictions.
2456 // At most one detach clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002457 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002458 Diag(Tok, diag::err_omp_more_one_clause)
2459 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002460 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002461 }
2462
Alexey Bataev10e775f2015-07-30 11:36:16 +00002463 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002464 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00002465 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002466 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002467 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002468 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002469 case OMPC_proc_bind:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00002470 case OMPC_atomic_default_mem_order:
Alexey Bataevcb8e6912020-01-31 16:09:26 -05002471 case OMPC_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002472 // OpenMP [2.14.3.1, Restrictions]
2473 // Only a single default clause may be specified on a parallel, task or
2474 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002475 // OpenMP [2.5, parallel Construct, Restrictions]
2476 // At most one proc_bind clause can appear on the directive.
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00002477 // OpenMP [5.0, Requires directive, Restrictions]
2478 // At most one atomic_default_mem_order clause can appear
2479 // on the directive
Alexey Bataevcb8e6912020-01-31 16:09:26 -05002480 if (!FirstClause && CKind != OMPC_order) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002481 Diag(Tok, diag::err_omp_more_one_clause)
2482 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002483 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002484 }
2485
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002486 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002487 break;
Alexey Bataev2f8894a2020-03-18 15:01:15 -04002488 case OMPC_device:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002489 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00002490 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002491 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002492 // OpenMP [2.7.1, Restrictions, p. 3]
2493 // Only one schedule clause can appear on a loop directive.
cchene06f3e02019-11-15 13:02:06 -05002494 // OpenMP 4.5 [2.10.4, Restrictions, p. 106]
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002495 // At most one defaultmap clause can appear on the directive.
Alexey Bataev2f8894a2020-03-18 15:01:15 -04002496 // OpenMP 5.0 [2.12.5, target construct, Restrictions]
2497 // At most one device clause can appear on the directive.
cchene06f3e02019-11-15 13:02:06 -05002498 if ((getLangOpts().OpenMP < 50 || CKind != OMPC_defaultmap) &&
2499 !FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002500 Diag(Tok, diag::err_omp_more_one_clause)
2501 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002502 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002503 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00002504 LLVM_FALLTHROUGH;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002505 case OMPC_if:
Alexey Bataev2f8894a2020-03-18 15:01:15 -04002506 Clause = ParseOpenMPSingleExprWithArgClause(DKind, CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002507 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002508 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002509 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002510 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002511 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002512 case OMPC_write:
Alexey Bataev459dec02014-07-24 06:46:57 +00002513 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002514 case OMPC_seq_cst:
Alexey Bataevea9166b2020-02-06 16:30:23 -05002515 case OMPC_acq_rel:
Alexey Bataev04a830f2020-02-10 14:30:39 -05002516 case OMPC_acquire:
Alexey Bataev95598342020-02-10 15:49:05 -05002517 case OMPC_release:
Alexey Bataev9a8defc2020-02-11 11:10:43 -05002518 case OMPC_relaxed:
Alexey Bataev346265e2015-09-25 10:37:12 +00002519 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002520 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00002521 case OMPC_nogroup:
Kelvin Li1408f912018-09-26 04:28:39 +00002522 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00002523 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00002524 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00002525 case OMPC_dynamic_allocators:
Alexey Bataev375437a2020-03-02 14:21:20 -05002526 case OMPC_destroy:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002527 // OpenMP [2.7.1, Restrictions, p. 9]
2528 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00002529 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
2530 // Only one nowait clause can appear on a for directive.
Kelvin Li1408f912018-09-26 04:28:39 +00002531 // OpenMP [5.0, Requires directive, Restrictions]
2532 // Each of the requires clauses can appear at most once on the directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002533 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002534 Diag(Tok, diag::err_omp_more_one_clause)
2535 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002536 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002537 }
2538
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002539 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002540 break;
Alexey Bataev82f7c202020-03-03 13:22:35 -05002541 case OMPC_update:
2542 if (!FirstClause) {
2543 Diag(Tok, diag::err_omp_more_one_clause)
2544 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
2545 ErrorFound = true;
2546 }
2547
2548 Clause = (DKind == OMPD_depobj)
2549 ? ParseOpenMPSimpleClause(CKind, WrongDirective)
2550 : ParseOpenMPClause(CKind, WrongDirective);
2551 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002552 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002553 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002554 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002555 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002556 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00002557 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00002558 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002559 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002560 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002561 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002562 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002563 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002564 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00002565 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00002566 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00002567 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00002568 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00002569 case OMPC_is_device_ptr:
Alexey Bataeve04483e2019-03-27 14:14:31 +00002570 case OMPC_allocate:
Alexey Bataevb6e70842019-12-16 15:54:17 -05002571 case OMPC_nontemporal:
Alexey Bataev06dea732020-03-20 09:41:22 -04002572 case OMPC_inclusive:
Alexey Bataev63828a32020-03-23 10:41:08 -04002573 case OMPC_exclusive:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002574 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002575 break;
Alexey Bataev729e2422019-08-23 16:11:14 +00002576 case OMPC_device_type:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002577 case OMPC_unknown:
Johannes Doerfert56d15532020-02-21 13:48:56 -06002578 skipUntilPragmaOpenMPEnd(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002579 break;
2580 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002581 case OMPC_uniform:
Alexey Bataevdba792c2019-09-23 18:13:31 +00002582 case OMPC_match:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002583 if (!WrongDirective)
2584 Diag(Tok, diag::err_omp_unexpected_clause)
2585 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00002586 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002587 break;
2588 }
Craig Topper161e4db2014-05-21 06:02:52 +00002589 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002590}
2591
Alexey Bataev2af33e32016-04-07 12:45:37 +00002592/// Parses simple expression in parens for single-expression clauses of OpenMP
2593/// constructs.
2594/// \param RLoc Returned location of right paren.
2595ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
Alexey Bataevd158cf62019-09-13 20:18:17 +00002596 SourceLocation &RLoc,
2597 bool IsAddressOfOperand) {
Alexey Bataev2af33e32016-04-07 12:45:37 +00002598 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2599 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
2600 return ExprError();
2601
2602 SourceLocation ELoc = Tok.getLocation();
Saar Razb65b1f32020-01-09 15:07:51 +02002603 ExprResult LHS(ParseCastExpression(AnyCastExpr, IsAddressOfOperand,
2604 NotTypeCast));
Alexey Bataev2af33e32016-04-07 12:45:37 +00002605 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002606 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev2af33e32016-04-07 12:45:37 +00002607
2608 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002609 RLoc = Tok.getLocation();
2610 if (!T.consumeClose())
2611 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00002612
Alexey Bataev2af33e32016-04-07 12:45:37 +00002613 return Val;
2614}
2615
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002616/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00002617/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev0f0564b2020-03-17 09:17:42 -04002618/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks', 'hint' or
2619/// 'detach'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002620///
Alexey Bataev3778b602014-07-17 07:32:53 +00002621/// final-clause:
2622/// 'final' '(' expression ')'
2623///
Alexey Bataev62c87d22014-03-21 04:51:18 +00002624/// num_threads-clause:
2625/// 'num_threads' '(' expression ')'
2626///
2627/// safelen-clause:
2628/// 'safelen' '(' expression ')'
2629///
Alexey Bataev66b15b52015-08-21 11:14:16 +00002630/// simdlen-clause:
2631/// 'simdlen' '(' expression ')'
2632///
Alexander Musman8bd31e62014-05-27 15:12:19 +00002633/// collapse-clause:
2634/// 'collapse' '(' expression ')'
2635///
Alexey Bataeva0569352015-12-01 10:17:31 +00002636/// priority-clause:
2637/// 'priority' '(' expression ')'
2638///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002639/// grainsize-clause:
2640/// 'grainsize' '(' expression ')'
2641///
Alexey Bataev382967a2015-12-08 12:06:20 +00002642/// num_tasks-clause:
2643/// 'num_tasks' '(' expression ')'
2644///
Alexey Bataev28c75412015-12-15 08:19:24 +00002645/// hint-clause:
2646/// 'hint' '(' expression ')'
2647///
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002648/// allocator-clause:
2649/// 'allocator' '(' expression ')'
2650///
Alexey Bataev0f0564b2020-03-17 09:17:42 -04002651/// detach-clause:
2652/// 'detach' '(' event-handler-expression ')'
2653///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002654OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
2655 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002656 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00002657 SourceLocation LLoc = Tok.getLocation();
2658 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002659
Alexey Bataev2af33e32016-04-07 12:45:37 +00002660 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002661
2662 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00002663 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002664
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002665 if (ParseOnly)
2666 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00002667 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002668}
2669
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002670/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002671///
2672/// default-clause:
Alexey Bataev82f7c202020-03-03 13:22:35 -05002673/// 'default' '(' 'none' | 'shared' ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002674///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002675/// proc_bind-clause:
Alexey Bataev82f7c202020-03-03 13:22:35 -05002676/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')'
2677///
2678/// update-clause:
2679/// 'update' '(' 'in' | 'out' | 'inout' | 'mutexinoutset' ')'
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002680///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002681OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
2682 bool ParseOnly) {
Alexey Bataev729e2422019-08-23 16:11:14 +00002683 llvm::Optional<SimpleClauseData> Val = parseOpenMPSimpleClause(*this, Kind);
2684 if (!Val || ParseOnly)
Craig Topper161e4db2014-05-21 06:02:52 +00002685 return nullptr;
Alexey Bataev729e2422019-08-23 16:11:14 +00002686 return Actions.ActOnOpenMPSimpleClause(
2687 Kind, Val.getValue().Type, Val.getValue().TypeLoc, Val.getValue().LOpen,
2688 Val.getValue().Loc, Val.getValue().RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002689}
2690
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002691/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002692///
2693/// ordered-clause:
2694/// 'ordered'
2695///
Alexey Bataev236070f2014-06-20 11:19:47 +00002696/// nowait-clause:
2697/// 'nowait'
2698///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002699/// untied-clause:
2700/// 'untied'
2701///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002702/// mergeable-clause:
2703/// 'mergeable'
2704///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002705/// read-clause:
2706/// 'read'
2707///
Alexey Bataev346265e2015-09-25 10:37:12 +00002708/// threads-clause:
2709/// 'threads'
2710///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002711/// simd-clause:
2712/// 'simd'
2713///
Alexey Bataevb825de12015-12-07 10:51:44 +00002714/// nogroup-clause:
2715/// 'nogroup'
2716///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002717OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002718 SourceLocation Loc = Tok.getLocation();
2719 ConsumeAnyToken();
2720
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002721 if (ParseOnly)
2722 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002723 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
2724}
2725
2726
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002727/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00002728/// argument like 'schedule' or 'dist_schedule'.
2729///
2730/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00002731/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
2732/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00002733///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002734/// if-clause:
2735/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
2736///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002737/// defaultmap:
2738/// 'defaultmap' '(' modifier ':' kind ')'
2739///
Alexey Bataev2f8894a2020-03-18 15:01:15 -04002740/// device-clause:
2741/// 'device' '(' [ device-modifier ':' ] expression ')'
2742///
2743OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPDirectiveKind DKind,
2744 OpenMPClauseKind Kind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002745 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00002746 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002747 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002748 // Parse '('.
2749 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2750 if (T.expectAndConsume(diag::err_expected_lparen_after,
2751 getOpenMPClauseName(Kind)))
2752 return nullptr;
2753
2754 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002755 SmallVector<unsigned, 4> Arg;
2756 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002757 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00002758 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
2759 Arg.resize(NumberOfElements);
2760 KLoc.resize(NumberOfElements);
2761 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
2762 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
2763 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00002764 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002765 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00002766 if (KindModifier > OMPC_SCHEDULE_unknown) {
2767 // Parse 'modifier'
2768 Arg[Modifier1] = KindModifier;
2769 KLoc[Modifier1] = Tok.getLocation();
2770 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2771 Tok.isNot(tok::annot_pragma_openmp_end))
2772 ConsumeAnyToken();
2773 if (Tok.is(tok::comma)) {
2774 // Parse ',' 'modifier'
2775 ConsumeAnyToken();
2776 KindModifier = getOpenMPSimpleClauseType(
2777 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2778 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
2779 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00002780 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002781 KLoc[Modifier2] = Tok.getLocation();
2782 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2783 Tok.isNot(tok::annot_pragma_openmp_end))
2784 ConsumeAnyToken();
2785 }
2786 // Parse ':'
2787 if (Tok.is(tok::colon))
2788 ConsumeAnyToken();
2789 else
2790 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
2791 KindModifier = getOpenMPSimpleClauseType(
2792 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2793 }
2794 Arg[ScheduleKind] = KindModifier;
2795 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002796 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2797 Tok.isNot(tok::annot_pragma_openmp_end))
2798 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00002799 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
2800 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
2801 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002802 Tok.is(tok::comma))
2803 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00002804 } else if (Kind == OMPC_dist_schedule) {
2805 Arg.push_back(getOpenMPSimpleClauseType(
2806 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2807 KLoc.push_back(Tok.getLocation());
2808 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2809 Tok.isNot(tok::annot_pragma_openmp_end))
2810 ConsumeAnyToken();
2811 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
2812 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002813 } else if (Kind == OMPC_defaultmap) {
2814 // Get a defaultmap modifier
cchene06f3e02019-11-15 13:02:06 -05002815 unsigned Modifier = getOpenMPSimpleClauseType(
2816 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2817 // Set defaultmap modifier to unknown if it is either scalar, aggregate, or
2818 // pointer
2819 if (Modifier < OMPC_DEFAULTMAP_MODIFIER_unknown)
2820 Modifier = OMPC_DEFAULTMAP_MODIFIER_unknown;
2821 Arg.push_back(Modifier);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002822 KLoc.push_back(Tok.getLocation());
2823 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2824 Tok.isNot(tok::annot_pragma_openmp_end))
2825 ConsumeAnyToken();
2826 // Parse ':'
2827 if (Tok.is(tok::colon))
2828 ConsumeAnyToken();
2829 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
2830 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
2831 // Get a defaultmap kind
2832 Arg.push_back(getOpenMPSimpleClauseType(
2833 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2834 KLoc.push_back(Tok.getLocation());
2835 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2836 Tok.isNot(tok::annot_pragma_openmp_end))
2837 ConsumeAnyToken();
Alexey Bataev2f8894a2020-03-18 15:01:15 -04002838 } else if (Kind == OMPC_device) {
2839 // Only target executable directives support extended device construct.
2840 if (isOpenMPTargetExecutionDirective(DKind) && getLangOpts().OpenMP >= 50 &&
2841 NextToken().is(tok::colon)) {
2842 // Parse optional <device modifier> ':'
2843 Arg.push_back(getOpenMPSimpleClauseType(
2844 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2845 KLoc.push_back(Tok.getLocation());
2846 ConsumeAnyToken();
2847 // Parse ':'
2848 ConsumeAnyToken();
2849 } else {
2850 Arg.push_back(OMPC_DEVICE_unknown);
2851 KLoc.emplace_back();
2852 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002853 } else {
2854 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00002855 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002856 TentativeParsingAction TPA(*this);
Johannes Doerferteb3e81f2019-11-04 22:00:49 -06002857 auto DK = parseOpenMPDirectiveKind(*this);
2858 Arg.push_back(DK);
2859 if (DK != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002860 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002861 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
2862 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002863 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002864 } else {
2865 TPA.Revert();
Johannes Doerferteb3e81f2019-11-04 22:00:49 -06002866 Arg.back() = unsigned(OMPD_unknown);
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002867 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002868 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002869 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00002870 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002871 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00002872
Carlo Bertollib4adf552016-01-15 18:50:31 +00002873 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
2874 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
Alexey Bataev2f8894a2020-03-18 15:01:15 -04002875 Kind == OMPC_if || Kind == OMPC_device;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002876 if (NeedAnExpression) {
2877 SourceLocation ELoc = Tok.getLocation();
Saar Razb65b1f32020-01-09 15:07:51 +02002878 ExprResult LHS(ParseCastExpression(AnyCastExpr, false, NotTypeCast));
Alexey Bataev56dafe82014-06-20 07:16:17 +00002879 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002880 Val =
2881 Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002882 }
2883
2884 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002885 SourceLocation RLoc = Tok.getLocation();
2886 if (!T.consumeClose())
2887 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002888
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002889 if (NeedAnExpression && Val.isInvalid())
2890 return nullptr;
2891
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002892 if (ParseOnly)
2893 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002894 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002895 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002896}
2897
Alexey Bataevc5e02582014-06-16 07:08:35 +00002898static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
2899 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002900 if (ReductionIdScopeSpec.isEmpty()) {
2901 auto OOK = OO_None;
2902 switch (P.getCurToken().getKind()) {
2903 case tok::plus:
2904 OOK = OO_Plus;
2905 break;
2906 case tok::minus:
2907 OOK = OO_Minus;
2908 break;
2909 case tok::star:
2910 OOK = OO_Star;
2911 break;
2912 case tok::amp:
2913 OOK = OO_Amp;
2914 break;
2915 case tok::pipe:
2916 OOK = OO_Pipe;
2917 break;
2918 case tok::caret:
2919 OOK = OO_Caret;
2920 break;
2921 case tok::ampamp:
2922 OOK = OO_AmpAmp;
2923 break;
2924 case tok::pipepipe:
2925 OOK = OO_PipePipe;
2926 break;
2927 default:
2928 break;
2929 }
2930 if (OOK != OO_None) {
2931 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00002932 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00002933 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
2934 return false;
2935 }
2936 }
Haojian Wu0dd0b102020-03-19 09:12:29 +01002937 return P.ParseUnqualifiedId(
2938 ReductionIdScopeSpec, /*ObjectType=*/nullptr,
2939 /*ObjectHadErrors=*/false, /*EnteringContext*/ false,
2940 /*AllowDestructorName*/ false,
2941 /*AllowConstructorName*/ false,
2942 /*AllowDeductionGuide*/ false, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002943}
2944
Kelvin Lief579432018-12-18 22:18:41 +00002945/// Checks if the token is a valid map-type-modifier.
2946static OpenMPMapModifierKind isMapModifier(Parser &P) {
2947 Token Tok = P.getCurToken();
2948 if (!Tok.is(tok::identifier))
2949 return OMPC_MAP_MODIFIER_unknown;
2950
2951 Preprocessor &PP = P.getPreprocessor();
2952 OpenMPMapModifierKind TypeModifier = static_cast<OpenMPMapModifierKind>(
2953 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2954 return TypeModifier;
2955}
2956
Michael Kruse01f670d2019-02-22 22:29:42 +00002957/// Parse the mapper modifier in map, to, and from clauses.
2958bool Parser::parseMapperModifier(OpenMPVarListDataTy &Data) {
2959 // Parse '('.
2960 BalancedDelimiterTracker T(*this, tok::l_paren, tok::colon);
2961 if (T.expectAndConsume(diag::err_expected_lparen_after, "mapper")) {
2962 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2963 StopBeforeMatch);
2964 return true;
2965 }
2966 // Parse mapper-identifier
2967 if (getLangOpts().CPlusPlus)
2968 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
2969 /*ObjectType=*/nullptr,
Haojian Wu0dd0b102020-03-19 09:12:29 +01002970 /*ObjectHadErrors=*/false,
Michael Kruse01f670d2019-02-22 22:29:42 +00002971 /*EnteringContext=*/false);
2972 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
2973 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
2974 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2975 StopBeforeMatch);
2976 return true;
2977 }
2978 auto &DeclNames = Actions.getASTContext().DeclarationNames;
2979 Data.ReductionOrMapperId = DeclarationNameInfo(
2980 DeclNames.getIdentifier(Tok.getIdentifierInfo()), Tok.getLocation());
2981 ConsumeToken();
2982 // Parse ')'.
2983 return T.consumeClose();
2984}
2985
Kelvin Lief579432018-12-18 22:18:41 +00002986/// Parse map-type-modifiers in map clause.
2987/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002988/// where, map-type-modifier ::= always | close | mapper(mapper-identifier)
2989bool Parser::parseMapTypeModifiers(OpenMPVarListDataTy &Data) {
2990 while (getCurToken().isNot(tok::colon)) {
2991 OpenMPMapModifierKind TypeModifier = isMapModifier(*this);
Kelvin Lief579432018-12-18 22:18:41 +00002992 if (TypeModifier == OMPC_MAP_MODIFIER_always ||
2993 TypeModifier == OMPC_MAP_MODIFIER_close) {
2994 Data.MapTypeModifiers.push_back(TypeModifier);
2995 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
Michael Kruse4304e9d2019-02-19 16:38:20 +00002996 ConsumeToken();
2997 } else if (TypeModifier == OMPC_MAP_MODIFIER_mapper) {
2998 Data.MapTypeModifiers.push_back(TypeModifier);
2999 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
3000 ConsumeToken();
Michael Kruse01f670d2019-02-22 22:29:42 +00003001 if (parseMapperModifier(Data))
Michael Kruse4304e9d2019-02-19 16:38:20 +00003002 return true;
Kelvin Lief579432018-12-18 22:18:41 +00003003 } else {
3004 // For the case of unknown map-type-modifier or a map-type.
3005 // Map-type is followed by a colon; the function returns when it
3006 // encounters a token followed by a colon.
3007 if (Tok.is(tok::comma)) {
Michael Kruse4304e9d2019-02-19 16:38:20 +00003008 Diag(Tok, diag::err_omp_map_type_modifier_missing);
3009 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00003010 continue;
3011 }
3012 // Potential map-type token as it is followed by a colon.
3013 if (PP.LookAhead(0).is(tok::colon))
Michael Kruse4304e9d2019-02-19 16:38:20 +00003014 return false;
3015 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
3016 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00003017 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00003018 if (getCurToken().is(tok::comma))
3019 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00003020 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00003021 return false;
Kelvin Lief579432018-12-18 22:18:41 +00003022}
3023
3024/// Checks if the token is a valid map-type.
3025static OpenMPMapClauseKind isMapType(Parser &P) {
3026 Token Tok = P.getCurToken();
3027 // The map-type token can be either an identifier or the C++ delete keyword.
3028 if (!Tok.isOneOf(tok::identifier, tok::kw_delete))
3029 return OMPC_MAP_unknown;
3030 Preprocessor &PP = P.getPreprocessor();
3031 OpenMPMapClauseKind MapType = static_cast<OpenMPMapClauseKind>(
3032 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
3033 return MapType;
3034}
3035
3036/// Parse map-type in map clause.
3037/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Ilya Biryukovff2a9972019-02-26 11:01:50 +00003038/// where, map-type ::= to | from | tofrom | alloc | release | delete
Kelvin Lief579432018-12-18 22:18:41 +00003039static void parseMapType(Parser &P, Parser::OpenMPVarListDataTy &Data) {
3040 Token Tok = P.getCurToken();
3041 if (Tok.is(tok::colon)) {
3042 P.Diag(Tok, diag::err_omp_map_type_missing);
3043 return;
3044 }
Alexey Bataev93dc40d2019-12-20 11:04:57 -05003045 Data.ExtraModifier = isMapType(P);
3046 if (Data.ExtraModifier == OMPC_MAP_unknown)
Kelvin Lief579432018-12-18 22:18:41 +00003047 P.Diag(Tok, diag::err_omp_unknown_map_type);
3048 P.ConsumeToken();
3049}
3050
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003051/// Parses clauses with list.
3052bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
3053 OpenMPClauseKind Kind,
3054 SmallVectorImpl<Expr *> &Vars,
3055 OpenMPVarListDataTy &Data) {
3056 UnqualifiedId UnqualifiedReductionId;
3057 bool InvalidReductionId = false;
Michael Kruse01f670d2019-02-22 22:29:42 +00003058 bool IsInvalidMapperModifier = false;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003059
3060 // Parse '('.
3061 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
3062 if (T.expectAndConsume(diag::err_expected_lparen_after,
3063 getOpenMPClauseName(Kind)))
3064 return true;
3065
3066 bool NeedRParenForLinear = false;
3067 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
3068 tok::annot_pragma_openmp_end);
3069 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00003070 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
3071 Kind == OMPC_in_reduction) {
Alexey Bataev1236eb62020-03-23 17:30:38 -04003072 Data.ExtraModifier = OMPC_REDUCTION_unknown;
3073 if (Kind == OMPC_reduction && getLangOpts().OpenMP >= 50 &&
3074 (Tok.is(tok::identifier) || Tok.is(tok::kw_default)) &&
3075 NextToken().is(tok::comma)) {
3076 // Parse optional reduction modifier.
3077 Data.ExtraModifier = getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok));
3078 Data.ExtraModifierLoc = Tok.getLocation();
3079 ConsumeToken();
3080 assert(Tok.is(tok::comma) && "Expected comma.");
3081 (void)ConsumeToken();
3082 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003083 ColonProtectionRAIIObject ColonRAII(*this);
3084 if (getLangOpts().CPlusPlus)
Michael Kruse4304e9d2019-02-19 16:38:20 +00003085 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003086 /*ObjectType=*/nullptr,
Haojian Wu0dd0b102020-03-19 09:12:29 +01003087 /*ObjectHadErrors=*/false,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003088 /*EnteringContext=*/false);
Michael Kruse4304e9d2019-02-19 16:38:20 +00003089 InvalidReductionId = ParseReductionId(
3090 *this, Data.ReductionOrMapperIdScopeSpec, UnqualifiedReductionId);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003091 if (InvalidReductionId) {
3092 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
3093 StopBeforeMatch);
3094 }
3095 if (Tok.is(tok::colon))
3096 Data.ColonLoc = ConsumeToken();
3097 else
3098 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
3099 if (!InvalidReductionId)
Michael Kruse4304e9d2019-02-19 16:38:20 +00003100 Data.ReductionOrMapperId =
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003101 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
3102 } else if (Kind == OMPC_depend) {
Alexey Bataev93dc40d2019-12-20 11:04:57 -05003103 // Handle dependency type for depend clause.
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003104 ColonProtectionRAIIObject ColonRAII(*this);
Alexey Bataev93dc40d2019-12-20 11:04:57 -05003105 Data.ExtraModifier = getOpenMPSimpleClauseType(
3106 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : "");
Alexey Bataev1236eb62020-03-23 17:30:38 -04003107 Data.ExtraModifierLoc = Tok.getLocation();
Alexey Bataev93dc40d2019-12-20 11:04:57 -05003108 if (Data.ExtraModifier == OMPC_DEPEND_unknown) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003109 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
3110 StopBeforeMatch);
3111 } else {
3112 ConsumeToken();
3113 // Special processing for depend(source) clause.
Alexey Bataev93dc40d2019-12-20 11:04:57 -05003114 if (DKind == OMPD_ordered && Data.ExtraModifier == OMPC_DEPEND_source) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003115 // Parse ')'.
3116 T.consumeClose();
3117 return false;
3118 }
3119 }
Alexey Bataev61908f652018-04-23 19:53:05 +00003120 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003121 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00003122 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003123 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
3124 : diag::warn_pragma_expected_colon)
3125 << "dependency type";
3126 }
3127 } else if (Kind == OMPC_linear) {
3128 // Try to parse modifier if any.
Alexey Bataev93dc40d2019-12-20 11:04:57 -05003129 Data.ExtraModifier = OMPC_LINEAR_val;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003130 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev93dc40d2019-12-20 11:04:57 -05003131 Data.ExtraModifier = getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok));
Alexey Bataev1236eb62020-03-23 17:30:38 -04003132 Data.ExtraModifierLoc = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003133 LinearT.consumeOpen();
3134 NeedRParenForLinear = true;
3135 }
Alexey Bataev93dc40d2019-12-20 11:04:57 -05003136 } else if (Kind == OMPC_lastprivate) {
3137 // Try to parse modifier if any.
3138 Data.ExtraModifier = OMPC_LASTPRIVATE_unknown;
3139 // Conditional modifier allowed only in OpenMP 5.0 and not supported in
3140 // distribute and taskloop based directives.
3141 if ((getLangOpts().OpenMP >= 50 && !isOpenMPDistributeDirective(DKind) &&
3142 !isOpenMPTaskLoopDirective(DKind)) &&
3143 Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::colon)) {
3144 Data.ExtraModifier = getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok));
Alexey Bataev1236eb62020-03-23 17:30:38 -04003145 Data.ExtraModifierLoc = Tok.getLocation();
3146 ConsumeToken();
Alexey Bataev93dc40d2019-12-20 11:04:57 -05003147 assert(Tok.is(tok::colon) && "Expected colon.");
3148 Data.ColonLoc = ConsumeToken();
3149 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003150 } else if (Kind == OMPC_map) {
3151 // Handle map type for map clause.
3152 ColonProtectionRAIIObject ColonRAII(*this);
3153
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003154 // The first identifier may be a list item, a map-type or a
Kelvin Lief579432018-12-18 22:18:41 +00003155 // map-type-modifier. The map-type can also be delete which has the same
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003156 // spelling of the C++ delete keyword.
Alexey Bataev93dc40d2019-12-20 11:04:57 -05003157 Data.ExtraModifier = OMPC_MAP_unknown;
Alexey Bataev1236eb62020-03-23 17:30:38 -04003158 Data.ExtraModifierLoc = Tok.getLocation();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003159
Kelvin Lief579432018-12-18 22:18:41 +00003160 // Check for presence of a colon in the map clause.
3161 TentativeParsingAction TPA(*this);
3162 bool ColonPresent = false;
3163 if (SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
3164 StopBeforeMatch)) {
3165 if (Tok.is(tok::colon))
3166 ColonPresent = true;
3167 }
3168 TPA.Revert();
3169 // Only parse map-type-modifier[s] and map-type if a colon is present in
3170 // the map clause.
3171 if (ColonPresent) {
Michael Kruse01f670d2019-02-22 22:29:42 +00003172 IsInvalidMapperModifier = parseMapTypeModifiers(Data);
3173 if (!IsInvalidMapperModifier)
Michael Kruse4304e9d2019-02-19 16:38:20 +00003174 parseMapType(*this, Data);
Michael Kruse01f670d2019-02-22 22:29:42 +00003175 else
3176 SkipUntil(tok::colon, tok::annot_pragma_openmp_end, StopBeforeMatch);
Kelvin Lief579432018-12-18 22:18:41 +00003177 }
Alexey Bataev93dc40d2019-12-20 11:04:57 -05003178 if (Data.ExtraModifier == OMPC_MAP_unknown) {
3179 Data.ExtraModifier = OMPC_MAP_tofrom;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003180 Data.IsMapTypeImplicit = true;
3181 }
3182
3183 if (Tok.is(tok::colon))
3184 Data.ColonLoc = ConsumeToken();
Michael Kruse0336c752019-02-25 20:34:15 +00003185 } else if (Kind == OMPC_to || Kind == OMPC_from) {
Michael Kruse01f670d2019-02-22 22:29:42 +00003186 if (Tok.is(tok::identifier)) {
3187 bool IsMapperModifier = false;
Michael Kruse0336c752019-02-25 20:34:15 +00003188 if (Kind == OMPC_to) {
3189 auto Modifier = static_cast<OpenMPToModifierKind>(
3190 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
3191 if (Modifier == OMPC_TO_MODIFIER_mapper)
3192 IsMapperModifier = true;
3193 } else {
3194 auto Modifier = static_cast<OpenMPFromModifierKind>(
3195 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
3196 if (Modifier == OMPC_FROM_MODIFIER_mapper)
3197 IsMapperModifier = true;
3198 }
Michael Kruse01f670d2019-02-22 22:29:42 +00003199 if (IsMapperModifier) {
3200 // Parse the mapper modifier.
3201 ConsumeToken();
3202 IsInvalidMapperModifier = parseMapperModifier(Data);
3203 if (Tok.isNot(tok::colon)) {
3204 if (!IsInvalidMapperModifier)
3205 Diag(Tok, diag::warn_pragma_expected_colon) << ")";
3206 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
3207 StopBeforeMatch);
3208 }
3209 // Consume ':'.
3210 if (Tok.is(tok::colon))
3211 ConsumeToken();
3212 }
3213 }
Alexey Bataeve04483e2019-03-27 14:14:31 +00003214 } else if (Kind == OMPC_allocate) {
3215 // Handle optional allocator expression followed by colon delimiter.
3216 ColonProtectionRAIIObject ColonRAII(*this);
3217 TentativeParsingAction TPA(*this);
3218 ExprResult Tail =
3219 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
3220 Tail = Actions.ActOnFinishFullExpr(Tail.get(), T.getOpenLocation(),
3221 /*DiscardedValue=*/false);
3222 if (Tail.isUsable()) {
3223 if (Tok.is(tok::colon)) {
3224 Data.TailExpr = Tail.get();
3225 Data.ColonLoc = ConsumeToken();
3226 TPA.Commit();
3227 } else {
3228 // colon not found, no allocator specified, parse only list of
3229 // variables.
3230 TPA.Revert();
3231 }
3232 } else {
3233 // Parsing was unsuccessfull, revert and skip to the end of clause or
3234 // directive.
3235 TPA.Revert();
3236 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
3237 StopBeforeMatch);
3238 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003239 }
3240
Alexey Bataevfa312f32017-07-21 18:48:21 +00003241 bool IsComma =
3242 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
3243 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
3244 (Kind == OMPC_reduction && !InvalidReductionId) ||
Alexey Bataev93dc40d2019-12-20 11:04:57 -05003245 (Kind == OMPC_map && Data.ExtraModifier != OMPC_MAP_unknown) ||
3246 (Kind == OMPC_depend && Data.ExtraModifier != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003247 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
3248 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
3249 Tok.isNot(tok::annot_pragma_openmp_end))) {
3250 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
3251 // Parse variable
3252 ExprResult VarExpr =
3253 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00003254 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003255 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00003256 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003257 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
3258 StopBeforeMatch);
3259 }
3260 // Skip ',' if any
3261 IsComma = Tok.is(tok::comma);
3262 if (IsComma)
3263 ConsumeToken();
3264 else if (Tok.isNot(tok::r_paren) &&
3265 Tok.isNot(tok::annot_pragma_openmp_end) &&
3266 (!MayHaveTail || Tok.isNot(tok::colon)))
3267 Diag(Tok, diag::err_omp_expected_punc)
3268 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
3269 : getOpenMPClauseName(Kind))
3270 << (Kind == OMPC_flush);
3271 }
3272
3273 // Parse ')' for linear clause with modifier.
3274 if (NeedRParenForLinear)
3275 LinearT.consumeClose();
3276
3277 // Parse ':' linear-step (or ':' alignment).
3278 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
3279 if (MustHaveTail) {
3280 Data.ColonLoc = Tok.getLocation();
3281 SourceLocation ELoc = ConsumeToken();
3282 ExprResult Tail = ParseAssignmentExpression();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00003283 Tail =
3284 Actions.ActOnFinishFullExpr(Tail.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003285 if (Tail.isUsable())
3286 Data.TailExpr = Tail.get();
3287 else
3288 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
3289 StopBeforeMatch);
3290 }
3291
3292 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00003293 Data.RLoc = Tok.getLocation();
3294 if (!T.consumeClose())
3295 Data.RLoc = T.getCloseLocation();
Alexey Bataev5dadf572020-03-06 11:09:55 -05003296 return (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
Michael Kruse4304e9d2019-02-19 16:38:20 +00003297 (MustHaveTail && !Data.TailExpr) || InvalidReductionId ||
Michael Kruse01f670d2019-02-22 22:29:42 +00003298 IsInvalidMapperModifier;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003299}
3300
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003301/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev06dea732020-03-20 09:41:22 -04003302/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction',
Alexey Bataev63828a32020-03-23 10:41:08 -04003303/// 'in_reduction', 'nontemporal', 'exclusive' or 'inclusive'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003304///
3305/// private-clause:
3306/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003307/// firstprivate-clause:
3308/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00003309/// lastprivate-clause:
3310/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00003311/// shared-clause:
3312/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00003313/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00003314/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003315/// aligned-clause:
3316/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00003317/// reduction-clause:
Alexey Bataev1236eb62020-03-23 17:30:38 -04003318/// 'reduction' '(' [ modifier ',' ] reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00003319/// task_reduction-clause:
3320/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00003321/// in_reduction-clause:
3322/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00003323/// copyprivate-clause:
3324/// 'copyprivate' '(' list ')'
3325/// flush-clause:
3326/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00003327/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00003328/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00003329/// map-clause:
Kelvin Lief579432018-12-18 22:18:41 +00003330/// 'map' '(' [ [ always [,] ] [ close [,] ]
Michael Kruse01f670d2019-02-22 22:29:42 +00003331/// [ mapper '(' mapper-identifier ')' [,] ]
Kelvin Li0bff7af2015-11-23 05:32:03 +00003332/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00003333/// to-clause:
Michael Kruse01f670d2019-02-22 22:29:42 +00003334/// 'to' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00003335/// from-clause:
Michael Kruse0336c752019-02-25 20:34:15 +00003336/// 'from' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00003337/// use_device_ptr-clause:
3338/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00003339/// is_device_ptr-clause:
3340/// 'is_device_ptr' '(' list ')'
Alexey Bataeve04483e2019-03-27 14:14:31 +00003341/// allocate-clause:
3342/// 'allocate' '(' [ allocator ':' ] list ')'
Alexey Bataev06dea732020-03-20 09:41:22 -04003343/// nontemporal-clause:
3344/// 'nontemporal' '(' list ')'
3345/// inclusive-clause:
3346/// 'inclusive' '(' list ')'
Alexey Bataev63828a32020-03-23 10:41:08 -04003347/// exclusive-clause:
3348/// 'exclusive' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003349///
Alexey Bataev182227b2015-08-20 10:54:39 +00003350/// For 'linear' clause linear-list may have the following forms:
3351/// list
3352/// modifier(list)
3353/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00003354OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00003355 OpenMPClauseKind Kind,
3356 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003357 SourceLocation Loc = Tok.getLocation();
3358 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003359 SmallVector<Expr *, 4> Vars;
3360 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00003361
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003362 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00003363 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003364
Alexey Bataevf3c832a2018-01-09 19:21:04 +00003365 if (ParseOnly)
3366 return nullptr;
Michael Kruse4304e9d2019-02-19 16:38:20 +00003367 OMPVarListLocTy Locs(Loc, LOpen, Data.RLoc);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003368 return Actions.ActOnOpenMPVarListClause(
Michael Kruse4304e9d2019-02-19 16:38:20 +00003369 Kind, Vars, Data.TailExpr, Locs, Data.ColonLoc,
Alexey Bataev93dc40d2019-12-20 11:04:57 -05003370 Data.ReductionOrMapperIdScopeSpec, Data.ReductionOrMapperId,
3371 Data.ExtraModifier, Data.MapTypeModifiers, Data.MapTypeModifiersLoc,
Alexey Bataev1236eb62020-03-23 17:30:38 -04003372 Data.IsMapTypeImplicit, Data.ExtraModifierLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003373}
3374