blob: 669e9aff8021c8a8389b031d912e31ec7ccab588 [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;
253 TypeResult TR = ParseTypeName(&Range, Declarator::PrototypeContext, AS);
254 if (TR.isUsable()) {
255 auto ReductionType =
256 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
257 if (!ReductionType.isNull()) {
258 ReductionTypes.push_back(
259 std::make_pair(ReductionType, Range.getBegin()));
260 }
261 } else {
262 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
263 StopBeforeMatch);
264 }
265
266 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
267 break;
268
269 // Consume ','.
270 if (ExpectAndConsume(tok::comma)) {
271 IsCorrect = false;
272 if (Tok.is(tok::annot_pragma_openmp_end)) {
273 Diag(Tok.getLocation(), diag::err_expected_type);
274 return DeclGroupPtrTy();
275 }
276 }
277 } while (Tok.isNot(tok::annot_pragma_openmp_end));
278
279 if (ReductionTypes.empty()) {
280 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
281 return DeclGroupPtrTy();
282 }
283
284 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
285 return DeclGroupPtrTy();
286
287 // Consume ':'.
288 if (ExpectAndConsume(tok::colon))
289 IsCorrect = false;
290
291 if (Tok.is(tok::annot_pragma_openmp_end)) {
292 Diag(Tok.getLocation(), diag::err_expected_expression);
293 return DeclGroupPtrTy();
294 }
295
296 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
297 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
298
299 // Parse <combiner> expression and then parse initializer if any for each
300 // correct type.
301 unsigned I = 0, E = ReductionTypes.size();
302 for (auto *D : DRD.get()) {
303 TentativeParsingAction TPA(*this);
304 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000305 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000306 Scope::OpenMPDirectiveScope);
307 // Parse <combiner> expression.
308 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
309 ExprResult CombinerResult =
310 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
311 D->getLocation(), /*DiscardedValue=*/true);
312 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
313
314 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
315 Tok.isNot(tok::annot_pragma_openmp_end)) {
316 TPA.Commit();
317 IsCorrect = false;
318 break;
319 }
320 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
321 ExprResult InitializerResult;
322 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
323 // Parse <initializer> expression.
324 if (Tok.is(tok::identifier) &&
325 Tok.getIdentifierInfo()->isStr("initializer"))
326 ConsumeToken();
327 else {
328 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
329 TPA.Commit();
330 IsCorrect = false;
331 break;
332 }
333 // Parse '('.
334 BalancedDelimiterTracker T(*this, tok::l_paren,
335 tok::annot_pragma_openmp_end);
336 IsCorrect =
337 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
338 IsCorrect;
339 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
340 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000341 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000342 Scope::OpenMPDirectiveScope);
343 // Parse expression.
Alexey Bataev070f43a2017-09-06 14:49:58 +0000344 VarDecl *OmpPrivParm =
345 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
346 D);
347 // Check if initializer is omp_priv <init_expr> or something else.
348 if (Tok.is(tok::identifier) &&
349 Tok.getIdentifierInfo()->isStr("omp_priv")) {
350 ConsumeToken();
351 ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
352 } else {
353 InitializerResult = Actions.ActOnFinishFullExpr(
354 ParseAssignmentExpression().get(), D->getLocation(),
355 /*DiscardedValue=*/true);
356 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000357 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
Alexey Bataev070f43a2017-09-06 14:49:58 +0000358 D, InitializerResult.get(), OmpPrivParm);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000359 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
360 Tok.isNot(tok::annot_pragma_openmp_end)) {
361 TPA.Commit();
362 IsCorrect = false;
363 break;
364 }
365 IsCorrect =
366 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
367 }
368 }
369
370 ++I;
371 // Revert parsing if not the last type, otherwise accept it, we're done with
372 // parsing.
373 if (I != E)
374 TPA.Revert();
375 else
376 TPA.Commit();
377 }
378 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
379 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000380}
381
Alexey Bataev070f43a2017-09-06 14:49:58 +0000382void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
383 // Parse declarator '=' initializer.
384 // If a '==' or '+=' is found, suggest a fixit to '='.
385 if (isTokenEqualOrEqualTypo()) {
386 ConsumeToken();
387
388 if (Tok.is(tok::code_completion)) {
389 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
390 Actions.FinalizeDeclaration(OmpPrivParm);
391 cutOffParsing();
392 return;
393 }
394
395 ExprResult Init(ParseInitializer());
396
397 if (Init.isInvalid()) {
398 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
399 Actions.ActOnInitializerError(OmpPrivParm);
400 } else {
401 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
402 /*DirectInit=*/false);
403 }
404 } else if (Tok.is(tok::l_paren)) {
405 // Parse C++ direct initializer: '(' expression-list ')'
406 BalancedDelimiterTracker T(*this, tok::l_paren);
407 T.consumeOpen();
408
409 ExprVector Exprs;
410 CommaLocsTy CommaLocs;
411
412 if (ParseExpressionList(Exprs, CommaLocs, [this, OmpPrivParm, &Exprs] {
413 Actions.CodeCompleteConstructor(
414 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
415 OmpPrivParm->getLocation(), Exprs);
416 })) {
417 Actions.ActOnInitializerError(OmpPrivParm);
418 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
419 } else {
420 // Match the ')'.
421 T.consumeClose();
422
423 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
424 "Unexpected number of commas!");
425
426 ExprResult Initializer = Actions.ActOnParenListExpr(
427 T.getOpenLocation(), T.getCloseLocation(), Exprs);
428 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
429 /*DirectInit=*/true);
430 }
431 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
432 // Parse C++0x braced-init-list.
433 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
434
435 ExprResult Init(ParseBraceInitializer());
436
437 if (Init.isInvalid()) {
438 Actions.ActOnInitializerError(OmpPrivParm);
439 } else {
440 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
441 /*DirectInit=*/true);
442 }
443 } else {
444 Actions.ActOnUninitializedDecl(OmpPrivParm);
445 }
446}
447
Alexey Bataev2af33e32016-04-07 12:45:37 +0000448namespace {
449/// RAII that recreates function context for correct parsing of clauses of
450/// 'declare simd' construct.
451/// OpenMP, 2.8.2 declare simd Construct
452/// The expressions appearing in the clauses of this directive are evaluated in
453/// the scope of the arguments of the function declaration or definition.
454class FNContextRAII final {
455 Parser &P;
456 Sema::CXXThisScopeRAII *ThisScope;
457 Parser::ParseScope *TempScope;
458 Parser::ParseScope *FnScope;
459 bool HasTemplateScope = false;
460 bool HasFunScope = false;
461 FNContextRAII() = delete;
462 FNContextRAII(const FNContextRAII &) = delete;
463 FNContextRAII &operator=(const FNContextRAII &) = delete;
464
465public:
466 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
467 Decl *D = *Ptr.get().begin();
468 NamedDecl *ND = dyn_cast<NamedDecl>(D);
469 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
470 Sema &Actions = P.getActions();
471
472 // Allow 'this' within late-parsed attributes.
473 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
474 ND && ND->isCXXInstanceMember());
475
476 // If the Decl is templatized, add template parameters to scope.
477 HasTemplateScope = D->isTemplateDecl();
478 TempScope =
479 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
480 if (HasTemplateScope)
481 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
482
483 // If the Decl is on a function, add function parameters to the scope.
484 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000485 FnScope = new Parser::ParseScope(
486 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
487 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000488 if (HasFunScope)
489 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
490 }
491 ~FNContextRAII() {
492 if (HasFunScope) {
493 P.getActions().ActOnExitFunctionContext();
494 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
495 }
496 if (HasTemplateScope)
497 TempScope->Exit();
498 delete FnScope;
499 delete TempScope;
500 delete ThisScope;
501 }
502};
503} // namespace
504
Alexey Bataevd93d3762016-04-12 09:35:56 +0000505/// Parses clauses for 'declare simd' directive.
506/// clause:
507/// 'inbranch' | 'notinbranch'
508/// 'simdlen' '(' <expr> ')'
509/// { 'uniform' '(' <argument_list> ')' }
510/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000511/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
512static bool parseDeclareSimdClauses(
513 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
514 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
515 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
516 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000517 SourceRange BSRange;
518 const Token &Tok = P.getCurToken();
519 bool IsError = false;
520 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
521 if (Tok.isNot(tok::identifier))
522 break;
523 OMPDeclareSimdDeclAttr::BranchStateTy Out;
524 IdentifierInfo *II = Tok.getIdentifierInfo();
525 StringRef ClauseName = II->getName();
526 // Parse 'inranch|notinbranch' clauses.
527 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
528 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
529 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
530 << ClauseName
531 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
532 IsError = true;
533 }
534 BS = Out;
535 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
536 P.ConsumeToken();
537 } else if (ClauseName.equals("simdlen")) {
538 if (SimdLen.isUsable()) {
539 P.Diag(Tok, diag::err_omp_more_one_clause)
540 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
541 IsError = true;
542 }
543 P.ConsumeToken();
544 SourceLocation RLoc;
545 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
546 if (SimdLen.isInvalid())
547 IsError = true;
548 } else {
549 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000550 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
551 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000552 Parser::OpenMPVarListDataTy Data;
553 auto *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000554 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000555 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000556 else if (CKind == OMPC_linear)
557 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000558
559 P.ConsumeToken();
560 if (P.ParseOpenMPVarList(OMPD_declare_simd,
561 getOpenMPClauseKind(ClauseName), *Vars, Data))
562 IsError = true;
563 if (CKind == OMPC_aligned)
564 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000565 else if (CKind == OMPC_linear) {
566 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
567 Data.DepLinMapLoc))
568 Data.LinKind = OMPC_LINEAR_val;
569 LinModifiers.append(Linears.size() - LinModifiers.size(),
570 Data.LinKind);
571 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
572 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000573 } else
574 // TODO: add parsing of other clauses.
575 break;
576 }
577 // Skip ',' if any.
578 if (Tok.is(tok::comma))
579 P.ConsumeToken();
580 }
581 return IsError;
582}
583
Alexey Bataev2af33e32016-04-07 12:45:37 +0000584/// Parse clauses for '#pragma omp declare simd'.
585Parser::DeclGroupPtrTy
586Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
587 CachedTokens &Toks, SourceLocation Loc) {
588 PP.EnterToken(Tok);
589 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
590 // Consume the previously pushed token.
591 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
592
593 FNContextRAII FnContext(*this, Ptr);
594 OMPDeclareSimdDeclAttr::BranchStateTy BS =
595 OMPDeclareSimdDeclAttr::BS_Undefined;
596 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000597 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000598 SmallVector<Expr *, 4> Aligneds;
599 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000600 SmallVector<Expr *, 4> Linears;
601 SmallVector<unsigned, 4> LinModifiers;
602 SmallVector<Expr *, 4> Steps;
603 bool IsError =
604 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
605 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000606 // Need to check for extra tokens.
607 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
608 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
609 << getOpenMPDirectiveName(OMPD_declare_simd);
610 while (Tok.isNot(tok::annot_pragma_openmp_end))
611 ConsumeAnyToken();
612 }
613 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000614 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataevd93d3762016-04-12 09:35:56 +0000615 if (!IsError) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000616 return Actions.ActOnOpenMPDeclareSimdDirective(
Alexey Bataevecba70f2016-04-12 11:02:11 +0000617 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
618 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataevd93d3762016-04-12 09:35:56 +0000619 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000620 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000621}
622
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000623/// \brief Parsing of declarative OpenMP directives.
624///
625/// threadprivate-directive:
626/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000627/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000628///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000629/// declare-reduction-directive:
630/// annot_pragma_openmp 'declare' 'reduction' [...]
631/// annot_pragma_openmp_end
632///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000633/// declare-simd-directive:
634/// annot_pragma_openmp 'declare simd' {<clause> [,]}
635/// annot_pragma_openmp_end
636/// <function declaration/definition>
637///
638Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
639 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
640 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000641 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000642 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000643
Richard Smithaf3b3252017-05-18 19:21:48 +0000644 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev4acb8592014-07-07 13:01:15 +0000645 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000646
647 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000648 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000649 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000650 ThreadprivateListParserHelper Helper(this);
651 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000652 // The last seen token is annot_pragma_openmp_end - need to check for
653 // extra tokens.
654 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
655 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000656 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000657 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000658 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000659 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000660 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000661 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
662 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000663 }
664 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000665 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000666 case OMPD_declare_reduction:
667 ConsumeToken();
668 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
669 // The last seen token is annot_pragma_openmp_end - need to check for
670 // extra tokens.
671 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
672 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
673 << getOpenMPDirectiveName(OMPD_declare_reduction);
674 while (Tok.isNot(tok::annot_pragma_openmp_end))
675 ConsumeAnyToken();
676 }
677 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000678 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000679 return Res;
680 }
681 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000682 case OMPD_declare_simd: {
683 // The syntax is:
684 // { #pragma omp declare simd }
685 // <function-declaration-or-definition>
686 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000687 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000688 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000689 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
690 Toks.push_back(Tok);
691 ConsumeAnyToken();
692 }
693 Toks.push_back(Tok);
694 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000695
696 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000697 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000698 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000699 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000700 // Here we expect to see some function declaration.
701 if (AS == AS_none) {
702 assert(TagType == DeclSpec::TST_unspecified);
703 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000704 ParsingDeclSpec PDS(*this);
705 Ptr = ParseExternalDeclaration(Attrs, &PDS);
706 } else {
707 Ptr =
708 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
709 }
710 }
711 if (!Ptr) {
712 Diag(Loc, diag::err_omp_decl_in_declare_simd);
713 return DeclGroupPtrTy();
714 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000715 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000716 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000717 case OMPD_declare_target: {
718 SourceLocation DTLoc = ConsumeAnyToken();
719 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000720 // OpenMP 4.5 syntax with list of entities.
721 llvm::SmallSetVector<const NamedDecl*, 16> SameDirectiveDecls;
722 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
723 OMPDeclareTargetDeclAttr::MapTypeTy MT =
724 OMPDeclareTargetDeclAttr::MT_To;
725 if (Tok.is(tok::identifier)) {
726 IdentifierInfo *II = Tok.getIdentifierInfo();
727 StringRef ClauseName = II->getName();
728 // Parse 'to|link' clauses.
729 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
730 MT)) {
731 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
732 << ClauseName;
733 break;
734 }
735 ConsumeToken();
736 }
737 auto Callback = [this, MT, &SameDirectiveDecls](
738 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
739 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
740 SameDirectiveDecls);
741 };
742 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, true))
743 break;
744
745 // Consume optional ','.
746 if (Tok.is(tok::comma))
747 ConsumeToken();
748 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000749 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000750 ConsumeAnyToken();
751 return DeclGroupPtrTy();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000752 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000753
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000754 // Skip the last annot_pragma_openmp_end.
755 ConsumeAnyToken();
756
757 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
758 return DeclGroupPtrTy();
759
760 DKind = ParseOpenMPDirectiveKind(*this);
761 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
762 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
763 ParsedAttributesWithRange attrs(AttrFactory);
764 MaybeParseCXX11Attributes(attrs);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000765 ParseExternalDeclaration(attrs);
766 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
767 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +0000768 ConsumeAnnotationToken();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000769 DKind = ParseOpenMPDirectiveKind(*this);
770 if (DKind != OMPD_end_declare_target)
771 TPA.Revert();
772 else
773 TPA.Commit();
774 }
775 }
776
777 if (DKind == OMPD_end_declare_target) {
778 ConsumeAnyToken();
779 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
780 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
781 << getOpenMPDirectiveName(OMPD_end_declare_target);
782 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
783 }
784 // Skip the last annot_pragma_openmp_end.
785 ConsumeAnyToken();
786 } else {
787 Diag(Tok, diag::err_expected_end_declare_target);
788 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
789 }
790 Actions.ActOnFinishOpenMPDeclareTargetDirective();
791 return DeclGroupPtrTy();
792 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000793 case OMPD_unknown:
794 Diag(Tok, diag::err_omp_unknown_directive);
795 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000796 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000797 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000798 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000799 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000800 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000801 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000802 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000803 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000804 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000805 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000806 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000807 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000808 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000809 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000810 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000811 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000812 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000813 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000814 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000815 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000816 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000817 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000818 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000819 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000820 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000821 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000822 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000823 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000824 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000825 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000826 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000827 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000828 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000829 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000830 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000831 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000832 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000833 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000834 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000835 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000836 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000837 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000838 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +0000839 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +0000840 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +0000841 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +0000842 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +0000843 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000844 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000845 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000846 break;
847 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000848 while (Tok.isNot(tok::annot_pragma_openmp_end))
849 ConsumeAnyToken();
850 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000851 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000852}
853
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000854/// \brief Parsing of declarative or executable OpenMP directives.
855///
856/// threadprivate-directive:
857/// annot_pragma_openmp 'threadprivate' simple-variable-list
858/// annot_pragma_openmp_end
859///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000860/// declare-reduction-directive:
861/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
862/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
863/// ('omp_priv' '=' <expression>|<function_call>) ')']
864/// annot_pragma_openmp_end
865///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000866/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000867/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000868/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
869/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000870/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000871/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000872/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000873/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000874/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000875/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000876/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000877/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000878/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000879/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +0000880/// 'teams distribute parallel for' | 'target teams' |
881/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +0000882/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +0000883/// 'target teams distribute parallel for simd' |
884/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000885/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000886///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000887StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000888 AllowedConstructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000889 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000890 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000891 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000892 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000893 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +0000894 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
895 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +0000896 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000897 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000898 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000899 // Name of critical directive.
900 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000901 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000902 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000903 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000904
905 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000906 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000907 if (Allowed != ACK_Any) {
908 Diag(Tok, diag::err_omp_immediate_directive)
909 << getOpenMPDirectiveName(DKind) << 0;
910 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000911 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000912 ThreadprivateListParserHelper Helper(this);
913 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000914 // The last seen token is annot_pragma_openmp_end - need to check for
915 // extra tokens.
916 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
917 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000918 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000919 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000920 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000921 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
922 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000923 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
924 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000925 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000926 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000927 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000928 case OMPD_declare_reduction:
929 ConsumeToken();
930 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
931 // The last seen token is annot_pragma_openmp_end - need to check for
932 // extra tokens.
933 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
934 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
935 << getOpenMPDirectiveName(OMPD_declare_reduction);
936 while (Tok.isNot(tok::annot_pragma_openmp_end))
937 ConsumeAnyToken();
938 }
939 ConsumeAnyToken();
940 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
941 } else
942 SkipUntil(tok::annot_pragma_openmp_end);
943 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000944 case OMPD_flush:
945 if (PP.LookAhead(0).is(tok::l_paren)) {
946 FlushHasClause = true;
947 // Push copy of the current token back to stream to properly parse
948 // pseudo-clause OMPFlushClause.
949 PP.EnterToken(Tok);
950 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000951 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +0000952 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000953 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000954 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000955 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000956 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000957 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000958 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000959 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000960 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000961 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000962 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000963 }
964 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000965 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000966 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000967 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000968 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000969 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000970 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000971 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000972 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000973 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000974 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000975 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000976 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000977 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000978 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000979 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000980 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000981 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000982 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000983 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000984 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000985 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000986 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000987 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000988 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000989 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000990 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +0000991 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000992 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000993 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000994 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000995 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +0000996 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +0000997 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000998 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +0000999 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001000 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001001 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001002 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001003 case OMPD_target_teams_distribute_parallel_for_simd:
1004 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001005 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001006 // Parse directive name of the 'critical' directive if any.
1007 if (DKind == OMPD_critical) {
1008 BalancedDelimiterTracker T(*this, tok::l_paren,
1009 tok::annot_pragma_openmp_end);
1010 if (!T.consumeOpen()) {
1011 if (Tok.isAnyIdentifier()) {
1012 DirName =
1013 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1014 ConsumeAnyToken();
1015 } else {
1016 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1017 }
1018 T.consumeClose();
1019 }
Alexey Bataev80909872015-07-02 11:25:17 +00001020 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001021 CancelRegion = ParseOpenMPDirectiveKind(*this);
1022 if (Tok.isNot(tok::annot_pragma_openmp_end))
1023 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001024 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001025
Alexey Bataevf29276e2014-06-18 04:14:57 +00001026 if (isOpenMPLoopDirective(DKind))
1027 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1028 if (isOpenMPSimdDirective(DKind))
1029 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1030 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001031 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001032
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001033 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001034 OpenMPClauseKind CKind =
1035 Tok.isAnnotation()
1036 ? OMPC_unknown
1037 : FlushHasClause ? OMPC_flush
1038 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001039 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001040 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001041 OMPClause *Clause =
1042 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001043 FirstClauses[CKind].setInt(true);
1044 if (Clause) {
1045 FirstClauses[CKind].setPointer(Clause);
1046 Clauses.push_back(Clause);
1047 }
1048
1049 // Skip ',' if any.
1050 if (Tok.is(tok::comma))
1051 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001052 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001053 }
1054 // End location of the directive.
1055 EndLoc = Tok.getLocation();
1056 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001057 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001058
Alexey Bataeveb482352015-12-18 05:05:56 +00001059 // OpenMP [2.13.8, ordered Construct, Syntax]
1060 // If the depend clause is specified, the ordered construct is a stand-alone
1061 // directive.
1062 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001063 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001064 Diag(Loc, diag::err_omp_immediate_directive)
1065 << getOpenMPDirectiveName(DKind) << 1
1066 << getOpenMPClauseName(OMPC_depend);
1067 }
1068 HasAssociatedStatement = false;
1069 }
1070
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001071 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001072 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001073 // The body is a block scope like in Lambdas and Blocks.
1074 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001075 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001076 Actions.ActOnStartOfCompoundStmt();
1077 // Parse statement
1078 AssociatedStmt = ParseStatement();
1079 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001080 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001081 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001082 Directive = Actions.ActOnOpenMPExecutableDirective(
1083 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1084 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001085
1086 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001087 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001088 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001089 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001090 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001091 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001092 case OMPD_declare_target:
1093 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001094 Diag(Tok, diag::err_omp_unexpected_directive)
1095 << getOpenMPDirectiveName(DKind);
1096 SkipUntil(tok::annot_pragma_openmp_end);
1097 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001098 case OMPD_unknown:
1099 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001100 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001101 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001102 }
1103 return Directive;
1104}
1105
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001106// Parses simple list:
1107// simple-variable-list:
1108// '(' id-expression {, id-expression} ')'
1109//
1110bool Parser::ParseOpenMPSimpleVarList(
1111 OpenMPDirectiveKind Kind,
1112 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1113 Callback,
1114 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001115 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001116 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001117 if (T.expectAndConsume(diag::err_expected_lparen_after,
1118 getOpenMPDirectiveName(Kind)))
1119 return true;
1120 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001121 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001122
1123 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001124 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001125 CXXScopeSpec SS;
1126 SourceLocation TemplateKWLoc;
1127 UnqualifiedId Name;
1128 // Read var name.
1129 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001130 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001131
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001132 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001133 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001134 IsCorrect = false;
1135 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001136 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001137 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001138 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001139 IsCorrect = false;
1140 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001141 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001142 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1143 Tok.isNot(tok::annot_pragma_openmp_end)) {
1144 IsCorrect = false;
1145 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001146 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001147 Diag(PrevTok.getLocation(), diag::err_expected)
1148 << tok::identifier
1149 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001150 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001151 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001152 }
1153 // Consume ','.
1154 if (Tok.is(tok::comma)) {
1155 ConsumeToken();
1156 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001157 }
1158
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001159 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001160 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001161 IsCorrect = false;
1162 }
1163
1164 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001165 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001166
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001167 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001168}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001169
1170/// \brief Parsing of OpenMP clauses.
1171///
1172/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001173/// if-clause | final-clause | num_threads-clause | safelen-clause |
1174/// default-clause | private-clause | firstprivate-clause | shared-clause
1175/// | linear-clause | aligned-clause | collapse-clause |
1176/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001177/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001178/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001179/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001180/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001181/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001182/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001183/// from-clause | is_device_ptr-clause | task_reduction-clause |
1184/// in_reduction-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001185///
1186OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1187 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001188 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001189 bool ErrorFound = false;
1190 // Check if clause is allowed for the given directive.
1191 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001192 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1193 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001194 ErrorFound = true;
1195 }
1196
1197 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001198 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001199 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001200 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001201 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001202 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001203 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001204 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001205 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001206 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001207 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001208 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001209 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001210 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001211 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001212 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001213 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001214 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001215 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001216 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001217 // OpenMP [2.9.1, target data construct, Restrictions]
1218 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001219 // OpenMP [2.11.1, task Construct, Restrictions]
1220 // At most one if clause can appear on the directive.
1221 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001222 // OpenMP [teams Construct, Restrictions]
1223 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001224 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001225 // OpenMP [2.9.1, task Construct, Restrictions]
1226 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001227 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1228 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001229 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1230 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001231 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001232 Diag(Tok, diag::err_omp_more_one_clause)
1233 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001234 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001235 }
1236
Alexey Bataev10e775f2015-07-30 11:36:16 +00001237 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1238 Clause = ParseOpenMPClause(CKind);
1239 else
1240 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001241 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001242 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001243 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001244 // OpenMP [2.14.3.1, Restrictions]
1245 // Only a single default clause may be specified on a parallel, task or
1246 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001247 // OpenMP [2.5, parallel Construct, Restrictions]
1248 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001249 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001250 Diag(Tok, diag::err_omp_more_one_clause)
1251 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001252 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001253 }
1254
1255 Clause = ParseOpenMPSimpleClause(CKind);
1256 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001257 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001258 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001259 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001260 // OpenMP [2.7.1, Restrictions, p. 3]
1261 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001262 // OpenMP [2.10.4, Restrictions, p. 106]
1263 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001264 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001265 Diag(Tok, diag::err_omp_more_one_clause)
1266 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001267 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001268 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001269 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001270
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001271 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001272 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1273 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001274 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001275 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001276 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001277 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001278 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001279 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001280 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001281 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001282 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001283 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001284 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001285 // OpenMP [2.7.1, Restrictions, p. 9]
1286 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001287 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1288 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001289 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001290 Diag(Tok, diag::err_omp_more_one_clause)
1291 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001292 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001293 }
1294
1295 Clause = ParseOpenMPClause(CKind);
1296 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001297 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001298 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001299 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001300 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001301 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00001302 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00001303 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001304 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001305 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001306 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001307 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001308 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001309 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001310 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001311 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001312 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001313 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001314 case OMPC_is_device_ptr:
Alexey Bataeveb482352015-12-18 05:05:56 +00001315 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001316 break;
1317 case OMPC_unknown:
1318 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001319 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001320 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001321 break;
1322 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001323 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001324 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1325 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001326 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001327 break;
1328 }
Craig Topper161e4db2014-05-21 06:02:52 +00001329 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001330}
1331
Alexey Bataev2af33e32016-04-07 12:45:37 +00001332/// Parses simple expression in parens for single-expression clauses of OpenMP
1333/// constructs.
1334/// \param RLoc Returned location of right paren.
1335ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1336 SourceLocation &RLoc) {
1337 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1338 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1339 return ExprError();
1340
1341 SourceLocation ELoc = Tok.getLocation();
1342 ExprResult LHS(ParseCastExpression(
1343 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1344 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1345 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1346
1347 // Parse ')'.
1348 T.consumeClose();
1349
1350 RLoc = T.getCloseLocation();
1351 return Val;
1352}
1353
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001354/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001355/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001356/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001357///
Alexey Bataev3778b602014-07-17 07:32:53 +00001358/// final-clause:
1359/// 'final' '(' expression ')'
1360///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001361/// num_threads-clause:
1362/// 'num_threads' '(' expression ')'
1363///
1364/// safelen-clause:
1365/// 'safelen' '(' expression ')'
1366///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001367/// simdlen-clause:
1368/// 'simdlen' '(' expression ')'
1369///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001370/// collapse-clause:
1371/// 'collapse' '(' expression ')'
1372///
Alexey Bataeva0569352015-12-01 10:17:31 +00001373/// priority-clause:
1374/// 'priority' '(' expression ')'
1375///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001376/// grainsize-clause:
1377/// 'grainsize' '(' expression ')'
1378///
Alexey Bataev382967a2015-12-08 12:06:20 +00001379/// num_tasks-clause:
1380/// 'num_tasks' '(' expression ')'
1381///
Alexey Bataev28c75412015-12-15 08:19:24 +00001382/// hint-clause:
1383/// 'hint' '(' expression ')'
1384///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001385OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1386 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001387 SourceLocation LLoc = Tok.getLocation();
1388 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001389
Alexey Bataev2af33e32016-04-07 12:45:37 +00001390 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001391
1392 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001393 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001394
Alexey Bataev2af33e32016-04-07 12:45:37 +00001395 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001396}
1397
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001398/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001399///
1400/// default-clause:
1401/// 'default' '(' 'none' | 'shared' ')
1402///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001403/// proc_bind-clause:
1404/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1405///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001406OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1407 SourceLocation Loc = Tok.getLocation();
1408 SourceLocation LOpen = ConsumeToken();
1409 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001410 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001411 if (T.expectAndConsume(diag::err_expected_lparen_after,
1412 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001413 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001414
Alexey Bataeva55ed262014-05-28 06:15:33 +00001415 unsigned Type = getOpenMPSimpleClauseType(
1416 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001417 SourceLocation TypeLoc = Tok.getLocation();
1418 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1419 Tok.isNot(tok::annot_pragma_openmp_end))
1420 ConsumeAnyToken();
1421
1422 // Parse ')'.
1423 T.consumeClose();
1424
1425 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1426 Tok.getLocation());
1427}
1428
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001429/// \brief Parsing of OpenMP clauses like 'ordered'.
1430///
1431/// ordered-clause:
1432/// 'ordered'
1433///
Alexey Bataev236070f2014-06-20 11:19:47 +00001434/// nowait-clause:
1435/// 'nowait'
1436///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001437/// untied-clause:
1438/// 'untied'
1439///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001440/// mergeable-clause:
1441/// 'mergeable'
1442///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001443/// read-clause:
1444/// 'read'
1445///
Alexey Bataev346265e2015-09-25 10:37:12 +00001446/// threads-clause:
1447/// 'threads'
1448///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001449/// simd-clause:
1450/// 'simd'
1451///
Alexey Bataevb825de12015-12-07 10:51:44 +00001452/// nogroup-clause:
1453/// 'nogroup'
1454///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001455OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1456 SourceLocation Loc = Tok.getLocation();
1457 ConsumeAnyToken();
1458
1459 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1460}
1461
1462
Alexey Bataev56dafe82014-06-20 07:16:17 +00001463/// \brief Parsing of OpenMP clauses with single expressions and some additional
1464/// argument like 'schedule' or 'dist_schedule'.
1465///
1466/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001467/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1468/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001469///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001470/// if-clause:
1471/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1472///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001473/// defaultmap:
1474/// 'defaultmap' '(' modifier ':' kind ')'
1475///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001476OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1477 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001478 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001479 // Parse '('.
1480 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1481 if (T.expectAndConsume(diag::err_expected_lparen_after,
1482 getOpenMPClauseName(Kind)))
1483 return nullptr;
1484
1485 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001486 SmallVector<unsigned, 4> Arg;
1487 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001488 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001489 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1490 Arg.resize(NumberOfElements);
1491 KLoc.resize(NumberOfElements);
1492 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1493 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1494 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1495 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001496 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001497 if (KindModifier > OMPC_SCHEDULE_unknown) {
1498 // Parse 'modifier'
1499 Arg[Modifier1] = KindModifier;
1500 KLoc[Modifier1] = Tok.getLocation();
1501 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1502 Tok.isNot(tok::annot_pragma_openmp_end))
1503 ConsumeAnyToken();
1504 if (Tok.is(tok::comma)) {
1505 // Parse ',' 'modifier'
1506 ConsumeAnyToken();
1507 KindModifier = getOpenMPSimpleClauseType(
1508 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1509 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1510 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001511 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001512 KLoc[Modifier2] = Tok.getLocation();
1513 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1514 Tok.isNot(tok::annot_pragma_openmp_end))
1515 ConsumeAnyToken();
1516 }
1517 // Parse ':'
1518 if (Tok.is(tok::colon))
1519 ConsumeAnyToken();
1520 else
1521 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1522 KindModifier = getOpenMPSimpleClauseType(
1523 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1524 }
1525 Arg[ScheduleKind] = KindModifier;
1526 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001527 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1528 Tok.isNot(tok::annot_pragma_openmp_end))
1529 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001530 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1531 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1532 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001533 Tok.is(tok::comma))
1534 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001535 } else if (Kind == OMPC_dist_schedule) {
1536 Arg.push_back(getOpenMPSimpleClauseType(
1537 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1538 KLoc.push_back(Tok.getLocation());
1539 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1540 Tok.isNot(tok::annot_pragma_openmp_end))
1541 ConsumeAnyToken();
1542 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1543 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001544 } else if (Kind == OMPC_defaultmap) {
1545 // Get a defaultmap modifier
1546 Arg.push_back(getOpenMPSimpleClauseType(
1547 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1548 KLoc.push_back(Tok.getLocation());
1549 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1550 Tok.isNot(tok::annot_pragma_openmp_end))
1551 ConsumeAnyToken();
1552 // Parse ':'
1553 if (Tok.is(tok::colon))
1554 ConsumeAnyToken();
1555 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1556 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1557 // Get a defaultmap kind
1558 Arg.push_back(getOpenMPSimpleClauseType(
1559 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1560 KLoc.push_back(Tok.getLocation());
1561 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1562 Tok.isNot(tok::annot_pragma_openmp_end))
1563 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001564 } else {
1565 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001566 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001567 TentativeParsingAction TPA(*this);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001568 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1569 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001570 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001571 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1572 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001573 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001574 } else {
1575 TPA.Revert();
1576 Arg.back() = OMPD_unknown;
1577 }
1578 } else
1579 TPA.Revert();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001580 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001581
Carlo Bertollib4adf552016-01-15 18:50:31 +00001582 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1583 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1584 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001585 if (NeedAnExpression) {
1586 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001587 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1588 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001589 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001590 }
1591
1592 // Parse ')'.
1593 T.consumeClose();
1594
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001595 if (NeedAnExpression && Val.isInvalid())
1596 return nullptr;
1597
Alexey Bataev56dafe82014-06-20 07:16:17 +00001598 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001599 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001600 T.getCloseLocation());
1601}
1602
Alexey Bataevc5e02582014-06-16 07:08:35 +00001603static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1604 UnqualifiedId &ReductionId) {
1605 SourceLocation TemplateKWLoc;
1606 if (ReductionIdScopeSpec.isEmpty()) {
1607 auto OOK = OO_None;
1608 switch (P.getCurToken().getKind()) {
1609 case tok::plus:
1610 OOK = OO_Plus;
1611 break;
1612 case tok::minus:
1613 OOK = OO_Minus;
1614 break;
1615 case tok::star:
1616 OOK = OO_Star;
1617 break;
1618 case tok::amp:
1619 OOK = OO_Amp;
1620 break;
1621 case tok::pipe:
1622 OOK = OO_Pipe;
1623 break;
1624 case tok::caret:
1625 OOK = OO_Caret;
1626 break;
1627 case tok::ampamp:
1628 OOK = OO_AmpAmp;
1629 break;
1630 case tok::pipepipe:
1631 OOK = OO_PipePipe;
1632 break;
1633 default:
1634 break;
1635 }
1636 if (OOK != OO_None) {
1637 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001638 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001639 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1640 return false;
1641 }
1642 }
1643 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1644 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00001645 /*AllowConstructorName*/ false,
1646 /*AllowDeductionGuide*/ false,
1647 nullptr, TemplateKWLoc, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001648}
1649
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001650/// Parses clauses with list.
1651bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1652 OpenMPClauseKind Kind,
1653 SmallVectorImpl<Expr *> &Vars,
1654 OpenMPVarListDataTy &Data) {
1655 UnqualifiedId UnqualifiedReductionId;
1656 bool InvalidReductionId = false;
1657 bool MapTypeModifierSpecified = false;
1658
1659 // Parse '('.
1660 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1661 if (T.expectAndConsume(diag::err_expected_lparen_after,
1662 getOpenMPClauseName(Kind)))
1663 return true;
1664
1665 bool NeedRParenForLinear = false;
1666 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1667 tok::annot_pragma_openmp_end);
1668 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00001669 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
1670 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001671 ColonProtectionRAIIObject ColonRAII(*this);
1672 if (getLangOpts().CPlusPlus)
1673 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1674 /*ObjectType=*/nullptr,
1675 /*EnteringContext=*/false);
1676 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1677 UnqualifiedReductionId);
1678 if (InvalidReductionId) {
1679 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1680 StopBeforeMatch);
1681 }
1682 if (Tok.is(tok::colon))
1683 Data.ColonLoc = ConsumeToken();
1684 else
1685 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1686 if (!InvalidReductionId)
1687 Data.ReductionId =
1688 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1689 } else if (Kind == OMPC_depend) {
1690 // Handle dependency type for depend clause.
1691 ColonProtectionRAIIObject ColonRAII(*this);
1692 Data.DepKind =
1693 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1694 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1695 Data.DepLinMapLoc = Tok.getLocation();
1696
1697 if (Data.DepKind == OMPC_DEPEND_unknown) {
1698 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1699 StopBeforeMatch);
1700 } else {
1701 ConsumeToken();
1702 // Special processing for depend(source) clause.
1703 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1704 // Parse ')'.
1705 T.consumeClose();
1706 return false;
1707 }
1708 }
1709 if (Tok.is(tok::colon))
1710 Data.ColonLoc = ConsumeToken();
1711 else {
1712 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1713 : diag::warn_pragma_expected_colon)
1714 << "dependency type";
1715 }
1716 } else if (Kind == OMPC_linear) {
1717 // Try to parse modifier if any.
1718 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1719 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1720 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1721 Data.DepLinMapLoc = ConsumeToken();
1722 LinearT.consumeOpen();
1723 NeedRParenForLinear = true;
1724 }
1725 } else if (Kind == OMPC_map) {
1726 // Handle map type for map clause.
1727 ColonProtectionRAIIObject ColonRAII(*this);
1728
1729 /// The map clause modifier token can be either a identifier or the C++
1730 /// delete keyword.
1731 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1732 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1733 };
1734
1735 // The first identifier may be a list item, a map-type or a
1736 // map-type-modifier. The map modifier can also be delete which has the same
1737 // spelling of the C++ delete keyword.
1738 Data.MapType =
1739 IsMapClauseModifierToken(Tok)
1740 ? static_cast<OpenMPMapClauseKind>(
1741 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1742 : OMPC_MAP_unknown;
1743 Data.DepLinMapLoc = Tok.getLocation();
1744 bool ColonExpected = false;
1745
1746 if (IsMapClauseModifierToken(Tok)) {
1747 if (PP.LookAhead(0).is(tok::colon)) {
1748 if (Data.MapType == OMPC_MAP_unknown)
1749 Diag(Tok, diag::err_omp_unknown_map_type);
1750 else if (Data.MapType == OMPC_MAP_always)
1751 Diag(Tok, diag::err_omp_map_type_missing);
1752 ConsumeToken();
1753 } else if (PP.LookAhead(0).is(tok::comma)) {
1754 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1755 PP.LookAhead(2).is(tok::colon)) {
1756 Data.MapTypeModifier = Data.MapType;
1757 if (Data.MapTypeModifier != OMPC_MAP_always) {
1758 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1759 Data.MapTypeModifier = OMPC_MAP_unknown;
1760 } else
1761 MapTypeModifierSpecified = true;
1762
1763 ConsumeToken();
1764 ConsumeToken();
1765
1766 Data.MapType =
1767 IsMapClauseModifierToken(Tok)
1768 ? static_cast<OpenMPMapClauseKind>(
1769 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1770 : OMPC_MAP_unknown;
1771 if (Data.MapType == OMPC_MAP_unknown ||
1772 Data.MapType == OMPC_MAP_always)
1773 Diag(Tok, diag::err_omp_unknown_map_type);
1774 ConsumeToken();
1775 } else {
1776 Data.MapType = OMPC_MAP_tofrom;
1777 Data.IsMapTypeImplicit = true;
1778 }
Carlo Bertollid8844b92017-05-03 15:28:48 +00001779 } else if (IsMapClauseModifierToken(PP.LookAhead(0))) {
1780 if (PP.LookAhead(1).is(tok::colon)) {
1781 Data.MapTypeModifier = Data.MapType;
1782 if (Data.MapTypeModifier != OMPC_MAP_always) {
1783 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1784 Data.MapTypeModifier = OMPC_MAP_unknown;
1785 } else
1786 MapTypeModifierSpecified = true;
1787
1788 ConsumeToken();
1789
1790 Data.MapType =
1791 IsMapClauseModifierToken(Tok)
1792 ? static_cast<OpenMPMapClauseKind>(
1793 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1794 : OMPC_MAP_unknown;
1795 if (Data.MapType == OMPC_MAP_unknown ||
1796 Data.MapType == OMPC_MAP_always)
1797 Diag(Tok, diag::err_omp_unknown_map_type);
1798 ConsumeToken();
1799 } else {
1800 Data.MapType = OMPC_MAP_tofrom;
1801 Data.IsMapTypeImplicit = true;
1802 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001803 } else {
1804 Data.MapType = OMPC_MAP_tofrom;
1805 Data.IsMapTypeImplicit = true;
1806 }
1807 } else {
1808 Data.MapType = OMPC_MAP_tofrom;
1809 Data.IsMapTypeImplicit = true;
1810 }
1811
1812 if (Tok.is(tok::colon))
1813 Data.ColonLoc = ConsumeToken();
1814 else if (ColonExpected)
1815 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1816 }
1817
Alexey Bataevfa312f32017-07-21 18:48:21 +00001818 bool IsComma =
1819 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
1820 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1821 (Kind == OMPC_reduction && !InvalidReductionId) ||
1822 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1823 (!MapTypeModifierSpecified ||
1824 Data.MapTypeModifier == OMPC_MAP_always)) ||
1825 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001826 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1827 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1828 Tok.isNot(tok::annot_pragma_openmp_end))) {
1829 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1830 // Parse variable
1831 ExprResult VarExpr =
1832 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1833 if (VarExpr.isUsable())
1834 Vars.push_back(VarExpr.get());
1835 else {
1836 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1837 StopBeforeMatch);
1838 }
1839 // Skip ',' if any
1840 IsComma = Tok.is(tok::comma);
1841 if (IsComma)
1842 ConsumeToken();
1843 else if (Tok.isNot(tok::r_paren) &&
1844 Tok.isNot(tok::annot_pragma_openmp_end) &&
1845 (!MayHaveTail || Tok.isNot(tok::colon)))
1846 Diag(Tok, diag::err_omp_expected_punc)
1847 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1848 : getOpenMPClauseName(Kind))
1849 << (Kind == OMPC_flush);
1850 }
1851
1852 // Parse ')' for linear clause with modifier.
1853 if (NeedRParenForLinear)
1854 LinearT.consumeClose();
1855
1856 // Parse ':' linear-step (or ':' alignment).
1857 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1858 if (MustHaveTail) {
1859 Data.ColonLoc = Tok.getLocation();
1860 SourceLocation ELoc = ConsumeToken();
1861 ExprResult Tail = ParseAssignmentExpression();
1862 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1863 if (Tail.isUsable())
1864 Data.TailExpr = Tail.get();
1865 else
1866 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1867 StopBeforeMatch);
1868 }
1869
1870 // Parse ')'.
1871 T.consumeClose();
1872 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1873 Vars.empty()) ||
1874 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1875 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1876 return true;
1877 return false;
1878}
1879
Alexander Musman1bb328c2014-06-04 13:06:39 +00001880/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00001881/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
1882/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001883///
1884/// private-clause:
1885/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001886/// firstprivate-clause:
1887/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001888/// lastprivate-clause:
1889/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001890/// shared-clause:
1891/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001892/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001893/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001894/// aligned-clause:
1895/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001896/// reduction-clause:
1897/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00001898/// task_reduction-clause:
1899/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00001900/// in_reduction-clause:
1901/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001902/// copyprivate-clause:
1903/// 'copyprivate' '(' list ')'
1904/// flush-clause:
1905/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001906/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001907/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001908/// map-clause:
1909/// 'map' '(' [ [ always , ]
1910/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001911/// to-clause:
1912/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001913/// from-clause:
1914/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001915/// use_device_ptr-clause:
1916/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001917/// is_device_ptr-clause:
1918/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001919///
Alexey Bataev182227b2015-08-20 10:54:39 +00001920/// For 'linear' clause linear-list may have the following forms:
1921/// list
1922/// modifier(list)
1923/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001924OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1925 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001926 SourceLocation Loc = Tok.getLocation();
1927 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001928 SmallVector<Expr *, 4> Vars;
1929 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001930
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001931 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001932 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001933
Alexey Bataevc5e02582014-06-16 07:08:35 +00001934 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001935 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1936 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1937 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1938 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001939}
1940