blob: 77c5d23b14a233116c4aa8efe259618e0c40f837 [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.
722 llvm::SmallSetVector<const NamedDecl*, 16> SameDirectiveDecls;
723 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](
739 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
740 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
741 SameDirectiveDecls);
742 };
743 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, true))
744 break;
745
746 // Consume optional ','.
747 if (Tok.is(tok::comma))
748 ConsumeToken();
749 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000750 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000751 ConsumeAnyToken();
752 return DeclGroupPtrTy();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000753 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000754
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000755 // Skip the last annot_pragma_openmp_end.
756 ConsumeAnyToken();
757
758 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
759 return DeclGroupPtrTy();
760
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000761 llvm::SmallVector<Decl *, 4> Decls;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000762 DKind = ParseOpenMPDirectiveKind(*this);
763 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
764 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +0000765 DeclGroupPtrTy Ptr;
766 // Here we expect to see some function declaration.
767 if (AS == AS_none) {
768 assert(TagType == DeclSpec::TST_unspecified);
769 MaybeParseCXX11Attributes(Attrs);
770 ParsingDeclSpec PDS(*this);
771 Ptr = ParseExternalDeclaration(Attrs, &PDS);
772 } else {
773 Ptr =
774 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
775 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000776 if (Ptr) {
777 DeclGroupRef Ref = Ptr.get();
778 Decls.append(Ref.begin(), Ref.end());
779 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000780 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
781 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +0000782 ConsumeAnnotationToken();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000783 DKind = ParseOpenMPDirectiveKind(*this);
784 if (DKind != OMPD_end_declare_target)
785 TPA.Revert();
786 else
787 TPA.Commit();
788 }
789 }
790
791 if (DKind == OMPD_end_declare_target) {
792 ConsumeAnyToken();
793 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
794 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
795 << getOpenMPDirectiveName(OMPD_end_declare_target);
796 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
797 }
798 // Skip the last annot_pragma_openmp_end.
799 ConsumeAnyToken();
800 } else {
801 Diag(Tok, diag::err_expected_end_declare_target);
802 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
803 }
804 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000805 return DeclGroupPtrTy::make(DeclGroupRef::Create(
806 Actions.getASTContext(), Decls.begin(), Decls.size()));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000807 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000808 case OMPD_unknown:
809 Diag(Tok, diag::err_omp_unknown_directive);
810 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000811 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000812 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000813 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000814 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000815 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000816 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000817 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000818 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000819 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000820 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000821 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000822 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000823 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000824 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000825 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000826 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000827 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000828 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000829 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000830 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000831 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000832 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000833 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000834 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000835 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000836 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000837 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000838 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000839 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000840 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000841 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000842 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000843 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000844 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000845 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000846 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000847 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000848 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000849 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000850 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000851 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000852 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000853 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +0000854 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +0000855 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +0000856 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +0000857 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +0000858 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000859 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +0000860 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000861 break;
862 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000863 while (Tok.isNot(tok::annot_pragma_openmp_end))
864 ConsumeAnyToken();
865 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000866 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000867}
868
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000869/// \brief Parsing of declarative or executable OpenMP directives.
870///
871/// threadprivate-directive:
872/// annot_pragma_openmp 'threadprivate' simple-variable-list
873/// annot_pragma_openmp_end
874///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000875/// declare-reduction-directive:
876/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
877/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
878/// ('omp_priv' '=' <expression>|<function_call>) ')']
879/// annot_pragma_openmp_end
880///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000881/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000882/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000883/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
884/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000885/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000886/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000887/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000888/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000889/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000890/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000891/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000892/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000893/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000894/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +0000895/// 'teams distribute parallel for' | 'target teams' |
896/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +0000897/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +0000898/// 'target teams distribute parallel for simd' |
899/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000900/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000901///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000902StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000903 AllowedConstructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000904 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000905 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000906 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000907 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000908 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +0000909 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
910 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +0000911 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000912 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000913 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000914 // Name of critical directive.
915 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000916 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000917 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000918 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000919
920 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000921 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000922 if (Allowed != ACK_Any) {
923 Diag(Tok, diag::err_omp_immediate_directive)
924 << getOpenMPDirectiveName(DKind) << 0;
925 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000926 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000927 ThreadprivateListParserHelper Helper(this);
928 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000929 // The last seen token is annot_pragma_openmp_end - need to check for
930 // extra tokens.
931 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
932 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000933 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000934 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000935 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000936 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
937 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000938 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
939 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000940 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000941 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000942 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000943 case OMPD_declare_reduction:
944 ConsumeToken();
945 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
946 // The last seen token is annot_pragma_openmp_end - need to check for
947 // extra tokens.
948 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
949 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
950 << getOpenMPDirectiveName(OMPD_declare_reduction);
951 while (Tok.isNot(tok::annot_pragma_openmp_end))
952 ConsumeAnyToken();
953 }
954 ConsumeAnyToken();
955 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
956 } else
957 SkipUntil(tok::annot_pragma_openmp_end);
958 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000959 case OMPD_flush:
960 if (PP.LookAhead(0).is(tok::l_paren)) {
961 FlushHasClause = true;
962 // Push copy of the current token back to stream to properly parse
963 // pseudo-clause OMPFlushClause.
964 PP.EnterToken(Tok);
965 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000966 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +0000967 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000968 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000969 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000970 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000971 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000972 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000973 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000974 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000975 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000976 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000977 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000978 }
979 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000980 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000981 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000982 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000983 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000984 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000985 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000986 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000987 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000988 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000989 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000990 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000991 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000992 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000993 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000994 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000995 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000996 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000997 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000998 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000999 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001000 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001001 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001002 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001003 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001004 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001005 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001006 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001007 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001008 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001009 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001010 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001011 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001012 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001013 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001014 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001015 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001016 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001017 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001018 case OMPD_target_teams_distribute_parallel_for_simd:
1019 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001020 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001021 // Parse directive name of the 'critical' directive if any.
1022 if (DKind == OMPD_critical) {
1023 BalancedDelimiterTracker T(*this, tok::l_paren,
1024 tok::annot_pragma_openmp_end);
1025 if (!T.consumeOpen()) {
1026 if (Tok.isAnyIdentifier()) {
1027 DirName =
1028 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1029 ConsumeAnyToken();
1030 } else {
1031 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1032 }
1033 T.consumeClose();
1034 }
Alexey Bataev80909872015-07-02 11:25:17 +00001035 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001036 CancelRegion = ParseOpenMPDirectiveKind(*this);
1037 if (Tok.isNot(tok::annot_pragma_openmp_end))
1038 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001039 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001040
Alexey Bataevf29276e2014-06-18 04:14:57 +00001041 if (isOpenMPLoopDirective(DKind))
1042 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1043 if (isOpenMPSimdDirective(DKind))
1044 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1045 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001046 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001047
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001048 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001049 OpenMPClauseKind CKind =
1050 Tok.isAnnotation()
1051 ? OMPC_unknown
1052 : FlushHasClause ? OMPC_flush
1053 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001054 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001055 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001056 OMPClause *Clause =
1057 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001058 FirstClauses[CKind].setInt(true);
1059 if (Clause) {
1060 FirstClauses[CKind].setPointer(Clause);
1061 Clauses.push_back(Clause);
1062 }
1063
1064 // Skip ',' if any.
1065 if (Tok.is(tok::comma))
1066 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001067 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001068 }
1069 // End location of the directive.
1070 EndLoc = Tok.getLocation();
1071 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001072 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001073
Alexey Bataeveb482352015-12-18 05:05:56 +00001074 // OpenMP [2.13.8, ordered Construct, Syntax]
1075 // If the depend clause is specified, the ordered construct is a stand-alone
1076 // directive.
1077 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001078 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001079 Diag(Loc, diag::err_omp_immediate_directive)
1080 << getOpenMPDirectiveName(DKind) << 1
1081 << getOpenMPClauseName(OMPC_depend);
1082 }
1083 HasAssociatedStatement = false;
1084 }
1085
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001086 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001087 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001088 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001089 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001090 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1091 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1092 // should have at least one compound statement scope within it.
1093 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001094 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001095 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1096 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001097 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001098 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1099 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1100 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001101 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001102 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001103 Directive = Actions.ActOnOpenMPExecutableDirective(
1104 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1105 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001106
1107 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001108 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001109 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001110 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001111 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001112 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001113 case OMPD_declare_target:
1114 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001115 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001116 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001117 SkipUntil(tok::annot_pragma_openmp_end);
1118 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001119 case OMPD_unknown:
1120 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001121 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001122 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001123 }
1124 return Directive;
1125}
1126
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001127// Parses simple list:
1128// simple-variable-list:
1129// '(' id-expression {, id-expression} ')'
1130//
1131bool Parser::ParseOpenMPSimpleVarList(
1132 OpenMPDirectiveKind Kind,
1133 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1134 Callback,
1135 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001136 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001137 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001138 if (T.expectAndConsume(diag::err_expected_lparen_after,
1139 getOpenMPDirectiveName(Kind)))
1140 return true;
1141 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001142 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001143
1144 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001145 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001146 CXXScopeSpec SS;
1147 SourceLocation TemplateKWLoc;
1148 UnqualifiedId Name;
1149 // Read var name.
1150 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001151 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001152
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001153 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001154 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001155 IsCorrect = false;
1156 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001157 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001158 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001159 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001160 IsCorrect = false;
1161 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001162 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001163 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1164 Tok.isNot(tok::annot_pragma_openmp_end)) {
1165 IsCorrect = false;
1166 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001167 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001168 Diag(PrevTok.getLocation(), diag::err_expected)
1169 << tok::identifier
1170 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001171 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001172 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001173 }
1174 // Consume ','.
1175 if (Tok.is(tok::comma)) {
1176 ConsumeToken();
1177 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001178 }
1179
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001180 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001181 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001182 IsCorrect = false;
1183 }
1184
1185 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001186 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001187
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001188 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001189}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001190
1191/// \brief Parsing of OpenMP clauses.
1192///
1193/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001194/// if-clause | final-clause | num_threads-clause | safelen-clause |
1195/// default-clause | private-clause | firstprivate-clause | shared-clause
1196/// | linear-clause | aligned-clause | collapse-clause |
1197/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001198/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001199/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001200/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001201/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001202/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001203/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001204/// from-clause | is_device_ptr-clause | task_reduction-clause |
1205/// in_reduction-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001206///
1207OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1208 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001209 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001210 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001211 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001212 // Check if clause is allowed for the given directive.
1213 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001214 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1215 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001216 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001217 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001218 }
1219
1220 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001221 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001222 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001223 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001224 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001225 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001226 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001227 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001228 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001229 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001230 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001231 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001232 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001233 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001234 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001235 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001236 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001237 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001238 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001239 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001240 // OpenMP [2.9.1, target data construct, Restrictions]
1241 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001242 // OpenMP [2.11.1, task Construct, Restrictions]
1243 // At most one if clause can appear on the directive.
1244 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001245 // OpenMP [teams Construct, Restrictions]
1246 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001247 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001248 // OpenMP [2.9.1, task Construct, Restrictions]
1249 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001250 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1251 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001252 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1253 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001254 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001255 Diag(Tok, diag::err_omp_more_one_clause)
1256 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001257 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001258 }
1259
Alexey Bataev10e775f2015-07-30 11:36:16 +00001260 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001261 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001262 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001263 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001264 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001265 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001266 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001267 // OpenMP [2.14.3.1, Restrictions]
1268 // Only a single default clause may be specified on a parallel, task or
1269 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001270 // OpenMP [2.5, parallel Construct, Restrictions]
1271 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001272 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001273 Diag(Tok, diag::err_omp_more_one_clause)
1274 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001275 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001276 }
1277
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001278 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001279 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001280 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001281 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001282 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001283 // OpenMP [2.7.1, Restrictions, p. 3]
1284 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001285 // OpenMP [2.10.4, Restrictions, p. 106]
1286 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001287 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001288 Diag(Tok, diag::err_omp_more_one_clause)
1289 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001290 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001291 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001292 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001293
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001294 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001295 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001296 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001297 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001298 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001299 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001300 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001301 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001302 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001303 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001304 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001305 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001306 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001307 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001308 // OpenMP [2.7.1, Restrictions, p. 9]
1309 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001310 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1311 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001312 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001313 Diag(Tok, diag::err_omp_more_one_clause)
1314 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001315 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001316 }
1317
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001318 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001319 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001320 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001321 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001322 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001323 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001324 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00001325 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00001326 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001327 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001328 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001329 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001330 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001331 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001332 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001333 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001334 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001335 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001336 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001337 case OMPC_is_device_ptr:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001338 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001339 break;
1340 case OMPC_unknown:
1341 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001342 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001343 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001344 break;
1345 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001346 case OMPC_uniform:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001347 if (!WrongDirective)
1348 Diag(Tok, diag::err_omp_unexpected_clause)
1349 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001350 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001351 break;
1352 }
Craig Topper161e4db2014-05-21 06:02:52 +00001353 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001354}
1355
Alexey Bataev2af33e32016-04-07 12:45:37 +00001356/// Parses simple expression in parens for single-expression clauses of OpenMP
1357/// constructs.
1358/// \param RLoc Returned location of right paren.
1359ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1360 SourceLocation &RLoc) {
1361 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1362 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1363 return ExprError();
1364
1365 SourceLocation ELoc = Tok.getLocation();
1366 ExprResult LHS(ParseCastExpression(
1367 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1368 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1369 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1370
1371 // Parse ')'.
1372 T.consumeClose();
1373
1374 RLoc = T.getCloseLocation();
1375 return Val;
1376}
1377
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001378/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001379/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001380/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001381///
Alexey Bataev3778b602014-07-17 07:32:53 +00001382/// final-clause:
1383/// 'final' '(' expression ')'
1384///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001385/// num_threads-clause:
1386/// 'num_threads' '(' expression ')'
1387///
1388/// safelen-clause:
1389/// 'safelen' '(' expression ')'
1390///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001391/// simdlen-clause:
1392/// 'simdlen' '(' expression ')'
1393///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001394/// collapse-clause:
1395/// 'collapse' '(' expression ')'
1396///
Alexey Bataeva0569352015-12-01 10:17:31 +00001397/// priority-clause:
1398/// 'priority' '(' expression ')'
1399///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001400/// grainsize-clause:
1401/// 'grainsize' '(' expression ')'
1402///
Alexey Bataev382967a2015-12-08 12:06:20 +00001403/// num_tasks-clause:
1404/// 'num_tasks' '(' expression ')'
1405///
Alexey Bataev28c75412015-12-15 08:19:24 +00001406/// hint-clause:
1407/// 'hint' '(' expression ')'
1408///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001409OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
1410 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001411 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001412 SourceLocation LLoc = Tok.getLocation();
1413 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001414
Alexey Bataev2af33e32016-04-07 12:45:37 +00001415 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001416
1417 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001418 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001419
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001420 if (ParseOnly)
1421 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00001422 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001423}
1424
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001425/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001426///
1427/// default-clause:
1428/// 'default' '(' 'none' | 'shared' ')
1429///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001430/// proc_bind-clause:
1431/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1432///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001433OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
1434 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001435 SourceLocation Loc = Tok.getLocation();
1436 SourceLocation LOpen = ConsumeToken();
1437 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001438 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001439 if (T.expectAndConsume(diag::err_expected_lparen_after,
1440 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001441 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001442
Alexey Bataeva55ed262014-05-28 06:15:33 +00001443 unsigned Type = getOpenMPSimpleClauseType(
1444 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001445 SourceLocation TypeLoc = Tok.getLocation();
1446 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1447 Tok.isNot(tok::annot_pragma_openmp_end))
1448 ConsumeAnyToken();
1449
1450 // Parse ')'.
1451 T.consumeClose();
1452
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001453 if (ParseOnly)
1454 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001455 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1456 Tok.getLocation());
1457}
1458
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001459/// \brief Parsing of OpenMP clauses like 'ordered'.
1460///
1461/// ordered-clause:
1462/// 'ordered'
1463///
Alexey Bataev236070f2014-06-20 11:19:47 +00001464/// nowait-clause:
1465/// 'nowait'
1466///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001467/// untied-clause:
1468/// 'untied'
1469///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001470/// mergeable-clause:
1471/// 'mergeable'
1472///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001473/// read-clause:
1474/// 'read'
1475///
Alexey Bataev346265e2015-09-25 10:37:12 +00001476/// threads-clause:
1477/// 'threads'
1478///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001479/// simd-clause:
1480/// 'simd'
1481///
Alexey Bataevb825de12015-12-07 10:51:44 +00001482/// nogroup-clause:
1483/// 'nogroup'
1484///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001485OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001486 SourceLocation Loc = Tok.getLocation();
1487 ConsumeAnyToken();
1488
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001489 if (ParseOnly)
1490 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001491 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1492}
1493
1494
Alexey Bataev56dafe82014-06-20 07:16:17 +00001495/// \brief Parsing of OpenMP clauses with single expressions and some additional
1496/// argument like 'schedule' or 'dist_schedule'.
1497///
1498/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001499/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1500/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001501///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001502/// if-clause:
1503/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1504///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001505/// defaultmap:
1506/// 'defaultmap' '(' modifier ':' kind ')'
1507///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001508OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
1509 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00001510 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001511 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001512 // Parse '('.
1513 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1514 if (T.expectAndConsume(diag::err_expected_lparen_after,
1515 getOpenMPClauseName(Kind)))
1516 return nullptr;
1517
1518 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001519 SmallVector<unsigned, 4> Arg;
1520 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001521 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001522 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1523 Arg.resize(NumberOfElements);
1524 KLoc.resize(NumberOfElements);
1525 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1526 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1527 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1528 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001529 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001530 if (KindModifier > OMPC_SCHEDULE_unknown) {
1531 // Parse 'modifier'
1532 Arg[Modifier1] = KindModifier;
1533 KLoc[Modifier1] = Tok.getLocation();
1534 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1535 Tok.isNot(tok::annot_pragma_openmp_end))
1536 ConsumeAnyToken();
1537 if (Tok.is(tok::comma)) {
1538 // Parse ',' 'modifier'
1539 ConsumeAnyToken();
1540 KindModifier = getOpenMPSimpleClauseType(
1541 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1542 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1543 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001544 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001545 KLoc[Modifier2] = Tok.getLocation();
1546 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1547 Tok.isNot(tok::annot_pragma_openmp_end))
1548 ConsumeAnyToken();
1549 }
1550 // Parse ':'
1551 if (Tok.is(tok::colon))
1552 ConsumeAnyToken();
1553 else
1554 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1555 KindModifier = getOpenMPSimpleClauseType(
1556 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1557 }
1558 Arg[ScheduleKind] = KindModifier;
1559 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001560 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1561 Tok.isNot(tok::annot_pragma_openmp_end))
1562 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001563 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1564 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1565 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001566 Tok.is(tok::comma))
1567 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001568 } else if (Kind == OMPC_dist_schedule) {
1569 Arg.push_back(getOpenMPSimpleClauseType(
1570 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1571 KLoc.push_back(Tok.getLocation());
1572 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1573 Tok.isNot(tok::annot_pragma_openmp_end))
1574 ConsumeAnyToken();
1575 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1576 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001577 } else if (Kind == OMPC_defaultmap) {
1578 // Get a defaultmap modifier
1579 Arg.push_back(getOpenMPSimpleClauseType(
1580 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1581 KLoc.push_back(Tok.getLocation());
1582 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1583 Tok.isNot(tok::annot_pragma_openmp_end))
1584 ConsumeAnyToken();
1585 // Parse ':'
1586 if (Tok.is(tok::colon))
1587 ConsumeAnyToken();
1588 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1589 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1590 // Get a defaultmap kind
1591 Arg.push_back(getOpenMPSimpleClauseType(
1592 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1593 KLoc.push_back(Tok.getLocation());
1594 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1595 Tok.isNot(tok::annot_pragma_openmp_end))
1596 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001597 } else {
1598 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001599 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001600 TentativeParsingAction TPA(*this);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001601 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1602 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001603 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001604 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1605 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001606 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001607 } else {
1608 TPA.Revert();
1609 Arg.back() = OMPD_unknown;
1610 }
1611 } else
1612 TPA.Revert();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001613 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001614
Carlo Bertollib4adf552016-01-15 18:50:31 +00001615 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1616 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1617 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001618 if (NeedAnExpression) {
1619 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001620 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1621 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001622 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001623 }
1624
1625 // Parse ')'.
1626 T.consumeClose();
1627
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001628 if (NeedAnExpression && Val.isInvalid())
1629 return nullptr;
1630
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001631 if (ParseOnly)
1632 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001633 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001634 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001635 T.getCloseLocation());
1636}
1637
Alexey Bataevc5e02582014-06-16 07:08:35 +00001638static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1639 UnqualifiedId &ReductionId) {
1640 SourceLocation TemplateKWLoc;
1641 if (ReductionIdScopeSpec.isEmpty()) {
1642 auto OOK = OO_None;
1643 switch (P.getCurToken().getKind()) {
1644 case tok::plus:
1645 OOK = OO_Plus;
1646 break;
1647 case tok::minus:
1648 OOK = OO_Minus;
1649 break;
1650 case tok::star:
1651 OOK = OO_Star;
1652 break;
1653 case tok::amp:
1654 OOK = OO_Amp;
1655 break;
1656 case tok::pipe:
1657 OOK = OO_Pipe;
1658 break;
1659 case tok::caret:
1660 OOK = OO_Caret;
1661 break;
1662 case tok::ampamp:
1663 OOK = OO_AmpAmp;
1664 break;
1665 case tok::pipepipe:
1666 OOK = OO_PipePipe;
1667 break;
1668 default:
1669 break;
1670 }
1671 if (OOK != OO_None) {
1672 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001673 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001674 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1675 return false;
1676 }
1677 }
1678 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1679 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00001680 /*AllowConstructorName*/ false,
1681 /*AllowDeductionGuide*/ false,
1682 nullptr, TemplateKWLoc, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001683}
1684
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001685/// Parses clauses with list.
1686bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1687 OpenMPClauseKind Kind,
1688 SmallVectorImpl<Expr *> &Vars,
1689 OpenMPVarListDataTy &Data) {
1690 UnqualifiedId UnqualifiedReductionId;
1691 bool InvalidReductionId = false;
1692 bool MapTypeModifierSpecified = false;
1693
1694 // Parse '('.
1695 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1696 if (T.expectAndConsume(diag::err_expected_lparen_after,
1697 getOpenMPClauseName(Kind)))
1698 return true;
1699
1700 bool NeedRParenForLinear = false;
1701 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1702 tok::annot_pragma_openmp_end);
1703 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00001704 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
1705 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001706 ColonProtectionRAIIObject ColonRAII(*this);
1707 if (getLangOpts().CPlusPlus)
1708 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1709 /*ObjectType=*/nullptr,
1710 /*EnteringContext=*/false);
1711 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1712 UnqualifiedReductionId);
1713 if (InvalidReductionId) {
1714 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1715 StopBeforeMatch);
1716 }
1717 if (Tok.is(tok::colon))
1718 Data.ColonLoc = ConsumeToken();
1719 else
1720 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1721 if (!InvalidReductionId)
1722 Data.ReductionId =
1723 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1724 } else if (Kind == OMPC_depend) {
1725 // Handle dependency type for depend clause.
1726 ColonProtectionRAIIObject ColonRAII(*this);
1727 Data.DepKind =
1728 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1729 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1730 Data.DepLinMapLoc = Tok.getLocation();
1731
1732 if (Data.DepKind == OMPC_DEPEND_unknown) {
1733 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1734 StopBeforeMatch);
1735 } else {
1736 ConsumeToken();
1737 // Special processing for depend(source) clause.
1738 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1739 // Parse ')'.
1740 T.consumeClose();
1741 return false;
1742 }
1743 }
1744 if (Tok.is(tok::colon))
1745 Data.ColonLoc = ConsumeToken();
1746 else {
1747 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1748 : diag::warn_pragma_expected_colon)
1749 << "dependency type";
1750 }
1751 } else if (Kind == OMPC_linear) {
1752 // Try to parse modifier if any.
1753 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1754 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1755 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1756 Data.DepLinMapLoc = ConsumeToken();
1757 LinearT.consumeOpen();
1758 NeedRParenForLinear = true;
1759 }
1760 } else if (Kind == OMPC_map) {
1761 // Handle map type for map clause.
1762 ColonProtectionRAIIObject ColonRAII(*this);
1763
1764 /// The map clause modifier token can be either a identifier or the C++
1765 /// delete keyword.
1766 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1767 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1768 };
1769
1770 // The first identifier may be a list item, a map-type or a
1771 // map-type-modifier. The map modifier can also be delete which has the same
1772 // spelling of the C++ delete keyword.
1773 Data.MapType =
1774 IsMapClauseModifierToken(Tok)
1775 ? static_cast<OpenMPMapClauseKind>(
1776 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1777 : OMPC_MAP_unknown;
1778 Data.DepLinMapLoc = Tok.getLocation();
1779 bool ColonExpected = false;
1780
1781 if (IsMapClauseModifierToken(Tok)) {
1782 if (PP.LookAhead(0).is(tok::colon)) {
1783 if (Data.MapType == OMPC_MAP_unknown)
1784 Diag(Tok, diag::err_omp_unknown_map_type);
1785 else if (Data.MapType == OMPC_MAP_always)
1786 Diag(Tok, diag::err_omp_map_type_missing);
1787 ConsumeToken();
1788 } else if (PP.LookAhead(0).is(tok::comma)) {
1789 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1790 PP.LookAhead(2).is(tok::colon)) {
1791 Data.MapTypeModifier = Data.MapType;
1792 if (Data.MapTypeModifier != OMPC_MAP_always) {
1793 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1794 Data.MapTypeModifier = OMPC_MAP_unknown;
1795 } else
1796 MapTypeModifierSpecified = true;
1797
1798 ConsumeToken();
1799 ConsumeToken();
1800
1801 Data.MapType =
1802 IsMapClauseModifierToken(Tok)
1803 ? static_cast<OpenMPMapClauseKind>(
1804 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1805 : OMPC_MAP_unknown;
1806 if (Data.MapType == OMPC_MAP_unknown ||
1807 Data.MapType == OMPC_MAP_always)
1808 Diag(Tok, diag::err_omp_unknown_map_type);
1809 ConsumeToken();
1810 } else {
1811 Data.MapType = OMPC_MAP_tofrom;
1812 Data.IsMapTypeImplicit = true;
1813 }
Carlo Bertollid8844b92017-05-03 15:28:48 +00001814 } else if (IsMapClauseModifierToken(PP.LookAhead(0))) {
1815 if (PP.LookAhead(1).is(tok::colon)) {
1816 Data.MapTypeModifier = Data.MapType;
1817 if (Data.MapTypeModifier != OMPC_MAP_always) {
1818 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1819 Data.MapTypeModifier = OMPC_MAP_unknown;
1820 } else
1821 MapTypeModifierSpecified = true;
1822
1823 ConsumeToken();
1824
1825 Data.MapType =
1826 IsMapClauseModifierToken(Tok)
1827 ? static_cast<OpenMPMapClauseKind>(
1828 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1829 : OMPC_MAP_unknown;
1830 if (Data.MapType == OMPC_MAP_unknown ||
1831 Data.MapType == OMPC_MAP_always)
1832 Diag(Tok, diag::err_omp_unknown_map_type);
1833 ConsumeToken();
1834 } else {
1835 Data.MapType = OMPC_MAP_tofrom;
1836 Data.IsMapTypeImplicit = true;
1837 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001838 } else {
1839 Data.MapType = OMPC_MAP_tofrom;
1840 Data.IsMapTypeImplicit = true;
1841 }
1842 } else {
1843 Data.MapType = OMPC_MAP_tofrom;
1844 Data.IsMapTypeImplicit = true;
1845 }
1846
1847 if (Tok.is(tok::colon))
1848 Data.ColonLoc = ConsumeToken();
1849 else if (ColonExpected)
1850 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1851 }
1852
Alexey Bataevfa312f32017-07-21 18:48:21 +00001853 bool IsComma =
1854 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
1855 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1856 (Kind == OMPC_reduction && !InvalidReductionId) ||
1857 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1858 (!MapTypeModifierSpecified ||
1859 Data.MapTypeModifier == OMPC_MAP_always)) ||
1860 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001861 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1862 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1863 Tok.isNot(tok::annot_pragma_openmp_end))) {
1864 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1865 // Parse variable
1866 ExprResult VarExpr =
1867 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1868 if (VarExpr.isUsable())
1869 Vars.push_back(VarExpr.get());
1870 else {
1871 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1872 StopBeforeMatch);
1873 }
1874 // Skip ',' if any
1875 IsComma = Tok.is(tok::comma);
1876 if (IsComma)
1877 ConsumeToken();
1878 else if (Tok.isNot(tok::r_paren) &&
1879 Tok.isNot(tok::annot_pragma_openmp_end) &&
1880 (!MayHaveTail || Tok.isNot(tok::colon)))
1881 Diag(Tok, diag::err_omp_expected_punc)
1882 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1883 : getOpenMPClauseName(Kind))
1884 << (Kind == OMPC_flush);
1885 }
1886
1887 // Parse ')' for linear clause with modifier.
1888 if (NeedRParenForLinear)
1889 LinearT.consumeClose();
1890
1891 // Parse ':' linear-step (or ':' alignment).
1892 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1893 if (MustHaveTail) {
1894 Data.ColonLoc = Tok.getLocation();
1895 SourceLocation ELoc = ConsumeToken();
1896 ExprResult Tail = ParseAssignmentExpression();
1897 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1898 if (Tail.isUsable())
1899 Data.TailExpr = Tail.get();
1900 else
1901 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1902 StopBeforeMatch);
1903 }
1904
1905 // Parse ')'.
1906 T.consumeClose();
1907 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1908 Vars.empty()) ||
1909 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1910 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1911 return true;
1912 return false;
1913}
1914
Alexander Musman1bb328c2014-06-04 13:06:39 +00001915/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00001916/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
1917/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001918///
1919/// private-clause:
1920/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001921/// firstprivate-clause:
1922/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001923/// lastprivate-clause:
1924/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001925/// shared-clause:
1926/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001927/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001928/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001929/// aligned-clause:
1930/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001931/// reduction-clause:
1932/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00001933/// task_reduction-clause:
1934/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00001935/// in_reduction-clause:
1936/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001937/// copyprivate-clause:
1938/// 'copyprivate' '(' list ')'
1939/// flush-clause:
1940/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001941/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001942/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001943/// map-clause:
1944/// 'map' '(' [ [ always , ]
1945/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001946/// to-clause:
1947/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001948/// from-clause:
1949/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001950/// use_device_ptr-clause:
1951/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001952/// is_device_ptr-clause:
1953/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001954///
Alexey Bataev182227b2015-08-20 10:54:39 +00001955/// For 'linear' clause linear-list may have the following forms:
1956/// list
1957/// modifier(list)
1958/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001959OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001960 OpenMPClauseKind Kind,
1961 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001962 SourceLocation Loc = Tok.getLocation();
1963 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001964 SmallVector<Expr *, 4> Vars;
1965 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001966
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001967 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001968 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001969
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001970 if (ParseOnly)
1971 return nullptr;
Alexey Bataevc5e02582014-06-16 07:08:35 +00001972 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001973 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1974 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1975 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1976 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001977}
1978