blob: 988916ac017e0edd43f519536f2a0102759f59e9 [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 Bataev2af33e32016-04-07 12:45:37 +0000335/// 'simdlen' '('<expr> ')'
336static bool parseDeclareSimdClauses(Parser &P,
337 OMPDeclareSimdDeclAttr::BranchStateTy &BS,
338 ExprResult &SimdLen) {
Alexey Bataev20dfd772016-04-04 10:12:15 +0000339 SourceRange BSRange;
340 const Token &Tok = P.getCurToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000341 bool IsError = false;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000342 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
343 if (Tok.isNot(tok::identifier))
344 break;
345 OMPDeclareSimdDeclAttr::BranchStateTy Out;
Alexey Bataev2af33e32016-04-07 12:45:37 +0000346 IdentifierInfo *II = Tok.getIdentifierInfo();
347 StringRef ClauseName = II->getName();
Alexey Bataev20dfd772016-04-04 10:12:15 +0000348 // Parse 'inranch|notinbranch' clauses.
Alexey Bataev2af33e32016-04-07 12:45:37 +0000349 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
Alexey Bataev20dfd772016-04-04 10:12:15 +0000350 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
351 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
Alexey Bataev2af33e32016-04-07 12:45:37 +0000352 << ClauseName
353 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
354 IsError = true;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000355 }
356 BS = Out;
357 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
Alexey Bataev2af33e32016-04-07 12:45:37 +0000358 P.ConsumeToken();
359 } else if (ClauseName.equals("simdlen")) {
360 if (SimdLen.isUsable()) {
361 P.Diag(Tok, diag::err_omp_more_one_clause)
362 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
363 IsError = true;
364 }
365 P.ConsumeToken();
366 SourceLocation RLoc;
367 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
368 if (SimdLen.isInvalid())
369 IsError = true;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000370 } else
371 // TODO: add parsing of other clauses.
372 break;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000373 // Skip ',' if any.
374 if (Tok.is(tok::comma))
375 P.ConsumeToken();
376 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000377 return IsError;
378}
379
380namespace {
381/// RAII that recreates function context for correct parsing of clauses of
382/// 'declare simd' construct.
383/// OpenMP, 2.8.2 declare simd Construct
384/// The expressions appearing in the clauses of this directive are evaluated in
385/// the scope of the arguments of the function declaration or definition.
386class FNContextRAII final {
387 Parser &P;
388 Sema::CXXThisScopeRAII *ThisScope;
389 Parser::ParseScope *TempScope;
390 Parser::ParseScope *FnScope;
391 bool HasTemplateScope = false;
392 bool HasFunScope = false;
393 FNContextRAII() = delete;
394 FNContextRAII(const FNContextRAII &) = delete;
395 FNContextRAII &operator=(const FNContextRAII &) = delete;
396
397public:
398 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
399 Decl *D = *Ptr.get().begin();
400 NamedDecl *ND = dyn_cast<NamedDecl>(D);
401 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
402 Sema &Actions = P.getActions();
403
404 // Allow 'this' within late-parsed attributes.
405 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
406 ND && ND->isCXXInstanceMember());
407
408 // If the Decl is templatized, add template parameters to scope.
409 HasTemplateScope = D->isTemplateDecl();
410 TempScope =
411 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
412 if (HasTemplateScope)
413 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
414
415 // If the Decl is on a function, add function parameters to the scope.
416 HasFunScope = D->isFunctionOrFunctionTemplate();
417 FnScope = new Parser::ParseScope(&P, Scope::FnScope | Scope::DeclScope,
418 HasFunScope);
419 if (HasFunScope)
420 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
421 }
422 ~FNContextRAII() {
423 if (HasFunScope) {
424 P.getActions().ActOnExitFunctionContext();
425 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
426 }
427 if (HasTemplateScope)
428 TempScope->Exit();
429 delete FnScope;
430 delete TempScope;
431 delete ThisScope;
432 }
433};
434} // namespace
435
436/// Parse clauses for '#pragma omp declare simd'.
437Parser::DeclGroupPtrTy
438Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
439 CachedTokens &Toks, SourceLocation Loc) {
440 PP.EnterToken(Tok);
441 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
442 // Consume the previously pushed token.
443 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
444
445 FNContextRAII FnContext(*this, Ptr);
446 OMPDeclareSimdDeclAttr::BranchStateTy BS =
447 OMPDeclareSimdDeclAttr::BS_Undefined;
448 ExprResult Simdlen;
449 bool IsError = parseDeclareSimdClauses(*this, BS, Simdlen);
450 // Need to check for extra tokens.
451 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
452 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
453 << getOpenMPDirectiveName(OMPD_declare_simd);
454 while (Tok.isNot(tok::annot_pragma_openmp_end))
455 ConsumeAnyToken();
456 }
457 // Skip the last annot_pragma_openmp_end.
458 SourceLocation EndLoc = ConsumeToken();
459 if (!IsError)
460 return Actions.ActOnOpenMPDeclareSimdDirective(Ptr, BS, Simdlen.get(),
461 SourceRange(Loc, EndLoc));
462 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000463}
464
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000465/// \brief Parsing of declarative OpenMP directives.
466///
467/// threadprivate-directive:
468/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000469/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000470///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000471/// declare-reduction-directive:
472/// annot_pragma_openmp 'declare' 'reduction' [...]
473/// annot_pragma_openmp_end
474///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000475/// declare-simd-directive:
476/// annot_pragma_openmp 'declare simd' {<clause> [,]}
477/// annot_pragma_openmp_end
478/// <function declaration/definition>
479///
480Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
481 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
482 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000483 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000484 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000485
486 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000487 SmallVector<Expr *, 4> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000488 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000489
490 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000491 case OMPD_threadprivate:
492 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000493 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000494 // The last seen token is annot_pragma_openmp_end - need to check for
495 // extra tokens.
496 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
497 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000498 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000499 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000500 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000501 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000502 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +0000503 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +0000504 }
505 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000506 case OMPD_declare_reduction:
507 ConsumeToken();
508 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
509 // The last seen token is annot_pragma_openmp_end - need to check for
510 // extra tokens.
511 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
512 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
513 << getOpenMPDirectiveName(OMPD_declare_reduction);
514 while (Tok.isNot(tok::annot_pragma_openmp_end))
515 ConsumeAnyToken();
516 }
517 // Skip the last annot_pragma_openmp_end.
518 ConsumeToken();
519 return Res;
520 }
521 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000522 case OMPD_declare_simd: {
523 // The syntax is:
524 // { #pragma omp declare simd }
525 // <function-declaration-or-definition>
526 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000527 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000528 CachedTokens Toks;
529 ConsumeAndStoreUntil(tok::annot_pragma_openmp_end, Toks,
530 /*StopAtSemi=*/false, /*ConsumeFinalToken=*/true);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000531
532 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000533 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000534 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000535 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000536 // Here we expect to see some function declaration.
537 if (AS == AS_none) {
538 assert(TagType == DeclSpec::TST_unspecified);
539 MaybeParseCXX11Attributes(Attrs);
540 MaybeParseMicrosoftAttributes(Attrs);
541 ParsingDeclSpec PDS(*this);
542 Ptr = ParseExternalDeclaration(Attrs, &PDS);
543 } else {
544 Ptr =
545 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
546 }
547 }
548 if (!Ptr) {
549 Diag(Loc, diag::err_omp_decl_in_declare_simd);
550 return DeclGroupPtrTy();
551 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000552 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000553 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000554 case OMPD_declare_target: {
555 SourceLocation DTLoc = ConsumeAnyToken();
556 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
557 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
558 << getOpenMPDirectiveName(OMPD_declare_target);
559 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
560 }
561 // Skip the last annot_pragma_openmp_end.
562 ConsumeAnyToken();
563
564 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
565 return DeclGroupPtrTy();
566
567 DKind = ParseOpenMPDirectiveKind(*this);
568 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
569 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
570 ParsedAttributesWithRange attrs(AttrFactory);
571 MaybeParseCXX11Attributes(attrs);
572 MaybeParseMicrosoftAttributes(attrs);
573 ParseExternalDeclaration(attrs);
574 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
575 TentativeParsingAction TPA(*this);
576 ConsumeToken();
577 DKind = ParseOpenMPDirectiveKind(*this);
578 if (DKind != OMPD_end_declare_target)
579 TPA.Revert();
580 else
581 TPA.Commit();
582 }
583 }
584
585 if (DKind == OMPD_end_declare_target) {
586 ConsumeAnyToken();
587 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
588 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
589 << getOpenMPDirectiveName(OMPD_end_declare_target);
590 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
591 }
592 // Skip the last annot_pragma_openmp_end.
593 ConsumeAnyToken();
594 } else {
595 Diag(Tok, diag::err_expected_end_declare_target);
596 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
597 }
598 Actions.ActOnFinishOpenMPDeclareTargetDirective();
599 return DeclGroupPtrTy();
600 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000601 case OMPD_unknown:
602 Diag(Tok, diag::err_omp_unknown_directive);
603 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000604 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000605 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000606 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000607 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000608 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000609 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000610 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000611 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000612 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000613 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000614 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000615 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000616 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000617 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000618 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000619 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000620 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000621 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000622 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000623 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000624 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000625 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000626 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000627 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000628 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000629 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000630 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000631 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000632 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000633 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000634 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000635 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000636 case OMPD_end_declare_target:
Alexey Bataeva769e072013-03-22 06:34:35 +0000637 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000638 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000639 break;
640 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000641 while (Tok.isNot(tok::annot_pragma_openmp_end))
642 ConsumeAnyToken();
643 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000644 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000645}
646
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000647/// \brief Parsing of declarative or executable OpenMP directives.
648///
649/// threadprivate-directive:
650/// annot_pragma_openmp 'threadprivate' simple-variable-list
651/// annot_pragma_openmp_end
652///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000653/// declare-reduction-directive:
654/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
655/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
656/// ('omp_priv' '=' <expression>|<function_call>) ')']
657/// annot_pragma_openmp_end
658///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000659/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000660/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000661/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
662/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000663/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000664/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000665/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000666/// 'distribute' | 'target enter data' | 'target exit data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000667/// 'target parallel' | 'target parallel for' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000668/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000669///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000670StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
671 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000672 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000673 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000674 SmallVector<Expr *, 5> Identifiers;
675 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000676 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000677 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000678 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000679 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000680 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000681 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000682 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000683 // Name of critical directive.
684 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000685 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000686 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000687 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000688
689 switch (DKind) {
690 case OMPD_threadprivate:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000691 if (Allowed != ACK_Any) {
692 Diag(Tok, diag::err_omp_immediate_directive)
693 << getOpenMPDirectiveName(DKind) << 0;
694 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000695 ConsumeToken();
696 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
697 // The last seen token is annot_pragma_openmp_end - need to check for
698 // extra tokens.
699 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
700 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000701 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000702 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000703 }
704 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000705 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000706 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
707 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000708 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000709 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000710 case OMPD_declare_reduction:
711 ConsumeToken();
712 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
713 // The last seen token is annot_pragma_openmp_end - need to check for
714 // extra tokens.
715 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
716 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
717 << getOpenMPDirectiveName(OMPD_declare_reduction);
718 while (Tok.isNot(tok::annot_pragma_openmp_end))
719 ConsumeAnyToken();
720 }
721 ConsumeAnyToken();
722 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
723 } else
724 SkipUntil(tok::annot_pragma_openmp_end);
725 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000726 case OMPD_flush:
727 if (PP.LookAhead(0).is(tok::l_paren)) {
728 FlushHasClause = true;
729 // Push copy of the current token back to stream to properly parse
730 // pseudo-clause OMPFlushClause.
731 PP.EnterToken(Tok);
732 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000733 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000734 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000735 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000736 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000737 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000738 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000739 case OMPD_target_exit_data:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000740 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000741 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000742 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000743 }
744 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000745 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000746 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000747 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000748 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000749 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000750 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000751 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000752 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000753 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000754 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000755 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000756 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000757 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000758 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000759 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000760 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000761 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000762 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000763 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000764 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000765 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000766 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000767 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000768 case OMPD_taskloop_simd:
769 case OMPD_distribute: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000770 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000771 // Parse directive name of the 'critical' directive if any.
772 if (DKind == OMPD_critical) {
773 BalancedDelimiterTracker T(*this, tok::l_paren,
774 tok::annot_pragma_openmp_end);
775 if (!T.consumeOpen()) {
776 if (Tok.isAnyIdentifier()) {
777 DirName =
778 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
779 ConsumeAnyToken();
780 } else {
781 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
782 }
783 T.consumeClose();
784 }
Alexey Bataev80909872015-07-02 11:25:17 +0000785 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000786 CancelRegion = ParseOpenMPDirectiveKind(*this);
787 if (Tok.isNot(tok::annot_pragma_openmp_end))
788 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000789 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000790
Alexey Bataevf29276e2014-06-18 04:14:57 +0000791 if (isOpenMPLoopDirective(DKind))
792 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
793 if (isOpenMPSimdDirective(DKind))
794 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
795 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000796 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000797
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000798 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000799 OpenMPClauseKind CKind =
800 Tok.isAnnotation()
801 ? OMPC_unknown
802 : FlushHasClause ? OMPC_flush
803 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000804 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000805 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000806 OMPClause *Clause =
807 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000808 FirstClauses[CKind].setInt(true);
809 if (Clause) {
810 FirstClauses[CKind].setPointer(Clause);
811 Clauses.push_back(Clause);
812 }
813
814 // Skip ',' if any.
815 if (Tok.is(tok::comma))
816 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000817 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000818 }
819 // End location of the directive.
820 EndLoc = Tok.getLocation();
821 // Consume final annot_pragma_openmp_end.
822 ConsumeToken();
823
Alexey Bataeveb482352015-12-18 05:05:56 +0000824 // OpenMP [2.13.8, ordered Construct, Syntax]
825 // If the depend clause is specified, the ordered construct is a stand-alone
826 // directive.
827 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000828 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000829 Diag(Loc, diag::err_omp_immediate_directive)
830 << getOpenMPDirectiveName(DKind) << 1
831 << getOpenMPClauseName(OMPC_depend);
832 }
833 HasAssociatedStatement = false;
834 }
835
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000836 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000837 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000838 // The body is a block scope like in Lambdas and Blocks.
839 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000840 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000841 Actions.ActOnStartOfCompoundStmt();
842 // Parse statement
843 AssociatedStmt = ParseStatement();
844 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000845 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000846 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000847 Directive = Actions.ActOnOpenMPExecutableDirective(
848 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
849 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000850
851 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000852 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000853 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000854 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000855 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000856 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000857 case OMPD_declare_target:
858 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +0000859 Diag(Tok, diag::err_omp_unexpected_directive)
860 << getOpenMPDirectiveName(DKind);
861 SkipUntil(tok::annot_pragma_openmp_end);
862 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000863 case OMPD_unknown:
864 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000865 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000866 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000867 }
868 return Directive;
869}
870
Alexey Bataeva769e072013-03-22 06:34:35 +0000871/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000872/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000873///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000874/// simple-variable-list:
875/// '(' id-expression {, id-expression} ')'
876///
877bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
878 SmallVectorImpl<Expr *> &VarList,
879 bool AllowScopeSpecifier) {
880 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000881 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000882 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000883 if (T.expectAndConsume(diag::err_expected_lparen_after,
884 getOpenMPDirectiveName(Kind)))
885 return true;
886 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000887 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000888
889 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000890 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000891 CXXScopeSpec SS;
892 SourceLocation TemplateKWLoc;
893 UnqualifiedId Name;
894 // Read var name.
895 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000896 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000897
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000898 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +0000899 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000900 IsCorrect = false;
901 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000902 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +0000903 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000904 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000905 IsCorrect = false;
906 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000907 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000908 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
909 Tok.isNot(tok::annot_pragma_openmp_end)) {
910 IsCorrect = false;
911 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000912 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000913 Diag(PrevTok.getLocation(), diag::err_expected)
914 << tok::identifier
915 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000916 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000917 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000918 ExprResult Res =
919 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000920 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000921 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000922 }
923 // Consume ','.
924 if (Tok.is(tok::comma)) {
925 ConsumeToken();
926 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000927 }
928
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000929 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000930 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000931 IsCorrect = false;
932 }
933
934 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000935 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000936
937 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000938}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000939
940/// \brief Parsing of OpenMP clauses.
941///
942/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000943/// if-clause | final-clause | num_threads-clause | safelen-clause |
944/// default-clause | private-clause | firstprivate-clause | shared-clause
945/// | linear-clause | aligned-clause | collapse-clause |
946/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000947/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000948/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +0000949/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000950/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000951/// thread_limit-clause | priority-clause | grainsize-clause |
Alexey Bataev28c75412015-12-15 08:19:24 +0000952/// nogroup-clause | num_tasks-clause | hint-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000953///
954OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
955 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000956 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000957 bool ErrorFound = false;
958 // Check if clause is allowed for the given directive.
959 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000960 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
961 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000962 ErrorFound = true;
963 }
964
965 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +0000966 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000967 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000968 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +0000969 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000970 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +0000971 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +0000972 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +0000973 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000974 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +0000975 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000976 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +0000977 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +0000978 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000979 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +0000980 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000981 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000982 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +0000983 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +0000984 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +0000985 // OpenMP [2.9.1, target data construct, Restrictions]
986 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000987 // OpenMP [2.11.1, task Construct, Restrictions]
988 // At most one if clause can appear on the directive.
989 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +0000990 // OpenMP [teams Construct, Restrictions]
991 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000992 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +0000993 // OpenMP [2.9.1, task Construct, Restrictions]
994 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000995 // OpenMP [2.9.2, taskloop Construct, Restrictions]
996 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +0000997 // OpenMP [2.9.2, taskloop Construct, Restrictions]
998 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000999 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001000 Diag(Tok, diag::err_omp_more_one_clause)
1001 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001002 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001003 }
1004
Alexey Bataev10e775f2015-07-30 11:36:16 +00001005 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1006 Clause = ParseOpenMPClause(CKind);
1007 else
1008 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001009 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001010 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001011 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001012 // OpenMP [2.14.3.1, Restrictions]
1013 // Only a single default clause may be specified on a parallel, task or
1014 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001015 // OpenMP [2.5, parallel Construct, Restrictions]
1016 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001017 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001018 Diag(Tok, diag::err_omp_more_one_clause)
1019 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001020 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001021 }
1022
1023 Clause = ParseOpenMPSimpleClause(CKind);
1024 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001025 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001026 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001027 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001028 // OpenMP [2.7.1, Restrictions, p. 3]
1029 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001030 // OpenMP [2.10.4, Restrictions, p. 106]
1031 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001032 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001033 Diag(Tok, diag::err_omp_more_one_clause)
1034 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001035 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001036 }
1037
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001038 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001039 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1040 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001041 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001042 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001043 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001044 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001045 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001046 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001047 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001048 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001049 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001050 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001051 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001052 // OpenMP [2.7.1, Restrictions, p. 9]
1053 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001054 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1055 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001056 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001057 Diag(Tok, diag::err_omp_more_one_clause)
1058 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001059 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001060 }
1061
1062 Clause = ParseOpenMPClause(CKind);
1063 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001064 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001065 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001066 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001067 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001068 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001069 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001070 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001071 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001072 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001073 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001074 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001075 case OMPC_map:
Alexey Bataeveb482352015-12-18 05:05:56 +00001076 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001077 break;
1078 case OMPC_unknown:
1079 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001080 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001081 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001082 break;
1083 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001084 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1085 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001086 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001087 break;
1088 }
Craig Topper161e4db2014-05-21 06:02:52 +00001089 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001090}
1091
Alexey Bataev2af33e32016-04-07 12:45:37 +00001092/// Parses simple expression in parens for single-expression clauses of OpenMP
1093/// constructs.
1094/// \param RLoc Returned location of right paren.
1095ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1096 SourceLocation &RLoc) {
1097 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1098 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1099 return ExprError();
1100
1101 SourceLocation ELoc = Tok.getLocation();
1102 ExprResult LHS(ParseCastExpression(
1103 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1104 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1105 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1106
1107 // Parse ')'.
1108 T.consumeClose();
1109
1110 RLoc = T.getCloseLocation();
1111 return Val;
1112}
1113
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001114/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001115/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001116/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001117///
Alexey Bataev3778b602014-07-17 07:32:53 +00001118/// final-clause:
1119/// 'final' '(' expression ')'
1120///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001121/// num_threads-clause:
1122/// 'num_threads' '(' expression ')'
1123///
1124/// safelen-clause:
1125/// 'safelen' '(' expression ')'
1126///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001127/// simdlen-clause:
1128/// 'simdlen' '(' expression ')'
1129///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001130/// collapse-clause:
1131/// 'collapse' '(' expression ')'
1132///
Alexey Bataeva0569352015-12-01 10:17:31 +00001133/// priority-clause:
1134/// 'priority' '(' expression ')'
1135///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001136/// grainsize-clause:
1137/// 'grainsize' '(' expression ')'
1138///
Alexey Bataev382967a2015-12-08 12:06:20 +00001139/// num_tasks-clause:
1140/// 'num_tasks' '(' expression ')'
1141///
Alexey Bataev28c75412015-12-15 08:19:24 +00001142/// hint-clause:
1143/// 'hint' '(' expression ')'
1144///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001145OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1146 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001147 SourceLocation LLoc = Tok.getLocation();
1148 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001149
Alexey Bataev2af33e32016-04-07 12:45:37 +00001150 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001151
1152 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001153 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001154
Alexey Bataev2af33e32016-04-07 12:45:37 +00001155 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001156}
1157
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001158/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001159///
1160/// default-clause:
1161/// 'default' '(' 'none' | 'shared' ')
1162///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001163/// proc_bind-clause:
1164/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1165///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001166OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1167 SourceLocation Loc = Tok.getLocation();
1168 SourceLocation LOpen = ConsumeToken();
1169 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001170 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001171 if (T.expectAndConsume(diag::err_expected_lparen_after,
1172 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001173 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001174
Alexey Bataeva55ed262014-05-28 06:15:33 +00001175 unsigned Type = getOpenMPSimpleClauseType(
1176 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001177 SourceLocation TypeLoc = Tok.getLocation();
1178 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1179 Tok.isNot(tok::annot_pragma_openmp_end))
1180 ConsumeAnyToken();
1181
1182 // Parse ')'.
1183 T.consumeClose();
1184
1185 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1186 Tok.getLocation());
1187}
1188
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001189/// \brief Parsing of OpenMP clauses like 'ordered'.
1190///
1191/// ordered-clause:
1192/// 'ordered'
1193///
Alexey Bataev236070f2014-06-20 11:19:47 +00001194/// nowait-clause:
1195/// 'nowait'
1196///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001197/// untied-clause:
1198/// 'untied'
1199///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001200/// mergeable-clause:
1201/// 'mergeable'
1202///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001203/// read-clause:
1204/// 'read'
1205///
Alexey Bataev346265e2015-09-25 10:37:12 +00001206/// threads-clause:
1207/// 'threads'
1208///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001209/// simd-clause:
1210/// 'simd'
1211///
Alexey Bataevb825de12015-12-07 10:51:44 +00001212/// nogroup-clause:
1213/// 'nogroup'
1214///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001215OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1216 SourceLocation Loc = Tok.getLocation();
1217 ConsumeAnyToken();
1218
1219 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1220}
1221
1222
Alexey Bataev56dafe82014-06-20 07:16:17 +00001223/// \brief Parsing of OpenMP clauses with single expressions and some additional
1224/// argument like 'schedule' or 'dist_schedule'.
1225///
1226/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001227/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1228/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001229///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001230/// if-clause:
1231/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1232///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001233/// defaultmap:
1234/// 'defaultmap' '(' modifier ':' kind ')'
1235///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001236OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1237 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001238 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001239 // Parse '('.
1240 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1241 if (T.expectAndConsume(diag::err_expected_lparen_after,
1242 getOpenMPClauseName(Kind)))
1243 return nullptr;
1244
1245 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001246 SmallVector<unsigned, 4> Arg;
1247 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001248 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001249 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1250 Arg.resize(NumberOfElements);
1251 KLoc.resize(NumberOfElements);
1252 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1253 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1254 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1255 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001256 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001257 if (KindModifier > OMPC_SCHEDULE_unknown) {
1258 // Parse 'modifier'
1259 Arg[Modifier1] = KindModifier;
1260 KLoc[Modifier1] = Tok.getLocation();
1261 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1262 Tok.isNot(tok::annot_pragma_openmp_end))
1263 ConsumeAnyToken();
1264 if (Tok.is(tok::comma)) {
1265 // Parse ',' 'modifier'
1266 ConsumeAnyToken();
1267 KindModifier = getOpenMPSimpleClauseType(
1268 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1269 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1270 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001271 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001272 KLoc[Modifier2] = Tok.getLocation();
1273 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1274 Tok.isNot(tok::annot_pragma_openmp_end))
1275 ConsumeAnyToken();
1276 }
1277 // Parse ':'
1278 if (Tok.is(tok::colon))
1279 ConsumeAnyToken();
1280 else
1281 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1282 KindModifier = getOpenMPSimpleClauseType(
1283 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1284 }
1285 Arg[ScheduleKind] = KindModifier;
1286 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001287 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1288 Tok.isNot(tok::annot_pragma_openmp_end))
1289 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001290 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1291 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1292 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001293 Tok.is(tok::comma))
1294 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001295 } else if (Kind == OMPC_dist_schedule) {
1296 Arg.push_back(getOpenMPSimpleClauseType(
1297 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1298 KLoc.push_back(Tok.getLocation());
1299 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1300 Tok.isNot(tok::annot_pragma_openmp_end))
1301 ConsumeAnyToken();
1302 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1303 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001304 } else if (Kind == OMPC_defaultmap) {
1305 // Get a defaultmap modifier
1306 Arg.push_back(getOpenMPSimpleClauseType(
1307 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1308 KLoc.push_back(Tok.getLocation());
1309 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1310 Tok.isNot(tok::annot_pragma_openmp_end))
1311 ConsumeAnyToken();
1312 // Parse ':'
1313 if (Tok.is(tok::colon))
1314 ConsumeAnyToken();
1315 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1316 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1317 // Get a defaultmap kind
1318 Arg.push_back(getOpenMPSimpleClauseType(
1319 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1320 KLoc.push_back(Tok.getLocation());
1321 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1322 Tok.isNot(tok::annot_pragma_openmp_end))
1323 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001324 } else {
1325 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001326 KLoc.push_back(Tok.getLocation());
1327 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1328 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001329 ConsumeToken();
1330 if (Tok.is(tok::colon))
1331 DelimLoc = ConsumeToken();
1332 else
1333 Diag(Tok, diag::warn_pragma_expected_colon)
1334 << "directive name modifier";
1335 }
1336 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001337
Carlo Bertollib4adf552016-01-15 18:50:31 +00001338 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1339 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1340 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001341 if (NeedAnExpression) {
1342 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001343 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1344 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001345 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001346 }
1347
1348 // Parse ')'.
1349 T.consumeClose();
1350
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001351 if (NeedAnExpression && Val.isInvalid())
1352 return nullptr;
1353
Alexey Bataev56dafe82014-06-20 07:16:17 +00001354 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001355 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001356 T.getCloseLocation());
1357}
1358
Alexey Bataevc5e02582014-06-16 07:08:35 +00001359static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1360 UnqualifiedId &ReductionId) {
1361 SourceLocation TemplateKWLoc;
1362 if (ReductionIdScopeSpec.isEmpty()) {
1363 auto OOK = OO_None;
1364 switch (P.getCurToken().getKind()) {
1365 case tok::plus:
1366 OOK = OO_Plus;
1367 break;
1368 case tok::minus:
1369 OOK = OO_Minus;
1370 break;
1371 case tok::star:
1372 OOK = OO_Star;
1373 break;
1374 case tok::amp:
1375 OOK = OO_Amp;
1376 break;
1377 case tok::pipe:
1378 OOK = OO_Pipe;
1379 break;
1380 case tok::caret:
1381 OOK = OO_Caret;
1382 break;
1383 case tok::ampamp:
1384 OOK = OO_AmpAmp;
1385 break;
1386 case tok::pipepipe:
1387 OOK = OO_PipePipe;
1388 break;
1389 default:
1390 break;
1391 }
1392 if (OOK != OO_None) {
1393 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001394 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001395 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1396 return false;
1397 }
1398 }
1399 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1400 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001401 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001402 TemplateKWLoc, ReductionId);
1403}
1404
Alexander Musman1bb328c2014-06-04 13:06:39 +00001405/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001406/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001407///
1408/// private-clause:
1409/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001410/// firstprivate-clause:
1411/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001412/// lastprivate-clause:
1413/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001414/// shared-clause:
1415/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001416/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001417/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001418/// aligned-clause:
1419/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001420/// reduction-clause:
1421/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001422/// copyprivate-clause:
1423/// 'copyprivate' '(' list ')'
1424/// flush-clause:
1425/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001426/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001427/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001428/// map-clause:
1429/// 'map' '(' [ [ always , ]
1430/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001431///
Alexey Bataev182227b2015-08-20 10:54:39 +00001432/// For 'linear' clause linear-list may have the following forms:
1433/// list
1434/// modifier(list)
1435/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001436OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1437 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001438 SourceLocation Loc = Tok.getLocation();
1439 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +00001440 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +00001441 // Optional scope specifier and unqualified id for reduction identifier.
1442 CXXScopeSpec ReductionIdScopeSpec;
1443 UnqualifiedId ReductionId;
1444 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001445 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
Alexey Bataev182227b2015-08-20 10:54:39 +00001446 // OpenMP 4.1 [2.15.3.7, linear Clause]
1447 // If no modifier is specified it is assumed to be val.
1448 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001449 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
1450 OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown;
Samuel Antao23abd722016-01-19 20:40:49 +00001451 bool MapTypeIsImplicit = false;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001452 bool MapTypeModifierSpecified = false;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001453 SourceLocation DepLinMapLoc;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001454
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001455 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001456 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001457 if (T.expectAndConsume(diag::err_expected_lparen_after,
1458 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001459 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001460
Alexey Bataev182227b2015-08-20 10:54:39 +00001461 bool NeedRParenForLinear = false;
1462 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1463 tok::annot_pragma_openmp_end);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001464 // Handle reduction-identifier for reduction clause.
1465 if (Kind == OMPC_reduction) {
1466 ColonProtectionRAIIObject ColonRAII(*this);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001467 if (getLangOpts().CPlusPlus)
1468 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec,
1469 /*ObjectType=*/nullptr,
1470 /*EnteringContext=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001471 InvalidReductionId =
1472 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
1473 if (InvalidReductionId) {
1474 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1475 StopBeforeMatch);
1476 }
1477 if (Tok.is(tok::colon)) {
1478 ColonLoc = ConsumeToken();
1479 } else {
1480 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1481 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001482 } else if (Kind == OMPC_depend) {
1483 // Handle dependency type for depend clause.
1484 ColonProtectionRAIIObject ColonRAII(*this);
1485 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1486 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001487 DepLinMapLoc = Tok.getLocation();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001488
1489 if (DepKind == OMPC_DEPEND_unknown) {
1490 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1491 StopBeforeMatch);
1492 } else {
1493 ConsumeToken();
Alexey Bataeveb482352015-12-18 05:05:56 +00001494 // Special processing for depend(source) clause.
1495 if (DKind == OMPD_ordered && DepKind == OMPC_DEPEND_source) {
1496 // Parse ')'.
1497 T.consumeClose();
1498 return Actions.ActOnOpenMPVarListClause(
1499 Kind, llvm::None, /*TailExpr=*/nullptr, Loc, LOpen,
1500 /*ColonLoc=*/SourceLocation(), Tok.getLocation(),
1501 ReductionIdScopeSpec, DeclarationNameInfo(), DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00001502 LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
1503 DepLinMapLoc);
Alexey Bataeveb482352015-12-18 05:05:56 +00001504 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001505 }
1506 if (Tok.is(tok::colon)) {
1507 ColonLoc = ConsumeToken();
1508 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00001509 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1510 : diag::warn_pragma_expected_colon)
1511 << "dependency type";
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001512 }
Alexey Bataev182227b2015-08-20 10:54:39 +00001513 } else if (Kind == OMPC_linear) {
1514 // Try to parse modifier if any.
1515 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev182227b2015-08-20 10:54:39 +00001516 LinearModifier = static_cast<OpenMPLinearClauseKind>(
Alexey Bataev1185e192015-08-20 12:15:57 +00001517 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001518 DepLinMapLoc = ConsumeToken();
Alexey Bataev182227b2015-08-20 10:54:39 +00001519 LinearT.consumeOpen();
1520 NeedRParenForLinear = true;
1521 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00001522 } else if (Kind == OMPC_map) {
1523 // Handle map type for map clause.
1524 ColonProtectionRAIIObject ColonRAII(*this);
1525
Samuel Antaof91b1632016-02-27 00:01:58 +00001526 /// The map clause modifier token can be either a identifier or the C++
1527 /// delete keyword.
1528 auto IsMapClauseModifierToken = [](const Token &Tok) {
1529 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1530 };
1531
1532 // The first identifier may be a list item, a map-type or a
1533 // map-type-modifier. The map modifier can also be delete which has the same
1534 // spelling of the C++ delete keyword.
Kelvin Li0bff7af2015-11-23 05:32:03 +00001535 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001536 Kind, IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001537 DepLinMapLoc = Tok.getLocation();
1538 bool ColonExpected = false;
1539
Samuel Antaof91b1632016-02-27 00:01:58 +00001540 if (IsMapClauseModifierToken(Tok)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00001541 if (PP.LookAhead(0).is(tok::colon)) {
1542 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001543 Kind, IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001544 if (MapType == OMPC_MAP_unknown) {
1545 Diag(Tok, diag::err_omp_unknown_map_type);
1546 } else if (MapType == OMPC_MAP_always) {
1547 Diag(Tok, diag::err_omp_map_type_missing);
1548 }
1549 ConsumeToken();
1550 } else if (PP.LookAhead(0).is(tok::comma)) {
Samuel Antaof91b1632016-02-27 00:01:58 +00001551 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
Kelvin Li0bff7af2015-11-23 05:32:03 +00001552 PP.LookAhead(2).is(tok::colon)) {
1553 MapTypeModifier =
1554 static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001555 Kind,
1556 IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001557 if (MapTypeModifier != OMPC_MAP_always) {
1558 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1559 MapTypeModifier = OMPC_MAP_unknown;
1560 } else {
1561 MapTypeModifierSpecified = true;
1562 }
1563
1564 ConsumeToken();
1565 ConsumeToken();
1566
1567 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001568 Kind, IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001569 if (MapType == OMPC_MAP_unknown || MapType == OMPC_MAP_always) {
1570 Diag(Tok, diag::err_omp_unknown_map_type);
1571 }
1572 ConsumeToken();
1573 } else {
1574 MapType = OMPC_MAP_tofrom;
Samuel Antao23abd722016-01-19 20:40:49 +00001575 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001576 }
1577 } else {
1578 MapType = OMPC_MAP_tofrom;
Samuel Antao23abd722016-01-19 20:40:49 +00001579 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001580 }
1581 } else {
Samuel Antao5de996e2016-01-22 20:21:36 +00001582 MapType = OMPC_MAP_tofrom;
1583 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001584 }
1585
1586 if (Tok.is(tok::colon)) {
1587 ColonLoc = ConsumeToken();
1588 } else if (ColonExpected) {
1589 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1590 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00001591 }
1592
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001593 SmallVector<Expr *, 5> Vars;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001594 bool IsComma =
1595 ((Kind != OMPC_reduction) && (Kind != OMPC_depend) &&
1596 (Kind != OMPC_map)) ||
1597 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
Samuel Antao5de996e2016-01-22 20:21:36 +00001598 ((Kind == OMPC_map) && (MapType != OMPC_MAP_unknown) &&
Kelvin Li0bff7af2015-11-23 05:32:03 +00001599 (!MapTypeModifierSpecified ||
1600 (MapTypeModifierSpecified && MapTypeModifier == OMPC_MAP_always))) ||
1601 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001602 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +00001603 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001604 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +00001605 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001606 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +00001607 ExprResult VarExpr =
1608 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataevc5970622016-04-01 08:43:42 +00001609 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001610 Vars.push_back(VarExpr.get());
Alexey Bataevc5970622016-04-01 08:43:42 +00001611 } else {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001612 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001613 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001614 }
1615 // Skip ',' if any
1616 IsComma = Tok.is(tok::comma);
Alexey Bataevc5970622016-04-01 08:43:42 +00001617 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001618 ConsumeToken();
Alexey Bataevc5970622016-04-01 08:43:42 +00001619 else if (Tok.isNot(tok::r_paren) &&
Alexander Musman8dba6642014-04-22 13:09:42 +00001620 Tok.isNot(tok::annot_pragma_openmp_end) &&
1621 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +00001622 Diag(Tok, diag::err_omp_expected_punc)
1623 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1624 : getOpenMPClauseName(Kind))
1625 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +00001626 }
1627
Alexey Bataev182227b2015-08-20 10:54:39 +00001628 // Parse ')' for linear clause with modifier.
1629 if (NeedRParenForLinear)
1630 LinearT.consumeClose();
1631
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001632 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +00001633 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00001634 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1635 if (MustHaveTail) {
1636 ColonLoc = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001637 SourceLocation ELoc = ConsumeToken();
1638 ExprResult Tail = ParseAssignmentExpression();
1639 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00001640 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001641 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00001642 else
1643 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1644 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001645 }
1646
1647 // Parse ')'.
1648 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001649 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
Samuel Antao5de996e2016-01-22 20:21:36 +00001650 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1651 (MustHaveTail && !TailExpr) || InvalidReductionId) {
Craig Topper161e4db2014-05-21 06:02:52 +00001652 return nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001653 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001654
Alexey Bataevc5e02582014-06-16 07:08:35 +00001655 return Actions.ActOnOpenMPVarListClause(
1656 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
1657 ReductionIdScopeSpec,
1658 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001659 : DeclarationNameInfo(),
Samuel Antao23abd722016-01-19 20:40:49 +00001660 DepKind, LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
1661 DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001662}
1663