blob: ce1b3cd2ebeb61858b81e98c25c6d4a8ef198dde [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,
Kelvin Li579e41c2016-11-30 23:51:03 +000042 OMPD_distribute_parallel,
43 OMPD_teams_distribute_parallel,
44 OMPD_teams_distribute_parallel_for
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 },
118 { OMPD_teams_distribute_parallel_for, OMPD_simd, OMPD_teams_distribute_parallel_for_simd }
Dmitry Polukhin82478332016-02-13 06:53:38 +0000119 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000120 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +0000121 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000122 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000123 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000124 ? static_cast<unsigned>(OMPD_unknown)
125 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
126 if (DKind == OMPD_unknown)
127 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000128
Alexander Musmanf82886e2014-09-18 05:12:34 +0000129 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000130 if (DKind != F[i][0])
131 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000132
Dmitry Polukhin82478332016-02-13 06:53:38 +0000133 Tok = P.getPreprocessor().LookAhead(0);
134 unsigned SDKind =
135 Tok.isAnnotation()
136 ? static_cast<unsigned>(OMPD_unknown)
137 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
138 if (SDKind == OMPD_unknown)
139 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000140
Dmitry Polukhin82478332016-02-13 06:53:38 +0000141 if (SDKind == F[i][1]) {
142 P.ConsumeToken();
143 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000144 }
145 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000146 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
147 : OMPD_unknown;
148}
149
150static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000151 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000152 Sema &Actions = P.getActions();
153 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000154 // Allow to use 'operator' keyword for C++ operators
155 bool WithOperator = false;
156 if (Tok.is(tok::kw_operator)) {
157 P.ConsumeToken();
158 Tok = P.getCurToken();
159 WithOperator = true;
160 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000161 switch (Tok.getKind()) {
162 case tok::plus: // '+'
163 OOK = OO_Plus;
164 break;
165 case tok::minus: // '-'
166 OOK = OO_Minus;
167 break;
168 case tok::star: // '*'
169 OOK = OO_Star;
170 break;
171 case tok::amp: // '&'
172 OOK = OO_Amp;
173 break;
174 case tok::pipe: // '|'
175 OOK = OO_Pipe;
176 break;
177 case tok::caret: // '^'
178 OOK = OO_Caret;
179 break;
180 case tok::ampamp: // '&&'
181 OOK = OO_AmpAmp;
182 break;
183 case tok::pipepipe: // '||'
184 OOK = OO_PipePipe;
185 break;
186 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000187 if (!WithOperator)
188 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000189 default:
190 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
191 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
192 Parser::StopBeforeMatch);
193 return DeclarationName();
194 }
195 P.ConsumeToken();
196 auto &DeclNames = Actions.getASTContext().DeclarationNames;
197 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
198 : DeclNames.getCXXOperatorName(OOK);
199}
200
201/// \brief Parse 'omp declare reduction' construct.
202///
203/// declare-reduction-directive:
204/// annot_pragma_openmp 'declare' 'reduction'
205/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
206/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
207/// annot_pragma_openmp_end
208/// <reduction_id> is either a base language identifier or one of the following
209/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
210///
211Parser::DeclGroupPtrTy
212Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
213 // Parse '('.
214 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
215 if (T.expectAndConsume(diag::err_expected_lparen_after,
216 getOpenMPDirectiveName(OMPD_declare_reduction))) {
217 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
218 return DeclGroupPtrTy();
219 }
220
221 DeclarationName Name = parseOpenMPReductionId(*this);
222 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
223 return DeclGroupPtrTy();
224
225 // Consume ':'.
226 bool IsCorrect = !ExpectAndConsume(tok::colon);
227
228 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
229 return DeclGroupPtrTy();
230
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000231 IsCorrect = IsCorrect && !Name.isEmpty();
232
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000233 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
234 Diag(Tok.getLocation(), diag::err_expected_type);
235 IsCorrect = false;
236 }
237
238 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
239 return DeclGroupPtrTy();
240
241 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
242 // Parse list of types until ':' token.
243 do {
244 ColonProtectionRAIIObject ColonRAII(*this);
245 SourceRange Range;
246 TypeResult TR = ParseTypeName(&Range, Declarator::PrototypeContext, AS);
247 if (TR.isUsable()) {
248 auto ReductionType =
249 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
250 if (!ReductionType.isNull()) {
251 ReductionTypes.push_back(
252 std::make_pair(ReductionType, Range.getBegin()));
253 }
254 } else {
255 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
256 StopBeforeMatch);
257 }
258
259 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
260 break;
261
262 // Consume ','.
263 if (ExpectAndConsume(tok::comma)) {
264 IsCorrect = false;
265 if (Tok.is(tok::annot_pragma_openmp_end)) {
266 Diag(Tok.getLocation(), diag::err_expected_type);
267 return DeclGroupPtrTy();
268 }
269 }
270 } while (Tok.isNot(tok::annot_pragma_openmp_end));
271
272 if (ReductionTypes.empty()) {
273 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
274 return DeclGroupPtrTy();
275 }
276
277 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
278 return DeclGroupPtrTy();
279
280 // Consume ':'.
281 if (ExpectAndConsume(tok::colon))
282 IsCorrect = false;
283
284 if (Tok.is(tok::annot_pragma_openmp_end)) {
285 Diag(Tok.getLocation(), diag::err_expected_expression);
286 return DeclGroupPtrTy();
287 }
288
289 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
290 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
291
292 // Parse <combiner> expression and then parse initializer if any for each
293 // correct type.
294 unsigned I = 0, E = ReductionTypes.size();
295 for (auto *D : DRD.get()) {
296 TentativeParsingAction TPA(*this);
297 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
298 Scope::OpenMPDirectiveScope);
299 // Parse <combiner> expression.
300 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
301 ExprResult CombinerResult =
302 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
303 D->getLocation(), /*DiscardedValue=*/true);
304 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
305
306 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
307 Tok.isNot(tok::annot_pragma_openmp_end)) {
308 TPA.Commit();
309 IsCorrect = false;
310 break;
311 }
312 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
313 ExprResult InitializerResult;
314 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
315 // Parse <initializer> expression.
316 if (Tok.is(tok::identifier) &&
317 Tok.getIdentifierInfo()->isStr("initializer"))
318 ConsumeToken();
319 else {
320 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
321 TPA.Commit();
322 IsCorrect = false;
323 break;
324 }
325 // Parse '('.
326 BalancedDelimiterTracker T(*this, tok::l_paren,
327 tok::annot_pragma_openmp_end);
328 IsCorrect =
329 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
330 IsCorrect;
331 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
332 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
333 Scope::OpenMPDirectiveScope);
334 // Parse expression.
335 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), D);
336 InitializerResult = Actions.ActOnFinishFullExpr(
337 ParseAssignmentExpression().get(), D->getLocation(),
338 /*DiscardedValue=*/true);
339 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
340 D, InitializerResult.get());
341 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
342 Tok.isNot(tok::annot_pragma_openmp_end)) {
343 TPA.Commit();
344 IsCorrect = false;
345 break;
346 }
347 IsCorrect =
348 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
349 }
350 }
351
352 ++I;
353 // Revert parsing if not the last type, otherwise accept it, we're done with
354 // parsing.
355 if (I != E)
356 TPA.Revert();
357 else
358 TPA.Commit();
359 }
360 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
361 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000362}
363
Alexey Bataev2af33e32016-04-07 12:45:37 +0000364namespace {
365/// RAII that recreates function context for correct parsing of clauses of
366/// 'declare simd' construct.
367/// OpenMP, 2.8.2 declare simd Construct
368/// The expressions appearing in the clauses of this directive are evaluated in
369/// the scope of the arguments of the function declaration or definition.
370class FNContextRAII final {
371 Parser &P;
372 Sema::CXXThisScopeRAII *ThisScope;
373 Parser::ParseScope *TempScope;
374 Parser::ParseScope *FnScope;
375 bool HasTemplateScope = false;
376 bool HasFunScope = false;
377 FNContextRAII() = delete;
378 FNContextRAII(const FNContextRAII &) = delete;
379 FNContextRAII &operator=(const FNContextRAII &) = delete;
380
381public:
382 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
383 Decl *D = *Ptr.get().begin();
384 NamedDecl *ND = dyn_cast<NamedDecl>(D);
385 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
386 Sema &Actions = P.getActions();
387
388 // Allow 'this' within late-parsed attributes.
389 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
390 ND && ND->isCXXInstanceMember());
391
392 // If the Decl is templatized, add template parameters to scope.
393 HasTemplateScope = D->isTemplateDecl();
394 TempScope =
395 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
396 if (HasTemplateScope)
397 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
398
399 // If the Decl is on a function, add function parameters to the scope.
400 HasFunScope = D->isFunctionOrFunctionTemplate();
401 FnScope = new Parser::ParseScope(&P, Scope::FnScope | Scope::DeclScope,
402 HasFunScope);
403 if (HasFunScope)
404 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
405 }
406 ~FNContextRAII() {
407 if (HasFunScope) {
408 P.getActions().ActOnExitFunctionContext();
409 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
410 }
411 if (HasTemplateScope)
412 TempScope->Exit();
413 delete FnScope;
414 delete TempScope;
415 delete ThisScope;
416 }
417};
418} // namespace
419
Alexey Bataevd93d3762016-04-12 09:35:56 +0000420/// Parses clauses for 'declare simd' directive.
421/// clause:
422/// 'inbranch' | 'notinbranch'
423/// 'simdlen' '(' <expr> ')'
424/// { 'uniform' '(' <argument_list> ')' }
425/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000426/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
427static bool parseDeclareSimdClauses(
428 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
429 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
430 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
431 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000432 SourceRange BSRange;
433 const Token &Tok = P.getCurToken();
434 bool IsError = false;
435 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
436 if (Tok.isNot(tok::identifier))
437 break;
438 OMPDeclareSimdDeclAttr::BranchStateTy Out;
439 IdentifierInfo *II = Tok.getIdentifierInfo();
440 StringRef ClauseName = II->getName();
441 // Parse 'inranch|notinbranch' clauses.
442 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
443 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
444 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
445 << ClauseName
446 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
447 IsError = true;
448 }
449 BS = Out;
450 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
451 P.ConsumeToken();
452 } else if (ClauseName.equals("simdlen")) {
453 if (SimdLen.isUsable()) {
454 P.Diag(Tok, diag::err_omp_more_one_clause)
455 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
456 IsError = true;
457 }
458 P.ConsumeToken();
459 SourceLocation RLoc;
460 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
461 if (SimdLen.isInvalid())
462 IsError = true;
463 } else {
464 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000465 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
466 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000467 Parser::OpenMPVarListDataTy Data;
468 auto *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000469 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000470 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000471 else if (CKind == OMPC_linear)
472 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000473
474 P.ConsumeToken();
475 if (P.ParseOpenMPVarList(OMPD_declare_simd,
476 getOpenMPClauseKind(ClauseName), *Vars, Data))
477 IsError = true;
478 if (CKind == OMPC_aligned)
479 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000480 else if (CKind == OMPC_linear) {
481 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
482 Data.DepLinMapLoc))
483 Data.LinKind = OMPC_LINEAR_val;
484 LinModifiers.append(Linears.size() - LinModifiers.size(),
485 Data.LinKind);
486 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
487 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000488 } else
489 // TODO: add parsing of other clauses.
490 break;
491 }
492 // Skip ',' if any.
493 if (Tok.is(tok::comma))
494 P.ConsumeToken();
495 }
496 return IsError;
497}
498
Alexey Bataev2af33e32016-04-07 12:45:37 +0000499/// Parse clauses for '#pragma omp declare simd'.
500Parser::DeclGroupPtrTy
501Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
502 CachedTokens &Toks, SourceLocation Loc) {
503 PP.EnterToken(Tok);
504 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
505 // Consume the previously pushed token.
506 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
507
508 FNContextRAII FnContext(*this, Ptr);
509 OMPDeclareSimdDeclAttr::BranchStateTy BS =
510 OMPDeclareSimdDeclAttr::BS_Undefined;
511 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000512 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000513 SmallVector<Expr *, 4> Aligneds;
514 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000515 SmallVector<Expr *, 4> Linears;
516 SmallVector<unsigned, 4> LinModifiers;
517 SmallVector<Expr *, 4> Steps;
518 bool IsError =
519 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
520 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000521 // Need to check for extra tokens.
522 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
523 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
524 << getOpenMPDirectiveName(OMPD_declare_simd);
525 while (Tok.isNot(tok::annot_pragma_openmp_end))
526 ConsumeAnyToken();
527 }
528 // Skip the last annot_pragma_openmp_end.
529 SourceLocation EndLoc = ConsumeToken();
Alexey Bataevd93d3762016-04-12 09:35:56 +0000530 if (!IsError) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000531 return Actions.ActOnOpenMPDeclareSimdDirective(
Alexey Bataevecba70f2016-04-12 11:02:11 +0000532 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
533 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataevd93d3762016-04-12 09:35:56 +0000534 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000535 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000536}
537
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000538/// \brief Parsing of declarative OpenMP directives.
539///
540/// threadprivate-directive:
541/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000542/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000543///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000544/// declare-reduction-directive:
545/// annot_pragma_openmp 'declare' 'reduction' [...]
546/// annot_pragma_openmp_end
547///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000548/// declare-simd-directive:
549/// annot_pragma_openmp 'declare simd' {<clause> [,]}
550/// annot_pragma_openmp_end
551/// <function declaration/definition>
552///
553Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
554 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
555 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000556 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000557 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000558
559 SourceLocation Loc = ConsumeToken();
Alexey Bataev4acb8592014-07-07 13:01:15 +0000560 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000561
562 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000563 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000564 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000565 ThreadprivateListParserHelper Helper(this);
566 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000567 // The last seen token is annot_pragma_openmp_end - need to check for
568 // extra tokens.
569 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
570 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000571 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000572 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000573 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000574 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000575 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000576 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
577 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000578 }
579 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000580 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000581 case OMPD_declare_reduction:
582 ConsumeToken();
583 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
584 // The last seen token is annot_pragma_openmp_end - need to check for
585 // extra tokens.
586 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
587 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
588 << getOpenMPDirectiveName(OMPD_declare_reduction);
589 while (Tok.isNot(tok::annot_pragma_openmp_end))
590 ConsumeAnyToken();
591 }
592 // Skip the last annot_pragma_openmp_end.
593 ConsumeToken();
594 return Res;
595 }
596 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000597 case OMPD_declare_simd: {
598 // The syntax is:
599 // { #pragma omp declare simd }
600 // <function-declaration-or-definition>
601 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000602 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000603 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000604 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
605 Toks.push_back(Tok);
606 ConsumeAnyToken();
607 }
608 Toks.push_back(Tok);
609 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000610
611 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000612 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000613 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000614 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000615 // Here we expect to see some function declaration.
616 if (AS == AS_none) {
617 assert(TagType == DeclSpec::TST_unspecified);
618 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000619 ParsingDeclSpec PDS(*this);
620 Ptr = ParseExternalDeclaration(Attrs, &PDS);
621 } else {
622 Ptr =
623 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
624 }
625 }
626 if (!Ptr) {
627 Diag(Loc, diag::err_omp_decl_in_declare_simd);
628 return DeclGroupPtrTy();
629 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000630 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000631 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000632 case OMPD_declare_target: {
633 SourceLocation DTLoc = ConsumeAnyToken();
634 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000635 // OpenMP 4.5 syntax with list of entities.
636 llvm::SmallSetVector<const NamedDecl*, 16> SameDirectiveDecls;
637 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
638 OMPDeclareTargetDeclAttr::MapTypeTy MT =
639 OMPDeclareTargetDeclAttr::MT_To;
640 if (Tok.is(tok::identifier)) {
641 IdentifierInfo *II = Tok.getIdentifierInfo();
642 StringRef ClauseName = II->getName();
643 // Parse 'to|link' clauses.
644 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
645 MT)) {
646 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
647 << ClauseName;
648 break;
649 }
650 ConsumeToken();
651 }
652 auto Callback = [this, MT, &SameDirectiveDecls](
653 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
654 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
655 SameDirectiveDecls);
656 };
657 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, true))
658 break;
659
660 // Consume optional ','.
661 if (Tok.is(tok::comma))
662 ConsumeToken();
663 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000664 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000665 ConsumeAnyToken();
666 return DeclGroupPtrTy();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000667 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000668
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000669 // Skip the last annot_pragma_openmp_end.
670 ConsumeAnyToken();
671
672 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
673 return DeclGroupPtrTy();
674
675 DKind = ParseOpenMPDirectiveKind(*this);
676 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
677 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
678 ParsedAttributesWithRange attrs(AttrFactory);
679 MaybeParseCXX11Attributes(attrs);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000680 ParseExternalDeclaration(attrs);
681 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
682 TentativeParsingAction TPA(*this);
683 ConsumeToken();
684 DKind = ParseOpenMPDirectiveKind(*this);
685 if (DKind != OMPD_end_declare_target)
686 TPA.Revert();
687 else
688 TPA.Commit();
689 }
690 }
691
692 if (DKind == OMPD_end_declare_target) {
693 ConsumeAnyToken();
694 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
695 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
696 << getOpenMPDirectiveName(OMPD_end_declare_target);
697 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
698 }
699 // Skip the last annot_pragma_openmp_end.
700 ConsumeAnyToken();
701 } else {
702 Diag(Tok, diag::err_expected_end_declare_target);
703 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
704 }
705 Actions.ActOnFinishOpenMPDeclareTargetDirective();
706 return DeclGroupPtrTy();
707 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000708 case OMPD_unknown:
709 Diag(Tok, diag::err_omp_unknown_directive);
710 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000711 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000712 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000713 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000714 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000715 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000716 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000717 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000718 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000719 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000720 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000721 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000722 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000723 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000724 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000725 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000726 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000727 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000728 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000729 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000730 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000731 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000732 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000733 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000734 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000735 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000736 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000737 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000738 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000739 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000740 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000741 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000742 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000743 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000744 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000745 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000746 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000747 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000748 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000749 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000750 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000751 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000752 case OMPD_teams_distribute_parallel_for_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000753 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000754 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000755 break;
756 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000757 while (Tok.isNot(tok::annot_pragma_openmp_end))
758 ConsumeAnyToken();
759 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000760 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000761}
762
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000763/// \brief Parsing of declarative or executable OpenMP directives.
764///
765/// threadprivate-directive:
766/// annot_pragma_openmp 'threadprivate' simple-variable-list
767/// annot_pragma_openmp_end
768///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000769/// declare-reduction-directive:
770/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
771/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
772/// ('omp_priv' '=' <expression>|<function_call>) ')']
773/// annot_pragma_openmp_end
774///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000775/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000776/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000777/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
778/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000779/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000780/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000781/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000782/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000783/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000784/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000785/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000786/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000787/// 'teams distribute' | 'teams distribute simd' |
788/// 'teams distribute parallel for simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000789/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000790///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000791StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
792 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000793 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000794 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000795 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000796 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000797 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000798 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000799 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000800 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000801 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000802 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000803 // Name of critical directive.
804 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000805 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000806 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000807 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000808
809 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000810 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000811 if (Allowed != ACK_Any) {
812 Diag(Tok, diag::err_omp_immediate_directive)
813 << getOpenMPDirectiveName(DKind) << 0;
814 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000815 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000816 ThreadprivateListParserHelper Helper(this);
817 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000818 // The last seen token is annot_pragma_openmp_end - need to check for
819 // extra tokens.
820 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
821 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000822 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000823 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000824 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000825 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
826 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000827 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
828 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000829 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000830 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000831 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000832 case OMPD_declare_reduction:
833 ConsumeToken();
834 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
835 // The last seen token is annot_pragma_openmp_end - need to check for
836 // extra tokens.
837 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
838 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
839 << getOpenMPDirectiveName(OMPD_declare_reduction);
840 while (Tok.isNot(tok::annot_pragma_openmp_end))
841 ConsumeAnyToken();
842 }
843 ConsumeAnyToken();
844 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
845 } else
846 SkipUntil(tok::annot_pragma_openmp_end);
847 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000848 case OMPD_flush:
849 if (PP.LookAhead(0).is(tok::l_paren)) {
850 FlushHasClause = true;
851 // Push copy of the current token back to stream to properly parse
852 // pseudo-clause OMPFlushClause.
853 PP.EnterToken(Tok);
854 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000855 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000856 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000857 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000858 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000859 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000860 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000861 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000862 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000863 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000864 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000865 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000866 }
867 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000868 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000869 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000870 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000871 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000872 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000873 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000874 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000875 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000876 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000877 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000878 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000879 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000880 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000881 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000882 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000883 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000884 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000885 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000886 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000887 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000888 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000889 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000890 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000891 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000892 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +0000893 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000894 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000895 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000896 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000897 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +0000898 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +0000899 case OMPD_teams_distribute_simd:
900 case OMPD_teams_distribute_parallel_for_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000901 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000902 // Parse directive name of the 'critical' directive if any.
903 if (DKind == OMPD_critical) {
904 BalancedDelimiterTracker T(*this, tok::l_paren,
905 tok::annot_pragma_openmp_end);
906 if (!T.consumeOpen()) {
907 if (Tok.isAnyIdentifier()) {
908 DirName =
909 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
910 ConsumeAnyToken();
911 } else {
912 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
913 }
914 T.consumeClose();
915 }
Alexey Bataev80909872015-07-02 11:25:17 +0000916 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000917 CancelRegion = ParseOpenMPDirectiveKind(*this);
918 if (Tok.isNot(tok::annot_pragma_openmp_end))
919 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000920 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000921
Alexey Bataevf29276e2014-06-18 04:14:57 +0000922 if (isOpenMPLoopDirective(DKind))
923 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
924 if (isOpenMPSimdDirective(DKind))
925 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
926 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000927 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000928
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000929 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000930 OpenMPClauseKind CKind =
931 Tok.isAnnotation()
932 ? OMPC_unknown
933 : FlushHasClause ? OMPC_flush
934 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000935 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000936 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000937 OMPClause *Clause =
938 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000939 FirstClauses[CKind].setInt(true);
940 if (Clause) {
941 FirstClauses[CKind].setPointer(Clause);
942 Clauses.push_back(Clause);
943 }
944
945 // Skip ',' if any.
946 if (Tok.is(tok::comma))
947 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000948 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000949 }
950 // End location of the directive.
951 EndLoc = Tok.getLocation();
952 // Consume final annot_pragma_openmp_end.
953 ConsumeToken();
954
Alexey Bataeveb482352015-12-18 05:05:56 +0000955 // OpenMP [2.13.8, ordered Construct, Syntax]
956 // If the depend clause is specified, the ordered construct is a stand-alone
957 // directive.
958 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000959 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000960 Diag(Loc, diag::err_omp_immediate_directive)
961 << getOpenMPDirectiveName(DKind) << 1
962 << getOpenMPClauseName(OMPC_depend);
963 }
964 HasAssociatedStatement = false;
965 }
966
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000967 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000968 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000969 // The body is a block scope like in Lambdas and Blocks.
970 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000971 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000972 Actions.ActOnStartOfCompoundStmt();
973 // Parse statement
974 AssociatedStmt = ParseStatement();
975 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000976 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000977 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000978 Directive = Actions.ActOnOpenMPExecutableDirective(
979 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
980 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000981
982 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000983 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000984 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000985 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000986 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000987 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000988 case OMPD_declare_target:
989 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +0000990 Diag(Tok, diag::err_omp_unexpected_directive)
991 << getOpenMPDirectiveName(DKind);
992 SkipUntil(tok::annot_pragma_openmp_end);
993 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000994 case OMPD_unknown:
995 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000996 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000997 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000998 }
999 return Directive;
1000}
1001
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001002// Parses simple list:
1003// simple-variable-list:
1004// '(' id-expression {, id-expression} ')'
1005//
1006bool Parser::ParseOpenMPSimpleVarList(
1007 OpenMPDirectiveKind Kind,
1008 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1009 Callback,
1010 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001011 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001012 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001013 if (T.expectAndConsume(diag::err_expected_lparen_after,
1014 getOpenMPDirectiveName(Kind)))
1015 return true;
1016 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001017 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001018
1019 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001020 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001021 CXXScopeSpec SS;
1022 SourceLocation TemplateKWLoc;
1023 UnqualifiedId Name;
1024 // Read var name.
1025 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001026 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001027
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001028 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001029 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001030 IsCorrect = false;
1031 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001032 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +00001033 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001034 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001035 IsCorrect = false;
1036 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001037 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001038 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1039 Tok.isNot(tok::annot_pragma_openmp_end)) {
1040 IsCorrect = false;
1041 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001042 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001043 Diag(PrevTok.getLocation(), diag::err_expected)
1044 << tok::identifier
1045 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001046 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001047 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001048 }
1049 // Consume ','.
1050 if (Tok.is(tok::comma)) {
1051 ConsumeToken();
1052 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001053 }
1054
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001055 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001056 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001057 IsCorrect = false;
1058 }
1059
1060 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001061 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001062
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001063 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001064}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001065
1066/// \brief Parsing of OpenMP clauses.
1067///
1068/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001069/// if-clause | final-clause | num_threads-clause | safelen-clause |
1070/// default-clause | private-clause | firstprivate-clause | shared-clause
1071/// | linear-clause | aligned-clause | collapse-clause |
1072/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001073/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001074/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001075/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001076/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001077/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001078/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Carlo Bertolli70594e92016-07-13 17:16:49 +00001079/// from-clause | is_device_ptr-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001080///
1081OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1082 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001083 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001084 bool ErrorFound = false;
1085 // Check if clause is allowed for the given directive.
1086 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001087 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1088 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001089 ErrorFound = true;
1090 }
1091
1092 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001093 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001094 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001095 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001096 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001097 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001098 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001099 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001100 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001101 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001102 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001103 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001104 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001105 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001106 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001107 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001108 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001109 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001110 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001111 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001112 // OpenMP [2.9.1, target data construct, Restrictions]
1113 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001114 // OpenMP [2.11.1, task Construct, Restrictions]
1115 // At most one if clause can appear on the directive.
1116 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001117 // OpenMP [teams Construct, Restrictions]
1118 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001119 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001120 // OpenMP [2.9.1, task Construct, Restrictions]
1121 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001122 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1123 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001124 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1125 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001126 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001127 Diag(Tok, diag::err_omp_more_one_clause)
1128 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001129 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001130 }
1131
Alexey Bataev10e775f2015-07-30 11:36:16 +00001132 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1133 Clause = ParseOpenMPClause(CKind);
1134 else
1135 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001136 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001137 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001138 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001139 // OpenMP [2.14.3.1, Restrictions]
1140 // Only a single default clause may be specified on a parallel, task or
1141 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001142 // OpenMP [2.5, parallel Construct, Restrictions]
1143 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001144 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001145 Diag(Tok, diag::err_omp_more_one_clause)
1146 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001147 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001148 }
1149
1150 Clause = ParseOpenMPSimpleClause(CKind);
1151 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001152 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001153 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001154 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001155 // OpenMP [2.7.1, Restrictions, p. 3]
1156 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001157 // OpenMP [2.10.4, Restrictions, p. 106]
1158 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001159 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001160 Diag(Tok, diag::err_omp_more_one_clause)
1161 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001162 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001163 }
1164
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001165 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001166 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1167 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001168 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001169 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001170 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001171 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001172 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001173 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001174 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001175 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001176 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001177 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001178 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001179 // OpenMP [2.7.1, Restrictions, p. 9]
1180 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001181 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1182 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001183 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001184 Diag(Tok, diag::err_omp_more_one_clause)
1185 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001186 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001187 }
1188
1189 Clause = ParseOpenMPClause(CKind);
1190 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001191 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001192 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001193 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001194 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001195 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001196 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001197 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001198 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001199 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001200 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001201 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001202 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001203 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001204 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001205 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001206 case OMPC_is_device_ptr:
Alexey Bataeveb482352015-12-18 05:05:56 +00001207 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001208 break;
1209 case OMPC_unknown:
1210 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001211 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001212 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001213 break;
1214 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001215 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001216 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1217 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001218 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001219 break;
1220 }
Craig Topper161e4db2014-05-21 06:02:52 +00001221 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001222}
1223
Alexey Bataev2af33e32016-04-07 12:45:37 +00001224/// Parses simple expression in parens for single-expression clauses of OpenMP
1225/// constructs.
1226/// \param RLoc Returned location of right paren.
1227ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1228 SourceLocation &RLoc) {
1229 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1230 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1231 return ExprError();
1232
1233 SourceLocation ELoc = Tok.getLocation();
1234 ExprResult LHS(ParseCastExpression(
1235 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1236 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1237 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1238
1239 // Parse ')'.
1240 T.consumeClose();
1241
1242 RLoc = T.getCloseLocation();
1243 return Val;
1244}
1245
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001246/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001247/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001248/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001249///
Alexey Bataev3778b602014-07-17 07:32:53 +00001250/// final-clause:
1251/// 'final' '(' expression ')'
1252///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001253/// num_threads-clause:
1254/// 'num_threads' '(' expression ')'
1255///
1256/// safelen-clause:
1257/// 'safelen' '(' expression ')'
1258///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001259/// simdlen-clause:
1260/// 'simdlen' '(' expression ')'
1261///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001262/// collapse-clause:
1263/// 'collapse' '(' expression ')'
1264///
Alexey Bataeva0569352015-12-01 10:17:31 +00001265/// priority-clause:
1266/// 'priority' '(' expression ')'
1267///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001268/// grainsize-clause:
1269/// 'grainsize' '(' expression ')'
1270///
Alexey Bataev382967a2015-12-08 12:06:20 +00001271/// num_tasks-clause:
1272/// 'num_tasks' '(' expression ')'
1273///
Alexey Bataev28c75412015-12-15 08:19:24 +00001274/// hint-clause:
1275/// 'hint' '(' expression ')'
1276///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001277OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1278 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001279 SourceLocation LLoc = Tok.getLocation();
1280 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001281
Alexey Bataev2af33e32016-04-07 12:45:37 +00001282 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001283
1284 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001285 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001286
Alexey Bataev2af33e32016-04-07 12:45:37 +00001287 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001288}
1289
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001290/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001291///
1292/// default-clause:
1293/// 'default' '(' 'none' | 'shared' ')
1294///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001295/// proc_bind-clause:
1296/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1297///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001298OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1299 SourceLocation Loc = Tok.getLocation();
1300 SourceLocation LOpen = ConsumeToken();
1301 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001302 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001303 if (T.expectAndConsume(diag::err_expected_lparen_after,
1304 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001305 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001306
Alexey Bataeva55ed262014-05-28 06:15:33 +00001307 unsigned Type = getOpenMPSimpleClauseType(
1308 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001309 SourceLocation TypeLoc = Tok.getLocation();
1310 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1311 Tok.isNot(tok::annot_pragma_openmp_end))
1312 ConsumeAnyToken();
1313
1314 // Parse ')'.
1315 T.consumeClose();
1316
1317 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1318 Tok.getLocation());
1319}
1320
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001321/// \brief Parsing of OpenMP clauses like 'ordered'.
1322///
1323/// ordered-clause:
1324/// 'ordered'
1325///
Alexey Bataev236070f2014-06-20 11:19:47 +00001326/// nowait-clause:
1327/// 'nowait'
1328///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001329/// untied-clause:
1330/// 'untied'
1331///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001332/// mergeable-clause:
1333/// 'mergeable'
1334///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001335/// read-clause:
1336/// 'read'
1337///
Alexey Bataev346265e2015-09-25 10:37:12 +00001338/// threads-clause:
1339/// 'threads'
1340///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001341/// simd-clause:
1342/// 'simd'
1343///
Alexey Bataevb825de12015-12-07 10:51:44 +00001344/// nogroup-clause:
1345/// 'nogroup'
1346///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001347OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1348 SourceLocation Loc = Tok.getLocation();
1349 ConsumeAnyToken();
1350
1351 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1352}
1353
1354
Alexey Bataev56dafe82014-06-20 07:16:17 +00001355/// \brief Parsing of OpenMP clauses with single expressions and some additional
1356/// argument like 'schedule' or 'dist_schedule'.
1357///
1358/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001359/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1360/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001361///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001362/// if-clause:
1363/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1364///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001365/// defaultmap:
1366/// 'defaultmap' '(' modifier ':' kind ')'
1367///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001368OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1369 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001370 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001371 // Parse '('.
1372 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1373 if (T.expectAndConsume(diag::err_expected_lparen_after,
1374 getOpenMPClauseName(Kind)))
1375 return nullptr;
1376
1377 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001378 SmallVector<unsigned, 4> Arg;
1379 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001380 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001381 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1382 Arg.resize(NumberOfElements);
1383 KLoc.resize(NumberOfElements);
1384 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1385 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1386 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1387 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001388 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001389 if (KindModifier > OMPC_SCHEDULE_unknown) {
1390 // Parse 'modifier'
1391 Arg[Modifier1] = KindModifier;
1392 KLoc[Modifier1] = Tok.getLocation();
1393 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1394 Tok.isNot(tok::annot_pragma_openmp_end))
1395 ConsumeAnyToken();
1396 if (Tok.is(tok::comma)) {
1397 // Parse ',' 'modifier'
1398 ConsumeAnyToken();
1399 KindModifier = getOpenMPSimpleClauseType(
1400 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1401 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1402 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001403 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001404 KLoc[Modifier2] = Tok.getLocation();
1405 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1406 Tok.isNot(tok::annot_pragma_openmp_end))
1407 ConsumeAnyToken();
1408 }
1409 // Parse ':'
1410 if (Tok.is(tok::colon))
1411 ConsumeAnyToken();
1412 else
1413 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1414 KindModifier = getOpenMPSimpleClauseType(
1415 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1416 }
1417 Arg[ScheduleKind] = KindModifier;
1418 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001419 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1420 Tok.isNot(tok::annot_pragma_openmp_end))
1421 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001422 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1423 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1424 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001425 Tok.is(tok::comma))
1426 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001427 } else if (Kind == OMPC_dist_schedule) {
1428 Arg.push_back(getOpenMPSimpleClauseType(
1429 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1430 KLoc.push_back(Tok.getLocation());
1431 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1432 Tok.isNot(tok::annot_pragma_openmp_end))
1433 ConsumeAnyToken();
1434 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1435 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001436 } else if (Kind == OMPC_defaultmap) {
1437 // Get a defaultmap modifier
1438 Arg.push_back(getOpenMPSimpleClauseType(
1439 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1440 KLoc.push_back(Tok.getLocation());
1441 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1442 Tok.isNot(tok::annot_pragma_openmp_end))
1443 ConsumeAnyToken();
1444 // Parse ':'
1445 if (Tok.is(tok::colon))
1446 ConsumeAnyToken();
1447 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1448 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1449 // Get a defaultmap kind
1450 Arg.push_back(getOpenMPSimpleClauseType(
1451 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1452 KLoc.push_back(Tok.getLocation());
1453 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1454 Tok.isNot(tok::annot_pragma_openmp_end))
1455 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001456 } else {
1457 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001458 KLoc.push_back(Tok.getLocation());
1459 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1460 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001461 ConsumeToken();
1462 if (Tok.is(tok::colon))
1463 DelimLoc = ConsumeToken();
1464 else
1465 Diag(Tok, diag::warn_pragma_expected_colon)
1466 << "directive name modifier";
1467 }
1468 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001469
Carlo Bertollib4adf552016-01-15 18:50:31 +00001470 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1471 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1472 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001473 if (NeedAnExpression) {
1474 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001475 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1476 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001477 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001478 }
1479
1480 // Parse ')'.
1481 T.consumeClose();
1482
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001483 if (NeedAnExpression && Val.isInvalid())
1484 return nullptr;
1485
Alexey Bataev56dafe82014-06-20 07:16:17 +00001486 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001487 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001488 T.getCloseLocation());
1489}
1490
Alexey Bataevc5e02582014-06-16 07:08:35 +00001491static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1492 UnqualifiedId &ReductionId) {
1493 SourceLocation TemplateKWLoc;
1494 if (ReductionIdScopeSpec.isEmpty()) {
1495 auto OOK = OO_None;
1496 switch (P.getCurToken().getKind()) {
1497 case tok::plus:
1498 OOK = OO_Plus;
1499 break;
1500 case tok::minus:
1501 OOK = OO_Minus;
1502 break;
1503 case tok::star:
1504 OOK = OO_Star;
1505 break;
1506 case tok::amp:
1507 OOK = OO_Amp;
1508 break;
1509 case tok::pipe:
1510 OOK = OO_Pipe;
1511 break;
1512 case tok::caret:
1513 OOK = OO_Caret;
1514 break;
1515 case tok::ampamp:
1516 OOK = OO_AmpAmp;
1517 break;
1518 case tok::pipepipe:
1519 OOK = OO_PipePipe;
1520 break;
1521 default:
1522 break;
1523 }
1524 if (OOK != OO_None) {
1525 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001526 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001527 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1528 return false;
1529 }
1530 }
1531 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1532 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001533 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001534 TemplateKWLoc, ReductionId);
1535}
1536
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001537/// Parses clauses with list.
1538bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1539 OpenMPClauseKind Kind,
1540 SmallVectorImpl<Expr *> &Vars,
1541 OpenMPVarListDataTy &Data) {
1542 UnqualifiedId UnqualifiedReductionId;
1543 bool InvalidReductionId = false;
1544 bool MapTypeModifierSpecified = false;
1545
1546 // Parse '('.
1547 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1548 if (T.expectAndConsume(diag::err_expected_lparen_after,
1549 getOpenMPClauseName(Kind)))
1550 return true;
1551
1552 bool NeedRParenForLinear = false;
1553 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1554 tok::annot_pragma_openmp_end);
1555 // Handle reduction-identifier for reduction clause.
1556 if (Kind == OMPC_reduction) {
1557 ColonProtectionRAIIObject ColonRAII(*this);
1558 if (getLangOpts().CPlusPlus)
1559 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1560 /*ObjectType=*/nullptr,
1561 /*EnteringContext=*/false);
1562 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1563 UnqualifiedReductionId);
1564 if (InvalidReductionId) {
1565 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1566 StopBeforeMatch);
1567 }
1568 if (Tok.is(tok::colon))
1569 Data.ColonLoc = ConsumeToken();
1570 else
1571 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1572 if (!InvalidReductionId)
1573 Data.ReductionId =
1574 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1575 } else if (Kind == OMPC_depend) {
1576 // Handle dependency type for depend clause.
1577 ColonProtectionRAIIObject ColonRAII(*this);
1578 Data.DepKind =
1579 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1580 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1581 Data.DepLinMapLoc = Tok.getLocation();
1582
1583 if (Data.DepKind == OMPC_DEPEND_unknown) {
1584 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1585 StopBeforeMatch);
1586 } else {
1587 ConsumeToken();
1588 // Special processing for depend(source) clause.
1589 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1590 // Parse ')'.
1591 T.consumeClose();
1592 return false;
1593 }
1594 }
1595 if (Tok.is(tok::colon))
1596 Data.ColonLoc = ConsumeToken();
1597 else {
1598 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1599 : diag::warn_pragma_expected_colon)
1600 << "dependency type";
1601 }
1602 } else if (Kind == OMPC_linear) {
1603 // Try to parse modifier if any.
1604 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1605 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1606 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1607 Data.DepLinMapLoc = ConsumeToken();
1608 LinearT.consumeOpen();
1609 NeedRParenForLinear = true;
1610 }
1611 } else if (Kind == OMPC_map) {
1612 // Handle map type for map clause.
1613 ColonProtectionRAIIObject ColonRAII(*this);
1614
1615 /// The map clause modifier token can be either a identifier or the C++
1616 /// delete keyword.
1617 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1618 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1619 };
1620
1621 // The first identifier may be a list item, a map-type or a
1622 // map-type-modifier. The map modifier can also be delete which has the same
1623 // spelling of the C++ delete keyword.
1624 Data.MapType =
1625 IsMapClauseModifierToken(Tok)
1626 ? static_cast<OpenMPMapClauseKind>(
1627 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1628 : OMPC_MAP_unknown;
1629 Data.DepLinMapLoc = Tok.getLocation();
1630 bool ColonExpected = false;
1631
1632 if (IsMapClauseModifierToken(Tok)) {
1633 if (PP.LookAhead(0).is(tok::colon)) {
1634 if (Data.MapType == OMPC_MAP_unknown)
1635 Diag(Tok, diag::err_omp_unknown_map_type);
1636 else if (Data.MapType == OMPC_MAP_always)
1637 Diag(Tok, diag::err_omp_map_type_missing);
1638 ConsumeToken();
1639 } else if (PP.LookAhead(0).is(tok::comma)) {
1640 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1641 PP.LookAhead(2).is(tok::colon)) {
1642 Data.MapTypeModifier = Data.MapType;
1643 if (Data.MapTypeModifier != OMPC_MAP_always) {
1644 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1645 Data.MapTypeModifier = OMPC_MAP_unknown;
1646 } else
1647 MapTypeModifierSpecified = true;
1648
1649 ConsumeToken();
1650 ConsumeToken();
1651
1652 Data.MapType =
1653 IsMapClauseModifierToken(Tok)
1654 ? static_cast<OpenMPMapClauseKind>(
1655 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1656 : OMPC_MAP_unknown;
1657 if (Data.MapType == OMPC_MAP_unknown ||
1658 Data.MapType == OMPC_MAP_always)
1659 Diag(Tok, diag::err_omp_unknown_map_type);
1660 ConsumeToken();
1661 } else {
1662 Data.MapType = OMPC_MAP_tofrom;
1663 Data.IsMapTypeImplicit = true;
1664 }
1665 } else {
1666 Data.MapType = OMPC_MAP_tofrom;
1667 Data.IsMapTypeImplicit = true;
1668 }
1669 } else {
1670 Data.MapType = OMPC_MAP_tofrom;
1671 Data.IsMapTypeImplicit = true;
1672 }
1673
1674 if (Tok.is(tok::colon))
1675 Data.ColonLoc = ConsumeToken();
1676 else if (ColonExpected)
1677 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1678 }
1679
1680 bool IsComma =
1681 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1682 (Kind == OMPC_reduction && !InvalidReductionId) ||
1683 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1684 (!MapTypeModifierSpecified ||
1685 Data.MapTypeModifier == OMPC_MAP_always)) ||
1686 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1687 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1688 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1689 Tok.isNot(tok::annot_pragma_openmp_end))) {
1690 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1691 // Parse variable
1692 ExprResult VarExpr =
1693 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1694 if (VarExpr.isUsable())
1695 Vars.push_back(VarExpr.get());
1696 else {
1697 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1698 StopBeforeMatch);
1699 }
1700 // Skip ',' if any
1701 IsComma = Tok.is(tok::comma);
1702 if (IsComma)
1703 ConsumeToken();
1704 else if (Tok.isNot(tok::r_paren) &&
1705 Tok.isNot(tok::annot_pragma_openmp_end) &&
1706 (!MayHaveTail || Tok.isNot(tok::colon)))
1707 Diag(Tok, diag::err_omp_expected_punc)
1708 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1709 : getOpenMPClauseName(Kind))
1710 << (Kind == OMPC_flush);
1711 }
1712
1713 // Parse ')' for linear clause with modifier.
1714 if (NeedRParenForLinear)
1715 LinearT.consumeClose();
1716
1717 // Parse ':' linear-step (or ':' alignment).
1718 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1719 if (MustHaveTail) {
1720 Data.ColonLoc = Tok.getLocation();
1721 SourceLocation ELoc = ConsumeToken();
1722 ExprResult Tail = ParseAssignmentExpression();
1723 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1724 if (Tail.isUsable())
1725 Data.TailExpr = Tail.get();
1726 else
1727 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1728 StopBeforeMatch);
1729 }
1730
1731 // Parse ')'.
1732 T.consumeClose();
1733 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1734 Vars.empty()) ||
1735 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1736 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1737 return true;
1738 return false;
1739}
1740
Alexander Musman1bb328c2014-06-04 13:06:39 +00001741/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001742/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001743///
1744/// private-clause:
1745/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001746/// firstprivate-clause:
1747/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001748/// lastprivate-clause:
1749/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001750/// shared-clause:
1751/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001752/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001753/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001754/// aligned-clause:
1755/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001756/// reduction-clause:
1757/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001758/// copyprivate-clause:
1759/// 'copyprivate' '(' list ')'
1760/// flush-clause:
1761/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001762/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001763/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001764/// map-clause:
1765/// 'map' '(' [ [ always , ]
1766/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001767/// to-clause:
1768/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001769/// from-clause:
1770/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001771/// use_device_ptr-clause:
1772/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001773/// is_device_ptr-clause:
1774/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001775///
Alexey Bataev182227b2015-08-20 10:54:39 +00001776/// For 'linear' clause linear-list may have the following forms:
1777/// list
1778/// modifier(list)
1779/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001780OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1781 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001782 SourceLocation Loc = Tok.getLocation();
1783 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001784 SmallVector<Expr *, 4> Vars;
1785 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001786
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001787 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001788 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001789
Alexey Bataevc5e02582014-06-16 07:08:35 +00001790 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001791 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1792 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1793 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1794 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001795}
1796