blob: d4cdc8e8596d1db1b75f6f586cc28aaadf205fa2 [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
Chandler Carruth5553d0d2014-01-07 11:51:46 +000014#include "RAIIObjectsForParser.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000016#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000017#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000018#include "clang/Parse/Parser.h"
19#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,
Carlo Bertolli9925f152016-06-27 14:55:37 +000042 OMPD_distribute_parallel
Dmitry Polukhin82478332016-02-13 06:53:38 +000043};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000044
45class ThreadprivateListParserHelper final {
46 SmallVector<Expr *, 4> Identifiers;
47 Parser *P;
48
49public:
50 ThreadprivateListParserHelper(Parser *P) : P(P) {}
51 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
52 ExprResult Res =
53 P->getActions().ActOnOpenMPIdExpression(P->getCurScope(), SS, NameInfo);
54 if (Res.isUsable())
55 Identifiers.push_back(Res.get());
56 }
57 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
58};
Dmitry Polukhin82478332016-02-13 06:53:38 +000059} // namespace
60
61// Map token string to extended OMP token kind that are
62// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
63static unsigned getOpenMPDirectiveKindEx(StringRef S) {
64 auto DKind = getOpenMPDirectiveKind(S);
65 if (DKind != OMPD_unknown)
66 return DKind;
67
68 return llvm::StringSwitch<unsigned>(S)
69 .Case("cancellation", OMPD_cancellation)
70 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000071 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000072 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000073 .Case("enter", OMPD_enter)
74 .Case("exit", OMPD_exit)
75 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000076 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000077 .Case("update", OMPD_update)
Dmitry Polukhin82478332016-02-13 06:53:38 +000078 .Default(OMPD_unknown);
79}
80
Alexey Bataev4acb8592014-07-07 13:01:15 +000081static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000082 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
83 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
84 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000085 static const unsigned F[][3] = {
86 { OMPD_cancellation, OMPD_point, OMPD_cancellation_point },
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000087 { OMPD_declare, OMPD_reduction, OMPD_declare_reduction },
Alexey Bataev587e1de2016-03-30 10:43:55 +000088 { OMPD_declare, OMPD_simd, OMPD_declare_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000089 { OMPD_declare, OMPD_target, OMPD_declare_target },
Carlo Bertolli9925f152016-06-27 14:55:37 +000090 { OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel },
91 { OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for },
Kelvin Li4a39add2016-07-05 05:00:15 +000092 { OMPD_distribute_parallel_for, OMPD_simd,
93 OMPD_distribute_parallel_for_simd },
Kelvin Li787f3fc2016-07-06 04:45:38 +000094 { OMPD_distribute, OMPD_simd, OMPD_distribute_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000095 { OMPD_end, OMPD_declare, OMPD_end_declare },
96 { OMPD_end_declare, OMPD_target, OMPD_end_declare_target },
Dmitry Polukhin82478332016-02-13 06:53:38 +000097 { OMPD_target, OMPD_data, OMPD_target_data },
98 { OMPD_target, OMPD_enter, OMPD_target_enter },
99 { OMPD_target, OMPD_exit, OMPD_target_exit },
Samuel Antao686c70c2016-05-26 17:30:50 +0000100 { OMPD_target, OMPD_update, OMPD_target_update },
Dmitry Polukhin82478332016-02-13 06:53:38 +0000101 { OMPD_target_enter, OMPD_data, OMPD_target_enter_data },
102 { OMPD_target_exit, OMPD_data, OMPD_target_exit_data },
103 { OMPD_for, OMPD_simd, OMPD_for_simd },
104 { OMPD_parallel, OMPD_for, OMPD_parallel_for },
105 { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
106 { OMPD_parallel, OMPD_sections, OMPD_parallel_sections },
107 { OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd },
108 { OMPD_target, OMPD_parallel, OMPD_target_parallel },
Kelvin Li986330c2016-07-20 22:57:10 +0000109 { OMPD_target, OMPD_simd, OMPD_target_simd },
Kelvin Lia579b912016-07-14 02:54:56 +0000110 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for },
Kelvin Li02532872016-08-05 14:37:37 +0000111 { OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd },
112 { OMPD_teams, OMPD_distribute, OMPD_teams_distribute }
Dmitry Polukhin82478332016-02-13 06:53:38 +0000113 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000114 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +0000115 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000116 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000117 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000118 ? static_cast<unsigned>(OMPD_unknown)
119 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
120 if (DKind == OMPD_unknown)
121 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000122
Alexander Musmanf82886e2014-09-18 05:12:34 +0000123 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000124 if (DKind != F[i][0])
125 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000126
Dmitry Polukhin82478332016-02-13 06:53:38 +0000127 Tok = P.getPreprocessor().LookAhead(0);
128 unsigned SDKind =
129 Tok.isAnnotation()
130 ? static_cast<unsigned>(OMPD_unknown)
131 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
132 if (SDKind == OMPD_unknown)
133 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000134
Dmitry Polukhin82478332016-02-13 06:53:38 +0000135 if (SDKind == F[i][1]) {
136 P.ConsumeToken();
137 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000138 }
139 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000140 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
141 : OMPD_unknown;
142}
143
144static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000145 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000146 Sema &Actions = P.getActions();
147 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000148 // Allow to use 'operator' keyword for C++ operators
149 bool WithOperator = false;
150 if (Tok.is(tok::kw_operator)) {
151 P.ConsumeToken();
152 Tok = P.getCurToken();
153 WithOperator = true;
154 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000155 switch (Tok.getKind()) {
156 case tok::plus: // '+'
157 OOK = OO_Plus;
158 break;
159 case tok::minus: // '-'
160 OOK = OO_Minus;
161 break;
162 case tok::star: // '*'
163 OOK = OO_Star;
164 break;
165 case tok::amp: // '&'
166 OOK = OO_Amp;
167 break;
168 case tok::pipe: // '|'
169 OOK = OO_Pipe;
170 break;
171 case tok::caret: // '^'
172 OOK = OO_Caret;
173 break;
174 case tok::ampamp: // '&&'
175 OOK = OO_AmpAmp;
176 break;
177 case tok::pipepipe: // '||'
178 OOK = OO_PipePipe;
179 break;
180 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000181 if (!WithOperator)
182 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000183 default:
184 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
185 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
186 Parser::StopBeforeMatch);
187 return DeclarationName();
188 }
189 P.ConsumeToken();
190 auto &DeclNames = Actions.getASTContext().DeclarationNames;
191 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
192 : DeclNames.getCXXOperatorName(OOK);
193}
194
195/// \brief Parse 'omp declare reduction' construct.
196///
197/// declare-reduction-directive:
198/// annot_pragma_openmp 'declare' 'reduction'
199/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
200/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
201/// annot_pragma_openmp_end
202/// <reduction_id> is either a base language identifier or one of the following
203/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
204///
205Parser::DeclGroupPtrTy
206Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
207 // Parse '('.
208 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
209 if (T.expectAndConsume(diag::err_expected_lparen_after,
210 getOpenMPDirectiveName(OMPD_declare_reduction))) {
211 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
212 return DeclGroupPtrTy();
213 }
214
215 DeclarationName Name = parseOpenMPReductionId(*this);
216 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
217 return DeclGroupPtrTy();
218
219 // Consume ':'.
220 bool IsCorrect = !ExpectAndConsume(tok::colon);
221
222 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
223 return DeclGroupPtrTy();
224
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000225 IsCorrect = IsCorrect && !Name.isEmpty();
226
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000227 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
228 Diag(Tok.getLocation(), diag::err_expected_type);
229 IsCorrect = false;
230 }
231
232 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
233 return DeclGroupPtrTy();
234
235 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
236 // Parse list of types until ':' token.
237 do {
238 ColonProtectionRAIIObject ColonRAII(*this);
239 SourceRange Range;
240 TypeResult TR = ParseTypeName(&Range, Declarator::PrototypeContext, AS);
241 if (TR.isUsable()) {
242 auto ReductionType =
243 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
244 if (!ReductionType.isNull()) {
245 ReductionTypes.push_back(
246 std::make_pair(ReductionType, Range.getBegin()));
247 }
248 } else {
249 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
250 StopBeforeMatch);
251 }
252
253 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
254 break;
255
256 // Consume ','.
257 if (ExpectAndConsume(tok::comma)) {
258 IsCorrect = false;
259 if (Tok.is(tok::annot_pragma_openmp_end)) {
260 Diag(Tok.getLocation(), diag::err_expected_type);
261 return DeclGroupPtrTy();
262 }
263 }
264 } while (Tok.isNot(tok::annot_pragma_openmp_end));
265
266 if (ReductionTypes.empty()) {
267 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
268 return DeclGroupPtrTy();
269 }
270
271 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
272 return DeclGroupPtrTy();
273
274 // Consume ':'.
275 if (ExpectAndConsume(tok::colon))
276 IsCorrect = false;
277
278 if (Tok.is(tok::annot_pragma_openmp_end)) {
279 Diag(Tok.getLocation(), diag::err_expected_expression);
280 return DeclGroupPtrTy();
281 }
282
283 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
284 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
285
286 // Parse <combiner> expression and then parse initializer if any for each
287 // correct type.
288 unsigned I = 0, E = ReductionTypes.size();
289 for (auto *D : DRD.get()) {
290 TentativeParsingAction TPA(*this);
291 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
292 Scope::OpenMPDirectiveScope);
293 // Parse <combiner> expression.
294 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
295 ExprResult CombinerResult =
296 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
297 D->getLocation(), /*DiscardedValue=*/true);
298 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
299
300 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
301 Tok.isNot(tok::annot_pragma_openmp_end)) {
302 TPA.Commit();
303 IsCorrect = false;
304 break;
305 }
306 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
307 ExprResult InitializerResult;
308 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
309 // Parse <initializer> expression.
310 if (Tok.is(tok::identifier) &&
311 Tok.getIdentifierInfo()->isStr("initializer"))
312 ConsumeToken();
313 else {
314 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
315 TPA.Commit();
316 IsCorrect = false;
317 break;
318 }
319 // Parse '('.
320 BalancedDelimiterTracker T(*this, tok::l_paren,
321 tok::annot_pragma_openmp_end);
322 IsCorrect =
323 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
324 IsCorrect;
325 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
326 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
327 Scope::OpenMPDirectiveScope);
328 // Parse expression.
329 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), D);
330 InitializerResult = Actions.ActOnFinishFullExpr(
331 ParseAssignmentExpression().get(), D->getLocation(),
332 /*DiscardedValue=*/true);
333 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
334 D, InitializerResult.get());
335 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
336 Tok.isNot(tok::annot_pragma_openmp_end)) {
337 TPA.Commit();
338 IsCorrect = false;
339 break;
340 }
341 IsCorrect =
342 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
343 }
344 }
345
346 ++I;
347 // Revert parsing if not the last type, otherwise accept it, we're done with
348 // parsing.
349 if (I != E)
350 TPA.Revert();
351 else
352 TPA.Commit();
353 }
354 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
355 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000356}
357
Alexey Bataev2af33e32016-04-07 12:45:37 +0000358namespace {
359/// RAII that recreates function context for correct parsing of clauses of
360/// 'declare simd' construct.
361/// OpenMP, 2.8.2 declare simd Construct
362/// The expressions appearing in the clauses of this directive are evaluated in
363/// the scope of the arguments of the function declaration or definition.
364class FNContextRAII final {
365 Parser &P;
366 Sema::CXXThisScopeRAII *ThisScope;
367 Parser::ParseScope *TempScope;
368 Parser::ParseScope *FnScope;
369 bool HasTemplateScope = false;
370 bool HasFunScope = false;
371 FNContextRAII() = delete;
372 FNContextRAII(const FNContextRAII &) = delete;
373 FNContextRAII &operator=(const FNContextRAII &) = delete;
374
375public:
376 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
377 Decl *D = *Ptr.get().begin();
378 NamedDecl *ND = dyn_cast<NamedDecl>(D);
379 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
380 Sema &Actions = P.getActions();
381
382 // Allow 'this' within late-parsed attributes.
383 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
384 ND && ND->isCXXInstanceMember());
385
386 // If the Decl is templatized, add template parameters to scope.
387 HasTemplateScope = D->isTemplateDecl();
388 TempScope =
389 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
390 if (HasTemplateScope)
391 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
392
393 // If the Decl is on a function, add function parameters to the scope.
394 HasFunScope = D->isFunctionOrFunctionTemplate();
395 FnScope = new Parser::ParseScope(&P, Scope::FnScope | Scope::DeclScope,
396 HasFunScope);
397 if (HasFunScope)
398 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
399 }
400 ~FNContextRAII() {
401 if (HasFunScope) {
402 P.getActions().ActOnExitFunctionContext();
403 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
404 }
405 if (HasTemplateScope)
406 TempScope->Exit();
407 delete FnScope;
408 delete TempScope;
409 delete ThisScope;
410 }
411};
412} // namespace
413
Alexey Bataevd93d3762016-04-12 09:35:56 +0000414/// Parses clauses for 'declare simd' directive.
415/// clause:
416/// 'inbranch' | 'notinbranch'
417/// 'simdlen' '(' <expr> ')'
418/// { 'uniform' '(' <argument_list> ')' }
419/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000420/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
421static bool parseDeclareSimdClauses(
422 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
423 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
424 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
425 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000426 SourceRange BSRange;
427 const Token &Tok = P.getCurToken();
428 bool IsError = false;
429 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
430 if (Tok.isNot(tok::identifier))
431 break;
432 OMPDeclareSimdDeclAttr::BranchStateTy Out;
433 IdentifierInfo *II = Tok.getIdentifierInfo();
434 StringRef ClauseName = II->getName();
435 // Parse 'inranch|notinbranch' clauses.
436 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
437 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
438 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
439 << ClauseName
440 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
441 IsError = true;
442 }
443 BS = Out;
444 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
445 P.ConsumeToken();
446 } else if (ClauseName.equals("simdlen")) {
447 if (SimdLen.isUsable()) {
448 P.Diag(Tok, diag::err_omp_more_one_clause)
449 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
450 IsError = true;
451 }
452 P.ConsumeToken();
453 SourceLocation RLoc;
454 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
455 if (SimdLen.isInvalid())
456 IsError = true;
457 } else {
458 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000459 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
460 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000461 Parser::OpenMPVarListDataTy Data;
462 auto *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000463 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000464 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000465 else if (CKind == OMPC_linear)
466 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000467
468 P.ConsumeToken();
469 if (P.ParseOpenMPVarList(OMPD_declare_simd,
470 getOpenMPClauseKind(ClauseName), *Vars, Data))
471 IsError = true;
472 if (CKind == OMPC_aligned)
473 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000474 else if (CKind == OMPC_linear) {
475 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
476 Data.DepLinMapLoc))
477 Data.LinKind = OMPC_LINEAR_val;
478 LinModifiers.append(Linears.size() - LinModifiers.size(),
479 Data.LinKind);
480 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
481 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000482 } else
483 // TODO: add parsing of other clauses.
484 break;
485 }
486 // Skip ',' if any.
487 if (Tok.is(tok::comma))
488 P.ConsumeToken();
489 }
490 return IsError;
491}
492
Alexey Bataev2af33e32016-04-07 12:45:37 +0000493/// Parse clauses for '#pragma omp declare simd'.
494Parser::DeclGroupPtrTy
495Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
496 CachedTokens &Toks, SourceLocation Loc) {
497 PP.EnterToken(Tok);
498 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
499 // Consume the previously pushed token.
500 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
501
502 FNContextRAII FnContext(*this, Ptr);
503 OMPDeclareSimdDeclAttr::BranchStateTy BS =
504 OMPDeclareSimdDeclAttr::BS_Undefined;
505 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000506 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000507 SmallVector<Expr *, 4> Aligneds;
508 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000509 SmallVector<Expr *, 4> Linears;
510 SmallVector<unsigned, 4> LinModifiers;
511 SmallVector<Expr *, 4> Steps;
512 bool IsError =
513 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
514 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000515 // Need to check for extra tokens.
516 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
517 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
518 << getOpenMPDirectiveName(OMPD_declare_simd);
519 while (Tok.isNot(tok::annot_pragma_openmp_end))
520 ConsumeAnyToken();
521 }
522 // Skip the last annot_pragma_openmp_end.
523 SourceLocation EndLoc = ConsumeToken();
Alexey Bataevd93d3762016-04-12 09:35:56 +0000524 if (!IsError) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000525 return Actions.ActOnOpenMPDeclareSimdDirective(
Alexey Bataevecba70f2016-04-12 11:02:11 +0000526 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
527 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataevd93d3762016-04-12 09:35:56 +0000528 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000529 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000530}
531
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000532/// \brief Parsing of declarative OpenMP directives.
533///
534/// threadprivate-directive:
535/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000536/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000537///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000538/// declare-reduction-directive:
539/// annot_pragma_openmp 'declare' 'reduction' [...]
540/// annot_pragma_openmp_end
541///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000542/// declare-simd-directive:
543/// annot_pragma_openmp 'declare simd' {<clause> [,]}
544/// annot_pragma_openmp_end
545/// <function declaration/definition>
546///
547Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
548 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
549 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000550 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000551 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000552
553 SourceLocation Loc = ConsumeToken();
Alexey Bataev4acb8592014-07-07 13:01:15 +0000554 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000555
556 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000557 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000558 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000559 ThreadprivateListParserHelper Helper(this);
560 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000561 // The last seen token is annot_pragma_openmp_end - need to check for
562 // extra tokens.
563 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
564 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000565 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000566 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000567 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000568 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000569 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000570 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
571 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000572 }
573 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000574 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000575 case OMPD_declare_reduction:
576 ConsumeToken();
577 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
578 // The last seen token is annot_pragma_openmp_end - need to check for
579 // extra tokens.
580 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
581 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
582 << getOpenMPDirectiveName(OMPD_declare_reduction);
583 while (Tok.isNot(tok::annot_pragma_openmp_end))
584 ConsumeAnyToken();
585 }
586 // Skip the last annot_pragma_openmp_end.
587 ConsumeToken();
588 return Res;
589 }
590 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000591 case OMPD_declare_simd: {
592 // The syntax is:
593 // { #pragma omp declare simd }
594 // <function-declaration-or-definition>
595 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000596 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000597 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000598 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
599 Toks.push_back(Tok);
600 ConsumeAnyToken();
601 }
602 Toks.push_back(Tok);
603 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000604
605 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000606 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000607 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000608 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000609 // Here we expect to see some function declaration.
610 if (AS == AS_none) {
611 assert(TagType == DeclSpec::TST_unspecified);
612 MaybeParseCXX11Attributes(Attrs);
613 MaybeParseMicrosoftAttributes(Attrs);
614 ParsingDeclSpec PDS(*this);
615 Ptr = ParseExternalDeclaration(Attrs, &PDS);
616 } else {
617 Ptr =
618 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
619 }
620 }
621 if (!Ptr) {
622 Diag(Loc, diag::err_omp_decl_in_declare_simd);
623 return DeclGroupPtrTy();
624 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000625 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000626 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000627 case OMPD_declare_target: {
628 SourceLocation DTLoc = ConsumeAnyToken();
629 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000630 // OpenMP 4.5 syntax with list of entities.
631 llvm::SmallSetVector<const NamedDecl*, 16> SameDirectiveDecls;
632 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
633 OMPDeclareTargetDeclAttr::MapTypeTy MT =
634 OMPDeclareTargetDeclAttr::MT_To;
635 if (Tok.is(tok::identifier)) {
636 IdentifierInfo *II = Tok.getIdentifierInfo();
637 StringRef ClauseName = II->getName();
638 // Parse 'to|link' clauses.
639 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
640 MT)) {
641 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
642 << ClauseName;
643 break;
644 }
645 ConsumeToken();
646 }
647 auto Callback = [this, MT, &SameDirectiveDecls](
648 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
649 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
650 SameDirectiveDecls);
651 };
652 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, true))
653 break;
654
655 // Consume optional ','.
656 if (Tok.is(tok::comma))
657 ConsumeToken();
658 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000659 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000660 ConsumeAnyToken();
661 return DeclGroupPtrTy();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000662 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000663
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000664 // Skip the last annot_pragma_openmp_end.
665 ConsumeAnyToken();
666
667 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
668 return DeclGroupPtrTy();
669
670 DKind = ParseOpenMPDirectiveKind(*this);
671 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
672 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
673 ParsedAttributesWithRange attrs(AttrFactory);
674 MaybeParseCXX11Attributes(attrs);
675 MaybeParseMicrosoftAttributes(attrs);
676 ParseExternalDeclaration(attrs);
677 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
678 TentativeParsingAction TPA(*this);
679 ConsumeToken();
680 DKind = ParseOpenMPDirectiveKind(*this);
681 if (DKind != OMPD_end_declare_target)
682 TPA.Revert();
683 else
684 TPA.Commit();
685 }
686 }
687
688 if (DKind == OMPD_end_declare_target) {
689 ConsumeAnyToken();
690 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
691 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
692 << getOpenMPDirectiveName(OMPD_end_declare_target);
693 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
694 }
695 // Skip the last annot_pragma_openmp_end.
696 ConsumeAnyToken();
697 } else {
698 Diag(Tok, diag::err_expected_end_declare_target);
699 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
700 }
701 Actions.ActOnFinishOpenMPDeclareTargetDirective();
702 return DeclGroupPtrTy();
703 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000704 case OMPD_unknown:
705 Diag(Tok, diag::err_omp_unknown_directive);
706 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000707 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000708 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000709 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000710 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000711 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000712 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000713 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000714 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000715 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000716 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000717 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000718 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000719 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000720 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000721 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000722 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000723 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000724 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000725 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000726 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000727 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000728 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000729 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000730 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000731 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000732 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000733 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000734 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000735 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000736 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000737 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000738 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000739 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000740 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000741 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000742 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000743 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000744 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000745 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000746 case OMPD_teams_distribute:
Alexey Bataeva769e072013-03-22 06:34:35 +0000747 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000748 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000749 break;
750 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000751 while (Tok.isNot(tok::annot_pragma_openmp_end))
752 ConsumeAnyToken();
753 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000754 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000755}
756
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000757/// \brief Parsing of declarative or executable OpenMP directives.
758///
759/// threadprivate-directive:
760/// annot_pragma_openmp 'threadprivate' simple-variable-list
761/// annot_pragma_openmp_end
762///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000763/// declare-reduction-directive:
764/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
765/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
766/// ('omp_priv' '=' <expression>|<function_call>) ')']
767/// annot_pragma_openmp_end
768///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000769/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000770/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000771/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
772/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000773/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000774/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000775/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000776/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000777/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000778/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000779/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000780/// 'target parallel for simd' | 'target simd' |
781/// 'teams distribute' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000782/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000783///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000784StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
785 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000786 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000787 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000788 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000789 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000790 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000791 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000792 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000793 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000794 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000795 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000796 // Name of critical directive.
797 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000798 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000799 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000800 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000801
802 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000803 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000804 if (Allowed != ACK_Any) {
805 Diag(Tok, diag::err_omp_immediate_directive)
806 << getOpenMPDirectiveName(DKind) << 0;
807 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000808 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000809 ThreadprivateListParserHelper Helper(this);
810 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000811 // The last seen token is annot_pragma_openmp_end - need to check for
812 // extra tokens.
813 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
814 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000815 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000816 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000817 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000818 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
819 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000820 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
821 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000822 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000823 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000824 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000825 case OMPD_declare_reduction:
826 ConsumeToken();
827 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
828 // The last seen token is annot_pragma_openmp_end - need to check for
829 // extra tokens.
830 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
831 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
832 << getOpenMPDirectiveName(OMPD_declare_reduction);
833 while (Tok.isNot(tok::annot_pragma_openmp_end))
834 ConsumeAnyToken();
835 }
836 ConsumeAnyToken();
837 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
838 } else
839 SkipUntil(tok::annot_pragma_openmp_end);
840 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000841 case OMPD_flush:
842 if (PP.LookAhead(0).is(tok::l_paren)) {
843 FlushHasClause = true;
844 // Push copy of the current token back to stream to properly parse
845 // pseudo-clause OMPFlushClause.
846 PP.EnterToken(Tok);
847 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000848 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000849 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000850 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000851 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000852 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000853 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000854 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000855 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000856 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000857 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000858 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000859 }
860 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000861 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000862 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000863 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000864 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000865 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000866 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000867 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000868 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000869 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000870 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000871 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000872 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000873 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000874 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000875 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000876 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000877 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000878 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000879 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000880 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000881 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000882 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000883 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000884 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000885 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +0000886 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000887 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000888 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000889 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000890 case OMPD_target_simd:
891 case OMPD_teams_distribute: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000892 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000893 // Parse directive name of the 'critical' directive if any.
894 if (DKind == OMPD_critical) {
895 BalancedDelimiterTracker T(*this, tok::l_paren,
896 tok::annot_pragma_openmp_end);
897 if (!T.consumeOpen()) {
898 if (Tok.isAnyIdentifier()) {
899 DirName =
900 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
901 ConsumeAnyToken();
902 } else {
903 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
904 }
905 T.consumeClose();
906 }
Alexey Bataev80909872015-07-02 11:25:17 +0000907 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000908 CancelRegion = ParseOpenMPDirectiveKind(*this);
909 if (Tok.isNot(tok::annot_pragma_openmp_end))
910 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000911 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000912
Alexey Bataevf29276e2014-06-18 04:14:57 +0000913 if (isOpenMPLoopDirective(DKind))
914 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
915 if (isOpenMPSimdDirective(DKind))
916 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
917 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000918 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000919
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000920 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000921 OpenMPClauseKind CKind =
922 Tok.isAnnotation()
923 ? OMPC_unknown
924 : FlushHasClause ? OMPC_flush
925 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000926 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000927 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000928 OMPClause *Clause =
929 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000930 FirstClauses[CKind].setInt(true);
931 if (Clause) {
932 FirstClauses[CKind].setPointer(Clause);
933 Clauses.push_back(Clause);
934 }
935
936 // Skip ',' if any.
937 if (Tok.is(tok::comma))
938 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000939 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000940 }
941 // End location of the directive.
942 EndLoc = Tok.getLocation();
943 // Consume final annot_pragma_openmp_end.
944 ConsumeToken();
945
Alexey Bataeveb482352015-12-18 05:05:56 +0000946 // OpenMP [2.13.8, ordered Construct, Syntax]
947 // If the depend clause is specified, the ordered construct is a stand-alone
948 // directive.
949 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000950 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000951 Diag(Loc, diag::err_omp_immediate_directive)
952 << getOpenMPDirectiveName(DKind) << 1
953 << getOpenMPClauseName(OMPC_depend);
954 }
955 HasAssociatedStatement = false;
956 }
957
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000958 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000959 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000960 // The body is a block scope like in Lambdas and Blocks.
961 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000962 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000963 Actions.ActOnStartOfCompoundStmt();
964 // Parse statement
965 AssociatedStmt = ParseStatement();
966 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000967 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000968 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000969 Directive = Actions.ActOnOpenMPExecutableDirective(
970 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
971 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000972
973 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000974 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000975 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000976 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000977 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000978 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000979 case OMPD_declare_target:
980 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +0000981 Diag(Tok, diag::err_omp_unexpected_directive)
982 << getOpenMPDirectiveName(DKind);
983 SkipUntil(tok::annot_pragma_openmp_end);
984 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000985 case OMPD_unknown:
986 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000987 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000988 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000989 }
990 return Directive;
991}
992
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000993// Parses simple list:
994// simple-variable-list:
995// '(' id-expression {, id-expression} ')'
996//
997bool Parser::ParseOpenMPSimpleVarList(
998 OpenMPDirectiveKind Kind,
999 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1000 Callback,
1001 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001002 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001003 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001004 if (T.expectAndConsume(diag::err_expected_lparen_after,
1005 getOpenMPDirectiveName(Kind)))
1006 return true;
1007 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001008 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001009
1010 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001011 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001012 CXXScopeSpec SS;
1013 SourceLocation TemplateKWLoc;
1014 UnqualifiedId Name;
1015 // Read var name.
1016 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001017 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001018
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001019 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001020 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001021 IsCorrect = false;
1022 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001023 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +00001024 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001025 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001026 IsCorrect = false;
1027 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001028 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001029 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1030 Tok.isNot(tok::annot_pragma_openmp_end)) {
1031 IsCorrect = false;
1032 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001033 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001034 Diag(PrevTok.getLocation(), diag::err_expected)
1035 << tok::identifier
1036 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001037 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001038 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001039 }
1040 // Consume ','.
1041 if (Tok.is(tok::comma)) {
1042 ConsumeToken();
1043 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001044 }
1045
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001046 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001047 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001048 IsCorrect = false;
1049 }
1050
1051 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001052 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001053
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001054 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001055}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001056
1057/// \brief Parsing of OpenMP clauses.
1058///
1059/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001060/// if-clause | final-clause | num_threads-clause | safelen-clause |
1061/// default-clause | private-clause | firstprivate-clause | shared-clause
1062/// | linear-clause | aligned-clause | collapse-clause |
1063/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001064/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001065/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001066/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001067/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001068/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001069/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Carlo Bertolli70594e92016-07-13 17:16:49 +00001070/// from-clause | is_device_ptr-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001071///
1072OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1073 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001074 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001075 bool ErrorFound = false;
1076 // Check if clause is allowed for the given directive.
1077 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001078 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1079 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001080 ErrorFound = true;
1081 }
1082
1083 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001084 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001085 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001086 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001087 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001088 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001089 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001090 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001091 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001092 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001093 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001094 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001095 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001096 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001097 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001098 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001099 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001100 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001101 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001102 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001103 // OpenMP [2.9.1, target data construct, Restrictions]
1104 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001105 // OpenMP [2.11.1, task Construct, Restrictions]
1106 // At most one if clause can appear on the directive.
1107 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001108 // OpenMP [teams Construct, Restrictions]
1109 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001110 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001111 // OpenMP [2.9.1, task Construct, Restrictions]
1112 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001113 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1114 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001115 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1116 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001117 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001118 Diag(Tok, diag::err_omp_more_one_clause)
1119 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001120 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001121 }
1122
Alexey Bataev10e775f2015-07-30 11:36:16 +00001123 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1124 Clause = ParseOpenMPClause(CKind);
1125 else
1126 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001127 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001128 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001129 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001130 // OpenMP [2.14.3.1, Restrictions]
1131 // Only a single default clause may be specified on a parallel, task or
1132 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001133 // OpenMP [2.5, parallel Construct, Restrictions]
1134 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001135 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001136 Diag(Tok, diag::err_omp_more_one_clause)
1137 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001138 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001139 }
1140
1141 Clause = ParseOpenMPSimpleClause(CKind);
1142 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001143 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001144 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001145 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001146 // OpenMP [2.7.1, Restrictions, p. 3]
1147 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001148 // OpenMP [2.10.4, Restrictions, p. 106]
1149 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001150 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001151 Diag(Tok, diag::err_omp_more_one_clause)
1152 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001153 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001154 }
1155
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001156 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001157 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1158 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001159 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001160 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001161 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001162 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001163 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001164 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001165 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001166 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001167 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001168 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001169 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001170 // OpenMP [2.7.1, Restrictions, p. 9]
1171 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001172 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1173 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001174 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001175 Diag(Tok, diag::err_omp_more_one_clause)
1176 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001177 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001178 }
1179
1180 Clause = ParseOpenMPClause(CKind);
1181 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001182 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001183 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001184 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001185 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001186 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001187 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001188 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001189 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001190 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001191 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001192 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001193 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001194 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001195 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001196 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001197 case OMPC_is_device_ptr:
Alexey Bataeveb482352015-12-18 05:05:56 +00001198 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001199 break;
1200 case OMPC_unknown:
1201 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001202 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001203 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001204 break;
1205 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001206 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001207 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1208 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001209 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001210 break;
1211 }
Craig Topper161e4db2014-05-21 06:02:52 +00001212 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001213}
1214
Alexey Bataev2af33e32016-04-07 12:45:37 +00001215/// Parses simple expression in parens for single-expression clauses of OpenMP
1216/// constructs.
1217/// \param RLoc Returned location of right paren.
1218ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1219 SourceLocation &RLoc) {
1220 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1221 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1222 return ExprError();
1223
1224 SourceLocation ELoc = Tok.getLocation();
1225 ExprResult LHS(ParseCastExpression(
1226 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1227 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1228 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1229
1230 // Parse ')'.
1231 T.consumeClose();
1232
1233 RLoc = T.getCloseLocation();
1234 return Val;
1235}
1236
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001237/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001238/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001239/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001240///
Alexey Bataev3778b602014-07-17 07:32:53 +00001241/// final-clause:
1242/// 'final' '(' expression ')'
1243///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001244/// num_threads-clause:
1245/// 'num_threads' '(' expression ')'
1246///
1247/// safelen-clause:
1248/// 'safelen' '(' expression ')'
1249///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001250/// simdlen-clause:
1251/// 'simdlen' '(' expression ')'
1252///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001253/// collapse-clause:
1254/// 'collapse' '(' expression ')'
1255///
Alexey Bataeva0569352015-12-01 10:17:31 +00001256/// priority-clause:
1257/// 'priority' '(' expression ')'
1258///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001259/// grainsize-clause:
1260/// 'grainsize' '(' expression ')'
1261///
Alexey Bataev382967a2015-12-08 12:06:20 +00001262/// num_tasks-clause:
1263/// 'num_tasks' '(' expression ')'
1264///
Alexey Bataev28c75412015-12-15 08:19:24 +00001265/// hint-clause:
1266/// 'hint' '(' expression ')'
1267///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001268OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1269 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001270 SourceLocation LLoc = Tok.getLocation();
1271 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001272
Alexey Bataev2af33e32016-04-07 12:45:37 +00001273 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001274
1275 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001276 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001277
Alexey Bataev2af33e32016-04-07 12:45:37 +00001278 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001279}
1280
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001281/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001282///
1283/// default-clause:
1284/// 'default' '(' 'none' | 'shared' ')
1285///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001286/// proc_bind-clause:
1287/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1288///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001289OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1290 SourceLocation Loc = Tok.getLocation();
1291 SourceLocation LOpen = ConsumeToken();
1292 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001293 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001294 if (T.expectAndConsume(diag::err_expected_lparen_after,
1295 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001296 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001297
Alexey Bataeva55ed262014-05-28 06:15:33 +00001298 unsigned Type = getOpenMPSimpleClauseType(
1299 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001300 SourceLocation TypeLoc = Tok.getLocation();
1301 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1302 Tok.isNot(tok::annot_pragma_openmp_end))
1303 ConsumeAnyToken();
1304
1305 // Parse ')'.
1306 T.consumeClose();
1307
1308 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1309 Tok.getLocation());
1310}
1311
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001312/// \brief Parsing of OpenMP clauses like 'ordered'.
1313///
1314/// ordered-clause:
1315/// 'ordered'
1316///
Alexey Bataev236070f2014-06-20 11:19:47 +00001317/// nowait-clause:
1318/// 'nowait'
1319///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001320/// untied-clause:
1321/// 'untied'
1322///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001323/// mergeable-clause:
1324/// 'mergeable'
1325///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001326/// read-clause:
1327/// 'read'
1328///
Alexey Bataev346265e2015-09-25 10:37:12 +00001329/// threads-clause:
1330/// 'threads'
1331///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001332/// simd-clause:
1333/// 'simd'
1334///
Alexey Bataevb825de12015-12-07 10:51:44 +00001335/// nogroup-clause:
1336/// 'nogroup'
1337///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001338OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1339 SourceLocation Loc = Tok.getLocation();
1340 ConsumeAnyToken();
1341
1342 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1343}
1344
1345
Alexey Bataev56dafe82014-06-20 07:16:17 +00001346/// \brief Parsing of OpenMP clauses with single expressions and some additional
1347/// argument like 'schedule' or 'dist_schedule'.
1348///
1349/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001350/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1351/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001352///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001353/// if-clause:
1354/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1355///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001356/// defaultmap:
1357/// 'defaultmap' '(' modifier ':' kind ')'
1358///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001359OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1360 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001361 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001362 // Parse '('.
1363 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1364 if (T.expectAndConsume(diag::err_expected_lparen_after,
1365 getOpenMPClauseName(Kind)))
1366 return nullptr;
1367
1368 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001369 SmallVector<unsigned, 4> Arg;
1370 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001371 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001372 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1373 Arg.resize(NumberOfElements);
1374 KLoc.resize(NumberOfElements);
1375 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1376 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1377 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1378 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001379 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001380 if (KindModifier > OMPC_SCHEDULE_unknown) {
1381 // Parse 'modifier'
1382 Arg[Modifier1] = KindModifier;
1383 KLoc[Modifier1] = Tok.getLocation();
1384 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1385 Tok.isNot(tok::annot_pragma_openmp_end))
1386 ConsumeAnyToken();
1387 if (Tok.is(tok::comma)) {
1388 // Parse ',' 'modifier'
1389 ConsumeAnyToken();
1390 KindModifier = getOpenMPSimpleClauseType(
1391 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1392 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1393 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001394 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001395 KLoc[Modifier2] = Tok.getLocation();
1396 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1397 Tok.isNot(tok::annot_pragma_openmp_end))
1398 ConsumeAnyToken();
1399 }
1400 // Parse ':'
1401 if (Tok.is(tok::colon))
1402 ConsumeAnyToken();
1403 else
1404 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1405 KindModifier = getOpenMPSimpleClauseType(
1406 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1407 }
1408 Arg[ScheduleKind] = KindModifier;
1409 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001410 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1411 Tok.isNot(tok::annot_pragma_openmp_end))
1412 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001413 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1414 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1415 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001416 Tok.is(tok::comma))
1417 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001418 } else if (Kind == OMPC_dist_schedule) {
1419 Arg.push_back(getOpenMPSimpleClauseType(
1420 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1421 KLoc.push_back(Tok.getLocation());
1422 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1423 Tok.isNot(tok::annot_pragma_openmp_end))
1424 ConsumeAnyToken();
1425 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1426 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001427 } else if (Kind == OMPC_defaultmap) {
1428 // Get a defaultmap modifier
1429 Arg.push_back(getOpenMPSimpleClauseType(
1430 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1431 KLoc.push_back(Tok.getLocation());
1432 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1433 Tok.isNot(tok::annot_pragma_openmp_end))
1434 ConsumeAnyToken();
1435 // Parse ':'
1436 if (Tok.is(tok::colon))
1437 ConsumeAnyToken();
1438 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1439 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1440 // Get a defaultmap kind
1441 Arg.push_back(getOpenMPSimpleClauseType(
1442 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1443 KLoc.push_back(Tok.getLocation());
1444 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1445 Tok.isNot(tok::annot_pragma_openmp_end))
1446 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001447 } else {
1448 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001449 KLoc.push_back(Tok.getLocation());
1450 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1451 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001452 ConsumeToken();
1453 if (Tok.is(tok::colon))
1454 DelimLoc = ConsumeToken();
1455 else
1456 Diag(Tok, diag::warn_pragma_expected_colon)
1457 << "directive name modifier";
1458 }
1459 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001460
Carlo Bertollib4adf552016-01-15 18:50:31 +00001461 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1462 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1463 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001464 if (NeedAnExpression) {
1465 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001466 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1467 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001468 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001469 }
1470
1471 // Parse ')'.
1472 T.consumeClose();
1473
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001474 if (NeedAnExpression && Val.isInvalid())
1475 return nullptr;
1476
Alexey Bataev56dafe82014-06-20 07:16:17 +00001477 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001478 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001479 T.getCloseLocation());
1480}
1481
Alexey Bataevc5e02582014-06-16 07:08:35 +00001482static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1483 UnqualifiedId &ReductionId) {
1484 SourceLocation TemplateKWLoc;
1485 if (ReductionIdScopeSpec.isEmpty()) {
1486 auto OOK = OO_None;
1487 switch (P.getCurToken().getKind()) {
1488 case tok::plus:
1489 OOK = OO_Plus;
1490 break;
1491 case tok::minus:
1492 OOK = OO_Minus;
1493 break;
1494 case tok::star:
1495 OOK = OO_Star;
1496 break;
1497 case tok::amp:
1498 OOK = OO_Amp;
1499 break;
1500 case tok::pipe:
1501 OOK = OO_Pipe;
1502 break;
1503 case tok::caret:
1504 OOK = OO_Caret;
1505 break;
1506 case tok::ampamp:
1507 OOK = OO_AmpAmp;
1508 break;
1509 case tok::pipepipe:
1510 OOK = OO_PipePipe;
1511 break;
1512 default:
1513 break;
1514 }
1515 if (OOK != OO_None) {
1516 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001517 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001518 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1519 return false;
1520 }
1521 }
1522 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1523 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001524 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001525 TemplateKWLoc, ReductionId);
1526}
1527
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001528/// Parses clauses with list.
1529bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1530 OpenMPClauseKind Kind,
1531 SmallVectorImpl<Expr *> &Vars,
1532 OpenMPVarListDataTy &Data) {
1533 UnqualifiedId UnqualifiedReductionId;
1534 bool InvalidReductionId = false;
1535 bool MapTypeModifierSpecified = false;
1536
1537 // Parse '('.
1538 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1539 if (T.expectAndConsume(diag::err_expected_lparen_after,
1540 getOpenMPClauseName(Kind)))
1541 return true;
1542
1543 bool NeedRParenForLinear = false;
1544 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1545 tok::annot_pragma_openmp_end);
1546 // Handle reduction-identifier for reduction clause.
1547 if (Kind == OMPC_reduction) {
1548 ColonProtectionRAIIObject ColonRAII(*this);
1549 if (getLangOpts().CPlusPlus)
1550 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1551 /*ObjectType=*/nullptr,
1552 /*EnteringContext=*/false);
1553 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1554 UnqualifiedReductionId);
1555 if (InvalidReductionId) {
1556 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1557 StopBeforeMatch);
1558 }
1559 if (Tok.is(tok::colon))
1560 Data.ColonLoc = ConsumeToken();
1561 else
1562 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1563 if (!InvalidReductionId)
1564 Data.ReductionId =
1565 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1566 } else if (Kind == OMPC_depend) {
1567 // Handle dependency type for depend clause.
1568 ColonProtectionRAIIObject ColonRAII(*this);
1569 Data.DepKind =
1570 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1571 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1572 Data.DepLinMapLoc = Tok.getLocation();
1573
1574 if (Data.DepKind == OMPC_DEPEND_unknown) {
1575 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1576 StopBeforeMatch);
1577 } else {
1578 ConsumeToken();
1579 // Special processing for depend(source) clause.
1580 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1581 // Parse ')'.
1582 T.consumeClose();
1583 return false;
1584 }
1585 }
1586 if (Tok.is(tok::colon))
1587 Data.ColonLoc = ConsumeToken();
1588 else {
1589 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1590 : diag::warn_pragma_expected_colon)
1591 << "dependency type";
1592 }
1593 } else if (Kind == OMPC_linear) {
1594 // Try to parse modifier if any.
1595 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1596 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1597 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1598 Data.DepLinMapLoc = ConsumeToken();
1599 LinearT.consumeOpen();
1600 NeedRParenForLinear = true;
1601 }
1602 } else if (Kind == OMPC_map) {
1603 // Handle map type for map clause.
1604 ColonProtectionRAIIObject ColonRAII(*this);
1605
1606 /// The map clause modifier token can be either a identifier or the C++
1607 /// delete keyword.
1608 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1609 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1610 };
1611
1612 // The first identifier may be a list item, a map-type or a
1613 // map-type-modifier. The map modifier can also be delete which has the same
1614 // spelling of the C++ delete keyword.
1615 Data.MapType =
1616 IsMapClauseModifierToken(Tok)
1617 ? static_cast<OpenMPMapClauseKind>(
1618 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1619 : OMPC_MAP_unknown;
1620 Data.DepLinMapLoc = Tok.getLocation();
1621 bool ColonExpected = false;
1622
1623 if (IsMapClauseModifierToken(Tok)) {
1624 if (PP.LookAhead(0).is(tok::colon)) {
1625 if (Data.MapType == OMPC_MAP_unknown)
1626 Diag(Tok, diag::err_omp_unknown_map_type);
1627 else if (Data.MapType == OMPC_MAP_always)
1628 Diag(Tok, diag::err_omp_map_type_missing);
1629 ConsumeToken();
1630 } else if (PP.LookAhead(0).is(tok::comma)) {
1631 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1632 PP.LookAhead(2).is(tok::colon)) {
1633 Data.MapTypeModifier = Data.MapType;
1634 if (Data.MapTypeModifier != OMPC_MAP_always) {
1635 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1636 Data.MapTypeModifier = OMPC_MAP_unknown;
1637 } else
1638 MapTypeModifierSpecified = true;
1639
1640 ConsumeToken();
1641 ConsumeToken();
1642
1643 Data.MapType =
1644 IsMapClauseModifierToken(Tok)
1645 ? static_cast<OpenMPMapClauseKind>(
1646 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1647 : OMPC_MAP_unknown;
1648 if (Data.MapType == OMPC_MAP_unknown ||
1649 Data.MapType == OMPC_MAP_always)
1650 Diag(Tok, diag::err_omp_unknown_map_type);
1651 ConsumeToken();
1652 } else {
1653 Data.MapType = OMPC_MAP_tofrom;
1654 Data.IsMapTypeImplicit = true;
1655 }
1656 } else {
1657 Data.MapType = OMPC_MAP_tofrom;
1658 Data.IsMapTypeImplicit = true;
1659 }
1660 } else {
1661 Data.MapType = OMPC_MAP_tofrom;
1662 Data.IsMapTypeImplicit = true;
1663 }
1664
1665 if (Tok.is(tok::colon))
1666 Data.ColonLoc = ConsumeToken();
1667 else if (ColonExpected)
1668 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1669 }
1670
1671 bool IsComma =
1672 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1673 (Kind == OMPC_reduction && !InvalidReductionId) ||
1674 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1675 (!MapTypeModifierSpecified ||
1676 Data.MapTypeModifier == OMPC_MAP_always)) ||
1677 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1678 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1679 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1680 Tok.isNot(tok::annot_pragma_openmp_end))) {
1681 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1682 // Parse variable
1683 ExprResult VarExpr =
1684 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1685 if (VarExpr.isUsable())
1686 Vars.push_back(VarExpr.get());
1687 else {
1688 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1689 StopBeforeMatch);
1690 }
1691 // Skip ',' if any
1692 IsComma = Tok.is(tok::comma);
1693 if (IsComma)
1694 ConsumeToken();
1695 else if (Tok.isNot(tok::r_paren) &&
1696 Tok.isNot(tok::annot_pragma_openmp_end) &&
1697 (!MayHaveTail || Tok.isNot(tok::colon)))
1698 Diag(Tok, diag::err_omp_expected_punc)
1699 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1700 : getOpenMPClauseName(Kind))
1701 << (Kind == OMPC_flush);
1702 }
1703
1704 // Parse ')' for linear clause with modifier.
1705 if (NeedRParenForLinear)
1706 LinearT.consumeClose();
1707
1708 // Parse ':' linear-step (or ':' alignment).
1709 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1710 if (MustHaveTail) {
1711 Data.ColonLoc = Tok.getLocation();
1712 SourceLocation ELoc = ConsumeToken();
1713 ExprResult Tail = ParseAssignmentExpression();
1714 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1715 if (Tail.isUsable())
1716 Data.TailExpr = Tail.get();
1717 else
1718 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1719 StopBeforeMatch);
1720 }
1721
1722 // Parse ')'.
1723 T.consumeClose();
1724 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1725 Vars.empty()) ||
1726 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1727 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1728 return true;
1729 return false;
1730}
1731
Alexander Musman1bb328c2014-06-04 13:06:39 +00001732/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001733/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001734///
1735/// private-clause:
1736/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001737/// firstprivate-clause:
1738/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001739/// lastprivate-clause:
1740/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001741/// shared-clause:
1742/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001743/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001744/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001745/// aligned-clause:
1746/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001747/// reduction-clause:
1748/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001749/// copyprivate-clause:
1750/// 'copyprivate' '(' list ')'
1751/// flush-clause:
1752/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001753/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001754/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001755/// map-clause:
1756/// 'map' '(' [ [ always , ]
1757/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001758/// to-clause:
1759/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001760/// from-clause:
1761/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001762/// use_device_ptr-clause:
1763/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001764/// is_device_ptr-clause:
1765/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001766///
Alexey Bataev182227b2015-08-20 10:54:39 +00001767/// For 'linear' clause linear-list may have the following forms:
1768/// list
1769/// modifier(list)
1770/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001771OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1772 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001773 SourceLocation Loc = Tok.getLocation();
1774 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001775 SmallVector<Expr *, 4> Vars;
1776 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001777
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001778 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001779 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001780
Alexey Bataevc5e02582014-06-16 07:08:35 +00001781 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001782 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1783 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1784 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1785 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001786}
1787