blob: 6112c90cacda028e51ebf36870c69e14531fe21d [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,
41 OMPD_target_exit
42};
43} // namespace
44
45// Map token string to extended OMP token kind that are
46// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
47static unsigned getOpenMPDirectiveKindEx(StringRef S) {
48 auto DKind = getOpenMPDirectiveKind(S);
49 if (DKind != OMPD_unknown)
50 return DKind;
51
52 return llvm::StringSwitch<unsigned>(S)
53 .Case("cancellation", OMPD_cancellation)
54 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000055 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000056 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000057 .Case("enter", OMPD_enter)
58 .Case("exit", OMPD_exit)
59 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000060 .Case("reduction", OMPD_reduction)
Dmitry Polukhin82478332016-02-13 06:53:38 +000061 .Default(OMPD_unknown);
62}
63
Alexey Bataev4acb8592014-07-07 13:01:15 +000064static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000065 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
66 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
67 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000068 static const unsigned F[][3] = {
69 { OMPD_cancellation, OMPD_point, OMPD_cancellation_point },
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000070 { OMPD_declare, OMPD_reduction, OMPD_declare_reduction },
Alexey Bataev587e1de2016-03-30 10:43:55 +000071 { OMPD_declare, OMPD_simd, OMPD_declare_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000072 { OMPD_declare, OMPD_target, OMPD_declare_target },
73 { OMPD_end, OMPD_declare, OMPD_end_declare },
74 { OMPD_end_declare, OMPD_target, OMPD_end_declare_target },
Dmitry Polukhin82478332016-02-13 06:53:38 +000075 { OMPD_target, OMPD_data, OMPD_target_data },
76 { OMPD_target, OMPD_enter, OMPD_target_enter },
77 { OMPD_target, OMPD_exit, OMPD_target_exit },
78 { OMPD_target_enter, OMPD_data, OMPD_target_enter_data },
79 { OMPD_target_exit, OMPD_data, OMPD_target_exit_data },
80 { OMPD_for, OMPD_simd, OMPD_for_simd },
81 { OMPD_parallel, OMPD_for, OMPD_parallel_for },
82 { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
83 { OMPD_parallel, OMPD_sections, OMPD_parallel_sections },
84 { OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd },
85 { OMPD_target, OMPD_parallel, OMPD_target_parallel },
86 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for }
87 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000088 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +000089 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +000090 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +000091 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +000092 ? static_cast<unsigned>(OMPD_unknown)
93 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
94 if (DKind == OMPD_unknown)
95 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +000096
Alexander Musmanf82886e2014-09-18 05:12:34 +000097 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +000098 if (DKind != F[i][0])
99 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000100
Dmitry Polukhin82478332016-02-13 06:53:38 +0000101 Tok = P.getPreprocessor().LookAhead(0);
102 unsigned SDKind =
103 Tok.isAnnotation()
104 ? static_cast<unsigned>(OMPD_unknown)
105 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
106 if (SDKind == OMPD_unknown)
107 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000108
Dmitry Polukhin82478332016-02-13 06:53:38 +0000109 if (SDKind == F[i][1]) {
110 P.ConsumeToken();
111 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000112 }
113 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000114 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
115 : OMPD_unknown;
116}
117
118static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000119 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000120 Sema &Actions = P.getActions();
121 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000122 // Allow to use 'operator' keyword for C++ operators
123 bool WithOperator = false;
124 if (Tok.is(tok::kw_operator)) {
125 P.ConsumeToken();
126 Tok = P.getCurToken();
127 WithOperator = true;
128 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000129 switch (Tok.getKind()) {
130 case tok::plus: // '+'
131 OOK = OO_Plus;
132 break;
133 case tok::minus: // '-'
134 OOK = OO_Minus;
135 break;
136 case tok::star: // '*'
137 OOK = OO_Star;
138 break;
139 case tok::amp: // '&'
140 OOK = OO_Amp;
141 break;
142 case tok::pipe: // '|'
143 OOK = OO_Pipe;
144 break;
145 case tok::caret: // '^'
146 OOK = OO_Caret;
147 break;
148 case tok::ampamp: // '&&'
149 OOK = OO_AmpAmp;
150 break;
151 case tok::pipepipe: // '||'
152 OOK = OO_PipePipe;
153 break;
154 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000155 if (!WithOperator)
156 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000157 default:
158 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
159 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
160 Parser::StopBeforeMatch);
161 return DeclarationName();
162 }
163 P.ConsumeToken();
164 auto &DeclNames = Actions.getASTContext().DeclarationNames;
165 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
166 : DeclNames.getCXXOperatorName(OOK);
167}
168
169/// \brief Parse 'omp declare reduction' construct.
170///
171/// declare-reduction-directive:
172/// annot_pragma_openmp 'declare' 'reduction'
173/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
174/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
175/// annot_pragma_openmp_end
176/// <reduction_id> is either a base language identifier or one of the following
177/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
178///
179Parser::DeclGroupPtrTy
180Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
181 // Parse '('.
182 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
183 if (T.expectAndConsume(diag::err_expected_lparen_after,
184 getOpenMPDirectiveName(OMPD_declare_reduction))) {
185 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
186 return DeclGroupPtrTy();
187 }
188
189 DeclarationName Name = parseOpenMPReductionId(*this);
190 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
191 return DeclGroupPtrTy();
192
193 // Consume ':'.
194 bool IsCorrect = !ExpectAndConsume(tok::colon);
195
196 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
197 return DeclGroupPtrTy();
198
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000199 IsCorrect = IsCorrect && !Name.isEmpty();
200
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000201 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
202 Diag(Tok.getLocation(), diag::err_expected_type);
203 IsCorrect = false;
204 }
205
206 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
207 return DeclGroupPtrTy();
208
209 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
210 // Parse list of types until ':' token.
211 do {
212 ColonProtectionRAIIObject ColonRAII(*this);
213 SourceRange Range;
214 TypeResult TR = ParseTypeName(&Range, Declarator::PrototypeContext, AS);
215 if (TR.isUsable()) {
216 auto ReductionType =
217 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
218 if (!ReductionType.isNull()) {
219 ReductionTypes.push_back(
220 std::make_pair(ReductionType, Range.getBegin()));
221 }
222 } else {
223 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
224 StopBeforeMatch);
225 }
226
227 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
228 break;
229
230 // Consume ','.
231 if (ExpectAndConsume(tok::comma)) {
232 IsCorrect = false;
233 if (Tok.is(tok::annot_pragma_openmp_end)) {
234 Diag(Tok.getLocation(), diag::err_expected_type);
235 return DeclGroupPtrTy();
236 }
237 }
238 } while (Tok.isNot(tok::annot_pragma_openmp_end));
239
240 if (ReductionTypes.empty()) {
241 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
242 return DeclGroupPtrTy();
243 }
244
245 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
246 return DeclGroupPtrTy();
247
248 // Consume ':'.
249 if (ExpectAndConsume(tok::colon))
250 IsCorrect = false;
251
252 if (Tok.is(tok::annot_pragma_openmp_end)) {
253 Diag(Tok.getLocation(), diag::err_expected_expression);
254 return DeclGroupPtrTy();
255 }
256
257 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
258 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
259
260 // Parse <combiner> expression and then parse initializer if any for each
261 // correct type.
262 unsigned I = 0, E = ReductionTypes.size();
263 for (auto *D : DRD.get()) {
264 TentativeParsingAction TPA(*this);
265 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
266 Scope::OpenMPDirectiveScope);
267 // Parse <combiner> expression.
268 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
269 ExprResult CombinerResult =
270 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
271 D->getLocation(), /*DiscardedValue=*/true);
272 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
273
274 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
275 Tok.isNot(tok::annot_pragma_openmp_end)) {
276 TPA.Commit();
277 IsCorrect = false;
278 break;
279 }
280 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
281 ExprResult InitializerResult;
282 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
283 // Parse <initializer> expression.
284 if (Tok.is(tok::identifier) &&
285 Tok.getIdentifierInfo()->isStr("initializer"))
286 ConsumeToken();
287 else {
288 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
289 TPA.Commit();
290 IsCorrect = false;
291 break;
292 }
293 // Parse '('.
294 BalancedDelimiterTracker T(*this, tok::l_paren,
295 tok::annot_pragma_openmp_end);
296 IsCorrect =
297 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
298 IsCorrect;
299 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
300 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
301 Scope::OpenMPDirectiveScope);
302 // Parse expression.
303 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), D);
304 InitializerResult = Actions.ActOnFinishFullExpr(
305 ParseAssignmentExpression().get(), D->getLocation(),
306 /*DiscardedValue=*/true);
307 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
308 D, InitializerResult.get());
309 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
310 Tok.isNot(tok::annot_pragma_openmp_end)) {
311 TPA.Commit();
312 IsCorrect = false;
313 break;
314 }
315 IsCorrect =
316 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
317 }
318 }
319
320 ++I;
321 // Revert parsing if not the last type, otherwise accept it, we're done with
322 // parsing.
323 if (I != E)
324 TPA.Revert();
325 else
326 TPA.Commit();
327 }
328 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
329 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000330}
331
Alexey Bataev20dfd772016-04-04 10:12:15 +0000332/// Parses clauses for 'declare simd' directive.
333/// clause:
334/// 'inbranch' | 'notinbranch'
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000335/// 'simdlen' '(' <expr> ')'
336/// { 'uniform' '(' <argument_list> ')' }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000337static bool parseDeclareSimdClauses(Parser &P,
338 OMPDeclareSimdDeclAttr::BranchStateTy &BS,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000339 ExprResult &SimdLen,
340 SmallVectorImpl<Expr *> &Uniforms) {
Alexey Bataev20dfd772016-04-04 10:12:15 +0000341 SourceRange BSRange;
342 const Token &Tok = P.getCurToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000343 bool IsError = false;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000344 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
345 if (Tok.isNot(tok::identifier))
346 break;
347 OMPDeclareSimdDeclAttr::BranchStateTy Out;
Alexey Bataev2af33e32016-04-07 12:45:37 +0000348 IdentifierInfo *II = Tok.getIdentifierInfo();
349 StringRef ClauseName = II->getName();
Alexey Bataev20dfd772016-04-04 10:12:15 +0000350 // Parse 'inranch|notinbranch' clauses.
Alexey Bataev2af33e32016-04-07 12:45:37 +0000351 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
Alexey Bataev20dfd772016-04-04 10:12:15 +0000352 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
353 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
Alexey Bataev2af33e32016-04-07 12:45:37 +0000354 << ClauseName
355 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
356 IsError = true;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000357 }
358 BS = Out;
359 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
Alexey Bataev2af33e32016-04-07 12:45:37 +0000360 P.ConsumeToken();
361 } else if (ClauseName.equals("simdlen")) {
362 if (SimdLen.isUsable()) {
363 P.Diag(Tok, diag::err_omp_more_one_clause)
364 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
365 IsError = true;
366 }
367 P.ConsumeToken();
368 SourceLocation RLoc;
369 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
370 if (SimdLen.isInvalid())
371 IsError = true;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000372 } else if (ClauseName.equals("uniform")) {
373 Parser::OpenMPVarListDataTy Data;
374
375 P.ConsumeToken();
376 if (P.ParseOpenMPVarList(OMPD_declare_simd,
377 getOpenMPClauseKind(ClauseName), Uniforms, Data))
378 IsError = true;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000379 } else
380 // TODO: add parsing of other clauses.
381 break;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000382 // Skip ',' if any.
383 if (Tok.is(tok::comma))
384 P.ConsumeToken();
385 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000386 return IsError;
387}
388
389namespace {
390/// RAII that recreates function context for correct parsing of clauses of
391/// 'declare simd' construct.
392/// OpenMP, 2.8.2 declare simd Construct
393/// The expressions appearing in the clauses of this directive are evaluated in
394/// the scope of the arguments of the function declaration or definition.
395class FNContextRAII final {
396 Parser &P;
397 Sema::CXXThisScopeRAII *ThisScope;
398 Parser::ParseScope *TempScope;
399 Parser::ParseScope *FnScope;
400 bool HasTemplateScope = false;
401 bool HasFunScope = false;
402 FNContextRAII() = delete;
403 FNContextRAII(const FNContextRAII &) = delete;
404 FNContextRAII &operator=(const FNContextRAII &) = delete;
405
406public:
407 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
408 Decl *D = *Ptr.get().begin();
409 NamedDecl *ND = dyn_cast<NamedDecl>(D);
410 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
411 Sema &Actions = P.getActions();
412
413 // Allow 'this' within late-parsed attributes.
414 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
415 ND && ND->isCXXInstanceMember());
416
417 // If the Decl is templatized, add template parameters to scope.
418 HasTemplateScope = D->isTemplateDecl();
419 TempScope =
420 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
421 if (HasTemplateScope)
422 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
423
424 // If the Decl is on a function, add function parameters to the scope.
425 HasFunScope = D->isFunctionOrFunctionTemplate();
426 FnScope = new Parser::ParseScope(&P, Scope::FnScope | Scope::DeclScope,
427 HasFunScope);
428 if (HasFunScope)
429 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
430 }
431 ~FNContextRAII() {
432 if (HasFunScope) {
433 P.getActions().ActOnExitFunctionContext();
434 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
435 }
436 if (HasTemplateScope)
437 TempScope->Exit();
438 delete FnScope;
439 delete TempScope;
440 delete ThisScope;
441 }
442};
443} // namespace
444
445/// Parse clauses for '#pragma omp declare simd'.
446Parser::DeclGroupPtrTy
447Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
448 CachedTokens &Toks, SourceLocation Loc) {
449 PP.EnterToken(Tok);
450 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
451 // Consume the previously pushed token.
452 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
453
454 FNContextRAII FnContext(*this, Ptr);
455 OMPDeclareSimdDeclAttr::BranchStateTy BS =
456 OMPDeclareSimdDeclAttr::BS_Undefined;
457 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000458 SmallVector<Expr *, 4> Uniforms;
459 bool IsError = parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000460 // Need to check for extra tokens.
461 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
462 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
463 << getOpenMPDirectiveName(OMPD_declare_simd);
464 while (Tok.isNot(tok::annot_pragma_openmp_end))
465 ConsumeAnyToken();
466 }
467 // Skip the last annot_pragma_openmp_end.
468 SourceLocation EndLoc = ConsumeToken();
469 if (!IsError)
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000470 return Actions.ActOnOpenMPDeclareSimdDirective(
471 Ptr, BS, Simdlen.get(), Uniforms, SourceRange(Loc, EndLoc));
Alexey Bataev2af33e32016-04-07 12:45:37 +0000472 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000473}
474
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000475/// \brief Parsing of declarative OpenMP directives.
476///
477/// threadprivate-directive:
478/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000479/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000480///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000481/// declare-reduction-directive:
482/// annot_pragma_openmp 'declare' 'reduction' [...]
483/// annot_pragma_openmp_end
484///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000485/// declare-simd-directive:
486/// annot_pragma_openmp 'declare simd' {<clause> [,]}
487/// annot_pragma_openmp_end
488/// <function declaration/definition>
489///
490Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
491 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
492 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000493 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000494 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000495
496 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000497 SmallVector<Expr *, 4> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000498 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000499
500 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000501 case OMPD_threadprivate:
502 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000503 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000504 // The last seen token is annot_pragma_openmp_end - need to check for
505 // extra tokens.
506 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
507 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000508 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000509 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000510 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000511 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000512 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +0000513 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +0000514 }
515 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000516 case OMPD_declare_reduction:
517 ConsumeToken();
518 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
519 // The last seen token is annot_pragma_openmp_end - need to check for
520 // 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_reduction);
524 while (Tok.isNot(tok::annot_pragma_openmp_end))
525 ConsumeAnyToken();
526 }
527 // Skip the last annot_pragma_openmp_end.
528 ConsumeToken();
529 return Res;
530 }
531 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000532 case OMPD_declare_simd: {
533 // The syntax is:
534 // { #pragma omp declare simd }
535 // <function-declaration-or-definition>
536 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000537 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000538 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000539 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
540 Toks.push_back(Tok);
541 ConsumeAnyToken();
542 }
543 Toks.push_back(Tok);
544 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000545
546 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000547 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000548 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000549 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000550 // Here we expect to see some function declaration.
551 if (AS == AS_none) {
552 assert(TagType == DeclSpec::TST_unspecified);
553 MaybeParseCXX11Attributes(Attrs);
554 MaybeParseMicrosoftAttributes(Attrs);
555 ParsingDeclSpec PDS(*this);
556 Ptr = ParseExternalDeclaration(Attrs, &PDS);
557 } else {
558 Ptr =
559 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
560 }
561 }
562 if (!Ptr) {
563 Diag(Loc, diag::err_omp_decl_in_declare_simd);
564 return DeclGroupPtrTy();
565 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000566 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000567 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000568 case OMPD_declare_target: {
569 SourceLocation DTLoc = ConsumeAnyToken();
570 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
571 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
572 << getOpenMPDirectiveName(OMPD_declare_target);
573 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
574 }
575 // Skip the last annot_pragma_openmp_end.
576 ConsumeAnyToken();
577
578 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
579 return DeclGroupPtrTy();
580
581 DKind = ParseOpenMPDirectiveKind(*this);
582 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
583 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
584 ParsedAttributesWithRange attrs(AttrFactory);
585 MaybeParseCXX11Attributes(attrs);
586 MaybeParseMicrosoftAttributes(attrs);
587 ParseExternalDeclaration(attrs);
588 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
589 TentativeParsingAction TPA(*this);
590 ConsumeToken();
591 DKind = ParseOpenMPDirectiveKind(*this);
592 if (DKind != OMPD_end_declare_target)
593 TPA.Revert();
594 else
595 TPA.Commit();
596 }
597 }
598
599 if (DKind == OMPD_end_declare_target) {
600 ConsumeAnyToken();
601 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
602 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
603 << getOpenMPDirectiveName(OMPD_end_declare_target);
604 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
605 }
606 // Skip the last annot_pragma_openmp_end.
607 ConsumeAnyToken();
608 } else {
609 Diag(Tok, diag::err_expected_end_declare_target);
610 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
611 }
612 Actions.ActOnFinishOpenMPDeclareTargetDirective();
613 return DeclGroupPtrTy();
614 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000615 case OMPD_unknown:
616 Diag(Tok, diag::err_omp_unknown_directive);
617 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000618 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000619 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000620 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000621 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000622 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000623 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000624 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000625 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000626 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000627 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000628 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000629 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000630 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000631 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000632 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000633 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000634 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000635 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000636 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000637 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000638 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000639 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000640 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000641 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000642 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000643 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000644 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000645 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000646 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000647 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000648 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000649 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000650 case OMPD_end_declare_target:
Alexey Bataeva769e072013-03-22 06:34:35 +0000651 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000652 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000653 break;
654 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000655 while (Tok.isNot(tok::annot_pragma_openmp_end))
656 ConsumeAnyToken();
657 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000658 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000659}
660
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000661/// \brief Parsing of declarative or executable OpenMP directives.
662///
663/// threadprivate-directive:
664/// annot_pragma_openmp 'threadprivate' simple-variable-list
665/// annot_pragma_openmp_end
666///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000667/// declare-reduction-directive:
668/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
669/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
670/// ('omp_priv' '=' <expression>|<function_call>) ')']
671/// annot_pragma_openmp_end
672///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000673/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000674/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000675/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
676/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000677/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000678/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000679/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000680/// 'distribute' | 'target enter data' | 'target exit data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000681/// 'target parallel' | 'target parallel for' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000682/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000683///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000684StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
685 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000686 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000687 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000688 SmallVector<Expr *, 5> Identifiers;
689 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000690 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000691 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000692 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000693 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000694 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000695 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000696 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000697 // Name of critical directive.
698 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000699 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000700 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000701 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000702
703 switch (DKind) {
704 case OMPD_threadprivate:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000705 if (Allowed != ACK_Any) {
706 Diag(Tok, diag::err_omp_immediate_directive)
707 << getOpenMPDirectiveName(DKind) << 0;
708 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000709 ConsumeToken();
710 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
711 // The last seen token is annot_pragma_openmp_end - need to check for
712 // extra tokens.
713 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
714 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000715 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000716 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000717 }
718 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000719 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000720 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
721 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000722 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000723 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000724 case OMPD_declare_reduction:
725 ConsumeToken();
726 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
727 // The last seen token is annot_pragma_openmp_end - need to check for
728 // extra tokens.
729 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
730 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
731 << getOpenMPDirectiveName(OMPD_declare_reduction);
732 while (Tok.isNot(tok::annot_pragma_openmp_end))
733 ConsumeAnyToken();
734 }
735 ConsumeAnyToken();
736 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
737 } else
738 SkipUntil(tok::annot_pragma_openmp_end);
739 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000740 case OMPD_flush:
741 if (PP.LookAhead(0).is(tok::l_paren)) {
742 FlushHasClause = true;
743 // Push copy of the current token back to stream to properly parse
744 // pseudo-clause OMPFlushClause.
745 PP.EnterToken(Tok);
746 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000747 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000748 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000749 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000750 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000751 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000752 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000753 case OMPD_target_exit_data:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000754 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000755 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000756 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000757 }
758 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000759 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000760 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000761 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000762 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000763 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000764 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000765 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000766 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000767 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000768 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000769 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000770 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000771 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000772 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000773 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000774 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000775 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000776 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000777 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000778 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000779 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000780 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000781 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000782 case OMPD_taskloop_simd:
783 case OMPD_distribute: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000784 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000785 // Parse directive name of the 'critical' directive if any.
786 if (DKind == OMPD_critical) {
787 BalancedDelimiterTracker T(*this, tok::l_paren,
788 tok::annot_pragma_openmp_end);
789 if (!T.consumeOpen()) {
790 if (Tok.isAnyIdentifier()) {
791 DirName =
792 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
793 ConsumeAnyToken();
794 } else {
795 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
796 }
797 T.consumeClose();
798 }
Alexey Bataev80909872015-07-02 11:25:17 +0000799 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000800 CancelRegion = ParseOpenMPDirectiveKind(*this);
801 if (Tok.isNot(tok::annot_pragma_openmp_end))
802 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000803 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000804
Alexey Bataevf29276e2014-06-18 04:14:57 +0000805 if (isOpenMPLoopDirective(DKind))
806 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
807 if (isOpenMPSimdDirective(DKind))
808 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
809 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000810 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000811
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000812 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000813 OpenMPClauseKind CKind =
814 Tok.isAnnotation()
815 ? OMPC_unknown
816 : FlushHasClause ? OMPC_flush
817 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000818 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000819 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000820 OMPClause *Clause =
821 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000822 FirstClauses[CKind].setInt(true);
823 if (Clause) {
824 FirstClauses[CKind].setPointer(Clause);
825 Clauses.push_back(Clause);
826 }
827
828 // Skip ',' if any.
829 if (Tok.is(tok::comma))
830 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000831 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000832 }
833 // End location of the directive.
834 EndLoc = Tok.getLocation();
835 // Consume final annot_pragma_openmp_end.
836 ConsumeToken();
837
Alexey Bataeveb482352015-12-18 05:05:56 +0000838 // OpenMP [2.13.8, ordered Construct, Syntax]
839 // If the depend clause is specified, the ordered construct is a stand-alone
840 // directive.
841 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000842 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000843 Diag(Loc, diag::err_omp_immediate_directive)
844 << getOpenMPDirectiveName(DKind) << 1
845 << getOpenMPClauseName(OMPC_depend);
846 }
847 HasAssociatedStatement = false;
848 }
849
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000850 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000851 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000852 // The body is a block scope like in Lambdas and Blocks.
853 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000854 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000855 Actions.ActOnStartOfCompoundStmt();
856 // Parse statement
857 AssociatedStmt = ParseStatement();
858 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000859 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000860 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000861 Directive = Actions.ActOnOpenMPExecutableDirective(
862 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
863 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000864
865 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000866 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000867 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000868 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000869 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000870 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000871 case OMPD_declare_target:
872 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +0000873 Diag(Tok, diag::err_omp_unexpected_directive)
874 << getOpenMPDirectiveName(DKind);
875 SkipUntil(tok::annot_pragma_openmp_end);
876 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000877 case OMPD_unknown:
878 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000879 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000880 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000881 }
882 return Directive;
883}
884
Alexey Bataeva769e072013-03-22 06:34:35 +0000885/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000886/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000887///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000888/// simple-variable-list:
889/// '(' id-expression {, id-expression} ')'
890///
891bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
892 SmallVectorImpl<Expr *> &VarList,
893 bool AllowScopeSpecifier) {
894 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000895 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000896 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000897 if (T.expectAndConsume(diag::err_expected_lparen_after,
898 getOpenMPDirectiveName(Kind)))
899 return true;
900 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000901 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000902
903 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000904 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000905 CXXScopeSpec SS;
906 SourceLocation TemplateKWLoc;
907 UnqualifiedId Name;
908 // Read var name.
909 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000910 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000911
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000912 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +0000913 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000914 IsCorrect = false;
915 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000916 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +0000917 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000918 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000919 IsCorrect = false;
920 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000921 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000922 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
923 Tok.isNot(tok::annot_pragma_openmp_end)) {
924 IsCorrect = false;
925 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000926 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000927 Diag(PrevTok.getLocation(), diag::err_expected)
928 << tok::identifier
929 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000930 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000931 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000932 ExprResult Res =
933 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000934 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000935 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000936 }
937 // Consume ','.
938 if (Tok.is(tok::comma)) {
939 ConsumeToken();
940 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000941 }
942
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000943 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000944 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000945 IsCorrect = false;
946 }
947
948 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000949 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000950
951 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000952}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000953
954/// \brief Parsing of OpenMP clauses.
955///
956/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000957/// if-clause | final-clause | num_threads-clause | safelen-clause |
958/// default-clause | private-clause | firstprivate-clause | shared-clause
959/// | linear-clause | aligned-clause | collapse-clause |
960/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000961/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000962/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +0000963/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000964/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000965/// thread_limit-clause | priority-clause | grainsize-clause |
Alexey Bataev28c75412015-12-15 08:19:24 +0000966/// nogroup-clause | num_tasks-clause | hint-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000967///
968OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
969 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000970 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000971 bool ErrorFound = false;
972 // Check if clause is allowed for the given directive.
973 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000974 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
975 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000976 ErrorFound = true;
977 }
978
979 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +0000980 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000981 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000982 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +0000983 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000984 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +0000985 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +0000986 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +0000987 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000988 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +0000989 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000990 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +0000991 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +0000992 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000993 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +0000994 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000995 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000996 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +0000997 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +0000998 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +0000999 // OpenMP [2.9.1, target data construct, Restrictions]
1000 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001001 // OpenMP [2.11.1, task Construct, Restrictions]
1002 // At most one if clause can appear on the directive.
1003 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001004 // OpenMP [teams Construct, Restrictions]
1005 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001006 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001007 // OpenMP [2.9.1, task Construct, Restrictions]
1008 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001009 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1010 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001011 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1012 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001013 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001014 Diag(Tok, diag::err_omp_more_one_clause)
1015 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001016 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001017 }
1018
Alexey Bataev10e775f2015-07-30 11:36:16 +00001019 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1020 Clause = ParseOpenMPClause(CKind);
1021 else
1022 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001023 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001024 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001025 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001026 // OpenMP [2.14.3.1, Restrictions]
1027 // Only a single default clause may be specified on a parallel, task or
1028 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001029 // OpenMP [2.5, parallel Construct, Restrictions]
1030 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001031 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001032 Diag(Tok, diag::err_omp_more_one_clause)
1033 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001034 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001035 }
1036
1037 Clause = ParseOpenMPSimpleClause(CKind);
1038 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001039 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001040 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001041 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001042 // OpenMP [2.7.1, Restrictions, p. 3]
1043 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001044 // OpenMP [2.10.4, Restrictions, p. 106]
1045 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001046 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001047 Diag(Tok, diag::err_omp_more_one_clause)
1048 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001049 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001050 }
1051
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001052 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001053 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1054 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001055 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001056 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001057 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001058 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001059 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001060 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001061 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001062 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001063 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001064 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001065 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001066 // OpenMP [2.7.1, Restrictions, p. 9]
1067 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001068 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1069 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001070 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001071 Diag(Tok, diag::err_omp_more_one_clause)
1072 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001073 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001074 }
1075
1076 Clause = ParseOpenMPClause(CKind);
1077 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001078 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001079 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001080 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001081 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001082 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001083 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001084 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001085 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001086 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001087 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001088 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001089 case OMPC_map:
Alexey Bataeveb482352015-12-18 05:05:56 +00001090 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001091 break;
1092 case OMPC_unknown:
1093 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001094 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001095 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001096 break;
1097 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001098 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001099 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1100 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001101 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001102 break;
1103 }
Craig Topper161e4db2014-05-21 06:02:52 +00001104 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001105}
1106
Alexey Bataev2af33e32016-04-07 12:45:37 +00001107/// Parses simple expression in parens for single-expression clauses of OpenMP
1108/// constructs.
1109/// \param RLoc Returned location of right paren.
1110ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1111 SourceLocation &RLoc) {
1112 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1113 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1114 return ExprError();
1115
1116 SourceLocation ELoc = Tok.getLocation();
1117 ExprResult LHS(ParseCastExpression(
1118 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1119 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1120 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1121
1122 // Parse ')'.
1123 T.consumeClose();
1124
1125 RLoc = T.getCloseLocation();
1126 return Val;
1127}
1128
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001129/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001130/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001131/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001132///
Alexey Bataev3778b602014-07-17 07:32:53 +00001133/// final-clause:
1134/// 'final' '(' expression ')'
1135///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001136/// num_threads-clause:
1137/// 'num_threads' '(' expression ')'
1138///
1139/// safelen-clause:
1140/// 'safelen' '(' expression ')'
1141///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001142/// simdlen-clause:
1143/// 'simdlen' '(' expression ')'
1144///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001145/// collapse-clause:
1146/// 'collapse' '(' expression ')'
1147///
Alexey Bataeva0569352015-12-01 10:17:31 +00001148/// priority-clause:
1149/// 'priority' '(' expression ')'
1150///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001151/// grainsize-clause:
1152/// 'grainsize' '(' expression ')'
1153///
Alexey Bataev382967a2015-12-08 12:06:20 +00001154/// num_tasks-clause:
1155/// 'num_tasks' '(' expression ')'
1156///
Alexey Bataev28c75412015-12-15 08:19:24 +00001157/// hint-clause:
1158/// 'hint' '(' expression ')'
1159///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001160OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1161 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001162 SourceLocation LLoc = Tok.getLocation();
1163 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001164
Alexey Bataev2af33e32016-04-07 12:45:37 +00001165 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001166
1167 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001168 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001169
Alexey Bataev2af33e32016-04-07 12:45:37 +00001170 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001171}
1172
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001173/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001174///
1175/// default-clause:
1176/// 'default' '(' 'none' | 'shared' ')
1177///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001178/// proc_bind-clause:
1179/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1180///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001181OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1182 SourceLocation Loc = Tok.getLocation();
1183 SourceLocation LOpen = ConsumeToken();
1184 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001185 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001186 if (T.expectAndConsume(diag::err_expected_lparen_after,
1187 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001188 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001189
Alexey Bataeva55ed262014-05-28 06:15:33 +00001190 unsigned Type = getOpenMPSimpleClauseType(
1191 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001192 SourceLocation TypeLoc = Tok.getLocation();
1193 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1194 Tok.isNot(tok::annot_pragma_openmp_end))
1195 ConsumeAnyToken();
1196
1197 // Parse ')'.
1198 T.consumeClose();
1199
1200 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1201 Tok.getLocation());
1202}
1203
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001204/// \brief Parsing of OpenMP clauses like 'ordered'.
1205///
1206/// ordered-clause:
1207/// 'ordered'
1208///
Alexey Bataev236070f2014-06-20 11:19:47 +00001209/// nowait-clause:
1210/// 'nowait'
1211///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001212/// untied-clause:
1213/// 'untied'
1214///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001215/// mergeable-clause:
1216/// 'mergeable'
1217///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001218/// read-clause:
1219/// 'read'
1220///
Alexey Bataev346265e2015-09-25 10:37:12 +00001221/// threads-clause:
1222/// 'threads'
1223///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001224/// simd-clause:
1225/// 'simd'
1226///
Alexey Bataevb825de12015-12-07 10:51:44 +00001227/// nogroup-clause:
1228/// 'nogroup'
1229///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001230OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1231 SourceLocation Loc = Tok.getLocation();
1232 ConsumeAnyToken();
1233
1234 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1235}
1236
1237
Alexey Bataev56dafe82014-06-20 07:16:17 +00001238/// \brief Parsing of OpenMP clauses with single expressions and some additional
1239/// argument like 'schedule' or 'dist_schedule'.
1240///
1241/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001242/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1243/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001244///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001245/// if-clause:
1246/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1247///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001248/// defaultmap:
1249/// 'defaultmap' '(' modifier ':' kind ')'
1250///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001251OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1252 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001253 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001254 // Parse '('.
1255 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1256 if (T.expectAndConsume(diag::err_expected_lparen_after,
1257 getOpenMPClauseName(Kind)))
1258 return nullptr;
1259
1260 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001261 SmallVector<unsigned, 4> Arg;
1262 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001263 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001264 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1265 Arg.resize(NumberOfElements);
1266 KLoc.resize(NumberOfElements);
1267 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1268 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1269 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1270 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001271 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001272 if (KindModifier > OMPC_SCHEDULE_unknown) {
1273 // Parse 'modifier'
1274 Arg[Modifier1] = KindModifier;
1275 KLoc[Modifier1] = Tok.getLocation();
1276 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1277 Tok.isNot(tok::annot_pragma_openmp_end))
1278 ConsumeAnyToken();
1279 if (Tok.is(tok::comma)) {
1280 // Parse ',' 'modifier'
1281 ConsumeAnyToken();
1282 KindModifier = getOpenMPSimpleClauseType(
1283 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1284 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1285 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001286 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001287 KLoc[Modifier2] = Tok.getLocation();
1288 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1289 Tok.isNot(tok::annot_pragma_openmp_end))
1290 ConsumeAnyToken();
1291 }
1292 // Parse ':'
1293 if (Tok.is(tok::colon))
1294 ConsumeAnyToken();
1295 else
1296 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1297 KindModifier = getOpenMPSimpleClauseType(
1298 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1299 }
1300 Arg[ScheduleKind] = KindModifier;
1301 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001302 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1303 Tok.isNot(tok::annot_pragma_openmp_end))
1304 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001305 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1306 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1307 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001308 Tok.is(tok::comma))
1309 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001310 } else if (Kind == OMPC_dist_schedule) {
1311 Arg.push_back(getOpenMPSimpleClauseType(
1312 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1313 KLoc.push_back(Tok.getLocation());
1314 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1315 Tok.isNot(tok::annot_pragma_openmp_end))
1316 ConsumeAnyToken();
1317 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1318 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001319 } else if (Kind == OMPC_defaultmap) {
1320 // Get a defaultmap modifier
1321 Arg.push_back(getOpenMPSimpleClauseType(
1322 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1323 KLoc.push_back(Tok.getLocation());
1324 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1325 Tok.isNot(tok::annot_pragma_openmp_end))
1326 ConsumeAnyToken();
1327 // Parse ':'
1328 if (Tok.is(tok::colon))
1329 ConsumeAnyToken();
1330 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1331 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1332 // Get a defaultmap kind
1333 Arg.push_back(getOpenMPSimpleClauseType(
1334 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1335 KLoc.push_back(Tok.getLocation());
1336 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1337 Tok.isNot(tok::annot_pragma_openmp_end))
1338 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001339 } else {
1340 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001341 KLoc.push_back(Tok.getLocation());
1342 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1343 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001344 ConsumeToken();
1345 if (Tok.is(tok::colon))
1346 DelimLoc = ConsumeToken();
1347 else
1348 Diag(Tok, diag::warn_pragma_expected_colon)
1349 << "directive name modifier";
1350 }
1351 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001352
Carlo Bertollib4adf552016-01-15 18:50:31 +00001353 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1354 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1355 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001356 if (NeedAnExpression) {
1357 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001358 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1359 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001360 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001361 }
1362
1363 // Parse ')'.
1364 T.consumeClose();
1365
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001366 if (NeedAnExpression && Val.isInvalid())
1367 return nullptr;
1368
Alexey Bataev56dafe82014-06-20 07:16:17 +00001369 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001370 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001371 T.getCloseLocation());
1372}
1373
Alexey Bataevc5e02582014-06-16 07:08:35 +00001374static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1375 UnqualifiedId &ReductionId) {
1376 SourceLocation TemplateKWLoc;
1377 if (ReductionIdScopeSpec.isEmpty()) {
1378 auto OOK = OO_None;
1379 switch (P.getCurToken().getKind()) {
1380 case tok::plus:
1381 OOK = OO_Plus;
1382 break;
1383 case tok::minus:
1384 OOK = OO_Minus;
1385 break;
1386 case tok::star:
1387 OOK = OO_Star;
1388 break;
1389 case tok::amp:
1390 OOK = OO_Amp;
1391 break;
1392 case tok::pipe:
1393 OOK = OO_Pipe;
1394 break;
1395 case tok::caret:
1396 OOK = OO_Caret;
1397 break;
1398 case tok::ampamp:
1399 OOK = OO_AmpAmp;
1400 break;
1401 case tok::pipepipe:
1402 OOK = OO_PipePipe;
1403 break;
1404 default:
1405 break;
1406 }
1407 if (OOK != OO_None) {
1408 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001409 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001410 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1411 return false;
1412 }
1413 }
1414 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1415 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001416 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001417 TemplateKWLoc, ReductionId);
1418}
1419
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001420/// Parses clauses with list.
1421bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1422 OpenMPClauseKind Kind,
1423 SmallVectorImpl<Expr *> &Vars,
1424 OpenMPVarListDataTy &Data) {
1425 UnqualifiedId UnqualifiedReductionId;
1426 bool InvalidReductionId = false;
1427 bool MapTypeModifierSpecified = false;
1428
1429 // Parse '('.
1430 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1431 if (T.expectAndConsume(diag::err_expected_lparen_after,
1432 getOpenMPClauseName(Kind)))
1433 return true;
1434
1435 bool NeedRParenForLinear = false;
1436 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1437 tok::annot_pragma_openmp_end);
1438 // Handle reduction-identifier for reduction clause.
1439 if (Kind == OMPC_reduction) {
1440 ColonProtectionRAIIObject ColonRAII(*this);
1441 if (getLangOpts().CPlusPlus)
1442 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1443 /*ObjectType=*/nullptr,
1444 /*EnteringContext=*/false);
1445 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1446 UnqualifiedReductionId);
1447 if (InvalidReductionId) {
1448 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1449 StopBeforeMatch);
1450 }
1451 if (Tok.is(tok::colon))
1452 Data.ColonLoc = ConsumeToken();
1453 else
1454 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1455 if (!InvalidReductionId)
1456 Data.ReductionId =
1457 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1458 } else if (Kind == OMPC_depend) {
1459 // Handle dependency type for depend clause.
1460 ColonProtectionRAIIObject ColonRAII(*this);
1461 Data.DepKind =
1462 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1463 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1464 Data.DepLinMapLoc = Tok.getLocation();
1465
1466 if (Data.DepKind == OMPC_DEPEND_unknown) {
1467 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1468 StopBeforeMatch);
1469 } else {
1470 ConsumeToken();
1471 // Special processing for depend(source) clause.
1472 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1473 // Parse ')'.
1474 T.consumeClose();
1475 return false;
1476 }
1477 }
1478 if (Tok.is(tok::colon))
1479 Data.ColonLoc = ConsumeToken();
1480 else {
1481 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1482 : diag::warn_pragma_expected_colon)
1483 << "dependency type";
1484 }
1485 } else if (Kind == OMPC_linear) {
1486 // Try to parse modifier if any.
1487 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1488 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1489 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1490 Data.DepLinMapLoc = ConsumeToken();
1491 LinearT.consumeOpen();
1492 NeedRParenForLinear = true;
1493 }
1494 } else if (Kind == OMPC_map) {
1495 // Handle map type for map clause.
1496 ColonProtectionRAIIObject ColonRAII(*this);
1497
1498 /// The map clause modifier token can be either a identifier or the C++
1499 /// delete keyword.
1500 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1501 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1502 };
1503
1504 // The first identifier may be a list item, a map-type or a
1505 // map-type-modifier. The map modifier can also be delete which has the same
1506 // spelling of the C++ delete keyword.
1507 Data.MapType =
1508 IsMapClauseModifierToken(Tok)
1509 ? static_cast<OpenMPMapClauseKind>(
1510 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1511 : OMPC_MAP_unknown;
1512 Data.DepLinMapLoc = Tok.getLocation();
1513 bool ColonExpected = false;
1514
1515 if (IsMapClauseModifierToken(Tok)) {
1516 if (PP.LookAhead(0).is(tok::colon)) {
1517 if (Data.MapType == OMPC_MAP_unknown)
1518 Diag(Tok, diag::err_omp_unknown_map_type);
1519 else if (Data.MapType == OMPC_MAP_always)
1520 Diag(Tok, diag::err_omp_map_type_missing);
1521 ConsumeToken();
1522 } else if (PP.LookAhead(0).is(tok::comma)) {
1523 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1524 PP.LookAhead(2).is(tok::colon)) {
1525 Data.MapTypeModifier = Data.MapType;
1526 if (Data.MapTypeModifier != OMPC_MAP_always) {
1527 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1528 Data.MapTypeModifier = OMPC_MAP_unknown;
1529 } else
1530 MapTypeModifierSpecified = true;
1531
1532 ConsumeToken();
1533 ConsumeToken();
1534
1535 Data.MapType =
1536 IsMapClauseModifierToken(Tok)
1537 ? static_cast<OpenMPMapClauseKind>(
1538 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1539 : OMPC_MAP_unknown;
1540 if (Data.MapType == OMPC_MAP_unknown ||
1541 Data.MapType == OMPC_MAP_always)
1542 Diag(Tok, diag::err_omp_unknown_map_type);
1543 ConsumeToken();
1544 } else {
1545 Data.MapType = OMPC_MAP_tofrom;
1546 Data.IsMapTypeImplicit = true;
1547 }
1548 } else {
1549 Data.MapType = OMPC_MAP_tofrom;
1550 Data.IsMapTypeImplicit = true;
1551 }
1552 } else {
1553 Data.MapType = OMPC_MAP_tofrom;
1554 Data.IsMapTypeImplicit = true;
1555 }
1556
1557 if (Tok.is(tok::colon))
1558 Data.ColonLoc = ConsumeToken();
1559 else if (ColonExpected)
1560 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1561 }
1562
1563 bool IsComma =
1564 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1565 (Kind == OMPC_reduction && !InvalidReductionId) ||
1566 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1567 (!MapTypeModifierSpecified ||
1568 Data.MapTypeModifier == OMPC_MAP_always)) ||
1569 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1570 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1571 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1572 Tok.isNot(tok::annot_pragma_openmp_end))) {
1573 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1574 // Parse variable
1575 ExprResult VarExpr =
1576 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1577 if (VarExpr.isUsable())
1578 Vars.push_back(VarExpr.get());
1579 else {
1580 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1581 StopBeforeMatch);
1582 }
1583 // Skip ',' if any
1584 IsComma = Tok.is(tok::comma);
1585 if (IsComma)
1586 ConsumeToken();
1587 else if (Tok.isNot(tok::r_paren) &&
1588 Tok.isNot(tok::annot_pragma_openmp_end) &&
1589 (!MayHaveTail || Tok.isNot(tok::colon)))
1590 Diag(Tok, diag::err_omp_expected_punc)
1591 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1592 : getOpenMPClauseName(Kind))
1593 << (Kind == OMPC_flush);
1594 }
1595
1596 // Parse ')' for linear clause with modifier.
1597 if (NeedRParenForLinear)
1598 LinearT.consumeClose();
1599
1600 // Parse ':' linear-step (or ':' alignment).
1601 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1602 if (MustHaveTail) {
1603 Data.ColonLoc = Tok.getLocation();
1604 SourceLocation ELoc = ConsumeToken();
1605 ExprResult Tail = ParseAssignmentExpression();
1606 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1607 if (Tail.isUsable())
1608 Data.TailExpr = Tail.get();
1609 else
1610 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1611 StopBeforeMatch);
1612 }
1613
1614 // Parse ')'.
1615 T.consumeClose();
1616 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1617 Vars.empty()) ||
1618 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1619 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1620 return true;
1621 return false;
1622}
1623
Alexander Musman1bb328c2014-06-04 13:06:39 +00001624/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001625/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001626///
1627/// private-clause:
1628/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001629/// firstprivate-clause:
1630/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001631/// lastprivate-clause:
1632/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001633/// shared-clause:
1634/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001635/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001636/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001637/// aligned-clause:
1638/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001639/// reduction-clause:
1640/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001641/// copyprivate-clause:
1642/// 'copyprivate' '(' list ')'
1643/// flush-clause:
1644/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001645/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001646/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001647/// map-clause:
1648/// 'map' '(' [ [ always , ]
1649/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001650///
Alexey Bataev182227b2015-08-20 10:54:39 +00001651/// For 'linear' clause linear-list may have the following forms:
1652/// list
1653/// modifier(list)
1654/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001655OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1656 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001657 SourceLocation Loc = Tok.getLocation();
1658 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001659 SmallVector<Expr *, 4> Vars;
1660 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001661
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001662 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001663 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001664
Alexey Bataevc5e02582014-06-16 07:08:35 +00001665 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001666 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1667 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1668 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1669 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001670}
1671