blob: 80fb0df3b637d9af4f7bb6eaa053241d95bed3f1 [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 |
Carlo Bertolli70594e92016-07-13 17:16:49 +00001060/// from-clause | is_device_ptr-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:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001186 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001187 case OMPC_is_device_ptr:
Alexey Bataeveb482352015-12-18 05:05:56 +00001188 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001189 break;
1190 case OMPC_unknown:
1191 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001192 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001193 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001194 break;
1195 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001196 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001197 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1198 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001199 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001200 break;
1201 }
Craig Topper161e4db2014-05-21 06:02:52 +00001202 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001203}
1204
Alexey Bataev2af33e32016-04-07 12:45:37 +00001205/// Parses simple expression in parens for single-expression clauses of OpenMP
1206/// constructs.
1207/// \param RLoc Returned location of right paren.
1208ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1209 SourceLocation &RLoc) {
1210 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1211 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1212 return ExprError();
1213
1214 SourceLocation ELoc = Tok.getLocation();
1215 ExprResult LHS(ParseCastExpression(
1216 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1217 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1218 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1219
1220 // Parse ')'.
1221 T.consumeClose();
1222
1223 RLoc = T.getCloseLocation();
1224 return Val;
1225}
1226
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001227/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001228/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001229/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001230///
Alexey Bataev3778b602014-07-17 07:32:53 +00001231/// final-clause:
1232/// 'final' '(' expression ')'
1233///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001234/// num_threads-clause:
1235/// 'num_threads' '(' expression ')'
1236///
1237/// safelen-clause:
1238/// 'safelen' '(' expression ')'
1239///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001240/// simdlen-clause:
1241/// 'simdlen' '(' expression ')'
1242///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001243/// collapse-clause:
1244/// 'collapse' '(' expression ')'
1245///
Alexey Bataeva0569352015-12-01 10:17:31 +00001246/// priority-clause:
1247/// 'priority' '(' expression ')'
1248///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001249/// grainsize-clause:
1250/// 'grainsize' '(' expression ')'
1251///
Alexey Bataev382967a2015-12-08 12:06:20 +00001252/// num_tasks-clause:
1253/// 'num_tasks' '(' expression ')'
1254///
Alexey Bataev28c75412015-12-15 08:19:24 +00001255/// hint-clause:
1256/// 'hint' '(' expression ')'
1257///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001258OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1259 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001260 SourceLocation LLoc = Tok.getLocation();
1261 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001262
Alexey Bataev2af33e32016-04-07 12:45:37 +00001263 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001264
1265 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001266 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001267
Alexey Bataev2af33e32016-04-07 12:45:37 +00001268 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001269}
1270
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001271/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001272///
1273/// default-clause:
1274/// 'default' '(' 'none' | 'shared' ')
1275///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001276/// proc_bind-clause:
1277/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1278///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001279OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1280 SourceLocation Loc = Tok.getLocation();
1281 SourceLocation LOpen = ConsumeToken();
1282 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001283 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001284 if (T.expectAndConsume(diag::err_expected_lparen_after,
1285 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001286 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001287
Alexey Bataeva55ed262014-05-28 06:15:33 +00001288 unsigned Type = getOpenMPSimpleClauseType(
1289 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001290 SourceLocation TypeLoc = Tok.getLocation();
1291 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1292 Tok.isNot(tok::annot_pragma_openmp_end))
1293 ConsumeAnyToken();
1294
1295 // Parse ')'.
1296 T.consumeClose();
1297
1298 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1299 Tok.getLocation());
1300}
1301
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001302/// \brief Parsing of OpenMP clauses like 'ordered'.
1303///
1304/// ordered-clause:
1305/// 'ordered'
1306///
Alexey Bataev236070f2014-06-20 11:19:47 +00001307/// nowait-clause:
1308/// 'nowait'
1309///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001310/// untied-clause:
1311/// 'untied'
1312///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001313/// mergeable-clause:
1314/// 'mergeable'
1315///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001316/// read-clause:
1317/// 'read'
1318///
Alexey Bataev346265e2015-09-25 10:37:12 +00001319/// threads-clause:
1320/// 'threads'
1321///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001322/// simd-clause:
1323/// 'simd'
1324///
Alexey Bataevb825de12015-12-07 10:51:44 +00001325/// nogroup-clause:
1326/// 'nogroup'
1327///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001328OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1329 SourceLocation Loc = Tok.getLocation();
1330 ConsumeAnyToken();
1331
1332 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1333}
1334
1335
Alexey Bataev56dafe82014-06-20 07:16:17 +00001336/// \brief Parsing of OpenMP clauses with single expressions and some additional
1337/// argument like 'schedule' or 'dist_schedule'.
1338///
1339/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001340/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1341/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001342///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001343/// if-clause:
1344/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1345///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001346/// defaultmap:
1347/// 'defaultmap' '(' modifier ':' kind ')'
1348///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001349OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1350 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001351 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001352 // Parse '('.
1353 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1354 if (T.expectAndConsume(diag::err_expected_lparen_after,
1355 getOpenMPClauseName(Kind)))
1356 return nullptr;
1357
1358 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001359 SmallVector<unsigned, 4> Arg;
1360 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001361 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001362 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1363 Arg.resize(NumberOfElements);
1364 KLoc.resize(NumberOfElements);
1365 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1366 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1367 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1368 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001369 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001370 if (KindModifier > OMPC_SCHEDULE_unknown) {
1371 // Parse 'modifier'
1372 Arg[Modifier1] = KindModifier;
1373 KLoc[Modifier1] = Tok.getLocation();
1374 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1375 Tok.isNot(tok::annot_pragma_openmp_end))
1376 ConsumeAnyToken();
1377 if (Tok.is(tok::comma)) {
1378 // Parse ',' 'modifier'
1379 ConsumeAnyToken();
1380 KindModifier = getOpenMPSimpleClauseType(
1381 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1382 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1383 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001384 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001385 KLoc[Modifier2] = Tok.getLocation();
1386 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1387 Tok.isNot(tok::annot_pragma_openmp_end))
1388 ConsumeAnyToken();
1389 }
1390 // Parse ':'
1391 if (Tok.is(tok::colon))
1392 ConsumeAnyToken();
1393 else
1394 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1395 KindModifier = getOpenMPSimpleClauseType(
1396 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1397 }
1398 Arg[ScheduleKind] = KindModifier;
1399 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001400 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1401 Tok.isNot(tok::annot_pragma_openmp_end))
1402 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001403 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1404 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1405 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001406 Tok.is(tok::comma))
1407 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001408 } else if (Kind == OMPC_dist_schedule) {
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 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1416 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001417 } else if (Kind == OMPC_defaultmap) {
1418 // Get a defaultmap modifier
1419 Arg.push_back(getOpenMPSimpleClauseType(
1420 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1421 KLoc.push_back(Tok.getLocation());
1422 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1423 Tok.isNot(tok::annot_pragma_openmp_end))
1424 ConsumeAnyToken();
1425 // Parse ':'
1426 if (Tok.is(tok::colon))
1427 ConsumeAnyToken();
1428 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1429 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1430 // Get a defaultmap kind
1431 Arg.push_back(getOpenMPSimpleClauseType(
1432 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1433 KLoc.push_back(Tok.getLocation());
1434 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1435 Tok.isNot(tok::annot_pragma_openmp_end))
1436 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001437 } else {
1438 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001439 KLoc.push_back(Tok.getLocation());
1440 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1441 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001442 ConsumeToken();
1443 if (Tok.is(tok::colon))
1444 DelimLoc = ConsumeToken();
1445 else
1446 Diag(Tok, diag::warn_pragma_expected_colon)
1447 << "directive name modifier";
1448 }
1449 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001450
Carlo Bertollib4adf552016-01-15 18:50:31 +00001451 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1452 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1453 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001454 if (NeedAnExpression) {
1455 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001456 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1457 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001458 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001459 }
1460
1461 // Parse ')'.
1462 T.consumeClose();
1463
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001464 if (NeedAnExpression && Val.isInvalid())
1465 return nullptr;
1466
Alexey Bataev56dafe82014-06-20 07:16:17 +00001467 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001468 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001469 T.getCloseLocation());
1470}
1471
Alexey Bataevc5e02582014-06-16 07:08:35 +00001472static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1473 UnqualifiedId &ReductionId) {
1474 SourceLocation TemplateKWLoc;
1475 if (ReductionIdScopeSpec.isEmpty()) {
1476 auto OOK = OO_None;
1477 switch (P.getCurToken().getKind()) {
1478 case tok::plus:
1479 OOK = OO_Plus;
1480 break;
1481 case tok::minus:
1482 OOK = OO_Minus;
1483 break;
1484 case tok::star:
1485 OOK = OO_Star;
1486 break;
1487 case tok::amp:
1488 OOK = OO_Amp;
1489 break;
1490 case tok::pipe:
1491 OOK = OO_Pipe;
1492 break;
1493 case tok::caret:
1494 OOK = OO_Caret;
1495 break;
1496 case tok::ampamp:
1497 OOK = OO_AmpAmp;
1498 break;
1499 case tok::pipepipe:
1500 OOK = OO_PipePipe;
1501 break;
1502 default:
1503 break;
1504 }
1505 if (OOK != OO_None) {
1506 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001507 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001508 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1509 return false;
1510 }
1511 }
1512 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1513 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001514 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001515 TemplateKWLoc, ReductionId);
1516}
1517
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001518/// Parses clauses with list.
1519bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1520 OpenMPClauseKind Kind,
1521 SmallVectorImpl<Expr *> &Vars,
1522 OpenMPVarListDataTy &Data) {
1523 UnqualifiedId UnqualifiedReductionId;
1524 bool InvalidReductionId = false;
1525 bool MapTypeModifierSpecified = false;
1526
1527 // Parse '('.
1528 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1529 if (T.expectAndConsume(diag::err_expected_lparen_after,
1530 getOpenMPClauseName(Kind)))
1531 return true;
1532
1533 bool NeedRParenForLinear = false;
1534 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1535 tok::annot_pragma_openmp_end);
1536 // Handle reduction-identifier for reduction clause.
1537 if (Kind == OMPC_reduction) {
1538 ColonProtectionRAIIObject ColonRAII(*this);
1539 if (getLangOpts().CPlusPlus)
1540 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1541 /*ObjectType=*/nullptr,
1542 /*EnteringContext=*/false);
1543 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1544 UnqualifiedReductionId);
1545 if (InvalidReductionId) {
1546 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1547 StopBeforeMatch);
1548 }
1549 if (Tok.is(tok::colon))
1550 Data.ColonLoc = ConsumeToken();
1551 else
1552 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1553 if (!InvalidReductionId)
1554 Data.ReductionId =
1555 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1556 } else if (Kind == OMPC_depend) {
1557 // Handle dependency type for depend clause.
1558 ColonProtectionRAIIObject ColonRAII(*this);
1559 Data.DepKind =
1560 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1561 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1562 Data.DepLinMapLoc = Tok.getLocation();
1563
1564 if (Data.DepKind == OMPC_DEPEND_unknown) {
1565 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1566 StopBeforeMatch);
1567 } else {
1568 ConsumeToken();
1569 // Special processing for depend(source) clause.
1570 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1571 // Parse ')'.
1572 T.consumeClose();
1573 return false;
1574 }
1575 }
1576 if (Tok.is(tok::colon))
1577 Data.ColonLoc = ConsumeToken();
1578 else {
1579 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1580 : diag::warn_pragma_expected_colon)
1581 << "dependency type";
1582 }
1583 } else if (Kind == OMPC_linear) {
1584 // Try to parse modifier if any.
1585 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1586 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1587 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1588 Data.DepLinMapLoc = ConsumeToken();
1589 LinearT.consumeOpen();
1590 NeedRParenForLinear = true;
1591 }
1592 } else if (Kind == OMPC_map) {
1593 // Handle map type for map clause.
1594 ColonProtectionRAIIObject ColonRAII(*this);
1595
1596 /// The map clause modifier token can be either a identifier or the C++
1597 /// delete keyword.
1598 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1599 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1600 };
1601
1602 // The first identifier may be a list item, a map-type or a
1603 // map-type-modifier. The map modifier can also be delete which has the same
1604 // spelling of the C++ delete keyword.
1605 Data.MapType =
1606 IsMapClauseModifierToken(Tok)
1607 ? static_cast<OpenMPMapClauseKind>(
1608 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1609 : OMPC_MAP_unknown;
1610 Data.DepLinMapLoc = Tok.getLocation();
1611 bool ColonExpected = false;
1612
1613 if (IsMapClauseModifierToken(Tok)) {
1614 if (PP.LookAhead(0).is(tok::colon)) {
1615 if (Data.MapType == OMPC_MAP_unknown)
1616 Diag(Tok, diag::err_omp_unknown_map_type);
1617 else if (Data.MapType == OMPC_MAP_always)
1618 Diag(Tok, diag::err_omp_map_type_missing);
1619 ConsumeToken();
1620 } else if (PP.LookAhead(0).is(tok::comma)) {
1621 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1622 PP.LookAhead(2).is(tok::colon)) {
1623 Data.MapTypeModifier = Data.MapType;
1624 if (Data.MapTypeModifier != OMPC_MAP_always) {
1625 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1626 Data.MapTypeModifier = OMPC_MAP_unknown;
1627 } else
1628 MapTypeModifierSpecified = true;
1629
1630 ConsumeToken();
1631 ConsumeToken();
1632
1633 Data.MapType =
1634 IsMapClauseModifierToken(Tok)
1635 ? static_cast<OpenMPMapClauseKind>(
1636 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1637 : OMPC_MAP_unknown;
1638 if (Data.MapType == OMPC_MAP_unknown ||
1639 Data.MapType == OMPC_MAP_always)
1640 Diag(Tok, diag::err_omp_unknown_map_type);
1641 ConsumeToken();
1642 } else {
1643 Data.MapType = OMPC_MAP_tofrom;
1644 Data.IsMapTypeImplicit = true;
1645 }
1646 } else {
1647 Data.MapType = OMPC_MAP_tofrom;
1648 Data.IsMapTypeImplicit = true;
1649 }
1650 } else {
1651 Data.MapType = OMPC_MAP_tofrom;
1652 Data.IsMapTypeImplicit = true;
1653 }
1654
1655 if (Tok.is(tok::colon))
1656 Data.ColonLoc = ConsumeToken();
1657 else if (ColonExpected)
1658 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1659 }
1660
1661 bool IsComma =
1662 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1663 (Kind == OMPC_reduction && !InvalidReductionId) ||
1664 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1665 (!MapTypeModifierSpecified ||
1666 Data.MapTypeModifier == OMPC_MAP_always)) ||
1667 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1668 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1669 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1670 Tok.isNot(tok::annot_pragma_openmp_end))) {
1671 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1672 // Parse variable
1673 ExprResult VarExpr =
1674 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1675 if (VarExpr.isUsable())
1676 Vars.push_back(VarExpr.get());
1677 else {
1678 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1679 StopBeforeMatch);
1680 }
1681 // Skip ',' if any
1682 IsComma = Tok.is(tok::comma);
1683 if (IsComma)
1684 ConsumeToken();
1685 else if (Tok.isNot(tok::r_paren) &&
1686 Tok.isNot(tok::annot_pragma_openmp_end) &&
1687 (!MayHaveTail || Tok.isNot(tok::colon)))
1688 Diag(Tok, diag::err_omp_expected_punc)
1689 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1690 : getOpenMPClauseName(Kind))
1691 << (Kind == OMPC_flush);
1692 }
1693
1694 // Parse ')' for linear clause with modifier.
1695 if (NeedRParenForLinear)
1696 LinearT.consumeClose();
1697
1698 // Parse ':' linear-step (or ':' alignment).
1699 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1700 if (MustHaveTail) {
1701 Data.ColonLoc = Tok.getLocation();
1702 SourceLocation ELoc = ConsumeToken();
1703 ExprResult Tail = ParseAssignmentExpression();
1704 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1705 if (Tail.isUsable())
1706 Data.TailExpr = Tail.get();
1707 else
1708 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1709 StopBeforeMatch);
1710 }
1711
1712 // Parse ')'.
1713 T.consumeClose();
1714 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1715 Vars.empty()) ||
1716 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1717 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1718 return true;
1719 return false;
1720}
1721
Alexander Musman1bb328c2014-06-04 13:06:39 +00001722/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001723/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001724///
1725/// private-clause:
1726/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001727/// firstprivate-clause:
1728/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001729/// lastprivate-clause:
1730/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001731/// shared-clause:
1732/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001733/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001734/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001735/// aligned-clause:
1736/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001737/// reduction-clause:
1738/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001739/// copyprivate-clause:
1740/// 'copyprivate' '(' list ')'
1741/// flush-clause:
1742/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001743/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001744/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001745/// map-clause:
1746/// 'map' '(' [ [ always , ]
1747/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001748/// to-clause:
1749/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001750/// from-clause:
1751/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001752/// use_device_ptr-clause:
1753/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001754/// is_device_ptr-clause:
1755/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001756///
Alexey Bataev182227b2015-08-20 10:54:39 +00001757/// For 'linear' clause linear-list may have the following forms:
1758/// list
1759/// modifier(list)
1760/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001761OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1762 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001763 SourceLocation Loc = Tok.getLocation();
1764 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001765 SmallVector<Expr *, 4> Vars;
1766 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001767
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001768 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001769 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001770
Alexey Bataevc5e02582014-06-16 07:08:35 +00001771 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001772 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1773 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1774 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1775 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001776}
1777