blob: 5b95dbbdea6d6883cacf06bc1bfda213cdc4c5d9 [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 Bataev3c676e32019-11-12 11:19:26 -0500371 ConsumeToken();
372 ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000373 } else {
374 InitializerResult = Actions.ActOnFinishFullExpr(
375 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000376 /*DiscardedValue*/ false);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000377 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000378 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
Alexey Bataev070f43a2017-09-06 14:49:58 +0000379 D, InitializerResult.get(), OmpPrivParm);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000380 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
381 Tok.isNot(tok::annot_pragma_openmp_end)) {
382 TPA.Commit();
383 IsCorrect = false;
384 break;
385 }
386 IsCorrect =
387 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
388 }
389 }
390
391 ++I;
392 // Revert parsing if not the last type, otherwise accept it, we're done with
393 // parsing.
394 if (I != E)
395 TPA.Revert();
396 else
397 TPA.Commit();
398 }
399 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
400 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000401}
402
Alexey Bataev070f43a2017-09-06 14:49:58 +0000403void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
404 // Parse declarator '=' initializer.
405 // If a '==' or '+=' is found, suggest a fixit to '='.
406 if (isTokenEqualOrEqualTypo()) {
407 ConsumeToken();
408
409 if (Tok.is(tok::code_completion)) {
410 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
411 Actions.FinalizeDeclaration(OmpPrivParm);
412 cutOffParsing();
413 return;
414 }
415
Alexey Bataev3c676e32019-11-12 11:19:26 -0500416 PreferredType.enterVariableInit(Tok.getLocation(), OmpPrivParm);
417 ExprResult Init = ParseAssignmentExpression();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000418
419 if (Init.isInvalid()) {
420 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
421 Actions.ActOnInitializerError(OmpPrivParm);
422 } else {
423 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
424 /*DirectInit=*/false);
425 }
426 } else if (Tok.is(tok::l_paren)) {
427 // Parse C++ direct initializer: '(' expression-list ')'
428 BalancedDelimiterTracker T(*this, tok::l_paren);
429 T.consumeOpen();
430
431 ExprVector Exprs;
432 CommaLocsTy CommaLocs;
433
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000434 SourceLocation LParLoc = T.getOpenLocation();
Ilya Biryukovff2a9972019-02-26 11:01:50 +0000435 auto RunSignatureHelp = [this, OmpPrivParm, LParLoc, &Exprs]() {
436 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
437 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
438 OmpPrivParm->getLocation(), Exprs, LParLoc);
439 CalledSignatureHelp = true;
440 return PreferredType;
441 };
442 if (ParseExpressionList(Exprs, CommaLocs, [&] {
443 PreferredType.enterFunctionArgument(Tok.getLocation(),
444 RunSignatureHelp);
445 })) {
446 if (PP.isCodeCompletionReached() && !CalledSignatureHelp)
447 RunSignatureHelp();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000448 Actions.ActOnInitializerError(OmpPrivParm);
449 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
450 } else {
451 // Match the ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000452 SourceLocation RLoc = Tok.getLocation();
453 if (!T.consumeClose())
454 RLoc = T.getCloseLocation();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000455
456 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
457 "Unexpected number of commas!");
458
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000459 ExprResult Initializer =
460 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000461 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
462 /*DirectInit=*/true);
463 }
464 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
465 // Parse C++0x braced-init-list.
466 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
467
468 ExprResult Init(ParseBraceInitializer());
469
470 if (Init.isInvalid()) {
471 Actions.ActOnInitializerError(OmpPrivParm);
472 } else {
473 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
474 /*DirectInit=*/true);
475 }
476 } else {
477 Actions.ActOnUninitializedDecl(OmpPrivParm);
478 }
479}
480
Michael Kruse251e1482019-02-01 20:25:04 +0000481/// Parses 'omp declare mapper' directive.
482///
483/// declare-mapper-directive:
484/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifier> ':']
485/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
486/// annot_pragma_openmp_end
487/// <mapper-identifier> and <var> are base language identifiers.
488///
489Parser::DeclGroupPtrTy
490Parser::ParseOpenMPDeclareMapperDirective(AccessSpecifier AS) {
491 bool IsCorrect = true;
492 // Parse '('
493 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
494 if (T.expectAndConsume(diag::err_expected_lparen_after,
495 getOpenMPDirectiveName(OMPD_declare_mapper))) {
496 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
497 return DeclGroupPtrTy();
498 }
499
500 // Parse <mapper-identifier>
501 auto &DeclNames = Actions.getASTContext().DeclarationNames;
502 DeclarationName MapperId;
503 if (PP.LookAhead(0).is(tok::colon)) {
504 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
505 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
506 IsCorrect = false;
507 } else {
508 MapperId = DeclNames.getIdentifier(Tok.getIdentifierInfo());
509 }
510 ConsumeToken();
511 // Consume ':'.
512 ExpectAndConsume(tok::colon);
513 } else {
514 // If no mapper identifier is provided, its name is "default" by default
515 MapperId =
516 DeclNames.getIdentifier(&Actions.getASTContext().Idents.get("default"));
517 }
518
519 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
520 return DeclGroupPtrTy();
521
522 // Parse <type> <var>
523 DeclarationName VName;
524 QualType MapperType;
525 SourceRange Range;
526 TypeResult ParsedType = parseOpenMPDeclareMapperVarDecl(Range, VName, AS);
527 if (ParsedType.isUsable())
528 MapperType =
529 Actions.ActOnOpenMPDeclareMapperType(Range.getBegin(), ParsedType);
530 if (MapperType.isNull())
531 IsCorrect = false;
532 if (!IsCorrect) {
533 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
534 return DeclGroupPtrTy();
535 }
536
537 // Consume ')'.
538 IsCorrect &= !T.consumeClose();
539 if (!IsCorrect) {
540 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
541 return DeclGroupPtrTy();
542 }
543
544 // Enter scope.
545 OMPDeclareMapperDecl *DMD = Actions.ActOnOpenMPDeclareMapperDirectiveStart(
546 getCurScope(), Actions.getCurLexicalContext(), MapperId, MapperType,
547 Range.getBegin(), VName, AS);
548 DeclarationNameInfo DirName;
549 SourceLocation Loc = Tok.getLocation();
550 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
551 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
552 ParseScope OMPDirectiveScope(this, ScopeFlags);
553 Actions.StartOpenMPDSABlock(OMPD_declare_mapper, DirName, getCurScope(), Loc);
554
555 // Add the mapper variable declaration.
556 Actions.ActOnOpenMPDeclareMapperDirectiveVarDecl(
557 DMD, getCurScope(), MapperType, Range.getBegin(), VName);
558
559 // Parse map clauses.
560 SmallVector<OMPClause *, 6> Clauses;
561 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
562 OpenMPClauseKind CKind = Tok.isAnnotation()
563 ? OMPC_unknown
564 : getOpenMPClauseKind(PP.getSpelling(Tok));
565 Actions.StartOpenMPClause(CKind);
566 OMPClause *Clause =
567 ParseOpenMPClause(OMPD_declare_mapper, CKind, Clauses.size() == 0);
568 if (Clause)
569 Clauses.push_back(Clause);
570 else
571 IsCorrect = false;
572 // Skip ',' if any.
573 if (Tok.is(tok::comma))
574 ConsumeToken();
575 Actions.EndOpenMPClause();
576 }
577 if (Clauses.empty()) {
578 Diag(Tok, diag::err_omp_expected_clause)
579 << getOpenMPDirectiveName(OMPD_declare_mapper);
580 IsCorrect = false;
581 }
582
583 // Exit scope.
584 Actions.EndOpenMPDSABlock(nullptr);
585 OMPDirectiveScope.Exit();
586
587 DeclGroupPtrTy DGP =
588 Actions.ActOnOpenMPDeclareMapperDirectiveEnd(DMD, getCurScope(), Clauses);
589 if (!IsCorrect)
590 return DeclGroupPtrTy();
591 return DGP;
592}
593
594TypeResult Parser::parseOpenMPDeclareMapperVarDecl(SourceRange &Range,
595 DeclarationName &Name,
596 AccessSpecifier AS) {
597 // Parse the common declaration-specifiers piece.
598 Parser::DeclSpecContext DSC = Parser::DeclSpecContext::DSC_type_specifier;
599 DeclSpec DS(AttrFactory);
600 ParseSpecifierQualifierList(DS, AS, DSC);
601
602 // Parse the declarator.
603 DeclaratorContext Context = DeclaratorContext::PrototypeContext;
604 Declarator DeclaratorInfo(DS, Context);
605 ParseDeclarator(DeclaratorInfo);
606 Range = DeclaratorInfo.getSourceRange();
607 if (DeclaratorInfo.getIdentifier() == nullptr) {
608 Diag(Tok.getLocation(), diag::err_omp_mapper_expected_declarator);
609 return true;
610 }
611 Name = Actions.GetNameForDeclarator(DeclaratorInfo).getName();
612
613 return Actions.ActOnOpenMPDeclareMapperVarDecl(getCurScope(), DeclaratorInfo);
614}
615
Alexey Bataev2af33e32016-04-07 12:45:37 +0000616namespace {
617/// RAII that recreates function context for correct parsing of clauses of
618/// 'declare simd' construct.
619/// OpenMP, 2.8.2 declare simd Construct
620/// The expressions appearing in the clauses of this directive are evaluated in
621/// the scope of the arguments of the function declaration or definition.
622class FNContextRAII final {
623 Parser &P;
624 Sema::CXXThisScopeRAII *ThisScope;
625 Parser::ParseScope *TempScope;
626 Parser::ParseScope *FnScope;
627 bool HasTemplateScope = false;
628 bool HasFunScope = false;
629 FNContextRAII() = delete;
630 FNContextRAII(const FNContextRAII &) = delete;
631 FNContextRAII &operator=(const FNContextRAII &) = delete;
632
633public:
634 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
635 Decl *D = *Ptr.get().begin();
636 NamedDecl *ND = dyn_cast<NamedDecl>(D);
637 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
638 Sema &Actions = P.getActions();
639
640 // Allow 'this' within late-parsed attributes.
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000641 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, Qualifiers(),
Alexey Bataev2af33e32016-04-07 12:45:37 +0000642 ND && ND->isCXXInstanceMember());
643
644 // If the Decl is templatized, add template parameters to scope.
645 HasTemplateScope = D->isTemplateDecl();
646 TempScope =
647 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
648 if (HasTemplateScope)
649 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
650
651 // If the Decl is on a function, add function parameters to the scope.
652 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000653 FnScope = new Parser::ParseScope(
654 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
655 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000656 if (HasFunScope)
657 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
658 }
659 ~FNContextRAII() {
660 if (HasFunScope) {
661 P.getActions().ActOnExitFunctionContext();
662 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
663 }
664 if (HasTemplateScope)
665 TempScope->Exit();
666 delete FnScope;
667 delete TempScope;
668 delete ThisScope;
669 }
670};
671} // namespace
672
Alexey Bataevd93d3762016-04-12 09:35:56 +0000673/// Parses clauses for 'declare simd' directive.
674/// clause:
675/// 'inbranch' | 'notinbranch'
676/// 'simdlen' '(' <expr> ')'
677/// { 'uniform' '(' <argument_list> ')' }
678/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000679/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
680static bool parseDeclareSimdClauses(
681 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
682 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
683 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
684 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000685 SourceRange BSRange;
686 const Token &Tok = P.getCurToken();
687 bool IsError = false;
688 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
689 if (Tok.isNot(tok::identifier))
690 break;
691 OMPDeclareSimdDeclAttr::BranchStateTy Out;
692 IdentifierInfo *II = Tok.getIdentifierInfo();
693 StringRef ClauseName = II->getName();
694 // Parse 'inranch|notinbranch' clauses.
695 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
696 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
697 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
698 << ClauseName
699 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
700 IsError = true;
701 }
702 BS = Out;
703 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
704 P.ConsumeToken();
705 } else if (ClauseName.equals("simdlen")) {
706 if (SimdLen.isUsable()) {
707 P.Diag(Tok, diag::err_omp_more_one_clause)
708 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
709 IsError = true;
710 }
711 P.ConsumeToken();
712 SourceLocation RLoc;
713 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
714 if (SimdLen.isInvalid())
715 IsError = true;
716 } else {
717 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000718 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
719 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000720 Parser::OpenMPVarListDataTy Data;
Alexey Bataev61908f652018-04-23 19:53:05 +0000721 SmallVectorImpl<Expr *> *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000722 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000723 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000724 else if (CKind == OMPC_linear)
725 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000726
727 P.ConsumeToken();
728 if (P.ParseOpenMPVarList(OMPD_declare_simd,
729 getOpenMPClauseKind(ClauseName), *Vars, Data))
730 IsError = true;
Alexey Bataev61908f652018-04-23 19:53:05 +0000731 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000732 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataev61908f652018-04-23 19:53:05 +0000733 } else if (CKind == OMPC_linear) {
Alexey Bataevecba70f2016-04-12 11:02:11 +0000734 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
735 Data.DepLinMapLoc))
736 Data.LinKind = OMPC_LINEAR_val;
737 LinModifiers.append(Linears.size() - LinModifiers.size(),
738 Data.LinKind);
739 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
740 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000741 } else
742 // TODO: add parsing of other clauses.
743 break;
744 }
745 // Skip ',' if any.
746 if (Tok.is(tok::comma))
747 P.ConsumeToken();
748 }
749 return IsError;
750}
751
Alexey Bataev2af33e32016-04-07 12:45:37 +0000752/// Parse clauses for '#pragma omp declare simd'.
753Parser::DeclGroupPtrTy
754Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
755 CachedTokens &Toks, SourceLocation Loc) {
Ilya Biryukov929af672019-05-17 09:32:05 +0000756 PP.EnterToken(Tok, /*IsReinject*/ true);
757 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
758 /*IsReinject*/ true);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000759 // Consume the previously pushed token.
760 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Alexey Bataevd158cf62019-09-13 20:18:17 +0000761 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000762
763 FNContextRAII FnContext(*this, Ptr);
764 OMPDeclareSimdDeclAttr::BranchStateTy BS =
765 OMPDeclareSimdDeclAttr::BS_Undefined;
766 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000767 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000768 SmallVector<Expr *, 4> Aligneds;
769 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000770 SmallVector<Expr *, 4> Linears;
771 SmallVector<unsigned, 4> LinModifiers;
772 SmallVector<Expr *, 4> Steps;
773 bool IsError =
774 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
775 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000776 // Need to check for extra tokens.
777 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
778 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
779 << getOpenMPDirectiveName(OMPD_declare_simd);
780 while (Tok.isNot(tok::annot_pragma_openmp_end))
781 ConsumeAnyToken();
782 }
783 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000784 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000785 if (IsError)
786 return Ptr;
787 return Actions.ActOnOpenMPDeclareSimdDirective(
788 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
789 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataev20dfd772016-04-04 10:12:15 +0000790}
791
Alexey Bataeva15a1412019-10-02 18:19:02 +0000792/// Parse optional 'score' '(' <expr> ')' ':'.
793static ExprResult parseContextScore(Parser &P) {
794 ExprResult ScoreExpr;
Alexey Bataevfde11e92019-11-07 11:03:10 -0500795 Sema::OMPCtxStringType Buffer;
Alexey Bataeva15a1412019-10-02 18:19:02 +0000796 StringRef SelectorName =
797 P.getPreprocessor().getSpelling(P.getCurToken(), Buffer);
Alexey Bataevdcec2ac2019-11-05 15:33:18 -0500798 if (!SelectorName.equals("score"))
Alexey Bataeva15a1412019-10-02 18:19:02 +0000799 return ScoreExpr;
Alexey Bataeva15a1412019-10-02 18:19:02 +0000800 (void)P.ConsumeToken();
801 SourceLocation RLoc;
802 ScoreExpr = P.ParseOpenMPParensExpr(SelectorName, RLoc);
803 // Parse ':'
804 if (P.getCurToken().is(tok::colon))
805 (void)P.ConsumeAnyToken();
806 else
807 P.Diag(P.getCurToken(), diag::warn_pragma_expected_colon)
808 << "context selector score clause";
809 return ScoreExpr;
810}
811
Alexey Bataev9ff34742019-09-25 19:43:37 +0000812/// Parse context selector for 'implementation' selector set:
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000813/// 'vendor' '(' [ 'score' '(' <score _expr> ')' ':' ] <vendor> { ',' <vendor> }
814/// ')'
Alexey Bataevfde11e92019-11-07 11:03:10 -0500815static void
816parseImplementationSelector(Parser &P, SourceLocation Loc,
817 llvm::StringMap<SourceLocation> &UsedCtx,
818 SmallVectorImpl<Sema::OMPCtxSelectorData> &Data) {
Alexey Bataev9ff34742019-09-25 19:43:37 +0000819 const Token &Tok = P.getCurToken();
820 // Parse inner context selector set name, if any.
821 if (!Tok.is(tok::identifier)) {
822 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
823 << "implementation";
824 // Skip until either '}', ')', or end of directive.
825 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
826 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
827 ;
828 return;
829 }
Alexey Bataevfde11e92019-11-07 11:03:10 -0500830 Sema::OMPCtxStringType Buffer;
Alexey Bataev9ff34742019-09-25 19:43:37 +0000831 StringRef CtxSelectorName = P.getPreprocessor().getSpelling(Tok, Buffer);
Alexey Bataev70d2e542019-10-08 17:47:52 +0000832 auto Res = UsedCtx.try_emplace(CtxSelectorName, Tok.getLocation());
833 if (!Res.second) {
834 // OpenMP 5.0, 2.3.2 Context Selectors, Restrictions.
835 // Each trait-selector-name can only be specified once.
836 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_ctx_mutiple_use)
837 << CtxSelectorName << "implementation";
838 P.Diag(Res.first->getValue(), diag::note_omp_declare_variant_ctx_used_here)
839 << CtxSelectorName;
840 }
Alexey Bataevfde11e92019-11-07 11:03:10 -0500841 OpenMPContextSelectorKind CSKind = getOpenMPContextSelector(CtxSelectorName);
Alexey Bataev9ff34742019-09-25 19:43:37 +0000842 (void)P.ConsumeToken();
843 switch (CSKind) {
Alexey Bataevfde11e92019-11-07 11:03:10 -0500844 case OMP_CTX_vendor: {
Alexey Bataev9ff34742019-09-25 19:43:37 +0000845 // Parse '('.
846 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
847 (void)T.expectAndConsume(diag::err_expected_lparen_after,
848 CtxSelectorName.data());
Alexey Bataevfde11e92019-11-07 11:03:10 -0500849 ExprResult Score = parseContextScore(P);
850 llvm::UniqueVector<Sema::OMPCtxStringType> Vendors;
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000851 do {
852 // Parse <vendor>.
853 StringRef VendorName;
854 if (Tok.is(tok::identifier)) {
855 Buffer.clear();
856 VendorName = P.getPreprocessor().getSpelling(P.getCurToken(), Buffer);
857 (void)P.ConsumeToken();
Alexey Bataev303657a2019-10-08 19:44:16 +0000858 if (!VendorName.empty())
Alexey Bataev4513e93f2019-10-10 15:15:26 +0000859 Vendors.insert(VendorName);
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000860 } else {
861 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_item_expected)
862 << "vendor identifier"
863 << "vendor"
864 << "implementation";
865 }
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000866 if (!P.TryConsumeToken(tok::comma) && Tok.isNot(tok::r_paren)) {
867 P.Diag(Tok, diag::err_expected_punc)
868 << (VendorName.empty() ? "vendor name" : VendorName);
869 }
870 } while (Tok.is(tok::identifier));
Alexey Bataev9ff34742019-09-25 19:43:37 +0000871 // Parse ')'.
872 (void)T.consumeClose();
Alexey Bataevfde11e92019-11-07 11:03:10 -0500873 if (!Vendors.empty())
874 Data.emplace_back(OMP_CTX_SET_implementation, CSKind, Score, Vendors);
Alexey Bataev9ff34742019-09-25 19:43:37 +0000875 break;
876 }
Alexey Bataevfde11e92019-11-07 11:03:10 -0500877 case OMP_CTX_unknown:
Alexey Bataev9ff34742019-09-25 19:43:37 +0000878 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
879 << "implementation";
880 // Skip until either '}', ')', or end of directive.
881 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
882 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
883 ;
884 return;
885 }
Alexey Bataev9ff34742019-09-25 19:43:37 +0000886}
887
Alexey Bataevd158cf62019-09-13 20:18:17 +0000888/// Parses clauses for 'declare variant' directive.
889/// clause:
Alexey Bataevd158cf62019-09-13 20:18:17 +0000890/// <selector_set_name> '=' '{' <context_selectors> '}'
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000891/// [ ',' <selector_set_name> '=' '{' <context_selectors> '}' ]
892bool Parser::parseOpenMPContextSelectors(
Alexey Bataevfde11e92019-11-07 11:03:10 -0500893 SourceLocation Loc, SmallVectorImpl<Sema::OMPCtxSelectorData> &Data) {
Alexey Bataev5d154c32019-10-08 15:56:43 +0000894 llvm::StringMap<SourceLocation> UsedCtxSets;
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000895 do {
896 // Parse inner context selector set name.
897 if (!Tok.is(tok::identifier)) {
898 Diag(Tok.getLocation(), diag::err_omp_declare_variant_no_ctx_selector)
Alexey Bataevdba792c2019-09-23 18:13:31 +0000899 << getOpenMPClauseName(OMPC_match);
Alexey Bataevd158cf62019-09-13 20:18:17 +0000900 return true;
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000901 }
Alexey Bataevfde11e92019-11-07 11:03:10 -0500902 Sema::OMPCtxStringType Buffer;
Alexey Bataev9ff34742019-09-25 19:43:37 +0000903 StringRef CtxSelectorSetName = PP.getSpelling(Tok, Buffer);
Alexey Bataev5d154c32019-10-08 15:56:43 +0000904 auto Res = UsedCtxSets.try_emplace(CtxSelectorSetName, Tok.getLocation());
905 if (!Res.second) {
906 // OpenMP 5.0, 2.3.2 Context Selectors, Restrictions.
907 // Each trait-set-selector-name can only be specified once.
908 Diag(Tok.getLocation(), diag::err_omp_declare_variant_ctx_set_mutiple_use)
909 << CtxSelectorSetName;
910 Diag(Res.first->getValue(),
911 diag::note_omp_declare_variant_ctx_set_used_here)
912 << CtxSelectorSetName;
913 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000914 // Parse '='.
915 (void)ConsumeToken();
916 if (Tok.isNot(tok::equal)) {
917 Diag(Tok.getLocation(), diag::err_omp_declare_variant_equal_expected)
Alexey Bataev9ff34742019-09-25 19:43:37 +0000918 << CtxSelectorSetName;
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000919 return true;
920 }
921 (void)ConsumeToken();
922 // TBD: add parsing of known context selectors.
923 // Unknown selector - just ignore it completely.
924 {
925 // Parse '{'.
926 BalancedDelimiterTracker TBr(*this, tok::l_brace,
927 tok::annot_pragma_openmp_end);
928 if (TBr.expectAndConsume(diag::err_expected_lbrace_after, "="))
929 return true;
Alexey Bataevfde11e92019-11-07 11:03:10 -0500930 OpenMPContextSelectorSetKind CSSKind =
931 getOpenMPContextSelectorSet(CtxSelectorSetName);
Alexey Bataev70d2e542019-10-08 17:47:52 +0000932 llvm::StringMap<SourceLocation> UsedCtx;
933 do {
934 switch (CSSKind) {
Alexey Bataevfde11e92019-11-07 11:03:10 -0500935 case OMP_CTX_SET_implementation:
936 parseImplementationSelector(*this, Loc, UsedCtx, Data);
Alexey Bataev70d2e542019-10-08 17:47:52 +0000937 break;
Alexey Bataevfde11e92019-11-07 11:03:10 -0500938 case OMP_CTX_SET_unknown:
Alexey Bataev70d2e542019-10-08 17:47:52 +0000939 // Skip until either '}', ')', or end of directive.
940 while (!SkipUntil(tok::r_brace, tok::r_paren,
941 tok::annot_pragma_openmp_end, StopBeforeMatch))
942 ;
943 break;
944 }
945 const Token PrevTok = Tok;
946 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
947 Diag(Tok, diag::err_omp_expected_comma_brace)
948 << (PrevTok.isAnnotation() ? "context selector trait"
949 : PP.getSpelling(PrevTok));
950 } while (Tok.is(tok::identifier));
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000951 // Parse '}'.
952 (void)TBr.consumeClose();
953 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000954 // Consume ','
955 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end))
956 (void)ExpectAndConsume(tok::comma);
957 } while (Tok.isAnyIdentifier());
Alexey Bataevd158cf62019-09-13 20:18:17 +0000958 return false;
959}
960
961/// Parse clauses for '#pragma omp declare variant ( variant-func-id ) clause'.
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000962void Parser::ParseOMPDeclareVariantClauses(Parser::DeclGroupPtrTy Ptr,
963 CachedTokens &Toks,
964 SourceLocation Loc) {
Alexey Bataevd158cf62019-09-13 20:18:17 +0000965 PP.EnterToken(Tok, /*IsReinject*/ true);
966 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
967 /*IsReinject*/ true);
968 // Consume the previously pushed token.
969 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
970 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
971
972 FNContextRAII FnContext(*this, Ptr);
973 // Parse function declaration id.
974 SourceLocation RLoc;
975 // Parse with IsAddressOfOperand set to true to parse methods as DeclRefExprs
976 // instead of MemberExprs.
977 ExprResult AssociatedFunction =
978 ParseOpenMPParensExpr(getOpenMPDirectiveName(OMPD_declare_variant), RLoc,
979 /*IsAddressOfOperand=*/true);
980 if (!AssociatedFunction.isUsable()) {
981 if (!Tok.is(tok::annot_pragma_openmp_end))
982 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
983 ;
984 // Skip the last annot_pragma_openmp_end.
985 (void)ConsumeAnnotationToken();
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000986 return;
987 }
988 Optional<std::pair<FunctionDecl *, Expr *>> DeclVarData =
989 Actions.checkOpenMPDeclareVariantFunction(
990 Ptr, AssociatedFunction.get(), SourceRange(Loc, Tok.getLocation()));
991
992 // Parse 'match'.
Alexey Bataevdba792c2019-09-23 18:13:31 +0000993 OpenMPClauseKind CKind = Tok.isAnnotation()
994 ? OMPC_unknown
995 : getOpenMPClauseKind(PP.getSpelling(Tok));
996 if (CKind != OMPC_match) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000997 Diag(Tok.getLocation(), diag::err_omp_declare_variant_wrong_clause)
Alexey Bataevdba792c2019-09-23 18:13:31 +0000998 << getOpenMPClauseName(OMPC_match);
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000999 while (!SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
1000 ;
1001 // Skip the last annot_pragma_openmp_end.
1002 (void)ConsumeAnnotationToken();
1003 return;
1004 }
1005 (void)ConsumeToken();
1006 // Parse '('.
1007 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataevdba792c2019-09-23 18:13:31 +00001008 if (T.expectAndConsume(diag::err_expected_lparen_after,
1009 getOpenMPClauseName(OMPC_match))) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001010 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
1011 ;
1012 // Skip the last annot_pragma_openmp_end.
1013 (void)ConsumeAnnotationToken();
1014 return;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001015 }
1016
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001017 // Parse inner context selectors.
Alexey Bataevfde11e92019-11-07 11:03:10 -05001018 SmallVector<Sema::OMPCtxSelectorData, 4> Data;
1019 if (!parseOpenMPContextSelectors(Loc, Data)) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001020 // Parse ')'.
1021 (void)T.consumeClose();
1022 // Need to check for extra tokens.
1023 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1024 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1025 << getOpenMPDirectiveName(OMPD_declare_variant);
1026 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001027 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001028
1029 // Skip last tokens.
1030 while (Tok.isNot(tok::annot_pragma_openmp_end))
1031 ConsumeAnyToken();
Alexey Bataevfde11e92019-11-07 11:03:10 -05001032 if (DeclVarData.hasValue())
1033 Actions.ActOnOpenMPDeclareVariantDirective(
1034 DeclVarData.getValue().first, DeclVarData.getValue().second,
1035 SourceRange(Loc, Tok.getLocation()), Data);
Alexey Bataevd158cf62019-09-13 20:18:17 +00001036 // Skip the last annot_pragma_openmp_end.
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001037 (void)ConsumeAnnotationToken();
Alexey Bataevd158cf62019-09-13 20:18:17 +00001038}
1039
Alexey Bataev729e2422019-08-23 16:11:14 +00001040/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
1041///
1042/// default-clause:
1043/// 'default' '(' 'none' | 'shared' ')
1044///
1045/// proc_bind-clause:
1046/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1047///
1048/// device_type-clause:
1049/// 'device_type' '(' 'host' | 'nohost' | 'any' )'
1050namespace {
1051 struct SimpleClauseData {
1052 unsigned Type;
1053 SourceLocation Loc;
1054 SourceLocation LOpen;
1055 SourceLocation TypeLoc;
1056 SourceLocation RLoc;
1057 SimpleClauseData(unsigned Type, SourceLocation Loc, SourceLocation LOpen,
1058 SourceLocation TypeLoc, SourceLocation RLoc)
1059 : Type(Type), Loc(Loc), LOpen(LOpen), TypeLoc(TypeLoc), RLoc(RLoc) {}
1060 };
1061} // anonymous namespace
1062
1063static Optional<SimpleClauseData>
1064parseOpenMPSimpleClause(Parser &P, OpenMPClauseKind Kind) {
1065 const Token &Tok = P.getCurToken();
1066 SourceLocation Loc = Tok.getLocation();
1067 SourceLocation LOpen = P.ConsumeToken();
1068 // Parse '('.
1069 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
1070 if (T.expectAndConsume(diag::err_expected_lparen_after,
1071 getOpenMPClauseName(Kind)))
1072 return llvm::None;
1073
1074 unsigned Type = getOpenMPSimpleClauseType(
1075 Kind, Tok.isAnnotation() ? "" : P.getPreprocessor().getSpelling(Tok));
1076 SourceLocation TypeLoc = Tok.getLocation();
1077 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1078 Tok.isNot(tok::annot_pragma_openmp_end))
1079 P.ConsumeAnyToken();
1080
1081 // Parse ')'.
1082 SourceLocation RLoc = Tok.getLocation();
1083 if (!T.consumeClose())
1084 RLoc = T.getCloseLocation();
1085
1086 return SimpleClauseData(Type, Loc, LOpen, TypeLoc, RLoc);
1087}
1088
Kelvin Lie0502752018-11-21 20:15:57 +00001089Parser::DeclGroupPtrTy Parser::ParseOMPDeclareTargetClauses() {
1090 // OpenMP 4.5 syntax with list of entities.
1091 Sema::NamedDeclSetType SameDirectiveDecls;
Alexey Bataev729e2422019-08-23 16:11:14 +00001092 SmallVector<std::tuple<OMPDeclareTargetDeclAttr::MapTypeTy, SourceLocation,
1093 NamedDecl *>,
1094 4>
1095 DeclareTargetDecls;
1096 OMPDeclareTargetDeclAttr::DevTypeTy DT = OMPDeclareTargetDeclAttr::DT_Any;
1097 SourceLocation DeviceTypeLoc;
Kelvin Lie0502752018-11-21 20:15:57 +00001098 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1099 OMPDeclareTargetDeclAttr::MapTypeTy MT = OMPDeclareTargetDeclAttr::MT_To;
1100 if (Tok.is(tok::identifier)) {
1101 IdentifierInfo *II = Tok.getIdentifierInfo();
1102 StringRef ClauseName = II->getName();
Alexey Bataev729e2422019-08-23 16:11:14 +00001103 bool IsDeviceTypeClause =
1104 getLangOpts().OpenMP >= 50 &&
1105 getOpenMPClauseKind(ClauseName) == OMPC_device_type;
1106 // Parse 'to|link|device_type' clauses.
1107 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName, MT) &&
1108 !IsDeviceTypeClause) {
1109 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
1110 << ClauseName << (getLangOpts().OpenMP >= 50 ? 1 : 0);
Kelvin Lie0502752018-11-21 20:15:57 +00001111 break;
1112 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001113 // Parse 'device_type' clause and go to next clause if any.
1114 if (IsDeviceTypeClause) {
1115 Optional<SimpleClauseData> DevTypeData =
1116 parseOpenMPSimpleClause(*this, OMPC_device_type);
1117 if (DevTypeData.hasValue()) {
1118 if (DeviceTypeLoc.isValid()) {
1119 // We already saw another device_type clause, diagnose it.
1120 Diag(DevTypeData.getValue().Loc,
1121 diag::warn_omp_more_one_device_type_clause);
1122 }
1123 switch(static_cast<OpenMPDeviceType>(DevTypeData.getValue().Type)) {
1124 case OMPC_DEVICE_TYPE_any:
1125 DT = OMPDeclareTargetDeclAttr::DT_Any;
1126 break;
1127 case OMPC_DEVICE_TYPE_host:
1128 DT = OMPDeclareTargetDeclAttr::DT_Host;
1129 break;
1130 case OMPC_DEVICE_TYPE_nohost:
1131 DT = OMPDeclareTargetDeclAttr::DT_NoHost;
1132 break;
1133 case OMPC_DEVICE_TYPE_unknown:
1134 llvm_unreachable("Unexpected device_type");
1135 }
1136 DeviceTypeLoc = DevTypeData.getValue().Loc;
1137 }
1138 continue;
1139 }
Kelvin Lie0502752018-11-21 20:15:57 +00001140 ConsumeToken();
1141 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001142 auto &&Callback = [this, MT, &DeclareTargetDecls, &SameDirectiveDecls](
1143 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
1144 NamedDecl *ND = Actions.lookupOpenMPDeclareTargetName(
1145 getCurScope(), SS, NameInfo, SameDirectiveDecls);
1146 if (ND)
1147 DeclareTargetDecls.emplace_back(MT, NameInfo.getLoc(), ND);
Kelvin Lie0502752018-11-21 20:15:57 +00001148 };
1149 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
1150 /*AllowScopeSpecifier=*/true))
1151 break;
1152
1153 // Consume optional ','.
1154 if (Tok.is(tok::comma))
1155 ConsumeToken();
1156 }
1157 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1158 ConsumeAnyToken();
Alexey Bataev729e2422019-08-23 16:11:14 +00001159 for (auto &MTLocDecl : DeclareTargetDecls) {
1160 OMPDeclareTargetDeclAttr::MapTypeTy MT;
1161 SourceLocation Loc;
1162 NamedDecl *ND;
1163 std::tie(MT, Loc, ND) = MTLocDecl;
1164 // device_type clause is applied only to functions.
1165 Actions.ActOnOpenMPDeclareTargetName(
1166 ND, Loc, MT, isa<VarDecl>(ND) ? OMPDeclareTargetDeclAttr::DT_Any : DT);
1167 }
Kelvin Lie0502752018-11-21 20:15:57 +00001168 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
1169 SameDirectiveDecls.end());
1170 if (Decls.empty())
1171 return DeclGroupPtrTy();
1172 return Actions.BuildDeclaratorGroup(Decls);
1173}
1174
1175void Parser::ParseOMPEndDeclareTargetDirective(OpenMPDirectiveKind DKind,
1176 SourceLocation DTLoc) {
1177 if (DKind != OMPD_end_declare_target) {
1178 Diag(Tok, diag::err_expected_end_declare_target);
1179 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
1180 return;
1181 }
1182 ConsumeAnyToken();
1183 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1184 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1185 << getOpenMPDirectiveName(OMPD_end_declare_target);
1186 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1187 }
1188 // Skip the last annot_pragma_openmp_end.
1189 ConsumeAnyToken();
1190}
1191
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001192/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001193///
1194/// threadprivate-directive:
1195/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001196/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +00001197///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001198/// allocate-directive:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001199/// annot_pragma_openmp 'allocate' simple-variable-list [<clause>]
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001200/// annot_pragma_openmp_end
1201///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001202/// declare-reduction-directive:
1203/// annot_pragma_openmp 'declare' 'reduction' [...]
1204/// annot_pragma_openmp_end
1205///
Michael Kruse251e1482019-02-01 20:25:04 +00001206/// declare-mapper-directive:
1207/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1208/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1209/// annot_pragma_openmp_end
1210///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001211/// declare-simd-directive:
1212/// annot_pragma_openmp 'declare simd' {<clause> [,]}
1213/// annot_pragma_openmp_end
1214/// <function declaration/definition>
1215///
Kelvin Li1408f912018-09-26 04:28:39 +00001216/// requires directive:
1217/// annot_pragma_openmp 'requires' <clause> [[[,] <clause>] ... ]
1218/// annot_pragma_openmp_end
1219///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001220Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
1221 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
1222 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001223 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +00001224 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +00001225
Richard Smithaf3b3252017-05-18 19:21:48 +00001226 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001227 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001228
1229 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001230 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +00001231 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001232 DeclDirectiveListParserHelper Helper(this, DKind);
1233 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1234 /*AllowScopeSpecifier=*/true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001235 // The last seen token is annot_pragma_openmp_end - need to check for
1236 // extra tokens.
1237 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1238 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001239 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001240 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +00001241 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001242 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001243 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001244 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
1245 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +00001246 }
1247 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001248 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001249 case OMPD_allocate: {
1250 ConsumeToken();
1251 DeclDirectiveListParserHelper Helper(this, DKind);
1252 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1253 /*AllowScopeSpecifier=*/true)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001254 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001255 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001256 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1257 OMPC_unknown + 1>
1258 FirstClauses(OMPC_unknown + 1);
1259 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1260 OpenMPClauseKind CKind =
1261 Tok.isAnnotation() ? OMPC_unknown
1262 : getOpenMPClauseKind(PP.getSpelling(Tok));
1263 Actions.StartOpenMPClause(CKind);
1264 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1265 !FirstClauses[CKind].getInt());
1266 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1267 StopBeforeMatch);
1268 FirstClauses[CKind].setInt(true);
1269 if (Clause != nullptr)
1270 Clauses.push_back(Clause);
1271 if (Tok.is(tok::annot_pragma_openmp_end)) {
1272 Actions.EndOpenMPClause();
1273 break;
1274 }
1275 // Skip ',' if any.
1276 if (Tok.is(tok::comma))
1277 ConsumeToken();
1278 Actions.EndOpenMPClause();
1279 }
1280 // The last seen token is annot_pragma_openmp_end - need to check for
1281 // extra tokens.
1282 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1283 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1284 << getOpenMPDirectiveName(DKind);
1285 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1286 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001287 }
1288 // Skip the last annot_pragma_openmp_end.
1289 ConsumeAnnotationToken();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001290 return Actions.ActOnOpenMPAllocateDirective(Loc, Helper.getIdentifiers(),
1291 Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001292 }
1293 break;
1294 }
Kelvin Li1408f912018-09-26 04:28:39 +00001295 case OMPD_requires: {
1296 SourceLocation StartLoc = ConsumeToken();
1297 SmallVector<OMPClause *, 5> Clauses;
1298 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
1299 FirstClauses(OMPC_unknown + 1);
1300 if (Tok.is(tok::annot_pragma_openmp_end)) {
Ilya Biryukovff2a9972019-02-26 11:01:50 +00001301 Diag(Tok, diag::err_omp_expected_clause)
Kelvin Li1408f912018-09-26 04:28:39 +00001302 << getOpenMPDirectiveName(OMPD_requires);
1303 break;
1304 }
1305 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1306 OpenMPClauseKind CKind = Tok.isAnnotation()
1307 ? OMPC_unknown
1308 : getOpenMPClauseKind(PP.getSpelling(Tok));
1309 Actions.StartOpenMPClause(CKind);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001310 OMPClause *Clause = ParseOpenMPClause(OMPD_requires, CKind,
1311 !FirstClauses[CKind].getInt());
1312 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1313 StopBeforeMatch);
Kelvin Li1408f912018-09-26 04:28:39 +00001314 FirstClauses[CKind].setInt(true);
1315 if (Clause != nullptr)
1316 Clauses.push_back(Clause);
1317 if (Tok.is(tok::annot_pragma_openmp_end)) {
1318 Actions.EndOpenMPClause();
1319 break;
1320 }
1321 // Skip ',' if any.
1322 if (Tok.is(tok::comma))
1323 ConsumeToken();
1324 Actions.EndOpenMPClause();
1325 }
1326 // Consume final annot_pragma_openmp_end
1327 if (Clauses.size() == 0) {
1328 Diag(Tok, diag::err_omp_expected_clause)
1329 << getOpenMPDirectiveName(OMPD_requires);
1330 ConsumeAnnotationToken();
1331 return nullptr;
1332 }
1333 ConsumeAnnotationToken();
1334 return Actions.ActOnOpenMPRequiresDirective(StartLoc, Clauses);
1335 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001336 case OMPD_declare_reduction:
1337 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001338 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001339 // The last seen token is annot_pragma_openmp_end - need to check for
1340 // extra tokens.
1341 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1342 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1343 << getOpenMPDirectiveName(OMPD_declare_reduction);
1344 while (Tok.isNot(tok::annot_pragma_openmp_end))
1345 ConsumeAnyToken();
1346 }
1347 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001348 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001349 return Res;
1350 }
1351 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001352 case OMPD_declare_mapper: {
1353 ConsumeToken();
1354 if (DeclGroupPtrTy Res = ParseOpenMPDeclareMapperDirective(AS)) {
1355 // Skip the last annot_pragma_openmp_end.
1356 ConsumeAnnotationToken();
1357 return Res;
1358 }
1359 break;
1360 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001361 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001362 case OMPD_declare_simd: {
1363 // The syntax is:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001364 // { #pragma omp declare {simd|variant} }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001365 // <function-declaration-or-definition>
1366 //
Alexey Bataev2af33e32016-04-07 12:45:37 +00001367 CachedTokens Toks;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001368 Toks.push_back(Tok);
1369 ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001370 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
1371 Toks.push_back(Tok);
1372 ConsumeAnyToken();
1373 }
1374 Toks.push_back(Tok);
1375 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +00001376
1377 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +00001378 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001379 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +00001380 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001381 // Here we expect to see some function declaration.
1382 if (AS == AS_none) {
1383 assert(TagType == DeclSpec::TST_unspecified);
1384 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001385 ParsingDeclSpec PDS(*this);
1386 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1387 } else {
1388 Ptr =
1389 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1390 }
1391 }
1392 if (!Ptr) {
Alexey Bataevd158cf62019-09-13 20:18:17 +00001393 Diag(Loc, diag::err_omp_decl_in_declare_simd_variant)
1394 << (DKind == OMPD_declare_simd ? 0 : 1);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001395 return DeclGroupPtrTy();
1396 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001397 if (DKind == OMPD_declare_simd)
1398 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
1399 assert(DKind == OMPD_declare_variant &&
1400 "Expected declare variant directive only");
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001401 ParseOMPDeclareVariantClauses(Ptr, Toks, Loc);
1402 return Ptr;
Alexey Bataev587e1de2016-03-30 10:43:55 +00001403 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001404 case OMPD_declare_target: {
1405 SourceLocation DTLoc = ConsumeAnyToken();
1406 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Kelvin Lie0502752018-11-21 20:15:57 +00001407 return ParseOMPDeclareTargetClauses();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001408 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001409
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001410 // Skip the last annot_pragma_openmp_end.
1411 ConsumeAnyToken();
1412
1413 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
1414 return DeclGroupPtrTy();
1415
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001416 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +00001417 DKind = parseOpenMPDirectiveKind(*this);
Kelvin Libc38e632018-09-10 02:07:09 +00001418 while (DKind != OMPD_end_declare_target && Tok.isNot(tok::eof) &&
1419 Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +00001420 DeclGroupPtrTy Ptr;
1421 // Here we expect to see some function declaration.
1422 if (AS == AS_none) {
1423 assert(TagType == DeclSpec::TST_unspecified);
1424 MaybeParseCXX11Attributes(Attrs);
1425 ParsingDeclSpec PDS(*this);
1426 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1427 } else {
1428 Ptr =
1429 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1430 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001431 if (Ptr) {
1432 DeclGroupRef Ref = Ptr.get();
1433 Decls.append(Ref.begin(), Ref.end());
1434 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001435 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
1436 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +00001437 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001438 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001439 if (DKind != OMPD_end_declare_target)
1440 TPA.Revert();
1441 else
1442 TPA.Commit();
1443 }
1444 }
1445
Kelvin Lie0502752018-11-21 20:15:57 +00001446 ParseOMPEndDeclareTargetDirective(DKind, DTLoc);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001447 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +00001448 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001449 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001450 case OMPD_unknown:
1451 Diag(Tok, diag::err_omp_unknown_directive);
1452 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001453 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001454 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001455 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +00001456 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001457 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001458 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001459 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +00001460 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001461 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001462 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001463 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001464 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001465 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +00001466 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001467 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001468 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001469 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001470 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001471 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +00001472 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001473 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001474 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001475 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001476 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +00001477 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001478 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001479 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001480 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001481 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001482 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001483 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +00001484 case OMPD_master_taskloop:
Alexey Bataevb8552ab2019-10-18 16:47:35 +00001485 case OMPD_master_taskloop_simd:
Alexey Bataev5bbcead2019-10-14 17:17:41 +00001486 case OMPD_parallel_master_taskloop:
Alexey Bataev14a388f2019-10-25 10:27:13 -04001487 case OMPD_parallel_master_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001488 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001489 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001490 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001491 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +00001492 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001493 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001494 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001495 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001496 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +00001497 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +00001498 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001499 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +00001500 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +00001501 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +00001502 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +00001503 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +00001504 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +00001505 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001506 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +00001507 break;
1508 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001509 while (Tok.isNot(tok::annot_pragma_openmp_end))
1510 ConsumeAnyToken();
1511 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +00001512 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001513}
1514
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001515/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001516///
1517/// threadprivate-directive:
1518/// annot_pragma_openmp 'threadprivate' simple-variable-list
1519/// annot_pragma_openmp_end
1520///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001521/// allocate-directive:
1522/// annot_pragma_openmp 'allocate' simple-variable-list
1523/// annot_pragma_openmp_end
1524///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001525/// declare-reduction-directive:
1526/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
1527/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
1528/// ('omp_priv' '=' <expression>|<function_call>) ')']
1529/// annot_pragma_openmp_end
1530///
Michael Kruse251e1482019-02-01 20:25:04 +00001531/// declare-mapper-directive:
1532/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1533/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1534/// annot_pragma_openmp_end
1535///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001536/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001537/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001538/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
1539/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001540/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +00001541/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Alexey Bataev60e51c42019-10-10 20:13:02 +00001542/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' | 'master
Alexey Bataevb8552ab2019-10-18 16:47:35 +00001543/// taskloop' | 'master taskloop simd' | 'parallel master taskloop' |
Alexey Bataev14a388f2019-10-25 10:27:13 -04001544/// 'parallel master taskloop simd' | 'distribute' | 'target enter data'
1545/// | 'target exit data' | 'target parallel' | 'target parallel for' |
1546/// 'target update' | 'distribute parallel for' | 'distribute paralle
1547/// for simd' | 'distribute simd' | 'target parallel for simd' | 'target
1548/// simd' | 'teams distribute' | 'teams distribute simd' | 'teams
1549/// distribute parallel for simd' | 'teams distribute parallel for' |
1550/// 'target teams' | 'target teams distribute' | 'target teams
1551/// distribute parallel for' | 'target teams distribute parallel for
1552/// simd' | 'target teams distribute simd' {clause}
1553/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001554///
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001555StmtResult
1556Parser::ParseOpenMPDeclarativeOrExecutableDirective(ParsedStmtContext StmtCtx) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001557 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +00001558 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001559 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001560 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +00001561 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +00001562 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
1563 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +00001564 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +00001565 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001566 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001567 // Name of critical directive.
1568 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001569 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +00001570 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +00001571 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001572
1573 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001574 case OMPD_threadprivate: {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001575 // FIXME: Should this be permitted in C++?
1576 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1577 ParsedStmtContext()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001578 Diag(Tok, diag::err_omp_immediate_directive)
1579 << getOpenMPDirectiveName(DKind) << 0;
1580 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001581 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001582 DeclDirectiveListParserHelper Helper(this, DKind);
1583 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1584 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001585 // The last seen token is annot_pragma_openmp_end - need to check for
1586 // extra tokens.
1587 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1588 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001589 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001590 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001591 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001592 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
1593 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001594 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1595 }
Alp Tokerd751fa72013-12-18 19:10:49 +00001596 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001597 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001598 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001599 case OMPD_allocate: {
1600 // FIXME: Should this be permitted in C++?
1601 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1602 ParsedStmtContext()) {
1603 Diag(Tok, diag::err_omp_immediate_directive)
1604 << getOpenMPDirectiveName(DKind) << 0;
1605 }
1606 ConsumeToken();
1607 DeclDirectiveListParserHelper Helper(this, DKind);
1608 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1609 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001610 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001611 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001612 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1613 OMPC_unknown + 1>
1614 FirstClauses(OMPC_unknown + 1);
1615 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1616 OpenMPClauseKind CKind =
1617 Tok.isAnnotation() ? OMPC_unknown
1618 : getOpenMPClauseKind(PP.getSpelling(Tok));
1619 Actions.StartOpenMPClause(CKind);
1620 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1621 !FirstClauses[CKind].getInt());
1622 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1623 StopBeforeMatch);
1624 FirstClauses[CKind].setInt(true);
1625 if (Clause != nullptr)
1626 Clauses.push_back(Clause);
1627 if (Tok.is(tok::annot_pragma_openmp_end)) {
1628 Actions.EndOpenMPClause();
1629 break;
1630 }
1631 // Skip ',' if any.
1632 if (Tok.is(tok::comma))
1633 ConsumeToken();
1634 Actions.EndOpenMPClause();
1635 }
1636 // The last seen token is annot_pragma_openmp_end - need to check for
1637 // extra tokens.
1638 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1639 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1640 << getOpenMPDirectiveName(DKind);
1641 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1642 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001643 }
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001644 DeclGroupPtrTy Res = Actions.ActOnOpenMPAllocateDirective(
1645 Loc, Helper.getIdentifiers(), Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001646 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1647 }
1648 SkipUntil(tok::annot_pragma_openmp_end);
1649 break;
1650 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001651 case OMPD_declare_reduction:
1652 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001653 if (DeclGroupPtrTy Res =
1654 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001655 // The last seen token is annot_pragma_openmp_end - need to check for
1656 // extra tokens.
1657 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1658 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1659 << getOpenMPDirectiveName(OMPD_declare_reduction);
1660 while (Tok.isNot(tok::annot_pragma_openmp_end))
1661 ConsumeAnyToken();
1662 }
1663 ConsumeAnyToken();
1664 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +00001665 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001666 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +00001667 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001668 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001669 case OMPD_declare_mapper: {
1670 ConsumeToken();
1671 if (DeclGroupPtrTy Res =
1672 ParseOpenMPDeclareMapperDirective(/*AS=*/AS_none)) {
1673 // Skip the last annot_pragma_openmp_end.
1674 ConsumeAnnotationToken();
1675 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1676 } else {
1677 SkipUntil(tok::annot_pragma_openmp_end);
1678 }
1679 break;
1680 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001681 case OMPD_flush:
1682 if (PP.LookAhead(0).is(tok::l_paren)) {
1683 FlushHasClause = true;
1684 // Push copy of the current token back to stream to properly parse
1685 // pseudo-clause OMPFlushClause.
Ilya Biryukov929af672019-05-17 09:32:05 +00001686 PP.EnterToken(Tok, /*IsReinject*/ true);
Alexey Bataev6125da92014-07-21 11:26:11 +00001687 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001688 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +00001689 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001690 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001691 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001692 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001693 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001694 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001695 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +00001696 case OMPD_target_update:
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001697 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1698 ParsedStmtContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00001699 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +00001700 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +00001701 }
1702 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001703 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001704 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001705 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001706 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001707 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001708 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001709 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001710 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001711 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001712 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001713 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001714 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001715 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001716 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001717 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001718 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001719 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001720 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001721 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001722 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001723 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001724 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001725 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001726 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001727 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +00001728 case OMPD_master_taskloop:
Alexey Bataevb8552ab2019-10-18 16:47:35 +00001729 case OMPD_master_taskloop_simd:
Alexey Bataev5bbcead2019-10-14 17:17:41 +00001730 case OMPD_parallel_master_taskloop:
Alexey Bataev14a388f2019-10-25 10:27:13 -04001731 case OMPD_parallel_master_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001732 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001733 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001734 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001735 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001736 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001737 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001738 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001739 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001740 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001741 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001742 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001743 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001744 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001745 case OMPD_target_teams_distribute_parallel_for_simd:
1746 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001747 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001748 // Parse directive name of the 'critical' directive if any.
1749 if (DKind == OMPD_critical) {
1750 BalancedDelimiterTracker T(*this, tok::l_paren,
1751 tok::annot_pragma_openmp_end);
1752 if (!T.consumeOpen()) {
1753 if (Tok.isAnyIdentifier()) {
1754 DirName =
1755 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1756 ConsumeAnyToken();
1757 } else {
1758 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1759 }
1760 T.consumeClose();
1761 }
Alexey Bataev80909872015-07-02 11:25:17 +00001762 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001763 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001764 if (Tok.isNot(tok::annot_pragma_openmp_end))
1765 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001766 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001767
Alexey Bataevf29276e2014-06-18 04:14:57 +00001768 if (isOpenMPLoopDirective(DKind))
1769 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1770 if (isOpenMPSimdDirective(DKind))
1771 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1772 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001773 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001774
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001775 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001776 OpenMPClauseKind CKind =
1777 Tok.isAnnotation()
1778 ? OMPC_unknown
1779 : FlushHasClause ? OMPC_flush
1780 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001781 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001782 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001783 OMPClause *Clause =
1784 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001785 FirstClauses[CKind].setInt(true);
1786 if (Clause) {
1787 FirstClauses[CKind].setPointer(Clause);
1788 Clauses.push_back(Clause);
1789 }
1790
1791 // Skip ',' if any.
1792 if (Tok.is(tok::comma))
1793 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001794 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001795 }
1796 // End location of the directive.
1797 EndLoc = Tok.getLocation();
1798 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001799 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001800
Alexey Bataeveb482352015-12-18 05:05:56 +00001801 // OpenMP [2.13.8, ordered Construct, Syntax]
1802 // If the depend clause is specified, the ordered construct is a stand-alone
1803 // directive.
1804 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001805 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1806 ParsedStmtContext()) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001807 Diag(Loc, diag::err_omp_immediate_directive)
1808 << getOpenMPDirectiveName(DKind) << 1
1809 << getOpenMPClauseName(OMPC_depend);
1810 }
1811 HasAssociatedStatement = false;
1812 }
1813
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001814 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001815 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001816 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001817 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001818 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1819 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1820 // should have at least one compound statement scope within it.
1821 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001822 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001823 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1824 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001825 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001826 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1827 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1828 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001829 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001830 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001831 Directive = Actions.ActOnOpenMPExecutableDirective(
1832 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1833 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001834
1835 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001836 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001837 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001838 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001839 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001840 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001841 case OMPD_declare_target:
1842 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00001843 case OMPD_requires:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001844 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001845 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001846 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001847 SkipUntil(tok::annot_pragma_openmp_end);
1848 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001849 case OMPD_unknown:
1850 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001851 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001852 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001853 }
1854 return Directive;
1855}
1856
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001857// Parses simple list:
1858// simple-variable-list:
1859// '(' id-expression {, id-expression} ')'
1860//
1861bool Parser::ParseOpenMPSimpleVarList(
1862 OpenMPDirectiveKind Kind,
1863 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1864 Callback,
1865 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001866 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001867 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001868 if (T.expectAndConsume(diag::err_expected_lparen_after,
1869 getOpenMPDirectiveName(Kind)))
1870 return true;
1871 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001872 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001873
1874 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001875 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001876 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001877 UnqualifiedId Name;
1878 // Read var name.
1879 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001880 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001881
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001882 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001883 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001884 IsCorrect = false;
1885 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001886 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001887 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00001888 nullptr, Name)) {
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);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001892 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1893 Tok.isNot(tok::annot_pragma_openmp_end)) {
1894 IsCorrect = false;
1895 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001896 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001897 Diag(PrevTok.getLocation(), diag::err_expected)
1898 << tok::identifier
1899 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001900 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001901 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001902 }
1903 // Consume ','.
1904 if (Tok.is(tok::comma)) {
1905 ConsumeToken();
1906 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001907 }
1908
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001909 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001910 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001911 IsCorrect = false;
1912 }
1913
1914 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001915 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001916
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001917 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001918}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001919
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001920/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001921///
1922/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001923/// if-clause | final-clause | num_threads-clause | safelen-clause |
1924/// default-clause | private-clause | firstprivate-clause | shared-clause
1925/// | linear-clause | aligned-clause | collapse-clause |
1926/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001927/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001928/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001929/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001930/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001931/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001932/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001933/// from-clause | is_device_ptr-clause | task_reduction-clause |
Alexey Bataeve04483e2019-03-27 14:14:31 +00001934/// in_reduction-clause | allocator-clause | allocate-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001935///
1936OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1937 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001938 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001939 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001940 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001941 // Check if clause is allowed for the given directive.
1942 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001943 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1944 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001945 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001946 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001947 }
1948
1949 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001950 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001951 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001952 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001953 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001954 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001955 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001956 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001957 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001958 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001959 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001960 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001961 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001962 case OMPC_hint:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001963 case OMPC_allocator:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001964 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001965 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001966 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001967 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001968 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001969 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001970 // OpenMP [2.9.1, target data construct, Restrictions]
1971 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001972 // OpenMP [2.11.1, task Construct, Restrictions]
1973 // At most one if clause can appear on the directive.
1974 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001975 // OpenMP [teams Construct, Restrictions]
1976 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001977 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001978 // OpenMP [2.9.1, task Construct, Restrictions]
1979 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001980 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1981 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001982 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1983 // At most one num_tasks clause can appear on the directive.
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001984 // OpenMP [2.11.3, allocate Directive, Restrictions]
1985 // At most one allocator clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001986 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001987 Diag(Tok, diag::err_omp_more_one_clause)
1988 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001989 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001990 }
1991
Alexey Bataev10e775f2015-07-30 11:36:16 +00001992 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001993 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001994 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001995 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001996 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001997 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001998 case OMPC_proc_bind:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001999 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002000 // OpenMP [2.14.3.1, Restrictions]
2001 // Only a single default clause may be specified on a parallel, task or
2002 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002003 // OpenMP [2.5, parallel Construct, Restrictions]
2004 // At most one proc_bind clause can appear on the directive.
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00002005 // OpenMP [5.0, Requires directive, Restrictions]
2006 // At most one atomic_default_mem_order clause can appear
2007 // on the directive
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002008 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002009 Diag(Tok, diag::err_omp_more_one_clause)
2010 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002011 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002012 }
2013
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002014 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002015 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002016 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00002017 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002018 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002019 // OpenMP [2.7.1, Restrictions, p. 3]
2020 // Only one schedule clause can appear on a loop directive.
cchene06f3e02019-11-15 13:02:06 -05002021 // OpenMP 4.5 [2.10.4, Restrictions, p. 106]
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002022 // At most one defaultmap clause can appear on the directive.
cchene06f3e02019-11-15 13:02:06 -05002023 if ((getLangOpts().OpenMP < 50 || CKind != OMPC_defaultmap) &&
2024 !FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002025 Diag(Tok, diag::err_omp_more_one_clause)
2026 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002027 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002028 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00002029 LLVM_FALLTHROUGH;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002030 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002031 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002032 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002033 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002034 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002035 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002036 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002037 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00002038 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00002039 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002040 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00002041 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002042 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00002043 case OMPC_nogroup:
Kelvin Li1408f912018-09-26 04:28:39 +00002044 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00002045 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00002046 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00002047 case OMPC_dynamic_allocators:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002048 // OpenMP [2.7.1, Restrictions, p. 9]
2049 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00002050 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
2051 // Only one nowait clause can appear on a for directive.
Kelvin Li1408f912018-09-26 04:28:39 +00002052 // OpenMP [5.0, Requires directive, Restrictions]
2053 // Each of the requires clauses can appear at most once on the directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002054 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002055 Diag(Tok, diag::err_omp_more_one_clause)
2056 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002057 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002058 }
2059
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002060 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002061 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002062 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002063 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002064 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002065 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002066 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00002067 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00002068 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002069 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002070 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002071 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002072 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002073 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002074 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00002075 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00002076 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00002077 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00002078 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00002079 case OMPC_is_device_ptr:
Alexey Bataeve04483e2019-03-27 14:14:31 +00002080 case OMPC_allocate:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002081 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002082 break;
Alexey Bataev729e2422019-08-23 16:11:14 +00002083 case OMPC_device_type:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002084 case OMPC_unknown:
2085 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00002086 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00002087 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002088 break;
2089 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002090 case OMPC_uniform:
Alexey Bataevdba792c2019-09-23 18:13:31 +00002091 case OMPC_match:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002092 if (!WrongDirective)
2093 Diag(Tok, diag::err_omp_unexpected_clause)
2094 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00002095 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002096 break;
2097 }
Craig Topper161e4db2014-05-21 06:02:52 +00002098 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002099}
2100
Alexey Bataev2af33e32016-04-07 12:45:37 +00002101/// Parses simple expression in parens for single-expression clauses of OpenMP
2102/// constructs.
2103/// \param RLoc Returned location of right paren.
2104ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
Alexey Bataevd158cf62019-09-13 20:18:17 +00002105 SourceLocation &RLoc,
2106 bool IsAddressOfOperand) {
Alexey Bataev2af33e32016-04-07 12:45:37 +00002107 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2108 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
2109 return ExprError();
2110
2111 SourceLocation ELoc = Tok.getLocation();
2112 ExprResult LHS(ParseCastExpression(
Alexey Bataevd158cf62019-09-13 20:18:17 +00002113 /*isUnaryExpression=*/false, IsAddressOfOperand, NotTypeCast));
Alexey Bataev2af33e32016-04-07 12:45:37 +00002114 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002115 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev2af33e32016-04-07 12:45:37 +00002116
2117 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002118 RLoc = Tok.getLocation();
2119 if (!T.consumeClose())
2120 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00002121
Alexey Bataev2af33e32016-04-07 12:45:37 +00002122 return Val;
2123}
2124
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002125/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00002126/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00002127/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002128///
Alexey Bataev3778b602014-07-17 07:32:53 +00002129/// final-clause:
2130/// 'final' '(' expression ')'
2131///
Alexey Bataev62c87d22014-03-21 04:51:18 +00002132/// num_threads-clause:
2133/// 'num_threads' '(' expression ')'
2134///
2135/// safelen-clause:
2136/// 'safelen' '(' expression ')'
2137///
Alexey Bataev66b15b52015-08-21 11:14:16 +00002138/// simdlen-clause:
2139/// 'simdlen' '(' expression ')'
2140///
Alexander Musman8bd31e62014-05-27 15:12:19 +00002141/// collapse-clause:
2142/// 'collapse' '(' expression ')'
2143///
Alexey Bataeva0569352015-12-01 10:17:31 +00002144/// priority-clause:
2145/// 'priority' '(' expression ')'
2146///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002147/// grainsize-clause:
2148/// 'grainsize' '(' expression ')'
2149///
Alexey Bataev382967a2015-12-08 12:06:20 +00002150/// num_tasks-clause:
2151/// 'num_tasks' '(' expression ')'
2152///
Alexey Bataev28c75412015-12-15 08:19:24 +00002153/// hint-clause:
2154/// 'hint' '(' expression ')'
2155///
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002156/// allocator-clause:
2157/// 'allocator' '(' expression ')'
2158///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002159OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
2160 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002161 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00002162 SourceLocation LLoc = Tok.getLocation();
2163 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002164
Alexey Bataev2af33e32016-04-07 12:45:37 +00002165 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002166
2167 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00002168 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002169
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002170 if (ParseOnly)
2171 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00002172 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002173}
2174
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002175/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002176///
2177/// default-clause:
2178/// 'default' '(' 'none' | 'shared' ')
2179///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002180/// proc_bind-clause:
2181/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
2182///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002183OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
2184 bool ParseOnly) {
Alexey Bataev729e2422019-08-23 16:11:14 +00002185 llvm::Optional<SimpleClauseData> Val = parseOpenMPSimpleClause(*this, Kind);
2186 if (!Val || ParseOnly)
Craig Topper161e4db2014-05-21 06:02:52 +00002187 return nullptr;
Alexey Bataev729e2422019-08-23 16:11:14 +00002188 return Actions.ActOnOpenMPSimpleClause(
2189 Kind, Val.getValue().Type, Val.getValue().TypeLoc, Val.getValue().LOpen,
2190 Val.getValue().Loc, Val.getValue().RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002191}
2192
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002193/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002194///
2195/// ordered-clause:
2196/// 'ordered'
2197///
Alexey Bataev236070f2014-06-20 11:19:47 +00002198/// nowait-clause:
2199/// 'nowait'
2200///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002201/// untied-clause:
2202/// 'untied'
2203///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002204/// mergeable-clause:
2205/// 'mergeable'
2206///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002207/// read-clause:
2208/// 'read'
2209///
Alexey Bataev346265e2015-09-25 10:37:12 +00002210/// threads-clause:
2211/// 'threads'
2212///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002213/// simd-clause:
2214/// 'simd'
2215///
Alexey Bataevb825de12015-12-07 10:51:44 +00002216/// nogroup-clause:
2217/// 'nogroup'
2218///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002219OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002220 SourceLocation Loc = Tok.getLocation();
2221 ConsumeAnyToken();
2222
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002223 if (ParseOnly)
2224 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002225 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
2226}
2227
2228
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002229/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00002230/// argument like 'schedule' or 'dist_schedule'.
2231///
2232/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00002233/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
2234/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00002235///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002236/// if-clause:
2237/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
2238///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002239/// defaultmap:
2240/// 'defaultmap' '(' modifier ':' kind ')'
2241///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002242OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
2243 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00002244 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002245 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002246 // Parse '('.
2247 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2248 if (T.expectAndConsume(diag::err_expected_lparen_after,
2249 getOpenMPClauseName(Kind)))
2250 return nullptr;
2251
2252 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002253 SmallVector<unsigned, 4> Arg;
2254 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002255 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00002256 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
2257 Arg.resize(NumberOfElements);
2258 KLoc.resize(NumberOfElements);
2259 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
2260 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
2261 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00002262 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002263 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00002264 if (KindModifier > OMPC_SCHEDULE_unknown) {
2265 // Parse 'modifier'
2266 Arg[Modifier1] = KindModifier;
2267 KLoc[Modifier1] = Tok.getLocation();
2268 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2269 Tok.isNot(tok::annot_pragma_openmp_end))
2270 ConsumeAnyToken();
2271 if (Tok.is(tok::comma)) {
2272 // Parse ',' 'modifier'
2273 ConsumeAnyToken();
2274 KindModifier = getOpenMPSimpleClauseType(
2275 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2276 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
2277 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00002278 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002279 KLoc[Modifier2] = Tok.getLocation();
2280 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2281 Tok.isNot(tok::annot_pragma_openmp_end))
2282 ConsumeAnyToken();
2283 }
2284 // Parse ':'
2285 if (Tok.is(tok::colon))
2286 ConsumeAnyToken();
2287 else
2288 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
2289 KindModifier = getOpenMPSimpleClauseType(
2290 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2291 }
2292 Arg[ScheduleKind] = KindModifier;
2293 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002294 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2295 Tok.isNot(tok::annot_pragma_openmp_end))
2296 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00002297 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
2298 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
2299 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002300 Tok.is(tok::comma))
2301 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00002302 } else if (Kind == OMPC_dist_schedule) {
2303 Arg.push_back(getOpenMPSimpleClauseType(
2304 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2305 KLoc.push_back(Tok.getLocation());
2306 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2307 Tok.isNot(tok::annot_pragma_openmp_end))
2308 ConsumeAnyToken();
2309 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
2310 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002311 } else if (Kind == OMPC_defaultmap) {
2312 // Get a defaultmap modifier
cchene06f3e02019-11-15 13:02:06 -05002313 unsigned Modifier = getOpenMPSimpleClauseType(
2314 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2315 // Set defaultmap modifier to unknown if it is either scalar, aggregate, or
2316 // pointer
2317 if (Modifier < OMPC_DEFAULTMAP_MODIFIER_unknown)
2318 Modifier = OMPC_DEFAULTMAP_MODIFIER_unknown;
2319 Arg.push_back(Modifier);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002320 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