blob: 87af5fc384d687123aaa38fc9e024360dce93ca9 [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 Lia579b912016-07-14 02:54:56 +0000109 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for },
110 { OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd }
Dmitry Polukhin82478332016-02-13 06:53:38 +0000111 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000112 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +0000113 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000114 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000115 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000116 ? static_cast<unsigned>(OMPD_unknown)
117 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
118 if (DKind == OMPD_unknown)
119 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000120
Alexander Musmanf82886e2014-09-18 05:12:34 +0000121 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000122 if (DKind != F[i][0])
123 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000124
Dmitry Polukhin82478332016-02-13 06:53:38 +0000125 Tok = P.getPreprocessor().LookAhead(0);
126 unsigned SDKind =
127 Tok.isAnnotation()
128 ? static_cast<unsigned>(OMPD_unknown)
129 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
130 if (SDKind == OMPD_unknown)
131 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000132
Dmitry Polukhin82478332016-02-13 06:53:38 +0000133 if (SDKind == F[i][1]) {
134 P.ConsumeToken();
135 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000136 }
137 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000138 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
139 : OMPD_unknown;
140}
141
142static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000143 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000144 Sema &Actions = P.getActions();
145 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000146 // Allow to use 'operator' keyword for C++ operators
147 bool WithOperator = false;
148 if (Tok.is(tok::kw_operator)) {
149 P.ConsumeToken();
150 Tok = P.getCurToken();
151 WithOperator = true;
152 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000153 switch (Tok.getKind()) {
154 case tok::plus: // '+'
155 OOK = OO_Plus;
156 break;
157 case tok::minus: // '-'
158 OOK = OO_Minus;
159 break;
160 case tok::star: // '*'
161 OOK = OO_Star;
162 break;
163 case tok::amp: // '&'
164 OOK = OO_Amp;
165 break;
166 case tok::pipe: // '|'
167 OOK = OO_Pipe;
168 break;
169 case tok::caret: // '^'
170 OOK = OO_Caret;
171 break;
172 case tok::ampamp: // '&&'
173 OOK = OO_AmpAmp;
174 break;
175 case tok::pipepipe: // '||'
176 OOK = OO_PipePipe;
177 break;
178 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000179 if (!WithOperator)
180 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000181 default:
182 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
183 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
184 Parser::StopBeforeMatch);
185 return DeclarationName();
186 }
187 P.ConsumeToken();
188 auto &DeclNames = Actions.getASTContext().DeclarationNames;
189 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
190 : DeclNames.getCXXOperatorName(OOK);
191}
192
193/// \brief Parse 'omp declare reduction' construct.
194///
195/// declare-reduction-directive:
196/// annot_pragma_openmp 'declare' 'reduction'
197/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
198/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
199/// annot_pragma_openmp_end
200/// <reduction_id> is either a base language identifier or one of the following
201/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
202///
203Parser::DeclGroupPtrTy
204Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
205 // Parse '('.
206 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
207 if (T.expectAndConsume(diag::err_expected_lparen_after,
208 getOpenMPDirectiveName(OMPD_declare_reduction))) {
209 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
210 return DeclGroupPtrTy();
211 }
212
213 DeclarationName Name = parseOpenMPReductionId(*this);
214 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
215 return DeclGroupPtrTy();
216
217 // Consume ':'.
218 bool IsCorrect = !ExpectAndConsume(tok::colon);
219
220 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
221 return DeclGroupPtrTy();
222
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000223 IsCorrect = IsCorrect && !Name.isEmpty();
224
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000225 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
226 Diag(Tok.getLocation(), diag::err_expected_type);
227 IsCorrect = false;
228 }
229
230 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
231 return DeclGroupPtrTy();
232
233 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
234 // Parse list of types until ':' token.
235 do {
236 ColonProtectionRAIIObject ColonRAII(*this);
237 SourceRange Range;
238 TypeResult TR = ParseTypeName(&Range, Declarator::PrototypeContext, AS);
239 if (TR.isUsable()) {
240 auto ReductionType =
241 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
242 if (!ReductionType.isNull()) {
243 ReductionTypes.push_back(
244 std::make_pair(ReductionType, Range.getBegin()));
245 }
246 } else {
247 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
248 StopBeforeMatch);
249 }
250
251 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
252 break;
253
254 // Consume ','.
255 if (ExpectAndConsume(tok::comma)) {
256 IsCorrect = false;
257 if (Tok.is(tok::annot_pragma_openmp_end)) {
258 Diag(Tok.getLocation(), diag::err_expected_type);
259 return DeclGroupPtrTy();
260 }
261 }
262 } while (Tok.isNot(tok::annot_pragma_openmp_end));
263
264 if (ReductionTypes.empty()) {
265 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
266 return DeclGroupPtrTy();
267 }
268
269 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
270 return DeclGroupPtrTy();
271
272 // Consume ':'.
273 if (ExpectAndConsume(tok::colon))
274 IsCorrect = false;
275
276 if (Tok.is(tok::annot_pragma_openmp_end)) {
277 Diag(Tok.getLocation(), diag::err_expected_expression);
278 return DeclGroupPtrTy();
279 }
280
281 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
282 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
283
284 // Parse <combiner> expression and then parse initializer if any for each
285 // correct type.
286 unsigned I = 0, E = ReductionTypes.size();
287 for (auto *D : DRD.get()) {
288 TentativeParsingAction TPA(*this);
289 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
290 Scope::OpenMPDirectiveScope);
291 // Parse <combiner> expression.
292 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
293 ExprResult CombinerResult =
294 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
295 D->getLocation(), /*DiscardedValue=*/true);
296 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
297
298 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
299 Tok.isNot(tok::annot_pragma_openmp_end)) {
300 TPA.Commit();
301 IsCorrect = false;
302 break;
303 }
304 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
305 ExprResult InitializerResult;
306 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
307 // Parse <initializer> expression.
308 if (Tok.is(tok::identifier) &&
309 Tok.getIdentifierInfo()->isStr("initializer"))
310 ConsumeToken();
311 else {
312 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
313 TPA.Commit();
314 IsCorrect = false;
315 break;
316 }
317 // Parse '('.
318 BalancedDelimiterTracker T(*this, tok::l_paren,
319 tok::annot_pragma_openmp_end);
320 IsCorrect =
321 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
322 IsCorrect;
323 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
324 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
325 Scope::OpenMPDirectiveScope);
326 // Parse expression.
327 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), D);
328 InitializerResult = Actions.ActOnFinishFullExpr(
329 ParseAssignmentExpression().get(), D->getLocation(),
330 /*DiscardedValue=*/true);
331 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
332 D, InitializerResult.get());
333 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
334 Tok.isNot(tok::annot_pragma_openmp_end)) {
335 TPA.Commit();
336 IsCorrect = false;
337 break;
338 }
339 IsCorrect =
340 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
341 }
342 }
343
344 ++I;
345 // Revert parsing if not the last type, otherwise accept it, we're done with
346 // parsing.
347 if (I != E)
348 TPA.Revert();
349 else
350 TPA.Commit();
351 }
352 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
353 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000354}
355
Alexey Bataev2af33e32016-04-07 12:45:37 +0000356namespace {
357/// RAII that recreates function context for correct parsing of clauses of
358/// 'declare simd' construct.
359/// OpenMP, 2.8.2 declare simd Construct
360/// The expressions appearing in the clauses of this directive are evaluated in
361/// the scope of the arguments of the function declaration or definition.
362class FNContextRAII final {
363 Parser &P;
364 Sema::CXXThisScopeRAII *ThisScope;
365 Parser::ParseScope *TempScope;
366 Parser::ParseScope *FnScope;
367 bool HasTemplateScope = false;
368 bool HasFunScope = false;
369 FNContextRAII() = delete;
370 FNContextRAII(const FNContextRAII &) = delete;
371 FNContextRAII &operator=(const FNContextRAII &) = delete;
372
373public:
374 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
375 Decl *D = *Ptr.get().begin();
376 NamedDecl *ND = dyn_cast<NamedDecl>(D);
377 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
378 Sema &Actions = P.getActions();
379
380 // Allow 'this' within late-parsed attributes.
381 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
382 ND && ND->isCXXInstanceMember());
383
384 // If the Decl is templatized, add template parameters to scope.
385 HasTemplateScope = D->isTemplateDecl();
386 TempScope =
387 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
388 if (HasTemplateScope)
389 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
390
391 // If the Decl is on a function, add function parameters to the scope.
392 HasFunScope = D->isFunctionOrFunctionTemplate();
393 FnScope = new Parser::ParseScope(&P, Scope::FnScope | Scope::DeclScope,
394 HasFunScope);
395 if (HasFunScope)
396 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
397 }
398 ~FNContextRAII() {
399 if (HasFunScope) {
400 P.getActions().ActOnExitFunctionContext();
401 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
402 }
403 if (HasTemplateScope)
404 TempScope->Exit();
405 delete FnScope;
406 delete TempScope;
407 delete ThisScope;
408 }
409};
410} // namespace
411
Alexey Bataevd93d3762016-04-12 09:35:56 +0000412/// Parses clauses for 'declare simd' directive.
413/// clause:
414/// 'inbranch' | 'notinbranch'
415/// 'simdlen' '(' <expr> ')'
416/// { 'uniform' '(' <argument_list> ')' }
417/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000418/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
419static bool parseDeclareSimdClauses(
420 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
421 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
422 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
423 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000424 SourceRange BSRange;
425 const Token &Tok = P.getCurToken();
426 bool IsError = false;
427 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
428 if (Tok.isNot(tok::identifier))
429 break;
430 OMPDeclareSimdDeclAttr::BranchStateTy Out;
431 IdentifierInfo *II = Tok.getIdentifierInfo();
432 StringRef ClauseName = II->getName();
433 // Parse 'inranch|notinbranch' clauses.
434 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
435 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
436 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
437 << ClauseName
438 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
439 IsError = true;
440 }
441 BS = Out;
442 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
443 P.ConsumeToken();
444 } else if (ClauseName.equals("simdlen")) {
445 if (SimdLen.isUsable()) {
446 P.Diag(Tok, diag::err_omp_more_one_clause)
447 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
448 IsError = true;
449 }
450 P.ConsumeToken();
451 SourceLocation RLoc;
452 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
453 if (SimdLen.isInvalid())
454 IsError = true;
455 } else {
456 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000457 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
458 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000459 Parser::OpenMPVarListDataTy Data;
460 auto *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000461 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000462 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000463 else if (CKind == OMPC_linear)
464 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000465
466 P.ConsumeToken();
467 if (P.ParseOpenMPVarList(OMPD_declare_simd,
468 getOpenMPClauseKind(ClauseName), *Vars, Data))
469 IsError = true;
470 if (CKind == OMPC_aligned)
471 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000472 else if (CKind == OMPC_linear) {
473 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
474 Data.DepLinMapLoc))
475 Data.LinKind = OMPC_LINEAR_val;
476 LinModifiers.append(Linears.size() - LinModifiers.size(),
477 Data.LinKind);
478 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
479 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000480 } else
481 // TODO: add parsing of other clauses.
482 break;
483 }
484 // Skip ',' if any.
485 if (Tok.is(tok::comma))
486 P.ConsumeToken();
487 }
488 return IsError;
489}
490
Alexey Bataev2af33e32016-04-07 12:45:37 +0000491/// Parse clauses for '#pragma omp declare simd'.
492Parser::DeclGroupPtrTy
493Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
494 CachedTokens &Toks, SourceLocation Loc) {
495 PP.EnterToken(Tok);
496 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
497 // Consume the previously pushed token.
498 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
499
500 FNContextRAII FnContext(*this, Ptr);
501 OMPDeclareSimdDeclAttr::BranchStateTy BS =
502 OMPDeclareSimdDeclAttr::BS_Undefined;
503 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000504 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000505 SmallVector<Expr *, 4> Aligneds;
506 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000507 SmallVector<Expr *, 4> Linears;
508 SmallVector<unsigned, 4> LinModifiers;
509 SmallVector<Expr *, 4> Steps;
510 bool IsError =
511 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
512 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000513 // Need to check for extra tokens.
514 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
515 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
516 << getOpenMPDirectiveName(OMPD_declare_simd);
517 while (Tok.isNot(tok::annot_pragma_openmp_end))
518 ConsumeAnyToken();
519 }
520 // Skip the last annot_pragma_openmp_end.
521 SourceLocation EndLoc = ConsumeToken();
Alexey Bataevd93d3762016-04-12 09:35:56 +0000522 if (!IsError) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000523 return Actions.ActOnOpenMPDeclareSimdDirective(
Alexey Bataevecba70f2016-04-12 11:02:11 +0000524 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
525 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataevd93d3762016-04-12 09:35:56 +0000526 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000527 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000528}
529
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000530/// \brief Parsing of declarative OpenMP directives.
531///
532/// threadprivate-directive:
533/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000534/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000535///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000536/// declare-reduction-directive:
537/// annot_pragma_openmp 'declare' 'reduction' [...]
538/// annot_pragma_openmp_end
539///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000540/// declare-simd-directive:
541/// annot_pragma_openmp 'declare simd' {<clause> [,]}
542/// annot_pragma_openmp_end
543/// <function declaration/definition>
544///
545Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
546 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
547 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000548 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000549 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000550
551 SourceLocation Loc = ConsumeToken();
Alexey Bataev4acb8592014-07-07 13:01:15 +0000552 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000553
554 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000555 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000556 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000557 ThreadprivateListParserHelper Helper(this);
558 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000559 // The last seen token is annot_pragma_openmp_end - need to check for
560 // extra tokens.
561 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
562 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000563 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000564 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000565 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000566 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000567 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000568 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
569 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000570 }
571 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000572 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000573 case OMPD_declare_reduction:
574 ConsumeToken();
575 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
576 // The last seen token is annot_pragma_openmp_end - need to check for
577 // extra tokens.
578 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
579 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
580 << getOpenMPDirectiveName(OMPD_declare_reduction);
581 while (Tok.isNot(tok::annot_pragma_openmp_end))
582 ConsumeAnyToken();
583 }
584 // Skip the last annot_pragma_openmp_end.
585 ConsumeToken();
586 return Res;
587 }
588 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000589 case OMPD_declare_simd: {
590 // The syntax is:
591 // { #pragma omp declare simd }
592 // <function-declaration-or-definition>
593 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000594 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000595 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000596 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
597 Toks.push_back(Tok);
598 ConsumeAnyToken();
599 }
600 Toks.push_back(Tok);
601 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000602
603 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000604 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000605 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000606 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000607 // Here we expect to see some function declaration.
608 if (AS == AS_none) {
609 assert(TagType == DeclSpec::TST_unspecified);
610 MaybeParseCXX11Attributes(Attrs);
611 MaybeParseMicrosoftAttributes(Attrs);
612 ParsingDeclSpec PDS(*this);
613 Ptr = ParseExternalDeclaration(Attrs, &PDS);
614 } else {
615 Ptr =
616 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
617 }
618 }
619 if (!Ptr) {
620 Diag(Loc, diag::err_omp_decl_in_declare_simd);
621 return DeclGroupPtrTy();
622 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000623 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000624 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000625 case OMPD_declare_target: {
626 SourceLocation DTLoc = ConsumeAnyToken();
627 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000628 // OpenMP 4.5 syntax with list of entities.
629 llvm::SmallSetVector<const NamedDecl*, 16> SameDirectiveDecls;
630 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
631 OMPDeclareTargetDeclAttr::MapTypeTy MT =
632 OMPDeclareTargetDeclAttr::MT_To;
633 if (Tok.is(tok::identifier)) {
634 IdentifierInfo *II = Tok.getIdentifierInfo();
635 StringRef ClauseName = II->getName();
636 // Parse 'to|link' clauses.
637 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
638 MT)) {
639 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
640 << ClauseName;
641 break;
642 }
643 ConsumeToken();
644 }
645 auto Callback = [this, MT, &SameDirectiveDecls](
646 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
647 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
648 SameDirectiveDecls);
649 };
650 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, true))
651 break;
652
653 // Consume optional ','.
654 if (Tok.is(tok::comma))
655 ConsumeToken();
656 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000657 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000658 ConsumeAnyToken();
659 return DeclGroupPtrTy();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000660 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000661
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000662 // Skip the last annot_pragma_openmp_end.
663 ConsumeAnyToken();
664
665 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
666 return DeclGroupPtrTy();
667
668 DKind = ParseOpenMPDirectiveKind(*this);
669 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
670 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
671 ParsedAttributesWithRange attrs(AttrFactory);
672 MaybeParseCXX11Attributes(attrs);
673 MaybeParseMicrosoftAttributes(attrs);
674 ParseExternalDeclaration(attrs);
675 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
676 TentativeParsingAction TPA(*this);
677 ConsumeToken();
678 DKind = ParseOpenMPDirectiveKind(*this);
679 if (DKind != OMPD_end_declare_target)
680 TPA.Revert();
681 else
682 TPA.Commit();
683 }
684 }
685
686 if (DKind == OMPD_end_declare_target) {
687 ConsumeAnyToken();
688 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
689 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
690 << getOpenMPDirectiveName(OMPD_end_declare_target);
691 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
692 }
693 // Skip the last annot_pragma_openmp_end.
694 ConsumeAnyToken();
695 } else {
696 Diag(Tok, diag::err_expected_end_declare_target);
697 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
698 }
699 Actions.ActOnFinishOpenMPDeclareTargetDirective();
700 return DeclGroupPtrTy();
701 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000702 case OMPD_unknown:
703 Diag(Tok, diag::err_omp_unknown_directive);
704 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000705 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000706 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000707 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000708 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000709 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000710 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000711 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000712 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000713 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000714 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000715 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000716 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000717 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000718 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000719 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000720 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000721 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000722 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000723 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000724 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000725 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000726 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000727 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000728 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000729 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000730 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000731 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000732 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000733 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000734 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000735 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000736 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000737 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000738 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000739 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000740 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000741 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000742 case OMPD_target_parallel_for_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000743 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000744 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000745 break;
746 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000747 while (Tok.isNot(tok::annot_pragma_openmp_end))
748 ConsumeAnyToken();
749 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000750 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000751}
752
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000753/// \brief Parsing of declarative or executable OpenMP directives.
754///
755/// threadprivate-directive:
756/// annot_pragma_openmp 'threadprivate' simple-variable-list
757/// annot_pragma_openmp_end
758///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000759/// declare-reduction-directive:
760/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
761/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
762/// ('omp_priv' '=' <expression>|<function_call>) ')']
763/// annot_pragma_openmp_end
764///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000765/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000766/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000767/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
768/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000769/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000770/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000771/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000772/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000773/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000774/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000775/// 'distribute paralle for simd' | 'distribute simd' |
776/// 'target parallel for simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000777/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000778///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000779StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
780 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000781 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000782 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000783 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000784 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000785 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000786 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000787 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000788 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000789 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000790 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000791 // Name of critical directive.
792 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000793 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000794 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000795 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000796
797 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000798 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000799 if (Allowed != ACK_Any) {
800 Diag(Tok, diag::err_omp_immediate_directive)
801 << getOpenMPDirectiveName(DKind) << 0;
802 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000803 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000804 ThreadprivateListParserHelper Helper(this);
805 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000806 // The last seen token is annot_pragma_openmp_end - need to check for
807 // extra tokens.
808 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
809 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000810 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000811 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000812 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000813 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
814 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000815 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
816 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000817 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000818 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000819 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000820 case OMPD_declare_reduction:
821 ConsumeToken();
822 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
823 // The last seen token is annot_pragma_openmp_end - need to check for
824 // extra tokens.
825 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
826 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
827 << getOpenMPDirectiveName(OMPD_declare_reduction);
828 while (Tok.isNot(tok::annot_pragma_openmp_end))
829 ConsumeAnyToken();
830 }
831 ConsumeAnyToken();
832 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
833 } else
834 SkipUntil(tok::annot_pragma_openmp_end);
835 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000836 case OMPD_flush:
837 if (PP.LookAhead(0).is(tok::l_paren)) {
838 FlushHasClause = true;
839 // Push copy of the current token back to stream to properly parse
840 // pseudo-clause OMPFlushClause.
841 PP.EnterToken(Tok);
842 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000843 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000844 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000845 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000846 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000847 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000848 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000849 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000850 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000851 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000852 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000853 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000854 }
855 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000856 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000857 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000858 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000859 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000860 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000861 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000862 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000863 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000864 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000865 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000866 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000867 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000868 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000869 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000870 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000871 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000872 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000873 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000874 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000875 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000876 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000877 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000878 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000879 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000880 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +0000881 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000882 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000883 case OMPD_distribute_simd:
884 case OMPD_target_parallel_for_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000885 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000886 // Parse directive name of the 'critical' directive if any.
887 if (DKind == OMPD_critical) {
888 BalancedDelimiterTracker T(*this, tok::l_paren,
889 tok::annot_pragma_openmp_end);
890 if (!T.consumeOpen()) {
891 if (Tok.isAnyIdentifier()) {
892 DirName =
893 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
894 ConsumeAnyToken();
895 } else {
896 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
897 }
898 T.consumeClose();
899 }
Alexey Bataev80909872015-07-02 11:25:17 +0000900 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000901 CancelRegion = ParseOpenMPDirectiveKind(*this);
902 if (Tok.isNot(tok::annot_pragma_openmp_end))
903 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000904 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000905
Alexey Bataevf29276e2014-06-18 04:14:57 +0000906 if (isOpenMPLoopDirective(DKind))
907 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
908 if (isOpenMPSimdDirective(DKind))
909 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
910 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000911 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000912
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000913 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000914 OpenMPClauseKind CKind =
915 Tok.isAnnotation()
916 ? OMPC_unknown
917 : FlushHasClause ? OMPC_flush
918 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000919 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000920 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000921 OMPClause *Clause =
922 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000923 FirstClauses[CKind].setInt(true);
924 if (Clause) {
925 FirstClauses[CKind].setPointer(Clause);
926 Clauses.push_back(Clause);
927 }
928
929 // Skip ',' if any.
930 if (Tok.is(tok::comma))
931 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000932 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000933 }
934 // End location of the directive.
935 EndLoc = Tok.getLocation();
936 // Consume final annot_pragma_openmp_end.
937 ConsumeToken();
938
Alexey Bataeveb482352015-12-18 05:05:56 +0000939 // OpenMP [2.13.8, ordered Construct, Syntax]
940 // If the depend clause is specified, the ordered construct is a stand-alone
941 // directive.
942 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000943 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000944 Diag(Loc, diag::err_omp_immediate_directive)
945 << getOpenMPDirectiveName(DKind) << 1
946 << getOpenMPClauseName(OMPC_depend);
947 }
948 HasAssociatedStatement = false;
949 }
950
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000951 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000952 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000953 // The body is a block scope like in Lambdas and Blocks.
954 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000955 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000956 Actions.ActOnStartOfCompoundStmt();
957 // Parse statement
958 AssociatedStmt = ParseStatement();
959 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000960 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000961 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000962 Directive = Actions.ActOnOpenMPExecutableDirective(
963 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
964 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000965
966 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000967 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000968 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000969 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000970 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000971 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000972 case OMPD_declare_target:
973 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +0000974 Diag(Tok, diag::err_omp_unexpected_directive)
975 << getOpenMPDirectiveName(DKind);
976 SkipUntil(tok::annot_pragma_openmp_end);
977 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000978 case OMPD_unknown:
979 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000980 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000981 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000982 }
983 return Directive;
984}
985
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000986// Parses simple list:
987// simple-variable-list:
988// '(' id-expression {, id-expression} ')'
989//
990bool Parser::ParseOpenMPSimpleVarList(
991 OpenMPDirectiveKind Kind,
992 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
993 Callback,
994 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000995 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000996 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000997 if (T.expectAndConsume(diag::err_expected_lparen_after,
998 getOpenMPDirectiveName(Kind)))
999 return true;
1000 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001001 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001002
1003 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001004 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001005 CXXScopeSpec SS;
1006 SourceLocation TemplateKWLoc;
1007 UnqualifiedId Name;
1008 // Read var name.
1009 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001010 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001011
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001012 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001013 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001014 IsCorrect = false;
1015 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001016 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +00001017 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001018 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001019 IsCorrect = false;
1020 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001021 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001022 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1023 Tok.isNot(tok::annot_pragma_openmp_end)) {
1024 IsCorrect = false;
1025 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001026 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001027 Diag(PrevTok.getLocation(), diag::err_expected)
1028 << tok::identifier
1029 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001030 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001031 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001032 }
1033 // Consume ','.
1034 if (Tok.is(tok::comma)) {
1035 ConsumeToken();
1036 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001037 }
1038
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001039 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001040 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001041 IsCorrect = false;
1042 }
1043
1044 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001045 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001046
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001047 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001048}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001049
1050/// \brief Parsing of OpenMP clauses.
1051///
1052/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001053/// if-clause | final-clause | num_threads-clause | safelen-clause |
1054/// default-clause | private-clause | firstprivate-clause | shared-clause
1055/// | linear-clause | aligned-clause | collapse-clause |
1056/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001057/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001058/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001059/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001060/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001061/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001062/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Carlo Bertolli70594e92016-07-13 17:16:49 +00001063/// from-clause | is_device_ptr-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001064///
1065OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1066 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001067 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001068 bool ErrorFound = false;
1069 // Check if clause is allowed for the given directive.
1070 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001071 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1072 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001073 ErrorFound = true;
1074 }
1075
1076 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001077 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001078 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001079 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001080 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001081 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001082 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001083 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001084 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001085 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001086 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001087 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001088 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001089 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001090 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001091 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001092 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001093 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001094 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001095 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001096 // OpenMP [2.9.1, target data construct, Restrictions]
1097 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001098 // OpenMP [2.11.1, task Construct, Restrictions]
1099 // At most one if clause can appear on the directive.
1100 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001101 // OpenMP [teams Construct, Restrictions]
1102 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001103 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001104 // OpenMP [2.9.1, task Construct, Restrictions]
1105 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001106 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1107 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001108 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1109 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001110 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001111 Diag(Tok, diag::err_omp_more_one_clause)
1112 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001113 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001114 }
1115
Alexey Bataev10e775f2015-07-30 11:36:16 +00001116 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1117 Clause = ParseOpenMPClause(CKind);
1118 else
1119 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001120 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001121 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001122 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001123 // OpenMP [2.14.3.1, Restrictions]
1124 // Only a single default clause may be specified on a parallel, task or
1125 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001126 // OpenMP [2.5, parallel Construct, Restrictions]
1127 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001128 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001129 Diag(Tok, diag::err_omp_more_one_clause)
1130 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001131 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001132 }
1133
1134 Clause = ParseOpenMPSimpleClause(CKind);
1135 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001136 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001137 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001138 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001139 // OpenMP [2.7.1, Restrictions, p. 3]
1140 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001141 // OpenMP [2.10.4, Restrictions, p. 106]
1142 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001143 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001144 Diag(Tok, diag::err_omp_more_one_clause)
1145 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001146 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001147 }
1148
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001149 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001150 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1151 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001152 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001153 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001154 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001155 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001156 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001157 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001158 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001159 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001160 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001161 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001162 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001163 // OpenMP [2.7.1, Restrictions, p. 9]
1164 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001165 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1166 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001167 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001168 Diag(Tok, diag::err_omp_more_one_clause)
1169 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001170 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001171 }
1172
1173 Clause = ParseOpenMPClause(CKind);
1174 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001175 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001176 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001177 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001178 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001179 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001180 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001181 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001182 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001183 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001184 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001185 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001186 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001187 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001188 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001189 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001190 case OMPC_is_device_ptr:
Alexey Bataeveb482352015-12-18 05:05:56 +00001191 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001192 break;
1193 case OMPC_unknown:
1194 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001195 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001196 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001197 break;
1198 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001199 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001200 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1201 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001202 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001203 break;
1204 }
Craig Topper161e4db2014-05-21 06:02:52 +00001205 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001206}
1207
Alexey Bataev2af33e32016-04-07 12:45:37 +00001208/// Parses simple expression in parens for single-expression clauses of OpenMP
1209/// constructs.
1210/// \param RLoc Returned location of right paren.
1211ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1212 SourceLocation &RLoc) {
1213 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1214 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1215 return ExprError();
1216
1217 SourceLocation ELoc = Tok.getLocation();
1218 ExprResult LHS(ParseCastExpression(
1219 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1220 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1221 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1222
1223 // Parse ')'.
1224 T.consumeClose();
1225
1226 RLoc = T.getCloseLocation();
1227 return Val;
1228}
1229
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001230/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001231/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001232/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001233///
Alexey Bataev3778b602014-07-17 07:32:53 +00001234/// final-clause:
1235/// 'final' '(' expression ')'
1236///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001237/// num_threads-clause:
1238/// 'num_threads' '(' expression ')'
1239///
1240/// safelen-clause:
1241/// 'safelen' '(' expression ')'
1242///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001243/// simdlen-clause:
1244/// 'simdlen' '(' expression ')'
1245///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001246/// collapse-clause:
1247/// 'collapse' '(' expression ')'
1248///
Alexey Bataeva0569352015-12-01 10:17:31 +00001249/// priority-clause:
1250/// 'priority' '(' expression ')'
1251///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001252/// grainsize-clause:
1253/// 'grainsize' '(' expression ')'
1254///
Alexey Bataev382967a2015-12-08 12:06:20 +00001255/// num_tasks-clause:
1256/// 'num_tasks' '(' expression ')'
1257///
Alexey Bataev28c75412015-12-15 08:19:24 +00001258/// hint-clause:
1259/// 'hint' '(' expression ')'
1260///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001261OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1262 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001263 SourceLocation LLoc = Tok.getLocation();
1264 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001265
Alexey Bataev2af33e32016-04-07 12:45:37 +00001266 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001267
1268 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001269 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001270
Alexey Bataev2af33e32016-04-07 12:45:37 +00001271 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001272}
1273
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001274/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001275///
1276/// default-clause:
1277/// 'default' '(' 'none' | 'shared' ')
1278///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001279/// proc_bind-clause:
1280/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1281///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001282OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1283 SourceLocation Loc = Tok.getLocation();
1284 SourceLocation LOpen = ConsumeToken();
1285 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001286 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001287 if (T.expectAndConsume(diag::err_expected_lparen_after,
1288 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001289 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001290
Alexey Bataeva55ed262014-05-28 06:15:33 +00001291 unsigned Type = getOpenMPSimpleClauseType(
1292 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001293 SourceLocation TypeLoc = Tok.getLocation();
1294 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1295 Tok.isNot(tok::annot_pragma_openmp_end))
1296 ConsumeAnyToken();
1297
1298 // Parse ')'.
1299 T.consumeClose();
1300
1301 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1302 Tok.getLocation());
1303}
1304
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001305/// \brief Parsing of OpenMP clauses like 'ordered'.
1306///
1307/// ordered-clause:
1308/// 'ordered'
1309///
Alexey Bataev236070f2014-06-20 11:19:47 +00001310/// nowait-clause:
1311/// 'nowait'
1312///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001313/// untied-clause:
1314/// 'untied'
1315///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001316/// mergeable-clause:
1317/// 'mergeable'
1318///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001319/// read-clause:
1320/// 'read'
1321///
Alexey Bataev346265e2015-09-25 10:37:12 +00001322/// threads-clause:
1323/// 'threads'
1324///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001325/// simd-clause:
1326/// 'simd'
1327///
Alexey Bataevb825de12015-12-07 10:51:44 +00001328/// nogroup-clause:
1329/// 'nogroup'
1330///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001331OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1332 SourceLocation Loc = Tok.getLocation();
1333 ConsumeAnyToken();
1334
1335 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1336}
1337
1338
Alexey Bataev56dafe82014-06-20 07:16:17 +00001339/// \brief Parsing of OpenMP clauses with single expressions and some additional
1340/// argument like 'schedule' or 'dist_schedule'.
1341///
1342/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001343/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1344/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001345///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001346/// if-clause:
1347/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1348///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001349/// defaultmap:
1350/// 'defaultmap' '(' modifier ':' kind ')'
1351///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001352OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1353 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001354 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001355 // Parse '('.
1356 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1357 if (T.expectAndConsume(diag::err_expected_lparen_after,
1358 getOpenMPClauseName(Kind)))
1359 return nullptr;
1360
1361 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001362 SmallVector<unsigned, 4> Arg;
1363 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001364 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001365 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1366 Arg.resize(NumberOfElements);
1367 KLoc.resize(NumberOfElements);
1368 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1369 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1370 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1371 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001372 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001373 if (KindModifier > OMPC_SCHEDULE_unknown) {
1374 // Parse 'modifier'
1375 Arg[Modifier1] = KindModifier;
1376 KLoc[Modifier1] = Tok.getLocation();
1377 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1378 Tok.isNot(tok::annot_pragma_openmp_end))
1379 ConsumeAnyToken();
1380 if (Tok.is(tok::comma)) {
1381 // Parse ',' 'modifier'
1382 ConsumeAnyToken();
1383 KindModifier = getOpenMPSimpleClauseType(
1384 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1385 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1386 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001387 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001388 KLoc[Modifier2] = Tok.getLocation();
1389 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1390 Tok.isNot(tok::annot_pragma_openmp_end))
1391 ConsumeAnyToken();
1392 }
1393 // Parse ':'
1394 if (Tok.is(tok::colon))
1395 ConsumeAnyToken();
1396 else
1397 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1398 KindModifier = getOpenMPSimpleClauseType(
1399 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1400 }
1401 Arg[ScheduleKind] = KindModifier;
1402 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001403 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1404 Tok.isNot(tok::annot_pragma_openmp_end))
1405 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001406 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1407 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1408 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001409 Tok.is(tok::comma))
1410 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001411 } else if (Kind == OMPC_dist_schedule) {
1412 Arg.push_back(getOpenMPSimpleClauseType(
1413 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1414 KLoc.push_back(Tok.getLocation());
1415 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1416 Tok.isNot(tok::annot_pragma_openmp_end))
1417 ConsumeAnyToken();
1418 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1419 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001420 } else if (Kind == OMPC_defaultmap) {
1421 // Get a defaultmap modifier
1422 Arg.push_back(getOpenMPSimpleClauseType(
1423 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1424 KLoc.push_back(Tok.getLocation());
1425 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1426 Tok.isNot(tok::annot_pragma_openmp_end))
1427 ConsumeAnyToken();
1428 // Parse ':'
1429 if (Tok.is(tok::colon))
1430 ConsumeAnyToken();
1431 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1432 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1433 // Get a defaultmap kind
1434 Arg.push_back(getOpenMPSimpleClauseType(
1435 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1436 KLoc.push_back(Tok.getLocation());
1437 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1438 Tok.isNot(tok::annot_pragma_openmp_end))
1439 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001440 } else {
1441 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001442 KLoc.push_back(Tok.getLocation());
1443 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1444 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001445 ConsumeToken();
1446 if (Tok.is(tok::colon))
1447 DelimLoc = ConsumeToken();
1448 else
1449 Diag(Tok, diag::warn_pragma_expected_colon)
1450 << "directive name modifier";
1451 }
1452 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001453
Carlo Bertollib4adf552016-01-15 18:50:31 +00001454 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1455 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1456 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001457 if (NeedAnExpression) {
1458 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001459 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1460 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001461 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001462 }
1463
1464 // Parse ')'.
1465 T.consumeClose();
1466
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001467 if (NeedAnExpression && Val.isInvalid())
1468 return nullptr;
1469
Alexey Bataev56dafe82014-06-20 07:16:17 +00001470 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001471 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001472 T.getCloseLocation());
1473}
1474
Alexey Bataevc5e02582014-06-16 07:08:35 +00001475static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1476 UnqualifiedId &ReductionId) {
1477 SourceLocation TemplateKWLoc;
1478 if (ReductionIdScopeSpec.isEmpty()) {
1479 auto OOK = OO_None;
1480 switch (P.getCurToken().getKind()) {
1481 case tok::plus:
1482 OOK = OO_Plus;
1483 break;
1484 case tok::minus:
1485 OOK = OO_Minus;
1486 break;
1487 case tok::star:
1488 OOK = OO_Star;
1489 break;
1490 case tok::amp:
1491 OOK = OO_Amp;
1492 break;
1493 case tok::pipe:
1494 OOK = OO_Pipe;
1495 break;
1496 case tok::caret:
1497 OOK = OO_Caret;
1498 break;
1499 case tok::ampamp:
1500 OOK = OO_AmpAmp;
1501 break;
1502 case tok::pipepipe:
1503 OOK = OO_PipePipe;
1504 break;
1505 default:
1506 break;
1507 }
1508 if (OOK != OO_None) {
1509 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001510 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001511 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1512 return false;
1513 }
1514 }
1515 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1516 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001517 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001518 TemplateKWLoc, ReductionId);
1519}
1520
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001521/// Parses clauses with list.
1522bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1523 OpenMPClauseKind Kind,
1524 SmallVectorImpl<Expr *> &Vars,
1525 OpenMPVarListDataTy &Data) {
1526 UnqualifiedId UnqualifiedReductionId;
1527 bool InvalidReductionId = false;
1528 bool MapTypeModifierSpecified = false;
1529
1530 // Parse '('.
1531 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1532 if (T.expectAndConsume(diag::err_expected_lparen_after,
1533 getOpenMPClauseName(Kind)))
1534 return true;
1535
1536 bool NeedRParenForLinear = false;
1537 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1538 tok::annot_pragma_openmp_end);
1539 // Handle reduction-identifier for reduction clause.
1540 if (Kind == OMPC_reduction) {
1541 ColonProtectionRAIIObject ColonRAII(*this);
1542 if (getLangOpts().CPlusPlus)
1543 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1544 /*ObjectType=*/nullptr,
1545 /*EnteringContext=*/false);
1546 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1547 UnqualifiedReductionId);
1548 if (InvalidReductionId) {
1549 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1550 StopBeforeMatch);
1551 }
1552 if (Tok.is(tok::colon))
1553 Data.ColonLoc = ConsumeToken();
1554 else
1555 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1556 if (!InvalidReductionId)
1557 Data.ReductionId =
1558 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1559 } else if (Kind == OMPC_depend) {
1560 // Handle dependency type for depend clause.
1561 ColonProtectionRAIIObject ColonRAII(*this);
1562 Data.DepKind =
1563 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1564 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1565 Data.DepLinMapLoc = Tok.getLocation();
1566
1567 if (Data.DepKind == OMPC_DEPEND_unknown) {
1568 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1569 StopBeforeMatch);
1570 } else {
1571 ConsumeToken();
1572 // Special processing for depend(source) clause.
1573 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1574 // Parse ')'.
1575 T.consumeClose();
1576 return false;
1577 }
1578 }
1579 if (Tok.is(tok::colon))
1580 Data.ColonLoc = ConsumeToken();
1581 else {
1582 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1583 : diag::warn_pragma_expected_colon)
1584 << "dependency type";
1585 }
1586 } else if (Kind == OMPC_linear) {
1587 // Try to parse modifier if any.
1588 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1589 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1590 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1591 Data.DepLinMapLoc = ConsumeToken();
1592 LinearT.consumeOpen();
1593 NeedRParenForLinear = true;
1594 }
1595 } else if (Kind == OMPC_map) {
1596 // Handle map type for map clause.
1597 ColonProtectionRAIIObject ColonRAII(*this);
1598
1599 /// The map clause modifier token can be either a identifier or the C++
1600 /// delete keyword.
1601 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1602 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1603 };
1604
1605 // The first identifier may be a list item, a map-type or a
1606 // map-type-modifier. The map modifier can also be delete which has the same
1607 // spelling of the C++ delete keyword.
1608 Data.MapType =
1609 IsMapClauseModifierToken(Tok)
1610 ? static_cast<OpenMPMapClauseKind>(
1611 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1612 : OMPC_MAP_unknown;
1613 Data.DepLinMapLoc = Tok.getLocation();
1614 bool ColonExpected = false;
1615
1616 if (IsMapClauseModifierToken(Tok)) {
1617 if (PP.LookAhead(0).is(tok::colon)) {
1618 if (Data.MapType == OMPC_MAP_unknown)
1619 Diag(Tok, diag::err_omp_unknown_map_type);
1620 else if (Data.MapType == OMPC_MAP_always)
1621 Diag(Tok, diag::err_omp_map_type_missing);
1622 ConsumeToken();
1623 } else if (PP.LookAhead(0).is(tok::comma)) {
1624 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1625 PP.LookAhead(2).is(tok::colon)) {
1626 Data.MapTypeModifier = Data.MapType;
1627 if (Data.MapTypeModifier != OMPC_MAP_always) {
1628 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1629 Data.MapTypeModifier = OMPC_MAP_unknown;
1630 } else
1631 MapTypeModifierSpecified = true;
1632
1633 ConsumeToken();
1634 ConsumeToken();
1635
1636 Data.MapType =
1637 IsMapClauseModifierToken(Tok)
1638 ? static_cast<OpenMPMapClauseKind>(
1639 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1640 : OMPC_MAP_unknown;
1641 if (Data.MapType == OMPC_MAP_unknown ||
1642 Data.MapType == OMPC_MAP_always)
1643 Diag(Tok, diag::err_omp_unknown_map_type);
1644 ConsumeToken();
1645 } else {
1646 Data.MapType = OMPC_MAP_tofrom;
1647 Data.IsMapTypeImplicit = true;
1648 }
1649 } else {
1650 Data.MapType = OMPC_MAP_tofrom;
1651 Data.IsMapTypeImplicit = true;
1652 }
1653 } else {
1654 Data.MapType = OMPC_MAP_tofrom;
1655 Data.IsMapTypeImplicit = true;
1656 }
1657
1658 if (Tok.is(tok::colon))
1659 Data.ColonLoc = ConsumeToken();
1660 else if (ColonExpected)
1661 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1662 }
1663
1664 bool IsComma =
1665 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1666 (Kind == OMPC_reduction && !InvalidReductionId) ||
1667 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1668 (!MapTypeModifierSpecified ||
1669 Data.MapTypeModifier == OMPC_MAP_always)) ||
1670 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1671 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1672 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1673 Tok.isNot(tok::annot_pragma_openmp_end))) {
1674 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1675 // Parse variable
1676 ExprResult VarExpr =
1677 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1678 if (VarExpr.isUsable())
1679 Vars.push_back(VarExpr.get());
1680 else {
1681 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1682 StopBeforeMatch);
1683 }
1684 // Skip ',' if any
1685 IsComma = Tok.is(tok::comma);
1686 if (IsComma)
1687 ConsumeToken();
1688 else if (Tok.isNot(tok::r_paren) &&
1689 Tok.isNot(tok::annot_pragma_openmp_end) &&
1690 (!MayHaveTail || Tok.isNot(tok::colon)))
1691 Diag(Tok, diag::err_omp_expected_punc)
1692 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1693 : getOpenMPClauseName(Kind))
1694 << (Kind == OMPC_flush);
1695 }
1696
1697 // Parse ')' for linear clause with modifier.
1698 if (NeedRParenForLinear)
1699 LinearT.consumeClose();
1700
1701 // Parse ':' linear-step (or ':' alignment).
1702 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1703 if (MustHaveTail) {
1704 Data.ColonLoc = Tok.getLocation();
1705 SourceLocation ELoc = ConsumeToken();
1706 ExprResult Tail = ParseAssignmentExpression();
1707 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1708 if (Tail.isUsable())
1709 Data.TailExpr = Tail.get();
1710 else
1711 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1712 StopBeforeMatch);
1713 }
1714
1715 // Parse ')'.
1716 T.consumeClose();
1717 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1718 Vars.empty()) ||
1719 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1720 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1721 return true;
1722 return false;
1723}
1724
Alexander Musman1bb328c2014-06-04 13:06:39 +00001725/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001726/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001727///
1728/// private-clause:
1729/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001730/// firstprivate-clause:
1731/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001732/// lastprivate-clause:
1733/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001734/// shared-clause:
1735/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001736/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001737/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001738/// aligned-clause:
1739/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001740/// reduction-clause:
1741/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001742/// copyprivate-clause:
1743/// 'copyprivate' '(' list ')'
1744/// flush-clause:
1745/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001746/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001747/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001748/// map-clause:
1749/// 'map' '(' [ [ always , ]
1750/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001751/// to-clause:
1752/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001753/// from-clause:
1754/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001755/// use_device_ptr-clause:
1756/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001757/// is_device_ptr-clause:
1758/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001759///
Alexey Bataev182227b2015-08-20 10:54:39 +00001760/// For 'linear' clause linear-list may have the following forms:
1761/// list
1762/// modifier(list)
1763/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001764OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1765 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001766 SourceLocation Loc = Tok.getLocation();
1767 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001768 SmallVector<Expr *, 4> Vars;
1769 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001770
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001771 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001772 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001773
Alexey Bataevc5e02582014-06-16 07:08:35 +00001774 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001775 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1776 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1777 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1778 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001779}
1780