blob: a52bb632535263d0960a856941966c4aede50188 [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/ASTConsumer.h"
16#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000017#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000019#include "clang/Parse/Parser.h"
20#include "clang/Sema/Scope.h"
21#include "llvm/ADT/PointerIntPair.h"
Michael Wong65f367f2015-07-21 13:44:28 +000022
Alexey Bataeva769e072013-03-22 06:34:35 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// OpenMP declarative directives.
27//===----------------------------------------------------------------------===//
28
Dmitry Polukhin82478332016-02-13 06:53:38 +000029namespace {
30enum OpenMPDirectiveKindEx {
31 OMPD_cancellation = OMPD_unknown + 1,
32 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000033 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000034 OMPD_end,
35 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000036 OMPD_enter,
37 OMPD_exit,
38 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000039 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000040 OMPD_target_enter,
Samuel Antao686c70c2016-05-26 17:30:50 +000041 OMPD_target_exit,
42 OMPD_update,
Carlo Bertolli9925f152016-06-27 14:55:37 +000043 OMPD_distribute_parallel
Dmitry Polukhin82478332016-02-13 06:53:38 +000044};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000045
46class ThreadprivateListParserHelper final {
47 SmallVector<Expr *, 4> Identifiers;
48 Parser *P;
49
50public:
51 ThreadprivateListParserHelper(Parser *P) : P(P) {}
52 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
53 ExprResult Res =
54 P->getActions().ActOnOpenMPIdExpression(P->getCurScope(), SS, NameInfo);
55 if (Res.isUsable())
56 Identifiers.push_back(Res.get());
57 }
58 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
59};
Dmitry Polukhin82478332016-02-13 06:53:38 +000060} // namespace
61
62// Map token string to extended OMP token kind that are
63// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
64static unsigned getOpenMPDirectiveKindEx(StringRef S) {
65 auto DKind = getOpenMPDirectiveKind(S);
66 if (DKind != OMPD_unknown)
67 return DKind;
68
69 return llvm::StringSwitch<unsigned>(S)
70 .Case("cancellation", OMPD_cancellation)
71 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000072 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000073 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000074 .Case("enter", OMPD_enter)
75 .Case("exit", OMPD_exit)
76 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000077 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000078 .Case("update", OMPD_update)
Dmitry Polukhin82478332016-02-13 06:53:38 +000079 .Default(OMPD_unknown);
80}
81
Alexey Bataev4acb8592014-07-07 13:01:15 +000082static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000083 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
84 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
85 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000086 static const unsigned F[][3] = {
87 { OMPD_cancellation, OMPD_point, OMPD_cancellation_point },
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000088 { OMPD_declare, OMPD_reduction, OMPD_declare_reduction },
Alexey Bataev587e1de2016-03-30 10:43:55 +000089 { OMPD_declare, OMPD_simd, OMPD_declare_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000090 { OMPD_declare, OMPD_target, OMPD_declare_target },
Carlo Bertolli9925f152016-06-27 14:55:37 +000091 { OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel },
92 { OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for },
Kelvin Li4a39add2016-07-05 05:00:15 +000093 { OMPD_distribute_parallel_for, OMPD_simd,
94 OMPD_distribute_parallel_for_simd },
Kelvin Li787f3fc2016-07-06 04:45:38 +000095 { OMPD_distribute, OMPD_simd, OMPD_distribute_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000096 { OMPD_end, OMPD_declare, OMPD_end_declare },
97 { OMPD_end_declare, OMPD_target, OMPD_end_declare_target },
Dmitry Polukhin82478332016-02-13 06:53:38 +000098 { OMPD_target, OMPD_data, OMPD_target_data },
99 { OMPD_target, OMPD_enter, OMPD_target_enter },
100 { OMPD_target, OMPD_exit, OMPD_target_exit },
Samuel Antao686c70c2016-05-26 17:30:50 +0000101 { OMPD_target, OMPD_update, OMPD_target_update },
Dmitry Polukhin82478332016-02-13 06:53:38 +0000102 { OMPD_target_enter, OMPD_data, OMPD_target_enter_data },
103 { OMPD_target_exit, OMPD_data, OMPD_target_exit_data },
104 { OMPD_for, OMPD_simd, OMPD_for_simd },
105 { OMPD_parallel, OMPD_for, OMPD_parallel_for },
106 { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
107 { OMPD_parallel, OMPD_sections, OMPD_parallel_sections },
108 { OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd },
109 { OMPD_target, OMPD_parallel, OMPD_target_parallel },
110 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for }
111 };
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:
Alexey Bataeva769e072013-03-22 06:34:35 +0000742 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000743 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000744 break;
745 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000746 while (Tok.isNot(tok::annot_pragma_openmp_end))
747 ConsumeAnyToken();
748 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000749 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000750}
751
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000752/// \brief Parsing of declarative or executable OpenMP directives.
753///
754/// threadprivate-directive:
755/// annot_pragma_openmp 'threadprivate' simple-variable-list
756/// annot_pragma_openmp_end
757///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000758/// declare-reduction-directive:
759/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
760/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
761/// ('omp_priv' '=' <expression>|<function_call>) ')']
762/// annot_pragma_openmp_end
763///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000764/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000765/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000766/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
767/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000768/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000769/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000770/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000771/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000772/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000773/// 'target update' | 'distribute parallel for' |
Kelvin Li787f3fc2016-07-06 04:45:38 +0000774/// 'distribute paralle for simd' | 'distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000775/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000776///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000777StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
778 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000779 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000780 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000781 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000782 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000783 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000784 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000785 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000786 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000787 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000788 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000789 // Name of critical directive.
790 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000791 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000792 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000793 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000794
795 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000796 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000797 if (Allowed != ACK_Any) {
798 Diag(Tok, diag::err_omp_immediate_directive)
799 << getOpenMPDirectiveName(DKind) << 0;
800 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000801 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000802 ThreadprivateListParserHelper Helper(this);
803 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000804 // The last seen token is annot_pragma_openmp_end - need to check for
805 // extra tokens.
806 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
807 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000808 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000809 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000810 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000811 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
812 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000813 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
814 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000815 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000816 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000817 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000818 case OMPD_declare_reduction:
819 ConsumeToken();
820 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
821 // The last seen token is annot_pragma_openmp_end - need to check for
822 // extra tokens.
823 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
824 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
825 << getOpenMPDirectiveName(OMPD_declare_reduction);
826 while (Tok.isNot(tok::annot_pragma_openmp_end))
827 ConsumeAnyToken();
828 }
829 ConsumeAnyToken();
830 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
831 } else
832 SkipUntil(tok::annot_pragma_openmp_end);
833 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000834 case OMPD_flush:
835 if (PP.LookAhead(0).is(tok::l_paren)) {
836 FlushHasClause = true;
837 // Push copy of the current token back to stream to properly parse
838 // pseudo-clause OMPFlushClause.
839 PP.EnterToken(Tok);
840 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000841 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000842 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000843 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000844 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000845 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000846 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000847 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000848 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000849 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000850 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000851 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000852 }
853 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000854 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000855 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000856 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000857 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000858 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000859 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000860 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000861 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000862 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000863 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000864 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000865 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000866 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000867 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000868 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000869 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000870 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000871 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000872 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000873 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000874 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000875 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000876 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000877 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000878 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +0000879 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000880 case OMPD_distribute_parallel_for_simd:
881 case OMPD_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000882 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000883 // Parse directive name of the 'critical' directive if any.
884 if (DKind == OMPD_critical) {
885 BalancedDelimiterTracker T(*this, tok::l_paren,
886 tok::annot_pragma_openmp_end);
887 if (!T.consumeOpen()) {
888 if (Tok.isAnyIdentifier()) {
889 DirName =
890 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
891 ConsumeAnyToken();
892 } else {
893 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
894 }
895 T.consumeClose();
896 }
Alexey Bataev80909872015-07-02 11:25:17 +0000897 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000898 CancelRegion = ParseOpenMPDirectiveKind(*this);
899 if (Tok.isNot(tok::annot_pragma_openmp_end))
900 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000901 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000902
Alexey Bataevf29276e2014-06-18 04:14:57 +0000903 if (isOpenMPLoopDirective(DKind))
904 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
905 if (isOpenMPSimdDirective(DKind))
906 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
907 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000908 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000909
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000910 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000911 OpenMPClauseKind CKind =
912 Tok.isAnnotation()
913 ? OMPC_unknown
914 : FlushHasClause ? OMPC_flush
915 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000916 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000917 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000918 OMPClause *Clause =
919 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000920 FirstClauses[CKind].setInt(true);
921 if (Clause) {
922 FirstClauses[CKind].setPointer(Clause);
923 Clauses.push_back(Clause);
924 }
925
926 // Skip ',' if any.
927 if (Tok.is(tok::comma))
928 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000929 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000930 }
931 // End location of the directive.
932 EndLoc = Tok.getLocation();
933 // Consume final annot_pragma_openmp_end.
934 ConsumeToken();
935
Alexey Bataeveb482352015-12-18 05:05:56 +0000936 // OpenMP [2.13.8, ordered Construct, Syntax]
937 // If the depend clause is specified, the ordered construct is a stand-alone
938 // directive.
939 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000940 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000941 Diag(Loc, diag::err_omp_immediate_directive)
942 << getOpenMPDirectiveName(DKind) << 1
943 << getOpenMPClauseName(OMPC_depend);
944 }
945 HasAssociatedStatement = false;
946 }
947
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000948 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000949 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000950 // The body is a block scope like in Lambdas and Blocks.
951 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000952 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000953 Actions.ActOnStartOfCompoundStmt();
954 // Parse statement
955 AssociatedStmt = ParseStatement();
956 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000957 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000958 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000959 Directive = Actions.ActOnOpenMPExecutableDirective(
960 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
961 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000962
963 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000964 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000965 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000966 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000967 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000968 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000969 case OMPD_declare_target:
970 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +0000971 Diag(Tok, diag::err_omp_unexpected_directive)
972 << getOpenMPDirectiveName(DKind);
973 SkipUntil(tok::annot_pragma_openmp_end);
974 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000975 case OMPD_unknown:
976 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000977 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000978 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000979 }
980 return Directive;
981}
982
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000983// Parses simple list:
984// simple-variable-list:
985// '(' id-expression {, id-expression} ')'
986//
987bool Parser::ParseOpenMPSimpleVarList(
988 OpenMPDirectiveKind Kind,
989 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
990 Callback,
991 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000992 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000993 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000994 if (T.expectAndConsume(diag::err_expected_lparen_after,
995 getOpenMPDirectiveName(Kind)))
996 return true;
997 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000998 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000999
1000 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001001 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001002 CXXScopeSpec SS;
1003 SourceLocation TemplateKWLoc;
1004 UnqualifiedId Name;
1005 // Read var name.
1006 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001007 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001008
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001009 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001010 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001011 IsCorrect = false;
1012 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001013 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +00001014 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001015 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001016 IsCorrect = false;
1017 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001018 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001019 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1020 Tok.isNot(tok::annot_pragma_openmp_end)) {
1021 IsCorrect = false;
1022 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001023 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001024 Diag(PrevTok.getLocation(), diag::err_expected)
1025 << tok::identifier
1026 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001027 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001028 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001029 }
1030 // Consume ','.
1031 if (Tok.is(tok::comma)) {
1032 ConsumeToken();
1033 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001034 }
1035
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001036 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001037 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001038 IsCorrect = false;
1039 }
1040
1041 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001042 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001043
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001044 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001045}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001046
1047/// \brief Parsing of OpenMP clauses.
1048///
1049/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001050/// if-clause | final-clause | num_threads-clause | safelen-clause |
1051/// default-clause | private-clause | firstprivate-clause | shared-clause
1052/// | linear-clause | aligned-clause | collapse-clause |
1053/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001054/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001055/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001056/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001057/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001058/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001059/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
1060/// from-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001061///
1062OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1063 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001064 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001065 bool ErrorFound = false;
1066 // Check if clause is allowed for the given directive.
1067 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001068 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1069 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001070 ErrorFound = true;
1071 }
1072
1073 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001074 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001075 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001076 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001077 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001078 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001079 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001080 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001081 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001082 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001083 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001084 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001085 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001086 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001087 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001088 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001089 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001090 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001091 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001092 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001093 // OpenMP [2.9.1, target data construct, Restrictions]
1094 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001095 // OpenMP [2.11.1, task Construct, Restrictions]
1096 // At most one if clause can appear on the directive.
1097 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001098 // OpenMP [teams Construct, Restrictions]
1099 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001100 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001101 // OpenMP [2.9.1, task Construct, Restrictions]
1102 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001103 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1104 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001105 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1106 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001107 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001108 Diag(Tok, diag::err_omp_more_one_clause)
1109 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001110 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001111 }
1112
Alexey Bataev10e775f2015-07-30 11:36:16 +00001113 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1114 Clause = ParseOpenMPClause(CKind);
1115 else
1116 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001117 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001118 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001119 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001120 // OpenMP [2.14.3.1, Restrictions]
1121 // Only a single default clause may be specified on a parallel, task or
1122 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001123 // OpenMP [2.5, parallel Construct, Restrictions]
1124 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001125 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001126 Diag(Tok, diag::err_omp_more_one_clause)
1127 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001128 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001129 }
1130
1131 Clause = ParseOpenMPSimpleClause(CKind);
1132 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001133 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001134 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001135 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001136 // OpenMP [2.7.1, Restrictions, p. 3]
1137 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001138 // OpenMP [2.10.4, Restrictions, p. 106]
1139 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001140 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001141 Diag(Tok, diag::err_omp_more_one_clause)
1142 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001143 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001144 }
1145
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001146 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001147 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1148 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001149 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001150 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001151 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001152 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001153 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001154 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001155 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001156 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001157 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001158 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001159 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001160 // OpenMP [2.7.1, Restrictions, p. 9]
1161 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001162 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1163 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001164 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001165 Diag(Tok, diag::err_omp_more_one_clause)
1166 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001167 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001168 }
1169
1170 Clause = ParseOpenMPClause(CKind);
1171 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001172 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001173 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001174 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001175 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001176 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001177 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001178 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001179 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001180 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001181 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001182 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001183 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001184 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001185 case OMPC_from:
Alexey Bataeveb482352015-12-18 05:05:56 +00001186 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001187 break;
1188 case OMPC_unknown:
1189 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001190 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001191 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001192 break;
1193 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001194 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001195 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1196 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001197 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001198 break;
1199 }
Craig Topper161e4db2014-05-21 06:02:52 +00001200 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001201}
1202
Alexey Bataev2af33e32016-04-07 12:45:37 +00001203/// Parses simple expression in parens for single-expression clauses of OpenMP
1204/// constructs.
1205/// \param RLoc Returned location of right paren.
1206ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1207 SourceLocation &RLoc) {
1208 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1209 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1210 return ExprError();
1211
1212 SourceLocation ELoc = Tok.getLocation();
1213 ExprResult LHS(ParseCastExpression(
1214 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1215 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1216 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1217
1218 // Parse ')'.
1219 T.consumeClose();
1220
1221 RLoc = T.getCloseLocation();
1222 return Val;
1223}
1224
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001225/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001226/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001227/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001228///
Alexey Bataev3778b602014-07-17 07:32:53 +00001229/// final-clause:
1230/// 'final' '(' expression ')'
1231///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001232/// num_threads-clause:
1233/// 'num_threads' '(' expression ')'
1234///
1235/// safelen-clause:
1236/// 'safelen' '(' expression ')'
1237///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001238/// simdlen-clause:
1239/// 'simdlen' '(' expression ')'
1240///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001241/// collapse-clause:
1242/// 'collapse' '(' expression ')'
1243///
Alexey Bataeva0569352015-12-01 10:17:31 +00001244/// priority-clause:
1245/// 'priority' '(' expression ')'
1246///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001247/// grainsize-clause:
1248/// 'grainsize' '(' expression ')'
1249///
Alexey Bataev382967a2015-12-08 12:06:20 +00001250/// num_tasks-clause:
1251/// 'num_tasks' '(' expression ')'
1252///
Alexey Bataev28c75412015-12-15 08:19:24 +00001253/// hint-clause:
1254/// 'hint' '(' expression ')'
1255///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001256OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1257 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001258 SourceLocation LLoc = Tok.getLocation();
1259 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001260
Alexey Bataev2af33e32016-04-07 12:45:37 +00001261 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001262
1263 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001264 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001265
Alexey Bataev2af33e32016-04-07 12:45:37 +00001266 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001267}
1268
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001269/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001270///
1271/// default-clause:
1272/// 'default' '(' 'none' | 'shared' ')
1273///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001274/// proc_bind-clause:
1275/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1276///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001277OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1278 SourceLocation Loc = Tok.getLocation();
1279 SourceLocation LOpen = ConsumeToken();
1280 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001281 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001282 if (T.expectAndConsume(diag::err_expected_lparen_after,
1283 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001284 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001285
Alexey Bataeva55ed262014-05-28 06:15:33 +00001286 unsigned Type = getOpenMPSimpleClauseType(
1287 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001288 SourceLocation TypeLoc = Tok.getLocation();
1289 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1290 Tok.isNot(tok::annot_pragma_openmp_end))
1291 ConsumeAnyToken();
1292
1293 // Parse ')'.
1294 T.consumeClose();
1295
1296 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1297 Tok.getLocation());
1298}
1299
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001300/// \brief Parsing of OpenMP clauses like 'ordered'.
1301///
1302/// ordered-clause:
1303/// 'ordered'
1304///
Alexey Bataev236070f2014-06-20 11:19:47 +00001305/// nowait-clause:
1306/// 'nowait'
1307///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001308/// untied-clause:
1309/// 'untied'
1310///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001311/// mergeable-clause:
1312/// 'mergeable'
1313///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001314/// read-clause:
1315/// 'read'
1316///
Alexey Bataev346265e2015-09-25 10:37:12 +00001317/// threads-clause:
1318/// 'threads'
1319///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001320/// simd-clause:
1321/// 'simd'
1322///
Alexey Bataevb825de12015-12-07 10:51:44 +00001323/// nogroup-clause:
1324/// 'nogroup'
1325///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001326OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1327 SourceLocation Loc = Tok.getLocation();
1328 ConsumeAnyToken();
1329
1330 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1331}
1332
1333
Alexey Bataev56dafe82014-06-20 07:16:17 +00001334/// \brief Parsing of OpenMP clauses with single expressions and some additional
1335/// argument like 'schedule' or 'dist_schedule'.
1336///
1337/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001338/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1339/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001340///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001341/// if-clause:
1342/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1343///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001344/// defaultmap:
1345/// 'defaultmap' '(' modifier ':' kind ')'
1346///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001347OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1348 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001349 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001350 // Parse '('.
1351 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1352 if (T.expectAndConsume(diag::err_expected_lparen_after,
1353 getOpenMPClauseName(Kind)))
1354 return nullptr;
1355
1356 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001357 SmallVector<unsigned, 4> Arg;
1358 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001359 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001360 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1361 Arg.resize(NumberOfElements);
1362 KLoc.resize(NumberOfElements);
1363 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1364 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1365 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1366 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001367 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001368 if (KindModifier > OMPC_SCHEDULE_unknown) {
1369 // Parse 'modifier'
1370 Arg[Modifier1] = KindModifier;
1371 KLoc[Modifier1] = Tok.getLocation();
1372 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1373 Tok.isNot(tok::annot_pragma_openmp_end))
1374 ConsumeAnyToken();
1375 if (Tok.is(tok::comma)) {
1376 // Parse ',' 'modifier'
1377 ConsumeAnyToken();
1378 KindModifier = getOpenMPSimpleClauseType(
1379 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1380 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1381 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001382 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001383 KLoc[Modifier2] = Tok.getLocation();
1384 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1385 Tok.isNot(tok::annot_pragma_openmp_end))
1386 ConsumeAnyToken();
1387 }
1388 // Parse ':'
1389 if (Tok.is(tok::colon))
1390 ConsumeAnyToken();
1391 else
1392 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1393 KindModifier = getOpenMPSimpleClauseType(
1394 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1395 }
1396 Arg[ScheduleKind] = KindModifier;
1397 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001398 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1399 Tok.isNot(tok::annot_pragma_openmp_end))
1400 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001401 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1402 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1403 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001404 Tok.is(tok::comma))
1405 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001406 } else if (Kind == OMPC_dist_schedule) {
1407 Arg.push_back(getOpenMPSimpleClauseType(
1408 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1409 KLoc.push_back(Tok.getLocation());
1410 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1411 Tok.isNot(tok::annot_pragma_openmp_end))
1412 ConsumeAnyToken();
1413 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1414 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001415 } else if (Kind == OMPC_defaultmap) {
1416 // Get a defaultmap modifier
1417 Arg.push_back(getOpenMPSimpleClauseType(
1418 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1419 KLoc.push_back(Tok.getLocation());
1420 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1421 Tok.isNot(tok::annot_pragma_openmp_end))
1422 ConsumeAnyToken();
1423 // Parse ':'
1424 if (Tok.is(tok::colon))
1425 ConsumeAnyToken();
1426 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1427 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1428 // Get a defaultmap kind
1429 Arg.push_back(getOpenMPSimpleClauseType(
1430 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1431 KLoc.push_back(Tok.getLocation());
1432 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1433 Tok.isNot(tok::annot_pragma_openmp_end))
1434 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001435 } else {
1436 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001437 KLoc.push_back(Tok.getLocation());
1438 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1439 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001440 ConsumeToken();
1441 if (Tok.is(tok::colon))
1442 DelimLoc = ConsumeToken();
1443 else
1444 Diag(Tok, diag::warn_pragma_expected_colon)
1445 << "directive name modifier";
1446 }
1447 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001448
Carlo Bertollib4adf552016-01-15 18:50:31 +00001449 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1450 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1451 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001452 if (NeedAnExpression) {
1453 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001454 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1455 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001456 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001457 }
1458
1459 // Parse ')'.
1460 T.consumeClose();
1461
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001462 if (NeedAnExpression && Val.isInvalid())
1463 return nullptr;
1464
Alexey Bataev56dafe82014-06-20 07:16:17 +00001465 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001466 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001467 T.getCloseLocation());
1468}
1469
Alexey Bataevc5e02582014-06-16 07:08:35 +00001470static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1471 UnqualifiedId &ReductionId) {
1472 SourceLocation TemplateKWLoc;
1473 if (ReductionIdScopeSpec.isEmpty()) {
1474 auto OOK = OO_None;
1475 switch (P.getCurToken().getKind()) {
1476 case tok::plus:
1477 OOK = OO_Plus;
1478 break;
1479 case tok::minus:
1480 OOK = OO_Minus;
1481 break;
1482 case tok::star:
1483 OOK = OO_Star;
1484 break;
1485 case tok::amp:
1486 OOK = OO_Amp;
1487 break;
1488 case tok::pipe:
1489 OOK = OO_Pipe;
1490 break;
1491 case tok::caret:
1492 OOK = OO_Caret;
1493 break;
1494 case tok::ampamp:
1495 OOK = OO_AmpAmp;
1496 break;
1497 case tok::pipepipe:
1498 OOK = OO_PipePipe;
1499 break;
1500 default:
1501 break;
1502 }
1503 if (OOK != OO_None) {
1504 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001505 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001506 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1507 return false;
1508 }
1509 }
1510 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1511 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001512 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001513 TemplateKWLoc, ReductionId);
1514}
1515
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001516/// Parses clauses with list.
1517bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1518 OpenMPClauseKind Kind,
1519 SmallVectorImpl<Expr *> &Vars,
1520 OpenMPVarListDataTy &Data) {
1521 UnqualifiedId UnqualifiedReductionId;
1522 bool InvalidReductionId = false;
1523 bool MapTypeModifierSpecified = false;
1524
1525 // Parse '('.
1526 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1527 if (T.expectAndConsume(diag::err_expected_lparen_after,
1528 getOpenMPClauseName(Kind)))
1529 return true;
1530
1531 bool NeedRParenForLinear = false;
1532 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1533 tok::annot_pragma_openmp_end);
1534 // Handle reduction-identifier for reduction clause.
1535 if (Kind == OMPC_reduction) {
1536 ColonProtectionRAIIObject ColonRAII(*this);
1537 if (getLangOpts().CPlusPlus)
1538 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1539 /*ObjectType=*/nullptr,
1540 /*EnteringContext=*/false);
1541 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1542 UnqualifiedReductionId);
1543 if (InvalidReductionId) {
1544 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1545 StopBeforeMatch);
1546 }
1547 if (Tok.is(tok::colon))
1548 Data.ColonLoc = ConsumeToken();
1549 else
1550 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1551 if (!InvalidReductionId)
1552 Data.ReductionId =
1553 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1554 } else if (Kind == OMPC_depend) {
1555 // Handle dependency type for depend clause.
1556 ColonProtectionRAIIObject ColonRAII(*this);
1557 Data.DepKind =
1558 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1559 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1560 Data.DepLinMapLoc = Tok.getLocation();
1561
1562 if (Data.DepKind == OMPC_DEPEND_unknown) {
1563 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1564 StopBeforeMatch);
1565 } else {
1566 ConsumeToken();
1567 // Special processing for depend(source) clause.
1568 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1569 // Parse ')'.
1570 T.consumeClose();
1571 return false;
1572 }
1573 }
1574 if (Tok.is(tok::colon))
1575 Data.ColonLoc = ConsumeToken();
1576 else {
1577 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1578 : diag::warn_pragma_expected_colon)
1579 << "dependency type";
1580 }
1581 } else if (Kind == OMPC_linear) {
1582 // Try to parse modifier if any.
1583 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1584 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1585 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1586 Data.DepLinMapLoc = ConsumeToken();
1587 LinearT.consumeOpen();
1588 NeedRParenForLinear = true;
1589 }
1590 } else if (Kind == OMPC_map) {
1591 // Handle map type for map clause.
1592 ColonProtectionRAIIObject ColonRAII(*this);
1593
1594 /// The map clause modifier token can be either a identifier or the C++
1595 /// delete keyword.
1596 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1597 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1598 };
1599
1600 // The first identifier may be a list item, a map-type or a
1601 // map-type-modifier. The map modifier can also be delete which has the same
1602 // spelling of the C++ delete keyword.
1603 Data.MapType =
1604 IsMapClauseModifierToken(Tok)
1605 ? static_cast<OpenMPMapClauseKind>(
1606 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1607 : OMPC_MAP_unknown;
1608 Data.DepLinMapLoc = Tok.getLocation();
1609 bool ColonExpected = false;
1610
1611 if (IsMapClauseModifierToken(Tok)) {
1612 if (PP.LookAhead(0).is(tok::colon)) {
1613 if (Data.MapType == OMPC_MAP_unknown)
1614 Diag(Tok, diag::err_omp_unknown_map_type);
1615 else if (Data.MapType == OMPC_MAP_always)
1616 Diag(Tok, diag::err_omp_map_type_missing);
1617 ConsumeToken();
1618 } else if (PP.LookAhead(0).is(tok::comma)) {
1619 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1620 PP.LookAhead(2).is(tok::colon)) {
1621 Data.MapTypeModifier = Data.MapType;
1622 if (Data.MapTypeModifier != OMPC_MAP_always) {
1623 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1624 Data.MapTypeModifier = OMPC_MAP_unknown;
1625 } else
1626 MapTypeModifierSpecified = true;
1627
1628 ConsumeToken();
1629 ConsumeToken();
1630
1631 Data.MapType =
1632 IsMapClauseModifierToken(Tok)
1633 ? static_cast<OpenMPMapClauseKind>(
1634 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1635 : OMPC_MAP_unknown;
1636 if (Data.MapType == OMPC_MAP_unknown ||
1637 Data.MapType == OMPC_MAP_always)
1638 Diag(Tok, diag::err_omp_unknown_map_type);
1639 ConsumeToken();
1640 } else {
1641 Data.MapType = OMPC_MAP_tofrom;
1642 Data.IsMapTypeImplicit = true;
1643 }
1644 } else {
1645 Data.MapType = OMPC_MAP_tofrom;
1646 Data.IsMapTypeImplicit = true;
1647 }
1648 } else {
1649 Data.MapType = OMPC_MAP_tofrom;
1650 Data.IsMapTypeImplicit = true;
1651 }
1652
1653 if (Tok.is(tok::colon))
1654 Data.ColonLoc = ConsumeToken();
1655 else if (ColonExpected)
1656 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1657 }
1658
1659 bool IsComma =
1660 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1661 (Kind == OMPC_reduction && !InvalidReductionId) ||
1662 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1663 (!MapTypeModifierSpecified ||
1664 Data.MapTypeModifier == OMPC_MAP_always)) ||
1665 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1666 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1667 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1668 Tok.isNot(tok::annot_pragma_openmp_end))) {
1669 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1670 // Parse variable
1671 ExprResult VarExpr =
1672 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1673 if (VarExpr.isUsable())
1674 Vars.push_back(VarExpr.get());
1675 else {
1676 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1677 StopBeforeMatch);
1678 }
1679 // Skip ',' if any
1680 IsComma = Tok.is(tok::comma);
1681 if (IsComma)
1682 ConsumeToken();
1683 else if (Tok.isNot(tok::r_paren) &&
1684 Tok.isNot(tok::annot_pragma_openmp_end) &&
1685 (!MayHaveTail || Tok.isNot(tok::colon)))
1686 Diag(Tok, diag::err_omp_expected_punc)
1687 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1688 : getOpenMPClauseName(Kind))
1689 << (Kind == OMPC_flush);
1690 }
1691
1692 // Parse ')' for linear clause with modifier.
1693 if (NeedRParenForLinear)
1694 LinearT.consumeClose();
1695
1696 // Parse ':' linear-step (or ':' alignment).
1697 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1698 if (MustHaveTail) {
1699 Data.ColonLoc = Tok.getLocation();
1700 SourceLocation ELoc = ConsumeToken();
1701 ExprResult Tail = ParseAssignmentExpression();
1702 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1703 if (Tail.isUsable())
1704 Data.TailExpr = Tail.get();
1705 else
1706 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1707 StopBeforeMatch);
1708 }
1709
1710 // Parse ')'.
1711 T.consumeClose();
1712 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1713 Vars.empty()) ||
1714 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1715 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1716 return true;
1717 return false;
1718}
1719
Alexander Musman1bb328c2014-06-04 13:06:39 +00001720/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001721/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001722///
1723/// private-clause:
1724/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001725/// firstprivate-clause:
1726/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001727/// lastprivate-clause:
1728/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001729/// shared-clause:
1730/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001731/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001732/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001733/// aligned-clause:
1734/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001735/// reduction-clause:
1736/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001737/// copyprivate-clause:
1738/// 'copyprivate' '(' list ')'
1739/// flush-clause:
1740/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001741/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001742/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001743/// map-clause:
1744/// 'map' '(' [ [ always , ]
1745/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001746/// to-clause:
1747/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001748/// from-clause:
1749/// 'from' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001750///
Alexey Bataev182227b2015-08-20 10:54:39 +00001751/// For 'linear' clause linear-list may have the following forms:
1752/// list
1753/// modifier(list)
1754/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001755OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1756 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001757 SourceLocation Loc = Tok.getLocation();
1758 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001759 SmallVector<Expr *, 4> Vars;
1760 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001761
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001762 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001763 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001764
Alexey Bataevc5e02582014-06-16 07:08:35 +00001765 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001766 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1767 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1768 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1769 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001770}
1771