blob: 3e8c75b201a808110eb67529e078d1c8eab89b89 [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexey Bataeva769e072013-03-22 06:34:35 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009/// This file implements parsing of all OpenMP directives and clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000010///
11//===----------------------------------------------------------------------===//
12
Alexey Bataev9959db52014-05-06 10:08:46 +000013#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000014#include "clang/AST/StmtOpenMP.h"
Alexey Bataev93dc40d2019-12-20 11:04:57 -050015#include "clang/Basic/OpenMPKinds.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000016#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000017#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000018#include "clang/Parse/RAIIObjectsForParser.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000019#include "clang/Sema/Scope.h"
20#include "llvm/ADT/PointerIntPair.h"
Alexey Bataev4513e93f2019-10-10 15:15:26 +000021#include "llvm/ADT/UniqueVector.h"
Michael Wong65f367f2015-07-21 13:44:28 +000022
Alexey Bataeva769e072013-03-22 06:34:35 +000023using namespace clang;
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060024using namespace llvm::omp;
Alexey Bataeva769e072013-03-22 06:34:35 +000025
26//===----------------------------------------------------------------------===//
27// OpenMP declarative directives.
28//===----------------------------------------------------------------------===//
29
Dmitry Polukhin82478332016-02-13 06:53:38 +000030namespace {
31enum OpenMPDirectiveKindEx {
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060032 OMPD_cancellation = unsigned(OMPD_unknown) + 1,
Dmitry Polukhin82478332016-02-13 06:53:38 +000033 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000034 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000035 OMPD_end,
36 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000037 OMPD_enter,
38 OMPD_exit,
39 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000040 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000041 OMPD_target_enter,
Samuel Antao686c70c2016-05-26 17:30:50 +000042 OMPD_target_exit,
43 OMPD_update,
Kelvin Li579e41c2016-11-30 23:51:03 +000044 OMPD_distribute_parallel,
Kelvin Li80e8f562016-12-29 22:16:30 +000045 OMPD_teams_distribute_parallel,
Michael Kruse251e1482019-02-01 20:25:04 +000046 OMPD_target_teams_distribute_parallel,
47 OMPD_mapper,
Alexey Bataevd158cf62019-09-13 20:18:17 +000048 OMPD_variant,
Dmitry Polukhin82478332016-02-13 06:53:38 +000049};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000050
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060051// Helper to unify the enum class OpenMPDirectiveKind with its extension
52// the OpenMPDirectiveKindEx enum which allows to use them together as if they
53// are unsigned values.
54struct OpenMPDirectiveKindExWrapper {
55 OpenMPDirectiveKindExWrapper(unsigned Value) : Value(Value) {}
56 OpenMPDirectiveKindExWrapper(OpenMPDirectiveKind DK) : Value(unsigned(DK)) {}
57 bool operator==(OpenMPDirectiveKind V) const { return Value == unsigned(V); }
58 bool operator!=(OpenMPDirectiveKind V) const { return Value != unsigned(V); }
59 bool operator<(OpenMPDirectiveKind V) const { return Value < unsigned(V); }
60 operator unsigned() const { return Value; }
61 operator OpenMPDirectiveKind() const { return OpenMPDirectiveKind(Value); }
62 unsigned Value;
63};
64
Alexey Bataev25ed0c02019-03-07 17:54:44 +000065class DeclDirectiveListParserHelper final {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000066 SmallVector<Expr *, 4> Identifiers;
67 Parser *P;
Alexey Bataev25ed0c02019-03-07 17:54:44 +000068 OpenMPDirectiveKind Kind;
Dmitry Polukhind69b5052016-05-09 14:59:13 +000069
70public:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000071 DeclDirectiveListParserHelper(Parser *P, OpenMPDirectiveKind Kind)
72 : P(P), Kind(Kind) {}
Dmitry Polukhind69b5052016-05-09 14:59:13 +000073 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
Alexey Bataev25ed0c02019-03-07 17:54:44 +000074 ExprResult Res = P->getActions().ActOnOpenMPIdExpression(
75 P->getCurScope(), SS, NameInfo, Kind);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000076 if (Res.isUsable())
77 Identifiers.push_back(Res.get());
78 }
79 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
80};
Dmitry Polukhin82478332016-02-13 06:53:38 +000081} // namespace
82
83// Map token string to extended OMP token kind that are
84// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
85static unsigned getOpenMPDirectiveKindEx(StringRef S) {
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060086 OpenMPDirectiveKindExWrapper DKind = getOpenMPDirectiveKind(S);
Dmitry Polukhin82478332016-02-13 06:53:38 +000087 if (DKind != OMPD_unknown)
88 return DKind;
89
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060090 return llvm::StringSwitch<OpenMPDirectiveKindExWrapper>(S)
Dmitry Polukhin82478332016-02-13 06:53:38 +000091 .Case("cancellation", OMPD_cancellation)
92 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000093 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000094 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000095 .Case("enter", OMPD_enter)
96 .Case("exit", OMPD_exit)
97 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000098 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000099 .Case("update", OMPD_update)
Michael Kruse251e1482019-02-01 20:25:04 +0000100 .Case("mapper", OMPD_mapper)
Alexey Bataevd158cf62019-09-13 20:18:17 +0000101 .Case("variant", OMPD_variant)
Dmitry Polukhin82478332016-02-13 06:53:38 +0000102 .Default(OMPD_unknown);
103}
104
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600105static OpenMPDirectiveKindExWrapper parseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +0000106 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
107 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
108 // TODO: add other combined directives in topological order.
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600109 static const OpenMPDirectiveKindExWrapper F[][3] = {
Alexey Bataev61908f652018-04-23 19:53:05 +0000110 {OMPD_cancellation, OMPD_point, OMPD_cancellation_point},
111 {OMPD_declare, OMPD_reduction, OMPD_declare_reduction},
Michael Kruse251e1482019-02-01 20:25:04 +0000112 {OMPD_declare, OMPD_mapper, OMPD_declare_mapper},
Alexey Bataev61908f652018-04-23 19:53:05 +0000113 {OMPD_declare, OMPD_simd, OMPD_declare_simd},
114 {OMPD_declare, OMPD_target, OMPD_declare_target},
Alexey Bataevd158cf62019-09-13 20:18:17 +0000115 {OMPD_declare, OMPD_variant, OMPD_declare_variant},
Alexey Bataev61908f652018-04-23 19:53:05 +0000116 {OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel},
117 {OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for},
118 {OMPD_distribute_parallel_for, OMPD_simd,
119 OMPD_distribute_parallel_for_simd},
120 {OMPD_distribute, OMPD_simd, OMPD_distribute_simd},
121 {OMPD_end, OMPD_declare, OMPD_end_declare},
122 {OMPD_end_declare, OMPD_target, OMPD_end_declare_target},
123 {OMPD_target, OMPD_data, OMPD_target_data},
124 {OMPD_target, OMPD_enter, OMPD_target_enter},
125 {OMPD_target, OMPD_exit, OMPD_target_exit},
126 {OMPD_target, OMPD_update, OMPD_target_update},
127 {OMPD_target_enter, OMPD_data, OMPD_target_enter_data},
128 {OMPD_target_exit, OMPD_data, OMPD_target_exit_data},
129 {OMPD_for, OMPD_simd, OMPD_for_simd},
130 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
131 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
132 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
133 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd},
134 {OMPD_target, OMPD_parallel, OMPD_target_parallel},
135 {OMPD_target, OMPD_simd, OMPD_target_simd},
136 {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for},
137 {OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd},
138 {OMPD_teams, OMPD_distribute, OMPD_teams_distribute},
139 {OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd},
140 {OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel},
141 {OMPD_teams_distribute_parallel, OMPD_for,
142 OMPD_teams_distribute_parallel_for},
143 {OMPD_teams_distribute_parallel_for, OMPD_simd,
144 OMPD_teams_distribute_parallel_for_simd},
145 {OMPD_target, OMPD_teams, OMPD_target_teams},
146 {OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute},
147 {OMPD_target_teams_distribute, OMPD_parallel,
148 OMPD_target_teams_distribute_parallel},
149 {OMPD_target_teams_distribute, OMPD_simd,
150 OMPD_target_teams_distribute_simd},
151 {OMPD_target_teams_distribute_parallel, OMPD_for,
152 OMPD_target_teams_distribute_parallel_for},
153 {OMPD_target_teams_distribute_parallel_for, OMPD_simd,
Alexey Bataev60e51c42019-10-10 20:13:02 +0000154 OMPD_target_teams_distribute_parallel_for_simd},
Alexey Bataev5bbcead2019-10-14 17:17:41 +0000155 {OMPD_master, OMPD_taskloop, OMPD_master_taskloop},
Alexey Bataevb8552ab2019-10-18 16:47:35 +0000156 {OMPD_master_taskloop, OMPD_simd, OMPD_master_taskloop_simd},
Alexey Bataev5bbcead2019-10-14 17:17:41 +0000157 {OMPD_parallel, OMPD_master, OMPD_parallel_master},
Alexey Bataev14a388f2019-10-25 10:27:13 -0400158 {OMPD_parallel_master, OMPD_taskloop, OMPD_parallel_master_taskloop},
159 {OMPD_parallel_master_taskloop, OMPD_simd,
160 OMPD_parallel_master_taskloop_simd}};
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000161 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev61908f652018-04-23 19:53:05 +0000162 Token Tok = P.getCurToken();
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600163 OpenMPDirectiveKindExWrapper DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000164 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000165 ? static_cast<unsigned>(OMPD_unknown)
166 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
167 if (DKind == OMPD_unknown)
168 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000169
Alexey Bataev61908f652018-04-23 19:53:05 +0000170 for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) {
171 if (DKind != F[I][0])
Dmitry Polukhin82478332016-02-13 06:53:38 +0000172 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000173
Dmitry Polukhin82478332016-02-13 06:53:38 +0000174 Tok = P.getPreprocessor().LookAhead(0);
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600175 OpenMPDirectiveKindExWrapper SDKind =
Dmitry Polukhin82478332016-02-13 06:53:38 +0000176 Tok.isAnnotation()
177 ? static_cast<unsigned>(OMPD_unknown)
178 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
179 if (SDKind == OMPD_unknown)
180 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000181
Alexey Bataev61908f652018-04-23 19:53:05 +0000182 if (SDKind == F[I][1]) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000183 P.ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000184 DKind = F[I][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000185 }
186 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000187 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
188 : OMPD_unknown;
189}
190
191static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000192 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000193 Sema &Actions = P.getActions();
194 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000195 // Allow to use 'operator' keyword for C++ operators
196 bool WithOperator = false;
197 if (Tok.is(tok::kw_operator)) {
198 P.ConsumeToken();
199 Tok = P.getCurToken();
200 WithOperator = true;
201 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000202 switch (Tok.getKind()) {
203 case tok::plus: // '+'
204 OOK = OO_Plus;
205 break;
206 case tok::minus: // '-'
207 OOK = OO_Minus;
208 break;
209 case tok::star: // '*'
210 OOK = OO_Star;
211 break;
212 case tok::amp: // '&'
213 OOK = OO_Amp;
214 break;
215 case tok::pipe: // '|'
216 OOK = OO_Pipe;
217 break;
218 case tok::caret: // '^'
219 OOK = OO_Caret;
220 break;
221 case tok::ampamp: // '&&'
222 OOK = OO_AmpAmp;
223 break;
224 case tok::pipepipe: // '||'
225 OOK = OO_PipePipe;
226 break;
227 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000228 if (!WithOperator)
229 break;
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000230 LLVM_FALLTHROUGH;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000231 default:
232 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
233 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
234 Parser::StopBeforeMatch);
235 return DeclarationName();
236 }
237 P.ConsumeToken();
238 auto &DeclNames = Actions.getASTContext().DeclarationNames;
239 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
240 : DeclNames.getCXXOperatorName(OOK);
241}
242
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000243/// Parse 'omp declare reduction' construct.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000244///
245/// declare-reduction-directive:
246/// annot_pragma_openmp 'declare' 'reduction'
247/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
248/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
249/// annot_pragma_openmp_end
250/// <reduction_id> is either a base language identifier or one of the following
251/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
252///
253Parser::DeclGroupPtrTy
254Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
255 // Parse '('.
256 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600257 if (T.expectAndConsume(
258 diag::err_expected_lparen_after,
259 getOpenMPDirectiveName(OMPD_declare_reduction).data())) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000260 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
261 return DeclGroupPtrTy();
262 }
263
264 DeclarationName Name = parseOpenMPReductionId(*this);
265 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
266 return DeclGroupPtrTy();
267
268 // Consume ':'.
269 bool IsCorrect = !ExpectAndConsume(tok::colon);
270
271 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
272 return DeclGroupPtrTy();
273
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000274 IsCorrect = IsCorrect && !Name.isEmpty();
275
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000276 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
277 Diag(Tok.getLocation(), diag::err_expected_type);
278 IsCorrect = false;
279 }
280
281 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
282 return DeclGroupPtrTy();
283
284 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
285 // Parse list of types until ':' token.
286 do {
287 ColonProtectionRAIIObject ColonRAII(*this);
288 SourceRange Range;
Faisal Vali421b2d12017-12-29 05:41:00 +0000289 TypeResult TR =
290 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000291 if (TR.isUsable()) {
Alexey Bataev61908f652018-04-23 19:53:05 +0000292 QualType ReductionType =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000293 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
294 if (!ReductionType.isNull()) {
295 ReductionTypes.push_back(
296 std::make_pair(ReductionType, Range.getBegin()));
297 }
298 } else {
299 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
300 StopBeforeMatch);
301 }
302
303 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
304 break;
305
306 // Consume ','.
307 if (ExpectAndConsume(tok::comma)) {
308 IsCorrect = false;
309 if (Tok.is(tok::annot_pragma_openmp_end)) {
310 Diag(Tok.getLocation(), diag::err_expected_type);
311 return DeclGroupPtrTy();
312 }
313 }
314 } while (Tok.isNot(tok::annot_pragma_openmp_end));
315
316 if (ReductionTypes.empty()) {
317 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
318 return DeclGroupPtrTy();
319 }
320
321 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
322 return DeclGroupPtrTy();
323
324 // Consume ':'.
325 if (ExpectAndConsume(tok::colon))
326 IsCorrect = false;
327
328 if (Tok.is(tok::annot_pragma_openmp_end)) {
329 Diag(Tok.getLocation(), diag::err_expected_expression);
330 return DeclGroupPtrTy();
331 }
332
333 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
334 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
335
336 // Parse <combiner> expression and then parse initializer if any for each
337 // correct type.
338 unsigned I = 0, E = ReductionTypes.size();
Alexey Bataev61908f652018-04-23 19:53:05 +0000339 for (Decl *D : DRD.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000340 TentativeParsingAction TPA(*this);
341 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000342 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000343 Scope::OpenMPDirectiveScope);
344 // Parse <combiner> expression.
345 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
346 ExprResult CombinerResult =
347 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000348 D->getLocation(), /*DiscardedValue*/ false);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000349 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
350
351 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
352 Tok.isNot(tok::annot_pragma_openmp_end)) {
353 TPA.Commit();
354 IsCorrect = false;
355 break;
356 }
357 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
358 ExprResult InitializerResult;
359 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
360 // Parse <initializer> expression.
361 if (Tok.is(tok::identifier) &&
Alexey Bataev61908f652018-04-23 19:53:05 +0000362 Tok.getIdentifierInfo()->isStr("initializer")) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000363 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000364 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000365 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
366 TPA.Commit();
367 IsCorrect = false;
368 break;
369 }
370 // Parse '('.
371 BalancedDelimiterTracker T(*this, tok::l_paren,
372 tok::annot_pragma_openmp_end);
373 IsCorrect =
374 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
375 IsCorrect;
376 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
377 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000378 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000379 Scope::OpenMPDirectiveScope);
380 // Parse expression.
Alexey Bataev070f43a2017-09-06 14:49:58 +0000381 VarDecl *OmpPrivParm =
382 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
383 D);
384 // Check if initializer is omp_priv <init_expr> or something else.
385 if (Tok.is(tok::identifier) &&
386 Tok.getIdentifierInfo()->isStr("omp_priv")) {
Alexey Bataev3c676e32019-11-12 11:19:26 -0500387 ConsumeToken();
388 ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000389 } else {
390 InitializerResult = Actions.ActOnFinishFullExpr(
391 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000392 /*DiscardedValue*/ false);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000393 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000394 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
Alexey Bataev070f43a2017-09-06 14:49:58 +0000395 D, InitializerResult.get(), OmpPrivParm);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000396 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
397 Tok.isNot(tok::annot_pragma_openmp_end)) {
398 TPA.Commit();
399 IsCorrect = false;
400 break;
401 }
402 IsCorrect =
403 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
404 }
405 }
406
407 ++I;
408 // Revert parsing if not the last type, otherwise accept it, we're done with
409 // parsing.
410 if (I != E)
411 TPA.Revert();
412 else
413 TPA.Commit();
414 }
415 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
416 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000417}
418
Alexey Bataev070f43a2017-09-06 14:49:58 +0000419void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
420 // Parse declarator '=' initializer.
421 // If a '==' or '+=' is found, suggest a fixit to '='.
422 if (isTokenEqualOrEqualTypo()) {
423 ConsumeToken();
424
425 if (Tok.is(tok::code_completion)) {
426 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
427 Actions.FinalizeDeclaration(OmpPrivParm);
428 cutOffParsing();
429 return;
430 }
431
Alexey Bataev3c676e32019-11-12 11:19:26 -0500432 PreferredType.enterVariableInit(Tok.getLocation(), OmpPrivParm);
433 ExprResult Init = ParseAssignmentExpression();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000434
435 if (Init.isInvalid()) {
436 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
437 Actions.ActOnInitializerError(OmpPrivParm);
438 } else {
439 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
440 /*DirectInit=*/false);
441 }
442 } else if (Tok.is(tok::l_paren)) {
443 // Parse C++ direct initializer: '(' expression-list ')'
444 BalancedDelimiterTracker T(*this, tok::l_paren);
445 T.consumeOpen();
446
447 ExprVector Exprs;
448 CommaLocsTy CommaLocs;
449
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000450 SourceLocation LParLoc = T.getOpenLocation();
Ilya Biryukovff2a9972019-02-26 11:01:50 +0000451 auto RunSignatureHelp = [this, OmpPrivParm, LParLoc, &Exprs]() {
452 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
453 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
454 OmpPrivParm->getLocation(), Exprs, LParLoc);
455 CalledSignatureHelp = true;
456 return PreferredType;
457 };
458 if (ParseExpressionList(Exprs, CommaLocs, [&] {
459 PreferredType.enterFunctionArgument(Tok.getLocation(),
460 RunSignatureHelp);
461 })) {
462 if (PP.isCodeCompletionReached() && !CalledSignatureHelp)
463 RunSignatureHelp();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000464 Actions.ActOnInitializerError(OmpPrivParm);
465 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
466 } else {
467 // Match the ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000468 SourceLocation RLoc = Tok.getLocation();
469 if (!T.consumeClose())
470 RLoc = T.getCloseLocation();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000471
472 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
473 "Unexpected number of commas!");
474
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000475 ExprResult Initializer =
476 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000477 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
478 /*DirectInit=*/true);
479 }
480 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
481 // Parse C++0x braced-init-list.
482 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
483
484 ExprResult Init(ParseBraceInitializer());
485
486 if (Init.isInvalid()) {
487 Actions.ActOnInitializerError(OmpPrivParm);
488 } else {
489 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
490 /*DirectInit=*/true);
491 }
492 } else {
493 Actions.ActOnUninitializedDecl(OmpPrivParm);
494 }
495}
496
Michael Kruse251e1482019-02-01 20:25:04 +0000497/// Parses 'omp declare mapper' directive.
498///
499/// declare-mapper-directive:
500/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifier> ':']
501/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
502/// annot_pragma_openmp_end
503/// <mapper-identifier> and <var> are base language identifiers.
504///
505Parser::DeclGroupPtrTy
506Parser::ParseOpenMPDeclareMapperDirective(AccessSpecifier AS) {
507 bool IsCorrect = true;
508 // Parse '('
509 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
510 if (T.expectAndConsume(diag::err_expected_lparen_after,
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600511 getOpenMPDirectiveName(OMPD_declare_mapper).data())) {
Michael Kruse251e1482019-02-01 20:25:04 +0000512 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
513 return DeclGroupPtrTy();
514 }
515
516 // Parse <mapper-identifier>
517 auto &DeclNames = Actions.getASTContext().DeclarationNames;
518 DeclarationName MapperId;
519 if (PP.LookAhead(0).is(tok::colon)) {
520 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
521 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
522 IsCorrect = false;
523 } else {
524 MapperId = DeclNames.getIdentifier(Tok.getIdentifierInfo());
525 }
526 ConsumeToken();
527 // Consume ':'.
528 ExpectAndConsume(tok::colon);
529 } else {
530 // If no mapper identifier is provided, its name is "default" by default
531 MapperId =
532 DeclNames.getIdentifier(&Actions.getASTContext().Idents.get("default"));
533 }
534
535 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
536 return DeclGroupPtrTy();
537
538 // Parse <type> <var>
539 DeclarationName VName;
540 QualType MapperType;
541 SourceRange Range;
542 TypeResult ParsedType = parseOpenMPDeclareMapperVarDecl(Range, VName, AS);
543 if (ParsedType.isUsable())
544 MapperType =
545 Actions.ActOnOpenMPDeclareMapperType(Range.getBegin(), ParsedType);
546 if (MapperType.isNull())
547 IsCorrect = false;
548 if (!IsCorrect) {
549 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
550 return DeclGroupPtrTy();
551 }
552
553 // Consume ')'.
554 IsCorrect &= !T.consumeClose();
555 if (!IsCorrect) {
556 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
557 return DeclGroupPtrTy();
558 }
559
560 // Enter scope.
561 OMPDeclareMapperDecl *DMD = Actions.ActOnOpenMPDeclareMapperDirectiveStart(
562 getCurScope(), Actions.getCurLexicalContext(), MapperId, MapperType,
563 Range.getBegin(), VName, AS);
564 DeclarationNameInfo DirName;
565 SourceLocation Loc = Tok.getLocation();
566 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
567 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
568 ParseScope OMPDirectiveScope(this, ScopeFlags);
569 Actions.StartOpenMPDSABlock(OMPD_declare_mapper, DirName, getCurScope(), Loc);
570
571 // Add the mapper variable declaration.
572 Actions.ActOnOpenMPDeclareMapperDirectiveVarDecl(
573 DMD, getCurScope(), MapperType, Range.getBegin(), VName);
574
575 // Parse map clauses.
576 SmallVector<OMPClause *, 6> Clauses;
577 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
578 OpenMPClauseKind CKind = Tok.isAnnotation()
579 ? OMPC_unknown
580 : getOpenMPClauseKind(PP.getSpelling(Tok));
581 Actions.StartOpenMPClause(CKind);
582 OMPClause *Clause =
583 ParseOpenMPClause(OMPD_declare_mapper, CKind, Clauses.size() == 0);
584 if (Clause)
585 Clauses.push_back(Clause);
586 else
587 IsCorrect = false;
588 // Skip ',' if any.
589 if (Tok.is(tok::comma))
590 ConsumeToken();
591 Actions.EndOpenMPClause();
592 }
593 if (Clauses.empty()) {
594 Diag(Tok, diag::err_omp_expected_clause)
595 << getOpenMPDirectiveName(OMPD_declare_mapper);
596 IsCorrect = false;
597 }
598
599 // Exit scope.
600 Actions.EndOpenMPDSABlock(nullptr);
601 OMPDirectiveScope.Exit();
602
603 DeclGroupPtrTy DGP =
604 Actions.ActOnOpenMPDeclareMapperDirectiveEnd(DMD, getCurScope(), Clauses);
605 if (!IsCorrect)
606 return DeclGroupPtrTy();
607 return DGP;
608}
609
610TypeResult Parser::parseOpenMPDeclareMapperVarDecl(SourceRange &Range,
611 DeclarationName &Name,
612 AccessSpecifier AS) {
613 // Parse the common declaration-specifiers piece.
614 Parser::DeclSpecContext DSC = Parser::DeclSpecContext::DSC_type_specifier;
615 DeclSpec DS(AttrFactory);
616 ParseSpecifierQualifierList(DS, AS, DSC);
617
618 // Parse the declarator.
619 DeclaratorContext Context = DeclaratorContext::PrototypeContext;
620 Declarator DeclaratorInfo(DS, Context);
621 ParseDeclarator(DeclaratorInfo);
622 Range = DeclaratorInfo.getSourceRange();
623 if (DeclaratorInfo.getIdentifier() == nullptr) {
624 Diag(Tok.getLocation(), diag::err_omp_mapper_expected_declarator);
625 return true;
626 }
627 Name = Actions.GetNameForDeclarator(DeclaratorInfo).getName();
628
629 return Actions.ActOnOpenMPDeclareMapperVarDecl(getCurScope(), DeclaratorInfo);
630}
631
Alexey Bataev2af33e32016-04-07 12:45:37 +0000632namespace {
633/// RAII that recreates function context for correct parsing of clauses of
634/// 'declare simd' construct.
635/// OpenMP, 2.8.2 declare simd Construct
636/// The expressions appearing in the clauses of this directive are evaluated in
637/// the scope of the arguments of the function declaration or definition.
638class FNContextRAII final {
639 Parser &P;
640 Sema::CXXThisScopeRAII *ThisScope;
641 Parser::ParseScope *TempScope;
642 Parser::ParseScope *FnScope;
643 bool HasTemplateScope = false;
644 bool HasFunScope = false;
645 FNContextRAII() = delete;
646 FNContextRAII(const FNContextRAII &) = delete;
647 FNContextRAII &operator=(const FNContextRAII &) = delete;
648
649public:
650 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
651 Decl *D = *Ptr.get().begin();
652 NamedDecl *ND = dyn_cast<NamedDecl>(D);
653 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
654 Sema &Actions = P.getActions();
655
656 // Allow 'this' within late-parsed attributes.
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000657 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, Qualifiers(),
Alexey Bataev2af33e32016-04-07 12:45:37 +0000658 ND && ND->isCXXInstanceMember());
659
660 // If the Decl is templatized, add template parameters to scope.
661 HasTemplateScope = D->isTemplateDecl();
662 TempScope =
663 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
664 if (HasTemplateScope)
665 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
666
667 // If the Decl is on a function, add function parameters to the scope.
668 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000669 FnScope = new Parser::ParseScope(
670 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
671 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000672 if (HasFunScope)
673 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
674 }
675 ~FNContextRAII() {
676 if (HasFunScope) {
677 P.getActions().ActOnExitFunctionContext();
678 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
679 }
680 if (HasTemplateScope)
681 TempScope->Exit();
682 delete FnScope;
683 delete TempScope;
684 delete ThisScope;
685 }
686};
687} // namespace
688
Alexey Bataevd93d3762016-04-12 09:35:56 +0000689/// Parses clauses for 'declare simd' directive.
690/// clause:
691/// 'inbranch' | 'notinbranch'
692/// 'simdlen' '(' <expr> ')'
693/// { 'uniform' '(' <argument_list> ')' }
694/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000695/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
696static bool parseDeclareSimdClauses(
697 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
698 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
699 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
700 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000701 SourceRange BSRange;
702 const Token &Tok = P.getCurToken();
703 bool IsError = false;
704 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
705 if (Tok.isNot(tok::identifier))
706 break;
707 OMPDeclareSimdDeclAttr::BranchStateTy Out;
708 IdentifierInfo *II = Tok.getIdentifierInfo();
709 StringRef ClauseName = II->getName();
710 // Parse 'inranch|notinbranch' clauses.
711 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
712 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
713 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
714 << ClauseName
715 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
716 IsError = true;
717 }
718 BS = Out;
719 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
720 P.ConsumeToken();
721 } else if (ClauseName.equals("simdlen")) {
722 if (SimdLen.isUsable()) {
723 P.Diag(Tok, diag::err_omp_more_one_clause)
724 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
725 IsError = true;
726 }
727 P.ConsumeToken();
728 SourceLocation RLoc;
729 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
730 if (SimdLen.isInvalid())
731 IsError = true;
732 } else {
733 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000734 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
735 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000736 Parser::OpenMPVarListDataTy Data;
Alexey Bataev61908f652018-04-23 19:53:05 +0000737 SmallVectorImpl<Expr *> *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000738 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000739 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000740 else if (CKind == OMPC_linear)
741 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000742
743 P.ConsumeToken();
744 if (P.ParseOpenMPVarList(OMPD_declare_simd,
745 getOpenMPClauseKind(ClauseName), *Vars, Data))
746 IsError = true;
Alexey Bataev61908f652018-04-23 19:53:05 +0000747 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000748 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataev61908f652018-04-23 19:53:05 +0000749 } else if (CKind == OMPC_linear) {
Alexey Bataev93dc40d2019-12-20 11:04:57 -0500750 if (P.getActions().CheckOpenMPLinearModifier(
751 static_cast<OpenMPLinearClauseKind>(Data.ExtraModifier),
752 Data.DepLinMapLastLoc))
753 Data.ExtraModifier = OMPC_LINEAR_val;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000754 LinModifiers.append(Linears.size() - LinModifiers.size(),
Alexey Bataev93dc40d2019-12-20 11:04:57 -0500755 Data.ExtraModifier);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000756 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
757 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000758 } else
759 // TODO: add parsing of other clauses.
760 break;
761 }
762 // Skip ',' if any.
763 if (Tok.is(tok::comma))
764 P.ConsumeToken();
765 }
766 return IsError;
767}
768
Alexey Bataev2af33e32016-04-07 12:45:37 +0000769/// Parse clauses for '#pragma omp declare simd'.
770Parser::DeclGroupPtrTy
771Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
772 CachedTokens &Toks, SourceLocation Loc) {
Ilya Biryukov929af672019-05-17 09:32:05 +0000773 PP.EnterToken(Tok, /*IsReinject*/ true);
774 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
775 /*IsReinject*/ true);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000776 // Consume the previously pushed token.
777 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Alexey Bataevd158cf62019-09-13 20:18:17 +0000778 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000779
780 FNContextRAII FnContext(*this, Ptr);
781 OMPDeclareSimdDeclAttr::BranchStateTy BS =
782 OMPDeclareSimdDeclAttr::BS_Undefined;
783 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000784 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000785 SmallVector<Expr *, 4> Aligneds;
786 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000787 SmallVector<Expr *, 4> Linears;
788 SmallVector<unsigned, 4> LinModifiers;
789 SmallVector<Expr *, 4> Steps;
790 bool IsError =
791 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
792 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000793 // Need to check for extra tokens.
794 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
795 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
796 << getOpenMPDirectiveName(OMPD_declare_simd);
797 while (Tok.isNot(tok::annot_pragma_openmp_end))
798 ConsumeAnyToken();
799 }
800 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000801 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000802 if (IsError)
803 return Ptr;
804 return Actions.ActOnOpenMPDeclareSimdDirective(
805 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
806 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataev20dfd772016-04-04 10:12:15 +0000807}
808
Alexey Bataeva15a1412019-10-02 18:19:02 +0000809/// Parse optional 'score' '(' <expr> ')' ':'.
810static ExprResult parseContextScore(Parser &P) {
811 ExprResult ScoreExpr;
Alexey Bataevfde11e92019-11-07 11:03:10 -0500812 Sema::OMPCtxStringType Buffer;
Alexey Bataeva15a1412019-10-02 18:19:02 +0000813 StringRef SelectorName =
814 P.getPreprocessor().getSpelling(P.getCurToken(), Buffer);
Alexey Bataevdcec2ac2019-11-05 15:33:18 -0500815 if (!SelectorName.equals("score"))
Alexey Bataeva15a1412019-10-02 18:19:02 +0000816 return ScoreExpr;
Alexey Bataeva15a1412019-10-02 18:19:02 +0000817 (void)P.ConsumeToken();
818 SourceLocation RLoc;
819 ScoreExpr = P.ParseOpenMPParensExpr(SelectorName, RLoc);
820 // Parse ':'
821 if (P.getCurToken().is(tok::colon))
822 (void)P.ConsumeAnyToken();
823 else
824 P.Diag(P.getCurToken(), diag::warn_pragma_expected_colon)
825 << "context selector score clause";
826 return ScoreExpr;
827}
828
Alexey Bataev9ff34742019-09-25 19:43:37 +0000829/// Parse context selector for 'implementation' selector set:
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000830/// 'vendor' '(' [ 'score' '(' <score _expr> ')' ':' ] <vendor> { ',' <vendor> }
831/// ')'
Alexey Bataevfde11e92019-11-07 11:03:10 -0500832static void
833parseImplementationSelector(Parser &P, SourceLocation Loc,
834 llvm::StringMap<SourceLocation> &UsedCtx,
835 SmallVectorImpl<Sema::OMPCtxSelectorData> &Data) {
Alexey Bataev9ff34742019-09-25 19:43:37 +0000836 const Token &Tok = P.getCurToken();
837 // Parse inner context selector set name, if any.
838 if (!Tok.is(tok::identifier)) {
839 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
840 << "implementation";
841 // Skip until either '}', ')', or end of directive.
842 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
843 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
844 ;
845 return;
846 }
Alexey Bataevfde11e92019-11-07 11:03:10 -0500847 Sema::OMPCtxStringType Buffer;
Alexey Bataev9ff34742019-09-25 19:43:37 +0000848 StringRef CtxSelectorName = P.getPreprocessor().getSpelling(Tok, Buffer);
Alexey Bataev70d2e542019-10-08 17:47:52 +0000849 auto Res = UsedCtx.try_emplace(CtxSelectorName, Tok.getLocation());
850 if (!Res.second) {
851 // OpenMP 5.0, 2.3.2 Context Selectors, Restrictions.
852 // Each trait-selector-name can only be specified once.
853 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_ctx_mutiple_use)
854 << CtxSelectorName << "implementation";
855 P.Diag(Res.first->getValue(), diag::note_omp_declare_variant_ctx_used_here)
856 << CtxSelectorName;
857 }
Alexey Bataevfde11e92019-11-07 11:03:10 -0500858 OpenMPContextSelectorKind CSKind = getOpenMPContextSelector(CtxSelectorName);
Alexey Bataev9ff34742019-09-25 19:43:37 +0000859 (void)P.ConsumeToken();
860 switch (CSKind) {
Alexey Bataevfde11e92019-11-07 11:03:10 -0500861 case OMP_CTX_vendor: {
Alexey Bataev9ff34742019-09-25 19:43:37 +0000862 // Parse '('.
863 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
864 (void)T.expectAndConsume(diag::err_expected_lparen_after,
865 CtxSelectorName.data());
Alexey Bataevfde11e92019-11-07 11:03:10 -0500866 ExprResult Score = parseContextScore(P);
867 llvm::UniqueVector<Sema::OMPCtxStringType> Vendors;
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000868 do {
869 // Parse <vendor>.
870 StringRef VendorName;
871 if (Tok.is(tok::identifier)) {
872 Buffer.clear();
873 VendorName = P.getPreprocessor().getSpelling(P.getCurToken(), Buffer);
874 (void)P.ConsumeToken();
Alexey Bataev303657a2019-10-08 19:44:16 +0000875 if (!VendorName.empty())
Alexey Bataev4513e93f2019-10-10 15:15:26 +0000876 Vendors.insert(VendorName);
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000877 } else {
878 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_item_expected)
879 << "vendor identifier"
880 << "vendor"
881 << "implementation";
882 }
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000883 if (!P.TryConsumeToken(tok::comma) && Tok.isNot(tok::r_paren)) {
884 P.Diag(Tok, diag::err_expected_punc)
885 << (VendorName.empty() ? "vendor name" : VendorName);
886 }
887 } while (Tok.is(tok::identifier));
Alexey Bataev9ff34742019-09-25 19:43:37 +0000888 // Parse ')'.
889 (void)T.consumeClose();
Alexey Bataevfde11e92019-11-07 11:03:10 -0500890 if (!Vendors.empty())
891 Data.emplace_back(OMP_CTX_SET_implementation, CSKind, Score, Vendors);
Alexey Bataev9ff34742019-09-25 19:43:37 +0000892 break;
893 }
Alexey Bataev4e8231b2019-11-05 15:13:30 -0500894 case OMP_CTX_kind:
Alexey Bataevfde11e92019-11-07 11:03:10 -0500895 case OMP_CTX_unknown:
Alexey Bataev9ff34742019-09-25 19:43:37 +0000896 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
897 << "implementation";
898 // Skip until either '}', ')', or end of directive.
899 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
900 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
901 ;
902 return;
903 }
Alexey Bataev9ff34742019-09-25 19:43:37 +0000904}
905
Alexey Bataev4e8231b2019-11-05 15:13:30 -0500906/// Parse context selector for 'device' selector set:
907/// 'kind' '(' <kind> { ',' <kind> } ')'
908static void
909parseDeviceSelector(Parser &P, SourceLocation Loc,
910 llvm::StringMap<SourceLocation> &UsedCtx,
911 SmallVectorImpl<Sema::OMPCtxSelectorData> &Data) {
912 const Token &Tok = P.getCurToken();
913 // Parse inner context selector set name, if any.
914 if (!Tok.is(tok::identifier)) {
915 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
916 << "device";
917 // Skip until either '}', ')', or end of directive.
918 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
919 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
920 ;
921 return;
922 }
923 Sema::OMPCtxStringType Buffer;
924 StringRef CtxSelectorName = P.getPreprocessor().getSpelling(Tok, Buffer);
925 auto Res = UsedCtx.try_emplace(CtxSelectorName, Tok.getLocation());
926 if (!Res.second) {
927 // OpenMP 5.0, 2.3.2 Context Selectors, Restrictions.
928 // Each trait-selector-name can only be specified once.
929 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_ctx_mutiple_use)
930 << CtxSelectorName << "device";
931 P.Diag(Res.first->getValue(), diag::note_omp_declare_variant_ctx_used_here)
932 << CtxSelectorName;
933 }
934 OpenMPContextSelectorKind CSKind = getOpenMPContextSelector(CtxSelectorName);
935 (void)P.ConsumeToken();
936 switch (CSKind) {
937 case OMP_CTX_kind: {
938 // Parse '('.
939 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
940 (void)T.expectAndConsume(diag::err_expected_lparen_after,
941 CtxSelectorName.data());
942 llvm::UniqueVector<Sema::OMPCtxStringType> Kinds;
943 do {
944 // Parse <kind>.
945 StringRef KindName;
946 if (Tok.is(tok::identifier)) {
947 Buffer.clear();
948 KindName = P.getPreprocessor().getSpelling(P.getCurToken(), Buffer);
949 SourceLocation SLoc = P.getCurToken().getLocation();
950 (void)P.ConsumeToken();
951 if (llvm::StringSwitch<bool>(KindName)
952 .Case("host", false)
953 .Case("nohost", false)
954 .Case("cpu", false)
955 .Case("gpu", false)
956 .Case("fpga", false)
957 .Default(true)) {
958 P.Diag(SLoc, diag::err_omp_wrong_device_kind_trait) << KindName;
959 } else {
960 Kinds.insert(KindName);
961 }
962 } else {
963 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_item_expected)
964 << "'host', 'nohost', 'cpu', 'gpu', or 'fpga'"
965 << "kind"
966 << "device";
967 }
968 if (!P.TryConsumeToken(tok::comma) && Tok.isNot(tok::r_paren)) {
969 P.Diag(Tok, diag::err_expected_punc)
970 << (KindName.empty() ? "kind of device" : KindName);
971 }
972 } while (Tok.is(tok::identifier));
973 // Parse ')'.
974 (void)T.consumeClose();
975 if (!Kinds.empty())
976 Data.emplace_back(OMP_CTX_SET_device, CSKind, ExprResult(), Kinds);
977 break;
978 }
979 case OMP_CTX_vendor:
980 case OMP_CTX_unknown:
981 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
982 << "device";
983 // Skip until either '}', ')', or end of directive.
984 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
985 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
986 ;
987 return;
988 }
989}
990
Alexey Bataevd158cf62019-09-13 20:18:17 +0000991/// Parses clauses for 'declare variant' directive.
992/// clause:
Alexey Bataevd158cf62019-09-13 20:18:17 +0000993/// <selector_set_name> '=' '{' <context_selectors> '}'
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000994/// [ ',' <selector_set_name> '=' '{' <context_selectors> '}' ]
995bool Parser::parseOpenMPContextSelectors(
Alexey Bataevfde11e92019-11-07 11:03:10 -0500996 SourceLocation Loc, SmallVectorImpl<Sema::OMPCtxSelectorData> &Data) {
Alexey Bataev5d154c32019-10-08 15:56:43 +0000997 llvm::StringMap<SourceLocation> UsedCtxSets;
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000998 do {
999 // Parse inner context selector set name.
1000 if (!Tok.is(tok::identifier)) {
1001 Diag(Tok.getLocation(), diag::err_omp_declare_variant_no_ctx_selector)
Alexey Bataevdba792c2019-09-23 18:13:31 +00001002 << getOpenMPClauseName(OMPC_match);
Alexey Bataevd158cf62019-09-13 20:18:17 +00001003 return true;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001004 }
Alexey Bataevfde11e92019-11-07 11:03:10 -05001005 Sema::OMPCtxStringType Buffer;
Alexey Bataev9ff34742019-09-25 19:43:37 +00001006 StringRef CtxSelectorSetName = PP.getSpelling(Tok, Buffer);
Alexey Bataev5d154c32019-10-08 15:56:43 +00001007 auto Res = UsedCtxSets.try_emplace(CtxSelectorSetName, Tok.getLocation());
1008 if (!Res.second) {
1009 // OpenMP 5.0, 2.3.2 Context Selectors, Restrictions.
1010 // Each trait-set-selector-name can only be specified once.
1011 Diag(Tok.getLocation(), diag::err_omp_declare_variant_ctx_set_mutiple_use)
1012 << CtxSelectorSetName;
1013 Diag(Res.first->getValue(),
1014 diag::note_omp_declare_variant_ctx_set_used_here)
1015 << CtxSelectorSetName;
1016 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001017 // Parse '='.
1018 (void)ConsumeToken();
1019 if (Tok.isNot(tok::equal)) {
1020 Diag(Tok.getLocation(), diag::err_omp_declare_variant_equal_expected)
Alexey Bataev9ff34742019-09-25 19:43:37 +00001021 << CtxSelectorSetName;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001022 return true;
1023 }
1024 (void)ConsumeToken();
1025 // TBD: add parsing of known context selectors.
1026 // Unknown selector - just ignore it completely.
1027 {
1028 // Parse '{'.
1029 BalancedDelimiterTracker TBr(*this, tok::l_brace,
1030 tok::annot_pragma_openmp_end);
1031 if (TBr.expectAndConsume(diag::err_expected_lbrace_after, "="))
1032 return true;
Alexey Bataevfde11e92019-11-07 11:03:10 -05001033 OpenMPContextSelectorSetKind CSSKind =
1034 getOpenMPContextSelectorSet(CtxSelectorSetName);
Alexey Bataev70d2e542019-10-08 17:47:52 +00001035 llvm::StringMap<SourceLocation> UsedCtx;
1036 do {
1037 switch (CSSKind) {
Alexey Bataevfde11e92019-11-07 11:03:10 -05001038 case OMP_CTX_SET_implementation:
1039 parseImplementationSelector(*this, Loc, UsedCtx, Data);
Alexey Bataev70d2e542019-10-08 17:47:52 +00001040 break;
Alexey Bataev4e8231b2019-11-05 15:13:30 -05001041 case OMP_CTX_SET_device:
1042 parseDeviceSelector(*this, Loc, UsedCtx, Data);
1043 break;
Alexey Bataevfde11e92019-11-07 11:03:10 -05001044 case OMP_CTX_SET_unknown:
Alexey Bataev70d2e542019-10-08 17:47:52 +00001045 // Skip until either '}', ')', or end of directive.
1046 while (!SkipUntil(tok::r_brace, tok::r_paren,
1047 tok::annot_pragma_openmp_end, StopBeforeMatch))
1048 ;
1049 break;
1050 }
1051 const Token PrevTok = Tok;
1052 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
1053 Diag(Tok, diag::err_omp_expected_comma_brace)
1054 << (PrevTok.isAnnotation() ? "context selector trait"
1055 : PP.getSpelling(PrevTok));
1056 } while (Tok.is(tok::identifier));
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001057 // Parse '}'.
1058 (void)TBr.consumeClose();
1059 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001060 // Consume ','
1061 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end))
1062 (void)ExpectAndConsume(tok::comma);
1063 } while (Tok.isAnyIdentifier());
Alexey Bataevd158cf62019-09-13 20:18:17 +00001064 return false;
1065}
1066
1067/// Parse clauses for '#pragma omp declare variant ( variant-func-id ) clause'.
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001068void Parser::ParseOMPDeclareVariantClauses(Parser::DeclGroupPtrTy Ptr,
1069 CachedTokens &Toks,
1070 SourceLocation Loc) {
Alexey Bataevd158cf62019-09-13 20:18:17 +00001071 PP.EnterToken(Tok, /*IsReinject*/ true);
1072 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
1073 /*IsReinject*/ true);
1074 // Consume the previously pushed token.
1075 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
1076 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
1077
1078 FNContextRAII FnContext(*this, Ptr);
1079 // Parse function declaration id.
1080 SourceLocation RLoc;
1081 // Parse with IsAddressOfOperand set to true to parse methods as DeclRefExprs
1082 // instead of MemberExprs.
Alexey Bataev02d04d52019-12-10 16:12:53 -05001083 ExprResult AssociatedFunction;
1084 {
1085 // Do not mark function as is used to prevent its emission if this is the
1086 // only place where it is used.
1087 EnterExpressionEvaluationContext Unevaluated(
1088 Actions, Sema::ExpressionEvaluationContext::Unevaluated);
1089 AssociatedFunction = ParseOpenMPParensExpr(
1090 getOpenMPDirectiveName(OMPD_declare_variant), RLoc,
1091 /*IsAddressOfOperand=*/true);
1092 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001093 if (!AssociatedFunction.isUsable()) {
1094 if (!Tok.is(tok::annot_pragma_openmp_end))
1095 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
1096 ;
1097 // Skip the last annot_pragma_openmp_end.
1098 (void)ConsumeAnnotationToken();
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001099 return;
1100 }
1101 Optional<std::pair<FunctionDecl *, Expr *>> DeclVarData =
1102 Actions.checkOpenMPDeclareVariantFunction(
1103 Ptr, AssociatedFunction.get(), SourceRange(Loc, Tok.getLocation()));
1104
1105 // Parse 'match'.
Alexey Bataevdba792c2019-09-23 18:13:31 +00001106 OpenMPClauseKind CKind = Tok.isAnnotation()
1107 ? OMPC_unknown
1108 : getOpenMPClauseKind(PP.getSpelling(Tok));
1109 if (CKind != OMPC_match) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001110 Diag(Tok.getLocation(), diag::err_omp_declare_variant_wrong_clause)
Alexey Bataevdba792c2019-09-23 18:13:31 +00001111 << getOpenMPClauseName(OMPC_match);
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001112 while (!SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
1113 ;
1114 // Skip the last annot_pragma_openmp_end.
1115 (void)ConsumeAnnotationToken();
1116 return;
1117 }
1118 (void)ConsumeToken();
1119 // Parse '('.
1120 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataevdba792c2019-09-23 18:13:31 +00001121 if (T.expectAndConsume(diag::err_expected_lparen_after,
1122 getOpenMPClauseName(OMPC_match))) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001123 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
1124 ;
1125 // Skip the last annot_pragma_openmp_end.
1126 (void)ConsumeAnnotationToken();
1127 return;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001128 }
1129
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001130 // Parse inner context selectors.
Alexey Bataevfde11e92019-11-07 11:03:10 -05001131 SmallVector<Sema::OMPCtxSelectorData, 4> Data;
1132 if (!parseOpenMPContextSelectors(Loc, Data)) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001133 // Parse ')'.
1134 (void)T.consumeClose();
1135 // Need to check for extra tokens.
1136 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1137 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1138 << getOpenMPDirectiveName(OMPD_declare_variant);
1139 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001140 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001141
1142 // Skip last tokens.
1143 while (Tok.isNot(tok::annot_pragma_openmp_end))
1144 ConsumeAnyToken();
Alexey Bataevfde11e92019-11-07 11:03:10 -05001145 if (DeclVarData.hasValue())
1146 Actions.ActOnOpenMPDeclareVariantDirective(
1147 DeclVarData.getValue().first, DeclVarData.getValue().second,
1148 SourceRange(Loc, Tok.getLocation()), Data);
Alexey Bataevd158cf62019-09-13 20:18:17 +00001149 // Skip the last annot_pragma_openmp_end.
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001150 (void)ConsumeAnnotationToken();
Alexey Bataevd158cf62019-09-13 20:18:17 +00001151}
1152
Alexey Bataev729e2422019-08-23 16:11:14 +00001153/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
1154///
1155/// default-clause:
1156/// 'default' '(' 'none' | 'shared' ')
1157///
1158/// proc_bind-clause:
1159/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1160///
1161/// device_type-clause:
1162/// 'device_type' '(' 'host' | 'nohost' | 'any' )'
1163namespace {
1164 struct SimpleClauseData {
1165 unsigned Type;
1166 SourceLocation Loc;
1167 SourceLocation LOpen;
1168 SourceLocation TypeLoc;
1169 SourceLocation RLoc;
1170 SimpleClauseData(unsigned Type, SourceLocation Loc, SourceLocation LOpen,
1171 SourceLocation TypeLoc, SourceLocation RLoc)
1172 : Type(Type), Loc(Loc), LOpen(LOpen), TypeLoc(TypeLoc), RLoc(RLoc) {}
1173 };
1174} // anonymous namespace
1175
1176static Optional<SimpleClauseData>
1177parseOpenMPSimpleClause(Parser &P, OpenMPClauseKind Kind) {
1178 const Token &Tok = P.getCurToken();
1179 SourceLocation Loc = Tok.getLocation();
1180 SourceLocation LOpen = P.ConsumeToken();
1181 // Parse '('.
1182 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
1183 if (T.expectAndConsume(diag::err_expected_lparen_after,
1184 getOpenMPClauseName(Kind)))
1185 return llvm::None;
1186
1187 unsigned Type = getOpenMPSimpleClauseType(
1188 Kind, Tok.isAnnotation() ? "" : P.getPreprocessor().getSpelling(Tok));
1189 SourceLocation TypeLoc = Tok.getLocation();
1190 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1191 Tok.isNot(tok::annot_pragma_openmp_end))
1192 P.ConsumeAnyToken();
1193
1194 // Parse ')'.
1195 SourceLocation RLoc = Tok.getLocation();
1196 if (!T.consumeClose())
1197 RLoc = T.getCloseLocation();
1198
1199 return SimpleClauseData(Type, Loc, LOpen, TypeLoc, RLoc);
1200}
1201
Kelvin Lie0502752018-11-21 20:15:57 +00001202Parser::DeclGroupPtrTy Parser::ParseOMPDeclareTargetClauses() {
1203 // OpenMP 4.5 syntax with list of entities.
1204 Sema::NamedDeclSetType SameDirectiveDecls;
Alexey Bataev729e2422019-08-23 16:11:14 +00001205 SmallVector<std::tuple<OMPDeclareTargetDeclAttr::MapTypeTy, SourceLocation,
1206 NamedDecl *>,
1207 4>
1208 DeclareTargetDecls;
1209 OMPDeclareTargetDeclAttr::DevTypeTy DT = OMPDeclareTargetDeclAttr::DT_Any;
1210 SourceLocation DeviceTypeLoc;
Kelvin Lie0502752018-11-21 20:15:57 +00001211 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1212 OMPDeclareTargetDeclAttr::MapTypeTy MT = OMPDeclareTargetDeclAttr::MT_To;
1213 if (Tok.is(tok::identifier)) {
1214 IdentifierInfo *II = Tok.getIdentifierInfo();
1215 StringRef ClauseName = II->getName();
Alexey Bataev729e2422019-08-23 16:11:14 +00001216 bool IsDeviceTypeClause =
1217 getLangOpts().OpenMP >= 50 &&
1218 getOpenMPClauseKind(ClauseName) == OMPC_device_type;
1219 // Parse 'to|link|device_type' clauses.
1220 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName, MT) &&
1221 !IsDeviceTypeClause) {
1222 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
1223 << ClauseName << (getLangOpts().OpenMP >= 50 ? 1 : 0);
Kelvin Lie0502752018-11-21 20:15:57 +00001224 break;
1225 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001226 // Parse 'device_type' clause and go to next clause if any.
1227 if (IsDeviceTypeClause) {
1228 Optional<SimpleClauseData> DevTypeData =
1229 parseOpenMPSimpleClause(*this, OMPC_device_type);
1230 if (DevTypeData.hasValue()) {
1231 if (DeviceTypeLoc.isValid()) {
1232 // We already saw another device_type clause, diagnose it.
1233 Diag(DevTypeData.getValue().Loc,
1234 diag::warn_omp_more_one_device_type_clause);
1235 }
1236 switch(static_cast<OpenMPDeviceType>(DevTypeData.getValue().Type)) {
1237 case OMPC_DEVICE_TYPE_any:
1238 DT = OMPDeclareTargetDeclAttr::DT_Any;
1239 break;
1240 case OMPC_DEVICE_TYPE_host:
1241 DT = OMPDeclareTargetDeclAttr::DT_Host;
1242 break;
1243 case OMPC_DEVICE_TYPE_nohost:
1244 DT = OMPDeclareTargetDeclAttr::DT_NoHost;
1245 break;
1246 case OMPC_DEVICE_TYPE_unknown:
1247 llvm_unreachable("Unexpected device_type");
1248 }
1249 DeviceTypeLoc = DevTypeData.getValue().Loc;
1250 }
1251 continue;
1252 }
Kelvin Lie0502752018-11-21 20:15:57 +00001253 ConsumeToken();
1254 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001255 auto &&Callback = [this, MT, &DeclareTargetDecls, &SameDirectiveDecls](
1256 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
1257 NamedDecl *ND = Actions.lookupOpenMPDeclareTargetName(
1258 getCurScope(), SS, NameInfo, SameDirectiveDecls);
1259 if (ND)
1260 DeclareTargetDecls.emplace_back(MT, NameInfo.getLoc(), ND);
Kelvin Lie0502752018-11-21 20:15:57 +00001261 };
1262 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
1263 /*AllowScopeSpecifier=*/true))
1264 break;
1265
1266 // Consume optional ','.
1267 if (Tok.is(tok::comma))
1268 ConsumeToken();
1269 }
1270 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1271 ConsumeAnyToken();
Alexey Bataev729e2422019-08-23 16:11:14 +00001272 for (auto &MTLocDecl : DeclareTargetDecls) {
1273 OMPDeclareTargetDeclAttr::MapTypeTy MT;
1274 SourceLocation Loc;
1275 NamedDecl *ND;
1276 std::tie(MT, Loc, ND) = MTLocDecl;
1277 // device_type clause is applied only to functions.
1278 Actions.ActOnOpenMPDeclareTargetName(
1279 ND, Loc, MT, isa<VarDecl>(ND) ? OMPDeclareTargetDeclAttr::DT_Any : DT);
1280 }
Kelvin Lie0502752018-11-21 20:15:57 +00001281 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
1282 SameDirectiveDecls.end());
1283 if (Decls.empty())
1284 return DeclGroupPtrTy();
1285 return Actions.BuildDeclaratorGroup(Decls);
1286}
1287
1288void Parser::ParseOMPEndDeclareTargetDirective(OpenMPDirectiveKind DKind,
1289 SourceLocation DTLoc) {
1290 if (DKind != OMPD_end_declare_target) {
1291 Diag(Tok, diag::err_expected_end_declare_target);
1292 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
1293 return;
1294 }
1295 ConsumeAnyToken();
1296 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1297 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1298 << getOpenMPDirectiveName(OMPD_end_declare_target);
1299 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1300 }
1301 // Skip the last annot_pragma_openmp_end.
1302 ConsumeAnyToken();
1303}
1304
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001305/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001306///
1307/// threadprivate-directive:
1308/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001309/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +00001310///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001311/// allocate-directive:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001312/// annot_pragma_openmp 'allocate' simple-variable-list [<clause>]
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001313/// annot_pragma_openmp_end
1314///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001315/// declare-reduction-directive:
1316/// annot_pragma_openmp 'declare' 'reduction' [...]
1317/// annot_pragma_openmp_end
1318///
Michael Kruse251e1482019-02-01 20:25:04 +00001319/// declare-mapper-directive:
1320/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1321/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1322/// annot_pragma_openmp_end
1323///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001324/// declare-simd-directive:
1325/// annot_pragma_openmp 'declare simd' {<clause> [,]}
1326/// annot_pragma_openmp_end
1327/// <function declaration/definition>
1328///
Kelvin Li1408f912018-09-26 04:28:39 +00001329/// requires directive:
1330/// annot_pragma_openmp 'requires' <clause> [[[,] <clause>] ... ]
1331/// annot_pragma_openmp_end
1332///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001333Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
1334 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
1335 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001336 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataev8035bb42019-12-13 16:05:30 -05001337 ParsingOpenMPDirectiveRAII DirScope(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001338 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +00001339
Richard Smithaf3b3252017-05-18 19:21:48 +00001340 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001341 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001342
1343 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001344 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +00001345 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001346 DeclDirectiveListParserHelper Helper(this, DKind);
1347 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1348 /*AllowScopeSpecifier=*/true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001349 // The last seen token is annot_pragma_openmp_end - need to check for
1350 // extra tokens.
1351 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1352 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001353 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001354 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +00001355 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001356 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001357 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001358 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
1359 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +00001360 }
1361 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001362 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001363 case OMPD_allocate: {
1364 ConsumeToken();
1365 DeclDirectiveListParserHelper Helper(this, DKind);
1366 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1367 /*AllowScopeSpecifier=*/true)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001368 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001369 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001370 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1371 OMPC_unknown + 1>
1372 FirstClauses(OMPC_unknown + 1);
1373 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1374 OpenMPClauseKind CKind =
1375 Tok.isAnnotation() ? OMPC_unknown
1376 : getOpenMPClauseKind(PP.getSpelling(Tok));
1377 Actions.StartOpenMPClause(CKind);
1378 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1379 !FirstClauses[CKind].getInt());
1380 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1381 StopBeforeMatch);
1382 FirstClauses[CKind].setInt(true);
1383 if (Clause != nullptr)
1384 Clauses.push_back(Clause);
1385 if (Tok.is(tok::annot_pragma_openmp_end)) {
1386 Actions.EndOpenMPClause();
1387 break;
1388 }
1389 // Skip ',' if any.
1390 if (Tok.is(tok::comma))
1391 ConsumeToken();
1392 Actions.EndOpenMPClause();
1393 }
1394 // The last seen token is annot_pragma_openmp_end - need to check for
1395 // extra tokens.
1396 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1397 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1398 << getOpenMPDirectiveName(DKind);
1399 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1400 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001401 }
1402 // Skip the last annot_pragma_openmp_end.
1403 ConsumeAnnotationToken();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001404 return Actions.ActOnOpenMPAllocateDirective(Loc, Helper.getIdentifiers(),
1405 Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001406 }
1407 break;
1408 }
Kelvin Li1408f912018-09-26 04:28:39 +00001409 case OMPD_requires: {
1410 SourceLocation StartLoc = ConsumeToken();
1411 SmallVector<OMPClause *, 5> Clauses;
1412 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
1413 FirstClauses(OMPC_unknown + 1);
1414 if (Tok.is(tok::annot_pragma_openmp_end)) {
Ilya Biryukovff2a9972019-02-26 11:01:50 +00001415 Diag(Tok, diag::err_omp_expected_clause)
Kelvin Li1408f912018-09-26 04:28:39 +00001416 << getOpenMPDirectiveName(OMPD_requires);
1417 break;
1418 }
1419 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1420 OpenMPClauseKind CKind = Tok.isAnnotation()
1421 ? OMPC_unknown
1422 : getOpenMPClauseKind(PP.getSpelling(Tok));
1423 Actions.StartOpenMPClause(CKind);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001424 OMPClause *Clause = ParseOpenMPClause(OMPD_requires, CKind,
1425 !FirstClauses[CKind].getInt());
1426 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1427 StopBeforeMatch);
Kelvin Li1408f912018-09-26 04:28:39 +00001428 FirstClauses[CKind].setInt(true);
1429 if (Clause != nullptr)
1430 Clauses.push_back(Clause);
1431 if (Tok.is(tok::annot_pragma_openmp_end)) {
1432 Actions.EndOpenMPClause();
1433 break;
1434 }
1435 // Skip ',' if any.
1436 if (Tok.is(tok::comma))
1437 ConsumeToken();
1438 Actions.EndOpenMPClause();
1439 }
1440 // Consume final annot_pragma_openmp_end
1441 if (Clauses.size() == 0) {
1442 Diag(Tok, diag::err_omp_expected_clause)
1443 << getOpenMPDirectiveName(OMPD_requires);
1444 ConsumeAnnotationToken();
1445 return nullptr;
1446 }
1447 ConsumeAnnotationToken();
1448 return Actions.ActOnOpenMPRequiresDirective(StartLoc, Clauses);
1449 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001450 case OMPD_declare_reduction:
1451 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001452 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001453 // The last seen token is annot_pragma_openmp_end - need to check for
1454 // extra tokens.
1455 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1456 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1457 << getOpenMPDirectiveName(OMPD_declare_reduction);
1458 while (Tok.isNot(tok::annot_pragma_openmp_end))
1459 ConsumeAnyToken();
1460 }
1461 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001462 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001463 return Res;
1464 }
1465 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001466 case OMPD_declare_mapper: {
1467 ConsumeToken();
1468 if (DeclGroupPtrTy Res = ParseOpenMPDeclareMapperDirective(AS)) {
1469 // Skip the last annot_pragma_openmp_end.
1470 ConsumeAnnotationToken();
1471 return Res;
1472 }
1473 break;
1474 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001475 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001476 case OMPD_declare_simd: {
1477 // The syntax is:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001478 // { #pragma omp declare {simd|variant} }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001479 // <function-declaration-or-definition>
1480 //
Alexey Bataev2af33e32016-04-07 12:45:37 +00001481 CachedTokens Toks;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001482 Toks.push_back(Tok);
1483 ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001484 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
1485 Toks.push_back(Tok);
1486 ConsumeAnyToken();
1487 }
1488 Toks.push_back(Tok);
1489 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +00001490
1491 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +00001492 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001493 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +00001494 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001495 // Here we expect to see some function declaration.
1496 if (AS == AS_none) {
1497 assert(TagType == DeclSpec::TST_unspecified);
1498 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001499 ParsingDeclSpec PDS(*this);
1500 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1501 } else {
1502 Ptr =
1503 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1504 }
1505 }
1506 if (!Ptr) {
Alexey Bataevd158cf62019-09-13 20:18:17 +00001507 Diag(Loc, diag::err_omp_decl_in_declare_simd_variant)
1508 << (DKind == OMPD_declare_simd ? 0 : 1);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001509 return DeclGroupPtrTy();
1510 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001511 if (DKind == OMPD_declare_simd)
1512 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
1513 assert(DKind == OMPD_declare_variant &&
1514 "Expected declare variant directive only");
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001515 ParseOMPDeclareVariantClauses(Ptr, Toks, Loc);
1516 return Ptr;
Alexey Bataev587e1de2016-03-30 10:43:55 +00001517 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001518 case OMPD_declare_target: {
1519 SourceLocation DTLoc = ConsumeAnyToken();
1520 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Kelvin Lie0502752018-11-21 20:15:57 +00001521 return ParseOMPDeclareTargetClauses();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001522 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001523
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001524 // Skip the last annot_pragma_openmp_end.
1525 ConsumeAnyToken();
1526
1527 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
1528 return DeclGroupPtrTy();
1529
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001530 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +00001531 DKind = parseOpenMPDirectiveKind(*this);
Kelvin Libc38e632018-09-10 02:07:09 +00001532 while (DKind != OMPD_end_declare_target && Tok.isNot(tok::eof) &&
1533 Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +00001534 DeclGroupPtrTy Ptr;
1535 // Here we expect to see some function declaration.
1536 if (AS == AS_none) {
1537 assert(TagType == DeclSpec::TST_unspecified);
1538 MaybeParseCXX11Attributes(Attrs);
1539 ParsingDeclSpec PDS(*this);
1540 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1541 } else {
1542 Ptr =
1543 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1544 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001545 if (Ptr) {
1546 DeclGroupRef Ref = Ptr.get();
1547 Decls.append(Ref.begin(), Ref.end());
1548 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001549 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
1550 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +00001551 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001552 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001553 if (DKind != OMPD_end_declare_target)
1554 TPA.Revert();
1555 else
1556 TPA.Commit();
1557 }
1558 }
1559
Kelvin Lie0502752018-11-21 20:15:57 +00001560 ParseOMPEndDeclareTargetDirective(DKind, DTLoc);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001561 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +00001562 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001563 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001564 case OMPD_unknown:
1565 Diag(Tok, diag::err_omp_unknown_directive);
1566 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001567 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001568 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001569 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +00001570 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001571 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001572 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001573 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +00001574 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001575 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001576 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001577 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001578 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001579 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +00001580 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001581 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001582 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001583 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001584 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001585 case OMPD_parallel_sections:
cchen47d60942019-12-05 13:43:48 -05001586 case OMPD_parallel_master:
Alexey Bataev0162e452014-07-22 10:10:35 +00001587 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001588 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001589 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001590 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001591 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +00001592 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001593 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001594 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001595 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001596 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001597 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001598 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +00001599 case OMPD_master_taskloop:
Alexey Bataevb8552ab2019-10-18 16:47:35 +00001600 case OMPD_master_taskloop_simd:
Alexey Bataev5bbcead2019-10-14 17:17:41 +00001601 case OMPD_parallel_master_taskloop:
Alexey Bataev14a388f2019-10-25 10:27:13 -04001602 case OMPD_parallel_master_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001603 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001604 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001605 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001606 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +00001607 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001608 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001609 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001610 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001611 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +00001612 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +00001613 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001614 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +00001615 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +00001616 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +00001617 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +00001618 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +00001619 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +00001620 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001621 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +00001622 break;
1623 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001624 while (Tok.isNot(tok::annot_pragma_openmp_end))
1625 ConsumeAnyToken();
1626 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +00001627 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001628}
1629
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001630/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001631///
1632/// threadprivate-directive:
1633/// annot_pragma_openmp 'threadprivate' simple-variable-list
1634/// annot_pragma_openmp_end
1635///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001636/// allocate-directive:
1637/// annot_pragma_openmp 'allocate' simple-variable-list
1638/// annot_pragma_openmp_end
1639///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001640/// declare-reduction-directive:
1641/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
1642/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
1643/// ('omp_priv' '=' <expression>|<function_call>) ')']
1644/// annot_pragma_openmp_end
1645///
Michael Kruse251e1482019-02-01 20:25:04 +00001646/// declare-mapper-directive:
1647/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1648/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1649/// annot_pragma_openmp_end
1650///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001651/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001652/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001653/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
cchen47d60942019-12-05 13:43:48 -05001654/// 'parallel for' | 'parallel sections' | 'parallel master' | 'task' |
1655/// 'taskyield' | 'barrier' | 'taskwait' | 'flush' | 'ordered' |
1656/// 'atomic' | 'for simd' | 'parallel for simd' | 'target' | 'target
1657/// data' | 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
1658/// 'master taskloop' | 'master taskloop simd' | 'parallel master
1659/// taskloop' | 'parallel master taskloop simd' | 'distribute' | 'target
1660/// enter data' | 'target exit data' | 'target parallel' | 'target
1661/// parallel for' | 'target update' | 'distribute parallel for' |
1662/// 'distribute paralle for simd' | 'distribute simd' | 'target parallel
1663/// for simd' | 'target simd' | 'teams distribute' | 'teams distribute
1664/// simd' | 'teams distribute parallel for simd' | 'teams distribute
1665/// parallel for' | 'target teams' | 'target teams distribute' | 'target
1666/// teams distribute parallel for' | 'target teams distribute parallel
1667/// for simd' | 'target teams distribute simd' {clause}
Alexey Bataev14a388f2019-10-25 10:27:13 -04001668/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001669///
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001670StmtResult
1671Parser::ParseOpenMPDeclarativeOrExecutableDirective(ParsedStmtContext StmtCtx) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001672 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataev8035bb42019-12-13 16:05:30 -05001673 ParsingOpenMPDirectiveRAII DirScope(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001674 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001675 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001676 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +00001677 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +00001678 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
1679 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +00001680 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +00001681 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001682 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001683 // Name of critical directive.
1684 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001685 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +00001686 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +00001687 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001688
1689 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001690 case OMPD_threadprivate: {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001691 // FIXME: Should this be permitted in C++?
1692 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1693 ParsedStmtContext()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001694 Diag(Tok, diag::err_omp_immediate_directive)
1695 << getOpenMPDirectiveName(DKind) << 0;
1696 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001697 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001698 DeclDirectiveListParserHelper Helper(this, DKind);
1699 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1700 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001701 // The last seen token is annot_pragma_openmp_end - need to check for
1702 // extra tokens.
1703 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1704 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001705 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001706 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001707 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001708 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
1709 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001710 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1711 }
Alp Tokerd751fa72013-12-18 19:10:49 +00001712 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001713 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001714 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001715 case OMPD_allocate: {
1716 // FIXME: Should this be permitted in C++?
1717 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1718 ParsedStmtContext()) {
1719 Diag(Tok, diag::err_omp_immediate_directive)
1720 << getOpenMPDirectiveName(DKind) << 0;
1721 }
1722 ConsumeToken();
1723 DeclDirectiveListParserHelper Helper(this, DKind);
1724 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1725 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001726 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001727 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001728 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1729 OMPC_unknown + 1>
1730 FirstClauses(OMPC_unknown + 1);
1731 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1732 OpenMPClauseKind CKind =
1733 Tok.isAnnotation() ? OMPC_unknown
1734 : getOpenMPClauseKind(PP.getSpelling(Tok));
1735 Actions.StartOpenMPClause(CKind);
1736 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1737 !FirstClauses[CKind].getInt());
1738 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1739 StopBeforeMatch);
1740 FirstClauses[CKind].setInt(true);
1741 if (Clause != nullptr)
1742 Clauses.push_back(Clause);
1743 if (Tok.is(tok::annot_pragma_openmp_end)) {
1744 Actions.EndOpenMPClause();
1745 break;
1746 }
1747 // Skip ',' if any.
1748 if (Tok.is(tok::comma))
1749 ConsumeToken();
1750 Actions.EndOpenMPClause();
1751 }
1752 // The last seen token is annot_pragma_openmp_end - need to check for
1753 // extra tokens.
1754 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1755 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1756 << getOpenMPDirectiveName(DKind);
1757 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1758 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001759 }
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001760 DeclGroupPtrTy Res = Actions.ActOnOpenMPAllocateDirective(
1761 Loc, Helper.getIdentifiers(), Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001762 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1763 }
1764 SkipUntil(tok::annot_pragma_openmp_end);
1765 break;
1766 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001767 case OMPD_declare_reduction:
1768 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001769 if (DeclGroupPtrTy Res =
1770 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001771 // The last seen token is annot_pragma_openmp_end - need to check for
1772 // extra tokens.
1773 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1774 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1775 << getOpenMPDirectiveName(OMPD_declare_reduction);
1776 while (Tok.isNot(tok::annot_pragma_openmp_end))
1777 ConsumeAnyToken();
1778 }
1779 ConsumeAnyToken();
1780 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +00001781 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001782 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +00001783 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001784 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001785 case OMPD_declare_mapper: {
1786 ConsumeToken();
1787 if (DeclGroupPtrTy Res =
1788 ParseOpenMPDeclareMapperDirective(/*AS=*/AS_none)) {
1789 // Skip the last annot_pragma_openmp_end.
1790 ConsumeAnnotationToken();
1791 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1792 } else {
1793 SkipUntil(tok::annot_pragma_openmp_end);
1794 }
1795 break;
1796 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001797 case OMPD_flush:
1798 if (PP.LookAhead(0).is(tok::l_paren)) {
1799 FlushHasClause = true;
1800 // Push copy of the current token back to stream to properly parse
1801 // pseudo-clause OMPFlushClause.
Ilya Biryukov929af672019-05-17 09:32:05 +00001802 PP.EnterToken(Tok, /*IsReinject*/ true);
Alexey Bataev6125da92014-07-21 11:26:11 +00001803 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001804 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +00001805 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001806 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001807 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001808 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001809 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001810 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001811 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +00001812 case OMPD_target_update:
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001813 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1814 ParsedStmtContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00001815 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +00001816 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +00001817 }
1818 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001819 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001820 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001821 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001822 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001823 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001824 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001825 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001826 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001827 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001828 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001829 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001830 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001831 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001832 case OMPD_parallel_sections:
cchen47d60942019-12-05 13:43:48 -05001833 case OMPD_parallel_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001834 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001835 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001836 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001837 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001838 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001839 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001840 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001841 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001842 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001843 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001844 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +00001845 case OMPD_master_taskloop:
Alexey Bataevb8552ab2019-10-18 16:47:35 +00001846 case OMPD_master_taskloop_simd:
Alexey Bataev5bbcead2019-10-14 17:17:41 +00001847 case OMPD_parallel_master_taskloop:
Alexey Bataev14a388f2019-10-25 10:27:13 -04001848 case OMPD_parallel_master_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001849 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001850 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001851 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001852 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001853 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001854 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001855 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001856 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001857 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001858 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001859 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001860 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001861 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001862 case OMPD_target_teams_distribute_parallel_for_simd:
1863 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001864 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001865 // Parse directive name of the 'critical' directive if any.
1866 if (DKind == OMPD_critical) {
1867 BalancedDelimiterTracker T(*this, tok::l_paren,
1868 tok::annot_pragma_openmp_end);
1869 if (!T.consumeOpen()) {
1870 if (Tok.isAnyIdentifier()) {
1871 DirName =
1872 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1873 ConsumeAnyToken();
1874 } else {
1875 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1876 }
1877 T.consumeClose();
1878 }
Alexey Bataev80909872015-07-02 11:25:17 +00001879 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001880 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001881 if (Tok.isNot(tok::annot_pragma_openmp_end))
1882 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001883 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001884
Alexey Bataevf29276e2014-06-18 04:14:57 +00001885 if (isOpenMPLoopDirective(DKind))
1886 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1887 if (isOpenMPSimdDirective(DKind))
1888 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1889 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001890 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001891
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001892 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001893 OpenMPClauseKind CKind =
1894 Tok.isAnnotation()
1895 ? OMPC_unknown
1896 : FlushHasClause ? OMPC_flush
1897 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001898 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001899 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001900 OMPClause *Clause =
1901 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001902 FirstClauses[CKind].setInt(true);
1903 if (Clause) {
1904 FirstClauses[CKind].setPointer(Clause);
1905 Clauses.push_back(Clause);
1906 }
1907
1908 // Skip ',' if any.
1909 if (Tok.is(tok::comma))
1910 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001911 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001912 }
1913 // End location of the directive.
1914 EndLoc = Tok.getLocation();
1915 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001916 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001917
Alexey Bataeveb482352015-12-18 05:05:56 +00001918 // OpenMP [2.13.8, ordered Construct, Syntax]
1919 // If the depend clause is specified, the ordered construct is a stand-alone
1920 // directive.
1921 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001922 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1923 ParsedStmtContext()) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001924 Diag(Loc, diag::err_omp_immediate_directive)
1925 << getOpenMPDirectiveName(DKind) << 1
1926 << getOpenMPClauseName(OMPC_depend);
1927 }
1928 HasAssociatedStatement = false;
1929 }
1930
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001931 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001932 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001933 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001934 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001935 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1936 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1937 // should have at least one compound statement scope within it.
1938 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001939 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001940 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1941 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001942 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001943 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1944 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1945 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001946 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001947 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001948 Directive = Actions.ActOnOpenMPExecutableDirective(
1949 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1950 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001951
1952 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001953 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001954 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001955 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001956 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001957 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001958 case OMPD_declare_target:
1959 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00001960 case OMPD_requires:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001961 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001962 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001963 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001964 SkipUntil(tok::annot_pragma_openmp_end);
1965 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001966 case OMPD_unknown:
1967 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001968 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001969 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001970 }
1971 return Directive;
1972}
1973
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001974// Parses simple list:
1975// simple-variable-list:
1976// '(' id-expression {, id-expression} ')'
1977//
1978bool Parser::ParseOpenMPSimpleVarList(
1979 OpenMPDirectiveKind Kind,
1980 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1981 Callback,
1982 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001983 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001984 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001985 if (T.expectAndConsume(diag::err_expected_lparen_after,
Johannes Doerferteb3e81f2019-11-04 22:00:49 -06001986 getOpenMPDirectiveName(Kind).data()))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001987 return true;
1988 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001989 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001990
1991 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001992 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001993 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001994 UnqualifiedId Name;
1995 // Read var name.
1996 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001997 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001998
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001999 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00002000 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002001 IsCorrect = false;
2002 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00002003 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00002004 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00002005 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002006 IsCorrect = false;
2007 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00002008 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002009 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
2010 Tok.isNot(tok::annot_pragma_openmp_end)) {
2011 IsCorrect = false;
2012 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00002013 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00002014 Diag(PrevTok.getLocation(), diag::err_expected)
2015 << tok::identifier
2016 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00002017 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002018 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00002019 }
2020 // Consume ','.
2021 if (Tok.is(tok::comma)) {
2022 ConsumeToken();
2023 }
Alexey Bataeva769e072013-03-22 06:34:35 +00002024 }
2025
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002026 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00002027 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002028 IsCorrect = false;
2029 }
2030
2031 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002032 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002033
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002034 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00002035}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002036
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002037/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002038///
2039/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00002040/// if-clause | final-clause | num_threads-clause | safelen-clause |
2041/// default-clause | private-clause | firstprivate-clause | shared-clause
2042/// | linear-clause | aligned-clause | collapse-clause |
2043/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002044/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00002045/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00002046/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00002047/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002048/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00002049/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00002050/// from-clause | is_device_ptr-clause | task_reduction-clause |
Alexey Bataeve04483e2019-03-27 14:14:31 +00002051/// in_reduction-clause | allocator-clause | allocate-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002052///
2053OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
2054 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00002055 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002056 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002057 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002058 // Check if clause is allowed for the given directive.
Alexey Bataevd08c0562019-11-19 12:07:54 -05002059 if (CKind != OMPC_unknown &&
2060 !isAllowedClauseForDirective(DKind, CKind, getLangOpts().OpenMP)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00002061 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
2062 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002063 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002064 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002065 }
2066
2067 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00002068 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002069 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002070 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00002071 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002072 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00002073 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00002074 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00002075 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00002076 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00002077 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002078 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00002079 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00002080 case OMPC_hint:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002081 case OMPC_allocator:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002082 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00002083 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00002084 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002085 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00002086 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00002087 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00002088 // OpenMP [2.9.1, target data construct, Restrictions]
2089 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00002090 // OpenMP [2.11.1, task Construct, Restrictions]
2091 // At most one if clause can appear on the directive.
2092 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00002093 // OpenMP [teams Construct, Restrictions]
2094 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00002095 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00002096 // OpenMP [2.9.1, task Construct, Restrictions]
2097 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002098 // OpenMP [2.9.2, taskloop Construct, Restrictions]
2099 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00002100 // OpenMP [2.9.2, taskloop Construct, Restrictions]
2101 // At most one num_tasks clause can appear on the directive.
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002102 // OpenMP [2.11.3, allocate Directive, Restrictions]
2103 // At most one allocator clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002104 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002105 Diag(Tok, diag::err_omp_more_one_clause)
2106 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002107 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002108 }
2109
Alexey Bataev10e775f2015-07-30 11:36:16 +00002110 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002111 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00002112 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002113 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002114 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002115 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002116 case OMPC_proc_bind:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00002117 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002118 // OpenMP [2.14.3.1, Restrictions]
2119 // Only a single default clause may be specified on a parallel, task or
2120 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002121 // OpenMP [2.5, parallel Construct, Restrictions]
2122 // At most one proc_bind clause can appear on the directive.
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00002123 // OpenMP [5.0, Requires directive, Restrictions]
2124 // At most one atomic_default_mem_order clause can appear
2125 // on the directive
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002126 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002127 Diag(Tok, diag::err_omp_more_one_clause)
2128 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002129 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002130 }
2131
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002132 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002133 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002134 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00002135 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002136 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002137 // OpenMP [2.7.1, Restrictions, p. 3]
2138 // Only one schedule clause can appear on a loop directive.
cchene06f3e02019-11-15 13:02:06 -05002139 // OpenMP 4.5 [2.10.4, Restrictions, p. 106]
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002140 // At most one defaultmap clause can appear on the directive.
cchene06f3e02019-11-15 13:02:06 -05002141 if ((getLangOpts().OpenMP < 50 || CKind != OMPC_defaultmap) &&
2142 !FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002143 Diag(Tok, diag::err_omp_more_one_clause)
2144 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002145 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002146 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00002147 LLVM_FALLTHROUGH;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002148 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002149 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002150 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002151 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002152 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002153 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002154 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002155 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00002156 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00002157 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002158 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00002159 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002160 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00002161 case OMPC_nogroup:
Kelvin Li1408f912018-09-26 04:28:39 +00002162 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00002163 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00002164 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00002165 case OMPC_dynamic_allocators:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002166 // OpenMP [2.7.1, Restrictions, p. 9]
2167 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00002168 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
2169 // Only one nowait clause can appear on a for directive.
Kelvin Li1408f912018-09-26 04:28:39 +00002170 // OpenMP [5.0, Requires directive, Restrictions]
2171 // Each of the requires clauses can appear at most once on the directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002172 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002173 Diag(Tok, diag::err_omp_more_one_clause)
2174 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002175 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002176 }
2177
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002178 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002179 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002180 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002181 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002182 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002183 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002184 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00002185 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00002186 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002187 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002188 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002189 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002190 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002191 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002192 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00002193 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00002194 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00002195 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00002196 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00002197 case OMPC_is_device_ptr:
Alexey Bataeve04483e2019-03-27 14:14:31 +00002198 case OMPC_allocate:
Alexey Bataevb6e70842019-12-16 15:54:17 -05002199 case OMPC_nontemporal:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002200 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002201 break;
Alexey Bataev729e2422019-08-23 16:11:14 +00002202 case OMPC_device_type:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002203 case OMPC_unknown:
2204 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00002205 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00002206 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002207 break;
2208 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002209 case OMPC_uniform:
Alexey Bataevdba792c2019-09-23 18:13:31 +00002210 case OMPC_match:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002211 if (!WrongDirective)
2212 Diag(Tok, diag::err_omp_unexpected_clause)
2213 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00002214 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002215 break;
2216 }
Craig Topper161e4db2014-05-21 06:02:52 +00002217 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002218}
2219
Alexey Bataev2af33e32016-04-07 12:45:37 +00002220/// Parses simple expression in parens for single-expression clauses of OpenMP
2221/// constructs.
2222/// \param RLoc Returned location of right paren.
2223ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
Alexey Bataevd158cf62019-09-13 20:18:17 +00002224 SourceLocation &RLoc,
2225 bool IsAddressOfOperand) {
Alexey Bataev2af33e32016-04-07 12:45:37 +00002226 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2227 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
2228 return ExprError();
2229
2230 SourceLocation ELoc = Tok.getLocation();
2231 ExprResult LHS(ParseCastExpression(
Alexey Bataevd158cf62019-09-13 20:18:17 +00002232 /*isUnaryExpression=*/false, IsAddressOfOperand, NotTypeCast));
Alexey Bataev2af33e32016-04-07 12:45:37 +00002233 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002234 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev2af33e32016-04-07 12:45:37 +00002235
2236 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002237 RLoc = Tok.getLocation();
2238 if (!T.consumeClose())
2239 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00002240
Alexey Bataev2af33e32016-04-07 12:45:37 +00002241 return Val;
2242}
2243
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002244/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00002245/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00002246/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002247///
Alexey Bataev3778b602014-07-17 07:32:53 +00002248/// final-clause:
2249/// 'final' '(' expression ')'
2250///
Alexey Bataev62c87d22014-03-21 04:51:18 +00002251/// num_threads-clause:
2252/// 'num_threads' '(' expression ')'
2253///
2254/// safelen-clause:
2255/// 'safelen' '(' expression ')'
2256///
Alexey Bataev66b15b52015-08-21 11:14:16 +00002257/// simdlen-clause:
2258/// 'simdlen' '(' expression ')'
2259///
Alexander Musman8bd31e62014-05-27 15:12:19 +00002260/// collapse-clause:
2261/// 'collapse' '(' expression ')'
2262///
Alexey Bataeva0569352015-12-01 10:17:31 +00002263/// priority-clause:
2264/// 'priority' '(' expression ')'
2265///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002266/// grainsize-clause:
2267/// 'grainsize' '(' expression ')'
2268///
Alexey Bataev382967a2015-12-08 12:06:20 +00002269/// num_tasks-clause:
2270/// 'num_tasks' '(' expression ')'
2271///
Alexey Bataev28c75412015-12-15 08:19:24 +00002272/// hint-clause:
2273/// 'hint' '(' expression ')'
2274///
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002275/// allocator-clause:
2276/// 'allocator' '(' expression ')'
2277///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002278OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
2279 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002280 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00002281 SourceLocation LLoc = Tok.getLocation();
2282 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002283
Alexey Bataev2af33e32016-04-07 12:45:37 +00002284 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002285
2286 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00002287 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002288
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002289 if (ParseOnly)
2290 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00002291 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002292}
2293
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002294/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002295///
2296/// default-clause:
2297/// 'default' '(' 'none' | 'shared' ')
2298///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002299/// proc_bind-clause:
2300/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
2301///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002302OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
2303 bool ParseOnly) {
Alexey Bataev729e2422019-08-23 16:11:14 +00002304 llvm::Optional<SimpleClauseData> Val = parseOpenMPSimpleClause(*this, Kind);
2305 if (!Val || ParseOnly)
Craig Topper161e4db2014-05-21 06:02:52 +00002306 return nullptr;
Alexey Bataev729e2422019-08-23 16:11:14 +00002307 return Actions.ActOnOpenMPSimpleClause(
2308 Kind, Val.getValue().Type, Val.getValue().TypeLoc, Val.getValue().LOpen,
2309 Val.getValue().Loc, Val.getValue().RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002310}
2311
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002312/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002313///
2314/// ordered-clause:
2315/// 'ordered'
2316///
Alexey Bataev236070f2014-06-20 11:19:47 +00002317/// nowait-clause:
2318/// 'nowait'
2319///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002320/// untied-clause:
2321/// 'untied'
2322///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002323/// mergeable-clause:
2324/// 'mergeable'
2325///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002326/// read-clause:
2327/// 'read'
2328///
Alexey Bataev346265e2015-09-25 10:37:12 +00002329/// threads-clause:
2330/// 'threads'
2331///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002332/// simd-clause:
2333/// 'simd'
2334///
Alexey Bataevb825de12015-12-07 10:51:44 +00002335/// nogroup-clause:
2336/// 'nogroup'
2337///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002338OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002339 SourceLocation Loc = Tok.getLocation();
2340 ConsumeAnyToken();
2341
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002342 if (ParseOnly)
2343 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002344 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
2345}
2346
2347
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002348/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00002349/// argument like 'schedule' or 'dist_schedule'.
2350///
2351/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00002352/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
2353/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00002354///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002355/// if-clause:
2356/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
2357///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002358/// defaultmap:
2359/// 'defaultmap' '(' modifier ':' kind ')'
2360///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002361OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
2362 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00002363 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002364 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002365 // Parse '('.
2366 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2367 if (T.expectAndConsume(diag::err_expected_lparen_after,
2368 getOpenMPClauseName(Kind)))
2369 return nullptr;
2370
2371 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002372 SmallVector<unsigned, 4> Arg;
2373 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002374 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00002375 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
2376 Arg.resize(NumberOfElements);
2377 KLoc.resize(NumberOfElements);
2378 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
2379 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
2380 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00002381 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002382 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00002383 if (KindModifier > OMPC_SCHEDULE_unknown) {
2384 // Parse 'modifier'
2385 Arg[Modifier1] = KindModifier;
2386 KLoc[Modifier1] = Tok.getLocation();
2387 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2388 Tok.isNot(tok::annot_pragma_openmp_end))
2389 ConsumeAnyToken();
2390 if (Tok.is(tok::comma)) {
2391 // Parse ',' 'modifier'
2392 ConsumeAnyToken();
2393 KindModifier = getOpenMPSimpleClauseType(
2394 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2395 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
2396 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00002397 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002398 KLoc[Modifier2] = Tok.getLocation();
2399 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2400 Tok.isNot(tok::annot_pragma_openmp_end))
2401 ConsumeAnyToken();
2402 }
2403 // Parse ':'
2404 if (Tok.is(tok::colon))
2405 ConsumeAnyToken();
2406 else
2407 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
2408 KindModifier = getOpenMPSimpleClauseType(
2409 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2410 }
2411 Arg[ScheduleKind] = KindModifier;
2412 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002413 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2414 Tok.isNot(tok::annot_pragma_openmp_end))
2415 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00002416 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
2417 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
2418 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002419 Tok.is(tok::comma))
2420 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00002421 } else if (Kind == OMPC_dist_schedule) {
2422 Arg.push_back(getOpenMPSimpleClauseType(
2423 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2424 KLoc.push_back(Tok.getLocation());
2425 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2426 Tok.isNot(tok::annot_pragma_openmp_end))
2427 ConsumeAnyToken();
2428 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
2429 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002430 } else if (Kind == OMPC_defaultmap) {
2431 // Get a defaultmap modifier
cchene06f3e02019-11-15 13:02:06 -05002432 unsigned Modifier = getOpenMPSimpleClauseType(
2433 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2434 // Set defaultmap modifier to unknown if it is either scalar, aggregate, or
2435 // pointer
2436 if (Modifier < OMPC_DEFAULTMAP_MODIFIER_unknown)
2437 Modifier = OMPC_DEFAULTMAP_MODIFIER_unknown;
2438 Arg.push_back(Modifier);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002439 KLoc.push_back(Tok.getLocation());
2440 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2441 Tok.isNot(tok::annot_pragma_openmp_end))
2442 ConsumeAnyToken();
2443 // Parse ':'
2444 if (Tok.is(tok::colon))
2445 ConsumeAnyToken();
2446 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
2447 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
2448 // Get a defaultmap kind
2449 Arg.push_back(getOpenMPSimpleClauseType(
2450 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2451 KLoc.push_back(Tok.getLocation());
2452 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2453 Tok.isNot(tok::annot_pragma_openmp_end))
2454 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002455 } else {
2456 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00002457 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002458 TentativeParsingAction TPA(*this);
Johannes Doerferteb3e81f2019-11-04 22:00:49 -06002459 auto DK = parseOpenMPDirectiveKind(*this);
2460 Arg.push_back(DK);
2461 if (DK != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002462 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002463 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
2464 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002465 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002466 } else {
2467 TPA.Revert();
Johannes Doerferteb3e81f2019-11-04 22:00:49 -06002468 Arg.back() = unsigned(OMPD_unknown);
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002469 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002470 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002471 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00002472 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002473 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00002474
Carlo Bertollib4adf552016-01-15 18:50:31 +00002475 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
2476 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
2477 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002478 if (NeedAnExpression) {
2479 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002480 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
2481 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002482 Val =
2483 Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002484 }
2485
2486 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002487 SourceLocation RLoc = Tok.getLocation();
2488 if (!T.consumeClose())
2489 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002490
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002491 if (NeedAnExpression && Val.isInvalid())
2492 return nullptr;
2493
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002494 if (ParseOnly)
2495 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002496 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002497 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002498}
2499
Alexey Bataevc5e02582014-06-16 07:08:35 +00002500static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
2501 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002502 if (ReductionIdScopeSpec.isEmpty()) {
2503 auto OOK = OO_None;
2504 switch (P.getCurToken().getKind()) {
2505 case tok::plus:
2506 OOK = OO_Plus;
2507 break;
2508 case tok::minus:
2509 OOK = OO_Minus;
2510 break;
2511 case tok::star:
2512 OOK = OO_Star;
2513 break;
2514 case tok::amp:
2515 OOK = OO_Amp;
2516 break;
2517 case tok::pipe:
2518 OOK = OO_Pipe;
2519 break;
2520 case tok::caret:
2521 OOK = OO_Caret;
2522 break;
2523 case tok::ampamp:
2524 OOK = OO_AmpAmp;
2525 break;
2526 case tok::pipepipe:
2527 OOK = OO_PipePipe;
2528 break;
2529 default:
2530 break;
2531 }
2532 if (OOK != OO_None) {
2533 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00002534 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00002535 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
2536 return false;
2537 }
2538 }
2539 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
2540 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00002541 /*AllowConstructorName*/ false,
2542 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00002543 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002544}
2545
Kelvin Lief579432018-12-18 22:18:41 +00002546/// Checks if the token is a valid map-type-modifier.
2547static OpenMPMapModifierKind isMapModifier(Parser &P) {
2548 Token Tok = P.getCurToken();
2549 if (!Tok.is(tok::identifier))
2550 return OMPC_MAP_MODIFIER_unknown;
2551
2552 Preprocessor &PP = P.getPreprocessor();
2553 OpenMPMapModifierKind TypeModifier = static_cast<OpenMPMapModifierKind>(
2554 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2555 return TypeModifier;
2556}
2557
Michael Kruse01f670d2019-02-22 22:29:42 +00002558/// Parse the mapper modifier in map, to, and from clauses.
2559bool Parser::parseMapperModifier(OpenMPVarListDataTy &Data) {
2560 // Parse '('.
2561 BalancedDelimiterTracker T(*this, tok::l_paren, tok::colon);
2562 if (T.expectAndConsume(diag::err_expected_lparen_after, "mapper")) {
2563 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2564 StopBeforeMatch);
2565 return true;
2566 }
2567 // Parse mapper-identifier
2568 if (getLangOpts().CPlusPlus)
2569 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
2570 /*ObjectType=*/nullptr,
2571 /*EnteringContext=*/false);
2572 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
2573 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
2574 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2575 StopBeforeMatch);
2576 return true;
2577 }
2578 auto &DeclNames = Actions.getASTContext().DeclarationNames;
2579 Data.ReductionOrMapperId = DeclarationNameInfo(
2580 DeclNames.getIdentifier(Tok.getIdentifierInfo()), Tok.getLocation());
2581 ConsumeToken();
2582 // Parse ')'.
2583 return T.consumeClose();
2584}
2585
Kelvin Lief579432018-12-18 22:18:41 +00002586/// Parse map-type-modifiers in map clause.
2587/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002588/// where, map-type-modifier ::= always | close | mapper(mapper-identifier)
2589bool Parser::parseMapTypeModifiers(OpenMPVarListDataTy &Data) {
2590 while (getCurToken().isNot(tok::colon)) {
2591 OpenMPMapModifierKind TypeModifier = isMapModifier(*this);
Kelvin Lief579432018-12-18 22:18:41 +00002592 if (TypeModifier == OMPC_MAP_MODIFIER_always ||
2593 TypeModifier == OMPC_MAP_MODIFIER_close) {
2594 Data.MapTypeModifiers.push_back(TypeModifier);
2595 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
Michael Kruse4304e9d2019-02-19 16:38:20 +00002596 ConsumeToken();
2597 } else if (TypeModifier == OMPC_MAP_MODIFIER_mapper) {
2598 Data.MapTypeModifiers.push_back(TypeModifier);
2599 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
2600 ConsumeToken();
Michael Kruse01f670d2019-02-22 22:29:42 +00002601 if (parseMapperModifier(Data))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002602 return true;
Kelvin Lief579432018-12-18 22:18:41 +00002603 } else {
2604 // For the case of unknown map-type-modifier or a map-type.
2605 // Map-type is followed by a colon; the function returns when it
2606 // encounters a token followed by a colon.
2607 if (Tok.is(tok::comma)) {
Michael Kruse4304e9d2019-02-19 16:38:20 +00002608 Diag(Tok, diag::err_omp_map_type_modifier_missing);
2609 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002610 continue;
2611 }
2612 // Potential map-type token as it is followed by a colon.
2613 if (PP.LookAhead(0).is(tok::colon))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002614 return false;
2615 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
2616 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002617 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002618 if (getCurToken().is(tok::comma))
2619 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002620 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002621 return false;
Kelvin Lief579432018-12-18 22:18:41 +00002622}
2623
2624/// Checks if the token is a valid map-type.
2625static OpenMPMapClauseKind isMapType(Parser &P) {
2626 Token Tok = P.getCurToken();
2627 // The map-type token can be either an identifier or the C++ delete keyword.
2628 if (!Tok.isOneOf(tok::identifier, tok::kw_delete))
2629 return OMPC_MAP_unknown;
2630 Preprocessor &PP = P.getPreprocessor();
2631 OpenMPMapClauseKind MapType = static_cast<OpenMPMapClauseKind>(
2632 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2633 return MapType;
2634}
2635
2636/// Parse map-type in map clause.
2637/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002638/// where, map-type ::= to | from | tofrom | alloc | release | delete
Kelvin Lief579432018-12-18 22:18:41 +00002639static void parseMapType(Parser &P, Parser::OpenMPVarListDataTy &Data) {
2640 Token Tok = P.getCurToken();
2641 if (Tok.is(tok::colon)) {
2642 P.Diag(Tok, diag::err_omp_map_type_missing);
2643 return;
2644 }
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002645 Data.ExtraModifier = isMapType(P);
2646 if (Data.ExtraModifier == OMPC_MAP_unknown)
Kelvin Lief579432018-12-18 22:18:41 +00002647 P.Diag(Tok, diag::err_omp_unknown_map_type);
2648 P.ConsumeToken();
2649}
2650
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002651/// Parses clauses with list.
2652bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
2653 OpenMPClauseKind Kind,
2654 SmallVectorImpl<Expr *> &Vars,
2655 OpenMPVarListDataTy &Data) {
2656 UnqualifiedId UnqualifiedReductionId;
2657 bool InvalidReductionId = false;
Michael Kruse01f670d2019-02-22 22:29:42 +00002658 bool IsInvalidMapperModifier = false;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002659
2660 // Parse '('.
2661 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2662 if (T.expectAndConsume(diag::err_expected_lparen_after,
2663 getOpenMPClauseName(Kind)))
2664 return true;
2665
2666 bool NeedRParenForLinear = false;
2667 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
2668 tok::annot_pragma_openmp_end);
2669 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00002670 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
2671 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002672 ColonProtectionRAIIObject ColonRAII(*this);
2673 if (getLangOpts().CPlusPlus)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002674 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002675 /*ObjectType=*/nullptr,
2676 /*EnteringContext=*/false);
Michael Kruse4304e9d2019-02-19 16:38:20 +00002677 InvalidReductionId = ParseReductionId(
2678 *this, Data.ReductionOrMapperIdScopeSpec, UnqualifiedReductionId);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002679 if (InvalidReductionId) {
2680 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2681 StopBeforeMatch);
2682 }
2683 if (Tok.is(tok::colon))
2684 Data.ColonLoc = ConsumeToken();
2685 else
2686 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
2687 if (!InvalidReductionId)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002688 Data.ReductionOrMapperId =
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002689 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
2690 } else if (Kind == OMPC_depend) {
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002691 // Handle dependency type for depend clause.
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002692 ColonProtectionRAIIObject ColonRAII(*this);
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002693 Data.ExtraModifier = getOpenMPSimpleClauseType(
2694 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : "");
2695 Data.DepLinMapLastLoc = Tok.getLocation();
2696 if (Data.ExtraModifier == OMPC_DEPEND_unknown) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002697 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2698 StopBeforeMatch);
2699 } else {
2700 ConsumeToken();
2701 // Special processing for depend(source) clause.
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002702 if (DKind == OMPD_ordered && Data.ExtraModifier == OMPC_DEPEND_source) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002703 // Parse ')'.
2704 T.consumeClose();
2705 return false;
2706 }
2707 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002708 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002709 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00002710 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002711 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
2712 : diag::warn_pragma_expected_colon)
2713 << "dependency type";
2714 }
2715 } else if (Kind == OMPC_linear) {
2716 // Try to parse modifier if any.
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002717 Data.ExtraModifier = OMPC_LINEAR_val;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002718 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002719 Data.ExtraModifier = getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok));
2720 Data.DepLinMapLastLoc = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002721 LinearT.consumeOpen();
2722 NeedRParenForLinear = true;
2723 }
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002724 } else if (Kind == OMPC_lastprivate) {
2725 // Try to parse modifier if any.
2726 Data.ExtraModifier = OMPC_LASTPRIVATE_unknown;
2727 // Conditional modifier allowed only in OpenMP 5.0 and not supported in
2728 // distribute and taskloop based directives.
2729 if ((getLangOpts().OpenMP >= 50 && !isOpenMPDistributeDirective(DKind) &&
2730 !isOpenMPTaskLoopDirective(DKind)) &&
2731 Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::colon)) {
2732 Data.ExtraModifier = getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok));
2733 Data.DepLinMapLastLoc = Tok.getLocation();
2734 if (Data.ExtraModifier == OMPC_LASTPRIVATE_unknown) {
2735 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2736 StopBeforeMatch);
2737 } else {
2738 ConsumeToken();
2739 }
2740 assert(Tok.is(tok::colon) && "Expected colon.");
2741 Data.ColonLoc = ConsumeToken();
2742 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002743 } else if (Kind == OMPC_map) {
2744 // Handle map type for map clause.
2745 ColonProtectionRAIIObject ColonRAII(*this);
2746
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002747 // The first identifier may be a list item, a map-type or a
Kelvin Lief579432018-12-18 22:18:41 +00002748 // map-type-modifier. The map-type can also be delete which has the same
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002749 // spelling of the C++ delete keyword.
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002750 Data.ExtraModifier = OMPC_MAP_unknown;
2751 Data.DepLinMapLastLoc = Tok.getLocation();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002752
Kelvin Lief579432018-12-18 22:18:41 +00002753 // Check for presence of a colon in the map clause.
2754 TentativeParsingAction TPA(*this);
2755 bool ColonPresent = false;
2756 if (SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2757 StopBeforeMatch)) {
2758 if (Tok.is(tok::colon))
2759 ColonPresent = true;
2760 }
2761 TPA.Revert();
2762 // Only parse map-type-modifier[s] and map-type if a colon is present in
2763 // the map clause.
2764 if (ColonPresent) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002765 IsInvalidMapperModifier = parseMapTypeModifiers(Data);
2766 if (!IsInvalidMapperModifier)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002767 parseMapType(*this, Data);
Michael Kruse01f670d2019-02-22 22:29:42 +00002768 else
2769 SkipUntil(tok::colon, tok::annot_pragma_openmp_end, StopBeforeMatch);
Kelvin Lief579432018-12-18 22:18:41 +00002770 }
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002771 if (Data.ExtraModifier == OMPC_MAP_unknown) {
2772 Data.ExtraModifier = OMPC_MAP_tofrom;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002773 Data.IsMapTypeImplicit = true;
2774 }
2775
2776 if (Tok.is(tok::colon))
2777 Data.ColonLoc = ConsumeToken();
Michael Kruse0336c752019-02-25 20:34:15 +00002778 } else if (Kind == OMPC_to || Kind == OMPC_from) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002779 if (Tok.is(tok::identifier)) {
2780 bool IsMapperModifier = false;
Michael Kruse0336c752019-02-25 20:34:15 +00002781 if (Kind == OMPC_to) {
2782 auto Modifier = static_cast<OpenMPToModifierKind>(
2783 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2784 if (Modifier == OMPC_TO_MODIFIER_mapper)
2785 IsMapperModifier = true;
2786 } else {
2787 auto Modifier = static_cast<OpenMPFromModifierKind>(
2788 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2789 if (Modifier == OMPC_FROM_MODIFIER_mapper)
2790 IsMapperModifier = true;
2791 }
Michael Kruse01f670d2019-02-22 22:29:42 +00002792 if (IsMapperModifier) {
2793 // Parse the mapper modifier.
2794 ConsumeToken();
2795 IsInvalidMapperModifier = parseMapperModifier(Data);
2796 if (Tok.isNot(tok::colon)) {
2797 if (!IsInvalidMapperModifier)
2798 Diag(Tok, diag::warn_pragma_expected_colon) << ")";
2799 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2800 StopBeforeMatch);
2801 }
2802 // Consume ':'.
2803 if (Tok.is(tok::colon))
2804 ConsumeToken();
2805 }
2806 }
Alexey Bataeve04483e2019-03-27 14:14:31 +00002807 } else if (Kind == OMPC_allocate) {
2808 // Handle optional allocator expression followed by colon delimiter.
2809 ColonProtectionRAIIObject ColonRAII(*this);
2810 TentativeParsingAction TPA(*this);
2811 ExprResult Tail =
2812 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
2813 Tail = Actions.ActOnFinishFullExpr(Tail.get(), T.getOpenLocation(),
2814 /*DiscardedValue=*/false);
2815 if (Tail.isUsable()) {
2816 if (Tok.is(tok::colon)) {
2817 Data.TailExpr = Tail.get();
2818 Data.ColonLoc = ConsumeToken();
2819 TPA.Commit();
2820 } else {
2821 // colon not found, no allocator specified, parse only list of
2822 // variables.
2823 TPA.Revert();
2824 }
2825 } else {
2826 // Parsing was unsuccessfull, revert and skip to the end of clause or
2827 // directive.
2828 TPA.Revert();
2829 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2830 StopBeforeMatch);
2831 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002832 }
2833
Alexey Bataevfa312f32017-07-21 18:48:21 +00002834 bool IsComma =
2835 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
2836 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
2837 (Kind == OMPC_reduction && !InvalidReductionId) ||
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002838 (Kind == OMPC_map && Data.ExtraModifier != OMPC_MAP_unknown) ||
2839 (Kind == OMPC_depend && Data.ExtraModifier != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002840 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
2841 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
2842 Tok.isNot(tok::annot_pragma_openmp_end))) {
2843 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
2844 // Parse variable
2845 ExprResult VarExpr =
2846 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00002847 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002848 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00002849 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002850 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2851 StopBeforeMatch);
2852 }
2853 // Skip ',' if any
2854 IsComma = Tok.is(tok::comma);
2855 if (IsComma)
2856 ConsumeToken();
2857 else if (Tok.isNot(tok::r_paren) &&
2858 Tok.isNot(tok::annot_pragma_openmp_end) &&
2859 (!MayHaveTail || Tok.isNot(tok::colon)))
2860 Diag(Tok, diag::err_omp_expected_punc)
2861 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
2862 : getOpenMPClauseName(Kind))
2863 << (Kind == OMPC_flush);
2864 }
2865
2866 // Parse ')' for linear clause with modifier.
2867 if (NeedRParenForLinear)
2868 LinearT.consumeClose();
2869
2870 // Parse ':' linear-step (or ':' alignment).
2871 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
2872 if (MustHaveTail) {
2873 Data.ColonLoc = Tok.getLocation();
2874 SourceLocation ELoc = ConsumeToken();
2875 ExprResult Tail = ParseAssignmentExpression();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002876 Tail =
2877 Actions.ActOnFinishFullExpr(Tail.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002878 if (Tail.isUsable())
2879 Data.TailExpr = Tail.get();
2880 else
2881 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2882 StopBeforeMatch);
2883 }
2884
2885 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002886 Data.RLoc = Tok.getLocation();
2887 if (!T.consumeClose())
2888 Data.RLoc = T.getCloseLocation();
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002889 return (Kind == OMPC_depend && Data.ExtraModifier != OMPC_DEPEND_unknown &&
Alexey Bataev61908f652018-04-23 19:53:05 +00002890 Vars.empty()) ||
2891 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
Michael Kruse4304e9d2019-02-19 16:38:20 +00002892 (MustHaveTail && !Data.TailExpr) || InvalidReductionId ||
Michael Kruse01f670d2019-02-22 22:29:42 +00002893 IsInvalidMapperModifier;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002894}
2895
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002896/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00002897/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
2898/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002899///
2900/// private-clause:
2901/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002902/// firstprivate-clause:
2903/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00002904/// lastprivate-clause:
2905/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00002906/// shared-clause:
2907/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00002908/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00002909/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002910/// aligned-clause:
2911/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00002912/// reduction-clause:
2913/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00002914/// task_reduction-clause:
2915/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00002916/// in_reduction-clause:
2917/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00002918/// copyprivate-clause:
2919/// 'copyprivate' '(' list ')'
2920/// flush-clause:
2921/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002922/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00002923/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00002924/// map-clause:
Kelvin Lief579432018-12-18 22:18:41 +00002925/// 'map' '(' [ [ always [,] ] [ close [,] ]
Michael Kruse01f670d2019-02-22 22:29:42 +00002926/// [ mapper '(' mapper-identifier ')' [,] ]
Kelvin Li0bff7af2015-11-23 05:32:03 +00002927/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00002928/// to-clause:
Michael Kruse01f670d2019-02-22 22:29:42 +00002929/// 'to' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00002930/// from-clause:
Michael Kruse0336c752019-02-25 20:34:15 +00002931/// 'from' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00002932/// use_device_ptr-clause:
2933/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00002934/// is_device_ptr-clause:
2935/// 'is_device_ptr' '(' list ')'
Alexey Bataeve04483e2019-03-27 14:14:31 +00002936/// allocate-clause:
2937/// 'allocate' '(' [ allocator ':' ] list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002938///
Alexey Bataev182227b2015-08-20 10:54:39 +00002939/// For 'linear' clause linear-list may have the following forms:
2940/// list
2941/// modifier(list)
2942/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00002943OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002944 OpenMPClauseKind Kind,
2945 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002946 SourceLocation Loc = Tok.getLocation();
2947 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002948 SmallVector<Expr *, 4> Vars;
2949 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002950
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002951 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00002952 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002953
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002954 if (ParseOnly)
2955 return nullptr;
Michael Kruse4304e9d2019-02-19 16:38:20 +00002956 OMPVarListLocTy Locs(Loc, LOpen, Data.RLoc);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002957 return Actions.ActOnOpenMPVarListClause(
Michael Kruse4304e9d2019-02-19 16:38:20 +00002958 Kind, Vars, Data.TailExpr, Locs, Data.ColonLoc,
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002959 Data.ReductionOrMapperIdScopeSpec, Data.ReductionOrMapperId,
2960 Data.ExtraModifier, Data.MapTypeModifiers, Data.MapTypeModifiersLoc,
2961 Data.IsMapTypeImplicit, Data.DepLinMapLastLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002962}
2963