blob: e9f2029fb705a98fc54998299ed32602ee7404f0 [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
761 DKind = ParseOpenMPDirectiveKind(*this);
762 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
763 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +0000764 DeclGroupPtrTy Ptr;
765 // Here we expect to see some function declaration.
766 if (AS == AS_none) {
767 assert(TagType == DeclSpec::TST_unspecified);
768 MaybeParseCXX11Attributes(Attrs);
769 ParsingDeclSpec PDS(*this);
770 Ptr = ParseExternalDeclaration(Attrs, &PDS);
771 } else {
772 Ptr =
773 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
774 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000775 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
776 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +0000777 ConsumeAnnotationToken();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000778 DKind = ParseOpenMPDirectiveKind(*this);
779 if (DKind != OMPD_end_declare_target)
780 TPA.Revert();
781 else
782 TPA.Commit();
783 }
784 }
785
786 if (DKind == OMPD_end_declare_target) {
787 ConsumeAnyToken();
788 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
789 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
790 << getOpenMPDirectiveName(OMPD_end_declare_target);
791 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
792 }
793 // Skip the last annot_pragma_openmp_end.
794 ConsumeAnyToken();
795 } else {
796 Diag(Tok, diag::err_expected_end_declare_target);
797 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
798 }
799 Actions.ActOnFinishOpenMPDeclareTargetDirective();
800 return DeclGroupPtrTy();
801 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000802 case OMPD_unknown:
803 Diag(Tok, diag::err_omp_unknown_directive);
804 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000805 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000806 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000807 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000808 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000809 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000810 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000811 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000812 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000813 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000814 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000815 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000816 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000817 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000818 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000819 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000820 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000821 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000822 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000823 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000824 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000825 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000826 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000827 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000828 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000829 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000830 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000831 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000832 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000833 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000834 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000835 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000836 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000837 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000838 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000839 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000840 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000841 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000842 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000843 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000844 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000845 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000846 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000847 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +0000848 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +0000849 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +0000850 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +0000851 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +0000852 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000853 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000854 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000855 break;
856 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000857 while (Tok.isNot(tok::annot_pragma_openmp_end))
858 ConsumeAnyToken();
859 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000860 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000861}
862
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000863/// \brief Parsing of declarative or executable OpenMP directives.
864///
865/// threadprivate-directive:
866/// annot_pragma_openmp 'threadprivate' simple-variable-list
867/// annot_pragma_openmp_end
868///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000869/// declare-reduction-directive:
870/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
871/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
872/// ('omp_priv' '=' <expression>|<function_call>) ')']
873/// annot_pragma_openmp_end
874///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000875/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000876/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000877/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
878/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000879/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000880/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000881/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000882/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000883/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000884/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000885/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000886/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000887/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000888/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +0000889/// 'teams distribute parallel for' | 'target teams' |
890/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +0000891/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +0000892/// 'target teams distribute parallel for simd' |
893/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000894/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000895///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000896StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000897 AllowedConstructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000898 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000899 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000900 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000901 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000902 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +0000903 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
904 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +0000905 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000906 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000907 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000908 // Name of critical directive.
909 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000910 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000911 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000912 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000913
914 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000915 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000916 if (Allowed != ACK_Any) {
917 Diag(Tok, diag::err_omp_immediate_directive)
918 << getOpenMPDirectiveName(DKind) << 0;
919 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000920 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000921 ThreadprivateListParserHelper Helper(this);
922 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000923 // The last seen token is annot_pragma_openmp_end - need to check for
924 // extra tokens.
925 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
926 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000927 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000928 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000929 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000930 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
931 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000932 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
933 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000934 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000935 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000936 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000937 case OMPD_declare_reduction:
938 ConsumeToken();
939 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
940 // The last seen token is annot_pragma_openmp_end - need to check for
941 // extra tokens.
942 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
943 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
944 << getOpenMPDirectiveName(OMPD_declare_reduction);
945 while (Tok.isNot(tok::annot_pragma_openmp_end))
946 ConsumeAnyToken();
947 }
948 ConsumeAnyToken();
949 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
950 } else
951 SkipUntil(tok::annot_pragma_openmp_end);
952 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000953 case OMPD_flush:
954 if (PP.LookAhead(0).is(tok::l_paren)) {
955 FlushHasClause = true;
956 // Push copy of the current token back to stream to properly parse
957 // pseudo-clause OMPFlushClause.
958 PP.EnterToken(Tok);
959 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000960 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +0000961 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000962 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000963 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000964 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000965 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000966 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000967 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000968 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000969 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000970 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000971 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000972 }
973 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000974 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000975 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000976 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000977 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000978 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000979 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000980 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000981 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000982 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000983 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000984 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000985 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000986 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000987 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000988 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000989 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000990 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000991 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000992 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000993 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000994 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000995 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000996 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000997 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000998 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000999 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001000 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001001 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001002 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001003 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001004 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001005 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001006 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001007 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001008 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001009 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001010 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001011 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001012 case OMPD_target_teams_distribute_parallel_for_simd:
1013 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001014 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001015 // Parse directive name of the 'critical' directive if any.
1016 if (DKind == OMPD_critical) {
1017 BalancedDelimiterTracker T(*this, tok::l_paren,
1018 tok::annot_pragma_openmp_end);
1019 if (!T.consumeOpen()) {
1020 if (Tok.isAnyIdentifier()) {
1021 DirName =
1022 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1023 ConsumeAnyToken();
1024 } else {
1025 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1026 }
1027 T.consumeClose();
1028 }
Alexey Bataev80909872015-07-02 11:25:17 +00001029 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001030 CancelRegion = ParseOpenMPDirectiveKind(*this);
1031 if (Tok.isNot(tok::annot_pragma_openmp_end))
1032 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001033 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001034
Alexey Bataevf29276e2014-06-18 04:14:57 +00001035 if (isOpenMPLoopDirective(DKind))
1036 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1037 if (isOpenMPSimdDirective(DKind))
1038 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1039 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001040 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001041
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001042 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001043 OpenMPClauseKind CKind =
1044 Tok.isAnnotation()
1045 ? OMPC_unknown
1046 : FlushHasClause ? OMPC_flush
1047 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001048 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001049 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001050 OMPClause *Clause =
1051 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001052 FirstClauses[CKind].setInt(true);
1053 if (Clause) {
1054 FirstClauses[CKind].setPointer(Clause);
1055 Clauses.push_back(Clause);
1056 }
1057
1058 // Skip ',' if any.
1059 if (Tok.is(tok::comma))
1060 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001061 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001062 }
1063 // End location of the directive.
1064 EndLoc = Tok.getLocation();
1065 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001066 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001067
Alexey Bataeveb482352015-12-18 05:05:56 +00001068 // OpenMP [2.13.8, ordered Construct, Syntax]
1069 // If the depend clause is specified, the ordered construct is a stand-alone
1070 // directive.
1071 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001072 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001073 Diag(Loc, diag::err_omp_immediate_directive)
1074 << getOpenMPDirectiveName(DKind) << 1
1075 << getOpenMPClauseName(OMPC_depend);
1076 }
1077 HasAssociatedStatement = false;
1078 }
1079
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001080 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001081 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001082 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001083 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001084 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1085 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1086 // should have at least one compound statement scope within it.
1087 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001088 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001089 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1090 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001091 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001092 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1093 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1094 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001095 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001096 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001097 Directive = Actions.ActOnOpenMPExecutableDirective(
1098 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1099 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001100
1101 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001102 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001103 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001104 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001105 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001106 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001107 case OMPD_declare_target:
1108 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001109 Diag(Tok, diag::err_omp_unexpected_directive)
1110 << getOpenMPDirectiveName(DKind);
1111 SkipUntil(tok::annot_pragma_openmp_end);
1112 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001113 case OMPD_unknown:
1114 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001115 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001116 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001117 }
1118 return Directive;
1119}
1120
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001121// Parses simple list:
1122// simple-variable-list:
1123// '(' id-expression {, id-expression} ')'
1124//
1125bool Parser::ParseOpenMPSimpleVarList(
1126 OpenMPDirectiveKind Kind,
1127 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1128 Callback,
1129 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001130 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001131 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001132 if (T.expectAndConsume(diag::err_expected_lparen_after,
1133 getOpenMPDirectiveName(Kind)))
1134 return true;
1135 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001136 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001137
1138 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001139 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001140 CXXScopeSpec SS;
1141 SourceLocation TemplateKWLoc;
1142 UnqualifiedId Name;
1143 // Read var name.
1144 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001145 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001146
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001147 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001148 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001149 IsCorrect = false;
1150 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001151 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001152 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001153 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001154 IsCorrect = false;
1155 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001156 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001157 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1158 Tok.isNot(tok::annot_pragma_openmp_end)) {
1159 IsCorrect = false;
1160 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001161 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001162 Diag(PrevTok.getLocation(), diag::err_expected)
1163 << tok::identifier
1164 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001165 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001166 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001167 }
1168 // Consume ','.
1169 if (Tok.is(tok::comma)) {
1170 ConsumeToken();
1171 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001172 }
1173
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001174 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001175 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001176 IsCorrect = false;
1177 }
1178
1179 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001180 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001181
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001182 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001183}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001184
1185/// \brief Parsing of OpenMP clauses.
1186///
1187/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001188/// if-clause | final-clause | num_threads-clause | safelen-clause |
1189/// default-clause | private-clause | firstprivate-clause | shared-clause
1190/// | linear-clause | aligned-clause | collapse-clause |
1191/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001192/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001193/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001194/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001195/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001196/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001197/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001198/// from-clause | is_device_ptr-clause | task_reduction-clause |
1199/// in_reduction-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001200///
1201OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1202 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001203 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001204 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001205 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001206 // Check if clause is allowed for the given directive.
1207 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001208 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1209 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001210 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001211 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001212 }
1213
1214 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001215 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001216 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001217 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001218 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001219 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001220 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001221 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001222 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001223 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001224 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001225 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001226 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001227 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001228 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001229 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001230 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001231 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001232 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001233 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001234 // OpenMP [2.9.1, target data construct, Restrictions]
1235 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001236 // OpenMP [2.11.1, task Construct, Restrictions]
1237 // At most one if clause can appear on the directive.
1238 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001239 // OpenMP [teams Construct, Restrictions]
1240 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001241 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001242 // OpenMP [2.9.1, task Construct, Restrictions]
1243 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001244 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1245 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001246 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1247 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001248 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001249 Diag(Tok, diag::err_omp_more_one_clause)
1250 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001251 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001252 }
1253
Alexey Bataev10e775f2015-07-30 11:36:16 +00001254 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001255 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001256 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001257 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001258 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001259 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001260 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001261 // OpenMP [2.14.3.1, Restrictions]
1262 // Only a single default clause may be specified on a parallel, task or
1263 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001264 // OpenMP [2.5, parallel Construct, Restrictions]
1265 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001266 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001267 Diag(Tok, diag::err_omp_more_one_clause)
1268 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001269 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001270 }
1271
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001272 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001273 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001274 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001275 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001276 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001277 // OpenMP [2.7.1, Restrictions, p. 3]
1278 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001279 // OpenMP [2.10.4, Restrictions, p. 106]
1280 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001281 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001282 Diag(Tok, diag::err_omp_more_one_clause)
1283 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001284 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001285 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001286 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001287
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001288 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001289 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001290 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001291 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001292 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001293 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001294 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001295 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001296 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001297 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001298 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001299 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001300 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001301 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001302 // OpenMP [2.7.1, Restrictions, p. 9]
1303 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001304 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1305 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001306 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001307 Diag(Tok, diag::err_omp_more_one_clause)
1308 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001309 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001310 }
1311
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001312 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001313 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001314 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001315 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001316 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001317 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001318 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00001319 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00001320 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001321 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001322 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001323 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001324 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001325 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001326 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001327 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001328 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001329 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001330 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001331 case OMPC_is_device_ptr:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001332 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001333 break;
1334 case OMPC_unknown:
1335 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001336 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001337 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001338 break;
1339 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001340 case OMPC_uniform:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001341 if (!WrongDirective)
1342 Diag(Tok, diag::err_omp_unexpected_clause)
1343 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001344 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001345 break;
1346 }
Craig Topper161e4db2014-05-21 06:02:52 +00001347 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001348}
1349
Alexey Bataev2af33e32016-04-07 12:45:37 +00001350/// Parses simple expression in parens for single-expression clauses of OpenMP
1351/// constructs.
1352/// \param RLoc Returned location of right paren.
1353ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1354 SourceLocation &RLoc) {
1355 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1356 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1357 return ExprError();
1358
1359 SourceLocation ELoc = Tok.getLocation();
1360 ExprResult LHS(ParseCastExpression(
1361 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1362 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1363 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1364
1365 // Parse ')'.
1366 T.consumeClose();
1367
1368 RLoc = T.getCloseLocation();
1369 return Val;
1370}
1371
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001372/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001373/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001374/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001375///
Alexey Bataev3778b602014-07-17 07:32:53 +00001376/// final-clause:
1377/// 'final' '(' expression ')'
1378///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001379/// num_threads-clause:
1380/// 'num_threads' '(' expression ')'
1381///
1382/// safelen-clause:
1383/// 'safelen' '(' expression ')'
1384///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001385/// simdlen-clause:
1386/// 'simdlen' '(' expression ')'
1387///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001388/// collapse-clause:
1389/// 'collapse' '(' expression ')'
1390///
Alexey Bataeva0569352015-12-01 10:17:31 +00001391/// priority-clause:
1392/// 'priority' '(' expression ')'
1393///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001394/// grainsize-clause:
1395/// 'grainsize' '(' expression ')'
1396///
Alexey Bataev382967a2015-12-08 12:06:20 +00001397/// num_tasks-clause:
1398/// 'num_tasks' '(' expression ')'
1399///
Alexey Bataev28c75412015-12-15 08:19:24 +00001400/// hint-clause:
1401/// 'hint' '(' expression ')'
1402///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001403OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
1404 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001405 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001406 SourceLocation LLoc = Tok.getLocation();
1407 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001408
Alexey Bataev2af33e32016-04-07 12:45:37 +00001409 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001410
1411 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001412 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001413
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001414 if (ParseOnly)
1415 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00001416 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001417}
1418
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001419/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001420///
1421/// default-clause:
1422/// 'default' '(' 'none' | 'shared' ')
1423///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001424/// proc_bind-clause:
1425/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1426///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001427OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
1428 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001429 SourceLocation Loc = Tok.getLocation();
1430 SourceLocation LOpen = ConsumeToken();
1431 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001432 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001433 if (T.expectAndConsume(diag::err_expected_lparen_after,
1434 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001435 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001436
Alexey Bataeva55ed262014-05-28 06:15:33 +00001437 unsigned Type = getOpenMPSimpleClauseType(
1438 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001439 SourceLocation TypeLoc = Tok.getLocation();
1440 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1441 Tok.isNot(tok::annot_pragma_openmp_end))
1442 ConsumeAnyToken();
1443
1444 // Parse ')'.
1445 T.consumeClose();
1446
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001447 if (ParseOnly)
1448 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001449 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1450 Tok.getLocation());
1451}
1452
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001453/// \brief Parsing of OpenMP clauses like 'ordered'.
1454///
1455/// ordered-clause:
1456/// 'ordered'
1457///
Alexey Bataev236070f2014-06-20 11:19:47 +00001458/// nowait-clause:
1459/// 'nowait'
1460///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001461/// untied-clause:
1462/// 'untied'
1463///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001464/// mergeable-clause:
1465/// 'mergeable'
1466///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001467/// read-clause:
1468/// 'read'
1469///
Alexey Bataev346265e2015-09-25 10:37:12 +00001470/// threads-clause:
1471/// 'threads'
1472///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001473/// simd-clause:
1474/// 'simd'
1475///
Alexey Bataevb825de12015-12-07 10:51:44 +00001476/// nogroup-clause:
1477/// 'nogroup'
1478///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001479OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001480 SourceLocation Loc = Tok.getLocation();
1481 ConsumeAnyToken();
1482
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001483 if (ParseOnly)
1484 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001485 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1486}
1487
1488
Alexey Bataev56dafe82014-06-20 07:16:17 +00001489/// \brief Parsing of OpenMP clauses with single expressions and some additional
1490/// argument like 'schedule' or 'dist_schedule'.
1491///
1492/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001493/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1494/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001495///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001496/// if-clause:
1497/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1498///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001499/// defaultmap:
1500/// 'defaultmap' '(' modifier ':' kind ')'
1501///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001502OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
1503 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00001504 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001505 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001506 // Parse '('.
1507 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1508 if (T.expectAndConsume(diag::err_expected_lparen_after,
1509 getOpenMPClauseName(Kind)))
1510 return nullptr;
1511
1512 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001513 SmallVector<unsigned, 4> Arg;
1514 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001515 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001516 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1517 Arg.resize(NumberOfElements);
1518 KLoc.resize(NumberOfElements);
1519 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1520 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1521 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1522 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001523 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001524 if (KindModifier > OMPC_SCHEDULE_unknown) {
1525 // Parse 'modifier'
1526 Arg[Modifier1] = KindModifier;
1527 KLoc[Modifier1] = Tok.getLocation();
1528 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1529 Tok.isNot(tok::annot_pragma_openmp_end))
1530 ConsumeAnyToken();
1531 if (Tok.is(tok::comma)) {
1532 // Parse ',' 'modifier'
1533 ConsumeAnyToken();
1534 KindModifier = getOpenMPSimpleClauseType(
1535 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1536 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1537 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001538 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001539 KLoc[Modifier2] = Tok.getLocation();
1540 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1541 Tok.isNot(tok::annot_pragma_openmp_end))
1542 ConsumeAnyToken();
1543 }
1544 // Parse ':'
1545 if (Tok.is(tok::colon))
1546 ConsumeAnyToken();
1547 else
1548 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1549 KindModifier = getOpenMPSimpleClauseType(
1550 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1551 }
1552 Arg[ScheduleKind] = KindModifier;
1553 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001554 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1555 Tok.isNot(tok::annot_pragma_openmp_end))
1556 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001557 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1558 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1559 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001560 Tok.is(tok::comma))
1561 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001562 } else if (Kind == OMPC_dist_schedule) {
1563 Arg.push_back(getOpenMPSimpleClauseType(
1564 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1565 KLoc.push_back(Tok.getLocation());
1566 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1567 Tok.isNot(tok::annot_pragma_openmp_end))
1568 ConsumeAnyToken();
1569 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1570 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001571 } else if (Kind == OMPC_defaultmap) {
1572 // Get a defaultmap modifier
1573 Arg.push_back(getOpenMPSimpleClauseType(
1574 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1575 KLoc.push_back(Tok.getLocation());
1576 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1577 Tok.isNot(tok::annot_pragma_openmp_end))
1578 ConsumeAnyToken();
1579 // Parse ':'
1580 if (Tok.is(tok::colon))
1581 ConsumeAnyToken();
1582 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1583 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1584 // Get a defaultmap kind
1585 Arg.push_back(getOpenMPSimpleClauseType(
1586 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1587 KLoc.push_back(Tok.getLocation());
1588 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1589 Tok.isNot(tok::annot_pragma_openmp_end))
1590 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001591 } else {
1592 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001593 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001594 TentativeParsingAction TPA(*this);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001595 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1596 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001597 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001598 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1599 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001600 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001601 } else {
1602 TPA.Revert();
1603 Arg.back() = OMPD_unknown;
1604 }
1605 } else
1606 TPA.Revert();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001607 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001608
Carlo Bertollib4adf552016-01-15 18:50:31 +00001609 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1610 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1611 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001612 if (NeedAnExpression) {
1613 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001614 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1615 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001616 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001617 }
1618
1619 // Parse ')'.
1620 T.consumeClose();
1621
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001622 if (NeedAnExpression && Val.isInvalid())
1623 return nullptr;
1624
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001625 if (ParseOnly)
1626 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001627 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001628 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001629 T.getCloseLocation());
1630}
1631
Alexey Bataevc5e02582014-06-16 07:08:35 +00001632static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1633 UnqualifiedId &ReductionId) {
1634 SourceLocation TemplateKWLoc;
1635 if (ReductionIdScopeSpec.isEmpty()) {
1636 auto OOK = OO_None;
1637 switch (P.getCurToken().getKind()) {
1638 case tok::plus:
1639 OOK = OO_Plus;
1640 break;
1641 case tok::minus:
1642 OOK = OO_Minus;
1643 break;
1644 case tok::star:
1645 OOK = OO_Star;
1646 break;
1647 case tok::amp:
1648 OOK = OO_Amp;
1649 break;
1650 case tok::pipe:
1651 OOK = OO_Pipe;
1652 break;
1653 case tok::caret:
1654 OOK = OO_Caret;
1655 break;
1656 case tok::ampamp:
1657 OOK = OO_AmpAmp;
1658 break;
1659 case tok::pipepipe:
1660 OOK = OO_PipePipe;
1661 break;
1662 default:
1663 break;
1664 }
1665 if (OOK != OO_None) {
1666 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001667 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001668 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1669 return false;
1670 }
1671 }
1672 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1673 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00001674 /*AllowConstructorName*/ false,
1675 /*AllowDeductionGuide*/ false,
1676 nullptr, TemplateKWLoc, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001677}
1678
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001679/// Parses clauses with list.
1680bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1681 OpenMPClauseKind Kind,
1682 SmallVectorImpl<Expr *> &Vars,
1683 OpenMPVarListDataTy &Data) {
1684 UnqualifiedId UnqualifiedReductionId;
1685 bool InvalidReductionId = false;
1686 bool MapTypeModifierSpecified = false;
1687
1688 // Parse '('.
1689 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1690 if (T.expectAndConsume(diag::err_expected_lparen_after,
1691 getOpenMPClauseName(Kind)))
1692 return true;
1693
1694 bool NeedRParenForLinear = false;
1695 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1696 tok::annot_pragma_openmp_end);
1697 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00001698 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
1699 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001700 ColonProtectionRAIIObject ColonRAII(*this);
1701 if (getLangOpts().CPlusPlus)
1702 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1703 /*ObjectType=*/nullptr,
1704 /*EnteringContext=*/false);
1705 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1706 UnqualifiedReductionId);
1707 if (InvalidReductionId) {
1708 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1709 StopBeforeMatch);
1710 }
1711 if (Tok.is(tok::colon))
1712 Data.ColonLoc = ConsumeToken();
1713 else
1714 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1715 if (!InvalidReductionId)
1716 Data.ReductionId =
1717 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1718 } else if (Kind == OMPC_depend) {
1719 // Handle dependency type for depend clause.
1720 ColonProtectionRAIIObject ColonRAII(*this);
1721 Data.DepKind =
1722 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1723 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1724 Data.DepLinMapLoc = Tok.getLocation();
1725
1726 if (Data.DepKind == OMPC_DEPEND_unknown) {
1727 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1728 StopBeforeMatch);
1729 } else {
1730 ConsumeToken();
1731 // Special processing for depend(source) clause.
1732 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1733 // Parse ')'.
1734 T.consumeClose();
1735 return false;
1736 }
1737 }
1738 if (Tok.is(tok::colon))
1739 Data.ColonLoc = ConsumeToken();
1740 else {
1741 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1742 : diag::warn_pragma_expected_colon)
1743 << "dependency type";
1744 }
1745 } else if (Kind == OMPC_linear) {
1746 // Try to parse modifier if any.
1747 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1748 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1749 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1750 Data.DepLinMapLoc = ConsumeToken();
1751 LinearT.consumeOpen();
1752 NeedRParenForLinear = true;
1753 }
1754 } else if (Kind == OMPC_map) {
1755 // Handle map type for map clause.
1756 ColonProtectionRAIIObject ColonRAII(*this);
1757
1758 /// The map clause modifier token can be either a identifier or the C++
1759 /// delete keyword.
1760 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1761 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1762 };
1763
1764 // The first identifier may be a list item, a map-type or a
1765 // map-type-modifier. The map modifier can also be delete which has the same
1766 // spelling of the C++ delete keyword.
1767 Data.MapType =
1768 IsMapClauseModifierToken(Tok)
1769 ? static_cast<OpenMPMapClauseKind>(
1770 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1771 : OMPC_MAP_unknown;
1772 Data.DepLinMapLoc = Tok.getLocation();
1773 bool ColonExpected = false;
1774
1775 if (IsMapClauseModifierToken(Tok)) {
1776 if (PP.LookAhead(0).is(tok::colon)) {
1777 if (Data.MapType == OMPC_MAP_unknown)
1778 Diag(Tok, diag::err_omp_unknown_map_type);
1779 else if (Data.MapType == OMPC_MAP_always)
1780 Diag(Tok, diag::err_omp_map_type_missing);
1781 ConsumeToken();
1782 } else if (PP.LookAhead(0).is(tok::comma)) {
1783 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1784 PP.LookAhead(2).is(tok::colon)) {
1785 Data.MapTypeModifier = Data.MapType;
1786 if (Data.MapTypeModifier != OMPC_MAP_always) {
1787 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1788 Data.MapTypeModifier = OMPC_MAP_unknown;
1789 } else
1790 MapTypeModifierSpecified = true;
1791
1792 ConsumeToken();
1793 ConsumeToken();
1794
1795 Data.MapType =
1796 IsMapClauseModifierToken(Tok)
1797 ? static_cast<OpenMPMapClauseKind>(
1798 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1799 : OMPC_MAP_unknown;
1800 if (Data.MapType == OMPC_MAP_unknown ||
1801 Data.MapType == OMPC_MAP_always)
1802 Diag(Tok, diag::err_omp_unknown_map_type);
1803 ConsumeToken();
1804 } else {
1805 Data.MapType = OMPC_MAP_tofrom;
1806 Data.IsMapTypeImplicit = true;
1807 }
Carlo Bertollid8844b92017-05-03 15:28:48 +00001808 } else if (IsMapClauseModifierToken(PP.LookAhead(0))) {
1809 if (PP.LookAhead(1).is(tok::colon)) {
1810 Data.MapTypeModifier = Data.MapType;
1811 if (Data.MapTypeModifier != OMPC_MAP_always) {
1812 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1813 Data.MapTypeModifier = OMPC_MAP_unknown;
1814 } else
1815 MapTypeModifierSpecified = true;
1816
1817 ConsumeToken();
1818
1819 Data.MapType =
1820 IsMapClauseModifierToken(Tok)
1821 ? static_cast<OpenMPMapClauseKind>(
1822 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1823 : OMPC_MAP_unknown;
1824 if (Data.MapType == OMPC_MAP_unknown ||
1825 Data.MapType == OMPC_MAP_always)
1826 Diag(Tok, diag::err_omp_unknown_map_type);
1827 ConsumeToken();
1828 } else {
1829 Data.MapType = OMPC_MAP_tofrom;
1830 Data.IsMapTypeImplicit = true;
1831 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001832 } else {
1833 Data.MapType = OMPC_MAP_tofrom;
1834 Data.IsMapTypeImplicit = true;
1835 }
1836 } else {
1837 Data.MapType = OMPC_MAP_tofrom;
1838 Data.IsMapTypeImplicit = true;
1839 }
1840
1841 if (Tok.is(tok::colon))
1842 Data.ColonLoc = ConsumeToken();
1843 else if (ColonExpected)
1844 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1845 }
1846
Alexey Bataevfa312f32017-07-21 18:48:21 +00001847 bool IsComma =
1848 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
1849 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1850 (Kind == OMPC_reduction && !InvalidReductionId) ||
1851 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1852 (!MapTypeModifierSpecified ||
1853 Data.MapTypeModifier == OMPC_MAP_always)) ||
1854 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001855 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1856 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1857 Tok.isNot(tok::annot_pragma_openmp_end))) {
1858 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1859 // Parse variable
1860 ExprResult VarExpr =
1861 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1862 if (VarExpr.isUsable())
1863 Vars.push_back(VarExpr.get());
1864 else {
1865 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1866 StopBeforeMatch);
1867 }
1868 // Skip ',' if any
1869 IsComma = Tok.is(tok::comma);
1870 if (IsComma)
1871 ConsumeToken();
1872 else if (Tok.isNot(tok::r_paren) &&
1873 Tok.isNot(tok::annot_pragma_openmp_end) &&
1874 (!MayHaveTail || Tok.isNot(tok::colon)))
1875 Diag(Tok, diag::err_omp_expected_punc)
1876 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1877 : getOpenMPClauseName(Kind))
1878 << (Kind == OMPC_flush);
1879 }
1880
1881 // Parse ')' for linear clause with modifier.
1882 if (NeedRParenForLinear)
1883 LinearT.consumeClose();
1884
1885 // Parse ':' linear-step (or ':' alignment).
1886 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1887 if (MustHaveTail) {
1888 Data.ColonLoc = Tok.getLocation();
1889 SourceLocation ELoc = ConsumeToken();
1890 ExprResult Tail = ParseAssignmentExpression();
1891 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1892 if (Tail.isUsable())
1893 Data.TailExpr = Tail.get();
1894 else
1895 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1896 StopBeforeMatch);
1897 }
1898
1899 // Parse ')'.
1900 T.consumeClose();
1901 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1902 Vars.empty()) ||
1903 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1904 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1905 return true;
1906 return false;
1907}
1908
Alexander Musman1bb328c2014-06-04 13:06:39 +00001909/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00001910/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
1911/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001912///
1913/// private-clause:
1914/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001915/// firstprivate-clause:
1916/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001917/// lastprivate-clause:
1918/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001919/// shared-clause:
1920/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001921/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001922/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001923/// aligned-clause:
1924/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001925/// reduction-clause:
1926/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00001927/// task_reduction-clause:
1928/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00001929/// in_reduction-clause:
1930/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001931/// copyprivate-clause:
1932/// 'copyprivate' '(' list ')'
1933/// flush-clause:
1934/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001935/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001936/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001937/// map-clause:
1938/// 'map' '(' [ [ always , ]
1939/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001940/// to-clause:
1941/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001942/// from-clause:
1943/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001944/// use_device_ptr-clause:
1945/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001946/// is_device_ptr-clause:
1947/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001948///
Alexey Bataev182227b2015-08-20 10:54:39 +00001949/// For 'linear' clause linear-list may have the following forms:
1950/// list
1951/// modifier(list)
1952/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001953OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001954 OpenMPClauseKind Kind,
1955 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001956 SourceLocation Loc = Tok.getLocation();
1957 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001958 SmallVector<Expr *, 4> Vars;
1959 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001960
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001961 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001962 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001963
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001964 if (ParseOnly)
1965 return nullptr;
Alexey Bataevc5e02582014-06-16 07:08:35 +00001966 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001967 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1968 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1969 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1970 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001971}
1972