blob: d5d2f08d9986f42557f3e43e56ef855f4394878e [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements parsing of all OpenMP directives and clauses.
11///
12//===----------------------------------------------------------------------===//
13
Chandler Carruth5553d0d2014-01-07 11:51:46 +000014#include "RAIIObjectsForParser.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000016#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000017#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000018#include "clang/Parse/Parser.h"
19#include "clang/Sema/Scope.h"
20#include "llvm/ADT/PointerIntPair.h"
Michael Wong65f367f2015-07-21 13:44:28 +000021
Alexey Bataeva769e072013-03-22 06:34:35 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// OpenMP declarative directives.
26//===----------------------------------------------------------------------===//
27
Dmitry Polukhin82478332016-02-13 06:53:38 +000028namespace {
29enum OpenMPDirectiveKindEx {
30 OMPD_cancellation = OMPD_unknown + 1,
31 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000032 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000033 OMPD_end,
34 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000035 OMPD_enter,
36 OMPD_exit,
37 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000038 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000039 OMPD_target_enter,
Samuel Antao686c70c2016-05-26 17:30:50 +000040 OMPD_target_exit,
41 OMPD_update,
Carlo Bertolli9925f152016-06-27 14:55:37 +000042 OMPD_distribute_parallel
Dmitry Polukhin82478332016-02-13 06:53:38 +000043};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000044
45class ThreadprivateListParserHelper final {
46 SmallVector<Expr *, 4> Identifiers;
47 Parser *P;
48
49public:
50 ThreadprivateListParserHelper(Parser *P) : P(P) {}
51 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
52 ExprResult Res =
53 P->getActions().ActOnOpenMPIdExpression(P->getCurScope(), SS, NameInfo);
54 if (Res.isUsable())
55 Identifiers.push_back(Res.get());
56 }
57 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
58};
Dmitry Polukhin82478332016-02-13 06:53:38 +000059} // namespace
60
61// Map token string to extended OMP token kind that are
62// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
63static unsigned getOpenMPDirectiveKindEx(StringRef S) {
64 auto DKind = getOpenMPDirectiveKind(S);
65 if (DKind != OMPD_unknown)
66 return DKind;
67
68 return llvm::StringSwitch<unsigned>(S)
69 .Case("cancellation", OMPD_cancellation)
70 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000071 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000072 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000073 .Case("enter", OMPD_enter)
74 .Case("exit", OMPD_exit)
75 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000076 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000077 .Case("update", OMPD_update)
Dmitry Polukhin82478332016-02-13 06:53:38 +000078 .Default(OMPD_unknown);
79}
80
Alexey Bataev4acb8592014-07-07 13:01:15 +000081static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000082 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
83 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
84 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000085 static const unsigned F[][3] = {
86 { OMPD_cancellation, OMPD_point, OMPD_cancellation_point },
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000087 { OMPD_declare, OMPD_reduction, OMPD_declare_reduction },
Alexey Bataev587e1de2016-03-30 10:43:55 +000088 { OMPD_declare, OMPD_simd, OMPD_declare_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000089 { OMPD_declare, OMPD_target, OMPD_declare_target },
Carlo Bertolli9925f152016-06-27 14:55:37 +000090 { OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel },
91 { OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for },
Kelvin Li4a39add2016-07-05 05:00:15 +000092 { OMPD_distribute_parallel_for, OMPD_simd,
93 OMPD_distribute_parallel_for_simd },
Kelvin Li787f3fc2016-07-06 04:45:38 +000094 { OMPD_distribute, OMPD_simd, OMPD_distribute_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000095 { OMPD_end, OMPD_declare, OMPD_end_declare },
96 { OMPD_end_declare, OMPD_target, OMPD_end_declare_target },
Dmitry Polukhin82478332016-02-13 06:53:38 +000097 { OMPD_target, OMPD_data, OMPD_target_data },
98 { OMPD_target, OMPD_enter, OMPD_target_enter },
99 { OMPD_target, OMPD_exit, OMPD_target_exit },
Samuel Antao686c70c2016-05-26 17:30:50 +0000100 { OMPD_target, OMPD_update, OMPD_target_update },
Dmitry Polukhin82478332016-02-13 06:53:38 +0000101 { OMPD_target_enter, OMPD_data, OMPD_target_enter_data },
102 { OMPD_target_exit, OMPD_data, OMPD_target_exit_data },
103 { OMPD_for, OMPD_simd, OMPD_for_simd },
104 { OMPD_parallel, OMPD_for, OMPD_parallel_for },
105 { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
106 { OMPD_parallel, OMPD_sections, OMPD_parallel_sections },
107 { OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd },
108 { OMPD_target, OMPD_parallel, OMPD_target_parallel },
Kelvin Li986330c2016-07-20 22:57:10 +0000109 { OMPD_target, OMPD_simd, OMPD_target_simd },
Kelvin Lia579b912016-07-14 02:54:56 +0000110 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for },
111 { OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd }
Dmitry Polukhin82478332016-02-13 06:53:38 +0000112 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000113 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +0000114 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000115 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000116 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000117 ? static_cast<unsigned>(OMPD_unknown)
118 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
119 if (DKind == OMPD_unknown)
120 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000121
Alexander Musmanf82886e2014-09-18 05:12:34 +0000122 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000123 if (DKind != F[i][0])
124 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000125
Dmitry Polukhin82478332016-02-13 06:53:38 +0000126 Tok = P.getPreprocessor().LookAhead(0);
127 unsigned SDKind =
128 Tok.isAnnotation()
129 ? static_cast<unsigned>(OMPD_unknown)
130 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
131 if (SDKind == OMPD_unknown)
132 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000133
Dmitry Polukhin82478332016-02-13 06:53:38 +0000134 if (SDKind == F[i][1]) {
135 P.ConsumeToken();
136 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000137 }
138 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000139 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
140 : OMPD_unknown;
141}
142
143static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000144 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000145 Sema &Actions = P.getActions();
146 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000147 // Allow to use 'operator' keyword for C++ operators
148 bool WithOperator = false;
149 if (Tok.is(tok::kw_operator)) {
150 P.ConsumeToken();
151 Tok = P.getCurToken();
152 WithOperator = true;
153 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000154 switch (Tok.getKind()) {
155 case tok::plus: // '+'
156 OOK = OO_Plus;
157 break;
158 case tok::minus: // '-'
159 OOK = OO_Minus;
160 break;
161 case tok::star: // '*'
162 OOK = OO_Star;
163 break;
164 case tok::amp: // '&'
165 OOK = OO_Amp;
166 break;
167 case tok::pipe: // '|'
168 OOK = OO_Pipe;
169 break;
170 case tok::caret: // '^'
171 OOK = OO_Caret;
172 break;
173 case tok::ampamp: // '&&'
174 OOK = OO_AmpAmp;
175 break;
176 case tok::pipepipe: // '||'
177 OOK = OO_PipePipe;
178 break;
179 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000180 if (!WithOperator)
181 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000182 default:
183 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
184 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
185 Parser::StopBeforeMatch);
186 return DeclarationName();
187 }
188 P.ConsumeToken();
189 auto &DeclNames = Actions.getASTContext().DeclarationNames;
190 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
191 : DeclNames.getCXXOperatorName(OOK);
192}
193
194/// \brief Parse 'omp declare reduction' construct.
195///
196/// declare-reduction-directive:
197/// annot_pragma_openmp 'declare' 'reduction'
198/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
199/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
200/// annot_pragma_openmp_end
201/// <reduction_id> is either a base language identifier or one of the following
202/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
203///
204Parser::DeclGroupPtrTy
205Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
206 // Parse '('.
207 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
208 if (T.expectAndConsume(diag::err_expected_lparen_after,
209 getOpenMPDirectiveName(OMPD_declare_reduction))) {
210 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
211 return DeclGroupPtrTy();
212 }
213
214 DeclarationName Name = parseOpenMPReductionId(*this);
215 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
216 return DeclGroupPtrTy();
217
218 // Consume ':'.
219 bool IsCorrect = !ExpectAndConsume(tok::colon);
220
221 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
222 return DeclGroupPtrTy();
223
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000224 IsCorrect = IsCorrect && !Name.isEmpty();
225
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000226 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
227 Diag(Tok.getLocation(), diag::err_expected_type);
228 IsCorrect = false;
229 }
230
231 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
232 return DeclGroupPtrTy();
233
234 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
235 // Parse list of types until ':' token.
236 do {
237 ColonProtectionRAIIObject ColonRAII(*this);
238 SourceRange Range;
239 TypeResult TR = ParseTypeName(&Range, Declarator::PrototypeContext, AS);
240 if (TR.isUsable()) {
241 auto ReductionType =
242 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
243 if (!ReductionType.isNull()) {
244 ReductionTypes.push_back(
245 std::make_pair(ReductionType, Range.getBegin()));
246 }
247 } else {
248 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
249 StopBeforeMatch);
250 }
251
252 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
253 break;
254
255 // Consume ','.
256 if (ExpectAndConsume(tok::comma)) {
257 IsCorrect = false;
258 if (Tok.is(tok::annot_pragma_openmp_end)) {
259 Diag(Tok.getLocation(), diag::err_expected_type);
260 return DeclGroupPtrTy();
261 }
262 }
263 } while (Tok.isNot(tok::annot_pragma_openmp_end));
264
265 if (ReductionTypes.empty()) {
266 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
267 return DeclGroupPtrTy();
268 }
269
270 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
271 return DeclGroupPtrTy();
272
273 // Consume ':'.
274 if (ExpectAndConsume(tok::colon))
275 IsCorrect = false;
276
277 if (Tok.is(tok::annot_pragma_openmp_end)) {
278 Diag(Tok.getLocation(), diag::err_expected_expression);
279 return DeclGroupPtrTy();
280 }
281
282 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
283 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
284
285 // Parse <combiner> expression and then parse initializer if any for each
286 // correct type.
287 unsigned I = 0, E = ReductionTypes.size();
288 for (auto *D : DRD.get()) {
289 TentativeParsingAction TPA(*this);
290 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
291 Scope::OpenMPDirectiveScope);
292 // Parse <combiner> expression.
293 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
294 ExprResult CombinerResult =
295 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
296 D->getLocation(), /*DiscardedValue=*/true);
297 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
298
299 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
300 Tok.isNot(tok::annot_pragma_openmp_end)) {
301 TPA.Commit();
302 IsCorrect = false;
303 break;
304 }
305 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
306 ExprResult InitializerResult;
307 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
308 // Parse <initializer> expression.
309 if (Tok.is(tok::identifier) &&
310 Tok.getIdentifierInfo()->isStr("initializer"))
311 ConsumeToken();
312 else {
313 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
314 TPA.Commit();
315 IsCorrect = false;
316 break;
317 }
318 // Parse '('.
319 BalancedDelimiterTracker T(*this, tok::l_paren,
320 tok::annot_pragma_openmp_end);
321 IsCorrect =
322 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
323 IsCorrect;
324 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
325 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
326 Scope::OpenMPDirectiveScope);
327 // Parse expression.
328 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), D);
329 InitializerResult = Actions.ActOnFinishFullExpr(
330 ParseAssignmentExpression().get(), D->getLocation(),
331 /*DiscardedValue=*/true);
332 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
333 D, InitializerResult.get());
334 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
335 Tok.isNot(tok::annot_pragma_openmp_end)) {
336 TPA.Commit();
337 IsCorrect = false;
338 break;
339 }
340 IsCorrect =
341 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
342 }
343 }
344
345 ++I;
346 // Revert parsing if not the last type, otherwise accept it, we're done with
347 // parsing.
348 if (I != E)
349 TPA.Revert();
350 else
351 TPA.Commit();
352 }
353 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
354 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000355}
356
Alexey Bataev2af33e32016-04-07 12:45:37 +0000357namespace {
358/// RAII that recreates function context for correct parsing of clauses of
359/// 'declare simd' construct.
360/// OpenMP, 2.8.2 declare simd Construct
361/// The expressions appearing in the clauses of this directive are evaluated in
362/// the scope of the arguments of the function declaration or definition.
363class FNContextRAII final {
364 Parser &P;
365 Sema::CXXThisScopeRAII *ThisScope;
366 Parser::ParseScope *TempScope;
367 Parser::ParseScope *FnScope;
368 bool HasTemplateScope = false;
369 bool HasFunScope = false;
370 FNContextRAII() = delete;
371 FNContextRAII(const FNContextRAII &) = delete;
372 FNContextRAII &operator=(const FNContextRAII &) = delete;
373
374public:
375 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
376 Decl *D = *Ptr.get().begin();
377 NamedDecl *ND = dyn_cast<NamedDecl>(D);
378 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
379 Sema &Actions = P.getActions();
380
381 // Allow 'this' within late-parsed attributes.
382 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
383 ND && ND->isCXXInstanceMember());
384
385 // If the Decl is templatized, add template parameters to scope.
386 HasTemplateScope = D->isTemplateDecl();
387 TempScope =
388 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
389 if (HasTemplateScope)
390 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
391
392 // If the Decl is on a function, add function parameters to the scope.
393 HasFunScope = D->isFunctionOrFunctionTemplate();
394 FnScope = new Parser::ParseScope(&P, Scope::FnScope | Scope::DeclScope,
395 HasFunScope);
396 if (HasFunScope)
397 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
398 }
399 ~FNContextRAII() {
400 if (HasFunScope) {
401 P.getActions().ActOnExitFunctionContext();
402 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
403 }
404 if (HasTemplateScope)
405 TempScope->Exit();
406 delete FnScope;
407 delete TempScope;
408 delete ThisScope;
409 }
410};
411} // namespace
412
Alexey Bataevd93d3762016-04-12 09:35:56 +0000413/// Parses clauses for 'declare simd' directive.
414/// clause:
415/// 'inbranch' | 'notinbranch'
416/// 'simdlen' '(' <expr> ')'
417/// { 'uniform' '(' <argument_list> ')' }
418/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000419/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
420static bool parseDeclareSimdClauses(
421 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
422 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
423 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
424 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000425 SourceRange BSRange;
426 const Token &Tok = P.getCurToken();
427 bool IsError = false;
428 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
429 if (Tok.isNot(tok::identifier))
430 break;
431 OMPDeclareSimdDeclAttr::BranchStateTy Out;
432 IdentifierInfo *II = Tok.getIdentifierInfo();
433 StringRef ClauseName = II->getName();
434 // Parse 'inranch|notinbranch' clauses.
435 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
436 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
437 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
438 << ClauseName
439 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
440 IsError = true;
441 }
442 BS = Out;
443 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
444 P.ConsumeToken();
445 } else if (ClauseName.equals("simdlen")) {
446 if (SimdLen.isUsable()) {
447 P.Diag(Tok, diag::err_omp_more_one_clause)
448 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
449 IsError = true;
450 }
451 P.ConsumeToken();
452 SourceLocation RLoc;
453 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
454 if (SimdLen.isInvalid())
455 IsError = true;
456 } else {
457 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000458 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
459 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000460 Parser::OpenMPVarListDataTy Data;
461 auto *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000462 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000463 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000464 else if (CKind == OMPC_linear)
465 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000466
467 P.ConsumeToken();
468 if (P.ParseOpenMPVarList(OMPD_declare_simd,
469 getOpenMPClauseKind(ClauseName), *Vars, Data))
470 IsError = true;
471 if (CKind == OMPC_aligned)
472 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000473 else if (CKind == OMPC_linear) {
474 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
475 Data.DepLinMapLoc))
476 Data.LinKind = OMPC_LINEAR_val;
477 LinModifiers.append(Linears.size() - LinModifiers.size(),
478 Data.LinKind);
479 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
480 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000481 } else
482 // TODO: add parsing of other clauses.
483 break;
484 }
485 // Skip ',' if any.
486 if (Tok.is(tok::comma))
487 P.ConsumeToken();
488 }
489 return IsError;
490}
491
Alexey Bataev2af33e32016-04-07 12:45:37 +0000492/// Parse clauses for '#pragma omp declare simd'.
493Parser::DeclGroupPtrTy
494Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
495 CachedTokens &Toks, SourceLocation Loc) {
496 PP.EnterToken(Tok);
497 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
498 // Consume the previously pushed token.
499 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
500
501 FNContextRAII FnContext(*this, Ptr);
502 OMPDeclareSimdDeclAttr::BranchStateTy BS =
503 OMPDeclareSimdDeclAttr::BS_Undefined;
504 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000505 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000506 SmallVector<Expr *, 4> Aligneds;
507 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000508 SmallVector<Expr *, 4> Linears;
509 SmallVector<unsigned, 4> LinModifiers;
510 SmallVector<Expr *, 4> Steps;
511 bool IsError =
512 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
513 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000514 // Need to check for extra tokens.
515 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
516 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
517 << getOpenMPDirectiveName(OMPD_declare_simd);
518 while (Tok.isNot(tok::annot_pragma_openmp_end))
519 ConsumeAnyToken();
520 }
521 // Skip the last annot_pragma_openmp_end.
522 SourceLocation EndLoc = ConsumeToken();
Alexey Bataevd93d3762016-04-12 09:35:56 +0000523 if (!IsError) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000524 return Actions.ActOnOpenMPDeclareSimdDirective(
Alexey Bataevecba70f2016-04-12 11:02:11 +0000525 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
526 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataevd93d3762016-04-12 09:35:56 +0000527 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000528 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000529}
530
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000531/// \brief Parsing of declarative OpenMP directives.
532///
533/// threadprivate-directive:
534/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000535/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000536///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000537/// declare-reduction-directive:
538/// annot_pragma_openmp 'declare' 'reduction' [...]
539/// annot_pragma_openmp_end
540///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000541/// declare-simd-directive:
542/// annot_pragma_openmp 'declare simd' {<clause> [,]}
543/// annot_pragma_openmp_end
544/// <function declaration/definition>
545///
546Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
547 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
548 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000549 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000550 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000551
552 SourceLocation Loc = ConsumeToken();
Alexey Bataev4acb8592014-07-07 13:01:15 +0000553 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000554
555 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000556 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000557 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000558 ThreadprivateListParserHelper Helper(this);
559 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000560 // The last seen token is annot_pragma_openmp_end - need to check for
561 // extra tokens.
562 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
563 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000564 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000565 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000566 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000567 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000568 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000569 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
570 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000571 }
572 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000573 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000574 case OMPD_declare_reduction:
575 ConsumeToken();
576 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
577 // The last seen token is annot_pragma_openmp_end - need to check for
578 // extra tokens.
579 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
580 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
581 << getOpenMPDirectiveName(OMPD_declare_reduction);
582 while (Tok.isNot(tok::annot_pragma_openmp_end))
583 ConsumeAnyToken();
584 }
585 // Skip the last annot_pragma_openmp_end.
586 ConsumeToken();
587 return Res;
588 }
589 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000590 case OMPD_declare_simd: {
591 // The syntax is:
592 // { #pragma omp declare simd }
593 // <function-declaration-or-definition>
594 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000595 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000596 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000597 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
598 Toks.push_back(Tok);
599 ConsumeAnyToken();
600 }
601 Toks.push_back(Tok);
602 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000603
604 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000605 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000606 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000607 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000608 // Here we expect to see some function declaration.
609 if (AS == AS_none) {
610 assert(TagType == DeclSpec::TST_unspecified);
611 MaybeParseCXX11Attributes(Attrs);
612 MaybeParseMicrosoftAttributes(Attrs);
613 ParsingDeclSpec PDS(*this);
614 Ptr = ParseExternalDeclaration(Attrs, &PDS);
615 } else {
616 Ptr =
617 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
618 }
619 }
620 if (!Ptr) {
621 Diag(Loc, diag::err_omp_decl_in_declare_simd);
622 return DeclGroupPtrTy();
623 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000624 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000625 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000626 case OMPD_declare_target: {
627 SourceLocation DTLoc = ConsumeAnyToken();
628 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000629 // OpenMP 4.5 syntax with list of entities.
630 llvm::SmallSetVector<const NamedDecl*, 16> SameDirectiveDecls;
631 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
632 OMPDeclareTargetDeclAttr::MapTypeTy MT =
633 OMPDeclareTargetDeclAttr::MT_To;
634 if (Tok.is(tok::identifier)) {
635 IdentifierInfo *II = Tok.getIdentifierInfo();
636 StringRef ClauseName = II->getName();
637 // Parse 'to|link' clauses.
638 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
639 MT)) {
640 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
641 << ClauseName;
642 break;
643 }
644 ConsumeToken();
645 }
646 auto Callback = [this, MT, &SameDirectiveDecls](
647 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
648 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
649 SameDirectiveDecls);
650 };
651 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, true))
652 break;
653
654 // Consume optional ','.
655 if (Tok.is(tok::comma))
656 ConsumeToken();
657 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000658 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000659 ConsumeAnyToken();
660 return DeclGroupPtrTy();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000661 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000662
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000663 // Skip the last annot_pragma_openmp_end.
664 ConsumeAnyToken();
665
666 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
667 return DeclGroupPtrTy();
668
669 DKind = ParseOpenMPDirectiveKind(*this);
670 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
671 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
672 ParsedAttributesWithRange attrs(AttrFactory);
673 MaybeParseCXX11Attributes(attrs);
674 MaybeParseMicrosoftAttributes(attrs);
675 ParseExternalDeclaration(attrs);
676 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
677 TentativeParsingAction TPA(*this);
678 ConsumeToken();
679 DKind = ParseOpenMPDirectiveKind(*this);
680 if (DKind != OMPD_end_declare_target)
681 TPA.Revert();
682 else
683 TPA.Commit();
684 }
685 }
686
687 if (DKind == OMPD_end_declare_target) {
688 ConsumeAnyToken();
689 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
690 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
691 << getOpenMPDirectiveName(OMPD_end_declare_target);
692 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
693 }
694 // Skip the last annot_pragma_openmp_end.
695 ConsumeAnyToken();
696 } else {
697 Diag(Tok, diag::err_expected_end_declare_target);
698 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
699 }
700 Actions.ActOnFinishOpenMPDeclareTargetDirective();
701 return DeclGroupPtrTy();
702 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000703 case OMPD_unknown:
704 Diag(Tok, diag::err_omp_unknown_directive);
705 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000706 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000707 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000708 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000709 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000710 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000711 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000712 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000713 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000714 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000715 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000716 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000717 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000718 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000719 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000720 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000721 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000722 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000723 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000724 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000725 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000726 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000727 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000728 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000729 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000730 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000731 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000732 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000733 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000734 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000735 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000736 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000737 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000738 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000739 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000740 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000741 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000742 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000743 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000744 case OMPD_target_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000745 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000746 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000747 break;
748 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000749 while (Tok.isNot(tok::annot_pragma_openmp_end))
750 ConsumeAnyToken();
751 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000752 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000753}
754
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000755/// \brief Parsing of declarative or executable OpenMP directives.
756///
757/// threadprivate-directive:
758/// annot_pragma_openmp 'threadprivate' simple-variable-list
759/// annot_pragma_openmp_end
760///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000761/// declare-reduction-directive:
762/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
763/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
764/// ('omp_priv' '=' <expression>|<function_call>) ')']
765/// annot_pragma_openmp_end
766///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000767/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000768/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000769/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
770/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000771/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000772/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000773/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000774/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000775/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000776/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000777/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li986330c2016-07-20 22:57:10 +0000778/// 'target parallel for simd' | 'target simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000779/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000780///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000781StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
782 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000783 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000784 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000785 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000786 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000787 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000788 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000789 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000790 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000791 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000792 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000793 // Name of critical directive.
794 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000795 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000796 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000797 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000798
799 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000800 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000801 if (Allowed != ACK_Any) {
802 Diag(Tok, diag::err_omp_immediate_directive)
803 << getOpenMPDirectiveName(DKind) << 0;
804 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000805 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000806 ThreadprivateListParserHelper Helper(this);
807 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000808 // The last seen token is annot_pragma_openmp_end - need to check for
809 // extra tokens.
810 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
811 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000812 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000813 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000814 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000815 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
816 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000817 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
818 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000819 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000820 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000821 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000822 case OMPD_declare_reduction:
823 ConsumeToken();
824 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
825 // The last seen token is annot_pragma_openmp_end - need to check for
826 // extra tokens.
827 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
828 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
829 << getOpenMPDirectiveName(OMPD_declare_reduction);
830 while (Tok.isNot(tok::annot_pragma_openmp_end))
831 ConsumeAnyToken();
832 }
833 ConsumeAnyToken();
834 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
835 } else
836 SkipUntil(tok::annot_pragma_openmp_end);
837 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000838 case OMPD_flush:
839 if (PP.LookAhead(0).is(tok::l_paren)) {
840 FlushHasClause = true;
841 // Push copy of the current token back to stream to properly parse
842 // pseudo-clause OMPFlushClause.
843 PP.EnterToken(Tok);
844 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000845 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000846 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000847 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000848 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000849 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000850 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000851 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000852 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000853 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000854 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000855 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000856 }
857 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000858 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000859 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000860 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000861 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000862 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000863 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000864 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000865 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000866 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000867 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000868 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000869 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000870 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000871 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000872 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000873 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000874 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000875 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000876 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000877 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000878 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000879 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000880 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000881 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000882 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +0000883 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000884 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000885 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000886 case OMPD_target_parallel_for_simd:
887 case OMPD_target_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000888 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000889 // Parse directive name of the 'critical' directive if any.
890 if (DKind == OMPD_critical) {
891 BalancedDelimiterTracker T(*this, tok::l_paren,
892 tok::annot_pragma_openmp_end);
893 if (!T.consumeOpen()) {
894 if (Tok.isAnyIdentifier()) {
895 DirName =
896 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
897 ConsumeAnyToken();
898 } else {
899 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
900 }
901 T.consumeClose();
902 }
Alexey Bataev80909872015-07-02 11:25:17 +0000903 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000904 CancelRegion = ParseOpenMPDirectiveKind(*this);
905 if (Tok.isNot(tok::annot_pragma_openmp_end))
906 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000907 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000908
Alexey Bataevf29276e2014-06-18 04:14:57 +0000909 if (isOpenMPLoopDirective(DKind))
910 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
911 if (isOpenMPSimdDirective(DKind))
912 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
913 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000914 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000915
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000916 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000917 OpenMPClauseKind CKind =
918 Tok.isAnnotation()
919 ? OMPC_unknown
920 : FlushHasClause ? OMPC_flush
921 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000922 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000923 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000924 OMPClause *Clause =
925 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000926 FirstClauses[CKind].setInt(true);
927 if (Clause) {
928 FirstClauses[CKind].setPointer(Clause);
929 Clauses.push_back(Clause);
930 }
931
932 // Skip ',' if any.
933 if (Tok.is(tok::comma))
934 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000935 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000936 }
937 // End location of the directive.
938 EndLoc = Tok.getLocation();
939 // Consume final annot_pragma_openmp_end.
940 ConsumeToken();
941
Alexey Bataeveb482352015-12-18 05:05:56 +0000942 // OpenMP [2.13.8, ordered Construct, Syntax]
943 // If the depend clause is specified, the ordered construct is a stand-alone
944 // directive.
945 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000946 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000947 Diag(Loc, diag::err_omp_immediate_directive)
948 << getOpenMPDirectiveName(DKind) << 1
949 << getOpenMPClauseName(OMPC_depend);
950 }
951 HasAssociatedStatement = false;
952 }
953
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000954 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000955 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000956 // The body is a block scope like in Lambdas and Blocks.
957 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000958 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000959 Actions.ActOnStartOfCompoundStmt();
960 // Parse statement
961 AssociatedStmt = ParseStatement();
962 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000963 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000964 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000965 Directive = Actions.ActOnOpenMPExecutableDirective(
966 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
967 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000968
969 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000970 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000971 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000972 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000973 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000974 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000975 case OMPD_declare_target:
976 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +0000977 Diag(Tok, diag::err_omp_unexpected_directive)
978 << getOpenMPDirectiveName(DKind);
979 SkipUntil(tok::annot_pragma_openmp_end);
980 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000981 case OMPD_unknown:
982 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000983 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000984 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000985 }
986 return Directive;
987}
988
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000989// Parses simple list:
990// simple-variable-list:
991// '(' id-expression {, id-expression} ')'
992//
993bool Parser::ParseOpenMPSimpleVarList(
994 OpenMPDirectiveKind Kind,
995 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
996 Callback,
997 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000998 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000999 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001000 if (T.expectAndConsume(diag::err_expected_lparen_after,
1001 getOpenMPDirectiveName(Kind)))
1002 return true;
1003 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001004 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001005
1006 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001007 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001008 CXXScopeSpec SS;
1009 SourceLocation TemplateKWLoc;
1010 UnqualifiedId Name;
1011 // Read var name.
1012 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001013 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001014
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001015 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001016 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001017 IsCorrect = false;
1018 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001019 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +00001020 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001021 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001022 IsCorrect = false;
1023 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001024 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001025 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1026 Tok.isNot(tok::annot_pragma_openmp_end)) {
1027 IsCorrect = false;
1028 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001029 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001030 Diag(PrevTok.getLocation(), diag::err_expected)
1031 << tok::identifier
1032 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001033 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001034 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001035 }
1036 // Consume ','.
1037 if (Tok.is(tok::comma)) {
1038 ConsumeToken();
1039 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001040 }
1041
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001042 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001043 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001044 IsCorrect = false;
1045 }
1046
1047 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001048 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001049
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001050 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001051}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001052
1053/// \brief Parsing of OpenMP clauses.
1054///
1055/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001056/// if-clause | final-clause | num_threads-clause | safelen-clause |
1057/// default-clause | private-clause | firstprivate-clause | shared-clause
1058/// | linear-clause | aligned-clause | collapse-clause |
1059/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001060/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001061/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001062/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001063/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001064/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001065/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Carlo Bertolli70594e92016-07-13 17:16:49 +00001066/// from-clause | is_device_ptr-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001067///
1068OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1069 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001070 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001071 bool ErrorFound = false;
1072 // Check if clause is allowed for the given directive.
1073 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001074 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1075 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001076 ErrorFound = true;
1077 }
1078
1079 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001080 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001081 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001082 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001083 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001084 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001085 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001086 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001087 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001088 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001089 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001090 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001091 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001092 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001093 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001094 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001095 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001096 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001097 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001098 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001099 // OpenMP [2.9.1, target data construct, Restrictions]
1100 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001101 // OpenMP [2.11.1, task Construct, Restrictions]
1102 // At most one if clause can appear on the directive.
1103 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001104 // OpenMP [teams Construct, Restrictions]
1105 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001106 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001107 // OpenMP [2.9.1, task Construct, Restrictions]
1108 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001109 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1110 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001111 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1112 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001113 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001114 Diag(Tok, diag::err_omp_more_one_clause)
1115 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001116 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001117 }
1118
Alexey Bataev10e775f2015-07-30 11:36:16 +00001119 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1120 Clause = ParseOpenMPClause(CKind);
1121 else
1122 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001123 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001124 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001125 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001126 // OpenMP [2.14.3.1, Restrictions]
1127 // Only a single default clause may be specified on a parallel, task or
1128 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001129 // OpenMP [2.5, parallel Construct, Restrictions]
1130 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +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 Bataev5ec3eb12013-07-19 03:13:43 +00001135 }
1136
1137 Clause = ParseOpenMPSimpleClause(CKind);
1138 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001139 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001140 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001141 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001142 // OpenMP [2.7.1, Restrictions, p. 3]
1143 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001144 // OpenMP [2.10.4, Restrictions, p. 106]
1145 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001146 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001147 Diag(Tok, diag::err_omp_more_one_clause)
1148 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001149 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001150 }
1151
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001152 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001153 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1154 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001155 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001156 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001157 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001158 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001159 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001160 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001161 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001162 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001163 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001164 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001165 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001166 // OpenMP [2.7.1, Restrictions, p. 9]
1167 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001168 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1169 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001170 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001171 Diag(Tok, diag::err_omp_more_one_clause)
1172 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001173 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001174 }
1175
1176 Clause = ParseOpenMPClause(CKind);
1177 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001178 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001179 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001180 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001181 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001182 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001183 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001184 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001185 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001186 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001187 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001188 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001189 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001190 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001191 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001192 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001193 case OMPC_is_device_ptr:
Alexey Bataeveb482352015-12-18 05:05:56 +00001194 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001195 break;
1196 case OMPC_unknown:
1197 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001198 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001199 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001200 break;
1201 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001202 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001203 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1204 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001205 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001206 break;
1207 }
Craig Topper161e4db2014-05-21 06:02:52 +00001208 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001209}
1210
Alexey Bataev2af33e32016-04-07 12:45:37 +00001211/// Parses simple expression in parens for single-expression clauses of OpenMP
1212/// constructs.
1213/// \param RLoc Returned location of right paren.
1214ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1215 SourceLocation &RLoc) {
1216 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1217 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1218 return ExprError();
1219
1220 SourceLocation ELoc = Tok.getLocation();
1221 ExprResult LHS(ParseCastExpression(
1222 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1223 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1224 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1225
1226 // Parse ')'.
1227 T.consumeClose();
1228
1229 RLoc = T.getCloseLocation();
1230 return Val;
1231}
1232
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001233/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001234/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001235/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001236///
Alexey Bataev3778b602014-07-17 07:32:53 +00001237/// final-clause:
1238/// 'final' '(' expression ')'
1239///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001240/// num_threads-clause:
1241/// 'num_threads' '(' expression ')'
1242///
1243/// safelen-clause:
1244/// 'safelen' '(' expression ')'
1245///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001246/// simdlen-clause:
1247/// 'simdlen' '(' expression ')'
1248///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001249/// collapse-clause:
1250/// 'collapse' '(' expression ')'
1251///
Alexey Bataeva0569352015-12-01 10:17:31 +00001252/// priority-clause:
1253/// 'priority' '(' expression ')'
1254///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001255/// grainsize-clause:
1256/// 'grainsize' '(' expression ')'
1257///
Alexey Bataev382967a2015-12-08 12:06:20 +00001258/// num_tasks-clause:
1259/// 'num_tasks' '(' expression ')'
1260///
Alexey Bataev28c75412015-12-15 08:19:24 +00001261/// hint-clause:
1262/// 'hint' '(' expression ')'
1263///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001264OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1265 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001266 SourceLocation LLoc = Tok.getLocation();
1267 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001268
Alexey Bataev2af33e32016-04-07 12:45:37 +00001269 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001270
1271 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001272 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001273
Alexey Bataev2af33e32016-04-07 12:45:37 +00001274 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001275}
1276
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001277/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001278///
1279/// default-clause:
1280/// 'default' '(' 'none' | 'shared' ')
1281///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001282/// proc_bind-clause:
1283/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1284///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001285OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1286 SourceLocation Loc = Tok.getLocation();
1287 SourceLocation LOpen = ConsumeToken();
1288 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001289 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001290 if (T.expectAndConsume(diag::err_expected_lparen_after,
1291 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001292 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001293
Alexey Bataeva55ed262014-05-28 06:15:33 +00001294 unsigned Type = getOpenMPSimpleClauseType(
1295 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001296 SourceLocation TypeLoc = Tok.getLocation();
1297 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1298 Tok.isNot(tok::annot_pragma_openmp_end))
1299 ConsumeAnyToken();
1300
1301 // Parse ')'.
1302 T.consumeClose();
1303
1304 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1305 Tok.getLocation());
1306}
1307
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001308/// \brief Parsing of OpenMP clauses like 'ordered'.
1309///
1310/// ordered-clause:
1311/// 'ordered'
1312///
Alexey Bataev236070f2014-06-20 11:19:47 +00001313/// nowait-clause:
1314/// 'nowait'
1315///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001316/// untied-clause:
1317/// 'untied'
1318///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001319/// mergeable-clause:
1320/// 'mergeable'
1321///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001322/// read-clause:
1323/// 'read'
1324///
Alexey Bataev346265e2015-09-25 10:37:12 +00001325/// threads-clause:
1326/// 'threads'
1327///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001328/// simd-clause:
1329/// 'simd'
1330///
Alexey Bataevb825de12015-12-07 10:51:44 +00001331/// nogroup-clause:
1332/// 'nogroup'
1333///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001334OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1335 SourceLocation Loc = Tok.getLocation();
1336 ConsumeAnyToken();
1337
1338 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1339}
1340
1341
Alexey Bataev56dafe82014-06-20 07:16:17 +00001342/// \brief Parsing of OpenMP clauses with single expressions and some additional
1343/// argument like 'schedule' or 'dist_schedule'.
1344///
1345/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001346/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1347/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001348///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001349/// if-clause:
1350/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1351///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001352/// defaultmap:
1353/// 'defaultmap' '(' modifier ':' kind ')'
1354///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001355OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1356 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001357 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001358 // Parse '('.
1359 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1360 if (T.expectAndConsume(diag::err_expected_lparen_after,
1361 getOpenMPClauseName(Kind)))
1362 return nullptr;
1363
1364 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001365 SmallVector<unsigned, 4> Arg;
1366 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001367 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001368 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1369 Arg.resize(NumberOfElements);
1370 KLoc.resize(NumberOfElements);
1371 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1372 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1373 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1374 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001375 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001376 if (KindModifier > OMPC_SCHEDULE_unknown) {
1377 // Parse 'modifier'
1378 Arg[Modifier1] = KindModifier;
1379 KLoc[Modifier1] = Tok.getLocation();
1380 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1381 Tok.isNot(tok::annot_pragma_openmp_end))
1382 ConsumeAnyToken();
1383 if (Tok.is(tok::comma)) {
1384 // Parse ',' 'modifier'
1385 ConsumeAnyToken();
1386 KindModifier = getOpenMPSimpleClauseType(
1387 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1388 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1389 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001390 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001391 KLoc[Modifier2] = Tok.getLocation();
1392 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1393 Tok.isNot(tok::annot_pragma_openmp_end))
1394 ConsumeAnyToken();
1395 }
1396 // Parse ':'
1397 if (Tok.is(tok::colon))
1398 ConsumeAnyToken();
1399 else
1400 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1401 KindModifier = getOpenMPSimpleClauseType(
1402 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1403 }
1404 Arg[ScheduleKind] = KindModifier;
1405 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001406 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1407 Tok.isNot(tok::annot_pragma_openmp_end))
1408 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001409 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1410 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1411 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001412 Tok.is(tok::comma))
1413 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001414 } else if (Kind == OMPC_dist_schedule) {
1415 Arg.push_back(getOpenMPSimpleClauseType(
1416 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1417 KLoc.push_back(Tok.getLocation());
1418 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1419 Tok.isNot(tok::annot_pragma_openmp_end))
1420 ConsumeAnyToken();
1421 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1422 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001423 } else if (Kind == OMPC_defaultmap) {
1424 // Get a defaultmap modifier
1425 Arg.push_back(getOpenMPSimpleClauseType(
1426 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1427 KLoc.push_back(Tok.getLocation());
1428 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1429 Tok.isNot(tok::annot_pragma_openmp_end))
1430 ConsumeAnyToken();
1431 // Parse ':'
1432 if (Tok.is(tok::colon))
1433 ConsumeAnyToken();
1434 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1435 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1436 // Get a defaultmap kind
1437 Arg.push_back(getOpenMPSimpleClauseType(
1438 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1439 KLoc.push_back(Tok.getLocation());
1440 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1441 Tok.isNot(tok::annot_pragma_openmp_end))
1442 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001443 } else {
1444 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001445 KLoc.push_back(Tok.getLocation());
1446 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1447 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001448 ConsumeToken();
1449 if (Tok.is(tok::colon))
1450 DelimLoc = ConsumeToken();
1451 else
1452 Diag(Tok, diag::warn_pragma_expected_colon)
1453 << "directive name modifier";
1454 }
1455 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001456
Carlo Bertollib4adf552016-01-15 18:50:31 +00001457 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1458 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1459 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001460 if (NeedAnExpression) {
1461 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001462 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1463 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001464 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001465 }
1466
1467 // Parse ')'.
1468 T.consumeClose();
1469
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001470 if (NeedAnExpression && Val.isInvalid())
1471 return nullptr;
1472
Alexey Bataev56dafe82014-06-20 07:16:17 +00001473 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001474 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001475 T.getCloseLocation());
1476}
1477
Alexey Bataevc5e02582014-06-16 07:08:35 +00001478static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1479 UnqualifiedId &ReductionId) {
1480 SourceLocation TemplateKWLoc;
1481 if (ReductionIdScopeSpec.isEmpty()) {
1482 auto OOK = OO_None;
1483 switch (P.getCurToken().getKind()) {
1484 case tok::plus:
1485 OOK = OO_Plus;
1486 break;
1487 case tok::minus:
1488 OOK = OO_Minus;
1489 break;
1490 case tok::star:
1491 OOK = OO_Star;
1492 break;
1493 case tok::amp:
1494 OOK = OO_Amp;
1495 break;
1496 case tok::pipe:
1497 OOK = OO_Pipe;
1498 break;
1499 case tok::caret:
1500 OOK = OO_Caret;
1501 break;
1502 case tok::ampamp:
1503 OOK = OO_AmpAmp;
1504 break;
1505 case tok::pipepipe:
1506 OOK = OO_PipePipe;
1507 break;
1508 default:
1509 break;
1510 }
1511 if (OOK != OO_None) {
1512 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001513 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001514 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1515 return false;
1516 }
1517 }
1518 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1519 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001520 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001521 TemplateKWLoc, ReductionId);
1522}
1523
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001524/// Parses clauses with list.
1525bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1526 OpenMPClauseKind Kind,
1527 SmallVectorImpl<Expr *> &Vars,
1528 OpenMPVarListDataTy &Data) {
1529 UnqualifiedId UnqualifiedReductionId;
1530 bool InvalidReductionId = false;
1531 bool MapTypeModifierSpecified = false;
1532
1533 // Parse '('.
1534 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1535 if (T.expectAndConsume(diag::err_expected_lparen_after,
1536 getOpenMPClauseName(Kind)))
1537 return true;
1538
1539 bool NeedRParenForLinear = false;
1540 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1541 tok::annot_pragma_openmp_end);
1542 // Handle reduction-identifier for reduction clause.
1543 if (Kind == OMPC_reduction) {
1544 ColonProtectionRAIIObject ColonRAII(*this);
1545 if (getLangOpts().CPlusPlus)
1546 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1547 /*ObjectType=*/nullptr,
1548 /*EnteringContext=*/false);
1549 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1550 UnqualifiedReductionId);
1551 if (InvalidReductionId) {
1552 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1553 StopBeforeMatch);
1554 }
1555 if (Tok.is(tok::colon))
1556 Data.ColonLoc = ConsumeToken();
1557 else
1558 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1559 if (!InvalidReductionId)
1560 Data.ReductionId =
1561 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1562 } else if (Kind == OMPC_depend) {
1563 // Handle dependency type for depend clause.
1564 ColonProtectionRAIIObject ColonRAII(*this);
1565 Data.DepKind =
1566 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1567 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1568 Data.DepLinMapLoc = Tok.getLocation();
1569
1570 if (Data.DepKind == OMPC_DEPEND_unknown) {
1571 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1572 StopBeforeMatch);
1573 } else {
1574 ConsumeToken();
1575 // Special processing for depend(source) clause.
1576 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1577 // Parse ')'.
1578 T.consumeClose();
1579 return false;
1580 }
1581 }
1582 if (Tok.is(tok::colon))
1583 Data.ColonLoc = ConsumeToken();
1584 else {
1585 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1586 : diag::warn_pragma_expected_colon)
1587 << "dependency type";
1588 }
1589 } else if (Kind == OMPC_linear) {
1590 // Try to parse modifier if any.
1591 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1592 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1593 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1594 Data.DepLinMapLoc = ConsumeToken();
1595 LinearT.consumeOpen();
1596 NeedRParenForLinear = true;
1597 }
1598 } else if (Kind == OMPC_map) {
1599 // Handle map type for map clause.
1600 ColonProtectionRAIIObject ColonRAII(*this);
1601
1602 /// The map clause modifier token can be either a identifier or the C++
1603 /// delete keyword.
1604 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1605 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1606 };
1607
1608 // The first identifier may be a list item, a map-type or a
1609 // map-type-modifier. The map modifier can also be delete which has the same
1610 // spelling of the C++ delete keyword.
1611 Data.MapType =
1612 IsMapClauseModifierToken(Tok)
1613 ? static_cast<OpenMPMapClauseKind>(
1614 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1615 : OMPC_MAP_unknown;
1616 Data.DepLinMapLoc = Tok.getLocation();
1617 bool ColonExpected = false;
1618
1619 if (IsMapClauseModifierToken(Tok)) {
1620 if (PP.LookAhead(0).is(tok::colon)) {
1621 if (Data.MapType == OMPC_MAP_unknown)
1622 Diag(Tok, diag::err_omp_unknown_map_type);
1623 else if (Data.MapType == OMPC_MAP_always)
1624 Diag(Tok, diag::err_omp_map_type_missing);
1625 ConsumeToken();
1626 } else if (PP.LookAhead(0).is(tok::comma)) {
1627 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1628 PP.LookAhead(2).is(tok::colon)) {
1629 Data.MapTypeModifier = Data.MapType;
1630 if (Data.MapTypeModifier != OMPC_MAP_always) {
1631 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1632 Data.MapTypeModifier = OMPC_MAP_unknown;
1633 } else
1634 MapTypeModifierSpecified = true;
1635
1636 ConsumeToken();
1637 ConsumeToken();
1638
1639 Data.MapType =
1640 IsMapClauseModifierToken(Tok)
1641 ? static_cast<OpenMPMapClauseKind>(
1642 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1643 : OMPC_MAP_unknown;
1644 if (Data.MapType == OMPC_MAP_unknown ||
1645 Data.MapType == OMPC_MAP_always)
1646 Diag(Tok, diag::err_omp_unknown_map_type);
1647 ConsumeToken();
1648 } else {
1649 Data.MapType = OMPC_MAP_tofrom;
1650 Data.IsMapTypeImplicit = true;
1651 }
1652 } else {
1653 Data.MapType = OMPC_MAP_tofrom;
1654 Data.IsMapTypeImplicit = true;
1655 }
1656 } else {
1657 Data.MapType = OMPC_MAP_tofrom;
1658 Data.IsMapTypeImplicit = true;
1659 }
1660
1661 if (Tok.is(tok::colon))
1662 Data.ColonLoc = ConsumeToken();
1663 else if (ColonExpected)
1664 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1665 }
1666
1667 bool IsComma =
1668 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1669 (Kind == OMPC_reduction && !InvalidReductionId) ||
1670 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1671 (!MapTypeModifierSpecified ||
1672 Data.MapTypeModifier == OMPC_MAP_always)) ||
1673 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1674 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1675 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1676 Tok.isNot(tok::annot_pragma_openmp_end))) {
1677 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1678 // Parse variable
1679 ExprResult VarExpr =
1680 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1681 if (VarExpr.isUsable())
1682 Vars.push_back(VarExpr.get());
1683 else {
1684 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1685 StopBeforeMatch);
1686 }
1687 // Skip ',' if any
1688 IsComma = Tok.is(tok::comma);
1689 if (IsComma)
1690 ConsumeToken();
1691 else if (Tok.isNot(tok::r_paren) &&
1692 Tok.isNot(tok::annot_pragma_openmp_end) &&
1693 (!MayHaveTail || Tok.isNot(tok::colon)))
1694 Diag(Tok, diag::err_omp_expected_punc)
1695 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1696 : getOpenMPClauseName(Kind))
1697 << (Kind == OMPC_flush);
1698 }
1699
1700 // Parse ')' for linear clause with modifier.
1701 if (NeedRParenForLinear)
1702 LinearT.consumeClose();
1703
1704 // Parse ':' linear-step (or ':' alignment).
1705 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1706 if (MustHaveTail) {
1707 Data.ColonLoc = Tok.getLocation();
1708 SourceLocation ELoc = ConsumeToken();
1709 ExprResult Tail = ParseAssignmentExpression();
1710 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1711 if (Tail.isUsable())
1712 Data.TailExpr = Tail.get();
1713 else
1714 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1715 StopBeforeMatch);
1716 }
1717
1718 // Parse ')'.
1719 T.consumeClose();
1720 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1721 Vars.empty()) ||
1722 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1723 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1724 return true;
1725 return false;
1726}
1727
Alexander Musman1bb328c2014-06-04 13:06:39 +00001728/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001729/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001730///
1731/// private-clause:
1732/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001733/// firstprivate-clause:
1734/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001735/// lastprivate-clause:
1736/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001737/// shared-clause:
1738/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001739/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001740/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001741/// aligned-clause:
1742/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001743/// reduction-clause:
1744/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001745/// copyprivate-clause:
1746/// 'copyprivate' '(' list ')'
1747/// flush-clause:
1748/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001749/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001750/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001751/// map-clause:
1752/// 'map' '(' [ [ always , ]
1753/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001754/// to-clause:
1755/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001756/// from-clause:
1757/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001758/// use_device_ptr-clause:
1759/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001760/// is_device_ptr-clause:
1761/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001762///
Alexey Bataev182227b2015-08-20 10:54:39 +00001763/// For 'linear' clause linear-list may have the following forms:
1764/// list
1765/// modifier(list)
1766/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001767OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1768 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001769 SourceLocation Loc = Tok.getLocation();
1770 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001771 SmallVector<Expr *, 4> Vars;
1772 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001773
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001774 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001775 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001776
Alexey Bataevc5e02582014-06-16 07:08:35 +00001777 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001778 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1779 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1780 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1781 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001782}
1783