blob: 7528e435f0417ec1bfa8842d1f99a1fd04da2ff7 [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements parsing of all OpenMP directives and clauses.
11///
12//===----------------------------------------------------------------------===//
13
Alexey Bataev9959db52014-05-06 10:08:46 +000014#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000015#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000016#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000017#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000018#include "clang/Parse/RAIIObjectsForParser.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000019#include "clang/Sema/Scope.h"
20#include "llvm/ADT/PointerIntPair.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,
44 OMPD_target_teams_distribute_parallel
Dmitry Polukhin82478332016-02-13 06:53:38 +000045};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000046
47class ThreadprivateListParserHelper final {
48 SmallVector<Expr *, 4> Identifiers;
49 Parser *P;
50
51public:
52 ThreadprivateListParserHelper(Parser *P) : P(P) {}
53 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
54 ExprResult Res =
55 P->getActions().ActOnOpenMPIdExpression(P->getCurScope(), SS, NameInfo);
56 if (Res.isUsable())
57 Identifiers.push_back(Res.get());
58 }
59 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
60};
Dmitry Polukhin82478332016-02-13 06:53:38 +000061} // namespace
62
63// Map token string to extended OMP token kind that are
64// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
65static unsigned getOpenMPDirectiveKindEx(StringRef S) {
66 auto DKind = getOpenMPDirectiveKind(S);
67 if (DKind != OMPD_unknown)
68 return DKind;
69
70 return llvm::StringSwitch<unsigned>(S)
71 .Case("cancellation", OMPD_cancellation)
72 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000073 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000074 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000075 .Case("enter", OMPD_enter)
76 .Case("exit", OMPD_exit)
77 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000078 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000079 .Case("update", OMPD_update)
Dmitry Polukhin82478332016-02-13 06:53:38 +000080 .Default(OMPD_unknown);
81}
82
Alexey Bataev4acb8592014-07-07 13:01:15 +000083static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000084 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
85 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
86 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000087 static const unsigned F[][3] = {
88 { OMPD_cancellation, OMPD_point, OMPD_cancellation_point },
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000089 { OMPD_declare, OMPD_reduction, OMPD_declare_reduction },
Alexey Bataev587e1de2016-03-30 10:43:55 +000090 { OMPD_declare, OMPD_simd, OMPD_declare_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000091 { OMPD_declare, OMPD_target, OMPD_declare_target },
Carlo Bertolli9925f152016-06-27 14:55:37 +000092 { OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel },
93 { OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for },
Kelvin Li4a39add2016-07-05 05:00:15 +000094 { OMPD_distribute_parallel_for, OMPD_simd,
95 OMPD_distribute_parallel_for_simd },
Kelvin Li787f3fc2016-07-06 04:45:38 +000096 { OMPD_distribute, OMPD_simd, OMPD_distribute_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000097 { OMPD_end, OMPD_declare, OMPD_end_declare },
98 { OMPD_end_declare, OMPD_target, OMPD_end_declare_target },
Dmitry Polukhin82478332016-02-13 06:53:38 +000099 { OMPD_target, OMPD_data, OMPD_target_data },
100 { OMPD_target, OMPD_enter, OMPD_target_enter },
101 { OMPD_target, OMPD_exit, OMPD_target_exit },
Samuel Antao686c70c2016-05-26 17:30:50 +0000102 { OMPD_target, OMPD_update, OMPD_target_update },
Dmitry Polukhin82478332016-02-13 06:53:38 +0000103 { OMPD_target_enter, OMPD_data, OMPD_target_enter_data },
104 { OMPD_target_exit, OMPD_data, OMPD_target_exit_data },
105 { OMPD_for, OMPD_simd, OMPD_for_simd },
106 { OMPD_parallel, OMPD_for, OMPD_parallel_for },
107 { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
108 { OMPD_parallel, OMPD_sections, OMPD_parallel_sections },
109 { OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd },
110 { OMPD_target, OMPD_parallel, OMPD_target_parallel },
Kelvin Li986330c2016-07-20 22:57:10 +0000111 { OMPD_target, OMPD_simd, OMPD_target_simd },
Kelvin Lia579b912016-07-14 02:54:56 +0000112 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for },
Kelvin Li02532872016-08-05 14:37:37 +0000113 { OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd },
Kelvin Li4e325f72016-10-25 12:50:55 +0000114 { OMPD_teams, OMPD_distribute, OMPD_teams_distribute },
Kelvin Li579e41c2016-11-30 23:51:03 +0000115 { OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd },
116 { OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel },
117 { OMPD_teams_distribute_parallel, OMPD_for, OMPD_teams_distribute_parallel_for },
Kelvin Libf594a52016-12-17 05:48:59 +0000118 { OMPD_teams_distribute_parallel_for, OMPD_simd, OMPD_teams_distribute_parallel_for_simd },
Kelvin Li83c451e2016-12-25 04:52:54 +0000119 { OMPD_target, OMPD_teams, OMPD_target_teams },
Kelvin Li80e8f562016-12-29 22:16:30 +0000120 { OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute },
121 { OMPD_target_teams_distribute, OMPD_parallel, OMPD_target_teams_distribute_parallel },
Kelvin Lida681182017-01-10 18:08:18 +0000122 { OMPD_target_teams_distribute, OMPD_simd, OMPD_target_teams_distribute_simd },
Kelvin Li1851df52017-01-03 05:23:48 +0000123 { OMPD_target_teams_distribute_parallel, OMPD_for, OMPD_target_teams_distribute_parallel_for },
124 { OMPD_target_teams_distribute_parallel_for, OMPD_simd, OMPD_target_teams_distribute_parallel_for_simd }
Dmitry Polukhin82478332016-02-13 06:53:38 +0000125 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000126 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +0000127 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000128 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000129 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000130 ? static_cast<unsigned>(OMPD_unknown)
131 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
132 if (DKind == OMPD_unknown)
133 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000134
Alexander Musmanf82886e2014-09-18 05:12:34 +0000135 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000136 if (DKind != F[i][0])
137 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000138
Dmitry Polukhin82478332016-02-13 06:53:38 +0000139 Tok = P.getPreprocessor().LookAhead(0);
140 unsigned SDKind =
141 Tok.isAnnotation()
142 ? static_cast<unsigned>(OMPD_unknown)
143 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
144 if (SDKind == OMPD_unknown)
145 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000146
Dmitry Polukhin82478332016-02-13 06:53:38 +0000147 if (SDKind == F[i][1]) {
148 P.ConsumeToken();
149 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000150 }
151 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000152 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
153 : OMPD_unknown;
154}
155
156static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000157 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000158 Sema &Actions = P.getActions();
159 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000160 // Allow to use 'operator' keyword for C++ operators
161 bool WithOperator = false;
162 if (Tok.is(tok::kw_operator)) {
163 P.ConsumeToken();
164 Tok = P.getCurToken();
165 WithOperator = true;
166 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000167 switch (Tok.getKind()) {
168 case tok::plus: // '+'
169 OOK = OO_Plus;
170 break;
171 case tok::minus: // '-'
172 OOK = OO_Minus;
173 break;
174 case tok::star: // '*'
175 OOK = OO_Star;
176 break;
177 case tok::amp: // '&'
178 OOK = OO_Amp;
179 break;
180 case tok::pipe: // '|'
181 OOK = OO_Pipe;
182 break;
183 case tok::caret: // '^'
184 OOK = OO_Caret;
185 break;
186 case tok::ampamp: // '&&'
187 OOK = OO_AmpAmp;
188 break;
189 case tok::pipepipe: // '||'
190 OOK = OO_PipePipe;
191 break;
192 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000193 if (!WithOperator)
194 break;
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000195 LLVM_FALLTHROUGH;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000196 default:
197 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
198 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
199 Parser::StopBeforeMatch);
200 return DeclarationName();
201 }
202 P.ConsumeToken();
203 auto &DeclNames = Actions.getASTContext().DeclarationNames;
204 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
205 : DeclNames.getCXXOperatorName(OOK);
206}
207
208/// \brief Parse 'omp declare reduction' construct.
209///
210/// declare-reduction-directive:
211/// annot_pragma_openmp 'declare' 'reduction'
212/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
213/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
214/// annot_pragma_openmp_end
215/// <reduction_id> is either a base language identifier or one of the following
216/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
217///
218Parser::DeclGroupPtrTy
219Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
220 // Parse '('.
221 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
222 if (T.expectAndConsume(diag::err_expected_lparen_after,
223 getOpenMPDirectiveName(OMPD_declare_reduction))) {
224 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
225 return DeclGroupPtrTy();
226 }
227
228 DeclarationName Name = parseOpenMPReductionId(*this);
229 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
230 return DeclGroupPtrTy();
231
232 // Consume ':'.
233 bool IsCorrect = !ExpectAndConsume(tok::colon);
234
235 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
236 return DeclGroupPtrTy();
237
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000238 IsCorrect = IsCorrect && !Name.isEmpty();
239
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000240 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
241 Diag(Tok.getLocation(), diag::err_expected_type);
242 IsCorrect = false;
243 }
244
245 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
246 return DeclGroupPtrTy();
247
248 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
249 // Parse list of types until ':' token.
250 do {
251 ColonProtectionRAIIObject ColonRAII(*this);
252 SourceRange Range;
Faisal Vali421b2d12017-12-29 05:41:00 +0000253 TypeResult TR =
254 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000255 if (TR.isUsable()) {
256 auto ReductionType =
257 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
258 if (!ReductionType.isNull()) {
259 ReductionTypes.push_back(
260 std::make_pair(ReductionType, Range.getBegin()));
261 }
262 } else {
263 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
264 StopBeforeMatch);
265 }
266
267 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
268 break;
269
270 // Consume ','.
271 if (ExpectAndConsume(tok::comma)) {
272 IsCorrect = false;
273 if (Tok.is(tok::annot_pragma_openmp_end)) {
274 Diag(Tok.getLocation(), diag::err_expected_type);
275 return DeclGroupPtrTy();
276 }
277 }
278 } while (Tok.isNot(tok::annot_pragma_openmp_end));
279
280 if (ReductionTypes.empty()) {
281 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
282 return DeclGroupPtrTy();
283 }
284
285 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
286 return DeclGroupPtrTy();
287
288 // Consume ':'.
289 if (ExpectAndConsume(tok::colon))
290 IsCorrect = false;
291
292 if (Tok.is(tok::annot_pragma_openmp_end)) {
293 Diag(Tok.getLocation(), diag::err_expected_expression);
294 return DeclGroupPtrTy();
295 }
296
297 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
298 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
299
300 // Parse <combiner> expression and then parse initializer if any for each
301 // correct type.
302 unsigned I = 0, E = ReductionTypes.size();
303 for (auto *D : DRD.get()) {
304 TentativeParsingAction TPA(*this);
305 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000306 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000307 Scope::OpenMPDirectiveScope);
308 // Parse <combiner> expression.
309 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
310 ExprResult CombinerResult =
311 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
312 D->getLocation(), /*DiscardedValue=*/true);
313 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
314
315 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
316 Tok.isNot(tok::annot_pragma_openmp_end)) {
317 TPA.Commit();
318 IsCorrect = false;
319 break;
320 }
321 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
322 ExprResult InitializerResult;
323 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
324 // Parse <initializer> expression.
325 if (Tok.is(tok::identifier) &&
326 Tok.getIdentifierInfo()->isStr("initializer"))
327 ConsumeToken();
328 else {
329 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
330 TPA.Commit();
331 IsCorrect = false;
332 break;
333 }
334 // Parse '('.
335 BalancedDelimiterTracker T(*this, tok::l_paren,
336 tok::annot_pragma_openmp_end);
337 IsCorrect =
338 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
339 IsCorrect;
340 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
341 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000342 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000343 Scope::OpenMPDirectiveScope);
344 // Parse expression.
Alexey Bataev070f43a2017-09-06 14:49:58 +0000345 VarDecl *OmpPrivParm =
346 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
347 D);
348 // Check if initializer is omp_priv <init_expr> or something else.
349 if (Tok.is(tok::identifier) &&
350 Tok.getIdentifierInfo()->isStr("omp_priv")) {
351 ConsumeToken();
352 ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
353 } else {
354 InitializerResult = Actions.ActOnFinishFullExpr(
355 ParseAssignmentExpression().get(), D->getLocation(),
356 /*DiscardedValue=*/true);
357 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000358 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
Alexey Bataev070f43a2017-09-06 14:49:58 +0000359 D, InitializerResult.get(), OmpPrivParm);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000360 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
361 Tok.isNot(tok::annot_pragma_openmp_end)) {
362 TPA.Commit();
363 IsCorrect = false;
364 break;
365 }
366 IsCorrect =
367 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
368 }
369 }
370
371 ++I;
372 // Revert parsing if not the last type, otherwise accept it, we're done with
373 // parsing.
374 if (I != E)
375 TPA.Revert();
376 else
377 TPA.Commit();
378 }
379 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
380 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000381}
382
Alexey Bataev070f43a2017-09-06 14:49:58 +0000383void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
384 // Parse declarator '=' initializer.
385 // If a '==' or '+=' is found, suggest a fixit to '='.
386 if (isTokenEqualOrEqualTypo()) {
387 ConsumeToken();
388
389 if (Tok.is(tok::code_completion)) {
390 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
391 Actions.FinalizeDeclaration(OmpPrivParm);
392 cutOffParsing();
393 return;
394 }
395
396 ExprResult Init(ParseInitializer());
397
398 if (Init.isInvalid()) {
399 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
400 Actions.ActOnInitializerError(OmpPrivParm);
401 } else {
402 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
403 /*DirectInit=*/false);
404 }
405 } else if (Tok.is(tok::l_paren)) {
406 // Parse C++ direct initializer: '(' expression-list ')'
407 BalancedDelimiterTracker T(*this, tok::l_paren);
408 T.consumeOpen();
409
410 ExprVector Exprs;
411 CommaLocsTy CommaLocs;
412
413 if (ParseExpressionList(Exprs, CommaLocs, [this, OmpPrivParm, &Exprs] {
414 Actions.CodeCompleteConstructor(
415 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
416 OmpPrivParm->getLocation(), Exprs);
417 })) {
418 Actions.ActOnInitializerError(OmpPrivParm);
419 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
420 } else {
421 // Match the ')'.
422 T.consumeClose();
423
424 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
425 "Unexpected number of commas!");
426
427 ExprResult Initializer = Actions.ActOnParenListExpr(
428 T.getOpenLocation(), T.getCloseLocation(), Exprs);
429 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
430 /*DirectInit=*/true);
431 }
432 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
433 // Parse C++0x braced-init-list.
434 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
435
436 ExprResult Init(ParseBraceInitializer());
437
438 if (Init.isInvalid()) {
439 Actions.ActOnInitializerError(OmpPrivParm);
440 } else {
441 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
442 /*DirectInit=*/true);
443 }
444 } else {
445 Actions.ActOnUninitializedDecl(OmpPrivParm);
446 }
447}
448
Alexey Bataev2af33e32016-04-07 12:45:37 +0000449namespace {
450/// RAII that recreates function context for correct parsing of clauses of
451/// 'declare simd' construct.
452/// OpenMP, 2.8.2 declare simd Construct
453/// The expressions appearing in the clauses of this directive are evaluated in
454/// the scope of the arguments of the function declaration or definition.
455class FNContextRAII final {
456 Parser &P;
457 Sema::CXXThisScopeRAII *ThisScope;
458 Parser::ParseScope *TempScope;
459 Parser::ParseScope *FnScope;
460 bool HasTemplateScope = false;
461 bool HasFunScope = false;
462 FNContextRAII() = delete;
463 FNContextRAII(const FNContextRAII &) = delete;
464 FNContextRAII &operator=(const FNContextRAII &) = delete;
465
466public:
467 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
468 Decl *D = *Ptr.get().begin();
469 NamedDecl *ND = dyn_cast<NamedDecl>(D);
470 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
471 Sema &Actions = P.getActions();
472
473 // Allow 'this' within late-parsed attributes.
474 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
475 ND && ND->isCXXInstanceMember());
476
477 // If the Decl is templatized, add template parameters to scope.
478 HasTemplateScope = D->isTemplateDecl();
479 TempScope =
480 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
481 if (HasTemplateScope)
482 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
483
484 // If the Decl is on a function, add function parameters to the scope.
485 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000486 FnScope = new Parser::ParseScope(
487 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
488 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000489 if (HasFunScope)
490 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
491 }
492 ~FNContextRAII() {
493 if (HasFunScope) {
494 P.getActions().ActOnExitFunctionContext();
495 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
496 }
497 if (HasTemplateScope)
498 TempScope->Exit();
499 delete FnScope;
500 delete TempScope;
501 delete ThisScope;
502 }
503};
504} // namespace
505
Alexey Bataevd93d3762016-04-12 09:35:56 +0000506/// Parses clauses for 'declare simd' directive.
507/// clause:
508/// 'inbranch' | 'notinbranch'
509/// 'simdlen' '(' <expr> ')'
510/// { 'uniform' '(' <argument_list> ')' }
511/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000512/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
513static bool parseDeclareSimdClauses(
514 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
515 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
516 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
517 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000518 SourceRange BSRange;
519 const Token &Tok = P.getCurToken();
520 bool IsError = false;
521 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
522 if (Tok.isNot(tok::identifier))
523 break;
524 OMPDeclareSimdDeclAttr::BranchStateTy Out;
525 IdentifierInfo *II = Tok.getIdentifierInfo();
526 StringRef ClauseName = II->getName();
527 // Parse 'inranch|notinbranch' clauses.
528 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
529 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
530 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
531 << ClauseName
532 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
533 IsError = true;
534 }
535 BS = Out;
536 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
537 P.ConsumeToken();
538 } else if (ClauseName.equals("simdlen")) {
539 if (SimdLen.isUsable()) {
540 P.Diag(Tok, diag::err_omp_more_one_clause)
541 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
542 IsError = true;
543 }
544 P.ConsumeToken();
545 SourceLocation RLoc;
546 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
547 if (SimdLen.isInvalid())
548 IsError = true;
549 } else {
550 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000551 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
552 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000553 Parser::OpenMPVarListDataTy Data;
554 auto *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000555 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000556 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000557 else if (CKind == OMPC_linear)
558 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000559
560 P.ConsumeToken();
561 if (P.ParseOpenMPVarList(OMPD_declare_simd,
562 getOpenMPClauseKind(ClauseName), *Vars, Data))
563 IsError = true;
564 if (CKind == OMPC_aligned)
565 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000566 else if (CKind == OMPC_linear) {
567 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
568 Data.DepLinMapLoc))
569 Data.LinKind = OMPC_LINEAR_val;
570 LinModifiers.append(Linears.size() - LinModifiers.size(),
571 Data.LinKind);
572 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
573 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000574 } else
575 // TODO: add parsing of other clauses.
576 break;
577 }
578 // Skip ',' if any.
579 if (Tok.is(tok::comma))
580 P.ConsumeToken();
581 }
582 return IsError;
583}
584
Alexey Bataev2af33e32016-04-07 12:45:37 +0000585/// Parse clauses for '#pragma omp declare simd'.
586Parser::DeclGroupPtrTy
587Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
588 CachedTokens &Toks, SourceLocation Loc) {
589 PP.EnterToken(Tok);
590 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
591 // Consume the previously pushed token.
592 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
593
594 FNContextRAII FnContext(*this, Ptr);
595 OMPDeclareSimdDeclAttr::BranchStateTy BS =
596 OMPDeclareSimdDeclAttr::BS_Undefined;
597 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000598 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000599 SmallVector<Expr *, 4> Aligneds;
600 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000601 SmallVector<Expr *, 4> Linears;
602 SmallVector<unsigned, 4> LinModifiers;
603 SmallVector<Expr *, 4> Steps;
604 bool IsError =
605 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
606 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000607 // Need to check for extra tokens.
608 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
609 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
610 << getOpenMPDirectiveName(OMPD_declare_simd);
611 while (Tok.isNot(tok::annot_pragma_openmp_end))
612 ConsumeAnyToken();
613 }
614 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000615 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataevd93d3762016-04-12 09:35:56 +0000616 if (!IsError) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000617 return Actions.ActOnOpenMPDeclareSimdDirective(
Alexey Bataevecba70f2016-04-12 11:02:11 +0000618 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
619 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataevd93d3762016-04-12 09:35:56 +0000620 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000621 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000622}
623
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000624/// \brief Parsing of declarative OpenMP directives.
625///
626/// threadprivate-directive:
627/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000628/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000629///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000630/// declare-reduction-directive:
631/// annot_pragma_openmp 'declare' 'reduction' [...]
632/// annot_pragma_openmp_end
633///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000634/// declare-simd-directive:
635/// annot_pragma_openmp 'declare simd' {<clause> [,]}
636/// annot_pragma_openmp_end
637/// <function declaration/definition>
638///
639Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
640 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
641 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000642 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000643 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000644
Richard Smithaf3b3252017-05-18 19:21:48 +0000645 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev4acb8592014-07-07 13:01:15 +0000646 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000647
648 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000649 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000650 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000651 ThreadprivateListParserHelper Helper(this);
652 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000653 // The last seen token is annot_pragma_openmp_end - need to check for
654 // extra tokens.
655 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
656 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000657 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000658 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000659 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000660 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000661 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000662 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
663 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000664 }
665 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000666 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000667 case OMPD_declare_reduction:
668 ConsumeToken();
669 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
670 // The last seen token is annot_pragma_openmp_end - need to check for
671 // extra tokens.
672 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
673 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
674 << getOpenMPDirectiveName(OMPD_declare_reduction);
675 while (Tok.isNot(tok::annot_pragma_openmp_end))
676 ConsumeAnyToken();
677 }
678 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000679 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000680 return Res;
681 }
682 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000683 case OMPD_declare_simd: {
684 // The syntax is:
685 // { #pragma omp declare simd }
686 // <function-declaration-or-definition>
687 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000688 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000689 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000690 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
691 Toks.push_back(Tok);
692 ConsumeAnyToken();
693 }
694 Toks.push_back(Tok);
695 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000696
697 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000698 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000699 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000700 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000701 // Here we expect to see some function declaration.
702 if (AS == AS_none) {
703 assert(TagType == DeclSpec::TST_unspecified);
704 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000705 ParsingDeclSpec PDS(*this);
706 Ptr = ParseExternalDeclaration(Attrs, &PDS);
707 } else {
708 Ptr =
709 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
710 }
711 }
712 if (!Ptr) {
713 Diag(Loc, diag::err_omp_decl_in_declare_simd);
714 return DeclGroupPtrTy();
715 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000716 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000717 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000718 case OMPD_declare_target: {
719 SourceLocation DTLoc = ConsumeAnyToken();
720 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000721 // OpenMP 4.5 syntax with list of entities.
Alexey Bataev34f8a702018-03-28 14:28:54 +0000722 Sema::NamedDeclSetType SameDirectiveDecls;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000723 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
724 OMPDeclareTargetDeclAttr::MapTypeTy MT =
725 OMPDeclareTargetDeclAttr::MT_To;
726 if (Tok.is(tok::identifier)) {
727 IdentifierInfo *II = Tok.getIdentifierInfo();
728 StringRef ClauseName = II->getName();
729 // Parse 'to|link' clauses.
730 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
731 MT)) {
732 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
733 << ClauseName;
734 break;
735 }
736 ConsumeToken();
737 }
738 auto Callback = [this, MT, &SameDirectiveDecls](
Alexey Bataev34f8a702018-03-28 14:28:54 +0000739 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000740 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
741 SameDirectiveDecls);
742 };
Alexey Bataev34f8a702018-03-28 14:28:54 +0000743 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
744 /*AllowScopeSpecifier=*/true))
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000745 break;
746
747 // Consume optional ','.
748 if (Tok.is(tok::comma))
749 ConsumeToken();
750 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000751 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000752 ConsumeAnyToken();
Alexey Bataev34f8a702018-03-28 14:28:54 +0000753 SmallVector<Decl *, 4> Decls;
754 Decls.reserve(SameDirectiveDecls.size());
755 for (Decl *D : SameDirectiveDecls)
756 Decls.emplace_back(D);
757 if (Decls.empty())
758 return DeclGroupPtrTy();
759 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000760 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000761
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000762 // Skip the last annot_pragma_openmp_end.
763 ConsumeAnyToken();
764
765 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
766 return DeclGroupPtrTy();
767
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000768 llvm::SmallVector<Decl *, 4> Decls;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000769 DKind = ParseOpenMPDirectiveKind(*this);
770 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
771 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +0000772 DeclGroupPtrTy Ptr;
773 // Here we expect to see some function declaration.
774 if (AS == AS_none) {
775 assert(TagType == DeclSpec::TST_unspecified);
776 MaybeParseCXX11Attributes(Attrs);
777 ParsingDeclSpec PDS(*this);
778 Ptr = ParseExternalDeclaration(Attrs, &PDS);
779 } else {
780 Ptr =
781 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
782 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000783 if (Ptr) {
784 DeclGroupRef Ref = Ptr.get();
785 Decls.append(Ref.begin(), Ref.end());
786 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000787 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
788 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +0000789 ConsumeAnnotationToken();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000790 DKind = ParseOpenMPDirectiveKind(*this);
791 if (DKind != OMPD_end_declare_target)
792 TPA.Revert();
793 else
794 TPA.Commit();
795 }
796 }
797
798 if (DKind == OMPD_end_declare_target) {
799 ConsumeAnyToken();
800 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
801 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
802 << getOpenMPDirectiveName(OMPD_end_declare_target);
803 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
804 }
805 // Skip the last annot_pragma_openmp_end.
806 ConsumeAnyToken();
807 } else {
808 Diag(Tok, diag::err_expected_end_declare_target);
809 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
810 }
811 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +0000812 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000813 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000814 case OMPD_unknown:
815 Diag(Tok, diag::err_omp_unknown_directive);
816 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000817 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000818 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000819 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000820 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000821 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000822 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000823 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000824 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000825 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000826 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000827 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000828 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000829 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000830 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000831 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000832 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000833 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000834 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000835 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000836 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000837 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000838 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000839 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000840 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000841 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000842 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000843 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000844 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000845 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000846 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000847 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000848 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000849 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000850 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000851 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000852 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000853 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000854 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000855 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000856 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000857 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000858 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000859 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +0000860 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +0000861 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +0000862 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +0000863 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +0000864 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000865 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +0000866 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000867 break;
868 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000869 while (Tok.isNot(tok::annot_pragma_openmp_end))
870 ConsumeAnyToken();
871 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000872 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000873}
874
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000875/// \brief Parsing of declarative or executable OpenMP directives.
876///
877/// threadprivate-directive:
878/// annot_pragma_openmp 'threadprivate' simple-variable-list
879/// annot_pragma_openmp_end
880///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000881/// declare-reduction-directive:
882/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
883/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
884/// ('omp_priv' '=' <expression>|<function_call>) ')']
885/// annot_pragma_openmp_end
886///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000887/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000888/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000889/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
890/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000891/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000892/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000893/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000894/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000895/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000896/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000897/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000898/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000899/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000900/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +0000901/// 'teams distribute parallel for' | 'target teams' |
902/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +0000903/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +0000904/// 'target teams distribute parallel for simd' |
905/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000906/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000907///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000908StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000909 AllowedConstructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000910 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000911 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000912 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000913 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000914 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +0000915 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
916 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +0000917 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000918 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000919 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000920 // Name of critical directive.
921 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000922 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000923 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000924 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000925
926 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000927 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000928 if (Allowed != ACK_Any) {
929 Diag(Tok, diag::err_omp_immediate_directive)
930 << getOpenMPDirectiveName(DKind) << 0;
931 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000932 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000933 ThreadprivateListParserHelper Helper(this);
934 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000935 // The last seen token is annot_pragma_openmp_end - need to check for
936 // extra tokens.
937 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
938 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000939 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000940 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000941 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000942 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
943 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000944 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
945 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000946 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000947 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000948 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000949 case OMPD_declare_reduction:
950 ConsumeToken();
951 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
952 // The last seen token is annot_pragma_openmp_end - need to check for
953 // extra tokens.
954 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
955 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
956 << getOpenMPDirectiveName(OMPD_declare_reduction);
957 while (Tok.isNot(tok::annot_pragma_openmp_end))
958 ConsumeAnyToken();
959 }
960 ConsumeAnyToken();
961 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
962 } else
963 SkipUntil(tok::annot_pragma_openmp_end);
964 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000965 case OMPD_flush:
966 if (PP.LookAhead(0).is(tok::l_paren)) {
967 FlushHasClause = true;
968 // Push copy of the current token back to stream to properly parse
969 // pseudo-clause OMPFlushClause.
970 PP.EnterToken(Tok);
971 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000972 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +0000973 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000974 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000975 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000976 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000977 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000978 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000979 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000980 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000981 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000982 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000983 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000984 }
985 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000986 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000987 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000988 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000989 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000990 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000991 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000992 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000993 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000994 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000995 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000996 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000997 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000998 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000999 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001000 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001001 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001002 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001003 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001004 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001005 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001006 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001007 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001008 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001009 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001010 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001011 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001012 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001013 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001014 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001015 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001016 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001017 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001018 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001019 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001020 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001021 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001022 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001023 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001024 case OMPD_target_teams_distribute_parallel_for_simd:
1025 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001026 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001027 // Parse directive name of the 'critical' directive if any.
1028 if (DKind == OMPD_critical) {
1029 BalancedDelimiterTracker T(*this, tok::l_paren,
1030 tok::annot_pragma_openmp_end);
1031 if (!T.consumeOpen()) {
1032 if (Tok.isAnyIdentifier()) {
1033 DirName =
1034 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1035 ConsumeAnyToken();
1036 } else {
1037 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1038 }
1039 T.consumeClose();
1040 }
Alexey Bataev80909872015-07-02 11:25:17 +00001041 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001042 CancelRegion = ParseOpenMPDirectiveKind(*this);
1043 if (Tok.isNot(tok::annot_pragma_openmp_end))
1044 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001045 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001046
Alexey Bataevf29276e2014-06-18 04:14:57 +00001047 if (isOpenMPLoopDirective(DKind))
1048 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1049 if (isOpenMPSimdDirective(DKind))
1050 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1051 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001052 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001053
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001054 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001055 OpenMPClauseKind CKind =
1056 Tok.isAnnotation()
1057 ? OMPC_unknown
1058 : FlushHasClause ? OMPC_flush
1059 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001060 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001061 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001062 OMPClause *Clause =
1063 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001064 FirstClauses[CKind].setInt(true);
1065 if (Clause) {
1066 FirstClauses[CKind].setPointer(Clause);
1067 Clauses.push_back(Clause);
1068 }
1069
1070 // Skip ',' if any.
1071 if (Tok.is(tok::comma))
1072 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001073 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001074 }
1075 // End location of the directive.
1076 EndLoc = Tok.getLocation();
1077 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001078 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001079
Alexey Bataeveb482352015-12-18 05:05:56 +00001080 // OpenMP [2.13.8, ordered Construct, Syntax]
1081 // If the depend clause is specified, the ordered construct is a stand-alone
1082 // directive.
1083 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001084 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001085 Diag(Loc, diag::err_omp_immediate_directive)
1086 << getOpenMPDirectiveName(DKind) << 1
1087 << getOpenMPClauseName(OMPC_depend);
1088 }
1089 HasAssociatedStatement = false;
1090 }
1091
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001092 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001093 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001094 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001095 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001096 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1097 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1098 // should have at least one compound statement scope within it.
1099 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001100 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001101 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1102 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001103 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001104 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1105 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1106 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001107 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001108 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001109 Directive = Actions.ActOnOpenMPExecutableDirective(
1110 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1111 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001112
1113 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001114 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001115 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001116 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001117 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001118 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001119 case OMPD_declare_target:
1120 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001121 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001122 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001123 SkipUntil(tok::annot_pragma_openmp_end);
1124 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001125 case OMPD_unknown:
1126 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001127 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001128 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001129 }
1130 return Directive;
1131}
1132
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001133// Parses simple list:
1134// simple-variable-list:
1135// '(' id-expression {, id-expression} ')'
1136//
1137bool Parser::ParseOpenMPSimpleVarList(
1138 OpenMPDirectiveKind Kind,
1139 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1140 Callback,
1141 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001142 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001143 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001144 if (T.expectAndConsume(diag::err_expected_lparen_after,
1145 getOpenMPDirectiveName(Kind)))
1146 return true;
1147 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001148 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001149
1150 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001151 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001152 CXXScopeSpec SS;
1153 SourceLocation TemplateKWLoc;
1154 UnqualifiedId Name;
1155 // Read var name.
1156 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001157 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001158
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001159 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001160 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001161 IsCorrect = false;
1162 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001163 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001164 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001165 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001166 IsCorrect = false;
1167 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001168 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001169 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1170 Tok.isNot(tok::annot_pragma_openmp_end)) {
1171 IsCorrect = false;
1172 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001173 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001174 Diag(PrevTok.getLocation(), diag::err_expected)
1175 << tok::identifier
1176 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001177 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001178 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001179 }
1180 // Consume ','.
1181 if (Tok.is(tok::comma)) {
1182 ConsumeToken();
1183 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001184 }
1185
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001186 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001187 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001188 IsCorrect = false;
1189 }
1190
1191 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001192 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001193
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001194 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001195}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001196
1197/// \brief Parsing of OpenMP clauses.
1198///
1199/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001200/// if-clause | final-clause | num_threads-clause | safelen-clause |
1201/// default-clause | private-clause | firstprivate-clause | shared-clause
1202/// | linear-clause | aligned-clause | collapse-clause |
1203/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001204/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001205/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001206/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001207/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001208/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001209/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001210/// from-clause | is_device_ptr-clause | task_reduction-clause |
1211/// in_reduction-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001212///
1213OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1214 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001215 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001216 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001217 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001218 // Check if clause is allowed for the given directive.
1219 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001220 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1221 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001222 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001223 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001224 }
1225
1226 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001227 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001228 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001229 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001230 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001231 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001232 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001233 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001234 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001235 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001236 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001237 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001238 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001239 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001240 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001241 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001242 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001243 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001244 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001245 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001246 // OpenMP [2.9.1, target data construct, Restrictions]
1247 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001248 // OpenMP [2.11.1, task Construct, Restrictions]
1249 // At most one if clause can appear on the directive.
1250 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001251 // OpenMP [teams Construct, Restrictions]
1252 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001253 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001254 // OpenMP [2.9.1, task Construct, Restrictions]
1255 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001256 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1257 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001258 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1259 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001260 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001261 Diag(Tok, diag::err_omp_more_one_clause)
1262 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001263 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001264 }
1265
Alexey Bataev10e775f2015-07-30 11:36:16 +00001266 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001267 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001268 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001269 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001270 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001271 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001272 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001273 // OpenMP [2.14.3.1, Restrictions]
1274 // Only a single default clause may be specified on a parallel, task or
1275 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001276 // OpenMP [2.5, parallel Construct, Restrictions]
1277 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001278 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001279 Diag(Tok, diag::err_omp_more_one_clause)
1280 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001281 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001282 }
1283
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001284 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001285 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001286 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001287 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001288 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001289 // OpenMP [2.7.1, Restrictions, p. 3]
1290 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001291 // OpenMP [2.10.4, Restrictions, p. 106]
1292 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001293 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001294 Diag(Tok, diag::err_omp_more_one_clause)
1295 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001296 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001297 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001298 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001299
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001300 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001301 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001302 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001303 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001304 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001305 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001306 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001307 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001308 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001309 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001310 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001311 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001312 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001313 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001314 // OpenMP [2.7.1, Restrictions, p. 9]
1315 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001316 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1317 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001318 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001319 Diag(Tok, diag::err_omp_more_one_clause)
1320 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001321 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001322 }
1323
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001324 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001325 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001326 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001327 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001328 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001329 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001330 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00001331 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00001332 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001333 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001334 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001335 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001336 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001337 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001338 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001339 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001340 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001341 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001342 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001343 case OMPC_is_device_ptr:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001344 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001345 break;
1346 case OMPC_unknown:
1347 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001348 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001349 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001350 break;
1351 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001352 case OMPC_uniform:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001353 if (!WrongDirective)
1354 Diag(Tok, diag::err_omp_unexpected_clause)
1355 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001356 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001357 break;
1358 }
Craig Topper161e4db2014-05-21 06:02:52 +00001359 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001360}
1361
Alexey Bataev2af33e32016-04-07 12:45:37 +00001362/// Parses simple expression in parens for single-expression clauses of OpenMP
1363/// constructs.
1364/// \param RLoc Returned location of right paren.
1365ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1366 SourceLocation &RLoc) {
1367 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1368 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1369 return ExprError();
1370
1371 SourceLocation ELoc = Tok.getLocation();
1372 ExprResult LHS(ParseCastExpression(
1373 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1374 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1375 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1376
1377 // Parse ')'.
1378 T.consumeClose();
1379
1380 RLoc = T.getCloseLocation();
1381 return Val;
1382}
1383
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001384/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001385/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001386/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001387///
Alexey Bataev3778b602014-07-17 07:32:53 +00001388/// final-clause:
1389/// 'final' '(' expression ')'
1390///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001391/// num_threads-clause:
1392/// 'num_threads' '(' expression ')'
1393///
1394/// safelen-clause:
1395/// 'safelen' '(' expression ')'
1396///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001397/// simdlen-clause:
1398/// 'simdlen' '(' expression ')'
1399///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001400/// collapse-clause:
1401/// 'collapse' '(' expression ')'
1402///
Alexey Bataeva0569352015-12-01 10:17:31 +00001403/// priority-clause:
1404/// 'priority' '(' expression ')'
1405///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001406/// grainsize-clause:
1407/// 'grainsize' '(' expression ')'
1408///
Alexey Bataev382967a2015-12-08 12:06:20 +00001409/// num_tasks-clause:
1410/// 'num_tasks' '(' expression ')'
1411///
Alexey Bataev28c75412015-12-15 08:19:24 +00001412/// hint-clause:
1413/// 'hint' '(' expression ')'
1414///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001415OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
1416 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001417 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001418 SourceLocation LLoc = Tok.getLocation();
1419 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001420
Alexey Bataev2af33e32016-04-07 12:45:37 +00001421 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001422
1423 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001424 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001425
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001426 if (ParseOnly)
1427 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00001428 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001429}
1430
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001431/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001432///
1433/// default-clause:
1434/// 'default' '(' 'none' | 'shared' ')
1435///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001436/// proc_bind-clause:
1437/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1438///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001439OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
1440 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001441 SourceLocation Loc = Tok.getLocation();
1442 SourceLocation LOpen = ConsumeToken();
1443 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001444 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001445 if (T.expectAndConsume(diag::err_expected_lparen_after,
1446 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001447 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001448
Alexey Bataeva55ed262014-05-28 06:15:33 +00001449 unsigned Type = getOpenMPSimpleClauseType(
1450 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001451 SourceLocation TypeLoc = Tok.getLocation();
1452 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1453 Tok.isNot(tok::annot_pragma_openmp_end))
1454 ConsumeAnyToken();
1455
1456 // Parse ')'.
1457 T.consumeClose();
1458
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001459 if (ParseOnly)
1460 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001461 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1462 Tok.getLocation());
1463}
1464
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001465/// \brief Parsing of OpenMP clauses like 'ordered'.
1466///
1467/// ordered-clause:
1468/// 'ordered'
1469///
Alexey Bataev236070f2014-06-20 11:19:47 +00001470/// nowait-clause:
1471/// 'nowait'
1472///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001473/// untied-clause:
1474/// 'untied'
1475///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001476/// mergeable-clause:
1477/// 'mergeable'
1478///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001479/// read-clause:
1480/// 'read'
1481///
Alexey Bataev346265e2015-09-25 10:37:12 +00001482/// threads-clause:
1483/// 'threads'
1484///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001485/// simd-clause:
1486/// 'simd'
1487///
Alexey Bataevb825de12015-12-07 10:51:44 +00001488/// nogroup-clause:
1489/// 'nogroup'
1490///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001491OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001492 SourceLocation Loc = Tok.getLocation();
1493 ConsumeAnyToken();
1494
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001495 if (ParseOnly)
1496 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001497 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1498}
1499
1500
Alexey Bataev56dafe82014-06-20 07:16:17 +00001501/// \brief Parsing of OpenMP clauses with single expressions and some additional
1502/// argument like 'schedule' or 'dist_schedule'.
1503///
1504/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001505/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1506/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001507///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001508/// if-clause:
1509/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1510///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001511/// defaultmap:
1512/// 'defaultmap' '(' modifier ':' kind ')'
1513///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001514OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
1515 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00001516 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001517 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001518 // Parse '('.
1519 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1520 if (T.expectAndConsume(diag::err_expected_lparen_after,
1521 getOpenMPClauseName(Kind)))
1522 return nullptr;
1523
1524 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001525 SmallVector<unsigned, 4> Arg;
1526 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001527 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001528 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1529 Arg.resize(NumberOfElements);
1530 KLoc.resize(NumberOfElements);
1531 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1532 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1533 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1534 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001535 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001536 if (KindModifier > OMPC_SCHEDULE_unknown) {
1537 // Parse 'modifier'
1538 Arg[Modifier1] = KindModifier;
1539 KLoc[Modifier1] = Tok.getLocation();
1540 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1541 Tok.isNot(tok::annot_pragma_openmp_end))
1542 ConsumeAnyToken();
1543 if (Tok.is(tok::comma)) {
1544 // Parse ',' 'modifier'
1545 ConsumeAnyToken();
1546 KindModifier = getOpenMPSimpleClauseType(
1547 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1548 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1549 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001550 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001551 KLoc[Modifier2] = Tok.getLocation();
1552 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1553 Tok.isNot(tok::annot_pragma_openmp_end))
1554 ConsumeAnyToken();
1555 }
1556 // Parse ':'
1557 if (Tok.is(tok::colon))
1558 ConsumeAnyToken();
1559 else
1560 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1561 KindModifier = getOpenMPSimpleClauseType(
1562 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1563 }
1564 Arg[ScheduleKind] = KindModifier;
1565 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001566 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1567 Tok.isNot(tok::annot_pragma_openmp_end))
1568 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001569 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1570 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1571 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001572 Tok.is(tok::comma))
1573 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001574 } else if (Kind == OMPC_dist_schedule) {
1575 Arg.push_back(getOpenMPSimpleClauseType(
1576 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1577 KLoc.push_back(Tok.getLocation());
1578 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1579 Tok.isNot(tok::annot_pragma_openmp_end))
1580 ConsumeAnyToken();
1581 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1582 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001583 } else if (Kind == OMPC_defaultmap) {
1584 // Get a defaultmap modifier
1585 Arg.push_back(getOpenMPSimpleClauseType(
1586 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1587 KLoc.push_back(Tok.getLocation());
1588 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1589 Tok.isNot(tok::annot_pragma_openmp_end))
1590 ConsumeAnyToken();
1591 // Parse ':'
1592 if (Tok.is(tok::colon))
1593 ConsumeAnyToken();
1594 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1595 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1596 // Get a defaultmap kind
1597 Arg.push_back(getOpenMPSimpleClauseType(
1598 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1599 KLoc.push_back(Tok.getLocation());
1600 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1601 Tok.isNot(tok::annot_pragma_openmp_end))
1602 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001603 } else {
1604 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001605 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001606 TentativeParsingAction TPA(*this);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001607 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1608 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001609 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001610 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1611 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001612 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001613 } else {
1614 TPA.Revert();
1615 Arg.back() = OMPD_unknown;
1616 }
1617 } else
1618 TPA.Revert();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001619 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001620
Carlo Bertollib4adf552016-01-15 18:50:31 +00001621 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1622 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1623 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001624 if (NeedAnExpression) {
1625 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001626 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1627 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001628 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001629 }
1630
1631 // Parse ')'.
1632 T.consumeClose();
1633
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001634 if (NeedAnExpression && Val.isInvalid())
1635 return nullptr;
1636
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001637 if (ParseOnly)
1638 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001639 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001640 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001641 T.getCloseLocation());
1642}
1643
Alexey Bataevc5e02582014-06-16 07:08:35 +00001644static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1645 UnqualifiedId &ReductionId) {
1646 SourceLocation TemplateKWLoc;
1647 if (ReductionIdScopeSpec.isEmpty()) {
1648 auto OOK = OO_None;
1649 switch (P.getCurToken().getKind()) {
1650 case tok::plus:
1651 OOK = OO_Plus;
1652 break;
1653 case tok::minus:
1654 OOK = OO_Minus;
1655 break;
1656 case tok::star:
1657 OOK = OO_Star;
1658 break;
1659 case tok::amp:
1660 OOK = OO_Amp;
1661 break;
1662 case tok::pipe:
1663 OOK = OO_Pipe;
1664 break;
1665 case tok::caret:
1666 OOK = OO_Caret;
1667 break;
1668 case tok::ampamp:
1669 OOK = OO_AmpAmp;
1670 break;
1671 case tok::pipepipe:
1672 OOK = OO_PipePipe;
1673 break;
1674 default:
1675 break;
1676 }
1677 if (OOK != OO_None) {
1678 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001679 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001680 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1681 return false;
1682 }
1683 }
1684 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1685 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00001686 /*AllowConstructorName*/ false,
1687 /*AllowDeductionGuide*/ false,
1688 nullptr, TemplateKWLoc, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001689}
1690
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001691/// Parses clauses with list.
1692bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1693 OpenMPClauseKind Kind,
1694 SmallVectorImpl<Expr *> &Vars,
1695 OpenMPVarListDataTy &Data) {
1696 UnqualifiedId UnqualifiedReductionId;
1697 bool InvalidReductionId = false;
1698 bool MapTypeModifierSpecified = false;
1699
1700 // Parse '('.
1701 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1702 if (T.expectAndConsume(diag::err_expected_lparen_after,
1703 getOpenMPClauseName(Kind)))
1704 return true;
1705
1706 bool NeedRParenForLinear = false;
1707 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1708 tok::annot_pragma_openmp_end);
1709 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00001710 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
1711 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001712 ColonProtectionRAIIObject ColonRAII(*this);
1713 if (getLangOpts().CPlusPlus)
1714 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1715 /*ObjectType=*/nullptr,
1716 /*EnteringContext=*/false);
1717 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1718 UnqualifiedReductionId);
1719 if (InvalidReductionId) {
1720 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1721 StopBeforeMatch);
1722 }
1723 if (Tok.is(tok::colon))
1724 Data.ColonLoc = ConsumeToken();
1725 else
1726 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1727 if (!InvalidReductionId)
1728 Data.ReductionId =
1729 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1730 } else if (Kind == OMPC_depend) {
1731 // Handle dependency type for depend clause.
1732 ColonProtectionRAIIObject ColonRAII(*this);
1733 Data.DepKind =
1734 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1735 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1736 Data.DepLinMapLoc = Tok.getLocation();
1737
1738 if (Data.DepKind == OMPC_DEPEND_unknown) {
1739 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1740 StopBeforeMatch);
1741 } else {
1742 ConsumeToken();
1743 // Special processing for depend(source) clause.
1744 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1745 // Parse ')'.
1746 T.consumeClose();
1747 return false;
1748 }
1749 }
1750 if (Tok.is(tok::colon))
1751 Data.ColonLoc = ConsumeToken();
1752 else {
1753 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1754 : diag::warn_pragma_expected_colon)
1755 << "dependency type";
1756 }
1757 } else if (Kind == OMPC_linear) {
1758 // Try to parse modifier if any.
1759 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1760 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1761 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1762 Data.DepLinMapLoc = ConsumeToken();
1763 LinearT.consumeOpen();
1764 NeedRParenForLinear = true;
1765 }
1766 } else if (Kind == OMPC_map) {
1767 // Handle map type for map clause.
1768 ColonProtectionRAIIObject ColonRAII(*this);
1769
1770 /// The map clause modifier token can be either a identifier or the C++
1771 /// delete keyword.
1772 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1773 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1774 };
1775
1776 // The first identifier may be a list item, a map-type or a
1777 // map-type-modifier. The map modifier can also be delete which has the same
1778 // spelling of the C++ delete keyword.
1779 Data.MapType =
1780 IsMapClauseModifierToken(Tok)
1781 ? static_cast<OpenMPMapClauseKind>(
1782 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1783 : OMPC_MAP_unknown;
1784 Data.DepLinMapLoc = Tok.getLocation();
1785 bool ColonExpected = false;
1786
1787 if (IsMapClauseModifierToken(Tok)) {
1788 if (PP.LookAhead(0).is(tok::colon)) {
1789 if (Data.MapType == OMPC_MAP_unknown)
1790 Diag(Tok, diag::err_omp_unknown_map_type);
1791 else if (Data.MapType == OMPC_MAP_always)
1792 Diag(Tok, diag::err_omp_map_type_missing);
1793 ConsumeToken();
1794 } else if (PP.LookAhead(0).is(tok::comma)) {
1795 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1796 PP.LookAhead(2).is(tok::colon)) {
1797 Data.MapTypeModifier = Data.MapType;
1798 if (Data.MapTypeModifier != OMPC_MAP_always) {
1799 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1800 Data.MapTypeModifier = OMPC_MAP_unknown;
1801 } else
1802 MapTypeModifierSpecified = true;
1803
1804 ConsumeToken();
1805 ConsumeToken();
1806
1807 Data.MapType =
1808 IsMapClauseModifierToken(Tok)
1809 ? static_cast<OpenMPMapClauseKind>(
1810 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1811 : OMPC_MAP_unknown;
1812 if (Data.MapType == OMPC_MAP_unknown ||
1813 Data.MapType == OMPC_MAP_always)
1814 Diag(Tok, diag::err_omp_unknown_map_type);
1815 ConsumeToken();
1816 } else {
1817 Data.MapType = OMPC_MAP_tofrom;
1818 Data.IsMapTypeImplicit = true;
1819 }
Carlo Bertollid8844b92017-05-03 15:28:48 +00001820 } else if (IsMapClauseModifierToken(PP.LookAhead(0))) {
1821 if (PP.LookAhead(1).is(tok::colon)) {
1822 Data.MapTypeModifier = Data.MapType;
1823 if (Data.MapTypeModifier != OMPC_MAP_always) {
1824 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1825 Data.MapTypeModifier = OMPC_MAP_unknown;
1826 } else
1827 MapTypeModifierSpecified = true;
1828
1829 ConsumeToken();
1830
1831 Data.MapType =
1832 IsMapClauseModifierToken(Tok)
1833 ? static_cast<OpenMPMapClauseKind>(
1834 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1835 : OMPC_MAP_unknown;
1836 if (Data.MapType == OMPC_MAP_unknown ||
1837 Data.MapType == OMPC_MAP_always)
1838 Diag(Tok, diag::err_omp_unknown_map_type);
1839 ConsumeToken();
1840 } else {
1841 Data.MapType = OMPC_MAP_tofrom;
1842 Data.IsMapTypeImplicit = true;
1843 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001844 } else {
1845 Data.MapType = OMPC_MAP_tofrom;
1846 Data.IsMapTypeImplicit = true;
1847 }
1848 } else {
1849 Data.MapType = OMPC_MAP_tofrom;
1850 Data.IsMapTypeImplicit = true;
1851 }
1852
1853 if (Tok.is(tok::colon))
1854 Data.ColonLoc = ConsumeToken();
1855 else if (ColonExpected)
1856 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1857 }
1858
Alexey Bataevfa312f32017-07-21 18:48:21 +00001859 bool IsComma =
1860 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
1861 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1862 (Kind == OMPC_reduction && !InvalidReductionId) ||
1863 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1864 (!MapTypeModifierSpecified ||
1865 Data.MapTypeModifier == OMPC_MAP_always)) ||
1866 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001867 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1868 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1869 Tok.isNot(tok::annot_pragma_openmp_end))) {
1870 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1871 // Parse variable
1872 ExprResult VarExpr =
1873 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1874 if (VarExpr.isUsable())
1875 Vars.push_back(VarExpr.get());
1876 else {
1877 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1878 StopBeforeMatch);
1879 }
1880 // Skip ',' if any
1881 IsComma = Tok.is(tok::comma);
1882 if (IsComma)
1883 ConsumeToken();
1884 else if (Tok.isNot(tok::r_paren) &&
1885 Tok.isNot(tok::annot_pragma_openmp_end) &&
1886 (!MayHaveTail || Tok.isNot(tok::colon)))
1887 Diag(Tok, diag::err_omp_expected_punc)
1888 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1889 : getOpenMPClauseName(Kind))
1890 << (Kind == OMPC_flush);
1891 }
1892
1893 // Parse ')' for linear clause with modifier.
1894 if (NeedRParenForLinear)
1895 LinearT.consumeClose();
1896
1897 // Parse ':' linear-step (or ':' alignment).
1898 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1899 if (MustHaveTail) {
1900 Data.ColonLoc = Tok.getLocation();
1901 SourceLocation ELoc = ConsumeToken();
1902 ExprResult Tail = ParseAssignmentExpression();
1903 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1904 if (Tail.isUsable())
1905 Data.TailExpr = Tail.get();
1906 else
1907 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1908 StopBeforeMatch);
1909 }
1910
1911 // Parse ')'.
1912 T.consumeClose();
1913 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1914 Vars.empty()) ||
1915 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1916 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1917 return true;
1918 return false;
1919}
1920
Alexander Musman1bb328c2014-06-04 13:06:39 +00001921/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00001922/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
1923/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001924///
1925/// private-clause:
1926/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001927/// firstprivate-clause:
1928/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001929/// lastprivate-clause:
1930/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001931/// shared-clause:
1932/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001933/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001934/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001935/// aligned-clause:
1936/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001937/// reduction-clause:
1938/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00001939/// task_reduction-clause:
1940/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00001941/// in_reduction-clause:
1942/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001943/// copyprivate-clause:
1944/// 'copyprivate' '(' list ')'
1945/// flush-clause:
1946/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001947/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001948/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001949/// map-clause:
1950/// 'map' '(' [ [ always , ]
1951/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001952/// to-clause:
1953/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001954/// from-clause:
1955/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001956/// use_device_ptr-clause:
1957/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001958/// is_device_ptr-clause:
1959/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001960///
Alexey Bataev182227b2015-08-20 10:54:39 +00001961/// For 'linear' clause linear-list may have the following forms:
1962/// list
1963/// modifier(list)
1964/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001965OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001966 OpenMPClauseKind Kind,
1967 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001968 SourceLocation Loc = Tok.getLocation();
1969 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001970 SmallVector<Expr *, 4> Vars;
1971 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001972
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001973 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001974 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001975
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001976 if (ParseOnly)
1977 return nullptr;
Alexey Bataevc5e02582014-06-16 07:08:35 +00001978 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001979 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1980 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1981 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1982 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001983}
1984