blob: 9c5b54b92a5b9be3c71cd625e32d249b82f8ee2f [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements parsing of all OpenMP directives and clauses.
11///
12//===----------------------------------------------------------------------===//
13
Chandler Carruth5553d0d2014-01-07 11:51:46 +000014#include "RAIIObjectsForParser.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000016#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000017#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000018#include "clang/Parse/Parser.h"
19#include "clang/Sema/Scope.h"
20#include "llvm/ADT/PointerIntPair.h"
Michael Wong65f367f2015-07-21 13:44:28 +000021
Alexey Bataeva769e072013-03-22 06:34:35 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// OpenMP declarative directives.
26//===----------------------------------------------------------------------===//
27
Dmitry Polukhin82478332016-02-13 06:53:38 +000028namespace {
29enum OpenMPDirectiveKindEx {
30 OMPD_cancellation = OMPD_unknown + 1,
31 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000032 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000033 OMPD_end,
34 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000035 OMPD_enter,
36 OMPD_exit,
37 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000038 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000039 OMPD_target_enter,
Samuel Antao686c70c2016-05-26 17:30:50 +000040 OMPD_target_exit,
41 OMPD_update,
Kelvin Li579e41c2016-11-30 23:51:03 +000042 OMPD_distribute_parallel,
Kelvin Li7ade93f2016-12-09 03:24:30 +000043 OMPD_teams_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 },
Kelvin Li986330c2016-07-20 22:57:10 +0000110 { OMPD_target, OMPD_simd, OMPD_target_simd },
Kelvin Lia579b912016-07-14 02:54:56 +0000111 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for },
Kelvin Li02532872016-08-05 14:37:37 +0000112 { OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd },
Kelvin Li4e325f72016-10-25 12:50:55 +0000113 { OMPD_teams, OMPD_distribute, OMPD_teams_distribute },
Kelvin Li579e41c2016-11-30 23:51:03 +0000114 { OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd },
115 { OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel },
116 { OMPD_teams_distribute_parallel, OMPD_for, OMPD_teams_distribute_parallel_for },
117 { OMPD_teams_distribute_parallel_for, OMPD_simd, OMPD_teams_distribute_parallel_for_simd }
Dmitry Polukhin82478332016-02-13 06:53:38 +0000118 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000119 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +0000120 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000121 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000122 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000123 ? static_cast<unsigned>(OMPD_unknown)
124 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
125 if (DKind == OMPD_unknown)
126 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000127
Alexander Musmanf82886e2014-09-18 05:12:34 +0000128 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000129 if (DKind != F[i][0])
130 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000131
Dmitry Polukhin82478332016-02-13 06:53:38 +0000132 Tok = P.getPreprocessor().LookAhead(0);
133 unsigned SDKind =
134 Tok.isAnnotation()
135 ? static_cast<unsigned>(OMPD_unknown)
136 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
137 if (SDKind == OMPD_unknown)
138 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000139
Dmitry Polukhin82478332016-02-13 06:53:38 +0000140 if (SDKind == F[i][1]) {
141 P.ConsumeToken();
142 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000143 }
144 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000145 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
146 : OMPD_unknown;
147}
148
149static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000150 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000151 Sema &Actions = P.getActions();
152 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000153 // Allow to use 'operator' keyword for C++ operators
154 bool WithOperator = false;
155 if (Tok.is(tok::kw_operator)) {
156 P.ConsumeToken();
157 Tok = P.getCurToken();
158 WithOperator = true;
159 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000160 switch (Tok.getKind()) {
161 case tok::plus: // '+'
162 OOK = OO_Plus;
163 break;
164 case tok::minus: // '-'
165 OOK = OO_Minus;
166 break;
167 case tok::star: // '*'
168 OOK = OO_Star;
169 break;
170 case tok::amp: // '&'
171 OOK = OO_Amp;
172 break;
173 case tok::pipe: // '|'
174 OOK = OO_Pipe;
175 break;
176 case tok::caret: // '^'
177 OOK = OO_Caret;
178 break;
179 case tok::ampamp: // '&&'
180 OOK = OO_AmpAmp;
181 break;
182 case tok::pipepipe: // '||'
183 OOK = OO_PipePipe;
184 break;
185 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000186 if (!WithOperator)
187 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000188 default:
189 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
190 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
191 Parser::StopBeforeMatch);
192 return DeclarationName();
193 }
194 P.ConsumeToken();
195 auto &DeclNames = Actions.getASTContext().DeclarationNames;
196 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
197 : DeclNames.getCXXOperatorName(OOK);
198}
199
200/// \brief Parse 'omp declare reduction' construct.
201///
202/// declare-reduction-directive:
203/// annot_pragma_openmp 'declare' 'reduction'
204/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
205/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
206/// annot_pragma_openmp_end
207/// <reduction_id> is either a base language identifier or one of the following
208/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
209///
210Parser::DeclGroupPtrTy
211Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
212 // Parse '('.
213 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
214 if (T.expectAndConsume(diag::err_expected_lparen_after,
215 getOpenMPDirectiveName(OMPD_declare_reduction))) {
216 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
217 return DeclGroupPtrTy();
218 }
219
220 DeclarationName Name = parseOpenMPReductionId(*this);
221 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
222 return DeclGroupPtrTy();
223
224 // Consume ':'.
225 bool IsCorrect = !ExpectAndConsume(tok::colon);
226
227 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
228 return DeclGroupPtrTy();
229
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000230 IsCorrect = IsCorrect && !Name.isEmpty();
231
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000232 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
233 Diag(Tok.getLocation(), diag::err_expected_type);
234 IsCorrect = false;
235 }
236
237 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
238 return DeclGroupPtrTy();
239
240 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
241 // Parse list of types until ':' token.
242 do {
243 ColonProtectionRAIIObject ColonRAII(*this);
244 SourceRange Range;
245 TypeResult TR = ParseTypeName(&Range, Declarator::PrototypeContext, AS);
246 if (TR.isUsable()) {
247 auto ReductionType =
248 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
249 if (!ReductionType.isNull()) {
250 ReductionTypes.push_back(
251 std::make_pair(ReductionType, Range.getBegin()));
252 }
253 } else {
254 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
255 StopBeforeMatch);
256 }
257
258 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
259 break;
260
261 // Consume ','.
262 if (ExpectAndConsume(tok::comma)) {
263 IsCorrect = false;
264 if (Tok.is(tok::annot_pragma_openmp_end)) {
265 Diag(Tok.getLocation(), diag::err_expected_type);
266 return DeclGroupPtrTy();
267 }
268 }
269 } while (Tok.isNot(tok::annot_pragma_openmp_end));
270
271 if (ReductionTypes.empty()) {
272 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
273 return DeclGroupPtrTy();
274 }
275
276 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
277 return DeclGroupPtrTy();
278
279 // Consume ':'.
280 if (ExpectAndConsume(tok::colon))
281 IsCorrect = false;
282
283 if (Tok.is(tok::annot_pragma_openmp_end)) {
284 Diag(Tok.getLocation(), diag::err_expected_expression);
285 return DeclGroupPtrTy();
286 }
287
288 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
289 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
290
291 // Parse <combiner> expression and then parse initializer if any for each
292 // correct type.
293 unsigned I = 0, E = ReductionTypes.size();
294 for (auto *D : DRD.get()) {
295 TentativeParsingAction TPA(*this);
296 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
297 Scope::OpenMPDirectiveScope);
298 // Parse <combiner> expression.
299 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
300 ExprResult CombinerResult =
301 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
302 D->getLocation(), /*DiscardedValue=*/true);
303 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
304
305 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
306 Tok.isNot(tok::annot_pragma_openmp_end)) {
307 TPA.Commit();
308 IsCorrect = false;
309 break;
310 }
311 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
312 ExprResult InitializerResult;
313 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
314 // Parse <initializer> expression.
315 if (Tok.is(tok::identifier) &&
316 Tok.getIdentifierInfo()->isStr("initializer"))
317 ConsumeToken();
318 else {
319 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
320 TPA.Commit();
321 IsCorrect = false;
322 break;
323 }
324 // Parse '('.
325 BalancedDelimiterTracker T(*this, tok::l_paren,
326 tok::annot_pragma_openmp_end);
327 IsCorrect =
328 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
329 IsCorrect;
330 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
331 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
332 Scope::OpenMPDirectiveScope);
333 // Parse expression.
334 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), D);
335 InitializerResult = Actions.ActOnFinishFullExpr(
336 ParseAssignmentExpression().get(), D->getLocation(),
337 /*DiscardedValue=*/true);
338 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
339 D, InitializerResult.get());
340 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
341 Tok.isNot(tok::annot_pragma_openmp_end)) {
342 TPA.Commit();
343 IsCorrect = false;
344 break;
345 }
346 IsCorrect =
347 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
348 }
349 }
350
351 ++I;
352 // Revert parsing if not the last type, otherwise accept it, we're done with
353 // parsing.
354 if (I != E)
355 TPA.Revert();
356 else
357 TPA.Commit();
358 }
359 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
360 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000361}
362
Alexey Bataev2af33e32016-04-07 12:45:37 +0000363namespace {
364/// RAII that recreates function context for correct parsing of clauses of
365/// 'declare simd' construct.
366/// OpenMP, 2.8.2 declare simd Construct
367/// The expressions appearing in the clauses of this directive are evaluated in
368/// the scope of the arguments of the function declaration or definition.
369class FNContextRAII final {
370 Parser &P;
371 Sema::CXXThisScopeRAII *ThisScope;
372 Parser::ParseScope *TempScope;
373 Parser::ParseScope *FnScope;
374 bool HasTemplateScope = false;
375 bool HasFunScope = false;
376 FNContextRAII() = delete;
377 FNContextRAII(const FNContextRAII &) = delete;
378 FNContextRAII &operator=(const FNContextRAII &) = delete;
379
380public:
381 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
382 Decl *D = *Ptr.get().begin();
383 NamedDecl *ND = dyn_cast<NamedDecl>(D);
384 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
385 Sema &Actions = P.getActions();
386
387 // Allow 'this' within late-parsed attributes.
388 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
389 ND && ND->isCXXInstanceMember());
390
391 // If the Decl is templatized, add template parameters to scope.
392 HasTemplateScope = D->isTemplateDecl();
393 TempScope =
394 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
395 if (HasTemplateScope)
396 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
397
398 // If the Decl is on a function, add function parameters to the scope.
399 HasFunScope = D->isFunctionOrFunctionTemplate();
400 FnScope = new Parser::ParseScope(&P, Scope::FnScope | Scope::DeclScope,
401 HasFunScope);
402 if (HasFunScope)
403 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
404 }
405 ~FNContextRAII() {
406 if (HasFunScope) {
407 P.getActions().ActOnExitFunctionContext();
408 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
409 }
410 if (HasTemplateScope)
411 TempScope->Exit();
412 delete FnScope;
413 delete TempScope;
414 delete ThisScope;
415 }
416};
417} // namespace
418
Alexey Bataevd93d3762016-04-12 09:35:56 +0000419/// Parses clauses for 'declare simd' directive.
420/// clause:
421/// 'inbranch' | 'notinbranch'
422/// 'simdlen' '(' <expr> ')'
423/// { 'uniform' '(' <argument_list> ')' }
424/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000425/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
426static bool parseDeclareSimdClauses(
427 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
428 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
429 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
430 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000431 SourceRange BSRange;
432 const Token &Tok = P.getCurToken();
433 bool IsError = false;
434 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
435 if (Tok.isNot(tok::identifier))
436 break;
437 OMPDeclareSimdDeclAttr::BranchStateTy Out;
438 IdentifierInfo *II = Tok.getIdentifierInfo();
439 StringRef ClauseName = II->getName();
440 // Parse 'inranch|notinbranch' clauses.
441 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
442 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
443 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
444 << ClauseName
445 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
446 IsError = true;
447 }
448 BS = Out;
449 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
450 P.ConsumeToken();
451 } else if (ClauseName.equals("simdlen")) {
452 if (SimdLen.isUsable()) {
453 P.Diag(Tok, diag::err_omp_more_one_clause)
454 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
455 IsError = true;
456 }
457 P.ConsumeToken();
458 SourceLocation RLoc;
459 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
460 if (SimdLen.isInvalid())
461 IsError = true;
462 } else {
463 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000464 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
465 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000466 Parser::OpenMPVarListDataTy Data;
467 auto *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000468 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000469 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000470 else if (CKind == OMPC_linear)
471 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000472
473 P.ConsumeToken();
474 if (P.ParseOpenMPVarList(OMPD_declare_simd,
475 getOpenMPClauseKind(ClauseName), *Vars, Data))
476 IsError = true;
477 if (CKind == OMPC_aligned)
478 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000479 else if (CKind == OMPC_linear) {
480 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
481 Data.DepLinMapLoc))
482 Data.LinKind = OMPC_LINEAR_val;
483 LinModifiers.append(Linears.size() - LinModifiers.size(),
484 Data.LinKind);
485 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
486 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000487 } else
488 // TODO: add parsing of other clauses.
489 break;
490 }
491 // Skip ',' if any.
492 if (Tok.is(tok::comma))
493 P.ConsumeToken();
494 }
495 return IsError;
496}
497
Alexey Bataev2af33e32016-04-07 12:45:37 +0000498/// Parse clauses for '#pragma omp declare simd'.
499Parser::DeclGroupPtrTy
500Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
501 CachedTokens &Toks, SourceLocation Loc) {
502 PP.EnterToken(Tok);
503 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
504 // Consume the previously pushed token.
505 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
506
507 FNContextRAII FnContext(*this, Ptr);
508 OMPDeclareSimdDeclAttr::BranchStateTy BS =
509 OMPDeclareSimdDeclAttr::BS_Undefined;
510 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000511 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000512 SmallVector<Expr *, 4> Aligneds;
513 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000514 SmallVector<Expr *, 4> Linears;
515 SmallVector<unsigned, 4> LinModifiers;
516 SmallVector<Expr *, 4> Steps;
517 bool IsError =
518 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
519 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000520 // Need to check for extra tokens.
521 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
522 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
523 << getOpenMPDirectiveName(OMPD_declare_simd);
524 while (Tok.isNot(tok::annot_pragma_openmp_end))
525 ConsumeAnyToken();
526 }
527 // Skip the last annot_pragma_openmp_end.
528 SourceLocation EndLoc = ConsumeToken();
Alexey Bataevd93d3762016-04-12 09:35:56 +0000529 if (!IsError) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000530 return Actions.ActOnOpenMPDeclareSimdDirective(
Alexey Bataevecba70f2016-04-12 11:02:11 +0000531 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
532 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataevd93d3762016-04-12 09:35:56 +0000533 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000534 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000535}
536
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000537/// \brief Parsing of declarative OpenMP directives.
538///
539/// threadprivate-directive:
540/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000541/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000542///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000543/// declare-reduction-directive:
544/// annot_pragma_openmp 'declare' 'reduction' [...]
545/// annot_pragma_openmp_end
546///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000547/// declare-simd-directive:
548/// annot_pragma_openmp 'declare simd' {<clause> [,]}
549/// annot_pragma_openmp_end
550/// <function declaration/definition>
551///
552Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
553 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
554 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000555 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000556 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000557
558 SourceLocation Loc = ConsumeToken();
Alexey Bataev4acb8592014-07-07 13:01:15 +0000559 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000560
561 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000562 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000563 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000564 ThreadprivateListParserHelper Helper(this);
565 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000566 // The last seen token is annot_pragma_openmp_end - need to check for
567 // extra tokens.
568 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
569 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000570 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000571 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000572 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000573 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000574 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000575 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
576 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000577 }
578 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000579 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000580 case OMPD_declare_reduction:
581 ConsumeToken();
582 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
583 // The last seen token is annot_pragma_openmp_end - need to check for
584 // extra tokens.
585 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
586 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
587 << getOpenMPDirectiveName(OMPD_declare_reduction);
588 while (Tok.isNot(tok::annot_pragma_openmp_end))
589 ConsumeAnyToken();
590 }
591 // Skip the last annot_pragma_openmp_end.
592 ConsumeToken();
593 return Res;
594 }
595 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000596 case OMPD_declare_simd: {
597 // The syntax is:
598 // { #pragma omp declare simd }
599 // <function-declaration-or-definition>
600 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000601 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000602 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000603 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
604 Toks.push_back(Tok);
605 ConsumeAnyToken();
606 }
607 Toks.push_back(Tok);
608 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000609
610 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000611 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000612 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000613 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000614 // Here we expect to see some function declaration.
615 if (AS == AS_none) {
616 assert(TagType == DeclSpec::TST_unspecified);
617 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000618 ParsingDeclSpec PDS(*this);
619 Ptr = ParseExternalDeclaration(Attrs, &PDS);
620 } else {
621 Ptr =
622 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
623 }
624 }
625 if (!Ptr) {
626 Diag(Loc, diag::err_omp_decl_in_declare_simd);
627 return DeclGroupPtrTy();
628 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000629 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000630 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000631 case OMPD_declare_target: {
632 SourceLocation DTLoc = ConsumeAnyToken();
633 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000634 // OpenMP 4.5 syntax with list of entities.
635 llvm::SmallSetVector<const NamedDecl*, 16> SameDirectiveDecls;
636 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
637 OMPDeclareTargetDeclAttr::MapTypeTy MT =
638 OMPDeclareTargetDeclAttr::MT_To;
639 if (Tok.is(tok::identifier)) {
640 IdentifierInfo *II = Tok.getIdentifierInfo();
641 StringRef ClauseName = II->getName();
642 // Parse 'to|link' clauses.
643 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
644 MT)) {
645 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
646 << ClauseName;
647 break;
648 }
649 ConsumeToken();
650 }
651 auto Callback = [this, MT, &SameDirectiveDecls](
652 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
653 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
654 SameDirectiveDecls);
655 };
656 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, true))
657 break;
658
659 // Consume optional ','.
660 if (Tok.is(tok::comma))
661 ConsumeToken();
662 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000663 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000664 ConsumeAnyToken();
665 return DeclGroupPtrTy();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000666 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000667
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000668 // Skip the last annot_pragma_openmp_end.
669 ConsumeAnyToken();
670
671 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
672 return DeclGroupPtrTy();
673
674 DKind = ParseOpenMPDirectiveKind(*this);
675 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
676 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
677 ParsedAttributesWithRange attrs(AttrFactory);
678 MaybeParseCXX11Attributes(attrs);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000679 ParseExternalDeclaration(attrs);
680 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
681 TentativeParsingAction TPA(*this);
682 ConsumeToken();
683 DKind = ParseOpenMPDirectiveKind(*this);
684 if (DKind != OMPD_end_declare_target)
685 TPA.Revert();
686 else
687 TPA.Commit();
688 }
689 }
690
691 if (DKind == OMPD_end_declare_target) {
692 ConsumeAnyToken();
693 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
694 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
695 << getOpenMPDirectiveName(OMPD_end_declare_target);
696 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
697 }
698 // Skip the last annot_pragma_openmp_end.
699 ConsumeAnyToken();
700 } else {
701 Diag(Tok, diag::err_expected_end_declare_target);
702 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
703 }
704 Actions.ActOnFinishOpenMPDeclareTargetDirective();
705 return DeclGroupPtrTy();
706 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000707 case OMPD_unknown:
708 Diag(Tok, diag::err_omp_unknown_directive);
709 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000710 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000711 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000712 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000713 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000714 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000715 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000716 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000717 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000718 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000719 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000720 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000721 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000722 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000723 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000724 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000725 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000726 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000727 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000728 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000729 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000730 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000731 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000732 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000733 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000734 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000735 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000736 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000737 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000738 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000739 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000740 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000741 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000742 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000743 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000744 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000745 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000746 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000747 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000748 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000749 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000750 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000751 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000752 case OMPD_teams_distribute_parallel_for:
Alexey Bataeva769e072013-03-22 06:34:35 +0000753 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000754 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000755 break;
756 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000757 while (Tok.isNot(tok::annot_pragma_openmp_end))
758 ConsumeAnyToken();
759 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000760 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000761}
762
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000763/// \brief Parsing of declarative or executable OpenMP directives.
764///
765/// threadprivate-directive:
766/// annot_pragma_openmp 'threadprivate' simple-variable-list
767/// annot_pragma_openmp_end
768///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000769/// declare-reduction-directive:
770/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
771/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
772/// ('omp_priv' '=' <expression>|<function_call>) ')']
773/// annot_pragma_openmp_end
774///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000775/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000776/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000777/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
778/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000779/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000780/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000781/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000782/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000783/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000784/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000785/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000786/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000787/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000788/// 'teams distribute parallel for simd' |
789/// 'teams distribute parallel for' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000790/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000791///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000792StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
793 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000794 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000795 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000796 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000797 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000798 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000799 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000800 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000801 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000802 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000803 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000804 // Name of critical directive.
805 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000806 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000807 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000808 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000809
810 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000811 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000812 if (Allowed != ACK_Any) {
813 Diag(Tok, diag::err_omp_immediate_directive)
814 << getOpenMPDirectiveName(DKind) << 0;
815 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000816 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000817 ThreadprivateListParserHelper Helper(this);
818 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000819 // The last seen token is annot_pragma_openmp_end - need to check for
820 // extra tokens.
821 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
822 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000823 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000824 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000825 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000826 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
827 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000828 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
829 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000830 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000831 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000832 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000833 case OMPD_declare_reduction:
834 ConsumeToken();
835 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
836 // The last seen token is annot_pragma_openmp_end - need to check for
837 // extra tokens.
838 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
839 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
840 << getOpenMPDirectiveName(OMPD_declare_reduction);
841 while (Tok.isNot(tok::annot_pragma_openmp_end))
842 ConsumeAnyToken();
843 }
844 ConsumeAnyToken();
845 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
846 } else
847 SkipUntil(tok::annot_pragma_openmp_end);
848 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000849 case OMPD_flush:
850 if (PP.LookAhead(0).is(tok::l_paren)) {
851 FlushHasClause = true;
852 // Push copy of the current token back to stream to properly parse
853 // pseudo-clause OMPFlushClause.
854 PP.EnterToken(Tok);
855 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000856 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000857 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000858 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000859 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000860 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000861 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000862 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000863 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000864 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000865 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000866 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000867 }
868 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000869 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000870 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000871 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000872 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000873 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000874 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000875 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000876 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000877 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000878 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000879 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000880 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000881 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000882 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000883 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000884 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000885 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000886 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000887 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000888 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000889 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000890 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000891 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000892 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000893 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +0000894 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000895 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000896 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000897 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000898 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +0000899 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +0000900 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000901 case OMPD_teams_distribute_parallel_for_simd:
902 case OMPD_teams_distribute_parallel_for: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000903 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000904 // Parse directive name of the 'critical' directive if any.
905 if (DKind == OMPD_critical) {
906 BalancedDelimiterTracker T(*this, tok::l_paren,
907 tok::annot_pragma_openmp_end);
908 if (!T.consumeOpen()) {
909 if (Tok.isAnyIdentifier()) {
910 DirName =
911 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
912 ConsumeAnyToken();
913 } else {
914 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
915 }
916 T.consumeClose();
917 }
Alexey Bataev80909872015-07-02 11:25:17 +0000918 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000919 CancelRegion = ParseOpenMPDirectiveKind(*this);
920 if (Tok.isNot(tok::annot_pragma_openmp_end))
921 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000922 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000923
Alexey Bataevf29276e2014-06-18 04:14:57 +0000924 if (isOpenMPLoopDirective(DKind))
925 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
926 if (isOpenMPSimdDirective(DKind))
927 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
928 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000929 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000930
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000931 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000932 OpenMPClauseKind CKind =
933 Tok.isAnnotation()
934 ? OMPC_unknown
935 : FlushHasClause ? OMPC_flush
936 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000937 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000938 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000939 OMPClause *Clause =
940 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000941 FirstClauses[CKind].setInt(true);
942 if (Clause) {
943 FirstClauses[CKind].setPointer(Clause);
944 Clauses.push_back(Clause);
945 }
946
947 // Skip ',' if any.
948 if (Tok.is(tok::comma))
949 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000950 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000951 }
952 // End location of the directive.
953 EndLoc = Tok.getLocation();
954 // Consume final annot_pragma_openmp_end.
955 ConsumeToken();
956
Alexey Bataeveb482352015-12-18 05:05:56 +0000957 // OpenMP [2.13.8, ordered Construct, Syntax]
958 // If the depend clause is specified, the ordered construct is a stand-alone
959 // directive.
960 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000961 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000962 Diag(Loc, diag::err_omp_immediate_directive)
963 << getOpenMPDirectiveName(DKind) << 1
964 << getOpenMPClauseName(OMPC_depend);
965 }
966 HasAssociatedStatement = false;
967 }
968
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000969 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000970 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000971 // The body is a block scope like in Lambdas and Blocks.
972 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000973 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000974 Actions.ActOnStartOfCompoundStmt();
975 // Parse statement
976 AssociatedStmt = ParseStatement();
977 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000978 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000979 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000980 Directive = Actions.ActOnOpenMPExecutableDirective(
981 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
982 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000983
984 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000985 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000986 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000987 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000988 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000989 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000990 case OMPD_declare_target:
991 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +0000992 Diag(Tok, diag::err_omp_unexpected_directive)
993 << getOpenMPDirectiveName(DKind);
994 SkipUntil(tok::annot_pragma_openmp_end);
995 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000996 case OMPD_unknown:
997 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000998 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000999 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001000 }
1001 return Directive;
1002}
1003
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001004// Parses simple list:
1005// simple-variable-list:
1006// '(' id-expression {, id-expression} ')'
1007//
1008bool Parser::ParseOpenMPSimpleVarList(
1009 OpenMPDirectiveKind Kind,
1010 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1011 Callback,
1012 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001013 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001014 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001015 if (T.expectAndConsume(diag::err_expected_lparen_after,
1016 getOpenMPDirectiveName(Kind)))
1017 return true;
1018 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001019 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001020
1021 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001022 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001023 CXXScopeSpec SS;
1024 SourceLocation TemplateKWLoc;
1025 UnqualifiedId Name;
1026 // Read var name.
1027 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001028 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001029
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001030 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001031 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001032 IsCorrect = false;
1033 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001034 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +00001035 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001036 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001037 IsCorrect = false;
1038 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001039 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001040 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1041 Tok.isNot(tok::annot_pragma_openmp_end)) {
1042 IsCorrect = false;
1043 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001044 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001045 Diag(PrevTok.getLocation(), diag::err_expected)
1046 << tok::identifier
1047 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001048 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001049 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001050 }
1051 // Consume ','.
1052 if (Tok.is(tok::comma)) {
1053 ConsumeToken();
1054 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001055 }
1056
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001057 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001058 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001059 IsCorrect = false;
1060 }
1061
1062 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001063 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001064
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001065 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001066}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001067
1068/// \brief Parsing of OpenMP clauses.
1069///
1070/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001071/// if-clause | final-clause | num_threads-clause | safelen-clause |
1072/// default-clause | private-clause | firstprivate-clause | shared-clause
1073/// | linear-clause | aligned-clause | collapse-clause |
1074/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001075/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001076/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001077/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001078/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001079/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001080/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Carlo Bertolli70594e92016-07-13 17:16:49 +00001081/// from-clause | is_device_ptr-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001082///
1083OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1084 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001085 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001086 bool ErrorFound = false;
1087 // Check if clause is allowed for the given directive.
1088 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001089 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1090 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001091 ErrorFound = true;
1092 }
1093
1094 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001095 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001096 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001097 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001098 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001099 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001100 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001101 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001102 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001103 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001104 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001105 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001106 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001107 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001108 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001109 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001110 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001111 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001112 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001113 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001114 // OpenMP [2.9.1, target data construct, Restrictions]
1115 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001116 // OpenMP [2.11.1, task Construct, Restrictions]
1117 // At most one if clause can appear on the directive.
1118 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001119 // OpenMP [teams Construct, Restrictions]
1120 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001121 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001122 // OpenMP [2.9.1, task Construct, Restrictions]
1123 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001124 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1125 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001126 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1127 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001128 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001129 Diag(Tok, diag::err_omp_more_one_clause)
1130 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001131 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001132 }
1133
Alexey Bataev10e775f2015-07-30 11:36:16 +00001134 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1135 Clause = ParseOpenMPClause(CKind);
1136 else
1137 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001138 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001139 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001140 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001141 // OpenMP [2.14.3.1, Restrictions]
1142 // Only a single default clause may be specified on a parallel, task or
1143 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001144 // OpenMP [2.5, parallel Construct, Restrictions]
1145 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001146 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001147 Diag(Tok, diag::err_omp_more_one_clause)
1148 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001149 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001150 }
1151
1152 Clause = ParseOpenMPSimpleClause(CKind);
1153 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001154 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001155 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001156 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001157 // OpenMP [2.7.1, Restrictions, p. 3]
1158 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001159 // OpenMP [2.10.4, Restrictions, p. 106]
1160 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001161 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001162 Diag(Tok, diag::err_omp_more_one_clause)
1163 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001164 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001165 }
1166
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001167 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001168 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1169 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001170 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001171 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001172 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001173 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001174 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001175 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001176 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001177 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001178 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001179 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001180 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001181 // OpenMP [2.7.1, Restrictions, p. 9]
1182 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001183 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1184 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001185 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001186 Diag(Tok, diag::err_omp_more_one_clause)
1187 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001188 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001189 }
1190
1191 Clause = ParseOpenMPClause(CKind);
1192 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001193 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001194 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001195 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001196 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001197 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001198 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001199 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001200 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001201 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001202 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001203 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001204 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001205 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001206 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001207 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001208 case OMPC_is_device_ptr:
Alexey Bataeveb482352015-12-18 05:05:56 +00001209 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001210 break;
1211 case OMPC_unknown:
1212 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001213 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001214 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001215 break;
1216 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001217 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001218 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1219 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001220 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001221 break;
1222 }
Craig Topper161e4db2014-05-21 06:02:52 +00001223 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001224}
1225
Alexey Bataev2af33e32016-04-07 12:45:37 +00001226/// Parses simple expression in parens for single-expression clauses of OpenMP
1227/// constructs.
1228/// \param RLoc Returned location of right paren.
1229ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1230 SourceLocation &RLoc) {
1231 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1232 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1233 return ExprError();
1234
1235 SourceLocation ELoc = Tok.getLocation();
1236 ExprResult LHS(ParseCastExpression(
1237 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1238 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1239 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1240
1241 // Parse ')'.
1242 T.consumeClose();
1243
1244 RLoc = T.getCloseLocation();
1245 return Val;
1246}
1247
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001248/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001249/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001250/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001251///
Alexey Bataev3778b602014-07-17 07:32:53 +00001252/// final-clause:
1253/// 'final' '(' expression ')'
1254///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001255/// num_threads-clause:
1256/// 'num_threads' '(' expression ')'
1257///
1258/// safelen-clause:
1259/// 'safelen' '(' expression ')'
1260///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001261/// simdlen-clause:
1262/// 'simdlen' '(' expression ')'
1263///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001264/// collapse-clause:
1265/// 'collapse' '(' expression ')'
1266///
Alexey Bataeva0569352015-12-01 10:17:31 +00001267/// priority-clause:
1268/// 'priority' '(' expression ')'
1269///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001270/// grainsize-clause:
1271/// 'grainsize' '(' expression ')'
1272///
Alexey Bataev382967a2015-12-08 12:06:20 +00001273/// num_tasks-clause:
1274/// 'num_tasks' '(' expression ')'
1275///
Alexey Bataev28c75412015-12-15 08:19:24 +00001276/// hint-clause:
1277/// 'hint' '(' expression ')'
1278///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001279OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1280 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001281 SourceLocation LLoc = Tok.getLocation();
1282 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001283
Alexey Bataev2af33e32016-04-07 12:45:37 +00001284 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001285
1286 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001287 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001288
Alexey Bataev2af33e32016-04-07 12:45:37 +00001289 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001290}
1291
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001292/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001293///
1294/// default-clause:
1295/// 'default' '(' 'none' | 'shared' ')
1296///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001297/// proc_bind-clause:
1298/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1299///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001300OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1301 SourceLocation Loc = Tok.getLocation();
1302 SourceLocation LOpen = ConsumeToken();
1303 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001304 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001305 if (T.expectAndConsume(diag::err_expected_lparen_after,
1306 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001307 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001308
Alexey Bataeva55ed262014-05-28 06:15:33 +00001309 unsigned Type = getOpenMPSimpleClauseType(
1310 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001311 SourceLocation TypeLoc = Tok.getLocation();
1312 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1313 Tok.isNot(tok::annot_pragma_openmp_end))
1314 ConsumeAnyToken();
1315
1316 // Parse ')'.
1317 T.consumeClose();
1318
1319 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1320 Tok.getLocation());
1321}
1322
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001323/// \brief Parsing of OpenMP clauses like 'ordered'.
1324///
1325/// ordered-clause:
1326/// 'ordered'
1327///
Alexey Bataev236070f2014-06-20 11:19:47 +00001328/// nowait-clause:
1329/// 'nowait'
1330///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001331/// untied-clause:
1332/// 'untied'
1333///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001334/// mergeable-clause:
1335/// 'mergeable'
1336///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001337/// read-clause:
1338/// 'read'
1339///
Alexey Bataev346265e2015-09-25 10:37:12 +00001340/// threads-clause:
1341/// 'threads'
1342///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001343/// simd-clause:
1344/// 'simd'
1345///
Alexey Bataevb825de12015-12-07 10:51:44 +00001346/// nogroup-clause:
1347/// 'nogroup'
1348///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001349OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1350 SourceLocation Loc = Tok.getLocation();
1351 ConsumeAnyToken();
1352
1353 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1354}
1355
1356
Alexey Bataev56dafe82014-06-20 07:16:17 +00001357/// \brief Parsing of OpenMP clauses with single expressions and some additional
1358/// argument like 'schedule' or 'dist_schedule'.
1359///
1360/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001361/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1362/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001363///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001364/// if-clause:
1365/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1366///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001367/// defaultmap:
1368/// 'defaultmap' '(' modifier ':' kind ')'
1369///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001370OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1371 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001372 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001373 // Parse '('.
1374 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1375 if (T.expectAndConsume(diag::err_expected_lparen_after,
1376 getOpenMPClauseName(Kind)))
1377 return nullptr;
1378
1379 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001380 SmallVector<unsigned, 4> Arg;
1381 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001382 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001383 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1384 Arg.resize(NumberOfElements);
1385 KLoc.resize(NumberOfElements);
1386 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1387 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1388 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1389 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001390 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001391 if (KindModifier > OMPC_SCHEDULE_unknown) {
1392 // Parse 'modifier'
1393 Arg[Modifier1] = KindModifier;
1394 KLoc[Modifier1] = Tok.getLocation();
1395 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1396 Tok.isNot(tok::annot_pragma_openmp_end))
1397 ConsumeAnyToken();
1398 if (Tok.is(tok::comma)) {
1399 // Parse ',' 'modifier'
1400 ConsumeAnyToken();
1401 KindModifier = getOpenMPSimpleClauseType(
1402 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1403 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1404 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001405 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001406 KLoc[Modifier2] = Tok.getLocation();
1407 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1408 Tok.isNot(tok::annot_pragma_openmp_end))
1409 ConsumeAnyToken();
1410 }
1411 // Parse ':'
1412 if (Tok.is(tok::colon))
1413 ConsumeAnyToken();
1414 else
1415 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1416 KindModifier = getOpenMPSimpleClauseType(
1417 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1418 }
1419 Arg[ScheduleKind] = KindModifier;
1420 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001421 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1422 Tok.isNot(tok::annot_pragma_openmp_end))
1423 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001424 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1425 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1426 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001427 Tok.is(tok::comma))
1428 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001429 } else if (Kind == OMPC_dist_schedule) {
1430 Arg.push_back(getOpenMPSimpleClauseType(
1431 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1432 KLoc.push_back(Tok.getLocation());
1433 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1434 Tok.isNot(tok::annot_pragma_openmp_end))
1435 ConsumeAnyToken();
1436 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1437 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001438 } else if (Kind == OMPC_defaultmap) {
1439 // Get a defaultmap modifier
1440 Arg.push_back(getOpenMPSimpleClauseType(
1441 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1442 KLoc.push_back(Tok.getLocation());
1443 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1444 Tok.isNot(tok::annot_pragma_openmp_end))
1445 ConsumeAnyToken();
1446 // Parse ':'
1447 if (Tok.is(tok::colon))
1448 ConsumeAnyToken();
1449 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1450 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1451 // Get a defaultmap kind
1452 Arg.push_back(getOpenMPSimpleClauseType(
1453 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1454 KLoc.push_back(Tok.getLocation());
1455 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1456 Tok.isNot(tok::annot_pragma_openmp_end))
1457 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001458 } else {
1459 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001460 KLoc.push_back(Tok.getLocation());
1461 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1462 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001463 ConsumeToken();
1464 if (Tok.is(tok::colon))
1465 DelimLoc = ConsumeToken();
1466 else
1467 Diag(Tok, diag::warn_pragma_expected_colon)
1468 << "directive name modifier";
1469 }
1470 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001471
Carlo Bertollib4adf552016-01-15 18:50:31 +00001472 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1473 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1474 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001475 if (NeedAnExpression) {
1476 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001477 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1478 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001479 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001480 }
1481
1482 // Parse ')'.
1483 T.consumeClose();
1484
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001485 if (NeedAnExpression && Val.isInvalid())
1486 return nullptr;
1487
Alexey Bataev56dafe82014-06-20 07:16:17 +00001488 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001489 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001490 T.getCloseLocation());
1491}
1492
Alexey Bataevc5e02582014-06-16 07:08:35 +00001493static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1494 UnqualifiedId &ReductionId) {
1495 SourceLocation TemplateKWLoc;
1496 if (ReductionIdScopeSpec.isEmpty()) {
1497 auto OOK = OO_None;
1498 switch (P.getCurToken().getKind()) {
1499 case tok::plus:
1500 OOK = OO_Plus;
1501 break;
1502 case tok::minus:
1503 OOK = OO_Minus;
1504 break;
1505 case tok::star:
1506 OOK = OO_Star;
1507 break;
1508 case tok::amp:
1509 OOK = OO_Amp;
1510 break;
1511 case tok::pipe:
1512 OOK = OO_Pipe;
1513 break;
1514 case tok::caret:
1515 OOK = OO_Caret;
1516 break;
1517 case tok::ampamp:
1518 OOK = OO_AmpAmp;
1519 break;
1520 case tok::pipepipe:
1521 OOK = OO_PipePipe;
1522 break;
1523 default:
1524 break;
1525 }
1526 if (OOK != OO_None) {
1527 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001528 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001529 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1530 return false;
1531 }
1532 }
1533 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1534 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001535 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001536 TemplateKWLoc, ReductionId);
1537}
1538
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001539/// Parses clauses with list.
1540bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1541 OpenMPClauseKind Kind,
1542 SmallVectorImpl<Expr *> &Vars,
1543 OpenMPVarListDataTy &Data) {
1544 UnqualifiedId UnqualifiedReductionId;
1545 bool InvalidReductionId = false;
1546 bool MapTypeModifierSpecified = false;
1547
1548 // Parse '('.
1549 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1550 if (T.expectAndConsume(diag::err_expected_lparen_after,
1551 getOpenMPClauseName(Kind)))
1552 return true;
1553
1554 bool NeedRParenForLinear = false;
1555 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1556 tok::annot_pragma_openmp_end);
1557 // Handle reduction-identifier for reduction clause.
1558 if (Kind == OMPC_reduction) {
1559 ColonProtectionRAIIObject ColonRAII(*this);
1560 if (getLangOpts().CPlusPlus)
1561 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1562 /*ObjectType=*/nullptr,
1563 /*EnteringContext=*/false);
1564 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1565 UnqualifiedReductionId);
1566 if (InvalidReductionId) {
1567 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1568 StopBeforeMatch);
1569 }
1570 if (Tok.is(tok::colon))
1571 Data.ColonLoc = ConsumeToken();
1572 else
1573 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1574 if (!InvalidReductionId)
1575 Data.ReductionId =
1576 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1577 } else if (Kind == OMPC_depend) {
1578 // Handle dependency type for depend clause.
1579 ColonProtectionRAIIObject ColonRAII(*this);
1580 Data.DepKind =
1581 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1582 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1583 Data.DepLinMapLoc = Tok.getLocation();
1584
1585 if (Data.DepKind == OMPC_DEPEND_unknown) {
1586 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1587 StopBeforeMatch);
1588 } else {
1589 ConsumeToken();
1590 // Special processing for depend(source) clause.
1591 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1592 // Parse ')'.
1593 T.consumeClose();
1594 return false;
1595 }
1596 }
1597 if (Tok.is(tok::colon))
1598 Data.ColonLoc = ConsumeToken();
1599 else {
1600 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1601 : diag::warn_pragma_expected_colon)
1602 << "dependency type";
1603 }
1604 } else if (Kind == OMPC_linear) {
1605 // Try to parse modifier if any.
1606 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1607 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1608 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1609 Data.DepLinMapLoc = ConsumeToken();
1610 LinearT.consumeOpen();
1611 NeedRParenForLinear = true;
1612 }
1613 } else if (Kind == OMPC_map) {
1614 // Handle map type for map clause.
1615 ColonProtectionRAIIObject ColonRAII(*this);
1616
1617 /// The map clause modifier token can be either a identifier or the C++
1618 /// delete keyword.
1619 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1620 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1621 };
1622
1623 // The first identifier may be a list item, a map-type or a
1624 // map-type-modifier. The map modifier can also be delete which has the same
1625 // spelling of the C++ delete keyword.
1626 Data.MapType =
1627 IsMapClauseModifierToken(Tok)
1628 ? static_cast<OpenMPMapClauseKind>(
1629 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1630 : OMPC_MAP_unknown;
1631 Data.DepLinMapLoc = Tok.getLocation();
1632 bool ColonExpected = false;
1633
1634 if (IsMapClauseModifierToken(Tok)) {
1635 if (PP.LookAhead(0).is(tok::colon)) {
1636 if (Data.MapType == OMPC_MAP_unknown)
1637 Diag(Tok, diag::err_omp_unknown_map_type);
1638 else if (Data.MapType == OMPC_MAP_always)
1639 Diag(Tok, diag::err_omp_map_type_missing);
1640 ConsumeToken();
1641 } else if (PP.LookAhead(0).is(tok::comma)) {
1642 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1643 PP.LookAhead(2).is(tok::colon)) {
1644 Data.MapTypeModifier = Data.MapType;
1645 if (Data.MapTypeModifier != OMPC_MAP_always) {
1646 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1647 Data.MapTypeModifier = OMPC_MAP_unknown;
1648 } else
1649 MapTypeModifierSpecified = true;
1650
1651 ConsumeToken();
1652 ConsumeToken();
1653
1654 Data.MapType =
1655 IsMapClauseModifierToken(Tok)
1656 ? static_cast<OpenMPMapClauseKind>(
1657 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1658 : OMPC_MAP_unknown;
1659 if (Data.MapType == OMPC_MAP_unknown ||
1660 Data.MapType == OMPC_MAP_always)
1661 Diag(Tok, diag::err_omp_unknown_map_type);
1662 ConsumeToken();
1663 } else {
1664 Data.MapType = OMPC_MAP_tofrom;
1665 Data.IsMapTypeImplicit = true;
1666 }
1667 } else {
1668 Data.MapType = OMPC_MAP_tofrom;
1669 Data.IsMapTypeImplicit = true;
1670 }
1671 } else {
1672 Data.MapType = OMPC_MAP_tofrom;
1673 Data.IsMapTypeImplicit = true;
1674 }
1675
1676 if (Tok.is(tok::colon))
1677 Data.ColonLoc = ConsumeToken();
1678 else if (ColonExpected)
1679 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1680 }
1681
1682 bool IsComma =
1683 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1684 (Kind == OMPC_reduction && !InvalidReductionId) ||
1685 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1686 (!MapTypeModifierSpecified ||
1687 Data.MapTypeModifier == OMPC_MAP_always)) ||
1688 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1689 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1690 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1691 Tok.isNot(tok::annot_pragma_openmp_end))) {
1692 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1693 // Parse variable
1694 ExprResult VarExpr =
1695 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1696 if (VarExpr.isUsable())
1697 Vars.push_back(VarExpr.get());
1698 else {
1699 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1700 StopBeforeMatch);
1701 }
1702 // Skip ',' if any
1703 IsComma = Tok.is(tok::comma);
1704 if (IsComma)
1705 ConsumeToken();
1706 else if (Tok.isNot(tok::r_paren) &&
1707 Tok.isNot(tok::annot_pragma_openmp_end) &&
1708 (!MayHaveTail || Tok.isNot(tok::colon)))
1709 Diag(Tok, diag::err_omp_expected_punc)
1710 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1711 : getOpenMPClauseName(Kind))
1712 << (Kind == OMPC_flush);
1713 }
1714
1715 // Parse ')' for linear clause with modifier.
1716 if (NeedRParenForLinear)
1717 LinearT.consumeClose();
1718
1719 // Parse ':' linear-step (or ':' alignment).
1720 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1721 if (MustHaveTail) {
1722 Data.ColonLoc = Tok.getLocation();
1723 SourceLocation ELoc = ConsumeToken();
1724 ExprResult Tail = ParseAssignmentExpression();
1725 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1726 if (Tail.isUsable())
1727 Data.TailExpr = Tail.get();
1728 else
1729 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1730 StopBeforeMatch);
1731 }
1732
1733 // Parse ')'.
1734 T.consumeClose();
1735 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1736 Vars.empty()) ||
1737 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1738 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1739 return true;
1740 return false;
1741}
1742
Alexander Musman1bb328c2014-06-04 13:06:39 +00001743/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001744/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001745///
1746/// private-clause:
1747/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001748/// firstprivate-clause:
1749/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001750/// lastprivate-clause:
1751/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001752/// shared-clause:
1753/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001754/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001755/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001756/// aligned-clause:
1757/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001758/// reduction-clause:
1759/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001760/// copyprivate-clause:
1761/// 'copyprivate' '(' list ')'
1762/// flush-clause:
1763/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001764/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001765/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001766/// map-clause:
1767/// 'map' '(' [ [ always , ]
1768/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001769/// to-clause:
1770/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001771/// from-clause:
1772/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001773/// use_device_ptr-clause:
1774/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001775/// is_device_ptr-clause:
1776/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001777///
Alexey Bataev182227b2015-08-20 10:54:39 +00001778/// For 'linear' clause linear-list may have the following forms:
1779/// list
1780/// modifier(list)
1781/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001782OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1783 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001784 SourceLocation Loc = Tok.getLocation();
1785 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001786 SmallVector<Expr *, 4> Vars;
1787 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001788
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001789 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001790 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001791
Alexey Bataevc5e02582014-06-16 07:08:35 +00001792 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001793 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1794 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1795 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1796 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001797}
1798