blob: 59d755a7291157be8d8aa3932cbe7ad1f63773a2 [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,
Kelvin Li7ade93f2016-12-09 03:24:30 +000043 OMPD_teams_distribute_parallel
Dmitry Polukhin82478332016-02-13 06:53:38 +000044};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000045
46class ThreadprivateListParserHelper final {
47 SmallVector<Expr *, 4> Identifiers;
48 Parser *P;
49
50public:
51 ThreadprivateListParserHelper(Parser *P) : P(P) {}
52 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
53 ExprResult Res =
54 P->getActions().ActOnOpenMPIdExpression(P->getCurScope(), SS, NameInfo);
55 if (Res.isUsable())
56 Identifiers.push_back(Res.get());
57 }
58 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
59};
Dmitry Polukhin82478332016-02-13 06:53:38 +000060} // namespace
61
62// Map token string to extended OMP token kind that are
63// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
64static unsigned getOpenMPDirectiveKindEx(StringRef S) {
65 auto DKind = getOpenMPDirectiveKind(S);
66 if (DKind != OMPD_unknown)
67 return DKind;
68
69 return llvm::StringSwitch<unsigned>(S)
70 .Case("cancellation", OMPD_cancellation)
71 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000072 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000073 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000074 .Case("enter", OMPD_enter)
75 .Case("exit", OMPD_exit)
76 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000077 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000078 .Case("update", OMPD_update)
Dmitry Polukhin82478332016-02-13 06:53:38 +000079 .Default(OMPD_unknown);
80}
81
Alexey Bataev4acb8592014-07-07 13:01:15 +000082static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000083 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
84 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
85 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000086 static const unsigned F[][3] = {
87 { OMPD_cancellation, OMPD_point, OMPD_cancellation_point },
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000088 { OMPD_declare, OMPD_reduction, OMPD_declare_reduction },
Alexey Bataev587e1de2016-03-30 10:43:55 +000089 { OMPD_declare, OMPD_simd, OMPD_declare_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000090 { OMPD_declare, OMPD_target, OMPD_declare_target },
Carlo Bertolli9925f152016-06-27 14:55:37 +000091 { OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel },
92 { OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for },
Kelvin Li4a39add2016-07-05 05:00:15 +000093 { OMPD_distribute_parallel_for, OMPD_simd,
94 OMPD_distribute_parallel_for_simd },
Kelvin Li787f3fc2016-07-06 04:45:38 +000095 { OMPD_distribute, OMPD_simd, OMPD_distribute_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000096 { OMPD_end, OMPD_declare, OMPD_end_declare },
97 { OMPD_end_declare, OMPD_target, OMPD_end_declare_target },
Dmitry Polukhin82478332016-02-13 06:53:38 +000098 { OMPD_target, OMPD_data, OMPD_target_data },
99 { OMPD_target, OMPD_enter, OMPD_target_enter },
100 { OMPD_target, OMPD_exit, OMPD_target_exit },
Samuel Antao686c70c2016-05-26 17:30:50 +0000101 { OMPD_target, OMPD_update, OMPD_target_update },
Dmitry Polukhin82478332016-02-13 06:53:38 +0000102 { OMPD_target_enter, OMPD_data, OMPD_target_enter_data },
103 { OMPD_target_exit, OMPD_data, OMPD_target_exit_data },
104 { OMPD_for, OMPD_simd, OMPD_for_simd },
105 { OMPD_parallel, OMPD_for, OMPD_parallel_for },
106 { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
107 { OMPD_parallel, OMPD_sections, OMPD_parallel_sections },
108 { OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd },
109 { OMPD_target, OMPD_parallel, OMPD_target_parallel },
Kelvin Li986330c2016-07-20 22:57:10 +0000110 { OMPD_target, OMPD_simd, OMPD_target_simd },
Kelvin Lia579b912016-07-14 02:54:56 +0000111 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for },
Kelvin Li02532872016-08-05 14:37:37 +0000112 { OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd },
Kelvin Li4e325f72016-10-25 12:50:55 +0000113 { OMPD_teams, OMPD_distribute, OMPD_teams_distribute },
Kelvin Li579e41c2016-11-30 23:51:03 +0000114 { OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd },
115 { OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel },
116 { OMPD_teams_distribute_parallel, OMPD_for, OMPD_teams_distribute_parallel_for },
Kelvin Libf594a52016-12-17 05:48:59 +0000117 { OMPD_teams_distribute_parallel_for, OMPD_simd, OMPD_teams_distribute_parallel_for_simd },
118 { OMPD_target, OMPD_teams, OMPD_target_teams }
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:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000753 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +0000754 case OMPD_target_teams:
Alexey Bataeva769e072013-03-22 06:34:35 +0000755 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000756 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000757 break;
758 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000759 while (Tok.isNot(tok::annot_pragma_openmp_end))
760 ConsumeAnyToken();
761 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000762 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000763}
764
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000765/// \brief Parsing of declarative or executable OpenMP directives.
766///
767/// threadprivate-directive:
768/// annot_pragma_openmp 'threadprivate' simple-variable-list
769/// annot_pragma_openmp_end
770///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000771/// declare-reduction-directive:
772/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
773/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
774/// ('omp_priv' '=' <expression>|<function_call>) ')']
775/// annot_pragma_openmp_end
776///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000777/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000778/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000779/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
780/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000781/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000782/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000783/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000784/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000785/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000786/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000787/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000788/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000789/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000790/// 'teams distribute parallel for simd' |
Kelvin Libf594a52016-12-17 05:48:59 +0000791/// 'teams distribute parallel for' | 'target teams' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000792/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000793///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000794StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
795 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000796 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000797 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000798 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000799 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000800 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000801 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000802 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000803 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000804 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000805 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000806 // Name of critical directive.
807 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000808 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000809 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000810 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000811
812 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000813 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000814 if (Allowed != ACK_Any) {
815 Diag(Tok, diag::err_omp_immediate_directive)
816 << getOpenMPDirectiveName(DKind) << 0;
817 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000818 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000819 ThreadprivateListParserHelper Helper(this);
820 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000821 // The last seen token is annot_pragma_openmp_end - need to check for
822 // extra tokens.
823 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
824 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000825 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000826 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000827 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000828 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
829 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000830 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
831 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000832 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000833 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000834 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000835 case OMPD_declare_reduction:
836 ConsumeToken();
837 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
838 // The last seen token is annot_pragma_openmp_end - need to check for
839 // extra tokens.
840 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
841 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
842 << getOpenMPDirectiveName(OMPD_declare_reduction);
843 while (Tok.isNot(tok::annot_pragma_openmp_end))
844 ConsumeAnyToken();
845 }
846 ConsumeAnyToken();
847 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
848 } else
849 SkipUntil(tok::annot_pragma_openmp_end);
850 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000851 case OMPD_flush:
852 if (PP.LookAhead(0).is(tok::l_paren)) {
853 FlushHasClause = true;
854 // Push copy of the current token back to stream to properly parse
855 // pseudo-clause OMPFlushClause.
856 PP.EnterToken(Tok);
857 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000858 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000859 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000860 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000861 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000862 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000863 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000864 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000865 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000866 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000867 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000868 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000869 }
870 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000871 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000872 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000873 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000874 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000875 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000876 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000877 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000878 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000879 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000880 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000881 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000882 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000883 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000884 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000885 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000886 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000887 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000888 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000889 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000890 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000891 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000892 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000893 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000894 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000895 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +0000896 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000897 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000898 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000899 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000900 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +0000901 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +0000902 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000903 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +0000904 case OMPD_teams_distribute_parallel_for:
905 case OMPD_target_teams: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000906 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000907 // Parse directive name of the 'critical' directive if any.
908 if (DKind == OMPD_critical) {
909 BalancedDelimiterTracker T(*this, tok::l_paren,
910 tok::annot_pragma_openmp_end);
911 if (!T.consumeOpen()) {
912 if (Tok.isAnyIdentifier()) {
913 DirName =
914 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
915 ConsumeAnyToken();
916 } else {
917 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
918 }
919 T.consumeClose();
920 }
Alexey Bataev80909872015-07-02 11:25:17 +0000921 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000922 CancelRegion = ParseOpenMPDirectiveKind(*this);
923 if (Tok.isNot(tok::annot_pragma_openmp_end))
924 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000925 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000926
Alexey Bataevf29276e2014-06-18 04:14:57 +0000927 if (isOpenMPLoopDirective(DKind))
928 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
929 if (isOpenMPSimdDirective(DKind))
930 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
931 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000932 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000933
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000934 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000935 OpenMPClauseKind CKind =
936 Tok.isAnnotation()
937 ? OMPC_unknown
938 : FlushHasClause ? OMPC_flush
939 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000940 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000941 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000942 OMPClause *Clause =
943 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000944 FirstClauses[CKind].setInt(true);
945 if (Clause) {
946 FirstClauses[CKind].setPointer(Clause);
947 Clauses.push_back(Clause);
948 }
949
950 // Skip ',' if any.
951 if (Tok.is(tok::comma))
952 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000953 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000954 }
955 // End location of the directive.
956 EndLoc = Tok.getLocation();
957 // Consume final annot_pragma_openmp_end.
958 ConsumeToken();
959
Alexey Bataeveb482352015-12-18 05:05:56 +0000960 // OpenMP [2.13.8, ordered Construct, Syntax]
961 // If the depend clause is specified, the ordered construct is a stand-alone
962 // directive.
963 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000964 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000965 Diag(Loc, diag::err_omp_immediate_directive)
966 << getOpenMPDirectiveName(DKind) << 1
967 << getOpenMPClauseName(OMPC_depend);
968 }
969 HasAssociatedStatement = false;
970 }
971
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000972 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000973 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000974 // The body is a block scope like in Lambdas and Blocks.
975 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000976 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000977 Actions.ActOnStartOfCompoundStmt();
978 // Parse statement
979 AssociatedStmt = ParseStatement();
980 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000981 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000982 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000983 Directive = Actions.ActOnOpenMPExecutableDirective(
984 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
985 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000986
987 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000988 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000989 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000990 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000991 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000992 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000993 case OMPD_declare_target:
994 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +0000995 Diag(Tok, diag::err_omp_unexpected_directive)
996 << getOpenMPDirectiveName(DKind);
997 SkipUntil(tok::annot_pragma_openmp_end);
998 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000999 case OMPD_unknown:
1000 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001001 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001002 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001003 }
1004 return Directive;
1005}
1006
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001007// Parses simple list:
1008// simple-variable-list:
1009// '(' id-expression {, id-expression} ')'
1010//
1011bool Parser::ParseOpenMPSimpleVarList(
1012 OpenMPDirectiveKind Kind,
1013 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1014 Callback,
1015 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001016 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001017 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001018 if (T.expectAndConsume(diag::err_expected_lparen_after,
1019 getOpenMPDirectiveName(Kind)))
1020 return true;
1021 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001022 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001023
1024 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001025 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001026 CXXScopeSpec SS;
1027 SourceLocation TemplateKWLoc;
1028 UnqualifiedId Name;
1029 // Read var name.
1030 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001031 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001032
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001033 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001034 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
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);
David Blaikieefdccaa2016-01-15 23:43:34 +00001038 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001039 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001040 IsCorrect = false;
1041 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001042 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001043 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1044 Tok.isNot(tok::annot_pragma_openmp_end)) {
1045 IsCorrect = false;
1046 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001047 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001048 Diag(PrevTok.getLocation(), diag::err_expected)
1049 << tok::identifier
1050 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001051 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001052 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001053 }
1054 // Consume ','.
1055 if (Tok.is(tok::comma)) {
1056 ConsumeToken();
1057 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001058 }
1059
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001060 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001061 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001062 IsCorrect = false;
1063 }
1064
1065 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001066 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001067
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001068 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001069}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001070
1071/// \brief Parsing of OpenMP clauses.
1072///
1073/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001074/// if-clause | final-clause | num_threads-clause | safelen-clause |
1075/// default-clause | private-clause | firstprivate-clause | shared-clause
1076/// | linear-clause | aligned-clause | collapse-clause |
1077/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001078/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001079/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001080/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001081/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001082/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001083/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Carlo Bertolli70594e92016-07-13 17:16:49 +00001084/// from-clause | is_device_ptr-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001085///
1086OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1087 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001088 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001089 bool ErrorFound = false;
1090 // Check if clause is allowed for the given directive.
1091 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001092 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1093 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001094 ErrorFound = true;
1095 }
1096
1097 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001098 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001099 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001100 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001101 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001102 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001103 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001104 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001105 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001106 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001107 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001108 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001109 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001110 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001111 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001112 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001113 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001114 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001115 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001116 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001117 // OpenMP [2.9.1, target data construct, Restrictions]
1118 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001119 // OpenMP [2.11.1, task Construct, Restrictions]
1120 // At most one if clause can appear on the directive.
1121 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001122 // OpenMP [teams Construct, Restrictions]
1123 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001124 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001125 // OpenMP [2.9.1, task Construct, Restrictions]
1126 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001127 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1128 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001129 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1130 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001131 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001132 Diag(Tok, diag::err_omp_more_one_clause)
1133 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001134 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001135 }
1136
Alexey Bataev10e775f2015-07-30 11:36:16 +00001137 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1138 Clause = ParseOpenMPClause(CKind);
1139 else
1140 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001141 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001142 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001143 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001144 // OpenMP [2.14.3.1, Restrictions]
1145 // Only a single default clause may be specified on a parallel, task or
1146 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001147 // OpenMP [2.5, parallel Construct, Restrictions]
1148 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001149 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001150 Diag(Tok, diag::err_omp_more_one_clause)
1151 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001152 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001153 }
1154
1155 Clause = ParseOpenMPSimpleClause(CKind);
1156 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001157 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001158 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001159 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001160 // OpenMP [2.7.1, Restrictions, p. 3]
1161 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001162 // OpenMP [2.10.4, Restrictions, p. 106]
1163 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001164 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001165 Diag(Tok, diag::err_omp_more_one_clause)
1166 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001167 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001168 }
1169
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001170 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001171 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1172 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001173 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001174 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001175 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001176 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001177 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001178 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001179 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001180 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001181 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001182 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001183 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001184 // OpenMP [2.7.1, Restrictions, p. 9]
1185 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001186 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1187 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001188 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001189 Diag(Tok, diag::err_omp_more_one_clause)
1190 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001191 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001192 }
1193
1194 Clause = ParseOpenMPClause(CKind);
1195 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001196 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001197 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001198 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001199 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001200 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001201 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001202 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001203 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001204 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001205 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001206 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001207 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001208 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001209 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001210 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001211 case OMPC_is_device_ptr:
Alexey Bataeveb482352015-12-18 05:05:56 +00001212 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001213 break;
1214 case OMPC_unknown:
1215 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001216 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001217 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001218 break;
1219 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001220 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001221 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1222 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001223 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001224 break;
1225 }
Craig Topper161e4db2014-05-21 06:02:52 +00001226 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001227}
1228
Alexey Bataev2af33e32016-04-07 12:45:37 +00001229/// Parses simple expression in parens for single-expression clauses of OpenMP
1230/// constructs.
1231/// \param RLoc Returned location of right paren.
1232ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1233 SourceLocation &RLoc) {
1234 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1235 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1236 return ExprError();
1237
1238 SourceLocation ELoc = Tok.getLocation();
1239 ExprResult LHS(ParseCastExpression(
1240 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1241 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1242 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1243
1244 // Parse ')'.
1245 T.consumeClose();
1246
1247 RLoc = T.getCloseLocation();
1248 return Val;
1249}
1250
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001251/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001252/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001253/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001254///
Alexey Bataev3778b602014-07-17 07:32:53 +00001255/// final-clause:
1256/// 'final' '(' expression ')'
1257///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001258/// num_threads-clause:
1259/// 'num_threads' '(' expression ')'
1260///
1261/// safelen-clause:
1262/// 'safelen' '(' expression ')'
1263///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001264/// simdlen-clause:
1265/// 'simdlen' '(' expression ')'
1266///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001267/// collapse-clause:
1268/// 'collapse' '(' expression ')'
1269///
Alexey Bataeva0569352015-12-01 10:17:31 +00001270/// priority-clause:
1271/// 'priority' '(' expression ')'
1272///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001273/// grainsize-clause:
1274/// 'grainsize' '(' expression ')'
1275///
Alexey Bataev382967a2015-12-08 12:06:20 +00001276/// num_tasks-clause:
1277/// 'num_tasks' '(' expression ')'
1278///
Alexey Bataev28c75412015-12-15 08:19:24 +00001279/// hint-clause:
1280/// 'hint' '(' expression ')'
1281///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001282OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1283 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001284 SourceLocation LLoc = Tok.getLocation();
1285 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001286
Alexey Bataev2af33e32016-04-07 12:45:37 +00001287 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001288
1289 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001290 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001291
Alexey Bataev2af33e32016-04-07 12:45:37 +00001292 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001293}
1294
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001295/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001296///
1297/// default-clause:
1298/// 'default' '(' 'none' | 'shared' ')
1299///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001300/// proc_bind-clause:
1301/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1302///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001303OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1304 SourceLocation Loc = Tok.getLocation();
1305 SourceLocation LOpen = ConsumeToken();
1306 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001307 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001308 if (T.expectAndConsume(diag::err_expected_lparen_after,
1309 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001310 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001311
Alexey Bataeva55ed262014-05-28 06:15:33 +00001312 unsigned Type = getOpenMPSimpleClauseType(
1313 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001314 SourceLocation TypeLoc = Tok.getLocation();
1315 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1316 Tok.isNot(tok::annot_pragma_openmp_end))
1317 ConsumeAnyToken();
1318
1319 // Parse ')'.
1320 T.consumeClose();
1321
1322 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1323 Tok.getLocation());
1324}
1325
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001326/// \brief Parsing of OpenMP clauses like 'ordered'.
1327///
1328/// ordered-clause:
1329/// 'ordered'
1330///
Alexey Bataev236070f2014-06-20 11:19:47 +00001331/// nowait-clause:
1332/// 'nowait'
1333///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001334/// untied-clause:
1335/// 'untied'
1336///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001337/// mergeable-clause:
1338/// 'mergeable'
1339///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001340/// read-clause:
1341/// 'read'
1342///
Alexey Bataev346265e2015-09-25 10:37:12 +00001343/// threads-clause:
1344/// 'threads'
1345///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001346/// simd-clause:
1347/// 'simd'
1348///
Alexey Bataevb825de12015-12-07 10:51:44 +00001349/// nogroup-clause:
1350/// 'nogroup'
1351///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001352OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1353 SourceLocation Loc = Tok.getLocation();
1354 ConsumeAnyToken();
1355
1356 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1357}
1358
1359
Alexey Bataev56dafe82014-06-20 07:16:17 +00001360/// \brief Parsing of OpenMP clauses with single expressions and some additional
1361/// argument like 'schedule' or 'dist_schedule'.
1362///
1363/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001364/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1365/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001366///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001367/// if-clause:
1368/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1369///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001370/// defaultmap:
1371/// 'defaultmap' '(' modifier ':' kind ')'
1372///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001373OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1374 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001375 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001376 // Parse '('.
1377 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1378 if (T.expectAndConsume(diag::err_expected_lparen_after,
1379 getOpenMPClauseName(Kind)))
1380 return nullptr;
1381
1382 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001383 SmallVector<unsigned, 4> Arg;
1384 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001385 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001386 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1387 Arg.resize(NumberOfElements);
1388 KLoc.resize(NumberOfElements);
1389 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1390 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1391 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1392 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001393 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001394 if (KindModifier > OMPC_SCHEDULE_unknown) {
1395 // Parse 'modifier'
1396 Arg[Modifier1] = KindModifier;
1397 KLoc[Modifier1] = Tok.getLocation();
1398 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1399 Tok.isNot(tok::annot_pragma_openmp_end))
1400 ConsumeAnyToken();
1401 if (Tok.is(tok::comma)) {
1402 // Parse ',' 'modifier'
1403 ConsumeAnyToken();
1404 KindModifier = getOpenMPSimpleClauseType(
1405 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1406 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1407 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001408 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001409 KLoc[Modifier2] = Tok.getLocation();
1410 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1411 Tok.isNot(tok::annot_pragma_openmp_end))
1412 ConsumeAnyToken();
1413 }
1414 // Parse ':'
1415 if (Tok.is(tok::colon))
1416 ConsumeAnyToken();
1417 else
1418 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1419 KindModifier = getOpenMPSimpleClauseType(
1420 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1421 }
1422 Arg[ScheduleKind] = KindModifier;
1423 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001424 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1425 Tok.isNot(tok::annot_pragma_openmp_end))
1426 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001427 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1428 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1429 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001430 Tok.is(tok::comma))
1431 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001432 } else if (Kind == OMPC_dist_schedule) {
1433 Arg.push_back(getOpenMPSimpleClauseType(
1434 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1435 KLoc.push_back(Tok.getLocation());
1436 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1437 Tok.isNot(tok::annot_pragma_openmp_end))
1438 ConsumeAnyToken();
1439 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1440 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001441 } else if (Kind == OMPC_defaultmap) {
1442 // Get a defaultmap modifier
1443 Arg.push_back(getOpenMPSimpleClauseType(
1444 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1445 KLoc.push_back(Tok.getLocation());
1446 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1447 Tok.isNot(tok::annot_pragma_openmp_end))
1448 ConsumeAnyToken();
1449 // Parse ':'
1450 if (Tok.is(tok::colon))
1451 ConsumeAnyToken();
1452 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1453 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1454 // Get a defaultmap kind
1455 Arg.push_back(getOpenMPSimpleClauseType(
1456 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1457 KLoc.push_back(Tok.getLocation());
1458 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1459 Tok.isNot(tok::annot_pragma_openmp_end))
1460 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001461 } else {
1462 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001463 KLoc.push_back(Tok.getLocation());
1464 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1465 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001466 ConsumeToken();
1467 if (Tok.is(tok::colon))
1468 DelimLoc = ConsumeToken();
1469 else
1470 Diag(Tok, diag::warn_pragma_expected_colon)
1471 << "directive name modifier";
1472 }
1473 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001474
Carlo Bertollib4adf552016-01-15 18:50:31 +00001475 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1476 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1477 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001478 if (NeedAnExpression) {
1479 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001480 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1481 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001482 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001483 }
1484
1485 // Parse ')'.
1486 T.consumeClose();
1487
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001488 if (NeedAnExpression && Val.isInvalid())
1489 return nullptr;
1490
Alexey Bataev56dafe82014-06-20 07:16:17 +00001491 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001492 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001493 T.getCloseLocation());
1494}
1495
Alexey Bataevc5e02582014-06-16 07:08:35 +00001496static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1497 UnqualifiedId &ReductionId) {
1498 SourceLocation TemplateKWLoc;
1499 if (ReductionIdScopeSpec.isEmpty()) {
1500 auto OOK = OO_None;
1501 switch (P.getCurToken().getKind()) {
1502 case tok::plus:
1503 OOK = OO_Plus;
1504 break;
1505 case tok::minus:
1506 OOK = OO_Minus;
1507 break;
1508 case tok::star:
1509 OOK = OO_Star;
1510 break;
1511 case tok::amp:
1512 OOK = OO_Amp;
1513 break;
1514 case tok::pipe:
1515 OOK = OO_Pipe;
1516 break;
1517 case tok::caret:
1518 OOK = OO_Caret;
1519 break;
1520 case tok::ampamp:
1521 OOK = OO_AmpAmp;
1522 break;
1523 case tok::pipepipe:
1524 OOK = OO_PipePipe;
1525 break;
1526 default:
1527 break;
1528 }
1529 if (OOK != OO_None) {
1530 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001531 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001532 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1533 return false;
1534 }
1535 }
1536 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1537 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001538 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001539 TemplateKWLoc, ReductionId);
1540}
1541
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001542/// Parses clauses with list.
1543bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1544 OpenMPClauseKind Kind,
1545 SmallVectorImpl<Expr *> &Vars,
1546 OpenMPVarListDataTy &Data) {
1547 UnqualifiedId UnqualifiedReductionId;
1548 bool InvalidReductionId = false;
1549 bool MapTypeModifierSpecified = false;
1550
1551 // Parse '('.
1552 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1553 if (T.expectAndConsume(diag::err_expected_lparen_after,
1554 getOpenMPClauseName(Kind)))
1555 return true;
1556
1557 bool NeedRParenForLinear = false;
1558 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1559 tok::annot_pragma_openmp_end);
1560 // Handle reduction-identifier for reduction clause.
1561 if (Kind == OMPC_reduction) {
1562 ColonProtectionRAIIObject ColonRAII(*this);
1563 if (getLangOpts().CPlusPlus)
1564 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1565 /*ObjectType=*/nullptr,
1566 /*EnteringContext=*/false);
1567 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1568 UnqualifiedReductionId);
1569 if (InvalidReductionId) {
1570 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1571 StopBeforeMatch);
1572 }
1573 if (Tok.is(tok::colon))
1574 Data.ColonLoc = ConsumeToken();
1575 else
1576 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1577 if (!InvalidReductionId)
1578 Data.ReductionId =
1579 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1580 } else if (Kind == OMPC_depend) {
1581 // Handle dependency type for depend clause.
1582 ColonProtectionRAIIObject ColonRAII(*this);
1583 Data.DepKind =
1584 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1585 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1586 Data.DepLinMapLoc = Tok.getLocation();
1587
1588 if (Data.DepKind == OMPC_DEPEND_unknown) {
1589 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1590 StopBeforeMatch);
1591 } else {
1592 ConsumeToken();
1593 // Special processing for depend(source) clause.
1594 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1595 // Parse ')'.
1596 T.consumeClose();
1597 return false;
1598 }
1599 }
1600 if (Tok.is(tok::colon))
1601 Data.ColonLoc = ConsumeToken();
1602 else {
1603 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1604 : diag::warn_pragma_expected_colon)
1605 << "dependency type";
1606 }
1607 } else if (Kind == OMPC_linear) {
1608 // Try to parse modifier if any.
1609 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1610 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1611 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1612 Data.DepLinMapLoc = ConsumeToken();
1613 LinearT.consumeOpen();
1614 NeedRParenForLinear = true;
1615 }
1616 } else if (Kind == OMPC_map) {
1617 // Handle map type for map clause.
1618 ColonProtectionRAIIObject ColonRAII(*this);
1619
1620 /// The map clause modifier token can be either a identifier or the C++
1621 /// delete keyword.
1622 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1623 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1624 };
1625
1626 // The first identifier may be a list item, a map-type or a
1627 // map-type-modifier. The map modifier can also be delete which has the same
1628 // spelling of the C++ delete keyword.
1629 Data.MapType =
1630 IsMapClauseModifierToken(Tok)
1631 ? static_cast<OpenMPMapClauseKind>(
1632 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1633 : OMPC_MAP_unknown;
1634 Data.DepLinMapLoc = Tok.getLocation();
1635 bool ColonExpected = false;
1636
1637 if (IsMapClauseModifierToken(Tok)) {
1638 if (PP.LookAhead(0).is(tok::colon)) {
1639 if (Data.MapType == OMPC_MAP_unknown)
1640 Diag(Tok, diag::err_omp_unknown_map_type);
1641 else if (Data.MapType == OMPC_MAP_always)
1642 Diag(Tok, diag::err_omp_map_type_missing);
1643 ConsumeToken();
1644 } else if (PP.LookAhead(0).is(tok::comma)) {
1645 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1646 PP.LookAhead(2).is(tok::colon)) {
1647 Data.MapTypeModifier = Data.MapType;
1648 if (Data.MapTypeModifier != OMPC_MAP_always) {
1649 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1650 Data.MapTypeModifier = OMPC_MAP_unknown;
1651 } else
1652 MapTypeModifierSpecified = true;
1653
1654 ConsumeToken();
1655 ConsumeToken();
1656
1657 Data.MapType =
1658 IsMapClauseModifierToken(Tok)
1659 ? static_cast<OpenMPMapClauseKind>(
1660 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1661 : OMPC_MAP_unknown;
1662 if (Data.MapType == OMPC_MAP_unknown ||
1663 Data.MapType == OMPC_MAP_always)
1664 Diag(Tok, diag::err_omp_unknown_map_type);
1665 ConsumeToken();
1666 } else {
1667 Data.MapType = OMPC_MAP_tofrom;
1668 Data.IsMapTypeImplicit = true;
1669 }
1670 } else {
1671 Data.MapType = OMPC_MAP_tofrom;
1672 Data.IsMapTypeImplicit = true;
1673 }
1674 } else {
1675 Data.MapType = OMPC_MAP_tofrom;
1676 Data.IsMapTypeImplicit = true;
1677 }
1678
1679 if (Tok.is(tok::colon))
1680 Data.ColonLoc = ConsumeToken();
1681 else if (ColonExpected)
1682 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1683 }
1684
1685 bool IsComma =
1686 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1687 (Kind == OMPC_reduction && !InvalidReductionId) ||
1688 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1689 (!MapTypeModifierSpecified ||
1690 Data.MapTypeModifier == OMPC_MAP_always)) ||
1691 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1692 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1693 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1694 Tok.isNot(tok::annot_pragma_openmp_end))) {
1695 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1696 // Parse variable
1697 ExprResult VarExpr =
1698 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1699 if (VarExpr.isUsable())
1700 Vars.push_back(VarExpr.get());
1701 else {
1702 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1703 StopBeforeMatch);
1704 }
1705 // Skip ',' if any
1706 IsComma = Tok.is(tok::comma);
1707 if (IsComma)
1708 ConsumeToken();
1709 else if (Tok.isNot(tok::r_paren) &&
1710 Tok.isNot(tok::annot_pragma_openmp_end) &&
1711 (!MayHaveTail || Tok.isNot(tok::colon)))
1712 Diag(Tok, diag::err_omp_expected_punc)
1713 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1714 : getOpenMPClauseName(Kind))
1715 << (Kind == OMPC_flush);
1716 }
1717
1718 // Parse ')' for linear clause with modifier.
1719 if (NeedRParenForLinear)
1720 LinearT.consumeClose();
1721
1722 // Parse ':' linear-step (or ':' alignment).
1723 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1724 if (MustHaveTail) {
1725 Data.ColonLoc = Tok.getLocation();
1726 SourceLocation ELoc = ConsumeToken();
1727 ExprResult Tail = ParseAssignmentExpression();
1728 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1729 if (Tail.isUsable())
1730 Data.TailExpr = Tail.get();
1731 else
1732 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1733 StopBeforeMatch);
1734 }
1735
1736 // Parse ')'.
1737 T.consumeClose();
1738 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1739 Vars.empty()) ||
1740 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1741 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1742 return true;
1743 return false;
1744}
1745
Alexander Musman1bb328c2014-06-04 13:06:39 +00001746/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001747/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001748///
1749/// private-clause:
1750/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001751/// firstprivate-clause:
1752/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001753/// lastprivate-clause:
1754/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001755/// shared-clause:
1756/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001757/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001758/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001759/// aligned-clause:
1760/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001761/// reduction-clause:
1762/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001763/// copyprivate-clause:
1764/// 'copyprivate' '(' list ')'
1765/// flush-clause:
1766/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001767/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001768/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001769/// map-clause:
1770/// 'map' '(' [ [ always , ]
1771/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001772/// to-clause:
1773/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001774/// from-clause:
1775/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001776/// use_device_ptr-clause:
1777/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001778/// is_device_ptr-clause:
1779/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001780///
Alexey Bataev182227b2015-08-20 10:54:39 +00001781/// For 'linear' clause linear-list may have the following forms:
1782/// list
1783/// modifier(list)
1784/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001785OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1786 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001787 SourceLocation Loc = Tok.getLocation();
1788 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001789 SmallVector<Expr *, 4> Vars;
1790 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001791
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001792 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001793 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001794
Alexey Bataevc5e02582014-06-16 07:08:35 +00001795 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001796 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1797 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1798 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1799 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001800}
1801