blob: 4f418e3946d6be7f207d35cb892c624e2194430f [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements parsing of all OpenMP directives and clauses.
11///
12//===----------------------------------------------------------------------===//
13
Chandler Carruth5553d0d2014-01-07 11:51:46 +000014#include "RAIIObjectsForParser.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000016#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000017#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000018#include "clang/Parse/Parser.h"
19#include "clang/Sema/Scope.h"
20#include "llvm/ADT/PointerIntPair.h"
Michael Wong65f367f2015-07-21 13:44:28 +000021
Alexey Bataeva769e072013-03-22 06:34:35 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// OpenMP declarative directives.
26//===----------------------------------------------------------------------===//
27
Dmitry Polukhin82478332016-02-13 06:53:38 +000028namespace {
29enum OpenMPDirectiveKindEx {
30 OMPD_cancellation = OMPD_unknown + 1,
31 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000032 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000033 OMPD_end,
34 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000035 OMPD_enter,
36 OMPD_exit,
37 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000038 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000039 OMPD_target_enter,
Samuel Antao686c70c2016-05-26 17:30:50 +000040 OMPD_target_exit,
41 OMPD_update,
Carlo Bertolli9925f152016-06-27 14:55:37 +000042 OMPD_distribute_parallel
Dmitry Polukhin82478332016-02-13 06:53:38 +000043};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000044
45class ThreadprivateListParserHelper final {
46 SmallVector<Expr *, 4> Identifiers;
47 Parser *P;
48
49public:
50 ThreadprivateListParserHelper(Parser *P) : P(P) {}
51 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
52 ExprResult Res =
53 P->getActions().ActOnOpenMPIdExpression(P->getCurScope(), SS, NameInfo);
54 if (Res.isUsable())
55 Identifiers.push_back(Res.get());
56 }
57 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
58};
Dmitry Polukhin82478332016-02-13 06:53:38 +000059} // namespace
60
61// Map token string to extended OMP token kind that are
62// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
63static unsigned getOpenMPDirectiveKindEx(StringRef S) {
64 auto DKind = getOpenMPDirectiveKind(S);
65 if (DKind != OMPD_unknown)
66 return DKind;
67
68 return llvm::StringSwitch<unsigned>(S)
69 .Case("cancellation", OMPD_cancellation)
70 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000071 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000072 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000073 .Case("enter", OMPD_enter)
74 .Case("exit", OMPD_exit)
75 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000076 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000077 .Case("update", OMPD_update)
Dmitry Polukhin82478332016-02-13 06:53:38 +000078 .Default(OMPD_unknown);
79}
80
Alexey Bataev4acb8592014-07-07 13:01:15 +000081static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000082 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
83 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
84 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000085 static const unsigned F[][3] = {
86 { OMPD_cancellation, OMPD_point, OMPD_cancellation_point },
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000087 { OMPD_declare, OMPD_reduction, OMPD_declare_reduction },
Alexey Bataev587e1de2016-03-30 10:43:55 +000088 { OMPD_declare, OMPD_simd, OMPD_declare_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000089 { OMPD_declare, OMPD_target, OMPD_declare_target },
Carlo Bertolli9925f152016-06-27 14:55:37 +000090 { OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel },
91 { OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for },
Kelvin Li4a39add2016-07-05 05:00:15 +000092 { OMPD_distribute_parallel_for, OMPD_simd,
93 OMPD_distribute_parallel_for_simd },
Kelvin Li787f3fc2016-07-06 04:45:38 +000094 { OMPD_distribute, OMPD_simd, OMPD_distribute_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000095 { OMPD_end, OMPD_declare, OMPD_end_declare },
96 { OMPD_end_declare, OMPD_target, OMPD_end_declare_target },
Dmitry Polukhin82478332016-02-13 06:53:38 +000097 { OMPD_target, OMPD_data, OMPD_target_data },
98 { OMPD_target, OMPD_enter, OMPD_target_enter },
99 { OMPD_target, OMPD_exit, OMPD_target_exit },
Samuel Antao686c70c2016-05-26 17:30:50 +0000100 { OMPD_target, OMPD_update, OMPD_target_update },
Dmitry Polukhin82478332016-02-13 06:53:38 +0000101 { OMPD_target_enter, OMPD_data, OMPD_target_enter_data },
102 { OMPD_target_exit, OMPD_data, OMPD_target_exit_data },
103 { OMPD_for, OMPD_simd, OMPD_for_simd },
104 { OMPD_parallel, OMPD_for, OMPD_parallel_for },
105 { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
106 { OMPD_parallel, OMPD_sections, OMPD_parallel_sections },
107 { OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd },
108 { OMPD_target, OMPD_parallel, OMPD_target_parallel },
Kelvin Li986330c2016-07-20 22:57:10 +0000109 { OMPD_target, OMPD_simd, OMPD_target_simd },
Kelvin Lia579b912016-07-14 02:54:56 +0000110 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for },
Kelvin Li02532872016-08-05 14:37:37 +0000111 { OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd },
Kelvin Li4e325f72016-10-25 12:50:55 +0000112 { OMPD_teams, OMPD_distribute, OMPD_teams_distribute },
113 { OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd }
Dmitry Polukhin82478332016-02-13 06:53:38 +0000114 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000115 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +0000116 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000117 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000118 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000119 ? static_cast<unsigned>(OMPD_unknown)
120 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
121 if (DKind == OMPD_unknown)
122 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000123
Alexander Musmanf82886e2014-09-18 05:12:34 +0000124 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000125 if (DKind != F[i][0])
126 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000127
Dmitry Polukhin82478332016-02-13 06:53:38 +0000128 Tok = P.getPreprocessor().LookAhead(0);
129 unsigned SDKind =
130 Tok.isAnnotation()
131 ? static_cast<unsigned>(OMPD_unknown)
132 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
133 if (SDKind == OMPD_unknown)
134 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000135
Dmitry Polukhin82478332016-02-13 06:53:38 +0000136 if (SDKind == F[i][1]) {
137 P.ConsumeToken();
138 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000139 }
140 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000141 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
142 : OMPD_unknown;
143}
144
145static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000146 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000147 Sema &Actions = P.getActions();
148 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000149 // Allow to use 'operator' keyword for C++ operators
150 bool WithOperator = false;
151 if (Tok.is(tok::kw_operator)) {
152 P.ConsumeToken();
153 Tok = P.getCurToken();
154 WithOperator = true;
155 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000156 switch (Tok.getKind()) {
157 case tok::plus: // '+'
158 OOK = OO_Plus;
159 break;
160 case tok::minus: // '-'
161 OOK = OO_Minus;
162 break;
163 case tok::star: // '*'
164 OOK = OO_Star;
165 break;
166 case tok::amp: // '&'
167 OOK = OO_Amp;
168 break;
169 case tok::pipe: // '|'
170 OOK = OO_Pipe;
171 break;
172 case tok::caret: // '^'
173 OOK = OO_Caret;
174 break;
175 case tok::ampamp: // '&&'
176 OOK = OO_AmpAmp;
177 break;
178 case tok::pipepipe: // '||'
179 OOK = OO_PipePipe;
180 break;
181 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000182 if (!WithOperator)
183 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000184 default:
185 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
186 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
187 Parser::StopBeforeMatch);
188 return DeclarationName();
189 }
190 P.ConsumeToken();
191 auto &DeclNames = Actions.getASTContext().DeclarationNames;
192 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
193 : DeclNames.getCXXOperatorName(OOK);
194}
195
196/// \brief Parse 'omp declare reduction' construct.
197///
198/// declare-reduction-directive:
199/// annot_pragma_openmp 'declare' 'reduction'
200/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
201/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
202/// annot_pragma_openmp_end
203/// <reduction_id> is either a base language identifier or one of the following
204/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
205///
206Parser::DeclGroupPtrTy
207Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
208 // Parse '('.
209 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
210 if (T.expectAndConsume(diag::err_expected_lparen_after,
211 getOpenMPDirectiveName(OMPD_declare_reduction))) {
212 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
213 return DeclGroupPtrTy();
214 }
215
216 DeclarationName Name = parseOpenMPReductionId(*this);
217 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
218 return DeclGroupPtrTy();
219
220 // Consume ':'.
221 bool IsCorrect = !ExpectAndConsume(tok::colon);
222
223 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
224 return DeclGroupPtrTy();
225
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000226 IsCorrect = IsCorrect && !Name.isEmpty();
227
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000228 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
229 Diag(Tok.getLocation(), diag::err_expected_type);
230 IsCorrect = false;
231 }
232
233 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
234 return DeclGroupPtrTy();
235
236 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
237 // Parse list of types until ':' token.
238 do {
239 ColonProtectionRAIIObject ColonRAII(*this);
240 SourceRange Range;
241 TypeResult TR = ParseTypeName(&Range, Declarator::PrototypeContext, AS);
242 if (TR.isUsable()) {
243 auto ReductionType =
244 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
245 if (!ReductionType.isNull()) {
246 ReductionTypes.push_back(
247 std::make_pair(ReductionType, Range.getBegin()));
248 }
249 } else {
250 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
251 StopBeforeMatch);
252 }
253
254 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
255 break;
256
257 // Consume ','.
258 if (ExpectAndConsume(tok::comma)) {
259 IsCorrect = false;
260 if (Tok.is(tok::annot_pragma_openmp_end)) {
261 Diag(Tok.getLocation(), diag::err_expected_type);
262 return DeclGroupPtrTy();
263 }
264 }
265 } while (Tok.isNot(tok::annot_pragma_openmp_end));
266
267 if (ReductionTypes.empty()) {
268 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
269 return DeclGroupPtrTy();
270 }
271
272 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
273 return DeclGroupPtrTy();
274
275 // Consume ':'.
276 if (ExpectAndConsume(tok::colon))
277 IsCorrect = false;
278
279 if (Tok.is(tok::annot_pragma_openmp_end)) {
280 Diag(Tok.getLocation(), diag::err_expected_expression);
281 return DeclGroupPtrTy();
282 }
283
284 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
285 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
286
287 // Parse <combiner> expression and then parse initializer if any for each
288 // correct type.
289 unsigned I = 0, E = ReductionTypes.size();
290 for (auto *D : DRD.get()) {
291 TentativeParsingAction TPA(*this);
292 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
293 Scope::OpenMPDirectiveScope);
294 // Parse <combiner> expression.
295 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
296 ExprResult CombinerResult =
297 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
298 D->getLocation(), /*DiscardedValue=*/true);
299 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
300
301 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
302 Tok.isNot(tok::annot_pragma_openmp_end)) {
303 TPA.Commit();
304 IsCorrect = false;
305 break;
306 }
307 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
308 ExprResult InitializerResult;
309 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
310 // Parse <initializer> expression.
311 if (Tok.is(tok::identifier) &&
312 Tok.getIdentifierInfo()->isStr("initializer"))
313 ConsumeToken();
314 else {
315 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
316 TPA.Commit();
317 IsCorrect = false;
318 break;
319 }
320 // Parse '('.
321 BalancedDelimiterTracker T(*this, tok::l_paren,
322 tok::annot_pragma_openmp_end);
323 IsCorrect =
324 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
325 IsCorrect;
326 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
327 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
328 Scope::OpenMPDirectiveScope);
329 // Parse expression.
330 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), D);
331 InitializerResult = Actions.ActOnFinishFullExpr(
332 ParseAssignmentExpression().get(), D->getLocation(),
333 /*DiscardedValue=*/true);
334 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
335 D, InitializerResult.get());
336 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
337 Tok.isNot(tok::annot_pragma_openmp_end)) {
338 TPA.Commit();
339 IsCorrect = false;
340 break;
341 }
342 IsCorrect =
343 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
344 }
345 }
346
347 ++I;
348 // Revert parsing if not the last type, otherwise accept it, we're done with
349 // parsing.
350 if (I != E)
351 TPA.Revert();
352 else
353 TPA.Commit();
354 }
355 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
356 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000357}
358
Alexey Bataev2af33e32016-04-07 12:45:37 +0000359namespace {
360/// RAII that recreates function context for correct parsing of clauses of
361/// 'declare simd' construct.
362/// OpenMP, 2.8.2 declare simd Construct
363/// The expressions appearing in the clauses of this directive are evaluated in
364/// the scope of the arguments of the function declaration or definition.
365class FNContextRAII final {
366 Parser &P;
367 Sema::CXXThisScopeRAII *ThisScope;
368 Parser::ParseScope *TempScope;
369 Parser::ParseScope *FnScope;
370 bool HasTemplateScope = false;
371 bool HasFunScope = false;
372 FNContextRAII() = delete;
373 FNContextRAII(const FNContextRAII &) = delete;
374 FNContextRAII &operator=(const FNContextRAII &) = delete;
375
376public:
377 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
378 Decl *D = *Ptr.get().begin();
379 NamedDecl *ND = dyn_cast<NamedDecl>(D);
380 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
381 Sema &Actions = P.getActions();
382
383 // Allow 'this' within late-parsed attributes.
384 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
385 ND && ND->isCXXInstanceMember());
386
387 // If the Decl is templatized, add template parameters to scope.
388 HasTemplateScope = D->isTemplateDecl();
389 TempScope =
390 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
391 if (HasTemplateScope)
392 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
393
394 // If the Decl is on a function, add function parameters to the scope.
395 HasFunScope = D->isFunctionOrFunctionTemplate();
396 FnScope = new Parser::ParseScope(&P, Scope::FnScope | Scope::DeclScope,
397 HasFunScope);
398 if (HasFunScope)
399 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
400 }
401 ~FNContextRAII() {
402 if (HasFunScope) {
403 P.getActions().ActOnExitFunctionContext();
404 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
405 }
406 if (HasTemplateScope)
407 TempScope->Exit();
408 delete FnScope;
409 delete TempScope;
410 delete ThisScope;
411 }
412};
413} // namespace
414
Alexey Bataevd93d3762016-04-12 09:35:56 +0000415/// Parses clauses for 'declare simd' directive.
416/// clause:
417/// 'inbranch' | 'notinbranch'
418/// 'simdlen' '(' <expr> ')'
419/// { 'uniform' '(' <argument_list> ')' }
420/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000421/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
422static bool parseDeclareSimdClauses(
423 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
424 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
425 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
426 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000427 SourceRange BSRange;
428 const Token &Tok = P.getCurToken();
429 bool IsError = false;
430 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
431 if (Tok.isNot(tok::identifier))
432 break;
433 OMPDeclareSimdDeclAttr::BranchStateTy Out;
434 IdentifierInfo *II = Tok.getIdentifierInfo();
435 StringRef ClauseName = II->getName();
436 // Parse 'inranch|notinbranch' clauses.
437 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
438 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
439 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
440 << ClauseName
441 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
442 IsError = true;
443 }
444 BS = Out;
445 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
446 P.ConsumeToken();
447 } else if (ClauseName.equals("simdlen")) {
448 if (SimdLen.isUsable()) {
449 P.Diag(Tok, diag::err_omp_more_one_clause)
450 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
451 IsError = true;
452 }
453 P.ConsumeToken();
454 SourceLocation RLoc;
455 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
456 if (SimdLen.isInvalid())
457 IsError = true;
458 } else {
459 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000460 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
461 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000462 Parser::OpenMPVarListDataTy Data;
463 auto *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000464 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000465 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000466 else if (CKind == OMPC_linear)
467 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000468
469 P.ConsumeToken();
470 if (P.ParseOpenMPVarList(OMPD_declare_simd,
471 getOpenMPClauseKind(ClauseName), *Vars, Data))
472 IsError = true;
473 if (CKind == OMPC_aligned)
474 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000475 else if (CKind == OMPC_linear) {
476 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
477 Data.DepLinMapLoc))
478 Data.LinKind = OMPC_LINEAR_val;
479 LinModifiers.append(Linears.size() - LinModifiers.size(),
480 Data.LinKind);
481 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
482 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000483 } else
484 // TODO: add parsing of other clauses.
485 break;
486 }
487 // Skip ',' if any.
488 if (Tok.is(tok::comma))
489 P.ConsumeToken();
490 }
491 return IsError;
492}
493
Alexey Bataev2af33e32016-04-07 12:45:37 +0000494/// Parse clauses for '#pragma omp declare simd'.
495Parser::DeclGroupPtrTy
496Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
497 CachedTokens &Toks, SourceLocation Loc) {
498 PP.EnterToken(Tok);
499 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
500 // Consume the previously pushed token.
501 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
502
503 FNContextRAII FnContext(*this, Ptr);
504 OMPDeclareSimdDeclAttr::BranchStateTy BS =
505 OMPDeclareSimdDeclAttr::BS_Undefined;
506 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000507 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000508 SmallVector<Expr *, 4> Aligneds;
509 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000510 SmallVector<Expr *, 4> Linears;
511 SmallVector<unsigned, 4> LinModifiers;
512 SmallVector<Expr *, 4> Steps;
513 bool IsError =
514 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
515 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000516 // Need to check for extra tokens.
517 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
518 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
519 << getOpenMPDirectiveName(OMPD_declare_simd);
520 while (Tok.isNot(tok::annot_pragma_openmp_end))
521 ConsumeAnyToken();
522 }
523 // Skip the last annot_pragma_openmp_end.
524 SourceLocation EndLoc = ConsumeToken();
Alexey Bataevd93d3762016-04-12 09:35:56 +0000525 if (!IsError) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000526 return Actions.ActOnOpenMPDeclareSimdDirective(
Alexey Bataevecba70f2016-04-12 11:02:11 +0000527 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
528 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataevd93d3762016-04-12 09:35:56 +0000529 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000530 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000531}
532
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000533/// \brief Parsing of declarative OpenMP directives.
534///
535/// threadprivate-directive:
536/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000537/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000538///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000539/// declare-reduction-directive:
540/// annot_pragma_openmp 'declare' 'reduction' [...]
541/// annot_pragma_openmp_end
542///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000543/// declare-simd-directive:
544/// annot_pragma_openmp 'declare simd' {<clause> [,]}
545/// annot_pragma_openmp_end
546/// <function declaration/definition>
547///
548Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
549 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
550 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000551 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000552 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000553
554 SourceLocation Loc = ConsumeToken();
Alexey Bataev4acb8592014-07-07 13:01:15 +0000555 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000556
557 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000558 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000559 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000560 ThreadprivateListParserHelper Helper(this);
561 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000562 // The last seen token is annot_pragma_openmp_end - need to check for
563 // extra tokens.
564 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
565 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000566 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000567 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000568 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000569 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000570 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000571 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
572 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000573 }
574 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000575 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000576 case OMPD_declare_reduction:
577 ConsumeToken();
578 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
579 // The last seen token is annot_pragma_openmp_end - need to check for
580 // extra tokens.
581 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
582 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
583 << getOpenMPDirectiveName(OMPD_declare_reduction);
584 while (Tok.isNot(tok::annot_pragma_openmp_end))
585 ConsumeAnyToken();
586 }
587 // Skip the last annot_pragma_openmp_end.
588 ConsumeToken();
589 return Res;
590 }
591 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000592 case OMPD_declare_simd: {
593 // The syntax is:
594 // { #pragma omp declare simd }
595 // <function-declaration-or-definition>
596 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000597 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000598 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000599 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
600 Toks.push_back(Tok);
601 ConsumeAnyToken();
602 }
603 Toks.push_back(Tok);
604 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000605
606 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000607 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000608 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000609 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000610 // Here we expect to see some function declaration.
611 if (AS == AS_none) {
612 assert(TagType == DeclSpec::TST_unspecified);
613 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000614 ParsingDeclSpec PDS(*this);
615 Ptr = ParseExternalDeclaration(Attrs, &PDS);
616 } else {
617 Ptr =
618 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
619 }
620 }
621 if (!Ptr) {
622 Diag(Loc, diag::err_omp_decl_in_declare_simd);
623 return DeclGroupPtrTy();
624 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000625 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000626 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000627 case OMPD_declare_target: {
628 SourceLocation DTLoc = ConsumeAnyToken();
629 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000630 // OpenMP 4.5 syntax with list of entities.
631 llvm::SmallSetVector<const NamedDecl*, 16> SameDirectiveDecls;
632 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
633 OMPDeclareTargetDeclAttr::MapTypeTy MT =
634 OMPDeclareTargetDeclAttr::MT_To;
635 if (Tok.is(tok::identifier)) {
636 IdentifierInfo *II = Tok.getIdentifierInfo();
637 StringRef ClauseName = II->getName();
638 // Parse 'to|link' clauses.
639 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
640 MT)) {
641 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
642 << ClauseName;
643 break;
644 }
645 ConsumeToken();
646 }
647 auto Callback = [this, MT, &SameDirectiveDecls](
648 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
649 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
650 SameDirectiveDecls);
651 };
652 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, true))
653 break;
654
655 // Consume optional ','.
656 if (Tok.is(tok::comma))
657 ConsumeToken();
658 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000659 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000660 ConsumeAnyToken();
661 return DeclGroupPtrTy();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000662 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000663
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000664 // Skip the last annot_pragma_openmp_end.
665 ConsumeAnyToken();
666
667 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
668 return DeclGroupPtrTy();
669
670 DKind = ParseOpenMPDirectiveKind(*this);
671 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
672 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
673 ParsedAttributesWithRange attrs(AttrFactory);
674 MaybeParseCXX11Attributes(attrs);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000675 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:
Kelvin Li02532872016-08-05 14:37:37 +0000745 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000746 case OMPD_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000747 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000748 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000749 break;
750 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000751 while (Tok.isNot(tok::annot_pragma_openmp_end))
752 ConsumeAnyToken();
753 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000754 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000755}
756
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000757/// \brief Parsing of declarative or executable OpenMP directives.
758///
759/// threadprivate-directive:
760/// annot_pragma_openmp 'threadprivate' simple-variable-list
761/// annot_pragma_openmp_end
762///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000763/// declare-reduction-directive:
764/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
765/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
766/// ('omp_priv' '=' <expression>|<function_call>) ')']
767/// annot_pragma_openmp_end
768///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000769/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000770/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000771/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
772/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000773/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000774/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000775/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000776/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000777/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000778/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000779/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000780/// 'target parallel for simd' | 'target simd' |
Kelvin Li4e325f72016-10-25 12:50:55 +0000781/// 'teams distribute' | 'teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000782/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000783///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000784StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
785 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000786 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000787 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000788 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000789 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000790 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000791 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000792 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000793 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000794 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000795 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000796 // Name of critical directive.
797 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000798 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000799 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000800 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000801
802 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000803 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000804 if (Allowed != ACK_Any) {
805 Diag(Tok, diag::err_omp_immediate_directive)
806 << getOpenMPDirectiveName(DKind) << 0;
807 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000808 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000809 ThreadprivateListParserHelper Helper(this);
810 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000811 // The last seen token is annot_pragma_openmp_end - need to check for
812 // extra tokens.
813 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
814 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000815 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000816 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000817 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000818 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
819 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000820 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
821 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000822 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000823 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000824 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000825 case OMPD_declare_reduction:
826 ConsumeToken();
827 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
828 // The last seen token is annot_pragma_openmp_end - need to check for
829 // extra tokens.
830 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
831 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
832 << getOpenMPDirectiveName(OMPD_declare_reduction);
833 while (Tok.isNot(tok::annot_pragma_openmp_end))
834 ConsumeAnyToken();
835 }
836 ConsumeAnyToken();
837 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
838 } else
839 SkipUntil(tok::annot_pragma_openmp_end);
840 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000841 case OMPD_flush:
842 if (PP.LookAhead(0).is(tok::l_paren)) {
843 FlushHasClause = true;
844 // Push copy of the current token back to stream to properly parse
845 // pseudo-clause OMPFlushClause.
846 PP.EnterToken(Tok);
847 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000848 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000849 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000850 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000851 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000852 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000853 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000854 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000855 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000856 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000857 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000858 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000859 }
860 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000861 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000862 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000863 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000864 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000865 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000866 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000867 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000868 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000869 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000870 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000871 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000872 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000873 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000874 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000875 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000876 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000877 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000878 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000879 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000880 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000881 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000882 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000883 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000884 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000885 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +0000886 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000887 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000888 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000889 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000890 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +0000891 case OMPD_teams_distribute:
892 case OMPD_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000893 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000894 // Parse directive name of the 'critical' directive if any.
895 if (DKind == OMPD_critical) {
896 BalancedDelimiterTracker T(*this, tok::l_paren,
897 tok::annot_pragma_openmp_end);
898 if (!T.consumeOpen()) {
899 if (Tok.isAnyIdentifier()) {
900 DirName =
901 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
902 ConsumeAnyToken();
903 } else {
904 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
905 }
906 T.consumeClose();
907 }
Alexey Bataev80909872015-07-02 11:25:17 +0000908 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000909 CancelRegion = ParseOpenMPDirectiveKind(*this);
910 if (Tok.isNot(tok::annot_pragma_openmp_end))
911 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000912 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000913
Alexey Bataevf29276e2014-06-18 04:14:57 +0000914 if (isOpenMPLoopDirective(DKind))
915 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
916 if (isOpenMPSimdDirective(DKind))
917 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
918 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000919 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000920
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000921 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000922 OpenMPClauseKind CKind =
923 Tok.isAnnotation()
924 ? OMPC_unknown
925 : FlushHasClause ? OMPC_flush
926 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000927 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000928 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000929 OMPClause *Clause =
930 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000931 FirstClauses[CKind].setInt(true);
932 if (Clause) {
933 FirstClauses[CKind].setPointer(Clause);
934 Clauses.push_back(Clause);
935 }
936
937 // Skip ',' if any.
938 if (Tok.is(tok::comma))
939 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000940 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000941 }
942 // End location of the directive.
943 EndLoc = Tok.getLocation();
944 // Consume final annot_pragma_openmp_end.
945 ConsumeToken();
946
Alexey Bataeveb482352015-12-18 05:05:56 +0000947 // OpenMP [2.13.8, ordered Construct, Syntax]
948 // If the depend clause is specified, the ordered construct is a stand-alone
949 // directive.
950 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000951 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000952 Diag(Loc, diag::err_omp_immediate_directive)
953 << getOpenMPDirectiveName(DKind) << 1
954 << getOpenMPClauseName(OMPC_depend);
955 }
956 HasAssociatedStatement = false;
957 }
958
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000959 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000960 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000961 // The body is a block scope like in Lambdas and Blocks.
962 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000963 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000964 Actions.ActOnStartOfCompoundStmt();
965 // Parse statement
966 AssociatedStmt = ParseStatement();
967 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000968 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000969 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000970 Directive = Actions.ActOnOpenMPExecutableDirective(
971 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
972 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000973
974 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000975 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000976 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000977 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000978 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000979 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000980 case OMPD_declare_target:
981 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +0000982 Diag(Tok, diag::err_omp_unexpected_directive)
983 << getOpenMPDirectiveName(DKind);
984 SkipUntil(tok::annot_pragma_openmp_end);
985 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000986 case OMPD_unknown:
987 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000988 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000989 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000990 }
991 return Directive;
992}
993
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000994// Parses simple list:
995// simple-variable-list:
996// '(' id-expression {, id-expression} ')'
997//
998bool Parser::ParseOpenMPSimpleVarList(
999 OpenMPDirectiveKind Kind,
1000 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1001 Callback,
1002 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001003 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001004 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001005 if (T.expectAndConsume(diag::err_expected_lparen_after,
1006 getOpenMPDirectiveName(Kind)))
1007 return true;
1008 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001009 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001010
1011 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001012 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001013 CXXScopeSpec SS;
1014 SourceLocation TemplateKWLoc;
1015 UnqualifiedId Name;
1016 // Read var name.
1017 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001018 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001019
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001020 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001021 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
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);
David Blaikieefdccaa2016-01-15 23:43:34 +00001025 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001026 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001027 IsCorrect = false;
1028 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001029 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001030 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1031 Tok.isNot(tok::annot_pragma_openmp_end)) {
1032 IsCorrect = false;
1033 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001034 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001035 Diag(PrevTok.getLocation(), diag::err_expected)
1036 << tok::identifier
1037 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001038 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001039 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001040 }
1041 // Consume ','.
1042 if (Tok.is(tok::comma)) {
1043 ConsumeToken();
1044 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001045 }
1046
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001047 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001048 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001049 IsCorrect = false;
1050 }
1051
1052 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001053 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001054
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001055 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001056}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001057
1058/// \brief Parsing of OpenMP clauses.
1059///
1060/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001061/// if-clause | final-clause | num_threads-clause | safelen-clause |
1062/// default-clause | private-clause | firstprivate-clause | shared-clause
1063/// | linear-clause | aligned-clause | collapse-clause |
1064/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001065/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001066/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001067/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001068/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001069/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001070/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Carlo Bertolli70594e92016-07-13 17:16:49 +00001071/// from-clause | is_device_ptr-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001072///
1073OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1074 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001075 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001076 bool ErrorFound = false;
1077 // Check if clause is allowed for the given directive.
1078 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001079 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1080 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001081 ErrorFound = true;
1082 }
1083
1084 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001085 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001086 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001087 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001088 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001089 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001090 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001091 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001092 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001093 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001094 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001095 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001096 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001097 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001098 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001099 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001100 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001101 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001102 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001103 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001104 // OpenMP [2.9.1, target data construct, Restrictions]
1105 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001106 // OpenMP [2.11.1, task Construct, Restrictions]
1107 // At most one if clause can appear on the directive.
1108 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001109 // OpenMP [teams Construct, Restrictions]
1110 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001111 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001112 // OpenMP [2.9.1, task Construct, Restrictions]
1113 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001114 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1115 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001116 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1117 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001118 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001119 Diag(Tok, diag::err_omp_more_one_clause)
1120 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001121 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001122 }
1123
Alexey Bataev10e775f2015-07-30 11:36:16 +00001124 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1125 Clause = ParseOpenMPClause(CKind);
1126 else
1127 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001128 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001129 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001130 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001131 // OpenMP [2.14.3.1, Restrictions]
1132 // Only a single default clause may be specified on a parallel, task or
1133 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001134 // OpenMP [2.5, parallel Construct, Restrictions]
1135 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001136 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001137 Diag(Tok, diag::err_omp_more_one_clause)
1138 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001139 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001140 }
1141
1142 Clause = ParseOpenMPSimpleClause(CKind);
1143 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001144 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001145 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001146 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001147 // OpenMP [2.7.1, Restrictions, p. 3]
1148 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001149 // OpenMP [2.10.4, Restrictions, p. 106]
1150 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001151 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001152 Diag(Tok, diag::err_omp_more_one_clause)
1153 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001154 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001155 }
1156
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001157 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001158 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1159 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001160 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001161 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001162 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001163 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001164 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001165 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001166 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001167 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001168 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001169 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001170 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001171 // OpenMP [2.7.1, Restrictions, p. 9]
1172 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001173 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1174 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001175 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001176 Diag(Tok, diag::err_omp_more_one_clause)
1177 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001178 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001179 }
1180
1181 Clause = ParseOpenMPClause(CKind);
1182 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001183 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001184 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001185 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001186 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001187 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001188 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001189 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001190 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001191 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001192 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001193 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001194 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001195 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001196 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001197 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001198 case OMPC_is_device_ptr:
Alexey Bataeveb482352015-12-18 05:05:56 +00001199 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001200 break;
1201 case OMPC_unknown:
1202 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001203 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001204 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001205 break;
1206 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001207 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001208 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1209 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001210 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001211 break;
1212 }
Craig Topper161e4db2014-05-21 06:02:52 +00001213 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001214}
1215
Alexey Bataev2af33e32016-04-07 12:45:37 +00001216/// Parses simple expression in parens for single-expression clauses of OpenMP
1217/// constructs.
1218/// \param RLoc Returned location of right paren.
1219ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1220 SourceLocation &RLoc) {
1221 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1222 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1223 return ExprError();
1224
1225 SourceLocation ELoc = Tok.getLocation();
1226 ExprResult LHS(ParseCastExpression(
1227 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1228 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1229 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1230
1231 // Parse ')'.
1232 T.consumeClose();
1233
1234 RLoc = T.getCloseLocation();
1235 return Val;
1236}
1237
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001238/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001239/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001240/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001241///
Alexey Bataev3778b602014-07-17 07:32:53 +00001242/// final-clause:
1243/// 'final' '(' expression ')'
1244///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001245/// num_threads-clause:
1246/// 'num_threads' '(' expression ')'
1247///
1248/// safelen-clause:
1249/// 'safelen' '(' expression ')'
1250///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001251/// simdlen-clause:
1252/// 'simdlen' '(' expression ')'
1253///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001254/// collapse-clause:
1255/// 'collapse' '(' expression ')'
1256///
Alexey Bataeva0569352015-12-01 10:17:31 +00001257/// priority-clause:
1258/// 'priority' '(' expression ')'
1259///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001260/// grainsize-clause:
1261/// 'grainsize' '(' expression ')'
1262///
Alexey Bataev382967a2015-12-08 12:06:20 +00001263/// num_tasks-clause:
1264/// 'num_tasks' '(' expression ')'
1265///
Alexey Bataev28c75412015-12-15 08:19:24 +00001266/// hint-clause:
1267/// 'hint' '(' expression ')'
1268///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001269OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1270 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001271 SourceLocation LLoc = Tok.getLocation();
1272 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001273
Alexey Bataev2af33e32016-04-07 12:45:37 +00001274 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001275
1276 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001277 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001278
Alexey Bataev2af33e32016-04-07 12:45:37 +00001279 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001280}
1281
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001282/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001283///
1284/// default-clause:
1285/// 'default' '(' 'none' | 'shared' ')
1286///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001287/// proc_bind-clause:
1288/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1289///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001290OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1291 SourceLocation Loc = Tok.getLocation();
1292 SourceLocation LOpen = ConsumeToken();
1293 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001294 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001295 if (T.expectAndConsume(diag::err_expected_lparen_after,
1296 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001297 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001298
Alexey Bataeva55ed262014-05-28 06:15:33 +00001299 unsigned Type = getOpenMPSimpleClauseType(
1300 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001301 SourceLocation TypeLoc = Tok.getLocation();
1302 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1303 Tok.isNot(tok::annot_pragma_openmp_end))
1304 ConsumeAnyToken();
1305
1306 // Parse ')'.
1307 T.consumeClose();
1308
1309 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1310 Tok.getLocation());
1311}
1312
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001313/// \brief Parsing of OpenMP clauses like 'ordered'.
1314///
1315/// ordered-clause:
1316/// 'ordered'
1317///
Alexey Bataev236070f2014-06-20 11:19:47 +00001318/// nowait-clause:
1319/// 'nowait'
1320///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001321/// untied-clause:
1322/// 'untied'
1323///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001324/// mergeable-clause:
1325/// 'mergeable'
1326///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001327/// read-clause:
1328/// 'read'
1329///
Alexey Bataev346265e2015-09-25 10:37:12 +00001330/// threads-clause:
1331/// 'threads'
1332///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001333/// simd-clause:
1334/// 'simd'
1335///
Alexey Bataevb825de12015-12-07 10:51:44 +00001336/// nogroup-clause:
1337/// 'nogroup'
1338///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001339OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1340 SourceLocation Loc = Tok.getLocation();
1341 ConsumeAnyToken();
1342
1343 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1344}
1345
1346
Alexey Bataev56dafe82014-06-20 07:16:17 +00001347/// \brief Parsing of OpenMP clauses with single expressions and some additional
1348/// argument like 'schedule' or 'dist_schedule'.
1349///
1350/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001351/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1352/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001353///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001354/// if-clause:
1355/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1356///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001357/// defaultmap:
1358/// 'defaultmap' '(' modifier ':' kind ')'
1359///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001360OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1361 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001362 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001363 // Parse '('.
1364 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1365 if (T.expectAndConsume(diag::err_expected_lparen_after,
1366 getOpenMPClauseName(Kind)))
1367 return nullptr;
1368
1369 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001370 SmallVector<unsigned, 4> Arg;
1371 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001372 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001373 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1374 Arg.resize(NumberOfElements);
1375 KLoc.resize(NumberOfElements);
1376 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1377 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1378 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1379 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001380 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001381 if (KindModifier > OMPC_SCHEDULE_unknown) {
1382 // Parse 'modifier'
1383 Arg[Modifier1] = KindModifier;
1384 KLoc[Modifier1] = Tok.getLocation();
1385 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1386 Tok.isNot(tok::annot_pragma_openmp_end))
1387 ConsumeAnyToken();
1388 if (Tok.is(tok::comma)) {
1389 // Parse ',' 'modifier'
1390 ConsumeAnyToken();
1391 KindModifier = getOpenMPSimpleClauseType(
1392 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1393 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1394 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001395 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001396 KLoc[Modifier2] = Tok.getLocation();
1397 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1398 Tok.isNot(tok::annot_pragma_openmp_end))
1399 ConsumeAnyToken();
1400 }
1401 // Parse ':'
1402 if (Tok.is(tok::colon))
1403 ConsumeAnyToken();
1404 else
1405 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1406 KindModifier = getOpenMPSimpleClauseType(
1407 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1408 }
1409 Arg[ScheduleKind] = KindModifier;
1410 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001411 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1412 Tok.isNot(tok::annot_pragma_openmp_end))
1413 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001414 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1415 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1416 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001417 Tok.is(tok::comma))
1418 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001419 } else if (Kind == OMPC_dist_schedule) {
1420 Arg.push_back(getOpenMPSimpleClauseType(
1421 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1422 KLoc.push_back(Tok.getLocation());
1423 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1424 Tok.isNot(tok::annot_pragma_openmp_end))
1425 ConsumeAnyToken();
1426 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1427 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001428 } else if (Kind == OMPC_defaultmap) {
1429 // Get a defaultmap modifier
1430 Arg.push_back(getOpenMPSimpleClauseType(
1431 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1432 KLoc.push_back(Tok.getLocation());
1433 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1434 Tok.isNot(tok::annot_pragma_openmp_end))
1435 ConsumeAnyToken();
1436 // Parse ':'
1437 if (Tok.is(tok::colon))
1438 ConsumeAnyToken();
1439 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1440 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1441 // Get a defaultmap kind
1442 Arg.push_back(getOpenMPSimpleClauseType(
1443 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1444 KLoc.push_back(Tok.getLocation());
1445 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1446 Tok.isNot(tok::annot_pragma_openmp_end))
1447 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001448 } else {
1449 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001450 KLoc.push_back(Tok.getLocation());
1451 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1452 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001453 ConsumeToken();
1454 if (Tok.is(tok::colon))
1455 DelimLoc = ConsumeToken();
1456 else
1457 Diag(Tok, diag::warn_pragma_expected_colon)
1458 << "directive name modifier";
1459 }
1460 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001461
Carlo Bertollib4adf552016-01-15 18:50:31 +00001462 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1463 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1464 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001465 if (NeedAnExpression) {
1466 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001467 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1468 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001469 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001470 }
1471
1472 // Parse ')'.
1473 T.consumeClose();
1474
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001475 if (NeedAnExpression && Val.isInvalid())
1476 return nullptr;
1477
Alexey Bataev56dafe82014-06-20 07:16:17 +00001478 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001479 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001480 T.getCloseLocation());
1481}
1482
Alexey Bataevc5e02582014-06-16 07:08:35 +00001483static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1484 UnqualifiedId &ReductionId) {
1485 SourceLocation TemplateKWLoc;
1486 if (ReductionIdScopeSpec.isEmpty()) {
1487 auto OOK = OO_None;
1488 switch (P.getCurToken().getKind()) {
1489 case tok::plus:
1490 OOK = OO_Plus;
1491 break;
1492 case tok::minus:
1493 OOK = OO_Minus;
1494 break;
1495 case tok::star:
1496 OOK = OO_Star;
1497 break;
1498 case tok::amp:
1499 OOK = OO_Amp;
1500 break;
1501 case tok::pipe:
1502 OOK = OO_Pipe;
1503 break;
1504 case tok::caret:
1505 OOK = OO_Caret;
1506 break;
1507 case tok::ampamp:
1508 OOK = OO_AmpAmp;
1509 break;
1510 case tok::pipepipe:
1511 OOK = OO_PipePipe;
1512 break;
1513 default:
1514 break;
1515 }
1516 if (OOK != OO_None) {
1517 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001518 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001519 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1520 return false;
1521 }
1522 }
1523 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1524 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001525 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001526 TemplateKWLoc, ReductionId);
1527}
1528
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001529/// Parses clauses with list.
1530bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1531 OpenMPClauseKind Kind,
1532 SmallVectorImpl<Expr *> &Vars,
1533 OpenMPVarListDataTy &Data) {
1534 UnqualifiedId UnqualifiedReductionId;
1535 bool InvalidReductionId = false;
1536 bool MapTypeModifierSpecified = false;
1537
1538 // Parse '('.
1539 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1540 if (T.expectAndConsume(diag::err_expected_lparen_after,
1541 getOpenMPClauseName(Kind)))
1542 return true;
1543
1544 bool NeedRParenForLinear = false;
1545 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1546 tok::annot_pragma_openmp_end);
1547 // Handle reduction-identifier for reduction clause.
1548 if (Kind == OMPC_reduction) {
1549 ColonProtectionRAIIObject ColonRAII(*this);
1550 if (getLangOpts().CPlusPlus)
1551 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1552 /*ObjectType=*/nullptr,
1553 /*EnteringContext=*/false);
1554 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1555 UnqualifiedReductionId);
1556 if (InvalidReductionId) {
1557 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1558 StopBeforeMatch);
1559 }
1560 if (Tok.is(tok::colon))
1561 Data.ColonLoc = ConsumeToken();
1562 else
1563 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1564 if (!InvalidReductionId)
1565 Data.ReductionId =
1566 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1567 } else if (Kind == OMPC_depend) {
1568 // Handle dependency type for depend clause.
1569 ColonProtectionRAIIObject ColonRAII(*this);
1570 Data.DepKind =
1571 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1572 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1573 Data.DepLinMapLoc = Tok.getLocation();
1574
1575 if (Data.DepKind == OMPC_DEPEND_unknown) {
1576 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1577 StopBeforeMatch);
1578 } else {
1579 ConsumeToken();
1580 // Special processing for depend(source) clause.
1581 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1582 // Parse ')'.
1583 T.consumeClose();
1584 return false;
1585 }
1586 }
1587 if (Tok.is(tok::colon))
1588 Data.ColonLoc = ConsumeToken();
1589 else {
1590 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1591 : diag::warn_pragma_expected_colon)
1592 << "dependency type";
1593 }
1594 } else if (Kind == OMPC_linear) {
1595 // Try to parse modifier if any.
1596 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1597 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1598 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1599 Data.DepLinMapLoc = ConsumeToken();
1600 LinearT.consumeOpen();
1601 NeedRParenForLinear = true;
1602 }
1603 } else if (Kind == OMPC_map) {
1604 // Handle map type for map clause.
1605 ColonProtectionRAIIObject ColonRAII(*this);
1606
1607 /// The map clause modifier token can be either a identifier or the C++
1608 /// delete keyword.
1609 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1610 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1611 };
1612
1613 // The first identifier may be a list item, a map-type or a
1614 // map-type-modifier. The map modifier can also be delete which has the same
1615 // spelling of the C++ delete keyword.
1616 Data.MapType =
1617 IsMapClauseModifierToken(Tok)
1618 ? static_cast<OpenMPMapClauseKind>(
1619 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1620 : OMPC_MAP_unknown;
1621 Data.DepLinMapLoc = Tok.getLocation();
1622 bool ColonExpected = false;
1623
1624 if (IsMapClauseModifierToken(Tok)) {
1625 if (PP.LookAhead(0).is(tok::colon)) {
1626 if (Data.MapType == OMPC_MAP_unknown)
1627 Diag(Tok, diag::err_omp_unknown_map_type);
1628 else if (Data.MapType == OMPC_MAP_always)
1629 Diag(Tok, diag::err_omp_map_type_missing);
1630 ConsumeToken();
1631 } else if (PP.LookAhead(0).is(tok::comma)) {
1632 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1633 PP.LookAhead(2).is(tok::colon)) {
1634 Data.MapTypeModifier = Data.MapType;
1635 if (Data.MapTypeModifier != OMPC_MAP_always) {
1636 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1637 Data.MapTypeModifier = OMPC_MAP_unknown;
1638 } else
1639 MapTypeModifierSpecified = true;
1640
1641 ConsumeToken();
1642 ConsumeToken();
1643
1644 Data.MapType =
1645 IsMapClauseModifierToken(Tok)
1646 ? static_cast<OpenMPMapClauseKind>(
1647 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1648 : OMPC_MAP_unknown;
1649 if (Data.MapType == OMPC_MAP_unknown ||
1650 Data.MapType == OMPC_MAP_always)
1651 Diag(Tok, diag::err_omp_unknown_map_type);
1652 ConsumeToken();
1653 } else {
1654 Data.MapType = OMPC_MAP_tofrom;
1655 Data.IsMapTypeImplicit = true;
1656 }
1657 } else {
1658 Data.MapType = OMPC_MAP_tofrom;
1659 Data.IsMapTypeImplicit = true;
1660 }
1661 } else {
1662 Data.MapType = OMPC_MAP_tofrom;
1663 Data.IsMapTypeImplicit = true;
1664 }
1665
1666 if (Tok.is(tok::colon))
1667 Data.ColonLoc = ConsumeToken();
1668 else if (ColonExpected)
1669 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1670 }
1671
1672 bool IsComma =
1673 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1674 (Kind == OMPC_reduction && !InvalidReductionId) ||
1675 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1676 (!MapTypeModifierSpecified ||
1677 Data.MapTypeModifier == OMPC_MAP_always)) ||
1678 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1679 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1680 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1681 Tok.isNot(tok::annot_pragma_openmp_end))) {
1682 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1683 // Parse variable
1684 ExprResult VarExpr =
1685 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1686 if (VarExpr.isUsable())
1687 Vars.push_back(VarExpr.get());
1688 else {
1689 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1690 StopBeforeMatch);
1691 }
1692 // Skip ',' if any
1693 IsComma = Tok.is(tok::comma);
1694 if (IsComma)
1695 ConsumeToken();
1696 else if (Tok.isNot(tok::r_paren) &&
1697 Tok.isNot(tok::annot_pragma_openmp_end) &&
1698 (!MayHaveTail || Tok.isNot(tok::colon)))
1699 Diag(Tok, diag::err_omp_expected_punc)
1700 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1701 : getOpenMPClauseName(Kind))
1702 << (Kind == OMPC_flush);
1703 }
1704
1705 // Parse ')' for linear clause with modifier.
1706 if (NeedRParenForLinear)
1707 LinearT.consumeClose();
1708
1709 // Parse ':' linear-step (or ':' alignment).
1710 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1711 if (MustHaveTail) {
1712 Data.ColonLoc = Tok.getLocation();
1713 SourceLocation ELoc = ConsumeToken();
1714 ExprResult Tail = ParseAssignmentExpression();
1715 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1716 if (Tail.isUsable())
1717 Data.TailExpr = Tail.get();
1718 else
1719 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1720 StopBeforeMatch);
1721 }
1722
1723 // Parse ')'.
1724 T.consumeClose();
1725 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1726 Vars.empty()) ||
1727 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1728 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1729 return true;
1730 return false;
1731}
1732
Alexander Musman1bb328c2014-06-04 13:06:39 +00001733/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001734/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001735///
1736/// private-clause:
1737/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001738/// firstprivate-clause:
1739/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001740/// lastprivate-clause:
1741/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001742/// shared-clause:
1743/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001744/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001745/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001746/// aligned-clause:
1747/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001748/// reduction-clause:
1749/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001750/// copyprivate-clause:
1751/// 'copyprivate' '(' list ')'
1752/// flush-clause:
1753/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001754/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001755/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001756/// map-clause:
1757/// 'map' '(' [ [ always , ]
1758/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001759/// to-clause:
1760/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001761/// from-clause:
1762/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001763/// use_device_ptr-clause:
1764/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001765/// is_device_ptr-clause:
1766/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001767///
Alexey Bataev182227b2015-08-20 10:54:39 +00001768/// For 'linear' clause linear-list may have the following forms:
1769/// list
1770/// modifier(list)
1771/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001772OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1773 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001774 SourceLocation Loc = Tok.getLocation();
1775 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001776 SmallVector<Expr *, 4> Vars;
1777 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001778
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001779 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001780 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001781
Alexey Bataevc5e02582014-06-16 07:08:35 +00001782 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001783 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1784 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1785 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1786 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001787}
1788