blob: 181ed331325384c2905c891c1db4f9dc5d06f63f [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexey Bataeva769e072013-03-22 06:34:35 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009/// This file implements parsing of all OpenMP directives and clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000010///
11//===----------------------------------------------------------------------===//
12
Alexey Bataev9959db52014-05-06 10:08:46 +000013#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000014#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000015#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000016#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000017#include "clang/Parse/RAIIObjectsForParser.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000018#include "clang/Sema/Scope.h"
19#include "llvm/ADT/PointerIntPair.h"
Alexey Bataev4513e93f2019-10-10 15:15:26 +000020#include "llvm/ADT/UniqueVector.h"
Michael Wong65f367f2015-07-21 13:44:28 +000021
Alexey Bataeva769e072013-03-22 06:34:35 +000022using namespace clang;
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060023using namespace llvm::omp;
Alexey Bataeva769e072013-03-22 06:34:35 +000024
25//===----------------------------------------------------------------------===//
26// OpenMP declarative directives.
27//===----------------------------------------------------------------------===//
28
Dmitry Polukhin82478332016-02-13 06:53:38 +000029namespace {
30enum OpenMPDirectiveKindEx {
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060031 OMPD_cancellation = unsigned(OMPD_unknown) + 1,
Dmitry Polukhin82478332016-02-13 06:53:38 +000032 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000033 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000034 OMPD_end,
35 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000036 OMPD_enter,
37 OMPD_exit,
38 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000039 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000040 OMPD_target_enter,
Samuel Antao686c70c2016-05-26 17:30:50 +000041 OMPD_target_exit,
42 OMPD_update,
Kelvin Li579e41c2016-11-30 23:51:03 +000043 OMPD_distribute_parallel,
Kelvin Li80e8f562016-12-29 22:16:30 +000044 OMPD_teams_distribute_parallel,
Michael Kruse251e1482019-02-01 20:25:04 +000045 OMPD_target_teams_distribute_parallel,
46 OMPD_mapper,
Alexey Bataevd158cf62019-09-13 20:18:17 +000047 OMPD_variant,
Dmitry Polukhin82478332016-02-13 06:53:38 +000048};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000049
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060050// Helper to unify the enum class OpenMPDirectiveKind with its extension
51// the OpenMPDirectiveKindEx enum which allows to use them together as if they
52// are unsigned values.
53struct OpenMPDirectiveKindExWrapper {
54 OpenMPDirectiveKindExWrapper(unsigned Value) : Value(Value) {}
55 OpenMPDirectiveKindExWrapper(OpenMPDirectiveKind DK) : Value(unsigned(DK)) {}
56 bool operator==(OpenMPDirectiveKind V) const { return Value == unsigned(V); }
57 bool operator!=(OpenMPDirectiveKind V) const { return Value != unsigned(V); }
58 bool operator<(OpenMPDirectiveKind V) const { return Value < unsigned(V); }
59 operator unsigned() const { return Value; }
60 operator OpenMPDirectiveKind() const { return OpenMPDirectiveKind(Value); }
61 unsigned Value;
62};
63
Alexey Bataev25ed0c02019-03-07 17:54:44 +000064class DeclDirectiveListParserHelper final {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000065 SmallVector<Expr *, 4> Identifiers;
66 Parser *P;
Alexey Bataev25ed0c02019-03-07 17:54:44 +000067 OpenMPDirectiveKind Kind;
Dmitry Polukhind69b5052016-05-09 14:59:13 +000068
69public:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000070 DeclDirectiveListParserHelper(Parser *P, OpenMPDirectiveKind Kind)
71 : P(P), Kind(Kind) {}
Dmitry Polukhind69b5052016-05-09 14:59:13 +000072 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
Alexey Bataev25ed0c02019-03-07 17:54:44 +000073 ExprResult Res = P->getActions().ActOnOpenMPIdExpression(
74 P->getCurScope(), SS, NameInfo, Kind);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000075 if (Res.isUsable())
76 Identifiers.push_back(Res.get());
77 }
78 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
79};
Dmitry Polukhin82478332016-02-13 06:53:38 +000080} // namespace
81
82// Map token string to extended OMP token kind that are
83// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
84static unsigned getOpenMPDirectiveKindEx(StringRef S) {
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060085 OpenMPDirectiveKindExWrapper DKind = getOpenMPDirectiveKind(S);
Dmitry Polukhin82478332016-02-13 06:53:38 +000086 if (DKind != OMPD_unknown)
87 return DKind;
88
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060089 return llvm::StringSwitch<OpenMPDirectiveKindExWrapper>(S)
Dmitry Polukhin82478332016-02-13 06:53:38 +000090 .Case("cancellation", OMPD_cancellation)
91 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000092 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000093 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000094 .Case("enter", OMPD_enter)
95 .Case("exit", OMPD_exit)
96 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000097 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000098 .Case("update", OMPD_update)
Michael Kruse251e1482019-02-01 20:25:04 +000099 .Case("mapper", OMPD_mapper)
Alexey Bataevd158cf62019-09-13 20:18:17 +0000100 .Case("variant", OMPD_variant)
Dmitry Polukhin82478332016-02-13 06:53:38 +0000101 .Default(OMPD_unknown);
102}
103
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600104static OpenMPDirectiveKindExWrapper parseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +0000105 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
106 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
107 // TODO: add other combined directives in topological order.
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600108 static const OpenMPDirectiveKindExWrapper F[][3] = {
Alexey Bataev61908f652018-04-23 19:53:05 +0000109 {OMPD_cancellation, OMPD_point, OMPD_cancellation_point},
110 {OMPD_declare, OMPD_reduction, OMPD_declare_reduction},
Michael Kruse251e1482019-02-01 20:25:04 +0000111 {OMPD_declare, OMPD_mapper, OMPD_declare_mapper},
Alexey Bataev61908f652018-04-23 19:53:05 +0000112 {OMPD_declare, OMPD_simd, OMPD_declare_simd},
113 {OMPD_declare, OMPD_target, OMPD_declare_target},
Alexey Bataevd158cf62019-09-13 20:18:17 +0000114 {OMPD_declare, OMPD_variant, OMPD_declare_variant},
Alexey Bataev61908f652018-04-23 19:53:05 +0000115 {OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel},
116 {OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for},
117 {OMPD_distribute_parallel_for, OMPD_simd,
118 OMPD_distribute_parallel_for_simd},
119 {OMPD_distribute, OMPD_simd, OMPD_distribute_simd},
120 {OMPD_end, OMPD_declare, OMPD_end_declare},
121 {OMPD_end_declare, OMPD_target, OMPD_end_declare_target},
122 {OMPD_target, OMPD_data, OMPD_target_data},
123 {OMPD_target, OMPD_enter, OMPD_target_enter},
124 {OMPD_target, OMPD_exit, OMPD_target_exit},
125 {OMPD_target, OMPD_update, OMPD_target_update},
126 {OMPD_target_enter, OMPD_data, OMPD_target_enter_data},
127 {OMPD_target_exit, OMPD_data, OMPD_target_exit_data},
128 {OMPD_for, OMPD_simd, OMPD_for_simd},
129 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
130 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
131 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
132 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd},
133 {OMPD_target, OMPD_parallel, OMPD_target_parallel},
134 {OMPD_target, OMPD_simd, OMPD_target_simd},
135 {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for},
136 {OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd},
137 {OMPD_teams, OMPD_distribute, OMPD_teams_distribute},
138 {OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd},
139 {OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel},
140 {OMPD_teams_distribute_parallel, OMPD_for,
141 OMPD_teams_distribute_parallel_for},
142 {OMPD_teams_distribute_parallel_for, OMPD_simd,
143 OMPD_teams_distribute_parallel_for_simd},
144 {OMPD_target, OMPD_teams, OMPD_target_teams},
145 {OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute},
146 {OMPD_target_teams_distribute, OMPD_parallel,
147 OMPD_target_teams_distribute_parallel},
148 {OMPD_target_teams_distribute, OMPD_simd,
149 OMPD_target_teams_distribute_simd},
150 {OMPD_target_teams_distribute_parallel, OMPD_for,
151 OMPD_target_teams_distribute_parallel_for},
152 {OMPD_target_teams_distribute_parallel_for, OMPD_simd,
Alexey Bataev60e51c42019-10-10 20:13:02 +0000153 OMPD_target_teams_distribute_parallel_for_simd},
Alexey Bataev5bbcead2019-10-14 17:17:41 +0000154 {OMPD_master, OMPD_taskloop, OMPD_master_taskloop},
Alexey Bataevb8552ab2019-10-18 16:47:35 +0000155 {OMPD_master_taskloop, OMPD_simd, OMPD_master_taskloop_simd},
Alexey Bataev5bbcead2019-10-14 17:17:41 +0000156 {OMPD_parallel, OMPD_master, OMPD_parallel_master},
Alexey Bataev14a388f2019-10-25 10:27:13 -0400157 {OMPD_parallel_master, OMPD_taskloop, OMPD_parallel_master_taskloop},
158 {OMPD_parallel_master_taskloop, OMPD_simd,
159 OMPD_parallel_master_taskloop_simd}};
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000160 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev61908f652018-04-23 19:53:05 +0000161 Token Tok = P.getCurToken();
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600162 OpenMPDirectiveKindExWrapper DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000163 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000164 ? static_cast<unsigned>(OMPD_unknown)
165 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
166 if (DKind == OMPD_unknown)
167 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000168
Alexey Bataev61908f652018-04-23 19:53:05 +0000169 for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) {
170 if (DKind != F[I][0])
Dmitry Polukhin82478332016-02-13 06:53:38 +0000171 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000172
Dmitry Polukhin82478332016-02-13 06:53:38 +0000173 Tok = P.getPreprocessor().LookAhead(0);
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600174 OpenMPDirectiveKindExWrapper SDKind =
Dmitry Polukhin82478332016-02-13 06:53:38 +0000175 Tok.isAnnotation()
176 ? static_cast<unsigned>(OMPD_unknown)
177 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
178 if (SDKind == OMPD_unknown)
179 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000180
Alexey Bataev61908f652018-04-23 19:53:05 +0000181 if (SDKind == F[I][1]) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000182 P.ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000183 DKind = F[I][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000184 }
185 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000186 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
187 : OMPD_unknown;
188}
189
190static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000191 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000192 Sema &Actions = P.getActions();
193 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000194 // Allow to use 'operator' keyword for C++ operators
195 bool WithOperator = false;
196 if (Tok.is(tok::kw_operator)) {
197 P.ConsumeToken();
198 Tok = P.getCurToken();
199 WithOperator = true;
200 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000201 switch (Tok.getKind()) {
202 case tok::plus: // '+'
203 OOK = OO_Plus;
204 break;
205 case tok::minus: // '-'
206 OOK = OO_Minus;
207 break;
208 case tok::star: // '*'
209 OOK = OO_Star;
210 break;
211 case tok::amp: // '&'
212 OOK = OO_Amp;
213 break;
214 case tok::pipe: // '|'
215 OOK = OO_Pipe;
216 break;
217 case tok::caret: // '^'
218 OOK = OO_Caret;
219 break;
220 case tok::ampamp: // '&&'
221 OOK = OO_AmpAmp;
222 break;
223 case tok::pipepipe: // '||'
224 OOK = OO_PipePipe;
225 break;
226 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000227 if (!WithOperator)
228 break;
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000229 LLVM_FALLTHROUGH;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000230 default:
231 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
232 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
233 Parser::StopBeforeMatch);
234 return DeclarationName();
235 }
236 P.ConsumeToken();
237 auto &DeclNames = Actions.getASTContext().DeclarationNames;
238 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
239 : DeclNames.getCXXOperatorName(OOK);
240}
241
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000242/// Parse 'omp declare reduction' construct.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000243///
244/// declare-reduction-directive:
245/// annot_pragma_openmp 'declare' 'reduction'
246/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
247/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
248/// annot_pragma_openmp_end
249/// <reduction_id> is either a base language identifier or one of the following
250/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
251///
252Parser::DeclGroupPtrTy
253Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
254 // Parse '('.
255 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600256 if (T.expectAndConsume(
257 diag::err_expected_lparen_after,
258 getOpenMPDirectiveName(OMPD_declare_reduction).data())) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000259 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
260 return DeclGroupPtrTy();
261 }
262
263 DeclarationName Name = parseOpenMPReductionId(*this);
264 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
265 return DeclGroupPtrTy();
266
267 // Consume ':'.
268 bool IsCorrect = !ExpectAndConsume(tok::colon);
269
270 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
271 return DeclGroupPtrTy();
272
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000273 IsCorrect = IsCorrect && !Name.isEmpty();
274
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000275 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
276 Diag(Tok.getLocation(), diag::err_expected_type);
277 IsCorrect = false;
278 }
279
280 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
281 return DeclGroupPtrTy();
282
283 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
284 // Parse list of types until ':' token.
285 do {
286 ColonProtectionRAIIObject ColonRAII(*this);
287 SourceRange Range;
Faisal Vali421b2d12017-12-29 05:41:00 +0000288 TypeResult TR =
289 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000290 if (TR.isUsable()) {
Alexey Bataev61908f652018-04-23 19:53:05 +0000291 QualType ReductionType =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000292 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
293 if (!ReductionType.isNull()) {
294 ReductionTypes.push_back(
295 std::make_pair(ReductionType, Range.getBegin()));
296 }
297 } else {
298 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
299 StopBeforeMatch);
300 }
301
302 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
303 break;
304
305 // Consume ','.
306 if (ExpectAndConsume(tok::comma)) {
307 IsCorrect = false;
308 if (Tok.is(tok::annot_pragma_openmp_end)) {
309 Diag(Tok.getLocation(), diag::err_expected_type);
310 return DeclGroupPtrTy();
311 }
312 }
313 } while (Tok.isNot(tok::annot_pragma_openmp_end));
314
315 if (ReductionTypes.empty()) {
316 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
317 return DeclGroupPtrTy();
318 }
319
320 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
321 return DeclGroupPtrTy();
322
323 // Consume ':'.
324 if (ExpectAndConsume(tok::colon))
325 IsCorrect = false;
326
327 if (Tok.is(tok::annot_pragma_openmp_end)) {
328 Diag(Tok.getLocation(), diag::err_expected_expression);
329 return DeclGroupPtrTy();
330 }
331
332 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
333 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
334
335 // Parse <combiner> expression and then parse initializer if any for each
336 // correct type.
337 unsigned I = 0, E = ReductionTypes.size();
Alexey Bataev61908f652018-04-23 19:53:05 +0000338 for (Decl *D : DRD.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000339 TentativeParsingAction TPA(*this);
340 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000341 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000342 Scope::OpenMPDirectiveScope);
343 // Parse <combiner> expression.
344 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
345 ExprResult CombinerResult =
346 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000347 D->getLocation(), /*DiscardedValue*/ false);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000348 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
349
350 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
351 Tok.isNot(tok::annot_pragma_openmp_end)) {
352 TPA.Commit();
353 IsCorrect = false;
354 break;
355 }
356 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
357 ExprResult InitializerResult;
358 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
359 // Parse <initializer> expression.
360 if (Tok.is(tok::identifier) &&
Alexey Bataev61908f652018-04-23 19:53:05 +0000361 Tok.getIdentifierInfo()->isStr("initializer")) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000362 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000363 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000364 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
365 TPA.Commit();
366 IsCorrect = false;
367 break;
368 }
369 // Parse '('.
370 BalancedDelimiterTracker T(*this, tok::l_paren,
371 tok::annot_pragma_openmp_end);
372 IsCorrect =
373 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
374 IsCorrect;
375 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
376 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000377 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000378 Scope::OpenMPDirectiveScope);
379 // Parse expression.
Alexey Bataev070f43a2017-09-06 14:49:58 +0000380 VarDecl *OmpPrivParm =
381 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
382 D);
383 // Check if initializer is omp_priv <init_expr> or something else.
384 if (Tok.is(tok::identifier) &&
385 Tok.getIdentifierInfo()->isStr("omp_priv")) {
Alexey Bataev3c676e32019-11-12 11:19:26 -0500386 ConsumeToken();
387 ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000388 } else {
389 InitializerResult = Actions.ActOnFinishFullExpr(
390 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000391 /*DiscardedValue*/ false);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000392 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000393 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
Alexey Bataev070f43a2017-09-06 14:49:58 +0000394 D, InitializerResult.get(), OmpPrivParm);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000395 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
396 Tok.isNot(tok::annot_pragma_openmp_end)) {
397 TPA.Commit();
398 IsCorrect = false;
399 break;
400 }
401 IsCorrect =
402 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
403 }
404 }
405
406 ++I;
407 // Revert parsing if not the last type, otherwise accept it, we're done with
408 // parsing.
409 if (I != E)
410 TPA.Revert();
411 else
412 TPA.Commit();
413 }
414 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
415 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000416}
417
Alexey Bataev070f43a2017-09-06 14:49:58 +0000418void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
419 // Parse declarator '=' initializer.
420 // If a '==' or '+=' is found, suggest a fixit to '='.
421 if (isTokenEqualOrEqualTypo()) {
422 ConsumeToken();
423
424 if (Tok.is(tok::code_completion)) {
425 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
426 Actions.FinalizeDeclaration(OmpPrivParm);
427 cutOffParsing();
428 return;
429 }
430
Alexey Bataev3c676e32019-11-12 11:19:26 -0500431 PreferredType.enterVariableInit(Tok.getLocation(), OmpPrivParm);
432 ExprResult Init = ParseAssignmentExpression();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000433
434 if (Init.isInvalid()) {
435 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
436 Actions.ActOnInitializerError(OmpPrivParm);
437 } else {
438 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
439 /*DirectInit=*/false);
440 }
441 } else if (Tok.is(tok::l_paren)) {
442 // Parse C++ direct initializer: '(' expression-list ')'
443 BalancedDelimiterTracker T(*this, tok::l_paren);
444 T.consumeOpen();
445
446 ExprVector Exprs;
447 CommaLocsTy CommaLocs;
448
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000449 SourceLocation LParLoc = T.getOpenLocation();
Ilya Biryukovff2a9972019-02-26 11:01:50 +0000450 auto RunSignatureHelp = [this, OmpPrivParm, LParLoc, &Exprs]() {
451 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
452 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
453 OmpPrivParm->getLocation(), Exprs, LParLoc);
454 CalledSignatureHelp = true;
455 return PreferredType;
456 };
457 if (ParseExpressionList(Exprs, CommaLocs, [&] {
458 PreferredType.enterFunctionArgument(Tok.getLocation(),
459 RunSignatureHelp);
460 })) {
461 if (PP.isCodeCompletionReached() && !CalledSignatureHelp)
462 RunSignatureHelp();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000463 Actions.ActOnInitializerError(OmpPrivParm);
464 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
465 } else {
466 // Match the ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000467 SourceLocation RLoc = Tok.getLocation();
468 if (!T.consumeClose())
469 RLoc = T.getCloseLocation();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000470
471 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
472 "Unexpected number of commas!");
473
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000474 ExprResult Initializer =
475 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000476 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
477 /*DirectInit=*/true);
478 }
479 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
480 // Parse C++0x braced-init-list.
481 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
482
483 ExprResult Init(ParseBraceInitializer());
484
485 if (Init.isInvalid()) {
486 Actions.ActOnInitializerError(OmpPrivParm);
487 } else {
488 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
489 /*DirectInit=*/true);
490 }
491 } else {
492 Actions.ActOnUninitializedDecl(OmpPrivParm);
493 }
494}
495
Michael Kruse251e1482019-02-01 20:25:04 +0000496/// Parses 'omp declare mapper' directive.
497///
498/// declare-mapper-directive:
499/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifier> ':']
500/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
501/// annot_pragma_openmp_end
502/// <mapper-identifier> and <var> are base language identifiers.
503///
504Parser::DeclGroupPtrTy
505Parser::ParseOpenMPDeclareMapperDirective(AccessSpecifier AS) {
506 bool IsCorrect = true;
507 // Parse '('
508 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
509 if (T.expectAndConsume(diag::err_expected_lparen_after,
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600510 getOpenMPDirectiveName(OMPD_declare_mapper).data())) {
Michael Kruse251e1482019-02-01 20:25:04 +0000511 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
512 return DeclGroupPtrTy();
513 }
514
515 // Parse <mapper-identifier>
516 auto &DeclNames = Actions.getASTContext().DeclarationNames;
517 DeclarationName MapperId;
518 if (PP.LookAhead(0).is(tok::colon)) {
519 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
520 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
521 IsCorrect = false;
522 } else {
523 MapperId = DeclNames.getIdentifier(Tok.getIdentifierInfo());
524 }
525 ConsumeToken();
526 // Consume ':'.
527 ExpectAndConsume(tok::colon);
528 } else {
529 // If no mapper identifier is provided, its name is "default" by default
530 MapperId =
531 DeclNames.getIdentifier(&Actions.getASTContext().Idents.get("default"));
532 }
533
534 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
535 return DeclGroupPtrTy();
536
537 // Parse <type> <var>
538 DeclarationName VName;
539 QualType MapperType;
540 SourceRange Range;
541 TypeResult ParsedType = parseOpenMPDeclareMapperVarDecl(Range, VName, AS);
542 if (ParsedType.isUsable())
543 MapperType =
544 Actions.ActOnOpenMPDeclareMapperType(Range.getBegin(), ParsedType);
545 if (MapperType.isNull())
546 IsCorrect = false;
547 if (!IsCorrect) {
548 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
549 return DeclGroupPtrTy();
550 }
551
552 // Consume ')'.
553 IsCorrect &= !T.consumeClose();
554 if (!IsCorrect) {
555 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
556 return DeclGroupPtrTy();
557 }
558
559 // Enter scope.
560 OMPDeclareMapperDecl *DMD = Actions.ActOnOpenMPDeclareMapperDirectiveStart(
561 getCurScope(), Actions.getCurLexicalContext(), MapperId, MapperType,
562 Range.getBegin(), VName, AS);
563 DeclarationNameInfo DirName;
564 SourceLocation Loc = Tok.getLocation();
565 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
566 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
567 ParseScope OMPDirectiveScope(this, ScopeFlags);
568 Actions.StartOpenMPDSABlock(OMPD_declare_mapper, DirName, getCurScope(), Loc);
569
570 // Add the mapper variable declaration.
571 Actions.ActOnOpenMPDeclareMapperDirectiveVarDecl(
572 DMD, getCurScope(), MapperType, Range.getBegin(), VName);
573
574 // Parse map clauses.
575 SmallVector<OMPClause *, 6> Clauses;
576 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
577 OpenMPClauseKind CKind = Tok.isAnnotation()
578 ? OMPC_unknown
579 : getOpenMPClauseKind(PP.getSpelling(Tok));
580 Actions.StartOpenMPClause(CKind);
581 OMPClause *Clause =
582 ParseOpenMPClause(OMPD_declare_mapper, CKind, Clauses.size() == 0);
583 if (Clause)
584 Clauses.push_back(Clause);
585 else
586 IsCorrect = false;
587 // Skip ',' if any.
588 if (Tok.is(tok::comma))
589 ConsumeToken();
590 Actions.EndOpenMPClause();
591 }
592 if (Clauses.empty()) {
593 Diag(Tok, diag::err_omp_expected_clause)
594 << getOpenMPDirectiveName(OMPD_declare_mapper);
595 IsCorrect = false;
596 }
597
598 // Exit scope.
599 Actions.EndOpenMPDSABlock(nullptr);
600 OMPDirectiveScope.Exit();
601
602 DeclGroupPtrTy DGP =
603 Actions.ActOnOpenMPDeclareMapperDirectiveEnd(DMD, getCurScope(), Clauses);
604 if (!IsCorrect)
605 return DeclGroupPtrTy();
606 return DGP;
607}
608
609TypeResult Parser::parseOpenMPDeclareMapperVarDecl(SourceRange &Range,
610 DeclarationName &Name,
611 AccessSpecifier AS) {
612 // Parse the common declaration-specifiers piece.
613 Parser::DeclSpecContext DSC = Parser::DeclSpecContext::DSC_type_specifier;
614 DeclSpec DS(AttrFactory);
615 ParseSpecifierQualifierList(DS, AS, DSC);
616
617 // Parse the declarator.
618 DeclaratorContext Context = DeclaratorContext::PrototypeContext;
619 Declarator DeclaratorInfo(DS, Context);
620 ParseDeclarator(DeclaratorInfo);
621 Range = DeclaratorInfo.getSourceRange();
622 if (DeclaratorInfo.getIdentifier() == nullptr) {
623 Diag(Tok.getLocation(), diag::err_omp_mapper_expected_declarator);
624 return true;
625 }
626 Name = Actions.GetNameForDeclarator(DeclaratorInfo).getName();
627
628 return Actions.ActOnOpenMPDeclareMapperVarDecl(getCurScope(), DeclaratorInfo);
629}
630
Alexey Bataev2af33e32016-04-07 12:45:37 +0000631namespace {
632/// RAII that recreates function context for correct parsing of clauses of
633/// 'declare simd' construct.
634/// OpenMP, 2.8.2 declare simd Construct
635/// The expressions appearing in the clauses of this directive are evaluated in
636/// the scope of the arguments of the function declaration or definition.
637class FNContextRAII final {
638 Parser &P;
639 Sema::CXXThisScopeRAII *ThisScope;
640 Parser::ParseScope *TempScope;
641 Parser::ParseScope *FnScope;
642 bool HasTemplateScope = false;
643 bool HasFunScope = false;
644 FNContextRAII() = delete;
645 FNContextRAII(const FNContextRAII &) = delete;
646 FNContextRAII &operator=(const FNContextRAII &) = delete;
647
648public:
649 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
650 Decl *D = *Ptr.get().begin();
651 NamedDecl *ND = dyn_cast<NamedDecl>(D);
652 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
653 Sema &Actions = P.getActions();
654
655 // Allow 'this' within late-parsed attributes.
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000656 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, Qualifiers(),
Alexey Bataev2af33e32016-04-07 12:45:37 +0000657 ND && ND->isCXXInstanceMember());
658
659 // If the Decl is templatized, add template parameters to scope.
660 HasTemplateScope = D->isTemplateDecl();
661 TempScope =
662 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
663 if (HasTemplateScope)
664 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
665
666 // If the Decl is on a function, add function parameters to the scope.
667 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000668 FnScope = new Parser::ParseScope(
669 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
670 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000671 if (HasFunScope)
672 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
673 }
674 ~FNContextRAII() {
675 if (HasFunScope) {
676 P.getActions().ActOnExitFunctionContext();
677 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
678 }
679 if (HasTemplateScope)
680 TempScope->Exit();
681 delete FnScope;
682 delete TempScope;
683 delete ThisScope;
684 }
685};
686} // namespace
687
Alexey Bataevd93d3762016-04-12 09:35:56 +0000688/// Parses clauses for 'declare simd' directive.
689/// clause:
690/// 'inbranch' | 'notinbranch'
691/// 'simdlen' '(' <expr> ')'
692/// { 'uniform' '(' <argument_list> ')' }
693/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000694/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
695static bool parseDeclareSimdClauses(
696 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
697 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
698 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
699 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000700 SourceRange BSRange;
701 const Token &Tok = P.getCurToken();
702 bool IsError = false;
703 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
704 if (Tok.isNot(tok::identifier))
705 break;
706 OMPDeclareSimdDeclAttr::BranchStateTy Out;
707 IdentifierInfo *II = Tok.getIdentifierInfo();
708 StringRef ClauseName = II->getName();
709 // Parse 'inranch|notinbranch' clauses.
710 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
711 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
712 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
713 << ClauseName
714 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
715 IsError = true;
716 }
717 BS = Out;
718 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
719 P.ConsumeToken();
720 } else if (ClauseName.equals("simdlen")) {
721 if (SimdLen.isUsable()) {
722 P.Diag(Tok, diag::err_omp_more_one_clause)
723 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
724 IsError = true;
725 }
726 P.ConsumeToken();
727 SourceLocation RLoc;
728 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
729 if (SimdLen.isInvalid())
730 IsError = true;
731 } else {
732 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000733 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
734 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000735 Parser::OpenMPVarListDataTy Data;
Alexey Bataev61908f652018-04-23 19:53:05 +0000736 SmallVectorImpl<Expr *> *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000737 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000738 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000739 else if (CKind == OMPC_linear)
740 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000741
742 P.ConsumeToken();
743 if (P.ParseOpenMPVarList(OMPD_declare_simd,
744 getOpenMPClauseKind(ClauseName), *Vars, Data))
745 IsError = true;
Alexey Bataev61908f652018-04-23 19:53:05 +0000746 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000747 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataev61908f652018-04-23 19:53:05 +0000748 } else if (CKind == OMPC_linear) {
Alexey Bataevecba70f2016-04-12 11:02:11 +0000749 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
750 Data.DepLinMapLoc))
751 Data.LinKind = OMPC_LINEAR_val;
752 LinModifiers.append(Linears.size() - LinModifiers.size(),
753 Data.LinKind);
754 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
755 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000756 } else
757 // TODO: add parsing of other clauses.
758 break;
759 }
760 // Skip ',' if any.
761 if (Tok.is(tok::comma))
762 P.ConsumeToken();
763 }
764 return IsError;
765}
766
Alexey Bataev2af33e32016-04-07 12:45:37 +0000767/// Parse clauses for '#pragma omp declare simd'.
768Parser::DeclGroupPtrTy
769Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
770 CachedTokens &Toks, SourceLocation Loc) {
Ilya Biryukov929af672019-05-17 09:32:05 +0000771 PP.EnterToken(Tok, /*IsReinject*/ true);
772 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
773 /*IsReinject*/ true);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000774 // Consume the previously pushed token.
775 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Alexey Bataevd158cf62019-09-13 20:18:17 +0000776 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000777
778 FNContextRAII FnContext(*this, Ptr);
779 OMPDeclareSimdDeclAttr::BranchStateTy BS =
780 OMPDeclareSimdDeclAttr::BS_Undefined;
781 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000782 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000783 SmallVector<Expr *, 4> Aligneds;
784 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000785 SmallVector<Expr *, 4> Linears;
786 SmallVector<unsigned, 4> LinModifiers;
787 SmallVector<Expr *, 4> Steps;
788 bool IsError =
789 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
790 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000791 // Need to check for extra tokens.
792 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
793 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
794 << getOpenMPDirectiveName(OMPD_declare_simd);
795 while (Tok.isNot(tok::annot_pragma_openmp_end))
796 ConsumeAnyToken();
797 }
798 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000799 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000800 if (IsError)
801 return Ptr;
802 return Actions.ActOnOpenMPDeclareSimdDirective(
803 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
804 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataev20dfd772016-04-04 10:12:15 +0000805}
806
Alexey Bataeva15a1412019-10-02 18:19:02 +0000807/// Parse optional 'score' '(' <expr> ')' ':'.
808static ExprResult parseContextScore(Parser &P) {
809 ExprResult ScoreExpr;
Alexey Bataevfde11e92019-11-07 11:03:10 -0500810 Sema::OMPCtxStringType Buffer;
Alexey Bataeva15a1412019-10-02 18:19:02 +0000811 StringRef SelectorName =
812 P.getPreprocessor().getSpelling(P.getCurToken(), Buffer);
Alexey Bataevdcec2ac2019-11-05 15:33:18 -0500813 if (!SelectorName.equals("score"))
Alexey Bataeva15a1412019-10-02 18:19:02 +0000814 return ScoreExpr;
Alexey Bataeva15a1412019-10-02 18:19:02 +0000815 (void)P.ConsumeToken();
816 SourceLocation RLoc;
817 ScoreExpr = P.ParseOpenMPParensExpr(SelectorName, RLoc);
818 // Parse ':'
819 if (P.getCurToken().is(tok::colon))
820 (void)P.ConsumeAnyToken();
821 else
822 P.Diag(P.getCurToken(), diag::warn_pragma_expected_colon)
823 << "context selector score clause";
824 return ScoreExpr;
825}
826
Alexey Bataev9ff34742019-09-25 19:43:37 +0000827/// Parse context selector for 'implementation' selector set:
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000828/// 'vendor' '(' [ 'score' '(' <score _expr> ')' ':' ] <vendor> { ',' <vendor> }
829/// ')'
Alexey Bataevfde11e92019-11-07 11:03:10 -0500830static void
831parseImplementationSelector(Parser &P, SourceLocation Loc,
832 llvm::StringMap<SourceLocation> &UsedCtx,
833 SmallVectorImpl<Sema::OMPCtxSelectorData> &Data) {
Alexey Bataev9ff34742019-09-25 19:43:37 +0000834 const Token &Tok = P.getCurToken();
835 // Parse inner context selector set name, if any.
836 if (!Tok.is(tok::identifier)) {
837 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
838 << "implementation";
839 // Skip until either '}', ')', or end of directive.
840 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
841 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
842 ;
843 return;
844 }
Alexey Bataevfde11e92019-11-07 11:03:10 -0500845 Sema::OMPCtxStringType Buffer;
Alexey Bataev9ff34742019-09-25 19:43:37 +0000846 StringRef CtxSelectorName = P.getPreprocessor().getSpelling(Tok, Buffer);
Alexey Bataev70d2e542019-10-08 17:47:52 +0000847 auto Res = UsedCtx.try_emplace(CtxSelectorName, Tok.getLocation());
848 if (!Res.second) {
849 // OpenMP 5.0, 2.3.2 Context Selectors, Restrictions.
850 // Each trait-selector-name can only be specified once.
851 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_ctx_mutiple_use)
852 << CtxSelectorName << "implementation";
853 P.Diag(Res.first->getValue(), diag::note_omp_declare_variant_ctx_used_here)
854 << CtxSelectorName;
855 }
Alexey Bataevfde11e92019-11-07 11:03:10 -0500856 OpenMPContextSelectorKind CSKind = getOpenMPContextSelector(CtxSelectorName);
Alexey Bataev9ff34742019-09-25 19:43:37 +0000857 (void)P.ConsumeToken();
858 switch (CSKind) {
Alexey Bataevfde11e92019-11-07 11:03:10 -0500859 case OMP_CTX_vendor: {
Alexey Bataev9ff34742019-09-25 19:43:37 +0000860 // Parse '('.
861 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
862 (void)T.expectAndConsume(diag::err_expected_lparen_after,
863 CtxSelectorName.data());
Alexey Bataevfde11e92019-11-07 11:03:10 -0500864 ExprResult Score = parseContextScore(P);
865 llvm::UniqueVector<Sema::OMPCtxStringType> Vendors;
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000866 do {
867 // Parse <vendor>.
868 StringRef VendorName;
869 if (Tok.is(tok::identifier)) {
870 Buffer.clear();
871 VendorName = P.getPreprocessor().getSpelling(P.getCurToken(), Buffer);
872 (void)P.ConsumeToken();
Alexey Bataev303657a2019-10-08 19:44:16 +0000873 if (!VendorName.empty())
Alexey Bataev4513e93f2019-10-10 15:15:26 +0000874 Vendors.insert(VendorName);
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000875 } else {
876 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_item_expected)
877 << "vendor identifier"
878 << "vendor"
879 << "implementation";
880 }
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000881 if (!P.TryConsumeToken(tok::comma) && Tok.isNot(tok::r_paren)) {
882 P.Diag(Tok, diag::err_expected_punc)
883 << (VendorName.empty() ? "vendor name" : VendorName);
884 }
885 } while (Tok.is(tok::identifier));
Alexey Bataev9ff34742019-09-25 19:43:37 +0000886 // Parse ')'.
887 (void)T.consumeClose();
Alexey Bataevfde11e92019-11-07 11:03:10 -0500888 if (!Vendors.empty())
889 Data.emplace_back(OMP_CTX_SET_implementation, CSKind, Score, Vendors);
Alexey Bataev9ff34742019-09-25 19:43:37 +0000890 break;
891 }
Alexey Bataev4e8231b2019-11-05 15:13:30 -0500892 case OMP_CTX_kind:
Alexey Bataevfde11e92019-11-07 11:03:10 -0500893 case OMP_CTX_unknown:
Alexey Bataev9ff34742019-09-25 19:43:37 +0000894 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
895 << "implementation";
896 // Skip until either '}', ')', or end of directive.
897 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
898 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
899 ;
900 return;
901 }
Alexey Bataev9ff34742019-09-25 19:43:37 +0000902}
903
Alexey Bataev4e8231b2019-11-05 15:13:30 -0500904/// Parse context selector for 'device' selector set:
905/// 'kind' '(' <kind> { ',' <kind> } ')'
906static void
907parseDeviceSelector(Parser &P, SourceLocation Loc,
908 llvm::StringMap<SourceLocation> &UsedCtx,
909 SmallVectorImpl<Sema::OMPCtxSelectorData> &Data) {
910 const Token &Tok = P.getCurToken();
911 // Parse inner context selector set name, if any.
912 if (!Tok.is(tok::identifier)) {
913 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
914 << "device";
915 // Skip until either '}', ')', or end of directive.
916 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
917 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
918 ;
919 return;
920 }
921 Sema::OMPCtxStringType Buffer;
922 StringRef CtxSelectorName = P.getPreprocessor().getSpelling(Tok, Buffer);
923 auto Res = UsedCtx.try_emplace(CtxSelectorName, Tok.getLocation());
924 if (!Res.second) {
925 // OpenMP 5.0, 2.3.2 Context Selectors, Restrictions.
926 // Each trait-selector-name can only be specified once.
927 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_ctx_mutiple_use)
928 << CtxSelectorName << "device";
929 P.Diag(Res.first->getValue(), diag::note_omp_declare_variant_ctx_used_here)
930 << CtxSelectorName;
931 }
932 OpenMPContextSelectorKind CSKind = getOpenMPContextSelector(CtxSelectorName);
933 (void)P.ConsumeToken();
934 switch (CSKind) {
935 case OMP_CTX_kind: {
936 // Parse '('.
937 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
938 (void)T.expectAndConsume(diag::err_expected_lparen_after,
939 CtxSelectorName.data());
940 llvm::UniqueVector<Sema::OMPCtxStringType> Kinds;
941 do {
942 // Parse <kind>.
943 StringRef KindName;
944 if (Tok.is(tok::identifier)) {
945 Buffer.clear();
946 KindName = P.getPreprocessor().getSpelling(P.getCurToken(), Buffer);
947 SourceLocation SLoc = P.getCurToken().getLocation();
948 (void)P.ConsumeToken();
949 if (llvm::StringSwitch<bool>(KindName)
950 .Case("host", false)
951 .Case("nohost", false)
952 .Case("cpu", false)
953 .Case("gpu", false)
954 .Case("fpga", false)
955 .Default(true)) {
956 P.Diag(SLoc, diag::err_omp_wrong_device_kind_trait) << KindName;
957 } else {
958 Kinds.insert(KindName);
959 }
960 } else {
961 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_item_expected)
962 << "'host', 'nohost', 'cpu', 'gpu', or 'fpga'"
963 << "kind"
964 << "device";
965 }
966 if (!P.TryConsumeToken(tok::comma) && Tok.isNot(tok::r_paren)) {
967 P.Diag(Tok, diag::err_expected_punc)
968 << (KindName.empty() ? "kind of device" : KindName);
969 }
970 } while (Tok.is(tok::identifier));
971 // Parse ')'.
972 (void)T.consumeClose();
973 if (!Kinds.empty())
974 Data.emplace_back(OMP_CTX_SET_device, CSKind, ExprResult(), Kinds);
975 break;
976 }
977 case OMP_CTX_vendor:
978 case OMP_CTX_unknown:
979 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
980 << "device";
981 // Skip until either '}', ')', or end of directive.
982 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
983 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
984 ;
985 return;
986 }
987}
988
Alexey Bataevd158cf62019-09-13 20:18:17 +0000989/// Parses clauses for 'declare variant' directive.
990/// clause:
Alexey Bataevd158cf62019-09-13 20:18:17 +0000991/// <selector_set_name> '=' '{' <context_selectors> '}'
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000992/// [ ',' <selector_set_name> '=' '{' <context_selectors> '}' ]
993bool Parser::parseOpenMPContextSelectors(
Alexey Bataevfde11e92019-11-07 11:03:10 -0500994 SourceLocation Loc, SmallVectorImpl<Sema::OMPCtxSelectorData> &Data) {
Alexey Bataev5d154c32019-10-08 15:56:43 +0000995 llvm::StringMap<SourceLocation> UsedCtxSets;
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000996 do {
997 // Parse inner context selector set name.
998 if (!Tok.is(tok::identifier)) {
999 Diag(Tok.getLocation(), diag::err_omp_declare_variant_no_ctx_selector)
Alexey Bataevdba792c2019-09-23 18:13:31 +00001000 << getOpenMPClauseName(OMPC_match);
Alexey Bataevd158cf62019-09-13 20:18:17 +00001001 return true;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001002 }
Alexey Bataevfde11e92019-11-07 11:03:10 -05001003 Sema::OMPCtxStringType Buffer;
Alexey Bataev9ff34742019-09-25 19:43:37 +00001004 StringRef CtxSelectorSetName = PP.getSpelling(Tok, Buffer);
Alexey Bataev5d154c32019-10-08 15:56:43 +00001005 auto Res = UsedCtxSets.try_emplace(CtxSelectorSetName, Tok.getLocation());
1006 if (!Res.second) {
1007 // OpenMP 5.0, 2.3.2 Context Selectors, Restrictions.
1008 // Each trait-set-selector-name can only be specified once.
1009 Diag(Tok.getLocation(), diag::err_omp_declare_variant_ctx_set_mutiple_use)
1010 << CtxSelectorSetName;
1011 Diag(Res.first->getValue(),
1012 diag::note_omp_declare_variant_ctx_set_used_here)
1013 << CtxSelectorSetName;
1014 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001015 // Parse '='.
1016 (void)ConsumeToken();
1017 if (Tok.isNot(tok::equal)) {
1018 Diag(Tok.getLocation(), diag::err_omp_declare_variant_equal_expected)
Alexey Bataev9ff34742019-09-25 19:43:37 +00001019 << CtxSelectorSetName;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001020 return true;
1021 }
1022 (void)ConsumeToken();
1023 // TBD: add parsing of known context selectors.
1024 // Unknown selector - just ignore it completely.
1025 {
1026 // Parse '{'.
1027 BalancedDelimiterTracker TBr(*this, tok::l_brace,
1028 tok::annot_pragma_openmp_end);
1029 if (TBr.expectAndConsume(diag::err_expected_lbrace_after, "="))
1030 return true;
Alexey Bataevfde11e92019-11-07 11:03:10 -05001031 OpenMPContextSelectorSetKind CSSKind =
1032 getOpenMPContextSelectorSet(CtxSelectorSetName);
Alexey Bataev70d2e542019-10-08 17:47:52 +00001033 llvm::StringMap<SourceLocation> UsedCtx;
1034 do {
1035 switch (CSSKind) {
Alexey Bataevfde11e92019-11-07 11:03:10 -05001036 case OMP_CTX_SET_implementation:
1037 parseImplementationSelector(*this, Loc, UsedCtx, Data);
Alexey Bataev70d2e542019-10-08 17:47:52 +00001038 break;
Alexey Bataev4e8231b2019-11-05 15:13:30 -05001039 case OMP_CTX_SET_device:
1040 parseDeviceSelector(*this, Loc, UsedCtx, Data);
1041 break;
Alexey Bataevfde11e92019-11-07 11:03:10 -05001042 case OMP_CTX_SET_unknown:
Alexey Bataev70d2e542019-10-08 17:47:52 +00001043 // Skip until either '}', ')', or end of directive.
1044 while (!SkipUntil(tok::r_brace, tok::r_paren,
1045 tok::annot_pragma_openmp_end, StopBeforeMatch))
1046 ;
1047 break;
1048 }
1049 const Token PrevTok = Tok;
1050 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
1051 Diag(Tok, diag::err_omp_expected_comma_brace)
1052 << (PrevTok.isAnnotation() ? "context selector trait"
1053 : PP.getSpelling(PrevTok));
1054 } while (Tok.is(tok::identifier));
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001055 // Parse '}'.
1056 (void)TBr.consumeClose();
1057 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001058 // Consume ','
1059 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end))
1060 (void)ExpectAndConsume(tok::comma);
1061 } while (Tok.isAnyIdentifier());
Alexey Bataevd158cf62019-09-13 20:18:17 +00001062 return false;
1063}
1064
1065/// Parse clauses for '#pragma omp declare variant ( variant-func-id ) clause'.
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001066void Parser::ParseOMPDeclareVariantClauses(Parser::DeclGroupPtrTy Ptr,
1067 CachedTokens &Toks,
1068 SourceLocation Loc) {
Alexey Bataevd158cf62019-09-13 20:18:17 +00001069 PP.EnterToken(Tok, /*IsReinject*/ true);
1070 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
1071 /*IsReinject*/ true);
1072 // Consume the previously pushed token.
1073 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
1074 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
1075
1076 FNContextRAII FnContext(*this, Ptr);
1077 // Parse function declaration id.
1078 SourceLocation RLoc;
1079 // Parse with IsAddressOfOperand set to true to parse methods as DeclRefExprs
1080 // instead of MemberExprs.
Alexey Bataev02d04d52019-12-10 16:12:53 -05001081 ExprResult AssociatedFunction;
1082 {
1083 // Do not mark function as is used to prevent its emission if this is the
1084 // only place where it is used.
1085 EnterExpressionEvaluationContext Unevaluated(
1086 Actions, Sema::ExpressionEvaluationContext::Unevaluated);
1087 AssociatedFunction = ParseOpenMPParensExpr(
1088 getOpenMPDirectiveName(OMPD_declare_variant), RLoc,
1089 /*IsAddressOfOperand=*/true);
1090 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001091 if (!AssociatedFunction.isUsable()) {
1092 if (!Tok.is(tok::annot_pragma_openmp_end))
1093 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
1094 ;
1095 // Skip the last annot_pragma_openmp_end.
1096 (void)ConsumeAnnotationToken();
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001097 return;
1098 }
1099 Optional<std::pair<FunctionDecl *, Expr *>> DeclVarData =
1100 Actions.checkOpenMPDeclareVariantFunction(
1101 Ptr, AssociatedFunction.get(), SourceRange(Loc, Tok.getLocation()));
1102
1103 // Parse 'match'.
Alexey Bataevdba792c2019-09-23 18:13:31 +00001104 OpenMPClauseKind CKind = Tok.isAnnotation()
1105 ? OMPC_unknown
1106 : getOpenMPClauseKind(PP.getSpelling(Tok));
1107 if (CKind != OMPC_match) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001108 Diag(Tok.getLocation(), diag::err_omp_declare_variant_wrong_clause)
Alexey Bataevdba792c2019-09-23 18:13:31 +00001109 << getOpenMPClauseName(OMPC_match);
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001110 while (!SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
1111 ;
1112 // Skip the last annot_pragma_openmp_end.
1113 (void)ConsumeAnnotationToken();
1114 return;
1115 }
1116 (void)ConsumeToken();
1117 // Parse '('.
1118 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataevdba792c2019-09-23 18:13:31 +00001119 if (T.expectAndConsume(diag::err_expected_lparen_after,
1120 getOpenMPClauseName(OMPC_match))) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001121 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
1122 ;
1123 // Skip the last annot_pragma_openmp_end.
1124 (void)ConsumeAnnotationToken();
1125 return;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001126 }
1127
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001128 // Parse inner context selectors.
Alexey Bataevfde11e92019-11-07 11:03:10 -05001129 SmallVector<Sema::OMPCtxSelectorData, 4> Data;
1130 if (!parseOpenMPContextSelectors(Loc, Data)) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001131 // Parse ')'.
1132 (void)T.consumeClose();
1133 // Need to check for extra tokens.
1134 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1135 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1136 << getOpenMPDirectiveName(OMPD_declare_variant);
1137 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001138 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001139
1140 // Skip last tokens.
1141 while (Tok.isNot(tok::annot_pragma_openmp_end))
1142 ConsumeAnyToken();
Alexey Bataevfde11e92019-11-07 11:03:10 -05001143 if (DeclVarData.hasValue())
1144 Actions.ActOnOpenMPDeclareVariantDirective(
1145 DeclVarData.getValue().first, DeclVarData.getValue().second,
1146 SourceRange(Loc, Tok.getLocation()), Data);
Alexey Bataevd158cf62019-09-13 20:18:17 +00001147 // Skip the last annot_pragma_openmp_end.
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001148 (void)ConsumeAnnotationToken();
Alexey Bataevd158cf62019-09-13 20:18:17 +00001149}
1150
Alexey Bataev729e2422019-08-23 16:11:14 +00001151/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
1152///
1153/// default-clause:
1154/// 'default' '(' 'none' | 'shared' ')
1155///
1156/// proc_bind-clause:
1157/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1158///
1159/// device_type-clause:
1160/// 'device_type' '(' 'host' | 'nohost' | 'any' )'
1161namespace {
1162 struct SimpleClauseData {
1163 unsigned Type;
1164 SourceLocation Loc;
1165 SourceLocation LOpen;
1166 SourceLocation TypeLoc;
1167 SourceLocation RLoc;
1168 SimpleClauseData(unsigned Type, SourceLocation Loc, SourceLocation LOpen,
1169 SourceLocation TypeLoc, SourceLocation RLoc)
1170 : Type(Type), Loc(Loc), LOpen(LOpen), TypeLoc(TypeLoc), RLoc(RLoc) {}
1171 };
1172} // anonymous namespace
1173
1174static Optional<SimpleClauseData>
1175parseOpenMPSimpleClause(Parser &P, OpenMPClauseKind Kind) {
1176 const Token &Tok = P.getCurToken();
1177 SourceLocation Loc = Tok.getLocation();
1178 SourceLocation LOpen = P.ConsumeToken();
1179 // Parse '('.
1180 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
1181 if (T.expectAndConsume(diag::err_expected_lparen_after,
1182 getOpenMPClauseName(Kind)))
1183 return llvm::None;
1184
1185 unsigned Type = getOpenMPSimpleClauseType(
1186 Kind, Tok.isAnnotation() ? "" : P.getPreprocessor().getSpelling(Tok));
1187 SourceLocation TypeLoc = Tok.getLocation();
1188 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1189 Tok.isNot(tok::annot_pragma_openmp_end))
1190 P.ConsumeAnyToken();
1191
1192 // Parse ')'.
1193 SourceLocation RLoc = Tok.getLocation();
1194 if (!T.consumeClose())
1195 RLoc = T.getCloseLocation();
1196
1197 return SimpleClauseData(Type, Loc, LOpen, TypeLoc, RLoc);
1198}
1199
Kelvin Lie0502752018-11-21 20:15:57 +00001200Parser::DeclGroupPtrTy Parser::ParseOMPDeclareTargetClauses() {
1201 // OpenMP 4.5 syntax with list of entities.
1202 Sema::NamedDeclSetType SameDirectiveDecls;
Alexey Bataev729e2422019-08-23 16:11:14 +00001203 SmallVector<std::tuple<OMPDeclareTargetDeclAttr::MapTypeTy, SourceLocation,
1204 NamedDecl *>,
1205 4>
1206 DeclareTargetDecls;
1207 OMPDeclareTargetDeclAttr::DevTypeTy DT = OMPDeclareTargetDeclAttr::DT_Any;
1208 SourceLocation DeviceTypeLoc;
Kelvin Lie0502752018-11-21 20:15:57 +00001209 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1210 OMPDeclareTargetDeclAttr::MapTypeTy MT = OMPDeclareTargetDeclAttr::MT_To;
1211 if (Tok.is(tok::identifier)) {
1212 IdentifierInfo *II = Tok.getIdentifierInfo();
1213 StringRef ClauseName = II->getName();
Alexey Bataev729e2422019-08-23 16:11:14 +00001214 bool IsDeviceTypeClause =
1215 getLangOpts().OpenMP >= 50 &&
1216 getOpenMPClauseKind(ClauseName) == OMPC_device_type;
1217 // Parse 'to|link|device_type' clauses.
1218 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName, MT) &&
1219 !IsDeviceTypeClause) {
1220 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
1221 << ClauseName << (getLangOpts().OpenMP >= 50 ? 1 : 0);
Kelvin Lie0502752018-11-21 20:15:57 +00001222 break;
1223 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001224 // Parse 'device_type' clause and go to next clause if any.
1225 if (IsDeviceTypeClause) {
1226 Optional<SimpleClauseData> DevTypeData =
1227 parseOpenMPSimpleClause(*this, OMPC_device_type);
1228 if (DevTypeData.hasValue()) {
1229 if (DeviceTypeLoc.isValid()) {
1230 // We already saw another device_type clause, diagnose it.
1231 Diag(DevTypeData.getValue().Loc,
1232 diag::warn_omp_more_one_device_type_clause);
1233 }
1234 switch(static_cast<OpenMPDeviceType>(DevTypeData.getValue().Type)) {
1235 case OMPC_DEVICE_TYPE_any:
1236 DT = OMPDeclareTargetDeclAttr::DT_Any;
1237 break;
1238 case OMPC_DEVICE_TYPE_host:
1239 DT = OMPDeclareTargetDeclAttr::DT_Host;
1240 break;
1241 case OMPC_DEVICE_TYPE_nohost:
1242 DT = OMPDeclareTargetDeclAttr::DT_NoHost;
1243 break;
1244 case OMPC_DEVICE_TYPE_unknown:
1245 llvm_unreachable("Unexpected device_type");
1246 }
1247 DeviceTypeLoc = DevTypeData.getValue().Loc;
1248 }
1249 continue;
1250 }
Kelvin Lie0502752018-11-21 20:15:57 +00001251 ConsumeToken();
1252 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001253 auto &&Callback = [this, MT, &DeclareTargetDecls, &SameDirectiveDecls](
1254 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
1255 NamedDecl *ND = Actions.lookupOpenMPDeclareTargetName(
1256 getCurScope(), SS, NameInfo, SameDirectiveDecls);
1257 if (ND)
1258 DeclareTargetDecls.emplace_back(MT, NameInfo.getLoc(), ND);
Kelvin Lie0502752018-11-21 20:15:57 +00001259 };
1260 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
1261 /*AllowScopeSpecifier=*/true))
1262 break;
1263
1264 // Consume optional ','.
1265 if (Tok.is(tok::comma))
1266 ConsumeToken();
1267 }
1268 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1269 ConsumeAnyToken();
Alexey Bataev729e2422019-08-23 16:11:14 +00001270 for (auto &MTLocDecl : DeclareTargetDecls) {
1271 OMPDeclareTargetDeclAttr::MapTypeTy MT;
1272 SourceLocation Loc;
1273 NamedDecl *ND;
1274 std::tie(MT, Loc, ND) = MTLocDecl;
1275 // device_type clause is applied only to functions.
1276 Actions.ActOnOpenMPDeclareTargetName(
1277 ND, Loc, MT, isa<VarDecl>(ND) ? OMPDeclareTargetDeclAttr::DT_Any : DT);
1278 }
Kelvin Lie0502752018-11-21 20:15:57 +00001279 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
1280 SameDirectiveDecls.end());
1281 if (Decls.empty())
1282 return DeclGroupPtrTy();
1283 return Actions.BuildDeclaratorGroup(Decls);
1284}
1285
1286void Parser::ParseOMPEndDeclareTargetDirective(OpenMPDirectiveKind DKind,
1287 SourceLocation DTLoc) {
1288 if (DKind != OMPD_end_declare_target) {
1289 Diag(Tok, diag::err_expected_end_declare_target);
1290 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
1291 return;
1292 }
1293 ConsumeAnyToken();
1294 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1295 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1296 << getOpenMPDirectiveName(OMPD_end_declare_target);
1297 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1298 }
1299 // Skip the last annot_pragma_openmp_end.
1300 ConsumeAnyToken();
1301}
1302
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001303/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001304///
1305/// threadprivate-directive:
1306/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001307/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +00001308///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001309/// allocate-directive:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001310/// annot_pragma_openmp 'allocate' simple-variable-list [<clause>]
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001311/// annot_pragma_openmp_end
1312///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001313/// declare-reduction-directive:
1314/// annot_pragma_openmp 'declare' 'reduction' [...]
1315/// annot_pragma_openmp_end
1316///
Michael Kruse251e1482019-02-01 20:25:04 +00001317/// declare-mapper-directive:
1318/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1319/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1320/// annot_pragma_openmp_end
1321///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001322/// declare-simd-directive:
1323/// annot_pragma_openmp 'declare simd' {<clause> [,]}
1324/// annot_pragma_openmp_end
1325/// <function declaration/definition>
1326///
Kelvin Li1408f912018-09-26 04:28:39 +00001327/// requires directive:
1328/// annot_pragma_openmp 'requires' <clause> [[[,] <clause>] ... ]
1329/// annot_pragma_openmp_end
1330///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001331Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
1332 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
1333 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001334 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataev8035bb42019-12-13 16:05:30 -05001335 ParsingOpenMPDirectiveRAII DirScope(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001336 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +00001337
Richard Smithaf3b3252017-05-18 19:21:48 +00001338 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001339 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001340
1341 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001342 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +00001343 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001344 DeclDirectiveListParserHelper Helper(this, DKind);
1345 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1346 /*AllowScopeSpecifier=*/true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001347 // The last seen token is annot_pragma_openmp_end - need to check for
1348 // extra tokens.
1349 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1350 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001351 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001352 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +00001353 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001354 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001355 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001356 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
1357 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +00001358 }
1359 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001360 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001361 case OMPD_allocate: {
1362 ConsumeToken();
1363 DeclDirectiveListParserHelper Helper(this, DKind);
1364 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1365 /*AllowScopeSpecifier=*/true)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001366 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001367 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001368 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1369 OMPC_unknown + 1>
1370 FirstClauses(OMPC_unknown + 1);
1371 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1372 OpenMPClauseKind CKind =
1373 Tok.isAnnotation() ? OMPC_unknown
1374 : getOpenMPClauseKind(PP.getSpelling(Tok));
1375 Actions.StartOpenMPClause(CKind);
1376 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1377 !FirstClauses[CKind].getInt());
1378 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1379 StopBeforeMatch);
1380 FirstClauses[CKind].setInt(true);
1381 if (Clause != nullptr)
1382 Clauses.push_back(Clause);
1383 if (Tok.is(tok::annot_pragma_openmp_end)) {
1384 Actions.EndOpenMPClause();
1385 break;
1386 }
1387 // Skip ',' if any.
1388 if (Tok.is(tok::comma))
1389 ConsumeToken();
1390 Actions.EndOpenMPClause();
1391 }
1392 // The last seen token is annot_pragma_openmp_end - need to check for
1393 // extra tokens.
1394 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1395 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1396 << getOpenMPDirectiveName(DKind);
1397 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1398 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001399 }
1400 // Skip the last annot_pragma_openmp_end.
1401 ConsumeAnnotationToken();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001402 return Actions.ActOnOpenMPAllocateDirective(Loc, Helper.getIdentifiers(),
1403 Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001404 }
1405 break;
1406 }
Kelvin Li1408f912018-09-26 04:28:39 +00001407 case OMPD_requires: {
1408 SourceLocation StartLoc = ConsumeToken();
1409 SmallVector<OMPClause *, 5> Clauses;
1410 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
1411 FirstClauses(OMPC_unknown + 1);
1412 if (Tok.is(tok::annot_pragma_openmp_end)) {
Ilya Biryukovff2a9972019-02-26 11:01:50 +00001413 Diag(Tok, diag::err_omp_expected_clause)
Kelvin Li1408f912018-09-26 04:28:39 +00001414 << getOpenMPDirectiveName(OMPD_requires);
1415 break;
1416 }
1417 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1418 OpenMPClauseKind CKind = Tok.isAnnotation()
1419 ? OMPC_unknown
1420 : getOpenMPClauseKind(PP.getSpelling(Tok));
1421 Actions.StartOpenMPClause(CKind);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001422 OMPClause *Clause = ParseOpenMPClause(OMPD_requires, CKind,
1423 !FirstClauses[CKind].getInt());
1424 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1425 StopBeforeMatch);
Kelvin Li1408f912018-09-26 04:28:39 +00001426 FirstClauses[CKind].setInt(true);
1427 if (Clause != nullptr)
1428 Clauses.push_back(Clause);
1429 if (Tok.is(tok::annot_pragma_openmp_end)) {
1430 Actions.EndOpenMPClause();
1431 break;
1432 }
1433 // Skip ',' if any.
1434 if (Tok.is(tok::comma))
1435 ConsumeToken();
1436 Actions.EndOpenMPClause();
1437 }
1438 // Consume final annot_pragma_openmp_end
1439 if (Clauses.size() == 0) {
1440 Diag(Tok, diag::err_omp_expected_clause)
1441 << getOpenMPDirectiveName(OMPD_requires);
1442 ConsumeAnnotationToken();
1443 return nullptr;
1444 }
1445 ConsumeAnnotationToken();
1446 return Actions.ActOnOpenMPRequiresDirective(StartLoc, Clauses);
1447 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001448 case OMPD_declare_reduction:
1449 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001450 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001451 // The last seen token is annot_pragma_openmp_end - need to check for
1452 // extra tokens.
1453 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1454 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1455 << getOpenMPDirectiveName(OMPD_declare_reduction);
1456 while (Tok.isNot(tok::annot_pragma_openmp_end))
1457 ConsumeAnyToken();
1458 }
1459 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001460 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001461 return Res;
1462 }
1463 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001464 case OMPD_declare_mapper: {
1465 ConsumeToken();
1466 if (DeclGroupPtrTy Res = ParseOpenMPDeclareMapperDirective(AS)) {
1467 // Skip the last annot_pragma_openmp_end.
1468 ConsumeAnnotationToken();
1469 return Res;
1470 }
1471 break;
1472 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001473 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001474 case OMPD_declare_simd: {
1475 // The syntax is:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001476 // { #pragma omp declare {simd|variant} }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001477 // <function-declaration-or-definition>
1478 //
Alexey Bataev2af33e32016-04-07 12:45:37 +00001479 CachedTokens Toks;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001480 Toks.push_back(Tok);
1481 ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001482 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
1483 Toks.push_back(Tok);
1484 ConsumeAnyToken();
1485 }
1486 Toks.push_back(Tok);
1487 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +00001488
1489 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +00001490 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001491 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +00001492 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001493 // Here we expect to see some function declaration.
1494 if (AS == AS_none) {
1495 assert(TagType == DeclSpec::TST_unspecified);
1496 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001497 ParsingDeclSpec PDS(*this);
1498 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1499 } else {
1500 Ptr =
1501 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1502 }
1503 }
1504 if (!Ptr) {
Alexey Bataevd158cf62019-09-13 20:18:17 +00001505 Diag(Loc, diag::err_omp_decl_in_declare_simd_variant)
1506 << (DKind == OMPD_declare_simd ? 0 : 1);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001507 return DeclGroupPtrTy();
1508 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001509 if (DKind == OMPD_declare_simd)
1510 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
1511 assert(DKind == OMPD_declare_variant &&
1512 "Expected declare variant directive only");
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001513 ParseOMPDeclareVariantClauses(Ptr, Toks, Loc);
1514 return Ptr;
Alexey Bataev587e1de2016-03-30 10:43:55 +00001515 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001516 case OMPD_declare_target: {
1517 SourceLocation DTLoc = ConsumeAnyToken();
1518 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Kelvin Lie0502752018-11-21 20:15:57 +00001519 return ParseOMPDeclareTargetClauses();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001520 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001521
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001522 // Skip the last annot_pragma_openmp_end.
1523 ConsumeAnyToken();
1524
1525 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
1526 return DeclGroupPtrTy();
1527
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001528 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +00001529 DKind = parseOpenMPDirectiveKind(*this);
Kelvin Libc38e632018-09-10 02:07:09 +00001530 while (DKind != OMPD_end_declare_target && Tok.isNot(tok::eof) &&
1531 Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +00001532 DeclGroupPtrTy Ptr;
1533 // Here we expect to see some function declaration.
1534 if (AS == AS_none) {
1535 assert(TagType == DeclSpec::TST_unspecified);
1536 MaybeParseCXX11Attributes(Attrs);
1537 ParsingDeclSpec PDS(*this);
1538 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1539 } else {
1540 Ptr =
1541 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1542 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001543 if (Ptr) {
1544 DeclGroupRef Ref = Ptr.get();
1545 Decls.append(Ref.begin(), Ref.end());
1546 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001547 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
1548 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +00001549 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001550 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001551 if (DKind != OMPD_end_declare_target)
1552 TPA.Revert();
1553 else
1554 TPA.Commit();
1555 }
1556 }
1557
Kelvin Lie0502752018-11-21 20:15:57 +00001558 ParseOMPEndDeclareTargetDirective(DKind, DTLoc);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001559 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +00001560 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001561 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001562 case OMPD_unknown:
1563 Diag(Tok, diag::err_omp_unknown_directive);
1564 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001565 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001566 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001567 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +00001568 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001569 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001570 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001571 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +00001572 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001573 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001574 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001575 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001576 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001577 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +00001578 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001579 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001580 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001581 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001582 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001583 case OMPD_parallel_sections:
cchen47d60942019-12-05 13:43:48 -05001584 case OMPD_parallel_master:
Alexey Bataev0162e452014-07-22 10:10:35 +00001585 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001586 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001587 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001588 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001589 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +00001590 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001591 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001592 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001593 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001594 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001595 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001596 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +00001597 case OMPD_master_taskloop:
Alexey Bataevb8552ab2019-10-18 16:47:35 +00001598 case OMPD_master_taskloop_simd:
Alexey Bataev5bbcead2019-10-14 17:17:41 +00001599 case OMPD_parallel_master_taskloop:
Alexey Bataev14a388f2019-10-25 10:27:13 -04001600 case OMPD_parallel_master_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001601 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001602 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001603 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001604 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +00001605 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001606 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001607 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001608 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001609 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +00001610 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +00001611 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001612 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +00001613 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +00001614 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +00001615 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +00001616 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +00001617 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +00001618 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001619 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +00001620 break;
1621 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001622 while (Tok.isNot(tok::annot_pragma_openmp_end))
1623 ConsumeAnyToken();
1624 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +00001625 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001626}
1627
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001628/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001629///
1630/// threadprivate-directive:
1631/// annot_pragma_openmp 'threadprivate' simple-variable-list
1632/// annot_pragma_openmp_end
1633///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001634/// allocate-directive:
1635/// annot_pragma_openmp 'allocate' simple-variable-list
1636/// annot_pragma_openmp_end
1637///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001638/// declare-reduction-directive:
1639/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
1640/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
1641/// ('omp_priv' '=' <expression>|<function_call>) ')']
1642/// annot_pragma_openmp_end
1643///
Michael Kruse251e1482019-02-01 20:25:04 +00001644/// declare-mapper-directive:
1645/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1646/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1647/// annot_pragma_openmp_end
1648///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001649/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001650/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001651/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
cchen47d60942019-12-05 13:43:48 -05001652/// 'parallel for' | 'parallel sections' | 'parallel master' | 'task' |
1653/// 'taskyield' | 'barrier' | 'taskwait' | 'flush' | 'ordered' |
1654/// 'atomic' | 'for simd' | 'parallel for simd' | 'target' | 'target
1655/// data' | 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
1656/// 'master taskloop' | 'master taskloop simd' | 'parallel master
1657/// taskloop' | 'parallel master taskloop simd' | 'distribute' | 'target
1658/// enter data' | 'target exit data' | 'target parallel' | 'target
1659/// parallel for' | 'target update' | 'distribute parallel for' |
1660/// 'distribute paralle for simd' | 'distribute simd' | 'target parallel
1661/// for simd' | 'target simd' | 'teams distribute' | 'teams distribute
1662/// simd' | 'teams distribute parallel for simd' | 'teams distribute
1663/// parallel for' | 'target teams' | 'target teams distribute' | 'target
1664/// teams distribute parallel for' | 'target teams distribute parallel
1665/// for simd' | 'target teams distribute simd' {clause}
Alexey Bataev14a388f2019-10-25 10:27:13 -04001666/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001667///
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001668StmtResult
1669Parser::ParseOpenMPDeclarativeOrExecutableDirective(ParsedStmtContext StmtCtx) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001670 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataev8035bb42019-12-13 16:05:30 -05001671 ParsingOpenMPDirectiveRAII DirScope(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001672 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001673 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001674 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +00001675 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +00001676 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
1677 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +00001678 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +00001679 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001680 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001681 // Name of critical directive.
1682 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001683 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +00001684 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +00001685 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001686
1687 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001688 case OMPD_threadprivate: {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001689 // FIXME: Should this be permitted in C++?
1690 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1691 ParsedStmtContext()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001692 Diag(Tok, diag::err_omp_immediate_directive)
1693 << getOpenMPDirectiveName(DKind) << 0;
1694 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001695 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001696 DeclDirectiveListParserHelper Helper(this, DKind);
1697 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1698 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001699 // The last seen token is annot_pragma_openmp_end - need to check for
1700 // extra tokens.
1701 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1702 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001703 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001704 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001705 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001706 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
1707 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001708 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1709 }
Alp Tokerd751fa72013-12-18 19:10:49 +00001710 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001711 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001712 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001713 case OMPD_allocate: {
1714 // FIXME: Should this be permitted in C++?
1715 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1716 ParsedStmtContext()) {
1717 Diag(Tok, diag::err_omp_immediate_directive)
1718 << getOpenMPDirectiveName(DKind) << 0;
1719 }
1720 ConsumeToken();
1721 DeclDirectiveListParserHelper Helper(this, DKind);
1722 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1723 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001724 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001725 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001726 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1727 OMPC_unknown + 1>
1728 FirstClauses(OMPC_unknown + 1);
1729 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1730 OpenMPClauseKind CKind =
1731 Tok.isAnnotation() ? OMPC_unknown
1732 : getOpenMPClauseKind(PP.getSpelling(Tok));
1733 Actions.StartOpenMPClause(CKind);
1734 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1735 !FirstClauses[CKind].getInt());
1736 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1737 StopBeforeMatch);
1738 FirstClauses[CKind].setInt(true);
1739 if (Clause != nullptr)
1740 Clauses.push_back(Clause);
1741 if (Tok.is(tok::annot_pragma_openmp_end)) {
1742 Actions.EndOpenMPClause();
1743 break;
1744 }
1745 // Skip ',' if any.
1746 if (Tok.is(tok::comma))
1747 ConsumeToken();
1748 Actions.EndOpenMPClause();
1749 }
1750 // The last seen token is annot_pragma_openmp_end - need to check for
1751 // extra tokens.
1752 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1753 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1754 << getOpenMPDirectiveName(DKind);
1755 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1756 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001757 }
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001758 DeclGroupPtrTy Res = Actions.ActOnOpenMPAllocateDirective(
1759 Loc, Helper.getIdentifiers(), Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001760 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1761 }
1762 SkipUntil(tok::annot_pragma_openmp_end);
1763 break;
1764 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001765 case OMPD_declare_reduction:
1766 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001767 if (DeclGroupPtrTy Res =
1768 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001769 // The last seen token is annot_pragma_openmp_end - need to check for
1770 // extra tokens.
1771 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1772 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1773 << getOpenMPDirectiveName(OMPD_declare_reduction);
1774 while (Tok.isNot(tok::annot_pragma_openmp_end))
1775 ConsumeAnyToken();
1776 }
1777 ConsumeAnyToken();
1778 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +00001779 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001780 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +00001781 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001782 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001783 case OMPD_declare_mapper: {
1784 ConsumeToken();
1785 if (DeclGroupPtrTy Res =
1786 ParseOpenMPDeclareMapperDirective(/*AS=*/AS_none)) {
1787 // Skip the last annot_pragma_openmp_end.
1788 ConsumeAnnotationToken();
1789 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1790 } else {
1791 SkipUntil(tok::annot_pragma_openmp_end);
1792 }
1793 break;
1794 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001795 case OMPD_flush:
1796 if (PP.LookAhead(0).is(tok::l_paren)) {
1797 FlushHasClause = true;
1798 // Push copy of the current token back to stream to properly parse
1799 // pseudo-clause OMPFlushClause.
Ilya Biryukov929af672019-05-17 09:32:05 +00001800 PP.EnterToken(Tok, /*IsReinject*/ true);
Alexey Bataev6125da92014-07-21 11:26:11 +00001801 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001802 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +00001803 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001804 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001805 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001806 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001807 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001808 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001809 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +00001810 case OMPD_target_update:
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001811 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1812 ParsedStmtContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00001813 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +00001814 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +00001815 }
1816 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001817 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001818 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001819 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001820 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001821 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001822 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001823 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001824 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001825 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001826 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001827 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001828 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001829 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001830 case OMPD_parallel_sections:
cchen47d60942019-12-05 13:43:48 -05001831 case OMPD_parallel_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001832 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001833 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001834 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001835 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001836 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001837 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001838 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001839 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001840 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001841 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001842 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +00001843 case OMPD_master_taskloop:
Alexey Bataevb8552ab2019-10-18 16:47:35 +00001844 case OMPD_master_taskloop_simd:
Alexey Bataev5bbcead2019-10-14 17:17:41 +00001845 case OMPD_parallel_master_taskloop:
Alexey Bataev14a388f2019-10-25 10:27:13 -04001846 case OMPD_parallel_master_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001847 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001848 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001849 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001850 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001851 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001852 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001853 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001854 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001855 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001856 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001857 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001858 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001859 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001860 case OMPD_target_teams_distribute_parallel_for_simd:
1861 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001862 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001863 // Parse directive name of the 'critical' directive if any.
1864 if (DKind == OMPD_critical) {
1865 BalancedDelimiterTracker T(*this, tok::l_paren,
1866 tok::annot_pragma_openmp_end);
1867 if (!T.consumeOpen()) {
1868 if (Tok.isAnyIdentifier()) {
1869 DirName =
1870 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1871 ConsumeAnyToken();
1872 } else {
1873 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1874 }
1875 T.consumeClose();
1876 }
Alexey Bataev80909872015-07-02 11:25:17 +00001877 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001878 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001879 if (Tok.isNot(tok::annot_pragma_openmp_end))
1880 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001881 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001882
Alexey Bataevf29276e2014-06-18 04:14:57 +00001883 if (isOpenMPLoopDirective(DKind))
1884 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1885 if (isOpenMPSimdDirective(DKind))
1886 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1887 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001888 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001889
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001890 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001891 OpenMPClauseKind CKind =
1892 Tok.isAnnotation()
1893 ? OMPC_unknown
1894 : FlushHasClause ? OMPC_flush
1895 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001896 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001897 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001898 OMPClause *Clause =
1899 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001900 FirstClauses[CKind].setInt(true);
1901 if (Clause) {
1902 FirstClauses[CKind].setPointer(Clause);
1903 Clauses.push_back(Clause);
1904 }
1905
1906 // Skip ',' if any.
1907 if (Tok.is(tok::comma))
1908 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001909 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001910 }
1911 // End location of the directive.
1912 EndLoc = Tok.getLocation();
1913 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001914 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001915
Alexey Bataeveb482352015-12-18 05:05:56 +00001916 // OpenMP [2.13.8, ordered Construct, Syntax]
1917 // If the depend clause is specified, the ordered construct is a stand-alone
1918 // directive.
1919 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001920 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1921 ParsedStmtContext()) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001922 Diag(Loc, diag::err_omp_immediate_directive)
1923 << getOpenMPDirectiveName(DKind) << 1
1924 << getOpenMPClauseName(OMPC_depend);
1925 }
1926 HasAssociatedStatement = false;
1927 }
1928
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001929 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001930 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001931 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001932 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001933 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1934 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1935 // should have at least one compound statement scope within it.
1936 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001937 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001938 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1939 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001940 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001941 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1942 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1943 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001944 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001945 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001946 Directive = Actions.ActOnOpenMPExecutableDirective(
1947 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1948 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001949
1950 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001951 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001952 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001953 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001954 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001955 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001956 case OMPD_declare_target:
1957 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00001958 case OMPD_requires:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001959 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001960 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001961 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001962 SkipUntil(tok::annot_pragma_openmp_end);
1963 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001964 case OMPD_unknown:
1965 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001966 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001967 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001968 }
1969 return Directive;
1970}
1971
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001972// Parses simple list:
1973// simple-variable-list:
1974// '(' id-expression {, id-expression} ')'
1975//
1976bool Parser::ParseOpenMPSimpleVarList(
1977 OpenMPDirectiveKind Kind,
1978 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1979 Callback,
1980 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001981 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001982 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001983 if (T.expectAndConsume(diag::err_expected_lparen_after,
Johannes Doerferteb3e81f2019-11-04 22:00:49 -06001984 getOpenMPDirectiveName(Kind).data()))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001985 return true;
1986 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001987 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001988
1989 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001990 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001991 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001992 UnqualifiedId Name;
1993 // Read var name.
1994 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001995 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001996
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001997 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001998 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001999 IsCorrect = false;
2000 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00002001 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00002002 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00002003 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002004 IsCorrect = false;
2005 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00002006 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002007 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
2008 Tok.isNot(tok::annot_pragma_openmp_end)) {
2009 IsCorrect = false;
2010 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00002011 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00002012 Diag(PrevTok.getLocation(), diag::err_expected)
2013 << tok::identifier
2014 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00002015 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002016 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00002017 }
2018 // Consume ','.
2019 if (Tok.is(tok::comma)) {
2020 ConsumeToken();
2021 }
Alexey Bataeva769e072013-03-22 06:34:35 +00002022 }
2023
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002024 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00002025 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002026 IsCorrect = false;
2027 }
2028
2029 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002030 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002031
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002032 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00002033}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002034
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002035/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002036///
2037/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00002038/// if-clause | final-clause | num_threads-clause | safelen-clause |
2039/// default-clause | private-clause | firstprivate-clause | shared-clause
2040/// | linear-clause | aligned-clause | collapse-clause |
2041/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002042/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00002043/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00002044/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00002045/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002046/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00002047/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00002048/// from-clause | is_device_ptr-clause | task_reduction-clause |
Alexey Bataeve04483e2019-03-27 14:14:31 +00002049/// in_reduction-clause | allocator-clause | allocate-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002050///
2051OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
2052 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00002053 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002054 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002055 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002056 // Check if clause is allowed for the given directive.
Alexey Bataevd08c0562019-11-19 12:07:54 -05002057 if (CKind != OMPC_unknown &&
2058 !isAllowedClauseForDirective(DKind, CKind, getLangOpts().OpenMP)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00002059 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
2060 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002061 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002062 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002063 }
2064
2065 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00002066 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002067 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002068 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00002069 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002070 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00002071 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00002072 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00002073 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00002074 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00002075 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002076 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00002077 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00002078 case OMPC_hint:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002079 case OMPC_allocator:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002080 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00002081 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00002082 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002083 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00002084 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00002085 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00002086 // OpenMP [2.9.1, target data construct, Restrictions]
2087 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00002088 // OpenMP [2.11.1, task Construct, Restrictions]
2089 // At most one if clause can appear on the directive.
2090 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00002091 // OpenMP [teams Construct, Restrictions]
2092 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00002093 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00002094 // OpenMP [2.9.1, task Construct, Restrictions]
2095 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002096 // OpenMP [2.9.2, taskloop Construct, Restrictions]
2097 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00002098 // OpenMP [2.9.2, taskloop Construct, Restrictions]
2099 // At most one num_tasks clause can appear on the directive.
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002100 // OpenMP [2.11.3, allocate Directive, Restrictions]
2101 // At most one allocator clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002102 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002103 Diag(Tok, diag::err_omp_more_one_clause)
2104 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002105 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002106 }
2107
Alexey Bataev10e775f2015-07-30 11:36:16 +00002108 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002109 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00002110 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002111 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002112 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002113 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002114 case OMPC_proc_bind:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00002115 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002116 // OpenMP [2.14.3.1, Restrictions]
2117 // Only a single default clause may be specified on a parallel, task or
2118 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002119 // OpenMP [2.5, parallel Construct, Restrictions]
2120 // At most one proc_bind clause can appear on the directive.
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00002121 // OpenMP [5.0, Requires directive, Restrictions]
2122 // At most one atomic_default_mem_order clause can appear
2123 // on the directive
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002124 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002125 Diag(Tok, diag::err_omp_more_one_clause)
2126 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002127 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002128 }
2129
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002130 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002131 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002132 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00002133 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002134 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002135 // OpenMP [2.7.1, Restrictions, p. 3]
2136 // Only one schedule clause can appear on a loop directive.
cchene06f3e02019-11-15 13:02:06 -05002137 // OpenMP 4.5 [2.10.4, Restrictions, p. 106]
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002138 // At most one defaultmap clause can appear on the directive.
cchene06f3e02019-11-15 13:02:06 -05002139 if ((getLangOpts().OpenMP < 50 || CKind != OMPC_defaultmap) &&
2140 !FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002141 Diag(Tok, diag::err_omp_more_one_clause)
2142 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002143 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002144 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00002145 LLVM_FALLTHROUGH;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002146 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002147 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002148 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002149 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002150 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002151 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002152 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002153 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00002154 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00002155 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002156 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00002157 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002158 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00002159 case OMPC_nogroup:
Kelvin Li1408f912018-09-26 04:28:39 +00002160 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00002161 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00002162 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00002163 case OMPC_dynamic_allocators:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002164 // OpenMP [2.7.1, Restrictions, p. 9]
2165 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00002166 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
2167 // Only one nowait clause can appear on a for directive.
Kelvin Li1408f912018-09-26 04:28:39 +00002168 // OpenMP [5.0, Requires directive, Restrictions]
2169 // Each of the requires clauses can appear at most once on the directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002170 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002171 Diag(Tok, diag::err_omp_more_one_clause)
2172 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002173 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002174 }
2175
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002176 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002177 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002178 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002179 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002180 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002181 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002182 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00002183 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00002184 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002185 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002186 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002187 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002188 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002189 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002190 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00002191 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00002192 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00002193 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00002194 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00002195 case OMPC_is_device_ptr:
Alexey Bataeve04483e2019-03-27 14:14:31 +00002196 case OMPC_allocate:
Alexey Bataevb6e70842019-12-16 15:54:17 -05002197 case OMPC_nontemporal:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002198 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002199 break;
Alexey Bataev729e2422019-08-23 16:11:14 +00002200 case OMPC_device_type:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002201 case OMPC_unknown:
2202 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00002203 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00002204 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002205 break;
2206 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002207 case OMPC_uniform:
Alexey Bataevdba792c2019-09-23 18:13:31 +00002208 case OMPC_match:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002209 if (!WrongDirective)
2210 Diag(Tok, diag::err_omp_unexpected_clause)
2211 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00002212 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002213 break;
2214 }
Craig Topper161e4db2014-05-21 06:02:52 +00002215 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002216}
2217
Alexey Bataev2af33e32016-04-07 12:45:37 +00002218/// Parses simple expression in parens for single-expression clauses of OpenMP
2219/// constructs.
2220/// \param RLoc Returned location of right paren.
2221ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
Alexey Bataevd158cf62019-09-13 20:18:17 +00002222 SourceLocation &RLoc,
2223 bool IsAddressOfOperand) {
Alexey Bataev2af33e32016-04-07 12:45:37 +00002224 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2225 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
2226 return ExprError();
2227
2228 SourceLocation ELoc = Tok.getLocation();
2229 ExprResult LHS(ParseCastExpression(
Alexey Bataevd158cf62019-09-13 20:18:17 +00002230 /*isUnaryExpression=*/false, IsAddressOfOperand, NotTypeCast));
Alexey Bataev2af33e32016-04-07 12:45:37 +00002231 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002232 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev2af33e32016-04-07 12:45:37 +00002233
2234 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002235 RLoc = Tok.getLocation();
2236 if (!T.consumeClose())
2237 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00002238
Alexey Bataev2af33e32016-04-07 12:45:37 +00002239 return Val;
2240}
2241
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002242/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00002243/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00002244/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002245///
Alexey Bataev3778b602014-07-17 07:32:53 +00002246/// final-clause:
2247/// 'final' '(' expression ')'
2248///
Alexey Bataev62c87d22014-03-21 04:51:18 +00002249/// num_threads-clause:
2250/// 'num_threads' '(' expression ')'
2251///
2252/// safelen-clause:
2253/// 'safelen' '(' expression ')'
2254///
Alexey Bataev66b15b52015-08-21 11:14:16 +00002255/// simdlen-clause:
2256/// 'simdlen' '(' expression ')'
2257///
Alexander Musman8bd31e62014-05-27 15:12:19 +00002258/// collapse-clause:
2259/// 'collapse' '(' expression ')'
2260///
Alexey Bataeva0569352015-12-01 10:17:31 +00002261/// priority-clause:
2262/// 'priority' '(' expression ')'
2263///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002264/// grainsize-clause:
2265/// 'grainsize' '(' expression ')'
2266///
Alexey Bataev382967a2015-12-08 12:06:20 +00002267/// num_tasks-clause:
2268/// 'num_tasks' '(' expression ')'
2269///
Alexey Bataev28c75412015-12-15 08:19:24 +00002270/// hint-clause:
2271/// 'hint' '(' expression ')'
2272///
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002273/// allocator-clause:
2274/// 'allocator' '(' expression ')'
2275///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002276OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
2277 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002278 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00002279 SourceLocation LLoc = Tok.getLocation();
2280 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002281
Alexey Bataev2af33e32016-04-07 12:45:37 +00002282 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002283
2284 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00002285 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002286
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002287 if (ParseOnly)
2288 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00002289 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002290}
2291
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002292/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002293///
2294/// default-clause:
2295/// 'default' '(' 'none' | 'shared' ')
2296///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002297/// proc_bind-clause:
2298/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
2299///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002300OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
2301 bool ParseOnly) {
Alexey Bataev729e2422019-08-23 16:11:14 +00002302 llvm::Optional<SimpleClauseData> Val = parseOpenMPSimpleClause(*this, Kind);
2303 if (!Val || ParseOnly)
Craig Topper161e4db2014-05-21 06:02:52 +00002304 return nullptr;
Alexey Bataev729e2422019-08-23 16:11:14 +00002305 return Actions.ActOnOpenMPSimpleClause(
2306 Kind, Val.getValue().Type, Val.getValue().TypeLoc, Val.getValue().LOpen,
2307 Val.getValue().Loc, Val.getValue().RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002308}
2309
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002310/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002311///
2312/// ordered-clause:
2313/// 'ordered'
2314///
Alexey Bataev236070f2014-06-20 11:19:47 +00002315/// nowait-clause:
2316/// 'nowait'
2317///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002318/// untied-clause:
2319/// 'untied'
2320///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002321/// mergeable-clause:
2322/// 'mergeable'
2323///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002324/// read-clause:
2325/// 'read'
2326///
Alexey Bataev346265e2015-09-25 10:37:12 +00002327/// threads-clause:
2328/// 'threads'
2329///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002330/// simd-clause:
2331/// 'simd'
2332///
Alexey Bataevb825de12015-12-07 10:51:44 +00002333/// nogroup-clause:
2334/// 'nogroup'
2335///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002336OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002337 SourceLocation Loc = Tok.getLocation();
2338 ConsumeAnyToken();
2339
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002340 if (ParseOnly)
2341 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002342 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
2343}
2344
2345
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002346/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00002347/// argument like 'schedule' or 'dist_schedule'.
2348///
2349/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00002350/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
2351/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00002352///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002353/// if-clause:
2354/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
2355///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002356/// defaultmap:
2357/// 'defaultmap' '(' modifier ':' kind ')'
2358///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002359OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
2360 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00002361 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002362 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002363 // Parse '('.
2364 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2365 if (T.expectAndConsume(diag::err_expected_lparen_after,
2366 getOpenMPClauseName(Kind)))
2367 return nullptr;
2368
2369 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002370 SmallVector<unsigned, 4> Arg;
2371 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002372 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00002373 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
2374 Arg.resize(NumberOfElements);
2375 KLoc.resize(NumberOfElements);
2376 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
2377 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
2378 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00002379 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002380 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00002381 if (KindModifier > OMPC_SCHEDULE_unknown) {
2382 // Parse 'modifier'
2383 Arg[Modifier1] = KindModifier;
2384 KLoc[Modifier1] = Tok.getLocation();
2385 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2386 Tok.isNot(tok::annot_pragma_openmp_end))
2387 ConsumeAnyToken();
2388 if (Tok.is(tok::comma)) {
2389 // Parse ',' 'modifier'
2390 ConsumeAnyToken();
2391 KindModifier = getOpenMPSimpleClauseType(
2392 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2393 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
2394 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00002395 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002396 KLoc[Modifier2] = Tok.getLocation();
2397 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2398 Tok.isNot(tok::annot_pragma_openmp_end))
2399 ConsumeAnyToken();
2400 }
2401 // Parse ':'
2402 if (Tok.is(tok::colon))
2403 ConsumeAnyToken();
2404 else
2405 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
2406 KindModifier = getOpenMPSimpleClauseType(
2407 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2408 }
2409 Arg[ScheduleKind] = KindModifier;
2410 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002411 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2412 Tok.isNot(tok::annot_pragma_openmp_end))
2413 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00002414 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
2415 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
2416 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002417 Tok.is(tok::comma))
2418 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00002419 } else if (Kind == OMPC_dist_schedule) {
2420 Arg.push_back(getOpenMPSimpleClauseType(
2421 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2422 KLoc.push_back(Tok.getLocation());
2423 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2424 Tok.isNot(tok::annot_pragma_openmp_end))
2425 ConsumeAnyToken();
2426 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
2427 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002428 } else if (Kind == OMPC_defaultmap) {
2429 // Get a defaultmap modifier
cchene06f3e02019-11-15 13:02:06 -05002430 unsigned Modifier = getOpenMPSimpleClauseType(
2431 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2432 // Set defaultmap modifier to unknown if it is either scalar, aggregate, or
2433 // pointer
2434 if (Modifier < OMPC_DEFAULTMAP_MODIFIER_unknown)
2435 Modifier = OMPC_DEFAULTMAP_MODIFIER_unknown;
2436 Arg.push_back(Modifier);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002437 KLoc.push_back(Tok.getLocation());
2438 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2439 Tok.isNot(tok::annot_pragma_openmp_end))
2440 ConsumeAnyToken();
2441 // Parse ':'
2442 if (Tok.is(tok::colon))
2443 ConsumeAnyToken();
2444 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
2445 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
2446 // Get a defaultmap kind
2447 Arg.push_back(getOpenMPSimpleClauseType(
2448 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2449 KLoc.push_back(Tok.getLocation());
2450 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2451 Tok.isNot(tok::annot_pragma_openmp_end))
2452 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002453 } else {
2454 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00002455 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002456 TentativeParsingAction TPA(*this);
Johannes Doerferteb3e81f2019-11-04 22:00:49 -06002457 auto DK = parseOpenMPDirectiveKind(*this);
2458 Arg.push_back(DK);
2459 if (DK != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002460 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002461 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
2462 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002463 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002464 } else {
2465 TPA.Revert();
Johannes Doerferteb3e81f2019-11-04 22:00:49 -06002466 Arg.back() = unsigned(OMPD_unknown);
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002467 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002468 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002469 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00002470 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002471 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00002472
Carlo Bertollib4adf552016-01-15 18:50:31 +00002473 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
2474 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
2475 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002476 if (NeedAnExpression) {
2477 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002478 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
2479 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002480 Val =
2481 Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002482 }
2483
2484 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002485 SourceLocation RLoc = Tok.getLocation();
2486 if (!T.consumeClose())
2487 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002488
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002489 if (NeedAnExpression && Val.isInvalid())
2490 return nullptr;
2491
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002492 if (ParseOnly)
2493 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002494 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002495 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002496}
2497
Alexey Bataevc5e02582014-06-16 07:08:35 +00002498static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
2499 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002500 if (ReductionIdScopeSpec.isEmpty()) {
2501 auto OOK = OO_None;
2502 switch (P.getCurToken().getKind()) {
2503 case tok::plus:
2504 OOK = OO_Plus;
2505 break;
2506 case tok::minus:
2507 OOK = OO_Minus;
2508 break;
2509 case tok::star:
2510 OOK = OO_Star;
2511 break;
2512 case tok::amp:
2513 OOK = OO_Amp;
2514 break;
2515 case tok::pipe:
2516 OOK = OO_Pipe;
2517 break;
2518 case tok::caret:
2519 OOK = OO_Caret;
2520 break;
2521 case tok::ampamp:
2522 OOK = OO_AmpAmp;
2523 break;
2524 case tok::pipepipe:
2525 OOK = OO_PipePipe;
2526 break;
2527 default:
2528 break;
2529 }
2530 if (OOK != OO_None) {
2531 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00002532 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00002533 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
2534 return false;
2535 }
2536 }
2537 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
2538 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00002539 /*AllowConstructorName*/ false,
2540 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00002541 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002542}
2543
Kelvin Lief579432018-12-18 22:18:41 +00002544/// Checks if the token is a valid map-type-modifier.
2545static OpenMPMapModifierKind isMapModifier(Parser &P) {
2546 Token Tok = P.getCurToken();
2547 if (!Tok.is(tok::identifier))
2548 return OMPC_MAP_MODIFIER_unknown;
2549
2550 Preprocessor &PP = P.getPreprocessor();
2551 OpenMPMapModifierKind TypeModifier = static_cast<OpenMPMapModifierKind>(
2552 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2553 return TypeModifier;
2554}
2555
Michael Kruse01f670d2019-02-22 22:29:42 +00002556/// Parse the mapper modifier in map, to, and from clauses.
2557bool Parser::parseMapperModifier(OpenMPVarListDataTy &Data) {
2558 // Parse '('.
2559 BalancedDelimiterTracker T(*this, tok::l_paren, tok::colon);
2560 if (T.expectAndConsume(diag::err_expected_lparen_after, "mapper")) {
2561 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2562 StopBeforeMatch);
2563 return true;
2564 }
2565 // Parse mapper-identifier
2566 if (getLangOpts().CPlusPlus)
2567 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
2568 /*ObjectType=*/nullptr,
2569 /*EnteringContext=*/false);
2570 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
2571 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
2572 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2573 StopBeforeMatch);
2574 return true;
2575 }
2576 auto &DeclNames = Actions.getASTContext().DeclarationNames;
2577 Data.ReductionOrMapperId = DeclarationNameInfo(
2578 DeclNames.getIdentifier(Tok.getIdentifierInfo()), Tok.getLocation());
2579 ConsumeToken();
2580 // Parse ')'.
2581 return T.consumeClose();
2582}
2583
Kelvin Lief579432018-12-18 22:18:41 +00002584/// Parse map-type-modifiers in map clause.
2585/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002586/// where, map-type-modifier ::= always | close | mapper(mapper-identifier)
2587bool Parser::parseMapTypeModifiers(OpenMPVarListDataTy &Data) {
2588 while (getCurToken().isNot(tok::colon)) {
2589 OpenMPMapModifierKind TypeModifier = isMapModifier(*this);
Kelvin Lief579432018-12-18 22:18:41 +00002590 if (TypeModifier == OMPC_MAP_MODIFIER_always ||
2591 TypeModifier == OMPC_MAP_MODIFIER_close) {
2592 Data.MapTypeModifiers.push_back(TypeModifier);
2593 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
Michael Kruse4304e9d2019-02-19 16:38:20 +00002594 ConsumeToken();
2595 } else if (TypeModifier == OMPC_MAP_MODIFIER_mapper) {
2596 Data.MapTypeModifiers.push_back(TypeModifier);
2597 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
2598 ConsumeToken();
Michael Kruse01f670d2019-02-22 22:29:42 +00002599 if (parseMapperModifier(Data))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002600 return true;
Kelvin Lief579432018-12-18 22:18:41 +00002601 } else {
2602 // For the case of unknown map-type-modifier or a map-type.
2603 // Map-type is followed by a colon; the function returns when it
2604 // encounters a token followed by a colon.
2605 if (Tok.is(tok::comma)) {
Michael Kruse4304e9d2019-02-19 16:38:20 +00002606 Diag(Tok, diag::err_omp_map_type_modifier_missing);
2607 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002608 continue;
2609 }
2610 // Potential map-type token as it is followed by a colon.
2611 if (PP.LookAhead(0).is(tok::colon))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002612 return false;
2613 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
2614 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002615 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002616 if (getCurToken().is(tok::comma))
2617 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002618 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002619 return false;
Kelvin Lief579432018-12-18 22:18:41 +00002620}
2621
2622/// Checks if the token is a valid map-type.
2623static OpenMPMapClauseKind isMapType(Parser &P) {
2624 Token Tok = P.getCurToken();
2625 // The map-type token can be either an identifier or the C++ delete keyword.
2626 if (!Tok.isOneOf(tok::identifier, tok::kw_delete))
2627 return OMPC_MAP_unknown;
2628 Preprocessor &PP = P.getPreprocessor();
2629 OpenMPMapClauseKind MapType = static_cast<OpenMPMapClauseKind>(
2630 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2631 return MapType;
2632}
2633
2634/// Parse map-type in map clause.
2635/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002636/// where, map-type ::= to | from | tofrom | alloc | release | delete
Kelvin Lief579432018-12-18 22:18:41 +00002637static void parseMapType(Parser &P, Parser::OpenMPVarListDataTy &Data) {
2638 Token Tok = P.getCurToken();
2639 if (Tok.is(tok::colon)) {
2640 P.Diag(Tok, diag::err_omp_map_type_missing);
2641 return;
2642 }
2643 Data.MapType = isMapType(P);
2644 if (Data.MapType == OMPC_MAP_unknown)
2645 P.Diag(Tok, diag::err_omp_unknown_map_type);
2646 P.ConsumeToken();
2647}
2648
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002649/// Parses clauses with list.
2650bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
2651 OpenMPClauseKind Kind,
2652 SmallVectorImpl<Expr *> &Vars,
2653 OpenMPVarListDataTy &Data) {
2654 UnqualifiedId UnqualifiedReductionId;
2655 bool InvalidReductionId = false;
Michael Kruse01f670d2019-02-22 22:29:42 +00002656 bool IsInvalidMapperModifier = false;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002657
2658 // Parse '('.
2659 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2660 if (T.expectAndConsume(diag::err_expected_lparen_after,
2661 getOpenMPClauseName(Kind)))
2662 return true;
2663
2664 bool NeedRParenForLinear = false;
2665 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
2666 tok::annot_pragma_openmp_end);
2667 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00002668 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
2669 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002670 ColonProtectionRAIIObject ColonRAII(*this);
2671 if (getLangOpts().CPlusPlus)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002672 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002673 /*ObjectType=*/nullptr,
2674 /*EnteringContext=*/false);
Michael Kruse4304e9d2019-02-19 16:38:20 +00002675 InvalidReductionId = ParseReductionId(
2676 *this, Data.ReductionOrMapperIdScopeSpec, UnqualifiedReductionId);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002677 if (InvalidReductionId) {
2678 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2679 StopBeforeMatch);
2680 }
2681 if (Tok.is(tok::colon))
2682 Data.ColonLoc = ConsumeToken();
2683 else
2684 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
2685 if (!InvalidReductionId)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002686 Data.ReductionOrMapperId =
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002687 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
2688 } else if (Kind == OMPC_depend) {
2689 // Handle dependency type for depend clause.
2690 ColonProtectionRAIIObject ColonRAII(*this);
2691 Data.DepKind =
2692 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
2693 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
2694 Data.DepLinMapLoc = Tok.getLocation();
2695
2696 if (Data.DepKind == OMPC_DEPEND_unknown) {
2697 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2698 StopBeforeMatch);
2699 } else {
2700 ConsumeToken();
2701 // Special processing for depend(source) clause.
2702 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
2703 // Parse ')'.
2704 T.consumeClose();
2705 return false;
2706 }
2707 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002708 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002709 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00002710 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002711 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
2712 : diag::warn_pragma_expected_colon)
2713 << "dependency type";
2714 }
2715 } else if (Kind == OMPC_linear) {
2716 // Try to parse modifier if any.
2717 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
2718 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
2719 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2720 Data.DepLinMapLoc = ConsumeToken();
2721 LinearT.consumeOpen();
2722 NeedRParenForLinear = true;
2723 }
2724 } else if (Kind == OMPC_map) {
2725 // Handle map type for map clause.
2726 ColonProtectionRAIIObject ColonRAII(*this);
2727
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002728 // The first identifier may be a list item, a map-type or a
Kelvin Lief579432018-12-18 22:18:41 +00002729 // map-type-modifier. The map-type can also be delete which has the same
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002730 // spelling of the C++ delete keyword.
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002731 Data.DepLinMapLoc = Tok.getLocation();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002732
Kelvin Lief579432018-12-18 22:18:41 +00002733 // Check for presence of a colon in the map clause.
2734 TentativeParsingAction TPA(*this);
2735 bool ColonPresent = false;
2736 if (SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2737 StopBeforeMatch)) {
2738 if (Tok.is(tok::colon))
2739 ColonPresent = true;
2740 }
2741 TPA.Revert();
2742 // Only parse map-type-modifier[s] and map-type if a colon is present in
2743 // the map clause.
2744 if (ColonPresent) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002745 IsInvalidMapperModifier = parseMapTypeModifiers(Data);
2746 if (!IsInvalidMapperModifier)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002747 parseMapType(*this, Data);
Michael Kruse01f670d2019-02-22 22:29:42 +00002748 else
2749 SkipUntil(tok::colon, tok::annot_pragma_openmp_end, StopBeforeMatch);
Kelvin Lief579432018-12-18 22:18:41 +00002750 }
2751 if (Data.MapType == OMPC_MAP_unknown) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002752 Data.MapType = OMPC_MAP_tofrom;
2753 Data.IsMapTypeImplicit = true;
2754 }
2755
2756 if (Tok.is(tok::colon))
2757 Data.ColonLoc = ConsumeToken();
Michael Kruse0336c752019-02-25 20:34:15 +00002758 } else if (Kind == OMPC_to || Kind == OMPC_from) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002759 if (Tok.is(tok::identifier)) {
2760 bool IsMapperModifier = false;
Michael Kruse0336c752019-02-25 20:34:15 +00002761 if (Kind == OMPC_to) {
2762 auto Modifier = static_cast<OpenMPToModifierKind>(
2763 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2764 if (Modifier == OMPC_TO_MODIFIER_mapper)
2765 IsMapperModifier = true;
2766 } else {
2767 auto Modifier = static_cast<OpenMPFromModifierKind>(
2768 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2769 if (Modifier == OMPC_FROM_MODIFIER_mapper)
2770 IsMapperModifier = true;
2771 }
Michael Kruse01f670d2019-02-22 22:29:42 +00002772 if (IsMapperModifier) {
2773 // Parse the mapper modifier.
2774 ConsumeToken();
2775 IsInvalidMapperModifier = parseMapperModifier(Data);
2776 if (Tok.isNot(tok::colon)) {
2777 if (!IsInvalidMapperModifier)
2778 Diag(Tok, diag::warn_pragma_expected_colon) << ")";
2779 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2780 StopBeforeMatch);
2781 }
2782 // Consume ':'.
2783 if (Tok.is(tok::colon))
2784 ConsumeToken();
2785 }
2786 }
Alexey Bataeve04483e2019-03-27 14:14:31 +00002787 } else if (Kind == OMPC_allocate) {
2788 // Handle optional allocator expression followed by colon delimiter.
2789 ColonProtectionRAIIObject ColonRAII(*this);
2790 TentativeParsingAction TPA(*this);
2791 ExprResult Tail =
2792 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
2793 Tail = Actions.ActOnFinishFullExpr(Tail.get(), T.getOpenLocation(),
2794 /*DiscardedValue=*/false);
2795 if (Tail.isUsable()) {
2796 if (Tok.is(tok::colon)) {
2797 Data.TailExpr = Tail.get();
2798 Data.ColonLoc = ConsumeToken();
2799 TPA.Commit();
2800 } else {
2801 // colon not found, no allocator specified, parse only list of
2802 // variables.
2803 TPA.Revert();
2804 }
2805 } else {
2806 // Parsing was unsuccessfull, revert and skip to the end of clause or
2807 // directive.
2808 TPA.Revert();
2809 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2810 StopBeforeMatch);
2811 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002812 }
2813
Alexey Bataevfa312f32017-07-21 18:48:21 +00002814 bool IsComma =
2815 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
2816 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
2817 (Kind == OMPC_reduction && !InvalidReductionId) ||
Kelvin Lida6bc702018-11-21 19:38:53 +00002818 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown) ||
Alexey Bataevfa312f32017-07-21 18:48:21 +00002819 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002820 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
2821 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
2822 Tok.isNot(tok::annot_pragma_openmp_end))) {
2823 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
2824 // Parse variable
2825 ExprResult VarExpr =
2826 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00002827 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002828 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00002829 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002830 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2831 StopBeforeMatch);
2832 }
2833 // Skip ',' if any
2834 IsComma = Tok.is(tok::comma);
2835 if (IsComma)
2836 ConsumeToken();
2837 else if (Tok.isNot(tok::r_paren) &&
2838 Tok.isNot(tok::annot_pragma_openmp_end) &&
2839 (!MayHaveTail || Tok.isNot(tok::colon)))
2840 Diag(Tok, diag::err_omp_expected_punc)
2841 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
2842 : getOpenMPClauseName(Kind))
2843 << (Kind == OMPC_flush);
2844 }
2845
2846 // Parse ')' for linear clause with modifier.
2847 if (NeedRParenForLinear)
2848 LinearT.consumeClose();
2849
2850 // Parse ':' linear-step (or ':' alignment).
2851 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
2852 if (MustHaveTail) {
2853 Data.ColonLoc = Tok.getLocation();
2854 SourceLocation ELoc = ConsumeToken();
2855 ExprResult Tail = ParseAssignmentExpression();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002856 Tail =
2857 Actions.ActOnFinishFullExpr(Tail.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002858 if (Tail.isUsable())
2859 Data.TailExpr = Tail.get();
2860 else
2861 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2862 StopBeforeMatch);
2863 }
2864
2865 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002866 Data.RLoc = Tok.getLocation();
2867 if (!T.consumeClose())
2868 Data.RLoc = T.getCloseLocation();
Alexey Bataev61908f652018-04-23 19:53:05 +00002869 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
2870 Vars.empty()) ||
2871 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
Michael Kruse4304e9d2019-02-19 16:38:20 +00002872 (MustHaveTail && !Data.TailExpr) || InvalidReductionId ||
Michael Kruse01f670d2019-02-22 22:29:42 +00002873 IsInvalidMapperModifier;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002874}
2875
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002876/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00002877/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
2878/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002879///
2880/// private-clause:
2881/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002882/// firstprivate-clause:
2883/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00002884/// lastprivate-clause:
2885/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00002886/// shared-clause:
2887/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00002888/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00002889/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002890/// aligned-clause:
2891/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00002892/// reduction-clause:
2893/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00002894/// task_reduction-clause:
2895/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00002896/// in_reduction-clause:
2897/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00002898/// copyprivate-clause:
2899/// 'copyprivate' '(' list ')'
2900/// flush-clause:
2901/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002902/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00002903/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00002904/// map-clause:
Kelvin Lief579432018-12-18 22:18:41 +00002905/// 'map' '(' [ [ always [,] ] [ close [,] ]
Michael Kruse01f670d2019-02-22 22:29:42 +00002906/// [ mapper '(' mapper-identifier ')' [,] ]
Kelvin Li0bff7af2015-11-23 05:32:03 +00002907/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00002908/// to-clause:
Michael Kruse01f670d2019-02-22 22:29:42 +00002909/// 'to' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00002910/// from-clause:
Michael Kruse0336c752019-02-25 20:34:15 +00002911/// 'from' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00002912/// use_device_ptr-clause:
2913/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00002914/// is_device_ptr-clause:
2915/// 'is_device_ptr' '(' list ')'
Alexey Bataeve04483e2019-03-27 14:14:31 +00002916/// allocate-clause:
2917/// 'allocate' '(' [ allocator ':' ] list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002918///
Alexey Bataev182227b2015-08-20 10:54:39 +00002919/// For 'linear' clause linear-list may have the following forms:
2920/// list
2921/// modifier(list)
2922/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00002923OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002924 OpenMPClauseKind Kind,
2925 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002926 SourceLocation Loc = Tok.getLocation();
2927 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002928 SmallVector<Expr *, 4> Vars;
2929 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002930
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002931 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00002932 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002933
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002934 if (ParseOnly)
2935 return nullptr;
Michael Kruse4304e9d2019-02-19 16:38:20 +00002936 OMPVarListLocTy Locs(Loc, LOpen, Data.RLoc);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002937 return Actions.ActOnOpenMPVarListClause(
Michael Kruse4304e9d2019-02-19 16:38:20 +00002938 Kind, Vars, Data.TailExpr, Locs, Data.ColonLoc,
2939 Data.ReductionOrMapperIdScopeSpec, Data.ReductionOrMapperId, Data.DepKind,
2940 Data.LinKind, Data.MapTypeModifiers, Data.MapTypeModifiersLoc,
2941 Data.MapType, Data.IsMapTypeImplicit, Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002942}
2943