blob: 8a15cf61ef54a2f58dab5b41bdcb4141c83fa511 [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 },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000093 { OMPD_end, OMPD_declare, OMPD_end_declare },
94 { OMPD_end_declare, OMPD_target, OMPD_end_declare_target },
Dmitry Polukhin82478332016-02-13 06:53:38 +000095 { OMPD_target, OMPD_data, OMPD_target_data },
96 { OMPD_target, OMPD_enter, OMPD_target_enter },
97 { OMPD_target, OMPD_exit, OMPD_target_exit },
Samuel Antao686c70c2016-05-26 17:30:50 +000098 { OMPD_target, OMPD_update, OMPD_target_update },
Dmitry Polukhin82478332016-02-13 06:53:38 +000099 { OMPD_target_enter, OMPD_data, OMPD_target_enter_data },
100 { OMPD_target_exit, OMPD_data, OMPD_target_exit_data },
101 { OMPD_for, OMPD_simd, OMPD_for_simd },
102 { OMPD_parallel, OMPD_for, OMPD_parallel_for },
103 { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
104 { OMPD_parallel, OMPD_sections, OMPD_parallel_sections },
105 { OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd },
106 { OMPD_target, OMPD_parallel, OMPD_target_parallel },
107 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for }
108 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000109 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +0000110 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000111 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000112 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000113 ? static_cast<unsigned>(OMPD_unknown)
114 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
115 if (DKind == OMPD_unknown)
116 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000117
Alexander Musmanf82886e2014-09-18 05:12:34 +0000118 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000119 if (DKind != F[i][0])
120 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000121
Dmitry Polukhin82478332016-02-13 06:53:38 +0000122 Tok = P.getPreprocessor().LookAhead(0);
123 unsigned SDKind =
124 Tok.isAnnotation()
125 ? static_cast<unsigned>(OMPD_unknown)
126 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
127 if (SDKind == OMPD_unknown)
128 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000129
Dmitry Polukhin82478332016-02-13 06:53:38 +0000130 if (SDKind == F[i][1]) {
131 P.ConsumeToken();
132 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000133 }
134 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000135 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
136 : OMPD_unknown;
137}
138
139static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000140 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000141 Sema &Actions = P.getActions();
142 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000143 // Allow to use 'operator' keyword for C++ operators
144 bool WithOperator = false;
145 if (Tok.is(tok::kw_operator)) {
146 P.ConsumeToken();
147 Tok = P.getCurToken();
148 WithOperator = true;
149 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000150 switch (Tok.getKind()) {
151 case tok::plus: // '+'
152 OOK = OO_Plus;
153 break;
154 case tok::minus: // '-'
155 OOK = OO_Minus;
156 break;
157 case tok::star: // '*'
158 OOK = OO_Star;
159 break;
160 case tok::amp: // '&'
161 OOK = OO_Amp;
162 break;
163 case tok::pipe: // '|'
164 OOK = OO_Pipe;
165 break;
166 case tok::caret: // '^'
167 OOK = OO_Caret;
168 break;
169 case tok::ampamp: // '&&'
170 OOK = OO_AmpAmp;
171 break;
172 case tok::pipepipe: // '||'
173 OOK = OO_PipePipe;
174 break;
175 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000176 if (!WithOperator)
177 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000178 default:
179 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
180 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
181 Parser::StopBeforeMatch);
182 return DeclarationName();
183 }
184 P.ConsumeToken();
185 auto &DeclNames = Actions.getASTContext().DeclarationNames;
186 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
187 : DeclNames.getCXXOperatorName(OOK);
188}
189
190/// \brief Parse 'omp declare reduction' construct.
191///
192/// declare-reduction-directive:
193/// annot_pragma_openmp 'declare' 'reduction'
194/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
195/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
196/// annot_pragma_openmp_end
197/// <reduction_id> is either a base language identifier or one of the following
198/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
199///
200Parser::DeclGroupPtrTy
201Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
202 // Parse '('.
203 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
204 if (T.expectAndConsume(diag::err_expected_lparen_after,
205 getOpenMPDirectiveName(OMPD_declare_reduction))) {
206 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
207 return DeclGroupPtrTy();
208 }
209
210 DeclarationName Name = parseOpenMPReductionId(*this);
211 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
212 return DeclGroupPtrTy();
213
214 // Consume ':'.
215 bool IsCorrect = !ExpectAndConsume(tok::colon);
216
217 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
218 return DeclGroupPtrTy();
219
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000220 IsCorrect = IsCorrect && !Name.isEmpty();
221
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000222 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
223 Diag(Tok.getLocation(), diag::err_expected_type);
224 IsCorrect = false;
225 }
226
227 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
228 return DeclGroupPtrTy();
229
230 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
231 // Parse list of types until ':' token.
232 do {
233 ColonProtectionRAIIObject ColonRAII(*this);
234 SourceRange Range;
235 TypeResult TR = ParseTypeName(&Range, Declarator::PrototypeContext, AS);
236 if (TR.isUsable()) {
237 auto ReductionType =
238 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
239 if (!ReductionType.isNull()) {
240 ReductionTypes.push_back(
241 std::make_pair(ReductionType, Range.getBegin()));
242 }
243 } else {
244 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
245 StopBeforeMatch);
246 }
247
248 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
249 break;
250
251 // Consume ','.
252 if (ExpectAndConsume(tok::comma)) {
253 IsCorrect = false;
254 if (Tok.is(tok::annot_pragma_openmp_end)) {
255 Diag(Tok.getLocation(), diag::err_expected_type);
256 return DeclGroupPtrTy();
257 }
258 }
259 } while (Tok.isNot(tok::annot_pragma_openmp_end));
260
261 if (ReductionTypes.empty()) {
262 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
263 return DeclGroupPtrTy();
264 }
265
266 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
267 return DeclGroupPtrTy();
268
269 // Consume ':'.
270 if (ExpectAndConsume(tok::colon))
271 IsCorrect = false;
272
273 if (Tok.is(tok::annot_pragma_openmp_end)) {
274 Diag(Tok.getLocation(), diag::err_expected_expression);
275 return DeclGroupPtrTy();
276 }
277
278 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
279 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
280
281 // Parse <combiner> expression and then parse initializer if any for each
282 // correct type.
283 unsigned I = 0, E = ReductionTypes.size();
284 for (auto *D : DRD.get()) {
285 TentativeParsingAction TPA(*this);
286 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
287 Scope::OpenMPDirectiveScope);
288 // Parse <combiner> expression.
289 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
290 ExprResult CombinerResult =
291 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
292 D->getLocation(), /*DiscardedValue=*/true);
293 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
294
295 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
296 Tok.isNot(tok::annot_pragma_openmp_end)) {
297 TPA.Commit();
298 IsCorrect = false;
299 break;
300 }
301 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
302 ExprResult InitializerResult;
303 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
304 // Parse <initializer> expression.
305 if (Tok.is(tok::identifier) &&
306 Tok.getIdentifierInfo()->isStr("initializer"))
307 ConsumeToken();
308 else {
309 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
310 TPA.Commit();
311 IsCorrect = false;
312 break;
313 }
314 // Parse '('.
315 BalancedDelimiterTracker T(*this, tok::l_paren,
316 tok::annot_pragma_openmp_end);
317 IsCorrect =
318 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
319 IsCorrect;
320 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
321 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
322 Scope::OpenMPDirectiveScope);
323 // Parse expression.
324 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), D);
325 InitializerResult = Actions.ActOnFinishFullExpr(
326 ParseAssignmentExpression().get(), D->getLocation(),
327 /*DiscardedValue=*/true);
328 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
329 D, InitializerResult.get());
330 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
331 Tok.isNot(tok::annot_pragma_openmp_end)) {
332 TPA.Commit();
333 IsCorrect = false;
334 break;
335 }
336 IsCorrect =
337 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
338 }
339 }
340
341 ++I;
342 // Revert parsing if not the last type, otherwise accept it, we're done with
343 // parsing.
344 if (I != E)
345 TPA.Revert();
346 else
347 TPA.Commit();
348 }
349 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
350 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000351}
352
Alexey Bataev2af33e32016-04-07 12:45:37 +0000353namespace {
354/// RAII that recreates function context for correct parsing of clauses of
355/// 'declare simd' construct.
356/// OpenMP, 2.8.2 declare simd Construct
357/// The expressions appearing in the clauses of this directive are evaluated in
358/// the scope of the arguments of the function declaration or definition.
359class FNContextRAII final {
360 Parser &P;
361 Sema::CXXThisScopeRAII *ThisScope;
362 Parser::ParseScope *TempScope;
363 Parser::ParseScope *FnScope;
364 bool HasTemplateScope = false;
365 bool HasFunScope = false;
366 FNContextRAII() = delete;
367 FNContextRAII(const FNContextRAII &) = delete;
368 FNContextRAII &operator=(const FNContextRAII &) = delete;
369
370public:
371 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
372 Decl *D = *Ptr.get().begin();
373 NamedDecl *ND = dyn_cast<NamedDecl>(D);
374 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
375 Sema &Actions = P.getActions();
376
377 // Allow 'this' within late-parsed attributes.
378 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
379 ND && ND->isCXXInstanceMember());
380
381 // If the Decl is templatized, add template parameters to scope.
382 HasTemplateScope = D->isTemplateDecl();
383 TempScope =
384 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
385 if (HasTemplateScope)
386 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
387
388 // If the Decl is on a function, add function parameters to the scope.
389 HasFunScope = D->isFunctionOrFunctionTemplate();
390 FnScope = new Parser::ParseScope(&P, Scope::FnScope | Scope::DeclScope,
391 HasFunScope);
392 if (HasFunScope)
393 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
394 }
395 ~FNContextRAII() {
396 if (HasFunScope) {
397 P.getActions().ActOnExitFunctionContext();
398 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
399 }
400 if (HasTemplateScope)
401 TempScope->Exit();
402 delete FnScope;
403 delete TempScope;
404 delete ThisScope;
405 }
406};
407} // namespace
408
Alexey Bataevd93d3762016-04-12 09:35:56 +0000409/// Parses clauses for 'declare simd' directive.
410/// clause:
411/// 'inbranch' | 'notinbranch'
412/// 'simdlen' '(' <expr> ')'
413/// { 'uniform' '(' <argument_list> ')' }
414/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000415/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
416static bool parseDeclareSimdClauses(
417 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
418 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
419 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
420 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000421 SourceRange BSRange;
422 const Token &Tok = P.getCurToken();
423 bool IsError = false;
424 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
425 if (Tok.isNot(tok::identifier))
426 break;
427 OMPDeclareSimdDeclAttr::BranchStateTy Out;
428 IdentifierInfo *II = Tok.getIdentifierInfo();
429 StringRef ClauseName = II->getName();
430 // Parse 'inranch|notinbranch' clauses.
431 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
432 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
433 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
434 << ClauseName
435 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
436 IsError = true;
437 }
438 BS = Out;
439 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
440 P.ConsumeToken();
441 } else if (ClauseName.equals("simdlen")) {
442 if (SimdLen.isUsable()) {
443 P.Diag(Tok, diag::err_omp_more_one_clause)
444 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
445 IsError = true;
446 }
447 P.ConsumeToken();
448 SourceLocation RLoc;
449 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
450 if (SimdLen.isInvalid())
451 IsError = true;
452 } else {
453 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000454 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
455 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000456 Parser::OpenMPVarListDataTy Data;
457 auto *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000458 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000459 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000460 else if (CKind == OMPC_linear)
461 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000462
463 P.ConsumeToken();
464 if (P.ParseOpenMPVarList(OMPD_declare_simd,
465 getOpenMPClauseKind(ClauseName), *Vars, Data))
466 IsError = true;
467 if (CKind == OMPC_aligned)
468 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000469 else if (CKind == OMPC_linear) {
470 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
471 Data.DepLinMapLoc))
472 Data.LinKind = OMPC_LINEAR_val;
473 LinModifiers.append(Linears.size() - LinModifiers.size(),
474 Data.LinKind);
475 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
476 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000477 } else
478 // TODO: add parsing of other clauses.
479 break;
480 }
481 // Skip ',' if any.
482 if (Tok.is(tok::comma))
483 P.ConsumeToken();
484 }
485 return IsError;
486}
487
Alexey Bataev2af33e32016-04-07 12:45:37 +0000488/// Parse clauses for '#pragma omp declare simd'.
489Parser::DeclGroupPtrTy
490Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
491 CachedTokens &Toks, SourceLocation Loc) {
492 PP.EnterToken(Tok);
493 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
494 // Consume the previously pushed token.
495 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
496
497 FNContextRAII FnContext(*this, Ptr);
498 OMPDeclareSimdDeclAttr::BranchStateTy BS =
499 OMPDeclareSimdDeclAttr::BS_Undefined;
500 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000501 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000502 SmallVector<Expr *, 4> Aligneds;
503 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000504 SmallVector<Expr *, 4> Linears;
505 SmallVector<unsigned, 4> LinModifiers;
506 SmallVector<Expr *, 4> Steps;
507 bool IsError =
508 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
509 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000510 // Need to check for extra tokens.
511 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
512 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
513 << getOpenMPDirectiveName(OMPD_declare_simd);
514 while (Tok.isNot(tok::annot_pragma_openmp_end))
515 ConsumeAnyToken();
516 }
517 // Skip the last annot_pragma_openmp_end.
518 SourceLocation EndLoc = ConsumeToken();
Alexey Bataevd93d3762016-04-12 09:35:56 +0000519 if (!IsError) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000520 return Actions.ActOnOpenMPDeclareSimdDirective(
Alexey Bataevecba70f2016-04-12 11:02:11 +0000521 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
522 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataevd93d3762016-04-12 09:35:56 +0000523 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000524 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000525}
526
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000527/// \brief Parsing of declarative OpenMP directives.
528///
529/// threadprivate-directive:
530/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000531/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000532///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000533/// declare-reduction-directive:
534/// annot_pragma_openmp 'declare' 'reduction' [...]
535/// annot_pragma_openmp_end
536///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000537/// declare-simd-directive:
538/// annot_pragma_openmp 'declare simd' {<clause> [,]}
539/// annot_pragma_openmp_end
540/// <function declaration/definition>
541///
542Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
543 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
544 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000545 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000546 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000547
548 SourceLocation Loc = ConsumeToken();
Alexey Bataev4acb8592014-07-07 13:01:15 +0000549 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000550
551 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000552 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000553 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000554 ThreadprivateListParserHelper Helper(this);
555 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000556 // The last seen token is annot_pragma_openmp_end - need to check for
557 // extra tokens.
558 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
559 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000560 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000561 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000562 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000563 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000564 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000565 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
566 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000567 }
568 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000569 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000570 case OMPD_declare_reduction:
571 ConsumeToken();
572 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
573 // The last seen token is annot_pragma_openmp_end - need to check for
574 // extra tokens.
575 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
576 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
577 << getOpenMPDirectiveName(OMPD_declare_reduction);
578 while (Tok.isNot(tok::annot_pragma_openmp_end))
579 ConsumeAnyToken();
580 }
581 // Skip the last annot_pragma_openmp_end.
582 ConsumeToken();
583 return Res;
584 }
585 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000586 case OMPD_declare_simd: {
587 // The syntax is:
588 // { #pragma omp declare simd }
589 // <function-declaration-or-definition>
590 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000591 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000592 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000593 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
594 Toks.push_back(Tok);
595 ConsumeAnyToken();
596 }
597 Toks.push_back(Tok);
598 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000599
600 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000601 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000602 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000603 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000604 // Here we expect to see some function declaration.
605 if (AS == AS_none) {
606 assert(TagType == DeclSpec::TST_unspecified);
607 MaybeParseCXX11Attributes(Attrs);
608 MaybeParseMicrosoftAttributes(Attrs);
609 ParsingDeclSpec PDS(*this);
610 Ptr = ParseExternalDeclaration(Attrs, &PDS);
611 } else {
612 Ptr =
613 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
614 }
615 }
616 if (!Ptr) {
617 Diag(Loc, diag::err_omp_decl_in_declare_simd);
618 return DeclGroupPtrTy();
619 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000620 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000621 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000622 case OMPD_declare_target: {
623 SourceLocation DTLoc = ConsumeAnyToken();
624 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000625 // OpenMP 4.5 syntax with list of entities.
626 llvm::SmallSetVector<const NamedDecl*, 16> SameDirectiveDecls;
627 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
628 OMPDeclareTargetDeclAttr::MapTypeTy MT =
629 OMPDeclareTargetDeclAttr::MT_To;
630 if (Tok.is(tok::identifier)) {
631 IdentifierInfo *II = Tok.getIdentifierInfo();
632 StringRef ClauseName = II->getName();
633 // Parse 'to|link' clauses.
634 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
635 MT)) {
636 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
637 << ClauseName;
638 break;
639 }
640 ConsumeToken();
641 }
642 auto Callback = [this, MT, &SameDirectiveDecls](
643 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
644 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
645 SameDirectiveDecls);
646 };
647 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, true))
648 break;
649
650 // Consume optional ','.
651 if (Tok.is(tok::comma))
652 ConsumeToken();
653 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000654 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000655 ConsumeAnyToken();
656 return DeclGroupPtrTy();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000657 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000658
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000659 // Skip the last annot_pragma_openmp_end.
660 ConsumeAnyToken();
661
662 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
663 return DeclGroupPtrTy();
664
665 DKind = ParseOpenMPDirectiveKind(*this);
666 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
667 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
668 ParsedAttributesWithRange attrs(AttrFactory);
669 MaybeParseCXX11Attributes(attrs);
670 MaybeParseMicrosoftAttributes(attrs);
671 ParseExternalDeclaration(attrs);
672 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
673 TentativeParsingAction TPA(*this);
674 ConsumeToken();
675 DKind = ParseOpenMPDirectiveKind(*this);
676 if (DKind != OMPD_end_declare_target)
677 TPA.Revert();
678 else
679 TPA.Commit();
680 }
681 }
682
683 if (DKind == OMPD_end_declare_target) {
684 ConsumeAnyToken();
685 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
686 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
687 << getOpenMPDirectiveName(OMPD_end_declare_target);
688 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
689 }
690 // Skip the last annot_pragma_openmp_end.
691 ConsumeAnyToken();
692 } else {
693 Diag(Tok, diag::err_expected_end_declare_target);
694 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
695 }
696 Actions.ActOnFinishOpenMPDeclareTargetDirective();
697 return DeclGroupPtrTy();
698 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000699 case OMPD_unknown:
700 Diag(Tok, diag::err_omp_unknown_directive);
701 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000702 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000703 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000704 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000705 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000706 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000707 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000708 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000709 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000710 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000711 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000712 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000713 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000714 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000715 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000716 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000717 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000718 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000719 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000720 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000721 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000722 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000723 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000724 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000725 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000726 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000727 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000728 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000729 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000730 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000731 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000732 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000733 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000734 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000735 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000736 case OMPD_distribute_parallel_for:
Alexey Bataeva769e072013-03-22 06:34:35 +0000737 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000738 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000739 break;
740 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000741 while (Tok.isNot(tok::annot_pragma_openmp_end))
742 ConsumeAnyToken();
743 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000744 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000745}
746
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000747/// \brief Parsing of declarative or executable OpenMP directives.
748///
749/// threadprivate-directive:
750/// annot_pragma_openmp 'threadprivate' simple-variable-list
751/// annot_pragma_openmp_end
752///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000753/// declare-reduction-directive:
754/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
755/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
756/// ('omp_priv' '=' <expression>|<function_call>) ')']
757/// annot_pragma_openmp_end
758///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000759/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000760/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000761/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
762/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000763/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000764/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000765/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000766/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000767/// 'target parallel' | 'target parallel for' |
Carlo Bertolli9925f152016-06-27 14:55:37 +0000768/// 'target update' | 'distribute parallel for' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000769/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000770///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000771StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
772 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000773 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000774 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000775 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000776 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000777 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000778 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000779 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000780 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000781 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000782 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000783 // Name of critical directive.
784 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000785 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000786 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000787 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000788
789 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000790 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000791 if (Allowed != ACK_Any) {
792 Diag(Tok, diag::err_omp_immediate_directive)
793 << getOpenMPDirectiveName(DKind) << 0;
794 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000795 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000796 ThreadprivateListParserHelper Helper(this);
797 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000798 // The last seen token is annot_pragma_openmp_end - need to check for
799 // extra tokens.
800 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
801 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000802 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000803 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000804 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000805 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
806 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000807 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
808 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000809 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000810 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000811 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000812 case OMPD_declare_reduction:
813 ConsumeToken();
814 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
815 // The last seen token is annot_pragma_openmp_end - need to check for
816 // extra tokens.
817 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
818 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
819 << getOpenMPDirectiveName(OMPD_declare_reduction);
820 while (Tok.isNot(tok::annot_pragma_openmp_end))
821 ConsumeAnyToken();
822 }
823 ConsumeAnyToken();
824 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
825 } else
826 SkipUntil(tok::annot_pragma_openmp_end);
827 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000828 case OMPD_flush:
829 if (PP.LookAhead(0).is(tok::l_paren)) {
830 FlushHasClause = true;
831 // Push copy of the current token back to stream to properly parse
832 // pseudo-clause OMPFlushClause.
833 PP.EnterToken(Tok);
834 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000835 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000836 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000837 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000838 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000839 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000840 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000841 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000842 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000843 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000844 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000845 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000846 }
847 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000848 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000849 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000850 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000851 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000852 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000853 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000854 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000855 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000856 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000857 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000858 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000859 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000860 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000861 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000862 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000863 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000864 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000865 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000866 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000867 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000868 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000869 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000870 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000871 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000872 case OMPD_distribute:
873 case OMPD_distribute_parallel_for: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000874 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000875 // Parse directive name of the 'critical' directive if any.
876 if (DKind == OMPD_critical) {
877 BalancedDelimiterTracker T(*this, tok::l_paren,
878 tok::annot_pragma_openmp_end);
879 if (!T.consumeOpen()) {
880 if (Tok.isAnyIdentifier()) {
881 DirName =
882 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
883 ConsumeAnyToken();
884 } else {
885 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
886 }
887 T.consumeClose();
888 }
Alexey Bataev80909872015-07-02 11:25:17 +0000889 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000890 CancelRegion = ParseOpenMPDirectiveKind(*this);
891 if (Tok.isNot(tok::annot_pragma_openmp_end))
892 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000893 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000894
Alexey Bataevf29276e2014-06-18 04:14:57 +0000895 if (isOpenMPLoopDirective(DKind))
896 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
897 if (isOpenMPSimdDirective(DKind))
898 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
899 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000900 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000901
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000902 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000903 OpenMPClauseKind CKind =
904 Tok.isAnnotation()
905 ? OMPC_unknown
906 : FlushHasClause ? OMPC_flush
907 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000908 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000909 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000910 OMPClause *Clause =
911 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000912 FirstClauses[CKind].setInt(true);
913 if (Clause) {
914 FirstClauses[CKind].setPointer(Clause);
915 Clauses.push_back(Clause);
916 }
917
918 // Skip ',' if any.
919 if (Tok.is(tok::comma))
920 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000921 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000922 }
923 // End location of the directive.
924 EndLoc = Tok.getLocation();
925 // Consume final annot_pragma_openmp_end.
926 ConsumeToken();
927
Alexey Bataeveb482352015-12-18 05:05:56 +0000928 // OpenMP [2.13.8, ordered Construct, Syntax]
929 // If the depend clause is specified, the ordered construct is a stand-alone
930 // directive.
931 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000932 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000933 Diag(Loc, diag::err_omp_immediate_directive)
934 << getOpenMPDirectiveName(DKind) << 1
935 << getOpenMPClauseName(OMPC_depend);
936 }
937 HasAssociatedStatement = false;
938 }
939
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000940 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000941 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000942 // The body is a block scope like in Lambdas and Blocks.
943 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000944 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000945 Actions.ActOnStartOfCompoundStmt();
946 // Parse statement
947 AssociatedStmt = ParseStatement();
948 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000949 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000950 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000951 Directive = Actions.ActOnOpenMPExecutableDirective(
952 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
953 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000954
955 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000956 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000957 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000958 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000959 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000960 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000961 case OMPD_declare_target:
962 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +0000963 Diag(Tok, diag::err_omp_unexpected_directive)
964 << getOpenMPDirectiveName(DKind);
965 SkipUntil(tok::annot_pragma_openmp_end);
966 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000967 case OMPD_unknown:
968 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000969 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000970 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000971 }
972 return Directive;
973}
974
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000975// Parses simple list:
976// simple-variable-list:
977// '(' id-expression {, id-expression} ')'
978//
979bool Parser::ParseOpenMPSimpleVarList(
980 OpenMPDirectiveKind Kind,
981 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
982 Callback,
983 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000984 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000985 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000986 if (T.expectAndConsume(diag::err_expected_lparen_after,
987 getOpenMPDirectiveName(Kind)))
988 return true;
989 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000990 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000991
992 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000993 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000994 CXXScopeSpec SS;
995 SourceLocation TemplateKWLoc;
996 UnqualifiedId Name;
997 // Read var name.
998 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000999 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001000
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001001 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001002 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001003 IsCorrect = false;
1004 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001005 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +00001006 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001007 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001008 IsCorrect = false;
1009 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001010 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001011 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1012 Tok.isNot(tok::annot_pragma_openmp_end)) {
1013 IsCorrect = false;
1014 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001015 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001016 Diag(PrevTok.getLocation(), diag::err_expected)
1017 << tok::identifier
1018 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001019 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001020 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001021 }
1022 // Consume ','.
1023 if (Tok.is(tok::comma)) {
1024 ConsumeToken();
1025 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001026 }
1027
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001028 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001029 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001030 IsCorrect = false;
1031 }
1032
1033 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001034 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001035
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001036 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001037}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001038
1039/// \brief Parsing of OpenMP clauses.
1040///
1041/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001042/// if-clause | final-clause | num_threads-clause | safelen-clause |
1043/// default-clause | private-clause | firstprivate-clause | shared-clause
1044/// | linear-clause | aligned-clause | collapse-clause |
1045/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001046/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001047/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001048/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001049/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001050/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001051/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
1052/// from-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001053///
1054OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1055 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001056 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001057 bool ErrorFound = false;
1058 // Check if clause is allowed for the given directive.
1059 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001060 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1061 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001062 ErrorFound = true;
1063 }
1064
1065 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001066 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001067 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001068 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001069 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001070 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001071 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001072 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001073 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001074 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001075 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001076 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001077 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001078 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001079 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001080 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001081 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001082 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001083 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001084 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001085 // OpenMP [2.9.1, target data construct, Restrictions]
1086 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001087 // OpenMP [2.11.1, task Construct, Restrictions]
1088 // At most one if clause can appear on the directive.
1089 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001090 // OpenMP [teams Construct, Restrictions]
1091 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001092 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001093 // OpenMP [2.9.1, task Construct, Restrictions]
1094 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001095 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1096 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001097 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1098 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001099 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001100 Diag(Tok, diag::err_omp_more_one_clause)
1101 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001102 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001103 }
1104
Alexey Bataev10e775f2015-07-30 11:36:16 +00001105 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1106 Clause = ParseOpenMPClause(CKind);
1107 else
1108 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001109 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001110 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001111 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001112 // OpenMP [2.14.3.1, Restrictions]
1113 // Only a single default clause may be specified on a parallel, task or
1114 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001115 // OpenMP [2.5, parallel Construct, Restrictions]
1116 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001117 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001118 Diag(Tok, diag::err_omp_more_one_clause)
1119 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001120 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001121 }
1122
1123 Clause = ParseOpenMPSimpleClause(CKind);
1124 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001125 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001126 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001127 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001128 // OpenMP [2.7.1, Restrictions, p. 3]
1129 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001130 // OpenMP [2.10.4, Restrictions, p. 106]
1131 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001132 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001133 Diag(Tok, diag::err_omp_more_one_clause)
1134 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001135 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001136 }
1137
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001138 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001139 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1140 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001141 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001142 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001143 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001144 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001145 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001146 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001147 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001148 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001149 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001150 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001151 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001152 // OpenMP [2.7.1, Restrictions, p. 9]
1153 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001154 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1155 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001156 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001157 Diag(Tok, diag::err_omp_more_one_clause)
1158 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001159 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001160 }
1161
1162 Clause = ParseOpenMPClause(CKind);
1163 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001164 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001165 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001166 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001167 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001168 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001169 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001170 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001171 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001172 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001173 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001174 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001175 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001176 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001177 case OMPC_from:
Alexey Bataeveb482352015-12-18 05:05:56 +00001178 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001179 break;
1180 case OMPC_unknown:
1181 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001182 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001183 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001184 break;
1185 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001186 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001187 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1188 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001189 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001190 break;
1191 }
Craig Topper161e4db2014-05-21 06:02:52 +00001192 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001193}
1194
Alexey Bataev2af33e32016-04-07 12:45:37 +00001195/// Parses simple expression in parens for single-expression clauses of OpenMP
1196/// constructs.
1197/// \param RLoc Returned location of right paren.
1198ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1199 SourceLocation &RLoc) {
1200 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1201 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1202 return ExprError();
1203
1204 SourceLocation ELoc = Tok.getLocation();
1205 ExprResult LHS(ParseCastExpression(
1206 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1207 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1208 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1209
1210 // Parse ')'.
1211 T.consumeClose();
1212
1213 RLoc = T.getCloseLocation();
1214 return Val;
1215}
1216
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001217/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001218/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001219/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001220///
Alexey Bataev3778b602014-07-17 07:32:53 +00001221/// final-clause:
1222/// 'final' '(' expression ')'
1223///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001224/// num_threads-clause:
1225/// 'num_threads' '(' expression ')'
1226///
1227/// safelen-clause:
1228/// 'safelen' '(' expression ')'
1229///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001230/// simdlen-clause:
1231/// 'simdlen' '(' expression ')'
1232///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001233/// collapse-clause:
1234/// 'collapse' '(' expression ')'
1235///
Alexey Bataeva0569352015-12-01 10:17:31 +00001236/// priority-clause:
1237/// 'priority' '(' expression ')'
1238///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001239/// grainsize-clause:
1240/// 'grainsize' '(' expression ')'
1241///
Alexey Bataev382967a2015-12-08 12:06:20 +00001242/// num_tasks-clause:
1243/// 'num_tasks' '(' expression ')'
1244///
Alexey Bataev28c75412015-12-15 08:19:24 +00001245/// hint-clause:
1246/// 'hint' '(' expression ')'
1247///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001248OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1249 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001250 SourceLocation LLoc = Tok.getLocation();
1251 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001252
Alexey Bataev2af33e32016-04-07 12:45:37 +00001253 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001254
1255 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001256 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001257
Alexey Bataev2af33e32016-04-07 12:45:37 +00001258 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001259}
1260
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001261/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001262///
1263/// default-clause:
1264/// 'default' '(' 'none' | 'shared' ')
1265///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001266/// proc_bind-clause:
1267/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1268///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001269OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1270 SourceLocation Loc = Tok.getLocation();
1271 SourceLocation LOpen = ConsumeToken();
1272 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001273 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001274 if (T.expectAndConsume(diag::err_expected_lparen_after,
1275 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001276 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001277
Alexey Bataeva55ed262014-05-28 06:15:33 +00001278 unsigned Type = getOpenMPSimpleClauseType(
1279 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001280 SourceLocation TypeLoc = Tok.getLocation();
1281 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1282 Tok.isNot(tok::annot_pragma_openmp_end))
1283 ConsumeAnyToken();
1284
1285 // Parse ')'.
1286 T.consumeClose();
1287
1288 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1289 Tok.getLocation());
1290}
1291
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001292/// \brief Parsing of OpenMP clauses like 'ordered'.
1293///
1294/// ordered-clause:
1295/// 'ordered'
1296///
Alexey Bataev236070f2014-06-20 11:19:47 +00001297/// nowait-clause:
1298/// 'nowait'
1299///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001300/// untied-clause:
1301/// 'untied'
1302///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001303/// mergeable-clause:
1304/// 'mergeable'
1305///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001306/// read-clause:
1307/// 'read'
1308///
Alexey Bataev346265e2015-09-25 10:37:12 +00001309/// threads-clause:
1310/// 'threads'
1311///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001312/// simd-clause:
1313/// 'simd'
1314///
Alexey Bataevb825de12015-12-07 10:51:44 +00001315/// nogroup-clause:
1316/// 'nogroup'
1317///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001318OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1319 SourceLocation Loc = Tok.getLocation();
1320 ConsumeAnyToken();
1321
1322 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1323}
1324
1325
Alexey Bataev56dafe82014-06-20 07:16:17 +00001326/// \brief Parsing of OpenMP clauses with single expressions and some additional
1327/// argument like 'schedule' or 'dist_schedule'.
1328///
1329/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001330/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1331/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001332///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001333/// if-clause:
1334/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1335///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001336/// defaultmap:
1337/// 'defaultmap' '(' modifier ':' kind ')'
1338///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001339OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1340 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001341 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001342 // Parse '('.
1343 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1344 if (T.expectAndConsume(diag::err_expected_lparen_after,
1345 getOpenMPClauseName(Kind)))
1346 return nullptr;
1347
1348 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001349 SmallVector<unsigned, 4> Arg;
1350 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001351 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001352 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1353 Arg.resize(NumberOfElements);
1354 KLoc.resize(NumberOfElements);
1355 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1356 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1357 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1358 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001359 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001360 if (KindModifier > OMPC_SCHEDULE_unknown) {
1361 // Parse 'modifier'
1362 Arg[Modifier1] = KindModifier;
1363 KLoc[Modifier1] = Tok.getLocation();
1364 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1365 Tok.isNot(tok::annot_pragma_openmp_end))
1366 ConsumeAnyToken();
1367 if (Tok.is(tok::comma)) {
1368 // Parse ',' 'modifier'
1369 ConsumeAnyToken();
1370 KindModifier = getOpenMPSimpleClauseType(
1371 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1372 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1373 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001374 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001375 KLoc[Modifier2] = Tok.getLocation();
1376 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1377 Tok.isNot(tok::annot_pragma_openmp_end))
1378 ConsumeAnyToken();
1379 }
1380 // Parse ':'
1381 if (Tok.is(tok::colon))
1382 ConsumeAnyToken();
1383 else
1384 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1385 KindModifier = getOpenMPSimpleClauseType(
1386 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1387 }
1388 Arg[ScheduleKind] = KindModifier;
1389 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001390 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1391 Tok.isNot(tok::annot_pragma_openmp_end))
1392 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001393 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1394 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1395 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001396 Tok.is(tok::comma))
1397 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001398 } else if (Kind == OMPC_dist_schedule) {
1399 Arg.push_back(getOpenMPSimpleClauseType(
1400 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1401 KLoc.push_back(Tok.getLocation());
1402 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1403 Tok.isNot(tok::annot_pragma_openmp_end))
1404 ConsumeAnyToken();
1405 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1406 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001407 } else if (Kind == OMPC_defaultmap) {
1408 // Get a defaultmap modifier
1409 Arg.push_back(getOpenMPSimpleClauseType(
1410 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1411 KLoc.push_back(Tok.getLocation());
1412 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1413 Tok.isNot(tok::annot_pragma_openmp_end))
1414 ConsumeAnyToken();
1415 // Parse ':'
1416 if (Tok.is(tok::colon))
1417 ConsumeAnyToken();
1418 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1419 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1420 // Get a defaultmap kind
1421 Arg.push_back(getOpenMPSimpleClauseType(
1422 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1423 KLoc.push_back(Tok.getLocation());
1424 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1425 Tok.isNot(tok::annot_pragma_openmp_end))
1426 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001427 } else {
1428 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001429 KLoc.push_back(Tok.getLocation());
1430 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1431 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001432 ConsumeToken();
1433 if (Tok.is(tok::colon))
1434 DelimLoc = ConsumeToken();
1435 else
1436 Diag(Tok, diag::warn_pragma_expected_colon)
1437 << "directive name modifier";
1438 }
1439 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001440
Carlo Bertollib4adf552016-01-15 18:50:31 +00001441 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1442 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1443 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001444 if (NeedAnExpression) {
1445 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001446 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1447 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001448 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001449 }
1450
1451 // Parse ')'.
1452 T.consumeClose();
1453
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001454 if (NeedAnExpression && Val.isInvalid())
1455 return nullptr;
1456
Alexey Bataev56dafe82014-06-20 07:16:17 +00001457 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001458 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001459 T.getCloseLocation());
1460}
1461
Alexey Bataevc5e02582014-06-16 07:08:35 +00001462static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1463 UnqualifiedId &ReductionId) {
1464 SourceLocation TemplateKWLoc;
1465 if (ReductionIdScopeSpec.isEmpty()) {
1466 auto OOK = OO_None;
1467 switch (P.getCurToken().getKind()) {
1468 case tok::plus:
1469 OOK = OO_Plus;
1470 break;
1471 case tok::minus:
1472 OOK = OO_Minus;
1473 break;
1474 case tok::star:
1475 OOK = OO_Star;
1476 break;
1477 case tok::amp:
1478 OOK = OO_Amp;
1479 break;
1480 case tok::pipe:
1481 OOK = OO_Pipe;
1482 break;
1483 case tok::caret:
1484 OOK = OO_Caret;
1485 break;
1486 case tok::ampamp:
1487 OOK = OO_AmpAmp;
1488 break;
1489 case tok::pipepipe:
1490 OOK = OO_PipePipe;
1491 break;
1492 default:
1493 break;
1494 }
1495 if (OOK != OO_None) {
1496 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001497 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001498 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1499 return false;
1500 }
1501 }
1502 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1503 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001504 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001505 TemplateKWLoc, ReductionId);
1506}
1507
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001508/// Parses clauses with list.
1509bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1510 OpenMPClauseKind Kind,
1511 SmallVectorImpl<Expr *> &Vars,
1512 OpenMPVarListDataTy &Data) {
1513 UnqualifiedId UnqualifiedReductionId;
1514 bool InvalidReductionId = false;
1515 bool MapTypeModifierSpecified = false;
1516
1517 // Parse '('.
1518 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1519 if (T.expectAndConsume(diag::err_expected_lparen_after,
1520 getOpenMPClauseName(Kind)))
1521 return true;
1522
1523 bool NeedRParenForLinear = false;
1524 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1525 tok::annot_pragma_openmp_end);
1526 // Handle reduction-identifier for reduction clause.
1527 if (Kind == OMPC_reduction) {
1528 ColonProtectionRAIIObject ColonRAII(*this);
1529 if (getLangOpts().CPlusPlus)
1530 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1531 /*ObjectType=*/nullptr,
1532 /*EnteringContext=*/false);
1533 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1534 UnqualifiedReductionId);
1535 if (InvalidReductionId) {
1536 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1537 StopBeforeMatch);
1538 }
1539 if (Tok.is(tok::colon))
1540 Data.ColonLoc = ConsumeToken();
1541 else
1542 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1543 if (!InvalidReductionId)
1544 Data.ReductionId =
1545 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1546 } else if (Kind == OMPC_depend) {
1547 // Handle dependency type for depend clause.
1548 ColonProtectionRAIIObject ColonRAII(*this);
1549 Data.DepKind =
1550 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1551 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1552 Data.DepLinMapLoc = Tok.getLocation();
1553
1554 if (Data.DepKind == OMPC_DEPEND_unknown) {
1555 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1556 StopBeforeMatch);
1557 } else {
1558 ConsumeToken();
1559 // Special processing for depend(source) clause.
1560 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1561 // Parse ')'.
1562 T.consumeClose();
1563 return false;
1564 }
1565 }
1566 if (Tok.is(tok::colon))
1567 Data.ColonLoc = ConsumeToken();
1568 else {
1569 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1570 : diag::warn_pragma_expected_colon)
1571 << "dependency type";
1572 }
1573 } else if (Kind == OMPC_linear) {
1574 // Try to parse modifier if any.
1575 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1576 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1577 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1578 Data.DepLinMapLoc = ConsumeToken();
1579 LinearT.consumeOpen();
1580 NeedRParenForLinear = true;
1581 }
1582 } else if (Kind == OMPC_map) {
1583 // Handle map type for map clause.
1584 ColonProtectionRAIIObject ColonRAII(*this);
1585
1586 /// The map clause modifier token can be either a identifier or the C++
1587 /// delete keyword.
1588 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1589 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1590 };
1591
1592 // The first identifier may be a list item, a map-type or a
1593 // map-type-modifier. The map modifier can also be delete which has the same
1594 // spelling of the C++ delete keyword.
1595 Data.MapType =
1596 IsMapClauseModifierToken(Tok)
1597 ? static_cast<OpenMPMapClauseKind>(
1598 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1599 : OMPC_MAP_unknown;
1600 Data.DepLinMapLoc = Tok.getLocation();
1601 bool ColonExpected = false;
1602
1603 if (IsMapClauseModifierToken(Tok)) {
1604 if (PP.LookAhead(0).is(tok::colon)) {
1605 if (Data.MapType == OMPC_MAP_unknown)
1606 Diag(Tok, diag::err_omp_unknown_map_type);
1607 else if (Data.MapType == OMPC_MAP_always)
1608 Diag(Tok, diag::err_omp_map_type_missing);
1609 ConsumeToken();
1610 } else if (PP.LookAhead(0).is(tok::comma)) {
1611 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1612 PP.LookAhead(2).is(tok::colon)) {
1613 Data.MapTypeModifier = Data.MapType;
1614 if (Data.MapTypeModifier != OMPC_MAP_always) {
1615 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1616 Data.MapTypeModifier = OMPC_MAP_unknown;
1617 } else
1618 MapTypeModifierSpecified = true;
1619
1620 ConsumeToken();
1621 ConsumeToken();
1622
1623 Data.MapType =
1624 IsMapClauseModifierToken(Tok)
1625 ? static_cast<OpenMPMapClauseKind>(
1626 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1627 : OMPC_MAP_unknown;
1628 if (Data.MapType == OMPC_MAP_unknown ||
1629 Data.MapType == OMPC_MAP_always)
1630 Diag(Tok, diag::err_omp_unknown_map_type);
1631 ConsumeToken();
1632 } else {
1633 Data.MapType = OMPC_MAP_tofrom;
1634 Data.IsMapTypeImplicit = true;
1635 }
1636 } else {
1637 Data.MapType = OMPC_MAP_tofrom;
1638 Data.IsMapTypeImplicit = true;
1639 }
1640 } else {
1641 Data.MapType = OMPC_MAP_tofrom;
1642 Data.IsMapTypeImplicit = true;
1643 }
1644
1645 if (Tok.is(tok::colon))
1646 Data.ColonLoc = ConsumeToken();
1647 else if (ColonExpected)
1648 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1649 }
1650
1651 bool IsComma =
1652 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1653 (Kind == OMPC_reduction && !InvalidReductionId) ||
1654 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1655 (!MapTypeModifierSpecified ||
1656 Data.MapTypeModifier == OMPC_MAP_always)) ||
1657 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1658 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1659 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1660 Tok.isNot(tok::annot_pragma_openmp_end))) {
1661 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1662 // Parse variable
1663 ExprResult VarExpr =
1664 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1665 if (VarExpr.isUsable())
1666 Vars.push_back(VarExpr.get());
1667 else {
1668 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1669 StopBeforeMatch);
1670 }
1671 // Skip ',' if any
1672 IsComma = Tok.is(tok::comma);
1673 if (IsComma)
1674 ConsumeToken();
1675 else if (Tok.isNot(tok::r_paren) &&
1676 Tok.isNot(tok::annot_pragma_openmp_end) &&
1677 (!MayHaveTail || Tok.isNot(tok::colon)))
1678 Diag(Tok, diag::err_omp_expected_punc)
1679 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1680 : getOpenMPClauseName(Kind))
1681 << (Kind == OMPC_flush);
1682 }
1683
1684 // Parse ')' for linear clause with modifier.
1685 if (NeedRParenForLinear)
1686 LinearT.consumeClose();
1687
1688 // Parse ':' linear-step (or ':' alignment).
1689 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1690 if (MustHaveTail) {
1691 Data.ColonLoc = Tok.getLocation();
1692 SourceLocation ELoc = ConsumeToken();
1693 ExprResult Tail = ParseAssignmentExpression();
1694 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1695 if (Tail.isUsable())
1696 Data.TailExpr = Tail.get();
1697 else
1698 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1699 StopBeforeMatch);
1700 }
1701
1702 // Parse ')'.
1703 T.consumeClose();
1704 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1705 Vars.empty()) ||
1706 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1707 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1708 return true;
1709 return false;
1710}
1711
Alexander Musman1bb328c2014-06-04 13:06:39 +00001712/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001713/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001714///
1715/// private-clause:
1716/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001717/// firstprivate-clause:
1718/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001719/// lastprivate-clause:
1720/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001721/// shared-clause:
1722/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001723/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001724/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001725/// aligned-clause:
1726/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001727/// reduction-clause:
1728/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001729/// copyprivate-clause:
1730/// 'copyprivate' '(' list ')'
1731/// flush-clause:
1732/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001733/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001734/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001735/// map-clause:
1736/// 'map' '(' [ [ always , ]
1737/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001738/// to-clause:
1739/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001740/// from-clause:
1741/// 'from' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001742///
Alexey Bataev182227b2015-08-20 10:54:39 +00001743/// For 'linear' clause linear-list may have the following forms:
1744/// list
1745/// modifier(list)
1746/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001747OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1748 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001749 SourceLocation Loc = Tok.getLocation();
1750 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001751 SmallVector<Expr *, 4> Vars;
1752 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001753
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001754 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001755 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001756
Alexey Bataevc5e02582014-06-16 07:08:35 +00001757 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001758 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1759 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1760 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1761 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001762}
1763