File: | build/llvm-toolchain-snapshot-15~++20220214100628+9dcb0061657e/clang/lib/Parse/ParseOpenMP.cpp |
Warning: | line 2849, column 7 Value stored to 'HasImplicitClause' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===// |
2 | // |
3 | // 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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | /// \file |
9 | /// This file implements parsing of all OpenMP directives and clauses. |
10 | /// |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "clang/AST/ASTContext.h" |
14 | #include "clang/AST/OpenMPClause.h" |
15 | #include "clang/AST/StmtOpenMP.h" |
16 | #include "clang/Basic/OpenMPKinds.h" |
17 | #include "clang/Basic/TargetInfo.h" |
18 | #include "clang/Basic/TokenKinds.h" |
19 | #include "clang/Parse/ParseDiagnostic.h" |
20 | #include "clang/Parse/Parser.h" |
21 | #include "clang/Parse/RAIIObjectsForParser.h" |
22 | #include "clang/Sema/Scope.h" |
23 | #include "llvm/ADT/PointerIntPair.h" |
24 | #include "llvm/ADT/StringSwitch.h" |
25 | #include "llvm/ADT/UniqueVector.h" |
26 | #include "llvm/Frontend/OpenMP/OMPAssume.h" |
27 | #include "llvm/Frontend/OpenMP/OMPContext.h" |
28 | |
29 | using namespace clang; |
30 | using namespace llvm::omp; |
31 | |
32 | //===----------------------------------------------------------------------===// |
33 | // OpenMP declarative directives. |
34 | //===----------------------------------------------------------------------===// |
35 | |
36 | namespace { |
37 | enum OpenMPDirectiveKindEx { |
38 | OMPD_cancellation = llvm::omp::Directive_enumSize + 1, |
39 | OMPD_data, |
40 | OMPD_declare, |
41 | OMPD_end, |
42 | OMPD_end_declare, |
43 | OMPD_enter, |
44 | OMPD_exit, |
45 | OMPD_point, |
46 | OMPD_reduction, |
47 | OMPD_target_enter, |
48 | OMPD_target_exit, |
49 | OMPD_update, |
50 | OMPD_distribute_parallel, |
51 | OMPD_teams_distribute_parallel, |
52 | OMPD_target_teams_distribute_parallel, |
53 | OMPD_mapper, |
54 | OMPD_variant, |
55 | OMPD_begin, |
56 | OMPD_begin_declare, |
57 | }; |
58 | |
59 | // Helper to unify the enum class OpenMPDirectiveKind with its extension |
60 | // the OpenMPDirectiveKindEx enum which allows to use them together as if they |
61 | // are unsigned values. |
62 | struct OpenMPDirectiveKindExWrapper { |
63 | OpenMPDirectiveKindExWrapper(unsigned Value) : Value(Value) {} |
64 | OpenMPDirectiveKindExWrapper(OpenMPDirectiveKind DK) : Value(unsigned(DK)) {} |
65 | bool operator==(OpenMPDirectiveKindExWrapper V) const { |
66 | return Value == V.Value; |
67 | } |
68 | bool operator!=(OpenMPDirectiveKindExWrapper V) const { |
69 | return Value != V.Value; |
70 | } |
71 | bool operator==(OpenMPDirectiveKind V) const { return Value == unsigned(V); } |
72 | bool operator!=(OpenMPDirectiveKind V) const { return Value != unsigned(V); } |
73 | bool operator<(OpenMPDirectiveKind V) const { return Value < unsigned(V); } |
74 | operator unsigned() const { return Value; } |
75 | operator OpenMPDirectiveKind() const { return OpenMPDirectiveKind(Value); } |
76 | unsigned Value; |
77 | }; |
78 | |
79 | class DeclDirectiveListParserHelper final { |
80 | SmallVector<Expr *, 4> Identifiers; |
81 | Parser *P; |
82 | OpenMPDirectiveKind Kind; |
83 | |
84 | public: |
85 | DeclDirectiveListParserHelper(Parser *P, OpenMPDirectiveKind Kind) |
86 | : P(P), Kind(Kind) {} |
87 | void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) { |
88 | ExprResult Res = P->getActions().ActOnOpenMPIdExpression( |
89 | P->getCurScope(), SS, NameInfo, Kind); |
90 | if (Res.isUsable()) |
91 | Identifiers.push_back(Res.get()); |
92 | } |
93 | llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; } |
94 | }; |
95 | } // namespace |
96 | |
97 | // Map token string to extended OMP token kind that are |
98 | // OpenMPDirectiveKind + OpenMPDirectiveKindEx. |
99 | static unsigned getOpenMPDirectiveKindEx(StringRef S) { |
100 | OpenMPDirectiveKindExWrapper DKind = getOpenMPDirectiveKind(S); |
101 | if (DKind != OMPD_unknown) |
102 | return DKind; |
103 | |
104 | return llvm::StringSwitch<OpenMPDirectiveKindExWrapper>(S) |
105 | .Case("cancellation", OMPD_cancellation) |
106 | .Case("data", OMPD_data) |
107 | .Case("declare", OMPD_declare) |
108 | .Case("end", OMPD_end) |
109 | .Case("enter", OMPD_enter) |
110 | .Case("exit", OMPD_exit) |
111 | .Case("point", OMPD_point) |
112 | .Case("reduction", OMPD_reduction) |
113 | .Case("update", OMPD_update) |
114 | .Case("mapper", OMPD_mapper) |
115 | .Case("variant", OMPD_variant) |
116 | .Case("begin", OMPD_begin) |
117 | .Default(OMPD_unknown); |
118 | } |
119 | |
120 | static OpenMPDirectiveKindExWrapper parseOpenMPDirectiveKind(Parser &P) { |
121 | // Array of foldings: F[i][0] F[i][1] ===> F[i][2]. |
122 | // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd |
123 | // TODO: add other combined directives in topological order. |
124 | static const OpenMPDirectiveKindExWrapper F[][3] = { |
125 | {OMPD_begin, OMPD_declare, OMPD_begin_declare}, |
126 | {OMPD_begin, OMPD_assumes, OMPD_begin_assumes}, |
127 | {OMPD_end, OMPD_declare, OMPD_end_declare}, |
128 | {OMPD_end, OMPD_assumes, OMPD_end_assumes}, |
129 | {OMPD_cancellation, OMPD_point, OMPD_cancellation_point}, |
130 | {OMPD_declare, OMPD_reduction, OMPD_declare_reduction}, |
131 | {OMPD_declare, OMPD_mapper, OMPD_declare_mapper}, |
132 | {OMPD_declare, OMPD_simd, OMPD_declare_simd}, |
133 | {OMPD_declare, OMPD_target, OMPD_declare_target}, |
134 | {OMPD_declare, OMPD_variant, OMPD_declare_variant}, |
135 | {OMPD_begin_declare, OMPD_target, OMPD_begin_declare_target}, |
136 | {OMPD_begin_declare, OMPD_variant, OMPD_begin_declare_variant}, |
137 | {OMPD_end_declare, OMPD_variant, OMPD_end_declare_variant}, |
138 | {OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel}, |
139 | {OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for}, |
140 | {OMPD_distribute_parallel_for, OMPD_simd, |
141 | OMPD_distribute_parallel_for_simd}, |
142 | {OMPD_distribute, OMPD_simd, OMPD_distribute_simd}, |
143 | {OMPD_end_declare, OMPD_target, OMPD_end_declare_target}, |
144 | {OMPD_target, OMPD_data, OMPD_target_data}, |
145 | {OMPD_target, OMPD_enter, OMPD_target_enter}, |
146 | {OMPD_target, OMPD_exit, OMPD_target_exit}, |
147 | {OMPD_target, OMPD_update, OMPD_target_update}, |
148 | {OMPD_target_enter, OMPD_data, OMPD_target_enter_data}, |
149 | {OMPD_target_exit, OMPD_data, OMPD_target_exit_data}, |
150 | {OMPD_for, OMPD_simd, OMPD_for_simd}, |
151 | {OMPD_parallel, OMPD_for, OMPD_parallel_for}, |
152 | {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd}, |
153 | {OMPD_parallel, OMPD_sections, OMPD_parallel_sections}, |
154 | {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd}, |
155 | {OMPD_target, OMPD_parallel, OMPD_target_parallel}, |
156 | {OMPD_target, OMPD_simd, OMPD_target_simd}, |
157 | {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for}, |
158 | {OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd}, |
159 | {OMPD_teams, OMPD_distribute, OMPD_teams_distribute}, |
160 | {OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd}, |
161 | {OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel}, |
162 | {OMPD_teams_distribute_parallel, OMPD_for, |
163 | OMPD_teams_distribute_parallel_for}, |
164 | {OMPD_teams_distribute_parallel_for, OMPD_simd, |
165 | OMPD_teams_distribute_parallel_for_simd}, |
166 | {OMPD_target, OMPD_teams, OMPD_target_teams}, |
167 | {OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute}, |
168 | {OMPD_target_teams_distribute, OMPD_parallel, |
169 | OMPD_target_teams_distribute_parallel}, |
170 | {OMPD_target_teams_distribute, OMPD_simd, |
171 | OMPD_target_teams_distribute_simd}, |
172 | {OMPD_target_teams_distribute_parallel, OMPD_for, |
173 | OMPD_target_teams_distribute_parallel_for}, |
174 | {OMPD_target_teams_distribute_parallel_for, OMPD_simd, |
175 | OMPD_target_teams_distribute_parallel_for_simd}, |
176 | {OMPD_master, OMPD_taskloop, OMPD_master_taskloop}, |
177 | {OMPD_master_taskloop, OMPD_simd, OMPD_master_taskloop_simd}, |
178 | {OMPD_parallel, OMPD_master, OMPD_parallel_master}, |
179 | {OMPD_parallel_master, OMPD_taskloop, OMPD_parallel_master_taskloop}, |
180 | {OMPD_parallel_master_taskloop, OMPD_simd, |
181 | OMPD_parallel_master_taskloop_simd}}; |
182 | enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 }; |
183 | Token Tok = P.getCurToken(); |
184 | OpenMPDirectiveKindExWrapper DKind = |
185 | Tok.isAnnotation() |
186 | ? static_cast<unsigned>(OMPD_unknown) |
187 | : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok)); |
188 | if (DKind == OMPD_unknown) |
189 | return OMPD_unknown; |
190 | |
191 | for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) { |
192 | if (DKind != F[I][0]) |
193 | continue; |
194 | |
195 | Tok = P.getPreprocessor().LookAhead(0); |
196 | OpenMPDirectiveKindExWrapper SDKind = |
197 | Tok.isAnnotation() |
198 | ? static_cast<unsigned>(OMPD_unknown) |
199 | : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok)); |
200 | if (SDKind == OMPD_unknown) |
201 | continue; |
202 | |
203 | if (SDKind == F[I][1]) { |
204 | P.ConsumeToken(); |
205 | DKind = F[I][2]; |
206 | } |
207 | } |
208 | return unsigned(DKind) < llvm::omp::Directive_enumSize |
209 | ? static_cast<OpenMPDirectiveKind>(DKind) |
210 | : OMPD_unknown; |
211 | } |
212 | |
213 | static DeclarationName parseOpenMPReductionId(Parser &P) { |
214 | Token Tok = P.getCurToken(); |
215 | Sema &Actions = P.getActions(); |
216 | OverloadedOperatorKind OOK = OO_None; |
217 | // Allow to use 'operator' keyword for C++ operators |
218 | bool WithOperator = false; |
219 | if (Tok.is(tok::kw_operator)) { |
220 | P.ConsumeToken(); |
221 | Tok = P.getCurToken(); |
222 | WithOperator = true; |
223 | } |
224 | switch (Tok.getKind()) { |
225 | case tok::plus: // '+' |
226 | OOK = OO_Plus; |
227 | break; |
228 | case tok::minus: // '-' |
229 | OOK = OO_Minus; |
230 | break; |
231 | case tok::star: // '*' |
232 | OOK = OO_Star; |
233 | break; |
234 | case tok::amp: // '&' |
235 | OOK = OO_Amp; |
236 | break; |
237 | case tok::pipe: // '|' |
238 | OOK = OO_Pipe; |
239 | break; |
240 | case tok::caret: // '^' |
241 | OOK = OO_Caret; |
242 | break; |
243 | case tok::ampamp: // '&&' |
244 | OOK = OO_AmpAmp; |
245 | break; |
246 | case tok::pipepipe: // '||' |
247 | OOK = OO_PipePipe; |
248 | break; |
249 | case tok::identifier: // identifier |
250 | if (!WithOperator) |
251 | break; |
252 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; |
253 | default: |
254 | P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier); |
255 | P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, |
256 | Parser::StopBeforeMatch); |
257 | return DeclarationName(); |
258 | } |
259 | P.ConsumeToken(); |
260 | auto &DeclNames = Actions.getASTContext().DeclarationNames; |
261 | return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo()) |
262 | : DeclNames.getCXXOperatorName(OOK); |
263 | } |
264 | |
265 | /// Parse 'omp declare reduction' construct. |
266 | /// |
267 | /// declare-reduction-directive: |
268 | /// annot_pragma_openmp 'declare' 'reduction' |
269 | /// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')' |
270 | /// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')'] |
271 | /// annot_pragma_openmp_end |
272 | /// <reduction_id> is either a base language identifier or one of the following |
273 | /// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'. |
274 | /// |
275 | Parser::DeclGroupPtrTy |
276 | Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) { |
277 | // Parse '('. |
278 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
279 | if (T.expectAndConsume( |
280 | diag::err_expected_lparen_after, |
281 | getOpenMPDirectiveName(OMPD_declare_reduction).data())) { |
282 | SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); |
283 | return DeclGroupPtrTy(); |
284 | } |
285 | |
286 | DeclarationName Name = parseOpenMPReductionId(*this); |
287 | if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end)) |
288 | return DeclGroupPtrTy(); |
289 | |
290 | // Consume ':'. |
291 | bool IsCorrect = !ExpectAndConsume(tok::colon); |
292 | |
293 | if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end)) |
294 | return DeclGroupPtrTy(); |
295 | |
296 | IsCorrect = IsCorrect && !Name.isEmpty(); |
297 | |
298 | if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) { |
299 | Diag(Tok.getLocation(), diag::err_expected_type); |
300 | IsCorrect = false; |
301 | } |
302 | |
303 | if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end)) |
304 | return DeclGroupPtrTy(); |
305 | |
306 | SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes; |
307 | // Parse list of types until ':' token. |
308 | do { |
309 | ColonProtectionRAIIObject ColonRAII(*this); |
310 | SourceRange Range; |
311 | TypeResult TR = ParseTypeName(&Range, DeclaratorContext::Prototype, AS); |
312 | if (TR.isUsable()) { |
313 | QualType ReductionType = |
314 | Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR); |
315 | if (!ReductionType.isNull()) { |
316 | ReductionTypes.push_back( |
317 | std::make_pair(ReductionType, Range.getBegin())); |
318 | } |
319 | } else { |
320 | SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end, |
321 | StopBeforeMatch); |
322 | } |
323 | |
324 | if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) |
325 | break; |
326 | |
327 | // Consume ','. |
328 | if (ExpectAndConsume(tok::comma)) { |
329 | IsCorrect = false; |
330 | if (Tok.is(tok::annot_pragma_openmp_end)) { |
331 | Diag(Tok.getLocation(), diag::err_expected_type); |
332 | return DeclGroupPtrTy(); |
333 | } |
334 | } |
335 | } while (Tok.isNot(tok::annot_pragma_openmp_end)); |
336 | |
337 | if (ReductionTypes.empty()) { |
338 | SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); |
339 | return DeclGroupPtrTy(); |
340 | } |
341 | |
342 | if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end)) |
343 | return DeclGroupPtrTy(); |
344 | |
345 | // Consume ':'. |
346 | if (ExpectAndConsume(tok::colon)) |
347 | IsCorrect = false; |
348 | |
349 | if (Tok.is(tok::annot_pragma_openmp_end)) { |
350 | Diag(Tok.getLocation(), diag::err_expected_expression); |
351 | return DeclGroupPtrTy(); |
352 | } |
353 | |
354 | DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart( |
355 | getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS); |
356 | |
357 | // Parse <combiner> expression and then parse initializer if any for each |
358 | // correct type. |
359 | unsigned I = 0, E = ReductionTypes.size(); |
360 | for (Decl *D : DRD.get()) { |
361 | TentativeParsingAction TPA(*this); |
362 | ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope | |
363 | Scope::CompoundStmtScope | |
364 | Scope::OpenMPDirectiveScope); |
365 | // Parse <combiner> expression. |
366 | Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D); |
367 | ExprResult CombinerResult = Actions.ActOnFinishFullExpr( |
368 | ParseExpression().get(), D->getLocation(), /*DiscardedValue*/ false); |
369 | Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get()); |
370 | |
371 | if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) && |
372 | Tok.isNot(tok::annot_pragma_openmp_end)) { |
373 | TPA.Commit(); |
374 | IsCorrect = false; |
375 | break; |
376 | } |
377 | IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable(); |
378 | ExprResult InitializerResult; |
379 | if (Tok.isNot(tok::annot_pragma_openmp_end)) { |
380 | // Parse <initializer> expression. |
381 | if (Tok.is(tok::identifier) && |
382 | Tok.getIdentifierInfo()->isStr("initializer")) { |
383 | ConsumeToken(); |
384 | } else { |
385 | Diag(Tok.getLocation(), diag::err_expected) << "'initializer'"; |
386 | TPA.Commit(); |
387 | IsCorrect = false; |
388 | break; |
389 | } |
390 | // Parse '('. |
391 | BalancedDelimiterTracker T(*this, tok::l_paren, |
392 | tok::annot_pragma_openmp_end); |
393 | IsCorrect = |
394 | !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") && |
395 | IsCorrect; |
396 | if (Tok.isNot(tok::annot_pragma_openmp_end)) { |
397 | ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope | |
398 | Scope::CompoundStmtScope | |
399 | Scope::OpenMPDirectiveScope); |
400 | // Parse expression. |
401 | VarDecl *OmpPrivParm = |
402 | Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), |
403 | D); |
404 | // Check if initializer is omp_priv <init_expr> or something else. |
405 | if (Tok.is(tok::identifier) && |
406 | Tok.getIdentifierInfo()->isStr("omp_priv")) { |
407 | ConsumeToken(); |
408 | ParseOpenMPReductionInitializerForDecl(OmpPrivParm); |
409 | } else { |
410 | InitializerResult = Actions.ActOnFinishFullExpr( |
411 | ParseAssignmentExpression().get(), D->getLocation(), |
412 | /*DiscardedValue*/ false); |
413 | } |
414 | Actions.ActOnOpenMPDeclareReductionInitializerEnd( |
415 | D, InitializerResult.get(), OmpPrivParm); |
416 | if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) && |
417 | Tok.isNot(tok::annot_pragma_openmp_end)) { |
418 | TPA.Commit(); |
419 | IsCorrect = false; |
420 | break; |
421 | } |
422 | IsCorrect = |
423 | !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid(); |
424 | } |
425 | } |
426 | |
427 | ++I; |
428 | // Revert parsing if not the last type, otherwise accept it, we're done with |
429 | // parsing. |
430 | if (I != E) |
431 | TPA.Revert(); |
432 | else |
433 | TPA.Commit(); |
434 | } |
435 | return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD, |
436 | IsCorrect); |
437 | } |
438 | |
439 | void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) { |
440 | // Parse declarator '=' initializer. |
441 | // If a '==' or '+=' is found, suggest a fixit to '='. |
442 | if (isTokenEqualOrEqualTypo()) { |
443 | ConsumeToken(); |
444 | |
445 | if (Tok.is(tok::code_completion)) { |
446 | cutOffParsing(); |
447 | Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm); |
448 | Actions.FinalizeDeclaration(OmpPrivParm); |
449 | return; |
450 | } |
451 | |
452 | PreferredType.enterVariableInit(Tok.getLocation(), OmpPrivParm); |
453 | ExprResult Init = ParseInitializer(); |
454 | |
455 | if (Init.isInvalid()) { |
456 | SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch); |
457 | Actions.ActOnInitializerError(OmpPrivParm); |
458 | } else { |
459 | Actions.AddInitializerToDecl(OmpPrivParm, Init.get(), |
460 | /*DirectInit=*/false); |
461 | } |
462 | } else if (Tok.is(tok::l_paren)) { |
463 | // Parse C++ direct initializer: '(' expression-list ')' |
464 | BalancedDelimiterTracker T(*this, tok::l_paren); |
465 | T.consumeOpen(); |
466 | |
467 | ExprVector Exprs; |
468 | CommaLocsTy CommaLocs; |
469 | |
470 | SourceLocation LParLoc = T.getOpenLocation(); |
471 | auto RunSignatureHelp = [this, OmpPrivParm, LParLoc, &Exprs]() { |
472 | QualType PreferredType = Actions.ProduceConstructorSignatureHelp( |
473 | OmpPrivParm->getType()->getCanonicalTypeInternal(), |
474 | OmpPrivParm->getLocation(), Exprs, LParLoc, /*Braced=*/false); |
475 | CalledSignatureHelp = true; |
476 | return PreferredType; |
477 | }; |
478 | if (ParseExpressionList(Exprs, CommaLocs, [&] { |
479 | PreferredType.enterFunctionArgument(Tok.getLocation(), |
480 | RunSignatureHelp); |
481 | })) { |
482 | if (PP.isCodeCompletionReached() && !CalledSignatureHelp) |
483 | RunSignatureHelp(); |
484 | Actions.ActOnInitializerError(OmpPrivParm); |
485 | SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch); |
486 | } else { |
487 | // Match the ')'. |
488 | SourceLocation RLoc = Tok.getLocation(); |
489 | if (!T.consumeClose()) |
490 | RLoc = T.getCloseLocation(); |
491 | |
492 | assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&(static_cast <bool> (!Exprs.empty() && Exprs.size () - 1 == CommaLocs.size() && "Unexpected number of commas!" ) ? void (0) : __assert_fail ("!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() && \"Unexpected number of commas!\"" , "clang/lib/Parse/ParseOpenMP.cpp", 493, __extension__ __PRETTY_FUNCTION__ )) |
493 | "Unexpected number of commas!")(static_cast <bool> (!Exprs.empty() && Exprs.size () - 1 == CommaLocs.size() && "Unexpected number of commas!" ) ? void (0) : __assert_fail ("!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() && \"Unexpected number of commas!\"" , "clang/lib/Parse/ParseOpenMP.cpp", 493, __extension__ __PRETTY_FUNCTION__ )); |
494 | |
495 | ExprResult Initializer = |
496 | Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs); |
497 | Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(), |
498 | /*DirectInit=*/true); |
499 | } |
500 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
501 | // Parse C++0x braced-init-list. |
502 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
503 | |
504 | ExprResult Init(ParseBraceInitializer()); |
505 | |
506 | if (Init.isInvalid()) { |
507 | Actions.ActOnInitializerError(OmpPrivParm); |
508 | } else { |
509 | Actions.AddInitializerToDecl(OmpPrivParm, Init.get(), |
510 | /*DirectInit=*/true); |
511 | } |
512 | } else { |
513 | Actions.ActOnUninitializedDecl(OmpPrivParm); |
514 | } |
515 | } |
516 | |
517 | /// Parses 'omp declare mapper' directive. |
518 | /// |
519 | /// declare-mapper-directive: |
520 | /// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifier> ':'] |
521 | /// <type> <var> ')' [<clause>[[,] <clause>] ... ] |
522 | /// annot_pragma_openmp_end |
523 | /// <mapper-identifier> and <var> are base language identifiers. |
524 | /// |
525 | Parser::DeclGroupPtrTy |
526 | Parser::ParseOpenMPDeclareMapperDirective(AccessSpecifier AS) { |
527 | bool IsCorrect = true; |
528 | // Parse '(' |
529 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
530 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
531 | getOpenMPDirectiveName(OMPD_declare_mapper).data())) { |
532 | SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); |
533 | return DeclGroupPtrTy(); |
534 | } |
535 | |
536 | // Parse <mapper-identifier> |
537 | auto &DeclNames = Actions.getASTContext().DeclarationNames; |
538 | DeclarationName MapperId; |
539 | if (PP.LookAhead(0).is(tok::colon)) { |
540 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) { |
541 | Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier); |
542 | IsCorrect = false; |
543 | } else { |
544 | MapperId = DeclNames.getIdentifier(Tok.getIdentifierInfo()); |
545 | } |
546 | ConsumeToken(); |
547 | // Consume ':'. |
548 | ExpectAndConsume(tok::colon); |
549 | } else { |
550 | // If no mapper identifier is provided, its name is "default" by default |
551 | MapperId = |
552 | DeclNames.getIdentifier(&Actions.getASTContext().Idents.get("default")); |
553 | } |
554 | |
555 | if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end)) |
556 | return DeclGroupPtrTy(); |
557 | |
558 | // Parse <type> <var> |
559 | DeclarationName VName; |
560 | QualType MapperType; |
561 | SourceRange Range; |
562 | TypeResult ParsedType = parseOpenMPDeclareMapperVarDecl(Range, VName, AS); |
563 | if (ParsedType.isUsable()) |
564 | MapperType = |
565 | Actions.ActOnOpenMPDeclareMapperType(Range.getBegin(), ParsedType); |
566 | if (MapperType.isNull()) |
567 | IsCorrect = false; |
568 | if (!IsCorrect) { |
569 | SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch); |
570 | return DeclGroupPtrTy(); |
571 | } |
572 | |
573 | // Consume ')'. |
574 | IsCorrect &= !T.consumeClose(); |
575 | if (!IsCorrect) { |
576 | SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch); |
577 | return DeclGroupPtrTy(); |
578 | } |
579 | |
580 | // Enter scope. |
581 | DeclarationNameInfo DirName; |
582 | SourceLocation Loc = Tok.getLocation(); |
583 | unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope | |
584 | Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope; |
585 | ParseScope OMPDirectiveScope(this, ScopeFlags); |
586 | Actions.StartOpenMPDSABlock(OMPD_declare_mapper, DirName, getCurScope(), Loc); |
587 | |
588 | // Add the mapper variable declaration. |
589 | ExprResult MapperVarRef = Actions.ActOnOpenMPDeclareMapperDirectiveVarDecl( |
590 | getCurScope(), MapperType, Range.getBegin(), VName); |
591 | |
592 | // Parse map clauses. |
593 | SmallVector<OMPClause *, 6> Clauses; |
594 | while (Tok.isNot(tok::annot_pragma_openmp_end)) { |
595 | OpenMPClauseKind CKind = Tok.isAnnotation() |
596 | ? OMPC_unknown |
597 | : getOpenMPClauseKind(PP.getSpelling(Tok)); |
598 | Actions.StartOpenMPClause(CKind); |
599 | OMPClause *Clause = |
600 | ParseOpenMPClause(OMPD_declare_mapper, CKind, Clauses.empty()); |
601 | if (Clause) |
602 | Clauses.push_back(Clause); |
603 | else |
604 | IsCorrect = false; |
605 | // Skip ',' if any. |
606 | if (Tok.is(tok::comma)) |
607 | ConsumeToken(); |
608 | Actions.EndOpenMPClause(); |
609 | } |
610 | if (Clauses.empty()) { |
611 | Diag(Tok, diag::err_omp_expected_clause) |
612 | << getOpenMPDirectiveName(OMPD_declare_mapper); |
613 | IsCorrect = false; |
614 | } |
615 | |
616 | // Exit scope. |
617 | Actions.EndOpenMPDSABlock(nullptr); |
618 | OMPDirectiveScope.Exit(); |
619 | DeclGroupPtrTy DG = Actions.ActOnOpenMPDeclareMapperDirective( |
620 | getCurScope(), Actions.getCurLexicalContext(), MapperId, MapperType, |
621 | Range.getBegin(), VName, AS, MapperVarRef.get(), Clauses); |
622 | if (!IsCorrect) |
623 | return DeclGroupPtrTy(); |
624 | |
625 | return DG; |
626 | } |
627 | |
628 | TypeResult Parser::parseOpenMPDeclareMapperVarDecl(SourceRange &Range, |
629 | DeclarationName &Name, |
630 | AccessSpecifier AS) { |
631 | // Parse the common declaration-specifiers piece. |
632 | Parser::DeclSpecContext DSC = Parser::DeclSpecContext::DSC_type_specifier; |
633 | DeclSpec DS(AttrFactory); |
634 | ParseSpecifierQualifierList(DS, AS, DSC); |
635 | |
636 | // Parse the declarator. |
637 | DeclaratorContext Context = DeclaratorContext::Prototype; |
638 | Declarator DeclaratorInfo(DS, Context); |
639 | ParseDeclarator(DeclaratorInfo); |
640 | Range = DeclaratorInfo.getSourceRange(); |
641 | if (DeclaratorInfo.getIdentifier() == nullptr) { |
642 | Diag(Tok.getLocation(), diag::err_omp_mapper_expected_declarator); |
643 | return true; |
644 | } |
645 | Name = Actions.GetNameForDeclarator(DeclaratorInfo).getName(); |
646 | |
647 | return Actions.ActOnOpenMPDeclareMapperVarDecl(getCurScope(), DeclaratorInfo); |
648 | } |
649 | |
650 | namespace { |
651 | /// RAII that recreates function context for correct parsing of clauses of |
652 | /// 'declare simd' construct. |
653 | /// OpenMP, 2.8.2 declare simd Construct |
654 | /// The expressions appearing in the clauses of this directive are evaluated in |
655 | /// the scope of the arguments of the function declaration or definition. |
656 | class FNContextRAII final { |
657 | Parser &P; |
658 | Sema::CXXThisScopeRAII *ThisScope; |
659 | Parser::MultiParseScope Scopes; |
660 | bool HasFunScope = false; |
661 | FNContextRAII() = delete; |
662 | FNContextRAII(const FNContextRAII &) = delete; |
663 | FNContextRAII &operator=(const FNContextRAII &) = delete; |
664 | |
665 | public: |
666 | FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P), Scopes(P) { |
667 | Decl *D = *Ptr.get().begin(); |
668 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
669 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
670 | Sema &Actions = P.getActions(); |
671 | |
672 | // Allow 'this' within late-parsed attributes. |
673 | ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, Qualifiers(), |
674 | ND && ND->isCXXInstanceMember()); |
675 | |
676 | // If the Decl is templatized, add template parameters to scope. |
677 | // FIXME: Track CurTemplateDepth? |
678 | P.ReenterTemplateScopes(Scopes, D); |
679 | |
680 | // If the Decl is on a function, add function parameters to the scope. |
681 | if (D->isFunctionOrFunctionTemplate()) { |
682 | HasFunScope = true; |
683 | Scopes.Enter(Scope::FnScope | Scope::DeclScope | |
684 | Scope::CompoundStmtScope); |
685 | Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D); |
686 | } |
687 | } |
688 | ~FNContextRAII() { |
689 | if (HasFunScope) |
690 | P.getActions().ActOnExitFunctionContext(); |
691 | delete ThisScope; |
692 | } |
693 | }; |
694 | } // namespace |
695 | |
696 | /// Parses clauses for 'declare simd' directive. |
697 | /// clause: |
698 | /// 'inbranch' | 'notinbranch' |
699 | /// 'simdlen' '(' <expr> ')' |
700 | /// { 'uniform' '(' <argument_list> ')' } |
701 | /// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' } |
702 | /// { 'linear '(' <argument_list> [ ':' <step> ] ')' } |
703 | static bool parseDeclareSimdClauses( |
704 | Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen, |
705 | SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds, |
706 | SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears, |
707 | SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) { |
708 | SourceRange BSRange; |
709 | const Token &Tok = P.getCurToken(); |
710 | bool IsError = false; |
711 | while (Tok.isNot(tok::annot_pragma_openmp_end)) { |
712 | if (Tok.isNot(tok::identifier)) |
713 | break; |
714 | OMPDeclareSimdDeclAttr::BranchStateTy Out; |
715 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
716 | StringRef ClauseName = II->getName(); |
717 | // Parse 'inranch|notinbranch' clauses. |
718 | if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) { |
719 | if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) { |
720 | P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch) |
721 | << ClauseName |
722 | << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange; |
723 | IsError = true; |
724 | } |
725 | BS = Out; |
726 | BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc()); |
727 | P.ConsumeToken(); |
728 | } else if (ClauseName.equals("simdlen")) { |
729 | if (SimdLen.isUsable()) { |
730 | P.Diag(Tok, diag::err_omp_more_one_clause) |
731 | << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0; |
732 | IsError = true; |
733 | } |
734 | P.ConsumeToken(); |
735 | SourceLocation RLoc; |
736 | SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc); |
737 | if (SimdLen.isInvalid()) |
738 | IsError = true; |
739 | } else { |
740 | OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName); |
741 | if (CKind == OMPC_uniform || CKind == OMPC_aligned || |
742 | CKind == OMPC_linear) { |
743 | Parser::OpenMPVarListDataTy Data; |
744 | SmallVectorImpl<Expr *> *Vars = &Uniforms; |
745 | if (CKind == OMPC_aligned) { |
746 | Vars = &Aligneds; |
747 | } else if (CKind == OMPC_linear) { |
748 | Data.ExtraModifier = OMPC_LINEAR_val; |
749 | Vars = &Linears; |
750 | } |
751 | |
752 | P.ConsumeToken(); |
753 | if (P.ParseOpenMPVarList(OMPD_declare_simd, |
754 | getOpenMPClauseKind(ClauseName), *Vars, Data)) |
755 | IsError = true; |
756 | if (CKind == OMPC_aligned) { |
757 | Alignments.append(Aligneds.size() - Alignments.size(), |
758 | Data.DepModOrTailExpr); |
759 | } else if (CKind == OMPC_linear) { |
760 | assert(0 <= Data.ExtraModifier &&(static_cast <bool> (0 <= Data.ExtraModifier && Data.ExtraModifier <= OMPC_LINEAR_unknown && "Unexpected linear modifier." ) ? void (0) : __assert_fail ("0 <= Data.ExtraModifier && Data.ExtraModifier <= OMPC_LINEAR_unknown && \"Unexpected linear modifier.\"" , "clang/lib/Parse/ParseOpenMP.cpp", 762, __extension__ __PRETTY_FUNCTION__ )) |
761 | Data.ExtraModifier <= OMPC_LINEAR_unknown &&(static_cast <bool> (0 <= Data.ExtraModifier && Data.ExtraModifier <= OMPC_LINEAR_unknown && "Unexpected linear modifier." ) ? void (0) : __assert_fail ("0 <= Data.ExtraModifier && Data.ExtraModifier <= OMPC_LINEAR_unknown && \"Unexpected linear modifier.\"" , "clang/lib/Parse/ParseOpenMP.cpp", 762, __extension__ __PRETTY_FUNCTION__ )) |
762 | "Unexpected linear modifier.")(static_cast <bool> (0 <= Data.ExtraModifier && Data.ExtraModifier <= OMPC_LINEAR_unknown && "Unexpected linear modifier." ) ? void (0) : __assert_fail ("0 <= Data.ExtraModifier && Data.ExtraModifier <= OMPC_LINEAR_unknown && \"Unexpected linear modifier.\"" , "clang/lib/Parse/ParseOpenMP.cpp", 762, __extension__ __PRETTY_FUNCTION__ )); |
763 | if (P.getActions().CheckOpenMPLinearModifier( |
764 | static_cast<OpenMPLinearClauseKind>(Data.ExtraModifier), |
765 | Data.ExtraModifierLoc)) |
766 | Data.ExtraModifier = OMPC_LINEAR_val; |
767 | LinModifiers.append(Linears.size() - LinModifiers.size(), |
768 | Data.ExtraModifier); |
769 | Steps.append(Linears.size() - Steps.size(), Data.DepModOrTailExpr); |
770 | } |
771 | } else |
772 | // TODO: add parsing of other clauses. |
773 | break; |
774 | } |
775 | // Skip ',' if any. |
776 | if (Tok.is(tok::comma)) |
777 | P.ConsumeToken(); |
778 | } |
779 | return IsError; |
780 | } |
781 | |
782 | /// Parse clauses for '#pragma omp declare simd'. |
783 | Parser::DeclGroupPtrTy |
784 | Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr, |
785 | CachedTokens &Toks, SourceLocation Loc) { |
786 | PP.EnterToken(Tok, /*IsReinject*/ true); |
787 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true, |
788 | /*IsReinject*/ true); |
789 | // Consume the previously pushed token. |
790 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
791 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
792 | |
793 | FNContextRAII FnContext(*this, Ptr); |
794 | OMPDeclareSimdDeclAttr::BranchStateTy BS = |
795 | OMPDeclareSimdDeclAttr::BS_Undefined; |
796 | ExprResult Simdlen; |
797 | SmallVector<Expr *, 4> Uniforms; |
798 | SmallVector<Expr *, 4> Aligneds; |
799 | SmallVector<Expr *, 4> Alignments; |
800 | SmallVector<Expr *, 4> Linears; |
801 | SmallVector<unsigned, 4> LinModifiers; |
802 | SmallVector<Expr *, 4> Steps; |
803 | bool IsError = |
804 | parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds, |
805 | Alignments, Linears, LinModifiers, Steps); |
806 | skipUntilPragmaOpenMPEnd(OMPD_declare_simd); |
807 | // Skip the last annot_pragma_openmp_end. |
808 | SourceLocation EndLoc = ConsumeAnnotationToken(); |
809 | if (IsError) |
810 | return Ptr; |
811 | return Actions.ActOnOpenMPDeclareSimdDirective( |
812 | Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears, |
813 | LinModifiers, Steps, SourceRange(Loc, EndLoc)); |
814 | } |
815 | |
816 | namespace { |
817 | /// Constant used in the diagnostics to distinguish the levels in an OpenMP |
818 | /// contexts: selector-set={selector(trait, ...), ...}, .... |
819 | enum OMPContextLvl { |
820 | CONTEXT_SELECTOR_SET_LVL = 0, |
821 | CONTEXT_SELECTOR_LVL = 1, |
822 | CONTEXT_TRAIT_LVL = 2, |
823 | }; |
824 | |
825 | static StringRef stringLiteralParser(Parser &P) { |
826 | ExprResult Res = P.ParseStringLiteralExpression(true); |
827 | return Res.isUsable() ? Res.getAs<StringLiteral>()->getString() : ""; |
828 | } |
829 | |
830 | static StringRef getNameFromIdOrString(Parser &P, Token &Tok, |
831 | OMPContextLvl Lvl) { |
832 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_for)) { |
833 | llvm::SmallString<16> Buffer; |
834 | StringRef Name = P.getPreprocessor().getSpelling(Tok, Buffer); |
835 | (void)P.ConsumeToken(); |
836 | return Name; |
837 | } |
838 | |
839 | if (tok::isStringLiteral(Tok.getKind())) |
840 | return stringLiteralParser(P); |
841 | |
842 | P.Diag(Tok.getLocation(), |
843 | diag::warn_omp_declare_variant_string_literal_or_identifier) |
844 | << Lvl; |
845 | return ""; |
846 | } |
847 | |
848 | static bool checkForDuplicates(Parser &P, StringRef Name, |
849 | SourceLocation NameLoc, |
850 | llvm::StringMap<SourceLocation> &Seen, |
851 | OMPContextLvl Lvl) { |
852 | auto Res = Seen.try_emplace(Name, NameLoc); |
853 | if (Res.second) |
854 | return false; |
855 | |
856 | // Each trait-set-selector-name, trait-selector-name and trait-name can |
857 | // only be specified once. |
858 | P.Diag(NameLoc, diag::warn_omp_declare_variant_ctx_mutiple_use) |
859 | << Lvl << Name; |
860 | P.Diag(Res.first->getValue(), diag::note_omp_declare_variant_ctx_used_here) |
861 | << Lvl << Name; |
862 | return true; |
863 | } |
864 | } // namespace |
865 | |
866 | void Parser::parseOMPTraitPropertyKind(OMPTraitProperty &TIProperty, |
867 | llvm::omp::TraitSet Set, |
868 | llvm::omp::TraitSelector Selector, |
869 | llvm::StringMap<SourceLocation> &Seen) { |
870 | TIProperty.Kind = TraitProperty::invalid; |
871 | |
872 | SourceLocation NameLoc = Tok.getLocation(); |
873 | StringRef Name = getNameFromIdOrString(*this, Tok, CONTEXT_TRAIT_LVL); |
874 | if (Name.empty()) { |
875 | Diag(Tok.getLocation(), diag::note_omp_declare_variant_ctx_options) |
876 | << CONTEXT_TRAIT_LVL << listOpenMPContextTraitProperties(Set, Selector); |
877 | return; |
878 | } |
879 | |
880 | TIProperty.RawString = Name; |
881 | TIProperty.Kind = getOpenMPContextTraitPropertyKind(Set, Selector, Name); |
882 | if (TIProperty.Kind != TraitProperty::invalid) { |
883 | if (checkForDuplicates(*this, Name, NameLoc, Seen, CONTEXT_TRAIT_LVL)) |
884 | TIProperty.Kind = TraitProperty::invalid; |
885 | return; |
886 | } |
887 | |
888 | // It follows diagnosis and helping notes. |
889 | // FIXME: We should move the diagnosis string generation into libFrontend. |
890 | Diag(NameLoc, diag::warn_omp_declare_variant_ctx_not_a_property) |
891 | << Name << getOpenMPContextTraitSelectorName(Selector) |
892 | << getOpenMPContextTraitSetName(Set); |
893 | |
894 | TraitSet SetForName = getOpenMPContextTraitSetKind(Name); |
895 | if (SetForName != TraitSet::invalid) { |
896 | Diag(NameLoc, diag::note_omp_declare_variant_ctx_is_a) |
897 | << Name << CONTEXT_SELECTOR_SET_LVL << CONTEXT_TRAIT_LVL; |
898 | Diag(NameLoc, diag::note_omp_declare_variant_ctx_try) |
899 | << Name << "<selector-name>" |
900 | << "(<property-name>)"; |
901 | return; |
902 | } |
903 | TraitSelector SelectorForName = getOpenMPContextTraitSelectorKind(Name); |
904 | if (SelectorForName != TraitSelector::invalid) { |
905 | Diag(NameLoc, diag::note_omp_declare_variant_ctx_is_a) |
906 | << Name << CONTEXT_SELECTOR_LVL << CONTEXT_TRAIT_LVL; |
907 | bool AllowsTraitScore = false; |
908 | bool RequiresProperty = false; |
909 | isValidTraitSelectorForTraitSet( |
910 | SelectorForName, getOpenMPContextTraitSetForSelector(SelectorForName), |
911 | AllowsTraitScore, RequiresProperty); |
912 | Diag(NameLoc, diag::note_omp_declare_variant_ctx_try) |
913 | << getOpenMPContextTraitSetName( |
914 | getOpenMPContextTraitSetForSelector(SelectorForName)) |
915 | << Name << (RequiresProperty ? "(<property-name>)" : ""); |
916 | return; |
917 | } |
918 | for (const auto &PotentialSet : |
919 | {TraitSet::construct, TraitSet::user, TraitSet::implementation, |
920 | TraitSet::device}) { |
921 | TraitProperty PropertyForName = |
922 | getOpenMPContextTraitPropertyKind(PotentialSet, Selector, Name); |
923 | if (PropertyForName == TraitProperty::invalid) |
924 | continue; |
925 | Diag(NameLoc, diag::note_omp_declare_variant_ctx_try) |
926 | << getOpenMPContextTraitSetName( |
927 | getOpenMPContextTraitSetForProperty(PropertyForName)) |
928 | << getOpenMPContextTraitSelectorName( |
929 | getOpenMPContextTraitSelectorForProperty(PropertyForName)) |
930 | << ("(" + Name + ")").str(); |
931 | return; |
932 | } |
933 | Diag(NameLoc, diag::note_omp_declare_variant_ctx_options) |
934 | << CONTEXT_TRAIT_LVL << listOpenMPContextTraitProperties(Set, Selector); |
935 | } |
936 | |
937 | static bool checkExtensionProperty(Parser &P, SourceLocation Loc, |
938 | OMPTraitProperty &TIProperty, |
939 | OMPTraitSelector &TISelector, |
940 | llvm::StringMap<SourceLocation> &Seen) { |
941 | assert(TISelector.Kind ==(static_cast <bool> (TISelector.Kind == llvm::omp::TraitSelector ::implementation_extension && "Only for extension properties, e.g., " "`implementation={extension(PROPERTY)}`") ? void (0) : __assert_fail ("TISelector.Kind == llvm::omp::TraitSelector::implementation_extension && \"Only for extension properties, e.g., \" \"`implementation={extension(PROPERTY)}`\"" , "clang/lib/Parse/ParseOpenMP.cpp", 944, __extension__ __PRETTY_FUNCTION__ )) |
942 | llvm::omp::TraitSelector::implementation_extension &&(static_cast <bool> (TISelector.Kind == llvm::omp::TraitSelector ::implementation_extension && "Only for extension properties, e.g., " "`implementation={extension(PROPERTY)}`") ? void (0) : __assert_fail ("TISelector.Kind == llvm::omp::TraitSelector::implementation_extension && \"Only for extension properties, e.g., \" \"`implementation={extension(PROPERTY)}`\"" , "clang/lib/Parse/ParseOpenMP.cpp", 944, __extension__ __PRETTY_FUNCTION__ )) |
943 | "Only for extension properties, e.g., "(static_cast <bool> (TISelector.Kind == llvm::omp::TraitSelector ::implementation_extension && "Only for extension properties, e.g., " "`implementation={extension(PROPERTY)}`") ? void (0) : __assert_fail ("TISelector.Kind == llvm::omp::TraitSelector::implementation_extension && \"Only for extension properties, e.g., \" \"`implementation={extension(PROPERTY)}`\"" , "clang/lib/Parse/ParseOpenMP.cpp", 944, __extension__ __PRETTY_FUNCTION__ )) |
944 | "`implementation={extension(PROPERTY)}`")(static_cast <bool> (TISelector.Kind == llvm::omp::TraitSelector ::implementation_extension && "Only for extension properties, e.g., " "`implementation={extension(PROPERTY)}`") ? void (0) : __assert_fail ("TISelector.Kind == llvm::omp::TraitSelector::implementation_extension && \"Only for extension properties, e.g., \" \"`implementation={extension(PROPERTY)}`\"" , "clang/lib/Parse/ParseOpenMP.cpp", 944, __extension__ __PRETTY_FUNCTION__ )); |
945 | if (TIProperty.Kind == TraitProperty::invalid) |
946 | return false; |
947 | |
948 | if (TIProperty.Kind == |
949 | TraitProperty::implementation_extension_disable_implicit_base) |
950 | return true; |
951 | |
952 | if (TIProperty.Kind == |
953 | TraitProperty::implementation_extension_allow_templates) |
954 | return true; |
955 | |
956 | auto IsMatchExtension = [](OMPTraitProperty &TP) { |
957 | return (TP.Kind == |
958 | llvm::omp::TraitProperty::implementation_extension_match_all || |
959 | TP.Kind == |
960 | llvm::omp::TraitProperty::implementation_extension_match_any || |
961 | TP.Kind == |
962 | llvm::omp::TraitProperty::implementation_extension_match_none); |
963 | }; |
964 | |
965 | if (IsMatchExtension(TIProperty)) { |
966 | for (OMPTraitProperty &SeenProp : TISelector.Properties) |
967 | if (IsMatchExtension(SeenProp)) { |
968 | P.Diag(Loc, diag::err_omp_variant_ctx_second_match_extension); |
969 | StringRef SeenName = llvm::omp::getOpenMPContextTraitPropertyName( |
970 | SeenProp.Kind, SeenProp.RawString); |
971 | SourceLocation SeenLoc = Seen[SeenName]; |
972 | P.Diag(SeenLoc, diag::note_omp_declare_variant_ctx_used_here) |
973 | << CONTEXT_TRAIT_LVL << SeenName; |
974 | return false; |
975 | } |
976 | return true; |
977 | } |
978 | |
979 | llvm_unreachable("Unknown extension property!")::llvm::llvm_unreachable_internal("Unknown extension property!" , "clang/lib/Parse/ParseOpenMP.cpp", 979); |
980 | } |
981 | |
982 | void Parser::parseOMPContextProperty(OMPTraitSelector &TISelector, |
983 | llvm::omp::TraitSet Set, |
984 | llvm::StringMap<SourceLocation> &Seen) { |
985 | assert(TISelector.Kind != TraitSelector::user_condition &&(static_cast <bool> (TISelector.Kind != TraitSelector:: user_condition && "User conditions are special properties not handled here!" ) ? void (0) : __assert_fail ("TISelector.Kind != TraitSelector::user_condition && \"User conditions are special properties not handled here!\"" , "clang/lib/Parse/ParseOpenMP.cpp", 986, __extension__ __PRETTY_FUNCTION__ )) |
986 | "User conditions are special properties not handled here!")(static_cast <bool> (TISelector.Kind != TraitSelector:: user_condition && "User conditions are special properties not handled here!" ) ? void (0) : __assert_fail ("TISelector.Kind != TraitSelector::user_condition && \"User conditions are special properties not handled here!\"" , "clang/lib/Parse/ParseOpenMP.cpp", 986, __extension__ __PRETTY_FUNCTION__ )); |
987 | |
988 | SourceLocation PropertyLoc = Tok.getLocation(); |
989 | OMPTraitProperty TIProperty; |
990 | parseOMPTraitPropertyKind(TIProperty, Set, TISelector.Kind, Seen); |
991 | |
992 | if (TISelector.Kind == llvm::omp::TraitSelector::implementation_extension) |
993 | if (!checkExtensionProperty(*this, Tok.getLocation(), TIProperty, |
994 | TISelector, Seen)) |
995 | TIProperty.Kind = TraitProperty::invalid; |
996 | |
997 | // If we have an invalid property here we already issued a warning. |
998 | if (TIProperty.Kind == TraitProperty::invalid) { |
999 | if (PropertyLoc != Tok.getLocation()) |
1000 | Diag(Tok.getLocation(), diag::note_omp_declare_variant_ctx_continue_here) |
1001 | << CONTEXT_TRAIT_LVL; |
1002 | return; |
1003 | } |
1004 | |
1005 | if (isValidTraitPropertyForTraitSetAndSelector(TIProperty.Kind, |
1006 | TISelector.Kind, Set)) { |
1007 | |
1008 | // If we make it here the property, selector, set, score, condition, ... are |
1009 | // all valid (or have been corrected). Thus we can record the property. |
1010 | TISelector.Properties.push_back(TIProperty); |
1011 | return; |
1012 | } |
1013 | |
1014 | Diag(PropertyLoc, diag::warn_omp_ctx_incompatible_property_for_selector) |
1015 | << getOpenMPContextTraitPropertyName(TIProperty.Kind, |
1016 | TIProperty.RawString) |
1017 | << getOpenMPContextTraitSelectorName(TISelector.Kind) |
1018 | << getOpenMPContextTraitSetName(Set); |
1019 | Diag(PropertyLoc, diag::note_omp_ctx_compatible_set_and_selector_for_property) |
1020 | << getOpenMPContextTraitPropertyName(TIProperty.Kind, |
1021 | TIProperty.RawString) |
1022 | << getOpenMPContextTraitSelectorName( |
1023 | getOpenMPContextTraitSelectorForProperty(TIProperty.Kind)) |
1024 | << getOpenMPContextTraitSetName( |
1025 | getOpenMPContextTraitSetForProperty(TIProperty.Kind)); |
1026 | Diag(Tok.getLocation(), diag::note_omp_declare_variant_ctx_continue_here) |
1027 | << CONTEXT_TRAIT_LVL; |
1028 | } |
1029 | |
1030 | void Parser::parseOMPTraitSelectorKind(OMPTraitSelector &TISelector, |
1031 | llvm::omp::TraitSet Set, |
1032 | llvm::StringMap<SourceLocation> &Seen) { |
1033 | TISelector.Kind = TraitSelector::invalid; |
1034 | |
1035 | SourceLocation NameLoc = Tok.getLocation(); |
1036 | StringRef Name = getNameFromIdOrString(*this, Tok, CONTEXT_SELECTOR_LVL); |
1037 | if (Name.empty()) { |
1038 | Diag(Tok.getLocation(), diag::note_omp_declare_variant_ctx_options) |
1039 | << CONTEXT_SELECTOR_LVL << listOpenMPContextTraitSelectors(Set); |
1040 | return; |
1041 | } |
1042 | |
1043 | TISelector.Kind = getOpenMPContextTraitSelectorKind(Name); |
1044 | if (TISelector.Kind != TraitSelector::invalid) { |
1045 | if (checkForDuplicates(*this, Name, NameLoc, Seen, CONTEXT_SELECTOR_LVL)) |
1046 | TISelector.Kind = TraitSelector::invalid; |
1047 | return; |
1048 | } |
1049 | |
1050 | // It follows diagnosis and helping notes. |
1051 | Diag(NameLoc, diag::warn_omp_declare_variant_ctx_not_a_selector) |
1052 | << Name << getOpenMPContextTraitSetName(Set); |
1053 | |
1054 | TraitSet SetForName = getOpenMPContextTraitSetKind(Name); |
1055 | if (SetForName != TraitSet::invalid) { |
1056 | Diag(NameLoc, diag::note_omp_declare_variant_ctx_is_a) |
1057 | << Name << CONTEXT_SELECTOR_SET_LVL << CONTEXT_SELECTOR_LVL; |
1058 | Diag(NameLoc, diag::note_omp_declare_variant_ctx_try) |
1059 | << Name << "<selector-name>" |
1060 | << "<property-name>"; |
1061 | return; |
1062 | } |
1063 | for (const auto &PotentialSet : |
1064 | {TraitSet::construct, TraitSet::user, TraitSet::implementation, |
1065 | TraitSet::device}) { |
1066 | TraitProperty PropertyForName = getOpenMPContextTraitPropertyKind( |
1067 | PotentialSet, TraitSelector::invalid, Name); |
1068 | if (PropertyForName == TraitProperty::invalid) |
1069 | continue; |
1070 | Diag(NameLoc, diag::note_omp_declare_variant_ctx_is_a) |
1071 | << Name << CONTEXT_TRAIT_LVL << CONTEXT_SELECTOR_LVL; |
1072 | Diag(NameLoc, diag::note_omp_declare_variant_ctx_try) |
1073 | << getOpenMPContextTraitSetName( |
1074 | getOpenMPContextTraitSetForProperty(PropertyForName)) |
1075 | << getOpenMPContextTraitSelectorName( |
1076 | getOpenMPContextTraitSelectorForProperty(PropertyForName)) |
1077 | << ("(" + Name + ")").str(); |
1078 | return; |
1079 | } |
1080 | Diag(NameLoc, diag::note_omp_declare_variant_ctx_options) |
1081 | << CONTEXT_SELECTOR_LVL << listOpenMPContextTraitSelectors(Set); |
1082 | } |
1083 | |
1084 | /// Parse optional 'score' '(' <expr> ')' ':'. |
1085 | static ExprResult parseContextScore(Parser &P) { |
1086 | ExprResult ScoreExpr; |
1087 | llvm::SmallString<16> Buffer; |
1088 | StringRef SelectorName = |
1089 | P.getPreprocessor().getSpelling(P.getCurToken(), Buffer); |
1090 | if (!SelectorName.equals("score")) |
1091 | return ScoreExpr; |
1092 | (void)P.ConsumeToken(); |
1093 | SourceLocation RLoc; |
1094 | ScoreExpr = P.ParseOpenMPParensExpr(SelectorName, RLoc); |
1095 | // Parse ':' |
1096 | if (P.getCurToken().is(tok::colon)) |
1097 | (void)P.ConsumeAnyToken(); |
1098 | else |
1099 | P.Diag(P.getCurToken(), diag::warn_omp_declare_variant_expected) |
1100 | << "':'" |
1101 | << "score expression"; |
1102 | return ScoreExpr; |
1103 | } |
1104 | |
1105 | /// Parses an OpenMP context selector. |
1106 | /// |
1107 | /// <trait-selector-name> ['('[<trait-score>] <trait-property> [, <t-p>]* ')'] |
1108 | void Parser::parseOMPContextSelector( |
1109 | OMPTraitSelector &TISelector, llvm::omp::TraitSet Set, |
1110 | llvm::StringMap<SourceLocation> &SeenSelectors) { |
1111 | unsigned short OuterPC = ParenCount; |
1112 | |
1113 | // If anything went wrong we issue an error or warning and then skip the rest |
1114 | // of the selector. However, commas are ambiguous so we look for the nesting |
1115 | // of parentheses here as well. |
1116 | auto FinishSelector = [OuterPC, this]() -> void { |
1117 | bool Done = false; |
1118 | while (!Done) { |
1119 | while (!SkipUntil({tok::r_brace, tok::r_paren, tok::comma, |
1120 | tok::annot_pragma_openmp_end}, |
1121 | StopBeforeMatch)) |
1122 | ; |
1123 | if (Tok.is(tok::r_paren) && OuterPC > ParenCount) |
1124 | (void)ConsumeParen(); |
1125 | if (OuterPC <= ParenCount) { |
1126 | Done = true; |
1127 | break; |
1128 | } |
1129 | if (!Tok.is(tok::comma) && !Tok.is(tok::r_paren)) { |
1130 | Done = true; |
1131 | break; |
1132 | } |
1133 | (void)ConsumeAnyToken(); |
1134 | } |
1135 | Diag(Tok.getLocation(), diag::note_omp_declare_variant_ctx_continue_here) |
1136 | << CONTEXT_SELECTOR_LVL; |
1137 | }; |
1138 | |
1139 | SourceLocation SelectorLoc = Tok.getLocation(); |
1140 | parseOMPTraitSelectorKind(TISelector, Set, SeenSelectors); |
1141 | if (TISelector.Kind == TraitSelector::invalid) |
1142 | return FinishSelector(); |
1143 | |
1144 | bool AllowsTraitScore = false; |
1145 | bool RequiresProperty = false; |
1146 | if (!isValidTraitSelectorForTraitSet(TISelector.Kind, Set, AllowsTraitScore, |
1147 | RequiresProperty)) { |
1148 | Diag(SelectorLoc, diag::warn_omp_ctx_incompatible_selector_for_set) |
1149 | << getOpenMPContextTraitSelectorName(TISelector.Kind) |
1150 | << getOpenMPContextTraitSetName(Set); |
1151 | Diag(SelectorLoc, diag::note_omp_ctx_compatible_set_for_selector) |
1152 | << getOpenMPContextTraitSelectorName(TISelector.Kind) |
1153 | << getOpenMPContextTraitSetName( |
1154 | getOpenMPContextTraitSetForSelector(TISelector.Kind)) |
1155 | << RequiresProperty; |
1156 | return FinishSelector(); |
1157 | } |
1158 | |
1159 | if (!RequiresProperty) { |
1160 | TISelector.Properties.push_back( |
1161 | {getOpenMPContextTraitPropertyForSelector(TISelector.Kind), |
1162 | getOpenMPContextTraitSelectorName(TISelector.Kind)}); |
1163 | return; |
1164 | } |
1165 | |
1166 | if (!Tok.is(tok::l_paren)) { |
1167 | Diag(SelectorLoc, diag::warn_omp_ctx_selector_without_properties) |
1168 | << getOpenMPContextTraitSelectorName(TISelector.Kind) |
1169 | << getOpenMPContextTraitSetName(Set); |
1170 | return FinishSelector(); |
1171 | } |
1172 | |
1173 | if (TISelector.Kind == TraitSelector::user_condition) { |
1174 | SourceLocation RLoc; |
1175 | ExprResult Condition = ParseOpenMPParensExpr("user condition", RLoc); |
1176 | if (!Condition.isUsable()) |
1177 | return FinishSelector(); |
1178 | TISelector.ScoreOrCondition = Condition.get(); |
1179 | TISelector.Properties.push_back( |
1180 | {TraitProperty::user_condition_unknown, "<condition>"}); |
1181 | return; |
1182 | } |
1183 | |
1184 | BalancedDelimiterTracker BDT(*this, tok::l_paren, |
1185 | tok::annot_pragma_openmp_end); |
1186 | // Parse '('. |
1187 | (void)BDT.consumeOpen(); |
1188 | |
1189 | SourceLocation ScoreLoc = Tok.getLocation(); |
1190 | ExprResult Score = parseContextScore(*this); |
1191 | |
1192 | if (!AllowsTraitScore && !Score.isUnset()) { |
1193 | if (Score.isUsable()) { |
1194 | Diag(ScoreLoc, diag::warn_omp_ctx_incompatible_score_for_property) |
1195 | << getOpenMPContextTraitSelectorName(TISelector.Kind) |
1196 | << getOpenMPContextTraitSetName(Set) << Score.get(); |
1197 | } else { |
1198 | Diag(ScoreLoc, diag::warn_omp_ctx_incompatible_score_for_property) |
1199 | << getOpenMPContextTraitSelectorName(TISelector.Kind) |
1200 | << getOpenMPContextTraitSetName(Set) << "<invalid>"; |
1201 | } |
1202 | Score = ExprResult(); |
1203 | } |
1204 | |
1205 | if (Score.isUsable()) |
1206 | TISelector.ScoreOrCondition = Score.get(); |
1207 | |
1208 | llvm::StringMap<SourceLocation> SeenProperties; |
1209 | do { |
1210 | parseOMPContextProperty(TISelector, Set, SeenProperties); |
1211 | } while (TryConsumeToken(tok::comma)); |
1212 | |
1213 | // Parse ')'. |
1214 | BDT.consumeClose(); |
1215 | } |
1216 | |
1217 | void Parser::parseOMPTraitSetKind(OMPTraitSet &TISet, |
1218 | llvm::StringMap<SourceLocation> &Seen) { |
1219 | TISet.Kind = TraitSet::invalid; |
1220 | |
1221 | SourceLocation NameLoc = Tok.getLocation(); |
1222 | StringRef Name = getNameFromIdOrString(*this, Tok, CONTEXT_SELECTOR_SET_LVL); |
1223 | if (Name.empty()) { |
1224 | Diag(Tok.getLocation(), diag::note_omp_declare_variant_ctx_options) |
1225 | << CONTEXT_SELECTOR_SET_LVL << listOpenMPContextTraitSets(); |
1226 | return; |
1227 | } |
1228 | |
1229 | TISet.Kind = getOpenMPContextTraitSetKind(Name); |
1230 | if (TISet.Kind != TraitSet::invalid) { |
1231 | if (checkForDuplicates(*this, Name, NameLoc, Seen, |
1232 | CONTEXT_SELECTOR_SET_LVL)) |
1233 | TISet.Kind = TraitSet::invalid; |
1234 | return; |
1235 | } |
1236 | |
1237 | // It follows diagnosis and helping notes. |
1238 | Diag(NameLoc, diag::warn_omp_declare_variant_ctx_not_a_set) << Name; |
1239 | |
1240 | TraitSelector SelectorForName = getOpenMPContextTraitSelectorKind(Name); |
1241 | if (SelectorForName != TraitSelector::invalid) { |
1242 | Diag(NameLoc, diag::note_omp_declare_variant_ctx_is_a) |
1243 | << Name << CONTEXT_SELECTOR_LVL << CONTEXT_SELECTOR_SET_LVL; |
1244 | bool AllowsTraitScore = false; |
1245 | bool RequiresProperty = false; |
1246 | isValidTraitSelectorForTraitSet( |
1247 | SelectorForName, getOpenMPContextTraitSetForSelector(SelectorForName), |
1248 | AllowsTraitScore, RequiresProperty); |
1249 | Diag(NameLoc, diag::note_omp_declare_variant_ctx_try) |
1250 | << getOpenMPContextTraitSetName( |
1251 | getOpenMPContextTraitSetForSelector(SelectorForName)) |
1252 | << Name << (RequiresProperty ? "(<property-name>)" : ""); |
1253 | return; |
1254 | } |
1255 | for (const auto &PotentialSet : |
1256 | {TraitSet::construct, TraitSet::user, TraitSet::implementation, |
1257 | TraitSet::device}) { |
1258 | TraitProperty PropertyForName = getOpenMPContextTraitPropertyKind( |
1259 | PotentialSet, TraitSelector::invalid, Name); |
1260 | if (PropertyForName == TraitProperty::invalid) |
1261 | continue; |
1262 | Diag(NameLoc, diag::note_omp_declare_variant_ctx_is_a) |
1263 | << Name << CONTEXT_TRAIT_LVL << CONTEXT_SELECTOR_SET_LVL; |
1264 | Diag(NameLoc, diag::note_omp_declare_variant_ctx_try) |
1265 | << getOpenMPContextTraitSetName( |
1266 | getOpenMPContextTraitSetForProperty(PropertyForName)) |
1267 | << getOpenMPContextTraitSelectorName( |
1268 | getOpenMPContextTraitSelectorForProperty(PropertyForName)) |
1269 | << ("(" + Name + ")").str(); |
1270 | return; |
1271 | } |
1272 | Diag(NameLoc, diag::note_omp_declare_variant_ctx_options) |
1273 | << CONTEXT_SELECTOR_SET_LVL << listOpenMPContextTraitSets(); |
1274 | } |
1275 | |
1276 | /// Parses an OpenMP context selector set. |
1277 | /// |
1278 | /// <trait-set-selector-name> '=' '{' <trait-selector> [, <trait-selector>]* '}' |
1279 | void Parser::parseOMPContextSelectorSet( |
1280 | OMPTraitSet &TISet, llvm::StringMap<SourceLocation> &SeenSets) { |
1281 | auto OuterBC = BraceCount; |
1282 | |
1283 | // If anything went wrong we issue an error or warning and then skip the rest |
1284 | // of the set. However, commas are ambiguous so we look for the nesting |
1285 | // of braces here as well. |
1286 | auto FinishSelectorSet = [this, OuterBC]() -> void { |
1287 | bool Done = false; |
1288 | while (!Done) { |
1289 | while (!SkipUntil({tok::comma, tok::r_brace, tok::r_paren, |
1290 | tok::annot_pragma_openmp_end}, |
1291 | StopBeforeMatch)) |
1292 | ; |
1293 | if (Tok.is(tok::r_brace) && OuterBC > BraceCount) |
1294 | (void)ConsumeBrace(); |
1295 | if (OuterBC <= BraceCount) { |
1296 | Done = true; |
1297 | break; |
1298 | } |
1299 | if (!Tok.is(tok::comma) && !Tok.is(tok::r_brace)) { |
1300 | Done = true; |
1301 | break; |
1302 | } |
1303 | (void)ConsumeAnyToken(); |
1304 | } |
1305 | Diag(Tok.getLocation(), diag::note_omp_declare_variant_ctx_continue_here) |
1306 | << CONTEXT_SELECTOR_SET_LVL; |
1307 | }; |
1308 | |
1309 | parseOMPTraitSetKind(TISet, SeenSets); |
1310 | if (TISet.Kind == TraitSet::invalid) |
1311 | return FinishSelectorSet(); |
1312 | |
1313 | // Parse '='. |
1314 | if (!TryConsumeToken(tok::equal)) |
1315 | Diag(Tok.getLocation(), diag::warn_omp_declare_variant_expected) |
1316 | << "=" |
1317 | << ("context set name \"" + getOpenMPContextTraitSetName(TISet.Kind) + |
1318 | "\"") |
1319 | .str(); |
1320 | |
1321 | // Parse '{'. |
1322 | if (Tok.is(tok::l_brace)) { |
1323 | (void)ConsumeBrace(); |
1324 | } else { |
1325 | Diag(Tok.getLocation(), diag::warn_omp_declare_variant_expected) |
1326 | << "{" |
1327 | << ("'=' that follows the context set name \"" + |
1328 | getOpenMPContextTraitSetName(TISet.Kind) + "\"") |
1329 | .str(); |
1330 | } |
1331 | |
1332 | llvm::StringMap<SourceLocation> SeenSelectors; |
1333 | do { |
1334 | OMPTraitSelector TISelector; |
1335 | parseOMPContextSelector(TISelector, TISet.Kind, SeenSelectors); |
1336 | if (TISelector.Kind != TraitSelector::invalid && |
1337 | !TISelector.Properties.empty()) |
1338 | TISet.Selectors.push_back(TISelector); |
1339 | } while (TryConsumeToken(tok::comma)); |
1340 | |
1341 | // Parse '}'. |
1342 | if (Tok.is(tok::r_brace)) { |
1343 | (void)ConsumeBrace(); |
1344 | } else { |
1345 | Diag(Tok.getLocation(), diag::warn_omp_declare_variant_expected) |
1346 | << "}" |
1347 | << ("context selectors for the context set \"" + |
1348 | getOpenMPContextTraitSetName(TISet.Kind) + "\"") |
1349 | .str(); |
1350 | } |
1351 | } |
1352 | |
1353 | /// Parse OpenMP context selectors: |
1354 | /// |
1355 | /// <trait-set-selector> [, <trait-set-selector>]* |
1356 | bool Parser::parseOMPContextSelectors(SourceLocation Loc, OMPTraitInfo &TI) { |
1357 | llvm::StringMap<SourceLocation> SeenSets; |
1358 | do { |
1359 | OMPTraitSet TISet; |
1360 | parseOMPContextSelectorSet(TISet, SeenSets); |
1361 | if (TISet.Kind != TraitSet::invalid && !TISet.Selectors.empty()) |
1362 | TI.Sets.push_back(TISet); |
1363 | } while (TryConsumeToken(tok::comma)); |
1364 | |
1365 | return false; |
1366 | } |
1367 | |
1368 | /// Parse clauses for '#pragma omp declare variant ( variant-func-id ) clause'. |
1369 | void Parser::ParseOMPDeclareVariantClauses(Parser::DeclGroupPtrTy Ptr, |
1370 | CachedTokens &Toks, |
1371 | SourceLocation Loc) { |
1372 | PP.EnterToken(Tok, /*IsReinject*/ true); |
1373 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true, |
1374 | /*IsReinject*/ true); |
1375 | // Consume the previously pushed token. |
1376 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
1377 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
1378 | |
1379 | FNContextRAII FnContext(*this, Ptr); |
1380 | // Parse function declaration id. |
1381 | SourceLocation RLoc; |
1382 | // Parse with IsAddressOfOperand set to true to parse methods as DeclRefExprs |
1383 | // instead of MemberExprs. |
1384 | ExprResult AssociatedFunction; |
1385 | { |
1386 | // Do not mark function as is used to prevent its emission if this is the |
1387 | // only place where it is used. |
1388 | EnterExpressionEvaluationContext Unevaluated( |
1389 | Actions, Sema::ExpressionEvaluationContext::Unevaluated); |
1390 | AssociatedFunction = ParseOpenMPParensExpr( |
1391 | getOpenMPDirectiveName(OMPD_declare_variant), RLoc, |
1392 | /*IsAddressOfOperand=*/true); |
1393 | } |
1394 | if (!AssociatedFunction.isUsable()) { |
1395 | if (!Tok.is(tok::annot_pragma_openmp_end)) |
1396 | while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch)) |
1397 | ; |
1398 | // Skip the last annot_pragma_openmp_end. |
1399 | (void)ConsumeAnnotationToken(); |
1400 | return; |
1401 | } |
1402 | |
1403 | OMPTraitInfo *ParentTI = Actions.getOMPTraitInfoForSurroundingScope(); |
1404 | ASTContext &ASTCtx = Actions.getASTContext(); |
1405 | OMPTraitInfo &TI = ASTCtx.getNewOMPTraitInfo(); |
1406 | SmallVector<Expr *, 6> AdjustNothing; |
1407 | SmallVector<Expr *, 6> AdjustNeedDevicePtr; |
1408 | SmallVector<OMPDeclareVariantAttr::InteropType, 3> AppendArgs; |
1409 | SourceLocation AdjustArgsLoc, AppendArgsLoc; |
1410 | |
1411 | // At least one clause is required. |
1412 | if (Tok.is(tok::annot_pragma_openmp_end)) { |
1413 | Diag(Tok.getLocation(), diag::err_omp_declare_variant_wrong_clause) |
1414 | << (getLangOpts().OpenMP < 51 ? 0 : 1); |
1415 | } |
1416 | |
1417 | bool IsError = false; |
1418 | while (Tok.isNot(tok::annot_pragma_openmp_end)) { |
1419 | OpenMPClauseKind CKind = Tok.isAnnotation() |
1420 | ? OMPC_unknown |
1421 | : getOpenMPClauseKind(PP.getSpelling(Tok)); |
1422 | if (!isAllowedClauseForDirective(OMPD_declare_variant, CKind, |
1423 | getLangOpts().OpenMP)) { |
1424 | Diag(Tok.getLocation(), diag::err_omp_declare_variant_wrong_clause) |
1425 | << (getLangOpts().OpenMP < 51 ? 0 : 1); |
1426 | IsError = true; |
1427 | } |
1428 | if (!IsError) { |
1429 | switch (CKind) { |
1430 | case OMPC_match: |
1431 | IsError = parseOMPDeclareVariantMatchClause(Loc, TI, ParentTI); |
1432 | break; |
1433 | case OMPC_adjust_args: { |
1434 | AdjustArgsLoc = Tok.getLocation(); |
1435 | ConsumeToken(); |
1436 | Parser::OpenMPVarListDataTy Data; |
1437 | SmallVector<Expr *> Vars; |
1438 | IsError = ParseOpenMPVarList(OMPD_declare_variant, OMPC_adjust_args, |
1439 | Vars, Data); |
1440 | if (!IsError) |
1441 | llvm::append_range(Data.ExtraModifier == OMPC_ADJUST_ARGS_nothing |
1442 | ? AdjustNothing |
1443 | : AdjustNeedDevicePtr, |
1444 | Vars); |
1445 | break; |
1446 | } |
1447 | case OMPC_append_args: |
1448 | if (!AppendArgs.empty()) { |
1449 | Diag(AppendArgsLoc, diag::err_omp_more_one_clause) |
1450 | << getOpenMPDirectiveName(OMPD_declare_variant) |
1451 | << getOpenMPClauseName(CKind) << 0; |
1452 | IsError = true; |
1453 | } |
1454 | if (!IsError) { |
1455 | AppendArgsLoc = Tok.getLocation(); |
1456 | ConsumeToken(); |
1457 | IsError = parseOpenMPAppendArgs(AppendArgs); |
1458 | } |
1459 | break; |
1460 | default: |
1461 | llvm_unreachable("Unexpected clause for declare variant.")::llvm::llvm_unreachable_internal("Unexpected clause for declare variant." , "clang/lib/Parse/ParseOpenMP.cpp", 1461); |
1462 | } |
1463 | } |
1464 | if (IsError) { |
1465 | while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch)) |
1466 | ; |
1467 | // Skip the last annot_pragma_openmp_end. |
1468 | (void)ConsumeAnnotationToken(); |
1469 | return; |
1470 | } |
1471 | // Skip ',' if any. |
1472 | if (Tok.is(tok::comma)) |
1473 | ConsumeToken(); |
1474 | } |
1475 | |
1476 | Optional<std::pair<FunctionDecl *, Expr *>> DeclVarData = |
1477 | Actions.checkOpenMPDeclareVariantFunction( |
1478 | Ptr, AssociatedFunction.get(), TI, AppendArgs.size(), |
1479 | SourceRange(Loc, Tok.getLocation())); |
1480 | |
1481 | if (DeclVarData && !TI.Sets.empty()) |
1482 | Actions.ActOnOpenMPDeclareVariantDirective( |
1483 | DeclVarData->first, DeclVarData->second, TI, AdjustNothing, |
1484 | AdjustNeedDevicePtr, AppendArgs, AdjustArgsLoc, AppendArgsLoc, |
1485 | SourceRange(Loc, Tok.getLocation())); |
1486 | |
1487 | // Skip the last annot_pragma_openmp_end. |
1488 | (void)ConsumeAnnotationToken(); |
1489 | } |
1490 | |
1491 | /// Parse a list of interop-types. These are 'target' and 'targetsync'. Both |
1492 | /// are allowed but duplication of either is not meaningful. |
1493 | static Optional<OMPDeclareVariantAttr::InteropType> |
1494 | parseInteropTypeList(Parser &P) { |
1495 | const Token &Tok = P.getCurToken(); |
1496 | bool HasError = false; |
1497 | bool IsTarget = false; |
1498 | bool IsTargetSync = false; |
1499 | |
1500 | while (Tok.is(tok::identifier)) { |
1501 | if (Tok.getIdentifierInfo()->isStr("target")) { |
1502 | // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] |
1503 | // Each interop-type may be specified on an action-clause at most |
1504 | // once. |
1505 | if (IsTarget) |
1506 | P.Diag(Tok, diag::warn_omp_more_one_interop_type) << "target"; |
1507 | IsTarget = true; |
1508 | } else if (Tok.getIdentifierInfo()->isStr("targetsync")) { |
1509 | if (IsTargetSync) |
1510 | P.Diag(Tok, diag::warn_omp_more_one_interop_type) << "targetsync"; |
1511 | IsTargetSync = true; |
1512 | } else { |
1513 | HasError = true; |
1514 | P.Diag(Tok, diag::err_omp_expected_interop_type); |
1515 | } |
1516 | P.ConsumeToken(); |
1517 | |
1518 | if (!Tok.is(tok::comma)) |
1519 | break; |
1520 | P.ConsumeToken(); |
1521 | } |
1522 | if (HasError) |
1523 | return None; |
1524 | |
1525 | if (!IsTarget && !IsTargetSync) { |
1526 | P.Diag(Tok, diag::err_omp_expected_interop_type); |
1527 | return None; |
1528 | } |
1529 | |
1530 | // As of OpenMP 5.1,there are two interop-types, "target" and |
1531 | // "targetsync". Either or both are allowed for a single interop. |
1532 | if (IsTarget && IsTargetSync) |
1533 | return OMPDeclareVariantAttr::Target_TargetSync; |
1534 | if (IsTarget) |
1535 | return OMPDeclareVariantAttr::Target; |
1536 | return OMPDeclareVariantAttr::TargetSync; |
1537 | } |
1538 | |
1539 | bool Parser::parseOpenMPAppendArgs( |
1540 | SmallVectorImpl<OMPDeclareVariantAttr::InteropType> &InterOpTypes) { |
1541 | bool HasError = false; |
1542 | // Parse '('. |
1543 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
1544 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
1545 | getOpenMPClauseName(OMPC_append_args).data())) |
1546 | return true; |
1547 | |
1548 | // Parse the list of append-ops, each is; |
1549 | // interop(interop-type[,interop-type]...) |
1550 | while (Tok.is(tok::identifier) && Tok.getIdentifierInfo()->isStr("interop")) { |
1551 | ConsumeToken(); |
1552 | BalancedDelimiterTracker IT(*this, tok::l_paren, |
1553 | tok::annot_pragma_openmp_end); |
1554 | if (IT.expectAndConsume(diag::err_expected_lparen_after, "interop")) |
1555 | return true; |
1556 | |
1557 | // Parse the interop-types. |
1558 | if (Optional<OMPDeclareVariantAttr::InteropType> IType = |
1559 | parseInteropTypeList(*this)) |
1560 | InterOpTypes.push_back(IType.getValue()); |
1561 | else |
1562 | HasError = true; |
1563 | |
1564 | IT.consumeClose(); |
1565 | if (Tok.is(tok::comma)) |
1566 | ConsumeToken(); |
1567 | } |
1568 | if (!HasError && InterOpTypes.empty()) { |
1569 | HasError = true; |
1570 | Diag(Tok.getLocation(), diag::err_omp_unexpected_append_op); |
1571 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
1572 | StopBeforeMatch); |
1573 | } |
1574 | HasError = T.consumeClose() || HasError; |
1575 | return HasError; |
1576 | } |
1577 | |
1578 | bool Parser::parseOMPDeclareVariantMatchClause(SourceLocation Loc, |
1579 | OMPTraitInfo &TI, |
1580 | OMPTraitInfo *ParentTI) { |
1581 | // Parse 'match'. |
1582 | OpenMPClauseKind CKind = Tok.isAnnotation() |
1583 | ? OMPC_unknown |
1584 | : getOpenMPClauseKind(PP.getSpelling(Tok)); |
1585 | if (CKind != OMPC_match) { |
1586 | Diag(Tok.getLocation(), diag::err_omp_declare_variant_wrong_clause) |
1587 | << (getLangOpts().OpenMP < 51 ? 0 : 1); |
1588 | return true; |
1589 | } |
1590 | (void)ConsumeToken(); |
1591 | // Parse '('. |
1592 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
1593 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
1594 | getOpenMPClauseName(OMPC_match).data())) |
1595 | return true; |
1596 | |
1597 | // Parse inner context selectors. |
1598 | parseOMPContextSelectors(Loc, TI); |
1599 | |
1600 | // Parse ')' |
1601 | (void)T.consumeClose(); |
1602 | |
1603 | if (!ParentTI) |
1604 | return false; |
1605 | |
1606 | // Merge the parent/outer trait info into the one we just parsed and diagnose |
1607 | // problems. |
1608 | // TODO: Keep some source location in the TI to provide better diagnostics. |
1609 | // TODO: Perform some kind of equivalence check on the condition and score |
1610 | // expressions. |
1611 | for (const OMPTraitSet &ParentSet : ParentTI->Sets) { |
1612 | bool MergedSet = false; |
1613 | for (OMPTraitSet &Set : TI.Sets) { |
1614 | if (Set.Kind != ParentSet.Kind) |
1615 | continue; |
1616 | MergedSet = true; |
1617 | for (const OMPTraitSelector &ParentSelector : ParentSet.Selectors) { |
1618 | bool MergedSelector = false; |
1619 | for (OMPTraitSelector &Selector : Set.Selectors) { |
1620 | if (Selector.Kind != ParentSelector.Kind) |
1621 | continue; |
1622 | MergedSelector = true; |
1623 | for (const OMPTraitProperty &ParentProperty : |
1624 | ParentSelector.Properties) { |
1625 | bool MergedProperty = false; |
1626 | for (OMPTraitProperty &Property : Selector.Properties) { |
1627 | // Ignore "equivalent" properties. |
1628 | if (Property.Kind != ParentProperty.Kind) |
1629 | continue; |
1630 | |
1631 | // If the kind is the same but the raw string not, we don't want |
1632 | // to skip out on the property. |
1633 | MergedProperty |= Property.RawString == ParentProperty.RawString; |
1634 | |
1635 | if (Property.RawString == ParentProperty.RawString && |
1636 | Selector.ScoreOrCondition == ParentSelector.ScoreOrCondition) |
1637 | continue; |
1638 | |
1639 | if (Selector.Kind == llvm::omp::TraitSelector::user_condition) { |
1640 | Diag(Loc, diag::err_omp_declare_variant_nested_user_condition); |
1641 | } else if (Selector.ScoreOrCondition != |
1642 | ParentSelector.ScoreOrCondition) { |
1643 | Diag(Loc, diag::err_omp_declare_variant_duplicate_nested_trait) |
1644 | << getOpenMPContextTraitPropertyName( |
1645 | ParentProperty.Kind, ParentProperty.RawString) |
1646 | << getOpenMPContextTraitSelectorName(ParentSelector.Kind) |
1647 | << getOpenMPContextTraitSetName(ParentSet.Kind); |
1648 | } |
1649 | } |
1650 | if (!MergedProperty) |
1651 | Selector.Properties.push_back(ParentProperty); |
1652 | } |
1653 | } |
1654 | if (!MergedSelector) |
1655 | Set.Selectors.push_back(ParentSelector); |
1656 | } |
1657 | } |
1658 | if (!MergedSet) |
1659 | TI.Sets.push_back(ParentSet); |
1660 | } |
1661 | |
1662 | return false; |
1663 | } |
1664 | |
1665 | /// `omp assumes` or `omp begin/end assumes` <clause> [[,]<clause>]... |
1666 | /// where |
1667 | /// |
1668 | /// clause: |
1669 | /// 'ext_IMPL_DEFINED' |
1670 | /// 'absent' '(' directive-name [, directive-name]* ')' |
1671 | /// 'contains' '(' directive-name [, directive-name]* ')' |
1672 | /// 'holds' '(' scalar-expression ')' |
1673 | /// 'no_openmp' |
1674 | /// 'no_openmp_routines' |
1675 | /// 'no_parallelism' |
1676 | /// |
1677 | void Parser::ParseOpenMPAssumesDirective(OpenMPDirectiveKind DKind, |
1678 | SourceLocation Loc) { |
1679 | SmallVector<std::string, 4> Assumptions; |
1680 | bool SkippedClauses = false; |
1681 | |
1682 | auto SkipBraces = [&](llvm::StringRef Spelling, bool IssueNote) { |
1683 | BalancedDelimiterTracker T(*this, tok::l_paren, |
1684 | tok::annot_pragma_openmp_end); |
1685 | if (T.expectAndConsume(diag::err_expected_lparen_after, Spelling.data())) |
1686 | return; |
1687 | T.skipToEnd(); |
1688 | if (IssueNote && T.getCloseLocation().isValid()) |
1689 | Diag(T.getCloseLocation(), |
1690 | diag::note_omp_assumption_clause_continue_here); |
1691 | }; |
1692 | |
1693 | /// Helper to determine which AssumptionClauseMapping (ACM) in the |
1694 | /// AssumptionClauseMappings table matches \p RawString. The return value is |
1695 | /// the index of the matching ACM into the table or -1 if there was no match. |
1696 | auto MatchACMClause = [&](StringRef RawString) { |
1697 | llvm::StringSwitch<int> SS(RawString); |
1698 | unsigned ACMIdx = 0; |
1699 | for (const AssumptionClauseMappingInfo &ACMI : AssumptionClauseMappings) { |
1700 | if (ACMI.StartsWith) |
1701 | SS.StartsWith(ACMI.Identifier, ACMIdx++); |
1702 | else |
1703 | SS.Case(ACMI.Identifier, ACMIdx++); |
1704 | } |
1705 | return SS.Default(-1); |
1706 | }; |
1707 | |
1708 | while (Tok.isNot(tok::annot_pragma_openmp_end)) { |
1709 | IdentifierInfo *II = nullptr; |
1710 | SourceLocation StartLoc = Tok.getLocation(); |
1711 | int Idx = -1; |
1712 | if (Tok.isAnyIdentifier()) { |
1713 | II = Tok.getIdentifierInfo(); |
1714 | Idx = MatchACMClause(II->getName()); |
1715 | } |
1716 | ConsumeAnyToken(); |
1717 | |
1718 | bool NextIsLPar = Tok.is(tok::l_paren); |
1719 | // Handle unknown clauses by skipping them. |
1720 | if (Idx == -1) { |
1721 | Diag(StartLoc, diag::warn_omp_unknown_assumption_clause_missing_id) |
1722 | << llvm::omp::getOpenMPDirectiveName(DKind) |
1723 | << llvm::omp::getAllAssumeClauseOptions() << NextIsLPar; |
1724 | if (NextIsLPar) |
1725 | SkipBraces(II ? II->getName() : "", /* IssueNote */ true); |
1726 | SkippedClauses = true; |
1727 | continue; |
1728 | } |
1729 | const AssumptionClauseMappingInfo &ACMI = AssumptionClauseMappings[Idx]; |
1730 | if (ACMI.HasDirectiveList || ACMI.HasExpression) { |
1731 | // TODO: We ignore absent, contains, and holds assumptions for now. We |
1732 | // also do not verify the content in the parenthesis at all. |
1733 | SkippedClauses = true; |
1734 | SkipBraces(II->getName(), /* IssueNote */ false); |
1735 | continue; |
1736 | } |
1737 | |
1738 | if (NextIsLPar) { |
1739 | Diag(Tok.getLocation(), |
1740 | diag::warn_omp_unknown_assumption_clause_without_args) |
1741 | << II; |
1742 | SkipBraces(II->getName(), /* IssueNote */ true); |
1743 | } |
1744 | |
1745 | assert(II && "Expected an identifier clause!")(static_cast <bool> (II && "Expected an identifier clause!" ) ? void (0) : __assert_fail ("II && \"Expected an identifier clause!\"" , "clang/lib/Parse/ParseOpenMP.cpp", 1745, __extension__ __PRETTY_FUNCTION__ )); |
1746 | std::string Assumption = II->getName().str(); |
1747 | if (ACMI.StartsWith) |
1748 | Assumption = "ompx_" + Assumption.substr(ACMI.Identifier.size()); |
1749 | else |
1750 | Assumption = "omp_" + Assumption; |
1751 | Assumptions.push_back(Assumption); |
1752 | } |
1753 | |
1754 | Actions.ActOnOpenMPAssumesDirective(Loc, DKind, Assumptions, SkippedClauses); |
1755 | } |
1756 | |
1757 | void Parser::ParseOpenMPEndAssumesDirective(SourceLocation Loc) { |
1758 | if (Actions.isInOpenMPAssumeScope()) |
1759 | Actions.ActOnOpenMPEndAssumesDirective(); |
1760 | else |
1761 | Diag(Loc, diag::err_expected_begin_assumes); |
1762 | } |
1763 | |
1764 | /// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'. |
1765 | /// |
1766 | /// default-clause: |
1767 | /// 'default' '(' 'none' | 'shared' | 'firstprivate' ') |
1768 | /// |
1769 | /// proc_bind-clause: |
1770 | /// 'proc_bind' '(' 'master' | 'close' | 'spread' ') |
1771 | /// |
1772 | /// device_type-clause: |
1773 | /// 'device_type' '(' 'host' | 'nohost' | 'any' )' |
1774 | namespace { |
1775 | struct SimpleClauseData { |
1776 | unsigned Type; |
1777 | SourceLocation Loc; |
1778 | SourceLocation LOpen; |
1779 | SourceLocation TypeLoc; |
1780 | SourceLocation RLoc; |
1781 | SimpleClauseData(unsigned Type, SourceLocation Loc, SourceLocation LOpen, |
1782 | SourceLocation TypeLoc, SourceLocation RLoc) |
1783 | : Type(Type), Loc(Loc), LOpen(LOpen), TypeLoc(TypeLoc), RLoc(RLoc) {} |
1784 | }; |
1785 | } // anonymous namespace |
1786 | |
1787 | static Optional<SimpleClauseData> |
1788 | parseOpenMPSimpleClause(Parser &P, OpenMPClauseKind Kind) { |
1789 | const Token &Tok = P.getCurToken(); |
1790 | SourceLocation Loc = Tok.getLocation(); |
1791 | SourceLocation LOpen = P.ConsumeToken(); |
1792 | // Parse '('. |
1793 | BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end); |
1794 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
1795 | getOpenMPClauseName(Kind).data())) |
1796 | return llvm::None; |
1797 | |
1798 | unsigned Type = getOpenMPSimpleClauseType( |
1799 | Kind, Tok.isAnnotation() ? "" : P.getPreprocessor().getSpelling(Tok), |
1800 | P.getLangOpts()); |
1801 | SourceLocation TypeLoc = Tok.getLocation(); |
1802 | if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && |
1803 | Tok.isNot(tok::annot_pragma_openmp_end)) |
1804 | P.ConsumeAnyToken(); |
1805 | |
1806 | // Parse ')'. |
1807 | SourceLocation RLoc = Tok.getLocation(); |
1808 | if (!T.consumeClose()) |
1809 | RLoc = T.getCloseLocation(); |
1810 | |
1811 | return SimpleClauseData(Type, Loc, LOpen, TypeLoc, RLoc); |
1812 | } |
1813 | |
1814 | void Parser::ParseOMPDeclareTargetClauses( |
1815 | Sema::DeclareTargetContextInfo &DTCI) { |
1816 | SourceLocation DeviceTypeLoc; |
1817 | bool RequiresToOrLinkOrIndirectClause = false; |
1818 | bool HasToOrLinkOrIndirectClause = false; |
1819 | while (Tok.isNot(tok::annot_pragma_openmp_end)) { |
1820 | OMPDeclareTargetDeclAttr::MapTypeTy MT = OMPDeclareTargetDeclAttr::MT_To; |
1821 | bool HasIdentifier = Tok.is(tok::identifier); |
1822 | if (HasIdentifier) { |
1823 | // If we see any clause we need a to or link clause. |
1824 | RequiresToOrLinkOrIndirectClause = true; |
1825 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
1826 | StringRef ClauseName = II->getName(); |
1827 | bool IsDeviceTypeClause = |
1828 | getLangOpts().OpenMP >= 50 && |
1829 | getOpenMPClauseKind(ClauseName) == OMPC_device_type; |
1830 | |
1831 | bool IsIndirectClause = getLangOpts().OpenMP >= 51 && |
1832 | getOpenMPClauseKind(ClauseName) == OMPC_indirect; |
1833 | if (DTCI.Indirect.hasValue() && IsIndirectClause) { |
1834 | Diag(Tok, diag::err_omp_more_one_clause) |
1835 | << getOpenMPDirectiveName(OMPD_declare_target) |
1836 | << getOpenMPClauseName(OMPC_indirect) << 0; |
1837 | break; |
1838 | } |
1839 | bool IsToOrLinkClause = |
1840 | OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName, MT); |
1841 | assert((!IsDeviceTypeClause || !IsToOrLinkClause) && "Cannot be both!")(static_cast <bool> ((!IsDeviceTypeClause || !IsToOrLinkClause ) && "Cannot be both!") ? void (0) : __assert_fail ("(!IsDeviceTypeClause || !IsToOrLinkClause) && \"Cannot be both!\"" , "clang/lib/Parse/ParseOpenMP.cpp", 1841, __extension__ __PRETTY_FUNCTION__ )); |
1842 | |
1843 | if (!IsDeviceTypeClause && !IsIndirectClause && |
1844 | DTCI.Kind == OMPD_begin_declare_target) { |
1845 | Diag(Tok, diag::err_omp_declare_target_unexpected_clause) |
1846 | << ClauseName << (getLangOpts().OpenMP >= 51 ? 3 : 0); |
1847 | break; |
1848 | } |
1849 | if (!IsDeviceTypeClause && !IsToOrLinkClause && !IsIndirectClause) { |
1850 | Diag(Tok, diag::err_omp_declare_target_unexpected_clause) |
1851 | << ClauseName |
1852 | << (getLangOpts().OpenMP >= 51 ? 4 |
1853 | : getLangOpts().OpenMP >= 50 ? 2 |
1854 | : 1); |
1855 | break; |
1856 | } |
1857 | |
1858 | if (IsToOrLinkClause || IsIndirectClause) |
1859 | HasToOrLinkOrIndirectClause = true; |
1860 | |
1861 | if (IsIndirectClause) { |
1862 | if (!ParseOpenMPIndirectClause(DTCI, /*ParseOnly*/ false)) |
1863 | break; |
1864 | continue; |
1865 | } |
1866 | // Parse 'device_type' clause and go to next clause if any. |
1867 | if (IsDeviceTypeClause) { |
1868 | Optional<SimpleClauseData> DevTypeData = |
1869 | parseOpenMPSimpleClause(*this, OMPC_device_type); |
1870 | if (DevTypeData.hasValue()) { |
1871 | if (DeviceTypeLoc.isValid()) { |
1872 | // We already saw another device_type clause, diagnose it. |
1873 | Diag(DevTypeData.getValue().Loc, |
1874 | diag::warn_omp_more_one_device_type_clause); |
1875 | break; |
1876 | } |
1877 | switch (static_cast<OpenMPDeviceType>(DevTypeData.getValue().Type)) { |
1878 | case OMPC_DEVICE_TYPE_any: |
1879 | DTCI.DT = OMPDeclareTargetDeclAttr::DT_Any; |
1880 | break; |
1881 | case OMPC_DEVICE_TYPE_host: |
1882 | DTCI.DT = OMPDeclareTargetDeclAttr::DT_Host; |
1883 | break; |
1884 | case OMPC_DEVICE_TYPE_nohost: |
1885 | DTCI.DT = OMPDeclareTargetDeclAttr::DT_NoHost; |
1886 | break; |
1887 | case OMPC_DEVICE_TYPE_unknown: |
1888 | llvm_unreachable("Unexpected device_type")::llvm::llvm_unreachable_internal("Unexpected device_type", "clang/lib/Parse/ParseOpenMP.cpp" , 1888); |
1889 | } |
1890 | DeviceTypeLoc = DevTypeData.getValue().Loc; |
1891 | } |
1892 | continue; |
1893 | } |
1894 | ConsumeToken(); |
1895 | } |
1896 | |
1897 | if (DTCI.Kind == OMPD_declare_target || HasIdentifier) { |
1898 | auto &&Callback = [this, MT, &DTCI](CXXScopeSpec &SS, |
1899 | DeclarationNameInfo NameInfo) { |
1900 | NamedDecl *ND = |
1901 | Actions.lookupOpenMPDeclareTargetName(getCurScope(), SS, NameInfo); |
1902 | if (!ND) |
1903 | return; |
1904 | Sema::DeclareTargetContextInfo::MapInfo MI{MT, NameInfo.getLoc()}; |
1905 | bool FirstMapping = DTCI.ExplicitlyMapped.try_emplace(ND, MI).second; |
1906 | if (!FirstMapping) |
1907 | Diag(NameInfo.getLoc(), diag::err_omp_declare_target_multiple) |
1908 | << NameInfo.getName(); |
1909 | }; |
1910 | if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, |
1911 | /*AllowScopeSpecifier=*/true)) |
1912 | break; |
1913 | } |
1914 | |
1915 | if (Tok.is(tok::l_paren)) { |
1916 | Diag(Tok, |
1917 | diag::err_omp_begin_declare_target_unexpected_implicit_to_clause); |
1918 | break; |
1919 | } |
1920 | if (!HasIdentifier && Tok.isNot(tok::annot_pragma_openmp_end)) { |
1921 | Diag(Tok, |
1922 | diag::err_omp_declare_target_unexpected_clause_after_implicit_to); |
1923 | break; |
1924 | } |
1925 | |
1926 | // Consume optional ','. |
1927 | if (Tok.is(tok::comma)) |
1928 | ConsumeToken(); |
1929 | } |
1930 | |
1931 | if (DTCI.Indirect.hasValue() && DTCI.DT != OMPDeclareTargetDeclAttr::DT_Any) |
1932 | Diag(DeviceTypeLoc, diag::err_omp_declare_target_indirect_device_type); |
1933 | |
1934 | // For declare target require at least 'to' or 'link' to be present. |
1935 | if (DTCI.Kind == OMPD_declare_target && RequiresToOrLinkOrIndirectClause && |
1936 | !HasToOrLinkOrIndirectClause) |
1937 | Diag(DTCI.Loc, diag::err_omp_declare_target_missing_to_or_link_clause) |
1938 | << (getLangOpts().OpenMP >= 51 ? 1 : 0); |
1939 | |
1940 | SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); |
1941 | } |
1942 | |
1943 | void Parser::skipUntilPragmaOpenMPEnd(OpenMPDirectiveKind DKind) { |
1944 | // The last seen token is annot_pragma_openmp_end - need to check for |
1945 | // extra tokens. |
1946 | if (Tok.is(tok::annot_pragma_openmp_end)) |
1947 | return; |
1948 | |
1949 | Diag(Tok, diag::warn_omp_extra_tokens_at_eol) |
1950 | << getOpenMPDirectiveName(DKind); |
1951 | while (Tok.isNot(tok::annot_pragma_openmp_end)) |
1952 | ConsumeAnyToken(); |
1953 | } |
1954 | |
1955 | void Parser::parseOMPEndDirective(OpenMPDirectiveKind BeginKind, |
1956 | OpenMPDirectiveKind ExpectedKind, |
1957 | OpenMPDirectiveKind FoundKind, |
1958 | SourceLocation BeginLoc, |
1959 | SourceLocation FoundLoc, |
1960 | bool SkipUntilOpenMPEnd) { |
1961 | int DiagSelection = ExpectedKind == OMPD_end_declare_target ? 0 : 1; |
1962 | |
1963 | if (FoundKind == ExpectedKind) { |
1964 | ConsumeAnyToken(); |
1965 | skipUntilPragmaOpenMPEnd(ExpectedKind); |
1966 | return; |
1967 | } |
1968 | |
1969 | Diag(FoundLoc, diag::err_expected_end_declare_target_or_variant) |
1970 | << DiagSelection; |
1971 | Diag(BeginLoc, diag::note_matching) |
1972 | << ("'#pragma omp " + getOpenMPDirectiveName(BeginKind) + "'").str(); |
1973 | if (SkipUntilOpenMPEnd) |
1974 | SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); |
1975 | } |
1976 | |
1977 | void Parser::ParseOMPEndDeclareTargetDirective(OpenMPDirectiveKind BeginDKind, |
1978 | OpenMPDirectiveKind EndDKind, |
1979 | SourceLocation DKLoc) { |
1980 | parseOMPEndDirective(BeginDKind, OMPD_end_declare_target, EndDKind, DKLoc, |
1981 | Tok.getLocation(), |
1982 | /* SkipUntilOpenMPEnd */ false); |
1983 | // Skip the last annot_pragma_openmp_end. |
1984 | if (Tok.is(tok::annot_pragma_openmp_end)) |
1985 | ConsumeAnnotationToken(); |
1986 | } |
1987 | |
1988 | /// Parsing of declarative OpenMP directives. |
1989 | /// |
1990 | /// threadprivate-directive: |
1991 | /// annot_pragma_openmp 'threadprivate' simple-variable-list |
1992 | /// annot_pragma_openmp_end |
1993 | /// |
1994 | /// allocate-directive: |
1995 | /// annot_pragma_openmp 'allocate' simple-variable-list [<clause>] |
1996 | /// annot_pragma_openmp_end |
1997 | /// |
1998 | /// declare-reduction-directive: |
1999 | /// annot_pragma_openmp 'declare' 'reduction' [...] |
2000 | /// annot_pragma_openmp_end |
2001 | /// |
2002 | /// declare-mapper-directive: |
2003 | /// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':'] |
2004 | /// <type> <var> ')' [<clause>[[,] <clause>] ... ] |
2005 | /// annot_pragma_openmp_end |
2006 | /// |
2007 | /// declare-simd-directive: |
2008 | /// annot_pragma_openmp 'declare simd' {<clause> [,]} |
2009 | /// annot_pragma_openmp_end |
2010 | /// <function declaration/definition> |
2011 | /// |
2012 | /// requires directive: |
2013 | /// annot_pragma_openmp 'requires' <clause> [[[,] <clause>] ... ] |
2014 | /// annot_pragma_openmp_end |
2015 | /// |
2016 | /// assumes directive: |
2017 | /// annot_pragma_openmp 'assumes' <clause> [[[,] <clause>] ... ] |
2018 | /// annot_pragma_openmp_end |
2019 | /// or |
2020 | /// annot_pragma_openmp 'begin assumes' <clause> [[[,] <clause>] ... ] |
2021 | /// annot_pragma_openmp 'end assumes' |
2022 | /// annot_pragma_openmp_end |
2023 | /// |
2024 | Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl( |
2025 | AccessSpecifier &AS, ParsedAttributesWithRange &Attrs, bool Delayed, |
2026 | DeclSpec::TST TagType, Decl *Tag) { |
2027 | assert(Tok.isOneOf(tok::annot_pragma_openmp, tok::annot_attr_openmp) &&(static_cast <bool> (Tok.isOneOf(tok::annot_pragma_openmp , tok::annot_attr_openmp) && "Not an OpenMP directive!" ) ? void (0) : __assert_fail ("Tok.isOneOf(tok::annot_pragma_openmp, tok::annot_attr_openmp) && \"Not an OpenMP directive!\"" , "clang/lib/Parse/ParseOpenMP.cpp", 2028, __extension__ __PRETTY_FUNCTION__ )) |
2028 | "Not an OpenMP directive!")(static_cast <bool> (Tok.isOneOf(tok::annot_pragma_openmp , tok::annot_attr_openmp) && "Not an OpenMP directive!" ) ? void (0) : __assert_fail ("Tok.isOneOf(tok::annot_pragma_openmp, tok::annot_attr_openmp) && \"Not an OpenMP directive!\"" , "clang/lib/Parse/ParseOpenMP.cpp", 2028, __extension__ __PRETTY_FUNCTION__ )); |
2029 | ParsingOpenMPDirectiveRAII DirScope(*this); |
2030 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
2031 | |
2032 | SourceLocation Loc; |
2033 | OpenMPDirectiveKind DKind; |
2034 | if (Delayed) { |
2035 | TentativeParsingAction TPA(*this); |
2036 | Loc = ConsumeAnnotationToken(); |
2037 | DKind = parseOpenMPDirectiveKind(*this); |
2038 | if (DKind == OMPD_declare_reduction || DKind == OMPD_declare_mapper) { |
2039 | // Need to delay parsing until completion of the parent class. |
2040 | TPA.Revert(); |
2041 | CachedTokens Toks; |
2042 | unsigned Cnt = 1; |
2043 | Toks.push_back(Tok); |
2044 | while (Cnt && Tok.isNot(tok::eof)) { |
2045 | (void)ConsumeAnyToken(); |
2046 | if (Tok.isOneOf(tok::annot_pragma_openmp, tok::annot_attr_openmp)) |
2047 | ++Cnt; |
2048 | else if (Tok.is(tok::annot_pragma_openmp_end)) |
2049 | --Cnt; |
2050 | Toks.push_back(Tok); |
2051 | } |
2052 | // Skip last annot_pragma_openmp_end. |
2053 | if (Cnt == 0) |
2054 | (void)ConsumeAnyToken(); |
2055 | auto *LP = new LateParsedPragma(this, AS); |
2056 | LP->takeToks(Toks); |
2057 | getCurrentClass().LateParsedDeclarations.push_back(LP); |
2058 | return nullptr; |
2059 | } |
2060 | TPA.Commit(); |
2061 | } else { |
2062 | Loc = ConsumeAnnotationToken(); |
2063 | DKind = parseOpenMPDirectiveKind(*this); |
2064 | } |
2065 | |
2066 | switch (DKind) { |
2067 | case OMPD_threadprivate: { |
2068 | ConsumeToken(); |
2069 | DeclDirectiveListParserHelper Helper(this, DKind); |
2070 | if (!ParseOpenMPSimpleVarList(DKind, Helper, |
2071 | /*AllowScopeSpecifier=*/true)) { |
2072 | skipUntilPragmaOpenMPEnd(DKind); |
2073 | // Skip the last annot_pragma_openmp_end. |
2074 | ConsumeAnnotationToken(); |
2075 | return Actions.ActOnOpenMPThreadprivateDirective(Loc, |
2076 | Helper.getIdentifiers()); |
2077 | } |
2078 | break; |
2079 | } |
2080 | case OMPD_allocate: { |
2081 | ConsumeToken(); |
2082 | DeclDirectiveListParserHelper Helper(this, DKind); |
2083 | if (!ParseOpenMPSimpleVarList(DKind, Helper, |
2084 | /*AllowScopeSpecifier=*/true)) { |
2085 | SmallVector<OMPClause *, 1> Clauses; |
2086 | if (Tok.isNot(tok::annot_pragma_openmp_end)) { |
2087 | SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, |
2088 | llvm::omp::Clause_enumSize + 1> |
2089 | FirstClauses(llvm::omp::Clause_enumSize + 1); |
2090 | while (Tok.isNot(tok::annot_pragma_openmp_end)) { |
2091 | OpenMPClauseKind CKind = |
2092 | Tok.isAnnotation() ? OMPC_unknown |
2093 | : getOpenMPClauseKind(PP.getSpelling(Tok)); |
2094 | Actions.StartOpenMPClause(CKind); |
2095 | OMPClause *Clause = ParseOpenMPClause( |
2096 | OMPD_allocate, CKind, !FirstClauses[unsigned(CKind)].getInt()); |
2097 | SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end, |
2098 | StopBeforeMatch); |
2099 | FirstClauses[unsigned(CKind)].setInt(true); |
2100 | if (Clause != nullptr) |
2101 | Clauses.push_back(Clause); |
2102 | if (Tok.is(tok::annot_pragma_openmp_end)) { |
2103 | Actions.EndOpenMPClause(); |
2104 | break; |
2105 | } |
2106 | // Skip ',' if any. |
2107 | if (Tok.is(tok::comma)) |
2108 | ConsumeToken(); |
2109 | Actions.EndOpenMPClause(); |
2110 | } |
2111 | skipUntilPragmaOpenMPEnd(DKind); |
2112 | } |
2113 | // Skip the last annot_pragma_openmp_end. |
2114 | ConsumeAnnotationToken(); |
2115 | return Actions.ActOnOpenMPAllocateDirective(Loc, Helper.getIdentifiers(), |
2116 | Clauses); |
2117 | } |
2118 | break; |
2119 | } |
2120 | case OMPD_requires: { |
2121 | SourceLocation StartLoc = ConsumeToken(); |
2122 | SmallVector<OMPClause *, 5> Clauses; |
2123 | SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, |
2124 | llvm::omp::Clause_enumSize + 1> |
2125 | FirstClauses(llvm::omp::Clause_enumSize + 1); |
2126 | if (Tok.is(tok::annot_pragma_openmp_end)) { |
2127 | Diag(Tok, diag::err_omp_expected_clause) |
2128 | << getOpenMPDirectiveName(OMPD_requires); |
2129 | break; |
2130 | } |
2131 | while (Tok.isNot(tok::annot_pragma_openmp_end)) { |
2132 | OpenMPClauseKind CKind = Tok.isAnnotation() |
2133 | ? OMPC_unknown |
2134 | : getOpenMPClauseKind(PP.getSpelling(Tok)); |
2135 | Actions.StartOpenMPClause(CKind); |
2136 | OMPClause *Clause = ParseOpenMPClause( |
2137 | OMPD_requires, CKind, !FirstClauses[unsigned(CKind)].getInt()); |
2138 | SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end, |
2139 | StopBeforeMatch); |
2140 | FirstClauses[unsigned(CKind)].setInt(true); |
2141 | if (Clause != nullptr) |
2142 | Clauses.push_back(Clause); |
2143 | if (Tok.is(tok::annot_pragma_openmp_end)) { |
2144 | Actions.EndOpenMPClause(); |
2145 | break; |
2146 | } |
2147 | // Skip ',' if any. |
2148 | if (Tok.is(tok::comma)) |
2149 | ConsumeToken(); |
2150 | Actions.EndOpenMPClause(); |
2151 | } |
2152 | // Consume final annot_pragma_openmp_end |
2153 | if (Clauses.empty()) { |
2154 | Diag(Tok, diag::err_omp_expected_clause) |
2155 | << getOpenMPDirectiveName(OMPD_requires); |
2156 | ConsumeAnnotationToken(); |
2157 | return nullptr; |
2158 | } |
2159 | ConsumeAnnotationToken(); |
2160 | return Actions.ActOnOpenMPRequiresDirective(StartLoc, Clauses); |
2161 | } |
2162 | case OMPD_assumes: |
2163 | case OMPD_begin_assumes: |
2164 | ParseOpenMPAssumesDirective(DKind, ConsumeToken()); |
2165 | break; |
2166 | case OMPD_end_assumes: |
2167 | ParseOpenMPEndAssumesDirective(ConsumeToken()); |
2168 | break; |
2169 | case OMPD_declare_reduction: |
2170 | ConsumeToken(); |
2171 | if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) { |
2172 | skipUntilPragmaOpenMPEnd(OMPD_declare_reduction); |
2173 | // Skip the last annot_pragma_openmp_end. |
2174 | ConsumeAnnotationToken(); |
2175 | return Res; |
2176 | } |
2177 | break; |
2178 | case OMPD_declare_mapper: { |
2179 | ConsumeToken(); |
2180 | if (DeclGroupPtrTy Res = ParseOpenMPDeclareMapperDirective(AS)) { |
2181 | // Skip the last annot_pragma_openmp_end. |
2182 | ConsumeAnnotationToken(); |
2183 | return Res; |
2184 | } |
2185 | break; |
2186 | } |
2187 | case OMPD_begin_declare_variant: { |
2188 | // The syntax is: |
2189 | // { #pragma omp begin declare variant clause } |
2190 | // <function-declaration-or-definition-sequence> |
2191 | // { #pragma omp end declare variant } |
2192 | // |
2193 | ConsumeToken(); |
2194 | OMPTraitInfo *ParentTI = Actions.getOMPTraitInfoForSurroundingScope(); |
2195 | ASTContext &ASTCtx = Actions.getASTContext(); |
2196 | OMPTraitInfo &TI = ASTCtx.getNewOMPTraitInfo(); |
2197 | if (parseOMPDeclareVariantMatchClause(Loc, TI, ParentTI)) { |
2198 | while (!SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch)) |
2199 | ; |
2200 | // Skip the last annot_pragma_openmp_end. |
2201 | (void)ConsumeAnnotationToken(); |
2202 | break; |
2203 | } |
2204 | |
2205 | // Skip last tokens. |
2206 | skipUntilPragmaOpenMPEnd(OMPD_begin_declare_variant); |
2207 | |
2208 | ParsingOpenMPDirectiveRAII NormalScope(*this, /*Value=*/false); |
2209 | |
2210 | VariantMatchInfo VMI; |
2211 | TI.getAsVariantMatchInfo(ASTCtx, VMI); |
2212 | |
2213 | std::function<void(StringRef)> DiagUnknownTrait = |
2214 | [this, Loc](StringRef ISATrait) { |
2215 | // TODO Track the selector locations in a way that is accessible here |
2216 | // to improve the diagnostic location. |
2217 | Diag(Loc, diag::warn_unknown_declare_variant_isa_trait) << ISATrait; |
2218 | }; |
2219 | TargetOMPContext OMPCtx( |
2220 | ASTCtx, std::move(DiagUnknownTrait), |
2221 | /* CurrentFunctionDecl */ nullptr, |
2222 | /* ConstructTraits */ ArrayRef<llvm::omp::TraitProperty>()); |
2223 | |
2224 | if (isVariantApplicableInContext(VMI, OMPCtx, /* DeviceSetOnly */ true)) { |
2225 | Actions.ActOnOpenMPBeginDeclareVariant(Loc, TI); |
2226 | break; |
2227 | } |
2228 | |
2229 | // Elide all the code till the matching end declare variant was found. |
2230 | unsigned Nesting = 1; |
2231 | SourceLocation DKLoc; |
2232 | OpenMPDirectiveKind DK = OMPD_unknown; |
2233 | do { |
2234 | DKLoc = Tok.getLocation(); |
2235 | DK = parseOpenMPDirectiveKind(*this); |
2236 | if (DK == OMPD_end_declare_variant) |
2237 | --Nesting; |
2238 | else if (DK == OMPD_begin_declare_variant) |
2239 | ++Nesting; |
2240 | if (!Nesting || isEofOrEom()) |
2241 | break; |
2242 | ConsumeAnyToken(); |
2243 | } while (true); |
2244 | |
2245 | parseOMPEndDirective(OMPD_begin_declare_variant, OMPD_end_declare_variant, |
2246 | DK, Loc, DKLoc, /* SkipUntilOpenMPEnd */ true); |
2247 | if (isEofOrEom()) |
2248 | return nullptr; |
2249 | break; |
2250 | } |
2251 | case OMPD_end_declare_variant: { |
2252 | if (Actions.isInOpenMPDeclareVariantScope()) |
2253 | Actions.ActOnOpenMPEndDeclareVariant(); |
2254 | else |
2255 | Diag(Loc, diag::err_expected_begin_declare_variant); |
2256 | ConsumeToken(); |
2257 | break; |
2258 | } |
2259 | case OMPD_declare_variant: |
2260 | case OMPD_declare_simd: { |
2261 | // The syntax is: |
2262 | // { #pragma omp declare {simd|variant} } |
2263 | // <function-declaration-or-definition> |
2264 | // |
2265 | CachedTokens Toks; |
2266 | Toks.push_back(Tok); |
2267 | ConsumeToken(); |
2268 | while (Tok.isNot(tok::annot_pragma_openmp_end)) { |
2269 | Toks.push_back(Tok); |
2270 | ConsumeAnyToken(); |
2271 | } |
2272 | Toks.push_back(Tok); |
2273 | ConsumeAnyToken(); |
2274 | |
2275 | DeclGroupPtrTy Ptr; |
2276 | if (Tok.isOneOf(tok::annot_pragma_openmp, tok::annot_attr_openmp)) { |
2277 | Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, Delayed, |
2278 | TagType, Tag); |
2279 | } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) { |
2280 | // Here we expect to see some function declaration. |
2281 | if (AS == AS_none) { |
2282 | assert(TagType == DeclSpec::TST_unspecified)(static_cast <bool> (TagType == DeclSpec::TST_unspecified ) ? void (0) : __assert_fail ("TagType == DeclSpec::TST_unspecified" , "clang/lib/Parse/ParseOpenMP.cpp", 2282, __extension__ __PRETTY_FUNCTION__ )); |
2283 | MaybeParseCXX11Attributes(Attrs); |
2284 | ParsingDeclSpec PDS(*this); |
2285 | Ptr = ParseExternalDeclaration(Attrs, &PDS); |
2286 | } else { |
2287 | Ptr = |
2288 | ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag); |
2289 | } |
2290 | } |
2291 | if (!Ptr) { |
2292 | Diag(Loc, diag::err_omp_decl_in_declare_simd_variant) |
2293 | << (DKind == OMPD_declare_simd ? 0 : 1); |
2294 | return DeclGroupPtrTy(); |
2295 | } |
2296 | if (DKind == OMPD_declare_simd) |
2297 | return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc); |
2298 | assert(DKind == OMPD_declare_variant &&(static_cast <bool> (DKind == OMPD_declare_variant && "Expected declare variant directive only") ? void (0) : __assert_fail ("DKind == OMPD_declare_variant && \"Expected declare variant directive only\"" , "clang/lib/Parse/ParseOpenMP.cpp", 2299, __extension__ __PRETTY_FUNCTION__ )) |
2299 | "Expected declare variant directive only")(static_cast <bool> (DKind == OMPD_declare_variant && "Expected declare variant directive only") ? void (0) : __assert_fail ("DKind == OMPD_declare_variant && \"Expected declare variant directive only\"" , "clang/lib/Parse/ParseOpenMP.cpp", 2299, __extension__ __PRETTY_FUNCTION__ )); |
2300 | ParseOMPDeclareVariantClauses(Ptr, Toks, Loc); |
2301 | return Ptr; |
2302 | } |
2303 | case OMPD_begin_declare_target: |
2304 | case OMPD_declare_target: { |
2305 | SourceLocation DTLoc = ConsumeAnyToken(); |
2306 | bool HasClauses = Tok.isNot(tok::annot_pragma_openmp_end); |
2307 | Sema::DeclareTargetContextInfo DTCI(DKind, DTLoc); |
2308 | if (HasClauses) |
2309 | ParseOMPDeclareTargetClauses(DTCI); |
2310 | bool HasImplicitMappings = |
2311 | DKind == OMPD_begin_declare_target || !HasClauses || |
2312 | (DTCI.ExplicitlyMapped.empty() && DTCI.Indirect.hasValue()); |
2313 | |
2314 | // Skip the last annot_pragma_openmp_end. |
2315 | ConsumeAnyToken(); |
2316 | |
2317 | if (HasImplicitMappings) { |
2318 | Actions.ActOnStartOpenMPDeclareTargetContext(DTCI); |
2319 | return nullptr; |
2320 | } |
2321 | |
2322 | Actions.ActOnFinishedOpenMPDeclareTargetContext(DTCI); |
2323 | llvm::SmallVector<Decl *, 4> Decls; |
2324 | for (auto &It : DTCI.ExplicitlyMapped) |
2325 | Decls.push_back(It.first); |
2326 | return Actions.BuildDeclaratorGroup(Decls); |
2327 | } |
2328 | case OMPD_end_declare_target: { |
2329 | if (!Actions.isInOpenMPDeclareTargetContext()) { |
2330 | Diag(Tok, diag::err_omp_unexpected_directive) |
2331 | << 1 << getOpenMPDirectiveName(DKind); |
2332 | break; |
2333 | } |
2334 | const Sema::DeclareTargetContextInfo &DTCI = |
2335 | Actions.ActOnOpenMPEndDeclareTargetDirective(); |
2336 | ParseOMPEndDeclareTargetDirective(DTCI.Kind, DKind, DTCI.Loc); |
2337 | return nullptr; |
2338 | } |
2339 | case OMPD_unknown: |
2340 | Diag(Tok, diag::err_omp_unknown_directive); |
2341 | break; |
2342 | case OMPD_parallel: |
2343 | case OMPD_simd: |
2344 | case OMPD_tile: |
2345 | case OMPD_unroll: |
2346 | case OMPD_task: |
2347 | case OMPD_taskyield: |
2348 | case OMPD_barrier: |
2349 | case OMPD_taskwait: |
2350 | case OMPD_taskgroup: |
2351 | case OMPD_flush: |
2352 | case OMPD_depobj: |
2353 | case OMPD_scan: |
2354 | case OMPD_for: |
2355 | case OMPD_for_simd: |
2356 | case OMPD_sections: |
2357 | case OMPD_section: |
2358 | case OMPD_single: |
2359 | case OMPD_master: |
2360 | case OMPD_ordered: |
2361 | case OMPD_critical: |
2362 | case OMPD_parallel_for: |
2363 | case OMPD_parallel_for_simd: |
2364 | case OMPD_parallel_sections: |
2365 | case OMPD_parallel_master: |
2366 | case OMPD_atomic: |
2367 | case OMPD_target: |
2368 | case OMPD_teams: |
2369 | case OMPD_cancellation_point: |
2370 | case OMPD_cancel: |
2371 | case OMPD_target_data: |
2372 | case OMPD_target_enter_data: |
2373 | case OMPD_target_exit_data: |
2374 | case OMPD_target_parallel: |
2375 | case OMPD_target_parallel_for: |
2376 | case OMPD_taskloop: |
2377 | case OMPD_taskloop_simd: |
2378 | case OMPD_master_taskloop: |
2379 | case OMPD_master_taskloop_simd: |
2380 | case OMPD_parallel_master_taskloop: |
2381 | case OMPD_parallel_master_taskloop_simd: |
2382 | case OMPD_distribute: |
2383 | case OMPD_target_update: |
2384 | case OMPD_distribute_parallel_for: |
2385 | case OMPD_distribute_parallel_for_simd: |
2386 | case OMPD_distribute_simd: |
2387 | case OMPD_target_parallel_for_simd: |
2388 | case OMPD_target_simd: |
2389 | case OMPD_teams_distribute: |
2390 | case OMPD_teams_distribute_simd: |
2391 | case OMPD_teams_distribute_parallel_for_simd: |
2392 | case OMPD_teams_distribute_parallel_for: |
2393 | case OMPD_target_teams: |
2394 | case OMPD_target_teams_distribute: |
2395 | case OMPD_target_teams_distribute_parallel_for: |
2396 | case OMPD_target_teams_distribute_parallel_for_simd: |
2397 | case OMPD_target_teams_distribute_simd: |
2398 | case OMPD_dispatch: |
2399 | case OMPD_masked: |
2400 | case OMPD_metadirective: |
2401 | case OMPD_loop: |
2402 | Diag(Tok, diag::err_omp_unexpected_directive) |
2403 | << 1 << getOpenMPDirectiveName(DKind); |
2404 | break; |
2405 | default: |
2406 | break; |
2407 | } |
2408 | while (Tok.isNot(tok::annot_pragma_openmp_end)) |
2409 | ConsumeAnyToken(); |
2410 | ConsumeAnyToken(); |
2411 | return nullptr; |
2412 | } |
2413 | |
2414 | /// Parsing of declarative or executable OpenMP directives. |
2415 | /// |
2416 | /// threadprivate-directive: |
2417 | /// annot_pragma_openmp 'threadprivate' simple-variable-list |
2418 | /// annot_pragma_openmp_end |
2419 | /// |
2420 | /// allocate-directive: |
2421 | /// annot_pragma_openmp 'allocate' simple-variable-list |
2422 | /// annot_pragma_openmp_end |
2423 | /// |
2424 | /// declare-reduction-directive: |
2425 | /// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':' |
2426 | /// <type> {',' <type>} ':' <expression> ')' ['initializer' '(' |
2427 | /// ('omp_priv' '=' <expression>|<function_call>) ')'] |
2428 | /// annot_pragma_openmp_end |
2429 | /// |
2430 | /// declare-mapper-directive: |
2431 | /// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':'] |
2432 | /// <type> <var> ')' [<clause>[[,] <clause>] ... ] |
2433 | /// annot_pragma_openmp_end |
2434 | /// |
2435 | /// executable-directive: |
2436 | /// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' | |
2437 | /// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] | |
2438 | /// 'parallel for' | 'parallel sections' | 'parallel master' | 'task' | |
2439 | /// 'taskyield' | 'barrier' | 'taskwait' | 'flush' | 'ordered' | |
2440 | /// 'atomic' | 'for simd' | 'parallel for simd' | 'target' | 'target |
2441 | /// data' | 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' | |
2442 | /// 'master taskloop' | 'master taskloop simd' | 'parallel master |
2443 | /// taskloop' | 'parallel master taskloop simd' | 'distribute' | 'target |
2444 | /// enter data' | 'target exit data' | 'target parallel' | 'target |
2445 | /// parallel for' | 'target update' | 'distribute parallel for' | |
2446 | /// 'distribute paralle for simd' | 'distribute simd' | 'target parallel |
2447 | /// for simd' | 'target simd' | 'teams distribute' | 'teams distribute |
2448 | /// simd' | 'teams distribute parallel for simd' | 'teams distribute |
2449 | /// parallel for' | 'target teams' | 'target teams distribute' | 'target |
2450 | /// teams distribute parallel for' | 'target teams distribute parallel |
2451 | /// for simd' | 'target teams distribute simd' | 'masked' {clause} |
2452 | /// annot_pragma_openmp_end |
2453 | /// |
2454 | StmtResult |
2455 | Parser::ParseOpenMPDeclarativeOrExecutableDirective(ParsedStmtContext StmtCtx) { |
2456 | static bool ReadDirectiveWithinMetadirective = false; |
2457 | if (!ReadDirectiveWithinMetadirective) |
2458 | assert(Tok.isOneOf(tok::annot_pragma_openmp, tok::annot_attr_openmp) &&(static_cast <bool> (Tok.isOneOf(tok::annot_pragma_openmp , tok::annot_attr_openmp) && "Not an OpenMP directive!" ) ? void (0) : __assert_fail ("Tok.isOneOf(tok::annot_pragma_openmp, tok::annot_attr_openmp) && \"Not an OpenMP directive!\"" , "clang/lib/Parse/ParseOpenMP.cpp", 2459, __extension__ __PRETTY_FUNCTION__ )) |
2459 | "Not an OpenMP directive!")(static_cast <bool> (Tok.isOneOf(tok::annot_pragma_openmp , tok::annot_attr_openmp) && "Not an OpenMP directive!" ) ? void (0) : __assert_fail ("Tok.isOneOf(tok::annot_pragma_openmp, tok::annot_attr_openmp) && \"Not an OpenMP directive!\"" , "clang/lib/Parse/ParseOpenMP.cpp", 2459, __extension__ __PRETTY_FUNCTION__ )); |
2460 | ParsingOpenMPDirectiveRAII DirScope(*this); |
2461 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
2462 | SmallVector<OMPClause *, 5> Clauses; |
2463 | SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, |
2464 | llvm::omp::Clause_enumSize + 1> |
2465 | FirstClauses(llvm::omp::Clause_enumSize + 1); |
2466 | unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope | |
2467 | Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope; |
2468 | SourceLocation Loc = ReadDirectiveWithinMetadirective |
2469 | ? Tok.getLocation() |
2470 | : ConsumeAnnotationToken(), |
2471 | EndLoc; |
2472 | OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this); |
2473 | if (ReadDirectiveWithinMetadirective && DKind == OMPD_unknown) { |
2474 | Diag(Tok, diag::err_omp_unknown_directive); |
2475 | return StmtError(); |
2476 | } |
2477 | OpenMPDirectiveKind CancelRegion = OMPD_unknown; |
2478 | // Name of critical directive. |
2479 | DeclarationNameInfo DirName; |
2480 | StmtResult Directive = StmtError(); |
2481 | bool HasAssociatedStatement = true; |
2482 | |
2483 | switch (DKind) { |
2484 | case OMPD_metadirective: { |
2485 | ConsumeToken(); |
2486 | SmallVector<VariantMatchInfo, 4> VMIs; |
2487 | |
2488 | // First iteration of parsing all clauses of metadirective. |
2489 | // This iteration only parses and collects all context selector ignoring the |
2490 | // associated directives. |
2491 | TentativeParsingAction TPA(*this); |
2492 | ASTContext &ASTContext = Actions.getASTContext(); |
2493 | |
2494 | BalancedDelimiterTracker T(*this, tok::l_paren, |
2495 | tok::annot_pragma_openmp_end); |
2496 | while (Tok.isNot(tok::annot_pragma_openmp_end)) { |
2497 | OpenMPClauseKind CKind = Tok.isAnnotation() |
2498 | ? OMPC_unknown |
2499 | : getOpenMPClauseKind(PP.getSpelling(Tok)); |
2500 | SourceLocation Loc = ConsumeToken(); |
2501 | |
2502 | // Parse '('. |
2503 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
2504 | getOpenMPClauseName(CKind).data())) |
2505 | return Directive; |
2506 | |
2507 | OMPTraitInfo &TI = Actions.getASTContext().getNewOMPTraitInfo(); |
2508 | if (CKind == OMPC_when) { |
2509 | // parse and get OMPTraitInfo to pass to the When clause |
2510 | parseOMPContextSelectors(Loc, TI); |
2511 | if (TI.Sets.size() == 0) { |
2512 | Diag(Tok, diag::err_omp_expected_context_selector) << "when clause"; |
2513 | TPA.Commit(); |
2514 | return Directive; |
2515 | } |
2516 | |
2517 | // Parse ':' |
2518 | if (Tok.is(tok::colon)) |
2519 | ConsumeAnyToken(); |
2520 | else { |
2521 | Diag(Tok, diag::err_omp_expected_colon) << "when clause"; |
2522 | TPA.Commit(); |
2523 | return Directive; |
2524 | } |
2525 | } |
2526 | // Skip Directive for now. We will parse directive in the second iteration |
2527 | int paren = 0; |
2528 | while (Tok.isNot(tok::r_paren) || paren != 0) { |
2529 | if (Tok.is(tok::l_paren)) |
2530 | paren++; |
2531 | if (Tok.is(tok::r_paren)) |
2532 | paren--; |
2533 | if (Tok.is(tok::annot_pragma_openmp_end)) { |
2534 | Diag(Tok, diag::err_omp_expected_punc) |
2535 | << getOpenMPClauseName(CKind) << 0; |
2536 | TPA.Commit(); |
2537 | return Directive; |
2538 | } |
2539 | ConsumeAnyToken(); |
2540 | } |
2541 | // Parse ')' |
2542 | if (Tok.is(tok::r_paren)) |
2543 | T.consumeClose(); |
2544 | |
2545 | VariantMatchInfo VMI; |
2546 | TI.getAsVariantMatchInfo(ASTContext, VMI); |
2547 | |
2548 | VMIs.push_back(VMI); |
2549 | } |
2550 | |
2551 | TPA.Revert(); |
2552 | // End of the first iteration. Parser is reset to the start of metadirective |
2553 | |
2554 | std::function<void(StringRef)> DiagUnknownTrait = |
2555 | [this, Loc](StringRef ISATrait) { |
2556 | // TODO Track the selector locations in a way that is accessible here |
2557 | // to improve the diagnostic location. |
2558 | Diag(Loc, diag::warn_unknown_declare_variant_isa_trait) << ISATrait; |
2559 | }; |
2560 | TargetOMPContext OMPCtx(ASTContext, std::move(DiagUnknownTrait), |
2561 | /* CurrentFunctionDecl */ nullptr, |
2562 | ArrayRef<llvm::omp::TraitProperty>()); |
2563 | |
2564 | // A single match is returned for OpenMP 5.0 |
2565 | int BestIdx = getBestVariantMatchForContext(VMIs, OMPCtx); |
2566 | |
2567 | int Idx = 0; |
2568 | // In OpenMP 5.0 metadirective is either replaced by another directive or |
2569 | // ignored. |
2570 | // TODO: In OpenMP 5.1 generate multiple directives based upon the matches |
2571 | // found by getBestWhenMatchForContext. |
2572 | while (Tok.isNot(tok::annot_pragma_openmp_end)) { |
2573 | // OpenMP 5.0 implementation - Skip to the best index found. |
2574 | if (Idx++ != BestIdx) { |
2575 | ConsumeToken(); // Consume clause name |
2576 | T.consumeOpen(); // Consume '(' |
2577 | int paren = 0; |
2578 | // Skip everything inside the clause |
2579 | while (Tok.isNot(tok::r_paren) || paren != 0) { |
2580 | if (Tok.is(tok::l_paren)) |
2581 | paren++; |
2582 | if (Tok.is(tok::r_paren)) |
2583 | paren--; |
2584 | ConsumeAnyToken(); |
2585 | } |
2586 | // Parse ')' |
2587 | if (Tok.is(tok::r_paren)) |
2588 | T.consumeClose(); |
2589 | continue; |
2590 | } |
2591 | |
2592 | OpenMPClauseKind CKind = Tok.isAnnotation() |
2593 | ? OMPC_unknown |
2594 | : getOpenMPClauseKind(PP.getSpelling(Tok)); |
2595 | SourceLocation Loc = ConsumeToken(); |
2596 | |
2597 | // Parse '('. |
2598 | T.consumeOpen(); |
2599 | |
2600 | // Skip ContextSelectors for when clause |
2601 | if (CKind == OMPC_when) { |
2602 | OMPTraitInfo &TI = Actions.getASTContext().getNewOMPTraitInfo(); |
2603 | // parse and skip the ContextSelectors |
2604 | parseOMPContextSelectors(Loc, TI); |
2605 | |
2606 | // Parse ':' |
2607 | ConsumeAnyToken(); |
2608 | } |
2609 | |
2610 | // If no directive is passed, skip in OpenMP 5.0. |
2611 | // TODO: Generate nothing directive from OpenMP 5.1. |
2612 | if (Tok.is(tok::r_paren)) { |
2613 | SkipUntil(tok::annot_pragma_openmp_end); |
2614 | break; |
2615 | } |
2616 | |
2617 | // Parse Directive |
2618 | ReadDirectiveWithinMetadirective = true; |
2619 | Directive = ParseOpenMPDeclarativeOrExecutableDirective(StmtCtx); |
2620 | ReadDirectiveWithinMetadirective = false; |
2621 | break; |
2622 | } |
2623 | break; |
2624 | } |
2625 | case OMPD_threadprivate: { |
2626 | // FIXME: Should this be permitted in C++? |
2627 | if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) == |
2628 | ParsedStmtContext()) { |
2629 | Diag(Tok, diag::err_omp_immediate_directive) |
2630 | << getOpenMPDirectiveName(DKind) << 0; |
2631 | } |
2632 | ConsumeToken(); |
2633 | DeclDirectiveListParserHelper Helper(this, DKind); |
2634 | if (!ParseOpenMPSimpleVarList(DKind, Helper, |
2635 | /*AllowScopeSpecifier=*/false)) { |
2636 | skipUntilPragmaOpenMPEnd(DKind); |
2637 | DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective( |
2638 | Loc, Helper.getIdentifiers()); |
2639 | Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation()); |
2640 | } |
2641 | SkipUntil(tok::annot_pragma_openmp_end); |
2642 | break; |
2643 | } |
2644 | case OMPD_allocate: { |
2645 | // FIXME: Should this be permitted in C++? |
2646 | if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) == |
2647 | ParsedStmtContext()) { |
2648 | Diag(Tok, diag::err_omp_immediate_directive) |
2649 | << getOpenMPDirectiveName(DKind) << 0; |
2650 | } |
2651 | ConsumeToken(); |
2652 | DeclDirectiveListParserHelper Helper(this, DKind); |
2653 | if (!ParseOpenMPSimpleVarList(DKind, Helper, |
2654 | /*AllowScopeSpecifier=*/false)) { |
2655 | SmallVector<OMPClause *, 1> Clauses; |
2656 | if (Tok.isNot(tok::annot_pragma_openmp_end)) { |
2657 | SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, |
2658 | llvm::omp::Clause_enumSize + 1> |
2659 | FirstClauses(llvm::omp::Clause_enumSize + 1); |
2660 | while (Tok.isNot(tok::annot_pragma_openmp_end)) { |
2661 | OpenMPClauseKind CKind = |
2662 | Tok.isAnnotation() ? OMPC_unknown |
2663 | : getOpenMPClauseKind(PP.getSpelling(Tok)); |
2664 | Actions.StartOpenMPClause(CKind); |
2665 | OMPClause *Clause = ParseOpenMPClause( |
2666 | OMPD_allocate, CKind, !FirstClauses[unsigned(CKind)].getInt()); |
2667 | SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end, |
2668 | StopBeforeMatch); |
2669 | FirstClauses[unsigned(CKind)].setInt(true); |
2670 | if (Clause != nullptr) |
2671 | Clauses.push_back(Clause); |
2672 | if (Tok.is(tok::annot_pragma_openmp_end)) { |
2673 | Actions.EndOpenMPClause(); |
2674 | break; |
2675 | } |
2676 | // Skip ',' if any. |
2677 | if (Tok.is(tok::comma)) |
2678 | ConsumeToken(); |
2679 | Actions.EndOpenMPClause(); |
2680 | } |
2681 | skipUntilPragmaOpenMPEnd(DKind); |
2682 | } |
2683 | DeclGroupPtrTy Res = Actions.ActOnOpenMPAllocateDirective( |
2684 | Loc, Helper.getIdentifiers(), Clauses); |
2685 | Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation()); |
2686 | } |
2687 | SkipUntil(tok::annot_pragma_openmp_end); |
2688 | break; |
2689 | } |
2690 | case OMPD_declare_reduction: |
2691 | ConsumeToken(); |
2692 | if (DeclGroupPtrTy Res = |
2693 | ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) { |
2694 | skipUntilPragmaOpenMPEnd(OMPD_declare_reduction); |
2695 | ConsumeAnyToken(); |
2696 | Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation()); |
2697 | } else { |
2698 | SkipUntil(tok::annot_pragma_openmp_end); |
2699 | } |
2700 | break; |
2701 | case OMPD_declare_mapper: { |
2702 | ConsumeToken(); |
2703 | if (DeclGroupPtrTy Res = |
2704 | ParseOpenMPDeclareMapperDirective(/*AS=*/AS_none)) { |
2705 | // Skip the last annot_pragma_openmp_end. |
2706 | ConsumeAnnotationToken(); |
2707 | Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation()); |
2708 | } else { |
2709 | SkipUntil(tok::annot_pragma_openmp_end); |
2710 | } |
2711 | break; |
2712 | } |
2713 | case OMPD_flush: |
2714 | case OMPD_depobj: |
2715 | case OMPD_scan: |
2716 | case OMPD_taskyield: |
2717 | case OMPD_barrier: |
2718 | case OMPD_taskwait: |
2719 | case OMPD_cancellation_point: |
2720 | case OMPD_cancel: |
2721 | case OMPD_target_enter_data: |
2722 | case OMPD_target_exit_data: |
2723 | case OMPD_target_update: |
2724 | case OMPD_interop: |
2725 | if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) == |
2726 | ParsedStmtContext()) { |
2727 | Diag(Tok, diag::err_omp_immediate_directive) |
2728 | << getOpenMPDirectiveName(DKind) << 0; |
2729 | } |
2730 | HasAssociatedStatement = false; |
2731 | // Fall through for further analysis. |
2732 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; |
2733 | case OMPD_parallel: |
2734 | case OMPD_simd: |
2735 | case OMPD_tile: |
2736 | case OMPD_unroll: |
2737 | case OMPD_for: |
2738 | case OMPD_for_simd: |
2739 | case OMPD_sections: |
2740 | case OMPD_single: |
2741 | case OMPD_section: |
2742 | case OMPD_master: |
2743 | case OMPD_critical: |
2744 | case OMPD_parallel_for: |
2745 | case OMPD_parallel_for_simd: |
2746 | case OMPD_parallel_sections: |
2747 | case OMPD_parallel_master: |
2748 | case OMPD_task: |
2749 | case OMPD_ordered: |
2750 | case OMPD_atomic: |
2751 | case OMPD_target: |
2752 | case OMPD_teams: |
2753 | case OMPD_taskgroup: |
2754 | case OMPD_target_data: |
2755 | case OMPD_target_parallel: |
2756 | case OMPD_target_parallel_for: |
2757 | case OMPD_loop: |
2758 | case OMPD_taskloop: |
2759 | case OMPD_taskloop_simd: |
2760 | case OMPD_master_taskloop: |
2761 | case OMPD_master_taskloop_simd: |
2762 | case OMPD_parallel_master_taskloop: |
2763 | case OMPD_parallel_master_taskloop_simd: |
2764 | case OMPD_distribute: |
2765 | case OMPD_distribute_parallel_for: |
2766 | case OMPD_distribute_parallel_for_simd: |
2767 | case OMPD_distribute_simd: |
2768 | case OMPD_target_parallel_for_simd: |
2769 | case OMPD_target_simd: |
2770 | case OMPD_teams_distribute: |
2771 | case OMPD_teams_distribute_simd: |
2772 | case OMPD_teams_distribute_parallel_for_simd: |
2773 | case OMPD_teams_distribute_parallel_for: |
2774 | case OMPD_target_teams: |
2775 | case OMPD_target_teams_distribute: |
2776 | case OMPD_target_teams_distribute_parallel_for: |
2777 | case OMPD_target_teams_distribute_parallel_for_simd: |
2778 | case OMPD_target_teams_distribute_simd: |
2779 | case OMPD_dispatch: |
2780 | case OMPD_masked: { |
2781 | // Special processing for flush and depobj clauses. |
2782 | Token ImplicitTok; |
2783 | bool ImplicitClauseAllowed = false; |
2784 | if (DKind == OMPD_flush || DKind == OMPD_depobj) { |
2785 | ImplicitTok = Tok; |
2786 | ImplicitClauseAllowed = true; |
2787 | } |
2788 | ConsumeToken(); |
2789 | // Parse directive name of the 'critical' directive if any. |
2790 | if (DKind == OMPD_critical) { |
2791 | BalancedDelimiterTracker T(*this, tok::l_paren, |
2792 | tok::annot_pragma_openmp_end); |
2793 | if (!T.consumeOpen()) { |
2794 | if (Tok.isAnyIdentifier()) { |
2795 | DirName = |
2796 | DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation()); |
2797 | ConsumeAnyToken(); |
2798 | } else { |
2799 | Diag(Tok, diag::err_omp_expected_identifier_for_critical); |
2800 | } |
2801 | T.consumeClose(); |
2802 | } |
2803 | } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) { |
2804 | CancelRegion = parseOpenMPDirectiveKind(*this); |
2805 | if (Tok.isNot(tok::annot_pragma_openmp_end)) |
2806 | ConsumeToken(); |
2807 | } |
2808 | |
2809 | if (isOpenMPLoopDirective(DKind)) |
2810 | ScopeFlags |= Scope::OpenMPLoopDirectiveScope; |
2811 | if (isOpenMPSimdDirective(DKind)) |
2812 | ScopeFlags |= Scope::OpenMPSimdDirectiveScope; |
2813 | ParseScope OMPDirectiveScope(this, ScopeFlags); |
2814 | Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc); |
2815 | |
2816 | while (Tok.isNot(tok::annot_pragma_openmp_end)) { |
2817 | // If we are parsing for a directive within a metadirective, the directive |
2818 | // ends with a ')'. |
2819 | if (ReadDirectiveWithinMetadirective && Tok.is(tok::r_paren)) { |
2820 | while (Tok.isNot(tok::annot_pragma_openmp_end)) |
2821 | ConsumeAnyToken(); |
2822 | break; |
2823 | } |
2824 | bool HasImplicitClause = false; |
2825 | if (ImplicitClauseAllowed && Tok.is(tok::l_paren)) { |
2826 | HasImplicitClause = true; |
2827 | // Push copy of the current token back to stream to properly parse |
2828 | // pseudo-clause OMPFlushClause or OMPDepobjClause. |
2829 | PP.EnterToken(Tok, /*IsReinject*/ true); |
2830 | PP.EnterToken(ImplicitTok, /*IsReinject*/ true); |
2831 | ConsumeAnyToken(); |
2832 | } |
2833 | OpenMPClauseKind CKind = Tok.isAnnotation() |
2834 | ? OMPC_unknown |
2835 | : getOpenMPClauseKind(PP.getSpelling(Tok)); |
2836 | if (HasImplicitClause) { |
2837 | assert(CKind == OMPC_unknown && "Must be unknown implicit clause.")(static_cast <bool> (CKind == OMPC_unknown && "Must be unknown implicit clause." ) ? void (0) : __assert_fail ("CKind == OMPC_unknown && \"Must be unknown implicit clause.\"" , "clang/lib/Parse/ParseOpenMP.cpp", 2837, __extension__ __PRETTY_FUNCTION__ )); |
2838 | if (DKind == OMPD_flush) { |
2839 | CKind = OMPC_flush; |
2840 | } else { |
2841 | assert(DKind == OMPD_depobj &&(static_cast <bool> (DKind == OMPD_depobj && "Expected flush or depobj directives." ) ? void (0) : __assert_fail ("DKind == OMPD_depobj && \"Expected flush or depobj directives.\"" , "clang/lib/Parse/ParseOpenMP.cpp", 2842, __extension__ __PRETTY_FUNCTION__ )) |
2842 | "Expected flush or depobj directives.")(static_cast <bool> (DKind == OMPD_depobj && "Expected flush or depobj directives." ) ? void (0) : __assert_fail ("DKind == OMPD_depobj && \"Expected flush or depobj directives.\"" , "clang/lib/Parse/ParseOpenMP.cpp", 2842, __extension__ __PRETTY_FUNCTION__ )); |
2843 | CKind = OMPC_depobj; |
2844 | } |
2845 | } |
2846 | // No more implicit clauses allowed. |
2847 | ImplicitClauseAllowed = false; |
2848 | Actions.StartOpenMPClause(CKind); |
2849 | HasImplicitClause = false; |
Value stored to 'HasImplicitClause' is never read | |
2850 | OMPClause *Clause = ParseOpenMPClause( |
2851 | DKind, CKind, !FirstClauses[unsigned(CKind)].getInt()); |
2852 | FirstClauses[unsigned(CKind)].setInt(true); |
2853 | if (Clause) { |
2854 | FirstClauses[unsigned(CKind)].setPointer(Clause); |
2855 | Clauses.push_back(Clause); |
2856 | } |
2857 | |
2858 | // Skip ',' if any. |
2859 | if (Tok.is(tok::comma)) |
2860 | ConsumeToken(); |
2861 | Actions.EndOpenMPClause(); |
2862 | } |
2863 | // End location of the directive. |
2864 | EndLoc = Tok.getLocation(); |
2865 | // Consume final annot_pragma_openmp_end. |
2866 | ConsumeAnnotationToken(); |
2867 | |
2868 | // OpenMP [2.13.8, ordered Construct, Syntax] |
2869 | // If the depend clause is specified, the ordered construct is a stand-alone |
2870 | // directive. |
2871 | if (DKind == OMPD_ordered && FirstClauses[unsigned(OMPC_depend)].getInt()) { |
2872 | if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) == |
2873 | ParsedStmtContext()) { |
2874 | Diag(Loc, diag::err_omp_immediate_directive) |
2875 | << getOpenMPDirectiveName(DKind) << 1 |
2876 | << getOpenMPClauseName(OMPC_depend); |
2877 | } |
2878 | HasAssociatedStatement = false; |
2879 | } |
2880 | |
2881 | if (DKind == OMPD_tile && !FirstClauses[unsigned(OMPC_sizes)].getInt()) { |
2882 | Diag(Loc, diag::err_omp_required_clause) |
2883 | << getOpenMPDirectiveName(OMPD_tile) << "sizes"; |
2884 | } |
2885 | |
2886 | StmtResult AssociatedStmt; |
2887 | if (HasAssociatedStatement) { |
2888 | // The body is a block scope like in Lambdas and Blocks. |
2889 | Actions.ActOnOpenMPRegionStart(DKind, getCurScope()); |
2890 | // FIXME: We create a bogus CompoundStmt scope to hold the contents of |
2891 | // the captured region. Code elsewhere assumes that any FunctionScopeInfo |
2892 | // should have at least one compound statement scope within it. |
2893 | ParsingOpenMPDirectiveRAII NormalScope(*this, /*Value=*/false); |
2894 | { |
2895 | Sema::CompoundScopeRAII Scope(Actions); |
2896 | AssociatedStmt = ParseStatement(); |
2897 | |
2898 | if (AssociatedStmt.isUsable() && isOpenMPLoopDirective(DKind) && |
2899 | getLangOpts().OpenMPIRBuilder) |
2900 | AssociatedStmt = Actions.ActOnOpenMPLoopnest(AssociatedStmt.get()); |
2901 | } |
2902 | AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses); |
2903 | } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data || |
2904 | DKind == OMPD_target_exit_data) { |
2905 | Actions.ActOnOpenMPRegionStart(DKind, getCurScope()); |
2906 | AssociatedStmt = (Sema::CompoundScopeRAII(Actions), |
2907 | Actions.ActOnCompoundStmt(Loc, Loc, llvm::None, |
2908 | /*isStmtExpr=*/false)); |
2909 | AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses); |
2910 | } |
2911 | Directive = Actions.ActOnOpenMPExecutableDirective( |
2912 | DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc, |
2913 | EndLoc); |
2914 | |
2915 | // Exit scope. |
2916 | Actions.EndOpenMPDSABlock(Directive.get()); |
2917 | OMPDirectiveScope.Exit(); |
2918 | break; |
2919 | } |
2920 | case OMPD_declare_simd: |
2921 | case OMPD_declare_target: |
2922 | case OMPD_begin_declare_target: |
2923 | case OMPD_end_declare_target: |
2924 | case OMPD_requires: |
2925 | case OMPD_begin_declare_variant: |
2926 | case OMPD_end_declare_variant: |
2927 | case OMPD_declare_variant: |
2928 | Diag(Tok, diag::err_omp_unexpected_directive) |
2929 | << 1 << getOpenMPDirectiveName(DKind); |
2930 | SkipUntil(tok::annot_pragma_openmp_end); |
2931 | break; |
2932 | case OMPD_unknown: |
2933 | default: |
2934 | Diag(Tok, diag::err_omp_unknown_directive); |
2935 | SkipUntil(tok::annot_pragma_openmp_end); |
2936 | break; |
2937 | } |
2938 | return Directive; |
2939 | } |
2940 | |
2941 | // Parses simple list: |
2942 | // simple-variable-list: |
2943 | // '(' id-expression {, id-expression} ')' |
2944 | // |
2945 | bool Parser::ParseOpenMPSimpleVarList( |
2946 | OpenMPDirectiveKind Kind, |
2947 | const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> |
2948 | &Callback, |
2949 | bool AllowScopeSpecifier) { |
2950 | // Parse '('. |
2951 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
2952 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
2953 | getOpenMPDirectiveName(Kind).data())) |
2954 | return true; |
2955 | bool IsCorrect = true; |
2956 | bool NoIdentIsFound = true; |
2957 | |
2958 | // Read tokens while ')' or annot_pragma_openmp_end is not found. |
2959 | while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) { |
2960 | CXXScopeSpec SS; |
2961 | UnqualifiedId Name; |
2962 | // Read var name. |
2963 | Token PrevTok = Tok; |
2964 | NoIdentIsFound = false; |
2965 | |
2966 | if (AllowScopeSpecifier && getLangOpts().CPlusPlus && |
2967 | ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, |
2968 | /*ObjectHasErrors=*/false, false)) { |
2969 | IsCorrect = false; |
2970 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
2971 | StopBeforeMatch); |
2972 | } else if (ParseUnqualifiedId(SS, /*ObjectType=*/nullptr, |
2973 | /*ObjectHadErrors=*/false, false, false, |
2974 | false, false, nullptr, Name)) { |
2975 | IsCorrect = false; |
2976 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
2977 | StopBeforeMatch); |
2978 | } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) && |
2979 | Tok.isNot(tok::annot_pragma_openmp_end)) { |
2980 | IsCorrect = false; |
2981 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
2982 | StopBeforeMatch); |
2983 | Diag(PrevTok.getLocation(), diag::err_expected) |
2984 | << tok::identifier |
2985 | << SourceRange(PrevTok.getLocation(), PrevTokLocation); |
2986 | } else { |
2987 | Callback(SS, Actions.GetNameFromUnqualifiedId(Name)); |
2988 | } |
2989 | // Consume ','. |
2990 | if (Tok.is(tok::comma)) { |
2991 | ConsumeToken(); |
2992 | } |
2993 | } |
2994 | |
2995 | if (NoIdentIsFound) { |
2996 | Diag(Tok, diag::err_expected) << tok::identifier; |
2997 | IsCorrect = false; |
2998 | } |
2999 | |
3000 | // Parse ')'. |
3001 | IsCorrect = !T.consumeClose() && IsCorrect; |
3002 | |
3003 | return !IsCorrect; |
3004 | } |
3005 | |
3006 | OMPClause *Parser::ParseOpenMPSizesClause() { |
3007 | SourceLocation ClauseNameLoc = ConsumeToken(); |
3008 | SmallVector<Expr *, 4> ValExprs; |
3009 | |
3010 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
3011 | if (T.consumeOpen()) { |
3012 | Diag(Tok, diag::err_expected) << tok::l_paren; |
3013 | return nullptr; |
3014 | } |
3015 | |
3016 | while (true) { |
3017 | ExprResult Val = ParseConstantExpression(); |
3018 | if (!Val.isUsable()) { |
3019 | T.skipToEnd(); |
3020 | return nullptr; |
3021 | } |
3022 | |
3023 | ValExprs.push_back(Val.get()); |
3024 | |
3025 | if (Tok.is(tok::r_paren) || Tok.is(tok::annot_pragma_openmp_end)) |
3026 | break; |
3027 | |
3028 | ExpectAndConsume(tok::comma); |
3029 | } |
3030 | |
3031 | T.consumeClose(); |
3032 | |
3033 | return Actions.ActOnOpenMPSizesClause( |
3034 | ValExprs, ClauseNameLoc, T.getOpenLocation(), T.getCloseLocation()); |
3035 | } |
3036 | |
3037 | OMPClause *Parser::ParseOpenMPUsesAllocatorClause(OpenMPDirectiveKind DKind) { |
3038 | SourceLocation Loc = Tok.getLocation(); |
3039 | ConsumeAnyToken(); |
3040 | |
3041 | // Parse '('. |
3042 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
3043 | if (T.expectAndConsume(diag::err_expected_lparen_after, "uses_allocator")) |
3044 | return nullptr; |
3045 | SmallVector<Sema::UsesAllocatorsData, 4> Data; |
3046 | do { |
3047 | ExprResult Allocator = |
3048 | getLangOpts().CPlusPlus ? ParseCXXIdExpression() : ParseExpression(); |
3049 | if (Allocator.isInvalid()) { |
3050 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
3051 | StopBeforeMatch); |
3052 | break; |
3053 | } |
3054 | Sema::UsesAllocatorsData &D = Data.emplace_back(); |
3055 | D.Allocator = Allocator.get(); |
3056 | if (Tok.is(tok::l_paren)) { |
3057 | BalancedDelimiterTracker T(*this, tok::l_paren, |
3058 | tok::annot_pragma_openmp_end); |
3059 | T.consumeOpen(); |
3060 | ExprResult AllocatorTraits = |
3061 | getLangOpts().CPlusPlus ? ParseCXXIdExpression() : ParseExpression(); |
3062 | T.consumeClose(); |
3063 | if (AllocatorTraits.isInvalid()) { |
3064 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
3065 | StopBeforeMatch); |
3066 | break; |
3067 | } |
3068 | D.AllocatorTraits = AllocatorTraits.get(); |
3069 | D.LParenLoc = T.getOpenLocation(); |
3070 | D.RParenLoc = T.getCloseLocation(); |
3071 | } |
3072 | if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren)) |
3073 | Diag(Tok, diag::err_omp_expected_punc) << "uses_allocators" << 0; |
3074 | // Parse ',' |
3075 | if (Tok.is(tok::comma)) |
3076 | ConsumeAnyToken(); |
3077 | } while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)); |
3078 | T.consumeClose(); |
3079 | return Actions.ActOnOpenMPUsesAllocatorClause(Loc, T.getOpenLocation(), |
3080 | T.getCloseLocation(), Data); |
3081 | } |
3082 | |
3083 | /// Parsing of OpenMP clauses. |
3084 | /// |
3085 | /// clause: |
3086 | /// if-clause | final-clause | num_threads-clause | safelen-clause | |
3087 | /// default-clause | private-clause | firstprivate-clause | shared-clause |
3088 | /// | linear-clause | aligned-clause | collapse-clause | bind-clause | |
3089 | /// lastprivate-clause | reduction-clause | proc_bind-clause | |
3090 | /// schedule-clause | copyin-clause | copyprivate-clause | untied-clause | |
3091 | /// mergeable-clause | flush-clause | read-clause | write-clause | |
3092 | /// update-clause | capture-clause | seq_cst-clause | device-clause | |
3093 | /// simdlen-clause | threads-clause | simd-clause | num_teams-clause | |
3094 | /// thread_limit-clause | priority-clause | grainsize-clause | |
3095 | /// nogroup-clause | num_tasks-clause | hint-clause | to-clause | |
3096 | /// from-clause | is_device_ptr-clause | task_reduction-clause | |
3097 | /// in_reduction-clause | allocator-clause | allocate-clause | |
3098 | /// acq_rel-clause | acquire-clause | release-clause | relaxed-clause | |
3099 | /// depobj-clause | destroy-clause | detach-clause | inclusive-clause | |
3100 | /// exclusive-clause | uses_allocators-clause | use_device_addr-clause |
3101 | /// |
3102 | OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind, |
3103 | OpenMPClauseKind CKind, bool FirstClause) { |
3104 | OMPClauseKind = CKind; |
3105 | OMPClause *Clause = nullptr; |
3106 | bool ErrorFound = false; |
3107 | bool WrongDirective = false; |
3108 | // Check if clause is allowed for the given directive. |
3109 | if (CKind != OMPC_unknown && |
3110 | !isAllowedClauseForDirective(DKind, CKind, getLangOpts().OpenMP)) { |
3111 | Diag(Tok, diag::err_omp_unexpected_clause) |
3112 | << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind); |
3113 | ErrorFound = true; |
3114 | WrongDirective = true; |
3115 | } |
3116 | |
3117 | switch (CKind) { |
3118 | case OMPC_final: |
3119 | case OMPC_num_threads: |
3120 | case OMPC_safelen: |
3121 | case OMPC_simdlen: |
3122 | case OMPC_collapse: |
3123 | case OMPC_ordered: |
3124 | case OMPC_num_teams: |
3125 | case OMPC_thread_limit: |
3126 | case OMPC_priority: |
3127 | case OMPC_grainsize: |
3128 | case OMPC_num_tasks: |
3129 | case OMPC_hint: |
3130 | case OMPC_allocator: |
3131 | case OMPC_depobj: |
3132 | case OMPC_detach: |
3133 | case OMPC_novariants: |
3134 | case OMPC_nocontext: |
3135 | case OMPC_filter: |
3136 | case OMPC_partial: |
3137 | case OMPC_align: |
3138 | // OpenMP [2.5, Restrictions] |
3139 | // At most one num_threads clause can appear on the directive. |
3140 | // OpenMP [2.8.1, simd construct, Restrictions] |
3141 | // Only one safelen clause can appear on a simd directive. |
3142 | // Only one simdlen clause can appear on a simd directive. |
3143 | // Only one collapse clause can appear on a simd directive. |
3144 | // OpenMP [2.11.1, task Construct, Restrictions] |
3145 | // At most one if clause can appear on the directive. |
3146 | // At most one final clause can appear on the directive. |
3147 | // OpenMP [teams Construct, Restrictions] |
3148 | // At most one num_teams clause can appear on the directive. |
3149 | // At most one thread_limit clause can appear on the directive. |
3150 | // OpenMP [2.9.1, task Construct, Restrictions] |
3151 | // At most one priority clause can appear on the directive. |
3152 | // OpenMP [2.9.2, taskloop Construct, Restrictions] |
3153 | // At most one grainsize clause can appear on the directive. |
3154 | // OpenMP [2.9.2, taskloop Construct, Restrictions] |
3155 | // At most one num_tasks clause can appear on the directive. |
3156 | // OpenMP [2.11.3, allocate Directive, Restrictions] |
3157 | // At most one allocator clause can appear on the directive. |
3158 | // OpenMP 5.0, 2.10.1 task Construct, Restrictions. |
3159 | // At most one detach clause can appear on the directive. |
3160 | // OpenMP 5.1, 2.3.6 dispatch Construct, Restrictions. |
3161 | // At most one novariants clause can appear on a dispatch directive. |
3162 | // At most one nocontext clause can appear on a dispatch directive. |
3163 | if (!FirstClause) { |
3164 | Diag(Tok, diag::err_omp_more_one_clause) |
3165 | << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; |
3166 | ErrorFound = true; |
3167 | } |
3168 | |
3169 | if ((CKind == OMPC_ordered || CKind == OMPC_partial) && |
3170 | PP.LookAhead(/*N=*/0).isNot(tok::l_paren)) |
3171 | Clause = ParseOpenMPClause(CKind, WrongDirective); |
3172 | else |
3173 | Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective); |
3174 | break; |
3175 | case OMPC_default: |
3176 | case OMPC_proc_bind: |
3177 | case OMPC_atomic_default_mem_order: |
3178 | case OMPC_order: |
3179 | case OMPC_bind: |
3180 | // OpenMP [2.14.3.1, Restrictions] |
3181 | // Only a single default clause may be specified on a parallel, task or |
3182 | // teams directive. |
3183 | // OpenMP [2.5, parallel Construct, Restrictions] |
3184 | // At most one proc_bind clause can appear on the directive. |
3185 | // OpenMP [5.0, Requires directive, Restrictions] |
3186 | // At most one atomic_default_mem_order clause can appear |
3187 | // on the directive |
3188 | // OpenMP 5.1, 2.11.7 loop Construct, Restrictions. |
3189 | // At most one bind clause can appear on a loop directive. |
3190 | if (!FirstClause && CKind != OMPC_order) { |
3191 | Diag(Tok, diag::err_omp_more_one_clause) |
3192 | << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; |
3193 | ErrorFound = true; |
3194 | } |
3195 | |
3196 | Clause = ParseOpenMPSimpleClause(CKind, WrongDirective); |
3197 | break; |
3198 | case OMPC_device: |
3199 | case OMPC_schedule: |
3200 | case OMPC_dist_schedule: |
3201 | case OMPC_defaultmap: |
3202 | // OpenMP [2.7.1, Restrictions, p. 3] |
3203 | // Only one schedule clause can appear on a loop directive. |
3204 | // OpenMP 4.5 [2.10.4, Restrictions, p. 106] |
3205 | // At most one defaultmap clause can appear on the directive. |
3206 | // OpenMP 5.0 [2.12.5, target construct, Restrictions] |
3207 | // At most one device clause can appear on the directive. |
3208 | if ((getLangOpts().OpenMP < 50 || CKind != OMPC_defaultmap) && |
3209 | !FirstClause) { |
3210 | Diag(Tok, diag::err_omp_more_one_clause) |
3211 | << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; |
3212 | ErrorFound = true; |
3213 | } |
3214 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; |
3215 | case OMPC_if: |
3216 | Clause = ParseOpenMPSingleExprWithArgClause(DKind, CKind, WrongDirective); |
3217 | break; |
3218 | case OMPC_nowait: |
3219 | case OMPC_untied: |
3220 | case OMPC_mergeable: |
3221 | case OMPC_read: |
3222 | case OMPC_write: |
3223 | case OMPC_capture: |
3224 | case OMPC_compare: |
3225 | case OMPC_seq_cst: |
3226 | case OMPC_acq_rel: |
3227 | case OMPC_acquire: |
3228 | case OMPC_release: |
3229 | case OMPC_relaxed: |
3230 | case OMPC_threads: |
3231 | case OMPC_simd: |
3232 | case OMPC_nogroup: |
3233 | case OMPC_unified_address: |
3234 | case OMPC_unified_shared_memory: |
3235 | case OMPC_reverse_offload: |
3236 | case OMPC_dynamic_allocators: |
3237 | case OMPC_full: |
3238 | // OpenMP [2.7.1, Restrictions, p. 9] |
3239 | // Only one ordered clause can appear on a loop directive. |
3240 | // OpenMP [2.7.1, Restrictions, C/C++, p. 4] |
3241 | // Only one nowait clause can appear on a for directive. |
3242 | // OpenMP [5.0, Requires directive, Restrictions] |
3243 | // Each of the requires clauses can appear at most once on the directive. |
3244 | if (!FirstClause) { |
3245 | Diag(Tok, diag::err_omp_more_one_clause) |
3246 | << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; |
3247 | ErrorFound = true; |
3248 | } |
3249 | |
3250 | Clause = ParseOpenMPClause(CKind, WrongDirective); |
3251 | break; |
3252 | case OMPC_update: |
3253 | if (!FirstClause) { |
3254 | Diag(Tok, diag::err_omp_more_one_clause) |
3255 | << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; |
3256 | ErrorFound = true; |
3257 | } |
3258 | |
3259 | Clause = (DKind == OMPD_depobj) |
3260 | ? ParseOpenMPSimpleClause(CKind, WrongDirective) |
3261 | : ParseOpenMPClause(CKind, WrongDirective); |
3262 | break; |
3263 | case OMPC_private: |
3264 | case OMPC_firstprivate: |
3265 | case OMPC_lastprivate: |
3266 | case OMPC_shared: |
3267 | case OMPC_reduction: |
3268 | case OMPC_task_reduction: |
3269 | case OMPC_in_reduction: |
3270 | case OMPC_linear: |
3271 | case OMPC_aligned: |
3272 | case OMPC_copyin: |
3273 | case OMPC_copyprivate: |
3274 | case OMPC_flush: |
3275 | case OMPC_depend: |
3276 | case OMPC_map: |
3277 | case OMPC_to: |
3278 | case OMPC_from: |
3279 | case OMPC_use_device_ptr: |
3280 | case OMPC_use_device_addr: |
3281 | case OMPC_is_device_ptr: |
3282 | case OMPC_allocate: |
3283 | case OMPC_nontemporal: |
3284 | case OMPC_inclusive: |
3285 | case OMPC_exclusive: |
3286 | case OMPC_affinity: |
3287 | Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective); |
3288 | break; |
3289 | case OMPC_sizes: |
3290 | if (!FirstClause) { |
3291 | Diag(Tok, diag::err_omp_more_one_clause) |
3292 | << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; |
3293 | ErrorFound = true; |
3294 | } |
3295 | |
3296 | Clause = ParseOpenMPSizesClause(); |
3297 | break; |
3298 | case OMPC_uses_allocators: |
3299 | Clause = ParseOpenMPUsesAllocatorClause(DKind); |
3300 | break; |
3301 | case OMPC_destroy: |
3302 | if (DKind != OMPD_interop) { |
3303 | if (!FirstClause) { |
3304 | Diag(Tok, diag::err_omp_more_one_clause) |
3305 | << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0; |
3306 | ErrorFound = true; |
3307 | } |
3308 | Clause = ParseOpenMPClause(CKind, WrongDirective); |
3309 | break; |
3310 | } |
3311 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; |
3312 | case OMPC_init: |
3313 | case OMPC_use: |
3314 | Clause = ParseOpenMPInteropClause(CKind, WrongDirective); |
3315 | break; |
3316 | case OMPC_device_type: |
3317 | case OMPC_unknown: |
3318 | skipUntilPragmaOpenMPEnd(DKind); |
3319 | break; |
3320 | case OMPC_threadprivate: |
3321 | case OMPC_uniform: |
3322 | case OMPC_match: |
3323 | if (!WrongDirective) |
3324 | Diag(Tok, diag::err_omp_unexpected_clause) |
3325 | << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind); |
3326 | SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch); |
3327 | break; |
3328 | default: |
3329 | break; |
3330 | } |
3331 | return ErrorFound ? nullptr : Clause; |
3332 | } |
3333 | |
3334 | /// Parses simple expression in parens for single-expression clauses of OpenMP |
3335 | /// constructs. |
3336 | /// \param RLoc Returned location of right paren. |
3337 | ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName, |
3338 | SourceLocation &RLoc, |
3339 | bool IsAddressOfOperand) { |
3340 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
3341 | if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data())) |
3342 | return ExprError(); |
3343 | |
3344 | SourceLocation ELoc = Tok.getLocation(); |
3345 | ExprResult LHS( |
3346 | ParseCastExpression(AnyCastExpr, IsAddressOfOperand, NotTypeCast)); |
3347 | ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional)); |
3348 | Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false); |
3349 | |
3350 | // Parse ')'. |
3351 | RLoc = Tok.getLocation(); |
3352 | if (!T.consumeClose()) |
3353 | RLoc = T.getCloseLocation(); |
3354 | |
3355 | return Val; |
3356 | } |
3357 | |
3358 | /// Parsing of OpenMP clauses with single expressions like 'final', |
3359 | /// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams', |
3360 | /// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks', 'hint' or |
3361 | /// 'detach'. |
3362 | /// |
3363 | /// final-clause: |
3364 | /// 'final' '(' expression ')' |
3365 | /// |
3366 | /// num_threads-clause: |
3367 | /// 'num_threads' '(' expression ')' |
3368 | /// |
3369 | /// safelen-clause: |
3370 | /// 'safelen' '(' expression ')' |
3371 | /// |
3372 | /// simdlen-clause: |
3373 | /// 'simdlen' '(' expression ')' |
3374 | /// |
3375 | /// collapse-clause: |
3376 | /// 'collapse' '(' expression ')' |
3377 | /// |
3378 | /// priority-clause: |
3379 | /// 'priority' '(' expression ')' |
3380 | /// |
3381 | /// grainsize-clause: |
3382 | /// 'grainsize' '(' expression ')' |
3383 | /// |
3384 | /// num_tasks-clause: |
3385 | /// 'num_tasks' '(' expression ')' |
3386 | /// |
3387 | /// hint-clause: |
3388 | /// 'hint' '(' expression ')' |
3389 | /// |
3390 | /// allocator-clause: |
3391 | /// 'allocator' '(' expression ')' |
3392 | /// |
3393 | /// detach-clause: |
3394 | /// 'detach' '(' event-handler-expression ')' |
3395 | /// |
3396 | /// align-clause |
3397 | /// 'align' '(' positive-integer-constant ')' |
3398 | /// |
3399 | OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind, |
3400 | bool ParseOnly) { |
3401 | SourceLocation Loc = ConsumeToken(); |
3402 | SourceLocation LLoc = Tok.getLocation(); |
3403 | SourceLocation RLoc; |
3404 | |
3405 | ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc); |
3406 | |
3407 | if (Val.isInvalid()) |
3408 | return nullptr; |
3409 | |
3410 | if (ParseOnly) |
3411 | return nullptr; |
3412 | return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc); |
3413 | } |
3414 | |
3415 | /// Parse indirect clause for '#pragma omp declare target' directive. |
3416 | /// 'indirect' '[' '(' invoked-by-fptr ')' ']' |
3417 | /// where invoked-by-fptr is a constant boolean expression that evaluates to |
3418 | /// true or false at compile time. |
3419 | bool Parser::ParseOpenMPIndirectClause(Sema::DeclareTargetContextInfo &DTCI, |
3420 | bool ParseOnly) { |
3421 | SourceLocation Loc = ConsumeToken(); |
3422 | SourceLocation RLoc; |
3423 | |
3424 | if (Tok.isNot(tok::l_paren)) { |
3425 | if (ParseOnly) |
3426 | return false; |
3427 | DTCI.Indirect = nullptr; |
3428 | return true; |
3429 | } |
3430 | |
3431 | ExprResult Val = |
3432 | ParseOpenMPParensExpr(getOpenMPClauseName(OMPC_indirect), RLoc); |
3433 | if (Val.isInvalid()) |
3434 | return false; |
3435 | |
3436 | if (ParseOnly) |
3437 | return false; |
3438 | |
3439 | if (!Val.get()->isValueDependent() && !Val.get()->isTypeDependent() && |
3440 | !Val.get()->isInstantiationDependent() && |
3441 | !Val.get()->containsUnexpandedParameterPack()) { |
3442 | ExprResult Ret = Actions.CheckBooleanCondition(Loc, Val.get()); |
3443 | if (Ret.isInvalid()) |
3444 | return false; |
3445 | llvm::APSInt Result; |
3446 | Ret = Actions.VerifyIntegerConstantExpression(Val.get(), &Result, |
3447 | Sema::AllowFold); |
3448 | if (Ret.isInvalid()) |
3449 | return false; |
3450 | DTCI.Indirect = Val.get(); |
3451 | return true; |
3452 | } |
3453 | return false; |
3454 | } |
3455 | |
3456 | /// Parsing of OpenMP clauses that use an interop-var. |
3457 | /// |
3458 | /// init-clause: |
3459 | /// init([interop-modifier, ]interop-type[[, interop-type] ... ]:interop-var) |
3460 | /// |
3461 | /// destroy-clause: |
3462 | /// destroy(interop-var) |
3463 | /// |
3464 | /// use-clause: |
3465 | /// use(interop-var) |
3466 | /// |
3467 | /// interop-modifier: |
3468 | /// prefer_type(preference-list) |
3469 | /// |
3470 | /// preference-list: |
3471 | /// foreign-runtime-id [, foreign-runtime-id]... |
3472 | /// |
3473 | /// foreign-runtime-id: |
3474 | /// <string-literal> | <constant-integral-expression> |
3475 | /// |
3476 | /// interop-type: |
3477 | /// target | targetsync |
3478 | /// |
3479 | OMPClause *Parser::ParseOpenMPInteropClause(OpenMPClauseKind Kind, |
3480 | bool ParseOnly) { |
3481 | SourceLocation Loc = ConsumeToken(); |
3482 | // Parse '('. |
3483 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
3484 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
3485 | getOpenMPClauseName(Kind).data())) |
3486 | return nullptr; |
3487 | |
3488 | bool IsTarget = false; |
3489 | bool IsTargetSync = false; |
3490 | SmallVector<Expr *, 4> Prefs; |
3491 | |
3492 | if (Kind == OMPC_init) { |
3493 | |
3494 | // Parse optional interop-modifier. |
3495 | if (Tok.is(tok::identifier) && PP.getSpelling(Tok) == "prefer_type") { |
3496 | ConsumeToken(); |
3497 | BalancedDelimiterTracker PT(*this, tok::l_paren, |
3498 | tok::annot_pragma_openmp_end); |
3499 | if (PT.expectAndConsume(diag::err_expected_lparen_after, "prefer_type")) |
3500 | return nullptr; |
3501 | |
3502 | while (Tok.isNot(tok::r_paren)) { |
3503 | SourceLocation Loc = Tok.getLocation(); |
3504 | ExprResult LHS = ParseCastExpression(AnyCastExpr); |
3505 | ExprResult PTExpr = Actions.CorrectDelayedTyposInExpr( |
3506 | ParseRHSOfBinaryExpression(LHS, prec::Conditional)); |
3507 | PTExpr = Actions.ActOnFinishFullExpr(PTExpr.get(), Loc, |
3508 | /*DiscardedValue=*/false); |
3509 | if (PTExpr.isUsable()) |
3510 | Prefs.push_back(PTExpr.get()); |
3511 | else |
3512 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
3513 | StopBeforeMatch); |
3514 | |
3515 | if (Tok.is(tok::comma)) |
3516 | ConsumeToken(); |
3517 | } |
3518 | PT.consumeClose(); |
3519 | } |
3520 | |
3521 | if (!Prefs.empty()) { |
3522 | if (Tok.is(tok::comma)) |
3523 | ConsumeToken(); |
3524 | else |
3525 | Diag(Tok, diag::err_omp_expected_punc_after_interop_mod); |
3526 | } |
3527 | |
3528 | // Parse the interop-types. |
3529 | if (Optional<OMPDeclareVariantAttr::InteropType> IType = |
3530 | parseInteropTypeList(*this)) { |
3531 | IsTarget = IType != OMPDeclareVariantAttr::TargetSync; |
3532 | IsTargetSync = IType != OMPDeclareVariantAttr::Target; |
3533 | if (Tok.isNot(tok::colon)) |
3534 | Diag(Tok, diag::warn_pragma_expected_colon) << "interop types"; |
3535 | } |
3536 | if (Tok.is(tok::colon)) |
3537 | ConsumeToken(); |
3538 | } |
3539 | |
3540 | // Parse the variable. |
3541 | SourceLocation VarLoc = Tok.getLocation(); |
3542 | ExprResult InteropVarExpr = |
3543 | Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()); |
3544 | if (!InteropVarExpr.isUsable()) { |
3545 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
3546 | StopBeforeMatch); |
3547 | } |
3548 | |
3549 | // Parse ')'. |
3550 | SourceLocation RLoc = Tok.getLocation(); |
3551 | if (!T.consumeClose()) |
3552 | RLoc = T.getCloseLocation(); |
3553 | |
3554 | if (ParseOnly || !InteropVarExpr.isUsable() || |
3555 | (Kind == OMPC_init && !IsTarget && !IsTargetSync)) |
3556 | return nullptr; |
3557 | |
3558 | if (Kind == OMPC_init) |
3559 | return Actions.ActOnOpenMPInitClause(InteropVarExpr.get(), Prefs, IsTarget, |
3560 | IsTargetSync, Loc, T.getOpenLocation(), |
3561 | VarLoc, RLoc); |
3562 | if (Kind == OMPC_use) |
3563 | return Actions.ActOnOpenMPUseClause(InteropVarExpr.get(), Loc, |
3564 | T.getOpenLocation(), VarLoc, RLoc); |
3565 | |
3566 | if (Kind == OMPC_destroy) |
3567 | return Actions.ActOnOpenMPDestroyClause(InteropVarExpr.get(), Loc, |
3568 | T.getOpenLocation(), VarLoc, RLoc); |
3569 | |
3570 | llvm_unreachable("Unexpected interop variable clause.")::llvm::llvm_unreachable_internal("Unexpected interop variable clause." , "clang/lib/Parse/ParseOpenMP.cpp", 3570); |
3571 | } |
3572 | |
3573 | /// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'. |
3574 | /// |
3575 | /// default-clause: |
3576 | /// 'default' '(' 'none' | 'shared' | 'firstprivate' ')' |
3577 | /// |
3578 | /// proc_bind-clause: |
3579 | /// 'proc_bind' '(' 'master' | 'close' | 'spread' ')' |
3580 | /// |
3581 | /// bind-clause: |
3582 | /// 'bind' '(' 'teams' | 'parallel' | 'thread' ')' |
3583 | /// |
3584 | /// update-clause: |
3585 | /// 'update' '(' 'in' | 'out' | 'inout' | 'mutexinoutset' | |
3586 | /// 'inoutset' ')' |
3587 | /// |
3588 | OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind, |
3589 | bool ParseOnly) { |
3590 | llvm::Optional<SimpleClauseData> Val = parseOpenMPSimpleClause(*this, Kind); |
3591 | if (!Val || ParseOnly) |
3592 | return nullptr; |
3593 | if (getLangOpts().OpenMP < 51 && Kind == OMPC_default && |
3594 | static_cast<DefaultKind>(Val.getValue().Type) == |
3595 | OMP_DEFAULT_firstprivate) { |
3596 | Diag(Val.getValue().LOpen, diag::err_omp_invalid_dsa) |
3597 | << getOpenMPClauseName(OMPC_firstprivate) |
3598 | << getOpenMPClauseName(OMPC_default) << "5.1"; |
3599 | return nullptr; |
3600 | } |
3601 | return Actions.ActOnOpenMPSimpleClause( |
3602 | Kind, Val.getValue().Type, Val.getValue().TypeLoc, Val.getValue().LOpen, |
3603 | Val.getValue().Loc, Val.getValue().RLoc); |
3604 | } |
3605 | |
3606 | /// Parsing of OpenMP clauses like 'ordered'. |
3607 | /// |
3608 | /// ordered-clause: |
3609 | /// 'ordered' |
3610 | /// |
3611 | /// nowait-clause: |
3612 | /// 'nowait' |
3613 | /// |
3614 | /// untied-clause: |
3615 | /// 'untied' |
3616 | /// |
3617 | /// mergeable-clause: |
3618 | /// 'mergeable' |
3619 | /// |
3620 | /// read-clause: |
3621 | /// 'read' |
3622 | /// |
3623 | /// threads-clause: |
3624 | /// 'threads' |
3625 | /// |
3626 | /// simd-clause: |
3627 | /// 'simd' |
3628 | /// |
3629 | /// nogroup-clause: |
3630 | /// 'nogroup' |
3631 | /// |
3632 | OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) { |
3633 | SourceLocation Loc = Tok.getLocation(); |
3634 | ConsumeAnyToken(); |
3635 | |
3636 | if (ParseOnly) |
3637 | return nullptr; |
3638 | return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation()); |
3639 | } |
3640 | |
3641 | /// Parsing of OpenMP clauses with single expressions and some additional |
3642 | /// argument like 'schedule' or 'dist_schedule'. |
3643 | /// |
3644 | /// schedule-clause: |
3645 | /// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ] |
3646 | /// ')' |
3647 | /// |
3648 | /// if-clause: |
3649 | /// 'if' '(' [ directive-name-modifier ':' ] expression ')' |
3650 | /// |
3651 | /// defaultmap: |
3652 | /// 'defaultmap' '(' modifier [ ':' kind ] ')' |
3653 | /// |
3654 | /// device-clause: |
3655 | /// 'device' '(' [ device-modifier ':' ] expression ')' |
3656 | /// |
3657 | OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPDirectiveKind DKind, |
3658 | OpenMPClauseKind Kind, |
3659 | bool ParseOnly) { |
3660 | SourceLocation Loc = ConsumeToken(); |
3661 | SourceLocation DelimLoc; |
3662 | // Parse '('. |
3663 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
3664 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
3665 | getOpenMPClauseName(Kind).data())) |
3666 | return nullptr; |
3667 | |
3668 | ExprResult Val; |
3669 | SmallVector<unsigned, 4> Arg; |
3670 | SmallVector<SourceLocation, 4> KLoc; |
3671 | if (Kind == OMPC_schedule) { |
3672 | enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; |
3673 | Arg.resize(NumberOfElements); |
3674 | KLoc.resize(NumberOfElements); |
3675 | Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown; |
3676 | Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown; |
3677 | Arg[ScheduleKind] = OMPC_SCHEDULE_unknown; |
3678 | unsigned KindModifier = getOpenMPSimpleClauseType( |
3679 | Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok), getLangOpts()); |
3680 | if (KindModifier > OMPC_SCHEDULE_unknown) { |
3681 | // Parse 'modifier' |
3682 | Arg[Modifier1] = KindModifier; |
3683 | KLoc[Modifier1] = Tok.getLocation(); |
3684 | if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && |
3685 | Tok.isNot(tok::annot_pragma_openmp_end)) |
3686 | ConsumeAnyToken(); |
3687 | if (Tok.is(tok::comma)) { |
3688 | // Parse ',' 'modifier' |
3689 | ConsumeAnyToken(); |
3690 | KindModifier = getOpenMPSimpleClauseType( |
3691 | Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok), getLangOpts()); |
3692 | Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown |
3693 | ? KindModifier |
3694 | : (unsigned)OMPC_SCHEDULE_unknown; |
3695 | KLoc[Modifier2] = Tok.getLocation(); |
3696 | if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && |
3697 | Tok.isNot(tok::annot_pragma_openmp_end)) |
3698 | ConsumeAnyToken(); |
3699 | } |
3700 | // Parse ':' |
3701 | if (Tok.is(tok::colon)) |
3702 | ConsumeAnyToken(); |
3703 | else |
3704 | Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier"; |
3705 | KindModifier = getOpenMPSimpleClauseType( |
3706 | Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok), getLangOpts()); |
3707 | } |
3708 | Arg[ScheduleKind] = KindModifier; |
3709 | KLoc[ScheduleKind] = Tok.getLocation(); |
3710 | if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && |
3711 | Tok.isNot(tok::annot_pragma_openmp_end)) |
3712 | ConsumeAnyToken(); |
3713 | if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static || |
3714 | Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic || |
3715 | Arg[ScheduleKind] == OMPC_SCHEDULE_guided) && |
3716 | Tok.is(tok::comma)) |
3717 | DelimLoc = ConsumeAnyToken(); |
3718 | } else if (Kind == OMPC_dist_schedule) { |
3719 | Arg.push_back(getOpenMPSimpleClauseType( |
3720 | Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok), getLangOpts())); |
3721 | KLoc.push_back(Tok.getLocation()); |
3722 | if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && |
3723 | Tok.isNot(tok::annot_pragma_openmp_end)) |
3724 | ConsumeAnyToken(); |
3725 | if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma)) |
3726 | DelimLoc = ConsumeAnyToken(); |
3727 | } else if (Kind == OMPC_defaultmap) { |
3728 | // Get a defaultmap modifier |
3729 | unsigned Modifier = getOpenMPSimpleClauseType( |
3730 | Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok), getLangOpts()); |
3731 | // Set defaultmap modifier to unknown if it is either scalar, aggregate, or |
3732 | // pointer |
3733 | if (Modifier < OMPC_DEFAULTMAP_MODIFIER_unknown) |
3734 | Modifier = OMPC_DEFAULTMAP_MODIFIER_unknown; |
3735 | Arg.push_back(Modifier); |
3736 | KLoc.push_back(Tok.getLocation()); |
3737 | if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && |
3738 | Tok.isNot(tok::annot_pragma_openmp_end)) |
3739 | ConsumeAnyToken(); |
3740 | // Parse ':' |
3741 | if (Tok.is(tok::colon) || getLangOpts().OpenMP < 50) { |
3742 | if (Tok.is(tok::colon)) |
3743 | ConsumeAnyToken(); |
3744 | else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown) |
3745 | Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier"; |
3746 | // Get a defaultmap kind |
3747 | Arg.push_back(getOpenMPSimpleClauseType( |
3748 | Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok), getLangOpts())); |
3749 | KLoc.push_back(Tok.getLocation()); |
3750 | if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && |
3751 | Tok.isNot(tok::annot_pragma_openmp_end)) |
3752 | ConsumeAnyToken(); |
3753 | } else { |
3754 | Arg.push_back(OMPC_DEFAULTMAP_unknown); |
3755 | KLoc.push_back(SourceLocation()); |
3756 | } |
3757 | } else if (Kind == OMPC_device) { |
3758 | // Only target executable directives support extended device construct. |
3759 | if (isOpenMPTargetExecutionDirective(DKind) && getLangOpts().OpenMP >= 50 && |
3760 | NextToken().is(tok::colon)) { |
3761 | // Parse optional <device modifier> ':' |
3762 | Arg.push_back(getOpenMPSimpleClauseType( |
3763 | Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok), getLangOpts())); |
3764 | KLoc.push_back(Tok.getLocation()); |
3765 | ConsumeAnyToken(); |
3766 | // Parse ':' |
3767 | ConsumeAnyToken(); |
3768 | } else { |
3769 | Arg.push_back(OMPC_DEVICE_unknown); |
3770 | KLoc.emplace_back(); |
3771 | } |
3772 | } else { |
3773 | assert(Kind == OMPC_if)(static_cast <bool> (Kind == OMPC_if) ? void (0) : __assert_fail ("Kind == OMPC_if", "clang/lib/Parse/ParseOpenMP.cpp", 3773, __extension__ __PRETTY_FUNCTION__)); |
3774 | KLoc.push_back(Tok.getLocation()); |
3775 | TentativeParsingAction TPA(*this); |
3776 | auto DK = parseOpenMPDirectiveKind(*this); |
3777 | Arg.push_back(DK); |
3778 | if (DK != OMPD_unknown) { |
3779 | ConsumeToken(); |
3780 | if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) { |
3781 | TPA.Commit(); |
3782 | DelimLoc = ConsumeToken(); |
3783 | } else { |
3784 | TPA.Revert(); |
3785 | Arg.back() = unsigned(OMPD_unknown); |
3786 | } |
3787 | } else { |
3788 | TPA.Revert(); |
3789 | } |
3790 | } |
3791 | |
3792 | bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) || |
3793 | (Kind == OMPC_dist_schedule && DelimLoc.isValid()) || |
3794 | Kind == OMPC_if || Kind == OMPC_device; |
3795 | if (NeedAnExpression) { |
3796 | SourceLocation ELoc = Tok.getLocation(); |
3797 | ExprResult LHS(ParseCastExpression(AnyCastExpr, false, NotTypeCast)); |
3798 | Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional); |
3799 | Val = |
3800 | Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false); |
3801 | } |
3802 | |
3803 | // Parse ')'. |
3804 | SourceLocation RLoc = Tok.getLocation(); |
3805 | if (!T.consumeClose()) |
3806 | RLoc = T.getCloseLocation(); |
3807 | |
3808 | if (NeedAnExpression && Val.isInvalid()) |
3809 | return nullptr; |
3810 | |
3811 | if (ParseOnly) |
3812 | return nullptr; |
3813 | return Actions.ActOnOpenMPSingleExprWithArgClause( |
3814 | Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc); |
3815 | } |
3816 | |
3817 | static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec, |
3818 | UnqualifiedId &ReductionId) { |
3819 | if (ReductionIdScopeSpec.isEmpty()) { |
3820 | auto OOK = OO_None; |
3821 | switch (P.getCurToken().getKind()) { |
3822 | case tok::plus: |
3823 | OOK = OO_Plus; |
3824 | break; |
3825 | case tok::minus: |
3826 | OOK = OO_Minus; |
3827 | break; |
3828 | case tok::star: |
3829 | OOK = OO_Star; |
3830 | break; |
3831 | case tok::amp: |
3832 | OOK = OO_Amp; |
3833 | break; |
3834 | case tok::pipe: |
3835 | OOK = OO_Pipe; |
3836 | break; |
3837 | case tok::caret: |
3838 | OOK = OO_Caret; |
3839 | break; |
3840 | case tok::ampamp: |
3841 | OOK = OO_AmpAmp; |
3842 | break; |
3843 | case tok::pipepipe: |
3844 | OOK = OO_PipePipe; |
3845 | break; |
3846 | default: |
3847 | break; |
3848 | } |
3849 | if (OOK != OO_None) { |
3850 | SourceLocation OpLoc = P.ConsumeToken(); |
3851 | SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()}; |
3852 | ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations); |
3853 | return false; |
3854 | } |
3855 | } |
3856 | return P.ParseUnqualifiedId( |
3857 | ReductionIdScopeSpec, /*ObjectType=*/nullptr, |
3858 | /*ObjectHadErrors=*/false, /*EnteringContext*/ false, |
3859 | /*AllowDestructorName*/ false, |
3860 | /*AllowConstructorName*/ false, |
3861 | /*AllowDeductionGuide*/ false, nullptr, ReductionId); |
3862 | } |
3863 | |
3864 | /// Checks if the token is a valid map-type-modifier. |
3865 | /// FIXME: It will return an OpenMPMapClauseKind if that's what it parses. |
3866 | static OpenMPMapModifierKind isMapModifier(Parser &P) { |
3867 | Token Tok = P.getCurToken(); |
3868 | if (!Tok.is(tok::identifier)) |
3869 | return OMPC_MAP_MODIFIER_unknown; |
3870 | |
3871 | Preprocessor &PP = P.getPreprocessor(); |
3872 | OpenMPMapModifierKind TypeModifier = |
3873 | static_cast<OpenMPMapModifierKind>(getOpenMPSimpleClauseType( |
3874 | OMPC_map, PP.getSpelling(Tok), P.getLangOpts())); |
3875 | return TypeModifier; |
3876 | } |
3877 | |
3878 | /// Parse the mapper modifier in map, to, and from clauses. |
3879 | bool Parser::parseMapperModifier(OpenMPVarListDataTy &Data) { |
3880 | // Parse '('. |
3881 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::colon); |
3882 | if (T.expectAndConsume(diag::err_expected_lparen_after, "mapper")) { |
3883 | SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, |
3884 | StopBeforeMatch); |
3885 | return true; |
3886 | } |
3887 | // Parse mapper-identifier |
3888 | if (getLangOpts().CPlusPlus) |
3889 | ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec, |
3890 | /*ObjectType=*/nullptr, |
3891 | /*ObjectHasErrors=*/false, |
3892 | /*EnteringContext=*/false); |
3893 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) { |
3894 | Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier); |
3895 | SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, |
3896 | StopBeforeMatch); |
3897 | return true; |
3898 | } |
3899 | auto &DeclNames = Actions.getASTContext().DeclarationNames; |
3900 | Data.ReductionOrMapperId = DeclarationNameInfo( |
3901 | DeclNames.getIdentifier(Tok.getIdentifierInfo()), Tok.getLocation()); |
3902 | ConsumeToken(); |
3903 | // Parse ')'. |
3904 | return T.consumeClose(); |
3905 | } |
3906 | |
3907 | /// Parse map-type-modifiers in map clause. |
3908 | /// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list) |
3909 | /// where, map-type-modifier ::= always | close | mapper(mapper-identifier) | |
3910 | /// present |
3911 | bool Parser::parseMapTypeModifiers(OpenMPVarListDataTy &Data) { |
3912 | while (getCurToken().isNot(tok::colon)) { |
3913 | OpenMPMapModifierKind TypeModifier = isMapModifier(*this); |
3914 | if (TypeModifier == OMPC_MAP_MODIFIER_always || |
3915 | TypeModifier == OMPC_MAP_MODIFIER_close || |
3916 | TypeModifier == OMPC_MAP_MODIFIER_present || |
3917 | TypeModifier == OMPC_MAP_MODIFIER_ompx_hold) { |
3918 | Data.MapTypeModifiers.push_back(TypeModifier); |
3919 | Data.MapTypeModifiersLoc.push_back(Tok.getLocation()); |
3920 | ConsumeToken(); |
3921 | } else if (TypeModifier == OMPC_MAP_MODIFIER_mapper) { |
3922 | Data.MapTypeModifiers.push_back(TypeModifier); |
3923 | Data.MapTypeModifiersLoc.push_back(Tok.getLocation()); |
3924 | ConsumeToken(); |
3925 | if (parseMapperModifier(Data)) |
3926 | return true; |
3927 | } else { |
3928 | // For the case of unknown map-type-modifier or a map-type. |
3929 | // Map-type is followed by a colon; the function returns when it |
3930 | // encounters a token followed by a colon. |
3931 | if (Tok.is(tok::comma)) { |
3932 | Diag(Tok, diag::err_omp_map_type_modifier_missing); |
3933 | ConsumeToken(); |
3934 | continue; |
3935 | } |
3936 | // Potential map-type token as it is followed by a colon. |
3937 | if (PP.LookAhead(0).is(tok::colon)) |
3938 | return false; |
3939 | Diag(Tok, diag::err_omp_unknown_map_type_modifier) |
3940 | << (getLangOpts().OpenMP >= 51 ? 1 : 0) |
3941 | << getLangOpts().OpenMPExtensions; |
3942 | ConsumeToken(); |
3943 | } |
3944 | if (getCurToken().is(tok::comma)) |
3945 | ConsumeToken(); |
3946 | } |
3947 | return false; |
3948 | } |
3949 | |
3950 | /// Checks if the token is a valid map-type. |
3951 | /// FIXME: It will return an OpenMPMapModifierKind if that's what it parses. |
3952 | static OpenMPMapClauseKind isMapType(Parser &P) { |
3953 | Token Tok = P.getCurToken(); |
3954 | // The map-type token can be either an identifier or the C++ delete keyword. |
3955 | if (!Tok.isOneOf(tok::identifier, tok::kw_delete)) |
3956 | return OMPC_MAP_unknown; |
3957 | Preprocessor &PP = P.getPreprocessor(); |
3958 | OpenMPMapClauseKind MapType = |
3959 | static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType( |
3960 | OMPC_map, PP.getSpelling(Tok), P.getLangOpts())); |
3961 | return MapType; |
3962 | } |
3963 | |
3964 | /// Parse map-type in map clause. |
3965 | /// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list) |
3966 | /// where, map-type ::= to | from | tofrom | alloc | release | delete |
3967 | static void parseMapType(Parser &P, Parser::OpenMPVarListDataTy &Data) { |
3968 | Token Tok = P.getCurToken(); |
3969 | if (Tok.is(tok::colon)) { |
3970 | P.Diag(Tok, diag::err_omp_map_type_missing); |
3971 | return; |
3972 | } |
3973 | Data.ExtraModifier = isMapType(P); |
3974 | if (Data.ExtraModifier == OMPC_MAP_unknown) |
3975 | P.Diag(Tok, diag::err_omp_unknown_map_type); |
3976 | P.ConsumeToken(); |
3977 | } |
3978 | |
3979 | /// Parses simple expression in parens for single-expression clauses of OpenMP |
3980 | /// constructs. |
3981 | ExprResult Parser::ParseOpenMPIteratorsExpr() { |
3982 | assert(Tok.is(tok::identifier) && PP.getSpelling(Tok) == "iterator" &&(static_cast <bool> (Tok.is(tok::identifier) && PP.getSpelling(Tok) == "iterator" && "Expected 'iterator' token." ) ? void (0) : __assert_fail ("Tok.is(tok::identifier) && PP.getSpelling(Tok) == \"iterator\" && \"Expected 'iterator' token.\"" , "clang/lib/Parse/ParseOpenMP.cpp", 3983, __extension__ __PRETTY_FUNCTION__ )) |
3983 | "Expected 'iterator' token.")(static_cast <bool> (Tok.is(tok::identifier) && PP.getSpelling(Tok) == "iterator" && "Expected 'iterator' token." ) ? void (0) : __assert_fail ("Tok.is(tok::identifier) && PP.getSpelling(Tok) == \"iterator\" && \"Expected 'iterator' token.\"" , "clang/lib/Parse/ParseOpenMP.cpp", 3983, __extension__ __PRETTY_FUNCTION__ )); |
3984 | SourceLocation IteratorKwLoc = ConsumeToken(); |
3985 | |
3986 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
3987 | if (T.expectAndConsume(diag::err_expected_lparen_after, "iterator")) |
3988 | return ExprError(); |
3989 | |
3990 | SourceLocation LLoc = T.getOpenLocation(); |
3991 | SmallVector<Sema::OMPIteratorData, 4> Data; |
3992 | while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) { |
3993 | // Check if the type parsing is required. |
3994 | ParsedType IteratorType; |
3995 | if (Tok.isNot(tok::identifier) || NextToken().isNot(tok::equal)) { |
3996 | // identifier '=' is not found - parse type. |
3997 | TypeResult TR = ParseTypeName(); |
3998 | if (TR.isInvalid()) { |
3999 | T.skipToEnd(); |
4000 | return ExprError(); |
4001 | } |
4002 | IteratorType = TR.get(); |
4003 | } |
4004 | |
4005 | // Parse identifier. |
4006 | IdentifierInfo *II = nullptr; |
4007 | SourceLocation IdLoc; |
4008 | if (Tok.is(tok::identifier)) { |
4009 | II = Tok.getIdentifierInfo(); |
4010 | IdLoc = ConsumeToken(); |
4011 | } else { |
4012 | Diag(Tok, diag::err_expected_unqualified_id) << 0; |
4013 | } |
4014 | |
4015 | // Parse '='. |
4016 | SourceLocation AssignLoc; |
4017 | if (Tok.is(tok::equal)) |
4018 | AssignLoc = ConsumeToken(); |
4019 | else |
4020 | Diag(Tok, diag::err_omp_expected_equal_in_iterator); |
4021 | |
4022 | // Parse range-specification - <begin> ':' <end> [ ':' <step> ] |
4023 | ColonProtectionRAIIObject ColonRAII(*this); |
4024 | // Parse <begin> |
4025 | SourceLocation Loc = Tok.getLocation(); |
4026 | ExprResult LHS = ParseCastExpression(AnyCastExpr); |
4027 | ExprResult Begin = Actions.CorrectDelayedTyposInExpr( |
4028 | ParseRHSOfBinaryExpression(LHS, prec::Conditional)); |
4029 | Begin = Actions.ActOnFinishFullExpr(Begin.get(), Loc, |
4030 | /*DiscardedValue=*/false); |
4031 | // Parse ':'. |
4032 | SourceLocation ColonLoc; |
4033 | if (Tok.is(tok::colon)) |
4034 | ColonLoc = ConsumeToken(); |
4035 | |
4036 | // Parse <end> |
4037 | Loc = Tok.getLocation(); |
4038 | LHS = ParseCastExpression(AnyCastExpr); |
4039 | ExprResult End = Actions.CorrectDelayedTyposInExpr( |
4040 | ParseRHSOfBinaryExpression(LHS, prec::Conditional)); |
4041 | End = Actions.ActOnFinishFullExpr(End.get(), Loc, |
4042 | /*DiscardedValue=*/false); |
4043 | |
4044 | SourceLocation SecColonLoc; |
4045 | ExprResult Step; |
4046 | // Parse optional step. |
4047 | if (Tok.is(tok::colon)) { |
4048 | // Parse ':' |
4049 | SecColonLoc = ConsumeToken(); |
4050 | // Parse <step> |
4051 | Loc = Tok.getLocation(); |
4052 | LHS = ParseCastExpression(AnyCastExpr); |
4053 | Step = Actions.CorrectDelayedTyposInExpr( |
4054 | ParseRHSOfBinaryExpression(LHS, prec::Conditional)); |
4055 | Step = Actions.ActOnFinishFullExpr(Step.get(), Loc, |
4056 | /*DiscardedValue=*/false); |
4057 | } |
4058 | |
4059 | // Parse ',' or ')' |
4060 | if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren)) |
4061 | Diag(Tok, diag::err_omp_expected_punc_after_iterator); |
4062 | if (Tok.is(tok::comma)) |
4063 | ConsumeToken(); |
4064 | |
4065 | Sema::OMPIteratorData &D = Data.emplace_back(); |
4066 | D.DeclIdent = II; |
4067 | D.DeclIdentLoc = IdLoc; |
4068 | D.Type = IteratorType; |
4069 | D.AssignLoc = AssignLoc; |
4070 | D.ColonLoc = ColonLoc; |
4071 | D.SecColonLoc = SecColonLoc; |
4072 | D.Range.Begin = Begin.get(); |
4073 | D.Range.End = End.get(); |
4074 | D.Range.Step = Step.get(); |
4075 | } |
4076 | |
4077 | // Parse ')'. |
4078 | SourceLocation RLoc = Tok.getLocation(); |
4079 | if (!T.consumeClose()) |
4080 | RLoc = T.getCloseLocation(); |
4081 | |
4082 | return Actions.ActOnOMPIteratorExpr(getCurScope(), IteratorKwLoc, LLoc, RLoc, |
4083 | Data); |
4084 | } |
4085 | |
4086 | /// Parses clauses with list. |
4087 | bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind, |
4088 | OpenMPClauseKind Kind, |
4089 | SmallVectorImpl<Expr *> &Vars, |
4090 | OpenMPVarListDataTy &Data) { |
4091 | UnqualifiedId UnqualifiedReductionId; |
4092 | bool InvalidReductionId = false; |
4093 | bool IsInvalidMapperModifier = false; |
4094 | |
4095 | // Parse '('. |
4096 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
4097 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
4098 | getOpenMPClauseName(Kind).data())) |
4099 | return true; |
4100 | |
4101 | bool HasIterator = false; |
4102 | bool NeedRParenForLinear = false; |
4103 | BalancedDelimiterTracker LinearT(*this, tok::l_paren, |
4104 | tok::annot_pragma_openmp_end); |
4105 | // Handle reduction-identifier for reduction clause. |
4106 | if (Kind == OMPC_reduction || Kind == OMPC_task_reduction || |
4107 | Kind == OMPC_in_reduction) { |
4108 | Data.ExtraModifier = OMPC_REDUCTION_unknown; |
4109 | if (Kind == OMPC_reduction && getLangOpts().OpenMP >= 50 && |
4110 | (Tok.is(tok::identifier) || Tok.is(tok::kw_default)) && |
4111 | NextToken().is(tok::comma)) { |
4112 | // Parse optional reduction modifier. |
4113 | Data.ExtraModifier = |
4114 | getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok), getLangOpts()); |
4115 | Data.ExtraModifierLoc = Tok.getLocation(); |
4116 | ConsumeToken(); |
4117 | assert(Tok.is(tok::comma) && "Expected comma.")(static_cast <bool> (Tok.is(tok::comma) && "Expected comma." ) ? void (0) : __assert_fail ("Tok.is(tok::comma) && \"Expected comma.\"" , "clang/lib/Parse/ParseOpenMP.cpp", 4117, __extension__ __PRETTY_FUNCTION__ )); |
4118 | (void)ConsumeToken(); |
4119 | } |
4120 | ColonProtectionRAIIObject ColonRAII(*this); |
4121 | if (getLangOpts().CPlusPlus) |
4122 | ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec, |
4123 | /*ObjectType=*/nullptr, |
4124 | /*ObjectHasErrors=*/false, |
4125 | /*EnteringContext=*/false); |
4126 | InvalidReductionId = ParseReductionId( |
4127 | *this, Data.ReductionOrMapperIdScopeSpec, UnqualifiedReductionId); |
4128 | if (InvalidReductionId) { |
4129 | SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, |
4130 | StopBeforeMatch); |
4131 | } |
4132 | if (Tok.is(tok::colon)) |
4133 | Data.ColonLoc = ConsumeToken(); |
4134 | else |
4135 | Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier"; |
4136 | if (!InvalidReductionId) |
4137 | Data.ReductionOrMapperId = |
4138 | Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId); |
4139 | } else if (Kind == OMPC_depend) { |
4140 | if (getLangOpts().OpenMP >= 50) { |
4141 | if (Tok.is(tok::identifier) && PP.getSpelling(Tok) == "iterator") { |
4142 | // Handle optional dependence modifier. |
4143 | // iterator(iterators-definition) |
4144 | // where iterators-definition is iterator-specifier [, |
4145 | // iterators-definition ] |
4146 | // where iterator-specifier is [ iterator-type ] identifier = |
4147 | // range-specification |
4148 | HasIterator = true; |
4149 | EnterScope(Scope::OpenMPDirectiveScope | Scope::DeclScope); |
4150 | ExprResult IteratorRes = ParseOpenMPIteratorsExpr(); |
4151 | Data.DepModOrTailExpr = IteratorRes.get(); |
4152 | // Parse ',' |
4153 | ExpectAndConsume(tok::comma); |
4154 | } |
4155 | } |
4156 | // Handle dependency type for depend clause. |
4157 | ColonProtectionRAIIObject ColonRAII(*this); |
4158 | Data.ExtraModifier = getOpenMPSimpleClauseType( |
4159 | Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : "", |
4160 | getLangOpts()); |
4161 | Data.ExtraModifierLoc = Tok.getLocation(); |
4162 | if (Data.ExtraModifier == OMPC_DEPEND_unknown) { |
4163 | SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, |
4164 | StopBeforeMatch); |
4165 | } else { |
4166 | ConsumeToken(); |
4167 | // Special processing for depend(source) clause. |
4168 | if (DKind == OMPD_ordered && Data.ExtraModifier == OMPC_DEPEND_source) { |
4169 | // Parse ')'. |
4170 | T.consumeClose(); |
4171 | return false; |
4172 | } |
4173 | } |
4174 | if (Tok.is(tok::colon)) { |
4175 | Data.ColonLoc = ConsumeToken(); |
4176 | } else { |
4177 | Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren |
4178 | : diag::warn_pragma_expected_colon) |
4179 | << "dependency type"; |
4180 | } |
4181 | } else if (Kind == OMPC_linear) { |
4182 | // Try to parse modifier if any. |
4183 | Data.ExtraModifier = OMPC_LINEAR_val; |
4184 | if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) { |
4185 | Data.ExtraModifier = |
4186 | getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok), getLangOpts()); |
4187 | Data.ExtraModifierLoc = ConsumeToken(); |
4188 | LinearT.consumeOpen(); |
4189 | NeedRParenForLinear = true; |
4190 | } |
4191 | } else if (Kind == OMPC_lastprivate) { |
4192 | // Try to parse modifier if any. |
4193 | Data.ExtraModifier = OMPC_LASTPRIVATE_unknown; |
4194 | // Conditional modifier allowed only in OpenMP 5.0 and not supported in |
4195 | // distribute and taskloop based directives. |
4196 | if ((getLangOpts().OpenMP >= 50 && !isOpenMPDistributeDirective(DKind) && |
4197 | !isOpenMPTaskLoopDirective(DKind)) && |
4198 | Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::colon)) { |
4199 | Data.ExtraModifier = |
4200 | getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok), getLangOpts()); |
4201 | Data.ExtraModifierLoc = Tok.getLocation(); |
4202 | ConsumeToken(); |
4203 | assert(Tok.is(tok::colon) && "Expected colon.")(static_cast <bool> (Tok.is(tok::colon) && "Expected colon." ) ? void (0) : __assert_fail ("Tok.is(tok::colon) && \"Expected colon.\"" , "clang/lib/Parse/ParseOpenMP.cpp", 4203, __extension__ __PRETTY_FUNCTION__ )); |
4204 | Data.ColonLoc = ConsumeToken(); |
4205 | } |
4206 | } else if (Kind == OMPC_map) { |
4207 | // Handle map type for map clause. |
4208 | ColonProtectionRAIIObject ColonRAII(*this); |
4209 | |
4210 | // The first identifier may be a list item, a map-type or a |
4211 | // map-type-modifier. The map-type can also be delete which has the same |
4212 | // spelling of the C++ delete keyword. |
4213 | Data.ExtraModifier = OMPC_MAP_unknown; |
4214 | Data.ExtraModifierLoc = Tok.getLocation(); |
4215 | |
4216 | // Check for presence of a colon in the map clause. |
4217 | TentativeParsingAction TPA(*this); |
4218 | bool ColonPresent = false; |
4219 | if (SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, |
4220 | StopBeforeMatch)) { |
4221 | if (Tok.is(tok::colon)) |
4222 | ColonPresent = true; |
4223 | } |
4224 | TPA.Revert(); |
4225 | // Only parse map-type-modifier[s] and map-type if a colon is present in |
4226 | // the map clause. |
4227 | if (ColonPresent) { |
4228 | IsInvalidMapperModifier = parseMapTypeModifiers(Data); |
4229 | if (!IsInvalidMapperModifier) |
4230 | parseMapType(*this, Data); |
4231 | else |
4232 | SkipUntil(tok::colon, tok::annot_pragma_openmp_end, StopBeforeMatch); |
4233 | } |
4234 | if (Data.ExtraModifier == OMPC_MAP_unknown) { |
4235 | Data.ExtraModifier = OMPC_MAP_tofrom; |
4236 | Data.IsMapTypeImplicit = true; |
4237 | } |
4238 | |
4239 | if (Tok.is(tok::colon)) |
4240 | Data.ColonLoc = ConsumeToken(); |
4241 | } else if (Kind == OMPC_to || Kind == OMPC_from) { |
4242 | while (Tok.is(tok::identifier)) { |
4243 | auto Modifier = static_cast<OpenMPMotionModifierKind>( |
4244 | getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok), getLangOpts())); |
4245 | if (Modifier == OMPC_MOTION_MODIFIER_unknown) |
4246 | break; |
4247 | Data.MotionModifiers.push_back(Modifier); |
4248 | Data.MotionModifiersLoc.push_back(Tok.getLocation()); |
4249 | ConsumeToken(); |
4250 | if (Modifier == OMPC_MOTION_MODIFIER_mapper) { |
4251 | IsInvalidMapperModifier = parseMapperModifier(Data); |
4252 | if (IsInvalidMapperModifier) |
4253 | break; |
4254 | } |
4255 | // OpenMP < 5.1 doesn't permit a ',' or additional modifiers. |
4256 | if (getLangOpts().OpenMP < 51) |
4257 | break; |
4258 | // OpenMP 5.1 accepts an optional ',' even if the next character is ':'. |
4259 | // TODO: Is that intentional? |
4260 | if (Tok.is(tok::comma)) |
4261 | ConsumeToken(); |
4262 | } |
4263 | if (!Data.MotionModifiers.empty() && Tok.isNot(tok::colon)) { |
4264 | if (!IsInvalidMapperModifier) { |
4265 | if (getLangOpts().OpenMP < 51) |
4266 | Diag(Tok, diag::warn_pragma_expected_colon) << ")"; |
4267 | else |
4268 | Diag(Tok, diag::warn_pragma_expected_colon) << "motion modifier"; |
4269 | } |
4270 | SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, |
4271 | StopBeforeMatch); |
4272 | } |
4273 | // OpenMP 5.1 permits a ':' even without a preceding modifier. TODO: Is |
4274 | // that intentional? |
4275 | if ((!Data.MotionModifiers.empty() || getLangOpts().OpenMP >= 51) && |
4276 | Tok.is(tok::colon)) |
4277 | Data.ColonLoc = ConsumeToken(); |
4278 | } else if (Kind == OMPC_allocate || |
4279 | (Kind == OMPC_affinity && Tok.is(tok::identifier) && |
4280 | PP.getSpelling(Tok) == "iterator")) { |
4281 | // Handle optional allocator expression followed by colon delimiter. |
4282 | ColonProtectionRAIIObject ColonRAII(*this); |
4283 | TentativeParsingAction TPA(*this); |
4284 | // OpenMP 5.0, 2.10.1, task Construct. |
4285 | // where aff-modifier is one of the following: |
4286 | // iterator(iterators-definition) |
4287 | ExprResult Tail; |
4288 | if (Kind == OMPC_allocate) { |
4289 | Tail = ParseAssignmentExpression(); |
4290 | } else { |
4291 | HasIterator = true; |
4292 | EnterScope(Scope::OpenMPDirectiveScope | Scope::DeclScope); |
4293 | Tail = ParseOpenMPIteratorsExpr(); |
4294 | } |
4295 | Tail = Actions.CorrectDelayedTyposInExpr(Tail); |
4296 | Tail = Actions.ActOnFinishFullExpr(Tail.get(), T.getOpenLocation(), |
4297 | /*DiscardedValue=*/false); |
4298 | if (Tail.isUsable()) { |
4299 | if (Tok.is(tok::colon)) { |
4300 | Data.DepModOrTailExpr = Tail.get(); |
4301 | Data.ColonLoc = ConsumeToken(); |
4302 | TPA.Commit(); |
4303 | } else { |
4304 | // Colon not found, parse only list of variables. |
4305 | TPA.Revert(); |
4306 | } |
4307 | } else { |
4308 | // Parsing was unsuccessfull, revert and skip to the end of clause or |
4309 | // directive. |
4310 | TPA.Revert(); |
4311 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
4312 | StopBeforeMatch); |
4313 | } |
4314 | } else if (Kind == OMPC_adjust_args) { |
4315 | // Handle adjust-op for adjust_args clause. |
4316 | ColonProtectionRAIIObject ColonRAII(*this); |
4317 | Data.ExtraModifier = getOpenMPSimpleClauseType( |
4318 | Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : "", |
4319 | getLangOpts()); |
4320 | Data.ExtraModifierLoc = Tok.getLocation(); |
4321 | if (Data.ExtraModifier == OMPC_ADJUST_ARGS_unknown) { |
4322 | SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, |
4323 | StopBeforeMatch); |
4324 | } else { |
4325 | ConsumeToken(); |
4326 | if (Tok.is(tok::colon)) |
4327 | Data.ColonLoc = Tok.getLocation(); |
4328 | ExpectAndConsume(tok::colon, diag::warn_pragma_expected_colon, |
4329 | "adjust-op"); |
4330 | } |
4331 | } |
4332 | |
4333 | bool IsComma = |
4334 | (Kind != OMPC_reduction && Kind != OMPC_task_reduction && |
4335 | Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) || |
4336 | (Kind == OMPC_reduction && !InvalidReductionId) || |
4337 | (Kind == OMPC_map && Data.ExtraModifier != OMPC_MAP_unknown) || |
4338 | (Kind == OMPC_depend && Data.ExtraModifier != OMPC_DEPEND_unknown) || |
4339 | (Kind == OMPC_adjust_args && |
4340 | Data.ExtraModifier != OMPC_ADJUST_ARGS_unknown); |
4341 | const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned); |
4342 | while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) && |
4343 | Tok.isNot(tok::annot_pragma_openmp_end))) { |
4344 | ParseScope OMPListScope(this, Scope::OpenMPDirectiveScope); |
4345 | ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail); |
4346 | // Parse variable |
4347 | ExprResult VarExpr = |
4348 | Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()); |
4349 | if (VarExpr.isUsable()) { |
4350 | Vars.push_back(VarExpr.get()); |
4351 | } else { |
4352 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
4353 | StopBeforeMatch); |
4354 | } |
4355 | // Skip ',' if any |
4356 | IsComma = Tok.is(tok::comma); |
4357 | if (IsComma) |
4358 | ConsumeToken(); |
4359 | else if (Tok.isNot(tok::r_paren) && |
4360 | Tok.isNot(tok::annot_pragma_openmp_end) && |
4361 | (!MayHaveTail || Tok.isNot(tok::colon))) |
4362 | Diag(Tok, diag::err_omp_expected_punc) |
4363 | << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush) |
4364 | : getOpenMPClauseName(Kind)) |
4365 | << (Kind == OMPC_flush); |
4366 | } |
4367 | |
4368 | // Parse ')' for linear clause with modifier. |
4369 | if (NeedRParenForLinear) |
4370 | LinearT.consumeClose(); |
4371 | |
4372 | // Parse ':' linear-step (or ':' alignment). |
4373 | const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon); |
4374 | if (MustHaveTail) { |
4375 | Data.ColonLoc = Tok.getLocation(); |
4376 | SourceLocation ELoc = ConsumeToken(); |
4377 | ExprResult Tail = ParseAssignmentExpression(); |
4378 | Tail = |
4379 | Actions.ActOnFinishFullExpr(Tail.get(), ELoc, /*DiscardedValue*/ false); |
4380 | if (Tail.isUsable()) |
4381 | Data.DepModOrTailExpr = Tail.get(); |
4382 | else |
4383 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
4384 | StopBeforeMatch); |
4385 | } |
4386 | |
4387 | // Parse ')'. |
4388 | Data.RLoc = Tok.getLocation(); |
4389 | if (!T.consumeClose()) |
4390 | Data.RLoc = T.getCloseLocation(); |
4391 | // Exit from scope when the iterator is used in depend clause. |
4392 | if (HasIterator) |
4393 | ExitScope(); |
4394 | return (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) || |
4395 | (MustHaveTail && !Data.DepModOrTailExpr) || InvalidReductionId || |
4396 | IsInvalidMapperModifier; |
4397 | } |
4398 | |
4399 | /// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate', |
4400 | /// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction', |
4401 | /// 'in_reduction', 'nontemporal', 'exclusive' or 'inclusive'. |
4402 | /// |
4403 | /// private-clause: |
4404 | /// 'private' '(' list ')' |
4405 | /// firstprivate-clause: |
4406 | /// 'firstprivate' '(' list ')' |
4407 | /// lastprivate-clause: |
4408 | /// 'lastprivate' '(' list ')' |
4409 | /// shared-clause: |
4410 | /// 'shared' '(' list ')' |
4411 | /// linear-clause: |
4412 | /// 'linear' '(' linear-list [ ':' linear-step ] ')' |
4413 | /// aligned-clause: |
4414 | /// 'aligned' '(' list [ ':' alignment ] ')' |
4415 | /// reduction-clause: |
4416 | /// 'reduction' '(' [ modifier ',' ] reduction-identifier ':' list ')' |
4417 | /// task_reduction-clause: |
4418 | /// 'task_reduction' '(' reduction-identifier ':' list ')' |
4419 | /// in_reduction-clause: |
4420 | /// 'in_reduction' '(' reduction-identifier ':' list ')' |
4421 | /// copyprivate-clause: |
4422 | /// 'copyprivate' '(' list ')' |
4423 | /// flush-clause: |
4424 | /// 'flush' '(' list ')' |
4425 | /// depend-clause: |
4426 | /// 'depend' '(' in | out | inout : list | source ')' |
4427 | /// map-clause: |
4428 | /// 'map' '(' [ [ always [,] ] [ close [,] ] |
4429 | /// [ mapper '(' mapper-identifier ')' [,] ] |
4430 | /// to | from | tofrom | alloc | release | delete ':' ] list ')'; |
4431 | /// to-clause: |
4432 | /// 'to' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')' |
4433 | /// from-clause: |
4434 | /// 'from' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')' |
4435 | /// use_device_ptr-clause: |
4436 | /// 'use_device_ptr' '(' list ')' |
4437 | /// use_device_addr-clause: |
4438 | /// 'use_device_addr' '(' list ')' |
4439 | /// is_device_ptr-clause: |
4440 | /// 'is_device_ptr' '(' list ')' |
4441 | /// allocate-clause: |
4442 | /// 'allocate' '(' [ allocator ':' ] list ')' |
4443 | /// nontemporal-clause: |
4444 | /// 'nontemporal' '(' list ')' |
4445 | /// inclusive-clause: |
4446 | /// 'inclusive' '(' list ')' |
4447 | /// exclusive-clause: |
4448 | /// 'exclusive' '(' list ')' |
4449 | /// |
4450 | /// For 'linear' clause linear-list may have the following forms: |
4451 | /// list |
4452 | /// modifier(list) |
4453 | /// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++). |
4454 | OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind, |
4455 | OpenMPClauseKind Kind, |
4456 | bool ParseOnly) { |
4457 | SourceLocation Loc = Tok.getLocation(); |
4458 | SourceLocation LOpen = ConsumeToken(); |
4459 | SmallVector<Expr *, 4> Vars; |
4460 | OpenMPVarListDataTy Data; |
4461 | |
4462 | if (ParseOpenMPVarList(DKind, Kind, Vars, Data)) |
4463 | return nullptr; |
4464 | |
4465 | if (ParseOnly) |
4466 | return nullptr; |
4467 | OMPVarListLocTy Locs(Loc, LOpen, Data.RLoc); |
4468 | return Actions.ActOnOpenMPVarListClause( |
4469 | Kind, Vars, Data.DepModOrTailExpr, Locs, Data.ColonLoc, |
4470 | Data.ReductionOrMapperIdScopeSpec, Data.ReductionOrMapperId, |
4471 | Data.ExtraModifier, Data.MapTypeModifiers, Data.MapTypeModifiersLoc, |
4472 | Data.IsMapTypeImplicit, Data.ExtraModifierLoc, Data.MotionModifiers, |
4473 | Data.MotionModifiersLoc); |
4474 | } |