blob: 8430e72d3c88f051ca224d7be7bd864ac47d92ac [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexey Bataeva769e072013-03-22 06:34:35 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009/// This file implements parsing of all OpenMP directives and clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000010///
11//===----------------------------------------------------------------------===//
12
Alexey Bataev9959db52014-05-06 10:08:46 +000013#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000014#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000015#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000016#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000017#include "clang/Parse/RAIIObjectsForParser.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000018#include "clang/Sema/Scope.h"
19#include "llvm/ADT/PointerIntPair.h"
Alexey Bataev4513e93f2019-10-10 15:15:26 +000020#include "llvm/ADT/UniqueVector.h"
Michael Wong65f367f2015-07-21 13:44:28 +000021
Alexey Bataeva769e072013-03-22 06:34:35 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// OpenMP declarative directives.
26//===----------------------------------------------------------------------===//
27
Dmitry Polukhin82478332016-02-13 06:53:38 +000028namespace {
29enum OpenMPDirectiveKindEx {
30 OMPD_cancellation = OMPD_unknown + 1,
31 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000032 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000033 OMPD_end,
34 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000035 OMPD_enter,
36 OMPD_exit,
37 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000038 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000039 OMPD_target_enter,
Samuel Antao686c70c2016-05-26 17:30:50 +000040 OMPD_target_exit,
41 OMPD_update,
Kelvin Li579e41c2016-11-30 23:51:03 +000042 OMPD_distribute_parallel,
Kelvin Li80e8f562016-12-29 22:16:30 +000043 OMPD_teams_distribute_parallel,
Michael Kruse251e1482019-02-01 20:25:04 +000044 OMPD_target_teams_distribute_parallel,
45 OMPD_mapper,
Alexey Bataevd158cf62019-09-13 20:18:17 +000046 OMPD_variant,
Alexey Bataev5bbcead2019-10-14 17:17:41 +000047 OMPD_parallel_master,
Dmitry Polukhin82478332016-02-13 06:53:38 +000048};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000049
Alexey Bataev25ed0c02019-03-07 17:54:44 +000050class DeclDirectiveListParserHelper final {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000051 SmallVector<Expr *, 4> Identifiers;
52 Parser *P;
Alexey Bataev25ed0c02019-03-07 17:54:44 +000053 OpenMPDirectiveKind Kind;
Dmitry Polukhind69b5052016-05-09 14:59:13 +000054
55public:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000056 DeclDirectiveListParserHelper(Parser *P, OpenMPDirectiveKind Kind)
57 : P(P), Kind(Kind) {}
Dmitry Polukhind69b5052016-05-09 14:59:13 +000058 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
Alexey Bataev25ed0c02019-03-07 17:54:44 +000059 ExprResult Res = P->getActions().ActOnOpenMPIdExpression(
60 P->getCurScope(), SS, NameInfo, Kind);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000061 if (Res.isUsable())
62 Identifiers.push_back(Res.get());
63 }
64 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
65};
Dmitry Polukhin82478332016-02-13 06:53:38 +000066} // namespace
67
68// Map token string to extended OMP token kind that are
69// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
70static unsigned getOpenMPDirectiveKindEx(StringRef S) {
71 auto DKind = getOpenMPDirectiveKind(S);
72 if (DKind != OMPD_unknown)
73 return DKind;
74
75 return llvm::StringSwitch<unsigned>(S)
76 .Case("cancellation", OMPD_cancellation)
77 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000078 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000079 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000080 .Case("enter", OMPD_enter)
81 .Case("exit", OMPD_exit)
82 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000083 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000084 .Case("update", OMPD_update)
Michael Kruse251e1482019-02-01 20:25:04 +000085 .Case("mapper", OMPD_mapper)
Alexey Bataevd158cf62019-09-13 20:18:17 +000086 .Case("variant", OMPD_variant)
Dmitry Polukhin82478332016-02-13 06:53:38 +000087 .Default(OMPD_unknown);
88}
89
Alexey Bataev61908f652018-04-23 19:53:05 +000090static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000091 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
92 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
93 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000094 static const unsigned F[][3] = {
Alexey Bataev61908f652018-04-23 19:53:05 +000095 {OMPD_cancellation, OMPD_point, OMPD_cancellation_point},
96 {OMPD_declare, OMPD_reduction, OMPD_declare_reduction},
Michael Kruse251e1482019-02-01 20:25:04 +000097 {OMPD_declare, OMPD_mapper, OMPD_declare_mapper},
Alexey Bataev61908f652018-04-23 19:53:05 +000098 {OMPD_declare, OMPD_simd, OMPD_declare_simd},
99 {OMPD_declare, OMPD_target, OMPD_declare_target},
Alexey Bataevd158cf62019-09-13 20:18:17 +0000100 {OMPD_declare, OMPD_variant, OMPD_declare_variant},
Alexey Bataev61908f652018-04-23 19:53:05 +0000101 {OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel},
102 {OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for},
103 {OMPD_distribute_parallel_for, OMPD_simd,
104 OMPD_distribute_parallel_for_simd},
105 {OMPD_distribute, OMPD_simd, OMPD_distribute_simd},
106 {OMPD_end, OMPD_declare, OMPD_end_declare},
107 {OMPD_end_declare, OMPD_target, OMPD_end_declare_target},
108 {OMPD_target, OMPD_data, OMPD_target_data},
109 {OMPD_target, OMPD_enter, OMPD_target_enter},
110 {OMPD_target, OMPD_exit, OMPD_target_exit},
111 {OMPD_target, OMPD_update, OMPD_target_update},
112 {OMPD_target_enter, OMPD_data, OMPD_target_enter_data},
113 {OMPD_target_exit, OMPD_data, OMPD_target_exit_data},
114 {OMPD_for, OMPD_simd, OMPD_for_simd},
115 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
116 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
117 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
118 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd},
119 {OMPD_target, OMPD_parallel, OMPD_target_parallel},
120 {OMPD_target, OMPD_simd, OMPD_target_simd},
121 {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for},
122 {OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd},
123 {OMPD_teams, OMPD_distribute, OMPD_teams_distribute},
124 {OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd},
125 {OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel},
126 {OMPD_teams_distribute_parallel, OMPD_for,
127 OMPD_teams_distribute_parallel_for},
128 {OMPD_teams_distribute_parallel_for, OMPD_simd,
129 OMPD_teams_distribute_parallel_for_simd},
130 {OMPD_target, OMPD_teams, OMPD_target_teams},
131 {OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute},
132 {OMPD_target_teams_distribute, OMPD_parallel,
133 OMPD_target_teams_distribute_parallel},
134 {OMPD_target_teams_distribute, OMPD_simd,
135 OMPD_target_teams_distribute_simd},
136 {OMPD_target_teams_distribute_parallel, OMPD_for,
137 OMPD_target_teams_distribute_parallel_for},
138 {OMPD_target_teams_distribute_parallel_for, OMPD_simd,
Alexey Bataev60e51c42019-10-10 20:13:02 +0000139 OMPD_target_teams_distribute_parallel_for_simd},
Alexey Bataev5bbcead2019-10-14 17:17:41 +0000140 {OMPD_master, OMPD_taskloop, OMPD_master_taskloop},
Alexey Bataevb8552ab2019-10-18 16:47:35 +0000141 {OMPD_master_taskloop, OMPD_simd, OMPD_master_taskloop_simd},
Alexey Bataev5bbcead2019-10-14 17:17:41 +0000142 {OMPD_parallel, OMPD_master, OMPD_parallel_master},
Alexey Bataev14a388f2019-10-25 10:27:13 -0400143 {OMPD_parallel_master, OMPD_taskloop, OMPD_parallel_master_taskloop},
144 {OMPD_parallel_master_taskloop, OMPD_simd,
145 OMPD_parallel_master_taskloop_simd}};
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000146 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev61908f652018-04-23 19:53:05 +0000147 Token Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000148 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000149 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000150 ? static_cast<unsigned>(OMPD_unknown)
151 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
152 if (DKind == OMPD_unknown)
153 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000154
Alexey Bataev61908f652018-04-23 19:53:05 +0000155 for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) {
156 if (DKind != F[I][0])
Dmitry Polukhin82478332016-02-13 06:53:38 +0000157 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000158
Dmitry Polukhin82478332016-02-13 06:53:38 +0000159 Tok = P.getPreprocessor().LookAhead(0);
160 unsigned SDKind =
161 Tok.isAnnotation()
162 ? static_cast<unsigned>(OMPD_unknown)
163 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
164 if (SDKind == OMPD_unknown)
165 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000166
Alexey Bataev61908f652018-04-23 19:53:05 +0000167 if (SDKind == F[I][1]) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000168 P.ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000169 DKind = F[I][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000170 }
171 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000172 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
173 : OMPD_unknown;
174}
175
176static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000177 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000178 Sema &Actions = P.getActions();
179 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000180 // Allow to use 'operator' keyword for C++ operators
181 bool WithOperator = false;
182 if (Tok.is(tok::kw_operator)) {
183 P.ConsumeToken();
184 Tok = P.getCurToken();
185 WithOperator = true;
186 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000187 switch (Tok.getKind()) {
188 case tok::plus: // '+'
189 OOK = OO_Plus;
190 break;
191 case tok::minus: // '-'
192 OOK = OO_Minus;
193 break;
194 case tok::star: // '*'
195 OOK = OO_Star;
196 break;
197 case tok::amp: // '&'
198 OOK = OO_Amp;
199 break;
200 case tok::pipe: // '|'
201 OOK = OO_Pipe;
202 break;
203 case tok::caret: // '^'
204 OOK = OO_Caret;
205 break;
206 case tok::ampamp: // '&&'
207 OOK = OO_AmpAmp;
208 break;
209 case tok::pipepipe: // '||'
210 OOK = OO_PipePipe;
211 break;
212 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000213 if (!WithOperator)
214 break;
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000215 LLVM_FALLTHROUGH;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000216 default:
217 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
218 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
219 Parser::StopBeforeMatch);
220 return DeclarationName();
221 }
222 P.ConsumeToken();
223 auto &DeclNames = Actions.getASTContext().DeclarationNames;
224 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
225 : DeclNames.getCXXOperatorName(OOK);
226}
227
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000228/// Parse 'omp declare reduction' construct.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000229///
230/// declare-reduction-directive:
231/// annot_pragma_openmp 'declare' 'reduction'
232/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
233/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
234/// annot_pragma_openmp_end
235/// <reduction_id> is either a base language identifier or one of the following
236/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
237///
238Parser::DeclGroupPtrTy
239Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
240 // Parse '('.
241 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
242 if (T.expectAndConsume(diag::err_expected_lparen_after,
243 getOpenMPDirectiveName(OMPD_declare_reduction))) {
244 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
245 return DeclGroupPtrTy();
246 }
247
248 DeclarationName Name = parseOpenMPReductionId(*this);
249 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
250 return DeclGroupPtrTy();
251
252 // Consume ':'.
253 bool IsCorrect = !ExpectAndConsume(tok::colon);
254
255 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
256 return DeclGroupPtrTy();
257
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000258 IsCorrect = IsCorrect && !Name.isEmpty();
259
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000260 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
261 Diag(Tok.getLocation(), diag::err_expected_type);
262 IsCorrect = false;
263 }
264
265 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
266 return DeclGroupPtrTy();
267
268 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
269 // Parse list of types until ':' token.
270 do {
271 ColonProtectionRAIIObject ColonRAII(*this);
272 SourceRange Range;
Faisal Vali421b2d12017-12-29 05:41:00 +0000273 TypeResult TR =
274 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000275 if (TR.isUsable()) {
Alexey Bataev61908f652018-04-23 19:53:05 +0000276 QualType ReductionType =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000277 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
278 if (!ReductionType.isNull()) {
279 ReductionTypes.push_back(
280 std::make_pair(ReductionType, Range.getBegin()));
281 }
282 } else {
283 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
284 StopBeforeMatch);
285 }
286
287 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
288 break;
289
290 // Consume ','.
291 if (ExpectAndConsume(tok::comma)) {
292 IsCorrect = false;
293 if (Tok.is(tok::annot_pragma_openmp_end)) {
294 Diag(Tok.getLocation(), diag::err_expected_type);
295 return DeclGroupPtrTy();
296 }
297 }
298 } while (Tok.isNot(tok::annot_pragma_openmp_end));
299
300 if (ReductionTypes.empty()) {
301 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
302 return DeclGroupPtrTy();
303 }
304
305 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
306 return DeclGroupPtrTy();
307
308 // Consume ':'.
309 if (ExpectAndConsume(tok::colon))
310 IsCorrect = false;
311
312 if (Tok.is(tok::annot_pragma_openmp_end)) {
313 Diag(Tok.getLocation(), diag::err_expected_expression);
314 return DeclGroupPtrTy();
315 }
316
317 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
318 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
319
320 // Parse <combiner> expression and then parse initializer if any for each
321 // correct type.
322 unsigned I = 0, E = ReductionTypes.size();
Alexey Bataev61908f652018-04-23 19:53:05 +0000323 for (Decl *D : DRD.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000324 TentativeParsingAction TPA(*this);
325 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000326 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000327 Scope::OpenMPDirectiveScope);
328 // Parse <combiner> expression.
329 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
330 ExprResult CombinerResult =
331 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000332 D->getLocation(), /*DiscardedValue*/ false);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000333 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
334
335 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
336 Tok.isNot(tok::annot_pragma_openmp_end)) {
337 TPA.Commit();
338 IsCorrect = false;
339 break;
340 }
341 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
342 ExprResult InitializerResult;
343 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
344 // Parse <initializer> expression.
345 if (Tok.is(tok::identifier) &&
Alexey Bataev61908f652018-04-23 19:53:05 +0000346 Tok.getIdentifierInfo()->isStr("initializer")) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000347 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000348 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000349 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
350 TPA.Commit();
351 IsCorrect = false;
352 break;
353 }
354 // Parse '('.
355 BalancedDelimiterTracker T(*this, tok::l_paren,
356 tok::annot_pragma_openmp_end);
357 IsCorrect =
358 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
359 IsCorrect;
360 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
361 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000362 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000363 Scope::OpenMPDirectiveScope);
364 // Parse expression.
Alexey Bataev070f43a2017-09-06 14:49:58 +0000365 VarDecl *OmpPrivParm =
366 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
367 D);
368 // Check if initializer is omp_priv <init_expr> or something else.
369 if (Tok.is(tok::identifier) &&
370 Tok.getIdentifierInfo()->isStr("omp_priv")) {
Alexey Bataeve6aa4692018-09-13 16:54:05 +0000371 if (Actions.getLangOpts().CPlusPlus) {
372 InitializerResult = Actions.ActOnFinishFullExpr(
373 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000374 /*DiscardedValue*/ false);
Alexey Bataeve6aa4692018-09-13 16:54:05 +0000375 } else {
376 ConsumeToken();
377 ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
378 }
Alexey Bataev070f43a2017-09-06 14:49:58 +0000379 } else {
380 InitializerResult = Actions.ActOnFinishFullExpr(
381 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000382 /*DiscardedValue*/ false);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000383 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000384 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
Alexey Bataev070f43a2017-09-06 14:49:58 +0000385 D, InitializerResult.get(), OmpPrivParm);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000386 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
387 Tok.isNot(tok::annot_pragma_openmp_end)) {
388 TPA.Commit();
389 IsCorrect = false;
390 break;
391 }
392 IsCorrect =
393 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
394 }
395 }
396
397 ++I;
398 // Revert parsing if not the last type, otherwise accept it, we're done with
399 // parsing.
400 if (I != E)
401 TPA.Revert();
402 else
403 TPA.Commit();
404 }
405 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
406 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000407}
408
Alexey Bataev070f43a2017-09-06 14:49:58 +0000409void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
410 // Parse declarator '=' initializer.
411 // If a '==' or '+=' is found, suggest a fixit to '='.
412 if (isTokenEqualOrEqualTypo()) {
413 ConsumeToken();
414
415 if (Tok.is(tok::code_completion)) {
416 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
417 Actions.FinalizeDeclaration(OmpPrivParm);
418 cutOffParsing();
419 return;
420 }
421
422 ExprResult Init(ParseInitializer());
423
424 if (Init.isInvalid()) {
425 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
426 Actions.ActOnInitializerError(OmpPrivParm);
427 } else {
428 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
429 /*DirectInit=*/false);
430 }
431 } else if (Tok.is(tok::l_paren)) {
432 // Parse C++ direct initializer: '(' expression-list ')'
433 BalancedDelimiterTracker T(*this, tok::l_paren);
434 T.consumeOpen();
435
436 ExprVector Exprs;
437 CommaLocsTy CommaLocs;
438
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000439 SourceLocation LParLoc = T.getOpenLocation();
Ilya Biryukovff2a9972019-02-26 11:01:50 +0000440 auto RunSignatureHelp = [this, OmpPrivParm, LParLoc, &Exprs]() {
441 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
442 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
443 OmpPrivParm->getLocation(), Exprs, LParLoc);
444 CalledSignatureHelp = true;
445 return PreferredType;
446 };
447 if (ParseExpressionList(Exprs, CommaLocs, [&] {
448 PreferredType.enterFunctionArgument(Tok.getLocation(),
449 RunSignatureHelp);
450 })) {
451 if (PP.isCodeCompletionReached() && !CalledSignatureHelp)
452 RunSignatureHelp();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000453 Actions.ActOnInitializerError(OmpPrivParm);
454 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
455 } else {
456 // Match the ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000457 SourceLocation RLoc = Tok.getLocation();
458 if (!T.consumeClose())
459 RLoc = T.getCloseLocation();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000460
461 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
462 "Unexpected number of commas!");
463
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000464 ExprResult Initializer =
465 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000466 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
467 /*DirectInit=*/true);
468 }
469 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
470 // Parse C++0x braced-init-list.
471 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
472
473 ExprResult Init(ParseBraceInitializer());
474
475 if (Init.isInvalid()) {
476 Actions.ActOnInitializerError(OmpPrivParm);
477 } else {
478 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
479 /*DirectInit=*/true);
480 }
481 } else {
482 Actions.ActOnUninitializedDecl(OmpPrivParm);
483 }
484}
485
Michael Kruse251e1482019-02-01 20:25:04 +0000486/// Parses 'omp declare mapper' directive.
487///
488/// declare-mapper-directive:
489/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifier> ':']
490/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
491/// annot_pragma_openmp_end
492/// <mapper-identifier> and <var> are base language identifiers.
493///
494Parser::DeclGroupPtrTy
495Parser::ParseOpenMPDeclareMapperDirective(AccessSpecifier AS) {
496 bool IsCorrect = true;
497 // Parse '('
498 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
499 if (T.expectAndConsume(diag::err_expected_lparen_after,
500 getOpenMPDirectiveName(OMPD_declare_mapper))) {
501 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
502 return DeclGroupPtrTy();
503 }
504
505 // Parse <mapper-identifier>
506 auto &DeclNames = Actions.getASTContext().DeclarationNames;
507 DeclarationName MapperId;
508 if (PP.LookAhead(0).is(tok::colon)) {
509 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
510 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
511 IsCorrect = false;
512 } else {
513 MapperId = DeclNames.getIdentifier(Tok.getIdentifierInfo());
514 }
515 ConsumeToken();
516 // Consume ':'.
517 ExpectAndConsume(tok::colon);
518 } else {
519 // If no mapper identifier is provided, its name is "default" by default
520 MapperId =
521 DeclNames.getIdentifier(&Actions.getASTContext().Idents.get("default"));
522 }
523
524 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
525 return DeclGroupPtrTy();
526
527 // Parse <type> <var>
528 DeclarationName VName;
529 QualType MapperType;
530 SourceRange Range;
531 TypeResult ParsedType = parseOpenMPDeclareMapperVarDecl(Range, VName, AS);
532 if (ParsedType.isUsable())
533 MapperType =
534 Actions.ActOnOpenMPDeclareMapperType(Range.getBegin(), ParsedType);
535 if (MapperType.isNull())
536 IsCorrect = false;
537 if (!IsCorrect) {
538 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
539 return DeclGroupPtrTy();
540 }
541
542 // Consume ')'.
543 IsCorrect &= !T.consumeClose();
544 if (!IsCorrect) {
545 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
546 return DeclGroupPtrTy();
547 }
548
549 // Enter scope.
550 OMPDeclareMapperDecl *DMD = Actions.ActOnOpenMPDeclareMapperDirectiveStart(
551 getCurScope(), Actions.getCurLexicalContext(), MapperId, MapperType,
552 Range.getBegin(), VName, AS);
553 DeclarationNameInfo DirName;
554 SourceLocation Loc = Tok.getLocation();
555 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
556 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
557 ParseScope OMPDirectiveScope(this, ScopeFlags);
558 Actions.StartOpenMPDSABlock(OMPD_declare_mapper, DirName, getCurScope(), Loc);
559
560 // Add the mapper variable declaration.
561 Actions.ActOnOpenMPDeclareMapperDirectiveVarDecl(
562 DMD, getCurScope(), MapperType, Range.getBegin(), VName);
563
564 // Parse map clauses.
565 SmallVector<OMPClause *, 6> Clauses;
566 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
567 OpenMPClauseKind CKind = Tok.isAnnotation()
568 ? OMPC_unknown
569 : getOpenMPClauseKind(PP.getSpelling(Tok));
570 Actions.StartOpenMPClause(CKind);
571 OMPClause *Clause =
572 ParseOpenMPClause(OMPD_declare_mapper, CKind, Clauses.size() == 0);
573 if (Clause)
574 Clauses.push_back(Clause);
575 else
576 IsCorrect = false;
577 // Skip ',' if any.
578 if (Tok.is(tok::comma))
579 ConsumeToken();
580 Actions.EndOpenMPClause();
581 }
582 if (Clauses.empty()) {
583 Diag(Tok, diag::err_omp_expected_clause)
584 << getOpenMPDirectiveName(OMPD_declare_mapper);
585 IsCorrect = false;
586 }
587
588 // Exit scope.
589 Actions.EndOpenMPDSABlock(nullptr);
590 OMPDirectiveScope.Exit();
591
592 DeclGroupPtrTy DGP =
593 Actions.ActOnOpenMPDeclareMapperDirectiveEnd(DMD, getCurScope(), Clauses);
594 if (!IsCorrect)
595 return DeclGroupPtrTy();
596 return DGP;
597}
598
599TypeResult Parser::parseOpenMPDeclareMapperVarDecl(SourceRange &Range,
600 DeclarationName &Name,
601 AccessSpecifier AS) {
602 // Parse the common declaration-specifiers piece.
603 Parser::DeclSpecContext DSC = Parser::DeclSpecContext::DSC_type_specifier;
604 DeclSpec DS(AttrFactory);
605 ParseSpecifierQualifierList(DS, AS, DSC);
606
607 // Parse the declarator.
608 DeclaratorContext Context = DeclaratorContext::PrototypeContext;
609 Declarator DeclaratorInfo(DS, Context);
610 ParseDeclarator(DeclaratorInfo);
611 Range = DeclaratorInfo.getSourceRange();
612 if (DeclaratorInfo.getIdentifier() == nullptr) {
613 Diag(Tok.getLocation(), diag::err_omp_mapper_expected_declarator);
614 return true;
615 }
616 Name = Actions.GetNameForDeclarator(DeclaratorInfo).getName();
617
618 return Actions.ActOnOpenMPDeclareMapperVarDecl(getCurScope(), DeclaratorInfo);
619}
620
Alexey Bataev2af33e32016-04-07 12:45:37 +0000621namespace {
622/// RAII that recreates function context for correct parsing of clauses of
623/// 'declare simd' construct.
624/// OpenMP, 2.8.2 declare simd Construct
625/// The expressions appearing in the clauses of this directive are evaluated in
626/// the scope of the arguments of the function declaration or definition.
627class FNContextRAII final {
628 Parser &P;
629 Sema::CXXThisScopeRAII *ThisScope;
630 Parser::ParseScope *TempScope;
631 Parser::ParseScope *FnScope;
632 bool HasTemplateScope = false;
633 bool HasFunScope = false;
634 FNContextRAII() = delete;
635 FNContextRAII(const FNContextRAII &) = delete;
636 FNContextRAII &operator=(const FNContextRAII &) = delete;
637
638public:
639 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
640 Decl *D = *Ptr.get().begin();
641 NamedDecl *ND = dyn_cast<NamedDecl>(D);
642 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
643 Sema &Actions = P.getActions();
644
645 // Allow 'this' within late-parsed attributes.
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000646 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, Qualifiers(),
Alexey Bataev2af33e32016-04-07 12:45:37 +0000647 ND && ND->isCXXInstanceMember());
648
649 // If the Decl is templatized, add template parameters to scope.
650 HasTemplateScope = D->isTemplateDecl();
651 TempScope =
652 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
653 if (HasTemplateScope)
654 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
655
656 // If the Decl is on a function, add function parameters to the scope.
657 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000658 FnScope = new Parser::ParseScope(
659 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
660 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000661 if (HasFunScope)
662 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
663 }
664 ~FNContextRAII() {
665 if (HasFunScope) {
666 P.getActions().ActOnExitFunctionContext();
667 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
668 }
669 if (HasTemplateScope)
670 TempScope->Exit();
671 delete FnScope;
672 delete TempScope;
673 delete ThisScope;
674 }
675};
676} // namespace
677
Alexey Bataevd93d3762016-04-12 09:35:56 +0000678/// Parses clauses for 'declare simd' directive.
679/// clause:
680/// 'inbranch' | 'notinbranch'
681/// 'simdlen' '(' <expr> ')'
682/// { 'uniform' '(' <argument_list> ')' }
683/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000684/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
685static bool parseDeclareSimdClauses(
686 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
687 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
688 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
689 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000690 SourceRange BSRange;
691 const Token &Tok = P.getCurToken();
692 bool IsError = false;
693 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
694 if (Tok.isNot(tok::identifier))
695 break;
696 OMPDeclareSimdDeclAttr::BranchStateTy Out;
697 IdentifierInfo *II = Tok.getIdentifierInfo();
698 StringRef ClauseName = II->getName();
699 // Parse 'inranch|notinbranch' clauses.
700 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
701 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
702 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
703 << ClauseName
704 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
705 IsError = true;
706 }
707 BS = Out;
708 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
709 P.ConsumeToken();
710 } else if (ClauseName.equals("simdlen")) {
711 if (SimdLen.isUsable()) {
712 P.Diag(Tok, diag::err_omp_more_one_clause)
713 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
714 IsError = true;
715 }
716 P.ConsumeToken();
717 SourceLocation RLoc;
718 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
719 if (SimdLen.isInvalid())
720 IsError = true;
721 } else {
722 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000723 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
724 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000725 Parser::OpenMPVarListDataTy Data;
Alexey Bataev61908f652018-04-23 19:53:05 +0000726 SmallVectorImpl<Expr *> *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000727 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000728 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000729 else if (CKind == OMPC_linear)
730 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000731
732 P.ConsumeToken();
733 if (P.ParseOpenMPVarList(OMPD_declare_simd,
734 getOpenMPClauseKind(ClauseName), *Vars, Data))
735 IsError = true;
Alexey Bataev61908f652018-04-23 19:53:05 +0000736 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000737 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataev61908f652018-04-23 19:53:05 +0000738 } else if (CKind == OMPC_linear) {
Alexey Bataevecba70f2016-04-12 11:02:11 +0000739 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
740 Data.DepLinMapLoc))
741 Data.LinKind = OMPC_LINEAR_val;
742 LinModifiers.append(Linears.size() - LinModifiers.size(),
743 Data.LinKind);
744 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
745 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000746 } else
747 // TODO: add parsing of other clauses.
748 break;
749 }
750 // Skip ',' if any.
751 if (Tok.is(tok::comma))
752 P.ConsumeToken();
753 }
754 return IsError;
755}
756
Alexey Bataev2af33e32016-04-07 12:45:37 +0000757/// Parse clauses for '#pragma omp declare simd'.
758Parser::DeclGroupPtrTy
759Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
760 CachedTokens &Toks, SourceLocation Loc) {
Ilya Biryukov929af672019-05-17 09:32:05 +0000761 PP.EnterToken(Tok, /*IsReinject*/ true);
762 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
763 /*IsReinject*/ true);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000764 // Consume the previously pushed token.
765 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Alexey Bataevd158cf62019-09-13 20:18:17 +0000766 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000767
768 FNContextRAII FnContext(*this, Ptr);
769 OMPDeclareSimdDeclAttr::BranchStateTy BS =
770 OMPDeclareSimdDeclAttr::BS_Undefined;
771 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000772 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000773 SmallVector<Expr *, 4> Aligneds;
774 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000775 SmallVector<Expr *, 4> Linears;
776 SmallVector<unsigned, 4> LinModifiers;
777 SmallVector<Expr *, 4> Steps;
778 bool IsError =
779 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
780 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000781 // Need to check for extra tokens.
782 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
783 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
784 << getOpenMPDirectiveName(OMPD_declare_simd);
785 while (Tok.isNot(tok::annot_pragma_openmp_end))
786 ConsumeAnyToken();
787 }
788 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000789 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000790 if (IsError)
791 return Ptr;
792 return Actions.ActOnOpenMPDeclareSimdDirective(
793 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
794 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataev20dfd772016-04-04 10:12:15 +0000795}
796
Alexey Bataeva15a1412019-10-02 18:19:02 +0000797/// Parse optional 'score' '(' <expr> ')' ':'.
798static ExprResult parseContextScore(Parser &P) {
799 ExprResult ScoreExpr;
Alexey Bataevfde11e92019-11-07 11:03:10 -0500800 Sema::OMPCtxStringType Buffer;
Alexey Bataeva15a1412019-10-02 18:19:02 +0000801 StringRef SelectorName =
802 P.getPreprocessor().getSpelling(P.getCurToken(), Buffer);
Alexey Bataevdcec2ac2019-11-05 15:33:18 -0500803 if (!SelectorName.equals("score"))
Alexey Bataeva15a1412019-10-02 18:19:02 +0000804 return ScoreExpr;
Alexey Bataeva15a1412019-10-02 18:19:02 +0000805 (void)P.ConsumeToken();
806 SourceLocation RLoc;
807 ScoreExpr = P.ParseOpenMPParensExpr(SelectorName, RLoc);
808 // Parse ':'
809 if (P.getCurToken().is(tok::colon))
810 (void)P.ConsumeAnyToken();
811 else
812 P.Diag(P.getCurToken(), diag::warn_pragma_expected_colon)
813 << "context selector score clause";
814 return ScoreExpr;
815}
816
Alexey Bataev9ff34742019-09-25 19:43:37 +0000817/// Parse context selector for 'implementation' selector set:
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000818/// 'vendor' '(' [ 'score' '(' <score _expr> ')' ':' ] <vendor> { ',' <vendor> }
819/// ')'
Alexey Bataevfde11e92019-11-07 11:03:10 -0500820static void
821parseImplementationSelector(Parser &P, SourceLocation Loc,
822 llvm::StringMap<SourceLocation> &UsedCtx,
823 SmallVectorImpl<Sema::OMPCtxSelectorData> &Data) {
Alexey Bataev9ff34742019-09-25 19:43:37 +0000824 const Token &Tok = P.getCurToken();
825 // Parse inner context selector set name, if any.
826 if (!Tok.is(tok::identifier)) {
827 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
828 << "implementation";
829 // Skip until either '}', ')', or end of directive.
830 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
831 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
832 ;
833 return;
834 }
Alexey Bataevfde11e92019-11-07 11:03:10 -0500835 Sema::OMPCtxStringType Buffer;
Alexey Bataev9ff34742019-09-25 19:43:37 +0000836 StringRef CtxSelectorName = P.getPreprocessor().getSpelling(Tok, Buffer);
Alexey Bataev70d2e542019-10-08 17:47:52 +0000837 auto Res = UsedCtx.try_emplace(CtxSelectorName, Tok.getLocation());
838 if (!Res.second) {
839 // OpenMP 5.0, 2.3.2 Context Selectors, Restrictions.
840 // Each trait-selector-name can only be specified once.
841 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_ctx_mutiple_use)
842 << CtxSelectorName << "implementation";
843 P.Diag(Res.first->getValue(), diag::note_omp_declare_variant_ctx_used_here)
844 << CtxSelectorName;
845 }
Alexey Bataevfde11e92019-11-07 11:03:10 -0500846 OpenMPContextSelectorKind CSKind = getOpenMPContextSelector(CtxSelectorName);
Alexey Bataev9ff34742019-09-25 19:43:37 +0000847 (void)P.ConsumeToken();
848 switch (CSKind) {
Alexey Bataevfde11e92019-11-07 11:03:10 -0500849 case OMP_CTX_vendor: {
Alexey Bataev9ff34742019-09-25 19:43:37 +0000850 // Parse '('.
851 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
852 (void)T.expectAndConsume(diag::err_expected_lparen_after,
853 CtxSelectorName.data());
Alexey Bataevfde11e92019-11-07 11:03:10 -0500854 ExprResult Score = parseContextScore(P);
855 llvm::UniqueVector<Sema::OMPCtxStringType> Vendors;
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000856 do {
857 // Parse <vendor>.
858 StringRef VendorName;
859 if (Tok.is(tok::identifier)) {
860 Buffer.clear();
861 VendorName = P.getPreprocessor().getSpelling(P.getCurToken(), Buffer);
862 (void)P.ConsumeToken();
Alexey Bataev303657a2019-10-08 19:44:16 +0000863 if (!VendorName.empty())
Alexey Bataev4513e93f2019-10-10 15:15:26 +0000864 Vendors.insert(VendorName);
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000865 } else {
866 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_item_expected)
867 << "vendor identifier"
868 << "vendor"
869 << "implementation";
870 }
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000871 if (!P.TryConsumeToken(tok::comma) && Tok.isNot(tok::r_paren)) {
872 P.Diag(Tok, diag::err_expected_punc)
873 << (VendorName.empty() ? "vendor name" : VendorName);
874 }
875 } while (Tok.is(tok::identifier));
Alexey Bataev9ff34742019-09-25 19:43:37 +0000876 // Parse ')'.
877 (void)T.consumeClose();
Alexey Bataevfde11e92019-11-07 11:03:10 -0500878 if (!Vendors.empty())
879 Data.emplace_back(OMP_CTX_SET_implementation, CSKind, Score, Vendors);
Alexey Bataev9ff34742019-09-25 19:43:37 +0000880 break;
881 }
Alexey Bataevfde11e92019-11-07 11:03:10 -0500882 case OMP_CTX_unknown:
Alexey Bataev9ff34742019-09-25 19:43:37 +0000883 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
884 << "implementation";
885 // Skip until either '}', ')', or end of directive.
886 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
887 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
888 ;
889 return;
890 }
Alexey Bataev9ff34742019-09-25 19:43:37 +0000891}
892
Alexey Bataevd158cf62019-09-13 20:18:17 +0000893/// Parses clauses for 'declare variant' directive.
894/// clause:
Alexey Bataevd158cf62019-09-13 20:18:17 +0000895/// <selector_set_name> '=' '{' <context_selectors> '}'
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000896/// [ ',' <selector_set_name> '=' '{' <context_selectors> '}' ]
897bool Parser::parseOpenMPContextSelectors(
Alexey Bataevfde11e92019-11-07 11:03:10 -0500898 SourceLocation Loc, SmallVectorImpl<Sema::OMPCtxSelectorData> &Data) {
Alexey Bataev5d154c32019-10-08 15:56:43 +0000899 llvm::StringMap<SourceLocation> UsedCtxSets;
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000900 do {
901 // Parse inner context selector set name.
902 if (!Tok.is(tok::identifier)) {
903 Diag(Tok.getLocation(), diag::err_omp_declare_variant_no_ctx_selector)
Alexey Bataevdba792c2019-09-23 18:13:31 +0000904 << getOpenMPClauseName(OMPC_match);
Alexey Bataevd158cf62019-09-13 20:18:17 +0000905 return true;
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000906 }
Alexey Bataevfde11e92019-11-07 11:03:10 -0500907 Sema::OMPCtxStringType Buffer;
Alexey Bataev9ff34742019-09-25 19:43:37 +0000908 StringRef CtxSelectorSetName = PP.getSpelling(Tok, Buffer);
Alexey Bataev5d154c32019-10-08 15:56:43 +0000909 auto Res = UsedCtxSets.try_emplace(CtxSelectorSetName, Tok.getLocation());
910 if (!Res.second) {
911 // OpenMP 5.0, 2.3.2 Context Selectors, Restrictions.
912 // Each trait-set-selector-name can only be specified once.
913 Diag(Tok.getLocation(), diag::err_omp_declare_variant_ctx_set_mutiple_use)
914 << CtxSelectorSetName;
915 Diag(Res.first->getValue(),
916 diag::note_omp_declare_variant_ctx_set_used_here)
917 << CtxSelectorSetName;
918 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000919 // Parse '='.
920 (void)ConsumeToken();
921 if (Tok.isNot(tok::equal)) {
922 Diag(Tok.getLocation(), diag::err_omp_declare_variant_equal_expected)
Alexey Bataev9ff34742019-09-25 19:43:37 +0000923 << CtxSelectorSetName;
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000924 return true;
925 }
926 (void)ConsumeToken();
927 // TBD: add parsing of known context selectors.
928 // Unknown selector - just ignore it completely.
929 {
930 // Parse '{'.
931 BalancedDelimiterTracker TBr(*this, tok::l_brace,
932 tok::annot_pragma_openmp_end);
933 if (TBr.expectAndConsume(diag::err_expected_lbrace_after, "="))
934 return true;
Alexey Bataevfde11e92019-11-07 11:03:10 -0500935 OpenMPContextSelectorSetKind CSSKind =
936 getOpenMPContextSelectorSet(CtxSelectorSetName);
Alexey Bataev70d2e542019-10-08 17:47:52 +0000937 llvm::StringMap<SourceLocation> UsedCtx;
938 do {
939 switch (CSSKind) {
Alexey Bataevfde11e92019-11-07 11:03:10 -0500940 case OMP_CTX_SET_implementation:
941 parseImplementationSelector(*this, Loc, UsedCtx, Data);
Alexey Bataev70d2e542019-10-08 17:47:52 +0000942 break;
Alexey Bataevfde11e92019-11-07 11:03:10 -0500943 case OMP_CTX_SET_unknown:
Alexey Bataev70d2e542019-10-08 17:47:52 +0000944 // Skip until either '}', ')', or end of directive.
945 while (!SkipUntil(tok::r_brace, tok::r_paren,
946 tok::annot_pragma_openmp_end, StopBeforeMatch))
947 ;
948 break;
949 }
950 const Token PrevTok = Tok;
951 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
952 Diag(Tok, diag::err_omp_expected_comma_brace)
953 << (PrevTok.isAnnotation() ? "context selector trait"
954 : PP.getSpelling(PrevTok));
955 } while (Tok.is(tok::identifier));
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000956 // Parse '}'.
957 (void)TBr.consumeClose();
958 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000959 // Consume ','
960 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end))
961 (void)ExpectAndConsume(tok::comma);
962 } while (Tok.isAnyIdentifier());
Alexey Bataevd158cf62019-09-13 20:18:17 +0000963 return false;
964}
965
966/// Parse clauses for '#pragma omp declare variant ( variant-func-id ) clause'.
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000967void Parser::ParseOMPDeclareVariantClauses(Parser::DeclGroupPtrTy Ptr,
968 CachedTokens &Toks,
969 SourceLocation Loc) {
Alexey Bataevd158cf62019-09-13 20:18:17 +0000970 PP.EnterToken(Tok, /*IsReinject*/ true);
971 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
972 /*IsReinject*/ true);
973 // Consume the previously pushed token.
974 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
975 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
976
977 FNContextRAII FnContext(*this, Ptr);
978 // Parse function declaration id.
979 SourceLocation RLoc;
980 // Parse with IsAddressOfOperand set to true to parse methods as DeclRefExprs
981 // instead of MemberExprs.
982 ExprResult AssociatedFunction =
983 ParseOpenMPParensExpr(getOpenMPDirectiveName(OMPD_declare_variant), RLoc,
984 /*IsAddressOfOperand=*/true);
985 if (!AssociatedFunction.isUsable()) {
986 if (!Tok.is(tok::annot_pragma_openmp_end))
987 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
988 ;
989 // Skip the last annot_pragma_openmp_end.
990 (void)ConsumeAnnotationToken();
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000991 return;
992 }
993 Optional<std::pair<FunctionDecl *, Expr *>> DeclVarData =
994 Actions.checkOpenMPDeclareVariantFunction(
995 Ptr, AssociatedFunction.get(), SourceRange(Loc, Tok.getLocation()));
996
997 // Parse 'match'.
Alexey Bataevdba792c2019-09-23 18:13:31 +0000998 OpenMPClauseKind CKind = Tok.isAnnotation()
999 ? OMPC_unknown
1000 : getOpenMPClauseKind(PP.getSpelling(Tok));
1001 if (CKind != OMPC_match) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001002 Diag(Tok.getLocation(), diag::err_omp_declare_variant_wrong_clause)
Alexey Bataevdba792c2019-09-23 18:13:31 +00001003 << getOpenMPClauseName(OMPC_match);
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001004 while (!SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
1005 ;
1006 // Skip the last annot_pragma_openmp_end.
1007 (void)ConsumeAnnotationToken();
1008 return;
1009 }
1010 (void)ConsumeToken();
1011 // Parse '('.
1012 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataevdba792c2019-09-23 18:13:31 +00001013 if (T.expectAndConsume(diag::err_expected_lparen_after,
1014 getOpenMPClauseName(OMPC_match))) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001015 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
1016 ;
1017 // Skip the last annot_pragma_openmp_end.
1018 (void)ConsumeAnnotationToken();
1019 return;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001020 }
1021
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001022 // Parse inner context selectors.
Alexey Bataevfde11e92019-11-07 11:03:10 -05001023 SmallVector<Sema::OMPCtxSelectorData, 4> Data;
1024 if (!parseOpenMPContextSelectors(Loc, Data)) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001025 // Parse ')'.
1026 (void)T.consumeClose();
1027 // Need to check for extra tokens.
1028 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1029 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1030 << getOpenMPDirectiveName(OMPD_declare_variant);
1031 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001032 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001033
1034 // Skip last tokens.
1035 while (Tok.isNot(tok::annot_pragma_openmp_end))
1036 ConsumeAnyToken();
Alexey Bataevfde11e92019-11-07 11:03:10 -05001037 if (DeclVarData.hasValue())
1038 Actions.ActOnOpenMPDeclareVariantDirective(
1039 DeclVarData.getValue().first, DeclVarData.getValue().second,
1040 SourceRange(Loc, Tok.getLocation()), Data);
Alexey Bataevd158cf62019-09-13 20:18:17 +00001041 // Skip the last annot_pragma_openmp_end.
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001042 (void)ConsumeAnnotationToken();
Alexey Bataevd158cf62019-09-13 20:18:17 +00001043}
1044
Alexey Bataev729e2422019-08-23 16:11:14 +00001045/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
1046///
1047/// default-clause:
1048/// 'default' '(' 'none' | 'shared' ')
1049///
1050/// proc_bind-clause:
1051/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1052///
1053/// device_type-clause:
1054/// 'device_type' '(' 'host' | 'nohost' | 'any' )'
1055namespace {
1056 struct SimpleClauseData {
1057 unsigned Type;
1058 SourceLocation Loc;
1059 SourceLocation LOpen;
1060 SourceLocation TypeLoc;
1061 SourceLocation RLoc;
1062 SimpleClauseData(unsigned Type, SourceLocation Loc, SourceLocation LOpen,
1063 SourceLocation TypeLoc, SourceLocation RLoc)
1064 : Type(Type), Loc(Loc), LOpen(LOpen), TypeLoc(TypeLoc), RLoc(RLoc) {}
1065 };
1066} // anonymous namespace
1067
1068static Optional<SimpleClauseData>
1069parseOpenMPSimpleClause(Parser &P, OpenMPClauseKind Kind) {
1070 const Token &Tok = P.getCurToken();
1071 SourceLocation Loc = Tok.getLocation();
1072 SourceLocation LOpen = P.ConsumeToken();
1073 // Parse '('.
1074 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
1075 if (T.expectAndConsume(diag::err_expected_lparen_after,
1076 getOpenMPClauseName(Kind)))
1077 return llvm::None;
1078
1079 unsigned Type = getOpenMPSimpleClauseType(
1080 Kind, Tok.isAnnotation() ? "" : P.getPreprocessor().getSpelling(Tok));
1081 SourceLocation TypeLoc = Tok.getLocation();
1082 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1083 Tok.isNot(tok::annot_pragma_openmp_end))
1084 P.ConsumeAnyToken();
1085
1086 // Parse ')'.
1087 SourceLocation RLoc = Tok.getLocation();
1088 if (!T.consumeClose())
1089 RLoc = T.getCloseLocation();
1090
1091 return SimpleClauseData(Type, Loc, LOpen, TypeLoc, RLoc);
1092}
1093
Kelvin Lie0502752018-11-21 20:15:57 +00001094Parser::DeclGroupPtrTy Parser::ParseOMPDeclareTargetClauses() {
1095 // OpenMP 4.5 syntax with list of entities.
1096 Sema::NamedDeclSetType SameDirectiveDecls;
Alexey Bataev729e2422019-08-23 16:11:14 +00001097 SmallVector<std::tuple<OMPDeclareTargetDeclAttr::MapTypeTy, SourceLocation,
1098 NamedDecl *>,
1099 4>
1100 DeclareTargetDecls;
1101 OMPDeclareTargetDeclAttr::DevTypeTy DT = OMPDeclareTargetDeclAttr::DT_Any;
1102 SourceLocation DeviceTypeLoc;
Kelvin Lie0502752018-11-21 20:15:57 +00001103 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1104 OMPDeclareTargetDeclAttr::MapTypeTy MT = OMPDeclareTargetDeclAttr::MT_To;
1105 if (Tok.is(tok::identifier)) {
1106 IdentifierInfo *II = Tok.getIdentifierInfo();
1107 StringRef ClauseName = II->getName();
Alexey Bataev729e2422019-08-23 16:11:14 +00001108 bool IsDeviceTypeClause =
1109 getLangOpts().OpenMP >= 50 &&
1110 getOpenMPClauseKind(ClauseName) == OMPC_device_type;
1111 // Parse 'to|link|device_type' clauses.
1112 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName, MT) &&
1113 !IsDeviceTypeClause) {
1114 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
1115 << ClauseName << (getLangOpts().OpenMP >= 50 ? 1 : 0);
Kelvin Lie0502752018-11-21 20:15:57 +00001116 break;
1117 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001118 // Parse 'device_type' clause and go to next clause if any.
1119 if (IsDeviceTypeClause) {
1120 Optional<SimpleClauseData> DevTypeData =
1121 parseOpenMPSimpleClause(*this, OMPC_device_type);
1122 if (DevTypeData.hasValue()) {
1123 if (DeviceTypeLoc.isValid()) {
1124 // We already saw another device_type clause, diagnose it.
1125 Diag(DevTypeData.getValue().Loc,
1126 diag::warn_omp_more_one_device_type_clause);
1127 }
1128 switch(static_cast<OpenMPDeviceType>(DevTypeData.getValue().Type)) {
1129 case OMPC_DEVICE_TYPE_any:
1130 DT = OMPDeclareTargetDeclAttr::DT_Any;
1131 break;
1132 case OMPC_DEVICE_TYPE_host:
1133 DT = OMPDeclareTargetDeclAttr::DT_Host;
1134 break;
1135 case OMPC_DEVICE_TYPE_nohost:
1136 DT = OMPDeclareTargetDeclAttr::DT_NoHost;
1137 break;
1138 case OMPC_DEVICE_TYPE_unknown:
1139 llvm_unreachable("Unexpected device_type");
1140 }
1141 DeviceTypeLoc = DevTypeData.getValue().Loc;
1142 }
1143 continue;
1144 }
Kelvin Lie0502752018-11-21 20:15:57 +00001145 ConsumeToken();
1146 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001147 auto &&Callback = [this, MT, &DeclareTargetDecls, &SameDirectiveDecls](
1148 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
1149 NamedDecl *ND = Actions.lookupOpenMPDeclareTargetName(
1150 getCurScope(), SS, NameInfo, SameDirectiveDecls);
1151 if (ND)
1152 DeclareTargetDecls.emplace_back(MT, NameInfo.getLoc(), ND);
Kelvin Lie0502752018-11-21 20:15:57 +00001153 };
1154 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
1155 /*AllowScopeSpecifier=*/true))
1156 break;
1157
1158 // Consume optional ','.
1159 if (Tok.is(tok::comma))
1160 ConsumeToken();
1161 }
1162 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1163 ConsumeAnyToken();
Alexey Bataev729e2422019-08-23 16:11:14 +00001164 for (auto &MTLocDecl : DeclareTargetDecls) {
1165 OMPDeclareTargetDeclAttr::MapTypeTy MT;
1166 SourceLocation Loc;
1167 NamedDecl *ND;
1168 std::tie(MT, Loc, ND) = MTLocDecl;
1169 // device_type clause is applied only to functions.
1170 Actions.ActOnOpenMPDeclareTargetName(
1171 ND, Loc, MT, isa<VarDecl>(ND) ? OMPDeclareTargetDeclAttr::DT_Any : DT);
1172 }
Kelvin Lie0502752018-11-21 20:15:57 +00001173 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
1174 SameDirectiveDecls.end());
1175 if (Decls.empty())
1176 return DeclGroupPtrTy();
1177 return Actions.BuildDeclaratorGroup(Decls);
1178}
1179
1180void Parser::ParseOMPEndDeclareTargetDirective(OpenMPDirectiveKind DKind,
1181 SourceLocation DTLoc) {
1182 if (DKind != OMPD_end_declare_target) {
1183 Diag(Tok, diag::err_expected_end_declare_target);
1184 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
1185 return;
1186 }
1187 ConsumeAnyToken();
1188 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1189 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1190 << getOpenMPDirectiveName(OMPD_end_declare_target);
1191 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1192 }
1193 // Skip the last annot_pragma_openmp_end.
1194 ConsumeAnyToken();
1195}
1196
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001197/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001198///
1199/// threadprivate-directive:
1200/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001201/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +00001202///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001203/// allocate-directive:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001204/// annot_pragma_openmp 'allocate' simple-variable-list [<clause>]
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001205/// annot_pragma_openmp_end
1206///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001207/// declare-reduction-directive:
1208/// annot_pragma_openmp 'declare' 'reduction' [...]
1209/// annot_pragma_openmp_end
1210///
Michael Kruse251e1482019-02-01 20:25:04 +00001211/// declare-mapper-directive:
1212/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1213/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1214/// annot_pragma_openmp_end
1215///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001216/// declare-simd-directive:
1217/// annot_pragma_openmp 'declare simd' {<clause> [,]}
1218/// annot_pragma_openmp_end
1219/// <function declaration/definition>
1220///
Kelvin Li1408f912018-09-26 04:28:39 +00001221/// requires directive:
1222/// annot_pragma_openmp 'requires' <clause> [[[,] <clause>] ... ]
1223/// annot_pragma_openmp_end
1224///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001225Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
1226 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
1227 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001228 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +00001229 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +00001230
Richard Smithaf3b3252017-05-18 19:21:48 +00001231 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001232 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001233
1234 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001235 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +00001236 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001237 DeclDirectiveListParserHelper Helper(this, DKind);
1238 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1239 /*AllowScopeSpecifier=*/true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001240 // The last seen token is annot_pragma_openmp_end - need to check for
1241 // extra tokens.
1242 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1243 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001244 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001245 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +00001246 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001247 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001248 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001249 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
1250 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +00001251 }
1252 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001253 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001254 case OMPD_allocate: {
1255 ConsumeToken();
1256 DeclDirectiveListParserHelper Helper(this, DKind);
1257 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1258 /*AllowScopeSpecifier=*/true)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001259 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001260 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001261 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1262 OMPC_unknown + 1>
1263 FirstClauses(OMPC_unknown + 1);
1264 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1265 OpenMPClauseKind CKind =
1266 Tok.isAnnotation() ? OMPC_unknown
1267 : getOpenMPClauseKind(PP.getSpelling(Tok));
1268 Actions.StartOpenMPClause(CKind);
1269 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1270 !FirstClauses[CKind].getInt());
1271 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1272 StopBeforeMatch);
1273 FirstClauses[CKind].setInt(true);
1274 if (Clause != nullptr)
1275 Clauses.push_back(Clause);
1276 if (Tok.is(tok::annot_pragma_openmp_end)) {
1277 Actions.EndOpenMPClause();
1278 break;
1279 }
1280 // Skip ',' if any.
1281 if (Tok.is(tok::comma))
1282 ConsumeToken();
1283 Actions.EndOpenMPClause();
1284 }
1285 // The last seen token is annot_pragma_openmp_end - need to check for
1286 // extra tokens.
1287 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1288 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1289 << getOpenMPDirectiveName(DKind);
1290 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1291 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001292 }
1293 // Skip the last annot_pragma_openmp_end.
1294 ConsumeAnnotationToken();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001295 return Actions.ActOnOpenMPAllocateDirective(Loc, Helper.getIdentifiers(),
1296 Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001297 }
1298 break;
1299 }
Kelvin Li1408f912018-09-26 04:28:39 +00001300 case OMPD_requires: {
1301 SourceLocation StartLoc = ConsumeToken();
1302 SmallVector<OMPClause *, 5> Clauses;
1303 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
1304 FirstClauses(OMPC_unknown + 1);
1305 if (Tok.is(tok::annot_pragma_openmp_end)) {
Ilya Biryukovff2a9972019-02-26 11:01:50 +00001306 Diag(Tok, diag::err_omp_expected_clause)
Kelvin Li1408f912018-09-26 04:28:39 +00001307 << getOpenMPDirectiveName(OMPD_requires);
1308 break;
1309 }
1310 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1311 OpenMPClauseKind CKind = Tok.isAnnotation()
1312 ? OMPC_unknown
1313 : getOpenMPClauseKind(PP.getSpelling(Tok));
1314 Actions.StartOpenMPClause(CKind);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001315 OMPClause *Clause = ParseOpenMPClause(OMPD_requires, CKind,
1316 !FirstClauses[CKind].getInt());
1317 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1318 StopBeforeMatch);
Kelvin Li1408f912018-09-26 04:28:39 +00001319 FirstClauses[CKind].setInt(true);
1320 if (Clause != nullptr)
1321 Clauses.push_back(Clause);
1322 if (Tok.is(tok::annot_pragma_openmp_end)) {
1323 Actions.EndOpenMPClause();
1324 break;
1325 }
1326 // Skip ',' if any.
1327 if (Tok.is(tok::comma))
1328 ConsumeToken();
1329 Actions.EndOpenMPClause();
1330 }
1331 // Consume final annot_pragma_openmp_end
1332 if (Clauses.size() == 0) {
1333 Diag(Tok, diag::err_omp_expected_clause)
1334 << getOpenMPDirectiveName(OMPD_requires);
1335 ConsumeAnnotationToken();
1336 return nullptr;
1337 }
1338 ConsumeAnnotationToken();
1339 return Actions.ActOnOpenMPRequiresDirective(StartLoc, Clauses);
1340 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001341 case OMPD_declare_reduction:
1342 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001343 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001344 // The last seen token is annot_pragma_openmp_end - need to check for
1345 // extra tokens.
1346 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1347 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1348 << getOpenMPDirectiveName(OMPD_declare_reduction);
1349 while (Tok.isNot(tok::annot_pragma_openmp_end))
1350 ConsumeAnyToken();
1351 }
1352 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001353 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001354 return Res;
1355 }
1356 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001357 case OMPD_declare_mapper: {
1358 ConsumeToken();
1359 if (DeclGroupPtrTy Res = ParseOpenMPDeclareMapperDirective(AS)) {
1360 // Skip the last annot_pragma_openmp_end.
1361 ConsumeAnnotationToken();
1362 return Res;
1363 }
1364 break;
1365 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001366 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001367 case OMPD_declare_simd: {
1368 // The syntax is:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001369 // { #pragma omp declare {simd|variant} }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001370 // <function-declaration-or-definition>
1371 //
Alexey Bataev2af33e32016-04-07 12:45:37 +00001372 CachedTokens Toks;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001373 Toks.push_back(Tok);
1374 ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001375 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
1376 Toks.push_back(Tok);
1377 ConsumeAnyToken();
1378 }
1379 Toks.push_back(Tok);
1380 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +00001381
1382 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +00001383 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001384 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +00001385 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001386 // Here we expect to see some function declaration.
1387 if (AS == AS_none) {
1388 assert(TagType == DeclSpec::TST_unspecified);
1389 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001390 ParsingDeclSpec PDS(*this);
1391 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1392 } else {
1393 Ptr =
1394 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1395 }
1396 }
1397 if (!Ptr) {
Alexey Bataevd158cf62019-09-13 20:18:17 +00001398 Diag(Loc, diag::err_omp_decl_in_declare_simd_variant)
1399 << (DKind == OMPD_declare_simd ? 0 : 1);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001400 return DeclGroupPtrTy();
1401 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001402 if (DKind == OMPD_declare_simd)
1403 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
1404 assert(DKind == OMPD_declare_variant &&
1405 "Expected declare variant directive only");
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001406 ParseOMPDeclareVariantClauses(Ptr, Toks, Loc);
1407 return Ptr;
Alexey Bataev587e1de2016-03-30 10:43:55 +00001408 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001409 case OMPD_declare_target: {
1410 SourceLocation DTLoc = ConsumeAnyToken();
1411 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Kelvin Lie0502752018-11-21 20:15:57 +00001412 return ParseOMPDeclareTargetClauses();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001413 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001414
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001415 // Skip the last annot_pragma_openmp_end.
1416 ConsumeAnyToken();
1417
1418 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
1419 return DeclGroupPtrTy();
1420
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001421 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +00001422 DKind = parseOpenMPDirectiveKind(*this);
Kelvin Libc38e632018-09-10 02:07:09 +00001423 while (DKind != OMPD_end_declare_target && Tok.isNot(tok::eof) &&
1424 Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +00001425 DeclGroupPtrTy Ptr;
1426 // Here we expect to see some function declaration.
1427 if (AS == AS_none) {
1428 assert(TagType == DeclSpec::TST_unspecified);
1429 MaybeParseCXX11Attributes(Attrs);
1430 ParsingDeclSpec PDS(*this);
1431 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1432 } else {
1433 Ptr =
1434 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1435 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001436 if (Ptr) {
1437 DeclGroupRef Ref = Ptr.get();
1438 Decls.append(Ref.begin(), Ref.end());
1439 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001440 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
1441 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +00001442 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001443 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001444 if (DKind != OMPD_end_declare_target)
1445 TPA.Revert();
1446 else
1447 TPA.Commit();
1448 }
1449 }
1450
Kelvin Lie0502752018-11-21 20:15:57 +00001451 ParseOMPEndDeclareTargetDirective(DKind, DTLoc);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001452 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +00001453 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001454 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001455 case OMPD_unknown:
1456 Diag(Tok, diag::err_omp_unknown_directive);
1457 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001458 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001459 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001460 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +00001461 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001462 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001463 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001464 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +00001465 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001466 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001467 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001468 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001469 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001470 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +00001471 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001472 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001473 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001474 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001475 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001476 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +00001477 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001478 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001479 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001480 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001481 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +00001482 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001483 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001484 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001485 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001486 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001487 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001488 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +00001489 case OMPD_master_taskloop:
Alexey Bataevb8552ab2019-10-18 16:47:35 +00001490 case OMPD_master_taskloop_simd:
Alexey Bataev5bbcead2019-10-14 17:17:41 +00001491 case OMPD_parallel_master_taskloop:
Alexey Bataev14a388f2019-10-25 10:27:13 -04001492 case OMPD_parallel_master_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001493 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001494 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001495 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001496 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +00001497 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001498 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001499 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001500 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001501 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +00001502 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +00001503 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001504 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +00001505 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +00001506 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +00001507 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +00001508 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +00001509 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +00001510 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001511 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +00001512 break;
1513 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001514 while (Tok.isNot(tok::annot_pragma_openmp_end))
1515 ConsumeAnyToken();
1516 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +00001517 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001518}
1519
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001520/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001521///
1522/// threadprivate-directive:
1523/// annot_pragma_openmp 'threadprivate' simple-variable-list
1524/// annot_pragma_openmp_end
1525///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001526/// allocate-directive:
1527/// annot_pragma_openmp 'allocate' simple-variable-list
1528/// annot_pragma_openmp_end
1529///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001530/// declare-reduction-directive:
1531/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
1532/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
1533/// ('omp_priv' '=' <expression>|<function_call>) ')']
1534/// annot_pragma_openmp_end
1535///
Michael Kruse251e1482019-02-01 20:25:04 +00001536/// declare-mapper-directive:
1537/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1538/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1539/// annot_pragma_openmp_end
1540///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001541/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001542/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001543/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
1544/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001545/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +00001546/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Alexey Bataev60e51c42019-10-10 20:13:02 +00001547/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' | 'master
Alexey Bataevb8552ab2019-10-18 16:47:35 +00001548/// taskloop' | 'master taskloop simd' | 'parallel master taskloop' |
Alexey Bataev14a388f2019-10-25 10:27:13 -04001549/// 'parallel master taskloop simd' | 'distribute' | 'target enter data'
1550/// | 'target exit data' | 'target parallel' | 'target parallel for' |
1551/// 'target update' | 'distribute parallel for' | 'distribute paralle
1552/// for simd' | 'distribute simd' | 'target parallel for simd' | 'target
1553/// simd' | 'teams distribute' | 'teams distribute simd' | 'teams
1554/// distribute parallel for simd' | 'teams distribute parallel for' |
1555/// 'target teams' | 'target teams distribute' | 'target teams
1556/// distribute parallel for' | 'target teams distribute parallel for
1557/// simd' | 'target teams distribute simd' {clause}
1558/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001559///
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001560StmtResult
1561Parser::ParseOpenMPDeclarativeOrExecutableDirective(ParsedStmtContext StmtCtx) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001562 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +00001563 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001564 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001565 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +00001566 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +00001567 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
1568 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +00001569 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +00001570 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001571 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001572 // Name of critical directive.
1573 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001574 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +00001575 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +00001576 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001577
1578 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001579 case OMPD_threadprivate: {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001580 // FIXME: Should this be permitted in C++?
1581 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1582 ParsedStmtContext()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001583 Diag(Tok, diag::err_omp_immediate_directive)
1584 << getOpenMPDirectiveName(DKind) << 0;
1585 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001586 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001587 DeclDirectiveListParserHelper Helper(this, DKind);
1588 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1589 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001590 // The last seen token is annot_pragma_openmp_end - need to check for
1591 // extra tokens.
1592 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1593 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001594 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001595 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001596 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001597 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
1598 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001599 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1600 }
Alp Tokerd751fa72013-12-18 19:10:49 +00001601 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001602 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001603 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001604 case OMPD_allocate: {
1605 // FIXME: Should this be permitted in C++?
1606 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1607 ParsedStmtContext()) {
1608 Diag(Tok, diag::err_omp_immediate_directive)
1609 << getOpenMPDirectiveName(DKind) << 0;
1610 }
1611 ConsumeToken();
1612 DeclDirectiveListParserHelper Helper(this, DKind);
1613 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1614 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001615 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001616 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001617 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1618 OMPC_unknown + 1>
1619 FirstClauses(OMPC_unknown + 1);
1620 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1621 OpenMPClauseKind CKind =
1622 Tok.isAnnotation() ? OMPC_unknown
1623 : getOpenMPClauseKind(PP.getSpelling(Tok));
1624 Actions.StartOpenMPClause(CKind);
1625 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1626 !FirstClauses[CKind].getInt());
1627 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1628 StopBeforeMatch);
1629 FirstClauses[CKind].setInt(true);
1630 if (Clause != nullptr)
1631 Clauses.push_back(Clause);
1632 if (Tok.is(tok::annot_pragma_openmp_end)) {
1633 Actions.EndOpenMPClause();
1634 break;
1635 }
1636 // Skip ',' if any.
1637 if (Tok.is(tok::comma))
1638 ConsumeToken();
1639 Actions.EndOpenMPClause();
1640 }
1641 // The last seen token is annot_pragma_openmp_end - need to check for
1642 // extra tokens.
1643 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1644 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1645 << getOpenMPDirectiveName(DKind);
1646 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1647 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001648 }
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001649 DeclGroupPtrTy Res = Actions.ActOnOpenMPAllocateDirective(
1650 Loc, Helper.getIdentifiers(), Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001651 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1652 }
1653 SkipUntil(tok::annot_pragma_openmp_end);
1654 break;
1655 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001656 case OMPD_declare_reduction:
1657 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001658 if (DeclGroupPtrTy Res =
1659 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001660 // The last seen token is annot_pragma_openmp_end - need to check for
1661 // extra tokens.
1662 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1663 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1664 << getOpenMPDirectiveName(OMPD_declare_reduction);
1665 while (Tok.isNot(tok::annot_pragma_openmp_end))
1666 ConsumeAnyToken();
1667 }
1668 ConsumeAnyToken();
1669 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +00001670 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001671 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +00001672 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001673 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001674 case OMPD_declare_mapper: {
1675 ConsumeToken();
1676 if (DeclGroupPtrTy Res =
1677 ParseOpenMPDeclareMapperDirective(/*AS=*/AS_none)) {
1678 // Skip the last annot_pragma_openmp_end.
1679 ConsumeAnnotationToken();
1680 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1681 } else {
1682 SkipUntil(tok::annot_pragma_openmp_end);
1683 }
1684 break;
1685 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001686 case OMPD_flush:
1687 if (PP.LookAhead(0).is(tok::l_paren)) {
1688 FlushHasClause = true;
1689 // Push copy of the current token back to stream to properly parse
1690 // pseudo-clause OMPFlushClause.
Ilya Biryukov929af672019-05-17 09:32:05 +00001691 PP.EnterToken(Tok, /*IsReinject*/ true);
Alexey Bataev6125da92014-07-21 11:26:11 +00001692 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001693 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +00001694 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001695 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001696 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001697 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001698 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001699 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001700 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +00001701 case OMPD_target_update:
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001702 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1703 ParsedStmtContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00001704 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +00001705 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +00001706 }
1707 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001708 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001709 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001710 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001711 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001712 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001713 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001714 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001715 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001716 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001717 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001718 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001719 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001720 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001721 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001722 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001723 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001724 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001725 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001726 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001727 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001728 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001729 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001730 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001731 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001732 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +00001733 case OMPD_master_taskloop:
Alexey Bataevb8552ab2019-10-18 16:47:35 +00001734 case OMPD_master_taskloop_simd:
Alexey Bataev5bbcead2019-10-14 17:17:41 +00001735 case OMPD_parallel_master_taskloop:
Alexey Bataev14a388f2019-10-25 10:27:13 -04001736 case OMPD_parallel_master_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001737 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001738 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001739 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001740 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001741 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001742 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001743 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001744 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001745 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001746 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001747 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001748 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001749 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001750 case OMPD_target_teams_distribute_parallel_for_simd:
1751 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001752 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001753 // Parse directive name of the 'critical' directive if any.
1754 if (DKind == OMPD_critical) {
1755 BalancedDelimiterTracker T(*this, tok::l_paren,
1756 tok::annot_pragma_openmp_end);
1757 if (!T.consumeOpen()) {
1758 if (Tok.isAnyIdentifier()) {
1759 DirName =
1760 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1761 ConsumeAnyToken();
1762 } else {
1763 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1764 }
1765 T.consumeClose();
1766 }
Alexey Bataev80909872015-07-02 11:25:17 +00001767 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001768 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001769 if (Tok.isNot(tok::annot_pragma_openmp_end))
1770 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001771 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001772
Alexey Bataevf29276e2014-06-18 04:14:57 +00001773 if (isOpenMPLoopDirective(DKind))
1774 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1775 if (isOpenMPSimdDirective(DKind))
1776 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1777 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001778 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001779
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001780 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001781 OpenMPClauseKind CKind =
1782 Tok.isAnnotation()
1783 ? OMPC_unknown
1784 : FlushHasClause ? OMPC_flush
1785 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001786 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001787 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001788 OMPClause *Clause =
1789 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001790 FirstClauses[CKind].setInt(true);
1791 if (Clause) {
1792 FirstClauses[CKind].setPointer(Clause);
1793 Clauses.push_back(Clause);
1794 }
1795
1796 // Skip ',' if any.
1797 if (Tok.is(tok::comma))
1798 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001799 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001800 }
1801 // End location of the directive.
1802 EndLoc = Tok.getLocation();
1803 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001804 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001805
Alexey Bataeveb482352015-12-18 05:05:56 +00001806 // OpenMP [2.13.8, ordered Construct, Syntax]
1807 // If the depend clause is specified, the ordered construct is a stand-alone
1808 // directive.
1809 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001810 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1811 ParsedStmtContext()) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001812 Diag(Loc, diag::err_omp_immediate_directive)
1813 << getOpenMPDirectiveName(DKind) << 1
1814 << getOpenMPClauseName(OMPC_depend);
1815 }
1816 HasAssociatedStatement = false;
1817 }
1818
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001819 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001820 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001821 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001822 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001823 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1824 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1825 // should have at least one compound statement scope within it.
1826 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001827 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001828 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1829 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001830 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001831 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1832 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1833 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001834 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001835 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001836 Directive = Actions.ActOnOpenMPExecutableDirective(
1837 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1838 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001839
1840 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001841 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001842 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001843 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001844 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001845 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001846 case OMPD_declare_target:
1847 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00001848 case OMPD_requires:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001849 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001850 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001851 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001852 SkipUntil(tok::annot_pragma_openmp_end);
1853 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001854 case OMPD_unknown:
1855 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001856 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001857 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001858 }
1859 return Directive;
1860}
1861
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001862// Parses simple list:
1863// simple-variable-list:
1864// '(' id-expression {, id-expression} ')'
1865//
1866bool Parser::ParseOpenMPSimpleVarList(
1867 OpenMPDirectiveKind Kind,
1868 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1869 Callback,
1870 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001871 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001872 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001873 if (T.expectAndConsume(diag::err_expected_lparen_after,
1874 getOpenMPDirectiveName(Kind)))
1875 return true;
1876 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001877 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001878
1879 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001880 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001881 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001882 UnqualifiedId Name;
1883 // Read var name.
1884 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001885 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001886
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001887 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001888 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001889 IsCorrect = false;
1890 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001891 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001892 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00001893 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001894 IsCorrect = false;
1895 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001896 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001897 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1898 Tok.isNot(tok::annot_pragma_openmp_end)) {
1899 IsCorrect = false;
1900 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001901 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001902 Diag(PrevTok.getLocation(), diag::err_expected)
1903 << tok::identifier
1904 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001905 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001906 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001907 }
1908 // Consume ','.
1909 if (Tok.is(tok::comma)) {
1910 ConsumeToken();
1911 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001912 }
1913
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001914 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001915 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001916 IsCorrect = false;
1917 }
1918
1919 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001920 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001921
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001922 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001923}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001924
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001925/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001926///
1927/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001928/// if-clause | final-clause | num_threads-clause | safelen-clause |
1929/// default-clause | private-clause | firstprivate-clause | shared-clause
1930/// | linear-clause | aligned-clause | collapse-clause |
1931/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001932/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001933/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001934/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001935/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001936/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001937/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001938/// from-clause | is_device_ptr-clause | task_reduction-clause |
Alexey Bataeve04483e2019-03-27 14:14:31 +00001939/// in_reduction-clause | allocator-clause | allocate-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001940///
1941OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1942 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001943 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001944 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001945 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001946 // Check if clause is allowed for the given directive.
1947 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001948 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1949 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001950 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001951 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001952 }
1953
1954 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001955 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001956 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001957 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001958 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001959 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001960 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001961 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001962 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001963 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001964 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001965 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001966 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001967 case OMPC_hint:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001968 case OMPC_allocator:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001969 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001970 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001971 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001972 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001973 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001974 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001975 // OpenMP [2.9.1, target data construct, Restrictions]
1976 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001977 // OpenMP [2.11.1, task Construct, Restrictions]
1978 // At most one if clause can appear on the directive.
1979 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001980 // OpenMP [teams Construct, Restrictions]
1981 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001982 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001983 // OpenMP [2.9.1, task Construct, Restrictions]
1984 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001985 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1986 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001987 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1988 // At most one num_tasks clause can appear on the directive.
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001989 // OpenMP [2.11.3, allocate Directive, Restrictions]
1990 // At most one allocator clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001991 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001992 Diag(Tok, diag::err_omp_more_one_clause)
1993 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001994 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001995 }
1996
Alexey Bataev10e775f2015-07-30 11:36:16 +00001997 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001998 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001999 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002000 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002001 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002002 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002003 case OMPC_proc_bind:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00002004 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002005 // OpenMP [2.14.3.1, Restrictions]
2006 // Only a single default clause may be specified on a parallel, task or
2007 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002008 // OpenMP [2.5, parallel Construct, Restrictions]
2009 // At most one proc_bind clause can appear on the directive.
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00002010 // OpenMP [5.0, Requires directive, Restrictions]
2011 // At most one atomic_default_mem_order clause can appear
2012 // on the directive
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002013 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002014 Diag(Tok, diag::err_omp_more_one_clause)
2015 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002016 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002017 }
2018
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002019 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002020 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002021 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00002022 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002023 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002024 // OpenMP [2.7.1, Restrictions, p. 3]
2025 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002026 // OpenMP [2.10.4, Restrictions, p. 106]
2027 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00002028 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002029 Diag(Tok, diag::err_omp_more_one_clause)
2030 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002031 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002032 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00002033 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002034
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002035 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002036 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002037 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002038 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002039 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002040 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002041 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002042 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00002043 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00002044 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002045 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00002046 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002047 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00002048 case OMPC_nogroup:
Kelvin Li1408f912018-09-26 04:28:39 +00002049 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00002050 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00002051 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00002052 case OMPC_dynamic_allocators:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002053 // OpenMP [2.7.1, Restrictions, p. 9]
2054 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00002055 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
2056 // Only one nowait clause can appear on a for directive.
Kelvin Li1408f912018-09-26 04:28:39 +00002057 // OpenMP [5.0, Requires directive, Restrictions]
2058 // Each of the requires clauses can appear at most once on the directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002059 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002060 Diag(Tok, diag::err_omp_more_one_clause)
2061 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002062 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002063 }
2064
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002065 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002066 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002067 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002068 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002069 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002070 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002071 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00002072 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00002073 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002074 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002075 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002076 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002077 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002078 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002079 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00002080 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00002081 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00002082 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00002083 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00002084 case OMPC_is_device_ptr:
Alexey Bataeve04483e2019-03-27 14:14:31 +00002085 case OMPC_allocate:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002086 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002087 break;
Alexey Bataev729e2422019-08-23 16:11:14 +00002088 case OMPC_device_type:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002089 case OMPC_unknown:
2090 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00002091 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00002092 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002093 break;
2094 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002095 case OMPC_uniform:
Alexey Bataevdba792c2019-09-23 18:13:31 +00002096 case OMPC_match:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002097 if (!WrongDirective)
2098 Diag(Tok, diag::err_omp_unexpected_clause)
2099 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00002100 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002101 break;
2102 }
Craig Topper161e4db2014-05-21 06:02:52 +00002103 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002104}
2105
Alexey Bataev2af33e32016-04-07 12:45:37 +00002106/// Parses simple expression in parens for single-expression clauses of OpenMP
2107/// constructs.
2108/// \param RLoc Returned location of right paren.
2109ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
Alexey Bataevd158cf62019-09-13 20:18:17 +00002110 SourceLocation &RLoc,
2111 bool IsAddressOfOperand) {
Alexey Bataev2af33e32016-04-07 12:45:37 +00002112 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2113 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
2114 return ExprError();
2115
2116 SourceLocation ELoc = Tok.getLocation();
2117 ExprResult LHS(ParseCastExpression(
Alexey Bataevd158cf62019-09-13 20:18:17 +00002118 /*isUnaryExpression=*/false, IsAddressOfOperand, NotTypeCast));
Alexey Bataev2af33e32016-04-07 12:45:37 +00002119 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002120 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev2af33e32016-04-07 12:45:37 +00002121
2122 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002123 RLoc = Tok.getLocation();
2124 if (!T.consumeClose())
2125 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00002126
Alexey Bataev2af33e32016-04-07 12:45:37 +00002127 return Val;
2128}
2129
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002130/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00002131/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00002132/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002133///
Alexey Bataev3778b602014-07-17 07:32:53 +00002134/// final-clause:
2135/// 'final' '(' expression ')'
2136///
Alexey Bataev62c87d22014-03-21 04:51:18 +00002137/// num_threads-clause:
2138/// 'num_threads' '(' expression ')'
2139///
2140/// safelen-clause:
2141/// 'safelen' '(' expression ')'
2142///
Alexey Bataev66b15b52015-08-21 11:14:16 +00002143/// simdlen-clause:
2144/// 'simdlen' '(' expression ')'
2145///
Alexander Musman8bd31e62014-05-27 15:12:19 +00002146/// collapse-clause:
2147/// 'collapse' '(' expression ')'
2148///
Alexey Bataeva0569352015-12-01 10:17:31 +00002149/// priority-clause:
2150/// 'priority' '(' expression ')'
2151///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002152/// grainsize-clause:
2153/// 'grainsize' '(' expression ')'
2154///
Alexey Bataev382967a2015-12-08 12:06:20 +00002155/// num_tasks-clause:
2156/// 'num_tasks' '(' expression ')'
2157///
Alexey Bataev28c75412015-12-15 08:19:24 +00002158/// hint-clause:
2159/// 'hint' '(' expression ')'
2160///
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002161/// allocator-clause:
2162/// 'allocator' '(' expression ')'
2163///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002164OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
2165 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002166 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00002167 SourceLocation LLoc = Tok.getLocation();
2168 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002169
Alexey Bataev2af33e32016-04-07 12:45:37 +00002170 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002171
2172 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00002173 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002174
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002175 if (ParseOnly)
2176 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00002177 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002178}
2179
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002180/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002181///
2182/// default-clause:
2183/// 'default' '(' 'none' | 'shared' ')
2184///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002185/// proc_bind-clause:
2186/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
2187///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002188OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
2189 bool ParseOnly) {
Alexey Bataev729e2422019-08-23 16:11:14 +00002190 llvm::Optional<SimpleClauseData> Val = parseOpenMPSimpleClause(*this, Kind);
2191 if (!Val || ParseOnly)
Craig Topper161e4db2014-05-21 06:02:52 +00002192 return nullptr;
Alexey Bataev729e2422019-08-23 16:11:14 +00002193 return Actions.ActOnOpenMPSimpleClause(
2194 Kind, Val.getValue().Type, Val.getValue().TypeLoc, Val.getValue().LOpen,
2195 Val.getValue().Loc, Val.getValue().RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002196}
2197
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002198/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002199///
2200/// ordered-clause:
2201/// 'ordered'
2202///
Alexey Bataev236070f2014-06-20 11:19:47 +00002203/// nowait-clause:
2204/// 'nowait'
2205///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002206/// untied-clause:
2207/// 'untied'
2208///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002209/// mergeable-clause:
2210/// 'mergeable'
2211///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002212/// read-clause:
2213/// 'read'
2214///
Alexey Bataev346265e2015-09-25 10:37:12 +00002215/// threads-clause:
2216/// 'threads'
2217///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002218/// simd-clause:
2219/// 'simd'
2220///
Alexey Bataevb825de12015-12-07 10:51:44 +00002221/// nogroup-clause:
2222/// 'nogroup'
2223///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002224OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002225 SourceLocation Loc = Tok.getLocation();
2226 ConsumeAnyToken();
2227
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002228 if (ParseOnly)
2229 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002230 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
2231}
2232
2233
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002234/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00002235/// argument like 'schedule' or 'dist_schedule'.
2236///
2237/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00002238/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
2239/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00002240///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002241/// if-clause:
2242/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
2243///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002244/// defaultmap:
2245/// 'defaultmap' '(' modifier ':' kind ')'
2246///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002247OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
2248 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00002249 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002250 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002251 // Parse '('.
2252 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2253 if (T.expectAndConsume(diag::err_expected_lparen_after,
2254 getOpenMPClauseName(Kind)))
2255 return nullptr;
2256
2257 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002258 SmallVector<unsigned, 4> Arg;
2259 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002260 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00002261 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
2262 Arg.resize(NumberOfElements);
2263 KLoc.resize(NumberOfElements);
2264 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
2265 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
2266 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00002267 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002268 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00002269 if (KindModifier > OMPC_SCHEDULE_unknown) {
2270 // Parse 'modifier'
2271 Arg[Modifier1] = KindModifier;
2272 KLoc[Modifier1] = Tok.getLocation();
2273 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2274 Tok.isNot(tok::annot_pragma_openmp_end))
2275 ConsumeAnyToken();
2276 if (Tok.is(tok::comma)) {
2277 // Parse ',' 'modifier'
2278 ConsumeAnyToken();
2279 KindModifier = getOpenMPSimpleClauseType(
2280 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2281 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
2282 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00002283 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002284 KLoc[Modifier2] = Tok.getLocation();
2285 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2286 Tok.isNot(tok::annot_pragma_openmp_end))
2287 ConsumeAnyToken();
2288 }
2289 // Parse ':'
2290 if (Tok.is(tok::colon))
2291 ConsumeAnyToken();
2292 else
2293 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
2294 KindModifier = getOpenMPSimpleClauseType(
2295 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2296 }
2297 Arg[ScheduleKind] = KindModifier;
2298 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002299 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2300 Tok.isNot(tok::annot_pragma_openmp_end))
2301 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00002302 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
2303 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
2304 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002305 Tok.is(tok::comma))
2306 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00002307 } else if (Kind == OMPC_dist_schedule) {
2308 Arg.push_back(getOpenMPSimpleClauseType(
2309 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2310 KLoc.push_back(Tok.getLocation());
2311 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2312 Tok.isNot(tok::annot_pragma_openmp_end))
2313 ConsumeAnyToken();
2314 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
2315 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002316 } else if (Kind == OMPC_defaultmap) {
2317 // Get a defaultmap modifier
2318 Arg.push_back(getOpenMPSimpleClauseType(
2319 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2320 KLoc.push_back(Tok.getLocation());
2321 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2322 Tok.isNot(tok::annot_pragma_openmp_end))
2323 ConsumeAnyToken();
2324 // Parse ':'
2325 if (Tok.is(tok::colon))
2326 ConsumeAnyToken();
2327 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
2328 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
2329 // Get a defaultmap kind
2330 Arg.push_back(getOpenMPSimpleClauseType(
2331 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2332 KLoc.push_back(Tok.getLocation());
2333 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2334 Tok.isNot(tok::annot_pragma_openmp_end))
2335 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002336 } else {
2337 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00002338 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002339 TentativeParsingAction TPA(*this);
Alexey Bataev61908f652018-04-23 19:53:05 +00002340 Arg.push_back(parseOpenMPDirectiveKind(*this));
Alexey Bataev6402bca2015-12-28 07:25:51 +00002341 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002342 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002343 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
2344 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002345 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002346 } else {
2347 TPA.Revert();
2348 Arg.back() = OMPD_unknown;
2349 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002350 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002351 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00002352 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002353 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00002354
Carlo Bertollib4adf552016-01-15 18:50:31 +00002355 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
2356 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
2357 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002358 if (NeedAnExpression) {
2359 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002360 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
2361 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002362 Val =
2363 Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002364 }
2365
2366 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002367 SourceLocation RLoc = Tok.getLocation();
2368 if (!T.consumeClose())
2369 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002370
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002371 if (NeedAnExpression && Val.isInvalid())
2372 return nullptr;
2373
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002374 if (ParseOnly)
2375 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002376 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002377 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002378}
2379
Alexey Bataevc5e02582014-06-16 07:08:35 +00002380static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
2381 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002382 if (ReductionIdScopeSpec.isEmpty()) {
2383 auto OOK = OO_None;
2384 switch (P.getCurToken().getKind()) {
2385 case tok::plus:
2386 OOK = OO_Plus;
2387 break;
2388 case tok::minus:
2389 OOK = OO_Minus;
2390 break;
2391 case tok::star:
2392 OOK = OO_Star;
2393 break;
2394 case tok::amp:
2395 OOK = OO_Amp;
2396 break;
2397 case tok::pipe:
2398 OOK = OO_Pipe;
2399 break;
2400 case tok::caret:
2401 OOK = OO_Caret;
2402 break;
2403 case tok::ampamp:
2404 OOK = OO_AmpAmp;
2405 break;
2406 case tok::pipepipe:
2407 OOK = OO_PipePipe;
2408 break;
2409 default:
2410 break;
2411 }
2412 if (OOK != OO_None) {
2413 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00002414 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00002415 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
2416 return false;
2417 }
2418 }
2419 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
2420 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00002421 /*AllowConstructorName*/ false,
2422 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00002423 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002424}
2425
Kelvin Lief579432018-12-18 22:18:41 +00002426/// Checks if the token is a valid map-type-modifier.
2427static OpenMPMapModifierKind isMapModifier(Parser &P) {
2428 Token Tok = P.getCurToken();
2429 if (!Tok.is(tok::identifier))
2430 return OMPC_MAP_MODIFIER_unknown;
2431
2432 Preprocessor &PP = P.getPreprocessor();
2433 OpenMPMapModifierKind TypeModifier = static_cast<OpenMPMapModifierKind>(
2434 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2435 return TypeModifier;
2436}
2437
Michael Kruse01f670d2019-02-22 22:29:42 +00002438/// Parse the mapper modifier in map, to, and from clauses.
2439bool Parser::parseMapperModifier(OpenMPVarListDataTy &Data) {
2440 // Parse '('.
2441 BalancedDelimiterTracker T(*this, tok::l_paren, tok::colon);
2442 if (T.expectAndConsume(diag::err_expected_lparen_after, "mapper")) {
2443 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2444 StopBeforeMatch);
2445 return true;
2446 }
2447 // Parse mapper-identifier
2448 if (getLangOpts().CPlusPlus)
2449 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
2450 /*ObjectType=*/nullptr,
2451 /*EnteringContext=*/false);
2452 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
2453 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
2454 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2455 StopBeforeMatch);
2456 return true;
2457 }
2458 auto &DeclNames = Actions.getASTContext().DeclarationNames;
2459 Data.ReductionOrMapperId = DeclarationNameInfo(
2460 DeclNames.getIdentifier(Tok.getIdentifierInfo()), Tok.getLocation());
2461 ConsumeToken();
2462 // Parse ')'.
2463 return T.consumeClose();
2464}
2465
Kelvin Lief579432018-12-18 22:18:41 +00002466/// Parse map-type-modifiers in map clause.
2467/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002468/// where, map-type-modifier ::= always | close | mapper(mapper-identifier)
2469bool Parser::parseMapTypeModifiers(OpenMPVarListDataTy &Data) {
2470 while (getCurToken().isNot(tok::colon)) {
2471 OpenMPMapModifierKind TypeModifier = isMapModifier(*this);
Kelvin Lief579432018-12-18 22:18:41 +00002472 if (TypeModifier == OMPC_MAP_MODIFIER_always ||
2473 TypeModifier == OMPC_MAP_MODIFIER_close) {
2474 Data.MapTypeModifiers.push_back(TypeModifier);
2475 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
Michael Kruse4304e9d2019-02-19 16:38:20 +00002476 ConsumeToken();
2477 } else if (TypeModifier == OMPC_MAP_MODIFIER_mapper) {
2478 Data.MapTypeModifiers.push_back(TypeModifier);
2479 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
2480 ConsumeToken();
Michael Kruse01f670d2019-02-22 22:29:42 +00002481 if (parseMapperModifier(Data))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002482 return true;
Kelvin Lief579432018-12-18 22:18:41 +00002483 } else {
2484 // For the case of unknown map-type-modifier or a map-type.
2485 // Map-type is followed by a colon; the function returns when it
2486 // encounters a token followed by a colon.
2487 if (Tok.is(tok::comma)) {
Michael Kruse4304e9d2019-02-19 16:38:20 +00002488 Diag(Tok, diag::err_omp_map_type_modifier_missing);
2489 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002490 continue;
2491 }
2492 // Potential map-type token as it is followed by a colon.
2493 if (PP.LookAhead(0).is(tok::colon))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002494 return false;
2495 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
2496 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002497 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002498 if (getCurToken().is(tok::comma))
2499 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002500 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002501 return false;
Kelvin Lief579432018-12-18 22:18:41 +00002502}
2503
2504/// Checks if the token is a valid map-type.
2505static OpenMPMapClauseKind isMapType(Parser &P) {
2506 Token Tok = P.getCurToken();
2507 // The map-type token can be either an identifier or the C++ delete keyword.
2508 if (!Tok.isOneOf(tok::identifier, tok::kw_delete))
2509 return OMPC_MAP_unknown;
2510 Preprocessor &PP = P.getPreprocessor();
2511 OpenMPMapClauseKind MapType = static_cast<OpenMPMapClauseKind>(
2512 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2513 return MapType;
2514}
2515
2516/// Parse map-type in map clause.
2517/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002518/// where, map-type ::= to | from | tofrom | alloc | release | delete
Kelvin Lief579432018-12-18 22:18:41 +00002519static void parseMapType(Parser &P, Parser::OpenMPVarListDataTy &Data) {
2520 Token Tok = P.getCurToken();
2521 if (Tok.is(tok::colon)) {
2522 P.Diag(Tok, diag::err_omp_map_type_missing);
2523 return;
2524 }
2525 Data.MapType = isMapType(P);
2526 if (Data.MapType == OMPC_MAP_unknown)
2527 P.Diag(Tok, diag::err_omp_unknown_map_type);
2528 P.ConsumeToken();
2529}
2530
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002531/// Parses clauses with list.
2532bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
2533 OpenMPClauseKind Kind,
2534 SmallVectorImpl<Expr *> &Vars,
2535 OpenMPVarListDataTy &Data) {
2536 UnqualifiedId UnqualifiedReductionId;
2537 bool InvalidReductionId = false;
Michael Kruse01f670d2019-02-22 22:29:42 +00002538 bool IsInvalidMapperModifier = false;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002539
2540 // Parse '('.
2541 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2542 if (T.expectAndConsume(diag::err_expected_lparen_after,
2543 getOpenMPClauseName(Kind)))
2544 return true;
2545
2546 bool NeedRParenForLinear = false;
2547 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
2548 tok::annot_pragma_openmp_end);
2549 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00002550 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
2551 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002552 ColonProtectionRAIIObject ColonRAII(*this);
2553 if (getLangOpts().CPlusPlus)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002554 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002555 /*ObjectType=*/nullptr,
2556 /*EnteringContext=*/false);
Michael Kruse4304e9d2019-02-19 16:38:20 +00002557 InvalidReductionId = ParseReductionId(
2558 *this, Data.ReductionOrMapperIdScopeSpec, UnqualifiedReductionId);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002559 if (InvalidReductionId) {
2560 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2561 StopBeforeMatch);
2562 }
2563 if (Tok.is(tok::colon))
2564 Data.ColonLoc = ConsumeToken();
2565 else
2566 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
2567 if (!InvalidReductionId)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002568 Data.ReductionOrMapperId =
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002569 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
2570 } else if (Kind == OMPC_depend) {
2571 // Handle dependency type for depend clause.
2572 ColonProtectionRAIIObject ColonRAII(*this);
2573 Data.DepKind =
2574 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
2575 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
2576 Data.DepLinMapLoc = Tok.getLocation();
2577
2578 if (Data.DepKind == OMPC_DEPEND_unknown) {
2579 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2580 StopBeforeMatch);
2581 } else {
2582 ConsumeToken();
2583 // Special processing for depend(source) clause.
2584 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
2585 // Parse ')'.
2586 T.consumeClose();
2587 return false;
2588 }
2589 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002590 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002591 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00002592 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002593 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
2594 : diag::warn_pragma_expected_colon)
2595 << "dependency type";
2596 }
2597 } else if (Kind == OMPC_linear) {
2598 // Try to parse modifier if any.
2599 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
2600 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
2601 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2602 Data.DepLinMapLoc = ConsumeToken();
2603 LinearT.consumeOpen();
2604 NeedRParenForLinear = true;
2605 }
2606 } else if (Kind == OMPC_map) {
2607 // Handle map type for map clause.
2608 ColonProtectionRAIIObject ColonRAII(*this);
2609
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002610 // The first identifier may be a list item, a map-type or a
Kelvin Lief579432018-12-18 22:18:41 +00002611 // map-type-modifier. The map-type can also be delete which has the same
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002612 // spelling of the C++ delete keyword.
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002613 Data.DepLinMapLoc = Tok.getLocation();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002614
Kelvin Lief579432018-12-18 22:18:41 +00002615 // Check for presence of a colon in the map clause.
2616 TentativeParsingAction TPA(*this);
2617 bool ColonPresent = false;
2618 if (SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2619 StopBeforeMatch)) {
2620 if (Tok.is(tok::colon))
2621 ColonPresent = true;
2622 }
2623 TPA.Revert();
2624 // Only parse map-type-modifier[s] and map-type if a colon is present in
2625 // the map clause.
2626 if (ColonPresent) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002627 IsInvalidMapperModifier = parseMapTypeModifiers(Data);
2628 if (!IsInvalidMapperModifier)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002629 parseMapType(*this, Data);
Michael Kruse01f670d2019-02-22 22:29:42 +00002630 else
2631 SkipUntil(tok::colon, tok::annot_pragma_openmp_end, StopBeforeMatch);
Kelvin Lief579432018-12-18 22:18:41 +00002632 }
2633 if (Data.MapType == OMPC_MAP_unknown) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002634 Data.MapType = OMPC_MAP_tofrom;
2635 Data.IsMapTypeImplicit = true;
2636 }
2637
2638 if (Tok.is(tok::colon))
2639 Data.ColonLoc = ConsumeToken();
Michael Kruse0336c752019-02-25 20:34:15 +00002640 } else if (Kind == OMPC_to || Kind == OMPC_from) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002641 if (Tok.is(tok::identifier)) {
2642 bool IsMapperModifier = false;
Michael Kruse0336c752019-02-25 20:34:15 +00002643 if (Kind == OMPC_to) {
2644 auto Modifier = static_cast<OpenMPToModifierKind>(
2645 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2646 if (Modifier == OMPC_TO_MODIFIER_mapper)
2647 IsMapperModifier = true;
2648 } else {
2649 auto Modifier = static_cast<OpenMPFromModifierKind>(
2650 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2651 if (Modifier == OMPC_FROM_MODIFIER_mapper)
2652 IsMapperModifier = true;
2653 }
Michael Kruse01f670d2019-02-22 22:29:42 +00002654 if (IsMapperModifier) {
2655 // Parse the mapper modifier.
2656 ConsumeToken();
2657 IsInvalidMapperModifier = parseMapperModifier(Data);
2658 if (Tok.isNot(tok::colon)) {
2659 if (!IsInvalidMapperModifier)
2660 Diag(Tok, diag::warn_pragma_expected_colon) << ")";
2661 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2662 StopBeforeMatch);
2663 }
2664 // Consume ':'.
2665 if (Tok.is(tok::colon))
2666 ConsumeToken();
2667 }
2668 }
Alexey Bataeve04483e2019-03-27 14:14:31 +00002669 } else if (Kind == OMPC_allocate) {
2670 // Handle optional allocator expression followed by colon delimiter.
2671 ColonProtectionRAIIObject ColonRAII(*this);
2672 TentativeParsingAction TPA(*this);
2673 ExprResult Tail =
2674 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
2675 Tail = Actions.ActOnFinishFullExpr(Tail.get(), T.getOpenLocation(),
2676 /*DiscardedValue=*/false);
2677 if (Tail.isUsable()) {
2678 if (Tok.is(tok::colon)) {
2679 Data.TailExpr = Tail.get();
2680 Data.ColonLoc = ConsumeToken();
2681 TPA.Commit();
2682 } else {
2683 // colon not found, no allocator specified, parse only list of
2684 // variables.
2685 TPA.Revert();
2686 }
2687 } else {
2688 // Parsing was unsuccessfull, revert and skip to the end of clause or
2689 // directive.
2690 TPA.Revert();
2691 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2692 StopBeforeMatch);
2693 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002694 }
2695
Alexey Bataevfa312f32017-07-21 18:48:21 +00002696 bool IsComma =
2697 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
2698 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
2699 (Kind == OMPC_reduction && !InvalidReductionId) ||
Kelvin Lida6bc702018-11-21 19:38:53 +00002700 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown) ||
Alexey Bataevfa312f32017-07-21 18:48:21 +00002701 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002702 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
2703 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
2704 Tok.isNot(tok::annot_pragma_openmp_end))) {
2705 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
2706 // Parse variable
2707 ExprResult VarExpr =
2708 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00002709 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002710 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00002711 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002712 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2713 StopBeforeMatch);
2714 }
2715 // Skip ',' if any
2716 IsComma = Tok.is(tok::comma);
2717 if (IsComma)
2718 ConsumeToken();
2719 else if (Tok.isNot(tok::r_paren) &&
2720 Tok.isNot(tok::annot_pragma_openmp_end) &&
2721 (!MayHaveTail || Tok.isNot(tok::colon)))
2722 Diag(Tok, diag::err_omp_expected_punc)
2723 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
2724 : getOpenMPClauseName(Kind))
2725 << (Kind == OMPC_flush);
2726 }
2727
2728 // Parse ')' for linear clause with modifier.
2729 if (NeedRParenForLinear)
2730 LinearT.consumeClose();
2731
2732 // Parse ':' linear-step (or ':' alignment).
2733 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
2734 if (MustHaveTail) {
2735 Data.ColonLoc = Tok.getLocation();
2736 SourceLocation ELoc = ConsumeToken();
2737 ExprResult Tail = ParseAssignmentExpression();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002738 Tail =
2739 Actions.ActOnFinishFullExpr(Tail.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002740 if (Tail.isUsable())
2741 Data.TailExpr = Tail.get();
2742 else
2743 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2744 StopBeforeMatch);
2745 }
2746
2747 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002748 Data.RLoc = Tok.getLocation();
2749 if (!T.consumeClose())
2750 Data.RLoc = T.getCloseLocation();
Alexey Bataev61908f652018-04-23 19:53:05 +00002751 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
2752 Vars.empty()) ||
2753 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
Michael Kruse4304e9d2019-02-19 16:38:20 +00002754 (MustHaveTail && !Data.TailExpr) || InvalidReductionId ||
Michael Kruse01f670d2019-02-22 22:29:42 +00002755 IsInvalidMapperModifier;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002756}
2757
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002758/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00002759/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
2760/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002761///
2762/// private-clause:
2763/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002764/// firstprivate-clause:
2765/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00002766/// lastprivate-clause:
2767/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00002768/// shared-clause:
2769/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00002770/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00002771/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002772/// aligned-clause:
2773/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00002774/// reduction-clause:
2775/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00002776/// task_reduction-clause:
2777/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00002778/// in_reduction-clause:
2779/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00002780/// copyprivate-clause:
2781/// 'copyprivate' '(' list ')'
2782/// flush-clause:
2783/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002784/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00002785/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00002786/// map-clause:
Kelvin Lief579432018-12-18 22:18:41 +00002787/// 'map' '(' [ [ always [,] ] [ close [,] ]
Michael Kruse01f670d2019-02-22 22:29:42 +00002788/// [ mapper '(' mapper-identifier ')' [,] ]
Kelvin Li0bff7af2015-11-23 05:32:03 +00002789/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00002790/// to-clause:
Michael Kruse01f670d2019-02-22 22:29:42 +00002791/// 'to' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00002792/// from-clause:
Michael Kruse0336c752019-02-25 20:34:15 +00002793/// 'from' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00002794/// use_device_ptr-clause:
2795/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00002796/// is_device_ptr-clause:
2797/// 'is_device_ptr' '(' list ')'
Alexey Bataeve04483e2019-03-27 14:14:31 +00002798/// allocate-clause:
2799/// 'allocate' '(' [ allocator ':' ] list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002800///
Alexey Bataev182227b2015-08-20 10:54:39 +00002801/// For 'linear' clause linear-list may have the following forms:
2802/// list
2803/// modifier(list)
2804/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00002805OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002806 OpenMPClauseKind Kind,
2807 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002808 SourceLocation Loc = Tok.getLocation();
2809 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002810 SmallVector<Expr *, 4> Vars;
2811 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002812
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002813 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00002814 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002815
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002816 if (ParseOnly)
2817 return nullptr;
Michael Kruse4304e9d2019-02-19 16:38:20 +00002818 OMPVarListLocTy Locs(Loc, LOpen, Data.RLoc);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002819 return Actions.ActOnOpenMPVarListClause(
Michael Kruse4304e9d2019-02-19 16:38:20 +00002820 Kind, Vars, Data.TailExpr, Locs, Data.ColonLoc,
2821 Data.ReductionOrMapperIdScopeSpec, Data.ReductionOrMapperId, Data.DepKind,
2822 Data.LinKind, Data.MapTypeModifiers, Data.MapTypeModifiersLoc,
2823 Data.MapType, Data.IsMapTypeImplicit, Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002824}
2825