File: | clang/lib/Sema/TreeTransform.h |
Warning: | line 13618, column 10 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// | |||
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 | // | |||
9 | // This file implements semantic analysis for expressions. | |||
10 | // | |||
11 | //===----------------------------------------------------------------------===// | |||
12 | ||||
13 | #include "TreeTransform.h" | |||
14 | #include "clang/AST/ASTConsumer.h" | |||
15 | #include "clang/AST/ASTContext.h" | |||
16 | #include "clang/AST/ASTLambda.h" | |||
17 | #include "clang/AST/ASTMutationListener.h" | |||
18 | #include "clang/AST/CXXInheritance.h" | |||
19 | #include "clang/AST/DeclObjC.h" | |||
20 | #include "clang/AST/DeclTemplate.h" | |||
21 | #include "clang/AST/EvaluatedExprVisitor.h" | |||
22 | #include "clang/AST/Expr.h" | |||
23 | #include "clang/AST/ExprCXX.h" | |||
24 | #include "clang/AST/ExprObjC.h" | |||
25 | #include "clang/AST/ExprOpenMP.h" | |||
26 | #include "clang/AST/RecursiveASTVisitor.h" | |||
27 | #include "clang/AST/TypeLoc.h" | |||
28 | #include "clang/Basic/Builtins.h" | |||
29 | #include "clang/Basic/FixedPoint.h" | |||
30 | #include "clang/Basic/PartialDiagnostic.h" | |||
31 | #include "clang/Basic/SourceManager.h" | |||
32 | #include "clang/Basic/TargetInfo.h" | |||
33 | #include "clang/Lex/LiteralSupport.h" | |||
34 | #include "clang/Lex/Preprocessor.h" | |||
35 | #include "clang/Sema/AnalysisBasedWarnings.h" | |||
36 | #include "clang/Sema/DeclSpec.h" | |||
37 | #include "clang/Sema/DelayedDiagnostic.h" | |||
38 | #include "clang/Sema/Designator.h" | |||
39 | #include "clang/Sema/Initialization.h" | |||
40 | #include "clang/Sema/Lookup.h" | |||
41 | #include "clang/Sema/Overload.h" | |||
42 | #include "clang/Sema/ParsedTemplate.h" | |||
43 | #include "clang/Sema/Scope.h" | |||
44 | #include "clang/Sema/ScopeInfo.h" | |||
45 | #include "clang/Sema/SemaFixItUtils.h" | |||
46 | #include "clang/Sema/SemaInternal.h" | |||
47 | #include "clang/Sema/Template.h" | |||
48 | #include "llvm/Support/ConvertUTF.h" | |||
49 | #include "llvm/Support/SaveAndRestore.h" | |||
50 | using namespace clang; | |||
51 | using namespace sema; | |||
52 | ||||
53 | /// Determine whether the use of this declaration is valid, without | |||
54 | /// emitting diagnostics. | |||
55 | bool Sema::CanUseDecl(NamedDecl *D, bool TreatUnavailableAsInvalid) { | |||
56 | // See if this is an auto-typed variable whose initializer we are parsing. | |||
57 | if (ParsingInitForAutoVars.count(D)) | |||
58 | return false; | |||
59 | ||||
60 | // See if this is a deleted function. | |||
61 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { | |||
62 | if (FD->isDeleted()) | |||
63 | return false; | |||
64 | ||||
65 | // If the function has a deduced return type, and we can't deduce it, | |||
66 | // then we can't use it either. | |||
67 | if (getLangOpts().CPlusPlus14 && FD->getReturnType()->isUndeducedType() && | |||
68 | DeduceReturnType(FD, SourceLocation(), /*Diagnose*/ false)) | |||
69 | return false; | |||
70 | ||||
71 | // See if this is an aligned allocation/deallocation function that is | |||
72 | // unavailable. | |||
73 | if (TreatUnavailableAsInvalid && | |||
74 | isUnavailableAlignedAllocationFunction(*FD)) | |||
75 | return false; | |||
76 | } | |||
77 | ||||
78 | // See if this function is unavailable. | |||
79 | if (TreatUnavailableAsInvalid && D->getAvailability() == AR_Unavailable && | |||
80 | cast<Decl>(CurContext)->getAvailability() != AR_Unavailable) | |||
81 | return false; | |||
82 | ||||
83 | return true; | |||
84 | } | |||
85 | ||||
86 | static void DiagnoseUnusedOfDecl(Sema &S, NamedDecl *D, SourceLocation Loc) { | |||
87 | // Warn if this is used but marked unused. | |||
88 | if (const auto *A = D->getAttr<UnusedAttr>()) { | |||
89 | // [[maybe_unused]] should not diagnose uses, but __attribute__((unused)) | |||
90 | // should diagnose them. | |||
91 | if (A->getSemanticSpelling() != UnusedAttr::CXX11_maybe_unused && | |||
92 | A->getSemanticSpelling() != UnusedAttr::C2x_maybe_unused) { | |||
93 | const Decl *DC = cast_or_null<Decl>(S.getCurObjCLexicalContext()); | |||
94 | if (DC && !DC->hasAttr<UnusedAttr>()) | |||
95 | S.Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); | |||
96 | } | |||
97 | } | |||
98 | } | |||
99 | ||||
100 | /// Emit a note explaining that this function is deleted. | |||
101 | void Sema::NoteDeletedFunction(FunctionDecl *Decl) { | |||
102 | assert(Decl && Decl->isDeleted())((Decl && Decl->isDeleted()) ? static_cast<void > (0) : __assert_fail ("Decl && Decl->isDeleted()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 102, __PRETTY_FUNCTION__)); | |||
103 | ||||
104 | if (Decl->isDefaulted()) { | |||
105 | // If the method was explicitly defaulted, point at that declaration. | |||
106 | if (!Decl->isImplicit()) | |||
107 | Diag(Decl->getLocation(), diag::note_implicitly_deleted); | |||
108 | ||||
109 | // Try to diagnose why this special member function was implicitly | |||
110 | // deleted. This might fail, if that reason no longer applies. | |||
111 | DiagnoseDeletedDefaultedFunction(Decl); | |||
112 | return; | |||
113 | } | |||
114 | ||||
115 | auto *Ctor = dyn_cast<CXXConstructorDecl>(Decl); | |||
116 | if (Ctor && Ctor->isInheritingConstructor()) | |||
117 | return NoteDeletedInheritingConstructor(Ctor); | |||
118 | ||||
119 | Diag(Decl->getLocation(), diag::note_availability_specified_here) | |||
120 | << Decl << 1; | |||
121 | } | |||
122 | ||||
123 | /// Determine whether a FunctionDecl was ever declared with an | |||
124 | /// explicit storage class. | |||
125 | static bool hasAnyExplicitStorageClass(const FunctionDecl *D) { | |||
126 | for (auto I : D->redecls()) { | |||
127 | if (I->getStorageClass() != SC_None) | |||
128 | return true; | |||
129 | } | |||
130 | return false; | |||
131 | } | |||
132 | ||||
133 | /// Check whether we're in an extern inline function and referring to a | |||
134 | /// variable or function with internal linkage (C11 6.7.4p3). | |||
135 | /// | |||
136 | /// This is only a warning because we used to silently accept this code, but | |||
137 | /// in many cases it will not behave correctly. This is not enabled in C++ mode | |||
138 | /// because the restriction language is a bit weaker (C++11 [basic.def.odr]p6) | |||
139 | /// and so while there may still be user mistakes, most of the time we can't | |||
140 | /// prove that there are errors. | |||
141 | static void diagnoseUseOfInternalDeclInInlineFunction(Sema &S, | |||
142 | const NamedDecl *D, | |||
143 | SourceLocation Loc) { | |||
144 | // This is disabled under C++; there are too many ways for this to fire in | |||
145 | // contexts where the warning is a false positive, or where it is technically | |||
146 | // correct but benign. | |||
147 | if (S.getLangOpts().CPlusPlus) | |||
148 | return; | |||
149 | ||||
150 | // Check if this is an inlined function or method. | |||
151 | FunctionDecl *Current = S.getCurFunctionDecl(); | |||
152 | if (!Current) | |||
153 | return; | |||
154 | if (!Current->isInlined()) | |||
155 | return; | |||
156 | if (!Current->isExternallyVisible()) | |||
157 | return; | |||
158 | ||||
159 | // Check if the decl has internal linkage. | |||
160 | if (D->getFormalLinkage() != InternalLinkage) | |||
161 | return; | |||
162 | ||||
163 | // Downgrade from ExtWarn to Extension if | |||
164 | // (1) the supposedly external inline function is in the main file, | |||
165 | // and probably won't be included anywhere else. | |||
166 | // (2) the thing we're referencing is a pure function. | |||
167 | // (3) the thing we're referencing is another inline function. | |||
168 | // This last can give us false negatives, but it's better than warning on | |||
169 | // wrappers for simple C library functions. | |||
170 | const FunctionDecl *UsedFn = dyn_cast<FunctionDecl>(D); | |||
171 | bool DowngradeWarning = S.getSourceManager().isInMainFile(Loc); | |||
172 | if (!DowngradeWarning && UsedFn) | |||
173 | DowngradeWarning = UsedFn->isInlined() || UsedFn->hasAttr<ConstAttr>(); | |||
174 | ||||
175 | S.Diag(Loc, DowngradeWarning ? diag::ext_internal_in_extern_inline_quiet | |||
176 | : diag::ext_internal_in_extern_inline) | |||
177 | << /*IsVar=*/!UsedFn << D; | |||
178 | ||||
179 | S.MaybeSuggestAddingStaticToDecl(Current); | |||
180 | ||||
181 | S.Diag(D->getCanonicalDecl()->getLocation(), diag::note_entity_declared_at) | |||
182 | << D; | |||
183 | } | |||
184 | ||||
185 | void Sema::MaybeSuggestAddingStaticToDecl(const FunctionDecl *Cur) { | |||
186 | const FunctionDecl *First = Cur->getFirstDecl(); | |||
187 | ||||
188 | // Suggest "static" on the function, if possible. | |||
189 | if (!hasAnyExplicitStorageClass(First)) { | |||
190 | SourceLocation DeclBegin = First->getSourceRange().getBegin(); | |||
191 | Diag(DeclBegin, diag::note_convert_inline_to_static) | |||
192 | << Cur << FixItHint::CreateInsertion(DeclBegin, "static "); | |||
193 | } | |||
194 | } | |||
195 | ||||
196 | /// Determine whether the use of this declaration is valid, and | |||
197 | /// emit any corresponding diagnostics. | |||
198 | /// | |||
199 | /// This routine diagnoses various problems with referencing | |||
200 | /// declarations that can occur when using a declaration. For example, | |||
201 | /// it might warn if a deprecated or unavailable declaration is being | |||
202 | /// used, or produce an error (and return true) if a C++0x deleted | |||
203 | /// function is being used. | |||
204 | /// | |||
205 | /// \returns true if there was an error (this declaration cannot be | |||
206 | /// referenced), false otherwise. | |||
207 | /// | |||
208 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, ArrayRef<SourceLocation> Locs, | |||
209 | const ObjCInterfaceDecl *UnknownObjCClass, | |||
210 | bool ObjCPropertyAccess, | |||
211 | bool AvoidPartialAvailabilityChecks, | |||
212 | ObjCInterfaceDecl *ClassReceiver) { | |||
213 | SourceLocation Loc = Locs.front(); | |||
214 | if (getLangOpts().CPlusPlus && isa<FunctionDecl>(D)) { | |||
215 | // If there were any diagnostics suppressed by template argument deduction, | |||
216 | // emit them now. | |||
217 | auto Pos = SuppressedDiagnostics.find(D->getCanonicalDecl()); | |||
218 | if (Pos != SuppressedDiagnostics.end()) { | |||
219 | for (const PartialDiagnosticAt &Suppressed : Pos->second) | |||
220 | Diag(Suppressed.first, Suppressed.second); | |||
221 | ||||
222 | // Clear out the list of suppressed diagnostics, so that we don't emit | |||
223 | // them again for this specialization. However, we don't obsolete this | |||
224 | // entry from the table, because we want to avoid ever emitting these | |||
225 | // diagnostics again. | |||
226 | Pos->second.clear(); | |||
227 | } | |||
228 | ||||
229 | // C++ [basic.start.main]p3: | |||
230 | // The function 'main' shall not be used within a program. | |||
231 | if (cast<FunctionDecl>(D)->isMain()) | |||
232 | Diag(Loc, diag::ext_main_used); | |||
233 | ||||
234 | diagnoseUnavailableAlignedAllocation(*cast<FunctionDecl>(D), Loc); | |||
235 | } | |||
236 | ||||
237 | // See if this is an auto-typed variable whose initializer we are parsing. | |||
238 | if (ParsingInitForAutoVars.count(D)) { | |||
239 | if (isa<BindingDecl>(D)) { | |||
240 | Diag(Loc, diag::err_binding_cannot_appear_in_own_initializer) | |||
241 | << D->getDeclName(); | |||
242 | } else { | |||
243 | Diag(Loc, diag::err_auto_variable_cannot_appear_in_own_initializer) | |||
244 | << D->getDeclName() << cast<VarDecl>(D)->getType(); | |||
245 | } | |||
246 | return true; | |||
247 | } | |||
248 | ||||
249 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { | |||
250 | // See if this is a deleted function. | |||
251 | if (FD->isDeleted()) { | |||
252 | auto *Ctor = dyn_cast<CXXConstructorDecl>(FD); | |||
253 | if (Ctor && Ctor->isInheritingConstructor()) | |||
254 | Diag(Loc, diag::err_deleted_inherited_ctor_use) | |||
255 | << Ctor->getParent() | |||
256 | << Ctor->getInheritedConstructor().getConstructor()->getParent(); | |||
257 | else | |||
258 | Diag(Loc, diag::err_deleted_function_use); | |||
259 | NoteDeletedFunction(FD); | |||
260 | return true; | |||
261 | } | |||
262 | ||||
263 | // [expr.prim.id]p4 | |||
264 | // A program that refers explicitly or implicitly to a function with a | |||
265 | // trailing requires-clause whose constraint-expression is not satisfied, | |||
266 | // other than to declare it, is ill-formed. [...] | |||
267 | // | |||
268 | // See if this is a function with constraints that need to be satisfied. | |||
269 | // Check this before deducing the return type, as it might instantiate the | |||
270 | // definition. | |||
271 | if (FD->getTrailingRequiresClause()) { | |||
272 | ConstraintSatisfaction Satisfaction; | |||
273 | if (CheckFunctionConstraints(FD, Satisfaction, Loc)) | |||
274 | // A diagnostic will have already been generated (non-constant | |||
275 | // constraint expression, for example) | |||
276 | return true; | |||
277 | if (!Satisfaction.IsSatisfied) { | |||
278 | Diag(Loc, | |||
279 | diag::err_reference_to_function_with_unsatisfied_constraints) | |||
280 | << D; | |||
281 | DiagnoseUnsatisfiedConstraint(Satisfaction); | |||
282 | return true; | |||
283 | } | |||
284 | } | |||
285 | ||||
286 | // If the function has a deduced return type, and we can't deduce it, | |||
287 | // then we can't use it either. | |||
288 | if (getLangOpts().CPlusPlus14 && FD->getReturnType()->isUndeducedType() && | |||
289 | DeduceReturnType(FD, Loc)) | |||
290 | return true; | |||
291 | ||||
292 | if (getLangOpts().CUDA && !CheckCUDACall(Loc, FD)) | |||
293 | return true; | |||
294 | } | |||
295 | ||||
296 | if (auto *MD = dyn_cast<CXXMethodDecl>(D)) { | |||
297 | // Lambdas are only default-constructible or assignable in C++2a onwards. | |||
298 | if (MD->getParent()->isLambda() && | |||
299 | ((isa<CXXConstructorDecl>(MD) && | |||
300 | cast<CXXConstructorDecl>(MD)->isDefaultConstructor()) || | |||
301 | MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())) { | |||
302 | Diag(Loc, diag::warn_cxx17_compat_lambda_def_ctor_assign) | |||
303 | << !isa<CXXConstructorDecl>(MD); | |||
304 | } | |||
305 | } | |||
306 | ||||
307 | auto getReferencedObjCProp = [](const NamedDecl *D) -> | |||
308 | const ObjCPropertyDecl * { | |||
309 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) | |||
310 | return MD->findPropertyDecl(); | |||
311 | return nullptr; | |||
312 | }; | |||
313 | if (const ObjCPropertyDecl *ObjCPDecl = getReferencedObjCProp(D)) { | |||
314 | if (diagnoseArgIndependentDiagnoseIfAttrs(ObjCPDecl, Loc)) | |||
315 | return true; | |||
316 | } else if (diagnoseArgIndependentDiagnoseIfAttrs(D, Loc)) { | |||
317 | return true; | |||
318 | } | |||
319 | ||||
320 | // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions | |||
321 | // Only the variables omp_in and omp_out are allowed in the combiner. | |||
322 | // Only the variables omp_priv and omp_orig are allowed in the | |||
323 | // initializer-clause. | |||
324 | auto *DRD = dyn_cast<OMPDeclareReductionDecl>(CurContext); | |||
325 | if (LangOpts.OpenMP && DRD && !CurContext->containsDecl(D) && | |||
326 | isa<VarDecl>(D)) { | |||
327 | Diag(Loc, diag::err_omp_wrong_var_in_declare_reduction) | |||
328 | << getCurFunction()->HasOMPDeclareReductionCombiner; | |||
329 | Diag(D->getLocation(), diag::note_entity_declared_at) << D; | |||
330 | return true; | |||
331 | } | |||
332 | ||||
333 | // [OpenMP 5.0], 2.19.7.3. declare mapper Directive, Restrictions | |||
334 | // List-items in map clauses on this construct may only refer to the declared | |||
335 | // variable var and entities that could be referenced by a procedure defined | |||
336 | // at the same location | |||
337 | auto *DMD = dyn_cast<OMPDeclareMapperDecl>(CurContext); | |||
338 | if (LangOpts.OpenMP && DMD && !CurContext->containsDecl(D) && | |||
339 | isa<VarDecl>(D)) { | |||
340 | Diag(Loc, diag::err_omp_declare_mapper_wrong_var) | |||
341 | << DMD->getVarName().getAsString(); | |||
342 | Diag(D->getLocation(), diag::note_entity_declared_at) << D; | |||
343 | return true; | |||
344 | } | |||
345 | ||||
346 | DiagnoseAvailabilityOfDecl(D, Locs, UnknownObjCClass, ObjCPropertyAccess, | |||
347 | AvoidPartialAvailabilityChecks, ClassReceiver); | |||
348 | ||||
349 | DiagnoseUnusedOfDecl(*this, D, Loc); | |||
350 | ||||
351 | diagnoseUseOfInternalDeclInInlineFunction(*this, D, Loc); | |||
352 | ||||
353 | if (isa<ParmVarDecl>(D) && isa<RequiresExprBodyDecl>(D->getDeclContext()) && | |||
354 | !isUnevaluatedContext()) { | |||
355 | // C++ [expr.prim.req.nested] p3 | |||
356 | // A local parameter shall only appear as an unevaluated operand | |||
357 | // (Clause 8) within the constraint-expression. | |||
358 | Diag(Loc, diag::err_requires_expr_parameter_referenced_in_evaluated_context) | |||
359 | << D; | |||
360 | Diag(D->getLocation(), diag::note_entity_declared_at) << D; | |||
361 | return true; | |||
362 | } | |||
363 | ||||
364 | return false; | |||
365 | } | |||
366 | ||||
367 | /// DiagnoseSentinelCalls - This routine checks whether a call or | |||
368 | /// message-send is to a declaration with the sentinel attribute, and | |||
369 | /// if so, it checks that the requirements of the sentinel are | |||
370 | /// satisfied. | |||
371 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, | |||
372 | ArrayRef<Expr *> Args) { | |||
373 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); | |||
374 | if (!attr) | |||
375 | return; | |||
376 | ||||
377 | // The number of formal parameters of the declaration. | |||
378 | unsigned numFormalParams; | |||
379 | ||||
380 | // The kind of declaration. This is also an index into a %select in | |||
381 | // the diagnostic. | |||
382 | enum CalleeType { CT_Function, CT_Method, CT_Block } calleeType; | |||
383 | ||||
384 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { | |||
385 | numFormalParams = MD->param_size(); | |||
386 | calleeType = CT_Method; | |||
387 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { | |||
388 | numFormalParams = FD->param_size(); | |||
389 | calleeType = CT_Function; | |||
390 | } else if (isa<VarDecl>(D)) { | |||
391 | QualType type = cast<ValueDecl>(D)->getType(); | |||
392 | const FunctionType *fn = nullptr; | |||
393 | if (const PointerType *ptr = type->getAs<PointerType>()) { | |||
394 | fn = ptr->getPointeeType()->getAs<FunctionType>(); | |||
395 | if (!fn) return; | |||
396 | calleeType = CT_Function; | |||
397 | } else if (const BlockPointerType *ptr = type->getAs<BlockPointerType>()) { | |||
398 | fn = ptr->getPointeeType()->castAs<FunctionType>(); | |||
399 | calleeType = CT_Block; | |||
400 | } else { | |||
401 | return; | |||
402 | } | |||
403 | ||||
404 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fn)) { | |||
405 | numFormalParams = proto->getNumParams(); | |||
406 | } else { | |||
407 | numFormalParams = 0; | |||
408 | } | |||
409 | } else { | |||
410 | return; | |||
411 | } | |||
412 | ||||
413 | // "nullPos" is the number of formal parameters at the end which | |||
414 | // effectively count as part of the variadic arguments. This is | |||
415 | // useful if you would prefer to not have *any* formal parameters, | |||
416 | // but the language forces you to have at least one. | |||
417 | unsigned nullPos = attr->getNullPos(); | |||
418 | assert((nullPos == 0 || nullPos == 1) && "invalid null position on sentinel")(((nullPos == 0 || nullPos == 1) && "invalid null position on sentinel" ) ? static_cast<void> (0) : __assert_fail ("(nullPos == 0 || nullPos == 1) && \"invalid null position on sentinel\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 418, __PRETTY_FUNCTION__)); | |||
419 | numFormalParams = (nullPos > numFormalParams ? 0 : numFormalParams - nullPos); | |||
420 | ||||
421 | // The number of arguments which should follow the sentinel. | |||
422 | unsigned numArgsAfterSentinel = attr->getSentinel(); | |||
423 | ||||
424 | // If there aren't enough arguments for all the formal parameters, | |||
425 | // the sentinel, and the args after the sentinel, complain. | |||
426 | if (Args.size() < numFormalParams + numArgsAfterSentinel + 1) { | |||
427 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); | |||
428 | Diag(D->getLocation(), diag::note_sentinel_here) << int(calleeType); | |||
429 | return; | |||
430 | } | |||
431 | ||||
432 | // Otherwise, find the sentinel expression. | |||
433 | Expr *sentinelExpr = Args[Args.size() - numArgsAfterSentinel - 1]; | |||
434 | if (!sentinelExpr) return; | |||
435 | if (sentinelExpr->isValueDependent()) return; | |||
436 | if (Context.isSentinelNullExpr(sentinelExpr)) return; | |||
437 | ||||
438 | // Pick a reasonable string to insert. Optimistically use 'nil', 'nullptr', | |||
439 | // or 'NULL' if those are actually defined in the context. Only use | |||
440 | // 'nil' for ObjC methods, where it's much more likely that the | |||
441 | // variadic arguments form a list of object pointers. | |||
442 | SourceLocation MissingNilLoc = getLocForEndOfToken(sentinelExpr->getEndLoc()); | |||
443 | std::string NullValue; | |||
444 | if (calleeType == CT_Method && PP.isMacroDefined("nil")) | |||
445 | NullValue = "nil"; | |||
446 | else if (getLangOpts().CPlusPlus11) | |||
447 | NullValue = "nullptr"; | |||
448 | else if (PP.isMacroDefined("NULL")) | |||
449 | NullValue = "NULL"; | |||
450 | else | |||
451 | NullValue = "(void*) 0"; | |||
452 | ||||
453 | if (MissingNilLoc.isInvalid()) | |||
454 | Diag(Loc, diag::warn_missing_sentinel) << int(calleeType); | |||
455 | else | |||
456 | Diag(MissingNilLoc, diag::warn_missing_sentinel) | |||
457 | << int(calleeType) | |||
458 | << FixItHint::CreateInsertion(MissingNilLoc, ", " + NullValue); | |||
459 | Diag(D->getLocation(), diag::note_sentinel_here) << int(calleeType); | |||
460 | } | |||
461 | ||||
462 | SourceRange Sema::getExprRange(Expr *E) const { | |||
463 | return E ? E->getSourceRange() : SourceRange(); | |||
464 | } | |||
465 | ||||
466 | //===----------------------------------------------------------------------===// | |||
467 | // Standard Promotions and Conversions | |||
468 | //===----------------------------------------------------------------------===// | |||
469 | ||||
470 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). | |||
471 | ExprResult Sema::DefaultFunctionArrayConversion(Expr *E, bool Diagnose) { | |||
472 | // Handle any placeholder expressions which made it here. | |||
473 | if (E->getType()->isPlaceholderType()) { | |||
474 | ExprResult result = CheckPlaceholderExpr(E); | |||
475 | if (result.isInvalid()) return ExprError(); | |||
476 | E = result.get(); | |||
477 | } | |||
478 | ||||
479 | QualType Ty = E->getType(); | |||
480 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type")((!Ty.isNull() && "DefaultFunctionArrayConversion - missing type" ) ? static_cast<void> (0) : __assert_fail ("!Ty.isNull() && \"DefaultFunctionArrayConversion - missing type\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 480, __PRETTY_FUNCTION__)); | |||
481 | ||||
482 | if (Ty->isFunctionType()) { | |||
483 | if (auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts())) | |||
484 | if (auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) | |||
485 | if (!checkAddressOfFunctionIsAvailable(FD, Diagnose, E->getExprLoc())) | |||
486 | return ExprError(); | |||
487 | ||||
488 | E = ImpCastExprToType(E, Context.getPointerType(Ty), | |||
489 | CK_FunctionToPointerDecay).get(); | |||
490 | } else if (Ty->isArrayType()) { | |||
491 | // In C90 mode, arrays only promote to pointers if the array expression is | |||
492 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has | |||
493 | // type 'array of type' is converted to an expression that has type 'pointer | |||
494 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression | |||
495 | // that has type 'array of type' ...". The relevant change is "an lvalue" | |||
496 | // (C90) to "an expression" (C99). | |||
497 | // | |||
498 | // C++ 4.2p1: | |||
499 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of | |||
500 | // T" can be converted to an rvalue of type "pointer to T". | |||
501 | // | |||
502 | if (getLangOpts().C99 || getLangOpts().CPlusPlus || E->isLValue()) | |||
503 | E = ImpCastExprToType(E, Context.getArrayDecayedType(Ty), | |||
504 | CK_ArrayToPointerDecay).get(); | |||
505 | } | |||
506 | return E; | |||
507 | } | |||
508 | ||||
509 | static void CheckForNullPointerDereference(Sema &S, Expr *E) { | |||
510 | // Check to see if we are dereferencing a null pointer. If so, | |||
511 | // and if not volatile-qualified, this is undefined behavior that the | |||
512 | // optimizer will delete, so warn about it. People sometimes try to use this | |||
513 | // to get a deterministic trap and are surprised by clang's behavior. This | |||
514 | // only handles the pattern "*null", which is a very syntactic check. | |||
515 | const auto *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts()); | |||
516 | if (UO && UO->getOpcode() == UO_Deref && | |||
517 | UO->getSubExpr()->getType()->isPointerType()) { | |||
518 | const LangAS AS = | |||
519 | UO->getSubExpr()->getType()->getPointeeType().getAddressSpace(); | |||
520 | if ((!isTargetAddressSpace(AS) || | |||
521 | (isTargetAddressSpace(AS) && toTargetAddressSpace(AS) == 0)) && | |||
522 | UO->getSubExpr()->IgnoreParenCasts()->isNullPointerConstant( | |||
523 | S.Context, Expr::NPC_ValueDependentIsNotNull) && | |||
524 | !UO->getType().isVolatileQualified()) { | |||
525 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, | |||
526 | S.PDiag(diag::warn_indirection_through_null) | |||
527 | << UO->getSubExpr()->getSourceRange()); | |||
528 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, | |||
529 | S.PDiag(diag::note_indirection_through_null)); | |||
530 | } | |||
531 | } | |||
532 | } | |||
533 | ||||
534 | static void DiagnoseDirectIsaAccess(Sema &S, const ObjCIvarRefExpr *OIRE, | |||
535 | SourceLocation AssignLoc, | |||
536 | const Expr* RHS) { | |||
537 | const ObjCIvarDecl *IV = OIRE->getDecl(); | |||
538 | if (!IV) | |||
539 | return; | |||
540 | ||||
541 | DeclarationName MemberName = IV->getDeclName(); | |||
542 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); | |||
543 | if (!Member || !Member->isStr("isa")) | |||
544 | return; | |||
545 | ||||
546 | const Expr *Base = OIRE->getBase(); | |||
547 | QualType BaseType = Base->getType(); | |||
548 | if (OIRE->isArrow()) | |||
549 | BaseType = BaseType->getPointeeType(); | |||
550 | if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) | |||
551 | if (ObjCInterfaceDecl *IDecl = OTy->getInterface()) { | |||
552 | ObjCInterfaceDecl *ClassDeclared = nullptr; | |||
553 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); | |||
554 | if (!ClassDeclared->getSuperClass() | |||
555 | && (*ClassDeclared->ivar_begin()) == IV) { | |||
556 | if (RHS) { | |||
557 | NamedDecl *ObjectSetClass = | |||
558 | S.LookupSingleName(S.TUScope, | |||
559 | &S.Context.Idents.get("object_setClass"), | |||
560 | SourceLocation(), S.LookupOrdinaryName); | |||
561 | if (ObjectSetClass) { | |||
562 | SourceLocation RHSLocEnd = S.getLocForEndOfToken(RHS->getEndLoc()); | |||
563 | S.Diag(OIRE->getExprLoc(), diag::warn_objc_isa_assign) | |||
564 | << FixItHint::CreateInsertion(OIRE->getBeginLoc(), | |||
565 | "object_setClass(") | |||
566 | << FixItHint::CreateReplacement( | |||
567 | SourceRange(OIRE->getOpLoc(), AssignLoc), ",") | |||
568 | << FixItHint::CreateInsertion(RHSLocEnd, ")"); | |||
569 | } | |||
570 | else | |||
571 | S.Diag(OIRE->getLocation(), diag::warn_objc_isa_assign); | |||
572 | } else { | |||
573 | NamedDecl *ObjectGetClass = | |||
574 | S.LookupSingleName(S.TUScope, | |||
575 | &S.Context.Idents.get("object_getClass"), | |||
576 | SourceLocation(), S.LookupOrdinaryName); | |||
577 | if (ObjectGetClass) | |||
578 | S.Diag(OIRE->getExprLoc(), diag::warn_objc_isa_use) | |||
579 | << FixItHint::CreateInsertion(OIRE->getBeginLoc(), | |||
580 | "object_getClass(") | |||
581 | << FixItHint::CreateReplacement( | |||
582 | SourceRange(OIRE->getOpLoc(), OIRE->getEndLoc()), ")"); | |||
583 | else | |||
584 | S.Diag(OIRE->getLocation(), diag::warn_objc_isa_use); | |||
585 | } | |||
586 | S.Diag(IV->getLocation(), diag::note_ivar_decl); | |||
587 | } | |||
588 | } | |||
589 | } | |||
590 | ||||
591 | ExprResult Sema::DefaultLvalueConversion(Expr *E) { | |||
592 | // Handle any placeholder expressions which made it here. | |||
593 | if (E->getType()->isPlaceholderType()) { | |||
594 | ExprResult result = CheckPlaceholderExpr(E); | |||
595 | if (result.isInvalid()) return ExprError(); | |||
596 | E = result.get(); | |||
597 | } | |||
598 | ||||
599 | // C++ [conv.lval]p1: | |||
600 | // A glvalue of a non-function, non-array type T can be | |||
601 | // converted to a prvalue. | |||
602 | if (!E->isGLValue()) return E; | |||
603 | ||||
604 | QualType T = E->getType(); | |||
605 | assert(!T.isNull() && "r-value conversion on typeless expression?")((!T.isNull() && "r-value conversion on typeless expression?" ) ? static_cast<void> (0) : __assert_fail ("!T.isNull() && \"r-value conversion on typeless expression?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 605, __PRETTY_FUNCTION__)); | |||
606 | ||||
607 | // We don't want to throw lvalue-to-rvalue casts on top of | |||
608 | // expressions of certain types in C++. | |||
609 | if (getLangOpts().CPlusPlus && | |||
610 | (E->getType() == Context.OverloadTy || | |||
611 | T->isDependentType() || | |||
612 | T->isRecordType())) | |||
613 | return E; | |||
614 | ||||
615 | // The C standard is actually really unclear on this point, and | |||
616 | // DR106 tells us what the result should be but not why. It's | |||
617 | // generally best to say that void types just doesn't undergo | |||
618 | // lvalue-to-rvalue at all. Note that expressions of unqualified | |||
619 | // 'void' type are never l-values, but qualified void can be. | |||
620 | if (T->isVoidType()) | |||
621 | return E; | |||
622 | ||||
623 | // OpenCL usually rejects direct accesses to values of 'half' type. | |||
624 | if (getLangOpts().OpenCL && !getOpenCLOptions().isEnabled("cl_khr_fp16") && | |||
625 | T->isHalfType()) { | |||
626 | Diag(E->getExprLoc(), diag::err_opencl_half_load_store) | |||
627 | << 0 << T; | |||
628 | return ExprError(); | |||
629 | } | |||
630 | ||||
631 | CheckForNullPointerDereference(*this, E); | |||
632 | if (const ObjCIsaExpr *OISA = dyn_cast<ObjCIsaExpr>(E->IgnoreParenCasts())) { | |||
633 | NamedDecl *ObjectGetClass = LookupSingleName(TUScope, | |||
634 | &Context.Idents.get("object_getClass"), | |||
635 | SourceLocation(), LookupOrdinaryName); | |||
636 | if (ObjectGetClass) | |||
637 | Diag(E->getExprLoc(), diag::warn_objc_isa_use) | |||
638 | << FixItHint::CreateInsertion(OISA->getBeginLoc(), "object_getClass(") | |||
639 | << FixItHint::CreateReplacement( | |||
640 | SourceRange(OISA->getOpLoc(), OISA->getIsaMemberLoc()), ")"); | |||
641 | else | |||
642 | Diag(E->getExprLoc(), diag::warn_objc_isa_use); | |||
643 | } | |||
644 | else if (const ObjCIvarRefExpr *OIRE = | |||
645 | dyn_cast<ObjCIvarRefExpr>(E->IgnoreParenCasts())) | |||
646 | DiagnoseDirectIsaAccess(*this, OIRE, SourceLocation(), /* Expr*/nullptr); | |||
647 | ||||
648 | // C++ [conv.lval]p1: | |||
649 | // [...] If T is a non-class type, the type of the prvalue is the | |||
650 | // cv-unqualified version of T. Otherwise, the type of the | |||
651 | // rvalue is T. | |||
652 | // | |||
653 | // C99 6.3.2.1p2: | |||
654 | // If the lvalue has qualified type, the value has the unqualified | |||
655 | // version of the type of the lvalue; otherwise, the value has the | |||
656 | // type of the lvalue. | |||
657 | if (T.hasQualifiers()) | |||
658 | T = T.getUnqualifiedType(); | |||
659 | ||||
660 | // Under the MS ABI, lock down the inheritance model now. | |||
661 | if (T->isMemberPointerType() && | |||
662 | Context.getTargetInfo().getCXXABI().isMicrosoft()) | |||
663 | (void)isCompleteType(E->getExprLoc(), T); | |||
664 | ||||
665 | ExprResult Res = CheckLValueToRValueConversionOperand(E); | |||
666 | if (Res.isInvalid()) | |||
667 | return Res; | |||
668 | E = Res.get(); | |||
669 | ||||
670 | // Loading a __weak object implicitly retains the value, so we need a cleanup to | |||
671 | // balance that. | |||
672 | if (E->getType().getObjCLifetime() == Qualifiers::OCL_Weak) | |||
673 | Cleanup.setExprNeedsCleanups(true); | |||
674 | ||||
675 | // C++ [conv.lval]p3: | |||
676 | // If T is cv std::nullptr_t, the result is a null pointer constant. | |||
677 | CastKind CK = T->isNullPtrType() ? CK_NullToPointer : CK_LValueToRValue; | |||
678 | Res = ImplicitCastExpr::Create(Context, T, CK, E, nullptr, VK_RValue); | |||
679 | ||||
680 | // C11 6.3.2.1p2: | |||
681 | // ... if the lvalue has atomic type, the value has the non-atomic version | |||
682 | // of the type of the lvalue ... | |||
683 | if (const AtomicType *Atomic = T->getAs<AtomicType>()) { | |||
684 | T = Atomic->getValueType().getUnqualifiedType(); | |||
685 | Res = ImplicitCastExpr::Create(Context, T, CK_AtomicToNonAtomic, Res.get(), | |||
686 | nullptr, VK_RValue); | |||
687 | } | |||
688 | ||||
689 | return Res; | |||
690 | } | |||
691 | ||||
692 | ExprResult Sema::DefaultFunctionArrayLvalueConversion(Expr *E, bool Diagnose) { | |||
693 | ExprResult Res = DefaultFunctionArrayConversion(E, Diagnose); | |||
694 | if (Res.isInvalid()) | |||
695 | return ExprError(); | |||
696 | Res = DefaultLvalueConversion(Res.get()); | |||
697 | if (Res.isInvalid()) | |||
698 | return ExprError(); | |||
699 | return Res; | |||
700 | } | |||
701 | ||||
702 | /// CallExprUnaryConversions - a special case of an unary conversion | |||
703 | /// performed on a function designator of a call expression. | |||
704 | ExprResult Sema::CallExprUnaryConversions(Expr *E) { | |||
705 | QualType Ty = E->getType(); | |||
706 | ExprResult Res = E; | |||
707 | // Only do implicit cast for a function type, but not for a pointer | |||
708 | // to function type. | |||
709 | if (Ty->isFunctionType()) { | |||
710 | Res = ImpCastExprToType(E, Context.getPointerType(Ty), | |||
711 | CK_FunctionToPointerDecay).get(); | |||
712 | if (Res.isInvalid()) | |||
713 | return ExprError(); | |||
714 | } | |||
715 | Res = DefaultLvalueConversion(Res.get()); | |||
716 | if (Res.isInvalid()) | |||
717 | return ExprError(); | |||
718 | return Res.get(); | |||
719 | } | |||
720 | ||||
721 | /// UsualUnaryConversions - Performs various conversions that are common to most | |||
722 | /// operators (C99 6.3). The conversions of array and function types are | |||
723 | /// sometimes suppressed. For example, the array->pointer conversion doesn't | |||
724 | /// apply if the array is an argument to the sizeof or address (&) operators. | |||
725 | /// In these instances, this routine should *not* be called. | |||
726 | ExprResult Sema::UsualUnaryConversions(Expr *E) { | |||
727 | // First, convert to an r-value. | |||
728 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); | |||
729 | if (Res.isInvalid()) | |||
730 | return ExprError(); | |||
731 | E = Res.get(); | |||
732 | ||||
733 | QualType Ty = E->getType(); | |||
734 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type")((!Ty.isNull() && "UsualUnaryConversions - missing type" ) ? static_cast<void> (0) : __assert_fail ("!Ty.isNull() && \"UsualUnaryConversions - missing type\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 734, __PRETTY_FUNCTION__)); | |||
735 | ||||
736 | // Half FP have to be promoted to float unless it is natively supported | |||
737 | if (Ty->isHalfType() && !getLangOpts().NativeHalfType) | |||
738 | return ImpCastExprToType(Res.get(), Context.FloatTy, CK_FloatingCast); | |||
739 | ||||
740 | // Try to perform integral promotions if the object has a theoretically | |||
741 | // promotable type. | |||
742 | if (Ty->isIntegralOrUnscopedEnumerationType()) { | |||
743 | // C99 6.3.1.1p2: | |||
744 | // | |||
745 | // The following may be used in an expression wherever an int or | |||
746 | // unsigned int may be used: | |||
747 | // - an object or expression with an integer type whose integer | |||
748 | // conversion rank is less than or equal to the rank of int | |||
749 | // and unsigned int. | |||
750 | // - A bit-field of type _Bool, int, signed int, or unsigned int. | |||
751 | // | |||
752 | // If an int can represent all values of the original type, the | |||
753 | // value is converted to an int; otherwise, it is converted to an | |||
754 | // unsigned int. These are called the integer promotions. All | |||
755 | // other types are unchanged by the integer promotions. | |||
756 | ||||
757 | QualType PTy = Context.isPromotableBitField(E); | |||
758 | if (!PTy.isNull()) { | |||
759 | E = ImpCastExprToType(E, PTy, CK_IntegralCast).get(); | |||
760 | return E; | |||
761 | } | |||
762 | if (Ty->isPromotableIntegerType()) { | |||
763 | QualType PT = Context.getPromotedIntegerType(Ty); | |||
764 | E = ImpCastExprToType(E, PT, CK_IntegralCast).get(); | |||
765 | return E; | |||
766 | } | |||
767 | } | |||
768 | return E; | |||
769 | } | |||
770 | ||||
771 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that | |||
772 | /// do not have a prototype. Arguments that have type float or __fp16 | |||
773 | /// are promoted to double. All other argument types are converted by | |||
774 | /// UsualUnaryConversions(). | |||
775 | ExprResult Sema::DefaultArgumentPromotion(Expr *E) { | |||
776 | QualType Ty = E->getType(); | |||
777 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type")((!Ty.isNull() && "DefaultArgumentPromotion - missing type" ) ? static_cast<void> (0) : __assert_fail ("!Ty.isNull() && \"DefaultArgumentPromotion - missing type\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 777, __PRETTY_FUNCTION__)); | |||
778 | ||||
779 | ExprResult Res = UsualUnaryConversions(E); | |||
780 | if (Res.isInvalid()) | |||
781 | return ExprError(); | |||
782 | E = Res.get(); | |||
783 | ||||
784 | // If this is a 'float' or '__fp16' (CVR qualified or typedef) | |||
785 | // promote to double. | |||
786 | // Note that default argument promotion applies only to float (and | |||
787 | // half/fp16); it does not apply to _Float16. | |||
788 | const BuiltinType *BTy = Ty->getAs<BuiltinType>(); | |||
789 | if (BTy && (BTy->getKind() == BuiltinType::Half || | |||
790 | BTy->getKind() == BuiltinType::Float)) { | |||
791 | if (getLangOpts().OpenCL && | |||
792 | !getOpenCLOptions().isEnabled("cl_khr_fp64")) { | |||
793 | if (BTy->getKind() == BuiltinType::Half) { | |||
794 | E = ImpCastExprToType(E, Context.FloatTy, CK_FloatingCast).get(); | |||
795 | } | |||
796 | } else { | |||
797 | E = ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast).get(); | |||
798 | } | |||
799 | } | |||
800 | ||||
801 | // C++ performs lvalue-to-rvalue conversion as a default argument | |||
802 | // promotion, even on class types, but note: | |||
803 | // C++11 [conv.lval]p2: | |||
804 | // When an lvalue-to-rvalue conversion occurs in an unevaluated | |||
805 | // operand or a subexpression thereof the value contained in the | |||
806 | // referenced object is not accessed. Otherwise, if the glvalue | |||
807 | // has a class type, the conversion copy-initializes a temporary | |||
808 | // of type T from the glvalue and the result of the conversion | |||
809 | // is a prvalue for the temporary. | |||
810 | // FIXME: add some way to gate this entire thing for correctness in | |||
811 | // potentially potentially evaluated contexts. | |||
812 | if (getLangOpts().CPlusPlus && E->isGLValue() && !isUnevaluatedContext()) { | |||
813 | ExprResult Temp = PerformCopyInitialization( | |||
814 | InitializedEntity::InitializeTemporary(E->getType()), | |||
815 | E->getExprLoc(), E); | |||
816 | if (Temp.isInvalid()) | |||
817 | return ExprError(); | |||
818 | E = Temp.get(); | |||
819 | } | |||
820 | ||||
821 | return E; | |||
822 | } | |||
823 | ||||
824 | /// Determine the degree of POD-ness for an expression. | |||
825 | /// Incomplete types are considered POD, since this check can be performed | |||
826 | /// when we're in an unevaluated context. | |||
827 | Sema::VarArgKind Sema::isValidVarArgType(const QualType &Ty) { | |||
828 | if (Ty->isIncompleteType()) { | |||
829 | // C++11 [expr.call]p7: | |||
830 | // After these conversions, if the argument does not have arithmetic, | |||
831 | // enumeration, pointer, pointer to member, or class type, the program | |||
832 | // is ill-formed. | |||
833 | // | |||
834 | // Since we've already performed array-to-pointer and function-to-pointer | |||
835 | // decay, the only such type in C++ is cv void. This also handles | |||
836 | // initializer lists as variadic arguments. | |||
837 | if (Ty->isVoidType()) | |||
838 | return VAK_Invalid; | |||
839 | ||||
840 | if (Ty->isObjCObjectType()) | |||
841 | return VAK_Invalid; | |||
842 | return VAK_Valid; | |||
843 | } | |||
844 | ||||
845 | if (Ty.isDestructedType() == QualType::DK_nontrivial_c_struct) | |||
846 | return VAK_Invalid; | |||
847 | ||||
848 | if (Ty.isCXX98PODType(Context)) | |||
849 | return VAK_Valid; | |||
850 | ||||
851 | // C++11 [expr.call]p7: | |||
852 | // Passing a potentially-evaluated argument of class type (Clause 9) | |||
853 | // having a non-trivial copy constructor, a non-trivial move constructor, | |||
854 | // or a non-trivial destructor, with no corresponding parameter, | |||
855 | // is conditionally-supported with implementation-defined semantics. | |||
856 | if (getLangOpts().CPlusPlus11 && !Ty->isDependentType()) | |||
857 | if (CXXRecordDecl *Record = Ty->getAsCXXRecordDecl()) | |||
858 | if (!Record->hasNonTrivialCopyConstructor() && | |||
859 | !Record->hasNonTrivialMoveConstructor() && | |||
860 | !Record->hasNonTrivialDestructor()) | |||
861 | return VAK_ValidInCXX11; | |||
862 | ||||
863 | if (getLangOpts().ObjCAutoRefCount && Ty->isObjCLifetimeType()) | |||
864 | return VAK_Valid; | |||
865 | ||||
866 | if (Ty->isObjCObjectType()) | |||
867 | return VAK_Invalid; | |||
868 | ||||
869 | if (getLangOpts().MSVCCompat) | |||
870 | return VAK_MSVCUndefined; | |||
871 | ||||
872 | // FIXME: In C++11, these cases are conditionally-supported, meaning we're | |||
873 | // permitted to reject them. We should consider doing so. | |||
874 | return VAK_Undefined; | |||
875 | } | |||
876 | ||||
877 | void Sema::checkVariadicArgument(const Expr *E, VariadicCallType CT) { | |||
878 | // Don't allow one to pass an Objective-C interface to a vararg. | |||
879 | const QualType &Ty = E->getType(); | |||
880 | VarArgKind VAK = isValidVarArgType(Ty); | |||
881 | ||||
882 | // Complain about passing non-POD types through varargs. | |||
883 | switch (VAK) { | |||
884 | case VAK_ValidInCXX11: | |||
885 | DiagRuntimeBehavior( | |||
886 | E->getBeginLoc(), nullptr, | |||
887 | PDiag(diag::warn_cxx98_compat_pass_non_pod_arg_to_vararg) << Ty << CT); | |||
888 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
889 | case VAK_Valid: | |||
890 | if (Ty->isRecordType()) { | |||
891 | // This is unlikely to be what the user intended. If the class has a | |||
892 | // 'c_str' member function, the user probably meant to call that. | |||
893 | DiagRuntimeBehavior(E->getBeginLoc(), nullptr, | |||
894 | PDiag(diag::warn_pass_class_arg_to_vararg) | |||
895 | << Ty << CT << hasCStrMethod(E) << ".c_str()"); | |||
896 | } | |||
897 | break; | |||
898 | ||||
899 | case VAK_Undefined: | |||
900 | case VAK_MSVCUndefined: | |||
901 | DiagRuntimeBehavior(E->getBeginLoc(), nullptr, | |||
902 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) | |||
903 | << getLangOpts().CPlusPlus11 << Ty << CT); | |||
904 | break; | |||
905 | ||||
906 | case VAK_Invalid: | |||
907 | if (Ty.isDestructedType() == QualType::DK_nontrivial_c_struct) | |||
908 | Diag(E->getBeginLoc(), | |||
909 | diag::err_cannot_pass_non_trivial_c_struct_to_vararg) | |||
910 | << Ty << CT; | |||
911 | else if (Ty->isObjCObjectType()) | |||
912 | DiagRuntimeBehavior(E->getBeginLoc(), nullptr, | |||
913 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) | |||
914 | << Ty << CT); | |||
915 | else | |||
916 | Diag(E->getBeginLoc(), diag::err_cannot_pass_to_vararg) | |||
917 | << isa<InitListExpr>(E) << Ty << CT; | |||
918 | break; | |||
919 | } | |||
920 | } | |||
921 | ||||
922 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but | |||
923 | /// will create a trap if the resulting type is not a POD type. | |||
924 | ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, | |||
925 | FunctionDecl *FDecl) { | |||
926 | if (const BuiltinType *PlaceholderTy = E->getType()->getAsPlaceholderType()) { | |||
927 | // Strip the unbridged-cast placeholder expression off, if applicable. | |||
928 | if (PlaceholderTy->getKind() == BuiltinType::ARCUnbridgedCast && | |||
929 | (CT == VariadicMethod || | |||
930 | (FDecl && FDecl->hasAttr<CFAuditedTransferAttr>()))) { | |||
931 | E = stripARCUnbridgedCast(E); | |||
932 | ||||
933 | // Otherwise, do normal placeholder checking. | |||
934 | } else { | |||
935 | ExprResult ExprRes = CheckPlaceholderExpr(E); | |||
936 | if (ExprRes.isInvalid()) | |||
937 | return ExprError(); | |||
938 | E = ExprRes.get(); | |||
939 | } | |||
940 | } | |||
941 | ||||
942 | ExprResult ExprRes = DefaultArgumentPromotion(E); | |||
943 | if (ExprRes.isInvalid()) | |||
944 | return ExprError(); | |||
945 | E = ExprRes.get(); | |||
946 | ||||
947 | // Diagnostics regarding non-POD argument types are | |||
948 | // emitted along with format string checking in Sema::CheckFunctionCall(). | |||
949 | if (isValidVarArgType(E->getType()) == VAK_Undefined) { | |||
950 | // Turn this into a trap. | |||
951 | CXXScopeSpec SS; | |||
952 | SourceLocation TemplateKWLoc; | |||
953 | UnqualifiedId Name; | |||
954 | Name.setIdentifier(PP.getIdentifierInfo("__builtin_trap"), | |||
955 | E->getBeginLoc()); | |||
956 | ExprResult TrapFn = ActOnIdExpression(TUScope, SS, TemplateKWLoc, Name, | |||
957 | /*HasTrailingLParen=*/true, | |||
958 | /*IsAddressOfOperand=*/false); | |||
959 | if (TrapFn.isInvalid()) | |||
960 | return ExprError(); | |||
961 | ||||
962 | ExprResult Call = BuildCallExpr(TUScope, TrapFn.get(), E->getBeginLoc(), | |||
963 | None, E->getEndLoc()); | |||
964 | if (Call.isInvalid()) | |||
965 | return ExprError(); | |||
966 | ||||
967 | ExprResult Comma = | |||
968 | ActOnBinOp(TUScope, E->getBeginLoc(), tok::comma, Call.get(), E); | |||
969 | if (Comma.isInvalid()) | |||
970 | return ExprError(); | |||
971 | return Comma.get(); | |||
972 | } | |||
973 | ||||
974 | if (!getLangOpts().CPlusPlus && | |||
975 | RequireCompleteType(E->getExprLoc(), E->getType(), | |||
976 | diag::err_call_incomplete_argument)) | |||
977 | return ExprError(); | |||
978 | ||||
979 | return E; | |||
980 | } | |||
981 | ||||
982 | /// Converts an integer to complex float type. Helper function of | |||
983 | /// UsualArithmeticConversions() | |||
984 | /// | |||
985 | /// \return false if the integer expression is an integer type and is | |||
986 | /// successfully converted to the complex type. | |||
987 | static bool handleIntegerToComplexFloatConversion(Sema &S, ExprResult &IntExpr, | |||
988 | ExprResult &ComplexExpr, | |||
989 | QualType IntTy, | |||
990 | QualType ComplexTy, | |||
991 | bool SkipCast) { | |||
992 | if (IntTy->isComplexType() || IntTy->isRealFloatingType()) return true; | |||
993 | if (SkipCast) return false; | |||
994 | if (IntTy->isIntegerType()) { | |||
995 | QualType fpTy = cast<ComplexType>(ComplexTy)->getElementType(); | |||
996 | IntExpr = S.ImpCastExprToType(IntExpr.get(), fpTy, CK_IntegralToFloating); | |||
997 | IntExpr = S.ImpCastExprToType(IntExpr.get(), ComplexTy, | |||
998 | CK_FloatingRealToComplex); | |||
999 | } else { | |||
1000 | assert(IntTy->isComplexIntegerType())((IntTy->isComplexIntegerType()) ? static_cast<void> (0) : __assert_fail ("IntTy->isComplexIntegerType()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1000, __PRETTY_FUNCTION__)); | |||
1001 | IntExpr = S.ImpCastExprToType(IntExpr.get(), ComplexTy, | |||
1002 | CK_IntegralComplexToFloatingComplex); | |||
1003 | } | |||
1004 | return false; | |||
1005 | } | |||
1006 | ||||
1007 | /// Handle arithmetic conversion with complex types. Helper function of | |||
1008 | /// UsualArithmeticConversions() | |||
1009 | static QualType handleComplexFloatConversion(Sema &S, ExprResult &LHS, | |||
1010 | ExprResult &RHS, QualType LHSType, | |||
1011 | QualType RHSType, | |||
1012 | bool IsCompAssign) { | |||
1013 | // if we have an integer operand, the result is the complex type. | |||
1014 | if (!handleIntegerToComplexFloatConversion(S, RHS, LHS, RHSType, LHSType, | |||
1015 | /*skipCast*/false)) | |||
1016 | return LHSType; | |||
1017 | if (!handleIntegerToComplexFloatConversion(S, LHS, RHS, LHSType, RHSType, | |||
1018 | /*skipCast*/IsCompAssign)) | |||
1019 | return RHSType; | |||
1020 | ||||
1021 | // This handles complex/complex, complex/float, or float/complex. | |||
1022 | // When both operands are complex, the shorter operand is converted to the | |||
1023 | // type of the longer, and that is the type of the result. This corresponds | |||
1024 | // to what is done when combining two real floating-point operands. | |||
1025 | // The fun begins when size promotion occur across type domains. | |||
1026 | // From H&S 6.3.4: When one operand is complex and the other is a real | |||
1027 | // floating-point type, the less precise type is converted, within it's | |||
1028 | // real or complex domain, to the precision of the other type. For example, | |||
1029 | // when combining a "long double" with a "double _Complex", the | |||
1030 | // "double _Complex" is promoted to "long double _Complex". | |||
1031 | ||||
1032 | // Compute the rank of the two types, regardless of whether they are complex. | |||
1033 | int Order = S.Context.getFloatingTypeOrder(LHSType, RHSType); | |||
1034 | ||||
1035 | auto *LHSComplexType = dyn_cast<ComplexType>(LHSType); | |||
1036 | auto *RHSComplexType = dyn_cast<ComplexType>(RHSType); | |||
1037 | QualType LHSElementType = | |||
1038 | LHSComplexType ? LHSComplexType->getElementType() : LHSType; | |||
1039 | QualType RHSElementType = | |||
1040 | RHSComplexType ? RHSComplexType->getElementType() : RHSType; | |||
1041 | ||||
1042 | QualType ResultType = S.Context.getComplexType(LHSElementType); | |||
1043 | if (Order < 0) { | |||
1044 | // Promote the precision of the LHS if not an assignment. | |||
1045 | ResultType = S.Context.getComplexType(RHSElementType); | |||
1046 | if (!IsCompAssign) { | |||
1047 | if (LHSComplexType) | |||
1048 | LHS = | |||
1049 | S.ImpCastExprToType(LHS.get(), ResultType, CK_FloatingComplexCast); | |||
1050 | else | |||
1051 | LHS = S.ImpCastExprToType(LHS.get(), RHSElementType, CK_FloatingCast); | |||
1052 | } | |||
1053 | } else if (Order > 0) { | |||
1054 | // Promote the precision of the RHS. | |||
1055 | if (RHSComplexType) | |||
1056 | RHS = S.ImpCastExprToType(RHS.get(), ResultType, CK_FloatingComplexCast); | |||
1057 | else | |||
1058 | RHS = S.ImpCastExprToType(RHS.get(), LHSElementType, CK_FloatingCast); | |||
1059 | } | |||
1060 | return ResultType; | |||
1061 | } | |||
1062 | ||||
1063 | /// Handle arithmetic conversion from integer to float. Helper function | |||
1064 | /// of UsualArithmeticConversions() | |||
1065 | static QualType handleIntToFloatConversion(Sema &S, ExprResult &FloatExpr, | |||
1066 | ExprResult &IntExpr, | |||
1067 | QualType FloatTy, QualType IntTy, | |||
1068 | bool ConvertFloat, bool ConvertInt) { | |||
1069 | if (IntTy->isIntegerType()) { | |||
1070 | if (ConvertInt) | |||
1071 | // Convert intExpr to the lhs floating point type. | |||
1072 | IntExpr = S.ImpCastExprToType(IntExpr.get(), FloatTy, | |||
1073 | CK_IntegralToFloating); | |||
1074 | return FloatTy; | |||
1075 | } | |||
1076 | ||||
1077 | // Convert both sides to the appropriate complex float. | |||
1078 | assert(IntTy->isComplexIntegerType())((IntTy->isComplexIntegerType()) ? static_cast<void> (0) : __assert_fail ("IntTy->isComplexIntegerType()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1078, __PRETTY_FUNCTION__)); | |||
1079 | QualType result = S.Context.getComplexType(FloatTy); | |||
1080 | ||||
1081 | // _Complex int -> _Complex float | |||
1082 | if (ConvertInt) | |||
1083 | IntExpr = S.ImpCastExprToType(IntExpr.get(), result, | |||
1084 | CK_IntegralComplexToFloatingComplex); | |||
1085 | ||||
1086 | // float -> _Complex float | |||
1087 | if (ConvertFloat) | |||
1088 | FloatExpr = S.ImpCastExprToType(FloatExpr.get(), result, | |||
1089 | CK_FloatingRealToComplex); | |||
1090 | ||||
1091 | return result; | |||
1092 | } | |||
1093 | ||||
1094 | /// Handle arithmethic conversion with floating point types. Helper | |||
1095 | /// function of UsualArithmeticConversions() | |||
1096 | static QualType handleFloatConversion(Sema &S, ExprResult &LHS, | |||
1097 | ExprResult &RHS, QualType LHSType, | |||
1098 | QualType RHSType, bool IsCompAssign) { | |||
1099 | bool LHSFloat = LHSType->isRealFloatingType(); | |||
1100 | bool RHSFloat = RHSType->isRealFloatingType(); | |||
1101 | ||||
1102 | // If we have two real floating types, convert the smaller operand | |||
1103 | // to the bigger result. | |||
1104 | if (LHSFloat && RHSFloat) { | |||
1105 | int order = S.Context.getFloatingTypeOrder(LHSType, RHSType); | |||
1106 | if (order > 0) { | |||
1107 | RHS = S.ImpCastExprToType(RHS.get(), LHSType, CK_FloatingCast); | |||
1108 | return LHSType; | |||
1109 | } | |||
1110 | ||||
1111 | assert(order < 0 && "illegal float comparison")((order < 0 && "illegal float comparison") ? static_cast <void> (0) : __assert_fail ("order < 0 && \"illegal float comparison\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1111, __PRETTY_FUNCTION__)); | |||
1112 | if (!IsCompAssign) | |||
1113 | LHS = S.ImpCastExprToType(LHS.get(), RHSType, CK_FloatingCast); | |||
1114 | return RHSType; | |||
1115 | } | |||
1116 | ||||
1117 | if (LHSFloat) { | |||
1118 | // Half FP has to be promoted to float unless it is natively supported | |||
1119 | if (LHSType->isHalfType() && !S.getLangOpts().NativeHalfType) | |||
1120 | LHSType = S.Context.FloatTy; | |||
1121 | ||||
1122 | return handleIntToFloatConversion(S, LHS, RHS, LHSType, RHSType, | |||
1123 | /*ConvertFloat=*/!IsCompAssign, | |||
1124 | /*ConvertInt=*/ true); | |||
1125 | } | |||
1126 | assert(RHSFloat)((RHSFloat) ? static_cast<void> (0) : __assert_fail ("RHSFloat" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1126, __PRETTY_FUNCTION__)); | |||
1127 | return handleIntToFloatConversion(S, RHS, LHS, RHSType, LHSType, | |||
1128 | /*convertInt=*/ true, | |||
1129 | /*convertFloat=*/!IsCompAssign); | |||
1130 | } | |||
1131 | ||||
1132 | /// Diagnose attempts to convert between __float128 and long double if | |||
1133 | /// there is no support for such conversion. Helper function of | |||
1134 | /// UsualArithmeticConversions(). | |||
1135 | static bool unsupportedTypeConversion(const Sema &S, QualType LHSType, | |||
1136 | QualType RHSType) { | |||
1137 | /* No issue converting if at least one of the types is not a floating point | |||
1138 | type or the two types have the same rank. | |||
1139 | */ | |||
1140 | if (!LHSType->isFloatingType() || !RHSType->isFloatingType() || | |||
1141 | S.Context.getFloatingTypeOrder(LHSType, RHSType) == 0) | |||
1142 | return false; | |||
1143 | ||||
1144 | assert(LHSType->isFloatingType() && RHSType->isFloatingType() &&((LHSType->isFloatingType() && RHSType->isFloatingType () && "The remaining types must be floating point types." ) ? static_cast<void> (0) : __assert_fail ("LHSType->isFloatingType() && RHSType->isFloatingType() && \"The remaining types must be floating point types.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1145, __PRETTY_FUNCTION__)) | |||
1145 | "The remaining types must be floating point types.")((LHSType->isFloatingType() && RHSType->isFloatingType () && "The remaining types must be floating point types." ) ? static_cast<void> (0) : __assert_fail ("LHSType->isFloatingType() && RHSType->isFloatingType() && \"The remaining types must be floating point types.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1145, __PRETTY_FUNCTION__)); | |||
1146 | ||||
1147 | auto *LHSComplex = LHSType->getAs<ComplexType>(); | |||
1148 | auto *RHSComplex = RHSType->getAs<ComplexType>(); | |||
1149 | ||||
1150 | QualType LHSElemType = LHSComplex ? | |||
1151 | LHSComplex->getElementType() : LHSType; | |||
1152 | QualType RHSElemType = RHSComplex ? | |||
1153 | RHSComplex->getElementType() : RHSType; | |||
1154 | ||||
1155 | // No issue if the two types have the same representation | |||
1156 | if (&S.Context.getFloatTypeSemantics(LHSElemType) == | |||
1157 | &S.Context.getFloatTypeSemantics(RHSElemType)) | |||
1158 | return false; | |||
1159 | ||||
1160 | bool Float128AndLongDouble = (LHSElemType == S.Context.Float128Ty && | |||
1161 | RHSElemType == S.Context.LongDoubleTy); | |||
1162 | Float128AndLongDouble |= (LHSElemType == S.Context.LongDoubleTy && | |||
1163 | RHSElemType == S.Context.Float128Ty); | |||
1164 | ||||
1165 | // We've handled the situation where __float128 and long double have the same | |||
1166 | // representation. We allow all conversions for all possible long double types | |||
1167 | // except PPC's double double. | |||
1168 | return Float128AndLongDouble && | |||
1169 | (&S.Context.getFloatTypeSemantics(S.Context.LongDoubleTy) == | |||
1170 | &llvm::APFloat::PPCDoubleDouble()); | |||
1171 | } | |||
1172 | ||||
1173 | typedef ExprResult PerformCastFn(Sema &S, Expr *operand, QualType toType); | |||
1174 | ||||
1175 | namespace { | |||
1176 | /// These helper callbacks are placed in an anonymous namespace to | |||
1177 | /// permit their use as function template parameters. | |||
1178 | ExprResult doIntegralCast(Sema &S, Expr *op, QualType toType) { | |||
1179 | return S.ImpCastExprToType(op, toType, CK_IntegralCast); | |||
1180 | } | |||
1181 | ||||
1182 | ExprResult doComplexIntegralCast(Sema &S, Expr *op, QualType toType) { | |||
1183 | return S.ImpCastExprToType(op, S.Context.getComplexType(toType), | |||
1184 | CK_IntegralComplexCast); | |||
1185 | } | |||
1186 | } | |||
1187 | ||||
1188 | /// Handle integer arithmetic conversions. Helper function of | |||
1189 | /// UsualArithmeticConversions() | |||
1190 | template <PerformCastFn doLHSCast, PerformCastFn doRHSCast> | |||
1191 | static QualType handleIntegerConversion(Sema &S, ExprResult &LHS, | |||
1192 | ExprResult &RHS, QualType LHSType, | |||
1193 | QualType RHSType, bool IsCompAssign) { | |||
1194 | // The rules for this case are in C99 6.3.1.8 | |||
1195 | int order = S.Context.getIntegerTypeOrder(LHSType, RHSType); | |||
1196 | bool LHSSigned = LHSType->hasSignedIntegerRepresentation(); | |||
1197 | bool RHSSigned = RHSType->hasSignedIntegerRepresentation(); | |||
1198 | if (LHSSigned == RHSSigned) { | |||
1199 | // Same signedness; use the higher-ranked type | |||
1200 | if (order >= 0) { | |||
1201 | RHS = (*doRHSCast)(S, RHS.get(), LHSType); | |||
1202 | return LHSType; | |||
1203 | } else if (!IsCompAssign) | |||
1204 | LHS = (*doLHSCast)(S, LHS.get(), RHSType); | |||
1205 | return RHSType; | |||
1206 | } else if (order != (LHSSigned ? 1 : -1)) { | |||
1207 | // The unsigned type has greater than or equal rank to the | |||
1208 | // signed type, so use the unsigned type | |||
1209 | if (RHSSigned) { | |||
1210 | RHS = (*doRHSCast)(S, RHS.get(), LHSType); | |||
1211 | return LHSType; | |||
1212 | } else if (!IsCompAssign) | |||
1213 | LHS = (*doLHSCast)(S, LHS.get(), RHSType); | |||
1214 | return RHSType; | |||
1215 | } else if (S.Context.getIntWidth(LHSType) != S.Context.getIntWidth(RHSType)) { | |||
1216 | // The two types are different widths; if we are here, that | |||
1217 | // means the signed type is larger than the unsigned type, so | |||
1218 | // use the signed type. | |||
1219 | if (LHSSigned) { | |||
1220 | RHS = (*doRHSCast)(S, RHS.get(), LHSType); | |||
1221 | return LHSType; | |||
1222 | } else if (!IsCompAssign) | |||
1223 | LHS = (*doLHSCast)(S, LHS.get(), RHSType); | |||
1224 | return RHSType; | |||
1225 | } else { | |||
1226 | // The signed type is higher-ranked than the unsigned type, | |||
1227 | // but isn't actually any bigger (like unsigned int and long | |||
1228 | // on most 32-bit systems). Use the unsigned type corresponding | |||
1229 | // to the signed type. | |||
1230 | QualType result = | |||
1231 | S.Context.getCorrespondingUnsignedType(LHSSigned ? LHSType : RHSType); | |||
1232 | RHS = (*doRHSCast)(S, RHS.get(), result); | |||
1233 | if (!IsCompAssign) | |||
1234 | LHS = (*doLHSCast)(S, LHS.get(), result); | |||
1235 | return result; | |||
1236 | } | |||
1237 | } | |||
1238 | ||||
1239 | /// Handle conversions with GCC complex int extension. Helper function | |||
1240 | /// of UsualArithmeticConversions() | |||
1241 | static QualType handleComplexIntConversion(Sema &S, ExprResult &LHS, | |||
1242 | ExprResult &RHS, QualType LHSType, | |||
1243 | QualType RHSType, | |||
1244 | bool IsCompAssign) { | |||
1245 | const ComplexType *LHSComplexInt = LHSType->getAsComplexIntegerType(); | |||
1246 | const ComplexType *RHSComplexInt = RHSType->getAsComplexIntegerType(); | |||
1247 | ||||
1248 | if (LHSComplexInt && RHSComplexInt) { | |||
1249 | QualType LHSEltType = LHSComplexInt->getElementType(); | |||
1250 | QualType RHSEltType = RHSComplexInt->getElementType(); | |||
1251 | QualType ScalarType = | |||
1252 | handleIntegerConversion<doComplexIntegralCast, doComplexIntegralCast> | |||
1253 | (S, LHS, RHS, LHSEltType, RHSEltType, IsCompAssign); | |||
1254 | ||||
1255 | return S.Context.getComplexType(ScalarType); | |||
1256 | } | |||
1257 | ||||
1258 | if (LHSComplexInt) { | |||
1259 | QualType LHSEltType = LHSComplexInt->getElementType(); | |||
1260 | QualType ScalarType = | |||
1261 | handleIntegerConversion<doComplexIntegralCast, doIntegralCast> | |||
1262 | (S, LHS, RHS, LHSEltType, RHSType, IsCompAssign); | |||
1263 | QualType ComplexType = S.Context.getComplexType(ScalarType); | |||
1264 | RHS = S.ImpCastExprToType(RHS.get(), ComplexType, | |||
1265 | CK_IntegralRealToComplex); | |||
1266 | ||||
1267 | return ComplexType; | |||
1268 | } | |||
1269 | ||||
1270 | assert(RHSComplexInt)((RHSComplexInt) ? static_cast<void> (0) : __assert_fail ("RHSComplexInt", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1270, __PRETTY_FUNCTION__)); | |||
1271 | ||||
1272 | QualType RHSEltType = RHSComplexInt->getElementType(); | |||
1273 | QualType ScalarType = | |||
1274 | handleIntegerConversion<doIntegralCast, doComplexIntegralCast> | |||
1275 | (S, LHS, RHS, LHSType, RHSEltType, IsCompAssign); | |||
1276 | QualType ComplexType = S.Context.getComplexType(ScalarType); | |||
1277 | ||||
1278 | if (!IsCompAssign) | |||
1279 | LHS = S.ImpCastExprToType(LHS.get(), ComplexType, | |||
1280 | CK_IntegralRealToComplex); | |||
1281 | return ComplexType; | |||
1282 | } | |||
1283 | ||||
1284 | /// Return the rank of a given fixed point or integer type. The value itself | |||
1285 | /// doesn't matter, but the values must be increasing with proper increasing | |||
1286 | /// rank as described in N1169 4.1.1. | |||
1287 | static unsigned GetFixedPointRank(QualType Ty) { | |||
1288 | const auto *BTy = Ty->getAs<BuiltinType>(); | |||
1289 | assert(BTy && "Expected a builtin type.")((BTy && "Expected a builtin type.") ? static_cast< void> (0) : __assert_fail ("BTy && \"Expected a builtin type.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1289, __PRETTY_FUNCTION__)); | |||
1290 | ||||
1291 | switch (BTy->getKind()) { | |||
1292 | case BuiltinType::ShortFract: | |||
1293 | case BuiltinType::UShortFract: | |||
1294 | case BuiltinType::SatShortFract: | |||
1295 | case BuiltinType::SatUShortFract: | |||
1296 | return 1; | |||
1297 | case BuiltinType::Fract: | |||
1298 | case BuiltinType::UFract: | |||
1299 | case BuiltinType::SatFract: | |||
1300 | case BuiltinType::SatUFract: | |||
1301 | return 2; | |||
1302 | case BuiltinType::LongFract: | |||
1303 | case BuiltinType::ULongFract: | |||
1304 | case BuiltinType::SatLongFract: | |||
1305 | case BuiltinType::SatULongFract: | |||
1306 | return 3; | |||
1307 | case BuiltinType::ShortAccum: | |||
1308 | case BuiltinType::UShortAccum: | |||
1309 | case BuiltinType::SatShortAccum: | |||
1310 | case BuiltinType::SatUShortAccum: | |||
1311 | return 4; | |||
1312 | case BuiltinType::Accum: | |||
1313 | case BuiltinType::UAccum: | |||
1314 | case BuiltinType::SatAccum: | |||
1315 | case BuiltinType::SatUAccum: | |||
1316 | return 5; | |||
1317 | case BuiltinType::LongAccum: | |||
1318 | case BuiltinType::ULongAccum: | |||
1319 | case BuiltinType::SatLongAccum: | |||
1320 | case BuiltinType::SatULongAccum: | |||
1321 | return 6; | |||
1322 | default: | |||
1323 | if (BTy->isInteger()) | |||
1324 | return 0; | |||
1325 | llvm_unreachable("Unexpected fixed point or integer type")::llvm::llvm_unreachable_internal("Unexpected fixed point or integer type" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1325); | |||
1326 | } | |||
1327 | } | |||
1328 | ||||
1329 | /// handleFixedPointConversion - Fixed point operations between fixed | |||
1330 | /// point types and integers or other fixed point types do not fall under | |||
1331 | /// usual arithmetic conversion since these conversions could result in loss | |||
1332 | /// of precsision (N1169 4.1.4). These operations should be calculated with | |||
1333 | /// the full precision of their result type (N1169 4.1.6.2.1). | |||
1334 | static QualType handleFixedPointConversion(Sema &S, QualType LHSTy, | |||
1335 | QualType RHSTy) { | |||
1336 | assert((LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) &&(((LHSTy->isFixedPointType() || RHSTy->isFixedPointType ()) && "Expected at least one of the operands to be a fixed point type" ) ? static_cast<void> (0) : __assert_fail ("(LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) && \"Expected at least one of the operands to be a fixed point type\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1337, __PRETTY_FUNCTION__)) | |||
1337 | "Expected at least one of the operands to be a fixed point type")(((LHSTy->isFixedPointType() || RHSTy->isFixedPointType ()) && "Expected at least one of the operands to be a fixed point type" ) ? static_cast<void> (0) : __assert_fail ("(LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) && \"Expected at least one of the operands to be a fixed point type\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1337, __PRETTY_FUNCTION__)); | |||
1338 | assert((LHSTy->isFixedPointOrIntegerType() ||(((LHSTy->isFixedPointOrIntegerType() || RHSTy->isFixedPointOrIntegerType ()) && "Special fixed point arithmetic operation conversions are only " "applied to ints or other fixed point types") ? static_cast< void> (0) : __assert_fail ("(LHSTy->isFixedPointOrIntegerType() || RHSTy->isFixedPointOrIntegerType()) && \"Special fixed point arithmetic operation conversions are only \" \"applied to ints or other fixed point types\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1341, __PRETTY_FUNCTION__)) | |||
1339 | RHSTy->isFixedPointOrIntegerType()) &&(((LHSTy->isFixedPointOrIntegerType() || RHSTy->isFixedPointOrIntegerType ()) && "Special fixed point arithmetic operation conversions are only " "applied to ints or other fixed point types") ? static_cast< void> (0) : __assert_fail ("(LHSTy->isFixedPointOrIntegerType() || RHSTy->isFixedPointOrIntegerType()) && \"Special fixed point arithmetic operation conversions are only \" \"applied to ints or other fixed point types\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1341, __PRETTY_FUNCTION__)) | |||
1340 | "Special fixed point arithmetic operation conversions are only "(((LHSTy->isFixedPointOrIntegerType() || RHSTy->isFixedPointOrIntegerType ()) && "Special fixed point arithmetic operation conversions are only " "applied to ints or other fixed point types") ? static_cast< void> (0) : __assert_fail ("(LHSTy->isFixedPointOrIntegerType() || RHSTy->isFixedPointOrIntegerType()) && \"Special fixed point arithmetic operation conversions are only \" \"applied to ints or other fixed point types\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1341, __PRETTY_FUNCTION__)) | |||
1341 | "applied to ints or other fixed point types")(((LHSTy->isFixedPointOrIntegerType() || RHSTy->isFixedPointOrIntegerType ()) && "Special fixed point arithmetic operation conversions are only " "applied to ints or other fixed point types") ? static_cast< void> (0) : __assert_fail ("(LHSTy->isFixedPointOrIntegerType() || RHSTy->isFixedPointOrIntegerType()) && \"Special fixed point arithmetic operation conversions are only \" \"applied to ints or other fixed point types\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1341, __PRETTY_FUNCTION__)); | |||
1342 | ||||
1343 | // If one operand has signed fixed-point type and the other operand has | |||
1344 | // unsigned fixed-point type, then the unsigned fixed-point operand is | |||
1345 | // converted to its corresponding signed fixed-point type and the resulting | |||
1346 | // type is the type of the converted operand. | |||
1347 | if (RHSTy->isSignedFixedPointType() && LHSTy->isUnsignedFixedPointType()) | |||
1348 | LHSTy = S.Context.getCorrespondingSignedFixedPointType(LHSTy); | |||
1349 | else if (RHSTy->isUnsignedFixedPointType() && LHSTy->isSignedFixedPointType()) | |||
1350 | RHSTy = S.Context.getCorrespondingSignedFixedPointType(RHSTy); | |||
1351 | ||||
1352 | // The result type is the type with the highest rank, whereby a fixed-point | |||
1353 | // conversion rank is always greater than an integer conversion rank; if the | |||
1354 | // type of either of the operands is a saturating fixedpoint type, the result | |||
1355 | // type shall be the saturating fixed-point type corresponding to the type | |||
1356 | // with the highest rank; the resulting value is converted (taking into | |||
1357 | // account rounding and overflow) to the precision of the resulting type. | |||
1358 | // Same ranks between signed and unsigned types are resolved earlier, so both | |||
1359 | // types are either signed or both unsigned at this point. | |||
1360 | unsigned LHSTyRank = GetFixedPointRank(LHSTy); | |||
1361 | unsigned RHSTyRank = GetFixedPointRank(RHSTy); | |||
1362 | ||||
1363 | QualType ResultTy = LHSTyRank > RHSTyRank ? LHSTy : RHSTy; | |||
1364 | ||||
1365 | if (LHSTy->isSaturatedFixedPointType() || RHSTy->isSaturatedFixedPointType()) | |||
1366 | ResultTy = S.Context.getCorrespondingSaturatedType(ResultTy); | |||
1367 | ||||
1368 | return ResultTy; | |||
1369 | } | |||
1370 | ||||
1371 | /// Check that the usual arithmetic conversions can be performed on this pair of | |||
1372 | /// expressions that might be of enumeration type. | |||
1373 | static void checkEnumArithmeticConversions(Sema &S, Expr *LHS, Expr *RHS, | |||
1374 | SourceLocation Loc, | |||
1375 | Sema::ArithConvKind ACK) { | |||
1376 | // C++2a [expr.arith.conv]p1: | |||
1377 | // If one operand is of enumeration type and the other operand is of a | |||
1378 | // different enumeration type or a floating-point type, this behavior is | |||
1379 | // deprecated ([depr.arith.conv.enum]). | |||
1380 | // | |||
1381 | // Warn on this in all language modes. Produce a deprecation warning in C++20. | |||
1382 | // Eventually we will presumably reject these cases (in C++23 onwards?). | |||
1383 | QualType L = LHS->getType(), R = RHS->getType(); | |||
1384 | bool LEnum = L->isUnscopedEnumerationType(), | |||
1385 | REnum = R->isUnscopedEnumerationType(); | |||
1386 | bool IsCompAssign = ACK == Sema::ACK_CompAssign; | |||
1387 | if ((!IsCompAssign && LEnum && R->isFloatingType()) || | |||
1388 | (REnum && L->isFloatingType())) { | |||
1389 | S.Diag(Loc, S.getLangOpts().CPlusPlus2a | |||
1390 | ? diag::warn_arith_conv_enum_float_cxx2a | |||
1391 | : diag::warn_arith_conv_enum_float) | |||
1392 | << LHS->getSourceRange() << RHS->getSourceRange() | |||
1393 | << (int)ACK << LEnum << L << R; | |||
1394 | } else if (!IsCompAssign && LEnum && REnum && | |||
1395 | !S.Context.hasSameUnqualifiedType(L, R)) { | |||
1396 | unsigned DiagID; | |||
1397 | if (!L->castAs<EnumType>()->getDecl()->hasNameForLinkage() || | |||
1398 | !R->castAs<EnumType>()->getDecl()->hasNameForLinkage()) { | |||
1399 | // If either enumeration type is unnamed, it's less likely that the | |||
1400 | // user cares about this, but this situation is still deprecated in | |||
1401 | // C++2a. Use a different warning group. | |||
1402 | DiagID = S.getLangOpts().CPlusPlus2a | |||
1403 | ? diag::warn_arith_conv_mixed_anon_enum_types_cxx2a | |||
1404 | : diag::warn_arith_conv_mixed_anon_enum_types; | |||
1405 | } else if (ACK == Sema::ACK_Conditional) { | |||
1406 | // Conditional expressions are separated out because they have | |||
1407 | // historically had a different warning flag. | |||
1408 | DiagID = S.getLangOpts().CPlusPlus2a | |||
1409 | ? diag::warn_conditional_mixed_enum_types_cxx2a | |||
1410 | : diag::warn_conditional_mixed_enum_types; | |||
1411 | } else if (ACK == Sema::ACK_Comparison) { | |||
1412 | // Comparison expressions are separated out because they have | |||
1413 | // historically had a different warning flag. | |||
1414 | DiagID = S.getLangOpts().CPlusPlus2a | |||
1415 | ? diag::warn_comparison_mixed_enum_types_cxx2a | |||
1416 | : diag::warn_comparison_mixed_enum_types; | |||
1417 | } else { | |||
1418 | DiagID = S.getLangOpts().CPlusPlus2a | |||
1419 | ? diag::warn_arith_conv_mixed_enum_types_cxx2a | |||
1420 | : diag::warn_arith_conv_mixed_enum_types; | |||
1421 | } | |||
1422 | S.Diag(Loc, DiagID) << LHS->getSourceRange() << RHS->getSourceRange() | |||
1423 | << (int)ACK << L << R; | |||
1424 | } | |||
1425 | } | |||
1426 | ||||
1427 | /// UsualArithmeticConversions - Performs various conversions that are common to | |||
1428 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this | |||
1429 | /// routine returns the first non-arithmetic type found. The client is | |||
1430 | /// responsible for emitting appropriate error diagnostics. | |||
1431 | QualType Sema::UsualArithmeticConversions(ExprResult &LHS, ExprResult &RHS, | |||
1432 | SourceLocation Loc, | |||
1433 | ArithConvKind ACK) { | |||
1434 | checkEnumArithmeticConversions(*this, LHS.get(), RHS.get(), Loc, ACK); | |||
1435 | ||||
1436 | if (ACK != ACK_CompAssign) { | |||
1437 | LHS = UsualUnaryConversions(LHS.get()); | |||
1438 | if (LHS.isInvalid()) | |||
1439 | return QualType(); | |||
1440 | } | |||
1441 | ||||
1442 | RHS = UsualUnaryConversions(RHS.get()); | |||
1443 | if (RHS.isInvalid()) | |||
1444 | return QualType(); | |||
1445 | ||||
1446 | // For conversion purposes, we ignore any qualifiers. | |||
1447 | // For example, "const float" and "float" are equivalent. | |||
1448 | QualType LHSType = | |||
1449 | Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); | |||
1450 | QualType RHSType = | |||
1451 | Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); | |||
1452 | ||||
1453 | // For conversion purposes, we ignore any atomic qualifier on the LHS. | |||
1454 | if (const AtomicType *AtomicLHS = LHSType->getAs<AtomicType>()) | |||
1455 | LHSType = AtomicLHS->getValueType(); | |||
1456 | ||||
1457 | // If both types are identical, no conversion is needed. | |||
1458 | if (LHSType == RHSType) | |||
1459 | return LHSType; | |||
1460 | ||||
1461 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. | |||
1462 | // The caller can deal with this (e.g. pointer + int). | |||
1463 | if (!LHSType->isArithmeticType() || !RHSType->isArithmeticType()) | |||
1464 | return QualType(); | |||
1465 | ||||
1466 | // Apply unary and bitfield promotions to the LHS's type. | |||
1467 | QualType LHSUnpromotedType = LHSType; | |||
1468 | if (LHSType->isPromotableIntegerType()) | |||
1469 | LHSType = Context.getPromotedIntegerType(LHSType); | |||
1470 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(LHS.get()); | |||
1471 | if (!LHSBitfieldPromoteTy.isNull()) | |||
1472 | LHSType = LHSBitfieldPromoteTy; | |||
1473 | if (LHSType != LHSUnpromotedType && ACK != ACK_CompAssign) | |||
1474 | LHS = ImpCastExprToType(LHS.get(), LHSType, CK_IntegralCast); | |||
1475 | ||||
1476 | // If both types are identical, no conversion is needed. | |||
1477 | if (LHSType == RHSType) | |||
1478 | return LHSType; | |||
1479 | ||||
1480 | // At this point, we have two different arithmetic types. | |||
1481 | ||||
1482 | // Diagnose attempts to convert between __float128 and long double where | |||
1483 | // such conversions currently can't be handled. | |||
1484 | if (unsupportedTypeConversion(*this, LHSType, RHSType)) | |||
1485 | return QualType(); | |||
1486 | ||||
1487 | // Handle complex types first (C99 6.3.1.8p1). | |||
1488 | if (LHSType->isComplexType() || RHSType->isComplexType()) | |||
1489 | return handleComplexFloatConversion(*this, LHS, RHS, LHSType, RHSType, | |||
1490 | ACK == ACK_CompAssign); | |||
1491 | ||||
1492 | // Now handle "real" floating types (i.e. float, double, long double). | |||
1493 | if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) | |||
1494 | return handleFloatConversion(*this, LHS, RHS, LHSType, RHSType, | |||
1495 | ACK == ACK_CompAssign); | |||
1496 | ||||
1497 | // Handle GCC complex int extension. | |||
1498 | if (LHSType->isComplexIntegerType() || RHSType->isComplexIntegerType()) | |||
1499 | return handleComplexIntConversion(*this, LHS, RHS, LHSType, RHSType, | |||
1500 | ACK == ACK_CompAssign); | |||
1501 | ||||
1502 | if (LHSType->isFixedPointType() || RHSType->isFixedPointType()) | |||
1503 | return handleFixedPointConversion(*this, LHSType, RHSType); | |||
1504 | ||||
1505 | // Finally, we have two differing integer types. | |||
1506 | return handleIntegerConversion<doIntegralCast, doIntegralCast> | |||
1507 | (*this, LHS, RHS, LHSType, RHSType, ACK == ACK_CompAssign); | |||
1508 | } | |||
1509 | ||||
1510 | //===----------------------------------------------------------------------===// | |||
1511 | // Semantic Analysis for various Expression Types | |||
1512 | //===----------------------------------------------------------------------===// | |||
1513 | ||||
1514 | ||||
1515 | ExprResult | |||
1516 | Sema::ActOnGenericSelectionExpr(SourceLocation KeyLoc, | |||
1517 | SourceLocation DefaultLoc, | |||
1518 | SourceLocation RParenLoc, | |||
1519 | Expr *ControllingExpr, | |||
1520 | ArrayRef<ParsedType> ArgTypes, | |||
1521 | ArrayRef<Expr *> ArgExprs) { | |||
1522 | unsigned NumAssocs = ArgTypes.size(); | |||
1523 | assert(NumAssocs == ArgExprs.size())((NumAssocs == ArgExprs.size()) ? static_cast<void> (0) : __assert_fail ("NumAssocs == ArgExprs.size()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1523, __PRETTY_FUNCTION__)); | |||
1524 | ||||
1525 | TypeSourceInfo **Types = new TypeSourceInfo*[NumAssocs]; | |||
1526 | for (unsigned i = 0; i < NumAssocs; ++i) { | |||
1527 | if (ArgTypes[i]) | |||
1528 | (void) GetTypeFromParser(ArgTypes[i], &Types[i]); | |||
1529 | else | |||
1530 | Types[i] = nullptr; | |||
1531 | } | |||
1532 | ||||
1533 | ExprResult ER = CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc, | |||
1534 | ControllingExpr, | |||
1535 | llvm::makeArrayRef(Types, NumAssocs), | |||
1536 | ArgExprs); | |||
1537 | delete [] Types; | |||
1538 | return ER; | |||
1539 | } | |||
1540 | ||||
1541 | ExprResult | |||
1542 | Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, | |||
1543 | SourceLocation DefaultLoc, | |||
1544 | SourceLocation RParenLoc, | |||
1545 | Expr *ControllingExpr, | |||
1546 | ArrayRef<TypeSourceInfo *> Types, | |||
1547 | ArrayRef<Expr *> Exprs) { | |||
1548 | unsigned NumAssocs = Types.size(); | |||
1549 | assert(NumAssocs == Exprs.size())((NumAssocs == Exprs.size()) ? static_cast<void> (0) : __assert_fail ("NumAssocs == Exprs.size()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1549, __PRETTY_FUNCTION__)); | |||
1550 | ||||
1551 | // Decay and strip qualifiers for the controlling expression type, and handle | |||
1552 | // placeholder type replacement. See committee discussion from WG14 DR423. | |||
1553 | { | |||
1554 | EnterExpressionEvaluationContext Unevaluated( | |||
1555 | *this, Sema::ExpressionEvaluationContext::Unevaluated); | |||
1556 | ExprResult R = DefaultFunctionArrayLvalueConversion(ControllingExpr); | |||
1557 | if (R.isInvalid()) | |||
1558 | return ExprError(); | |||
1559 | ControllingExpr = R.get(); | |||
1560 | } | |||
1561 | ||||
1562 | // The controlling expression is an unevaluated operand, so side effects are | |||
1563 | // likely unintended. | |||
1564 | if (!inTemplateInstantiation() && | |||
1565 | ControllingExpr->HasSideEffects(Context, false)) | |||
1566 | Diag(ControllingExpr->getExprLoc(), | |||
1567 | diag::warn_side_effects_unevaluated_context); | |||
1568 | ||||
1569 | bool TypeErrorFound = false, | |||
1570 | IsResultDependent = ControllingExpr->isTypeDependent(), | |||
1571 | ContainsUnexpandedParameterPack | |||
1572 | = ControllingExpr->containsUnexpandedParameterPack(); | |||
1573 | ||||
1574 | for (unsigned i = 0; i < NumAssocs; ++i) { | |||
1575 | if (Exprs[i]->containsUnexpandedParameterPack()) | |||
1576 | ContainsUnexpandedParameterPack = true; | |||
1577 | ||||
1578 | if (Types[i]) { | |||
1579 | if (Types[i]->getType()->containsUnexpandedParameterPack()) | |||
1580 | ContainsUnexpandedParameterPack = true; | |||
1581 | ||||
1582 | if (Types[i]->getType()->isDependentType()) { | |||
1583 | IsResultDependent = true; | |||
1584 | } else { | |||
1585 | // C11 6.5.1.1p2 "The type name in a generic association shall specify a | |||
1586 | // complete object type other than a variably modified type." | |||
1587 | unsigned D = 0; | |||
1588 | if (Types[i]->getType()->isIncompleteType()) | |||
1589 | D = diag::err_assoc_type_incomplete; | |||
1590 | else if (!Types[i]->getType()->isObjectType()) | |||
1591 | D = diag::err_assoc_type_nonobject; | |||
1592 | else if (Types[i]->getType()->isVariablyModifiedType()) | |||
1593 | D = diag::err_assoc_type_variably_modified; | |||
1594 | ||||
1595 | if (D != 0) { | |||
1596 | Diag(Types[i]->getTypeLoc().getBeginLoc(), D) | |||
1597 | << Types[i]->getTypeLoc().getSourceRange() | |||
1598 | << Types[i]->getType(); | |||
1599 | TypeErrorFound = true; | |||
1600 | } | |||
1601 | ||||
1602 | // C11 6.5.1.1p2 "No two generic associations in the same generic | |||
1603 | // selection shall specify compatible types." | |||
1604 | for (unsigned j = i+1; j < NumAssocs; ++j) | |||
1605 | if (Types[j] && !Types[j]->getType()->isDependentType() && | |||
1606 | Context.typesAreCompatible(Types[i]->getType(), | |||
1607 | Types[j]->getType())) { | |||
1608 | Diag(Types[j]->getTypeLoc().getBeginLoc(), | |||
1609 | diag::err_assoc_compatible_types) | |||
1610 | << Types[j]->getTypeLoc().getSourceRange() | |||
1611 | << Types[j]->getType() | |||
1612 | << Types[i]->getType(); | |||
1613 | Diag(Types[i]->getTypeLoc().getBeginLoc(), | |||
1614 | diag::note_compat_assoc) | |||
1615 | << Types[i]->getTypeLoc().getSourceRange() | |||
1616 | << Types[i]->getType(); | |||
1617 | TypeErrorFound = true; | |||
1618 | } | |||
1619 | } | |||
1620 | } | |||
1621 | } | |||
1622 | if (TypeErrorFound) | |||
1623 | return ExprError(); | |||
1624 | ||||
1625 | // If we determined that the generic selection is result-dependent, don't | |||
1626 | // try to compute the result expression. | |||
1627 | if (IsResultDependent) | |||
1628 | return GenericSelectionExpr::Create(Context, KeyLoc, ControllingExpr, Types, | |||
1629 | Exprs, DefaultLoc, RParenLoc, | |||
1630 | ContainsUnexpandedParameterPack); | |||
1631 | ||||
1632 | SmallVector<unsigned, 1> CompatIndices; | |||
1633 | unsigned DefaultIndex = -1U; | |||
1634 | for (unsigned i = 0; i < NumAssocs; ++i) { | |||
1635 | if (!Types[i]) | |||
1636 | DefaultIndex = i; | |||
1637 | else if (Context.typesAreCompatible(ControllingExpr->getType(), | |||
1638 | Types[i]->getType())) | |||
1639 | CompatIndices.push_back(i); | |||
1640 | } | |||
1641 | ||||
1642 | // C11 6.5.1.1p2 "The controlling expression of a generic selection shall have | |||
1643 | // type compatible with at most one of the types named in its generic | |||
1644 | // association list." | |||
1645 | if (CompatIndices.size() > 1) { | |||
1646 | // We strip parens here because the controlling expression is typically | |||
1647 | // parenthesized in macro definitions. | |||
1648 | ControllingExpr = ControllingExpr->IgnoreParens(); | |||
1649 | Diag(ControllingExpr->getBeginLoc(), diag::err_generic_sel_multi_match) | |||
1650 | << ControllingExpr->getSourceRange() << ControllingExpr->getType() | |||
1651 | << (unsigned)CompatIndices.size(); | |||
1652 | for (unsigned I : CompatIndices) { | |||
1653 | Diag(Types[I]->getTypeLoc().getBeginLoc(), | |||
1654 | diag::note_compat_assoc) | |||
1655 | << Types[I]->getTypeLoc().getSourceRange() | |||
1656 | << Types[I]->getType(); | |||
1657 | } | |||
1658 | return ExprError(); | |||
1659 | } | |||
1660 | ||||
1661 | // C11 6.5.1.1p2 "If a generic selection has no default generic association, | |||
1662 | // its controlling expression shall have type compatible with exactly one of | |||
1663 | // the types named in its generic association list." | |||
1664 | if (DefaultIndex == -1U && CompatIndices.size() == 0) { | |||
1665 | // We strip parens here because the controlling expression is typically | |||
1666 | // parenthesized in macro definitions. | |||
1667 | ControllingExpr = ControllingExpr->IgnoreParens(); | |||
1668 | Diag(ControllingExpr->getBeginLoc(), diag::err_generic_sel_no_match) | |||
1669 | << ControllingExpr->getSourceRange() << ControllingExpr->getType(); | |||
1670 | return ExprError(); | |||
1671 | } | |||
1672 | ||||
1673 | // C11 6.5.1.1p3 "If a generic selection has a generic association with a | |||
1674 | // type name that is compatible with the type of the controlling expression, | |||
1675 | // then the result expression of the generic selection is the expression | |||
1676 | // in that generic association. Otherwise, the result expression of the | |||
1677 | // generic selection is the expression in the default generic association." | |||
1678 | unsigned ResultIndex = | |||
1679 | CompatIndices.size() ? CompatIndices[0] : DefaultIndex; | |||
1680 | ||||
1681 | return GenericSelectionExpr::Create( | |||
1682 | Context, KeyLoc, ControllingExpr, Types, Exprs, DefaultLoc, RParenLoc, | |||
1683 | ContainsUnexpandedParameterPack, ResultIndex); | |||
1684 | } | |||
1685 | ||||
1686 | /// getUDSuffixLoc - Create a SourceLocation for a ud-suffix, given the | |||
1687 | /// location of the token and the offset of the ud-suffix within it. | |||
1688 | static SourceLocation getUDSuffixLoc(Sema &S, SourceLocation TokLoc, | |||
1689 | unsigned Offset) { | |||
1690 | return Lexer::AdvanceToTokenCharacter(TokLoc, Offset, S.getSourceManager(), | |||
1691 | S.getLangOpts()); | |||
1692 | } | |||
1693 | ||||
1694 | /// BuildCookedLiteralOperatorCall - A user-defined literal was found. Look up | |||
1695 | /// the corresponding cooked (non-raw) literal operator, and build a call to it. | |||
1696 | static ExprResult BuildCookedLiteralOperatorCall(Sema &S, Scope *Scope, | |||
1697 | IdentifierInfo *UDSuffix, | |||
1698 | SourceLocation UDSuffixLoc, | |||
1699 | ArrayRef<Expr*> Args, | |||
1700 | SourceLocation LitEndLoc) { | |||
1701 | assert(Args.size() <= 2 && "too many arguments for literal operator")((Args.size() <= 2 && "too many arguments for literal operator" ) ? static_cast<void> (0) : __assert_fail ("Args.size() <= 2 && \"too many arguments for literal operator\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1701, __PRETTY_FUNCTION__)); | |||
1702 | ||||
1703 | QualType ArgTy[2]; | |||
1704 | for (unsigned ArgIdx = 0; ArgIdx != Args.size(); ++ArgIdx) { | |||
1705 | ArgTy[ArgIdx] = Args[ArgIdx]->getType(); | |||
1706 | if (ArgTy[ArgIdx]->isArrayType()) | |||
1707 | ArgTy[ArgIdx] = S.Context.getArrayDecayedType(ArgTy[ArgIdx]); | |||
1708 | } | |||
1709 | ||||
1710 | DeclarationName OpName = | |||
1711 | S.Context.DeclarationNames.getCXXLiteralOperatorName(UDSuffix); | |||
1712 | DeclarationNameInfo OpNameInfo(OpName, UDSuffixLoc); | |||
1713 | OpNameInfo.setCXXLiteralOperatorNameLoc(UDSuffixLoc); | |||
1714 | ||||
1715 | LookupResult R(S, OpName, UDSuffixLoc, Sema::LookupOrdinaryName); | |||
1716 | if (S.LookupLiteralOperator(Scope, R, llvm::makeArrayRef(ArgTy, Args.size()), | |||
1717 | /*AllowRaw*/ false, /*AllowTemplate*/ false, | |||
1718 | /*AllowStringTemplate*/ false, | |||
1719 | /*DiagnoseMissing*/ true) == Sema::LOLR_Error) | |||
1720 | return ExprError(); | |||
1721 | ||||
1722 | return S.BuildLiteralOperatorCall(R, OpNameInfo, Args, LitEndLoc); | |||
1723 | } | |||
1724 | ||||
1725 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string | |||
1726 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string | |||
1727 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from | |||
1728 | /// multiple tokens. However, the common case is that StringToks points to one | |||
1729 | /// string. | |||
1730 | /// | |||
1731 | ExprResult | |||
1732 | Sema::ActOnStringLiteral(ArrayRef<Token> StringToks, Scope *UDLScope) { | |||
1733 | assert(!StringToks.empty() && "Must have at least one string!")((!StringToks.empty() && "Must have at least one string!" ) ? static_cast<void> (0) : __assert_fail ("!StringToks.empty() && \"Must have at least one string!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1733, __PRETTY_FUNCTION__)); | |||
1734 | ||||
1735 | StringLiteralParser Literal(StringToks, PP); | |||
1736 | if (Literal.hadError) | |||
1737 | return ExprError(); | |||
1738 | ||||
1739 | SmallVector<SourceLocation, 4> StringTokLocs; | |||
1740 | for (const Token &Tok : StringToks) | |||
1741 | StringTokLocs.push_back(Tok.getLocation()); | |||
1742 | ||||
1743 | QualType CharTy = Context.CharTy; | |||
1744 | StringLiteral::StringKind Kind = StringLiteral::Ascii; | |||
1745 | if (Literal.isWide()) { | |||
1746 | CharTy = Context.getWideCharType(); | |||
1747 | Kind = StringLiteral::Wide; | |||
1748 | } else if (Literal.isUTF8()) { | |||
1749 | if (getLangOpts().Char8) | |||
1750 | CharTy = Context.Char8Ty; | |||
1751 | Kind = StringLiteral::UTF8; | |||
1752 | } else if (Literal.isUTF16()) { | |||
1753 | CharTy = Context.Char16Ty; | |||
1754 | Kind = StringLiteral::UTF16; | |||
1755 | } else if (Literal.isUTF32()) { | |||
1756 | CharTy = Context.Char32Ty; | |||
1757 | Kind = StringLiteral::UTF32; | |||
1758 | } else if (Literal.isPascal()) { | |||
1759 | CharTy = Context.UnsignedCharTy; | |||
1760 | } | |||
1761 | ||||
1762 | // Warn on initializing an array of char from a u8 string literal; this | |||
1763 | // becomes ill-formed in C++2a. | |||
1764 | if (getLangOpts().CPlusPlus && !getLangOpts().CPlusPlus2a && | |||
1765 | !getLangOpts().Char8 && Kind == StringLiteral::UTF8) { | |||
1766 | Diag(StringTokLocs.front(), diag::warn_cxx2a_compat_utf8_string); | |||
1767 | ||||
1768 | // Create removals for all 'u8' prefixes in the string literal(s). This | |||
1769 | // ensures C++2a compatibility (but may change the program behavior when | |||
1770 | // built by non-Clang compilers for which the execution character set is | |||
1771 | // not always UTF-8). | |||
1772 | auto RemovalDiag = PDiag(diag::note_cxx2a_compat_utf8_string_remove_u8); | |||
1773 | SourceLocation RemovalDiagLoc; | |||
1774 | for (const Token &Tok : StringToks) { | |||
1775 | if (Tok.getKind() == tok::utf8_string_literal) { | |||
1776 | if (RemovalDiagLoc.isInvalid()) | |||
1777 | RemovalDiagLoc = Tok.getLocation(); | |||
1778 | RemovalDiag << FixItHint::CreateRemoval(CharSourceRange::getCharRange( | |||
1779 | Tok.getLocation(), | |||
1780 | Lexer::AdvanceToTokenCharacter(Tok.getLocation(), 2, | |||
1781 | getSourceManager(), getLangOpts()))); | |||
1782 | } | |||
1783 | } | |||
1784 | Diag(RemovalDiagLoc, RemovalDiag); | |||
1785 | } | |||
1786 | ||||
1787 | QualType StrTy = | |||
1788 | Context.getStringLiteralArrayType(CharTy, Literal.GetNumStringChars()); | |||
1789 | ||||
1790 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! | |||
1791 | StringLiteral *Lit = StringLiteral::Create(Context, Literal.GetString(), | |||
1792 | Kind, Literal.Pascal, StrTy, | |||
1793 | &StringTokLocs[0], | |||
1794 | StringTokLocs.size()); | |||
1795 | if (Literal.getUDSuffix().empty()) | |||
1796 | return Lit; | |||
1797 | ||||
1798 | // We're building a user-defined literal. | |||
1799 | IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); | |||
1800 | SourceLocation UDSuffixLoc = | |||
1801 | getUDSuffixLoc(*this, StringTokLocs[Literal.getUDSuffixToken()], | |||
1802 | Literal.getUDSuffixOffset()); | |||
1803 | ||||
1804 | // Make sure we're allowed user-defined literals here. | |||
1805 | if (!UDLScope) | |||
1806 | return ExprError(Diag(UDSuffixLoc, diag::err_invalid_string_udl)); | |||
1807 | ||||
1808 | // C++11 [lex.ext]p5: The literal L is treated as a call of the form | |||
1809 | // operator "" X (str, len) | |||
1810 | QualType SizeType = Context.getSizeType(); | |||
1811 | ||||
1812 | DeclarationName OpName = | |||
1813 | Context.DeclarationNames.getCXXLiteralOperatorName(UDSuffix); | |||
1814 | DeclarationNameInfo OpNameInfo(OpName, UDSuffixLoc); | |||
1815 | OpNameInfo.setCXXLiteralOperatorNameLoc(UDSuffixLoc); | |||
1816 | ||||
1817 | QualType ArgTy[] = { | |||
1818 | Context.getArrayDecayedType(StrTy), SizeType | |||
1819 | }; | |||
1820 | ||||
1821 | LookupResult R(*this, OpName, UDSuffixLoc, LookupOrdinaryName); | |||
1822 | switch (LookupLiteralOperator(UDLScope, R, ArgTy, | |||
1823 | /*AllowRaw*/ false, /*AllowTemplate*/ false, | |||
1824 | /*AllowStringTemplate*/ true, | |||
1825 | /*DiagnoseMissing*/ true)) { | |||
1826 | ||||
1827 | case LOLR_Cooked: { | |||
1828 | llvm::APInt Len(Context.getIntWidth(SizeType), Literal.GetNumStringChars()); | |||
1829 | IntegerLiteral *LenArg = IntegerLiteral::Create(Context, Len, SizeType, | |||
1830 | StringTokLocs[0]); | |||
1831 | Expr *Args[] = { Lit, LenArg }; | |||
1832 | ||||
1833 | return BuildLiteralOperatorCall(R, OpNameInfo, Args, StringTokLocs.back()); | |||
1834 | } | |||
1835 | ||||
1836 | case LOLR_StringTemplate: { | |||
1837 | TemplateArgumentListInfo ExplicitArgs; | |||
1838 | ||||
1839 | unsigned CharBits = Context.getIntWidth(CharTy); | |||
1840 | bool CharIsUnsigned = CharTy->isUnsignedIntegerType(); | |||
1841 | llvm::APSInt Value(CharBits, CharIsUnsigned); | |||
1842 | ||||
1843 | TemplateArgument TypeArg(CharTy); | |||
1844 | TemplateArgumentLocInfo TypeArgInfo(Context.getTrivialTypeSourceInfo(CharTy)); | |||
1845 | ExplicitArgs.addArgument(TemplateArgumentLoc(TypeArg, TypeArgInfo)); | |||
1846 | ||||
1847 | for (unsigned I = 0, N = Lit->getLength(); I != N; ++I) { | |||
1848 | Value = Lit->getCodeUnit(I); | |||
1849 | TemplateArgument Arg(Context, Value, CharTy); | |||
1850 | TemplateArgumentLocInfo ArgInfo; | |||
1851 | ExplicitArgs.addArgument(TemplateArgumentLoc(Arg, ArgInfo)); | |||
1852 | } | |||
1853 | return BuildLiteralOperatorCall(R, OpNameInfo, None, StringTokLocs.back(), | |||
1854 | &ExplicitArgs); | |||
1855 | } | |||
1856 | case LOLR_Raw: | |||
1857 | case LOLR_Template: | |||
1858 | case LOLR_ErrorNoDiagnostic: | |||
1859 | llvm_unreachable("unexpected literal operator lookup result")::llvm::llvm_unreachable_internal("unexpected literal operator lookup result" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1859); | |||
1860 | case LOLR_Error: | |||
1861 | return ExprError(); | |||
1862 | } | |||
1863 | llvm_unreachable("unexpected literal operator lookup result")::llvm::llvm_unreachable_internal("unexpected literal operator lookup result" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 1863); | |||
1864 | } | |||
1865 | ||||
1866 | DeclRefExpr * | |||
1867 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, | |||
1868 | SourceLocation Loc, | |||
1869 | const CXXScopeSpec *SS) { | |||
1870 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); | |||
1871 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); | |||
1872 | } | |||
1873 | ||||
1874 | DeclRefExpr * | |||
1875 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, | |||
1876 | const DeclarationNameInfo &NameInfo, | |||
1877 | const CXXScopeSpec *SS, NamedDecl *FoundD, | |||
1878 | SourceLocation TemplateKWLoc, | |||
1879 | const TemplateArgumentListInfo *TemplateArgs) { | |||
1880 | NestedNameSpecifierLoc NNS = | |||
1881 | SS ? SS->getWithLocInContext(Context) : NestedNameSpecifierLoc(); | |||
1882 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, NNS, FoundD, TemplateKWLoc, | |||
1883 | TemplateArgs); | |||
1884 | } | |||
1885 | ||||
1886 | NonOdrUseReason Sema::getNonOdrUseReasonInCurrentContext(ValueDecl *D) { | |||
1887 | // A declaration named in an unevaluated operand never constitutes an odr-use. | |||
1888 | if (isUnevaluatedContext()) | |||
1889 | return NOUR_Unevaluated; | |||
1890 | ||||
1891 | // C++2a [basic.def.odr]p4: | |||
1892 | // A variable x whose name appears as a potentially-evaluated expression e | |||
1893 | // is odr-used by e unless [...] x is a reference that is usable in | |||
1894 | // constant expressions. | |||
1895 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { | |||
1896 | if (VD->getType()->isReferenceType() && | |||
1897 | !(getLangOpts().OpenMP && isOpenMPCapturedDecl(D)) && | |||
1898 | VD->isUsableInConstantExpressions(Context)) | |||
1899 | return NOUR_Constant; | |||
1900 | } | |||
1901 | ||||
1902 | // All remaining non-variable cases constitute an odr-use. For variables, we | |||
1903 | // need to wait and see how the expression is used. | |||
1904 | return NOUR_None; | |||
1905 | } | |||
1906 | ||||
1907 | /// BuildDeclRefExpr - Build an expression that references a | |||
1908 | /// declaration that does not require a closure capture. | |||
1909 | DeclRefExpr * | |||
1910 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, | |||
1911 | const DeclarationNameInfo &NameInfo, | |||
1912 | NestedNameSpecifierLoc NNS, NamedDecl *FoundD, | |||
1913 | SourceLocation TemplateKWLoc, | |||
1914 | const TemplateArgumentListInfo *TemplateArgs) { | |||
1915 | bool RefersToCapturedVariable = | |||
1916 | isa<VarDecl>(D) && | |||
1917 | NeedToCaptureVariable(cast<VarDecl>(D), NameInfo.getLoc()); | |||
1918 | ||||
1919 | DeclRefExpr *E = DeclRefExpr::Create( | |||
1920 | Context, NNS, TemplateKWLoc, D, RefersToCapturedVariable, NameInfo, Ty, | |||
1921 | VK, FoundD, TemplateArgs, getNonOdrUseReasonInCurrentContext(D)); | |||
1922 | MarkDeclRefReferenced(E); | |||
1923 | ||||
1924 | // C++ [except.spec]p17: | |||
1925 | // An exception-specification is considered to be needed when: | |||
1926 | // - in an expression, the function is the unique lookup result or | |||
1927 | // the selected member of a set of overloaded functions. | |||
1928 | // | |||
1929 | // We delay doing this until after we've built the function reference and | |||
1930 | // marked it as used so that: | |||
1931 | // a) if the function is defaulted, we get errors from defining it before / | |||
1932 | // instead of errors from computing its exception specification, and | |||
1933 | // b) if the function is a defaulted comparison, we can use the body we | |||
1934 | // build when defining it as input to the exception specification | |||
1935 | // computation rather than computing a new body. | |||
1936 | if (auto *FPT = Ty->getAs<FunctionProtoType>()) { | |||
1937 | if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) { | |||
1938 | if (auto *NewFPT = ResolveExceptionSpec(NameInfo.getLoc(), FPT)) | |||
1939 | E->setType(Context.getQualifiedType(NewFPT, Ty.getQualifiers())); | |||
1940 | } | |||
1941 | } | |||
1942 | ||||
1943 | if (getLangOpts().ObjCWeak && isa<VarDecl>(D) && | |||
1944 | Ty.getObjCLifetime() == Qualifiers::OCL_Weak && !isUnevaluatedContext() && | |||
1945 | !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, E->getBeginLoc())) | |||
1946 | getCurFunction()->recordUseOfWeak(E); | |||
1947 | ||||
1948 | FieldDecl *FD = dyn_cast<FieldDecl>(D); | |||
1949 | if (IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(D)) | |||
1950 | FD = IFD->getAnonField(); | |||
1951 | if (FD) { | |||
1952 | UnusedPrivateFields.remove(FD); | |||
1953 | // Just in case we're building an illegal pointer-to-member. | |||
1954 | if (FD->isBitField()) | |||
1955 | E->setObjectKind(OK_BitField); | |||
1956 | } | |||
1957 | ||||
1958 | // C++ [expr.prim]/8: The expression [...] is a bit-field if the identifier | |||
1959 | // designates a bit-field. | |||
1960 | if (auto *BD = dyn_cast<BindingDecl>(D)) | |||
1961 | if (auto *BE = BD->getBinding()) | |||
1962 | E->setObjectKind(BE->getObjectKind()); | |||
1963 | ||||
1964 | return E; | |||
1965 | } | |||
1966 | ||||
1967 | /// Decomposes the given name into a DeclarationNameInfo, its location, and | |||
1968 | /// possibly a list of template arguments. | |||
1969 | /// | |||
1970 | /// If this produces template arguments, it is permitted to call | |||
1971 | /// DecomposeTemplateName. | |||
1972 | /// | |||
1973 | /// This actually loses a lot of source location information for | |||
1974 | /// non-standard name kinds; we should consider preserving that in | |||
1975 | /// some way. | |||
1976 | void | |||
1977 | Sema::DecomposeUnqualifiedId(const UnqualifiedId &Id, | |||
1978 | TemplateArgumentListInfo &Buffer, | |||
1979 | DeclarationNameInfo &NameInfo, | |||
1980 | const TemplateArgumentListInfo *&TemplateArgs) { | |||
1981 | if (Id.getKind() == UnqualifiedIdKind::IK_TemplateId) { | |||
1982 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); | |||
1983 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); | |||
1984 | ||||
1985 | ASTTemplateArgsPtr TemplateArgsPtr(Id.TemplateId->getTemplateArgs(), | |||
1986 | Id.TemplateId->NumArgs); | |||
1987 | translateTemplateArguments(TemplateArgsPtr, Buffer); | |||
1988 | ||||
1989 | TemplateName TName = Id.TemplateId->Template.get(); | |||
1990 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; | |||
1991 | NameInfo = Context.getNameForTemplate(TName, TNameLoc); | |||
1992 | TemplateArgs = &Buffer; | |||
1993 | } else { | |||
1994 | NameInfo = GetNameFromUnqualifiedId(Id); | |||
1995 | TemplateArgs = nullptr; | |||
1996 | } | |||
1997 | } | |||
1998 | ||||
1999 | static void emitEmptyLookupTypoDiagnostic( | |||
2000 | const TypoCorrection &TC, Sema &SemaRef, const CXXScopeSpec &SS, | |||
2001 | DeclarationName Typo, SourceLocation TypoLoc, ArrayRef<Expr *> Args, | |||
2002 | unsigned DiagnosticID, unsigned DiagnosticSuggestID) { | |||
2003 | DeclContext *Ctx = | |||
2004 | SS.isEmpty() ? nullptr : SemaRef.computeDeclContext(SS, false); | |||
2005 | if (!TC) { | |||
2006 | // Emit a special diagnostic for failed member lookups. | |||
2007 | // FIXME: computing the declaration context might fail here (?) | |||
2008 | if (Ctx) | |||
2009 | SemaRef.Diag(TypoLoc, diag::err_no_member) << Typo << Ctx | |||
2010 | << SS.getRange(); | |||
2011 | else | |||
2012 | SemaRef.Diag(TypoLoc, DiagnosticID) << Typo; | |||
2013 | return; | |||
2014 | } | |||
2015 | ||||
2016 | std::string CorrectedStr = TC.getAsString(SemaRef.getLangOpts()); | |||
2017 | bool DroppedSpecifier = | |||
2018 | TC.WillReplaceSpecifier() && Typo.getAsString() == CorrectedStr; | |||
2019 | unsigned NoteID = TC.getCorrectionDeclAs<ImplicitParamDecl>() | |||
2020 | ? diag::note_implicit_param_decl | |||
2021 | : diag::note_previous_decl; | |||
2022 | if (!Ctx) | |||
2023 | SemaRef.diagnoseTypo(TC, SemaRef.PDiag(DiagnosticSuggestID) << Typo, | |||
2024 | SemaRef.PDiag(NoteID)); | |||
2025 | else | |||
2026 | SemaRef.diagnoseTypo(TC, SemaRef.PDiag(diag::err_no_member_suggest) | |||
2027 | << Typo << Ctx << DroppedSpecifier | |||
2028 | << SS.getRange(), | |||
2029 | SemaRef.PDiag(NoteID)); | |||
2030 | } | |||
2031 | ||||
2032 | /// Diagnose an empty lookup. | |||
2033 | /// | |||
2034 | /// \return false if new lookup candidates were found | |||
2035 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, | |||
2036 | CorrectionCandidateCallback &CCC, | |||
2037 | TemplateArgumentListInfo *ExplicitTemplateArgs, | |||
2038 | ArrayRef<Expr *> Args, TypoExpr **Out) { | |||
2039 | DeclarationName Name = R.getLookupName(); | |||
2040 | ||||
2041 | unsigned diagnostic = diag::err_undeclared_var_use; | |||
2042 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; | |||
2043 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || | |||
2044 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || | |||
2045 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { | |||
2046 | diagnostic = diag::err_undeclared_use; | |||
2047 | diagnostic_suggest = diag::err_undeclared_use_suggest; | |||
2048 | } | |||
2049 | ||||
2050 | // If the original lookup was an unqualified lookup, fake an | |||
2051 | // unqualified lookup. This is useful when (for example) the | |||
2052 | // original lookup would not have found something because it was a | |||
2053 | // dependent name. | |||
2054 | DeclContext *DC = SS.isEmpty() ? CurContext : nullptr; | |||
2055 | while (DC) { | |||
2056 | if (isa<CXXRecordDecl>(DC)) { | |||
2057 | LookupQualifiedName(R, DC); | |||
2058 | ||||
2059 | if (!R.empty()) { | |||
2060 | // Don't give errors about ambiguities in this lookup. | |||
2061 | R.suppressDiagnostics(); | |||
2062 | ||||
2063 | // During a default argument instantiation the CurContext points | |||
2064 | // to a CXXMethodDecl; but we can't apply a this-> fixit inside a | |||
2065 | // function parameter list, hence add an explicit check. | |||
2066 | bool isDefaultArgument = | |||
2067 | !CodeSynthesisContexts.empty() && | |||
2068 | CodeSynthesisContexts.back().Kind == | |||
2069 | CodeSynthesisContext::DefaultFunctionArgumentInstantiation; | |||
2070 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); | |||
2071 | bool isInstance = CurMethod && | |||
2072 | CurMethod->isInstance() && | |||
2073 | DC == CurMethod->getParent() && !isDefaultArgument; | |||
2074 | ||||
2075 | // Give a code modification hint to insert 'this->'. | |||
2076 | // TODO: fixit for inserting 'Base<T>::' in the other cases. | |||
2077 | // Actually quite difficult! | |||
2078 | if (getLangOpts().MSVCCompat) | |||
2079 | diagnostic = diag::ext_found_via_dependent_bases_lookup; | |||
2080 | if (isInstance) { | |||
2081 | Diag(R.getNameLoc(), diagnostic) << Name | |||
2082 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); | |||
2083 | CheckCXXThisCapture(R.getNameLoc()); | |||
2084 | } else { | |||
2085 | Diag(R.getNameLoc(), diagnostic) << Name; | |||
2086 | } | |||
2087 | ||||
2088 | // Do we really want to note all of these? | |||
2089 | for (NamedDecl *D : R) | |||
2090 | Diag(D->getLocation(), diag::note_dependent_var_use); | |||
2091 | ||||
2092 | // Return true if we are inside a default argument instantiation | |||
2093 | // and the found name refers to an instance member function, otherwise | |||
2094 | // the function calling DiagnoseEmptyLookup will try to create an | |||
2095 | // implicit member call and this is wrong for default argument. | |||
2096 | if (isDefaultArgument && ((*R.begin())->isCXXInstanceMember())) { | |||
2097 | Diag(R.getNameLoc(), diag::err_member_call_without_object); | |||
2098 | return true; | |||
2099 | } | |||
2100 | ||||
2101 | // Tell the callee to try to recover. | |||
2102 | return false; | |||
2103 | } | |||
2104 | ||||
2105 | R.clear(); | |||
2106 | } | |||
2107 | ||||
2108 | DC = DC->getLookupParent(); | |||
2109 | } | |||
2110 | ||||
2111 | // We didn't find anything, so try to correct for a typo. | |||
2112 | TypoCorrection Corrected; | |||
2113 | if (S && Out) { | |||
2114 | SourceLocation TypoLoc = R.getNameLoc(); | |||
2115 | assert(!ExplicitTemplateArgs &&((!ExplicitTemplateArgs && "Diagnosing an empty lookup with explicit template args!" ) ? static_cast<void> (0) : __assert_fail ("!ExplicitTemplateArgs && \"Diagnosing an empty lookup with explicit template args!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 2116, __PRETTY_FUNCTION__)) | |||
2116 | "Diagnosing an empty lookup with explicit template args!")((!ExplicitTemplateArgs && "Diagnosing an empty lookup with explicit template args!" ) ? static_cast<void> (0) : __assert_fail ("!ExplicitTemplateArgs && \"Diagnosing an empty lookup with explicit template args!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 2116, __PRETTY_FUNCTION__)); | |||
2117 | *Out = CorrectTypoDelayed( | |||
2118 | R.getLookupNameInfo(), R.getLookupKind(), S, &SS, CCC, | |||
2119 | [=](const TypoCorrection &TC) { | |||
2120 | emitEmptyLookupTypoDiagnostic(TC, *this, SS, Name, TypoLoc, Args, | |||
2121 | diagnostic, diagnostic_suggest); | |||
2122 | }, | |||
2123 | nullptr, CTK_ErrorRecovery); | |||
2124 | if (*Out) | |||
2125 | return true; | |||
2126 | } else if (S && | |||
2127 | (Corrected = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), | |||
2128 | S, &SS, CCC, CTK_ErrorRecovery))) { | |||
2129 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); | |||
2130 | bool DroppedSpecifier = | |||
2131 | Corrected.WillReplaceSpecifier() && Name.getAsString() == CorrectedStr; | |||
2132 | R.setLookupName(Corrected.getCorrection()); | |||
2133 | ||||
2134 | bool AcceptableWithRecovery = false; | |||
2135 | bool AcceptableWithoutRecovery = false; | |||
2136 | NamedDecl *ND = Corrected.getFoundDecl(); | |||
2137 | if (ND) { | |||
2138 | if (Corrected.isOverloaded()) { | |||
2139 | OverloadCandidateSet OCS(R.getNameLoc(), | |||
2140 | OverloadCandidateSet::CSK_Normal); | |||
2141 | OverloadCandidateSet::iterator Best; | |||
2142 | for (NamedDecl *CD : Corrected) { | |||
2143 | if (FunctionTemplateDecl *FTD = | |||
2144 | dyn_cast<FunctionTemplateDecl>(CD)) | |||
2145 | AddTemplateOverloadCandidate( | |||
2146 | FTD, DeclAccessPair::make(FTD, AS_none), ExplicitTemplateArgs, | |||
2147 | Args, OCS); | |||
2148 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(CD)) | |||
2149 | if (!ExplicitTemplateArgs || ExplicitTemplateArgs->size() == 0) | |||
2150 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, AS_none), | |||
2151 | Args, OCS); | |||
2152 | } | |||
2153 | switch (OCS.BestViableFunction(*this, R.getNameLoc(), Best)) { | |||
2154 | case OR_Success: | |||
2155 | ND = Best->FoundDecl; | |||
2156 | Corrected.setCorrectionDecl(ND); | |||
2157 | break; | |||
2158 | default: | |||
2159 | // FIXME: Arbitrarily pick the first declaration for the note. | |||
2160 | Corrected.setCorrectionDecl(ND); | |||
2161 | break; | |||
2162 | } | |||
2163 | } | |||
2164 | R.addDecl(ND); | |||
2165 | if (getLangOpts().CPlusPlus && ND->isCXXClassMember()) { | |||
2166 | CXXRecordDecl *Record = nullptr; | |||
2167 | if (Corrected.getCorrectionSpecifier()) { | |||
2168 | const Type *Ty = Corrected.getCorrectionSpecifier()->getAsType(); | |||
2169 | Record = Ty->getAsCXXRecordDecl(); | |||
2170 | } | |||
2171 | if (!Record) | |||
2172 | Record = cast<CXXRecordDecl>( | |||
2173 | ND->getDeclContext()->getRedeclContext()); | |||
2174 | R.setNamingClass(Record); | |||
2175 | } | |||
2176 | ||||
2177 | auto *UnderlyingND = ND->getUnderlyingDecl(); | |||
2178 | AcceptableWithRecovery = isa<ValueDecl>(UnderlyingND) || | |||
2179 | isa<FunctionTemplateDecl>(UnderlyingND); | |||
2180 | // FIXME: If we ended up with a typo for a type name or | |||
2181 | // Objective-C class name, we're in trouble because the parser | |||
2182 | // is in the wrong place to recover. Suggest the typo | |||
2183 | // correction, but don't make it a fix-it since we're not going | |||
2184 | // to recover well anyway. | |||
2185 | AcceptableWithoutRecovery = isa<TypeDecl>(UnderlyingND) || | |||
2186 | getAsTypeTemplateDecl(UnderlyingND) || | |||
2187 | isa<ObjCInterfaceDecl>(UnderlyingND); | |||
2188 | } else { | |||
2189 | // FIXME: We found a keyword. Suggest it, but don't provide a fix-it | |||
2190 | // because we aren't able to recover. | |||
2191 | AcceptableWithoutRecovery = true; | |||
2192 | } | |||
2193 | ||||
2194 | if (AcceptableWithRecovery || AcceptableWithoutRecovery) { | |||
2195 | unsigned NoteID = Corrected.getCorrectionDeclAs<ImplicitParamDecl>() | |||
2196 | ? diag::note_implicit_param_decl | |||
2197 | : diag::note_previous_decl; | |||
2198 | if (SS.isEmpty()) | |||
2199 | diagnoseTypo(Corrected, PDiag(diagnostic_suggest) << Name, | |||
2200 | PDiag(NoteID), AcceptableWithRecovery); | |||
2201 | else | |||
2202 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_suggest) | |||
2203 | << Name << computeDeclContext(SS, false) | |||
2204 | << DroppedSpecifier << SS.getRange(), | |||
2205 | PDiag(NoteID), AcceptableWithRecovery); | |||
2206 | ||||
2207 | // Tell the callee whether to try to recover. | |||
2208 | return !AcceptableWithRecovery; | |||
2209 | } | |||
2210 | } | |||
2211 | R.clear(); | |||
2212 | ||||
2213 | // Emit a special diagnostic for failed member lookups. | |||
2214 | // FIXME: computing the declaration context might fail here (?) | |||
2215 | if (!SS.isEmpty()) { | |||
2216 | Diag(R.getNameLoc(), diag::err_no_member) | |||
2217 | << Name << computeDeclContext(SS, false) | |||
2218 | << SS.getRange(); | |||
2219 | return true; | |||
2220 | } | |||
2221 | ||||
2222 | // Give up, we can't recover. | |||
2223 | Diag(R.getNameLoc(), diagnostic) << Name; | |||
2224 | return true; | |||
2225 | } | |||
2226 | ||||
2227 | /// In Microsoft mode, if we are inside a template class whose parent class has | |||
2228 | /// dependent base classes, and we can't resolve an unqualified identifier, then | |||
2229 | /// assume the identifier is a member of a dependent base class. We can only | |||
2230 | /// recover successfully in static methods, instance methods, and other contexts | |||
2231 | /// where 'this' is available. This doesn't precisely match MSVC's | |||
2232 | /// instantiation model, but it's close enough. | |||
2233 | static Expr * | |||
2234 | recoverFromMSUnqualifiedLookup(Sema &S, ASTContext &Context, | |||
2235 | DeclarationNameInfo &NameInfo, | |||
2236 | SourceLocation TemplateKWLoc, | |||
2237 | const TemplateArgumentListInfo *TemplateArgs) { | |||
2238 | // Only try to recover from lookup into dependent bases in static methods or | |||
2239 | // contexts where 'this' is available. | |||
2240 | QualType ThisType = S.getCurrentThisType(); | |||
2241 | const CXXRecordDecl *RD = nullptr; | |||
2242 | if (!ThisType.isNull()) | |||
2243 | RD = ThisType->getPointeeType()->getAsCXXRecordDecl(); | |||
2244 | else if (auto *MD = dyn_cast<CXXMethodDecl>(S.CurContext)) | |||
2245 | RD = MD->getParent(); | |||
2246 | if (!RD || !RD->hasAnyDependentBases()) | |||
2247 | return nullptr; | |||
2248 | ||||
2249 | // Diagnose this as unqualified lookup into a dependent base class. If 'this' | |||
2250 | // is available, suggest inserting 'this->' as a fixit. | |||
2251 | SourceLocation Loc = NameInfo.getLoc(); | |||
2252 | auto DB = S.Diag(Loc, diag::ext_undeclared_unqual_id_with_dependent_base); | |||
2253 | DB << NameInfo.getName() << RD; | |||
2254 | ||||
2255 | if (!ThisType.isNull()) { | |||
2256 | DB << FixItHint::CreateInsertion(Loc, "this->"); | |||
2257 | return CXXDependentScopeMemberExpr::Create( | |||
2258 | Context, /*This=*/nullptr, ThisType, /*IsArrow=*/true, | |||
2259 | /*Op=*/SourceLocation(), NestedNameSpecifierLoc(), TemplateKWLoc, | |||
2260 | /*FirstQualifierFoundInScope=*/nullptr, NameInfo, TemplateArgs); | |||
2261 | } | |||
2262 | ||||
2263 | // Synthesize a fake NNS that points to the derived class. This will | |||
2264 | // perform name lookup during template instantiation. | |||
2265 | CXXScopeSpec SS; | |||
2266 | auto *NNS = | |||
2267 | NestedNameSpecifier::Create(Context, nullptr, true, RD->getTypeForDecl()); | |||
2268 | SS.MakeTrivial(Context, NNS, SourceRange(Loc, Loc)); | |||
2269 | return DependentScopeDeclRefExpr::Create( | |||
2270 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, | |||
2271 | TemplateArgs); | |||
2272 | } | |||
2273 | ||||
2274 | ExprResult | |||
2275 | Sema::ActOnIdExpression(Scope *S, CXXScopeSpec &SS, | |||
2276 | SourceLocation TemplateKWLoc, UnqualifiedId &Id, | |||
2277 | bool HasTrailingLParen, bool IsAddressOfOperand, | |||
2278 | CorrectionCandidateCallback *CCC, | |||
2279 | bool IsInlineAsmIdentifier, Token *KeywordReplacement) { | |||
2280 | assert(!(IsAddressOfOperand && HasTrailingLParen) &&((!(IsAddressOfOperand && HasTrailingLParen) && "cannot be direct & operand and have a trailing lparen") ? static_cast<void> (0) : __assert_fail ("!(IsAddressOfOperand && HasTrailingLParen) && \"cannot be direct & operand and have a trailing lparen\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 2281, __PRETTY_FUNCTION__)) | |||
2281 | "cannot be direct & operand and have a trailing lparen")((!(IsAddressOfOperand && HasTrailingLParen) && "cannot be direct & operand and have a trailing lparen") ? static_cast<void> (0) : __assert_fail ("!(IsAddressOfOperand && HasTrailingLParen) && \"cannot be direct & operand and have a trailing lparen\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 2281, __PRETTY_FUNCTION__)); | |||
2282 | if (SS.isInvalid()) | |||
2283 | return ExprError(); | |||
2284 | ||||
2285 | TemplateArgumentListInfo TemplateArgsBuffer; | |||
2286 | ||||
2287 | // Decompose the UnqualifiedId into the following data. | |||
2288 | DeclarationNameInfo NameInfo; | |||
2289 | const TemplateArgumentListInfo *TemplateArgs; | |||
2290 | DecomposeUnqualifiedId(Id, TemplateArgsBuffer, NameInfo, TemplateArgs); | |||
2291 | ||||
2292 | DeclarationName Name = NameInfo.getName(); | |||
2293 | IdentifierInfo *II = Name.getAsIdentifierInfo(); | |||
2294 | SourceLocation NameLoc = NameInfo.getLoc(); | |||
2295 | ||||
2296 | if (II && II->isEditorPlaceholder()) { | |||
2297 | // FIXME: When typed placeholders are supported we can create a typed | |||
2298 | // placeholder expression node. | |||
2299 | return ExprError(); | |||
2300 | } | |||
2301 | ||||
2302 | // C++ [temp.dep.expr]p3: | |||
2303 | // An id-expression is type-dependent if it contains: | |||
2304 | // -- an identifier that was declared with a dependent type, | |||
2305 | // (note: handled after lookup) | |||
2306 | // -- a template-id that is dependent, | |||
2307 | // (note: handled in BuildTemplateIdExpr) | |||
2308 | // -- a conversion-function-id that specifies a dependent type, | |||
2309 | // -- a nested-name-specifier that contains a class-name that | |||
2310 | // names a dependent type. | |||
2311 | // Determine whether this is a member of an unknown specialization; | |||
2312 | // we need to handle these differently. | |||
2313 | bool DependentID = false; | |||
2314 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && | |||
2315 | Name.getCXXNameType()->isDependentType()) { | |||
2316 | DependentID = true; | |||
2317 | } else if (SS.isSet()) { | |||
2318 | if (DeclContext *DC = computeDeclContext(SS, false)) { | |||
2319 | if (RequireCompleteDeclContext(SS, DC)) | |||
2320 | return ExprError(); | |||
2321 | } else { | |||
2322 | DependentID = true; | |||
2323 | } | |||
2324 | } | |||
2325 | ||||
2326 | if (DependentID) | |||
2327 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, | |||
2328 | IsAddressOfOperand, TemplateArgs); | |||
2329 | ||||
2330 | // Perform the required lookup. | |||
2331 | LookupResult R(*this, NameInfo, | |||
2332 | (Id.getKind() == UnqualifiedIdKind::IK_ImplicitSelfParam) | |||
2333 | ? LookupObjCImplicitSelfParam | |||
2334 | : LookupOrdinaryName); | |||
2335 | if (TemplateKWLoc.isValid() || TemplateArgs) { | |||
2336 | // Lookup the template name again to correctly establish the context in | |||
2337 | // which it was found. This is really unfortunate as we already did the | |||
2338 | // lookup to determine that it was a template name in the first place. If | |||
2339 | // this becomes a performance hit, we can work harder to preserve those | |||
2340 | // results until we get here but it's likely not worth it. | |||
2341 | bool MemberOfUnknownSpecialization; | |||
2342 | AssumedTemplateKind AssumedTemplate; | |||
2343 | if (LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, | |||
2344 | MemberOfUnknownSpecialization, TemplateKWLoc, | |||
2345 | &AssumedTemplate)) | |||
2346 | return ExprError(); | |||
2347 | ||||
2348 | if (MemberOfUnknownSpecialization || | |||
2349 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) | |||
2350 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, | |||
2351 | IsAddressOfOperand, TemplateArgs); | |||
2352 | } else { | |||
2353 | bool IvarLookupFollowUp = II && !SS.isSet() && getCurMethodDecl(); | |||
2354 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); | |||
2355 | ||||
2356 | // If the result might be in a dependent base class, this is a dependent | |||
2357 | // id-expression. | |||
2358 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) | |||
2359 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, | |||
2360 | IsAddressOfOperand, TemplateArgs); | |||
2361 | ||||
2362 | // If this reference is in an Objective-C method, then we need to do | |||
2363 | // some special Objective-C lookup, too. | |||
2364 | if (IvarLookupFollowUp) { | |||
2365 | ExprResult E(LookupInObjCMethod(R, S, II, true)); | |||
2366 | if (E.isInvalid()) | |||
2367 | return ExprError(); | |||
2368 | ||||
2369 | if (Expr *Ex = E.getAs<Expr>()) | |||
2370 | return Ex; | |||
2371 | } | |||
2372 | } | |||
2373 | ||||
2374 | if (R.isAmbiguous()) | |||
2375 | return ExprError(); | |||
2376 | ||||
2377 | // This could be an implicitly declared function reference (legal in C90, | |||
2378 | // extension in C99, forbidden in C++). | |||
2379 | if (R.empty() && HasTrailingLParen && II && !getLangOpts().CPlusPlus) { | |||
2380 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); | |||
2381 | if (D) R.addDecl(D); | |||
2382 | } | |||
2383 | ||||
2384 | // Determine whether this name might be a candidate for | |||
2385 | // argument-dependent lookup. | |||
2386 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); | |||
2387 | ||||
2388 | if (R.empty() && !ADL) { | |||
2389 | if (SS.isEmpty() && getLangOpts().MSVCCompat) { | |||
2390 | if (Expr *E = recoverFromMSUnqualifiedLookup(*this, Context, NameInfo, | |||
2391 | TemplateKWLoc, TemplateArgs)) | |||
2392 | return E; | |||
2393 | } | |||
2394 | ||||
2395 | // Don't diagnose an empty lookup for inline assembly. | |||
2396 | if (IsInlineAsmIdentifier) | |||
2397 | return ExprError(); | |||
2398 | ||||
2399 | // If this name wasn't predeclared and if this is not a function | |||
2400 | // call, diagnose the problem. | |||
2401 | TypoExpr *TE = nullptr; | |||
2402 | DefaultFilterCCC DefaultValidator(II, SS.isValid() ? SS.getScopeRep() | |||
2403 | : nullptr); | |||
2404 | DefaultValidator.IsAddressOfOperand = IsAddressOfOperand; | |||
2405 | assert((!CCC || CCC->IsAddressOfOperand == IsAddressOfOperand) &&(((!CCC || CCC->IsAddressOfOperand == IsAddressOfOperand) && "Typo correction callback misconfigured") ? static_cast<void > (0) : __assert_fail ("(!CCC || CCC->IsAddressOfOperand == IsAddressOfOperand) && \"Typo correction callback misconfigured\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 2406, __PRETTY_FUNCTION__)) | |||
2406 | "Typo correction callback misconfigured")(((!CCC || CCC->IsAddressOfOperand == IsAddressOfOperand) && "Typo correction callback misconfigured") ? static_cast<void > (0) : __assert_fail ("(!CCC || CCC->IsAddressOfOperand == IsAddressOfOperand) && \"Typo correction callback misconfigured\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 2406, __PRETTY_FUNCTION__)); | |||
2407 | if (CCC) { | |||
2408 | // Make sure the callback knows what the typo being diagnosed is. | |||
2409 | CCC->setTypoName(II); | |||
2410 | if (SS.isValid()) | |||
2411 | CCC->setTypoNNS(SS.getScopeRep()); | |||
2412 | } | |||
2413 | // FIXME: DiagnoseEmptyLookup produces bad diagnostics if we're looking for | |||
2414 | // a template name, but we happen to have always already looked up the name | |||
2415 | // before we get here if it must be a template name. | |||
2416 | if (DiagnoseEmptyLookup(S, SS, R, CCC ? *CCC : DefaultValidator, nullptr, | |||
2417 | None, &TE)) { | |||
2418 | if (TE && KeywordReplacement) { | |||
2419 | auto &State = getTypoExprState(TE); | |||
2420 | auto BestTC = State.Consumer->getNextCorrection(); | |||
2421 | if (BestTC.isKeyword()) { | |||
2422 | auto *II = BestTC.getCorrectionAsIdentifierInfo(); | |||
2423 | if (State.DiagHandler) | |||
2424 | State.DiagHandler(BestTC); | |||
2425 | KeywordReplacement->startToken(); | |||
2426 | KeywordReplacement->setKind(II->getTokenID()); | |||
2427 | KeywordReplacement->setIdentifierInfo(II); | |||
2428 | KeywordReplacement->setLocation(BestTC.getCorrectionRange().getBegin()); | |||
2429 | // Clean up the state associated with the TypoExpr, since it has | |||
2430 | // now been diagnosed (without a call to CorrectDelayedTyposInExpr). | |||
2431 | clearDelayedTypo(TE); | |||
2432 | // Signal that a correction to a keyword was performed by returning a | |||
2433 | // valid-but-null ExprResult. | |||
2434 | return (Expr*)nullptr; | |||
2435 | } | |||
2436 | State.Consumer->resetCorrectionStream(); | |||
2437 | } | |||
2438 | return TE ? TE : ExprError(); | |||
2439 | } | |||
2440 | ||||
2441 | assert(!R.empty() &&((!R.empty() && "DiagnoseEmptyLookup returned false but added no results" ) ? static_cast<void> (0) : __assert_fail ("!R.empty() && \"DiagnoseEmptyLookup returned false but added no results\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 2442, __PRETTY_FUNCTION__)) | |||
2442 | "DiagnoseEmptyLookup returned false but added no results")((!R.empty() && "DiagnoseEmptyLookup returned false but added no results" ) ? static_cast<void> (0) : __assert_fail ("!R.empty() && \"DiagnoseEmptyLookup returned false but added no results\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 2442, __PRETTY_FUNCTION__)); | |||
2443 | ||||
2444 | // If we found an Objective-C instance variable, let | |||
2445 | // LookupInObjCMethod build the appropriate expression to | |||
2446 | // reference the ivar. | |||
2447 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { | |||
2448 | R.clear(); | |||
2449 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); | |||
2450 | // In a hopelessly buggy code, Objective-C instance variable | |||
2451 | // lookup fails and no expression will be built to reference it. | |||
2452 | if (!E.isInvalid() && !E.get()) | |||
2453 | return ExprError(); | |||
2454 | return E; | |||
2455 | } | |||
2456 | } | |||
2457 | ||||
2458 | // This is guaranteed from this point on. | |||
2459 | assert(!R.empty() || ADL)((!R.empty() || ADL) ? static_cast<void> (0) : __assert_fail ("!R.empty() || ADL", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 2459, __PRETTY_FUNCTION__)); | |||
2460 | ||||
2461 | // Check whether this might be a C++ implicit instance member access. | |||
2462 | // C++ [class.mfct.non-static]p3: | |||
2463 | // When an id-expression that is not part of a class member access | |||
2464 | // syntax and not used to form a pointer to member is used in the | |||
2465 | // body of a non-static member function of class X, if name lookup | |||
2466 | // resolves the name in the id-expression to a non-static non-type | |||
2467 | // member of some class C, the id-expression is transformed into a | |||
2468 | // class member access expression using (*this) as the | |||
2469 | // postfix-expression to the left of the . operator. | |||
2470 | // | |||
2471 | // But we don't actually need to do this for '&' operands if R | |||
2472 | // resolved to a function or overloaded function set, because the | |||
2473 | // expression is ill-formed if it actually works out to be a | |||
2474 | // non-static member function: | |||
2475 | // | |||
2476 | // C++ [expr.ref]p4: | |||
2477 | // Otherwise, if E1.E2 refers to a non-static member function. . . | |||
2478 | // [t]he expression can be used only as the left-hand operand of a | |||
2479 | // member function call. | |||
2480 | // | |||
2481 | // There are other safeguards against such uses, but it's important | |||
2482 | // to get this right here so that we don't end up making a | |||
2483 | // spuriously dependent expression if we're inside a dependent | |||
2484 | // instance method. | |||
2485 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { | |||
2486 | bool MightBeImplicitMember; | |||
2487 | if (!IsAddressOfOperand) | |||
2488 | MightBeImplicitMember = true; | |||
2489 | else if (!SS.isEmpty()) | |||
2490 | MightBeImplicitMember = false; | |||
2491 | else if (R.isOverloadedResult()) | |||
2492 | MightBeImplicitMember = false; | |||
2493 | else if (R.isUnresolvableResult()) | |||
2494 | MightBeImplicitMember = true; | |||
2495 | else | |||
2496 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || | |||
2497 | isa<IndirectFieldDecl>(R.getFoundDecl()) || | |||
2498 | isa<MSPropertyDecl>(R.getFoundDecl()); | |||
2499 | ||||
2500 | if (MightBeImplicitMember) | |||
2501 | return BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, | |||
2502 | R, TemplateArgs, S); | |||
2503 | } | |||
2504 | ||||
2505 | if (TemplateArgs || TemplateKWLoc.isValid()) { | |||
2506 | ||||
2507 | // In C++1y, if this is a variable template id, then check it | |||
2508 | // in BuildTemplateIdExpr(). | |||
2509 | // The single lookup result must be a variable template declaration. | |||
2510 | if (Id.getKind() == UnqualifiedIdKind::IK_TemplateId && Id.TemplateId && | |||
2511 | Id.TemplateId->Kind == TNK_Var_template) { | |||
2512 | assert(R.getAsSingle<VarTemplateDecl>() &&((R.getAsSingle<VarTemplateDecl>() && "There should only be one declaration found." ) ? static_cast<void> (0) : __assert_fail ("R.getAsSingle<VarTemplateDecl>() && \"There should only be one declaration found.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 2513, __PRETTY_FUNCTION__)) | |||
2513 | "There should only be one declaration found.")((R.getAsSingle<VarTemplateDecl>() && "There should only be one declaration found." ) ? static_cast<void> (0) : __assert_fail ("R.getAsSingle<VarTemplateDecl>() && \"There should only be one declaration found.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 2513, __PRETTY_FUNCTION__)); | |||
2514 | } | |||
2515 | ||||
2516 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, ADL, TemplateArgs); | |||
2517 | } | |||
2518 | ||||
2519 | return BuildDeclarationNameExpr(SS, R, ADL); | |||
2520 | } | |||
2521 | ||||
2522 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified | |||
2523 | /// declaration name, generally during template instantiation. | |||
2524 | /// There's a large number of things which don't need to be done along | |||
2525 | /// this path. | |||
2526 | ExprResult Sema::BuildQualifiedDeclarationNameExpr( | |||
2527 | CXXScopeSpec &SS, const DeclarationNameInfo &NameInfo, | |||
2528 | bool IsAddressOfOperand, const Scope *S, TypeSourceInfo **RecoveryTSI) { | |||
2529 | DeclContext *DC = computeDeclContext(SS, false); | |||
2530 | if (!DC) | |||
2531 | return BuildDependentDeclRefExpr(SS, /*TemplateKWLoc=*/SourceLocation(), | |||
2532 | NameInfo, /*TemplateArgs=*/nullptr); | |||
2533 | ||||
2534 | if (RequireCompleteDeclContext(SS, DC)) | |||
2535 | return ExprError(); | |||
2536 | ||||
2537 | LookupResult R(*this, NameInfo, LookupOrdinaryName); | |||
2538 | LookupQualifiedName(R, DC); | |||
2539 | ||||
2540 | if (R.isAmbiguous()) | |||
2541 | return ExprError(); | |||
2542 | ||||
2543 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) | |||
2544 | return BuildDependentDeclRefExpr(SS, /*TemplateKWLoc=*/SourceLocation(), | |||
2545 | NameInfo, /*TemplateArgs=*/nullptr); | |||
2546 | ||||
2547 | if (R.empty()) { | |||
2548 | Diag(NameInfo.getLoc(), diag::err_no_member) | |||
2549 | << NameInfo.getName() << DC << SS.getRange(); | |||
2550 | return ExprError(); | |||
2551 | } | |||
2552 | ||||
2553 | if (const TypeDecl *TD = R.getAsSingle<TypeDecl>()) { | |||
2554 | // Diagnose a missing typename if this resolved unambiguously to a type in | |||
2555 | // a dependent context. If we can recover with a type, downgrade this to | |||
2556 | // a warning in Microsoft compatibility mode. | |||
2557 | unsigned DiagID = diag::err_typename_missing; | |||
2558 | if (RecoveryTSI && getLangOpts().MSVCCompat) | |||
2559 | DiagID = diag::ext_typename_missing; | |||
2560 | SourceLocation Loc = SS.getBeginLoc(); | |||
2561 | auto D = Diag(Loc, DiagID); | |||
2562 | D << SS.getScopeRep() << NameInfo.getName().getAsString() | |||
2563 | << SourceRange(Loc, NameInfo.getEndLoc()); | |||
2564 | ||||
2565 | // Don't recover if the caller isn't expecting us to or if we're in a SFINAE | |||
2566 | // context. | |||
2567 | if (!RecoveryTSI) | |||
2568 | return ExprError(); | |||
2569 | ||||
2570 | // Only issue the fixit if we're prepared to recover. | |||
2571 | D << FixItHint::CreateInsertion(Loc, "typename "); | |||
2572 | ||||
2573 | // Recover by pretending this was an elaborated type. | |||
2574 | QualType Ty = Context.getTypeDeclType(TD); | |||
2575 | TypeLocBuilder TLB; | |||
2576 | TLB.pushTypeSpec(Ty).setNameLoc(NameInfo.getLoc()); | |||
2577 | ||||
2578 | QualType ET = getElaboratedType(ETK_None, SS, Ty); | |||
2579 | ElaboratedTypeLoc QTL = TLB.push<ElaboratedTypeLoc>(ET); | |||
2580 | QTL.setElaboratedKeywordLoc(SourceLocation()); | |||
2581 | QTL.setQualifierLoc(SS.getWithLocInContext(Context)); | |||
2582 | ||||
2583 | *RecoveryTSI = TLB.getTypeSourceInfo(Context, ET); | |||
2584 | ||||
2585 | return ExprEmpty(); | |||
2586 | } | |||
2587 | ||||
2588 | // Defend against this resolving to an implicit member access. We usually | |||
2589 | // won't get here if this might be a legitimate a class member (we end up in | |||
2590 | // BuildMemberReferenceExpr instead), but this can be valid if we're forming | |||
2591 | // a pointer-to-member or in an unevaluated context in C++11. | |||
2592 | if (!R.empty() && (*R.begin())->isCXXClassMember() && !IsAddressOfOperand) | |||
2593 | return BuildPossibleImplicitMemberExpr(SS, | |||
2594 | /*TemplateKWLoc=*/SourceLocation(), | |||
2595 | R, /*TemplateArgs=*/nullptr, S); | |||
2596 | ||||
2597 | return BuildDeclarationNameExpr(SS, R, /* ADL */ false); | |||
2598 | } | |||
2599 | ||||
2600 | /// The parser has read a name in, and Sema has detected that we're currently | |||
2601 | /// inside an ObjC method. Perform some additional checks and determine if we | |||
2602 | /// should form a reference to an ivar. | |||
2603 | /// | |||
2604 | /// Ideally, most of this would be done by lookup, but there's | |||
2605 | /// actually quite a lot of extra work involved. | |||
2606 | DeclResult Sema::LookupIvarInObjCMethod(LookupResult &Lookup, Scope *S, | |||
2607 | IdentifierInfo *II) { | |||
2608 | SourceLocation Loc = Lookup.getNameLoc(); | |||
2609 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); | |||
2610 | ||||
2611 | // Check for error condition which is already reported. | |||
2612 | if (!CurMethod) | |||
2613 | return DeclResult(true); | |||
2614 | ||||
2615 | // There are two cases to handle here. 1) scoped lookup could have failed, | |||
2616 | // in which case we should look for an ivar. 2) scoped lookup could have | |||
2617 | // found a decl, but that decl is outside the current instance method (i.e. | |||
2618 | // a global variable). In these two cases, we do a lookup for an ivar with | |||
2619 | // this name, if the lookup sucedes, we replace it our current decl. | |||
2620 | ||||
2621 | // If we're in a class method, we don't normally want to look for | |||
2622 | // ivars. But if we don't find anything else, and there's an | |||
2623 | // ivar, that's an error. | |||
2624 | bool IsClassMethod = CurMethod->isClassMethod(); | |||
2625 | ||||
2626 | bool LookForIvars; | |||
2627 | if (Lookup.empty()) | |||
2628 | LookForIvars = true; | |||
2629 | else if (IsClassMethod) | |||
2630 | LookForIvars = false; | |||
2631 | else | |||
2632 | LookForIvars = (Lookup.isSingleResult() && | |||
2633 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); | |||
2634 | ObjCInterfaceDecl *IFace = nullptr; | |||
2635 | if (LookForIvars) { | |||
2636 | IFace = CurMethod->getClassInterface(); | |||
2637 | ObjCInterfaceDecl *ClassDeclared; | |||
2638 | ObjCIvarDecl *IV = nullptr; | |||
2639 | if (IFace && (IV = IFace->lookupInstanceVariable(II, ClassDeclared))) { | |||
2640 | // Diagnose using an ivar in a class method. | |||
2641 | if (IsClassMethod) { | |||
2642 | Diag(Loc, diag::err_ivar_use_in_class_method) << IV->getDeclName(); | |||
2643 | return DeclResult(true); | |||
2644 | } | |||
2645 | ||||
2646 | // Diagnose the use of an ivar outside of the declaring class. | |||
2647 | if (IV->getAccessControl() == ObjCIvarDecl::Private && | |||
2648 | !declaresSameEntity(ClassDeclared, IFace) && | |||
2649 | !getLangOpts().DebuggerSupport) | |||
2650 | Diag(Loc, diag::err_private_ivar_access) << IV->getDeclName(); | |||
2651 | ||||
2652 | // Success. | |||
2653 | return IV; | |||
2654 | } | |||
2655 | } else if (CurMethod->isInstanceMethod()) { | |||
2656 | // We should warn if a local variable hides an ivar. | |||
2657 | if (ObjCInterfaceDecl *IFace = CurMethod->getClassInterface()) { | |||
2658 | ObjCInterfaceDecl *ClassDeclared; | |||
2659 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { | |||
2660 | if (IV->getAccessControl() != ObjCIvarDecl::Private || | |||
2661 | declaresSameEntity(IFace, ClassDeclared)) | |||
2662 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); | |||
2663 | } | |||
2664 | } | |||
2665 | } else if (Lookup.isSingleResult() && | |||
2666 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()) { | |||
2667 | // If accessing a stand-alone ivar in a class method, this is an error. | |||
2668 | if (const ObjCIvarDecl *IV = | |||
2669 | dyn_cast<ObjCIvarDecl>(Lookup.getFoundDecl())) { | |||
2670 | Diag(Loc, diag::err_ivar_use_in_class_method) << IV->getDeclName(); | |||
2671 | return DeclResult(true); | |||
2672 | } | |||
2673 | } | |||
2674 | ||||
2675 | // Didn't encounter an error, didn't find an ivar. | |||
2676 | return DeclResult(false); | |||
2677 | } | |||
2678 | ||||
2679 | ExprResult Sema::BuildIvarRefExpr(Scope *S, SourceLocation Loc, | |||
2680 | ObjCIvarDecl *IV) { | |||
2681 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); | |||
2682 | assert(CurMethod && CurMethod->isInstanceMethod() &&((CurMethod && CurMethod->isInstanceMethod() && "should not reference ivar from this context") ? static_cast <void> (0) : __assert_fail ("CurMethod && CurMethod->isInstanceMethod() && \"should not reference ivar from this context\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 2683, __PRETTY_FUNCTION__)) | |||
2683 | "should not reference ivar from this context")((CurMethod && CurMethod->isInstanceMethod() && "should not reference ivar from this context") ? static_cast <void> (0) : __assert_fail ("CurMethod && CurMethod->isInstanceMethod() && \"should not reference ivar from this context\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 2683, __PRETTY_FUNCTION__)); | |||
2684 | ||||
2685 | ObjCInterfaceDecl *IFace = CurMethod->getClassInterface(); | |||
2686 | assert(IFace && "should not reference ivar from this context")((IFace && "should not reference ivar from this context" ) ? static_cast<void> (0) : __assert_fail ("IFace && \"should not reference ivar from this context\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 2686, __PRETTY_FUNCTION__)); | |||
2687 | ||||
2688 | // If we're referencing an invalid decl, just return this as a silent | |||
2689 | // error node. The error diagnostic was already emitted on the decl. | |||
2690 | if (IV->isInvalidDecl()) | |||
2691 | return ExprError(); | |||
2692 | ||||
2693 | // Check if referencing a field with __attribute__((deprecated)). | |||
2694 | if (DiagnoseUseOfDecl(IV, Loc)) | |||
2695 | return ExprError(); | |||
2696 | ||||
2697 | // FIXME: This should use a new expr for a direct reference, don't | |||
2698 | // turn this into Self->ivar, just return a BareIVarExpr or something. | |||
2699 | IdentifierInfo &II = Context.Idents.get("self"); | |||
2700 | UnqualifiedId SelfName; | |||
2701 | SelfName.setIdentifier(&II, SourceLocation()); | |||
2702 | SelfName.setKind(UnqualifiedIdKind::IK_ImplicitSelfParam); | |||
2703 | CXXScopeSpec SelfScopeSpec; | |||
2704 | SourceLocation TemplateKWLoc; | |||
2705 | ExprResult SelfExpr = | |||
2706 | ActOnIdExpression(S, SelfScopeSpec, TemplateKWLoc, SelfName, | |||
2707 | /*HasTrailingLParen=*/false, | |||
2708 | /*IsAddressOfOperand=*/false); | |||
2709 | if (SelfExpr.isInvalid()) | |||
2710 | return ExprError(); | |||
2711 | ||||
2712 | SelfExpr = DefaultLvalueConversion(SelfExpr.get()); | |||
2713 | if (SelfExpr.isInvalid()) | |||
2714 | return ExprError(); | |||
2715 | ||||
2716 | MarkAnyDeclReferenced(Loc, IV, true); | |||
2717 | ||||
2718 | ObjCMethodFamily MF = CurMethod->getMethodFamily(); | |||
2719 | if (MF != OMF_init && MF != OMF_dealloc && MF != OMF_finalize && | |||
2720 | !IvarBacksCurrentMethodAccessor(IFace, CurMethod, IV)) | |||
2721 | Diag(Loc, diag::warn_direct_ivar_access) << IV->getDeclName(); | |||
2722 | ||||
2723 | ObjCIvarRefExpr *Result = new (Context) | |||
2724 | ObjCIvarRefExpr(IV, IV->getUsageType(SelfExpr.get()->getType()), Loc, | |||
2725 | IV->getLocation(), SelfExpr.get(), true, true); | |||
2726 | ||||
2727 | if (IV->getType().getObjCLifetime() == Qualifiers::OCL_Weak) { | |||
2728 | if (!isUnevaluatedContext() && | |||
2729 | !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, Loc)) | |||
2730 | getCurFunction()->recordUseOfWeak(Result); | |||
2731 | } | |||
2732 | if (getLangOpts().ObjCAutoRefCount) | |||
2733 | if (const BlockDecl *BD = CurContext->getInnermostBlockDecl()) | |||
2734 | ImplicitlyRetainedSelfLocs.push_back({Loc, BD}); | |||
2735 | ||||
2736 | return Result; | |||
2737 | } | |||
2738 | ||||
2739 | /// The parser has read a name in, and Sema has detected that we're currently | |||
2740 | /// inside an ObjC method. Perform some additional checks and determine if we | |||
2741 | /// should form a reference to an ivar. If so, build an expression referencing | |||
2742 | /// that ivar. | |||
2743 | ExprResult | |||
2744 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, | |||
2745 | IdentifierInfo *II, bool AllowBuiltinCreation) { | |||
2746 | // FIXME: Integrate this lookup step into LookupParsedName. | |||
2747 | DeclResult Ivar = LookupIvarInObjCMethod(Lookup, S, II); | |||
2748 | if (Ivar.isInvalid()) | |||
2749 | return ExprError(); | |||
2750 | if (Ivar.isUsable()) | |||
2751 | return BuildIvarRefExpr(S, Lookup.getNameLoc(), | |||
2752 | cast<ObjCIvarDecl>(Ivar.get())); | |||
2753 | ||||
2754 | if (Lookup.empty() && II && AllowBuiltinCreation) | |||
2755 | LookupBuiltin(Lookup); | |||
2756 | ||||
2757 | // Sentinel value saying that we didn't do anything special. | |||
2758 | return ExprResult(false); | |||
2759 | } | |||
2760 | ||||
2761 | /// Cast a base object to a member's actual type. | |||
2762 | /// | |||
2763 | /// Logically this happens in three phases: | |||
2764 | /// | |||
2765 | /// * First we cast from the base type to the naming class. | |||
2766 | /// The naming class is the class into which we were looking | |||
2767 | /// when we found the member; it's the qualifier type if a | |||
2768 | /// qualifier was provided, and otherwise it's the base type. | |||
2769 | /// | |||
2770 | /// * Next we cast from the naming class to the declaring class. | |||
2771 | /// If the member we found was brought into a class's scope by | |||
2772 | /// a using declaration, this is that class; otherwise it's | |||
2773 | /// the class declaring the member. | |||
2774 | /// | |||
2775 | /// * Finally we cast from the declaring class to the "true" | |||
2776 | /// declaring class of the member. This conversion does not | |||
2777 | /// obey access control. | |||
2778 | ExprResult | |||
2779 | Sema::PerformObjectMemberConversion(Expr *From, | |||
2780 | NestedNameSpecifier *Qualifier, | |||
2781 | NamedDecl *FoundDecl, | |||
2782 | NamedDecl *Member) { | |||
2783 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); | |||
2784 | if (!RD) | |||
2785 | return From; | |||
2786 | ||||
2787 | QualType DestRecordType; | |||
2788 | QualType DestType; | |||
2789 | QualType FromRecordType; | |||
2790 | QualType FromType = From->getType(); | |||
2791 | bool PointerConversions = false; | |||
2792 | if (isa<FieldDecl>(Member)) { | |||
2793 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); | |||
2794 | auto FromPtrType = FromType->getAs<PointerType>(); | |||
2795 | DestRecordType = Context.getAddrSpaceQualType( | |||
2796 | DestRecordType, FromPtrType | |||
2797 | ? FromType->getPointeeType().getAddressSpace() | |||
2798 | : FromType.getAddressSpace()); | |||
2799 | ||||
2800 | if (FromPtrType) { | |||
2801 | DestType = Context.getPointerType(DestRecordType); | |||
2802 | FromRecordType = FromPtrType->getPointeeType(); | |||
2803 | PointerConversions = true; | |||
2804 | } else { | |||
2805 | DestType = DestRecordType; | |||
2806 | FromRecordType = FromType; | |||
2807 | } | |||
2808 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { | |||
2809 | if (Method->isStatic()) | |||
2810 | return From; | |||
2811 | ||||
2812 | DestType = Method->getThisType(); | |||
2813 | DestRecordType = DestType->getPointeeType(); | |||
2814 | ||||
2815 | if (FromType->getAs<PointerType>()) { | |||
2816 | FromRecordType = FromType->getPointeeType(); | |||
2817 | PointerConversions = true; | |||
2818 | } else { | |||
2819 | FromRecordType = FromType; | |||
2820 | DestType = DestRecordType; | |||
2821 | } | |||
2822 | ||||
2823 | LangAS FromAS = FromRecordType.getAddressSpace(); | |||
2824 | LangAS DestAS = DestRecordType.getAddressSpace(); | |||
2825 | if (FromAS != DestAS) { | |||
2826 | QualType FromRecordTypeWithoutAS = | |||
2827 | Context.removeAddrSpaceQualType(FromRecordType); | |||
2828 | QualType FromTypeWithDestAS = | |||
2829 | Context.getAddrSpaceQualType(FromRecordTypeWithoutAS, DestAS); | |||
2830 | if (PointerConversions) | |||
2831 | FromTypeWithDestAS = Context.getPointerType(FromTypeWithDestAS); | |||
2832 | From = ImpCastExprToType(From, FromTypeWithDestAS, | |||
2833 | CK_AddressSpaceConversion, From->getValueKind()) | |||
2834 | .get(); | |||
2835 | } | |||
2836 | } else { | |||
2837 | // No conversion necessary. | |||
2838 | return From; | |||
2839 | } | |||
2840 | ||||
2841 | if (DestType->isDependentType() || FromType->isDependentType()) | |||
2842 | return From; | |||
2843 | ||||
2844 | // If the unqualified types are the same, no conversion is necessary. | |||
2845 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) | |||
2846 | return From; | |||
2847 | ||||
2848 | SourceRange FromRange = From->getSourceRange(); | |||
2849 | SourceLocation FromLoc = FromRange.getBegin(); | |||
2850 | ||||
2851 | ExprValueKind VK = From->getValueKind(); | |||
2852 | ||||
2853 | // C++ [class.member.lookup]p8: | |||
2854 | // [...] Ambiguities can often be resolved by qualifying a name with its | |||
2855 | // class name. | |||
2856 | // | |||
2857 | // If the member was a qualified name and the qualified referred to a | |||
2858 | // specific base subobject type, we'll cast to that intermediate type | |||
2859 | // first and then to the object in which the member is declared. That allows | |||
2860 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: | |||
2861 | // | |||
2862 | // class Base { public: int x; }; | |||
2863 | // class Derived1 : public Base { }; | |||
2864 | // class Derived2 : public Base { }; | |||
2865 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; | |||
2866 | // | |||
2867 | // void VeryDerived::f() { | |||
2868 | // x = 17; // error: ambiguous base subobjects | |||
2869 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 | |||
2870 | // } | |||
2871 | if (Qualifier && Qualifier->getAsType()) { | |||
2872 | QualType QType = QualType(Qualifier->getAsType(), 0); | |||
2873 | assert(QType->isRecordType() && "lookup done with non-record type")((QType->isRecordType() && "lookup done with non-record type" ) ? static_cast<void> (0) : __assert_fail ("QType->isRecordType() && \"lookup done with non-record type\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 2873, __PRETTY_FUNCTION__)); | |||
2874 | ||||
2875 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); | |||
2876 | ||||
2877 | // In C++98, the qualifier type doesn't actually have to be a base | |||
2878 | // type of the object type, in which case we just ignore it. | |||
2879 | // Otherwise build the appropriate casts. | |||
2880 | if (IsDerivedFrom(FromLoc, FromRecordType, QRecordType)) { | |||
2881 | CXXCastPath BasePath; | |||
2882 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, | |||
2883 | FromLoc, FromRange, &BasePath)) | |||
2884 | return ExprError(); | |||
2885 | ||||
2886 | if (PointerConversions) | |||
2887 | QType = Context.getPointerType(QType); | |||
2888 | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, | |||
2889 | VK, &BasePath).get(); | |||
2890 | ||||
2891 | FromType = QType; | |||
2892 | FromRecordType = QRecordType; | |||
2893 | ||||
2894 | // If the qualifier type was the same as the destination type, | |||
2895 | // we're done. | |||
2896 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) | |||
2897 | return From; | |||
2898 | } | |||
2899 | } | |||
2900 | ||||
2901 | bool IgnoreAccess = false; | |||
2902 | ||||
2903 | // If we actually found the member through a using declaration, cast | |||
2904 | // down to the using declaration's type. | |||
2905 | // | |||
2906 | // Pointer equality is fine here because only one declaration of a | |||
2907 | // class ever has member declarations. | |||
2908 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { | |||
2909 | assert(isa<UsingShadowDecl>(FoundDecl))((isa<UsingShadowDecl>(FoundDecl)) ? static_cast<void > (0) : __assert_fail ("isa<UsingShadowDecl>(FoundDecl)" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 2909, __PRETTY_FUNCTION__)); | |||
2910 | QualType URecordType = Context.getTypeDeclType( | |||
2911 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); | |||
2912 | ||||
2913 | // We only need to do this if the naming-class to declaring-class | |||
2914 | // conversion is non-trivial. | |||
2915 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { | |||
2916 | assert(IsDerivedFrom(FromLoc, FromRecordType, URecordType))((IsDerivedFrom(FromLoc, FromRecordType, URecordType)) ? static_cast <void> (0) : __assert_fail ("IsDerivedFrom(FromLoc, FromRecordType, URecordType)" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 2916, __PRETTY_FUNCTION__)); | |||
2917 | CXXCastPath BasePath; | |||
2918 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, | |||
2919 | FromLoc, FromRange, &BasePath)) | |||
2920 | return ExprError(); | |||
2921 | ||||
2922 | QualType UType = URecordType; | |||
2923 | if (PointerConversions) | |||
2924 | UType = Context.getPointerType(UType); | |||
2925 | From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, | |||
2926 | VK, &BasePath).get(); | |||
2927 | FromType = UType; | |||
2928 | FromRecordType = URecordType; | |||
2929 | } | |||
2930 | ||||
2931 | // We don't do access control for the conversion from the | |||
2932 | // declaring class to the true declaring class. | |||
2933 | IgnoreAccess = true; | |||
2934 | } | |||
2935 | ||||
2936 | CXXCastPath BasePath; | |||
2937 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, | |||
2938 | FromLoc, FromRange, &BasePath, | |||
2939 | IgnoreAccess)) | |||
2940 | return ExprError(); | |||
2941 | ||||
2942 | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, | |||
2943 | VK, &BasePath); | |||
2944 | } | |||
2945 | ||||
2946 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, | |||
2947 | const LookupResult &R, | |||
2948 | bool HasTrailingLParen) { | |||
2949 | // Only when used directly as the postfix-expression of a call. | |||
2950 | if (!HasTrailingLParen) | |||
2951 | return false; | |||
2952 | ||||
2953 | // Never if a scope specifier was provided. | |||
2954 | if (SS.isSet()) | |||
2955 | return false; | |||
2956 | ||||
2957 | // Only in C++ or ObjC++. | |||
2958 | if (!getLangOpts().CPlusPlus) | |||
2959 | return false; | |||
2960 | ||||
2961 | // Turn off ADL when we find certain kinds of declarations during | |||
2962 | // normal lookup: | |||
2963 | for (NamedDecl *D : R) { | |||
2964 | // C++0x [basic.lookup.argdep]p3: | |||
2965 | // -- a declaration of a class member | |||
2966 | // Since using decls preserve this property, we check this on the | |||
2967 | // original decl. | |||
2968 | if (D->isCXXClassMember()) | |||
2969 | return false; | |||
2970 | ||||
2971 | // C++0x [basic.lookup.argdep]p3: | |||
2972 | // -- a block-scope function declaration that is not a | |||
2973 | // using-declaration | |||
2974 | // NOTE: we also trigger this for function templates (in fact, we | |||
2975 | // don't check the decl type at all, since all other decl types | |||
2976 | // turn off ADL anyway). | |||
2977 | if (isa<UsingShadowDecl>(D)) | |||
2978 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); | |||
2979 | else if (D->getLexicalDeclContext()->isFunctionOrMethod()) | |||
2980 | return false; | |||
2981 | ||||
2982 | // C++0x [basic.lookup.argdep]p3: | |||
2983 | // -- a declaration that is neither a function or a function | |||
2984 | // template | |||
2985 | // And also for builtin functions. | |||
2986 | if (isa<FunctionDecl>(D)) { | |||
2987 | FunctionDecl *FDecl = cast<FunctionDecl>(D); | |||
2988 | ||||
2989 | // But also builtin functions. | |||
2990 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) | |||
2991 | return false; | |||
2992 | } else if (!isa<FunctionTemplateDecl>(D)) | |||
2993 | return false; | |||
2994 | } | |||
2995 | ||||
2996 | return true; | |||
2997 | } | |||
2998 | ||||
2999 | ||||
3000 | /// Diagnoses obvious problems with the use of the given declaration | |||
3001 | /// as an expression. This is only actually called for lookups that | |||
3002 | /// were not overloaded, and it doesn't promise that the declaration | |||
3003 | /// will in fact be used. | |||
3004 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { | |||
3005 | if (D->isInvalidDecl()) | |||
3006 | return true; | |||
3007 | ||||
3008 | if (isa<TypedefNameDecl>(D)) { | |||
3009 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); | |||
3010 | return true; | |||
3011 | } | |||
3012 | ||||
3013 | if (isa<ObjCInterfaceDecl>(D)) { | |||
3014 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); | |||
3015 | return true; | |||
3016 | } | |||
3017 | ||||
3018 | if (isa<NamespaceDecl>(D)) { | |||
3019 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); | |||
3020 | return true; | |||
3021 | } | |||
3022 | ||||
3023 | return false; | |||
3024 | } | |||
3025 | ||||
3026 | // Certain multiversion types should be treated as overloaded even when there is | |||
3027 | // only one result. | |||
3028 | static bool ShouldLookupResultBeMultiVersionOverload(const LookupResult &R) { | |||
3029 | assert(R.isSingleResult() && "Expected only a single result")((R.isSingleResult() && "Expected only a single result" ) ? static_cast<void> (0) : __assert_fail ("R.isSingleResult() && \"Expected only a single result\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3029, __PRETTY_FUNCTION__)); | |||
3030 | const auto *FD = dyn_cast<FunctionDecl>(R.getFoundDecl()); | |||
3031 | return FD && | |||
3032 | (FD->isCPUDispatchMultiVersion() || FD->isCPUSpecificMultiVersion()); | |||
3033 | } | |||
3034 | ||||
3035 | ExprResult Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, | |||
3036 | LookupResult &R, bool NeedsADL, | |||
3037 | bool AcceptInvalidDecl) { | |||
3038 | // If this is a single, fully-resolved result and we don't need ADL, | |||
3039 | // just build an ordinary singleton decl ref. | |||
3040 | if (!NeedsADL && R.isSingleResult() && | |||
3041 | !R.getAsSingle<FunctionTemplateDecl>() && | |||
3042 | !ShouldLookupResultBeMultiVersionOverload(R)) | |||
3043 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), R.getFoundDecl(), | |||
3044 | R.getRepresentativeDecl(), nullptr, | |||
3045 | AcceptInvalidDecl); | |||
3046 | ||||
3047 | // We only need to check the declaration if there's exactly one | |||
3048 | // result, because in the overloaded case the results can only be | |||
3049 | // functions and function templates. | |||
3050 | if (R.isSingleResult() && !ShouldLookupResultBeMultiVersionOverload(R) && | |||
3051 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) | |||
3052 | return ExprError(); | |||
3053 | ||||
3054 | // Otherwise, just build an unresolved lookup expression. Suppress | |||
3055 | // any lookup-related diagnostics; we'll hash these out later, when | |||
3056 | // we've picked a target. | |||
3057 | R.suppressDiagnostics(); | |||
3058 | ||||
3059 | UnresolvedLookupExpr *ULE | |||
3060 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), | |||
3061 | SS.getWithLocInContext(Context), | |||
3062 | R.getLookupNameInfo(), | |||
3063 | NeedsADL, R.isOverloadedResult(), | |||
3064 | R.begin(), R.end()); | |||
3065 | ||||
3066 | return ULE; | |||
3067 | } | |||
3068 | ||||
3069 | static void | |||
3070 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, | |||
3071 | ValueDecl *var, DeclContext *DC); | |||
3072 | ||||
3073 | /// Complete semantic analysis for a reference to the given declaration. | |||
3074 | ExprResult Sema::BuildDeclarationNameExpr( | |||
3075 | const CXXScopeSpec &SS, const DeclarationNameInfo &NameInfo, NamedDecl *D, | |||
3076 | NamedDecl *FoundD, const TemplateArgumentListInfo *TemplateArgs, | |||
3077 | bool AcceptInvalidDecl) { | |||
3078 | assert(D && "Cannot refer to a NULL declaration")((D && "Cannot refer to a NULL declaration") ? static_cast <void> (0) : __assert_fail ("D && \"Cannot refer to a NULL declaration\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3078, __PRETTY_FUNCTION__)); | |||
3079 | assert(!isa<FunctionTemplateDecl>(D) &&((!isa<FunctionTemplateDecl>(D) && "Cannot refer unambiguously to a function template" ) ? static_cast<void> (0) : __assert_fail ("!isa<FunctionTemplateDecl>(D) && \"Cannot refer unambiguously to a function template\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3080, __PRETTY_FUNCTION__)) | |||
3080 | "Cannot refer unambiguously to a function template")((!isa<FunctionTemplateDecl>(D) && "Cannot refer unambiguously to a function template" ) ? static_cast<void> (0) : __assert_fail ("!isa<FunctionTemplateDecl>(D) && \"Cannot refer unambiguously to a function template\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3080, __PRETTY_FUNCTION__)); | |||
3081 | ||||
3082 | SourceLocation Loc = NameInfo.getLoc(); | |||
3083 | if (CheckDeclInExpr(*this, Loc, D)) | |||
3084 | return ExprError(); | |||
3085 | ||||
3086 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { | |||
3087 | // Specifically diagnose references to class templates that are missing | |||
3088 | // a template argument list. | |||
3089 | diagnoseMissingTemplateArguments(TemplateName(Template), Loc); | |||
3090 | return ExprError(); | |||
3091 | } | |||
3092 | ||||
3093 | // Make sure that we're referring to a value. | |||
3094 | ValueDecl *VD = dyn_cast<ValueDecl>(D); | |||
3095 | if (!VD) { | |||
3096 | Diag(Loc, diag::err_ref_non_value) | |||
3097 | << D << SS.getRange(); | |||
3098 | Diag(D->getLocation(), diag::note_declared_at); | |||
3099 | return ExprError(); | |||
3100 | } | |||
3101 | ||||
3102 | // Check whether this declaration can be used. Note that we suppress | |||
3103 | // this check when we're going to perform argument-dependent lookup | |||
3104 | // on this function name, because this might not be the function | |||
3105 | // that overload resolution actually selects. | |||
3106 | if (DiagnoseUseOfDecl(VD, Loc)) | |||
3107 | return ExprError(); | |||
3108 | ||||
3109 | // Only create DeclRefExpr's for valid Decl's. | |||
3110 | if (VD->isInvalidDecl() && !AcceptInvalidDecl) | |||
3111 | return ExprError(); | |||
3112 | ||||
3113 | // Handle members of anonymous structs and unions. If we got here, | |||
3114 | // and the reference is to a class member indirect field, then this | |||
3115 | // must be the subject of a pointer-to-member expression. | |||
3116 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) | |||
3117 | if (!indirectField->isCXXClassMember()) | |||
3118 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), | |||
3119 | indirectField); | |||
3120 | ||||
3121 | { | |||
3122 | QualType type = VD->getType(); | |||
3123 | if (type.isNull()) | |||
3124 | return ExprError(); | |||
3125 | ExprValueKind valueKind = VK_RValue; | |||
3126 | ||||
3127 | switch (D->getKind()) { | |||
3128 | // Ignore all the non-ValueDecl kinds. | |||
3129 | #define ABSTRACT_DECL(kind) | |||
3130 | #define VALUE(type, base) | |||
3131 | #define DECL(type, base) \ | |||
3132 | case Decl::type: | |||
3133 | #include "clang/AST/DeclNodes.inc" | |||
3134 | llvm_unreachable("invalid value decl kind")::llvm::llvm_unreachable_internal("invalid value decl kind", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3134); | |||
3135 | ||||
3136 | // These shouldn't make it here. | |||
3137 | case Decl::ObjCAtDefsField: | |||
3138 | llvm_unreachable("forming non-member reference to ivar?")::llvm::llvm_unreachable_internal("forming non-member reference to ivar?" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3138); | |||
3139 | ||||
3140 | // Enum constants are always r-values and never references. | |||
3141 | // Unresolved using declarations are dependent. | |||
3142 | case Decl::EnumConstant: | |||
3143 | case Decl::UnresolvedUsingValue: | |||
3144 | case Decl::OMPDeclareReduction: | |||
3145 | case Decl::OMPDeclareMapper: | |||
3146 | valueKind = VK_RValue; | |||
3147 | break; | |||
3148 | ||||
3149 | // Fields and indirect fields that got here must be for | |||
3150 | // pointer-to-member expressions; we just call them l-values for | |||
3151 | // internal consistency, because this subexpression doesn't really | |||
3152 | // exist in the high-level semantics. | |||
3153 | case Decl::Field: | |||
3154 | case Decl::IndirectField: | |||
3155 | case Decl::ObjCIvar: | |||
3156 | assert(getLangOpts().CPlusPlus &&((getLangOpts().CPlusPlus && "building reference to field in C?" ) ? static_cast<void> (0) : __assert_fail ("getLangOpts().CPlusPlus && \"building reference to field in C?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3157, __PRETTY_FUNCTION__)) | |||
3157 | "building reference to field in C?")((getLangOpts().CPlusPlus && "building reference to field in C?" ) ? static_cast<void> (0) : __assert_fail ("getLangOpts().CPlusPlus && \"building reference to field in C?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3157, __PRETTY_FUNCTION__)); | |||
3158 | ||||
3159 | // These can't have reference type in well-formed programs, but | |||
3160 | // for internal consistency we do this anyway. | |||
3161 | type = type.getNonReferenceType(); | |||
3162 | valueKind = VK_LValue; | |||
3163 | break; | |||
3164 | ||||
3165 | // Non-type template parameters are either l-values or r-values | |||
3166 | // depending on the type. | |||
3167 | case Decl::NonTypeTemplateParm: { | |||
3168 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { | |||
3169 | type = reftype->getPointeeType(); | |||
3170 | valueKind = VK_LValue; // even if the parameter is an r-value reference | |||
3171 | break; | |||
3172 | } | |||
3173 | ||||
3174 | // For non-references, we need to strip qualifiers just in case | |||
3175 | // the template parameter was declared as 'const int' or whatever. | |||
3176 | valueKind = VK_RValue; | |||
3177 | type = type.getUnqualifiedType(); | |||
3178 | break; | |||
3179 | } | |||
3180 | ||||
3181 | case Decl::Var: | |||
3182 | case Decl::VarTemplateSpecialization: | |||
3183 | case Decl::VarTemplatePartialSpecialization: | |||
3184 | case Decl::Decomposition: | |||
3185 | case Decl::OMPCapturedExpr: | |||
3186 | // In C, "extern void blah;" is valid and is an r-value. | |||
3187 | if (!getLangOpts().CPlusPlus && | |||
3188 | !type.hasQualifiers() && | |||
3189 | type->isVoidType()) { | |||
3190 | valueKind = VK_RValue; | |||
3191 | break; | |||
3192 | } | |||
3193 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
3194 | ||||
3195 | case Decl::ImplicitParam: | |||
3196 | case Decl::ParmVar: { | |||
3197 | // These are always l-values. | |||
3198 | valueKind = VK_LValue; | |||
3199 | type = type.getNonReferenceType(); | |||
3200 | ||||
3201 | // FIXME: Does the addition of const really only apply in | |||
3202 | // potentially-evaluated contexts? Since the variable isn't actually | |||
3203 | // captured in an unevaluated context, it seems that the answer is no. | |||
3204 | if (!isUnevaluatedContext()) { | |||
3205 | QualType CapturedType = getCapturedDeclRefType(cast<VarDecl>(VD), Loc); | |||
3206 | if (!CapturedType.isNull()) | |||
3207 | type = CapturedType; | |||
3208 | } | |||
3209 | ||||
3210 | break; | |||
3211 | } | |||
3212 | ||||
3213 | case Decl::Binding: { | |||
3214 | // These are always lvalues. | |||
3215 | valueKind = VK_LValue; | |||
3216 | type = type.getNonReferenceType(); | |||
3217 | // FIXME: Support lambda-capture of BindingDecls, once CWG actually | |||
3218 | // decides how that's supposed to work. | |||
3219 | auto *BD = cast<BindingDecl>(VD); | |||
3220 | if (BD->getDeclContext() != CurContext) { | |||
3221 | auto *DD = dyn_cast_or_null<VarDecl>(BD->getDecomposedDecl()); | |||
3222 | if (DD && DD->hasLocalStorage()) | |||
3223 | diagnoseUncapturableValueReference(*this, Loc, BD, CurContext); | |||
3224 | } | |||
3225 | break; | |||
3226 | } | |||
3227 | ||||
3228 | case Decl::Function: { | |||
3229 | if (unsigned BID = cast<FunctionDecl>(VD)->getBuiltinID()) { | |||
3230 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BID)) { | |||
3231 | type = Context.BuiltinFnTy; | |||
3232 | valueKind = VK_RValue; | |||
3233 | break; | |||
3234 | } | |||
3235 | } | |||
3236 | ||||
3237 | const FunctionType *fty = type->castAs<FunctionType>(); | |||
3238 | ||||
3239 | // If we're referring to a function with an __unknown_anytype | |||
3240 | // result type, make the entire expression __unknown_anytype. | |||
3241 | if (fty->getReturnType() == Context.UnknownAnyTy) { | |||
3242 | type = Context.UnknownAnyTy; | |||
3243 | valueKind = VK_RValue; | |||
3244 | break; | |||
3245 | } | |||
3246 | ||||
3247 | // Functions are l-values in C++. | |||
3248 | if (getLangOpts().CPlusPlus) { | |||
3249 | valueKind = VK_LValue; | |||
3250 | break; | |||
3251 | } | |||
3252 | ||||
3253 | // C99 DR 316 says that, if a function type comes from a | |||
3254 | // function definition (without a prototype), that type is only | |||
3255 | // used for checking compatibility. Therefore, when referencing | |||
3256 | // the function, we pretend that we don't have the full function | |||
3257 | // type. | |||
3258 | if (!cast<FunctionDecl>(VD)->hasPrototype() && | |||
3259 | isa<FunctionProtoType>(fty)) | |||
3260 | type = Context.getFunctionNoProtoType(fty->getReturnType(), | |||
3261 | fty->getExtInfo()); | |||
3262 | ||||
3263 | // Functions are r-values in C. | |||
3264 | valueKind = VK_RValue; | |||
3265 | break; | |||
3266 | } | |||
3267 | ||||
3268 | case Decl::CXXDeductionGuide: | |||
3269 | llvm_unreachable("building reference to deduction guide")::llvm::llvm_unreachable_internal("building reference to deduction guide" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3269); | |||
3270 | ||||
3271 | case Decl::MSProperty: | |||
3272 | valueKind = VK_LValue; | |||
3273 | break; | |||
3274 | ||||
3275 | case Decl::CXXMethod: | |||
3276 | // If we're referring to a method with an __unknown_anytype | |||
3277 | // result type, make the entire expression __unknown_anytype. | |||
3278 | // This should only be possible with a type written directly. | |||
3279 | if (const FunctionProtoType *proto | |||
3280 | = dyn_cast<FunctionProtoType>(VD->getType())) | |||
3281 | if (proto->getReturnType() == Context.UnknownAnyTy) { | |||
3282 | type = Context.UnknownAnyTy; | |||
3283 | valueKind = VK_RValue; | |||
3284 | break; | |||
3285 | } | |||
3286 | ||||
3287 | // C++ methods are l-values if static, r-values if non-static. | |||
3288 | if (cast<CXXMethodDecl>(VD)->isStatic()) { | |||
3289 | valueKind = VK_LValue; | |||
3290 | break; | |||
3291 | } | |||
3292 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
3293 | ||||
3294 | case Decl::CXXConversion: | |||
3295 | case Decl::CXXDestructor: | |||
3296 | case Decl::CXXConstructor: | |||
3297 | valueKind = VK_RValue; | |||
3298 | break; | |||
3299 | } | |||
3300 | ||||
3301 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS, FoundD, | |||
3302 | /*FIXME: TemplateKWLoc*/ SourceLocation(), | |||
3303 | TemplateArgs); | |||
3304 | } | |||
3305 | } | |||
3306 | ||||
3307 | static void ConvertUTF8ToWideString(unsigned CharByteWidth, StringRef Source, | |||
3308 | SmallString<32> &Target) { | |||
3309 | Target.resize(CharByteWidth * (Source.size() + 1)); | |||
3310 | char *ResultPtr = &Target[0]; | |||
3311 | const llvm::UTF8 *ErrorPtr; | |||
3312 | bool success = | |||
3313 | llvm::ConvertUTF8toWide(CharByteWidth, Source, ResultPtr, ErrorPtr); | |||
3314 | (void)success; | |||
3315 | assert(success)((success) ? static_cast<void> (0) : __assert_fail ("success" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3315, __PRETTY_FUNCTION__)); | |||
3316 | Target.resize(ResultPtr - &Target[0]); | |||
3317 | } | |||
3318 | ||||
3319 | ExprResult Sema::BuildPredefinedExpr(SourceLocation Loc, | |||
3320 | PredefinedExpr::IdentKind IK) { | |||
3321 | // Pick the current block, lambda, captured statement or function. | |||
3322 | Decl *currentDecl = nullptr; | |||
3323 | if (const BlockScopeInfo *BSI = getCurBlock()) | |||
3324 | currentDecl = BSI->TheDecl; | |||
3325 | else if (const LambdaScopeInfo *LSI = getCurLambda()) | |||
3326 | currentDecl = LSI->CallOperator; | |||
3327 | else if (const CapturedRegionScopeInfo *CSI = getCurCapturedRegion()) | |||
3328 | currentDecl = CSI->TheCapturedDecl; | |||
3329 | else | |||
3330 | currentDecl = getCurFunctionOrMethodDecl(); | |||
3331 | ||||
3332 | if (!currentDecl) { | |||
3333 | Diag(Loc, diag::ext_predef_outside_function); | |||
3334 | currentDecl = Context.getTranslationUnitDecl(); | |||
3335 | } | |||
3336 | ||||
3337 | QualType ResTy; | |||
3338 | StringLiteral *SL = nullptr; | |||
3339 | if (cast<DeclContext>(currentDecl)->isDependentContext()) | |||
3340 | ResTy = Context.DependentTy; | |||
3341 | else { | |||
3342 | // Pre-defined identifiers are of type char[x], where x is the length of | |||
3343 | // the string. | |||
3344 | auto Str = PredefinedExpr::ComputeName(IK, currentDecl); | |||
3345 | unsigned Length = Str.length(); | |||
3346 | ||||
3347 | llvm::APInt LengthI(32, Length + 1); | |||
3348 | if (IK == PredefinedExpr::LFunction || IK == PredefinedExpr::LFuncSig) { | |||
3349 | ResTy = | |||
3350 | Context.adjustStringLiteralBaseType(Context.WideCharTy.withConst()); | |||
3351 | SmallString<32> RawChars; | |||
3352 | ConvertUTF8ToWideString(Context.getTypeSizeInChars(ResTy).getQuantity(), | |||
3353 | Str, RawChars); | |||
3354 | ResTy = Context.getConstantArrayType(ResTy, LengthI, nullptr, | |||
3355 | ArrayType::Normal, | |||
3356 | /*IndexTypeQuals*/ 0); | |||
3357 | SL = StringLiteral::Create(Context, RawChars, StringLiteral::Wide, | |||
3358 | /*Pascal*/ false, ResTy, Loc); | |||
3359 | } else { | |||
3360 | ResTy = Context.adjustStringLiteralBaseType(Context.CharTy.withConst()); | |||
3361 | ResTy = Context.getConstantArrayType(ResTy, LengthI, nullptr, | |||
3362 | ArrayType::Normal, | |||
3363 | /*IndexTypeQuals*/ 0); | |||
3364 | SL = StringLiteral::Create(Context, Str, StringLiteral::Ascii, | |||
3365 | /*Pascal*/ false, ResTy, Loc); | |||
3366 | } | |||
3367 | } | |||
3368 | ||||
3369 | return PredefinedExpr::Create(Context, Loc, ResTy, IK, SL); | |||
3370 | } | |||
3371 | ||||
3372 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { | |||
3373 | PredefinedExpr::IdentKind IK; | |||
3374 | ||||
3375 | switch (Kind) { | |||
3376 | default: llvm_unreachable("Unknown simple primary expr!")::llvm::llvm_unreachable_internal("Unknown simple primary expr!" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3376); | |||
3377 | case tok::kw___func__: IK = PredefinedExpr::Func; break; // [C99 6.4.2.2] | |||
3378 | case tok::kw___FUNCTION__: IK = PredefinedExpr::Function; break; | |||
3379 | case tok::kw___FUNCDNAME__: IK = PredefinedExpr::FuncDName; break; // [MS] | |||
3380 | case tok::kw___FUNCSIG__: IK = PredefinedExpr::FuncSig; break; // [MS] | |||
3381 | case tok::kw_L__FUNCTION__: IK = PredefinedExpr::LFunction; break; // [MS] | |||
3382 | case tok::kw_L__FUNCSIG__: IK = PredefinedExpr::LFuncSig; break; // [MS] | |||
3383 | case tok::kw___PRETTY_FUNCTION__: IK = PredefinedExpr::PrettyFunction; break; | |||
3384 | } | |||
3385 | ||||
3386 | return BuildPredefinedExpr(Loc, IK); | |||
3387 | } | |||
3388 | ||||
3389 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok, Scope *UDLScope) { | |||
3390 | SmallString<16> CharBuffer; | |||
3391 | bool Invalid = false; | |||
3392 | StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); | |||
3393 | if (Invalid) | |||
3394 | return ExprError(); | |||
3395 | ||||
3396 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), | |||
3397 | PP, Tok.getKind()); | |||
3398 | if (Literal.hadError()) | |||
3399 | return ExprError(); | |||
3400 | ||||
3401 | QualType Ty; | |||
3402 | if (Literal.isWide()) | |||
3403 | Ty = Context.WideCharTy; // L'x' -> wchar_t in C and C++. | |||
3404 | else if (Literal.isUTF8() && getLangOpts().Char8) | |||
3405 | Ty = Context.Char8Ty; // u8'x' -> char8_t when it exists. | |||
3406 | else if (Literal.isUTF16()) | |||
3407 | Ty = Context.Char16Ty; // u'x' -> char16_t in C11 and C++11. | |||
3408 | else if (Literal.isUTF32()) | |||
3409 | Ty = Context.Char32Ty; // U'x' -> char32_t in C11 and C++11. | |||
3410 | else if (!getLangOpts().CPlusPlus || Literal.isMultiChar()) | |||
3411 | Ty = Context.IntTy; // 'x' -> int in C, 'wxyz' -> int in C++. | |||
3412 | else | |||
3413 | Ty = Context.CharTy; // 'x' -> char in C++ | |||
3414 | ||||
3415 | CharacterLiteral::CharacterKind Kind = CharacterLiteral::Ascii; | |||
3416 | if (Literal.isWide()) | |||
3417 | Kind = CharacterLiteral::Wide; | |||
3418 | else if (Literal.isUTF16()) | |||
3419 | Kind = CharacterLiteral::UTF16; | |||
3420 | else if (Literal.isUTF32()) | |||
3421 | Kind = CharacterLiteral::UTF32; | |||
3422 | else if (Literal.isUTF8()) | |||
3423 | Kind = CharacterLiteral::UTF8; | |||
3424 | ||||
3425 | Expr *Lit = new (Context) CharacterLiteral(Literal.getValue(), Kind, Ty, | |||
3426 | Tok.getLocation()); | |||
3427 | ||||
3428 | if (Literal.getUDSuffix().empty()) | |||
3429 | return Lit; | |||
3430 | ||||
3431 | // We're building a user-defined literal. | |||
3432 | IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); | |||
3433 | SourceLocation UDSuffixLoc = | |||
3434 | getUDSuffixLoc(*this, Tok.getLocation(), Literal.getUDSuffixOffset()); | |||
3435 | ||||
3436 | // Make sure we're allowed user-defined literals here. | |||
3437 | if (!UDLScope) | |||
3438 | return ExprError(Diag(UDSuffixLoc, diag::err_invalid_character_udl)); | |||
3439 | ||||
3440 | // C++11 [lex.ext]p6: The literal L is treated as a call of the form | |||
3441 | // operator "" X (ch) | |||
3442 | return BuildCookedLiteralOperatorCall(*this, UDLScope, UDSuffix, UDSuffixLoc, | |||
3443 | Lit, Tok.getLocation()); | |||
3444 | } | |||
3445 | ||||
3446 | ExprResult Sema::ActOnIntegerConstant(SourceLocation Loc, uint64_t Val) { | |||
3447 | unsigned IntSize = Context.getTargetInfo().getIntWidth(); | |||
3448 | return IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val), | |||
3449 | Context.IntTy, Loc); | |||
3450 | } | |||
3451 | ||||
3452 | static Expr *BuildFloatingLiteral(Sema &S, NumericLiteralParser &Literal, | |||
3453 | QualType Ty, SourceLocation Loc) { | |||
3454 | const llvm::fltSemantics &Format = S.Context.getFloatTypeSemantics(Ty); | |||
3455 | ||||
3456 | using llvm::APFloat; | |||
3457 | APFloat Val(Format); | |||
3458 | ||||
3459 | APFloat::opStatus result = Literal.GetFloatValue(Val); | |||
3460 | ||||
3461 | // Overflow is always an error, but underflow is only an error if | |||
3462 | // we underflowed to zero (APFloat reports denormals as underflow). | |||
3463 | if ((result & APFloat::opOverflow) || | |||
3464 | ((result & APFloat::opUnderflow) && Val.isZero())) { | |||
3465 | unsigned diagnostic; | |||
3466 | SmallString<20> buffer; | |||
3467 | if (result & APFloat::opOverflow) { | |||
3468 | diagnostic = diag::warn_float_overflow; | |||
3469 | APFloat::getLargest(Format).toString(buffer); | |||
3470 | } else { | |||
3471 | diagnostic = diag::warn_float_underflow; | |||
3472 | APFloat::getSmallest(Format).toString(buffer); | |||
3473 | } | |||
3474 | ||||
3475 | S.Diag(Loc, diagnostic) | |||
3476 | << Ty | |||
3477 | << StringRef(buffer.data(), buffer.size()); | |||
3478 | } | |||
3479 | ||||
3480 | bool isExact = (result == APFloat::opOK); | |||
3481 | return FloatingLiteral::Create(S.Context, Val, isExact, Ty, Loc); | |||
3482 | } | |||
3483 | ||||
3484 | bool Sema::CheckLoopHintExpr(Expr *E, SourceLocation Loc) { | |||
3485 | assert(E && "Invalid expression")((E && "Invalid expression") ? static_cast<void> (0) : __assert_fail ("E && \"Invalid expression\"", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3485, __PRETTY_FUNCTION__)); | |||
3486 | ||||
3487 | if (E->isValueDependent()) | |||
3488 | return false; | |||
3489 | ||||
3490 | QualType QT = E->getType(); | |||
3491 | if (!QT->isIntegerType() || QT->isBooleanType() || QT->isCharType()) { | |||
3492 | Diag(E->getExprLoc(), diag::err_pragma_loop_invalid_argument_type) << QT; | |||
3493 | return true; | |||
3494 | } | |||
3495 | ||||
3496 | llvm::APSInt ValueAPS; | |||
3497 | ExprResult R = VerifyIntegerConstantExpression(E, &ValueAPS); | |||
3498 | ||||
3499 | if (R.isInvalid()) | |||
3500 | return true; | |||
3501 | ||||
3502 | bool ValueIsPositive = ValueAPS.isStrictlyPositive(); | |||
3503 | if (!ValueIsPositive || ValueAPS.getActiveBits() > 31) { | |||
3504 | Diag(E->getExprLoc(), diag::err_pragma_loop_invalid_argument_value) | |||
3505 | << ValueAPS.toString(10) << ValueIsPositive; | |||
3506 | return true; | |||
3507 | } | |||
3508 | ||||
3509 | return false; | |||
3510 | } | |||
3511 | ||||
3512 | ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) { | |||
3513 | // Fast path for a single digit (which is quite common). A single digit | |||
3514 | // cannot have a trigraph, escaped newline, radix prefix, or suffix. | |||
3515 | if (Tok.getLength() == 1) { | |||
3516 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); | |||
3517 | return ActOnIntegerConstant(Tok.getLocation(), Val-'0'); | |||
3518 | } | |||
3519 | ||||
3520 | SmallString<128> SpellingBuffer; | |||
3521 | // NumericLiteralParser wants to overread by one character. Add padding to | |||
3522 | // the buffer in case the token is copied to the buffer. If getSpelling() | |||
3523 | // returns a StringRef to the memory buffer, it should have a null char at | |||
3524 | // the EOF, so it is also safe. | |||
3525 | SpellingBuffer.resize(Tok.getLength() + 1); | |||
3526 | ||||
3527 | // Get the spelling of the token, which eliminates trigraphs, etc. | |||
3528 | bool Invalid = false; | |||
3529 | StringRef TokSpelling = PP.getSpelling(Tok, SpellingBuffer, &Invalid); | |||
3530 | if (Invalid) | |||
3531 | return ExprError(); | |||
3532 | ||||
3533 | NumericLiteralParser Literal(TokSpelling, Tok.getLocation(), PP); | |||
3534 | if (Literal.hadError) | |||
3535 | return ExprError(); | |||
3536 | ||||
3537 | if (Literal.hasUDSuffix()) { | |||
3538 | // We're building a user-defined literal. | |||
3539 | IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); | |||
3540 | SourceLocation UDSuffixLoc = | |||
3541 | getUDSuffixLoc(*this, Tok.getLocation(), Literal.getUDSuffixOffset()); | |||
3542 | ||||
3543 | // Make sure we're allowed user-defined literals here. | |||
3544 | if (!UDLScope) | |||
3545 | return ExprError(Diag(UDSuffixLoc, diag::err_invalid_numeric_udl)); | |||
3546 | ||||
3547 | QualType CookedTy; | |||
3548 | if (Literal.isFloatingLiteral()) { | |||
3549 | // C++11 [lex.ext]p4: If S contains a literal operator with parameter type | |||
3550 | // long double, the literal is treated as a call of the form | |||
3551 | // operator "" X (f L) | |||
3552 | CookedTy = Context.LongDoubleTy; | |||
3553 | } else { | |||
3554 | // C++11 [lex.ext]p3: If S contains a literal operator with parameter type | |||
3555 | // unsigned long long, the literal is treated as a call of the form | |||
3556 | // operator "" X (n ULL) | |||
3557 | CookedTy = Context.UnsignedLongLongTy; | |||
3558 | } | |||
3559 | ||||
3560 | DeclarationName OpName = | |||
3561 | Context.DeclarationNames.getCXXLiteralOperatorName(UDSuffix); | |||
3562 | DeclarationNameInfo OpNameInfo(OpName, UDSuffixLoc); | |||
3563 | OpNameInfo.setCXXLiteralOperatorNameLoc(UDSuffixLoc); | |||
3564 | ||||
3565 | SourceLocation TokLoc = Tok.getLocation(); | |||
3566 | ||||
3567 | // Perform literal operator lookup to determine if we're building a raw | |||
3568 | // literal or a cooked one. | |||
3569 | LookupResult R(*this, OpName, UDSuffixLoc, LookupOrdinaryName); | |||
3570 | switch (LookupLiteralOperator(UDLScope, R, CookedTy, | |||
3571 | /*AllowRaw*/ true, /*AllowTemplate*/ true, | |||
3572 | /*AllowStringTemplate*/ false, | |||
3573 | /*DiagnoseMissing*/ !Literal.isImaginary)) { | |||
3574 | case LOLR_ErrorNoDiagnostic: | |||
3575 | // Lookup failure for imaginary constants isn't fatal, there's still the | |||
3576 | // GNU extension producing _Complex types. | |||
3577 | break; | |||
3578 | case LOLR_Error: | |||
3579 | return ExprError(); | |||
3580 | case LOLR_Cooked: { | |||
3581 | Expr *Lit; | |||
3582 | if (Literal.isFloatingLiteral()) { | |||
3583 | Lit = BuildFloatingLiteral(*this, Literal, CookedTy, Tok.getLocation()); | |||
3584 | } else { | |||
3585 | llvm::APInt ResultVal(Context.getTargetInfo().getLongLongWidth(), 0); | |||
3586 | if (Literal.GetIntegerValue(ResultVal)) | |||
3587 | Diag(Tok.getLocation(), diag::err_integer_literal_too_large) | |||
3588 | << /* Unsigned */ 1; | |||
3589 | Lit = IntegerLiteral::Create(Context, ResultVal, CookedTy, | |||
3590 | Tok.getLocation()); | |||
3591 | } | |||
3592 | return BuildLiteralOperatorCall(R, OpNameInfo, Lit, TokLoc); | |||
3593 | } | |||
3594 | ||||
3595 | case LOLR_Raw: { | |||
3596 | // C++11 [lit.ext]p3, p4: If S contains a raw literal operator, the | |||
3597 | // literal is treated as a call of the form | |||
3598 | // operator "" X ("n") | |||
3599 | unsigned Length = Literal.getUDSuffixOffset(); | |||
3600 | QualType StrTy = Context.getConstantArrayType( | |||
3601 | Context.adjustStringLiteralBaseType(Context.CharTy.withConst()), | |||
3602 | llvm::APInt(32, Length + 1), nullptr, ArrayType::Normal, 0); | |||
3603 | Expr *Lit = StringLiteral::Create( | |||
3604 | Context, StringRef(TokSpelling.data(), Length), StringLiteral::Ascii, | |||
3605 | /*Pascal*/false, StrTy, &TokLoc, 1); | |||
3606 | return BuildLiteralOperatorCall(R, OpNameInfo, Lit, TokLoc); | |||
3607 | } | |||
3608 | ||||
3609 | case LOLR_Template: { | |||
3610 | // C++11 [lit.ext]p3, p4: Otherwise (S contains a literal operator | |||
3611 | // template), L is treated as a call fo the form | |||
3612 | // operator "" X <'c1', 'c2', ... 'ck'>() | |||
3613 | // where n is the source character sequence c1 c2 ... ck. | |||
3614 | TemplateArgumentListInfo ExplicitArgs; | |||
3615 | unsigned CharBits = Context.getIntWidth(Context.CharTy); | |||
3616 | bool CharIsUnsigned = Context.CharTy->isUnsignedIntegerType(); | |||
3617 | llvm::APSInt Value(CharBits, CharIsUnsigned); | |||
3618 | for (unsigned I = 0, N = Literal.getUDSuffixOffset(); I != N; ++I) { | |||
3619 | Value = TokSpelling[I]; | |||
3620 | TemplateArgument Arg(Context, Value, Context.CharTy); | |||
3621 | TemplateArgumentLocInfo ArgInfo; | |||
3622 | ExplicitArgs.addArgument(TemplateArgumentLoc(Arg, ArgInfo)); | |||
3623 | } | |||
3624 | return BuildLiteralOperatorCall(R, OpNameInfo, None, TokLoc, | |||
3625 | &ExplicitArgs); | |||
3626 | } | |||
3627 | case LOLR_StringTemplate: | |||
3628 | llvm_unreachable("unexpected literal operator lookup result")::llvm::llvm_unreachable_internal("unexpected literal operator lookup result" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3628); | |||
3629 | } | |||
3630 | } | |||
3631 | ||||
3632 | Expr *Res; | |||
3633 | ||||
3634 | if (Literal.isFixedPointLiteral()) { | |||
3635 | QualType Ty; | |||
3636 | ||||
3637 | if (Literal.isAccum) { | |||
3638 | if (Literal.isHalf) { | |||
3639 | Ty = Context.ShortAccumTy; | |||
3640 | } else if (Literal.isLong) { | |||
3641 | Ty = Context.LongAccumTy; | |||
3642 | } else { | |||
3643 | Ty = Context.AccumTy; | |||
3644 | } | |||
3645 | } else if (Literal.isFract) { | |||
3646 | if (Literal.isHalf) { | |||
3647 | Ty = Context.ShortFractTy; | |||
3648 | } else if (Literal.isLong) { | |||
3649 | Ty = Context.LongFractTy; | |||
3650 | } else { | |||
3651 | Ty = Context.FractTy; | |||
3652 | } | |||
3653 | } | |||
3654 | ||||
3655 | if (Literal.isUnsigned) Ty = Context.getCorrespondingUnsignedType(Ty); | |||
3656 | ||||
3657 | bool isSigned = !Literal.isUnsigned; | |||
3658 | unsigned scale = Context.getFixedPointScale(Ty); | |||
3659 | unsigned bit_width = Context.getTypeInfo(Ty).Width; | |||
3660 | ||||
3661 | llvm::APInt Val(bit_width, 0, isSigned); | |||
3662 | bool Overflowed = Literal.GetFixedPointValue(Val, scale); | |||
3663 | bool ValIsZero = Val.isNullValue() && !Overflowed; | |||
3664 | ||||
3665 | auto MaxVal = Context.getFixedPointMax(Ty).getValue(); | |||
3666 | if (Literal.isFract && Val == MaxVal + 1 && !ValIsZero) | |||
3667 | // Clause 6.4.4 - The value of a constant shall be in the range of | |||
3668 | // representable values for its type, with exception for constants of a | |||
3669 | // fract type with a value of exactly 1; such a constant shall denote | |||
3670 | // the maximal value for the type. | |||
3671 | --Val; | |||
3672 | else if (Val.ugt(MaxVal) || Overflowed) | |||
3673 | Diag(Tok.getLocation(), diag::err_too_large_for_fixed_point); | |||
3674 | ||||
3675 | Res = FixedPointLiteral::CreateFromRawInt(Context, Val, Ty, | |||
3676 | Tok.getLocation(), scale); | |||
3677 | } else if (Literal.isFloatingLiteral()) { | |||
3678 | QualType Ty; | |||
3679 | if (Literal.isHalf){ | |||
3680 | if (getOpenCLOptions().isEnabled("cl_khr_fp16")) | |||
3681 | Ty = Context.HalfTy; | |||
3682 | else { | |||
3683 | Diag(Tok.getLocation(), diag::err_half_const_requires_fp16); | |||
3684 | return ExprError(); | |||
3685 | } | |||
3686 | } else if (Literal.isFloat) | |||
3687 | Ty = Context.FloatTy; | |||
3688 | else if (Literal.isLong) | |||
3689 | Ty = Context.LongDoubleTy; | |||
3690 | else if (Literal.isFloat16) | |||
3691 | Ty = Context.Float16Ty; | |||
3692 | else if (Literal.isFloat128) | |||
3693 | Ty = Context.Float128Ty; | |||
3694 | else | |||
3695 | Ty = Context.DoubleTy; | |||
3696 | ||||
3697 | Res = BuildFloatingLiteral(*this, Literal, Ty, Tok.getLocation()); | |||
3698 | ||||
3699 | if (Ty == Context.DoubleTy) { | |||
3700 | if (getLangOpts().SinglePrecisionConstants) { | |||
3701 | const BuiltinType *BTy = Ty->getAs<BuiltinType>(); | |||
3702 | if (BTy->getKind() != BuiltinType::Float) { | |||
3703 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).get(); | |||
3704 | } | |||
3705 | } else if (getLangOpts().OpenCL && | |||
3706 | !getOpenCLOptions().isEnabled("cl_khr_fp64")) { | |||
3707 | // Impose single-precision float type when cl_khr_fp64 is not enabled. | |||
3708 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); | |||
3709 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).get(); | |||
3710 | } | |||
3711 | } | |||
3712 | } else if (!Literal.isIntegerLiteral()) { | |||
3713 | return ExprError(); | |||
3714 | } else { | |||
3715 | QualType Ty; | |||
3716 | ||||
3717 | // 'long long' is a C99 or C++11 feature. | |||
3718 | if (!getLangOpts().C99 && Literal.isLongLong) { | |||
3719 | if (getLangOpts().CPlusPlus) | |||
3720 | Diag(Tok.getLocation(), | |||
3721 | getLangOpts().CPlusPlus11 ? | |||
3722 | diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong); | |||
3723 | else | |||
3724 | Diag(Tok.getLocation(), diag::ext_c99_longlong); | |||
3725 | } | |||
3726 | ||||
3727 | // Get the value in the widest-possible width. | |||
3728 | unsigned MaxWidth = Context.getTargetInfo().getIntMaxTWidth(); | |||
3729 | llvm::APInt ResultVal(MaxWidth, 0); | |||
3730 | ||||
3731 | if (Literal.GetIntegerValue(ResultVal)) { | |||
3732 | // If this value didn't fit into uintmax_t, error and force to ull. | |||
3733 | Diag(Tok.getLocation(), diag::err_integer_literal_too_large) | |||
3734 | << /* Unsigned */ 1; | |||
3735 | Ty = Context.UnsignedLongLongTy; | |||
3736 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() &&((Context.getTypeSize(Ty) == ResultVal.getBitWidth() && "long long is not intmax_t?") ? static_cast<void> (0) : __assert_fail ("Context.getTypeSize(Ty) == ResultVal.getBitWidth() && \"long long is not intmax_t?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3737, __PRETTY_FUNCTION__)) | |||
3737 | "long long is not intmax_t?")((Context.getTypeSize(Ty) == ResultVal.getBitWidth() && "long long is not intmax_t?") ? static_cast<void> (0) : __assert_fail ("Context.getTypeSize(Ty) == ResultVal.getBitWidth() && \"long long is not intmax_t?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3737, __PRETTY_FUNCTION__)); | |||
3738 | } else { | |||
3739 | // If this value fits into a ULL, try to figure out what else it fits into | |||
3740 | // according to the rules of C99 6.4.4.1p5. | |||
3741 | ||||
3742 | // Octal, Hexadecimal, and integers with a U suffix are allowed to | |||
3743 | // be an unsigned int. | |||
3744 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; | |||
3745 | ||||
3746 | // Check from smallest to largest, picking the smallest type we can. | |||
3747 | unsigned Width = 0; | |||
3748 | ||||
3749 | // Microsoft specific integer suffixes are explicitly sized. | |||
3750 | if (Literal.MicrosoftInteger) { | |||
3751 | if (Literal.MicrosoftInteger == 8 && !Literal.isUnsigned) { | |||
3752 | Width = 8; | |||
3753 | Ty = Context.CharTy; | |||
3754 | } else { | |||
3755 | Width = Literal.MicrosoftInteger; | |||
3756 | Ty = Context.getIntTypeForBitwidth(Width, | |||
3757 | /*Signed=*/!Literal.isUnsigned); | |||
3758 | } | |||
3759 | } | |||
3760 | ||||
3761 | if (Ty.isNull() && !Literal.isLong && !Literal.isLongLong) { | |||
3762 | // Are int/unsigned possibilities? | |||
3763 | unsigned IntSize = Context.getTargetInfo().getIntWidth(); | |||
3764 | ||||
3765 | // Does it fit in a unsigned int? | |||
3766 | if (ResultVal.isIntN(IntSize)) { | |||
3767 | // Does it fit in a signed int? | |||
3768 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) | |||
3769 | Ty = Context.IntTy; | |||
3770 | else if (AllowUnsigned) | |||
3771 | Ty = Context.UnsignedIntTy; | |||
3772 | Width = IntSize; | |||
3773 | } | |||
3774 | } | |||
3775 | ||||
3776 | // Are long/unsigned long possibilities? | |||
3777 | if (Ty.isNull() && !Literal.isLongLong) { | |||
3778 | unsigned LongSize = Context.getTargetInfo().getLongWidth(); | |||
3779 | ||||
3780 | // Does it fit in a unsigned long? | |||
3781 | if (ResultVal.isIntN(LongSize)) { | |||
3782 | // Does it fit in a signed long? | |||
3783 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) | |||
3784 | Ty = Context.LongTy; | |||
3785 | else if (AllowUnsigned) | |||
3786 | Ty = Context.UnsignedLongTy; | |||
3787 | // Check according to the rules of C90 6.1.3.2p5. C++03 [lex.icon]p2 | |||
3788 | // is compatible. | |||
3789 | else if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11) { | |||
3790 | const unsigned LongLongSize = | |||
3791 | Context.getTargetInfo().getLongLongWidth(); | |||
3792 | Diag(Tok.getLocation(), | |||
3793 | getLangOpts().CPlusPlus | |||
3794 | ? Literal.isLong | |||
3795 | ? diag::warn_old_implicitly_unsigned_long_cxx | |||
3796 | : /*C++98 UB*/ diag:: | |||
3797 | ext_old_implicitly_unsigned_long_cxx | |||
3798 | : diag::warn_old_implicitly_unsigned_long) | |||
3799 | << (LongLongSize > LongSize ? /*will have type 'long long'*/ 0 | |||
3800 | : /*will be ill-formed*/ 1); | |||
3801 | Ty = Context.UnsignedLongTy; | |||
3802 | } | |||
3803 | Width = LongSize; | |||
3804 | } | |||
3805 | } | |||
3806 | ||||
3807 | // Check long long if needed. | |||
3808 | if (Ty.isNull()) { | |||
3809 | unsigned LongLongSize = Context.getTargetInfo().getLongLongWidth(); | |||
3810 | ||||
3811 | // Does it fit in a unsigned long long? | |||
3812 | if (ResultVal.isIntN(LongLongSize)) { | |||
3813 | // Does it fit in a signed long long? | |||
3814 | // To be compatible with MSVC, hex integer literals ending with the | |||
3815 | // LL or i64 suffix are always signed in Microsoft mode. | |||
3816 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || | |||
3817 | (getLangOpts().MSVCCompat && Literal.isLongLong))) | |||
3818 | Ty = Context.LongLongTy; | |||
3819 | else if (AllowUnsigned) | |||
3820 | Ty = Context.UnsignedLongLongTy; | |||
3821 | Width = LongLongSize; | |||
3822 | } | |||
3823 | } | |||
3824 | ||||
3825 | // If we still couldn't decide a type, we probably have something that | |||
3826 | // does not fit in a signed long long, but has no U suffix. | |||
3827 | if (Ty.isNull()) { | |||
3828 | Diag(Tok.getLocation(), diag::ext_integer_literal_too_large_for_signed); | |||
3829 | Ty = Context.UnsignedLongLongTy; | |||
3830 | Width = Context.getTargetInfo().getLongLongWidth(); | |||
3831 | } | |||
3832 | ||||
3833 | if (ResultVal.getBitWidth() != Width) | |||
3834 | ResultVal = ResultVal.trunc(Width); | |||
3835 | } | |||
3836 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); | |||
3837 | } | |||
3838 | ||||
3839 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. | |||
3840 | if (Literal.isImaginary) { | |||
3841 | Res = new (Context) ImaginaryLiteral(Res, | |||
3842 | Context.getComplexType(Res->getType())); | |||
3843 | ||||
3844 | Diag(Tok.getLocation(), diag::ext_imaginary_constant); | |||
3845 | } | |||
3846 | return Res; | |||
3847 | } | |||
3848 | ||||
3849 | ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R, Expr *E) { | |||
3850 | assert(E && "ActOnParenExpr() missing expr")((E && "ActOnParenExpr() missing expr") ? static_cast <void> (0) : __assert_fail ("E && \"ActOnParenExpr() missing expr\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3850, __PRETTY_FUNCTION__)); | |||
3851 | return new (Context) ParenExpr(L, R, E); | |||
3852 | } | |||
3853 | ||||
3854 | static bool CheckVecStepTraitOperandType(Sema &S, QualType T, | |||
3855 | SourceLocation Loc, | |||
3856 | SourceRange ArgRange) { | |||
3857 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in | |||
3858 | // scalar or vector data type argument..." | |||
3859 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic | |||
3860 | // type (C99 6.2.5p18) or void. | |||
3861 | if (!(T->isArithmeticType() || T->isVoidType() || T->isVectorType())) { | |||
3862 | S.Diag(Loc, diag::err_vecstep_non_scalar_vector_type) | |||
3863 | << T << ArgRange; | |||
3864 | return true; | |||
3865 | } | |||
3866 | ||||
3867 | assert((T->isVoidType() || !T->isIncompleteType()) &&(((T->isVoidType() || !T->isIncompleteType()) && "Scalar types should always be complete") ? static_cast<void > (0) : __assert_fail ("(T->isVoidType() || !T->isIncompleteType()) && \"Scalar types should always be complete\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3868, __PRETTY_FUNCTION__)) | |||
3868 | "Scalar types should always be complete")(((T->isVoidType() || !T->isIncompleteType()) && "Scalar types should always be complete") ? static_cast<void > (0) : __assert_fail ("(T->isVoidType() || !T->isIncompleteType()) && \"Scalar types should always be complete\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3868, __PRETTY_FUNCTION__)); | |||
3869 | return false; | |||
3870 | } | |||
3871 | ||||
3872 | static bool CheckExtensionTraitOperandType(Sema &S, QualType T, | |||
3873 | SourceLocation Loc, | |||
3874 | SourceRange ArgRange, | |||
3875 | UnaryExprOrTypeTrait TraitKind) { | |||
3876 | // Invalid types must be hard errors for SFINAE in C++. | |||
3877 | if (S.LangOpts.CPlusPlus) | |||
3878 | return true; | |||
3879 | ||||
3880 | // C99 6.5.3.4p1: | |||
3881 | if (T->isFunctionType() && | |||
3882 | (TraitKind == UETT_SizeOf || TraitKind == UETT_AlignOf || | |||
3883 | TraitKind == UETT_PreferredAlignOf)) { | |||
3884 | // sizeof(function)/alignof(function) is allowed as an extension. | |||
3885 | S.Diag(Loc, diag::ext_sizeof_alignof_function_type) | |||
3886 | << TraitKind << ArgRange; | |||
3887 | return false; | |||
3888 | } | |||
3889 | ||||
3890 | // Allow sizeof(void)/alignof(void) as an extension, unless in OpenCL where | |||
3891 | // this is an error (OpenCL v1.1 s6.3.k) | |||
3892 | if (T->isVoidType()) { | |||
3893 | unsigned DiagID = S.LangOpts.OpenCL ? diag::err_opencl_sizeof_alignof_type | |||
3894 | : diag::ext_sizeof_alignof_void_type; | |||
3895 | S.Diag(Loc, DiagID) << TraitKind << ArgRange; | |||
3896 | return false; | |||
3897 | } | |||
3898 | ||||
3899 | return true; | |||
3900 | } | |||
3901 | ||||
3902 | static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T, | |||
3903 | SourceLocation Loc, | |||
3904 | SourceRange ArgRange, | |||
3905 | UnaryExprOrTypeTrait TraitKind) { | |||
3906 | // Reject sizeof(interface) and sizeof(interface<proto>) if the | |||
3907 | // runtime doesn't allow it. | |||
3908 | if (!S.LangOpts.ObjCRuntime.allowsSizeofAlignof() && T->isObjCObjectType()) { | |||
3909 | S.Diag(Loc, diag::err_sizeof_nonfragile_interface) | |||
3910 | << T << (TraitKind == UETT_SizeOf) | |||
3911 | << ArgRange; | |||
3912 | return true; | |||
3913 | } | |||
3914 | ||||
3915 | return false; | |||
3916 | } | |||
3917 | ||||
3918 | /// Check whether E is a pointer from a decayed array type (the decayed | |||
3919 | /// pointer type is equal to T) and emit a warning if it is. | |||
3920 | static void warnOnSizeofOnArrayDecay(Sema &S, SourceLocation Loc, QualType T, | |||
3921 | Expr *E) { | |||
3922 | // Don't warn if the operation changed the type. | |||
3923 | if (T != E->getType()) | |||
3924 | return; | |||
3925 | ||||
3926 | // Now look for array decays. | |||
3927 | ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E); | |||
3928 | if (!ICE || ICE->getCastKind() != CK_ArrayToPointerDecay) | |||
3929 | return; | |||
3930 | ||||
3931 | S.Diag(Loc, diag::warn_sizeof_array_decay) << ICE->getSourceRange() | |||
3932 | << ICE->getType() | |||
3933 | << ICE->getSubExpr()->getType(); | |||
3934 | } | |||
3935 | ||||
3936 | /// Check the constraints on expression operands to unary type expression | |||
3937 | /// and type traits. | |||
3938 | /// | |||
3939 | /// Completes any types necessary and validates the constraints on the operand | |||
3940 | /// expression. The logic mostly mirrors the type-based overload, but may modify | |||
3941 | /// the expression as it completes the type for that expression through template | |||
3942 | /// instantiation, etc. | |||
3943 | bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *E, | |||
3944 | UnaryExprOrTypeTrait ExprKind) { | |||
3945 | QualType ExprTy = E->getType(); | |||
3946 | assert(!ExprTy->isReferenceType())((!ExprTy->isReferenceType()) ? static_cast<void> (0 ) : __assert_fail ("!ExprTy->isReferenceType()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3946, __PRETTY_FUNCTION__)); | |||
3947 | ||||
3948 | bool IsUnevaluatedOperand = | |||
3949 | (ExprKind == UETT_SizeOf || ExprKind == UETT_AlignOf || | |||
3950 | ExprKind == UETT_PreferredAlignOf); | |||
3951 | if (IsUnevaluatedOperand) { | |||
3952 | ExprResult Result = CheckUnevaluatedOperand(E); | |||
3953 | if (Result.isInvalid()) | |||
3954 | return true; | |||
3955 | E = Result.get(); | |||
3956 | } | |||
3957 | ||||
3958 | if (ExprKind == UETT_VecStep) | |||
3959 | return CheckVecStepTraitOperandType(*this, ExprTy, E->getExprLoc(), | |||
3960 | E->getSourceRange()); | |||
3961 | ||||
3962 | // Whitelist some types as extensions | |||
3963 | if (!CheckExtensionTraitOperandType(*this, ExprTy, E->getExprLoc(), | |||
3964 | E->getSourceRange(), ExprKind)) | |||
3965 | return false; | |||
3966 | ||||
3967 | // 'alignof' applied to an expression only requires the base element type of | |||
3968 | // the expression to be complete. 'sizeof' requires the expression's type to | |||
3969 | // be complete (and will attempt to complete it if it's an array of unknown | |||
3970 | // bound). | |||
3971 | if (ExprKind == UETT_AlignOf || ExprKind == UETT_PreferredAlignOf) { | |||
3972 | if (RequireCompleteType(E->getExprLoc(), | |||
3973 | Context.getBaseElementType(E->getType()), | |||
3974 | diag::err_sizeof_alignof_incomplete_type, ExprKind, | |||
3975 | E->getSourceRange())) | |||
3976 | return true; | |||
3977 | } else { | |||
3978 | if (RequireCompleteExprType(E, diag::err_sizeof_alignof_incomplete_type, | |||
3979 | ExprKind, E->getSourceRange())) | |||
3980 | return true; | |||
3981 | } | |||
3982 | ||||
3983 | // Completing the expression's type may have changed it. | |||
3984 | ExprTy = E->getType(); | |||
3985 | assert(!ExprTy->isReferenceType())((!ExprTy->isReferenceType()) ? static_cast<void> (0 ) : __assert_fail ("!ExprTy->isReferenceType()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 3985, __PRETTY_FUNCTION__)); | |||
3986 | ||||
3987 | if (ExprTy->isFunctionType()) { | |||
3988 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_function_type) | |||
3989 | << ExprKind << E->getSourceRange(); | |||
3990 | return true; | |||
3991 | } | |||
3992 | ||||
3993 | // The operand for sizeof and alignof is in an unevaluated expression context, | |||
3994 | // so side effects could result in unintended consequences. | |||
3995 | if (IsUnevaluatedOperand && !inTemplateInstantiation() && | |||
3996 | E->HasSideEffects(Context, false)) | |||
3997 | Diag(E->getExprLoc(), diag::warn_side_effects_unevaluated_context); | |||
3998 | ||||
3999 | if (CheckObjCTraitOperandConstraints(*this, ExprTy, E->getExprLoc(), | |||
4000 | E->getSourceRange(), ExprKind)) | |||
4001 | return true; | |||
4002 | ||||
4003 | if (ExprKind == UETT_SizeOf) { | |||
4004 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E->IgnoreParens())) { | |||
4005 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) { | |||
4006 | QualType OType = PVD->getOriginalType(); | |||
4007 | QualType Type = PVD->getType(); | |||
4008 | if (Type->isPointerType() && OType->isArrayType()) { | |||
4009 | Diag(E->getExprLoc(), diag::warn_sizeof_array_param) | |||
4010 | << Type << OType; | |||
4011 | Diag(PVD->getLocation(), diag::note_declared_at); | |||
4012 | } | |||
4013 | } | |||
4014 | } | |||
4015 | ||||
4016 | // Warn on "sizeof(array op x)" and "sizeof(x op array)", where the array | |||
4017 | // decays into a pointer and returns an unintended result. This is most | |||
4018 | // likely a typo for "sizeof(array) op x". | |||
4019 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E->IgnoreParens())) { | |||
4020 | warnOnSizeofOnArrayDecay(*this, BO->getOperatorLoc(), BO->getType(), | |||
4021 | BO->getLHS()); | |||
4022 | warnOnSizeofOnArrayDecay(*this, BO->getOperatorLoc(), BO->getType(), | |||
4023 | BO->getRHS()); | |||
4024 | } | |||
4025 | } | |||
4026 | ||||
4027 | return false; | |||
4028 | } | |||
4029 | ||||
4030 | /// Check the constraints on operands to unary expression and type | |||
4031 | /// traits. | |||
4032 | /// | |||
4033 | /// This will complete any types necessary, and validate the various constraints | |||
4034 | /// on those operands. | |||
4035 | /// | |||
4036 | /// The UsualUnaryConversions() function is *not* called by this routine. | |||
4037 | /// C99 6.3.2.1p[2-4] all state: | |||
4038 | /// Except when it is the operand of the sizeof operator ... | |||
4039 | /// | |||
4040 | /// C++ [expr.sizeof]p4 | |||
4041 | /// The lvalue-to-rvalue, array-to-pointer, and function-to-pointer | |||
4042 | /// standard conversions are not applied to the operand of sizeof. | |||
4043 | /// | |||
4044 | /// This policy is followed for all of the unary trait expressions. | |||
4045 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType, | |||
4046 | SourceLocation OpLoc, | |||
4047 | SourceRange ExprRange, | |||
4048 | UnaryExprOrTypeTrait ExprKind) { | |||
4049 | if (ExprType->isDependentType()) | |||
4050 | return false; | |||
4051 | ||||
4052 | // C++ [expr.sizeof]p2: | |||
4053 | // When applied to a reference or a reference type, the result | |||
4054 | // is the size of the referenced type. | |||
4055 | // C++11 [expr.alignof]p3: | |||
4056 | // When alignof is applied to a reference type, the result | |||
4057 | // shall be the alignment of the referenced type. | |||
4058 | if (const ReferenceType *Ref = ExprType->getAs<ReferenceType>()) | |||
4059 | ExprType = Ref->getPointeeType(); | |||
4060 | ||||
4061 | // C11 6.5.3.4/3, C++11 [expr.alignof]p3: | |||
4062 | // When alignof or _Alignof is applied to an array type, the result | |||
4063 | // is the alignment of the element type. | |||
4064 | if (ExprKind == UETT_AlignOf || ExprKind == UETT_PreferredAlignOf || | |||
4065 | ExprKind == UETT_OpenMPRequiredSimdAlign) | |||
4066 | ExprType = Context.getBaseElementType(ExprType); | |||
4067 | ||||
4068 | if (ExprKind == UETT_VecStep) | |||
4069 | return CheckVecStepTraitOperandType(*this, ExprType, OpLoc, ExprRange); | |||
4070 | ||||
4071 | // Whitelist some types as extensions | |||
4072 | if (!CheckExtensionTraitOperandType(*this, ExprType, OpLoc, ExprRange, | |||
4073 | ExprKind)) | |||
4074 | return false; | |||
4075 | ||||
4076 | if (RequireCompleteType(OpLoc, ExprType, | |||
4077 | diag::err_sizeof_alignof_incomplete_type, | |||
4078 | ExprKind, ExprRange)) | |||
4079 | return true; | |||
4080 | ||||
4081 | if (ExprType->isFunctionType()) { | |||
4082 | Diag(OpLoc, diag::err_sizeof_alignof_function_type) | |||
4083 | << ExprKind << ExprRange; | |||
4084 | return true; | |||
4085 | } | |||
4086 | ||||
4087 | if (CheckObjCTraitOperandConstraints(*this, ExprType, OpLoc, ExprRange, | |||
4088 | ExprKind)) | |||
4089 | return true; | |||
4090 | ||||
4091 | return false; | |||
4092 | } | |||
4093 | ||||
4094 | static bool CheckAlignOfExpr(Sema &S, Expr *E, UnaryExprOrTypeTrait ExprKind) { | |||
4095 | // Cannot know anything else if the expression is dependent. | |||
4096 | if (E->isTypeDependent()) | |||
4097 | return false; | |||
4098 | ||||
4099 | if (E->getObjectKind() == OK_BitField) { | |||
4100 | S.Diag(E->getExprLoc(), diag::err_sizeof_alignof_typeof_bitfield) | |||
4101 | << 1 << E->getSourceRange(); | |||
4102 | return true; | |||
4103 | } | |||
4104 | ||||
4105 | ValueDecl *D = nullptr; | |||
4106 | Expr *Inner = E->IgnoreParens(); | |||
4107 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Inner)) { | |||
4108 | D = DRE->getDecl(); | |||
4109 | } else if (MemberExpr *ME = dyn_cast<MemberExpr>(Inner)) { | |||
4110 | D = ME->getMemberDecl(); | |||
4111 | } | |||
4112 | ||||
4113 | // If it's a field, require the containing struct to have a | |||
4114 | // complete definition so that we can compute the layout. | |||
4115 | // | |||
4116 | // This can happen in C++11 onwards, either by naming the member | |||
4117 | // in a way that is not transformed into a member access expression | |||
4118 | // (in an unevaluated operand, for instance), or by naming the member | |||
4119 | // in a trailing-return-type. | |||
4120 | // | |||
4121 | // For the record, since __alignof__ on expressions is a GCC | |||
4122 | // extension, GCC seems to permit this but always gives the | |||
4123 | // nonsensical answer 0. | |||
4124 | // | |||
4125 | // We don't really need the layout here --- we could instead just | |||
4126 | // directly check for all the appropriate alignment-lowing | |||
4127 | // attributes --- but that would require duplicating a lot of | |||
4128 | // logic that just isn't worth duplicating for such a marginal | |||
4129 | // use-case. | |||
4130 | if (FieldDecl *FD = dyn_cast_or_null<FieldDecl>(D)) { | |||
4131 | // Fast path this check, since we at least know the record has a | |||
4132 | // definition if we can find a member of it. | |||
4133 | if (!FD->getParent()->isCompleteDefinition()) { | |||
4134 | S.Diag(E->getExprLoc(), diag::err_alignof_member_of_incomplete_type) | |||
4135 | << E->getSourceRange(); | |||
4136 | return true; | |||
4137 | } | |||
4138 | ||||
4139 | // Otherwise, if it's a field, and the field doesn't have | |||
4140 | // reference type, then it must have a complete type (or be a | |||
4141 | // flexible array member, which we explicitly want to | |||
4142 | // white-list anyway), which makes the following checks trivial. | |||
4143 | if (!FD->getType()->isReferenceType()) | |||
4144 | return false; | |||
4145 | } | |||
4146 | ||||
4147 | return S.CheckUnaryExprOrTypeTraitOperand(E, ExprKind); | |||
4148 | } | |||
4149 | ||||
4150 | bool Sema::CheckVecStepExpr(Expr *E) { | |||
4151 | E = E->IgnoreParens(); | |||
4152 | ||||
4153 | // Cannot know anything else if the expression is dependent. | |||
4154 | if (E->isTypeDependent()) | |||
4155 | return false; | |||
4156 | ||||
4157 | return CheckUnaryExprOrTypeTraitOperand(E, UETT_VecStep); | |||
4158 | } | |||
4159 | ||||
4160 | static void captureVariablyModifiedType(ASTContext &Context, QualType T, | |||
4161 | CapturingScopeInfo *CSI) { | |||
4162 | assert(T->isVariablyModifiedType())((T->isVariablyModifiedType()) ? static_cast<void> ( 0) : __assert_fail ("T->isVariablyModifiedType()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 4162, __PRETTY_FUNCTION__)); | |||
4163 | assert(CSI != nullptr)((CSI != nullptr) ? static_cast<void> (0) : __assert_fail ("CSI != nullptr", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 4163, __PRETTY_FUNCTION__)); | |||
4164 | ||||
4165 | // We're going to walk down into the type and look for VLA expressions. | |||
4166 | do { | |||
4167 | const Type *Ty = T.getTypePtr(); | |||
4168 | switch (Ty->getTypeClass()) { | |||
4169 | #define TYPE(Class, Base) | |||
4170 | #define ABSTRACT_TYPE(Class, Base) | |||
4171 | #define NON_CANONICAL_TYPE(Class, Base) | |||
4172 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: | |||
4173 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) | |||
4174 | #include "clang/AST/TypeNodes.inc" | |||
4175 | T = QualType(); | |||
4176 | break; | |||
4177 | // These types are never variably-modified. | |||
4178 | case Type::Builtin: | |||
4179 | case Type::Complex: | |||
4180 | case Type::Vector: | |||
4181 | case Type::ExtVector: | |||
4182 | case Type::Record: | |||
4183 | case Type::Enum: | |||
4184 | case Type::Elaborated: | |||
4185 | case Type::TemplateSpecialization: | |||
4186 | case Type::ObjCObject: | |||
4187 | case Type::ObjCInterface: | |||
4188 | case Type::ObjCObjectPointer: | |||
4189 | case Type::ObjCTypeParam: | |||
4190 | case Type::Pipe: | |||
4191 | llvm_unreachable("type class is never variably-modified!")::llvm::llvm_unreachable_internal("type class is never variably-modified!" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 4191); | |||
4192 | case Type::Adjusted: | |||
4193 | T = cast<AdjustedType>(Ty)->getOriginalType(); | |||
4194 | break; | |||
4195 | case Type::Decayed: | |||
4196 | T = cast<DecayedType>(Ty)->getPointeeType(); | |||
4197 | break; | |||
4198 | case Type::Pointer: | |||
4199 | T = cast<PointerType>(Ty)->getPointeeType(); | |||
4200 | break; | |||
4201 | case Type::BlockPointer: | |||
4202 | T = cast<BlockPointerType>(Ty)->getPointeeType(); | |||
4203 | break; | |||
4204 | case Type::LValueReference: | |||
4205 | case Type::RValueReference: | |||
4206 | T = cast<ReferenceType>(Ty)->getPointeeType(); | |||
4207 | break; | |||
4208 | case Type::MemberPointer: | |||
4209 | T = cast<MemberPointerType>(Ty)->getPointeeType(); | |||
4210 | break; | |||
4211 | case Type::ConstantArray: | |||
4212 | case Type::IncompleteArray: | |||
4213 | // Losing element qualification here is fine. | |||
4214 | T = cast<ArrayType>(Ty)->getElementType(); | |||
4215 | break; | |||
4216 | case Type::VariableArray: { | |||
4217 | // Losing element qualification here is fine. | |||
4218 | const VariableArrayType *VAT = cast<VariableArrayType>(Ty); | |||
4219 | ||||
4220 | // Unknown size indication requires no size computation. | |||
4221 | // Otherwise, evaluate and record it. | |||
4222 | auto Size = VAT->getSizeExpr(); | |||
4223 | if (Size && !CSI->isVLATypeCaptured(VAT) && | |||
4224 | (isa<CapturedRegionScopeInfo>(CSI) || isa<LambdaScopeInfo>(CSI))) | |||
4225 | CSI->addVLATypeCapture(Size->getExprLoc(), VAT, Context.getSizeType()); | |||
4226 | ||||
4227 | T = VAT->getElementType(); | |||
4228 | break; | |||
4229 | } | |||
4230 | case Type::FunctionProto: | |||
4231 | case Type::FunctionNoProto: | |||
4232 | T = cast<FunctionType>(Ty)->getReturnType(); | |||
4233 | break; | |||
4234 | case Type::Paren: | |||
4235 | case Type::TypeOf: | |||
4236 | case Type::UnaryTransform: | |||
4237 | case Type::Attributed: | |||
4238 | case Type::SubstTemplateTypeParm: | |||
4239 | case Type::PackExpansion: | |||
4240 | case Type::MacroQualified: | |||
4241 | // Keep walking after single level desugaring. | |||
4242 | T = T.getSingleStepDesugaredType(Context); | |||
4243 | break; | |||
4244 | case Type::Typedef: | |||
4245 | T = cast<TypedefType>(Ty)->desugar(); | |||
4246 | break; | |||
4247 | case Type::Decltype: | |||
4248 | T = cast<DecltypeType>(Ty)->desugar(); | |||
4249 | break; | |||
4250 | case Type::Auto: | |||
4251 | case Type::DeducedTemplateSpecialization: | |||
4252 | T = cast<DeducedType>(Ty)->getDeducedType(); | |||
4253 | break; | |||
4254 | case Type::TypeOfExpr: | |||
4255 | T = cast<TypeOfExprType>(Ty)->getUnderlyingExpr()->getType(); | |||
4256 | break; | |||
4257 | case Type::Atomic: | |||
4258 | T = cast<AtomicType>(Ty)->getValueType(); | |||
4259 | break; | |||
4260 | } | |||
4261 | } while (!T.isNull() && T->isVariablyModifiedType()); | |||
4262 | } | |||
4263 | ||||
4264 | /// Build a sizeof or alignof expression given a type operand. | |||
4265 | ExprResult | |||
4266 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, | |||
4267 | SourceLocation OpLoc, | |||
4268 | UnaryExprOrTypeTrait ExprKind, | |||
4269 | SourceRange R) { | |||
4270 | if (!TInfo) | |||
4271 | return ExprError(); | |||
4272 | ||||
4273 | QualType T = TInfo->getType(); | |||
4274 | ||||
4275 | if (!T->isDependentType() && | |||
4276 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) | |||
4277 | return ExprError(); | |||
4278 | ||||
4279 | if (T->isVariablyModifiedType() && FunctionScopes.size() > 1) { | |||
4280 | if (auto *TT = T->getAs<TypedefType>()) { | |||
4281 | for (auto I = FunctionScopes.rbegin(), | |||
4282 | E = std::prev(FunctionScopes.rend()); | |||
4283 | I != E; ++I) { | |||
4284 | auto *CSI = dyn_cast<CapturingScopeInfo>(*I); | |||
4285 | if (CSI == nullptr) | |||
4286 | break; | |||
4287 | DeclContext *DC = nullptr; | |||
4288 | if (auto *LSI = dyn_cast<LambdaScopeInfo>(CSI)) | |||
4289 | DC = LSI->CallOperator; | |||
4290 | else if (auto *CRSI = dyn_cast<CapturedRegionScopeInfo>(CSI)) | |||
4291 | DC = CRSI->TheCapturedDecl; | |||
4292 | else if (auto *BSI = dyn_cast<BlockScopeInfo>(CSI)) | |||
4293 | DC = BSI->TheDecl; | |||
4294 | if (DC) { | |||
4295 | if (DC->containsDecl(TT->getDecl())) | |||
4296 | break; | |||
4297 | captureVariablyModifiedType(Context, T, CSI); | |||
4298 | } | |||
4299 | } | |||
4300 | } | |||
4301 | } | |||
4302 | ||||
4303 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. | |||
4304 | return new (Context) UnaryExprOrTypeTraitExpr( | |||
4305 | ExprKind, TInfo, Context.getSizeType(), OpLoc, R.getEnd()); | |||
4306 | } | |||
4307 | ||||
4308 | /// Build a sizeof or alignof expression given an expression | |||
4309 | /// operand. | |||
4310 | ExprResult | |||
4311 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, | |||
4312 | UnaryExprOrTypeTrait ExprKind) { | |||
4313 | ExprResult PE = CheckPlaceholderExpr(E); | |||
4314 | if (PE.isInvalid()) | |||
4315 | return ExprError(); | |||
4316 | ||||
4317 | E = PE.get(); | |||
4318 | ||||
4319 | // Verify that the operand is valid. | |||
4320 | bool isInvalid = false; | |||
4321 | if (E->isTypeDependent()) { | |||
4322 | // Delay type-checking for type-dependent expressions. | |||
4323 | } else if (ExprKind == UETT_AlignOf || ExprKind == UETT_PreferredAlignOf) { | |||
4324 | isInvalid = CheckAlignOfExpr(*this, E, ExprKind); | |||
4325 | } else if (ExprKind == UETT_VecStep) { | |||
4326 | isInvalid = CheckVecStepExpr(E); | |||
4327 | } else if (ExprKind == UETT_OpenMPRequiredSimdAlign) { | |||
4328 | Diag(E->getExprLoc(), diag::err_openmp_default_simd_align_expr); | |||
4329 | isInvalid = true; | |||
4330 | } else if (E->refersToBitField()) { // C99 6.5.3.4p1. | |||
4331 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_typeof_bitfield) << 0; | |||
4332 | isInvalid = true; | |||
4333 | } else { | |||
4334 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_SizeOf); | |||
4335 | } | |||
4336 | ||||
4337 | if (isInvalid) | |||
4338 | return ExprError(); | |||
4339 | ||||
4340 | if (ExprKind == UETT_SizeOf && E->getType()->isVariableArrayType()) { | |||
4341 | PE = TransformToPotentiallyEvaluated(E); | |||
4342 | if (PE.isInvalid()) return ExprError(); | |||
4343 | E = PE.get(); | |||
4344 | } | |||
4345 | ||||
4346 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. | |||
4347 | return new (Context) UnaryExprOrTypeTraitExpr( | |||
4348 | ExprKind, E, Context.getSizeType(), OpLoc, E->getSourceRange().getEnd()); | |||
4349 | } | |||
4350 | ||||
4351 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c | |||
4352 | /// expr and the same for @c alignof and @c __alignof | |||
4353 | /// Note that the ArgRange is invalid if isType is false. | |||
4354 | ExprResult | |||
4355 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, | |||
4356 | UnaryExprOrTypeTrait ExprKind, bool IsType, | |||
4357 | void *TyOrEx, SourceRange ArgRange) { | |||
4358 | // If error parsing type, ignore. | |||
4359 | if (!TyOrEx) return ExprError(); | |||
4360 | ||||
4361 | if (IsType) { | |||
4362 | TypeSourceInfo *TInfo; | |||
4363 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); | |||
4364 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); | |||
4365 | } | |||
4366 | ||||
4367 | Expr *ArgEx = (Expr *)TyOrEx; | |||
4368 | ExprResult Result = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind); | |||
4369 | return Result; | |||
4370 | } | |||
4371 | ||||
4372 | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, | |||
4373 | bool IsReal) { | |||
4374 | if (V.get()->isTypeDependent()) | |||
4375 | return S.Context.DependentTy; | |||
4376 | ||||
4377 | // _Real and _Imag are only l-values for normal l-values. | |||
4378 | if (V.get()->getObjectKind() != OK_Ordinary) { | |||
4379 | V = S.DefaultLvalueConversion(V.get()); | |||
4380 | if (V.isInvalid()) | |||
4381 | return QualType(); | |||
4382 | } | |||
4383 | ||||
4384 | // These operators return the element type of a complex type. | |||
4385 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) | |||
4386 | return CT->getElementType(); | |||
4387 | ||||
4388 | // Otherwise they pass through real integer and floating point types here. | |||
4389 | if (V.get()->getType()->isArithmeticType()) | |||
4390 | return V.get()->getType(); | |||
4391 | ||||
4392 | // Test for placeholders. | |||
4393 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); | |||
4394 | if (PR.isInvalid()) return QualType(); | |||
4395 | if (PR.get() != V.get()) { | |||
4396 | V = PR; | |||
4397 | return CheckRealImagOperand(S, V, Loc, IsReal); | |||
4398 | } | |||
4399 | ||||
4400 | // Reject anything else. | |||
4401 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() | |||
4402 | << (IsReal ? "__real" : "__imag"); | |||
4403 | return QualType(); | |||
4404 | } | |||
4405 | ||||
4406 | ||||
4407 | ||||
4408 | ExprResult | |||
4409 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, | |||
4410 | tok::TokenKind Kind, Expr *Input) { | |||
4411 | UnaryOperatorKind Opc; | |||
4412 | switch (Kind) { | |||
4413 | default: llvm_unreachable("Unknown unary op!")::llvm::llvm_unreachable_internal("Unknown unary op!", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 4413); | |||
4414 | case tok::plusplus: Opc = UO_PostInc; break; | |||
4415 | case tok::minusminus: Opc = UO_PostDec; break; | |||
4416 | } | |||
4417 | ||||
4418 | // Since this might is a postfix expression, get rid of ParenListExprs. | |||
4419 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Input); | |||
4420 | if (Result.isInvalid()) return ExprError(); | |||
4421 | Input = Result.get(); | |||
4422 | ||||
4423 | return BuildUnaryOp(S, OpLoc, Opc, Input); | |||
4424 | } | |||
4425 | ||||
4426 | /// Diagnose if arithmetic on the given ObjC pointer is illegal. | |||
4427 | /// | |||
4428 | /// \return true on error | |||
4429 | static bool checkArithmeticOnObjCPointer(Sema &S, | |||
4430 | SourceLocation opLoc, | |||
4431 | Expr *op) { | |||
4432 | assert(op->getType()->isObjCObjectPointerType())((op->getType()->isObjCObjectPointerType()) ? static_cast <void> (0) : __assert_fail ("op->getType()->isObjCObjectPointerType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 4432, __PRETTY_FUNCTION__)); | |||
4433 | if (S.LangOpts.ObjCRuntime.allowsPointerArithmetic() && | |||
4434 | !S.LangOpts.ObjCSubscriptingLegacyRuntime) | |||
4435 | return false; | |||
4436 | ||||
4437 | S.Diag(opLoc, diag::err_arithmetic_nonfragile_interface) | |||
4438 | << op->getType()->castAs<ObjCObjectPointerType>()->getPointeeType() | |||
4439 | << op->getSourceRange(); | |||
4440 | return true; | |||
4441 | } | |||
4442 | ||||
4443 | static bool isMSPropertySubscriptExpr(Sema &S, Expr *Base) { | |||
4444 | auto *BaseNoParens = Base->IgnoreParens(); | |||
4445 | if (auto *MSProp = dyn_cast<MSPropertyRefExpr>(BaseNoParens)) | |||
4446 | return MSProp->getPropertyDecl()->getType()->isArrayType(); | |||
4447 | return isa<MSPropertySubscriptExpr>(BaseNoParens); | |||
4448 | } | |||
4449 | ||||
4450 | ExprResult | |||
4451 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *base, SourceLocation lbLoc, | |||
4452 | Expr *idx, SourceLocation rbLoc) { | |||
4453 | if (base && !base->getType().isNull() && | |||
4454 | base->getType()->isSpecificPlaceholderType(BuiltinType::OMPArraySection)) | |||
4455 | return ActOnOMPArraySectionExpr(base, lbLoc, idx, SourceLocation(), | |||
4456 | /*Length=*/nullptr, rbLoc); | |||
4457 | ||||
4458 | // Since this might be a postfix expression, get rid of ParenListExprs. | |||
4459 | if (isa<ParenListExpr>(base)) { | |||
4460 | ExprResult result = MaybeConvertParenListExprToParenExpr(S, base); | |||
4461 | if (result.isInvalid()) return ExprError(); | |||
4462 | base = result.get(); | |||
4463 | } | |||
4464 | ||||
4465 | // A comma-expression as the index is deprecated in C++2a onwards. | |||
4466 | if (getLangOpts().CPlusPlus2a && | |||
4467 | ((isa<BinaryOperator>(idx) && cast<BinaryOperator>(idx)->isCommaOp()) || | |||
4468 | (isa<CXXOperatorCallExpr>(idx) && | |||
4469 | cast<CXXOperatorCallExpr>(idx)->getOperator() == OO_Comma))) { | |||
4470 | Diag(idx->getExprLoc(), diag::warn_deprecated_comma_subscript) | |||
4471 | << SourceRange(base->getBeginLoc(), rbLoc); | |||
4472 | } | |||
4473 | ||||
4474 | // Handle any non-overload placeholder types in the base and index | |||
4475 | // expressions. We can't handle overloads here because the other | |||
4476 | // operand might be an overloadable type, in which case the overload | |||
4477 | // resolution for the operator overload should get the first crack | |||
4478 | // at the overload. | |||
4479 | bool IsMSPropertySubscript = false; | |||
4480 | if (base->getType()->isNonOverloadPlaceholderType()) { | |||
4481 | IsMSPropertySubscript = isMSPropertySubscriptExpr(*this, base); | |||
4482 | if (!IsMSPropertySubscript) { | |||
4483 | ExprResult result = CheckPlaceholderExpr(base); | |||
4484 | if (result.isInvalid()) | |||
4485 | return ExprError(); | |||
4486 | base = result.get(); | |||
4487 | } | |||
4488 | } | |||
4489 | if (idx->getType()->isNonOverloadPlaceholderType()) { | |||
4490 | ExprResult result = CheckPlaceholderExpr(idx); | |||
4491 | if (result.isInvalid()) return ExprError(); | |||
4492 | idx = result.get(); | |||
4493 | } | |||
4494 | ||||
4495 | // Build an unanalyzed expression if either operand is type-dependent. | |||
4496 | if (getLangOpts().CPlusPlus && | |||
4497 | (base->isTypeDependent() || idx->isTypeDependent())) { | |||
4498 | return new (Context) ArraySubscriptExpr(base, idx, Context.DependentTy, | |||
4499 | VK_LValue, OK_Ordinary, rbLoc); | |||
4500 | } | |||
4501 | ||||
4502 | // MSDN, property (C++) | |||
4503 | // https://msdn.microsoft.com/en-us/library/yhfk0thd(v=vs.120).aspx | |||
4504 | // This attribute can also be used in the declaration of an empty array in a | |||
4505 | // class or structure definition. For example: | |||
4506 | // __declspec(property(get=GetX, put=PutX)) int x[]; | |||
4507 | // The above statement indicates that x[] can be used with one or more array | |||
4508 | // indices. In this case, i=p->x[a][b] will be turned into i=p->GetX(a, b), | |||
4509 | // and p->x[a][b] = i will be turned into p->PutX(a, b, i); | |||
4510 | if (IsMSPropertySubscript) { | |||
4511 | // Build MS property subscript expression if base is MS property reference | |||
4512 | // or MS property subscript. | |||
4513 | return new (Context) MSPropertySubscriptExpr( | |||
4514 | base, idx, Context.PseudoObjectTy, VK_LValue, OK_Ordinary, rbLoc); | |||
4515 | } | |||
4516 | ||||
4517 | // Use C++ overloaded-operator rules if either operand has record | |||
4518 | // type. The spec says to do this if either type is *overloadable*, | |||
4519 | // but enum types can't declare subscript operators or conversion | |||
4520 | // operators, so there's nothing interesting for overload resolution | |||
4521 | // to do if there aren't any record types involved. | |||
4522 | // | |||
4523 | // ObjC pointers have their own subscripting logic that is not tied | |||
4524 | // to overload resolution and so should not take this path. | |||
4525 | if (getLangOpts().CPlusPlus && | |||
4526 | (base->getType()->isRecordType() || | |||
4527 | (!base->getType()->isObjCObjectPointerType() && | |||
4528 | idx->getType()->isRecordType()))) { | |||
4529 | return CreateOverloadedArraySubscriptExpr(lbLoc, rbLoc, base, idx); | |||
4530 | } | |||
4531 | ||||
4532 | ExprResult Res = CreateBuiltinArraySubscriptExpr(base, lbLoc, idx, rbLoc); | |||
4533 | ||||
4534 | if (!Res.isInvalid() && isa<ArraySubscriptExpr>(Res.get())) | |||
4535 | CheckSubscriptAccessOfNoDeref(cast<ArraySubscriptExpr>(Res.get())); | |||
4536 | ||||
4537 | return Res; | |||
4538 | } | |||
4539 | ||||
4540 | void Sema::CheckAddressOfNoDeref(const Expr *E) { | |||
4541 | ExpressionEvaluationContextRecord &LastRecord = ExprEvalContexts.back(); | |||
4542 | const Expr *StrippedExpr = E->IgnoreParenImpCasts(); | |||
4543 | ||||
4544 | // For expressions like `&(*s).b`, the base is recorded and what should be | |||
4545 | // checked. | |||
4546 | const MemberExpr *Member = nullptr; | |||
4547 | while ((Member = dyn_cast<MemberExpr>(StrippedExpr)) && !Member->isArrow()) | |||
4548 | StrippedExpr = Member->getBase()->IgnoreParenImpCasts(); | |||
4549 | ||||
4550 | LastRecord.PossibleDerefs.erase(StrippedExpr); | |||
4551 | } | |||
4552 | ||||
4553 | void Sema::CheckSubscriptAccessOfNoDeref(const ArraySubscriptExpr *E) { | |||
4554 | QualType ResultTy = E->getType(); | |||
4555 | ExpressionEvaluationContextRecord &LastRecord = ExprEvalContexts.back(); | |||
4556 | ||||
4557 | // Bail if the element is an array since it is not memory access. | |||
4558 | if (isa<ArrayType>(ResultTy)) | |||
4559 | return; | |||
4560 | ||||
4561 | if (ResultTy->hasAttr(attr::NoDeref)) { | |||
4562 | LastRecord.PossibleDerefs.insert(E); | |||
4563 | return; | |||
4564 | } | |||
4565 | ||||
4566 | // Check if the base type is a pointer to a member access of a struct | |||
4567 | // marked with noderef. | |||
4568 | const Expr *Base = E->getBase(); | |||
4569 | QualType BaseTy = Base->getType(); | |||
4570 | if (!(isa<ArrayType>(BaseTy) || isa<PointerType>(BaseTy))) | |||
4571 | // Not a pointer access | |||
4572 | return; | |||
4573 | ||||
4574 | const MemberExpr *Member = nullptr; | |||
4575 | while ((Member = dyn_cast<MemberExpr>(Base->IgnoreParenCasts())) && | |||
4576 | Member->isArrow()) | |||
4577 | Base = Member->getBase(); | |||
4578 | ||||
4579 | if (const auto *Ptr = dyn_cast<PointerType>(Base->getType())) { | |||
4580 | if (Ptr->getPointeeType()->hasAttr(attr::NoDeref)) | |||
4581 | LastRecord.PossibleDerefs.insert(E); | |||
4582 | } | |||
4583 | } | |||
4584 | ||||
4585 | ExprResult Sema::ActOnOMPArraySectionExpr(Expr *Base, SourceLocation LBLoc, | |||
4586 | Expr *LowerBound, | |||
4587 | SourceLocation ColonLoc, Expr *Length, | |||
4588 | SourceLocation RBLoc) { | |||
4589 | if (Base->getType()->isPlaceholderType() && | |||
4590 | !Base->getType()->isSpecificPlaceholderType( | |||
4591 | BuiltinType::OMPArraySection)) { | |||
4592 | ExprResult Result = CheckPlaceholderExpr(Base); | |||
4593 | if (Result.isInvalid()) | |||
4594 | return ExprError(); | |||
4595 | Base = Result.get(); | |||
4596 | } | |||
4597 | if (LowerBound && LowerBound->getType()->isNonOverloadPlaceholderType()) { | |||
4598 | ExprResult Result = CheckPlaceholderExpr(LowerBound); | |||
4599 | if (Result.isInvalid()) | |||
4600 | return ExprError(); | |||
4601 | Result = DefaultLvalueConversion(Result.get()); | |||
4602 | if (Result.isInvalid()) | |||
4603 | return ExprError(); | |||
4604 | LowerBound = Result.get(); | |||
4605 | } | |||
4606 | if (Length && Length->getType()->isNonOverloadPlaceholderType()) { | |||
4607 | ExprResult Result = CheckPlaceholderExpr(Length); | |||
4608 | if (Result.isInvalid()) | |||
4609 | return ExprError(); | |||
4610 | Result = DefaultLvalueConversion(Result.get()); | |||
4611 | if (Result.isInvalid()) | |||
4612 | return ExprError(); | |||
4613 | Length = Result.get(); | |||
4614 | } | |||
4615 | ||||
4616 | // Build an unanalyzed expression if either operand is type-dependent. | |||
4617 | if (Base->isTypeDependent() || | |||
4618 | (LowerBound && | |||
4619 | (LowerBound->isTypeDependent() || LowerBound->isValueDependent())) || | |||
4620 | (Length && (Length->isTypeDependent() || Length->isValueDependent()))) { | |||
4621 | return new (Context) | |||
4622 | OMPArraySectionExpr(Base, LowerBound, Length, Context.DependentTy, | |||
4623 | VK_LValue, OK_Ordinary, ColonLoc, RBLoc); | |||
4624 | } | |||
4625 | ||||
4626 | // Perform default conversions. | |||
4627 | QualType OriginalTy = OMPArraySectionExpr::getBaseOriginalType(Base); | |||
4628 | QualType ResultTy; | |||
4629 | if (OriginalTy->isAnyPointerType()) { | |||
4630 | ResultTy = OriginalTy->getPointeeType(); | |||
4631 | } else if (OriginalTy->isArrayType()) { | |||
4632 | ResultTy = OriginalTy->getAsArrayTypeUnsafe()->getElementType(); | |||
4633 | } else { | |||
4634 | return ExprError( | |||
4635 | Diag(Base->getExprLoc(), diag::err_omp_typecheck_section_value) | |||
4636 | << Base->getSourceRange()); | |||
4637 | } | |||
4638 | // C99 6.5.2.1p1 | |||
4639 | if (LowerBound) { | |||
4640 | auto Res = PerformOpenMPImplicitIntegerConversion(LowerBound->getExprLoc(), | |||
4641 | LowerBound); | |||
4642 | if (Res.isInvalid()) | |||
4643 | return ExprError(Diag(LowerBound->getExprLoc(), | |||
4644 | diag::err_omp_typecheck_section_not_integer) | |||
4645 | << 0 << LowerBound->getSourceRange()); | |||
4646 | LowerBound = Res.get(); | |||
4647 | ||||
4648 | if (LowerBound->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || | |||
4649 | LowerBound->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) | |||
4650 | Diag(LowerBound->getExprLoc(), diag::warn_omp_section_is_char) | |||
4651 | << 0 << LowerBound->getSourceRange(); | |||
4652 | } | |||
4653 | if (Length) { | |||
4654 | auto Res = | |||
4655 | PerformOpenMPImplicitIntegerConversion(Length->getExprLoc(), Length); | |||
4656 | if (Res.isInvalid()) | |||
4657 | return ExprError(Diag(Length->getExprLoc(), | |||
4658 | diag::err_omp_typecheck_section_not_integer) | |||
4659 | << 1 << Length->getSourceRange()); | |||
4660 | Length = Res.get(); | |||
4661 | ||||
4662 | if (Length->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || | |||
4663 | Length->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) | |||
4664 | Diag(Length->getExprLoc(), diag::warn_omp_section_is_char) | |||
4665 | << 1 << Length->getSourceRange(); | |||
4666 | } | |||
4667 | ||||
4668 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly, | |||
4669 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object | |||
4670 | // type. Note that functions are not objects, and that (in C99 parlance) | |||
4671 | // incomplete types are not object types. | |||
4672 | if (ResultTy->isFunctionType()) { | |||
4673 | Diag(Base->getExprLoc(), diag::err_omp_section_function_type) | |||
4674 | << ResultTy << Base->getSourceRange(); | |||
4675 | return ExprError(); | |||
4676 | } | |||
4677 | ||||
4678 | if (RequireCompleteType(Base->getExprLoc(), ResultTy, | |||
4679 | diag::err_omp_section_incomplete_type, Base)) | |||
4680 | return ExprError(); | |||
4681 | ||||
4682 | if (LowerBound && !OriginalTy->isAnyPointerType()) { | |||
4683 | Expr::EvalResult Result; | |||
4684 | if (LowerBound->EvaluateAsInt(Result, Context)) { | |||
4685 | // OpenMP 4.5, [2.4 Array Sections] | |||
4686 | // The array section must be a subset of the original array. | |||
4687 | llvm::APSInt LowerBoundValue = Result.Val.getInt(); | |||
4688 | if (LowerBoundValue.isNegative()) { | |||
4689 | Diag(LowerBound->getExprLoc(), diag::err_omp_section_not_subset_of_array) | |||
4690 | << LowerBound->getSourceRange(); | |||
4691 | return ExprError(); | |||
4692 | } | |||
4693 | } | |||
4694 | } | |||
4695 | ||||
4696 | if (Length) { | |||
4697 | Expr::EvalResult Result; | |||
4698 | if (Length->EvaluateAsInt(Result, Context)) { | |||
4699 | // OpenMP 4.5, [2.4 Array Sections] | |||
4700 | // The length must evaluate to non-negative integers. | |||
4701 | llvm::APSInt LengthValue = Result.Val.getInt(); | |||
4702 | if (LengthValue.isNegative()) { | |||
4703 | Diag(Length->getExprLoc(), diag::err_omp_section_length_negative) | |||
4704 | << LengthValue.toString(/*Radix=*/10, /*Signed=*/true) | |||
4705 | << Length->getSourceRange(); | |||
4706 | return ExprError(); | |||
4707 | } | |||
4708 | } | |||
4709 | } else if (ColonLoc.isValid() && | |||
4710 | (OriginalTy.isNull() || (!OriginalTy->isConstantArrayType() && | |||
4711 | !OriginalTy->isVariableArrayType()))) { | |||
4712 | // OpenMP 4.5, [2.4 Array Sections] | |||
4713 | // When the size of the array dimension is not known, the length must be | |||
4714 | // specified explicitly. | |||
4715 | Diag(ColonLoc, diag::err_omp_section_length_undefined) | |||
4716 | << (!OriginalTy.isNull() && OriginalTy->isArrayType()); | |||
4717 | return ExprError(); | |||
4718 | } | |||
4719 | ||||
4720 | if (!Base->getType()->isSpecificPlaceholderType( | |||
4721 | BuiltinType::OMPArraySection)) { | |||
4722 | ExprResult Result = DefaultFunctionArrayLvalueConversion(Base); | |||
4723 | if (Result.isInvalid()) | |||
4724 | return ExprError(); | |||
4725 | Base = Result.get(); | |||
4726 | } | |||
4727 | return new (Context) | |||
4728 | OMPArraySectionExpr(Base, LowerBound, Length, Context.OMPArraySectionTy, | |||
4729 | VK_LValue, OK_Ordinary, ColonLoc, RBLoc); | |||
4730 | } | |||
4731 | ||||
4732 | ExprResult | |||
4733 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, | |||
4734 | Expr *Idx, SourceLocation RLoc) { | |||
4735 | Expr *LHSExp = Base; | |||
4736 | Expr *RHSExp = Idx; | |||
4737 | ||||
4738 | ExprValueKind VK = VK_LValue; | |||
4739 | ExprObjectKind OK = OK_Ordinary; | |||
4740 | ||||
4741 | // Per C++ core issue 1213, the result is an xvalue if either operand is | |||
4742 | // a non-lvalue array, and an lvalue otherwise. | |||
4743 | if (getLangOpts().CPlusPlus11) { | |||
4744 | for (auto *Op : {LHSExp, RHSExp}) { | |||
4745 | Op = Op->IgnoreImplicit(); | |||
4746 | if (Op->getType()->isArrayType() && !Op->isLValue()) | |||
4747 | VK = VK_XValue; | |||
4748 | } | |||
4749 | } | |||
4750 | ||||
4751 | // Perform default conversions. | |||
4752 | if (!LHSExp->getType()->getAs<VectorType>()) { | |||
4753 | ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); | |||
4754 | if (Result.isInvalid()) | |||
4755 | return ExprError(); | |||
4756 | LHSExp = Result.get(); | |||
4757 | } | |||
4758 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); | |||
4759 | if (Result.isInvalid()) | |||
4760 | return ExprError(); | |||
4761 | RHSExp = Result.get(); | |||
4762 | ||||
4763 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); | |||
4764 | ||||
4765 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent | |||
4766 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be | |||
4767 | // in the subscript position. As a result, we need to derive the array base | |||
4768 | // and index from the expression types. | |||
4769 | Expr *BaseExpr, *IndexExpr; | |||
4770 | QualType ResultType; | |||
4771 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { | |||
4772 | BaseExpr = LHSExp; | |||
4773 | IndexExpr = RHSExp; | |||
4774 | ResultType = Context.DependentTy; | |||
4775 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { | |||
4776 | BaseExpr = LHSExp; | |||
4777 | IndexExpr = RHSExp; | |||
4778 | ResultType = PTy->getPointeeType(); | |||
4779 | } else if (const ObjCObjectPointerType *PTy = | |||
4780 | LHSTy->getAs<ObjCObjectPointerType>()) { | |||
4781 | BaseExpr = LHSExp; | |||
4782 | IndexExpr = RHSExp; | |||
4783 | ||||
4784 | // Use custom logic if this should be the pseudo-object subscript | |||
4785 | // expression. | |||
4786 | if (!LangOpts.isSubscriptPointerArithmetic()) | |||
4787 | return BuildObjCSubscriptExpression(RLoc, BaseExpr, IndexExpr, nullptr, | |||
4788 | nullptr); | |||
4789 | ||||
4790 | ResultType = PTy->getPointeeType(); | |||
4791 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { | |||
4792 | // Handle the uncommon case of "123[Ptr]". | |||
4793 | BaseExpr = RHSExp; | |||
4794 | IndexExpr = LHSExp; | |||
4795 | ResultType = PTy->getPointeeType(); | |||
4796 | } else if (const ObjCObjectPointerType *PTy = | |||
4797 | RHSTy->getAs<ObjCObjectPointerType>()) { | |||
4798 | // Handle the uncommon case of "123[Ptr]". | |||
4799 | BaseExpr = RHSExp; | |||
4800 | IndexExpr = LHSExp; | |||
4801 | ResultType = PTy->getPointeeType(); | |||
4802 | if (!LangOpts.isSubscriptPointerArithmetic()) { | |||
4803 | Diag(LLoc, diag::err_subscript_nonfragile_interface) | |||
4804 | << ResultType << BaseExpr->getSourceRange(); | |||
4805 | return ExprError(); | |||
4806 | } | |||
4807 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { | |||
4808 | BaseExpr = LHSExp; // vectors: V[123] | |||
4809 | IndexExpr = RHSExp; | |||
4810 | // We apply C++ DR1213 to vector subscripting too. | |||
4811 | if (getLangOpts().CPlusPlus11 && LHSExp->getValueKind() == VK_RValue) { | |||
4812 | ExprResult Materialized = TemporaryMaterializationConversion(LHSExp); | |||
4813 | if (Materialized.isInvalid()) | |||
4814 | return ExprError(); | |||
4815 | LHSExp = Materialized.get(); | |||
4816 | } | |||
4817 | VK = LHSExp->getValueKind(); | |||
4818 | if (VK != VK_RValue) | |||
4819 | OK = OK_VectorComponent; | |||
4820 | ||||
4821 | ResultType = VTy->getElementType(); | |||
4822 | QualType BaseType = BaseExpr->getType(); | |||
4823 | Qualifiers BaseQuals = BaseType.getQualifiers(); | |||
4824 | Qualifiers MemberQuals = ResultType.getQualifiers(); | |||
4825 | Qualifiers Combined = BaseQuals + MemberQuals; | |||
4826 | if (Combined != MemberQuals) | |||
4827 | ResultType = Context.getQualifiedType(ResultType, Combined); | |||
4828 | } else if (LHSTy->isArrayType()) { | |||
4829 | // If we see an array that wasn't promoted by | |||
4830 | // DefaultFunctionArrayLvalueConversion, it must be an array that | |||
4831 | // wasn't promoted because of the C90 rule that doesn't | |||
4832 | // allow promoting non-lvalue arrays. Warn, then | |||
4833 | // force the promotion here. | |||
4834 | Diag(LHSExp->getBeginLoc(), diag::ext_subscript_non_lvalue) | |||
4835 | << LHSExp->getSourceRange(); | |||
4836 | LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), | |||
4837 | CK_ArrayToPointerDecay).get(); | |||
4838 | LHSTy = LHSExp->getType(); | |||
4839 | ||||
4840 | BaseExpr = LHSExp; | |||
4841 | IndexExpr = RHSExp; | |||
4842 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); | |||
4843 | } else if (RHSTy->isArrayType()) { | |||
4844 | // Same as previous, except for 123[f().a] case | |||
4845 | Diag(RHSExp->getBeginLoc(), diag::ext_subscript_non_lvalue) | |||
4846 | << RHSExp->getSourceRange(); | |||
4847 | RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), | |||
4848 | CK_ArrayToPointerDecay).get(); | |||
4849 | RHSTy = RHSExp->getType(); | |||
4850 | ||||
4851 | BaseExpr = RHSExp; | |||
4852 | IndexExpr = LHSExp; | |||
4853 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); | |||
4854 | } else { | |||
4855 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) | |||
4856 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); | |||
4857 | } | |||
4858 | // C99 6.5.2.1p1 | |||
4859 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) | |||
4860 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) | |||
4861 | << IndexExpr->getSourceRange()); | |||
4862 | ||||
4863 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || | |||
4864 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) | |||
4865 | && !IndexExpr->isTypeDependent()) | |||
4866 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); | |||
4867 | ||||
4868 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly, | |||
4869 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object | |||
4870 | // type. Note that Functions are not objects, and that (in C99 parlance) | |||
4871 | // incomplete types are not object types. | |||
4872 | if (ResultType->isFunctionType()) { | |||
4873 | Diag(BaseExpr->getBeginLoc(), diag::err_subscript_function_type) | |||
4874 | << ResultType << BaseExpr->getSourceRange(); | |||
4875 | return ExprError(); | |||
4876 | } | |||
4877 | ||||
4878 | if (ResultType->isVoidType() && !getLangOpts().CPlusPlus) { | |||
4879 | // GNU extension: subscripting on pointer to void | |||
4880 | Diag(LLoc, diag::ext_gnu_subscript_void_type) | |||
4881 | << BaseExpr->getSourceRange(); | |||
4882 | ||||
4883 | // C forbids expressions of unqualified void type from being l-values. | |||
4884 | // See IsCForbiddenLValueType. | |||
4885 | if (!ResultType.hasQualifiers()) VK = VK_RValue; | |||
4886 | } else if (!ResultType->isDependentType() && | |||
4887 | RequireCompleteType(LLoc, ResultType, | |||
4888 | diag::err_subscript_incomplete_type, BaseExpr)) | |||
4889 | return ExprError(); | |||
4890 | ||||
4891 | assert(VK == VK_RValue || LangOpts.CPlusPlus ||((VK == VK_RValue || LangOpts.CPlusPlus || !ResultType.isCForbiddenLValueType ()) ? static_cast<void> (0) : __assert_fail ("VK == VK_RValue || LangOpts.CPlusPlus || !ResultType.isCForbiddenLValueType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 4892, __PRETTY_FUNCTION__)) | |||
4892 | !ResultType.isCForbiddenLValueType())((VK == VK_RValue || LangOpts.CPlusPlus || !ResultType.isCForbiddenLValueType ()) ? static_cast<void> (0) : __assert_fail ("VK == VK_RValue || LangOpts.CPlusPlus || !ResultType.isCForbiddenLValueType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 4892, __PRETTY_FUNCTION__)); | |||
4893 | ||||
4894 | if (LHSExp->IgnoreParenImpCasts()->getType()->isVariablyModifiedType() && | |||
4895 | FunctionScopes.size() > 1) { | |||
4896 | if (auto *TT = | |||
4897 | LHSExp->IgnoreParenImpCasts()->getType()->getAs<TypedefType>()) { | |||
4898 | for (auto I = FunctionScopes.rbegin(), | |||
4899 | E = std::prev(FunctionScopes.rend()); | |||
4900 | I != E; ++I) { | |||
4901 | auto *CSI = dyn_cast<CapturingScopeInfo>(*I); | |||
4902 | if (CSI == nullptr) | |||
4903 | break; | |||
4904 | DeclContext *DC = nullptr; | |||
4905 | if (auto *LSI = dyn_cast<LambdaScopeInfo>(CSI)) | |||
4906 | DC = LSI->CallOperator; | |||
4907 | else if (auto *CRSI = dyn_cast<CapturedRegionScopeInfo>(CSI)) | |||
4908 | DC = CRSI->TheCapturedDecl; | |||
4909 | else if (auto *BSI = dyn_cast<BlockScopeInfo>(CSI)) | |||
4910 | DC = BSI->TheDecl; | |||
4911 | if (DC) { | |||
4912 | if (DC->containsDecl(TT->getDecl())) | |||
4913 | break; | |||
4914 | captureVariablyModifiedType( | |||
4915 | Context, LHSExp->IgnoreParenImpCasts()->getType(), CSI); | |||
4916 | } | |||
4917 | } | |||
4918 | } | |||
4919 | } | |||
4920 | ||||
4921 | return new (Context) | |||
4922 | ArraySubscriptExpr(LHSExp, RHSExp, ResultType, VK, OK, RLoc); | |||
4923 | } | |||
4924 | ||||
4925 | bool Sema::CheckCXXDefaultArgExpr(SourceLocation CallLoc, FunctionDecl *FD, | |||
4926 | ParmVarDecl *Param) { | |||
4927 | if (Param->hasUnparsedDefaultArg()) { | |||
4928 | Diag(CallLoc, | |||
4929 | diag::err_use_of_default_argument_to_function_declared_later) << | |||
4930 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); | |||
4931 | Diag(UnparsedDefaultArgLocs[Param], | |||
4932 | diag::note_default_argument_declared_here); | |||
4933 | return true; | |||
4934 | } | |||
4935 | ||||
4936 | if (Param->hasUninstantiatedDefaultArg()) { | |||
4937 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); | |||
4938 | ||||
4939 | EnterExpressionEvaluationContext EvalContext( | |||
4940 | *this, ExpressionEvaluationContext::PotentiallyEvaluated, Param); | |||
4941 | ||||
4942 | // Instantiate the expression. | |||
4943 | // | |||
4944 | // FIXME: Pass in a correct Pattern argument, otherwise | |||
4945 | // getTemplateInstantiationArgs uses the lexical context of FD, e.g. | |||
4946 | // | |||
4947 | // template<typename T> | |||
4948 | // struct A { | |||
4949 | // static int FooImpl(); | |||
4950 | // | |||
4951 | // template<typename Tp> | |||
4952 | // // bug: default argument A<T>::FooImpl() is evaluated with 2-level | |||
4953 | // // template argument list [[T], [Tp]], should be [[Tp]]. | |||
4954 | // friend A<Tp> Foo(int a); | |||
4955 | // }; | |||
4956 | // | |||
4957 | // template<typename T> | |||
4958 | // A<T> Foo(int a = A<T>::FooImpl()); | |||
4959 | MultiLevelTemplateArgumentList MutiLevelArgList | |||
4960 | = getTemplateInstantiationArgs(FD, nullptr, /*RelativeToPrimary=*/true); | |||
4961 | ||||
4962 | InstantiatingTemplate Inst(*this, CallLoc, Param, | |||
4963 | MutiLevelArgList.getInnermost()); | |||
4964 | if (Inst.isInvalid()) | |||
4965 | return true; | |||
4966 | if (Inst.isAlreadyInstantiating()) { | |||
4967 | Diag(Param->getBeginLoc(), diag::err_recursive_default_argument) << FD; | |||
4968 | Param->setInvalidDecl(); | |||
4969 | return true; | |||
4970 | } | |||
4971 | ||||
4972 | ExprResult Result; | |||
4973 | { | |||
4974 | // C++ [dcl.fct.default]p5: | |||
4975 | // The names in the [default argument] expression are bound, and | |||
4976 | // the semantic constraints are checked, at the point where the | |||
4977 | // default argument expression appears. | |||
4978 | ContextRAII SavedContext(*this, FD); | |||
4979 | LocalInstantiationScope Local(*this); | |||
4980 | runWithSufficientStackSpace(CallLoc, [&] { | |||
4981 | Result = SubstInitializer(UninstExpr, MutiLevelArgList, | |||
4982 | /*DirectInit*/false); | |||
4983 | }); | |||
4984 | } | |||
4985 | if (Result.isInvalid()) | |||
4986 | return true; | |||
4987 | ||||
4988 | // Check the expression as an initializer for the parameter. | |||
4989 | InitializedEntity Entity | |||
4990 | = InitializedEntity::InitializeParameter(Context, Param); | |||
4991 | InitializationKind Kind = InitializationKind::CreateCopy( | |||
4992 | Param->getLocation(), | |||
4993 | /*FIXME:EqualLoc*/ UninstExpr->getBeginLoc()); | |||
4994 | Expr *ResultE = Result.getAs<Expr>(); | |||
4995 | ||||
4996 | InitializationSequence InitSeq(*this, Entity, Kind, ResultE); | |||
4997 | Result = InitSeq.Perform(*this, Entity, Kind, ResultE); | |||
4998 | if (Result.isInvalid()) | |||
4999 | return true; | |||
5000 | ||||
5001 | Result = | |||
5002 | ActOnFinishFullExpr(Result.getAs<Expr>(), Param->getOuterLocStart(), | |||
5003 | /*DiscardedValue*/ false); | |||
5004 | if (Result.isInvalid()) | |||
5005 | return true; | |||
5006 | ||||
5007 | // Remember the instantiated default argument. | |||
5008 | Param->setDefaultArg(Result.getAs<Expr>()); | |||
5009 | if (ASTMutationListener *L = getASTMutationListener()) { | |||
5010 | L->DefaultArgumentInstantiated(Param); | |||
5011 | } | |||
5012 | } | |||
5013 | ||||
5014 | // If the default argument expression is not set yet, we are building it now. | |||
5015 | if (!Param->hasInit()) { | |||
5016 | Diag(Param->getBeginLoc(), diag::err_recursive_default_argument) << FD; | |||
5017 | Param->setInvalidDecl(); | |||
5018 | return true; | |||
5019 | } | |||
5020 | ||||
5021 | // If the default expression creates temporaries, we need to | |||
5022 | // push them to the current stack of expression temporaries so they'll | |||
5023 | // be properly destroyed. | |||
5024 | // FIXME: We should really be rebuilding the default argument with new | |||
5025 | // bound temporaries; see the comment in PR5810. | |||
5026 | // We don't need to do that with block decls, though, because | |||
5027 | // blocks in default argument expression can never capture anything. | |||
5028 | if (auto Init = dyn_cast<ExprWithCleanups>(Param->getInit())) { | |||
5029 | // Set the "needs cleanups" bit regardless of whether there are | |||
5030 | // any explicit objects. | |||
5031 | Cleanup.setExprNeedsCleanups(Init->cleanupsHaveSideEffects()); | |||
5032 | ||||
5033 | // Append all the objects to the cleanup list. Right now, this | |||
5034 | // should always be a no-op, because blocks in default argument | |||
5035 | // expressions should never be able to capture anything. | |||
5036 | assert(!Init->getNumObjects() &&((!Init->getNumObjects() && "default argument expression has capturing blocks?" ) ? static_cast<void> (0) : __assert_fail ("!Init->getNumObjects() && \"default argument expression has capturing blocks?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 5037, __PRETTY_FUNCTION__)) | |||
5037 | "default argument expression has capturing blocks?")((!Init->getNumObjects() && "default argument expression has capturing blocks?" ) ? static_cast<void> (0) : __assert_fail ("!Init->getNumObjects() && \"default argument expression has capturing blocks?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 5037, __PRETTY_FUNCTION__)); | |||
5038 | } | |||
5039 | ||||
5040 | // We already type-checked the argument, so we know it works. | |||
5041 | // Just mark all of the declarations in this potentially-evaluated expression | |||
5042 | // as being "referenced". | |||
5043 | EnterExpressionEvaluationContext EvalContext( | |||
5044 | *this, ExpressionEvaluationContext::PotentiallyEvaluated, Param); | |||
5045 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg(), | |||
5046 | /*SkipLocalVariables=*/true); | |||
5047 | return false; | |||
5048 | } | |||
5049 | ||||
5050 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, | |||
5051 | FunctionDecl *FD, ParmVarDecl *Param) { | |||
5052 | if (CheckCXXDefaultArgExpr(CallLoc, FD, Param)) | |||
5053 | return ExprError(); | |||
5054 | return CXXDefaultArgExpr::Create(Context, CallLoc, Param, CurContext); | |||
5055 | } | |||
5056 | ||||
5057 | Sema::VariadicCallType | |||
5058 | Sema::getVariadicCallType(FunctionDecl *FDecl, const FunctionProtoType *Proto, | |||
5059 | Expr *Fn) { | |||
5060 | if (Proto && Proto->isVariadic()) { | |||
5061 | if (dyn_cast_or_null<CXXConstructorDecl>(FDecl)) | |||
5062 | return VariadicConstructor; | |||
5063 | else if (Fn && Fn->getType()->isBlockPointerType()) | |||
5064 | return VariadicBlock; | |||
5065 | else if (FDecl) { | |||
5066 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) | |||
5067 | if (Method->isInstance()) | |||
5068 | return VariadicMethod; | |||
5069 | } else if (Fn && Fn->getType() == Context.BoundMemberTy) | |||
5070 | return VariadicMethod; | |||
5071 | return VariadicFunction; | |||
5072 | } | |||
5073 | return VariadicDoesNotApply; | |||
5074 | } | |||
5075 | ||||
5076 | namespace { | |||
5077 | class FunctionCallCCC final : public FunctionCallFilterCCC { | |||
5078 | public: | |||
5079 | FunctionCallCCC(Sema &SemaRef, const IdentifierInfo *FuncName, | |||
5080 | unsigned NumArgs, MemberExpr *ME) | |||
5081 | : FunctionCallFilterCCC(SemaRef, NumArgs, false, ME), | |||
5082 | FunctionName(FuncName) {} | |||
5083 | ||||
5084 | bool ValidateCandidate(const TypoCorrection &candidate) override { | |||
5085 | if (!candidate.getCorrectionSpecifier() || | |||
5086 | candidate.getCorrectionAsIdentifierInfo() != FunctionName) { | |||
5087 | return false; | |||
5088 | } | |||
5089 | ||||
5090 | return FunctionCallFilterCCC::ValidateCandidate(candidate); | |||
5091 | } | |||
5092 | ||||
5093 | std::unique_ptr<CorrectionCandidateCallback> clone() override { | |||
5094 | return std::make_unique<FunctionCallCCC>(*this); | |||
5095 | } | |||
5096 | ||||
5097 | private: | |||
5098 | const IdentifierInfo *const FunctionName; | |||
5099 | }; | |||
5100 | } | |||
5101 | ||||
5102 | static TypoCorrection TryTypoCorrectionForCall(Sema &S, Expr *Fn, | |||
5103 | FunctionDecl *FDecl, | |||
5104 | ArrayRef<Expr *> Args) { | |||
5105 | MemberExpr *ME = dyn_cast<MemberExpr>(Fn); | |||
5106 | DeclarationName FuncName = FDecl->getDeclName(); | |||
5107 | SourceLocation NameLoc = ME ? ME->getMemberLoc() : Fn->getBeginLoc(); | |||
5108 | ||||
5109 | FunctionCallCCC CCC(S, FuncName.getAsIdentifierInfo(), Args.size(), ME); | |||
5110 | if (TypoCorrection Corrected = S.CorrectTypo( | |||
5111 | DeclarationNameInfo(FuncName, NameLoc), Sema::LookupOrdinaryName, | |||
5112 | S.getScopeForContext(S.CurContext), nullptr, CCC, | |||
5113 | Sema::CTK_ErrorRecovery)) { | |||
5114 | if (NamedDecl *ND = Corrected.getFoundDecl()) { | |||
5115 | if (Corrected.isOverloaded()) { | |||
5116 | OverloadCandidateSet OCS(NameLoc, OverloadCandidateSet::CSK_Normal); | |||
5117 | OverloadCandidateSet::iterator Best; | |||
5118 | for (NamedDecl *CD : Corrected) { | |||
5119 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(CD)) | |||
5120 | S.AddOverloadCandidate(FD, DeclAccessPair::make(FD, AS_none), Args, | |||
5121 | OCS); | |||
5122 | } | |||
5123 | switch (OCS.BestViableFunction(S, NameLoc, Best)) { | |||
5124 | case OR_Success: | |||
5125 | ND = Best->FoundDecl; | |||
5126 | Corrected.setCorrectionDecl(ND); | |||
5127 | break; | |||
5128 | default: | |||
5129 | break; | |||
5130 | } | |||
5131 | } | |||
5132 | ND = ND->getUnderlyingDecl(); | |||
5133 | if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) | |||
5134 | return Corrected; | |||
5135 | } | |||
5136 | } | |||
5137 | return TypoCorrection(); | |||
5138 | } | |||
5139 | ||||
5140 | /// ConvertArgumentsForCall - Converts the arguments specified in | |||
5141 | /// Args/NumArgs to the parameter types of the function FDecl with | |||
5142 | /// function prototype Proto. Call is the call expression itself, and | |||
5143 | /// Fn is the function expression. For a C++ member function, this | |||
5144 | /// routine does not attempt to convert the object argument. Returns | |||
5145 | /// true if the call is ill-formed. | |||
5146 | bool | |||
5147 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, | |||
5148 | FunctionDecl *FDecl, | |||
5149 | const FunctionProtoType *Proto, | |||
5150 | ArrayRef<Expr *> Args, | |||
5151 | SourceLocation RParenLoc, | |||
5152 | bool IsExecConfig) { | |||
5153 | // Bail out early if calling a builtin with custom typechecking. | |||
5154 | if (FDecl) | |||
5155 | if (unsigned ID = FDecl->getBuiltinID()) | |||
5156 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) | |||
5157 | return false; | |||
5158 | ||||
5159 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by | |||
5160 | // assignment, to the types of the corresponding parameter, ... | |||
5161 | unsigned NumParams = Proto->getNumParams(); | |||
5162 | bool Invalid = false; | |||
5163 | unsigned MinArgs = FDecl ? FDecl->getMinRequiredArguments() : NumParams; | |||
5164 | unsigned FnKind = Fn->getType()->isBlockPointerType() | |||
5165 | ? 1 /* block */ | |||
5166 | : (IsExecConfig ? 3 /* kernel function (exec config) */ | |||
5167 | : 0 /* function */); | |||
5168 | ||||
5169 | // If too few arguments are available (and we don't have default | |||
5170 | // arguments for the remaining parameters), don't make the call. | |||
5171 | if (Args.size() < NumParams) { | |||
5172 | if (Args.size() < MinArgs) { | |||
5173 | TypoCorrection TC; | |||
5174 | if (FDecl && (TC = TryTypoCorrectionForCall(*this, Fn, FDecl, Args))) { | |||
5175 | unsigned diag_id = | |||
5176 | MinArgs == NumParams && !Proto->isVariadic() | |||
5177 | ? diag::err_typecheck_call_too_few_args_suggest | |||
5178 | : diag::err_typecheck_call_too_few_args_at_least_suggest; | |||
5179 | diagnoseTypo(TC, PDiag(diag_id) << FnKind << MinArgs | |||
5180 | << static_cast<unsigned>(Args.size()) | |||
5181 | << TC.getCorrectionRange()); | |||
5182 | } else if (MinArgs == 1 && FDecl && FDecl->getParamDecl(0)->getDeclName()) | |||
5183 | Diag(RParenLoc, | |||
5184 | MinArgs == NumParams && !Proto->isVariadic() | |||
5185 | ? diag::err_typecheck_call_too_few_args_one | |||
5186 | : diag::err_typecheck_call_too_few_args_at_least_one) | |||
5187 | << FnKind << FDecl->getParamDecl(0) << Fn->getSourceRange(); | |||
5188 | else | |||
5189 | Diag(RParenLoc, MinArgs == NumParams && !Proto->isVariadic() | |||
5190 | ? diag::err_typecheck_call_too_few_args | |||
5191 | : diag::err_typecheck_call_too_few_args_at_least) | |||
5192 | << FnKind << MinArgs << static_cast<unsigned>(Args.size()) | |||
5193 | << Fn->getSourceRange(); | |||
5194 | ||||
5195 | // Emit the location of the prototype. | |||
5196 | if (!TC && FDecl && !FDecl->getBuiltinID() && !IsExecConfig) | |||
5197 | Diag(FDecl->getLocation(), diag::note_callee_decl) << FDecl; | |||
5198 | ||||
5199 | return true; | |||
5200 | } | |||
5201 | // We reserve space for the default arguments when we create | |||
5202 | // the call expression, before calling ConvertArgumentsForCall. | |||
5203 | assert((Call->getNumArgs() == NumParams) &&(((Call->getNumArgs() == NumParams) && "We should have reserved space for the default arguments before!" ) ? static_cast<void> (0) : __assert_fail ("(Call->getNumArgs() == NumParams) && \"We should have reserved space for the default arguments before!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 5204, __PRETTY_FUNCTION__)) | |||
5204 | "We should have reserved space for the default arguments before!")(((Call->getNumArgs() == NumParams) && "We should have reserved space for the default arguments before!" ) ? static_cast<void> (0) : __assert_fail ("(Call->getNumArgs() == NumParams) && \"We should have reserved space for the default arguments before!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 5204, __PRETTY_FUNCTION__)); | |||
5205 | } | |||
5206 | ||||
5207 | // If too many are passed and not variadic, error on the extras and drop | |||
5208 | // them. | |||
5209 | if (Args.size() > NumParams) { | |||
5210 | if (!Proto->isVariadic()) { | |||
5211 | TypoCorrection TC; | |||
5212 | if (FDecl && (TC = TryTypoCorrectionForCall(*this, Fn, FDecl, Args))) { | |||
5213 | unsigned diag_id = | |||
5214 | MinArgs == NumParams && !Proto->isVariadic() | |||
5215 | ? diag::err_typecheck_call_too_many_args_suggest | |||
5216 | : diag::err_typecheck_call_too_many_args_at_most_suggest; | |||
5217 | diagnoseTypo(TC, PDiag(diag_id) << FnKind << NumParams | |||
5218 | << static_cast<unsigned>(Args.size()) | |||
5219 | << TC.getCorrectionRange()); | |||
5220 | } else if (NumParams == 1 && FDecl && | |||
5221 | FDecl->getParamDecl(0)->getDeclName()) | |||
5222 | Diag(Args[NumParams]->getBeginLoc(), | |||
5223 | MinArgs == NumParams | |||
5224 | ? diag::err_typecheck_call_too_many_args_one | |||
5225 | : diag::err_typecheck_call_too_many_args_at_most_one) | |||
5226 | << FnKind << FDecl->getParamDecl(0) | |||
5227 | << static_cast<unsigned>(Args.size()) << Fn->getSourceRange() | |||
5228 | << SourceRange(Args[NumParams]->getBeginLoc(), | |||
5229 | Args.back()->getEndLoc()); | |||
5230 | else | |||
5231 | Diag(Args[NumParams]->getBeginLoc(), | |||
5232 | MinArgs == NumParams | |||
5233 | ? diag::err_typecheck_call_too_many_args | |||
5234 | : diag::err_typecheck_call_too_many_args_at_most) | |||
5235 | << FnKind << NumParams << static_cast<unsigned>(Args.size()) | |||
5236 | << Fn->getSourceRange() | |||
5237 | << SourceRange(Args[NumParams]->getBeginLoc(), | |||
5238 | Args.back()->getEndLoc()); | |||
5239 | ||||
5240 | // Emit the location of the prototype. | |||
5241 | if (!TC && FDecl && !FDecl->getBuiltinID() && !IsExecConfig) | |||
5242 | Diag(FDecl->getLocation(), diag::note_callee_decl) << FDecl; | |||
5243 | ||||
5244 | // This deletes the extra arguments. | |||
5245 | Call->shrinkNumArgs(NumParams); | |||
5246 | return true; | |||
5247 | } | |||
5248 | } | |||
5249 | SmallVector<Expr *, 8> AllArgs; | |||
5250 | VariadicCallType CallType = getVariadicCallType(FDecl, Proto, Fn); | |||
5251 | ||||
5252 | Invalid = GatherArgumentsForCall(Call->getBeginLoc(), FDecl, Proto, 0, Args, | |||
5253 | AllArgs, CallType); | |||
5254 | if (Invalid) | |||
5255 | return true; | |||
5256 | unsigned TotalNumArgs = AllArgs.size(); | |||
5257 | for (unsigned i = 0; i < TotalNumArgs; ++i) | |||
5258 | Call->setArg(i, AllArgs[i]); | |||
5259 | ||||
5260 | return false; | |||
5261 | } | |||
5262 | ||||
5263 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, FunctionDecl *FDecl, | |||
5264 | const FunctionProtoType *Proto, | |||
5265 | unsigned FirstParam, ArrayRef<Expr *> Args, | |||
5266 | SmallVectorImpl<Expr *> &AllArgs, | |||
5267 | VariadicCallType CallType, bool AllowExplicit, | |||
5268 | bool IsListInitialization) { | |||
5269 | unsigned NumParams = Proto->getNumParams(); | |||
5270 | bool Invalid = false; | |||
5271 | size_t ArgIx = 0; | |||
5272 | // Continue to check argument types (even if we have too few/many args). | |||
5273 | for (unsigned i = FirstParam; i < NumParams; i++) { | |||
5274 | QualType ProtoArgType = Proto->getParamType(i); | |||
5275 | ||||
5276 | Expr *Arg; | |||
5277 | ParmVarDecl *Param = FDecl ? FDecl->getParamDecl(i) : nullptr; | |||
5278 | if (ArgIx < Args.size()) { | |||
5279 | Arg = Args[ArgIx++]; | |||
5280 | ||||
5281 | if (RequireCompleteType(Arg->getBeginLoc(), ProtoArgType, | |||
5282 | diag::err_call_incomplete_argument, Arg)) | |||
5283 | return true; | |||
5284 | ||||
5285 | // Strip the unbridged-cast placeholder expression off, if applicable. | |||
5286 | bool CFAudited = false; | |||
5287 | if (Arg->getType() == Context.ARCUnbridgedCastTy && | |||
5288 | FDecl && FDecl->hasAttr<CFAuditedTransferAttr>() && | |||
5289 | (!Param || !Param->hasAttr<CFConsumedAttr>())) | |||
5290 | Arg = stripARCUnbridgedCast(Arg); | |||
5291 | else if (getLangOpts().ObjCAutoRefCount && | |||
5292 | FDecl && FDecl->hasAttr<CFAuditedTransferAttr>() && | |||
5293 | (!Param || !Param->hasAttr<CFConsumedAttr>())) | |||
5294 | CFAudited = true; | |||
5295 | ||||
5296 | if (Proto->getExtParameterInfo(i).isNoEscape()) | |||
5297 | if (auto *BE = dyn_cast<BlockExpr>(Arg->IgnoreParenNoopCasts(Context))) | |||
5298 | BE->getBlockDecl()->setDoesNotEscape(); | |||
5299 | ||||
5300 | InitializedEntity Entity = | |||
5301 | Param ? InitializedEntity::InitializeParameter(Context, Param, | |||
5302 | ProtoArgType) | |||
5303 | : InitializedEntity::InitializeParameter( | |||
5304 | Context, ProtoArgType, Proto->isParamConsumed(i)); | |||
5305 | ||||
5306 | // Remember that parameter belongs to a CF audited API. | |||
5307 | if (CFAudited) | |||
5308 | Entity.setParameterCFAudited(); | |||
5309 | ||||
5310 | ExprResult ArgE = PerformCopyInitialization( | |||
5311 | Entity, SourceLocation(), Arg, IsListInitialization, AllowExplicit); | |||
5312 | if (ArgE.isInvalid()) | |||
5313 | return true; | |||
5314 | ||||
5315 | Arg = ArgE.getAs<Expr>(); | |||
5316 | } else { | |||
5317 | assert(Param && "can't use default arguments without a known callee")((Param && "can't use default arguments without a known callee" ) ? static_cast<void> (0) : __assert_fail ("Param && \"can't use default arguments without a known callee\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 5317, __PRETTY_FUNCTION__)); | |||
5318 | ||||
5319 | ExprResult ArgExpr = BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); | |||
5320 | if (ArgExpr.isInvalid()) | |||
5321 | return true; | |||
5322 | ||||
5323 | Arg = ArgExpr.getAs<Expr>(); | |||
5324 | } | |||
5325 | ||||
5326 | // Check for array bounds violations for each argument to the call. This | |||
5327 | // check only triggers warnings when the argument isn't a more complex Expr | |||
5328 | // with its own checking, such as a BinaryOperator. | |||
5329 | CheckArrayAccess(Arg); | |||
5330 | ||||
5331 | // Check for violations of C99 static array rules (C99 6.7.5.3p7). | |||
5332 | CheckStaticArrayArgument(CallLoc, Param, Arg); | |||
5333 | ||||
5334 | AllArgs.push_back(Arg); | |||
5335 | } | |||
5336 | ||||
5337 | // If this is a variadic call, handle args passed through "...". | |||
5338 | if (CallType != VariadicDoesNotApply) { | |||
5339 | // Assume that extern "C" functions with variadic arguments that | |||
5340 | // return __unknown_anytype aren't *really* variadic. | |||
5341 | if (Proto->getReturnType() == Context.UnknownAnyTy && FDecl && | |||
5342 | FDecl->isExternC()) { | |||
5343 | for (Expr *A : Args.slice(ArgIx)) { | |||
5344 | QualType paramType; // ignored | |||
5345 | ExprResult arg = checkUnknownAnyArg(CallLoc, A, paramType); | |||
5346 | Invalid |= arg.isInvalid(); | |||
5347 | AllArgs.push_back(arg.get()); | |||
5348 | } | |||
5349 | ||||
5350 | // Otherwise do argument promotion, (C99 6.5.2.2p7). | |||
5351 | } else { | |||
5352 | for (Expr *A : Args.slice(ArgIx)) { | |||
5353 | ExprResult Arg = DefaultVariadicArgumentPromotion(A, CallType, FDecl); | |||
5354 | Invalid |= Arg.isInvalid(); | |||
5355 | // Copy blocks to the heap. | |||
5356 | if (A->getType()->isBlockPointerType()) | |||
5357 | maybeExtendBlockObject(Arg); | |||
5358 | AllArgs.push_back(Arg.get()); | |||
5359 | } | |||
5360 | } | |||
5361 | ||||
5362 | // Check for array bounds violations. | |||
5363 | for (Expr *A : Args.slice(ArgIx)) | |||
5364 | CheckArrayAccess(A); | |||
5365 | } | |||
5366 | return Invalid; | |||
5367 | } | |||
5368 | ||||
5369 | static void DiagnoseCalleeStaticArrayParam(Sema &S, ParmVarDecl *PVD) { | |||
5370 | TypeLoc TL = PVD->getTypeSourceInfo()->getTypeLoc(); | |||
5371 | if (DecayedTypeLoc DTL = TL.getAs<DecayedTypeLoc>()) | |||
5372 | TL = DTL.getOriginalLoc(); | |||
5373 | if (ArrayTypeLoc ATL = TL.getAs<ArrayTypeLoc>()) | |||
5374 | S.Diag(PVD->getLocation(), diag::note_callee_static_array) | |||
5375 | << ATL.getLocalSourceRange(); | |||
5376 | } | |||
5377 | ||||
5378 | /// CheckStaticArrayArgument - If the given argument corresponds to a static | |||
5379 | /// array parameter, check that it is non-null, and that if it is formed by | |||
5380 | /// array-to-pointer decay, the underlying array is sufficiently large. | |||
5381 | /// | |||
5382 | /// C99 6.7.5.3p7: If the keyword static also appears within the [ and ] of the | |||
5383 | /// array type derivation, then for each call to the function, the value of the | |||
5384 | /// corresponding actual argument shall provide access to the first element of | |||
5385 | /// an array with at least as many elements as specified by the size expression. | |||
5386 | void | |||
5387 | Sema::CheckStaticArrayArgument(SourceLocation CallLoc, | |||
5388 | ParmVarDecl *Param, | |||
5389 | const Expr *ArgExpr) { | |||
5390 | // Static array parameters are not supported in C++. | |||
5391 | if (!Param || getLangOpts().CPlusPlus) | |||
5392 | return; | |||
5393 | ||||
5394 | QualType OrigTy = Param->getOriginalType(); | |||
5395 | ||||
5396 | const ArrayType *AT = Context.getAsArrayType(OrigTy); | |||
5397 | if (!AT || AT->getSizeModifier() != ArrayType::Static) | |||
5398 | return; | |||
5399 | ||||
5400 | if (ArgExpr->isNullPointerConstant(Context, | |||
5401 | Expr::NPC_NeverValueDependent)) { | |||
5402 | Diag(CallLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); | |||
5403 | DiagnoseCalleeStaticArrayParam(*this, Param); | |||
5404 | return; | |||
5405 | } | |||
5406 | ||||
5407 | const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT); | |||
5408 | if (!CAT) | |||
5409 | return; | |||
5410 | ||||
5411 | const ConstantArrayType *ArgCAT = | |||
5412 | Context.getAsConstantArrayType(ArgExpr->IgnoreParenCasts()->getType()); | |||
5413 | if (!ArgCAT) | |||
5414 | return; | |||
5415 | ||||
5416 | if (getASTContext().hasSameUnqualifiedType(CAT->getElementType(), | |||
5417 | ArgCAT->getElementType())) { | |||
5418 | if (ArgCAT->getSize().ult(CAT->getSize())) { | |||
5419 | Diag(CallLoc, diag::warn_static_array_too_small) | |||
5420 | << ArgExpr->getSourceRange() | |||
5421 | << (unsigned)ArgCAT->getSize().getZExtValue() | |||
5422 | << (unsigned)CAT->getSize().getZExtValue() << 0; | |||
5423 | DiagnoseCalleeStaticArrayParam(*this, Param); | |||
5424 | } | |||
5425 | return; | |||
5426 | } | |||
5427 | ||||
5428 | Optional<CharUnits> ArgSize = | |||
5429 | getASTContext().getTypeSizeInCharsIfKnown(ArgCAT); | |||
5430 | Optional<CharUnits> ParmSize = getASTContext().getTypeSizeInCharsIfKnown(CAT); | |||
5431 | if (ArgSize && ParmSize && *ArgSize < *ParmSize) { | |||
5432 | Diag(CallLoc, diag::warn_static_array_too_small) | |||
5433 | << ArgExpr->getSourceRange() << (unsigned)ArgSize->getQuantity() | |||
5434 | << (unsigned)ParmSize->getQuantity() << 1; | |||
5435 | DiagnoseCalleeStaticArrayParam(*this, Param); | |||
5436 | } | |||
5437 | } | |||
5438 | ||||
5439 | /// Given a function expression of unknown-any type, try to rebuild it | |||
5440 | /// to have a function type. | |||
5441 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); | |||
5442 | ||||
5443 | /// Is the given type a placeholder that we need to lower out | |||
5444 | /// immediately during argument processing? | |||
5445 | static bool isPlaceholderToRemoveAsArg(QualType type) { | |||
5446 | // Placeholders are never sugared. | |||
5447 | const BuiltinType *placeholder = dyn_cast<BuiltinType>(type); | |||
5448 | if (!placeholder) return false; | |||
5449 | ||||
5450 | switch (placeholder->getKind()) { | |||
5451 | // Ignore all the non-placeholder types. | |||
5452 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ | |||
5453 | case BuiltinType::Id: | |||
5454 | #include "clang/Basic/OpenCLImageTypes.def" | |||
5455 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ | |||
5456 | case BuiltinType::Id: | |||
5457 | #include "clang/Basic/OpenCLExtensionTypes.def" | |||
5458 | // In practice we'll never use this, since all SVE types are sugared | |||
5459 | // via TypedefTypes rather than exposed directly as BuiltinTypes. | |||
5460 | #define SVE_TYPE(Name, Id, SingletonId) \ | |||
5461 | case BuiltinType::Id: | |||
5462 | #include "clang/Basic/AArch64SVEACLETypes.def" | |||
5463 | #define PLACEHOLDER_TYPE(ID, SINGLETON_ID) | |||
5464 | #define BUILTIN_TYPE(ID, SINGLETON_ID) case BuiltinType::ID: | |||
5465 | #include "clang/AST/BuiltinTypes.def" | |||
5466 | return false; | |||
5467 | ||||
5468 | // We cannot lower out overload sets; they might validly be resolved | |||
5469 | // by the call machinery. | |||
5470 | case BuiltinType::Overload: | |||
5471 | return false; | |||
5472 | ||||
5473 | // Unbridged casts in ARC can be handled in some call positions and | |||
5474 | // should be left in place. | |||
5475 | case BuiltinType::ARCUnbridgedCast: | |||
5476 | return false; | |||
5477 | ||||
5478 | // Pseudo-objects should be converted as soon as possible. | |||
5479 | case BuiltinType::PseudoObject: | |||
5480 | return true; | |||
5481 | ||||
5482 | // The debugger mode could theoretically but currently does not try | |||
5483 | // to resolve unknown-typed arguments based on known parameter types. | |||
5484 | case BuiltinType::UnknownAny: | |||
5485 | return true; | |||
5486 | ||||
5487 | // These are always invalid as call arguments and should be reported. | |||
5488 | case BuiltinType::BoundMember: | |||
5489 | case BuiltinType::BuiltinFn: | |||
5490 | case BuiltinType::OMPArraySection: | |||
5491 | return true; | |||
5492 | ||||
5493 | } | |||
5494 | llvm_unreachable("bad builtin type kind")::llvm::llvm_unreachable_internal("bad builtin type kind", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 5494); | |||
5495 | } | |||
5496 | ||||
5497 | /// Check an argument list for placeholders that we won't try to | |||
5498 | /// handle later. | |||
5499 | static bool checkArgsForPlaceholders(Sema &S, MultiExprArg args) { | |||
5500 | // Apply this processing to all the arguments at once instead of | |||
5501 | // dying at the first failure. | |||
5502 | bool hasInvalid = false; | |||
5503 | for (size_t i = 0, e = args.size(); i != e; i++) { | |||
5504 | if (isPlaceholderToRemoveAsArg(args[i]->getType())) { | |||
5505 | ExprResult result = S.CheckPlaceholderExpr(args[i]); | |||
5506 | if (result.isInvalid()) hasInvalid = true; | |||
5507 | else args[i] = result.get(); | |||
5508 | } else if (hasInvalid) { | |||
5509 | (void)S.CorrectDelayedTyposInExpr(args[i]); | |||
5510 | } | |||
5511 | } | |||
5512 | return hasInvalid; | |||
5513 | } | |||
5514 | ||||
5515 | /// If a builtin function has a pointer argument with no explicit address | |||
5516 | /// space, then it should be able to accept a pointer to any address | |||
5517 | /// space as input. In order to do this, we need to replace the | |||
5518 | /// standard builtin declaration with one that uses the same address space | |||
5519 | /// as the call. | |||
5520 | /// | |||
5521 | /// \returns nullptr If this builtin is not a candidate for a rewrite i.e. | |||
5522 | /// it does not contain any pointer arguments without | |||
5523 | /// an address space qualifer. Otherwise the rewritten | |||
5524 | /// FunctionDecl is returned. | |||
5525 | /// TODO: Handle pointer return types. | |||
5526 | static FunctionDecl *rewriteBuiltinFunctionDecl(Sema *Sema, ASTContext &Context, | |||
5527 | FunctionDecl *FDecl, | |||
5528 | MultiExprArg ArgExprs) { | |||
5529 | ||||
5530 | QualType DeclType = FDecl->getType(); | |||
5531 | const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(DeclType); | |||
5532 | ||||
5533 | if (!Context.BuiltinInfo.hasPtrArgsOrResult(FDecl->getBuiltinID()) || !FT || | |||
5534 | ArgExprs.size() < FT->getNumParams()) | |||
5535 | return nullptr; | |||
5536 | ||||
5537 | bool NeedsNewDecl = false; | |||
5538 | unsigned i = 0; | |||
5539 | SmallVector<QualType, 8> OverloadParams; | |||
5540 | ||||
5541 | for (QualType ParamType : FT->param_types()) { | |||
5542 | ||||
5543 | // Convert array arguments to pointer to simplify type lookup. | |||
5544 | ExprResult ArgRes = | |||
5545 | Sema->DefaultFunctionArrayLvalueConversion(ArgExprs[i++]); | |||
5546 | if (ArgRes.isInvalid()) | |||
5547 | return nullptr; | |||
5548 | Expr *Arg = ArgRes.get(); | |||
5549 | QualType ArgType = Arg->getType(); | |||
5550 | if (!ParamType->isPointerType() || | |||
5551 | ParamType.hasAddressSpace() || | |||
5552 | !ArgType->isPointerType() || | |||
5553 | !ArgType->getPointeeType().hasAddressSpace()) { | |||
5554 | OverloadParams.push_back(ParamType); | |||
5555 | continue; | |||
5556 | } | |||
5557 | ||||
5558 | QualType PointeeType = ParamType->getPointeeType(); | |||
5559 | if (PointeeType.hasAddressSpace()) | |||
5560 | continue; | |||
5561 | ||||
5562 | NeedsNewDecl = true; | |||
5563 | LangAS AS = ArgType->getPointeeType().getAddressSpace(); | |||
5564 | ||||
5565 | PointeeType = Context.getAddrSpaceQualType(PointeeType, AS); | |||
5566 | OverloadParams.push_back(Context.getPointerType(PointeeType)); | |||
5567 | } | |||
5568 | ||||
5569 | if (!NeedsNewDecl) | |||
5570 | return nullptr; | |||
5571 | ||||
5572 | FunctionProtoType::ExtProtoInfo EPI; | |||
5573 | EPI.Variadic = FT->isVariadic(); | |||
5574 | QualType OverloadTy = Context.getFunctionType(FT->getReturnType(), | |||
5575 | OverloadParams, EPI); | |||
5576 | DeclContext *Parent = FDecl->getParent(); | |||
5577 | FunctionDecl *OverloadDecl = FunctionDecl::Create(Context, Parent, | |||
5578 | FDecl->getLocation(), | |||
5579 | FDecl->getLocation(), | |||
5580 | FDecl->getIdentifier(), | |||
5581 | OverloadTy, | |||
5582 | /*TInfo=*/nullptr, | |||
5583 | SC_Extern, false, | |||
5584 | /*hasPrototype=*/true); | |||
5585 | SmallVector<ParmVarDecl*, 16> Params; | |||
5586 | FT = cast<FunctionProtoType>(OverloadTy); | |||
5587 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { | |||
5588 | QualType ParamType = FT->getParamType(i); | |||
5589 | ParmVarDecl *Parm = | |||
5590 | ParmVarDecl::Create(Context, OverloadDecl, SourceLocation(), | |||
5591 | SourceLocation(), nullptr, ParamType, | |||
5592 | /*TInfo=*/nullptr, SC_None, nullptr); | |||
5593 | Parm->setScopeInfo(0, i); | |||
5594 | Params.push_back(Parm); | |||
5595 | } | |||
5596 | OverloadDecl->setParams(Params); | |||
5597 | return OverloadDecl; | |||
5598 | } | |||
5599 | ||||
5600 | static void checkDirectCallValidity(Sema &S, const Expr *Fn, | |||
5601 | FunctionDecl *Callee, | |||
5602 | MultiExprArg ArgExprs) { | |||
5603 | // `Callee` (when called with ArgExprs) may be ill-formed. enable_if (and | |||
5604 | // similar attributes) really don't like it when functions are called with an | |||
5605 | // invalid number of args. | |||
5606 | if (S.TooManyArguments(Callee->getNumParams(), ArgExprs.size(), | |||
5607 | /*PartialOverloading=*/false) && | |||
5608 | !Callee->isVariadic()) | |||
5609 | return; | |||
5610 | if (Callee->getMinRequiredArguments() > ArgExprs.size()) | |||
5611 | return; | |||
5612 | ||||
5613 | if (const EnableIfAttr *Attr = S.CheckEnableIf(Callee, ArgExprs, true)) { | |||
5614 | S.Diag(Fn->getBeginLoc(), | |||
5615 | isa<CXXMethodDecl>(Callee) | |||
5616 | ? diag::err_ovl_no_viable_member_function_in_call | |||
5617 | : diag::err_ovl_no_viable_function_in_call) | |||
5618 | << Callee << Callee->getSourceRange(); | |||
5619 | S.Diag(Callee->getLocation(), | |||
5620 | diag::note_ovl_candidate_disabled_by_function_cond_attr) | |||
5621 | << Attr->getCond()->getSourceRange() << Attr->getMessage(); | |||
5622 | return; | |||
5623 | } | |||
5624 | } | |||
5625 | ||||
5626 | static bool enclosingClassIsRelatedToClassInWhichMembersWereFound( | |||
5627 | const UnresolvedMemberExpr *const UME, Sema &S) { | |||
5628 | ||||
5629 | const auto GetFunctionLevelDCIfCXXClass = | |||
5630 | [](Sema &S) -> const CXXRecordDecl * { | |||
5631 | const DeclContext *const DC = S.getFunctionLevelDeclContext(); | |||
5632 | if (!DC || !DC->getParent()) | |||
5633 | return nullptr; | |||
5634 | ||||
5635 | // If the call to some member function was made from within a member | |||
5636 | // function body 'M' return return 'M's parent. | |||
5637 | if (const auto *MD = dyn_cast<CXXMethodDecl>(DC)) | |||
5638 | return MD->getParent()->getCanonicalDecl(); | |||
5639 | // else the call was made from within a default member initializer of a | |||
5640 | // class, so return the class. | |||
5641 | if (const auto *RD = dyn_cast<CXXRecordDecl>(DC)) | |||
5642 | return RD->getCanonicalDecl(); | |||
5643 | return nullptr; | |||
5644 | }; | |||
5645 | // If our DeclContext is neither a member function nor a class (in the | |||
5646 | // case of a lambda in a default member initializer), we can't have an | |||
5647 | // enclosing 'this'. | |||
5648 | ||||
5649 | const CXXRecordDecl *const CurParentClass = GetFunctionLevelDCIfCXXClass(S); | |||
5650 | if (!CurParentClass) | |||
5651 | return false; | |||
5652 | ||||
5653 | // The naming class for implicit member functions call is the class in which | |||
5654 | // name lookup starts. | |||
5655 | const CXXRecordDecl *const NamingClass = | |||
5656 | UME->getNamingClass()->getCanonicalDecl(); | |||
5657 | assert(NamingClass && "Must have naming class even for implicit access")((NamingClass && "Must have naming class even for implicit access" ) ? static_cast<void> (0) : __assert_fail ("NamingClass && \"Must have naming class even for implicit access\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 5657, __PRETTY_FUNCTION__)); | |||
5658 | ||||
5659 | // If the unresolved member functions were found in a 'naming class' that is | |||
5660 | // related (either the same or derived from) to the class that contains the | |||
5661 | // member function that itself contained the implicit member access. | |||
5662 | ||||
5663 | return CurParentClass == NamingClass || | |||
5664 | CurParentClass->isDerivedFrom(NamingClass); | |||
5665 | } | |||
5666 | ||||
5667 | static void | |||
5668 | tryImplicitlyCaptureThisIfImplicitMemberFunctionAccessWithDependentArgs( | |||
5669 | Sema &S, const UnresolvedMemberExpr *const UME, SourceLocation CallLoc) { | |||
5670 | ||||
5671 | if (!UME) | |||
5672 | return; | |||
5673 | ||||
5674 | LambdaScopeInfo *const CurLSI = S.getCurLambda(); | |||
5675 | // Only try and implicitly capture 'this' within a C++ Lambda if it hasn't | |||
5676 | // already been captured, or if this is an implicit member function call (if | |||
5677 | // it isn't, an attempt to capture 'this' should already have been made). | |||
5678 | if (!CurLSI || CurLSI->ImpCaptureStyle == CurLSI->ImpCap_None || | |||
5679 | !UME->isImplicitAccess() || CurLSI->isCXXThisCaptured()) | |||
5680 | return; | |||
5681 | ||||
5682 | // Check if the naming class in which the unresolved members were found is | |||
5683 | // related (same as or is a base of) to the enclosing class. | |||
5684 | ||||
5685 | if (!enclosingClassIsRelatedToClassInWhichMembersWereFound(UME, S)) | |||
5686 | return; | |||
5687 | ||||
5688 | ||||
5689 | DeclContext *EnclosingFunctionCtx = S.CurContext->getParent()->getParent(); | |||
5690 | // If the enclosing function is not dependent, then this lambda is | |||
5691 | // capture ready, so if we can capture this, do so. | |||
5692 | if (!EnclosingFunctionCtx->isDependentContext()) { | |||
5693 | // If the current lambda and all enclosing lambdas can capture 'this' - | |||
5694 | // then go ahead and capture 'this' (since our unresolved overload set | |||
5695 | // contains at least one non-static member function). | |||
5696 | if (!S.CheckCXXThisCapture(CallLoc, /*Explcit*/ false, /*Diagnose*/ false)) | |||
5697 | S.CheckCXXThisCapture(CallLoc); | |||
5698 | } else if (S.CurContext->isDependentContext()) { | |||
5699 | // ... since this is an implicit member reference, that might potentially | |||
5700 | // involve a 'this' capture, mark 'this' for potential capture in | |||
5701 | // enclosing lambdas. | |||
5702 | if (CurLSI->ImpCaptureStyle != CurLSI->ImpCap_None) | |||
5703 | CurLSI->addPotentialThisCapture(CallLoc); | |||
5704 | } | |||
5705 | } | |||
5706 | ||||
5707 | ExprResult Sema::ActOnCallExpr(Scope *Scope, Expr *Fn, SourceLocation LParenLoc, | |||
5708 | MultiExprArg ArgExprs, SourceLocation RParenLoc, | |||
5709 | Expr *ExecConfig) { | |||
5710 | ExprResult Call = | |||
5711 | BuildCallExpr(Scope, Fn, LParenLoc, ArgExprs, RParenLoc, ExecConfig); | |||
5712 | if (Call.isInvalid()) | |||
5713 | return Call; | |||
5714 | ||||
5715 | // Diagnose uses of the C++20 "ADL-only template-id call" feature in earlier | |||
5716 | // language modes. | |||
5717 | if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(Fn)) { | |||
5718 | if (ULE->hasExplicitTemplateArgs() && | |||
5719 | ULE->decls_begin() == ULE->decls_end()) { | |||
5720 | Diag(Fn->getExprLoc(), getLangOpts().CPlusPlus2a | |||
5721 | ? diag::warn_cxx17_compat_adl_only_template_id | |||
5722 | : diag::ext_adl_only_template_id) | |||
5723 | << ULE->getName(); | |||
5724 | } | |||
5725 | } | |||
5726 | ||||
5727 | return Call; | |||
5728 | } | |||
5729 | ||||
5730 | /// BuildCallExpr - Handle a call to Fn with the specified array of arguments. | |||
5731 | /// This provides the location of the left/right parens and a list of comma | |||
5732 | /// locations. | |||
5733 | ExprResult Sema::BuildCallExpr(Scope *Scope, Expr *Fn, SourceLocation LParenLoc, | |||
5734 | MultiExprArg ArgExprs, SourceLocation RParenLoc, | |||
5735 | Expr *ExecConfig, bool IsExecConfig) { | |||
5736 | // Since this might be a postfix expression, get rid of ParenListExprs. | |||
5737 | ExprResult Result = MaybeConvertParenListExprToParenExpr(Scope, Fn); | |||
5738 | if (Result.isInvalid()) return ExprError(); | |||
5739 | Fn = Result.get(); | |||
5740 | ||||
5741 | if (checkArgsForPlaceholders(*this, ArgExprs)) | |||
5742 | return ExprError(); | |||
5743 | ||||
5744 | if (getLangOpts().CPlusPlus) { | |||
5745 | // If this is a pseudo-destructor expression, build the call immediately. | |||
5746 | if (isa<CXXPseudoDestructorExpr>(Fn)) { | |||
5747 | if (!ArgExprs.empty()) { | |||
5748 | // Pseudo-destructor calls should not have any arguments. | |||
5749 | Diag(Fn->getBeginLoc(), diag::err_pseudo_dtor_call_with_args) | |||
5750 | << FixItHint::CreateRemoval( | |||
5751 | SourceRange(ArgExprs.front()->getBeginLoc(), | |||
5752 | ArgExprs.back()->getEndLoc())); | |||
5753 | } | |||
5754 | ||||
5755 | return CallExpr::Create(Context, Fn, /*Args=*/{}, Context.VoidTy, | |||
5756 | VK_RValue, RParenLoc); | |||
5757 | } | |||
5758 | if (Fn->getType() == Context.PseudoObjectTy) { | |||
5759 | ExprResult result = CheckPlaceholderExpr(Fn); | |||
5760 | if (result.isInvalid()) return ExprError(); | |||
5761 | Fn = result.get(); | |||
5762 | } | |||
5763 | ||||
5764 | // Determine whether this is a dependent call inside a C++ template, | |||
5765 | // in which case we won't do any semantic analysis now. | |||
5766 | if (Fn->isTypeDependent() || Expr::hasAnyTypeDependentArguments(ArgExprs)) { | |||
5767 | if (ExecConfig) { | |||
5768 | return CUDAKernelCallExpr::Create( | |||
5769 | Context, Fn, cast<CallExpr>(ExecConfig), ArgExprs, | |||
5770 | Context.DependentTy, VK_RValue, RParenLoc); | |||
5771 | } else { | |||
5772 | ||||
5773 | tryImplicitlyCaptureThisIfImplicitMemberFunctionAccessWithDependentArgs( | |||
5774 | *this, dyn_cast<UnresolvedMemberExpr>(Fn->IgnoreParens()), | |||
5775 | Fn->getBeginLoc()); | |||
5776 | ||||
5777 | return CallExpr::Create(Context, Fn, ArgExprs, Context.DependentTy, | |||
5778 | VK_RValue, RParenLoc); | |||
5779 | } | |||
5780 | } | |||
5781 | ||||
5782 | // Determine whether this is a call to an object (C++ [over.call.object]). | |||
5783 | if (Fn->getType()->isRecordType()) | |||
5784 | return BuildCallToObjectOfClassType(Scope, Fn, LParenLoc, ArgExprs, | |||
5785 | RParenLoc); | |||
5786 | ||||
5787 | if (Fn->getType() == Context.UnknownAnyTy) { | |||
5788 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); | |||
5789 | if (result.isInvalid()) return ExprError(); | |||
5790 | Fn = result.get(); | |||
5791 | } | |||
5792 | ||||
5793 | if (Fn->getType() == Context.BoundMemberTy) { | |||
5794 | return BuildCallToMemberFunction(Scope, Fn, LParenLoc, ArgExprs, | |||
5795 | RParenLoc); | |||
5796 | } | |||
5797 | } | |||
5798 | ||||
5799 | // Check for overloaded calls. This can happen even in C due to extensions. | |||
5800 | if (Fn->getType() == Context.OverloadTy) { | |||
5801 | OverloadExpr::FindResult find = OverloadExpr::find(Fn); | |||
5802 | ||||
5803 | // We aren't supposed to apply this logic if there's an '&' involved. | |||
5804 | if (!find.HasFormOfMemberPointer) { | |||
5805 | if (Expr::hasAnyTypeDependentArguments(ArgExprs)) | |||
5806 | return CallExpr::Create(Context, Fn, ArgExprs, Context.DependentTy, | |||
5807 | VK_RValue, RParenLoc); | |||
5808 | OverloadExpr *ovl = find.Expression; | |||
5809 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(ovl)) | |||
5810 | return BuildOverloadedCallExpr( | |||
5811 | Scope, Fn, ULE, LParenLoc, ArgExprs, RParenLoc, ExecConfig, | |||
5812 | /*AllowTypoCorrection=*/true, find.IsAddressOfOperand); | |||
5813 | return BuildCallToMemberFunction(Scope, Fn, LParenLoc, ArgExprs, | |||
5814 | RParenLoc); | |||
5815 | } | |||
5816 | } | |||
5817 | ||||
5818 | // If we're directly calling a function, get the appropriate declaration. | |||
5819 | if (Fn->getType() == Context.UnknownAnyTy) { | |||
5820 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); | |||
5821 | if (result.isInvalid()) return ExprError(); | |||
5822 | Fn = result.get(); | |||
5823 | } | |||
5824 | ||||
5825 | Expr *NakedFn = Fn->IgnoreParens(); | |||
5826 | ||||
5827 | bool CallingNDeclIndirectly = false; | |||
5828 | NamedDecl *NDecl = nullptr; | |||
5829 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) { | |||
5830 | if (UnOp->getOpcode() == UO_AddrOf) { | |||
5831 | CallingNDeclIndirectly = true; | |||
5832 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); | |||
5833 | } | |||
5834 | } | |||
5835 | ||||
5836 | if (auto *DRE = dyn_cast<DeclRefExpr>(NakedFn)) { | |||
5837 | NDecl = DRE->getDecl(); | |||
5838 | ||||
5839 | FunctionDecl *FDecl = dyn_cast<FunctionDecl>(NDecl); | |||
5840 | if (FDecl && FDecl->getBuiltinID()) { | |||
5841 | // Rewrite the function decl for this builtin by replacing parameters | |||
5842 | // with no explicit address space with the address space of the arguments | |||
5843 | // in ArgExprs. | |||
5844 | if ((FDecl = | |||
5845 | rewriteBuiltinFunctionDecl(this, Context, FDecl, ArgExprs))) { | |||
5846 | NDecl = FDecl; | |||
5847 | Fn = DeclRefExpr::Create( | |||
5848 | Context, FDecl->getQualifierLoc(), SourceLocation(), FDecl, false, | |||
5849 | SourceLocation(), FDecl->getType(), Fn->getValueKind(), FDecl, | |||
5850 | nullptr, DRE->isNonOdrUse()); | |||
5851 | } | |||
5852 | } | |||
5853 | } else if (isa<MemberExpr>(NakedFn)) | |||
5854 | NDecl = cast<MemberExpr>(NakedFn)->getMemberDecl(); | |||
5855 | ||||
5856 | if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(NDecl)) { | |||
5857 | if (CallingNDeclIndirectly && !checkAddressOfFunctionIsAvailable( | |||
5858 | FD, /*Complain=*/true, Fn->getBeginLoc())) | |||
5859 | return ExprError(); | |||
5860 | ||||
5861 | if (getLangOpts().OpenCL && checkOpenCLDisabledDecl(*FD, *Fn)) | |||
5862 | return ExprError(); | |||
5863 | ||||
5864 | checkDirectCallValidity(*this, Fn, FD, ArgExprs); | |||
5865 | } | |||
5866 | ||||
5867 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, ArgExprs, RParenLoc, | |||
5868 | ExecConfig, IsExecConfig); | |||
5869 | } | |||
5870 | ||||
5871 | /// ActOnAsTypeExpr - create a new asType (bitcast) from the arguments. | |||
5872 | /// | |||
5873 | /// __builtin_astype( value, dst type ) | |||
5874 | /// | |||
5875 | ExprResult Sema::ActOnAsTypeExpr(Expr *E, ParsedType ParsedDestTy, | |||
5876 | SourceLocation BuiltinLoc, | |||
5877 | SourceLocation RParenLoc) { | |||
5878 | ExprValueKind VK = VK_RValue; | |||
5879 | ExprObjectKind OK = OK_Ordinary; | |||
5880 | QualType DstTy = GetTypeFromParser(ParsedDestTy); | |||
5881 | QualType SrcTy = E->getType(); | |||
5882 | if (Context.getTypeSize(DstTy) != Context.getTypeSize(SrcTy)) | |||
5883 | return ExprError(Diag(BuiltinLoc, | |||
5884 | diag::err_invalid_astype_of_different_size) | |||
5885 | << DstTy | |||
5886 | << SrcTy | |||
5887 | << E->getSourceRange()); | |||
5888 | return new (Context) AsTypeExpr(E, DstTy, VK, OK, BuiltinLoc, RParenLoc); | |||
5889 | } | |||
5890 | ||||
5891 | /// ActOnConvertVectorExpr - create a new convert-vector expression from the | |||
5892 | /// provided arguments. | |||
5893 | /// | |||
5894 | /// __builtin_convertvector( value, dst type ) | |||
5895 | /// | |||
5896 | ExprResult Sema::ActOnConvertVectorExpr(Expr *E, ParsedType ParsedDestTy, | |||
5897 | SourceLocation BuiltinLoc, | |||
5898 | SourceLocation RParenLoc) { | |||
5899 | TypeSourceInfo *TInfo; | |||
5900 | GetTypeFromParser(ParsedDestTy, &TInfo); | |||
5901 | return SemaConvertVectorExpr(E, TInfo, BuiltinLoc, RParenLoc); | |||
5902 | } | |||
5903 | ||||
5904 | /// BuildResolvedCallExpr - Build a call to a resolved expression, | |||
5905 | /// i.e. an expression not of \p OverloadTy. The expression should | |||
5906 | /// unary-convert to an expression of function-pointer or | |||
5907 | /// block-pointer type. | |||
5908 | /// | |||
5909 | /// \param NDecl the declaration being called, if available | |||
5910 | ExprResult Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, | |||
5911 | SourceLocation LParenLoc, | |||
5912 | ArrayRef<Expr *> Args, | |||
5913 | SourceLocation RParenLoc, Expr *Config, | |||
5914 | bool IsExecConfig, ADLCallKind UsesADL) { | |||
5915 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); | |||
5916 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); | |||
5917 | ||||
5918 | // Functions with 'interrupt' attribute cannot be called directly. | |||
5919 | if (FDecl && FDecl->hasAttr<AnyX86InterruptAttr>()) { | |||
5920 | Diag(Fn->getExprLoc(), diag::err_anyx86_interrupt_called); | |||
5921 | return ExprError(); | |||
5922 | } | |||
5923 | ||||
5924 | // Interrupt handlers don't save off the VFP regs automatically on ARM, | |||
5925 | // so there's some risk when calling out to non-interrupt handler functions | |||
5926 | // that the callee might not preserve them. This is easy to diagnose here, | |||
5927 | // but can be very challenging to debug. | |||
5928 | if (auto *Caller = getCurFunctionDecl()) | |||
5929 | if (Caller->hasAttr<ARMInterruptAttr>()) { | |||
5930 | bool VFP = Context.getTargetInfo().hasFeature("vfp"); | |||
5931 | if (VFP && (!FDecl || !FDecl->hasAttr<ARMInterruptAttr>())) | |||
5932 | Diag(Fn->getExprLoc(), diag::warn_arm_interrupt_calling_convention); | |||
5933 | } | |||
5934 | ||||
5935 | // Promote the function operand. | |||
5936 | // We special-case function promotion here because we only allow promoting | |||
5937 | // builtin functions to function pointers in the callee of a call. | |||
5938 | ExprResult Result; | |||
5939 | QualType ResultTy; | |||
5940 | if (BuiltinID && | |||
5941 | Fn->getType()->isSpecificBuiltinType(BuiltinType::BuiltinFn)) { | |||
5942 | // Extract the return type from the (builtin) function pointer type. | |||
5943 | // FIXME Several builtins still have setType in | |||
5944 | // Sema::CheckBuiltinFunctionCall. One should review their definitions in | |||
5945 | // Builtins.def to ensure they are correct before removing setType calls. | |||
5946 | QualType FnPtrTy = Context.getPointerType(FDecl->getType()); | |||
5947 | Result = ImpCastExprToType(Fn, FnPtrTy, CK_BuiltinFnToFnPtr).get(); | |||
5948 | ResultTy = FDecl->getCallResultType(); | |||
5949 | } else { | |||
5950 | Result = CallExprUnaryConversions(Fn); | |||
5951 | ResultTy = Context.BoolTy; | |||
5952 | } | |||
5953 | if (Result.isInvalid()) | |||
5954 | return ExprError(); | |||
5955 | Fn = Result.get(); | |||
5956 | ||||
5957 | // Check for a valid function type, but only if it is not a builtin which | |||
5958 | // requires custom type checking. These will be handled by | |||
5959 | // CheckBuiltinFunctionCall below just after creation of the call expression. | |||
5960 | const FunctionType *FuncT = nullptr; | |||
5961 | if (!BuiltinID || !Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) { | |||
5962 | retry: | |||
5963 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { | |||
5964 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall | |||
5965 | // have type pointer to function". | |||
5966 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); | |||
5967 | if (!FuncT) | |||
5968 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) | |||
5969 | << Fn->getType() << Fn->getSourceRange()); | |||
5970 | } else if (const BlockPointerType *BPT = | |||
5971 | Fn->getType()->getAs<BlockPointerType>()) { | |||
5972 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); | |||
5973 | } else { | |||
5974 | // Handle calls to expressions of unknown-any type. | |||
5975 | if (Fn->getType() == Context.UnknownAnyTy) { | |||
5976 | ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); | |||
5977 | if (rewrite.isInvalid()) | |||
5978 | return ExprError(); | |||
5979 | Fn = rewrite.get(); | |||
5980 | goto retry; | |||
5981 | } | |||
5982 | ||||
5983 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) | |||
5984 | << Fn->getType() << Fn->getSourceRange()); | |||
5985 | } | |||
5986 | } | |||
5987 | ||||
5988 | // Get the number of parameters in the function prototype, if any. | |||
5989 | // We will allocate space for max(Args.size(), NumParams) arguments | |||
5990 | // in the call expression. | |||
5991 | const auto *Proto = dyn_cast_or_null<FunctionProtoType>(FuncT); | |||
5992 | unsigned NumParams = Proto ? Proto->getNumParams() : 0; | |||
5993 | ||||
5994 | CallExpr *TheCall; | |||
5995 | if (Config) { | |||
5996 | assert(UsesADL == ADLCallKind::NotADL &&((UsesADL == ADLCallKind::NotADL && "CUDAKernelCallExpr should not use ADL" ) ? static_cast<void> (0) : __assert_fail ("UsesADL == ADLCallKind::NotADL && \"CUDAKernelCallExpr should not use ADL\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 5997, __PRETTY_FUNCTION__)) | |||
5997 | "CUDAKernelCallExpr should not use ADL")((UsesADL == ADLCallKind::NotADL && "CUDAKernelCallExpr should not use ADL" ) ? static_cast<void> (0) : __assert_fail ("UsesADL == ADLCallKind::NotADL && \"CUDAKernelCallExpr should not use ADL\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 5997, __PRETTY_FUNCTION__)); | |||
5998 | TheCall = | |||
5999 | CUDAKernelCallExpr::Create(Context, Fn, cast<CallExpr>(Config), Args, | |||
6000 | ResultTy, VK_RValue, RParenLoc, NumParams); | |||
6001 | } else { | |||
6002 | TheCall = CallExpr::Create(Context, Fn, Args, ResultTy, VK_RValue, | |||
6003 | RParenLoc, NumParams, UsesADL); | |||
6004 | } | |||
6005 | ||||
6006 | if (!getLangOpts().CPlusPlus) { | |||
6007 | // Forget about the nulled arguments since typo correction | |||
6008 | // do not handle them well. | |||
6009 | TheCall->shrinkNumArgs(Args.size()); | |||
6010 | // C cannot always handle TypoExpr nodes in builtin calls and direct | |||
6011 | // function calls as their argument checking don't necessarily handle | |||
6012 | // dependent types properly, so make sure any TypoExprs have been | |||
6013 | // dealt with. | |||
6014 | ExprResult Result = CorrectDelayedTyposInExpr(TheCall); | |||
6015 | if (!Result.isUsable()) return ExprError(); | |||
6016 | CallExpr *TheOldCall = TheCall; | |||
6017 | TheCall = dyn_cast<CallExpr>(Result.get()); | |||
6018 | bool CorrectedTypos = TheCall != TheOldCall; | |||
6019 | if (!TheCall) return Result; | |||
6020 | Args = llvm::makeArrayRef(TheCall->getArgs(), TheCall->getNumArgs()); | |||
6021 | ||||
6022 | // A new call expression node was created if some typos were corrected. | |||
6023 | // However it may not have been constructed with enough storage. In this | |||
6024 | // case, rebuild the node with enough storage. The waste of space is | |||
6025 | // immaterial since this only happens when some typos were corrected. | |||
6026 | if (CorrectedTypos && Args.size() < NumParams) { | |||
6027 | if (Config) | |||
6028 | TheCall = CUDAKernelCallExpr::Create( | |||
6029 | Context, Fn, cast<CallExpr>(Config), Args, ResultTy, VK_RValue, | |||
6030 | RParenLoc, NumParams); | |||
6031 | else | |||
6032 | TheCall = CallExpr::Create(Context, Fn, Args, ResultTy, VK_RValue, | |||
6033 | RParenLoc, NumParams, UsesADL); | |||
6034 | } | |||
6035 | // We can now handle the nulled arguments for the default arguments. | |||
6036 | TheCall->setNumArgsUnsafe(std::max<unsigned>(Args.size(), NumParams)); | |||
6037 | } | |||
6038 | ||||
6039 | // Bail out early if calling a builtin with custom type checking. | |||
6040 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) | |||
6041 | return CheckBuiltinFunctionCall(FDecl, BuiltinID, TheCall); | |||
6042 | ||||
6043 | if (getLangOpts().CUDA) { | |||
6044 | if (Config) { | |||
6045 | // CUDA: Kernel calls must be to global functions | |||
6046 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) | |||
6047 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) | |||
6048 | << FDecl << Fn->getSourceRange()); | |||
6049 | ||||
6050 | // CUDA: Kernel function must have 'void' return type | |||
6051 | if (!FuncT->getReturnType()->isVoidType() && | |||
6052 | !FuncT->getReturnType()->getAs<AutoType>() && | |||
6053 | !FuncT->getReturnType()->isInstantiationDependentType()) | |||
6054 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) | |||
6055 | << Fn->getType() << Fn->getSourceRange()); | |||
6056 | } else { | |||
6057 | // CUDA: Calls to global functions must be configured | |||
6058 | if (FDecl && FDecl->hasAttr<CUDAGlobalAttr>()) | |||
6059 | return ExprError(Diag(LParenLoc, diag::err_global_call_not_config) | |||
6060 | << FDecl << Fn->getSourceRange()); | |||
6061 | } | |||
6062 | } | |||
6063 | ||||
6064 | // Check for a valid return type | |||
6065 | if (CheckCallReturnType(FuncT->getReturnType(), Fn->getBeginLoc(), TheCall, | |||
6066 | FDecl)) | |||
6067 | return ExprError(); | |||
6068 | ||||
6069 | // We know the result type of the call, set it. | |||
6070 | TheCall->setType(FuncT->getCallResultType(Context)); | |||
6071 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getReturnType())); | |||
6072 | ||||
6073 | if (Proto) { | |||
6074 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, RParenLoc, | |||
6075 | IsExecConfig)) | |||
6076 | return ExprError(); | |||
6077 | } else { | |||
6078 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!")((isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!" ) ? static_cast<void> (0) : __assert_fail ("isa<FunctionNoProtoType>(FuncT) && \"Unknown FunctionType!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6078, __PRETTY_FUNCTION__)); | |||
6079 | ||||
6080 | if (FDecl) { | |||
6081 | // Check if we have too few/too many template arguments, based | |||
6082 | // on our knowledge of the function definition. | |||
6083 | const FunctionDecl *Def = nullptr; | |||
6084 | if (FDecl->hasBody(Def) && Args.size() != Def->param_size()) { | |||
6085 | Proto = Def->getType()->getAs<FunctionProtoType>(); | |||
6086 | if (!Proto || !(Proto->isVariadic() && Args.size() >= Def->param_size())) | |||
6087 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) | |||
6088 | << (Args.size() > Def->param_size()) << FDecl << Fn->getSourceRange(); | |||
6089 | } | |||
6090 | ||||
6091 | // If the function we're calling isn't a function prototype, but we have | |||
6092 | // a function prototype from a prior declaratiom, use that prototype. | |||
6093 | if (!FDecl->hasPrototype()) | |||
6094 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); | |||
6095 | } | |||
6096 | ||||
6097 | // Promote the arguments (C99 6.5.2.2p6). | |||
6098 | for (unsigned i = 0, e = Args.size(); i != e; i++) { | |||
6099 | Expr *Arg = Args[i]; | |||
6100 | ||||
6101 | if (Proto && i < Proto->getNumParams()) { | |||
6102 | InitializedEntity Entity = InitializedEntity::InitializeParameter( | |||
6103 | Context, Proto->getParamType(i), Proto->isParamConsumed(i)); | |||
6104 | ExprResult ArgE = | |||
6105 | PerformCopyInitialization(Entity, SourceLocation(), Arg); | |||
6106 | if (ArgE.isInvalid()) | |||
6107 | return true; | |||
6108 | ||||
6109 | Arg = ArgE.getAs<Expr>(); | |||
6110 | ||||
6111 | } else { | |||
6112 | ExprResult ArgE = DefaultArgumentPromotion(Arg); | |||
6113 | ||||
6114 | if (ArgE.isInvalid()) | |||
6115 | return true; | |||
6116 | ||||
6117 | Arg = ArgE.getAs<Expr>(); | |||
6118 | } | |||
6119 | ||||
6120 | if (RequireCompleteType(Arg->getBeginLoc(), Arg->getType(), | |||
6121 | diag::err_call_incomplete_argument, Arg)) | |||
6122 | return ExprError(); | |||
6123 | ||||
6124 | TheCall->setArg(i, Arg); | |||
6125 | } | |||
6126 | } | |||
6127 | ||||
6128 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) | |||
6129 | if (!Method->isStatic()) | |||
6130 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) | |||
6131 | << Fn->getSourceRange()); | |||
6132 | ||||
6133 | // Check for sentinels | |||
6134 | if (NDecl) | |||
6135 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args); | |||
6136 | ||||
6137 | // Do special checking on direct calls to functions. | |||
6138 | if (FDecl) { | |||
6139 | if (CheckFunctionCall(FDecl, TheCall, Proto)) | |||
6140 | return ExprError(); | |||
6141 | ||||
6142 | checkFortifiedBuiltinMemoryFunction(FDecl, TheCall); | |||
6143 | ||||
6144 | if (BuiltinID) | |||
6145 | return CheckBuiltinFunctionCall(FDecl, BuiltinID, TheCall); | |||
6146 | } else if (NDecl) { | |||
6147 | if (CheckPointerCall(NDecl, TheCall, Proto)) | |||
6148 | return ExprError(); | |||
6149 | } else { | |||
6150 | if (CheckOtherCall(TheCall, Proto)) | |||
6151 | return ExprError(); | |||
6152 | } | |||
6153 | ||||
6154 | return CheckForImmediateInvocation(MaybeBindToTemporary(TheCall), FDecl); | |||
6155 | } | |||
6156 | ||||
6157 | ExprResult | |||
6158 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, | |||
6159 | SourceLocation RParenLoc, Expr *InitExpr) { | |||
6160 | assert(Ty && "ActOnCompoundLiteral(): missing type")((Ty && "ActOnCompoundLiteral(): missing type") ? static_cast <void> (0) : __assert_fail ("Ty && \"ActOnCompoundLiteral(): missing type\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6160, __PRETTY_FUNCTION__)); | |||
6161 | assert(InitExpr && "ActOnCompoundLiteral(): missing expression")((InitExpr && "ActOnCompoundLiteral(): missing expression" ) ? static_cast<void> (0) : __assert_fail ("InitExpr && \"ActOnCompoundLiteral(): missing expression\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6161, __PRETTY_FUNCTION__)); | |||
6162 | ||||
6163 | TypeSourceInfo *TInfo; | |||
6164 | QualType literalType = GetTypeFromParser(Ty, &TInfo); | |||
6165 | if (!TInfo) | |||
6166 | TInfo = Context.getTrivialTypeSourceInfo(literalType); | |||
6167 | ||||
6168 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); | |||
6169 | } | |||
6170 | ||||
6171 | ExprResult | |||
6172 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, | |||
6173 | SourceLocation RParenLoc, Expr *LiteralExpr) { | |||
6174 | QualType literalType = TInfo->getType(); | |||
6175 | ||||
6176 | if (literalType->isArrayType()) { | |||
6177 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), | |||
6178 | diag::err_illegal_decl_array_incomplete_type, | |||
6179 | SourceRange(LParenLoc, | |||
6180 | LiteralExpr->getSourceRange().getEnd()))) | |||
6181 | return ExprError(); | |||
6182 | if (literalType->isVariableArrayType()) | |||
6183 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) | |||
6184 | << SourceRange(LParenLoc, LiteralExpr->getSourceRange().getEnd())); | |||
6185 | } else if (!literalType->isDependentType() && | |||
6186 | RequireCompleteType(LParenLoc, literalType, | |||
6187 | diag::err_typecheck_decl_incomplete_type, | |||
6188 | SourceRange(LParenLoc, LiteralExpr->getSourceRange().getEnd()))) | |||
6189 | return ExprError(); | |||
6190 | ||||
6191 | InitializedEntity Entity | |||
6192 | = InitializedEntity::InitializeCompoundLiteralInit(TInfo); | |||
6193 | InitializationKind Kind | |||
6194 | = InitializationKind::CreateCStyleCast(LParenLoc, | |||
6195 | SourceRange(LParenLoc, RParenLoc), | |||
6196 | /*InitList=*/true); | |||
6197 | InitializationSequence InitSeq(*this, Entity, Kind, LiteralExpr); | |||
6198 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, LiteralExpr, | |||
6199 | &literalType); | |||
6200 | if (Result.isInvalid()) | |||
6201 | return ExprError(); | |||
6202 | LiteralExpr = Result.get(); | |||
6203 | ||||
6204 | bool isFileScope = !CurContext->isFunctionOrMethod(); | |||
6205 | ||||
6206 | // In C, compound literals are l-values for some reason. | |||
6207 | // For GCC compatibility, in C++, file-scope array compound literals with | |||
6208 | // constant initializers are also l-values, and compound literals are | |||
6209 | // otherwise prvalues. | |||
6210 | // | |||
6211 | // (GCC also treats C++ list-initialized file-scope array prvalues with | |||
6212 | // constant initializers as l-values, but that's non-conforming, so we don't | |||
6213 | // follow it there.) | |||
6214 | // | |||
6215 | // FIXME: It would be better to handle the lvalue cases as materializing and | |||
6216 | // lifetime-extending a temporary object, but our materialized temporaries | |||
6217 | // representation only supports lifetime extension from a variable, not "out | |||
6218 | // of thin air". | |||
6219 | // FIXME: For C++, we might want to instead lifetime-extend only if a pointer | |||
6220 | // is bound to the result of applying array-to-pointer decay to the compound | |||
6221 | // literal. | |||
6222 | // FIXME: GCC supports compound literals of reference type, which should | |||
6223 | // obviously have a value kind derived from the kind of reference involved. | |||
6224 | ExprValueKind VK = | |||
6225 | (getLangOpts().CPlusPlus && !(isFileScope && literalType->isArrayType())) | |||
6226 | ? VK_RValue | |||
6227 | : VK_LValue; | |||
6228 | ||||
6229 | if (isFileScope) | |||
6230 | if (auto ILE = dyn_cast<InitListExpr>(LiteralExpr)) | |||
6231 | for (unsigned i = 0, j = ILE->getNumInits(); i != j; i++) { | |||
6232 | Expr *Init = ILE->getInit(i); | |||
6233 | ILE->setInit(i, ConstantExpr::Create(Context, Init)); | |||
6234 | } | |||
6235 | ||||
6236 | auto *E = new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, | |||
6237 | VK, LiteralExpr, isFileScope); | |||
6238 | if (isFileScope) { | |||
6239 | if (!LiteralExpr->isTypeDependent() && | |||
6240 | !LiteralExpr->isValueDependent() && | |||
6241 | !literalType->isDependentType()) // C99 6.5.2.5p3 | |||
6242 | if (CheckForConstantInitializer(LiteralExpr, literalType)) | |||
6243 | return ExprError(); | |||
6244 | } else if (literalType.getAddressSpace() != LangAS::opencl_private && | |||
6245 | literalType.getAddressSpace() != LangAS::Default) { | |||
6246 | // Embedded-C extensions to C99 6.5.2.5: | |||
6247 | // "If the compound literal occurs inside the body of a function, the | |||
6248 | // type name shall not be qualified by an address-space qualifier." | |||
6249 | Diag(LParenLoc, diag::err_compound_literal_with_address_space) | |||
6250 | << SourceRange(LParenLoc, LiteralExpr->getSourceRange().getEnd()); | |||
6251 | return ExprError(); | |||
6252 | } | |||
6253 | ||||
6254 | // Compound literals that have automatic storage duration are destroyed at | |||
6255 | // the end of the scope. Emit diagnostics if it is or contains a C union type | |||
6256 | // that is non-trivial to destruct. | |||
6257 | if (!isFileScope) | |||
6258 | if (E->getType().hasNonTrivialToPrimitiveDestructCUnion()) | |||
6259 | checkNonTrivialCUnion(E->getType(), E->getExprLoc(), | |||
6260 | NTCUC_CompoundLiteral, NTCUK_Destruct); | |||
6261 | ||||
6262 | if (E->getType().hasNonTrivialToPrimitiveDefaultInitializeCUnion() || | |||
6263 | E->getType().hasNonTrivialToPrimitiveCopyCUnion()) | |||
6264 | checkNonTrivialCUnionInInitializer(E->getInitializer(), | |||
6265 | E->getInitializer()->getExprLoc()); | |||
6266 | ||||
6267 | return MaybeBindToTemporary(E); | |||
6268 | } | |||
6269 | ||||
6270 | ExprResult | |||
6271 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList, | |||
6272 | SourceLocation RBraceLoc) { | |||
6273 | // Only produce each kind of designated initialization diagnostic once. | |||
6274 | SourceLocation FirstDesignator; | |||
6275 | bool DiagnosedArrayDesignator = false; | |||
6276 | bool DiagnosedNestedDesignator = false; | |||
6277 | bool DiagnosedMixedDesignator = false; | |||
6278 | ||||
6279 | // Check that any designated initializers are syntactically valid in the | |||
6280 | // current language mode. | |||
6281 | for (unsigned I = 0, E = InitArgList.size(); I != E; ++I) { | |||
6282 | if (auto *DIE = dyn_cast<DesignatedInitExpr>(InitArgList[I])) { | |||
6283 | if (FirstDesignator.isInvalid()) | |||
6284 | FirstDesignator = DIE->getBeginLoc(); | |||
6285 | ||||
6286 | if (!getLangOpts().CPlusPlus) | |||
6287 | break; | |||
6288 | ||||
6289 | if (!DiagnosedNestedDesignator && DIE->size() > 1) { | |||
6290 | DiagnosedNestedDesignator = true; | |||
6291 | Diag(DIE->getBeginLoc(), diag::ext_designated_init_nested) | |||
6292 | << DIE->getDesignatorsSourceRange(); | |||
6293 | } | |||
6294 | ||||
6295 | for (auto &Desig : DIE->designators()) { | |||
6296 | if (!Desig.isFieldDesignator() && !DiagnosedArrayDesignator) { | |||
6297 | DiagnosedArrayDesignator = true; | |||
6298 | Diag(Desig.getBeginLoc(), diag::ext_designated_init_array) | |||
6299 | << Desig.getSourceRange(); | |||
6300 | } | |||
6301 | } | |||
6302 | ||||
6303 | if (!DiagnosedMixedDesignator && | |||
6304 | !isa<DesignatedInitExpr>(InitArgList[0])) { | |||
6305 | DiagnosedMixedDesignator = true; | |||
6306 | Diag(DIE->getBeginLoc(), diag::ext_designated_init_mixed) | |||
6307 | << DIE->getSourceRange(); | |||
6308 | Diag(InitArgList[0]->getBeginLoc(), diag::note_designated_init_mixed) | |||
6309 | << InitArgList[0]->getSourceRange(); | |||
6310 | } | |||
6311 | } else if (getLangOpts().CPlusPlus && !DiagnosedMixedDesignator && | |||
6312 | isa<DesignatedInitExpr>(InitArgList[0])) { | |||
6313 | DiagnosedMixedDesignator = true; | |||
6314 | auto *DIE = cast<DesignatedInitExpr>(InitArgList[0]); | |||
6315 | Diag(DIE->getBeginLoc(), diag::ext_designated_init_mixed) | |||
6316 | << DIE->getSourceRange(); | |||
6317 | Diag(InitArgList[I]->getBeginLoc(), diag::note_designated_init_mixed) | |||
6318 | << InitArgList[I]->getSourceRange(); | |||
6319 | } | |||
6320 | } | |||
6321 | ||||
6322 | if (FirstDesignator.isValid()) { | |||
6323 | // Only diagnose designated initiaization as a C++20 extension if we didn't | |||
6324 | // already diagnose use of (non-C++20) C99 designator syntax. | |||
6325 | if (getLangOpts().CPlusPlus && !DiagnosedArrayDesignator && | |||
6326 | !DiagnosedNestedDesignator && !DiagnosedMixedDesignator) { | |||
6327 | Diag(FirstDesignator, getLangOpts().CPlusPlus2a | |||
6328 | ? diag::warn_cxx17_compat_designated_init | |||
6329 | : diag::ext_cxx_designated_init); | |||
6330 | } else if (!getLangOpts().CPlusPlus && !getLangOpts().C99) { | |||
6331 | Diag(FirstDesignator, diag::ext_designated_init); | |||
6332 | } | |||
6333 | } | |||
6334 | ||||
6335 | return BuildInitList(LBraceLoc, InitArgList, RBraceLoc); | |||
6336 | } | |||
6337 | ||||
6338 | ExprResult | |||
6339 | Sema::BuildInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList, | |||
6340 | SourceLocation RBraceLoc) { | |||
6341 | // Semantic analysis for initializers is done by ActOnDeclarator() and | |||
6342 | // CheckInitializer() - it requires knowledge of the object being initialized. | |||
6343 | ||||
6344 | // Immediately handle non-overload placeholders. Overloads can be | |||
6345 | // resolved contextually, but everything else here can't. | |||
6346 | for (unsigned I = 0, E = InitArgList.size(); I != E; ++I) { | |||
6347 | if (InitArgList[I]->getType()->isNonOverloadPlaceholderType()) { | |||
6348 | ExprResult result = CheckPlaceholderExpr(InitArgList[I]); | |||
6349 | ||||
6350 | // Ignore failures; dropping the entire initializer list because | |||
6351 | // of one failure would be terrible for indexing/etc. | |||
6352 | if (result.isInvalid()) continue; | |||
6353 | ||||
6354 | InitArgList[I] = result.get(); | |||
6355 | } | |||
6356 | } | |||
6357 | ||||
6358 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitArgList, | |||
6359 | RBraceLoc); | |||
6360 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. | |||
6361 | return E; | |||
6362 | } | |||
6363 | ||||
6364 | /// Do an explicit extend of the given block pointer if we're in ARC. | |||
6365 | void Sema::maybeExtendBlockObject(ExprResult &E) { | |||
6366 | assert(E.get()->getType()->isBlockPointerType())((E.get()->getType()->isBlockPointerType()) ? static_cast <void> (0) : __assert_fail ("E.get()->getType()->isBlockPointerType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6366, __PRETTY_FUNCTION__)); | |||
6367 | assert(E.get()->isRValue())((E.get()->isRValue()) ? static_cast<void> (0) : __assert_fail ("E.get()->isRValue()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6367, __PRETTY_FUNCTION__)); | |||
6368 | ||||
6369 | // Only do this in an r-value context. | |||
6370 | if (!getLangOpts().ObjCAutoRefCount) return; | |||
6371 | ||||
6372 | E = ImplicitCastExpr::Create(Context, E.get()->getType(), | |||
6373 | CK_ARCExtendBlockObject, E.get(), | |||
6374 | /*base path*/ nullptr, VK_RValue); | |||
6375 | Cleanup.setExprNeedsCleanups(true); | |||
6376 | } | |||
6377 | ||||
6378 | /// Prepare a conversion of the given expression to an ObjC object | |||
6379 | /// pointer type. | |||
6380 | CastKind Sema::PrepareCastToObjCObjectPointer(ExprResult &E) { | |||
6381 | QualType type = E.get()->getType(); | |||
6382 | if (type->isObjCObjectPointerType()) { | |||
6383 | return CK_BitCast; | |||
6384 | } else if (type->isBlockPointerType()) { | |||
6385 | maybeExtendBlockObject(E); | |||
6386 | return CK_BlockPointerToObjCPointerCast; | |||
6387 | } else { | |||
6388 | assert(type->isPointerType())((type->isPointerType()) ? static_cast<void> (0) : __assert_fail ("type->isPointerType()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6388, __PRETTY_FUNCTION__)); | |||
6389 | return CK_CPointerToObjCPointerCast; | |||
6390 | } | |||
6391 | } | |||
6392 | ||||
6393 | /// Prepares for a scalar cast, performing all the necessary stages | |||
6394 | /// except the final cast and returning the kind required. | |||
6395 | CastKind Sema::PrepareScalarCast(ExprResult &Src, QualType DestTy) { | |||
6396 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. | |||
6397 | // Also, callers should have filtered out the invalid cases with | |||
6398 | // pointers. Everything else should be possible. | |||
6399 | ||||
6400 | QualType SrcTy = Src.get()->getType(); | |||
6401 | if (Context.hasSameUnqualifiedType(SrcTy, DestTy)) | |||
6402 | return CK_NoOp; | |||
6403 | ||||
6404 | switch (Type::ScalarTypeKind SrcKind = SrcTy->getScalarTypeKind()) { | |||
6405 | case Type::STK_MemberPointer: | |||
6406 | llvm_unreachable("member pointer type in C")::llvm::llvm_unreachable_internal("member pointer type in C", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6406); | |||
6407 | ||||
6408 | case Type::STK_CPointer: | |||
6409 | case Type::STK_BlockPointer: | |||
6410 | case Type::STK_ObjCObjectPointer: | |||
6411 | switch (DestTy->getScalarTypeKind()) { | |||
6412 | case Type::STK_CPointer: { | |||
6413 | LangAS SrcAS = SrcTy->getPointeeType().getAddressSpace(); | |||
6414 | LangAS DestAS = DestTy->getPointeeType().getAddressSpace(); | |||
6415 | if (SrcAS != DestAS) | |||
6416 | return CK_AddressSpaceConversion; | |||
6417 | if (Context.hasCvrSimilarType(SrcTy, DestTy)) | |||
6418 | return CK_NoOp; | |||
6419 | return CK_BitCast; | |||
6420 | } | |||
6421 | case Type::STK_BlockPointer: | |||
6422 | return (SrcKind == Type::STK_BlockPointer | |||
6423 | ? CK_BitCast : CK_AnyPointerToBlockPointerCast); | |||
6424 | case Type::STK_ObjCObjectPointer: | |||
6425 | if (SrcKind == Type::STK_ObjCObjectPointer) | |||
6426 | return CK_BitCast; | |||
6427 | if (SrcKind == Type::STK_CPointer) | |||
6428 | return CK_CPointerToObjCPointerCast; | |||
6429 | maybeExtendBlockObject(Src); | |||
6430 | return CK_BlockPointerToObjCPointerCast; | |||
6431 | case Type::STK_Bool: | |||
6432 | return CK_PointerToBoolean; | |||
6433 | case Type::STK_Integral: | |||
6434 | return CK_PointerToIntegral; | |||
6435 | case Type::STK_Floating: | |||
6436 | case Type::STK_FloatingComplex: | |||
6437 | case Type::STK_IntegralComplex: | |||
6438 | case Type::STK_MemberPointer: | |||
6439 | case Type::STK_FixedPoint: | |||
6440 | llvm_unreachable("illegal cast from pointer")::llvm::llvm_unreachable_internal("illegal cast from pointer" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6440); | |||
6441 | } | |||
6442 | llvm_unreachable("Should have returned before this")::llvm::llvm_unreachable_internal("Should have returned before this" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6442); | |||
6443 | ||||
6444 | case Type::STK_FixedPoint: | |||
6445 | switch (DestTy->getScalarTypeKind()) { | |||
6446 | case Type::STK_FixedPoint: | |||
6447 | return CK_FixedPointCast; | |||
6448 | case Type::STK_Bool: | |||
6449 | return CK_FixedPointToBoolean; | |||
6450 | case Type::STK_Integral: | |||
6451 | return CK_FixedPointToIntegral; | |||
6452 | case Type::STK_Floating: | |||
6453 | case Type::STK_IntegralComplex: | |||
6454 | case Type::STK_FloatingComplex: | |||
6455 | Diag(Src.get()->getExprLoc(), | |||
6456 | diag::err_unimplemented_conversion_with_fixed_point_type) | |||
6457 | << DestTy; | |||
6458 | return CK_IntegralCast; | |||
6459 | case Type::STK_CPointer: | |||
6460 | case Type::STK_ObjCObjectPointer: | |||
6461 | case Type::STK_BlockPointer: | |||
6462 | case Type::STK_MemberPointer: | |||
6463 | llvm_unreachable("illegal cast to pointer type")::llvm::llvm_unreachable_internal("illegal cast to pointer type" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6463); | |||
6464 | } | |||
6465 | llvm_unreachable("Should have returned before this")::llvm::llvm_unreachable_internal("Should have returned before this" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6465); | |||
6466 | ||||
6467 | case Type::STK_Bool: // casting from bool is like casting from an integer | |||
6468 | case Type::STK_Integral: | |||
6469 | switch (DestTy->getScalarTypeKind()) { | |||
6470 | case Type::STK_CPointer: | |||
6471 | case Type::STK_ObjCObjectPointer: | |||
6472 | case Type::STK_BlockPointer: | |||
6473 | if (Src.get()->isNullPointerConstant(Context, | |||
6474 | Expr::NPC_ValueDependentIsNull)) | |||
6475 | return CK_NullToPointer; | |||
6476 | return CK_IntegralToPointer; | |||
6477 | case Type::STK_Bool: | |||
6478 | return CK_IntegralToBoolean; | |||
6479 | case Type::STK_Integral: | |||
6480 | return CK_IntegralCast; | |||
6481 | case Type::STK_Floating: | |||
6482 | return CK_IntegralToFloating; | |||
6483 | case Type::STK_IntegralComplex: | |||
6484 | Src = ImpCastExprToType(Src.get(), | |||
6485 | DestTy->castAs<ComplexType>()->getElementType(), | |||
6486 | CK_IntegralCast); | |||
6487 | return CK_IntegralRealToComplex; | |||
6488 | case Type::STK_FloatingComplex: | |||
6489 | Src = ImpCastExprToType(Src.get(), | |||
6490 | DestTy->castAs<ComplexType>()->getElementType(), | |||
6491 | CK_IntegralToFloating); | |||
6492 | return CK_FloatingRealToComplex; | |||
6493 | case Type::STK_MemberPointer: | |||
6494 | llvm_unreachable("member pointer type in C")::llvm::llvm_unreachable_internal("member pointer type in C", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6494); | |||
6495 | case Type::STK_FixedPoint: | |||
6496 | return CK_IntegralToFixedPoint; | |||
6497 | } | |||
6498 | llvm_unreachable("Should have returned before this")::llvm::llvm_unreachable_internal("Should have returned before this" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6498); | |||
6499 | ||||
6500 | case Type::STK_Floating: | |||
6501 | switch (DestTy->getScalarTypeKind()) { | |||
6502 | case Type::STK_Floating: | |||
6503 | return CK_FloatingCast; | |||
6504 | case Type::STK_Bool: | |||
6505 | return CK_FloatingToBoolean; | |||
6506 | case Type::STK_Integral: | |||
6507 | return CK_FloatingToIntegral; | |||
6508 | case Type::STK_FloatingComplex: | |||
6509 | Src = ImpCastExprToType(Src.get(), | |||
6510 | DestTy->castAs<ComplexType>()->getElementType(), | |||
6511 | CK_FloatingCast); | |||
6512 | return CK_FloatingRealToComplex; | |||
6513 | case Type::STK_IntegralComplex: | |||
6514 | Src = ImpCastExprToType(Src.get(), | |||
6515 | DestTy->castAs<ComplexType>()->getElementType(), | |||
6516 | CK_FloatingToIntegral); | |||
6517 | return CK_IntegralRealToComplex; | |||
6518 | case Type::STK_CPointer: | |||
6519 | case Type::STK_ObjCObjectPointer: | |||
6520 | case Type::STK_BlockPointer: | |||
6521 | llvm_unreachable("valid float->pointer cast?")::llvm::llvm_unreachable_internal("valid float->pointer cast?" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6521); | |||
6522 | case Type::STK_MemberPointer: | |||
6523 | llvm_unreachable("member pointer type in C")::llvm::llvm_unreachable_internal("member pointer type in C", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6523); | |||
6524 | case Type::STK_FixedPoint: | |||
6525 | Diag(Src.get()->getExprLoc(), | |||
6526 | diag::err_unimplemented_conversion_with_fixed_point_type) | |||
6527 | << SrcTy; | |||
6528 | return CK_IntegralCast; | |||
6529 | } | |||
6530 | llvm_unreachable("Should have returned before this")::llvm::llvm_unreachable_internal("Should have returned before this" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6530); | |||
6531 | ||||
6532 | case Type::STK_FloatingComplex: | |||
6533 | switch (DestTy->getScalarTypeKind()) { | |||
6534 | case Type::STK_FloatingComplex: | |||
6535 | return CK_FloatingComplexCast; | |||
6536 | case Type::STK_IntegralComplex: | |||
6537 | return CK_FloatingComplexToIntegralComplex; | |||
6538 | case Type::STK_Floating: { | |||
6539 | QualType ET = SrcTy->castAs<ComplexType>()->getElementType(); | |||
6540 | if (Context.hasSameType(ET, DestTy)) | |||
6541 | return CK_FloatingComplexToReal; | |||
6542 | Src = ImpCastExprToType(Src.get(), ET, CK_FloatingComplexToReal); | |||
6543 | return CK_FloatingCast; | |||
6544 | } | |||
6545 | case Type::STK_Bool: | |||
6546 | return CK_FloatingComplexToBoolean; | |||
6547 | case Type::STK_Integral: | |||
6548 | Src = ImpCastExprToType(Src.get(), | |||
6549 | SrcTy->castAs<ComplexType>()->getElementType(), | |||
6550 | CK_FloatingComplexToReal); | |||
6551 | return CK_FloatingToIntegral; | |||
6552 | case Type::STK_CPointer: | |||
6553 | case Type::STK_ObjCObjectPointer: | |||
6554 | case Type::STK_BlockPointer: | |||
6555 | llvm_unreachable("valid complex float->pointer cast?")::llvm::llvm_unreachable_internal("valid complex float->pointer cast?" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6555); | |||
6556 | case Type::STK_MemberPointer: | |||
6557 | llvm_unreachable("member pointer type in C")::llvm::llvm_unreachable_internal("member pointer type in C", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6557); | |||
6558 | case Type::STK_FixedPoint: | |||
6559 | Diag(Src.get()->getExprLoc(), | |||
6560 | diag::err_unimplemented_conversion_with_fixed_point_type) | |||
6561 | << SrcTy; | |||
6562 | return CK_IntegralCast; | |||
6563 | } | |||
6564 | llvm_unreachable("Should have returned before this")::llvm::llvm_unreachable_internal("Should have returned before this" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6564); | |||
6565 | ||||
6566 | case Type::STK_IntegralComplex: | |||
6567 | switch (DestTy->getScalarTypeKind()) { | |||
6568 | case Type::STK_FloatingComplex: | |||
6569 | return CK_IntegralComplexToFloatingComplex; | |||
6570 | case Type::STK_IntegralComplex: | |||
6571 | return CK_IntegralComplexCast; | |||
6572 | case Type::STK_Integral: { | |||
6573 | QualType ET = SrcTy->castAs<ComplexType>()->getElementType(); | |||
6574 | if (Context.hasSameType(ET, DestTy)) | |||
6575 | return CK_IntegralComplexToReal; | |||
6576 | Src = ImpCastExprToType(Src.get(), ET, CK_IntegralComplexToReal); | |||
6577 | return CK_IntegralCast; | |||
6578 | } | |||
6579 | case Type::STK_Bool: | |||
6580 | return CK_IntegralComplexToBoolean; | |||
6581 | case Type::STK_Floating: | |||
6582 | Src = ImpCastExprToType(Src.get(), | |||
6583 | SrcTy->castAs<ComplexType>()->getElementType(), | |||
6584 | CK_IntegralComplexToReal); | |||
6585 | return CK_IntegralToFloating; | |||
6586 | case Type::STK_CPointer: | |||
6587 | case Type::STK_ObjCObjectPointer: | |||
6588 | case Type::STK_BlockPointer: | |||
6589 | llvm_unreachable("valid complex int->pointer cast?")::llvm::llvm_unreachable_internal("valid complex int->pointer cast?" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6589); | |||
6590 | case Type::STK_MemberPointer: | |||
6591 | llvm_unreachable("member pointer type in C")::llvm::llvm_unreachable_internal("member pointer type in C", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6591); | |||
6592 | case Type::STK_FixedPoint: | |||
6593 | Diag(Src.get()->getExprLoc(), | |||
6594 | diag::err_unimplemented_conversion_with_fixed_point_type) | |||
6595 | << SrcTy; | |||
6596 | return CK_IntegralCast; | |||
6597 | } | |||
6598 | llvm_unreachable("Should have returned before this")::llvm::llvm_unreachable_internal("Should have returned before this" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6598); | |||
6599 | } | |||
6600 | ||||
6601 | llvm_unreachable("Unhandled scalar cast")::llvm::llvm_unreachable_internal("Unhandled scalar cast", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6601); | |||
6602 | } | |||
6603 | ||||
6604 | static bool breakDownVectorType(QualType type, uint64_t &len, | |||
6605 | QualType &eltType) { | |||
6606 | // Vectors are simple. | |||
6607 | if (const VectorType *vecType = type->getAs<VectorType>()) { | |||
6608 | len = vecType->getNumElements(); | |||
6609 | eltType = vecType->getElementType(); | |||
6610 | assert(eltType->isScalarType())((eltType->isScalarType()) ? static_cast<void> (0) : __assert_fail ("eltType->isScalarType()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6610, __PRETTY_FUNCTION__)); | |||
6611 | return true; | |||
6612 | } | |||
6613 | ||||
6614 | // We allow lax conversion to and from non-vector types, but only if | |||
6615 | // they're real types (i.e. non-complex, non-pointer scalar types). | |||
6616 | if (!type->isRealType()) return false; | |||
6617 | ||||
6618 | len = 1; | |||
6619 | eltType = type; | |||
6620 | return true; | |||
6621 | } | |||
6622 | ||||
6623 | /// Are the two types lax-compatible vector types? That is, given | |||
6624 | /// that one of them is a vector, do they have equal storage sizes, | |||
6625 | /// where the storage size is the number of elements times the element | |||
6626 | /// size? | |||
6627 | /// | |||
6628 | /// This will also return false if either of the types is neither a | |||
6629 | /// vector nor a real type. | |||
6630 | bool Sema::areLaxCompatibleVectorTypes(QualType srcTy, QualType destTy) { | |||
6631 | assert(destTy->isVectorType() || srcTy->isVectorType())((destTy->isVectorType() || srcTy->isVectorType()) ? static_cast <void> (0) : __assert_fail ("destTy->isVectorType() || srcTy->isVectorType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6631, __PRETTY_FUNCTION__)); | |||
6632 | ||||
6633 | // Disallow lax conversions between scalars and ExtVectors (these | |||
6634 | // conversions are allowed for other vector types because common headers | |||
6635 | // depend on them). Most scalar OP ExtVector cases are handled by the | |||
6636 | // splat path anyway, which does what we want (convert, not bitcast). | |||
6637 | // What this rules out for ExtVectors is crazy things like char4*float. | |||
6638 | if (srcTy->isScalarType() && destTy->isExtVectorType()) return false; | |||
6639 | if (destTy->isScalarType() && srcTy->isExtVectorType()) return false; | |||
6640 | ||||
6641 | uint64_t srcLen, destLen; | |||
6642 | QualType srcEltTy, destEltTy; | |||
6643 | if (!breakDownVectorType(srcTy, srcLen, srcEltTy)) return false; | |||
6644 | if (!breakDownVectorType(destTy, destLen, destEltTy)) return false; | |||
6645 | ||||
6646 | // ASTContext::getTypeSize will return the size rounded up to a | |||
6647 | // power of 2, so instead of using that, we need to use the raw | |||
6648 | // element size multiplied by the element count. | |||
6649 | uint64_t srcEltSize = Context.getTypeSize(srcEltTy); | |||
6650 | uint64_t destEltSize = Context.getTypeSize(destEltTy); | |||
6651 | ||||
6652 | return (srcLen * srcEltSize == destLen * destEltSize); | |||
6653 | } | |||
6654 | ||||
6655 | /// Is this a legal conversion between two types, one of which is | |||
6656 | /// known to be a vector type? | |||
6657 | bool Sema::isLaxVectorConversion(QualType srcTy, QualType destTy) { | |||
6658 | assert(destTy->isVectorType() || srcTy->isVectorType())((destTy->isVectorType() || srcTy->isVectorType()) ? static_cast <void> (0) : __assert_fail ("destTy->isVectorType() || srcTy->isVectorType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6658, __PRETTY_FUNCTION__)); | |||
6659 | ||||
6660 | switch (Context.getLangOpts().getLaxVectorConversions()) { | |||
6661 | case LangOptions::LaxVectorConversionKind::None: | |||
6662 | return false; | |||
6663 | ||||
6664 | case LangOptions::LaxVectorConversionKind::Integer: | |||
6665 | if (!srcTy->isIntegralOrEnumerationType()) { | |||
6666 | auto *Vec = srcTy->getAs<VectorType>(); | |||
6667 | if (!Vec || !Vec->getElementType()->isIntegralOrEnumerationType()) | |||
6668 | return false; | |||
6669 | } | |||
6670 | if (!destTy->isIntegralOrEnumerationType()) { | |||
6671 | auto *Vec = destTy->getAs<VectorType>(); | |||
6672 | if (!Vec || !Vec->getElementType()->isIntegralOrEnumerationType()) | |||
6673 | return false; | |||
6674 | } | |||
6675 | // OK, integer (vector) -> integer (vector) bitcast. | |||
6676 | break; | |||
6677 | ||||
6678 | case LangOptions::LaxVectorConversionKind::All: | |||
6679 | break; | |||
6680 | } | |||
6681 | ||||
6682 | return areLaxCompatibleVectorTypes(srcTy, destTy); | |||
6683 | } | |||
6684 | ||||
6685 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, | |||
6686 | CastKind &Kind) { | |||
6687 | assert(VectorTy->isVectorType() && "Not a vector type!")((VectorTy->isVectorType() && "Not a vector type!" ) ? static_cast<void> (0) : __assert_fail ("VectorTy->isVectorType() && \"Not a vector type!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6687, __PRETTY_FUNCTION__)); | |||
6688 | ||||
6689 | if (Ty->isVectorType() || Ty->isIntegralType(Context)) { | |||
6690 | if (!areLaxCompatibleVectorTypes(Ty, VectorTy)) | |||
6691 | return Diag(R.getBegin(), | |||
6692 | Ty->isVectorType() ? | |||
6693 | diag::err_invalid_conversion_between_vectors : | |||
6694 | diag::err_invalid_conversion_between_vector_and_integer) | |||
6695 | << VectorTy << Ty << R; | |||
6696 | } else | |||
6697 | return Diag(R.getBegin(), | |||
6698 | diag::err_invalid_conversion_between_vector_and_scalar) | |||
6699 | << VectorTy << Ty << R; | |||
6700 | ||||
6701 | Kind = CK_BitCast; | |||
6702 | return false; | |||
6703 | } | |||
6704 | ||||
6705 | ExprResult Sema::prepareVectorSplat(QualType VectorTy, Expr *SplattedExpr) { | |||
6706 | QualType DestElemTy = VectorTy->castAs<VectorType>()->getElementType(); | |||
6707 | ||||
6708 | if (DestElemTy == SplattedExpr->getType()) | |||
6709 | return SplattedExpr; | |||
6710 | ||||
6711 | assert(DestElemTy->isFloatingType() ||((DestElemTy->isFloatingType() || DestElemTy->isIntegralOrEnumerationType ()) ? static_cast<void> (0) : __assert_fail ("DestElemTy->isFloatingType() || DestElemTy->isIntegralOrEnumerationType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6712, __PRETTY_FUNCTION__)) | |||
6712 | DestElemTy->isIntegralOrEnumerationType())((DestElemTy->isFloatingType() || DestElemTy->isIntegralOrEnumerationType ()) ? static_cast<void> (0) : __assert_fail ("DestElemTy->isFloatingType() || DestElemTy->isIntegralOrEnumerationType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6712, __PRETTY_FUNCTION__)); | |||
6713 | ||||
6714 | CastKind CK; | |||
6715 | if (VectorTy->isExtVectorType() && SplattedExpr->getType()->isBooleanType()) { | |||
6716 | // OpenCL requires that we convert `true` boolean expressions to -1, but | |||
6717 | // only when splatting vectors. | |||
6718 | if (DestElemTy->isFloatingType()) { | |||
6719 | // To avoid having to have a CK_BooleanToSignedFloating cast kind, we cast | |||
6720 | // in two steps: boolean to signed integral, then to floating. | |||
6721 | ExprResult CastExprRes = ImpCastExprToType(SplattedExpr, Context.IntTy, | |||
6722 | CK_BooleanToSignedIntegral); | |||
6723 | SplattedExpr = CastExprRes.get(); | |||
6724 | CK = CK_IntegralToFloating; | |||
6725 | } else { | |||
6726 | CK = CK_BooleanToSignedIntegral; | |||
6727 | } | |||
6728 | } else { | |||
6729 | ExprResult CastExprRes = SplattedExpr; | |||
6730 | CK = PrepareScalarCast(CastExprRes, DestElemTy); | |||
6731 | if (CastExprRes.isInvalid()) | |||
6732 | return ExprError(); | |||
6733 | SplattedExpr = CastExprRes.get(); | |||
6734 | } | |||
6735 | return ImpCastExprToType(SplattedExpr, DestElemTy, CK); | |||
6736 | } | |||
6737 | ||||
6738 | ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, | |||
6739 | Expr *CastExpr, CastKind &Kind) { | |||
6740 | assert(DestTy->isExtVectorType() && "Not an extended vector type!")((DestTy->isExtVectorType() && "Not an extended vector type!" ) ? static_cast<void> (0) : __assert_fail ("DestTy->isExtVectorType() && \"Not an extended vector type!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6740, __PRETTY_FUNCTION__)); | |||
6741 | ||||
6742 | QualType SrcTy = CastExpr->getType(); | |||
6743 | ||||
6744 | // If SrcTy is a VectorType, the total size must match to explicitly cast to | |||
6745 | // an ExtVectorType. | |||
6746 | // In OpenCL, casts between vectors of different types are not allowed. | |||
6747 | // (See OpenCL 6.2). | |||
6748 | if (SrcTy->isVectorType()) { | |||
6749 | if (!areLaxCompatibleVectorTypes(SrcTy, DestTy) || | |||
6750 | (getLangOpts().OpenCL && | |||
6751 | !Context.hasSameUnqualifiedType(DestTy, SrcTy))) { | |||
6752 | Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) | |||
6753 | << DestTy << SrcTy << R; | |||
6754 | return ExprError(); | |||
6755 | } | |||
6756 | Kind = CK_BitCast; | |||
6757 | return CastExpr; | |||
6758 | } | |||
6759 | ||||
6760 | // All non-pointer scalars can be cast to ExtVector type. The appropriate | |||
6761 | // conversion will take place first from scalar to elt type, and then | |||
6762 | // splat from elt type to vector. | |||
6763 | if (SrcTy->isPointerType()) | |||
6764 | return Diag(R.getBegin(), | |||
6765 | diag::err_invalid_conversion_between_vector_and_scalar) | |||
6766 | << DestTy << SrcTy << R; | |||
6767 | ||||
6768 | Kind = CK_VectorSplat; | |||
6769 | return prepareVectorSplat(DestTy, CastExpr); | |||
6770 | } | |||
6771 | ||||
6772 | ExprResult | |||
6773 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, | |||
6774 | Declarator &D, ParsedType &Ty, | |||
6775 | SourceLocation RParenLoc, Expr *CastExpr) { | |||
6776 | assert(!D.isInvalidType() && (CastExpr != nullptr) &&((!D.isInvalidType() && (CastExpr != nullptr) && "ActOnCastExpr(): missing type or expr") ? static_cast<void > (0) : __assert_fail ("!D.isInvalidType() && (CastExpr != nullptr) && \"ActOnCastExpr(): missing type or expr\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6777, __PRETTY_FUNCTION__)) | |||
6777 | "ActOnCastExpr(): missing type or expr")((!D.isInvalidType() && (CastExpr != nullptr) && "ActOnCastExpr(): missing type or expr") ? static_cast<void > (0) : __assert_fail ("!D.isInvalidType() && (CastExpr != nullptr) && \"ActOnCastExpr(): missing type or expr\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6777, __PRETTY_FUNCTION__)); | |||
6778 | ||||
6779 | TypeSourceInfo *castTInfo = GetTypeForDeclaratorCast(D, CastExpr->getType()); | |||
6780 | if (D.isInvalidType()) | |||
6781 | return ExprError(); | |||
6782 | ||||
6783 | if (getLangOpts().CPlusPlus) { | |||
6784 | // Check that there are no default arguments (C++ only). | |||
6785 | CheckExtraCXXDefaultArguments(D); | |||
6786 | } else { | |||
6787 | // Make sure any TypoExprs have been dealt with. | |||
6788 | ExprResult Res = CorrectDelayedTyposInExpr(CastExpr); | |||
6789 | if (!Res.isUsable()) | |||
6790 | return ExprError(); | |||
6791 | CastExpr = Res.get(); | |||
6792 | } | |||
6793 | ||||
6794 | checkUnusedDeclAttributes(D); | |||
6795 | ||||
6796 | QualType castType = castTInfo->getType(); | |||
6797 | Ty = CreateParsedType(castType, castTInfo); | |||
6798 | ||||
6799 | bool isVectorLiteral = false; | |||
6800 | ||||
6801 | // Check for an altivec or OpenCL literal, | |||
6802 | // i.e. all the elements are integer constants. | |||
6803 | ParenExpr *PE = dyn_cast<ParenExpr>(CastExpr); | |||
6804 | ParenListExpr *PLE = dyn_cast<ParenListExpr>(CastExpr); | |||
6805 | if ((getLangOpts().AltiVec || getLangOpts().ZVector || getLangOpts().OpenCL) | |||
6806 | && castType->isVectorType() && (PE || PLE)) { | |||
6807 | if (PLE && PLE->getNumExprs() == 0) { | |||
6808 | Diag(PLE->getExprLoc(), diag::err_altivec_empty_initializer); | |||
6809 | return ExprError(); | |||
6810 | } | |||
6811 | if (PE || PLE->getNumExprs() == 1) { | |||
6812 | Expr *E = (PE ? PE->getSubExpr() : PLE->getExpr(0)); | |||
6813 | if (!E->getType()->isVectorType()) | |||
6814 | isVectorLiteral = true; | |||
6815 | } | |||
6816 | else | |||
6817 | isVectorLiteral = true; | |||
6818 | } | |||
6819 | ||||
6820 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' | |||
6821 | // then handle it as such. | |||
6822 | if (isVectorLiteral) | |||
6823 | return BuildVectorLiteral(LParenLoc, RParenLoc, CastExpr, castTInfo); | |||
6824 | ||||
6825 | // If the Expr being casted is a ParenListExpr, handle it specially. | |||
6826 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a | |||
6827 | // sequence of BinOp comma operators. | |||
6828 | if (isa<ParenListExpr>(CastExpr)) { | |||
6829 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, CastExpr); | |||
6830 | if (Result.isInvalid()) return ExprError(); | |||
6831 | CastExpr = Result.get(); | |||
6832 | } | |||
6833 | ||||
6834 | if (getLangOpts().CPlusPlus && !castType->isVoidType() && | |||
6835 | !getSourceManager().isInSystemMacro(LParenLoc)) | |||
6836 | Diag(LParenLoc, diag::warn_old_style_cast) << CastExpr->getSourceRange(); | |||
6837 | ||||
6838 | CheckTollFreeBridgeCast(castType, CastExpr); | |||
6839 | ||||
6840 | CheckObjCBridgeRelatedCast(castType, CastExpr); | |||
6841 | ||||
6842 | DiscardMisalignedMemberAddress(castType.getTypePtr(), CastExpr); | |||
6843 | ||||
6844 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, CastExpr); | |||
6845 | } | |||
6846 | ||||
6847 | ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc, | |||
6848 | SourceLocation RParenLoc, Expr *E, | |||
6849 | TypeSourceInfo *TInfo) { | |||
6850 | assert((isa<ParenListExpr>(E) || isa<ParenExpr>(E)) &&(((isa<ParenListExpr>(E) || isa<ParenExpr>(E)) && "Expected paren or paren list expression") ? static_cast< void> (0) : __assert_fail ("(isa<ParenListExpr>(E) || isa<ParenExpr>(E)) && \"Expected paren or paren list expression\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6851, __PRETTY_FUNCTION__)) | |||
6851 | "Expected paren or paren list expression")(((isa<ParenListExpr>(E) || isa<ParenExpr>(E)) && "Expected paren or paren list expression") ? static_cast< void> (0) : __assert_fail ("(isa<ParenListExpr>(E) || isa<ParenExpr>(E)) && \"Expected paren or paren list expression\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6851, __PRETTY_FUNCTION__)); | |||
6852 | ||||
6853 | Expr **exprs; | |||
6854 | unsigned numExprs; | |||
6855 | Expr *subExpr; | |||
6856 | SourceLocation LiteralLParenLoc, LiteralRParenLoc; | |||
6857 | if (ParenListExpr *PE = dyn_cast<ParenListExpr>(E)) { | |||
6858 | LiteralLParenLoc = PE->getLParenLoc(); | |||
6859 | LiteralRParenLoc = PE->getRParenLoc(); | |||
6860 | exprs = PE->getExprs(); | |||
6861 | numExprs = PE->getNumExprs(); | |||
6862 | } else { // isa<ParenExpr> by assertion at function entrance | |||
6863 | LiteralLParenLoc = cast<ParenExpr>(E)->getLParen(); | |||
6864 | LiteralRParenLoc = cast<ParenExpr>(E)->getRParen(); | |||
6865 | subExpr = cast<ParenExpr>(E)->getSubExpr(); | |||
6866 | exprs = &subExpr; | |||
6867 | numExprs = 1; | |||
6868 | } | |||
6869 | ||||
6870 | QualType Ty = TInfo->getType(); | |||
6871 | assert(Ty->isVectorType() && "Expected vector type")((Ty->isVectorType() && "Expected vector type") ? static_cast <void> (0) : __assert_fail ("Ty->isVectorType() && \"Expected vector type\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 6871, __PRETTY_FUNCTION__)); | |||
6872 | ||||
6873 | SmallVector<Expr *, 8> initExprs; | |||
6874 | const VectorType *VTy = Ty->castAs<VectorType>(); | |||
6875 | unsigned numElems = VTy->getNumElements(); | |||
6876 | ||||
6877 | // '(...)' form of vector initialization in AltiVec: the number of | |||
6878 | // initializers must be one or must match the size of the vector. | |||
6879 | // If a single value is specified in the initializer then it will be | |||
6880 | // replicated to all the components of the vector | |||
6881 | if (VTy->getVectorKind() == VectorType::AltiVecVector) { | |||
6882 | // The number of initializers must be one or must match the size of the | |||
6883 | // vector. If a single value is specified in the initializer then it will | |||
6884 | // be replicated to all the components of the vector | |||
6885 | if (numExprs == 1) { | |||
6886 | QualType ElemTy = VTy->getElementType(); | |||
6887 | ExprResult Literal = DefaultLvalueConversion(exprs[0]); | |||
6888 | if (Literal.isInvalid()) | |||
6889 | return ExprError(); | |||
6890 | Literal = ImpCastExprToType(Literal.get(), ElemTy, | |||
6891 | PrepareScalarCast(Literal, ElemTy)); | |||
6892 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.get()); | |||
6893 | } | |||
6894 | else if (numExprs < numElems) { | |||
6895 | Diag(E->getExprLoc(), | |||
6896 | diag::err_incorrect_number_of_vector_initializers); | |||
6897 | return ExprError(); | |||
6898 | } | |||
6899 | else | |||
6900 | initExprs.append(exprs, exprs + numExprs); | |||
6901 | } | |||
6902 | else { | |||
6903 | // For OpenCL, when the number of initializers is a single value, | |||
6904 | // it will be replicated to all components of the vector. | |||
6905 | if (getLangOpts().OpenCL && | |||
6906 | VTy->getVectorKind() == VectorType::GenericVector && | |||
6907 | numExprs == 1) { | |||
6908 | QualType ElemTy = VTy->getElementType(); | |||
6909 | ExprResult Literal = DefaultLvalueConversion(exprs[0]); | |||
6910 | if (Literal.isInvalid()) | |||
6911 | return ExprError(); | |||
6912 | Literal = ImpCastExprToType(Literal.get(), ElemTy, | |||
6913 | PrepareScalarCast(Literal, ElemTy)); | |||
6914 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.get()); | |||
6915 | } | |||
6916 | ||||
6917 | initExprs.append(exprs, exprs + numExprs); | |||
6918 | } | |||
6919 | // FIXME: This means that pretty-printing the final AST will produce curly | |||
6920 | // braces instead of the original commas. | |||
6921 | InitListExpr *initE = new (Context) InitListExpr(Context, LiteralLParenLoc, | |||
6922 | initExprs, LiteralRParenLoc); | |||
6923 | initE->setType(Ty); | |||
6924 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, initE); | |||
6925 | } | |||
6926 | ||||
6927 | /// This is not an AltiVec-style cast or or C++ direct-initialization, so turn | |||
6928 | /// the ParenListExpr into a sequence of comma binary operators. | |||
6929 | ExprResult | |||
6930 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *OrigExpr) { | |||
6931 | ParenListExpr *E = dyn_cast<ParenListExpr>(OrigExpr); | |||
6932 | if (!E) | |||
6933 | return OrigExpr; | |||
6934 | ||||
6935 | ExprResult Result(E->getExpr(0)); | |||
6936 | ||||
6937 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) | |||
6938 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), | |||
6939 | E->getExpr(i)); | |||
6940 | ||||
6941 | if (Result.isInvalid()) return ExprError(); | |||
6942 | ||||
6943 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); | |||
6944 | } | |||
6945 | ||||
6946 | ExprResult Sema::ActOnParenListExpr(SourceLocation L, | |||
6947 | SourceLocation R, | |||
6948 | MultiExprArg Val) { | |||
6949 | return ParenListExpr::Create(Context, L, Val, R); | |||
6950 | } | |||
6951 | ||||
6952 | /// Emit a specialized diagnostic when one expression is a null pointer | |||
6953 | /// constant and the other is not a pointer. Returns true if a diagnostic is | |||
6954 | /// emitted. | |||
6955 | bool Sema::DiagnoseConditionalForNull(Expr *LHSExpr, Expr *RHSExpr, | |||
6956 | SourceLocation QuestionLoc) { | |||
6957 | Expr *NullExpr = LHSExpr; | |||
6958 | Expr *NonPointerExpr = RHSExpr; | |||
6959 | Expr::NullPointerConstantKind NullKind = | |||
6960 | NullExpr->isNullPointerConstant(Context, | |||
6961 | Expr::NPC_ValueDependentIsNotNull); | |||
6962 | ||||
6963 | if (NullKind == Expr::NPCK_NotNull) { | |||
6964 | NullExpr = RHSExpr; | |||
6965 | NonPointerExpr = LHSExpr; | |||
6966 | NullKind = | |||
6967 | NullExpr->isNullPointerConstant(Context, | |||
6968 | Expr::NPC_ValueDependentIsNotNull); | |||
6969 | } | |||
6970 | ||||
6971 | if (NullKind == Expr::NPCK_NotNull) | |||
6972 | return false; | |||
6973 | ||||
6974 | if (NullKind == Expr::NPCK_ZeroExpression) | |||
6975 | return false; | |||
6976 | ||||
6977 | if (NullKind == Expr::NPCK_ZeroLiteral) { | |||
6978 | // In this case, check to make sure that we got here from a "NULL" | |||
6979 | // string in the source code. | |||
6980 | NullExpr = NullExpr->IgnoreParenImpCasts(); | |||
6981 | SourceLocation loc = NullExpr->getExprLoc(); | |||
6982 | if (!findMacroSpelling(loc, "NULL")) | |||
6983 | return false; | |||
6984 | } | |||
6985 | ||||
6986 | int DiagType = (NullKind == Expr::NPCK_CXX11_nullptr); | |||
6987 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) | |||
6988 | << NonPointerExpr->getType() << DiagType | |||
6989 | << NonPointerExpr->getSourceRange(); | |||
6990 | return true; | |||
6991 | } | |||
6992 | ||||
6993 | /// Return false if the condition expression is valid, true otherwise. | |||
6994 | static bool checkCondition(Sema &S, Expr *Cond, SourceLocation QuestionLoc) { | |||
6995 | QualType CondTy = Cond->getType(); | |||
6996 | ||||
6997 | // OpenCL v1.1 s6.3.i says the condition cannot be a floating point type. | |||
6998 | if (S.getLangOpts().OpenCL && CondTy->isFloatingType()) { | |||
6999 | S.Diag(QuestionLoc, diag::err_typecheck_cond_expect_nonfloat) | |||
7000 | << CondTy << Cond->getSourceRange(); | |||
7001 | return true; | |||
7002 | } | |||
7003 | ||||
7004 | // C99 6.5.15p2 | |||
7005 | if (CondTy->isScalarType()) return false; | |||
7006 | ||||
7007 | S.Diag(QuestionLoc, diag::err_typecheck_cond_expect_scalar) | |||
7008 | << CondTy << Cond->getSourceRange(); | |||
7009 | return true; | |||
7010 | } | |||
7011 | ||||
7012 | /// Handle when one or both operands are void type. | |||
7013 | static QualType checkConditionalVoidType(Sema &S, ExprResult &LHS, | |||
7014 | ExprResult &RHS) { | |||
7015 | Expr *LHSExpr = LHS.get(); | |||
7016 | Expr *RHSExpr = RHS.get(); | |||
7017 | ||||
7018 | if (!LHSExpr->getType()->isVoidType()) | |||
7019 | S.Diag(RHSExpr->getBeginLoc(), diag::ext_typecheck_cond_one_void) | |||
7020 | << RHSExpr->getSourceRange(); | |||
7021 | if (!RHSExpr->getType()->isVoidType()) | |||
7022 | S.Diag(LHSExpr->getBeginLoc(), diag::ext_typecheck_cond_one_void) | |||
7023 | << LHSExpr->getSourceRange(); | |||
7024 | LHS = S.ImpCastExprToType(LHS.get(), S.Context.VoidTy, CK_ToVoid); | |||
7025 | RHS = S.ImpCastExprToType(RHS.get(), S.Context.VoidTy, CK_ToVoid); | |||
7026 | return S.Context.VoidTy; | |||
7027 | } | |||
7028 | ||||
7029 | /// Return false if the NullExpr can be promoted to PointerTy, | |||
7030 | /// true otherwise. | |||
7031 | static bool checkConditionalNullPointer(Sema &S, ExprResult &NullExpr, | |||
7032 | QualType PointerTy) { | |||
7033 | if ((!PointerTy->isAnyPointerType() && !PointerTy->isBlockPointerType()) || | |||
7034 | !NullExpr.get()->isNullPointerConstant(S.Context, | |||
7035 | Expr::NPC_ValueDependentIsNull)) | |||
7036 | return true; | |||
7037 | ||||
7038 | NullExpr = S.ImpCastExprToType(NullExpr.get(), PointerTy, CK_NullToPointer); | |||
7039 | return false; | |||
7040 | } | |||
7041 | ||||
7042 | /// Checks compatibility between two pointers and return the resulting | |||
7043 | /// type. | |||
7044 | static QualType checkConditionalPointerCompatibility(Sema &S, ExprResult &LHS, | |||
7045 | ExprResult &RHS, | |||
7046 | SourceLocation Loc) { | |||
7047 | QualType LHSTy = LHS.get()->getType(); | |||
7048 | QualType RHSTy = RHS.get()->getType(); | |||
7049 | ||||
7050 | if (S.Context.hasSameType(LHSTy, RHSTy)) { | |||
7051 | // Two identical pointers types are always compatible. | |||
7052 | return LHSTy; | |||
7053 | } | |||
7054 | ||||
7055 | QualType lhptee, rhptee; | |||
7056 | ||||
7057 | // Get the pointee types. | |||
7058 | bool IsBlockPointer = false; | |||
7059 | if (const BlockPointerType *LHSBTy = LHSTy->getAs<BlockPointerType>()) { | |||
7060 | lhptee = LHSBTy->getPointeeType(); | |||
7061 | rhptee = RHSTy->castAs<BlockPointerType>()->getPointeeType(); | |||
7062 | IsBlockPointer = true; | |||
7063 | } else { | |||
7064 | lhptee = LHSTy->castAs<PointerType>()->getPointeeType(); | |||
7065 | rhptee = RHSTy->castAs<PointerType>()->getPointeeType(); | |||
7066 | } | |||
7067 | ||||
7068 | // C99 6.5.15p6: If both operands are pointers to compatible types or to | |||
7069 | // differently qualified versions of compatible types, the result type is | |||
7070 | // a pointer to an appropriately qualified version of the composite | |||
7071 | // type. | |||
7072 | ||||
7073 | // Only CVR-qualifiers exist in the standard, and the differently-qualified | |||
7074 | // clause doesn't make sense for our extensions. E.g. address space 2 should | |||
7075 | // be incompatible with address space 3: they may live on different devices or | |||
7076 | // anything. | |||
7077 | Qualifiers lhQual = lhptee.getQualifiers(); | |||
7078 | Qualifiers rhQual = rhptee.getQualifiers(); | |||
7079 | ||||
7080 | LangAS ResultAddrSpace = LangAS::Default; | |||
7081 | LangAS LAddrSpace = lhQual.getAddressSpace(); | |||
7082 | LangAS RAddrSpace = rhQual.getAddressSpace(); | |||
7083 | ||||
7084 | // OpenCL v1.1 s6.5 - Conversion between pointers to distinct address | |||
7085 | // spaces is disallowed. | |||
7086 | if (lhQual.isAddressSpaceSupersetOf(rhQual)) | |||
7087 | ResultAddrSpace = LAddrSpace; | |||
7088 | else if (rhQual.isAddressSpaceSupersetOf(lhQual)) | |||
7089 | ResultAddrSpace = RAddrSpace; | |||
7090 | else { | |||
7091 | S.Diag(Loc, diag::err_typecheck_op_on_nonoverlapping_address_space_pointers) | |||
7092 | << LHSTy << RHSTy << 2 << LHS.get()->getSourceRange() | |||
7093 | << RHS.get()->getSourceRange(); | |||
7094 | return QualType(); | |||
7095 | } | |||
7096 | ||||
7097 | unsigned MergedCVRQual = lhQual.getCVRQualifiers() | rhQual.getCVRQualifiers(); | |||
7098 | auto LHSCastKind = CK_BitCast, RHSCastKind = CK_BitCast; | |||
7099 | lhQual.removeCVRQualifiers(); | |||
7100 | rhQual.removeCVRQualifiers(); | |||
7101 | ||||
7102 | // OpenCL v2.0 specification doesn't extend compatibility of type qualifiers | |||
7103 | // (C99 6.7.3) for address spaces. We assume that the check should behave in | |||
7104 | // the same manner as it's defined for CVR qualifiers, so for OpenCL two | |||
7105 | // qual types are compatible iff | |||
7106 | // * corresponded types are compatible | |||
7107 | // * CVR qualifiers are equal | |||
7108 | // * address spaces are equal | |||
7109 | // Thus for conditional operator we merge CVR and address space unqualified | |||
7110 | // pointees and if there is a composite type we return a pointer to it with | |||
7111 | // merged qualifiers. | |||
7112 | LHSCastKind = | |||
7113 | LAddrSpace == ResultAddrSpace ? CK_BitCast : CK_AddressSpaceConversion; | |||
7114 | RHSCastKind = | |||
7115 | RAddrSpace == ResultAddrSpace ? CK_BitCast : CK_AddressSpaceConversion; | |||
7116 | lhQual.removeAddressSpace(); | |||
7117 | rhQual.removeAddressSpace(); | |||
7118 | ||||
7119 | lhptee = S.Context.getQualifiedType(lhptee.getUnqualifiedType(), lhQual); | |||
7120 | rhptee = S.Context.getQualifiedType(rhptee.getUnqualifiedType(), rhQual); | |||
7121 | ||||
7122 | QualType CompositeTy = S.Context.mergeTypes(lhptee, rhptee); | |||
7123 | ||||
7124 | if (CompositeTy.isNull()) { | |||
7125 | // In this situation, we assume void* type. No especially good | |||
7126 | // reason, but this is what gcc does, and we do have to pick | |||
7127 | // to get a consistent AST. | |||
7128 | QualType incompatTy; | |||
7129 | incompatTy = S.Context.getPointerType( | |||
7130 | S.Context.getAddrSpaceQualType(S.Context.VoidTy, ResultAddrSpace)); | |||
7131 | LHS = S.ImpCastExprToType(LHS.get(), incompatTy, LHSCastKind); | |||
7132 | RHS = S.ImpCastExprToType(RHS.get(), incompatTy, RHSCastKind); | |||
7133 | ||||
7134 | // FIXME: For OpenCL the warning emission and cast to void* leaves a room | |||
7135 | // for casts between types with incompatible address space qualifiers. | |||
7136 | // For the following code the compiler produces casts between global and | |||
7137 | // local address spaces of the corresponded innermost pointees: | |||
7138 | // local int *global *a; | |||
7139 | // global int *global *b; | |||
7140 | // a = (0 ? a : b); // see C99 6.5.16.1.p1. | |||
7141 | S.Diag(Loc, diag::ext_typecheck_cond_incompatible_pointers) | |||
7142 | << LHSTy << RHSTy << LHS.get()->getSourceRange() | |||
7143 | << RHS.get()->getSourceRange(); | |||
7144 | ||||
7145 | return incompatTy; | |||
7146 | } | |||
7147 | ||||
7148 | // The pointer types are compatible. | |||
7149 | // In case of OpenCL ResultTy should have the address space qualifier | |||
7150 | // which is a superset of address spaces of both the 2nd and the 3rd | |||
7151 | // operands of the conditional operator. | |||
7152 | QualType ResultTy = [&, ResultAddrSpace]() { | |||
7153 | if (S.getLangOpts().OpenCL) { | |||
7154 | Qualifiers CompositeQuals = CompositeTy.getQualifiers(); | |||
7155 | CompositeQuals.setAddressSpace(ResultAddrSpace); | |||
7156 | return S.Context | |||
7157 | .getQualifiedType(CompositeTy.getUnqualifiedType(), CompositeQuals) | |||
7158 | .withCVRQualifiers(MergedCVRQual); | |||
7159 | } | |||
7160 | return CompositeTy.withCVRQualifiers(MergedCVRQual); | |||
7161 | }(); | |||
7162 | if (IsBlockPointer) | |||
7163 | ResultTy = S.Context.getBlockPointerType(ResultTy); | |||
7164 | else | |||
7165 | ResultTy = S.Context.getPointerType(ResultTy); | |||
7166 | ||||
7167 | LHS = S.ImpCastExprToType(LHS.get(), ResultTy, LHSCastKind); | |||
7168 | RHS = S.ImpCastExprToType(RHS.get(), ResultTy, RHSCastKind); | |||
7169 | return ResultTy; | |||
7170 | } | |||
7171 | ||||
7172 | /// Return the resulting type when the operands are both block pointers. | |||
7173 | static QualType checkConditionalBlockPointerCompatibility(Sema &S, | |||
7174 | ExprResult &LHS, | |||
7175 | ExprResult &RHS, | |||
7176 | SourceLocation Loc) { | |||
7177 | QualType LHSTy = LHS.get()->getType(); | |||
7178 | QualType RHSTy = RHS.get()->getType(); | |||
7179 | ||||
7180 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { | |||
7181 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { | |||
7182 | QualType destType = S.Context.getPointerType(S.Context.VoidTy); | |||
7183 | LHS = S.ImpCastExprToType(LHS.get(), destType, CK_BitCast); | |||
7184 | RHS = S.ImpCastExprToType(RHS.get(), destType, CK_BitCast); | |||
7185 | return destType; | |||
7186 | } | |||
7187 | S.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) | |||
7188 | << LHSTy << RHSTy << LHS.get()->getSourceRange() | |||
7189 | << RHS.get()->getSourceRange(); | |||
7190 | return QualType(); | |||
7191 | } | |||
7192 | ||||
7193 | // We have 2 block pointer types. | |||
7194 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); | |||
7195 | } | |||
7196 | ||||
7197 | /// Return the resulting type when the operands are both pointers. | |||
7198 | static QualType | |||
7199 | checkConditionalObjectPointersCompatibility(Sema &S, ExprResult &LHS, | |||
7200 | ExprResult &RHS, | |||
7201 | SourceLocation Loc) { | |||
7202 | // get the pointer types | |||
7203 | QualType LHSTy = LHS.get()->getType(); | |||
7204 | QualType RHSTy = RHS.get()->getType(); | |||
7205 | ||||
7206 | // get the "pointed to" types | |||
7207 | QualType lhptee = LHSTy->castAs<PointerType>()->getPointeeType(); | |||
7208 | QualType rhptee = RHSTy->castAs<PointerType>()->getPointeeType(); | |||
7209 | ||||
7210 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) | |||
7211 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { | |||
7212 | // Figure out necessary qualifiers (C99 6.5.15p6) | |||
7213 | QualType destPointee | |||
7214 | = S.Context.getQualifiedType(lhptee, rhptee.getQualifiers()); | |||
7215 | QualType destType = S.Context.getPointerType(destPointee); | |||
7216 | // Add qualifiers if necessary. | |||
7217 | LHS = S.ImpCastExprToType(LHS.get(), destType, CK_NoOp); | |||
7218 | // Promote to void*. | |||
7219 | RHS = S.ImpCastExprToType(RHS.get(), destType, CK_BitCast); | |||
7220 | return destType; | |||
7221 | } | |||
7222 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { | |||
7223 | QualType destPointee | |||
7224 | = S.Context.getQualifiedType(rhptee, lhptee.getQualifiers()); | |||
7225 | QualType destType = S.Context.getPointerType(destPointee); | |||
7226 | // Add qualifiers if necessary. | |||
7227 | RHS = S.ImpCastExprToType(RHS.get(), destType, CK_NoOp); | |||
7228 | // Promote to void*. | |||
7229 | LHS = S.ImpCastExprToType(LHS.get(), destType, CK_BitCast); | |||
7230 | return destType; | |||
7231 | } | |||
7232 | ||||
7233 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); | |||
7234 | } | |||
7235 | ||||
7236 | /// Return false if the first expression is not an integer and the second | |||
7237 | /// expression is not a pointer, true otherwise. | |||
7238 | static bool checkPointerIntegerMismatch(Sema &S, ExprResult &Int, | |||
7239 | Expr* PointerExpr, SourceLocation Loc, | |||
7240 | bool IsIntFirstExpr) { | |||
7241 | if (!PointerExpr->getType()->isPointerType() || | |||
7242 | !Int.get()->getType()->isIntegerType()) | |||
7243 | return false; | |||
7244 | ||||
7245 | Expr *Expr1 = IsIntFirstExpr ? Int.get() : PointerExpr; | |||
7246 | Expr *Expr2 = IsIntFirstExpr ? PointerExpr : Int.get(); | |||
7247 | ||||
7248 | S.Diag(Loc, diag::ext_typecheck_cond_pointer_integer_mismatch) | |||
7249 | << Expr1->getType() << Expr2->getType() | |||
7250 | << Expr1->getSourceRange() << Expr2->getSourceRange(); | |||
7251 | Int = S.ImpCastExprToType(Int.get(), PointerExpr->getType(), | |||
7252 | CK_IntegralToPointer); | |||
7253 | return true; | |||
7254 | } | |||
7255 | ||||
7256 | /// Simple conversion between integer and floating point types. | |||
7257 | /// | |||
7258 | /// Used when handling the OpenCL conditional operator where the | |||
7259 | /// condition is a vector while the other operands are scalar. | |||
7260 | /// | |||
7261 | /// OpenCL v1.1 s6.3.i and s6.11.6 together require that the scalar | |||
7262 | /// types are either integer or floating type. Between the two | |||
7263 | /// operands, the type with the higher rank is defined as the "result | |||
7264 | /// type". The other operand needs to be promoted to the same type. No | |||
7265 | /// other type promotion is allowed. We cannot use | |||
7266 | /// UsualArithmeticConversions() for this purpose, since it always | |||
7267 | /// promotes promotable types. | |||
7268 | static QualType OpenCLArithmeticConversions(Sema &S, ExprResult &LHS, | |||
7269 | ExprResult &RHS, | |||
7270 | SourceLocation QuestionLoc) { | |||
7271 | LHS = S.DefaultFunctionArrayLvalueConversion(LHS.get()); | |||
7272 | if (LHS.isInvalid()) | |||
7273 | return QualType(); | |||
7274 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.get()); | |||
7275 | if (RHS.isInvalid()) | |||
7276 | return QualType(); | |||
7277 | ||||
7278 | // For conversion purposes, we ignore any qualifiers. | |||
7279 | // For example, "const float" and "float" are equivalent. | |||
7280 | QualType LHSType = | |||
7281 | S.Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); | |||
7282 | QualType RHSType = | |||
7283 | S.Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); | |||
7284 | ||||
7285 | if (!LHSType->isIntegerType() && !LHSType->isRealFloatingType()) { | |||
7286 | S.Diag(QuestionLoc, diag::err_typecheck_cond_expect_int_float) | |||
7287 | << LHSType << LHS.get()->getSourceRange(); | |||
7288 | return QualType(); | |||
7289 | } | |||
7290 | ||||
7291 | if (!RHSType->isIntegerType() && !RHSType->isRealFloatingType()) { | |||
7292 | S.Diag(QuestionLoc, diag::err_typecheck_cond_expect_int_float) | |||
7293 | << RHSType << RHS.get()->getSourceRange(); | |||
7294 | return QualType(); | |||
7295 | } | |||
7296 | ||||
7297 | // If both types are identical, no conversion is needed. | |||
7298 | if (LHSType == RHSType) | |||
7299 | return LHSType; | |||
7300 | ||||
7301 | // Now handle "real" floating types (i.e. float, double, long double). | |||
7302 | if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) | |||
7303 | return handleFloatConversion(S, LHS, RHS, LHSType, RHSType, | |||
7304 | /*IsCompAssign = */ false); | |||
7305 | ||||
7306 | // Finally, we have two differing integer types. | |||
7307 | return handleIntegerConversion<doIntegralCast, doIntegralCast> | |||
7308 | (S, LHS, RHS, LHSType, RHSType, /*IsCompAssign = */ false); | |||
7309 | } | |||
7310 | ||||
7311 | /// Convert scalar operands to a vector that matches the | |||
7312 | /// condition in length. | |||
7313 | /// | |||
7314 | /// Used when handling the OpenCL conditional operator where the | |||
7315 | /// condition is a vector while the other operands are scalar. | |||
7316 | /// | |||
7317 | /// We first compute the "result type" for the scalar operands | |||
7318 | /// according to OpenCL v1.1 s6.3.i. Both operands are then converted | |||
7319 | /// into a vector of that type where the length matches the condition | |||
7320 | /// vector type. s6.11.6 requires that the element types of the result | |||
7321 | /// and the condition must have the same number of bits. | |||
7322 | static QualType | |||
7323 | OpenCLConvertScalarsToVectors(Sema &S, ExprResult &LHS, ExprResult &RHS, | |||
7324 | QualType CondTy, SourceLocation QuestionLoc) { | |||
7325 | QualType ResTy = OpenCLArithmeticConversions(S, LHS, RHS, QuestionLoc); | |||
7326 | if (ResTy.isNull()) return QualType(); | |||
7327 | ||||
7328 | const VectorType *CV = CondTy->getAs<VectorType>(); | |||
7329 | assert(CV)((CV) ? static_cast<void> (0) : __assert_fail ("CV", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 7329, __PRETTY_FUNCTION__)); | |||
7330 | ||||
7331 | // Determine the vector result type | |||
7332 | unsigned NumElements = CV->getNumElements(); | |||
7333 | QualType VectorTy = S.Context.getExtVectorType(ResTy, NumElements); | |||
7334 | ||||
7335 | // Ensure that all types have the same number of bits | |||
7336 | if (S.Context.getTypeSize(CV->getElementType()) | |||
7337 | != S.Context.getTypeSize(ResTy)) { | |||
7338 | // Since VectorTy is created internally, it does not pretty print | |||
7339 | // with an OpenCL name. Instead, we just print a description. | |||
7340 | std::string EleTyName = ResTy.getUnqualifiedType().getAsString(); | |||
7341 | SmallString<64> Str; | |||
7342 | llvm::raw_svector_ostream OS(Str); | |||
7343 | OS << "(vector of " << NumElements << " '" << EleTyName << "' values)"; | |||
7344 | S.Diag(QuestionLoc, diag::err_conditional_vector_element_size) | |||
7345 | << CondTy << OS.str(); | |||
7346 | return QualType(); | |||
7347 | } | |||
7348 | ||||
7349 | // Convert operands to the vector result type | |||
7350 | LHS = S.ImpCastExprToType(LHS.get(), VectorTy, CK_VectorSplat); | |||
7351 | RHS = S.ImpCastExprToType(RHS.get(), VectorTy, CK_VectorSplat); | |||
7352 | ||||
7353 | return VectorTy; | |||
7354 | } | |||
7355 | ||||
7356 | /// Return false if this is a valid OpenCL condition vector | |||
7357 | static bool checkOpenCLConditionVector(Sema &S, Expr *Cond, | |||
7358 | SourceLocation QuestionLoc) { | |||
7359 | // OpenCL v1.1 s6.11.6 says the elements of the vector must be of | |||
7360 | // integral type. | |||
7361 | const VectorType *CondTy = Cond->getType()->getAs<VectorType>(); | |||
7362 | assert(CondTy)((CondTy) ? static_cast<void> (0) : __assert_fail ("CondTy" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 7362, __PRETTY_FUNCTION__)); | |||
7363 | QualType EleTy = CondTy->getElementType(); | |||
7364 | if (EleTy->isIntegerType()) return false; | |||
7365 | ||||
7366 | S.Diag(QuestionLoc, diag::err_typecheck_cond_expect_nonfloat) | |||
7367 | << Cond->getType() << Cond->getSourceRange(); | |||
7368 | return true; | |||
7369 | } | |||
7370 | ||||
7371 | /// Return false if the vector condition type and the vector | |||
7372 | /// result type are compatible. | |||
7373 | /// | |||
7374 | /// OpenCL v1.1 s6.11.6 requires that both vector types have the same | |||
7375 | /// number of elements, and their element types have the same number | |||
7376 | /// of bits. | |||
7377 | static bool checkVectorResult(Sema &S, QualType CondTy, QualType VecResTy, | |||
7378 | SourceLocation QuestionLoc) { | |||
7379 | const VectorType *CV = CondTy->getAs<VectorType>(); | |||
7380 | const VectorType *RV = VecResTy->getAs<VectorType>(); | |||
7381 | assert(CV && RV)((CV && RV) ? static_cast<void> (0) : __assert_fail ("CV && RV", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 7381, __PRETTY_FUNCTION__)); | |||
7382 | ||||
7383 | if (CV->getNumElements() != RV->getNumElements()) { | |||
7384 | S.Diag(QuestionLoc, diag::err_conditional_vector_size) | |||
7385 | << CondTy << VecResTy; | |||
7386 | return true; | |||
7387 | } | |||
7388 | ||||
7389 | QualType CVE = CV->getElementType(); | |||
7390 | QualType RVE = RV->getElementType(); | |||
7391 | ||||
7392 | if (S.Context.getTypeSize(CVE) != S.Context.getTypeSize(RVE)) { | |||
7393 | S.Diag(QuestionLoc, diag::err_conditional_vector_element_size) | |||
7394 | << CondTy << VecResTy; | |||
7395 | return true; | |||
7396 | } | |||
7397 | ||||
7398 | return false; | |||
7399 | } | |||
7400 | ||||
7401 | /// Return the resulting type for the conditional operator in | |||
7402 | /// OpenCL (aka "ternary selection operator", OpenCL v1.1 | |||
7403 | /// s6.3.i) when the condition is a vector type. | |||
7404 | static QualType | |||
7405 | OpenCLCheckVectorConditional(Sema &S, ExprResult &Cond, | |||
7406 | ExprResult &LHS, ExprResult &RHS, | |||
7407 | SourceLocation QuestionLoc) { | |||
7408 | Cond = S.DefaultFunctionArrayLvalueConversion(Cond.get()); | |||
7409 | if (Cond.isInvalid()) | |||
7410 | return QualType(); | |||
7411 | QualType CondTy = Cond.get()->getType(); | |||
7412 | ||||
7413 | if (checkOpenCLConditionVector(S, Cond.get(), QuestionLoc)) | |||
7414 | return QualType(); | |||
7415 | ||||
7416 | // If either operand is a vector then find the vector type of the | |||
7417 | // result as specified in OpenCL v1.1 s6.3.i. | |||
7418 | if (LHS.get()->getType()->isVectorType() || | |||
7419 | RHS.get()->getType()->isVectorType()) { | |||
7420 | QualType VecResTy = S.CheckVectorOperands(LHS, RHS, QuestionLoc, | |||
7421 | /*isCompAssign*/false, | |||
7422 | /*AllowBothBool*/true, | |||
7423 | /*AllowBoolConversions*/false); | |||
7424 | if (VecResTy.isNull()) return QualType(); | |||
7425 | // The result type must match the condition type as specified in | |||
7426 | // OpenCL v1.1 s6.11.6. | |||
7427 | if (checkVectorResult(S, CondTy, VecResTy, QuestionLoc)) | |||
7428 | return QualType(); | |||
7429 | return VecResTy; | |||
7430 | } | |||
7431 | ||||
7432 | // Both operands are scalar. | |||
7433 | return OpenCLConvertScalarsToVectors(S, LHS, RHS, CondTy, QuestionLoc); | |||
7434 | } | |||
7435 | ||||
7436 | /// Return true if the Expr is block type | |||
7437 | static bool checkBlockType(Sema &S, const Expr *E) { | |||
7438 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { | |||
7439 | QualType Ty = CE->getCallee()->getType(); | |||
7440 | if (Ty->isBlockPointerType()) { | |||
7441 | S.Diag(E->getExprLoc(), diag::err_opencl_ternary_with_block); | |||
7442 | return true; | |||
7443 | } | |||
7444 | } | |||
7445 | return false; | |||
7446 | } | |||
7447 | ||||
7448 | /// Note that LHS is not null here, even if this is the gnu "x ?: y" extension. | |||
7449 | /// In that case, LHS = cond. | |||
7450 | /// C99 6.5.15 | |||
7451 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, | |||
7452 | ExprResult &RHS, ExprValueKind &VK, | |||
7453 | ExprObjectKind &OK, | |||
7454 | SourceLocation QuestionLoc) { | |||
7455 | ||||
7456 | ExprResult LHSResult = CheckPlaceholderExpr(LHS.get()); | |||
7457 | if (!LHSResult.isUsable()) return QualType(); | |||
7458 | LHS = LHSResult; | |||
7459 | ||||
7460 | ExprResult RHSResult = CheckPlaceholderExpr(RHS.get()); | |||
7461 | if (!RHSResult.isUsable()) return QualType(); | |||
7462 | RHS = RHSResult; | |||
7463 | ||||
7464 | // C++ is sufficiently different to merit its own checker. | |||
7465 | if (getLangOpts().CPlusPlus) | |||
7466 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); | |||
7467 | ||||
7468 | VK = VK_RValue; | |||
7469 | OK = OK_Ordinary; | |||
7470 | ||||
7471 | // The OpenCL operator with a vector condition is sufficiently | |||
7472 | // different to merit its own checker. | |||
7473 | if (getLangOpts().OpenCL && Cond.get()->getType()->isVectorType()) | |||
7474 | return OpenCLCheckVectorConditional(*this, Cond, LHS, RHS, QuestionLoc); | |||
7475 | ||||
7476 | // First, check the condition. | |||
7477 | Cond = UsualUnaryConversions(Cond.get()); | |||
7478 | if (Cond.isInvalid()) | |||
7479 | return QualType(); | |||
7480 | if (checkCondition(*this, Cond.get(), QuestionLoc)) | |||
7481 | return QualType(); | |||
7482 | ||||
7483 | // Now check the two expressions. | |||
7484 | if (LHS.get()->getType()->isVectorType() || | |||
7485 | RHS.get()->getType()->isVectorType()) | |||
7486 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false, | |||
7487 | /*AllowBothBool*/true, | |||
7488 | /*AllowBoolConversions*/false); | |||
7489 | ||||
7490 | QualType ResTy = | |||
7491 | UsualArithmeticConversions(LHS, RHS, QuestionLoc, ACK_Conditional); | |||
7492 | if (LHS.isInvalid() || RHS.isInvalid()) | |||
7493 | return QualType(); | |||
7494 | ||||
7495 | QualType LHSTy = LHS.get()->getType(); | |||
7496 | QualType RHSTy = RHS.get()->getType(); | |||
7497 | ||||
7498 | // Diagnose attempts to convert between __float128 and long double where | |||
7499 | // such conversions currently can't be handled. | |||
7500 | if (unsupportedTypeConversion(*this, LHSTy, RHSTy)) { | |||
7501 | Diag(QuestionLoc, | |||
7502 | diag::err_typecheck_cond_incompatible_operands) << LHSTy << RHSTy | |||
7503 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | |||
7504 | return QualType(); | |||
7505 | } | |||
7506 | ||||
7507 | // OpenCL v2.0 s6.12.5 - Blocks cannot be used as expressions of the ternary | |||
7508 | // selection operator (?:). | |||
7509 | if (getLangOpts().OpenCL && | |||
7510 | (checkBlockType(*this, LHS.get()) | checkBlockType(*this, RHS.get()))) { | |||
7511 | return QualType(); | |||
7512 | } | |||
7513 | ||||
7514 | // If both operands have arithmetic type, do the usual arithmetic conversions | |||
7515 | // to find a common type: C99 6.5.15p3,5. | |||
7516 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { | |||
7517 | LHS = ImpCastExprToType(LHS.get(), ResTy, PrepareScalarCast(LHS, ResTy)); | |||
7518 | RHS = ImpCastExprToType(RHS.get(), ResTy, PrepareScalarCast(RHS, ResTy)); | |||
7519 | ||||
7520 | return ResTy; | |||
7521 | } | |||
7522 | ||||
7523 | // If both operands are the same structure or union type, the result is that | |||
7524 | // type. | |||
7525 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 | |||
7526 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) | |||
7527 | if (LHSRT->getDecl() == RHSRT->getDecl()) | |||
7528 | // "If both the operands have structure or union type, the result has | |||
7529 | // that type." This implies that CV qualifiers are dropped. | |||
7530 | return LHSTy.getUnqualifiedType(); | |||
7531 | // FIXME: Type of conditional expression must be complete in C mode. | |||
7532 | } | |||
7533 | ||||
7534 | // C99 6.5.15p5: "If both operands have void type, the result has void type." | |||
7535 | // The following || allows only one side to be void (a GCC-ism). | |||
7536 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { | |||
7537 | return checkConditionalVoidType(*this, LHS, RHS); | |||
7538 | } | |||
7539 | ||||
7540 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has | |||
7541 | // the type of the other operand." | |||
7542 | if (!checkConditionalNullPointer(*this, RHS, LHSTy)) return LHSTy; | |||
7543 | if (!checkConditionalNullPointer(*this, LHS, RHSTy)) return RHSTy; | |||
7544 | ||||
7545 | // All objective-c pointer type analysis is done here. | |||
7546 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, | |||
7547 | QuestionLoc); | |||
7548 | if (LHS.isInvalid() || RHS.isInvalid()) | |||
7549 | return QualType(); | |||
7550 | if (!compositeType.isNull()) | |||
7551 | return compositeType; | |||
7552 | ||||
7553 | ||||
7554 | // Handle block pointer types. | |||
7555 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) | |||
7556 | return checkConditionalBlockPointerCompatibility(*this, LHS, RHS, | |||
7557 | QuestionLoc); | |||
7558 | ||||
7559 | // Check constraints for C object pointers types (C99 6.5.15p3,6). | |||
7560 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) | |||
7561 | return checkConditionalObjectPointersCompatibility(*this, LHS, RHS, | |||
7562 | QuestionLoc); | |||
7563 | ||||
7564 | // GCC compatibility: soften pointer/integer mismatch. Note that | |||
7565 | // null pointers have been filtered out by this point. | |||
7566 | if (checkPointerIntegerMismatch(*this, LHS, RHS.get(), QuestionLoc, | |||
7567 | /*IsIntFirstExpr=*/true)) | |||
7568 | return RHSTy; | |||
7569 | if (checkPointerIntegerMismatch(*this, RHS, LHS.get(), QuestionLoc, | |||
7570 | /*IsIntFirstExpr=*/false)) | |||
7571 | return LHSTy; | |||
7572 | ||||
7573 | // Emit a better diagnostic if one of the expressions is a null pointer | |||
7574 | // constant and the other is not a pointer type. In this case, the user most | |||
7575 | // likely forgot to take the address of the other expression. | |||
7576 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) | |||
7577 | return QualType(); | |||
7578 | ||||
7579 | // Otherwise, the operands are not compatible. | |||
7580 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) | |||
7581 | << LHSTy << RHSTy << LHS.get()->getSourceRange() | |||
7582 | << RHS.get()->getSourceRange(); | |||
7583 | return QualType(); | |||
7584 | } | |||
7585 | ||||
7586 | /// FindCompositeObjCPointerType - Helper method to find composite type of | |||
7587 | /// two objective-c pointer types of the two input expressions. | |||
7588 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, | |||
7589 | SourceLocation QuestionLoc) { | |||
7590 | QualType LHSTy = LHS.get()->getType(); | |||
7591 | QualType RHSTy = RHS.get()->getType(); | |||
7592 | ||||
7593 | // Handle things like Class and struct objc_class*. Here we case the result | |||
7594 | // to the pseudo-builtin, because that will be implicitly cast back to the | |||
7595 | // redefinition type if an attempt is made to access its fields. | |||
7596 | if (LHSTy->isObjCClassType() && | |||
7597 | (Context.hasSameType(RHSTy, Context.getObjCClassRedefinitionType()))) { | |||
7598 | RHS = ImpCastExprToType(RHS.get(), LHSTy, CK_CPointerToObjCPointerCast); | |||
7599 | return LHSTy; | |||
7600 | } | |||
7601 | if (RHSTy->isObjCClassType() && | |||
7602 | (Context.hasSameType(LHSTy, Context.getObjCClassRedefinitionType()))) { | |||
7603 | LHS = ImpCastExprToType(LHS.get(), RHSTy, CK_CPointerToObjCPointerCast); | |||
7604 | return RHSTy; | |||
7605 | } | |||
7606 | // And the same for struct objc_object* / id | |||
7607 | if (LHSTy->isObjCIdType() && | |||
7608 | (Context.hasSameType(RHSTy, Context.getObjCIdRedefinitionType()))) { | |||
7609 | RHS = ImpCastExprToType(RHS.get(), LHSTy, CK_CPointerToObjCPointerCast); | |||
7610 | return LHSTy; | |||
7611 | } | |||
7612 | if (RHSTy->isObjCIdType() && | |||
7613 | (Context.hasSameType(LHSTy, Context.getObjCIdRedefinitionType()))) { | |||
7614 | LHS = ImpCastExprToType(LHS.get(), RHSTy, CK_CPointerToObjCPointerCast); | |||
7615 | return RHSTy; | |||
7616 | } | |||
7617 | // And the same for struct objc_selector* / SEL | |||
7618 | if (Context.isObjCSelType(LHSTy) && | |||
7619 | (Context.hasSameType(RHSTy, Context.getObjCSelRedefinitionType()))) { | |||
7620 | RHS = ImpCastExprToType(RHS.get(), LHSTy, CK_BitCast); | |||
7621 | return LHSTy; | |||
7622 | } | |||
7623 | if (Context.isObjCSelType(RHSTy) && | |||
7624 | (Context.hasSameType(LHSTy, Context.getObjCSelRedefinitionType()))) { | |||
7625 | LHS = ImpCastExprToType(LHS.get(), RHSTy, CK_BitCast); | |||
7626 | return RHSTy; | |||
7627 | } | |||
7628 | // Check constraints for Objective-C object pointers types. | |||
7629 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { | |||
7630 | ||||
7631 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { | |||
7632 | // Two identical object pointer types are always compatible. | |||
7633 | return LHSTy; | |||
7634 | } | |||
7635 | const ObjCObjectPointerType *LHSOPT = LHSTy->castAs<ObjCObjectPointerType>(); | |||
7636 | const ObjCObjectPointerType *RHSOPT = RHSTy->castAs<ObjCObjectPointerType>(); | |||
7637 | QualType compositeType = LHSTy; | |||
7638 | ||||
7639 | // If both operands are interfaces and either operand can be | |||
7640 | // assigned to the other, use that type as the composite | |||
7641 | // type. This allows | |||
7642 | // xxx ? (A*) a : (B*) b | |||
7643 | // where B is a subclass of A. | |||
7644 | // | |||
7645 | // Additionally, as for assignment, if either type is 'id' | |||
7646 | // allow silent coercion. Finally, if the types are | |||
7647 | // incompatible then make sure to use 'id' as the composite | |||
7648 | // type so the result is acceptable for sending messages to. | |||
7649 | ||||
7650 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. | |||
7651 | // It could return the composite type. | |||
7652 | if (!(compositeType = | |||
7653 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) { | |||
7654 | // Nothing more to do. | |||
7655 | } else if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { | |||
7656 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; | |||
7657 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { | |||
7658 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; | |||
7659 | } else if ((LHSOPT->isObjCQualifiedIdType() || | |||
7660 | RHSOPT->isObjCQualifiedIdType()) && | |||
7661 | Context.ObjCQualifiedIdTypesAreCompatible(LHSOPT, RHSOPT, | |||
7662 | true)) { | |||
7663 | // Need to handle "id<xx>" explicitly. | |||
7664 | // GCC allows qualified id and any Objective-C type to devolve to | |||
7665 | // id. Currently localizing to here until clear this should be | |||
7666 | // part of ObjCQualifiedIdTypesAreCompatible. | |||
7667 | compositeType = Context.getObjCIdType(); | |||
7668 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { | |||
7669 | compositeType = Context.getObjCIdType(); | |||
7670 | } else { | |||
7671 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) | |||
7672 | << LHSTy << RHSTy | |||
7673 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | |||
7674 | QualType incompatTy = Context.getObjCIdType(); | |||
7675 | LHS = ImpCastExprToType(LHS.get(), incompatTy, CK_BitCast); | |||
7676 | RHS = ImpCastExprToType(RHS.get(), incompatTy, CK_BitCast); | |||
7677 | return incompatTy; | |||
7678 | } | |||
7679 | // The object pointer types are compatible. | |||
7680 | LHS = ImpCastExprToType(LHS.get(), compositeType, CK_BitCast); | |||
7681 | RHS = ImpCastExprToType(RHS.get(), compositeType, CK_BitCast); | |||
7682 | return compositeType; | |||
7683 | } | |||
7684 | // Check Objective-C object pointer types and 'void *' | |||
7685 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { | |||
7686 | if (getLangOpts().ObjCAutoRefCount) { | |||
7687 | // ARC forbids the implicit conversion of object pointers to 'void *', | |||
7688 | // so these types are not compatible. | |||
7689 | Diag(QuestionLoc, diag::err_cond_voidptr_arc) << LHSTy << RHSTy | |||
7690 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | |||
7691 | LHS = RHS = true; | |||
7692 | return QualType(); | |||
7693 | } | |||
7694 | QualType lhptee = LHSTy->castAs<PointerType>()->getPointeeType(); | |||
7695 | QualType rhptee = RHSTy->castAs<ObjCObjectPointerType>()->getPointeeType(); | |||
7696 | QualType destPointee | |||
7697 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); | |||
7698 | QualType destType = Context.getPointerType(destPointee); | |||
7699 | // Add qualifiers if necessary. | |||
7700 | LHS = ImpCastExprToType(LHS.get(), destType, CK_NoOp); | |||
7701 | // Promote to void*. | |||
7702 | RHS = ImpCastExprToType(RHS.get(), destType, CK_BitCast); | |||
7703 | return destType; | |||
7704 | } | |||
7705 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { | |||
7706 | if (getLangOpts().ObjCAutoRefCount) { | |||
7707 | // ARC forbids the implicit conversion of object pointers to 'void *', | |||
7708 | // so these types are not compatible. | |||
7709 | Diag(QuestionLoc, diag::err_cond_voidptr_arc) << LHSTy << RHSTy | |||
7710 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | |||
7711 | LHS = RHS = true; | |||
7712 | return QualType(); | |||
7713 | } | |||
7714 | QualType lhptee = LHSTy->castAs<ObjCObjectPointerType>()->getPointeeType(); | |||
7715 | QualType rhptee = RHSTy->castAs<PointerType>()->getPointeeType(); | |||
7716 | QualType destPointee | |||
7717 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); | |||
7718 | QualType destType = Context.getPointerType(destPointee); | |||
7719 | // Add qualifiers if necessary. | |||
7720 | RHS = ImpCastExprToType(RHS.get(), destType, CK_NoOp); | |||
7721 | // Promote to void*. | |||
7722 | LHS = ImpCastExprToType(LHS.get(), destType, CK_BitCast); | |||
7723 | return destType; | |||
7724 | } | |||
7725 | return QualType(); | |||
7726 | } | |||
7727 | ||||
7728 | /// SuggestParentheses - Emit a note with a fixit hint that wraps | |||
7729 | /// ParenRange in parentheses. | |||
7730 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, | |||
7731 | const PartialDiagnostic &Note, | |||
7732 | SourceRange ParenRange) { | |||
7733 | SourceLocation EndLoc = Self.getLocForEndOfToken(ParenRange.getEnd()); | |||
7734 | if (ParenRange.getBegin().isFileID() && ParenRange.getEnd().isFileID() && | |||
7735 | EndLoc.isValid()) { | |||
7736 | Self.Diag(Loc, Note) | |||
7737 | << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") | |||
7738 | << FixItHint::CreateInsertion(EndLoc, ")"); | |||
7739 | } else { | |||
7740 | // We can't display the parentheses, so just show the bare note. | |||
7741 | Self.Diag(Loc, Note) << ParenRange; | |||
7742 | } | |||
7743 | } | |||
7744 | ||||
7745 | static bool IsArithmeticOp(BinaryOperatorKind Opc) { | |||
7746 | return BinaryOperator::isAdditiveOp(Opc) || | |||
7747 | BinaryOperator::isMultiplicativeOp(Opc) || | |||
7748 | BinaryOperator::isShiftOp(Opc) || Opc == BO_And || Opc == BO_Or; | |||
7749 | // This only checks for bitwise-or and bitwise-and, but not bitwise-xor and | |||
7750 | // not any of the logical operators. Bitwise-xor is commonly used as a | |||
7751 | // logical-xor because there is no logical-xor operator. The logical | |||
7752 | // operators, including uses of xor, have a high false positive rate for | |||
7753 | // precedence warnings. | |||
7754 | } | |||
7755 | ||||
7756 | /// IsArithmeticBinaryExpr - Returns true if E is an arithmetic binary | |||
7757 | /// expression, either using a built-in or overloaded operator, | |||
7758 | /// and sets *OpCode to the opcode and *RHSExprs to the right-hand side | |||
7759 | /// expression. | |||
7760 | static bool IsArithmeticBinaryExpr(Expr *E, BinaryOperatorKind *Opcode, | |||
7761 | Expr **RHSExprs) { | |||
7762 | // Don't strip parenthesis: we should not warn if E is in parenthesis. | |||
7763 | E = E->IgnoreImpCasts(); | |||
7764 | E = E->IgnoreConversionOperator(); | |||
7765 | E = E->IgnoreImpCasts(); | |||
7766 | if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) { | |||
7767 | E = MTE->getSubExpr(); | |||
7768 | E = E->IgnoreImpCasts(); | |||
7769 | } | |||
7770 | ||||
7771 | // Built-in binary operator. | |||
7772 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) { | |||
7773 | if (IsArithmeticOp(OP->getOpcode())) { | |||
7774 | *Opcode = OP->getOpcode(); | |||
7775 | *RHSExprs = OP->getRHS(); | |||
7776 | return true; | |||
7777 | } | |||
7778 | } | |||
7779 | ||||
7780 | // Overloaded operator. | |||
7781 | if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(E)) { | |||
7782 | if (Call->getNumArgs() != 2) | |||
7783 | return false; | |||
7784 | ||||
7785 | // Make sure this is really a binary operator that is safe to pass into | |||
7786 | // BinaryOperator::getOverloadedOpcode(), e.g. it's not a subscript op. | |||
7787 | OverloadedOperatorKind OO = Call->getOperator(); | |||
7788 | if (OO < OO_Plus || OO > OO_Arrow || | |||
7789 | OO == OO_PlusPlus || OO == OO_MinusMinus) | |||
7790 | return false; | |||
7791 | ||||
7792 | BinaryOperatorKind OpKind = BinaryOperator::getOverloadedOpcode(OO); | |||
7793 | if (IsArithmeticOp(OpKind)) { | |||
7794 | *Opcode = OpKind; | |||
7795 | *RHSExprs = Call->getArg(1); | |||
7796 | return true; | |||
7797 | } | |||
7798 | } | |||
7799 | ||||
7800 | return false; | |||
7801 | } | |||
7802 | ||||
7803 | /// ExprLooksBoolean - Returns true if E looks boolean, i.e. it has boolean type | |||
7804 | /// or is a logical expression such as (x==y) which has int type, but is | |||
7805 | /// commonly interpreted as boolean. | |||
7806 | static bool ExprLooksBoolean(Expr *E) { | |||
7807 | E = E->IgnoreParenImpCasts(); | |||
7808 | ||||
7809 | if (E->getType()->isBooleanType()) | |||
7810 | return true; | |||
7811 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) | |||
7812 | return OP->isComparisonOp() || OP->isLogicalOp(); | |||
7813 | if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E)) | |||
7814 | return OP->getOpcode() == UO_LNot; | |||
7815 | if (E->getType()->isPointerType()) | |||
7816 | return true; | |||
7817 | // FIXME: What about overloaded operator calls returning "unspecified boolean | |||
7818 | // type"s (commonly pointer-to-members)? | |||
7819 | ||||
7820 | return false; | |||
7821 | } | |||
7822 | ||||
7823 | /// DiagnoseConditionalPrecedence - Emit a warning when a conditional operator | |||
7824 | /// and binary operator are mixed in a way that suggests the programmer assumed | |||
7825 | /// the conditional operator has higher precedence, for example: | |||
7826 | /// "int x = a + someBinaryCondition ? 1 : 2". | |||
7827 | static void DiagnoseConditionalPrecedence(Sema &Self, | |||
7828 | SourceLocation OpLoc, | |||
7829 | Expr *Condition, | |||
7830 | Expr *LHSExpr, | |||
7831 | Expr *RHSExpr) { | |||
7832 | BinaryOperatorKind CondOpcode; | |||
7833 | Expr *CondRHS; | |||
7834 | ||||
7835 | if (!IsArithmeticBinaryExpr(Condition, &CondOpcode, &CondRHS)) | |||
7836 | return; | |||
7837 | if (!ExprLooksBoolean(CondRHS)) | |||
7838 | return; | |||
7839 | ||||
7840 | // The condition is an arithmetic binary expression, with a right- | |||
7841 | // hand side that looks boolean, so warn. | |||
7842 | ||||
7843 | unsigned DiagID = BinaryOperator::isBitwiseOp(CondOpcode) | |||
7844 | ? diag::warn_precedence_bitwise_conditional | |||
7845 | : diag::warn_precedence_conditional; | |||
7846 | ||||
7847 | Self.Diag(OpLoc, DiagID) | |||
7848 | << Condition->getSourceRange() | |||
7849 | << BinaryOperator::getOpcodeStr(CondOpcode); | |||
7850 | ||||
7851 | SuggestParentheses( | |||
7852 | Self, OpLoc, | |||
7853 | Self.PDiag(diag::note_precedence_silence) | |||
7854 | << BinaryOperator::getOpcodeStr(CondOpcode), | |||
7855 | SourceRange(Condition->getBeginLoc(), Condition->getEndLoc())); | |||
7856 | ||||
7857 | SuggestParentheses(Self, OpLoc, | |||
7858 | Self.PDiag(diag::note_precedence_conditional_first), | |||
7859 | SourceRange(CondRHS->getBeginLoc(), RHSExpr->getEndLoc())); | |||
7860 | } | |||
7861 | ||||
7862 | /// Compute the nullability of a conditional expression. | |||
7863 | static QualType computeConditionalNullability(QualType ResTy, bool IsBin, | |||
7864 | QualType LHSTy, QualType RHSTy, | |||
7865 | ASTContext &Ctx) { | |||
7866 | if (!ResTy->isAnyPointerType()) | |||
7867 | return ResTy; | |||
7868 | ||||
7869 | auto GetNullability = [&Ctx](QualType Ty) { | |||
7870 | Optional<NullabilityKind> Kind = Ty->getNullability(Ctx); | |||
7871 | if (Kind) | |||
7872 | return *Kind; | |||
7873 | return NullabilityKind::Unspecified; | |||
7874 | }; | |||
7875 | ||||
7876 | auto LHSKind = GetNullability(LHSTy), RHSKind = GetNullability(RHSTy); | |||
7877 | NullabilityKind MergedKind; | |||
7878 | ||||
7879 | // Compute nullability of a binary conditional expression. | |||
7880 | if (IsBin) { | |||
7881 | if (LHSKind == NullabilityKind::NonNull) | |||
7882 | MergedKind = NullabilityKind::NonNull; | |||
7883 | else | |||
7884 | MergedKind = RHSKind; | |||
7885 | // Compute nullability of a normal conditional expression. | |||
7886 | } else { | |||
7887 | if (LHSKind == NullabilityKind::Nullable || | |||
7888 | RHSKind == NullabilityKind::Nullable) | |||
7889 | MergedKind = NullabilityKind::Nullable; | |||
7890 | else if (LHSKind == NullabilityKind::NonNull) | |||
7891 | MergedKind = RHSKind; | |||
7892 | else if (RHSKind == NullabilityKind::NonNull) | |||
7893 | MergedKind = LHSKind; | |||
7894 | else | |||
7895 | MergedKind = NullabilityKind::Unspecified; | |||
7896 | } | |||
7897 | ||||
7898 | // Return if ResTy already has the correct nullability. | |||
7899 | if (GetNullability(ResTy) == MergedKind) | |||
7900 | return ResTy; | |||
7901 | ||||
7902 | // Strip all nullability from ResTy. | |||
7903 | while (ResTy->getNullability(Ctx)) | |||
7904 | ResTy = ResTy.getSingleStepDesugaredType(Ctx); | |||
7905 | ||||
7906 | // Create a new AttributedType with the new nullability kind. | |||
7907 | auto NewAttr = AttributedType::getNullabilityAttrKind(MergedKind); | |||
7908 | return Ctx.getAttributedType(NewAttr, ResTy, ResTy); | |||
7909 | } | |||
7910 | ||||
7911 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null | |||
7912 | /// in the case of a the GNU conditional expr extension. | |||
7913 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, | |||
7914 | SourceLocation ColonLoc, | |||
7915 | Expr *CondExpr, Expr *LHSExpr, | |||
7916 | Expr *RHSExpr) { | |||
7917 | if (!getLangOpts().CPlusPlus) { | |||
7918 | // C cannot handle TypoExpr nodes in the condition because it | |||
7919 | // doesn't handle dependent types properly, so make sure any TypoExprs have | |||
7920 | // been dealt with before checking the operands. | |||
7921 | ExprResult CondResult = CorrectDelayedTyposInExpr(CondExpr); | |||
7922 | ExprResult LHSResult = CorrectDelayedTyposInExpr(LHSExpr); | |||
7923 | ExprResult RHSResult = CorrectDelayedTyposInExpr(RHSExpr); | |||
7924 | ||||
7925 | if (!CondResult.isUsable()) | |||
7926 | return ExprError(); | |||
7927 | ||||
7928 | if (LHSExpr) { | |||
7929 | if (!LHSResult.isUsable()) | |||
7930 | return ExprError(); | |||
7931 | } | |||
7932 | ||||
7933 | if (!RHSResult.isUsable()) | |||
7934 | return ExprError(); | |||
7935 | ||||
7936 | CondExpr = CondResult.get(); | |||
7937 | LHSExpr = LHSResult.get(); | |||
7938 | RHSExpr = RHSResult.get(); | |||
7939 | } | |||
7940 | ||||
7941 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS | |||
7942 | // was the condition. | |||
7943 | OpaqueValueExpr *opaqueValue = nullptr; | |||
7944 | Expr *commonExpr = nullptr; | |||
7945 | if (!LHSExpr) { | |||
7946 | commonExpr = CondExpr; | |||
7947 | // Lower out placeholder types first. This is important so that we don't | |||
7948 | // try to capture a placeholder. This happens in few cases in C++; such | |||
7949 | // as Objective-C++'s dictionary subscripting syntax. | |||
7950 | if (commonExpr->hasPlaceholderType()) { | |||
7951 | ExprResult result = CheckPlaceholderExpr(commonExpr); | |||
7952 | if (!result.isUsable()) return ExprError(); | |||
7953 | commonExpr = result.get(); | |||
7954 | } | |||
7955 | // We usually want to apply unary conversions *before* saving, except | |||
7956 | // in the special case of a C++ l-value conditional. | |||
7957 | if (!(getLangOpts().CPlusPlus | |||
7958 | && !commonExpr->isTypeDependent() | |||
7959 | && commonExpr->getValueKind() == RHSExpr->getValueKind() | |||
7960 | && commonExpr->isGLValue() | |||
7961 | && commonExpr->isOrdinaryOrBitFieldObject() | |||
7962 | && RHSExpr->isOrdinaryOrBitFieldObject() | |||
7963 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { | |||
7964 | ExprResult commonRes = UsualUnaryConversions(commonExpr); | |||
7965 | if (commonRes.isInvalid()) | |||
7966 | return ExprError(); | |||
7967 | commonExpr = commonRes.get(); | |||
7968 | } | |||
7969 | ||||
7970 | // If the common expression is a class or array prvalue, materialize it | |||
7971 | // so that we can safely refer to it multiple times. | |||
7972 | if (commonExpr->isRValue() && (commonExpr->getType()->isRecordType() || | |||
7973 | commonExpr->getType()->isArrayType())) { | |||
7974 | ExprResult MatExpr = TemporaryMaterializationConversion(commonExpr); | |||
7975 | if (MatExpr.isInvalid()) | |||
7976 | return ExprError(); | |||
7977 | commonExpr = MatExpr.get(); | |||
7978 | } | |||
7979 | ||||
7980 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), | |||
7981 | commonExpr->getType(), | |||
7982 | commonExpr->getValueKind(), | |||
7983 | commonExpr->getObjectKind(), | |||
7984 | commonExpr); | |||
7985 | LHSExpr = CondExpr = opaqueValue; | |||
7986 | } | |||
7987 | ||||
7988 | QualType LHSTy = LHSExpr->getType(), RHSTy = RHSExpr->getType(); | |||
7989 | ExprValueKind VK = VK_RValue; | |||
7990 | ExprObjectKind OK = OK_Ordinary; | |||
7991 | ExprResult Cond = CondExpr, LHS = LHSExpr, RHS = RHSExpr; | |||
7992 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, | |||
7993 | VK, OK, QuestionLoc); | |||
7994 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || | |||
7995 | RHS.isInvalid()) | |||
7996 | return ExprError(); | |||
7997 | ||||
7998 | DiagnoseConditionalPrecedence(*this, QuestionLoc, Cond.get(), LHS.get(), | |||
7999 | RHS.get()); | |||
8000 | ||||
8001 | CheckBoolLikeConversion(Cond.get(), QuestionLoc); | |||
8002 | ||||
8003 | result = computeConditionalNullability(result, commonExpr, LHSTy, RHSTy, | |||
8004 | Context); | |||
8005 | ||||
8006 | if (!commonExpr) | |||
8007 | return new (Context) | |||
8008 | ConditionalOperator(Cond.get(), QuestionLoc, LHS.get(), ColonLoc, | |||
8009 | RHS.get(), result, VK, OK); | |||
8010 | ||||
8011 | return new (Context) BinaryConditionalOperator( | |||
8012 | commonExpr, opaqueValue, Cond.get(), LHS.get(), RHS.get(), QuestionLoc, | |||
8013 | ColonLoc, result, VK, OK); | |||
8014 | } | |||
8015 | ||||
8016 | // checkPointerTypesForAssignment - This is a very tricky routine (despite | |||
8017 | // being closely modeled after the C99 spec:-). The odd characteristic of this | |||
8018 | // routine is it effectively iqnores the qualifiers on the top level pointee. | |||
8019 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. | |||
8020 | // FIXME: add a couple examples in this comment. | |||
8021 | static Sema::AssignConvertType | |||
8022 | checkPointerTypesForAssignment(Sema &S, QualType LHSType, QualType RHSType) { | |||
8023 | assert(LHSType.isCanonical() && "LHS not canonicalized!")((LHSType.isCanonical() && "LHS not canonicalized!") ? static_cast<void> (0) : __assert_fail ("LHSType.isCanonical() && \"LHS not canonicalized!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 8023, __PRETTY_FUNCTION__)); | |||
8024 | assert(RHSType.isCanonical() && "RHS not canonicalized!")((RHSType.isCanonical() && "RHS not canonicalized!") ? static_cast<void> (0) : __assert_fail ("RHSType.isCanonical() && \"RHS not canonicalized!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 8024, __PRETTY_FUNCTION__)); | |||
8025 | ||||
8026 | // get the "pointed to" type (ignoring qualifiers at the top level) | |||
8027 | const Type *lhptee, *rhptee; | |||
8028 | Qualifiers lhq, rhq; | |||
8029 | std::tie(lhptee, lhq) = | |||
8030 | cast<PointerType>(LHSType)->getPointeeType().split().asPair(); | |||
8031 | std::tie(rhptee, rhq) = | |||
8032 | cast<PointerType>(RHSType)->getPointeeType().split().asPair(); | |||
8033 | ||||
8034 | Sema::AssignConvertType ConvTy = Sema::Compatible; | |||
8035 | ||||
8036 | // C99 6.5.16.1p1: This following citation is common to constraints | |||
8037 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the | |||
8038 | // qualifiers of the type *pointed to* by the right; | |||
8039 | ||||
8040 | // As a special case, 'non-__weak A *' -> 'non-__weak const *' is okay. | |||
8041 | if (lhq.getObjCLifetime() != rhq.getObjCLifetime() && | |||
8042 | lhq.compatiblyIncludesObjCLifetime(rhq)) { | |||
8043 | // Ignore lifetime for further calculation. | |||
8044 | lhq.removeObjCLifetime(); | |||
8045 | rhq.removeObjCLifetime(); | |||
8046 | } | |||
8047 | ||||
8048 | if (!lhq.compatiblyIncludes(rhq)) { | |||
8049 | // Treat address-space mismatches as fatal. | |||
8050 | if (!lhq.isAddressSpaceSupersetOf(rhq)) | |||
8051 | return Sema::IncompatiblePointerDiscardsQualifiers; | |||
8052 | ||||
8053 | // It's okay to add or remove GC or lifetime qualifiers when converting to | |||
8054 | // and from void*. | |||
8055 | else if (lhq.withoutObjCGCAttr().withoutObjCLifetime() | |||
8056 | .compatiblyIncludes( | |||
8057 | rhq.withoutObjCGCAttr().withoutObjCLifetime()) | |||
8058 | && (lhptee->isVoidType() || rhptee->isVoidType())) | |||
8059 | ; // keep old | |||
8060 | ||||
8061 | // Treat lifetime mismatches as fatal. | |||
8062 | else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) | |||
8063 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; | |||
8064 | ||||
8065 | // For GCC/MS compatibility, other qualifier mismatches are treated | |||
8066 | // as still compatible in C. | |||
8067 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; | |||
8068 | } | |||
8069 | ||||
8070 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or | |||
8071 | // incomplete type and the other is a pointer to a qualified or unqualified | |||
8072 | // version of void... | |||
8073 | if (lhptee->isVoidType()) { | |||
8074 | if (rhptee->isIncompleteOrObjectType()) | |||
8075 | return ConvTy; | |||
8076 | ||||
8077 | // As an extension, we allow cast to/from void* to function pointer. | |||
8078 | assert(rhptee->isFunctionType())((rhptee->isFunctionType()) ? static_cast<void> (0) : __assert_fail ("rhptee->isFunctionType()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 8078, __PRETTY_FUNCTION__)); | |||
8079 | return Sema::FunctionVoidPointer; | |||
8080 | } | |||
8081 | ||||
8082 | if (rhptee->isVoidType()) { | |||
8083 | if (lhptee->isIncompleteOrObjectType()) | |||
8084 | return ConvTy; | |||
8085 | ||||
8086 | // As an extension, we allow cast to/from void* to function pointer. | |||
8087 | assert(lhptee->isFunctionType())((lhptee->isFunctionType()) ? static_cast<void> (0) : __assert_fail ("lhptee->isFunctionType()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 8087, __PRETTY_FUNCTION__)); | |||
8088 | return Sema::FunctionVoidPointer; | |||
8089 | } | |||
8090 | ||||
8091 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or | |||
8092 | // unqualified versions of compatible types, ... | |||
8093 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); | |||
8094 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { | |||
8095 | // Check if the pointee types are compatible ignoring the sign. | |||
8096 | // We explicitly check for char so that we catch "char" vs | |||
8097 | // "unsigned char" on systems where "char" is unsigned. | |||
8098 | if (lhptee->isCharType()) | |||
8099 | ltrans = S.Context.UnsignedCharTy; | |||
8100 | else if (lhptee->hasSignedIntegerRepresentation()) | |||
8101 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); | |||
8102 | ||||
8103 | if (rhptee->isCharType()) | |||
8104 | rtrans = S.Context.UnsignedCharTy; | |||
8105 | else if (rhptee->hasSignedIntegerRepresentation()) | |||
8106 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); | |||
8107 | ||||
8108 | if (ltrans == rtrans) { | |||
8109 | // Types are compatible ignoring the sign. Qualifier incompatibility | |||
8110 | // takes priority over sign incompatibility because the sign | |||
8111 | // warning can be disabled. | |||
8112 | if (ConvTy != Sema::Compatible) | |||
8113 | return ConvTy; | |||
8114 | ||||
8115 | return Sema::IncompatiblePointerSign; | |||
8116 | } | |||
8117 | ||||
8118 | // If we are a multi-level pointer, it's possible that our issue is simply | |||
8119 | // one of qualification - e.g. char ** -> const char ** is not allowed. If | |||
8120 | // the eventual target type is the same and the pointers have the same | |||
8121 | // level of indirection, this must be the issue. | |||
8122 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { | |||
8123 | do { | |||
8124 | std::tie(lhptee, lhq) = | |||
8125 | cast<PointerType>(lhptee)->getPointeeType().split().asPair(); | |||
8126 | std::tie(rhptee, rhq) = | |||
8127 | cast<PointerType>(rhptee)->getPointeeType().split().asPair(); | |||
8128 | ||||
8129 | // Inconsistent address spaces at this point is invalid, even if the | |||
8130 | // address spaces would be compatible. | |||
8131 | // FIXME: This doesn't catch address space mismatches for pointers of | |||
8132 | // different nesting levels, like: | |||
8133 | // __local int *** a; | |||
8134 | // int ** b = a; | |||
8135 | // It's not clear how to actually determine when such pointers are | |||
8136 | // invalidly incompatible. | |||
8137 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) | |||
8138 | return Sema::IncompatibleNestedPointerAddressSpaceMismatch; | |||
8139 | ||||
8140 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); | |||
8141 | ||||
8142 | if (lhptee == rhptee) | |||
8143 | return Sema::IncompatibleNestedPointerQualifiers; | |||
8144 | } | |||
8145 | ||||
8146 | // General pointer incompatibility takes priority over qualifiers. | |||
8147 | if (RHSType->isFunctionPointerType() && LHSType->isFunctionPointerType()) | |||
8148 | return Sema::IncompatibleFunctionPointer; | |||
8149 | return Sema::IncompatiblePointer; | |||
8150 | } | |||
8151 | if (!S.getLangOpts().CPlusPlus && | |||
8152 | S.IsFunctionConversion(ltrans, rtrans, ltrans)) | |||
8153 | return Sema::IncompatibleFunctionPointer; | |||
8154 | return ConvTy; | |||
8155 | } | |||
8156 | ||||
8157 | /// checkBlockPointerTypesForAssignment - This routine determines whether two | |||
8158 | /// block pointer types are compatible or whether a block and normal pointer | |||
8159 | /// are compatible. It is more restrict than comparing two function pointer | |||
8160 | // types. | |||
8161 | static Sema::AssignConvertType | |||
8162 | checkBlockPointerTypesForAssignment(Sema &S, QualType LHSType, | |||
8163 | QualType RHSType) { | |||
8164 | assert(LHSType.isCanonical() && "LHS not canonicalized!")((LHSType.isCanonical() && "LHS not canonicalized!") ? static_cast<void> (0) : __assert_fail ("LHSType.isCanonical() && \"LHS not canonicalized!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 8164, __PRETTY_FUNCTION__)); | |||
8165 | assert(RHSType.isCanonical() && "RHS not canonicalized!")((RHSType.isCanonical() && "RHS not canonicalized!") ? static_cast<void> (0) : __assert_fail ("RHSType.isCanonical() && \"RHS not canonicalized!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 8165, __PRETTY_FUNCTION__)); | |||
8166 | ||||
8167 | QualType lhptee, rhptee; | |||
8168 | ||||
8169 | // get the "pointed to" type (ignoring qualifiers at the top level) | |||
8170 | lhptee = cast<BlockPointerType>(LHSType)->getPointeeType(); | |||
8171 | rhptee = cast<BlockPointerType>(RHSType)->getPointeeType(); | |||
8172 | ||||
8173 | // In C++, the types have to match exactly. | |||
8174 | if (S.getLangOpts().CPlusPlus) | |||
8175 | return Sema::IncompatibleBlockPointer; | |||
8176 | ||||
8177 | Sema::AssignConvertType ConvTy = Sema::Compatible; | |||
8178 | ||||
8179 | // For blocks we enforce that qualifiers are identical. | |||
8180 | Qualifiers LQuals = lhptee.getLocalQualifiers(); | |||
8181 | Qualifiers RQuals = rhptee.getLocalQualifiers(); | |||
8182 | if (S.getLangOpts().OpenCL) { | |||
8183 | LQuals.removeAddressSpace(); | |||
8184 | RQuals.removeAddressSpace(); | |||
8185 | } | |||
8186 | if (LQuals != RQuals) | |||
8187 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; | |||
8188 | ||||
8189 | // FIXME: OpenCL doesn't define the exact compile time semantics for a block | |||
8190 | // assignment. | |||
8191 | // The current behavior is similar to C++ lambdas. A block might be | |||
8192 | // assigned to a variable iff its return type and parameters are compatible | |||
8193 | // (C99 6.2.7) with the corresponding return type and parameters of the LHS of | |||
8194 | // an assignment. Presumably it should behave in way that a function pointer | |||
8195 | // assignment does in C, so for each parameter and return type: | |||
8196 | // * CVR and address space of LHS should be a superset of CVR and address | |||
8197 | // space of RHS. | |||
8198 | // * unqualified types should be compatible. | |||
8199 | if (S.getLangOpts().OpenCL) { | |||
8200 | if (!S.Context.typesAreBlockPointerCompatible( | |||
8201 | S.Context.getQualifiedType(LHSType.getUnqualifiedType(), LQuals), | |||
8202 | S.Context.getQualifiedType(RHSType.getUnqualifiedType(), RQuals))) | |||
8203 | return Sema::IncompatibleBlockPointer; | |||
8204 | } else if (!S.Context.typesAreBlockPointerCompatible(LHSType, RHSType)) | |||
8205 | return Sema::IncompatibleBlockPointer; | |||
8206 | ||||
8207 | return ConvTy; | |||
8208 | } | |||
8209 | ||||
8210 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types | |||
8211 | /// for assignment compatibility. | |||
8212 | static Sema::AssignConvertType | |||
8213 | checkObjCPointerTypesForAssignment(Sema &S, QualType LHSType, | |||
8214 | QualType RHSType) { | |||
8215 | assert(LHSType.isCanonical() && "LHS was not canonicalized!")((LHSType.isCanonical() && "LHS was not canonicalized!" ) ? static_cast<void> (0) : __assert_fail ("LHSType.isCanonical() && \"LHS was not canonicalized!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 8215, __PRETTY_FUNCTION__)); | |||
8216 | assert(RHSType.isCanonical() && "RHS was not canonicalized!")((RHSType.isCanonical() && "RHS was not canonicalized!" ) ? static_cast<void> (0) : __assert_fail ("RHSType.isCanonical() && \"RHS was not canonicalized!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 8216, __PRETTY_FUNCTION__)); | |||
8217 | ||||
8218 | if (LHSType->isObjCBuiltinType()) { | |||
8219 | // Class is not compatible with ObjC object pointers. | |||
8220 | if (LHSType->isObjCClassType() && !RHSType->isObjCBuiltinType() && | |||
8221 | !RHSType->isObjCQualifiedClassType()) | |||
8222 | return Sema::IncompatiblePointer; | |||
8223 | return Sema::Compatible; | |||
8224 | } | |||
8225 | if (RHSType->isObjCBuiltinType()) { | |||
8226 | if (RHSType->isObjCClassType() && !LHSType->isObjCBuiltinType() && | |||
8227 | !LHSType->isObjCQualifiedClassType()) | |||
8228 | return Sema::IncompatiblePointer; | |||
8229 | return Sema::Compatible; | |||
8230 | } | |||
8231 | QualType lhptee = LHSType->castAs<ObjCObjectPointerType>()->getPointeeType(); | |||
8232 | QualType rhptee = RHSType->castAs<ObjCObjectPointerType>()->getPointeeType(); | |||
8233 | ||||
8234 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee) && | |||
8235 | // make an exception for id<P> | |||
8236 | !LHSType->isObjCQualifiedIdType()) | |||
8237 | return Sema::CompatiblePointerDiscardsQualifiers; | |||
8238 | ||||
8239 | if (S.Context.typesAreCompatible(LHSType, RHSType)) | |||
8240 | return Sema::Compatible; | |||
8241 | if (LHSType->isObjCQualifiedIdType() || RHSType->isObjCQualifiedIdType()) | |||
8242 | return Sema::IncompatibleObjCQualifiedId; | |||
8243 | return Sema::IncompatiblePointer; | |||
8244 | } | |||
8245 | ||||
8246 | Sema::AssignConvertType | |||
8247 | Sema::CheckAssignmentConstraints(SourceLocation Loc, | |||
8248 | QualType LHSType, QualType RHSType) { | |||
8249 | // Fake up an opaque expression. We don't actually care about what | |||
8250 | // cast operations are required, so if CheckAssignmentConstraints | |||
8251 | // adds casts to this they'll be wasted, but fortunately that doesn't | |||
8252 | // usually happen on valid code. | |||
8253 | OpaqueValueExpr RHSExpr(Loc, RHSType, VK_RValue); | |||
8254 | ExprResult RHSPtr = &RHSExpr; | |||
8255 | CastKind K; | |||
8256 | ||||
8257 | return CheckAssignmentConstraints(LHSType, RHSPtr, K, /*ConvertRHS=*/false); | |||
8258 | } | |||
8259 | ||||
8260 | /// This helper function returns true if QT is a vector type that has element | |||
8261 | /// type ElementType. | |||
8262 | static bool isVector(QualType QT, QualType ElementType) { | |||
8263 | if (const VectorType *VT = QT->getAs<VectorType>()) | |||
8264 | return VT->getElementType() == ElementType; | |||
8265 | return false; | |||
8266 | } | |||
8267 | ||||
8268 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently | |||
8269 | /// has code to accommodate several GCC extensions when type checking | |||
8270 | /// pointers. Here are some objectionable examples that GCC considers warnings: | |||
8271 | /// | |||
8272 | /// int a, *pint; | |||
8273 | /// short *pshort; | |||
8274 | /// struct foo *pfoo; | |||
8275 | /// | |||
8276 | /// pint = pshort; // warning: assignment from incompatible pointer type | |||
8277 | /// a = pint; // warning: assignment makes integer from pointer without a cast | |||
8278 | /// pint = a; // warning: assignment makes pointer from integer without a cast | |||
8279 | /// pint = pfoo; // warning: assignment from incompatible pointer type | |||
8280 | /// | |||
8281 | /// As a result, the code for dealing with pointers is more complex than the | |||
8282 | /// C99 spec dictates. | |||
8283 | /// | |||
8284 | /// Sets 'Kind' for any result kind except Incompatible. | |||
8285 | Sema::AssignConvertType | |||
8286 | Sema::CheckAssignmentConstraints(QualType LHSType, ExprResult &RHS, | |||
8287 | CastKind &Kind, bool ConvertRHS) { | |||
8288 | QualType RHSType = RHS.get()->getType(); | |||
8289 | QualType OrigLHSType = LHSType; | |||
8290 | ||||
8291 | // Get canonical types. We're not formatting these types, just comparing | |||
8292 | // them. | |||
8293 | LHSType = Context.getCanonicalType(LHSType).getUnqualifiedType(); | |||
8294 | RHSType = Context.getCanonicalType(RHSType).getUnqualifiedType(); | |||
8295 | ||||
8296 | // Common case: no conversion required. | |||
8297 | if (LHSType == RHSType) { | |||
8298 | Kind = CK_NoOp; | |||
8299 | return Compatible; | |||
8300 | } | |||
8301 | ||||
8302 | // If we have an atomic type, try a non-atomic assignment, then just add an | |||
8303 | // atomic qualification step. | |||
8304 | if (const AtomicType *AtomicTy = dyn_cast<AtomicType>(LHSType)) { | |||
8305 | Sema::AssignConvertType result = | |||
8306 | CheckAssignmentConstraints(AtomicTy->getValueType(), RHS, Kind); | |||
8307 | if (result != Compatible) | |||
8308 | return result; | |||
8309 | if (Kind != CK_NoOp && ConvertRHS) | |||
8310 | RHS = ImpCastExprToType(RHS.get(), AtomicTy->getValueType(), Kind); | |||
8311 | Kind = CK_NonAtomicToAtomic; | |||
8312 | return Compatible; | |||
8313 | } | |||
8314 | ||||
8315 | // If the left-hand side is a reference type, then we are in a | |||
8316 | // (rare!) case where we've allowed the use of references in C, | |||
8317 | // e.g., as a parameter type in a built-in function. In this case, | |||
8318 | // just make sure that the type referenced is compatible with the | |||
8319 | // right-hand side type. The caller is responsible for adjusting | |||
8320 | // LHSType so that the resulting expression does not have reference | |||
8321 | // type. | |||
8322 | if (const ReferenceType *LHSTypeRef = LHSType->getAs<ReferenceType>()) { | |||
8323 | if (Context.typesAreCompatible(LHSTypeRef->getPointeeType(), RHSType)) { | |||
8324 | Kind = CK_LValueBitCast; | |||
8325 | return Compatible; | |||
8326 | } | |||
8327 | return Incompatible; | |||
8328 | } | |||
8329 | ||||
8330 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type | |||
8331 | // to the same ExtVector type. | |||
8332 | if (LHSType->isExtVectorType()) { | |||
8333 | if (RHSType->isExtVectorType()) | |||
8334 | return Incompatible; | |||
8335 | if (RHSType->isArithmeticType()) { | |||
8336 | // CK_VectorSplat does T -> vector T, so first cast to the element type. | |||
8337 | if (ConvertRHS) | |||
8338 | RHS = prepareVectorSplat(LHSType, RHS.get()); | |||
8339 | Kind = CK_VectorSplat; | |||
8340 | return Compatible; | |||
8341 | } | |||
8342 | } | |||
8343 | ||||
8344 | // Conversions to or from vector type. | |||
8345 | if (LHSType->isVectorType() || RHSType->isVectorType()) { | |||
8346 | if (LHSType->isVectorType() && RHSType->isVectorType()) { | |||
8347 | // Allow assignments of an AltiVec vector type to an equivalent GCC | |||
8348 | // vector type and vice versa | |||
8349 | if (Context.areCompatibleVectorTypes(LHSType, RHSType)) { | |||
8350 | Kind = CK_BitCast; | |||
8351 | return Compatible; | |||
8352 | } | |||
8353 | ||||
8354 | // If we are allowing lax vector conversions, and LHS and RHS are both | |||
8355 | // vectors, the total size only needs to be the same. This is a bitcast; | |||
8356 | // no bits are changed but the result type is different. | |||
8357 | if (isLaxVectorConversion(RHSType, LHSType)) { | |||
8358 | Kind = CK_BitCast; | |||
8359 | return IncompatibleVectors; | |||
8360 | } | |||
8361 | } | |||
8362 | ||||
8363 | // When the RHS comes from another lax conversion (e.g. binops between | |||
8364 | // scalars and vectors) the result is canonicalized as a vector. When the | |||
8365 | // LHS is also a vector, the lax is allowed by the condition above. Handle | |||
8366 | // the case where LHS is a scalar. | |||
8367 | if (LHSType->isScalarType()) { | |||
8368 | const VectorType *VecType = RHSType->getAs<VectorType>(); | |||
8369 | if (VecType && VecType->getNumElements() == 1 && | |||
8370 | isLaxVectorConversion(RHSType, LHSType)) { | |||
8371 | ExprResult *VecExpr = &RHS; | |||
8372 | *VecExpr = ImpCastExprToType(VecExpr->get(), LHSType, CK_BitCast); | |||
8373 | Kind = CK_BitCast; | |||
8374 | return Compatible; | |||
8375 | } | |||
8376 | } | |||
8377 | ||||
8378 | return Incompatible; | |||
8379 | } | |||
8380 | ||||
8381 | // Diagnose attempts to convert between __float128 and long double where | |||
8382 | // such conversions currently can't be handled. | |||
8383 | if (unsupportedTypeConversion(*this, LHSType, RHSType)) | |||
8384 | return Incompatible; | |||
8385 | ||||
8386 | // Disallow assigning a _Complex to a real type in C++ mode since it simply | |||
8387 | // discards the imaginary part. | |||
8388 | if (getLangOpts().CPlusPlus && RHSType->getAs<ComplexType>() && | |||
8389 | !LHSType->getAs<ComplexType>()) | |||
8390 | return Incompatible; | |||
8391 | ||||
8392 | // Arithmetic conversions. | |||
8393 | if (LHSType->isArithmeticType() && RHSType->isArithmeticType() && | |||
8394 | !(getLangOpts().CPlusPlus && LHSType->isEnumeralType())) { | |||
8395 | if (ConvertRHS) | |||
8396 | Kind = PrepareScalarCast(RHS, LHSType); | |||
8397 | return Compatible; | |||
8398 | } | |||
8399 | ||||
8400 | // Conversions to normal pointers. | |||
8401 | if (const PointerType *LHSPointer = dyn_cast<PointerType>(LHSType)) { | |||
8402 | // U* -> T* | |||
8403 | if (isa<PointerType>(RHSType)) { | |||
8404 | LangAS AddrSpaceL = LHSPointer->getPointeeType().getAddressSpace(); | |||
8405 | LangAS AddrSpaceR = RHSType->getPointeeType().getAddressSpace(); | |||
8406 | if (AddrSpaceL != AddrSpaceR) | |||
8407 | Kind = CK_AddressSpaceConversion; | |||
8408 | else if (Context.hasCvrSimilarType(RHSType, LHSType)) | |||
8409 | Kind = CK_NoOp; | |||
8410 | else | |||
8411 | Kind = CK_BitCast; | |||
8412 | return checkPointerTypesForAssignment(*this, LHSType, RHSType); | |||
8413 | } | |||
8414 | ||||
8415 | // int -> T* | |||
8416 | if (RHSType->isIntegerType()) { | |||
8417 | Kind = CK_IntegralToPointer; // FIXME: null? | |||
8418 | return IntToPointer; | |||
8419 | } | |||
8420 | ||||
8421 | // C pointers are not compatible with ObjC object pointers, | |||
8422 | // with two exceptions: | |||
8423 | if (isa<ObjCObjectPointerType>(RHSType)) { | |||
8424 | // - conversions to void* | |||
8425 | if (LHSPointer->getPointeeType()->isVoidType()) { | |||
8426 | Kind = CK_BitCast; | |||
8427 | return Compatible; | |||
8428 | } | |||
8429 | ||||
8430 | // - conversions from 'Class' to the redefinition type | |||
8431 | if (RHSType->isObjCClassType() && | |||
8432 | Context.hasSameType(LHSType, | |||
8433 | Context.getObjCClassRedefinitionType())) { | |||
8434 | Kind = CK_BitCast; | |||
8435 | return Compatible; | |||
8436 | } | |||
8437 | ||||
8438 | Kind = CK_BitCast; | |||
8439 | return IncompatiblePointer; | |||
8440 | } | |||
8441 | ||||
8442 | // U^ -> void* | |||
8443 | if (RHSType->getAs<BlockPointerType>()) { | |||
8444 | if (LHSPointer->getPointeeType()->isVoidType()) { | |||
8445 | LangAS AddrSpaceL = LHSPointer->getPointeeType().getAddressSpace(); | |||
8446 | LangAS AddrSpaceR = RHSType->getAs<BlockPointerType>() | |||
8447 | ->getPointeeType() | |||
8448 | .getAddressSpace(); | |||
8449 | Kind = | |||
8450 | AddrSpaceL != AddrSpaceR ? CK_AddressSpaceConversion : CK_BitCast; | |||
8451 | return Compatible; | |||
8452 | } | |||
8453 | } | |||
8454 | ||||
8455 | return Incompatible; | |||
8456 | } | |||
8457 | ||||
8458 | // Conversions to block pointers. | |||
8459 | if (isa<BlockPointerType>(LHSType)) { | |||
8460 | // U^ -> T^ | |||
8461 | if (RHSType->isBlockPointerType()) { | |||
8462 | LangAS AddrSpaceL = LHSType->getAs<BlockPointerType>() | |||
8463 | ->getPointeeType() | |||
8464 | .getAddressSpace(); | |||
8465 | LangAS AddrSpaceR = RHSType->getAs<BlockPointerType>() | |||
8466 | ->getPointeeType() | |||
8467 | .getAddressSpace(); | |||
8468 | Kind = AddrSpaceL != AddrSpaceR ? CK_AddressSpaceConversion : CK_BitCast; | |||
8469 | return checkBlockPointerTypesForAssignment(*this, LHSType, RHSType); | |||
8470 | } | |||
8471 | ||||
8472 | // int or null -> T^ | |||
8473 | if (RHSType->isIntegerType()) { | |||
8474 | Kind = CK_IntegralToPointer; // FIXME: null | |||
8475 | return IntToBlockPointer; | |||
8476 | } | |||
8477 | ||||
8478 | // id -> T^ | |||
8479 | if (getLangOpts().ObjC && RHSType->isObjCIdType()) { | |||
8480 | Kind = CK_AnyPointerToBlockPointerCast; | |||
8481 | return Compatible; | |||
8482 | } | |||
8483 | ||||
8484 | // void* -> T^ | |||
8485 | if (const PointerType *RHSPT = RHSType->getAs<PointerType>()) | |||
8486 | if (RHSPT->getPointeeType()->isVoidType()) { | |||
8487 | Kind = CK_AnyPointerToBlockPointerCast; | |||
8488 | return Compatible; | |||
8489 | } | |||
8490 | ||||
8491 | return Incompatible; | |||
8492 | } | |||
8493 | ||||
8494 | // Conversions to Objective-C pointers. | |||
8495 | if (isa<ObjCObjectPointerType>(LHSType)) { | |||
8496 | // A* -> B* | |||
8497 | if (RHSType->isObjCObjectPointerType()) { | |||
8498 | Kind = CK_BitCast; | |||
8499 | Sema::AssignConvertType result = | |||
8500 | checkObjCPointerTypesForAssignment(*this, LHSType, RHSType); | |||
8501 | if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() && | |||
8502 | result == Compatible && | |||
8503 | !CheckObjCARCUnavailableWeakConversion(OrigLHSType, RHSType)) | |||
8504 | result = IncompatibleObjCWeakRef; | |||
8505 | return result; | |||
8506 | } | |||
8507 | ||||
8508 | // int or null -> A* | |||
8509 | if (RHSType->isIntegerType()) { | |||
8510 | Kind = CK_IntegralToPointer; // FIXME: null | |||
8511 | return IntToPointer; | |||
8512 | } | |||
8513 | ||||
8514 | // In general, C pointers are not compatible with ObjC object pointers, | |||
8515 | // with two exceptions: | |||
8516 | if (isa<PointerType>(RHSType)) { | |||
8517 | Kind = CK_CPointerToObjCPointerCast; | |||
8518 | ||||
8519 | // - conversions from 'void*' | |||
8520 | if (RHSType->isVoidPointerType()) { | |||
8521 | return Compatible; | |||
8522 | } | |||
8523 | ||||
8524 | // - conversions to 'Class' from its redefinition type | |||
8525 | if (LHSType->isObjCClassType() && | |||
8526 | Context.hasSameType(RHSType, | |||
8527 | Context.getObjCClassRedefinitionType())) { | |||
8528 | return Compatible; | |||
8529 | } | |||
8530 | ||||
8531 | return IncompatiblePointer; | |||
8532 | } | |||
8533 | ||||
8534 | // Only under strict condition T^ is compatible with an Objective-C pointer. | |||
8535 | if (RHSType->isBlockPointerType() && | |||
8536 | LHSType->isBlockCompatibleObjCPointerType(Context)) { | |||
8537 | if (ConvertRHS) | |||
8538 | maybeExtendBlockObject(RHS); | |||
8539 | Kind = CK_BlockPointerToObjCPointerCast; | |||
8540 | return Compatible; | |||
8541 | } | |||
8542 | ||||
8543 | return Incompatible; | |||
8544 | } | |||
8545 | ||||
8546 | // Conversions from pointers that are not covered by the above. | |||
8547 | if (isa<PointerType>(RHSType)) { | |||
8548 | // T* -> _Bool | |||
8549 | if (LHSType == Context.BoolTy) { | |||
8550 | Kind = CK_PointerToBoolean; | |||
8551 | return Compatible; | |||
8552 | } | |||
8553 | ||||
8554 | // T* -> int | |||
8555 | if (LHSType->isIntegerType()) { | |||
8556 | Kind = CK_PointerToIntegral; | |||
8557 | return PointerToInt; | |||
8558 | } | |||
8559 | ||||
8560 | return Incompatible; | |||
8561 | } | |||
8562 | ||||
8563 | // Conversions from Objective-C pointers that are not covered by the above. | |||
8564 | if (isa<ObjCObjectPointerType>(RHSType)) { | |||
8565 | // T* -> _Bool | |||
8566 | if (LHSType == Context.BoolTy) { | |||
8567 | Kind = CK_PointerToBoolean; | |||
8568 | return Compatible; | |||
8569 | } | |||
8570 | ||||
8571 | // T* -> int | |||
8572 | if (LHSType->isIntegerType()) { | |||
8573 | Kind = CK_PointerToIntegral; | |||
8574 | return PointerToInt; | |||
8575 | } | |||
8576 | ||||
8577 | return Incompatible; | |||
8578 | } | |||
8579 | ||||
8580 | // struct A -> struct B | |||
8581 | if (isa<TagType>(LHSType) && isa<TagType>(RHSType)) { | |||
8582 | if (Context.typesAreCompatible(LHSType, RHSType)) { | |||
8583 | Kind = CK_NoOp; | |||
8584 | return Compatible; | |||
8585 | } | |||
8586 | } | |||
8587 | ||||
8588 | if (LHSType->isSamplerT() && RHSType->isIntegerType()) { | |||
8589 | Kind = CK_IntToOCLSampler; | |||
8590 | return Compatible; | |||
8591 | } | |||
8592 | ||||
8593 | return Incompatible; | |||
8594 | } | |||
8595 | ||||
8596 | /// Constructs a transparent union from an expression that is | |||
8597 | /// used to initialize the transparent union. | |||
8598 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, | |||
8599 | ExprResult &EResult, QualType UnionType, | |||
8600 | FieldDecl *Field) { | |||
8601 | // Build an initializer list that designates the appropriate member | |||
8602 | // of the transparent union. | |||
8603 | Expr *E = EResult.get(); | |||
8604 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), | |||
8605 | E, SourceLocation()); | |||
8606 | Initializer->setType(UnionType); | |||
8607 | Initializer->setInitializedFieldInUnion(Field); | |||
8608 | ||||
8609 | // Build a compound literal constructing a value of the transparent | |||
8610 | // union type from this initializer list. | |||
8611 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); | |||
8612 | EResult = new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, | |||
8613 | VK_RValue, Initializer, false); | |||
8614 | } | |||
8615 | ||||
8616 | Sema::AssignConvertType | |||
8617 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, | |||
8618 | ExprResult &RHS) { | |||
8619 | QualType RHSType = RHS.get()->getType(); | |||
8620 | ||||
8621 | // If the ArgType is a Union type, we want to handle a potential | |||
8622 | // transparent_union GCC extension. | |||
8623 | const RecordType *UT = ArgType->getAsUnionType(); | |||
8624 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) | |||
8625 | return Incompatible; | |||
8626 | ||||
8627 | // The field to initialize within the transparent union. | |||
8628 | RecordDecl *UD = UT->getDecl(); | |||
8629 | FieldDecl *InitField = nullptr; | |||
8630 | // It's compatible if the expression matches any of the fields. | |||
8631 | for (auto *it : UD->fields()) { | |||
8632 | if (it->getType()->isPointerType()) { | |||
8633 | // If the transparent union contains a pointer type, we allow: | |||
8634 | // 1) void pointer | |||
8635 | // 2) null pointer constant | |||
8636 | if (RHSType->isPointerType()) | |||
8637 | if (RHSType->castAs<PointerType>()->getPointeeType()->isVoidType()) { | |||
8638 | RHS = ImpCastExprToType(RHS.get(), it->getType(), CK_BitCast); | |||
8639 | InitField = it; | |||
8640 | break; | |||
8641 | } | |||
8642 | ||||
8643 | if (RHS.get()->isNullPointerConstant(Context, | |||
8644 | Expr::NPC_ValueDependentIsNull)) { | |||
8645 | RHS = ImpCastExprToType(RHS.get(), it->getType(), | |||
8646 | CK_NullToPointer); | |||
8647 | InitField = it; | |||
8648 | break; | |||
8649 | } | |||
8650 | } | |||
8651 | ||||
8652 | CastKind Kind; | |||
8653 | if (CheckAssignmentConstraints(it->getType(), RHS, Kind) | |||
8654 | == Compatible) { | |||
8655 | RHS = ImpCastExprToType(RHS.get(), it->getType(), Kind); | |||
8656 | InitField = it; | |||
8657 | break; | |||
8658 | } | |||
8659 | } | |||
8660 | ||||
8661 | if (!InitField) | |||
8662 | return Incompatible; | |||
8663 | ||||
8664 | ConstructTransparentUnion(*this, Context, RHS, ArgType, InitField); | |||
8665 | return Compatible; | |||
8666 | } | |||
8667 | ||||
8668 | Sema::AssignConvertType | |||
8669 | Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &CallerRHS, | |||
8670 | bool Diagnose, | |||
8671 | bool DiagnoseCFAudited, | |||
8672 | bool ConvertRHS) { | |||
8673 | // We need to be able to tell the caller whether we diagnosed a problem, if | |||
8674 | // they ask us to issue diagnostics. | |||
8675 | assert((ConvertRHS || !Diagnose) && "can't indicate whether we diagnosed")(((ConvertRHS || !Diagnose) && "can't indicate whether we diagnosed" ) ? static_cast<void> (0) : __assert_fail ("(ConvertRHS || !Diagnose) && \"can't indicate whether we diagnosed\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 8675, __PRETTY_FUNCTION__)); | |||
8676 | ||||
8677 | // If ConvertRHS is false, we want to leave the caller's RHS untouched. Sadly, | |||
8678 | // we can't avoid *all* modifications at the moment, so we need some somewhere | |||
8679 | // to put the updated value. | |||
8680 | ExprResult LocalRHS = CallerRHS; | |||
8681 | ExprResult &RHS = ConvertRHS ? CallerRHS : LocalRHS; | |||
8682 | ||||
8683 | if (const auto *LHSPtrType = LHSType->getAs<PointerType>()) { | |||
8684 | if (const auto *RHSPtrType = RHS.get()->getType()->getAs<PointerType>()) { | |||
8685 | if (RHSPtrType->getPointeeType()->hasAttr(attr::NoDeref) && | |||
8686 | !LHSPtrType->getPointeeType()->hasAttr(attr::NoDeref)) { | |||
8687 | Diag(RHS.get()->getExprLoc(), | |||
8688 | diag::warn_noderef_to_dereferenceable_pointer) | |||
8689 | << RHS.get()->getSourceRange(); | |||
8690 | } | |||
8691 | } | |||
8692 | } | |||
8693 | ||||
8694 | if (getLangOpts().CPlusPlus) { | |||
8695 | if (!LHSType->isRecordType() && !LHSType->isAtomicType()) { | |||
8696 | // C++ 5.17p3: If the left operand is not of class type, the | |||
8697 | // expression is implicitly converted (C++ 4) to the | |||
8698 | // cv-unqualified type of the left operand. | |||
8699 | QualType RHSType = RHS.get()->getType(); | |||
8700 | if (Diagnose) { | |||
8701 | RHS = PerformImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), | |||
8702 | AA_Assigning); | |||
8703 | } else { | |||
8704 | ImplicitConversionSequence ICS = | |||
8705 | TryImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), | |||
8706 | /*SuppressUserConversions=*/false, | |||
8707 | AllowedExplicit::None, | |||
8708 | /*InOverloadResolution=*/false, | |||
8709 | /*CStyle=*/false, | |||
8710 | /*AllowObjCWritebackConversion=*/false); | |||
8711 | if (ICS.isFailure()) | |||
8712 | return Incompatible; | |||
8713 | RHS = PerformImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), | |||
8714 | ICS, AA_Assigning); | |||
8715 | } | |||
8716 | if (RHS.isInvalid()) | |||
8717 | return Incompatible; | |||
8718 | Sema::AssignConvertType result = Compatible; | |||
8719 | if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() && | |||
8720 | !CheckObjCARCUnavailableWeakConversion(LHSType, RHSType)) | |||
8721 | result = IncompatibleObjCWeakRef; | |||
8722 | return result; | |||
8723 | } | |||
8724 | ||||
8725 | // FIXME: Currently, we fall through and treat C++ classes like C | |||
8726 | // structures. | |||
8727 | // FIXME: We also fall through for atomics; not sure what should | |||
8728 | // happen there, though. | |||
8729 | } else if (RHS.get()->getType() == Context.OverloadTy) { | |||
8730 | // As a set of extensions to C, we support overloading on functions. These | |||
8731 | // functions need to be resolved here. | |||
8732 | DeclAccessPair DAP; | |||
8733 | if (FunctionDecl *FD = ResolveAddressOfOverloadedFunction( | |||
8734 | RHS.get(), LHSType, /*Complain=*/false, DAP)) | |||
8735 | RHS = FixOverloadedFunctionReference(RHS.get(), DAP, FD); | |||
8736 | else | |||
8737 | return Incompatible; | |||
8738 | } | |||
8739 | ||||
8740 | // C99 6.5.16.1p1: the left operand is a pointer and the right is | |||
8741 | // a null pointer constant. | |||
8742 | if ((LHSType->isPointerType() || LHSType->isObjCObjectPointerType() || | |||
8743 | LHSType->isBlockPointerType()) && | |||
8744 | RHS.get()->isNullPointerConstant(Context, | |||
8745 | Expr::NPC_ValueDependentIsNull)) { | |||
8746 | if (Diagnose || ConvertRHS) { | |||
8747 | CastKind Kind; | |||
8748 | CXXCastPath Path; | |||
8749 | CheckPointerConversion(RHS.get(), LHSType, Kind, Path, | |||
8750 | /*IgnoreBaseAccess=*/false, Diagnose); | |||
8751 | if (ConvertRHS) | |||
8752 | RHS = ImpCastExprToType(RHS.get(), LHSType, Kind, VK_RValue, &Path); | |||
8753 | } | |||
8754 | return Compatible; | |||
8755 | } | |||
8756 | ||||
8757 | // OpenCL queue_t type assignment. | |||
8758 | if (LHSType->isQueueT() && RHS.get()->isNullPointerConstant( | |||
8759 | Context, Expr::NPC_ValueDependentIsNull)) { | |||
8760 | RHS = ImpCastExprToType(RHS.get(), LHSType, CK_NullToPointer); | |||
8761 | return Compatible; | |||
8762 | } | |||
8763 | ||||
8764 | // This check seems unnatural, however it is necessary to ensure the proper | |||
8765 | // conversion of functions/arrays. If the conversion were done for all | |||
8766 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary | |||
8767 | // expressions that suppress this implicit conversion (&, sizeof). | |||
8768 | // | |||
8769 | // Suppress this for references: C++ 8.5.3p5. | |||
8770 | if (!LHSType->isReferenceType()) { | |||
8771 | // FIXME: We potentially allocate here even if ConvertRHS is false. | |||
8772 | RHS = DefaultFunctionArrayLvalueConversion(RHS.get(), Diagnose); | |||
8773 | if (RHS.isInvalid()) | |||
8774 | return Incompatible; | |||
8775 | } | |||
8776 | CastKind Kind; | |||
8777 | Sema::AssignConvertType result = | |||
8778 | CheckAssignmentConstraints(LHSType, RHS, Kind, ConvertRHS); | |||
8779 | ||||
8780 | // C99 6.5.16.1p2: The value of the right operand is converted to the | |||
8781 | // type of the assignment expression. | |||
8782 | // CheckAssignmentConstraints allows the left-hand side to be a reference, | |||
8783 | // so that we can use references in built-in functions even in C. | |||
8784 | // The getNonReferenceType() call makes sure that the resulting expression | |||
8785 | // does not have reference type. | |||
8786 | if (result != Incompatible && RHS.get()->getType() != LHSType) { | |||
8787 | QualType Ty = LHSType.getNonLValueExprType(Context); | |||
8788 | Expr *E = RHS.get(); | |||
8789 | ||||
8790 | // Check for various Objective-C errors. If we are not reporting | |||
8791 | // diagnostics and just checking for errors, e.g., during overload | |||
8792 | // resolution, return Incompatible to indicate the failure. | |||
8793 | if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() && | |||
8794 | CheckObjCConversion(SourceRange(), Ty, E, CCK_ImplicitConversion, | |||
8795 | Diagnose, DiagnoseCFAudited) != ACR_okay) { | |||
8796 | if (!Diagnose) | |||
8797 | return Incompatible; | |||
8798 | } | |||
8799 | if (getLangOpts().ObjC && | |||
8800 | (CheckObjCBridgeRelatedConversions(E->getBeginLoc(), LHSType, | |||
8801 | E->getType(), E, Diagnose) || | |||
8802 | ConversionToObjCStringLiteralCheck(LHSType, E, Diagnose))) { | |||
8803 | if (!Diagnose) | |||
8804 | return Incompatible; | |||
8805 | // Replace the expression with a corrected version and continue so we | |||
8806 | // can find further errors. | |||
8807 | RHS = E; | |||
8808 | return Compatible; | |||
8809 | } | |||
8810 | ||||
8811 | if (ConvertRHS) | |||
8812 | RHS = ImpCastExprToType(E, Ty, Kind); | |||
8813 | } | |||
8814 | ||||
8815 | return result; | |||
8816 | } | |||
8817 | ||||
8818 | namespace { | |||
8819 | /// The original operand to an operator, prior to the application of the usual | |||
8820 | /// arithmetic conversions and converting the arguments of a builtin operator | |||
8821 | /// candidate. | |||
8822 | struct OriginalOperand { | |||
8823 | explicit OriginalOperand(Expr *Op) : Orig(Op), Conversion(nullptr) { | |||
8824 | if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Op)) | |||
8825 | Op = MTE->getSubExpr(); | |||
8826 | if (auto *BTE = dyn_cast<CXXBindTemporaryExpr>(Op)) | |||
8827 | Op = BTE->getSubExpr(); | |||
8828 | if (auto *ICE = dyn_cast<ImplicitCastExpr>(Op)) { | |||
8829 | Orig = ICE->getSubExprAsWritten(); | |||
8830 | Conversion = ICE->getConversionFunction(); | |||
8831 | } | |||
8832 | } | |||
8833 | ||||
8834 | QualType getType() const { return Orig->getType(); } | |||
8835 | ||||
8836 | Expr *Orig; | |||
8837 | NamedDecl *Conversion; | |||
8838 | }; | |||
8839 | } | |||
8840 | ||||
8841 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &LHS, | |||
8842 | ExprResult &RHS) { | |||
8843 | OriginalOperand OrigLHS(LHS.get()), OrigRHS(RHS.get()); | |||
8844 | ||||
8845 | Diag(Loc, diag::err_typecheck_invalid_operands) | |||
8846 | << OrigLHS.getType() << OrigRHS.getType() | |||
8847 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | |||
8848 | ||||
8849 | // If a user-defined conversion was applied to either of the operands prior | |||
8850 | // to applying the built-in operator rules, tell the user about it. | |||
8851 | if (OrigLHS.Conversion) { | |||
8852 | Diag(OrigLHS.Conversion->getLocation(), | |||
8853 | diag::note_typecheck_invalid_operands_converted) | |||
8854 | << 0 << LHS.get()->getType(); | |||
8855 | } | |||
8856 | if (OrigRHS.Conversion) { | |||
8857 | Diag(OrigRHS.Conversion->getLocation(), | |||
8858 | diag::note_typecheck_invalid_operands_converted) | |||
8859 | << 1 << RHS.get()->getType(); | |||
8860 | } | |||
8861 | ||||
8862 | return QualType(); | |||
8863 | } | |||
8864 | ||||
8865 | // Diagnose cases where a scalar was implicitly converted to a vector and | |||
8866 | // diagnose the underlying types. Otherwise, diagnose the error | |||
8867 | // as invalid vector logical operands for non-C++ cases. | |||
8868 | QualType Sema::InvalidLogicalVectorOperands(SourceLocation Loc, ExprResult &LHS, | |||
8869 | ExprResult &RHS) { | |||
8870 | QualType LHSType = LHS.get()->IgnoreImpCasts()->getType(); | |||
8871 | QualType RHSType = RHS.get()->IgnoreImpCasts()->getType(); | |||
8872 | ||||
8873 | bool LHSNatVec = LHSType->isVectorType(); | |||
8874 | bool RHSNatVec = RHSType->isVectorType(); | |||
8875 | ||||
8876 | if (!(LHSNatVec && RHSNatVec)) { | |||
8877 | Expr *Vector = LHSNatVec ? LHS.get() : RHS.get(); | |||
8878 | Expr *NonVector = !LHSNatVec ? LHS.get() : RHS.get(); | |||
8879 | Diag(Loc, diag::err_typecheck_logical_vector_expr_gnu_cpp_restrict) | |||
8880 | << 0 << Vector->getType() << NonVector->IgnoreImpCasts()->getType() | |||
8881 | << Vector->getSourceRange(); | |||
8882 | return QualType(); | |||
8883 | } | |||
8884 | ||||
8885 | Diag(Loc, diag::err_typecheck_logical_vector_expr_gnu_cpp_restrict) | |||
8886 | << 1 << LHSType << RHSType << LHS.get()->getSourceRange() | |||
8887 | << RHS.get()->getSourceRange(); | |||
8888 | ||||
8889 | return QualType(); | |||
8890 | } | |||
8891 | ||||
8892 | /// Try to convert a value of non-vector type to a vector type by converting | |||
8893 | /// the type to the element type of the vector and then performing a splat. | |||
8894 | /// If the language is OpenCL, we only use conversions that promote scalar | |||
8895 | /// rank; for C, Obj-C, and C++ we allow any real scalar conversion except | |||
8896 | /// for float->int. | |||
8897 | /// | |||
8898 | /// OpenCL V2.0 6.2.6.p2: | |||
8899 | /// An error shall occur if any scalar operand type has greater rank | |||
8900 | /// than the type of the vector element. | |||
8901 | /// | |||
8902 | /// \param scalar - if non-null, actually perform the conversions | |||
8903 | /// \return true if the operation fails (but without diagnosing the failure) | |||
8904 | static bool tryVectorConvertAndSplat(Sema &S, ExprResult *scalar, | |||
8905 | QualType scalarTy, | |||
8906 | QualType vectorEltTy, | |||
8907 | QualType vectorTy, | |||
8908 | unsigned &DiagID) { | |||
8909 | // The conversion to apply to the scalar before splatting it, | |||
8910 | // if necessary. | |||
8911 | CastKind scalarCast = CK_NoOp; | |||
8912 | ||||
8913 | if (vectorEltTy->isIntegralType(S.Context)) { | |||
8914 | if (S.getLangOpts().OpenCL && (scalarTy->isRealFloatingType() || | |||
8915 | (scalarTy->isIntegerType() && | |||
8916 | S.Context.getIntegerTypeOrder(vectorEltTy, scalarTy) < 0))) { | |||
8917 | DiagID = diag::err_opencl_scalar_type_rank_greater_than_vector_type; | |||
8918 | return true; | |||
8919 | } | |||
8920 | if (!scalarTy->isIntegralType(S.Context)) | |||
8921 | return true; | |||
8922 | scalarCast = CK_IntegralCast; | |||
8923 | } else if (vectorEltTy->isRealFloatingType()) { | |||
8924 | if (scalarTy->isRealFloatingType()) { | |||
8925 | if (S.getLangOpts().OpenCL && | |||
8926 | S.Context.getFloatingTypeOrder(vectorEltTy, scalarTy) < 0) { | |||
8927 | DiagID = diag::err_opencl_scalar_type_rank_greater_than_vector_type; | |||
8928 | return true; | |||
8929 | } | |||
8930 | scalarCast = CK_FloatingCast; | |||
8931 | } | |||
8932 | else if (scalarTy->isIntegralType(S.Context)) | |||
8933 | scalarCast = CK_IntegralToFloating; | |||
8934 | else | |||
8935 | return true; | |||
8936 | } else { | |||
8937 | return true; | |||
8938 | } | |||
8939 | ||||
8940 | // Adjust scalar if desired. | |||
8941 | if (scalar) { | |||
8942 | if (scalarCast != CK_NoOp) | |||
8943 | *scalar = S.ImpCastExprToType(scalar->get(), vectorEltTy, scalarCast); | |||
8944 | *scalar = S.ImpCastExprToType(scalar->get(), vectorTy, CK_VectorSplat); | |||
8945 | } | |||
8946 | return false; | |||
8947 | } | |||
8948 | ||||
8949 | /// Convert vector E to a vector with the same number of elements but different | |||
8950 | /// element type. | |||
8951 | static ExprResult convertVector(Expr *E, QualType ElementType, Sema &S) { | |||
8952 | const auto *VecTy = E->getType()->getAs<VectorType>(); | |||
8953 | assert(VecTy && "Expression E must be a vector")((VecTy && "Expression E must be a vector") ? static_cast <void> (0) : __assert_fail ("VecTy && \"Expression E must be a vector\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 8953, __PRETTY_FUNCTION__)); | |||
8954 | QualType NewVecTy = S.Context.getVectorType(ElementType, | |||
8955 | VecTy->getNumElements(), | |||
8956 | VecTy->getVectorKind()); | |||
8957 | ||||
8958 | // Look through the implicit cast. Return the subexpression if its type is | |||
8959 | // NewVecTy. | |||
8960 | if (auto *ICE = dyn_cast<ImplicitCastExpr>(E)) | |||
8961 | if (ICE->getSubExpr()->getType() == NewVecTy) | |||
8962 | return ICE->getSubExpr(); | |||
8963 | ||||
8964 | auto Cast = ElementType->isIntegerType() ? CK_IntegralCast : CK_FloatingCast; | |||
8965 | return S.ImpCastExprToType(E, NewVecTy, Cast); | |||
8966 | } | |||
8967 | ||||
8968 | /// Test if a (constant) integer Int can be casted to another integer type | |||
8969 | /// IntTy without losing precision. | |||
8970 | static bool canConvertIntToOtherIntTy(Sema &S, ExprResult *Int, | |||
8971 | QualType OtherIntTy) { | |||
8972 | QualType IntTy = Int->get()->getType().getUnqualifiedType(); | |||
8973 | ||||
8974 | // Reject cases where the value of the Int is unknown as that would | |||
8975 | // possibly cause truncation, but accept cases where the scalar can be | |||
8976 | // demoted without loss of precision. | |||
8977 | Expr::EvalResult EVResult; | |||
8978 | bool CstInt = Int->get()->EvaluateAsInt(EVResult, S.Context); | |||
8979 | int Order = S.Context.getIntegerTypeOrder(OtherIntTy, IntTy); | |||
8980 | bool IntSigned = IntTy->hasSignedIntegerRepresentation(); | |||
8981 | bool OtherIntSigned = OtherIntTy->hasSignedIntegerRepresentation(); | |||
8982 | ||||
8983 | if (CstInt) { | |||
8984 | // If the scalar is constant and is of a higher order and has more active | |||
8985 | // bits that the vector element type, reject it. | |||
8986 | llvm::APSInt Result = EVResult.Val.getInt(); | |||
8987 | unsigned NumBits = IntSigned | |||
8988 | ? (Result.isNegative() ? Result.getMinSignedBits() | |||
8989 | : Result.getActiveBits()) | |||
8990 | : Result.getActiveBits(); | |||
8991 | if (Order < 0 && S.Context.getIntWidth(OtherIntTy) < NumBits) | |||
8992 | return true; | |||
8993 | ||||
8994 | // If the signedness of the scalar type and the vector element type | |||
8995 | // differs and the number of bits is greater than that of the vector | |||
8996 | // element reject it. | |||
8997 | return (IntSigned != OtherIntSigned && | |||
8998 | NumBits > S.Context.getIntWidth(OtherIntTy)); | |||
8999 | } | |||
9000 | ||||
9001 | // Reject cases where the value of the scalar is not constant and it's | |||
9002 | // order is greater than that of the vector element type. | |||
9003 | return (Order < 0); | |||
9004 | } | |||
9005 | ||||
9006 | /// Test if a (constant) integer Int can be casted to floating point type | |||
9007 | /// FloatTy without losing precision. | |||
9008 | static bool canConvertIntTyToFloatTy(Sema &S, ExprResult *Int, | |||
9009 | QualType FloatTy) { | |||
9010 | QualType IntTy = Int->get()->getType().getUnqualifiedType(); | |||
9011 | ||||
9012 | // Determine if the integer constant can be expressed as a floating point | |||
9013 | // number of the appropriate type. | |||
9014 | Expr::EvalResult EVResult; | |||
9015 | bool CstInt = Int->get()->EvaluateAsInt(EVResult, S.Context); | |||
9016 | ||||
9017 | uint64_t Bits = 0; | |||
9018 | if (CstInt) { | |||
9019 | // Reject constants that would be truncated if they were converted to | |||
9020 | // the floating point type. Test by simple to/from conversion. | |||
9021 | // FIXME: Ideally the conversion to an APFloat and from an APFloat | |||
9022 | // could be avoided if there was a convertFromAPInt method | |||
9023 | // which could signal back if implicit truncation occurred. | |||
9024 | llvm::APSInt Result = EVResult.Val.getInt(); | |||
9025 | llvm::APFloat Float(S.Context.getFloatTypeSemantics(FloatTy)); | |||
9026 | Float.convertFromAPInt(Result, IntTy->hasSignedIntegerRepresentation(), | |||
9027 | llvm::APFloat::rmTowardZero); | |||
9028 | llvm::APSInt ConvertBack(S.Context.getIntWidth(IntTy), | |||
9029 | !IntTy->hasSignedIntegerRepresentation()); | |||
9030 | bool Ignored = false; | |||
9031 | Float.convertToInteger(ConvertBack, llvm::APFloat::rmNearestTiesToEven, | |||
9032 | &Ignored); | |||
9033 | if (Result != ConvertBack) | |||
9034 | return true; | |||
9035 | } else { | |||
9036 | // Reject types that cannot be fully encoded into the mantissa of | |||
9037 | // the float. | |||
9038 | Bits = S.Context.getTypeSize(IntTy); | |||
9039 | unsigned FloatPrec = llvm::APFloat::semanticsPrecision( | |||
9040 | S.Context.getFloatTypeSemantics(FloatTy)); | |||
9041 | if (Bits > FloatPrec) | |||
9042 | return true; | |||
9043 | } | |||
9044 | ||||
9045 | return false; | |||
9046 | } | |||
9047 | ||||
9048 | /// Attempt to convert and splat Scalar into a vector whose types matches | |||
9049 | /// Vector following GCC conversion rules. The rule is that implicit | |||
9050 | /// conversion can occur when Scalar can be casted to match Vector's element | |||
9051 | /// type without causing truncation of Scalar. | |||
9052 | static bool tryGCCVectorConvertAndSplat(Sema &S, ExprResult *Scalar, | |||
9053 | ExprResult *Vector) { | |||
9054 | QualType ScalarTy = Scalar->get()->getType().getUnqualifiedType(); | |||
9055 | QualType VectorTy = Vector->get()->getType().getUnqualifiedType(); | |||
9056 | const VectorType *VT = VectorTy->getAs<VectorType>(); | |||
9057 | ||||
9058 | assert(!isa<ExtVectorType>(VT) &&((!isa<ExtVectorType>(VT) && "ExtVectorTypes should not be handled here!" ) ? static_cast<void> (0) : __assert_fail ("!isa<ExtVectorType>(VT) && \"ExtVectorTypes should not be handled here!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 9059, __PRETTY_FUNCTION__)) | |||
9059 | "ExtVectorTypes should not be handled here!")((!isa<ExtVectorType>(VT) && "ExtVectorTypes should not be handled here!" ) ? static_cast<void> (0) : __assert_fail ("!isa<ExtVectorType>(VT) && \"ExtVectorTypes should not be handled here!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 9059, __PRETTY_FUNCTION__)); | |||
9060 | ||||
9061 | QualType VectorEltTy = VT->getElementType(); | |||
9062 | ||||
9063 | // Reject cases where the vector element type or the scalar element type are | |||
9064 | // not integral or floating point types. | |||
9065 | if (!VectorEltTy->isArithmeticType() || !ScalarTy->isArithmeticType()) | |||
9066 | return true; | |||
9067 | ||||
9068 | // The conversion to apply to the scalar before splatting it, | |||
9069 | // if necessary. | |||
9070 | CastKind ScalarCast = CK_NoOp; | |||
9071 | ||||
9072 | // Accept cases where the vector elements are integers and the scalar is | |||
9073 | // an integer. | |||
9074 | // FIXME: Notionally if the scalar was a floating point value with a precise | |||
9075 | // integral representation, we could cast it to an appropriate integer | |||
9076 | // type and then perform the rest of the checks here. GCC will perform | |||
9077 | // this conversion in some cases as determined by the input language. | |||
9078 | // We should accept it on a language independent basis. | |||
9079 | if (VectorEltTy->isIntegralType(S.Context) && | |||
9080 | ScalarTy->isIntegralType(S.Context) && | |||
9081 | S.Context.getIntegerTypeOrder(VectorEltTy, ScalarTy)) { | |||
9082 | ||||
9083 | if (canConvertIntToOtherIntTy(S, Scalar, VectorEltTy)) | |||
9084 | return true; | |||
9085 | ||||
9086 | ScalarCast = CK_IntegralCast; | |||
9087 | } else if (VectorEltTy->isIntegralType(S.Context) && | |||
9088 | ScalarTy->isRealFloatingType()) { | |||
9089 | if (S.Context.getTypeSize(VectorEltTy) == S.Context.getTypeSize(ScalarTy)) | |||
9090 | ScalarCast = CK_FloatingToIntegral; | |||
9091 | else | |||
9092 | return true; | |||
9093 | } else if (VectorEltTy->isRealFloatingType()) { | |||
9094 | if (ScalarTy->isRealFloatingType()) { | |||
9095 | ||||
9096 | // Reject cases where the scalar type is not a constant and has a higher | |||
9097 | // Order than the vector element type. | |||
9098 | llvm::APFloat Result(0.0); | |||
9099 | bool CstScalar = Scalar->get()->EvaluateAsFloat(Result, S.Context); | |||
9100 | int Order = S.Context.getFloatingTypeOrder(VectorEltTy, ScalarTy); | |||
9101 | if (!CstScalar && Order < 0) | |||
9102 | return true; | |||
9103 | ||||
9104 | // If the scalar cannot be safely casted to the vector element type, | |||
9105 | // reject it. | |||
9106 | if (CstScalar) { | |||
9107 | bool Truncated = false; | |||
9108 | Result.convert(S.Context.getFloatTypeSemantics(VectorEltTy), | |||
9109 | llvm::APFloat::rmNearestTiesToEven, &Truncated); | |||
9110 | if (Truncated) | |||
9111 | return true; | |||
9112 | } | |||
9113 | ||||
9114 | ScalarCast = CK_FloatingCast; | |||
9115 | } else if (ScalarTy->isIntegralType(S.Context)) { | |||
9116 | if (canConvertIntTyToFloatTy(S, Scalar, VectorEltTy)) | |||
9117 | return true; | |||
9118 | ||||
9119 | ScalarCast = CK_IntegralToFloating; | |||
9120 | } else | |||
9121 | return true; | |||
9122 | } | |||
9123 | ||||
9124 | // Adjust scalar if desired. | |||
9125 | if (Scalar) { | |||
9126 | if (ScalarCast != CK_NoOp) | |||
9127 | *Scalar = S.ImpCastExprToType(Scalar->get(), VectorEltTy, ScalarCast); | |||
9128 | *Scalar = S.ImpCastExprToType(Scalar->get(), VectorTy, CK_VectorSplat); | |||
9129 | } | |||
9130 | return false; | |||
9131 | } | |||
9132 | ||||
9133 | QualType Sema::CheckVectorOperands(ExprResult &LHS, ExprResult &RHS, | |||
9134 | SourceLocation Loc, bool IsCompAssign, | |||
9135 | bool AllowBothBool, | |||
9136 | bool AllowBoolConversions) { | |||
9137 | if (!IsCompAssign) { | |||
9138 | LHS = DefaultFunctionArrayLvalueConversion(LHS.get()); | |||
9139 | if (LHS.isInvalid()) | |||
9140 | return QualType(); | |||
9141 | } | |||
9142 | RHS = DefaultFunctionArrayLvalueConversion(RHS.get()); | |||
9143 | if (RHS.isInvalid()) | |||
9144 | return QualType(); | |||
9145 | ||||
9146 | // For conversion purposes, we ignore any qualifiers. | |||
9147 | // For example, "const float" and "float" are equivalent. | |||
9148 | QualType LHSType = LHS.get()->getType().getUnqualifiedType(); | |||
9149 | QualType RHSType = RHS.get()->getType().getUnqualifiedType(); | |||
9150 | ||||
9151 | const VectorType *LHSVecType = LHSType->getAs<VectorType>(); | |||
9152 | const VectorType *RHSVecType = RHSType->getAs<VectorType>(); | |||
9153 | assert(LHSVecType || RHSVecType)((LHSVecType || RHSVecType) ? static_cast<void> (0) : __assert_fail ("LHSVecType || RHSVecType", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 9153, __PRETTY_FUNCTION__)); | |||
9154 | ||||
9155 | // AltiVec-style "vector bool op vector bool" combinations are allowed | |||
9156 | // for some operators but not others. | |||
9157 | if (!AllowBothBool && | |||
9158 | LHSVecType && LHSVecType->getVectorKind() == VectorType::AltiVecBool && | |||
9159 | RHSVecType && RHSVecType->getVectorKind() == VectorType::AltiVecBool) | |||
9160 | return InvalidOperands(Loc, LHS, RHS); | |||
9161 | ||||
9162 | // If the vector types are identical, return. | |||
9163 | if (Context.hasSameType(LHSType, RHSType)) | |||
9164 | return LHSType; | |||
9165 | ||||
9166 | // If we have compatible AltiVec and GCC vector types, use the AltiVec type. | |||
9167 | if (LHSVecType && RHSVecType && | |||
9168 | Context.areCompatibleVectorTypes(LHSType, RHSType)) { | |||
9169 | if (isa<ExtVectorType>(LHSVecType)) { | |||
9170 | RHS = ImpCastExprToType(RHS.get(), LHSType, CK_BitCast); | |||
9171 | return LHSType; | |||
9172 | } | |||
9173 | ||||
9174 | if (!IsCompAssign) | |||
9175 | LHS = ImpCastExprToType(LHS.get(), RHSType, CK_BitCast); | |||
9176 | return RHSType; | |||
9177 | } | |||
9178 | ||||
9179 | // AllowBoolConversions says that bool and non-bool AltiVec vectors | |||
9180 | // can be mixed, with the result being the non-bool type. The non-bool | |||
9181 | // operand must have integer element type. | |||
9182 | if (AllowBoolConversions && LHSVecType && RHSVecType && | |||
9183 | LHSVecType->getNumElements() == RHSVecType->getNumElements() && | |||
9184 | (Context.getTypeSize(LHSVecType->getElementType()) == | |||
9185 | Context.getTypeSize(RHSVecType->getElementType()))) { | |||
9186 | if (LHSVecType->getVectorKind() == VectorType::AltiVecVector && | |||
9187 | LHSVecType->getElementType()->isIntegerType() && | |||
9188 | RHSVecType->getVectorKind() == VectorType::AltiVecBool) { | |||
9189 | RHS = ImpCastExprToType(RHS.get(), LHSType, CK_BitCast); | |||
9190 | return LHSType; | |||
9191 | } | |||
9192 | if (!IsCompAssign && | |||
9193 | LHSVecType->getVectorKind() == VectorType::AltiVecBool && | |||
9194 | RHSVecType->getVectorKind() == VectorType::AltiVecVector && | |||
9195 | RHSVecType->getElementType()->isIntegerType()) { | |||
9196 | LHS = ImpCastExprToType(LHS.get(), RHSType, CK_BitCast); | |||
9197 | return RHSType; | |||
9198 | } | |||
9199 | } | |||
9200 | ||||
9201 | // If there's a vector type and a scalar, try to convert the scalar to | |||
9202 | // the vector element type and splat. | |||
9203 | unsigned DiagID = diag::err_typecheck_vector_not_convertable; | |||
9204 | if (!RHSVecType) { | |||
9205 | if (isa<ExtVectorType>(LHSVecType)) { | |||
9206 | if (!tryVectorConvertAndSplat(*this, &RHS, RHSType, | |||
9207 | LHSVecType->getElementType(), LHSType, | |||
9208 | DiagID)) | |||
9209 | return LHSType; | |||
9210 | } else { | |||
9211 | if (!tryGCCVectorConvertAndSplat(*this, &RHS, &LHS)) | |||
9212 | return LHSType; | |||
9213 | } | |||
9214 | } | |||
9215 | if (!LHSVecType) { | |||
9216 | if (isa<ExtVectorType>(RHSVecType)) { | |||
9217 | if (!tryVectorConvertAndSplat(*this, (IsCompAssign ? nullptr : &LHS), | |||
9218 | LHSType, RHSVecType->getElementType(), | |||
9219 | RHSType, DiagID)) | |||
9220 | return RHSType; | |||
9221 | } else { | |||
9222 | if (LHS.get()->getValueKind() == VK_LValue || | |||
9223 | !tryGCCVectorConvertAndSplat(*this, &LHS, &RHS)) | |||
9224 | return RHSType; | |||
9225 | } | |||
9226 | } | |||
9227 | ||||
9228 | // FIXME: The code below also handles conversion between vectors and | |||
9229 | // non-scalars, we should break this down into fine grained specific checks | |||
9230 | // and emit proper diagnostics. | |||
9231 | QualType VecType = LHSVecType ? LHSType : RHSType; | |||
9232 | const VectorType *VT = LHSVecType ? LHSVecType : RHSVecType; | |||
9233 | QualType OtherType = LHSVecType ? RHSType : LHSType; | |||
9234 | ExprResult *OtherExpr = LHSVecType ? &RHS : &LHS; | |||
9235 | if (isLaxVectorConversion(OtherType, VecType)) { | |||
9236 | // If we're allowing lax vector conversions, only the total (data) size | |||
9237 | // needs to be the same. For non compound assignment, if one of the types is | |||
9238 | // scalar, the result is always the vector type. | |||
9239 | if (!IsCompAssign) { | |||
9240 | *OtherExpr = ImpCastExprToType(OtherExpr->get(), VecType, CK_BitCast); | |||
9241 | return VecType; | |||
9242 | // In a compound assignment, lhs += rhs, 'lhs' is a lvalue src, forbidding | |||
9243 | // any implicit cast. Here, the 'rhs' should be implicit casted to 'lhs' | |||
9244 | // type. Note that this is already done by non-compound assignments in | |||
9245 | // CheckAssignmentConstraints. If it's a scalar type, only bitcast for | |||
9246 | // <1 x T> -> T. The result is also a vector type. | |||
9247 | } else if (OtherType->isExtVectorType() || OtherType->isVectorType() || | |||
9248 | (OtherType->isScalarType() && VT->getNumElements() == 1)) { | |||
9249 | ExprResult *RHSExpr = &RHS; | |||
9250 | *RHSExpr = ImpCastExprToType(RHSExpr->get(), LHSType, CK_BitCast); | |||
9251 | return VecType; | |||
9252 | } | |||
9253 | } | |||
9254 | ||||
9255 | // Okay, the expression is invalid. | |||
9256 | ||||
9257 | // If there's a non-vector, non-real operand, diagnose that. | |||
9258 | if ((!RHSVecType && !RHSType->isRealType()) || | |||
9259 | (!LHSVecType && !LHSType->isRealType())) { | |||
9260 | Diag(Loc, diag::err_typecheck_vector_not_convertable_non_scalar) | |||
9261 | << LHSType << RHSType | |||
9262 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | |||
9263 | return QualType(); | |||
9264 | } | |||
9265 | ||||
9266 | // OpenCL V1.1 6.2.6.p1: | |||
9267 | // If the operands are of more than one vector type, then an error shall | |||
9268 | // occur. Implicit conversions between vector types are not permitted, per | |||
9269 | // section 6.2.1. | |||
9270 | if (getLangOpts().OpenCL && | |||
9271 | RHSVecType && isa<ExtVectorType>(RHSVecType) && | |||
9272 | LHSVecType && isa<ExtVectorType>(LHSVecType)) { | |||
9273 | Diag(Loc, diag::err_opencl_implicit_vector_conversion) << LHSType | |||
9274 | << RHSType; | |||
9275 | return QualType(); | |||
9276 | } | |||
9277 | ||||
9278 | ||||
9279 | // If there is a vector type that is not a ExtVector and a scalar, we reach | |||
9280 | // this point if scalar could not be converted to the vector's element type | |||
9281 | // without truncation. | |||
9282 | if ((RHSVecType && !isa<ExtVectorType>(RHSVecType)) || | |||
9283 | (LHSVecType && !isa<ExtVectorType>(LHSVecType))) { | |||
9284 | QualType Scalar = LHSVecType ? RHSType : LHSType; | |||
9285 | QualType Vector = LHSVecType ? LHSType : RHSType; | |||
9286 | unsigned ScalarOrVector = LHSVecType && RHSVecType ? 1 : 0; | |||
9287 | Diag(Loc, | |||
9288 | diag::err_typecheck_vector_not_convertable_implict_truncation) | |||
9289 | << ScalarOrVector << Scalar << Vector; | |||
9290 | ||||
9291 | return QualType(); | |||
9292 | } | |||
9293 | ||||
9294 | // Otherwise, use the generic diagnostic. | |||
9295 | Diag(Loc, DiagID) | |||
9296 | << LHSType << RHSType | |||
9297 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | |||
9298 | return QualType(); | |||
9299 | } | |||
9300 | ||||
9301 | // checkArithmeticNull - Detect when a NULL constant is used improperly in an | |||
9302 | // expression. These are mainly cases where the null pointer is used as an | |||
9303 | // integer instead of a pointer. | |||
9304 | static void checkArithmeticNull(Sema &S, ExprResult &LHS, ExprResult &RHS, | |||
9305 | SourceLocation Loc, bool IsCompare) { | |||
9306 | // The canonical way to check for a GNU null is with isNullPointerConstant, | |||
9307 | // but we use a bit of a hack here for speed; this is a relatively | |||
9308 | // hot path, and isNullPointerConstant is slow. | |||
9309 | bool LHSNull = isa<GNUNullExpr>(LHS.get()->IgnoreParenImpCasts()); | |||
9310 | bool RHSNull = isa<GNUNullExpr>(RHS.get()->IgnoreParenImpCasts()); | |||
9311 | ||||
9312 | QualType NonNullType = LHSNull ? RHS.get()->getType() : LHS.get()->getType(); | |||
9313 | ||||
9314 | // Avoid analyzing cases where the result will either be invalid (and | |||
9315 | // diagnosed as such) or entirely valid and not something to warn about. | |||
9316 | if ((!LHSNull && !RHSNull) || NonNullType->isBlockPointerType() || | |||
9317 | NonNullType->isMemberPointerType() || NonNullType->isFunctionType()) | |||
9318 | return; | |||
9319 | ||||
9320 | // Comparison operations would not make sense with a null pointer no matter | |||
9321 | // what the other expression is. | |||
9322 | if (!IsCompare) { | |||
9323 | S.Diag(Loc, diag::warn_null_in_arithmetic_operation) | |||
9324 | << (LHSNull ? LHS.get()->getSourceRange() : SourceRange()) | |||
9325 | << (RHSNull ? RHS.get()->getSourceRange() : SourceRange()); | |||
9326 | return; | |||
9327 | } | |||
9328 | ||||
9329 | // The rest of the operations only make sense with a null pointer | |||
9330 | // if the other expression is a pointer. | |||
9331 | if (LHSNull == RHSNull || NonNullType->isAnyPointerType() || | |||
9332 | NonNullType->canDecayToPointerType()) | |||
9333 | return; | |||
9334 | ||||
9335 | S.Diag(Loc, diag::warn_null_in_comparison_operation) | |||
9336 | << LHSNull /* LHS is NULL */ << NonNullType | |||
9337 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | |||
9338 | } | |||
9339 | ||||
9340 | static void DiagnoseDivisionSizeofPointerOrArray(Sema &S, Expr *LHS, Expr *RHS, | |||
9341 | SourceLocation Loc) { | |||
9342 | const auto *LUE = dyn_cast<UnaryExprOrTypeTraitExpr>(LHS); | |||
9343 | const auto *RUE = dyn_cast<UnaryExprOrTypeTraitExpr>(RHS); | |||
9344 | if (!LUE || !RUE) | |||
9345 | return; | |||
9346 | if (LUE->getKind() != UETT_SizeOf || LUE->isArgumentType() || | |||
9347 | RUE->getKind() != UETT_SizeOf) | |||
9348 | return; | |||
9349 | ||||
9350 | const Expr *LHSArg = LUE->getArgumentExpr()->IgnoreParens(); | |||
9351 | QualType LHSTy = LHSArg->getType(); | |||
9352 | QualType RHSTy; | |||
9353 | ||||
9354 | if (RUE->isArgumentType()) | |||
9355 | RHSTy = RUE->getArgumentType(); | |||
9356 | else | |||
9357 | RHSTy = RUE->getArgumentExpr()->IgnoreParens()->getType(); | |||
9358 | ||||
9359 | if (LHSTy->isPointerType() && !RHSTy->isPointerType()) { | |||
9360 | if (!S.Context.hasSameUnqualifiedType(LHSTy->getPointeeType(), RHSTy)) | |||
9361 | return; | |||
9362 | ||||
9363 | S.Diag(Loc, diag::warn_division_sizeof_ptr) << LHS << LHS->getSourceRange(); | |||
9364 | if (const auto *DRE = dyn_cast<DeclRefExpr>(LHSArg)) { | |||
9365 | if (const ValueDecl *LHSArgDecl = DRE->getDecl()) | |||
9366 | S.Diag(LHSArgDecl->getLocation(), diag::note_pointer_declared_here) | |||
9367 | << LHSArgDecl; | |||
9368 | } | |||
9369 | } else if (const auto *ArrayTy = S.Context.getAsArrayType(LHSTy)) { | |||
9370 | QualType ArrayElemTy = ArrayTy->getElementType(); | |||
9371 | if (ArrayElemTy != S.Context.getBaseElementType(ArrayTy) || | |||
9372 | ArrayElemTy->isDependentType() || RHSTy->isDependentType() || | |||
9373 | ArrayElemTy->isCharType() || | |||
9374 | S.Context.getTypeSize(ArrayElemTy) == S.Context.getTypeSize(RHSTy)) | |||
9375 | return; | |||
9376 | S.Diag(Loc, diag::warn_division_sizeof_array) | |||
9377 | << LHSArg->getSourceRange() << ArrayElemTy << RHSTy; | |||
9378 | if (const auto *DRE = dyn_cast<DeclRefExpr>(LHSArg)) { | |||
9379 | if (const ValueDecl *LHSArgDecl = DRE->getDecl()) | |||
9380 | S.Diag(LHSArgDecl->getLocation(), diag::note_array_declared_here) | |||
9381 | << LHSArgDecl; | |||
9382 | } | |||
9383 | ||||
9384 | S.Diag(Loc, diag::note_precedence_silence) << RHS; | |||
9385 | } | |||
9386 | } | |||
9387 | ||||
9388 | static void DiagnoseBadDivideOrRemainderValues(Sema& S, ExprResult &LHS, | |||
9389 | ExprResult &RHS, | |||
9390 | SourceLocation Loc, bool IsDiv) { | |||
9391 | // Check for division/remainder by zero. | |||
9392 | Expr::EvalResult RHSValue; | |||
9393 | if (!RHS.get()->isValueDependent() && | |||
9394 | RHS.get()->EvaluateAsInt(RHSValue, S.Context) && | |||
9395 | RHSValue.Val.getInt() == 0) | |||
9396 | S.DiagRuntimeBehavior(Loc, RHS.get(), | |||
9397 | S.PDiag(diag::warn_remainder_division_by_zero) | |||
9398 | << IsDiv << RHS.get()->getSourceRange()); | |||
9399 | } | |||
9400 | ||||
9401 | QualType Sema::CheckMultiplyDivideOperands(ExprResult &LHS, ExprResult &RHS, | |||
9402 | SourceLocation Loc, | |||
9403 | bool IsCompAssign, bool IsDiv) { | |||
9404 | checkArithmeticNull(*this, LHS, RHS, Loc, /*IsCompare=*/false); | |||
9405 | ||||
9406 | if (LHS.get()->getType()->isVectorType() || | |||
9407 | RHS.get()->getType()->isVectorType()) | |||
9408 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign, | |||
9409 | /*AllowBothBool*/getLangOpts().AltiVec, | |||
9410 | /*AllowBoolConversions*/false); | |||
9411 | ||||
9412 | QualType compType = UsualArithmeticConversions( | |||
9413 | LHS, RHS, Loc, IsCompAssign ? ACK_CompAssign : ACK_Arithmetic); | |||
9414 | if (LHS.isInvalid() || RHS.isInvalid()) | |||
9415 | return QualType(); | |||
9416 | ||||
9417 | ||||
9418 | if (compType.isNull() || !compType->isArithmeticType()) | |||
9419 | return InvalidOperands(Loc, LHS, RHS); | |||
9420 | if (IsDiv) { | |||
9421 | DiagnoseBadDivideOrRemainderValues(*this, LHS, RHS, Loc, IsDiv); | |||
9422 | DiagnoseDivisionSizeofPointerOrArray(*this, LHS.get(), RHS.get(), Loc); | |||
9423 | } | |||
9424 | return compType; | |||
9425 | } | |||
9426 | ||||
9427 | QualType Sema::CheckRemainderOperands( | |||
9428 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { | |||
9429 | checkArithmeticNull(*this, LHS, RHS, Loc, /*IsCompare=*/false); | |||
9430 | ||||
9431 | if (LHS.get()->getType()->isVectorType() || | |||
9432 | RHS.get()->getType()->isVectorType()) { | |||
9433 | if (LHS.get()->getType()->hasIntegerRepresentation() && | |||
9434 | RHS.get()->getType()->hasIntegerRepresentation()) | |||
9435 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign, | |||
9436 | /*AllowBothBool*/getLangOpts().AltiVec, | |||
9437 | /*AllowBoolConversions*/false); | |||
9438 | return InvalidOperands(Loc, LHS, RHS); | |||
9439 | } | |||
9440 | ||||
9441 | QualType compType = UsualArithmeticConversions( | |||
9442 | LHS, RHS, Loc, IsCompAssign ? ACK_CompAssign : ACK_Arithmetic); | |||
9443 | if (LHS.isInvalid() || RHS.isInvalid()) | |||
9444 | return QualType(); | |||
9445 | ||||
9446 | if (compType.isNull() || !compType->isIntegerType()) | |||
9447 | return InvalidOperands(Loc, LHS, RHS); | |||
9448 | DiagnoseBadDivideOrRemainderValues(*this, LHS, RHS, Loc, false /* IsDiv */); | |||
9449 | return compType; | |||
9450 | } | |||
9451 | ||||
9452 | /// Diagnose invalid arithmetic on two void pointers. | |||
9453 | static void diagnoseArithmeticOnTwoVoidPointers(Sema &S, SourceLocation Loc, | |||
9454 | Expr *LHSExpr, Expr *RHSExpr) { | |||
9455 | S.Diag(Loc, S.getLangOpts().CPlusPlus | |||
9456 | ? diag::err_typecheck_pointer_arith_void_type | |||
9457 | : diag::ext_gnu_void_ptr) | |||
9458 | << 1 /* two pointers */ << LHSExpr->getSourceRange() | |||
9459 | << RHSExpr->getSourceRange(); | |||
9460 | } | |||
9461 | ||||
9462 | /// Diagnose invalid arithmetic on a void pointer. | |||
9463 | static void diagnoseArithmeticOnVoidPointer(Sema &S, SourceLocation Loc, | |||
9464 | Expr *Pointer) { | |||
9465 | S.Diag(Loc, S.getLangOpts().CPlusPlus | |||
9466 | ? diag::err_typecheck_pointer_arith_void_type | |||
9467 | : diag::ext_gnu_void_ptr) | |||
9468 | << 0 /* one pointer */ << Pointer->getSourceRange(); | |||
9469 | } | |||
9470 | ||||
9471 | /// Diagnose invalid arithmetic on a null pointer. | |||
9472 | /// | |||
9473 | /// If \p IsGNUIdiom is true, the operation is using the 'p = (i8*)nullptr + n' | |||
9474 | /// idiom, which we recognize as a GNU extension. | |||
9475 | /// | |||
9476 | static void diagnoseArithmeticOnNullPointer(Sema &S, SourceLocation Loc, | |||
9477 | Expr *Pointer, bool IsGNUIdiom) { | |||
9478 | if (IsGNUIdiom) | |||
9479 | S.Diag(Loc, diag::warn_gnu_null_ptr_arith) | |||
9480 | << Pointer->getSourceRange(); | |||
9481 | else | |||
9482 | S.Diag(Loc, diag::warn_pointer_arith_null_ptr) | |||
9483 | << S.getLangOpts().CPlusPlus << Pointer->getSourceRange(); | |||
9484 | } | |||
9485 | ||||
9486 | /// Diagnose invalid arithmetic on two function pointers. | |||
9487 | static void diagnoseArithmeticOnTwoFunctionPointers(Sema &S, SourceLocation Loc, | |||
9488 | Expr *LHS, Expr *RHS) { | |||
9489 | assert(LHS->getType()->isAnyPointerType())((LHS->getType()->isAnyPointerType()) ? static_cast< void> (0) : __assert_fail ("LHS->getType()->isAnyPointerType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 9489, __PRETTY_FUNCTION__)); | |||
9490 | assert(RHS->getType()->isAnyPointerType())((RHS->getType()->isAnyPointerType()) ? static_cast< void> (0) : __assert_fail ("RHS->getType()->isAnyPointerType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 9490, __PRETTY_FUNCTION__)); | |||
9491 | S.Diag(Loc, S.getLangOpts().CPlusPlus | |||
9492 | ? diag::err_typecheck_pointer_arith_function_type | |||
9493 | : diag::ext_gnu_ptr_func_arith) | |||
9494 | << 1 /* two pointers */ << LHS->getType()->getPointeeType() | |||
9495 | // We only show the second type if it differs from the first. | |||
9496 | << (unsigned)!S.Context.hasSameUnqualifiedType(LHS->getType(), | |||
9497 | RHS->getType()) | |||
9498 | << RHS->getType()->getPointeeType() | |||
9499 | << LHS->getSourceRange() << RHS->getSourceRange(); | |||
9500 | } | |||
9501 | ||||
9502 | /// Diagnose invalid arithmetic on a function pointer. | |||
9503 | static void diagnoseArithmeticOnFunctionPointer(Sema &S, SourceLocation Loc, | |||
9504 | Expr *Pointer) { | |||
9505 | assert(Pointer->getType()->isAnyPointerType())((Pointer->getType()->isAnyPointerType()) ? static_cast <void> (0) : __assert_fail ("Pointer->getType()->isAnyPointerType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 9505, __PRETTY_FUNCTION__)); | |||
9506 | S.Diag(Loc, S.getLangOpts().CPlusPlus | |||
9507 | ? diag::err_typecheck_pointer_arith_function_type | |||
9508 | : diag::ext_gnu_ptr_func_arith) | |||
9509 | << 0 /* one pointer */ << Pointer->getType()->getPointeeType() | |||
9510 | << 0 /* one pointer, so only one type */ | |||
9511 | << Pointer->getSourceRange(); | |||
9512 | } | |||
9513 | ||||
9514 | /// Emit error if Operand is incomplete pointer type | |||
9515 | /// | |||
9516 | /// \returns True if pointer has incomplete type | |||
9517 | static bool checkArithmeticIncompletePointerType(Sema &S, SourceLocation Loc, | |||
9518 | Expr *Operand) { | |||
9519 | QualType ResType = Operand->getType(); | |||
9520 | if (const AtomicType *ResAtomicType = ResType->getAs<AtomicType>()) | |||
9521 | ResType = ResAtomicType->getValueType(); | |||
9522 | ||||
9523 | assert(ResType->isAnyPointerType() && !ResType->isDependentType())((ResType->isAnyPointerType() && !ResType->isDependentType ()) ? static_cast<void> (0) : __assert_fail ("ResType->isAnyPointerType() && !ResType->isDependentType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 9523, __PRETTY_FUNCTION__)); | |||
9524 | QualType PointeeTy = ResType->getPointeeType(); | |||
9525 | return S.RequireCompleteType(Loc, PointeeTy, | |||
9526 | diag::err_typecheck_arithmetic_incomplete_type, | |||
9527 | PointeeTy, Operand->getSourceRange()); | |||
9528 | } | |||
9529 | ||||
9530 | /// Check the validity of an arithmetic pointer operand. | |||
9531 | /// | |||
9532 | /// If the operand has pointer type, this code will check for pointer types | |||
9533 | /// which are invalid in arithmetic operations. These will be diagnosed | |||
9534 | /// appropriately, including whether or not the use is supported as an | |||
9535 | /// extension. | |||
9536 | /// | |||
9537 | /// \returns True when the operand is valid to use (even if as an extension). | |||
9538 | static bool checkArithmeticOpPointerOperand(Sema &S, SourceLocation Loc, | |||
9539 | Expr *Operand) { | |||
9540 | QualType ResType = Operand->getType(); | |||
9541 | if (const AtomicType *ResAtomicType = ResType->getAs<AtomicType>()) | |||
9542 | ResType = ResAtomicType->getValueType(); | |||
9543 | ||||
9544 | if (!ResType->isAnyPointerType()) return true; | |||
9545 | ||||
9546 | QualType PointeeTy = ResType->getPointeeType(); | |||
9547 | if (PointeeTy->isVoidType()) { | |||
9548 | diagnoseArithmeticOnVoidPointer(S, Loc, Operand); | |||
9549 | return !S.getLangOpts().CPlusPlus; | |||
9550 | } | |||
9551 | if (PointeeTy->isFunctionType()) { | |||
9552 | diagnoseArithmeticOnFunctionPointer(S, Loc, Operand); | |||
9553 | return !S.getLangOpts().CPlusPlus; | |||
9554 | } | |||
9555 | ||||
9556 | if (checkArithmeticIncompletePointerType(S, Loc, Operand)) return false; | |||
9557 | ||||
9558 | return true; | |||
9559 | } | |||
9560 | ||||
9561 | /// Check the validity of a binary arithmetic operation w.r.t. pointer | |||
9562 | /// operands. | |||
9563 | /// | |||
9564 | /// This routine will diagnose any invalid arithmetic on pointer operands much | |||
9565 | /// like \see checkArithmeticOpPointerOperand. However, it has special logic | |||
9566 | /// for emitting a single diagnostic even for operations where both LHS and RHS | |||
9567 | /// are (potentially problematic) pointers. | |||
9568 | /// | |||
9569 | /// \returns True when the operand is valid to use (even if as an extension). | |||
9570 | static bool checkArithmeticBinOpPointerOperands(Sema &S, SourceLocation Loc, | |||
9571 | Expr *LHSExpr, Expr *RHSExpr) { | |||
9572 | bool isLHSPointer = LHSExpr->getType()->isAnyPointerType(); | |||
9573 | bool isRHSPointer = RHSExpr->getType()->isAnyPointerType(); | |||
9574 | if (!isLHSPointer && !isRHSPointer) return true; | |||
9575 | ||||
9576 | QualType LHSPointeeTy, RHSPointeeTy; | |||
9577 | if (isLHSPointer) LHSPointeeTy = LHSExpr->getType()->getPointeeType(); | |||
9578 | if (isRHSPointer) RHSPointeeTy = RHSExpr->getType()->getPointeeType(); | |||
9579 | ||||
9580 | // if both are pointers check if operation is valid wrt address spaces | |||
9581 | if (S.getLangOpts().OpenCL && isLHSPointer && isRHSPointer) { | |||
9582 | const PointerType *lhsPtr = LHSExpr->getType()->castAs<PointerType>(); | |||
9583 | const PointerType *rhsPtr = RHSExpr->getType()->castAs<PointerType>(); | |||
9584 | if (!lhsPtr->isAddressSpaceOverlapping(*rhsPtr)) { | |||
9585 | S.Diag(Loc, | |||
9586 | diag::err_typecheck_op_on_nonoverlapping_address_space_pointers) | |||
9587 | << LHSExpr->getType() << RHSExpr->getType() << 1 /*arithmetic op*/ | |||
9588 | << LHSExpr->getSourceRange() << RHSExpr->getSourceRange(); | |||
9589 | return false; | |||
9590 | } | |||
9591 | } | |||
9592 | ||||
9593 | // Check for arithmetic on pointers to incomplete types. | |||
9594 | bool isLHSVoidPtr = isLHSPointer && LHSPointeeTy->isVoidType(); | |||
9595 | bool isRHSVoidPtr = isRHSPointer && RHSPointeeTy->isVoidType(); | |||
9596 | if (isLHSVoidPtr || isRHSVoidPtr) { | |||
9597 | if (!isRHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, LHSExpr); | |||
9598 | else if (!isLHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, RHSExpr); | |||
9599 | else diagnoseArithmeticOnTwoVoidPointers(S, Loc, LHSExpr, RHSExpr); | |||
9600 | ||||
9601 | return !S.getLangOpts().CPlusPlus; | |||
9602 | } | |||
9603 | ||||
9604 | bool isLHSFuncPtr = isLHSPointer && LHSPointeeTy->isFunctionType(); | |||
9605 | bool isRHSFuncPtr = isRHSPointer && RHSPointeeTy->isFunctionType(); | |||
9606 | if (isLHSFuncPtr || isRHSFuncPtr) { | |||
9607 | if (!isRHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, LHSExpr); | |||
9608 | else if (!isLHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, | |||
9609 | RHSExpr); | |||
9610 | else diagnoseArithmeticOnTwoFunctionPointers(S, Loc, LHSExpr, RHSExpr); | |||
9611 | ||||
9612 | return !S.getLangOpts().CPlusPlus; | |||
9613 | } | |||
9614 | ||||
9615 | if (isLHSPointer && checkArithmeticIncompletePointerType(S, Loc, LHSExpr)) | |||
9616 | return false; | |||
9617 | if (isRHSPointer && checkArithmeticIncompletePointerType(S, Loc, RHSExpr)) | |||
9618 | return false; | |||
9619 | ||||
9620 | return true; | |||
9621 | } | |||
9622 | ||||
9623 | /// diagnoseStringPlusInt - Emit a warning when adding an integer to a string | |||
9624 | /// literal. | |||
9625 | static void diagnoseStringPlusInt(Sema &Self, SourceLocation OpLoc, | |||
9626 | Expr *LHSExpr, Expr *RHSExpr) { | |||
9627 | StringLiteral* StrExpr = dyn_cast<StringLiteral>(LHSExpr->IgnoreImpCasts()); | |||
9628 | Expr* IndexExpr = RHSExpr; | |||
9629 | if (!StrExpr) { | |||
9630 | StrExpr = dyn_cast<StringLiteral>(RHSExpr->IgnoreImpCasts()); | |||
9631 | IndexExpr = LHSExpr; | |||
9632 | } | |||
9633 | ||||
9634 | bool IsStringPlusInt = StrExpr && | |||
9635 | IndexExpr->getType()->isIntegralOrUnscopedEnumerationType(); | |||
9636 | if (!IsStringPlusInt || IndexExpr->isValueDependent()) | |||
9637 | return; | |||
9638 | ||||
9639 | SourceRange DiagRange(LHSExpr->getBeginLoc(), RHSExpr->getEndLoc()); | |||
9640 | Self.Diag(OpLoc, diag::warn_string_plus_int) | |||
9641 | << DiagRange << IndexExpr->IgnoreImpCasts()->getType(); | |||
9642 | ||||
9643 | // Only print a fixit for "str" + int, not for int + "str". | |||
9644 | if (IndexExpr == RHSExpr) { | |||
9645 | SourceLocation EndLoc = Self.getLocForEndOfToken(RHSExpr->getEndLoc()); | |||
9646 | Self.Diag(OpLoc, diag::note_string_plus_scalar_silence) | |||
9647 | << FixItHint::CreateInsertion(LHSExpr->getBeginLoc(), "&") | |||
9648 | << FixItHint::CreateReplacement(SourceRange(OpLoc), "[") | |||
9649 | << FixItHint::CreateInsertion(EndLoc, "]"); | |||
9650 | } else | |||
9651 | Self.Diag(OpLoc, diag::note_string_plus_scalar_silence); | |||
9652 | } | |||
9653 | ||||
9654 | /// Emit a warning when adding a char literal to a string. | |||
9655 | static void diagnoseStringPlusChar(Sema &Self, SourceLocation OpLoc, | |||
9656 | Expr *LHSExpr, Expr *RHSExpr) { | |||
9657 | const Expr *StringRefExpr = LHSExpr; | |||
9658 | const CharacterLiteral *CharExpr = | |||
9659 | dyn_cast<CharacterLiteral>(RHSExpr->IgnoreImpCasts()); | |||
9660 | ||||
9661 | if (!CharExpr) { | |||
9662 | CharExpr = dyn_cast<CharacterLiteral>(LHSExpr->IgnoreImpCasts()); | |||
9663 | StringRefExpr = RHSExpr; | |||
9664 | } | |||
9665 | ||||
9666 | if (!CharExpr || !StringRefExpr) | |||
9667 | return; | |||
9668 | ||||
9669 | const QualType StringType = StringRefExpr->getType(); | |||
9670 | ||||
9671 | // Return if not a PointerType. | |||
9672 | if (!StringType->isAnyPointerType()) | |||
9673 | return; | |||
9674 | ||||
9675 | // Return if not a CharacterType. | |||
9676 | if (!StringType->getPointeeType()->isAnyCharacterType()) | |||
9677 | return; | |||
9678 | ||||
9679 | ASTContext &Ctx = Self.getASTContext(); | |||
9680 | SourceRange DiagRange(LHSExpr->getBeginLoc(), RHSExpr->getEndLoc()); | |||
9681 | ||||
9682 | const QualType CharType = CharExpr->getType(); | |||
9683 | if (!CharType->isAnyCharacterType() && | |||
9684 | CharType->isIntegerType() && | |||
9685 | llvm::isUIntN(Ctx.getCharWidth(), CharExpr->getValue())) { | |||
9686 | Self.Diag(OpLoc, diag::warn_string_plus_char) | |||
9687 | << DiagRange << Ctx.CharTy; | |||
9688 | } else { | |||
9689 | Self.Diag(OpLoc, diag::warn_string_plus_char) | |||
9690 | << DiagRange << CharExpr->getType(); | |||
9691 | } | |||
9692 | ||||
9693 | // Only print a fixit for str + char, not for char + str. | |||
9694 | if (isa<CharacterLiteral>(RHSExpr->IgnoreImpCasts())) { | |||
9695 | SourceLocation EndLoc = Self.getLocForEndOfToken(RHSExpr->getEndLoc()); | |||
9696 | Self.Diag(OpLoc, diag::note_string_plus_scalar_silence) | |||
9697 | << FixItHint::CreateInsertion(LHSExpr->getBeginLoc(), "&") | |||
9698 | << FixItHint::CreateReplacement(SourceRange(OpLoc), "[") | |||
9699 | << FixItHint::CreateInsertion(EndLoc, "]"); | |||
9700 | } else { | |||
9701 | Self.Diag(OpLoc, diag::note_string_plus_scalar_silence); | |||
9702 | } | |||
9703 | } | |||
9704 | ||||
9705 | /// Emit error when two pointers are incompatible. | |||
9706 | static void diagnosePointerIncompatibility(Sema &S, SourceLocation Loc, | |||
9707 | Expr *LHSExpr, Expr *RHSExpr) { | |||
9708 | assert(LHSExpr->getType()->isAnyPointerType())((LHSExpr->getType()->isAnyPointerType()) ? static_cast <void> (0) : __assert_fail ("LHSExpr->getType()->isAnyPointerType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 9708, __PRETTY_FUNCTION__)); | |||
9709 | assert(RHSExpr->getType()->isAnyPointerType())((RHSExpr->getType()->isAnyPointerType()) ? static_cast <void> (0) : __assert_fail ("RHSExpr->getType()->isAnyPointerType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 9709, __PRETTY_FUNCTION__)); | |||
9710 | S.Diag(Loc, diag::err_typecheck_sub_ptr_compatible) | |||
9711 | << LHSExpr->getType() << RHSExpr->getType() << LHSExpr->getSourceRange() | |||
9712 | << RHSExpr->getSourceRange(); | |||
9713 | } | |||
9714 | ||||
9715 | // C99 6.5.6 | |||
9716 | QualType Sema::CheckAdditionOperands(ExprResult &LHS, ExprResult &RHS, | |||
9717 | SourceLocation Loc, BinaryOperatorKind Opc, | |||
9718 | QualType* CompLHSTy) { | |||
9719 | checkArithmeticNull(*this, LHS, RHS, Loc, /*IsCompare=*/false); | |||
9720 | ||||
9721 | if (LHS.get()->getType()->isVectorType() || | |||
9722 | RHS.get()->getType()->isVectorType()) { | |||
9723 | QualType compType = CheckVectorOperands( | |||
9724 | LHS, RHS, Loc, CompLHSTy, | |||
9725 | /*AllowBothBool*/getLangOpts().AltiVec, | |||
9726 | /*AllowBoolConversions*/getLangOpts().ZVector); | |||
9727 | if (CompLHSTy) *CompLHSTy = compType; | |||
9728 | return compType; | |||
9729 | } | |||
9730 | ||||
9731 | QualType compType = UsualArithmeticConversions( | |||
9732 | LHS, RHS, Loc, CompLHSTy ? ACK_CompAssign : ACK_Arithmetic); | |||
9733 | if (LHS.isInvalid() || RHS.isInvalid()) | |||
9734 | return QualType(); | |||
9735 | ||||
9736 | // Diagnose "string literal" '+' int and string '+' "char literal". | |||
9737 | if (Opc == BO_Add) { | |||
9738 | diagnoseStringPlusInt(*this, Loc, LHS.get(), RHS.get()); | |||
9739 | diagnoseStringPlusChar(*this, Loc, LHS.get(), RHS.get()); | |||
9740 | } | |||
9741 | ||||
9742 | // handle the common case first (both operands are arithmetic). | |||
9743 | if (!compType.isNull() && compType->isArithmeticType()) { | |||
9744 | if (CompLHSTy) *CompLHSTy = compType; | |||
9745 | return compType; | |||
9746 | } | |||
9747 | ||||
9748 | // Type-checking. Ultimately the pointer's going to be in PExp; | |||
9749 | // note that we bias towards the LHS being the pointer. | |||
9750 | Expr *PExp = LHS.get(), *IExp = RHS.get(); | |||
9751 | ||||
9752 | bool isObjCPointer; | |||
9753 | if (PExp->getType()->isPointerType()) { | |||
9754 | isObjCPointer = false; | |||
9755 | } else if (PExp->getType()->isObjCObjectPointerType()) { | |||
9756 | isObjCPointer = true; | |||
9757 | } else { | |||
9758 | std::swap(PExp, IExp); | |||
9759 | if (PExp->getType()->isPointerType()) { | |||
9760 | isObjCPointer = false; | |||
9761 | } else if (PExp->getType()->isObjCObjectPointerType()) { | |||
9762 | isObjCPointer = true; | |||
9763 | } else { | |||
9764 | return InvalidOperands(Loc, LHS, RHS); | |||
9765 | } | |||
9766 | } | |||
9767 | assert(PExp->getType()->isAnyPointerType())((PExp->getType()->isAnyPointerType()) ? static_cast< void> (0) : __assert_fail ("PExp->getType()->isAnyPointerType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 9767, __PRETTY_FUNCTION__)); | |||
9768 | ||||
9769 | if (!IExp->getType()->isIntegerType()) | |||
9770 | return InvalidOperands(Loc, LHS, RHS); | |||
9771 | ||||
9772 | // Adding to a null pointer results in undefined behavior. | |||
9773 | if (PExp->IgnoreParenCasts()->isNullPointerConstant( | |||
9774 | Context, Expr::NPC_ValueDependentIsNotNull)) { | |||
9775 | // In C++ adding zero to a null pointer is defined. | |||
9776 | Expr::EvalResult KnownVal; | |||
9777 | if (!getLangOpts().CPlusPlus || | |||
9778 | (!IExp->isValueDependent() && | |||
9779 | (!IExp->EvaluateAsInt(KnownVal, Context) || | |||
9780 | KnownVal.Val.getInt() != 0))) { | |||
9781 | // Check the conditions to see if this is the 'p = nullptr + n' idiom. | |||
9782 | bool IsGNUIdiom = BinaryOperator::isNullPointerArithmeticExtension( | |||
9783 | Context, BO_Add, PExp, IExp); | |||
9784 | diagnoseArithmeticOnNullPointer(*this, Loc, PExp, IsGNUIdiom); | |||
9785 | } | |||
9786 | } | |||
9787 | ||||
9788 | if (!checkArithmeticOpPointerOperand(*this, Loc, PExp)) | |||
9789 | return QualType(); | |||
9790 | ||||
9791 | if (isObjCPointer && checkArithmeticOnObjCPointer(*this, Loc, PExp)) | |||
9792 | return QualType(); | |||
9793 | ||||
9794 | // Check array bounds for pointer arithemtic | |||
9795 | CheckArrayAccess(PExp, IExp); | |||
9796 | ||||
9797 | if (CompLHSTy) { | |||
9798 | QualType LHSTy = Context.isPromotableBitField(LHS.get()); | |||
9799 | if (LHSTy.isNull()) { | |||
9800 | LHSTy = LHS.get()->getType(); | |||
9801 | if (LHSTy->isPromotableIntegerType()) | |||
9802 | LHSTy = Context.getPromotedIntegerType(LHSTy); | |||
9803 | } | |||
9804 | *CompLHSTy = LHSTy; | |||
9805 | } | |||
9806 | ||||
9807 | return PExp->getType(); | |||
9808 | } | |||
9809 | ||||
9810 | // C99 6.5.6 | |||
9811 | QualType Sema::CheckSubtractionOperands(ExprResult &LHS, ExprResult &RHS, | |||
9812 | SourceLocation Loc, | |||
9813 | QualType* CompLHSTy) { | |||
9814 | checkArithmeticNull(*this, LHS, RHS, Loc, /*IsCompare=*/false); | |||
9815 | ||||
9816 | if (LHS.get()->getType()->isVectorType() || | |||
9817 | RHS.get()->getType()->isVectorType()) { | |||
9818 | QualType compType = CheckVectorOperands( | |||
9819 | LHS, RHS, Loc, CompLHSTy, | |||
9820 | /*AllowBothBool*/getLangOpts().AltiVec, | |||
9821 | /*AllowBoolConversions*/getLangOpts().ZVector); | |||
9822 | if (CompLHSTy) *CompLHSTy = compType; | |||
9823 | return compType; | |||
9824 | } | |||
9825 | ||||
9826 | QualType compType = UsualArithmeticConversions( | |||
9827 | LHS, RHS, Loc, CompLHSTy ? ACK_CompAssign : ACK_Arithmetic); | |||
9828 | if (LHS.isInvalid() || RHS.isInvalid()) | |||
9829 | return QualType(); | |||
9830 | ||||
9831 | // Enforce type constraints: C99 6.5.6p3. | |||
9832 | ||||
9833 | // Handle the common case first (both operands are arithmetic). | |||
9834 | if (!compType.isNull() && compType->isArithmeticType()) { | |||
9835 | if (CompLHSTy) *CompLHSTy = compType; | |||
9836 | return compType; | |||
9837 | } | |||
9838 | ||||
9839 | // Either ptr - int or ptr - ptr. | |||
9840 | if (LHS.get()->getType()->isAnyPointerType()) { | |||
9841 | QualType lpointee = LHS.get()->getType()->getPointeeType(); | |||
9842 | ||||
9843 | // Diagnose bad cases where we step over interface counts. | |||
9844 | if (LHS.get()->getType()->isObjCObjectPointerType() && | |||
9845 | checkArithmeticOnObjCPointer(*this, Loc, LHS.get())) | |||
9846 | return QualType(); | |||
9847 | ||||
9848 | // The result type of a pointer-int computation is the pointer type. | |||
9849 | if (RHS.get()->getType()->isIntegerType()) { | |||
9850 | // Subtracting from a null pointer should produce a warning. | |||
9851 | // The last argument to the diagnose call says this doesn't match the | |||
9852 | // GNU int-to-pointer idiom. | |||
9853 | if (LHS.get()->IgnoreParenCasts()->isNullPointerConstant(Context, | |||
9854 | Expr::NPC_ValueDependentIsNotNull)) { | |||
9855 | // In C++ adding zero to a null pointer is defined. | |||
9856 | Expr::EvalResult KnownVal; | |||
9857 | if (!getLangOpts().CPlusPlus || | |||
9858 | (!RHS.get()->isValueDependent() && | |||
9859 | (!RHS.get()->EvaluateAsInt(KnownVal, Context) || | |||
9860 | KnownVal.Val.getInt() != 0))) { | |||
9861 | diagnoseArithmeticOnNullPointer(*this, Loc, LHS.get(), false); | |||
9862 | } | |||
9863 | } | |||
9864 | ||||
9865 | if (!checkArithmeticOpPointerOperand(*this, Loc, LHS.get())) | |||
9866 | return QualType(); | |||
9867 | ||||
9868 | // Check array bounds for pointer arithemtic | |||
9869 | CheckArrayAccess(LHS.get(), RHS.get(), /*ArraySubscriptExpr*/nullptr, | |||
9870 | /*AllowOnePastEnd*/true, /*IndexNegated*/true); | |||
9871 | ||||
9872 | if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); | |||
9873 | return LHS.get()->getType(); | |||
9874 | } | |||
9875 | ||||
9876 | // Handle pointer-pointer subtractions. | |||
9877 | if (const PointerType *RHSPTy | |||
9878 | = RHS.get()->getType()->getAs<PointerType>()) { | |||
9879 | QualType rpointee = RHSPTy->getPointeeType(); | |||
9880 | ||||
9881 | if (getLangOpts().CPlusPlus) { | |||
9882 | // Pointee types must be the same: C++ [expr.add] | |||
9883 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { | |||
9884 | diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); | |||
9885 | } | |||
9886 | } else { | |||
9887 | // Pointee types must be compatible C99 6.5.6p3 | |||
9888 | if (!Context.typesAreCompatible( | |||
9889 | Context.getCanonicalType(lpointee).getUnqualifiedType(), | |||
9890 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { | |||
9891 | diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); | |||
9892 | return QualType(); | |||
9893 | } | |||
9894 | } | |||
9895 | ||||
9896 | if (!checkArithmeticBinOpPointerOperands(*this, Loc, | |||
9897 | LHS.get(), RHS.get())) | |||
9898 | return QualType(); | |||
9899 | ||||
9900 | // FIXME: Add warnings for nullptr - ptr. | |||
9901 | ||||
9902 | // The pointee type may have zero size. As an extension, a structure or | |||
9903 | // union may have zero size or an array may have zero length. In this | |||
9904 | // case subtraction does not make sense. | |||
9905 | if (!rpointee->isVoidType() && !rpointee->isFunctionType()) { | |||
9906 | CharUnits ElementSize = Context.getTypeSizeInChars(rpointee); | |||
9907 | if (ElementSize.isZero()) { | |||
9908 | Diag(Loc,diag::warn_sub_ptr_zero_size_types) | |||
9909 | << rpointee.getUnqualifiedType() | |||
9910 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | |||
9911 | } | |||
9912 | } | |||
9913 | ||||
9914 | if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); | |||
9915 | return Context.getPointerDiffType(); | |||
9916 | } | |||
9917 | } | |||
9918 | ||||
9919 | return InvalidOperands(Loc, LHS, RHS); | |||
9920 | } | |||
9921 | ||||
9922 | static bool isScopedEnumerationType(QualType T) { | |||
9923 | if (const EnumType *ET = T->getAs<EnumType>()) | |||
9924 | return ET->getDecl()->isScoped(); | |||
9925 | return false; | |||
9926 | } | |||
9927 | ||||
9928 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS, | |||
9929 | SourceLocation Loc, BinaryOperatorKind Opc, | |||
9930 | QualType LHSType) { | |||
9931 | // OpenCL 6.3j: shift values are effectively % word size of LHS (more defined), | |||
9932 | // so skip remaining warnings as we don't want to modify values within Sema. | |||
9933 | if (S.getLangOpts().OpenCL) | |||
9934 | return; | |||
9935 | ||||
9936 | // Check right/shifter operand | |||
9937 | Expr::EvalResult RHSResult; | |||
9938 | if (RHS.get()->isValueDependent() || | |||
9939 | !RHS.get()->EvaluateAsInt(RHSResult, S.Context)) | |||
9940 | return; | |||
9941 | llvm::APSInt Right = RHSResult.Val.getInt(); | |||
9942 | ||||
9943 | if (Right.isNegative()) { | |||
9944 | S.DiagRuntimeBehavior(Loc, RHS.get(), | |||
9945 | S.PDiag(diag::warn_shift_negative) | |||
9946 | << RHS.get()->getSourceRange()); | |||
9947 | return; | |||
9948 | } | |||
9949 | llvm::APInt LeftBits(Right.getBitWidth(), | |||
9950 | S.Context.getTypeSize(LHS.get()->getType())); | |||
9951 | if (Right.uge(LeftBits)) { | |||
9952 | S.DiagRuntimeBehavior(Loc, RHS.get(), | |||
9953 | S.PDiag(diag::warn_shift_gt_typewidth) | |||
9954 | << RHS.get()->getSourceRange()); | |||
9955 | return; | |||
9956 | } | |||
9957 | if (Opc != BO_Shl) | |||
9958 | return; | |||
9959 | ||||
9960 | // When left shifting an ICE which is signed, we can check for overflow which | |||
9961 | // according to C++ standards prior to C++2a has undefined behavior | |||
9962 | // ([expr.shift] 5.8/2). Unsigned integers have defined behavior modulo one | |||
9963 | // more than the maximum value representable in the result type, so never | |||
9964 | // warn for those. (FIXME: Unsigned left-shift overflow in a constant | |||
9965 | // expression is still probably a bug.) | |||
9966 | Expr::EvalResult LHSResult; | |||
9967 | if (LHS.get()->isValueDependent() || | |||
9968 | LHSType->hasUnsignedIntegerRepresentation() || | |||
9969 | !LHS.get()->EvaluateAsInt(LHSResult, S.Context)) | |||
9970 | return; | |||
9971 | llvm::APSInt Left = LHSResult.Val.getInt(); | |||
9972 | ||||
9973 | // If LHS does not have a signed type and non-negative value | |||
9974 | // then, the behavior is undefined before C++2a. Warn about it. | |||
9975 | if (Left.isNegative() && !S.getLangOpts().isSignedOverflowDefined() && | |||
9976 | !S.getLangOpts().CPlusPlus2a) { | |||
9977 | S.DiagRuntimeBehavior(Loc, LHS.get(), | |||
9978 | S.PDiag(diag::warn_shift_lhs_negative) | |||
9979 | << LHS.get()->getSourceRange()); | |||
9980 | return; | |||
9981 | } | |||
9982 | ||||
9983 | llvm::APInt ResultBits = | |||
9984 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); | |||
9985 | if (LeftBits.uge(ResultBits)) | |||
9986 | return; | |||
9987 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); | |||
9988 | Result = Result.shl(Right); | |||
9989 | ||||
9990 | // Print the bit representation of the signed integer as an unsigned | |||
9991 | // hexadecimal number. | |||
9992 | SmallString<40> HexResult; | |||
9993 | Result.toString(HexResult, 16, /*Signed =*/false, /*Literal =*/true); | |||
9994 | ||||
9995 | // If we are only missing a sign bit, this is less likely to result in actual | |||
9996 | // bugs -- if the result is cast back to an unsigned type, it will have the | |||
9997 | // expected value. Thus we place this behind a different warning that can be | |||
9998 | // turned off separately if needed. | |||
9999 | if (LeftBits == ResultBits - 1) { | |||
10000 | S.Diag(Loc, diag::warn_shift_result_sets_sign_bit) | |||
10001 | << HexResult << LHSType | |||
10002 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | |||
10003 | return; | |||
10004 | } | |||
10005 | ||||
10006 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) | |||
10007 | << HexResult.str() << Result.getMinSignedBits() << LHSType | |||
10008 | << Left.getBitWidth() << LHS.get()->getSourceRange() | |||
10009 | << RHS.get()->getSourceRange(); | |||
10010 | } | |||
10011 | ||||
10012 | /// Return the resulting type when a vector is shifted | |||
10013 | /// by a scalar or vector shift amount. | |||
10014 | static QualType checkVectorShift(Sema &S, ExprResult &LHS, ExprResult &RHS, | |||
10015 | SourceLocation Loc, bool IsCompAssign) { | |||
10016 | // OpenCL v1.1 s6.3.j says RHS can be a vector only if LHS is a vector. | |||
10017 | if ((S.LangOpts.OpenCL || S.LangOpts.ZVector) && | |||
10018 | !LHS.get()->getType()->isVectorType()) { | |||
10019 | S.Diag(Loc, diag::err_shift_rhs_only_vector) | |||
10020 | << RHS.get()->getType() << LHS.get()->getType() | |||
10021 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | |||
10022 | return QualType(); | |||
10023 | } | |||
10024 | ||||
10025 | if (!IsCompAssign) { | |||
10026 | LHS = S.UsualUnaryConversions(LHS.get()); | |||
10027 | if (LHS.isInvalid()) return QualType(); | |||
10028 | } | |||
10029 | ||||
10030 | RHS = S.UsualUnaryConversions(RHS.get()); | |||
10031 | if (RHS.isInvalid()) return QualType(); | |||
10032 | ||||
10033 | QualType LHSType = LHS.get()->getType(); | |||
10034 | // Note that LHS might be a scalar because the routine calls not only in | |||
10035 | // OpenCL case. | |||
10036 | const VectorType *LHSVecTy = LHSType->getAs<VectorType>(); | |||
10037 | QualType LHSEleType = LHSVecTy ? LHSVecTy->getElementType() : LHSType; | |||
10038 | ||||
10039 | // Note that RHS might not be a vector. | |||
10040 | QualType RHSType = RHS.get()->getType(); | |||
10041 | const VectorType *RHSVecTy = RHSType->getAs<VectorType>(); | |||
10042 | QualType RHSEleType = RHSVecTy ? RHSVecTy->getElementType() : RHSType; | |||
10043 | ||||
10044 | // The operands need to be integers. | |||
10045 | if (!LHSEleType->isIntegerType()) { | |||
10046 | S.Diag(Loc, diag::err_typecheck_expect_int) | |||
10047 | << LHS.get()->getType() << LHS.get()->getSourceRange(); | |||
10048 | return QualType(); | |||
10049 | } | |||
10050 | ||||
10051 | if (!RHSEleType->isIntegerType()) { | |||
10052 | S.Diag(Loc, diag::err_typecheck_expect_int) | |||
10053 | << RHS.get()->getType() << RHS.get()->getSourceRange(); | |||
10054 | return QualType(); | |||
10055 | } | |||
10056 | ||||
10057 | if (!LHSVecTy) { | |||
10058 | assert(RHSVecTy)((RHSVecTy) ? static_cast<void> (0) : __assert_fail ("RHSVecTy" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 10058, __PRETTY_FUNCTION__)); | |||
10059 | if (IsCompAssign) | |||
10060 | return RHSType; | |||
10061 | if (LHSEleType != RHSEleType) { | |||
10062 | LHS = S.ImpCastExprToType(LHS.get(),RHSEleType, CK_IntegralCast); | |||
10063 | LHSEleType = RHSEleType; | |||
10064 | } | |||
10065 | QualType VecTy = | |||
10066 | S.Context.getExtVectorType(LHSEleType, RHSVecTy->getNumElements()); | |||
10067 | LHS = S.ImpCastExprToType(LHS.get(), VecTy, CK_VectorSplat); | |||
10068 | LHSType = VecTy; | |||
10069 | } else if (RHSVecTy) { | |||
10070 | // OpenCL v1.1 s6.3.j says that for vector types, the operators | |||
10071 | // are applied component-wise. So if RHS is a vector, then ensure | |||
10072 | // that the number of elements is the same as LHS... | |||
10073 | if (RHSVecTy->getNumElements() != LHSVecTy->getNumElements()) { | |||
10074 | S.Diag(Loc, diag::err_typecheck_vector_lengths_not_equal) | |||
10075 | << LHS.get()->getType() << RHS.get()->getType() | |||
10076 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | |||
10077 | return QualType(); | |||
10078 | } | |||
10079 | if (!S.LangOpts.OpenCL && !S.LangOpts.ZVector) { | |||
10080 | const BuiltinType *LHSBT = LHSEleType->getAs<clang::BuiltinType>(); | |||
10081 | const BuiltinType *RHSBT = RHSEleType->getAs<clang::BuiltinType>(); | |||
10082 | if (LHSBT != RHSBT && | |||
10083 | S.Context.getTypeSize(LHSBT) != S.Context.getTypeSize(RHSBT)) { | |||
10084 | S.Diag(Loc, diag::warn_typecheck_vector_element_sizes_not_equal) | |||
10085 | << LHS.get()->getType() << RHS.get()->getType() | |||
10086 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | |||
10087 | } | |||
10088 | } | |||
10089 | } else { | |||
10090 | // ...else expand RHS to match the number of elements in LHS. | |||
10091 | QualType VecTy = | |||
10092 | S.Context.getExtVectorType(RHSEleType, LHSVecTy->getNumElements()); | |||
10093 | RHS = S.ImpCastExprToType(RHS.get(), VecTy, CK_VectorSplat); | |||
10094 | } | |||
10095 | ||||
10096 | return LHSType; | |||
10097 | } | |||
10098 | ||||
10099 | // C99 6.5.7 | |||
10100 | QualType Sema::CheckShiftOperands(ExprResult &LHS, ExprResult &RHS, | |||
10101 | SourceLocation Loc, BinaryOperatorKind Opc, | |||
10102 | bool IsCompAssign) { | |||
10103 | checkArithmeticNull(*this, LHS, RHS, Loc, /*IsCompare=*/false); | |||
10104 | ||||
10105 | // Vector shifts promote their scalar inputs to vector type. | |||
10106 | if (LHS.get()->getType()->isVectorType() || | |||
10107 | RHS.get()->getType()->isVectorType()) { | |||
10108 | if (LangOpts.ZVector) { | |||
10109 | // The shift operators for the z vector extensions work basically | |||
10110 | // like general shifts, except that neither the LHS nor the RHS is | |||
10111 | // allowed to be a "vector bool". | |||
10112 | if (auto LHSVecType = LHS.get()->getType()->getAs<VectorType>()) | |||
10113 | if (LHSVecType->getVectorKind() == VectorType::AltiVecBool) | |||
10114 | return InvalidOperands(Loc, LHS, RHS); | |||
10115 | if (auto RHSVecType = RHS.get()->getType()->getAs<VectorType>()) | |||
10116 | if (RHSVecType->getVectorKind() == VectorType::AltiVecBool) | |||
10117 | return InvalidOperands(Loc, LHS, RHS); | |||
10118 | } | |||
10119 | return checkVectorShift(*this, LHS, RHS, Loc, IsCompAssign); | |||
10120 | } | |||
10121 | ||||
10122 | // Shifts don't perform usual arithmetic conversions, they just do integer | |||
10123 | // promotions on each operand. C99 6.5.7p3 | |||
10124 | ||||
10125 | // For the LHS, do usual unary conversions, but then reset them away | |||
10126 | // if this is a compound assignment. | |||
10127 | ExprResult OldLHS = LHS; | |||
10128 | LHS = UsualUnaryConversions(LHS.get()); | |||
10129 | if (LHS.isInvalid()) | |||
10130 | return QualType(); | |||
10131 | QualType LHSType = LHS.get()->getType(); | |||
10132 | if (IsCompAssign) LHS = OldLHS; | |||
10133 | ||||
10134 | // The RHS is simpler. | |||
10135 | RHS = UsualUnaryConversions(RHS.get()); | |||
10136 | if (RHS.isInvalid()) | |||
10137 | return QualType(); | |||
10138 | QualType RHSType = RHS.get()->getType(); | |||
10139 | ||||
10140 | // C99 6.5.7p2: Each of the operands shall have integer type. | |||
10141 | if (!LHSType->hasIntegerRepresentation() || | |||
10142 | !RHSType->hasIntegerRepresentation()) | |||
10143 | return InvalidOperands(Loc, LHS, RHS); | |||
10144 | ||||
10145 | // C++0x: Don't allow scoped enums. FIXME: Use something better than | |||
10146 | // hasIntegerRepresentation() above instead of this. | |||
10147 | if (isScopedEnumerationType(LHSType) || | |||
10148 | isScopedEnumerationType(RHSType)) { | |||
10149 | return InvalidOperands(Loc, LHS, RHS); | |||
10150 | } | |||
10151 | // Sanity-check shift operands | |||
10152 | DiagnoseBadShiftValues(*this, LHS, RHS, Loc, Opc, LHSType); | |||
10153 | ||||
10154 | // "The type of the result is that of the promoted left operand." | |||
10155 | return LHSType; | |||
10156 | } | |||
10157 | ||||
10158 | /// Diagnose bad pointer comparisons. | |||
10159 | static void diagnoseDistinctPointerComparison(Sema &S, SourceLocation Loc, | |||
10160 | ExprResult &LHS, ExprResult &RHS, | |||
10161 | bool IsError) { | |||
10162 | S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_distinct_pointers | |||
10163 | : diag::ext_typecheck_comparison_of_distinct_pointers) | |||
10164 | << LHS.get()->getType() << RHS.get()->getType() | |||
10165 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | |||
10166 | } | |||
10167 | ||||
10168 | /// Returns false if the pointers are converted to a composite type, | |||
10169 | /// true otherwise. | |||
10170 | static bool convertPointersToCompositeType(Sema &S, SourceLocation Loc, | |||
10171 | ExprResult &LHS, ExprResult &RHS) { | |||
10172 | // C++ [expr.rel]p2: | |||
10173 | // [...] Pointer conversions (4.10) and qualification | |||
10174 | // conversions (4.4) are performed on pointer operands (or on | |||
10175 | // a pointer operand and a null pointer constant) to bring | |||
10176 | // them to their composite pointer type. [...] | |||
10177 | // | |||
10178 | // C++ [expr.eq]p1 uses the same notion for (in)equality | |||
10179 | // comparisons of pointers. | |||
10180 | ||||
10181 | QualType LHSType = LHS.get()->getType(); | |||
10182 | QualType RHSType = RHS.get()->getType(); | |||
10183 | assert(LHSType->isPointerType() || RHSType->isPointerType() ||((LHSType->isPointerType() || RHSType->isPointerType() || LHSType->isMemberPointerType() || RHSType->isMemberPointerType ()) ? static_cast<void> (0) : __assert_fail ("LHSType->isPointerType() || RHSType->isPointerType() || LHSType->isMemberPointerType() || RHSType->isMemberPointerType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 10184, __PRETTY_FUNCTION__)) | |||
10184 | LHSType->isMemberPointerType() || RHSType->isMemberPointerType())((LHSType->isPointerType() || RHSType->isPointerType() || LHSType->isMemberPointerType() || RHSType->isMemberPointerType ()) ? static_cast<void> (0) : __assert_fail ("LHSType->isPointerType() || RHSType->isPointerType() || LHSType->isMemberPointerType() || RHSType->isMemberPointerType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 10184, __PRETTY_FUNCTION__)); | |||
10185 | ||||
10186 | QualType T = S.FindCompositePointerType(Loc, LHS, RHS); | |||
10187 | if (T.isNull()) { | |||
10188 | if ((LHSType->isAnyPointerType() || LHSType->isMemberPointerType()) && | |||
10189 | (RHSType->isAnyPointerType() || RHSType->isMemberPointerType())) | |||
10190 | diagnoseDistinctPointerComparison(S, Loc, LHS, RHS, /*isError*/true); | |||
10191 | else | |||
10192 | S.InvalidOperands(Loc, LHS, RHS); | |||
10193 | return true; | |||
10194 | } | |||
10195 | ||||
10196 | return false; | |||
10197 | } | |||
10198 | ||||
10199 | static void diagnoseFunctionPointerToVoidComparison(Sema &S, SourceLocation Loc, | |||
10200 | ExprResult &LHS, | |||
10201 | ExprResult &RHS, | |||
10202 | bool IsError) { | |||
10203 | S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_fptr_to_void | |||
10204 | : diag::ext_typecheck_comparison_of_fptr_to_void) | |||
10205 | << LHS.get()->getType() << RHS.get()->getType() | |||
10206 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | |||
10207 | } | |||
10208 | ||||
10209 | static bool isObjCObjectLiteral(ExprResult &E) { | |||
10210 | switch (E.get()->IgnoreParenImpCasts()->getStmtClass()) { | |||
10211 | case Stmt::ObjCArrayLiteralClass: | |||
10212 | case Stmt::ObjCDictionaryLiteralClass: | |||
10213 | case Stmt::ObjCStringLiteralClass: | |||
10214 | case Stmt::ObjCBoxedExprClass: | |||
10215 | return true; | |||
10216 | default: | |||
10217 | // Note that ObjCBoolLiteral is NOT an object literal! | |||
10218 | return false; | |||
10219 | } | |||
10220 | } | |||
10221 | ||||
10222 | static bool hasIsEqualMethod(Sema &S, const Expr *LHS, const Expr *RHS) { | |||
10223 | const ObjCObjectPointerType *Type = | |||
10224 | LHS->getType()->getAs<ObjCObjectPointerType>(); | |||
10225 | ||||
10226 | // If this is not actually an Objective-C object, bail out. | |||
10227 | if (!Type) | |||
10228 | return false; | |||
10229 | ||||
10230 | // Get the LHS object's interface type. | |||
10231 | QualType InterfaceType = Type->getPointeeType(); | |||
10232 | ||||
10233 | // If the RHS isn't an Objective-C object, bail out. | |||
10234 | if (!RHS->getType()->isObjCObjectPointerType()) | |||
10235 | return false; | |||
10236 | ||||
10237 | // Try to find the -isEqual: method. | |||
10238 | Selector IsEqualSel = S.NSAPIObj->getIsEqualSelector(); | |||
10239 | ObjCMethodDecl *Method = S.LookupMethodInObjectType(IsEqualSel, | |||
10240 | InterfaceType, | |||
10241 | /*IsInstance=*/true); | |||
10242 | if (!Method) { | |||
10243 | if (Type->isObjCIdType()) { | |||
10244 | // For 'id', just check the global pool. | |||
10245 | Method = S.LookupInstanceMethodInGlobalPool(IsEqualSel, SourceRange(), | |||
10246 | /*receiverId=*/true); | |||
10247 | } else { | |||
10248 | // Check protocols. | |||
10249 | Method = S.LookupMethodInQualifiedType(IsEqualSel, Type, | |||
10250 | /*IsInstance=*/true); | |||
10251 | } | |||
10252 | } | |||
10253 | ||||
10254 | if (!Method) | |||
10255 | return false; | |||
10256 | ||||
10257 | QualType T = Method->parameters()[0]->getType(); | |||
10258 | if (!T->isObjCObjectPointerType()) | |||
10259 | return false; | |||
10260 | ||||
10261 | QualType R = Method->getReturnType(); | |||
10262 | if (!R->isScalarType()) | |||
10263 | return false; | |||
10264 | ||||
10265 | return true; | |||
10266 | } | |||
10267 | ||||
10268 | Sema::ObjCLiteralKind Sema::CheckLiteralKind(Expr *FromE) { | |||
10269 | FromE = FromE->IgnoreParenImpCasts(); | |||
10270 | switch (FromE->getStmtClass()) { | |||
10271 | default: | |||
10272 | break; | |||
10273 | case Stmt::ObjCStringLiteralClass: | |||
10274 | // "string literal" | |||
10275 | return LK_String; | |||
10276 | case Stmt::ObjCArrayLiteralClass: | |||
10277 | // "array literal" | |||
10278 | return LK_Array; | |||
10279 | case Stmt::ObjCDictionaryLiteralClass: | |||
10280 | // "dictionary literal" | |||
10281 | return LK_Dictionary; | |||
10282 | case Stmt::BlockExprClass: | |||
10283 | return LK_Block; | |||
10284 | case Stmt::ObjCBoxedExprClass: { | |||
10285 | Expr *Inner = cast<ObjCBoxedExpr>(FromE)->getSubExpr()->IgnoreParens(); | |||
10286 | switch (Inner->getStmtClass()) { | |||
10287 | case Stmt::IntegerLiteralClass: | |||
10288 | case Stmt::FloatingLiteralClass: | |||
10289 | case Stmt::CharacterLiteralClass: | |||
10290 | case Stmt::ObjCBoolLiteralExprClass: | |||
10291 | case Stmt::CXXBoolLiteralExprClass: | |||
10292 | // "numeric literal" | |||
10293 | return LK_Numeric; | |||
10294 | case Stmt::ImplicitCastExprClass: { | |||
10295 | CastKind CK = cast<CastExpr>(Inner)->getCastKind(); | |||
10296 | // Boolean literals can be represented by implicit casts. | |||
10297 | if (CK == CK_IntegralToBoolean || CK == CK_IntegralCast) | |||
10298 | return LK_Numeric; | |||
10299 | break; | |||
10300 | } | |||
10301 | default: | |||
10302 | break; | |||
10303 | } | |||
10304 | return LK_Boxed; | |||
10305 | } | |||
10306 | } | |||
10307 | return LK_None; | |||
10308 | } | |||
10309 | ||||
10310 | static void diagnoseObjCLiteralComparison(Sema &S, SourceLocation Loc, | |||
10311 | ExprResult &LHS, ExprResult &RHS, | |||
10312 | BinaryOperator::Opcode Opc){ | |||
10313 | Expr *Literal; | |||
10314 | Expr *Other; | |||
10315 | if (isObjCObjectLiteral(LHS)) { | |||
10316 | Literal = LHS.get(); | |||
10317 | Other = RHS.get(); | |||
10318 | } else { | |||
10319 | Literal = RHS.get(); | |||
10320 | Other = LHS.get(); | |||
10321 | } | |||
10322 | ||||
10323 | // Don't warn on comparisons against nil. | |||
10324 | Other = Other->IgnoreParenCasts(); | |||
10325 | if (Other->isNullPointerConstant(S.getASTContext(), | |||
10326 | Expr::NPC_ValueDependentIsNotNull)) | |||
10327 | return; | |||
10328 | ||||
10329 | // This should be kept in sync with warn_objc_literal_comparison. | |||
10330 | // LK_String should always be after the other literals, since it has its own | |||
10331 | // warning flag. | |||
10332 | Sema::ObjCLiteralKind LiteralKind = S.CheckLiteralKind(Literal); | |||
10333 | assert(LiteralKind != Sema::LK_Block)((LiteralKind != Sema::LK_Block) ? static_cast<void> (0 ) : __assert_fail ("LiteralKind != Sema::LK_Block", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 10333, __PRETTY_FUNCTION__)); | |||
10334 | if (LiteralKind == Sema::LK_None) { | |||
10335 | llvm_unreachable("Unknown Objective-C object literal kind")::llvm::llvm_unreachable_internal("Unknown Objective-C object literal kind" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 10335); | |||
10336 | } | |||
10337 | ||||
10338 | if (LiteralKind == Sema::LK_String) | |||
10339 | S.Diag(Loc, diag::warn_objc_string_literal_comparison) | |||
10340 | << Literal->getSourceRange(); | |||
10341 | else | |||
10342 | S.Diag(Loc, diag::warn_objc_literal_comparison) | |||
10343 | << LiteralKind << Literal->getSourceRange(); | |||
10344 | ||||
10345 | if (BinaryOperator::isEqualityOp(Opc) && | |||
10346 | hasIsEqualMethod(S, LHS.get(), RHS.get())) { | |||
10347 | SourceLocation Start = LHS.get()->getBeginLoc(); | |||
10348 | SourceLocation End = S.getLocForEndOfToken(RHS.get()->getEndLoc()); | |||
10349 | CharSourceRange OpRange = | |||
10350 | CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc)); | |||
10351 | ||||
10352 | S.Diag(Loc, diag::note_objc_literal_comparison_isequal) | |||
10353 | << FixItHint::CreateInsertion(Start, Opc == BO_EQ ? "[" : "![") | |||
10354 | << FixItHint::CreateReplacement(OpRange, " isEqual:") | |||
10355 | << FixItHint::CreateInsertion(End, "]"); | |||
10356 | } | |||
10357 | } | |||
10358 | ||||
10359 | /// Warns on !x < y, !x & y where !(x < y), !(x & y) was probably intended. | |||
10360 | static void diagnoseLogicalNotOnLHSofCheck(Sema &S, ExprResult &LHS, | |||
10361 | ExprResult &RHS, SourceLocation Loc, | |||
10362 | BinaryOperatorKind Opc) { | |||
10363 | // Check that left hand side is !something. | |||
10364 | UnaryOperator *UO = dyn_cast<UnaryOperator>(LHS.get()->IgnoreImpCasts()); | |||
10365 | if (!UO || UO->getOpcode() != UO_LNot) return; | |||
10366 | ||||
10367 | // Only check if the right hand side is non-bool arithmetic type. | |||
10368 | if (RHS.get()->isKnownToHaveBooleanValue()) return; | |||
10369 | ||||
10370 | // Make sure that the something in !something is not bool. | |||
10371 | Expr *SubExpr = UO->getSubExpr()->IgnoreImpCasts(); | |||
10372 | if (SubExpr->isKnownToHaveBooleanValue()) return; | |||
10373 | ||||
10374 | // Emit warning. | |||
10375 | bool IsBitwiseOp = Opc == BO_And || Opc == BO_Or || Opc == BO_Xor; | |||
10376 | S.Diag(UO->getOperatorLoc(), diag::warn_logical_not_on_lhs_of_check) | |||
10377 | << Loc << IsBitwiseOp; | |||
10378 | ||||
10379 | // First note suggest !(x < y) | |||
10380 | SourceLocation FirstOpen = SubExpr->getBeginLoc(); | |||
10381 | SourceLocation FirstClose = RHS.get()->getEndLoc(); | |||
10382 | FirstClose = S.getLocForEndOfToken(FirstClose); | |||
10383 | if (FirstClose.isInvalid()) | |||
10384 | FirstOpen = SourceLocation(); | |||
10385 | S.Diag(UO->getOperatorLoc(), diag::note_logical_not_fix) | |||
10386 | << IsBitwiseOp | |||
10387 | << FixItHint::CreateInsertion(FirstOpen, "(") | |||
10388 | << FixItHint::CreateInsertion(FirstClose, ")"); | |||
10389 | ||||
10390 | // Second note suggests (!x) < y | |||
10391 | SourceLocation SecondOpen = LHS.get()->getBeginLoc(); | |||
10392 | SourceLocation SecondClose = LHS.get()->getEndLoc(); | |||
10393 | SecondClose = S.getLocForEndOfToken(SecondClose); | |||
10394 | if (SecondClose.isInvalid()) | |||
10395 | SecondOpen = SourceLocation(); | |||
10396 | S.Diag(UO->getOperatorLoc(), diag::note_logical_not_silence_with_parens) | |||
10397 | << FixItHint::CreateInsertion(SecondOpen, "(") | |||
10398 | << FixItHint::CreateInsertion(SecondClose, ")"); | |||
10399 | } | |||
10400 | ||||
10401 | // Returns true if E refers to a non-weak array. | |||
10402 | static bool checkForArray(const Expr *E) { | |||
10403 | const ValueDecl *D = nullptr; | |||
10404 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) { | |||
10405 | D = DR->getDecl(); | |||
10406 | } else if (const MemberExpr *Mem = dyn_cast<MemberExpr>(E)) { | |||
10407 | if (Mem->isImplicitAccess()) | |||
10408 | D = Mem->getMemberDecl(); | |||
10409 | } | |||
10410 | if (!D) | |||
10411 | return false; | |||
10412 | return D->getType()->isArrayType() && !D->isWeak(); | |||
10413 | } | |||
10414 | ||||
10415 | /// Diagnose some forms of syntactically-obvious tautological comparison. | |||
10416 | static void diagnoseTautologicalComparison(Sema &S, SourceLocation Loc, | |||
10417 | Expr *LHS, Expr *RHS, | |||
10418 | BinaryOperatorKind Opc) { | |||
10419 | Expr *LHSStripped = LHS->IgnoreParenImpCasts(); | |||
10420 | Expr *RHSStripped = RHS->IgnoreParenImpCasts(); | |||
10421 | ||||
10422 | QualType LHSType = LHS->getType(); | |||
10423 | QualType RHSType = RHS->getType(); | |||
10424 | if (LHSType->hasFloatingRepresentation() || | |||
10425 | (LHSType->isBlockPointerType() && !BinaryOperator::isEqualityOp(Opc)) || | |||
10426 | S.inTemplateInstantiation()) | |||
10427 | return; | |||
10428 | ||||
10429 | // Comparisons between two array types are ill-formed for operator<=>, so | |||
10430 | // we shouldn't emit any additional warnings about it. | |||
10431 | if (Opc == BO_Cmp && LHSType->isArrayType() && RHSType->isArrayType()) | |||
10432 | return; | |||
10433 | ||||
10434 | // For non-floating point types, check for self-comparisons of the form | |||
10435 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and | |||
10436 | // often indicate logic errors in the program. | |||
10437 | // | |||
10438 | // NOTE: Don't warn about comparison expressions resulting from macro | |||
10439 | // expansion. Also don't warn about comparisons which are only self | |||
10440 | // comparisons within a template instantiation. The warnings should catch | |||
10441 | // obvious cases in the definition of the template anyways. The idea is to | |||
10442 | // warn when the typed comparison operator will always evaluate to the same | |||
10443 | // result. | |||
10444 | ||||
10445 | // Used for indexing into %select in warn_comparison_always | |||
10446 | enum { | |||
10447 | AlwaysConstant, | |||
10448 | AlwaysTrue, | |||
10449 | AlwaysFalse, | |||
10450 | AlwaysEqual, // std::strong_ordering::equal from operator<=> | |||
10451 | }; | |||
10452 | ||||
10453 | // C++2a [depr.array.comp]: | |||
10454 | // Equality and relational comparisons ([expr.eq], [expr.rel]) between two | |||
10455 | // operands of array type are deprecated. | |||
10456 | if (S.getLangOpts().CPlusPlus2a && LHSStripped->getType()->isArrayType() && | |||
10457 | RHSStripped->getType()->isArrayType()) { | |||
10458 | S.Diag(Loc, diag::warn_depr_array_comparison) | |||
10459 | << LHS->getSourceRange() << RHS->getSourceRange() | |||
10460 | << LHSStripped->getType() << RHSStripped->getType(); | |||
10461 | // Carry on to produce the tautological comparison warning, if this | |||
10462 | // expression is potentially-evaluated, we can resolve the array to a | |||
10463 | // non-weak declaration, and so on. | |||
10464 | } | |||
10465 | ||||
10466 | if (!LHS->getBeginLoc().isMacroID() && !RHS->getBeginLoc().isMacroID()) { | |||
10467 | if (Expr::isSameComparisonOperand(LHS, RHS)) { | |||
10468 | unsigned Result; | |||
10469 | switch (Opc) { | |||
10470 | case BO_EQ: | |||
10471 | case BO_LE: | |||
10472 | case BO_GE: | |||
10473 | Result = AlwaysTrue; | |||
10474 | break; | |||
10475 | case BO_NE: | |||
10476 | case BO_LT: | |||
10477 | case BO_GT: | |||
10478 | Result = AlwaysFalse; | |||
10479 | break; | |||
10480 | case BO_Cmp: | |||
10481 | Result = AlwaysEqual; | |||
10482 | break; | |||
10483 | default: | |||
10484 | Result = AlwaysConstant; | |||
10485 | break; | |||
10486 | } | |||
10487 | S.DiagRuntimeBehavior(Loc, nullptr, | |||
10488 | S.PDiag(diag::warn_comparison_always) | |||
10489 | << 0 /*self-comparison*/ | |||
10490 | << Result); | |||
10491 | } else if (checkForArray(LHSStripped) && checkForArray(RHSStripped)) { | |||
10492 | // What is it always going to evaluate to? | |||
10493 | unsigned Result; | |||
10494 | switch (Opc) { | |||
10495 | case BO_EQ: // e.g. array1 == array2 | |||
10496 | Result = AlwaysFalse; | |||
10497 | break; | |||
10498 | case BO_NE: // e.g. array1 != array2 | |||
10499 | Result = AlwaysTrue; | |||
10500 | break; | |||
10501 | default: // e.g. array1 <= array2 | |||
10502 | // The best we can say is 'a constant' | |||
10503 | Result = AlwaysConstant; | |||
10504 | break; | |||
10505 | } | |||
10506 | S.DiagRuntimeBehavior(Loc, nullptr, | |||
10507 | S.PDiag(diag::warn_comparison_always) | |||
10508 | << 1 /*array comparison*/ | |||
10509 | << Result); | |||
10510 | } | |||
10511 | } | |||
10512 | ||||
10513 | if (isa<CastExpr>(LHSStripped)) | |||
10514 | LHSStripped = LHSStripped->IgnoreParenCasts(); | |||
10515 | if (isa<CastExpr>(RHSStripped)) | |||
10516 | RHSStripped = RHSStripped->IgnoreParenCasts(); | |||
10517 | ||||
10518 | // Warn about comparisons against a string constant (unless the other | |||
10519 | // operand is null); the user probably wants string comparison function. | |||
10520 | Expr *LiteralString = nullptr; | |||
10521 | Expr *LiteralStringStripped = nullptr; | |||
10522 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && | |||
10523 | !RHSStripped->isNullPointerConstant(S.Context, | |||
10524 | Expr::NPC_ValueDependentIsNull)) { | |||
10525 | LiteralString = LHS; | |||
10526 | LiteralStringStripped = LHSStripped; | |||
10527 | } else if ((isa<StringLiteral>(RHSStripped) || | |||
10528 | isa<ObjCEncodeExpr>(RHSStripped)) && | |||
10529 | !LHSStripped->isNullPointerConstant(S.Context, | |||
10530 | Expr::NPC_ValueDependentIsNull)) { | |||
10531 | LiteralString = RHS; | |||
10532 | LiteralStringStripped = RHSStripped; | |||
10533 | } | |||
10534 | ||||
10535 | if (LiteralString) { | |||
10536 | S.DiagRuntimeBehavior(Loc, nullptr, | |||
10537 | S.PDiag(diag::warn_stringcompare) | |||
10538 | << isa<ObjCEncodeExpr>(LiteralStringStripped) | |||
10539 | << LiteralString->getSourceRange()); | |||
10540 | } | |||
10541 | } | |||
10542 | ||||
10543 | static ImplicitConversionKind castKindToImplicitConversionKind(CastKind CK) { | |||
10544 | switch (CK) { | |||
10545 | default: { | |||
10546 | #ifndef NDEBUG | |||
10547 | llvm::errs() << "unhandled cast kind: " << CastExpr::getCastKindName(CK) | |||
10548 | << "\n"; | |||
10549 | #endif | |||
10550 | llvm_unreachable("unhandled cast kind")::llvm::llvm_unreachable_internal("unhandled cast kind", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 10550); | |||
10551 | } | |||
10552 | case CK_UserDefinedConversion: | |||
10553 | return ICK_Identity; | |||
10554 | case CK_LValueToRValue: | |||
10555 | return ICK_Lvalue_To_Rvalue; | |||
10556 | case CK_ArrayToPointerDecay: | |||
10557 | return ICK_Array_To_Pointer; | |||
10558 | case CK_FunctionToPointerDecay: | |||
10559 | return ICK_Function_To_Pointer; | |||
10560 | case CK_IntegralCast: | |||
10561 | return ICK_Integral_Conversion; | |||
10562 | case CK_FloatingCast: | |||
10563 | return ICK_Floating_Conversion; | |||
10564 | case CK_IntegralToFloating: | |||
10565 | case CK_FloatingToIntegral: | |||
10566 | return ICK_Floating_Integral; | |||
10567 | case CK_IntegralComplexCast: | |||
10568 | case CK_FloatingComplexCast: | |||
10569 | case CK_FloatingComplexToIntegralComplex: | |||
10570 | case CK_IntegralComplexToFloatingComplex: | |||
10571 | return ICK_Complex_Conversion; | |||
10572 | case CK_FloatingComplexToReal: | |||
10573 | case CK_FloatingRealToComplex: | |||
10574 | case CK_IntegralComplexToReal: | |||
10575 | case CK_IntegralRealToComplex: | |||
10576 | return ICK_Complex_Real; | |||
10577 | } | |||
10578 | } | |||
10579 | ||||
10580 | static bool checkThreeWayNarrowingConversion(Sema &S, QualType ToType, Expr *E, | |||
10581 | QualType FromType, | |||
10582 | SourceLocation Loc) { | |||
10583 | // Check for a narrowing implicit conversion. | |||
10584 | StandardConversionSequence SCS; | |||
10585 | SCS.setAsIdentityConversion(); | |||
10586 | SCS.setToType(0, FromType); | |||
10587 | SCS.setToType(1, ToType); | |||
10588 | if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) | |||
10589 | SCS.Second = castKindToImplicitConversionKind(ICE->getCastKind()); | |||
10590 | ||||
10591 | APValue PreNarrowingValue; | |||
10592 | QualType PreNarrowingType; | |||
10593 | switch (SCS.getNarrowingKind(S.Context, E, PreNarrowingValue, | |||
10594 | PreNarrowingType, | |||
10595 | /*IgnoreFloatToIntegralConversion*/ true)) { | |||
10596 | case NK_Dependent_Narrowing: | |||
10597 | // Implicit conversion to a narrower type, but the expression is | |||
10598 | // value-dependent so we can't tell whether it's actually narrowing. | |||
10599 | case NK_Not_Narrowing: | |||
10600 | return false; | |||
10601 | ||||
10602 | case NK_Constant_Narrowing: | |||
10603 | // Implicit conversion to a narrower type, and the value is not a constant | |||
10604 | // expression. | |||
10605 | S.Diag(E->getBeginLoc(), diag::err_spaceship_argument_narrowing) | |||
10606 | << /*Constant*/ 1 | |||
10607 | << PreNarrowingValue.getAsString(S.Context, PreNarrowingType) << ToType; | |||
10608 | return true; | |||
10609 | ||||
10610 | case NK_Variable_Narrowing: | |||
10611 | // Implicit conversion to a narrower type, and the value is not a constant | |||
10612 | // expression. | |||
10613 | case NK_Type_Narrowing: | |||
10614 | S.Diag(E->getBeginLoc(), diag::err_spaceship_argument_narrowing) | |||
10615 | << /*Constant*/ 0 << FromType << ToType; | |||
10616 | // TODO: It's not a constant expression, but what if the user intended it | |||
10617 | // to be? Can we produce notes to help them figure out why it isn't? | |||
10618 | return true; | |||
10619 | } | |||
10620 | llvm_unreachable("unhandled case in switch")::llvm::llvm_unreachable_internal("unhandled case in switch", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 10620); | |||
10621 | } | |||
10622 | ||||
10623 | static QualType checkArithmeticOrEnumeralThreeWayCompare(Sema &S, | |||
10624 | ExprResult &LHS, | |||
10625 | ExprResult &RHS, | |||
10626 | SourceLocation Loc) { | |||
10627 | QualType LHSType = LHS.get()->getType(); | |||
10628 | QualType RHSType = RHS.get()->getType(); | |||
10629 | // Dig out the original argument type and expression before implicit casts | |||
10630 | // were applied. These are the types/expressions we need to check the | |||
10631 | // [expr.spaceship] requirements against. | |||
10632 | ExprResult LHSStripped = LHS.get()->IgnoreParenImpCasts(); | |||
10633 | ExprResult RHSStripped = RHS.get()->IgnoreParenImpCasts(); | |||
10634 | QualType LHSStrippedType = LHSStripped.get()->getType(); | |||
10635 | QualType RHSStrippedType = RHSStripped.get()->getType(); | |||
10636 | ||||
10637 | // C++2a [expr.spaceship]p3: If one of the operands is of type bool and the | |||
10638 | // other is not, the program is ill-formed. | |||
10639 | if (LHSStrippedType->isBooleanType() != RHSStrippedType->isBooleanType()) { | |||
10640 | S.InvalidOperands(Loc, LHSStripped, RHSStripped); | |||
10641 | return QualType(); | |||
10642 | } | |||
10643 | ||||
10644 | // FIXME: Consider combining this with checkEnumArithmeticConversions. | |||
10645 | int NumEnumArgs = (int)LHSStrippedType->isEnumeralType() + | |||
10646 | RHSStrippedType->isEnumeralType(); | |||
10647 | if (NumEnumArgs == 1) { | |||
10648 | bool LHSIsEnum = LHSStrippedType->isEnumeralType(); | |||
10649 | QualType OtherTy = LHSIsEnum ? RHSStrippedType : LHSStrippedType; | |||
10650 | if (OtherTy->hasFloatingRepresentation()) { | |||
10651 | S.InvalidOperands(Loc, LHSStripped, RHSStripped); | |||
10652 | return QualType(); | |||
10653 | } | |||
10654 | } | |||
10655 | if (NumEnumArgs == 2) { | |||
10656 | // C++2a [expr.spaceship]p5: If both operands have the same enumeration | |||
10657 | // type E, the operator yields the result of converting the operands | |||
10658 | // to the underlying type of E and applying <=> to the converted operands. | |||
10659 | if (!S.Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) { | |||
10660 | S.InvalidOperands(Loc, LHS, RHS); | |||
10661 | return QualType(); | |||
10662 | } | |||
10663 | QualType IntType = | |||
10664 | LHSStrippedType->castAs<EnumType>()->getDecl()->getIntegerType(); | |||
10665 | assert(IntType->isArithmeticType())((IntType->isArithmeticType()) ? static_cast<void> ( 0) : __assert_fail ("IntType->isArithmeticType()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 10665, __PRETTY_FUNCTION__)); | |||
10666 | ||||
10667 | // We can't use `CK_IntegralCast` when the underlying type is 'bool', so we | |||
10668 | // promote the boolean type, and all other promotable integer types, to | |||
10669 | // avoid this. | |||
10670 | if (IntType->isPromotableIntegerType()) | |||
10671 | IntType = S.Context.getPromotedIntegerType(IntType); | |||
10672 | ||||
10673 | LHS = S.ImpCastExprToType(LHS.get(), IntType, CK_IntegralCast); | |||
10674 | RHS = S.ImpCastExprToType(RHS.get(), IntType, CK_IntegralCast); | |||
10675 | LHSType = RHSType = IntType; | |||
10676 | } | |||
10677 | ||||
10678 | // C++2a [expr.spaceship]p4: If both operands have arithmetic types, the | |||
10679 | // usual arithmetic conversions are applied to the operands. | |||
10680 | QualType Type = | |||
10681 | S.UsualArithmeticConversions(LHS, RHS, Loc, Sema::ACK_Comparison); | |||
10682 | if (LHS.isInvalid() || RHS.isInvalid()) | |||
10683 | return QualType(); | |||
10684 | if (Type.isNull()) | |||
10685 | return S.InvalidOperands(Loc, LHS, RHS); | |||
10686 | ||||
10687 | Optional<ComparisonCategoryType> CCT = | |||
10688 | getComparisonCategoryForBuiltinCmp(Type); | |||
10689 | if (!CCT) | |||
10690 | return S.InvalidOperands(Loc, LHS, RHS); | |||
10691 | ||||
10692 | bool HasNarrowing = checkThreeWayNarrowingConversion( | |||
10693 | S, Type, LHS.get(), LHSType, LHS.get()->getBeginLoc()); | |||
10694 | HasNarrowing |= checkThreeWayNarrowingConversion(S, Type, RHS.get(), RHSType, | |||
10695 | RHS.get()->getBeginLoc()); | |||
10696 | if (HasNarrowing) | |||
10697 | return QualType(); | |||
10698 | ||||
10699 | assert(!Type.isNull() && "composite type for <=> has not been set")((!Type.isNull() && "composite type for <=> has not been set" ) ? static_cast<void> (0) : __assert_fail ("!Type.isNull() && \"composite type for <=> has not been set\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 10699, __PRETTY_FUNCTION__)); | |||
10700 | ||||
10701 | return S.CheckComparisonCategoryType( | |||
10702 | *CCT, Loc, Sema::ComparisonCategoryUsage::OperatorInExpression); | |||
10703 | } | |||
10704 | ||||
10705 | static QualType checkArithmeticOrEnumeralCompare(Sema &S, ExprResult &LHS, | |||
10706 | ExprResult &RHS, | |||
10707 | SourceLocation Loc, | |||
10708 | BinaryOperatorKind Opc) { | |||
10709 | if (Opc == BO_Cmp) | |||
10710 | return checkArithmeticOrEnumeralThreeWayCompare(S, LHS, RHS, Loc); | |||
10711 | ||||
10712 | // C99 6.5.8p3 / C99 6.5.9p4 | |||
10713 | QualType Type = | |||
10714 | S.UsualArithmeticConversions(LHS, RHS, Loc, Sema::ACK_Comparison); | |||
10715 | if (LHS.isInvalid() || RHS.isInvalid()) | |||
10716 | return QualType(); | |||
10717 | if (Type.isNull()) | |||
10718 | return S.InvalidOperands(Loc, LHS, RHS); | |||
10719 | assert(Type->isArithmeticType() || Type->isEnumeralType())((Type->isArithmeticType() || Type->isEnumeralType()) ? static_cast<void> (0) : __assert_fail ("Type->isArithmeticType() || Type->isEnumeralType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 10719, __PRETTY_FUNCTION__)); | |||
10720 | ||||
10721 | if (Type->isAnyComplexType() && BinaryOperator::isRelationalOp(Opc)) | |||
10722 | return S.InvalidOperands(Loc, LHS, RHS); | |||
10723 | ||||
10724 | // Check for comparisons of floating point operands using != and ==. | |||
10725 | if (Type->hasFloatingRepresentation() && BinaryOperator::isEqualityOp(Opc)) | |||
10726 | S.CheckFloatComparison(Loc, LHS.get(), RHS.get()); | |||
10727 | ||||
10728 | // The result of comparisons is 'bool' in C++, 'int' in C. | |||
10729 | return S.Context.getLogicalOperationType(); | |||
10730 | } | |||
10731 | ||||
10732 | void Sema::CheckPtrComparisonWithNullChar(ExprResult &E, ExprResult &NullE) { | |||
10733 | if (!NullE.get()->getType()->isAnyPointerType()) | |||
10734 | return; | |||
10735 | int NullValue = PP.isMacroDefined("NULL") ? 0 : 1; | |||
10736 | if (!E.get()->getType()->isAnyPointerType() && | |||
10737 | E.get()->isNullPointerConstant(Context, | |||
10738 | Expr::NPC_ValueDependentIsNotNull) == | |||
10739 | Expr::NPCK_ZeroExpression) { | |||
10740 | if (const auto *CL = dyn_cast<CharacterLiteral>(E.get())) { | |||
10741 | if (CL->getValue() == 0) | |||
10742 | Diag(E.get()->getExprLoc(), diag::warn_pointer_compare) | |||
10743 | << NullValue | |||
10744 | << FixItHint::CreateReplacement(E.get()->getExprLoc(), | |||
10745 | NullValue ? "NULL" : "(void *)0"); | |||
10746 | } else if (const auto *CE = dyn_cast<CStyleCastExpr>(E.get())) { | |||
10747 | TypeSourceInfo *TI = CE->getTypeInfoAsWritten(); | |||
10748 | QualType T = Context.getCanonicalType(TI->getType()).getUnqualifiedType(); | |||
10749 | if (T == Context.CharTy) | |||
10750 | Diag(E.get()->getExprLoc(), diag::warn_pointer_compare) | |||
10751 | << NullValue | |||
10752 | << FixItHint::CreateReplacement(E.get()->getExprLoc(), | |||
10753 | NullValue ? "NULL" : "(void *)0"); | |||
10754 | } | |||
10755 | } | |||
10756 | } | |||
10757 | ||||
10758 | // C99 6.5.8, C++ [expr.rel] | |||
10759 | QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, | |||
10760 | SourceLocation Loc, | |||
10761 | BinaryOperatorKind Opc) { | |||
10762 | bool IsRelational = BinaryOperator::isRelationalOp(Opc); | |||
10763 | bool IsThreeWay = Opc == BO_Cmp; | |||
10764 | bool IsOrdered = IsRelational || IsThreeWay; | |||
10765 | auto IsAnyPointerType = [](ExprResult E) { | |||
10766 | QualType Ty = E.get()->getType(); | |||
10767 | return Ty->isPointerType() || Ty->isMemberPointerType(); | |||
10768 | }; | |||
10769 | ||||
10770 | // C++2a [expr.spaceship]p6: If at least one of the operands is of pointer | |||
10771 | // type, array-to-pointer, ..., conversions are performed on both operands to | |||
10772 | // bring them to their composite type. | |||
10773 | // Otherwise, all comparisons expect an rvalue, so convert to rvalue before | |||
10774 | // any type-related checks. | |||
10775 | if (!IsThreeWay || IsAnyPointerType(LHS) || IsAnyPointerType(RHS)) { | |||
10776 | LHS = DefaultFunctionArrayLvalueConversion(LHS.get()); | |||
10777 | if (LHS.isInvalid()) | |||
10778 | return QualType(); | |||
10779 | RHS = DefaultFunctionArrayLvalueConversion(RHS.get()); | |||
10780 | if (RHS.isInvalid()) | |||
10781 | return QualType(); | |||
10782 | } else { | |||
10783 | LHS = DefaultLvalueConversion(LHS.get()); | |||
10784 | if (LHS.isInvalid()) | |||
10785 | return QualType(); | |||
10786 | RHS = DefaultLvalueConversion(RHS.get()); | |||
10787 | if (RHS.isInvalid()) | |||
10788 | return QualType(); | |||
10789 | } | |||
10790 | ||||
10791 | checkArithmeticNull(*this, LHS, RHS, Loc, /*IsCompare=*/true); | |||
10792 | if (!getLangOpts().CPlusPlus && BinaryOperator::isEqualityOp(Opc)) { | |||
10793 | CheckPtrComparisonWithNullChar(LHS, RHS); | |||
10794 | CheckPtrComparisonWithNullChar(RHS, LHS); | |||
10795 | } | |||
10796 | ||||
10797 | // Handle vector comparisons separately. | |||
10798 | if (LHS.get()->getType()->isVectorType() || | |||
10799 | RHS.get()->getType()->isVectorType()) | |||
10800 | return CheckVectorCompareOperands(LHS, RHS, Loc, Opc); | |||
10801 | ||||
10802 | diagnoseLogicalNotOnLHSofCheck(*this, LHS, RHS, Loc, Opc); | |||
10803 | diagnoseTautologicalComparison(*this, Loc, LHS.get(), RHS.get(), Opc); | |||
10804 | ||||
10805 | QualType LHSType = LHS.get()->getType(); | |||
10806 | QualType RHSType = RHS.get()->getType(); | |||
10807 | if ((LHSType->isArithmeticType() || LHSType->isEnumeralType()) && | |||
10808 | (RHSType->isArithmeticType() || RHSType->isEnumeralType())) | |||
10809 | return checkArithmeticOrEnumeralCompare(*this, LHS, RHS, Loc, Opc); | |||
10810 | ||||
10811 | const Expr::NullPointerConstantKind LHSNullKind = | |||
10812 | LHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull); | |||
10813 | const Expr::NullPointerConstantKind RHSNullKind = | |||
10814 | RHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull); | |||
10815 | bool LHSIsNull = LHSNullKind != Expr::NPCK_NotNull; | |||
10816 | bool RHSIsNull = RHSNullKind != Expr::NPCK_NotNull; | |||
10817 | ||||
10818 | auto computeResultTy = [&]() { | |||
10819 | if (Opc != BO_Cmp) | |||
10820 | return Context.getLogicalOperationType(); | |||
10821 | assert(getLangOpts().CPlusPlus)((getLangOpts().CPlusPlus) ? static_cast<void> (0) : __assert_fail ("getLangOpts().CPlusPlus", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 10821, __PRETTY_FUNCTION__)); | |||
10822 | assert(Context.hasSameType(LHS.get()->getType(), RHS.get()->getType()))((Context.hasSameType(LHS.get()->getType(), RHS.get()-> getType())) ? static_cast<void> (0) : __assert_fail ("Context.hasSameType(LHS.get()->getType(), RHS.get()->getType())" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 10822, __PRETTY_FUNCTION__)); | |||
10823 | ||||
10824 | QualType CompositeTy = LHS.get()->getType(); | |||
10825 | assert(!CompositeTy->isReferenceType())((!CompositeTy->isReferenceType()) ? static_cast<void> (0) : __assert_fail ("!CompositeTy->isReferenceType()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 10825, __PRETTY_FUNCTION__)); | |||
10826 | ||||
10827 | Optional<ComparisonCategoryType> CCT = | |||
10828 | getComparisonCategoryForBuiltinCmp(CompositeTy); | |||
10829 | if (!CCT) | |||
10830 | return InvalidOperands(Loc, LHS, RHS); | |||
10831 | ||||
10832 | if (CompositeTy->isPointerType() && LHSIsNull != RHSIsNull) { | |||
10833 | // P0946R0: Comparisons between a null pointer constant and an object | |||
10834 | // pointer result in std::strong_equality, which is ill-formed under | |||
10835 | // P1959R0. | |||
10836 | Diag(Loc, diag::err_typecheck_three_way_comparison_of_pointer_and_zero) | |||
10837 | << (LHSIsNull ? LHS.get()->getSourceRange() | |||
10838 | : RHS.get()->getSourceRange()); | |||
10839 | return QualType(); | |||
10840 | } | |||
10841 | ||||
10842 | return CheckComparisonCategoryType( | |||
10843 | *CCT, Loc, ComparisonCategoryUsage::OperatorInExpression); | |||
10844 | }; | |||
10845 | ||||
10846 | if (!IsOrdered && LHSIsNull != RHSIsNull) { | |||
10847 | bool IsEquality = Opc == BO_EQ; | |||
10848 | if (RHSIsNull) | |||
10849 | DiagnoseAlwaysNonNullPointer(LHS.get(), RHSNullKind, IsEquality, | |||
10850 | RHS.get()->getSourceRange()); | |||
10851 | else | |||
10852 | DiagnoseAlwaysNonNullPointer(RHS.get(), LHSNullKind, IsEquality, | |||
10853 | LHS.get()->getSourceRange()); | |||
10854 | } | |||
10855 | ||||
10856 | if ((LHSType->isIntegerType() && !LHSIsNull) || | |||
10857 | (RHSType->isIntegerType() && !RHSIsNull)) { | |||
10858 | // Skip normal pointer conversion checks in this case; we have better | |||
10859 | // diagnostics for this below. | |||
10860 | } else if (getLangOpts().CPlusPlus) { | |||
10861 | // Equality comparison of a function pointer to a void pointer is invalid, | |||
10862 | // but we allow it as an extension. | |||
10863 | // FIXME: If we really want to allow this, should it be part of composite | |||
10864 | // pointer type computation so it works in conditionals too? | |||
10865 | if (!IsOrdered && | |||
10866 | ((LHSType->isFunctionPointerType() && RHSType->isVoidPointerType()) || | |||
10867 | (RHSType->isFunctionPointerType() && LHSType->isVoidPointerType()))) { | |||
10868 | // This is a gcc extension compatibility comparison. | |||
10869 | // In a SFINAE context, we treat this as a hard error to maintain | |||
10870 | // conformance with the C++ standard. | |||
10871 | diagnoseFunctionPointerToVoidComparison( | |||
10872 | *this, Loc, LHS, RHS, /*isError*/ (bool)isSFINAEContext()); | |||
10873 | ||||
10874 | if (isSFINAEContext()) | |||
10875 | return QualType(); | |||
10876 | ||||
10877 | RHS = ImpCastExprToType(RHS.get(), LHSType, CK_BitCast); | |||
10878 | return computeResultTy(); | |||
10879 | } | |||
10880 | ||||
10881 | // C++ [expr.eq]p2: | |||
10882 | // If at least one operand is a pointer [...] bring them to their | |||
10883 | // composite pointer type. | |||
10884 | // C++ [expr.spaceship]p6 | |||
10885 | // If at least one of the operands is of pointer type, [...] bring them | |||
10886 | // to their composite pointer type. | |||
10887 | // C++ [expr.rel]p2: | |||
10888 | // If both operands are pointers, [...] bring them to their composite | |||
10889 | // pointer type. | |||
10890 | // For <=>, the only valid non-pointer types are arrays and functions, and | |||
10891 | // we already decayed those, so this is really the same as the relational | |||
10892 | // comparison rule. | |||
10893 | if ((int)LHSType->isPointerType() + (int)RHSType->isPointerType() >= | |||
10894 | (IsOrdered ? 2 : 1) && | |||
10895 | (!LangOpts.ObjCAutoRefCount || !(LHSType->isObjCObjectPointerType() || | |||
10896 | RHSType->isObjCObjectPointerType()))) { | |||
10897 | if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) | |||
10898 | return QualType(); | |||
10899 | return computeResultTy(); | |||
10900 | } | |||
10901 | } else if (LHSType->isPointerType() && | |||
10902 | RHSType->isPointerType()) { // C99 6.5.8p2 | |||
10903 | // All of the following pointer-related warnings are GCC extensions, except | |||
10904 | // when handling null pointer constants. | |||
10905 | QualType LCanPointeeTy = | |||
10906 | LHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); | |||
10907 | QualType RCanPointeeTy = | |||
10908 | RHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); | |||
10909 | ||||
10910 | // C99 6.5.9p2 and C99 6.5.8p2 | |||
10911 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), | |||
10912 | RCanPointeeTy.getUnqualifiedType())) { | |||
10913 | // Valid unless a relational comparison of function pointers | |||
10914 | if (IsRelational && LCanPointeeTy->isFunctionType()) { | |||
10915 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) | |||
10916 | << LHSType << RHSType << LHS.get()->getSourceRange() | |||
10917 | << RHS.get()->getSourceRange(); | |||
10918 | } | |||
10919 | } else if (!IsRelational && | |||
10920 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { | |||
10921 | // Valid unless comparison between non-null pointer and function pointer | |||
10922 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) | |||
10923 | && !LHSIsNull && !RHSIsNull) | |||
10924 | diagnoseFunctionPointerToVoidComparison(*this, Loc, LHS, RHS, | |||
10925 | /*isError*/false); | |||
10926 | } else { | |||
10927 | // Invalid | |||
10928 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, /*isError*/false); | |||
10929 | } | |||
10930 | if (LCanPointeeTy != RCanPointeeTy) { | |||
10931 | // Treat NULL constant as a special case in OpenCL. | |||
10932 | if (getLangOpts().OpenCL && !LHSIsNull && !RHSIsNull) { | |||
10933 | const PointerType *LHSPtr = LHSType->castAs<PointerType>(); | |||
10934 | if (!LHSPtr->isAddressSpaceOverlapping(*RHSType->castAs<PointerType>())) { | |||
10935 | Diag(Loc, | |||
10936 | diag::err_typecheck_op_on_nonoverlapping_address_space_pointers) | |||
10937 | << LHSType << RHSType << 0 /* comparison */ | |||
10938 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | |||
10939 | } | |||
10940 | } | |||
10941 | LangAS AddrSpaceL = LCanPointeeTy.getAddressSpace(); | |||
10942 | LangAS AddrSpaceR = RCanPointeeTy.getAddressSpace(); | |||
10943 | CastKind Kind = AddrSpaceL != AddrSpaceR ? CK_AddressSpaceConversion | |||
10944 | : CK_BitCast; | |||
10945 | if (LHSIsNull && !RHSIsNull) | |||
10946 | LHS = ImpCastExprToType(LHS.get(), RHSType, Kind); | |||
10947 | else | |||
10948 | RHS = ImpCastExprToType(RHS.get(), LHSType, Kind); | |||
10949 | } | |||
10950 | return computeResultTy(); | |||
10951 | } | |||
10952 | ||||
10953 | if (getLangOpts().CPlusPlus) { | |||
10954 | // C++ [expr.eq]p4: | |||
10955 | // Two operands of type std::nullptr_t or one operand of type | |||
10956 | // std::nullptr_t and the other a null pointer constant compare equal. | |||
10957 | if (!IsOrdered && LHSIsNull && RHSIsNull) { | |||
10958 | if (LHSType->isNullPtrType()) { | |||
10959 | RHS = ImpCastExprToType(RHS.get(), LHSType, CK_NullToPointer); | |||
10960 | return computeResultTy(); | |||
10961 | } | |||
10962 | if (RHSType->isNullPtrType()) { | |||
10963 | LHS = ImpCastExprToType(LHS.get(), RHSType, CK_NullToPointer); | |||
10964 | return computeResultTy(); | |||
10965 | } | |||
10966 | } | |||
10967 | ||||
10968 | // Comparison of Objective-C pointers and block pointers against nullptr_t. | |||
10969 | // These aren't covered by the composite pointer type rules. | |||
10970 | if (!IsOrdered && RHSType->isNullPtrType() && | |||
10971 | (LHSType->isObjCObjectPointerType() || LHSType->isBlockPointerType())) { | |||
10972 | RHS = ImpCastExprToType(RHS.get(), LHSType, CK_NullToPointer); | |||
10973 | return computeResultTy(); | |||
10974 | } | |||
10975 | if (!IsOrdered && LHSType->isNullPtrType() && | |||
10976 | (RHSType->isObjCObjectPointerType() || RHSType->isBlockPointerType())) { | |||
10977 | LHS = ImpCastExprToType(LHS.get(), RHSType, CK_NullToPointer); | |||
10978 | return computeResultTy(); | |||
10979 | } | |||
10980 | ||||
10981 | if (IsRelational && | |||
10982 | ((LHSType->isNullPtrType() && RHSType->isPointerType()) || | |||
10983 | (RHSType->isNullPtrType() && LHSType->isPointerType()))) { | |||
10984 | // HACK: Relational comparison of nullptr_t against a pointer type is | |||
10985 | // invalid per DR583, but we allow it within std::less<> and friends, | |||
10986 | // since otherwise common uses of it break. | |||
10987 | // FIXME: Consider removing this hack once LWG fixes std::less<> and | |||
10988 | // friends to have std::nullptr_t overload candidates. | |||
10989 | DeclContext *DC = CurContext; | |||
10990 | if (isa<FunctionDecl>(DC)) | |||
10991 | DC = DC->getParent(); | |||
10992 | if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { | |||
10993 | if (CTSD->isInStdNamespace() && | |||
10994 | llvm::StringSwitch<bool>(CTSD->getName()) | |||
10995 | .Cases("less", "less_equal", "greater", "greater_equal", true) | |||
10996 | .Default(false)) { | |||
10997 | if (RHSType->isNullPtrType()) | |||
10998 | RHS = ImpCastExprToType(RHS.get(), LHSType, CK_NullToPointer); | |||
10999 | else | |||
11000 | LHS = ImpCastExprToType(LHS.get(), RHSType, CK_NullToPointer); | |||
11001 | return computeResultTy(); | |||
11002 | } | |||
11003 | } | |||
11004 | } | |||
11005 | ||||
11006 | // C++ [expr.eq]p2: | |||
11007 | // If at least one operand is a pointer to member, [...] bring them to | |||
11008 | // their composite pointer type. | |||
11009 | if (!IsOrdered && | |||
11010 | (LHSType->isMemberPointerType() || RHSType->isMemberPointerType())) { | |||
11011 | if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) | |||
11012 | return QualType(); | |||
11013 | else | |||
11014 | return computeResultTy(); | |||
11015 | } | |||
11016 | } | |||
11017 | ||||
11018 | // Handle block pointer types. | |||
11019 | if (!IsOrdered && LHSType->isBlockPointerType() && | |||
11020 | RHSType->isBlockPointerType()) { | |||
11021 | QualType lpointee = LHSType->castAs<BlockPointerType>()->getPointeeType(); | |||
11022 | QualType rpointee = RHSType->castAs<BlockPointerType>()->getPointeeType(); | |||
11023 | ||||
11024 | if (!LHSIsNull && !RHSIsNull && | |||
11025 | !Context.typesAreCompatible(lpointee, rpointee)) { | |||
11026 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) | |||
11027 | << LHSType << RHSType << LHS.get()->getSourceRange() | |||
11028 | << RHS.get()->getSourceRange(); | |||
11029 | } | |||
11030 | RHS = ImpCastExprToType(RHS.get(), LHSType, CK_BitCast); | |||
11031 | return computeResultTy(); | |||
11032 | } | |||
11033 | ||||
11034 | // Allow block pointers to be compared with null pointer constants. | |||
11035 | if (!IsOrdered | |||
11036 | && ((LHSType->isBlockPointerType() && RHSType->isPointerType()) | |||
11037 | || (LHSType->isPointerType() && RHSType->isBlockPointerType()))) { | |||
11038 | if (!LHSIsNull && !RHSIsNull) { | |||
11039 | if (!((RHSType->isPointerType() && RHSType->castAs<PointerType>() | |||
11040 | ->getPointeeType()->isVoidType()) | |||
11041 | || (LHSType->isPointerType() && LHSType->castAs<PointerType>() | |||
11042 | ->getPointeeType()->isVoidType()))) | |||
11043 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) | |||
11044 | << LHSType << RHSType << LHS.get()->getSourceRange() | |||
11045 | << RHS.get()->getSourceRange(); | |||
11046 | } | |||
11047 | if (LHSIsNull && !RHSIsNull) | |||
11048 | LHS = ImpCastExprToType(LHS.get(), RHSType, | |||
11049 | RHSType->isPointerType() ? CK_BitCast | |||
11050 | : CK_AnyPointerToBlockPointerCast); | |||
11051 | else | |||
11052 | RHS = ImpCastExprToType(RHS.get(), LHSType, | |||
11053 | LHSType->isPointerType() ? CK_BitCast | |||
11054 | : CK_AnyPointerToBlockPointerCast); | |||
11055 | return computeResultTy(); | |||
11056 | } | |||
11057 | ||||
11058 | if (LHSType->isObjCObjectPointerType() || | |||
11059 | RHSType->isObjCObjectPointerType()) { | |||
11060 | const PointerType *LPT = LHSType->getAs<PointerType>(); | |||
11061 | const PointerType *RPT = RHSType->getAs<PointerType>(); | |||
11062 | if (LPT || RPT) { | |||
11063 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; | |||
11064 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; | |||
11065 | ||||
11066 | if (!LPtrToVoid && !RPtrToVoid && | |||
11067 | !Context.typesAreCompatible(LHSType, RHSType)) { | |||
11068 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, | |||
11069 | /*isError*/false); | |||
11070 | } | |||
11071 | // FIXME: If LPtrToVoid, we should presumably convert the LHS rather than | |||
11072 | // the RHS, but we have test coverage for this behavior. | |||
11073 | // FIXME: Consider using convertPointersToCompositeType in C++. | |||
11074 | if (LHSIsNull && !RHSIsNull) { | |||
11075 | Expr *E = LHS.get(); | |||
11076 | if (getLangOpts().ObjCAutoRefCount) | |||
11077 | CheckObjCConversion(SourceRange(), RHSType, E, | |||
11078 | CCK_ImplicitConversion); | |||
11079 | LHS = ImpCastExprToType(E, RHSType, | |||
11080 | RPT ? CK_BitCast :CK_CPointerToObjCPointerCast); | |||
11081 | } | |||
11082 | else { | |||
11083 | Expr *E = RHS.get(); | |||
11084 | if (getLangOpts().ObjCAutoRefCount) | |||
11085 | CheckObjCConversion(SourceRange(), LHSType, E, CCK_ImplicitConversion, | |||
11086 | /*Diagnose=*/true, | |||
11087 | /*DiagnoseCFAudited=*/false, Opc); | |||
11088 | RHS = ImpCastExprToType(E, LHSType, | |||
11089 | LPT ? CK_BitCast :CK_CPointerToObjCPointerCast); | |||
11090 | } | |||
11091 | return computeResultTy(); | |||
11092 | } | |||
11093 | if (LHSType->isObjCObjectPointerType() && | |||
11094 | RHSType->isObjCObjectPointerType()) { | |||
11095 | if (!Context.areComparableObjCPointerTypes(LHSType, RHSType)) | |||
11096 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, | |||
11097 | /*isError*/false); | |||
11098 | if (isObjCObjectLiteral(LHS) || isObjCObjectLiteral(RHS)) | |||
11099 | diagnoseObjCLiteralComparison(*this, Loc, LHS, RHS, Opc); | |||
11100 | ||||
11101 | if (LHSIsNull && !RHSIsNull) | |||
11102 | LHS = ImpCastExprToType(LHS.get(), RHSType, CK_BitCast); | |||
11103 | else | |||
11104 | RHS = ImpCastExprToType(RHS.get(), LHSType, CK_BitCast); | |||
11105 | return computeResultTy(); | |||
11106 | } | |||
11107 | ||||
11108 | if (!IsOrdered && LHSType->isBlockPointerType() && | |||
11109 | RHSType->isBlockCompatibleObjCPointerType(Context)) { | |||
11110 | LHS = ImpCastExprToType(LHS.get(), RHSType, | |||
11111 | CK_BlockPointerToObjCPointerCast); | |||
11112 | return computeResultTy(); | |||
11113 | } else if (!IsOrdered && | |||
11114 | LHSType->isBlockCompatibleObjCPointerType(Context) && | |||
11115 | RHSType->isBlockPointerType()) { | |||
11116 | RHS = ImpCastExprToType(RHS.get(), LHSType, | |||
11117 | CK_BlockPointerToObjCPointerCast); | |||
11118 | return computeResultTy(); | |||
11119 | } | |||
11120 | } | |||
11121 | if ((LHSType->isAnyPointerType() && RHSType->isIntegerType()) || | |||
11122 | (LHSType->isIntegerType() && RHSType->isAnyPointerType())) { | |||
11123 | unsigned DiagID = 0; | |||
11124 | bool isError = false; | |||
11125 | if (LangOpts.DebuggerSupport) { | |||
11126 | // Under a debugger, allow the comparison of pointers to integers, | |||
11127 | // since users tend to want to compare addresses. | |||
11128 | } else if ((LHSIsNull && LHSType->isIntegerType()) || | |||
11129 | (RHSIsNull && RHSType->isIntegerType())) { | |||
11130 | if (IsOrdered) { | |||
11131 | isError = getLangOpts().CPlusPlus; | |||
11132 | DiagID = | |||
11133 | isError ? diag::err_typecheck_ordered_comparison_of_pointer_and_zero | |||
11134 | : diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; | |||
11135 | } | |||
11136 | } else if (getLangOpts().CPlusPlus) { | |||
11137 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; | |||
11138 | isError = true; | |||
11139 | } else if (IsOrdered) | |||
11140 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; | |||
11141 | else | |||
11142 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; | |||
11143 | ||||
11144 | if (DiagID) { | |||
11145 | Diag(Loc, DiagID) | |||
11146 | << LHSType << RHSType << LHS.get()->getSourceRange() | |||
11147 | << RHS.get()->getSourceRange(); | |||
11148 | if (isError) | |||
11149 | return QualType(); | |||
11150 | } | |||
11151 | ||||
11152 | if (LHSType->isIntegerType()) | |||
11153 | LHS = ImpCastExprToType(LHS.get(), RHSType, | |||
11154 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); | |||
11155 | else | |||
11156 | RHS = ImpCastExprToType(RHS.get(), LHSType, | |||
11157 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); | |||
11158 | return computeResultTy(); | |||
11159 | } | |||
11160 | ||||
11161 | // Handle block pointers. | |||
11162 | if (!IsOrdered && RHSIsNull | |||
11163 | && LHSType->isBlockPointerType() && RHSType->isIntegerType()) { | |||
11164 | RHS = ImpCastExprToType(RHS.get(), LHSType, CK_NullToPointer); | |||
11165 | return computeResultTy(); | |||
11166 | } | |||
11167 | if (!IsOrdered && LHSIsNull | |||
11168 | && LHSType->isIntegerType() && RHSType->isBlockPointerType()) { | |||
11169 | LHS = ImpCastExprToType(LHS.get(), RHSType, CK_NullToPointer); | |||
11170 | return computeResultTy(); | |||
11171 | } | |||
11172 | ||||
11173 | if (getLangOpts().OpenCLVersion >= 200 || getLangOpts().OpenCLCPlusPlus) { | |||
11174 | if (LHSType->isClkEventT() && RHSType->isClkEventT()) { | |||
11175 | return computeResultTy(); | |||
11176 | } | |||
11177 | ||||
11178 | if (LHSType->isQueueT() && RHSType->isQueueT()) { | |||
11179 | return computeResultTy(); | |||
11180 | } | |||
11181 | ||||
11182 | if (LHSIsNull && RHSType->isQueueT()) { | |||
11183 | LHS = ImpCastExprToType(LHS.get(), RHSType, CK_NullToPointer); | |||
11184 | return computeResultTy(); | |||
11185 | } | |||
11186 | ||||
11187 | if (LHSType->isQueueT() && RHSIsNull) { | |||
11188 | RHS = ImpCastExprToType(RHS.get(), LHSType, CK_NullToPointer); | |||
11189 | return computeResultTy(); | |||
11190 | } | |||
11191 | } | |||
11192 | ||||
11193 | return InvalidOperands(Loc, LHS, RHS); | |||
11194 | } | |||
11195 | ||||
11196 | // Return a signed ext_vector_type that is of identical size and number of | |||
11197 | // elements. For floating point vectors, return an integer type of identical | |||
11198 | // size and number of elements. In the non ext_vector_type case, search from | |||
11199 | // the largest type to the smallest type to avoid cases where long long == long, | |||
11200 | // where long gets picked over long long. | |||
11201 | QualType Sema::GetSignedVectorType(QualType V) { | |||
11202 | const VectorType *VTy = V->castAs<VectorType>(); | |||
11203 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); | |||
11204 | ||||
11205 | if (isa<ExtVectorType>(VTy)) { | |||
11206 | if (TypeSize == Context.getTypeSize(Context.CharTy)) | |||
11207 | return Context.getExtVectorType(Context.CharTy, VTy->getNumElements()); | |||
11208 | else if (TypeSize == Context.getTypeSize(Context.ShortTy)) | |||
11209 | return Context.getExtVectorType(Context.ShortTy, VTy->getNumElements()); | |||
11210 | else if (TypeSize == Context.getTypeSize(Context.IntTy)) | |||
11211 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); | |||
11212 | else if (TypeSize == Context.getTypeSize(Context.LongTy)) | |||
11213 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); | |||
11214 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) &&((TypeSize == Context.getTypeSize(Context.LongLongTy) && "Unhandled vector element size in vector compare") ? static_cast <void> (0) : __assert_fail ("TypeSize == Context.getTypeSize(Context.LongLongTy) && \"Unhandled vector element size in vector compare\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 11215, __PRETTY_FUNCTION__)) | |||
11215 | "Unhandled vector element size in vector compare")((TypeSize == Context.getTypeSize(Context.LongLongTy) && "Unhandled vector element size in vector compare") ? static_cast <void> (0) : __assert_fail ("TypeSize == Context.getTypeSize(Context.LongLongTy) && \"Unhandled vector element size in vector compare\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 11215, __PRETTY_FUNCTION__)); | |||
11216 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); | |||
11217 | } | |||
11218 | ||||
11219 | if (TypeSize == Context.getTypeSize(Context.LongLongTy)) | |||
11220 | return Context.getVectorType(Context.LongLongTy, VTy->getNumElements(), | |||
11221 | VectorType::GenericVector); | |||
11222 | else if (TypeSize == Context.getTypeSize(Context.LongTy)) | |||
11223 | return Context.getVectorType(Context.LongTy, VTy->getNumElements(), | |||
11224 | VectorType::GenericVector); | |||
11225 | else if (TypeSize == Context.getTypeSize(Context.IntTy)) | |||
11226 | return Context.getVectorType(Context.IntTy, VTy->getNumElements(), | |||
11227 | VectorType::GenericVector); | |||
11228 | else if (TypeSize == Context.getTypeSize(Context.ShortTy)) | |||
11229 | return Context.getVectorType(Context.ShortTy, VTy->getNumElements(), | |||
11230 | VectorType::GenericVector); | |||
11231 | assert(TypeSize == Context.getTypeSize(Context.CharTy) &&((TypeSize == Context.getTypeSize(Context.CharTy) && "Unhandled vector element size in vector compare" ) ? static_cast<void> (0) : __assert_fail ("TypeSize == Context.getTypeSize(Context.CharTy) && \"Unhandled vector element size in vector compare\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 11232, __PRETTY_FUNCTION__)) | |||
11232 | "Unhandled vector element size in vector compare")((TypeSize == Context.getTypeSize(Context.CharTy) && "Unhandled vector element size in vector compare" ) ? static_cast<void> (0) : __assert_fail ("TypeSize == Context.getTypeSize(Context.CharTy) && \"Unhandled vector element size in vector compare\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 11232, __PRETTY_FUNCTION__)); | |||
11233 | return Context.getVectorType(Context.CharTy, VTy->getNumElements(), | |||
11234 | VectorType::GenericVector); | |||
11235 | } | |||
11236 | ||||
11237 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that | |||
11238 | /// operates on extended vector types. Instead of producing an IntTy result, | |||
11239 | /// like a scalar comparison, a vector comparison produces a vector of integer | |||
11240 | /// types. | |||
11241 | QualType Sema::CheckVectorCompareOperands(ExprResult &LHS, ExprResult &RHS, | |||
11242 | SourceLocation Loc, | |||
11243 | BinaryOperatorKind Opc) { | |||
11244 | if (Opc == BO_Cmp) { | |||
11245 | Diag(Loc, diag::err_three_way_vector_comparison); | |||
11246 | return QualType(); | |||
11247 | } | |||
11248 | ||||
11249 | // Check to make sure we're operating on vectors of the same type and width, | |||
11250 | // Allowing one side to be a scalar of element type. | |||
11251 | QualType vType = CheckVectorOperands(LHS, RHS, Loc, /*isCompAssign*/false, | |||
11252 | /*AllowBothBool*/true, | |||
11253 | /*AllowBoolConversions*/getLangOpts().ZVector); | |||
11254 | if (vType.isNull()) | |||
11255 | return vType; | |||
11256 | ||||
11257 | QualType LHSType = LHS.get()->getType(); | |||
11258 | ||||
11259 | // If AltiVec, the comparison results in a numeric type, i.e. | |||
11260 | // bool for C++, int for C | |||
11261 | if (getLangOpts().AltiVec && | |||
11262 | vType->castAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) | |||
11263 | return Context.getLogicalOperationType(); | |||
11264 | ||||
11265 | // For non-floating point types, check for self-comparisons of the form | |||
11266 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and | |||
11267 | // often indicate logic errors in the program. | |||
11268 | diagnoseTautologicalComparison(*this, Loc, LHS.get(), RHS.get(), Opc); | |||
11269 | ||||
11270 | // Check for comparisons of floating point operands using != and ==. | |||
11271 | if (BinaryOperator::isEqualityOp(Opc) && | |||
11272 | LHSType->hasFloatingRepresentation()) { | |||
11273 | assert(RHS.get()->getType()->hasFloatingRepresentation())((RHS.get()->getType()->hasFloatingRepresentation()) ? static_cast <void> (0) : __assert_fail ("RHS.get()->getType()->hasFloatingRepresentation()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 11273, __PRETTY_FUNCTION__)); | |||
11274 | CheckFloatComparison(Loc, LHS.get(), RHS.get()); | |||
11275 | } | |||
11276 | ||||
11277 | // Return a signed type for the vector. | |||
11278 | return GetSignedVectorType(vType); | |||
11279 | } | |||
11280 | ||||
11281 | static void diagnoseXorMisusedAsPow(Sema &S, const ExprResult &XorLHS, | |||
11282 | const ExprResult &XorRHS, | |||
11283 | const SourceLocation Loc) { | |||
11284 | // Do not diagnose macros. | |||
11285 | if (Loc.isMacroID()) | |||
11286 | return; | |||
11287 | ||||
11288 | bool Negative = false; | |||
11289 | bool ExplicitPlus = false; | |||
11290 | const auto *LHSInt = dyn_cast<IntegerLiteral>(XorLHS.get()); | |||
11291 | const auto *RHSInt = dyn_cast<IntegerLiteral>(XorRHS.get()); | |||
11292 | ||||
11293 | if (!LHSInt) | |||
11294 | return; | |||
11295 | if (!RHSInt) { | |||
11296 | // Check negative literals. | |||
11297 | if (const auto *UO = dyn_cast<UnaryOperator>(XorRHS.get())) { | |||
11298 | UnaryOperatorKind Opc = UO->getOpcode(); | |||
11299 | if (Opc != UO_Minus && Opc != UO_Plus) | |||
11300 | return; | |||
11301 | RHSInt = dyn_cast<IntegerLiteral>(UO->getSubExpr()); | |||
11302 | if (!RHSInt) | |||
11303 | return; | |||
11304 | Negative = (Opc == UO_Minus); | |||
11305 | ExplicitPlus = !Negative; | |||
11306 | } else { | |||
11307 | return; | |||
11308 | } | |||
11309 | } | |||
11310 | ||||
11311 | const llvm::APInt &LeftSideValue = LHSInt->getValue(); | |||
11312 | llvm::APInt RightSideValue = RHSInt->getValue(); | |||
11313 | if (LeftSideValue != 2 && LeftSideValue != 10) | |||
11314 | return; | |||
11315 | ||||
11316 | if (LeftSideValue.getBitWidth() != RightSideValue.getBitWidth()) | |||
11317 | return; | |||
11318 | ||||
11319 | CharSourceRange ExprRange = CharSourceRange::getCharRange( | |||
11320 | LHSInt->getBeginLoc(), S.getLocForEndOfToken(RHSInt->getLocation())); | |||
11321 | llvm::StringRef ExprStr = | |||
11322 | Lexer::getSourceText(ExprRange, S.getSourceManager(), S.getLangOpts()); | |||
11323 | ||||
11324 | CharSourceRange XorRange = | |||
11325 | CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc)); | |||
11326 | llvm::StringRef XorStr = | |||
11327 | Lexer::getSourceText(XorRange, S.getSourceManager(), S.getLangOpts()); | |||
11328 | // Do not diagnose if xor keyword/macro is used. | |||
11329 | if (XorStr == "xor") | |||
11330 | return; | |||
11331 | ||||
11332 | std::string LHSStr = std::string(Lexer::getSourceText( | |||
11333 | CharSourceRange::getTokenRange(LHSInt->getSourceRange()), | |||
11334 | S.getSourceManager(), S.getLangOpts())); | |||
11335 | std::string RHSStr = std::string(Lexer::getSourceText( | |||
11336 | CharSourceRange::getTokenRange(RHSInt->getSourceRange()), | |||
11337 | S.getSourceManager(), S.getLangOpts())); | |||
11338 | ||||
11339 | if (Negative) { | |||
11340 | RightSideValue = -RightSideValue; | |||
11341 | RHSStr = "-" + RHSStr; | |||
11342 | } else if (ExplicitPlus) { | |||
11343 | RHSStr = "+" + RHSStr; | |||
11344 | } | |||
11345 | ||||
11346 | StringRef LHSStrRef = LHSStr; | |||
11347 | StringRef RHSStrRef = RHSStr; | |||
11348 | // Do not diagnose literals with digit separators, binary, hexadecimal, octal | |||
11349 | // literals. | |||
11350 | if (LHSStrRef.startswith("0b") || LHSStrRef.startswith("0B") || | |||
11351 | RHSStrRef.startswith("0b") || RHSStrRef.startswith("0B") || | |||
11352 | LHSStrRef.startswith("0x") || LHSStrRef.startswith("0X") || | |||
11353 | RHSStrRef.startswith("0x") || RHSStrRef.startswith("0X") || | |||
11354 | (LHSStrRef.size() > 1 && LHSStrRef.startswith("0")) || | |||
11355 | (RHSStrRef.size() > 1 && RHSStrRef.startswith("0")) || | |||
11356 | LHSStrRef.find('\'') != StringRef::npos || | |||
11357 | RHSStrRef.find('\'') != StringRef::npos) | |||
11358 | return; | |||
11359 | ||||
11360 | bool SuggestXor = S.getLangOpts().CPlusPlus || S.getPreprocessor().isMacroDefined("xor"); | |||
11361 | const llvm::APInt XorValue = LeftSideValue ^ RightSideValue; | |||
11362 | int64_t RightSideIntValue = RightSideValue.getSExtValue(); | |||
11363 | if (LeftSideValue == 2 && RightSideIntValue >= 0) { | |||
11364 | std::string SuggestedExpr = "1 << " + RHSStr; | |||
11365 | bool Overflow = false; | |||
11366 | llvm::APInt One = (LeftSideValue - 1); | |||
11367 | llvm::APInt PowValue = One.sshl_ov(RightSideValue, Overflow); | |||
11368 | if (Overflow) { | |||
11369 | if (RightSideIntValue < 64) | |||
11370 | S.Diag(Loc, diag::warn_xor_used_as_pow_base) | |||
11371 | << ExprStr << XorValue.toString(10, true) << ("1LL << " + RHSStr) | |||
11372 | << FixItHint::CreateReplacement(ExprRange, "1LL << " + RHSStr); | |||
11373 | else if (RightSideIntValue == 64) | |||
11374 | S.Diag(Loc, diag::warn_xor_used_as_pow) << ExprStr << XorValue.toString(10, true); | |||
11375 | else | |||
11376 | return; | |||
11377 | } else { | |||
11378 | S.Diag(Loc, diag::warn_xor_used_as_pow_base_extra) | |||
11379 | << ExprStr << XorValue.toString(10, true) << SuggestedExpr | |||
11380 | << PowValue.toString(10, true) | |||
11381 | << FixItHint::CreateReplacement( | |||
11382 | ExprRange, (RightSideIntValue == 0) ? "1" : SuggestedExpr); | |||
11383 | } | |||
11384 | ||||
11385 | S.Diag(Loc, diag::note_xor_used_as_pow_silence) << ("0x2 ^ " + RHSStr) << SuggestXor; | |||
11386 | } else if (LeftSideValue == 10) { | |||
11387 | std::string SuggestedValue = "1e" + std::to_string(RightSideIntValue); | |||
11388 | S.Diag(Loc, diag::warn_xor_used_as_pow_base) | |||
11389 | << ExprStr << XorValue.toString(10, true) << SuggestedValue | |||
11390 | << FixItHint::CreateReplacement(ExprRange, SuggestedValue); | |||
11391 | S.Diag(Loc, diag::note_xor_used_as_pow_silence) << ("0xA ^ " + RHSStr) << SuggestXor; | |||
11392 | } | |||
11393 | } | |||
11394 | ||||
11395 | QualType Sema::CheckVectorLogicalOperands(ExprResult &LHS, ExprResult &RHS, | |||
11396 | SourceLocation Loc) { | |||
11397 | // Ensure that either both operands are of the same vector type, or | |||
11398 | // one operand is of a vector type and the other is of its element type. | |||
11399 | QualType vType = CheckVectorOperands(LHS, RHS, Loc, false, | |||
11400 | /*AllowBothBool*/true, | |||
11401 | /*AllowBoolConversions*/false); | |||
11402 | if (vType.isNull()) | |||
11403 | return InvalidOperands(Loc, LHS, RHS); | |||
11404 | if (getLangOpts().OpenCL && getLangOpts().OpenCLVersion < 120 && | |||
11405 | !getLangOpts().OpenCLCPlusPlus && vType->hasFloatingRepresentation()) | |||
11406 | return InvalidOperands(Loc, LHS, RHS); | |||
11407 | // FIXME: The check for C++ here is for GCC compatibility. GCC rejects the | |||
11408 | // usage of the logical operators && and || with vectors in C. This | |||
11409 | // check could be notionally dropped. | |||
11410 | if (!getLangOpts().CPlusPlus && | |||
11411 | !(isa<ExtVectorType>(vType->getAs<VectorType>()))) | |||
11412 | return InvalidLogicalVectorOperands(Loc, LHS, RHS); | |||
11413 | ||||
11414 | return GetSignedVectorType(LHS.get()->getType()); | |||
11415 | } | |||
11416 | ||||
11417 | inline QualType Sema::CheckBitwiseOperands(ExprResult &LHS, ExprResult &RHS, | |||
11418 | SourceLocation Loc, | |||
11419 | BinaryOperatorKind Opc) { | |||
11420 | checkArithmeticNull(*this, LHS, RHS, Loc, /*IsCompare=*/false); | |||
11421 | ||||
11422 | bool IsCompAssign = | |||
11423 | Opc == BO_AndAssign || Opc == BO_OrAssign || Opc == BO_XorAssign; | |||
11424 | ||||
11425 | if (LHS.get()->getType()->isVectorType() || | |||
11426 | RHS.get()->getType()->isVectorType()) { | |||
11427 | if (LHS.get()->getType()->hasIntegerRepresentation() && | |||
11428 | RHS.get()->getType()->hasIntegerRepresentation()) | |||
11429 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign, | |||
11430 | /*AllowBothBool*/true, | |||
11431 | /*AllowBoolConversions*/getLangOpts().ZVector); | |||
11432 | return InvalidOperands(Loc, LHS, RHS); | |||
11433 | } | |||
11434 | ||||
11435 | if (Opc == BO_And) | |||
11436 | diagnoseLogicalNotOnLHSofCheck(*this, LHS, RHS, Loc, Opc); | |||
11437 | ||||
11438 | if (LHS.get()->getType()->hasFloatingRepresentation() || | |||
11439 | RHS.get()->getType()->hasFloatingRepresentation()) | |||
11440 | return InvalidOperands(Loc, LHS, RHS); | |||
11441 | ||||
11442 | ExprResult LHSResult = LHS, RHSResult = RHS; | |||
11443 | QualType compType = UsualArithmeticConversions( | |||
11444 | LHSResult, RHSResult, Loc, IsCompAssign ? ACK_CompAssign : ACK_BitwiseOp); | |||
11445 | if (LHSResult.isInvalid() || RHSResult.isInvalid()) | |||
11446 | return QualType(); | |||
11447 | LHS = LHSResult.get(); | |||
11448 | RHS = RHSResult.get(); | |||
11449 | ||||
11450 | if (Opc == BO_Xor) | |||
11451 | diagnoseXorMisusedAsPow(*this, LHS, RHS, Loc); | |||
11452 | ||||
11453 | if (!compType.isNull() && compType->isIntegralOrUnscopedEnumerationType()) | |||
11454 | return compType; | |||
11455 | return InvalidOperands(Loc, LHS, RHS); | |||
11456 | } | |||
11457 | ||||
11458 | // C99 6.5.[13,14] | |||
11459 | inline QualType Sema::CheckLogicalOperands(ExprResult &LHS, ExprResult &RHS, | |||
11460 | SourceLocation Loc, | |||
11461 | BinaryOperatorKind Opc) { | |||
11462 | // Check vector operands differently. | |||
11463 | if (LHS.get()->getType()->isVectorType() || RHS.get()->getType()->isVectorType()) | |||
11464 | return CheckVectorLogicalOperands(LHS, RHS, Loc); | |||
11465 | ||||
11466 | bool EnumConstantInBoolContext = false; | |||
11467 | for (const ExprResult &HS : {LHS, RHS}) { | |||
11468 | if (const auto *DREHS = dyn_cast<DeclRefExpr>(HS.get())) { | |||
11469 | const auto *ECDHS = dyn_cast<EnumConstantDecl>(DREHS->getDecl()); | |||
11470 | if (ECDHS && ECDHS->getInitVal() != 0 && ECDHS->getInitVal() != 1) | |||
11471 | EnumConstantInBoolContext = true; | |||
11472 | } | |||
11473 | } | |||
11474 | ||||
11475 | if (EnumConstantInBoolContext) | |||
11476 | Diag(Loc, diag::warn_enum_constant_in_bool_context); | |||
11477 | ||||
11478 | // Diagnose cases where the user write a logical and/or but probably meant a | |||
11479 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS | |||
11480 | // is a constant. | |||
11481 | if (!EnumConstantInBoolContext && LHS.get()->getType()->isIntegerType() && | |||
11482 | !LHS.get()->getType()->isBooleanType() && | |||
11483 | RHS.get()->getType()->isIntegerType() && !RHS.get()->isValueDependent() && | |||
11484 | // Don't warn in macros or template instantiations. | |||
11485 | !Loc.isMacroID() && !inTemplateInstantiation()) { | |||
11486 | // If the RHS can be constant folded, and if it constant folds to something | |||
11487 | // that isn't 0 or 1 (which indicate a potential logical operation that | |||
11488 | // happened to fold to true/false) then warn. | |||
11489 | // Parens on the RHS are ignored. | |||
11490 | Expr::EvalResult EVResult; | |||
11491 | if (RHS.get()->EvaluateAsInt(EVResult, Context)) { | |||
11492 | llvm::APSInt Result = EVResult.Val.getInt(); | |||
11493 | if ((getLangOpts().Bool && !RHS.get()->getType()->isBooleanType() && | |||
11494 | !RHS.get()->getExprLoc().isMacroID()) || | |||
11495 | (Result != 0 && Result != 1)) { | |||
11496 | Diag(Loc, diag::warn_logical_instead_of_bitwise) | |||
11497 | << RHS.get()->getSourceRange() | |||
11498 | << (Opc == BO_LAnd ? "&&" : "||"); | |||
11499 | // Suggest replacing the logical operator with the bitwise version | |||
11500 | Diag(Loc, diag::note_logical_instead_of_bitwise_change_operator) | |||
11501 | << (Opc == BO_LAnd ? "&" : "|") | |||
11502 | << FixItHint::CreateReplacement(SourceRange( | |||
11503 | Loc, getLocForEndOfToken(Loc)), | |||
11504 | Opc == BO_LAnd ? "&" : "|"); | |||
11505 | if (Opc == BO_LAnd) | |||
11506 | // Suggest replacing "Foo() && kNonZero" with "Foo()" | |||
11507 | Diag(Loc, diag::note_logical_instead_of_bitwise_remove_constant) | |||
11508 | << FixItHint::CreateRemoval( | |||
11509 | SourceRange(getLocForEndOfToken(LHS.get()->getEndLoc()), | |||
11510 | RHS.get()->getEndLoc())); | |||
11511 | } | |||
11512 | } | |||
11513 | } | |||
11514 | ||||
11515 | if (!Context.getLangOpts().CPlusPlus) { | |||
11516 | // OpenCL v1.1 s6.3.g: The logical operators and (&&), or (||) do | |||
11517 | // not operate on the built-in scalar and vector float types. | |||
11518 | if (Context.getLangOpts().OpenCL && | |||
11519 | Context.getLangOpts().OpenCLVersion < 120) { | |||
11520 | if (LHS.get()->getType()->isFloatingType() || | |||
11521 | RHS.get()->getType()->isFloatingType()) | |||
11522 | return InvalidOperands(Loc, LHS, RHS); | |||
11523 | } | |||
11524 | ||||
11525 | LHS = UsualUnaryConversions(LHS.get()); | |||
11526 | if (LHS.isInvalid()) | |||
11527 | return QualType(); | |||
11528 | ||||
11529 | RHS = UsualUnaryConversions(RHS.get()); | |||
11530 | if (RHS.isInvalid()) | |||
11531 | return QualType(); | |||
11532 | ||||
11533 | if (!LHS.get()->getType()->isScalarType() || | |||
11534 | !RHS.get()->getType()->isScalarType()) | |||
11535 | return InvalidOperands(Loc, LHS, RHS); | |||
11536 | ||||
11537 | return Context.IntTy; | |||
11538 | } | |||
11539 | ||||
11540 | // The following is safe because we only use this method for | |||
11541 | // non-overloadable operands. | |||
11542 | ||||
11543 | // C++ [expr.log.and]p1 | |||
11544 | // C++ [expr.log.or]p1 | |||
11545 | // The operands are both contextually converted to type bool. | |||
11546 | ExprResult LHSRes = PerformContextuallyConvertToBool(LHS.get()); | |||
11547 | if (LHSRes.isInvalid()) | |||
11548 | return InvalidOperands(Loc, LHS, RHS); | |||
11549 | LHS = LHSRes; | |||
11550 | ||||
11551 | ExprResult RHSRes = PerformContextuallyConvertToBool(RHS.get()); | |||
11552 | if (RHSRes.isInvalid()) | |||
11553 | return InvalidOperands(Loc, LHS, RHS); | |||
11554 | RHS = RHSRes; | |||
11555 | ||||
11556 | // C++ [expr.log.and]p2 | |||
11557 | // C++ [expr.log.or]p2 | |||
11558 | // The result is a bool. | |||
11559 | return Context.BoolTy; | |||
11560 | } | |||
11561 | ||||
11562 | static bool IsReadonlyMessage(Expr *E, Sema &S) { | |||
11563 | const MemberExpr *ME = dyn_cast<MemberExpr>(E); | |||
11564 | if (!ME) return false; | |||
11565 | if (!isa<FieldDecl>(ME->getMemberDecl())) return false; | |||
11566 | ObjCMessageExpr *Base = dyn_cast<ObjCMessageExpr>( | |||
11567 | ME->getBase()->IgnoreImplicit()->IgnoreParenImpCasts()); | |||
11568 | if (!Base) return false; | |||
11569 | return Base->getMethodDecl() != nullptr; | |||
11570 | } | |||
11571 | ||||
11572 | /// Is the given expression (which must be 'const') a reference to a | |||
11573 | /// variable which was originally non-const, but which has become | |||
11574 | /// 'const' due to being captured within a block? | |||
11575 | enum NonConstCaptureKind { NCCK_None, NCCK_Block, NCCK_Lambda }; | |||
11576 | static NonConstCaptureKind isReferenceToNonConstCapture(Sema &S, Expr *E) { | |||
11577 | assert(E->isLValue() && E->getType().isConstQualified())((E->isLValue() && E->getType().isConstQualified ()) ? static_cast<void> (0) : __assert_fail ("E->isLValue() && E->getType().isConstQualified()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 11577, __PRETTY_FUNCTION__)); | |||
11578 | E = E->IgnoreParens(); | |||
11579 | ||||
11580 | // Must be a reference to a declaration from an enclosing scope. | |||
11581 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); | |||
11582 | if (!DRE) return NCCK_None; | |||
11583 | if (!DRE->refersToEnclosingVariableOrCapture()) return NCCK_None; | |||
11584 | ||||
11585 | // The declaration must be a variable which is not declared 'const'. | |||
11586 | VarDecl *var = dyn_cast<VarDecl>(DRE->getDecl()); | |||
11587 | if (!var) return NCCK_None; | |||
11588 | if (var->getType().isConstQualified()) return NCCK_None; | |||
11589 | assert(var->hasLocalStorage() && "capture added 'const' to non-local?")((var->hasLocalStorage() && "capture added 'const' to non-local?" ) ? static_cast<void> (0) : __assert_fail ("var->hasLocalStorage() && \"capture added 'const' to non-local?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 11589, __PRETTY_FUNCTION__)); | |||
11590 | ||||
11591 | // Decide whether the first capture was for a block or a lambda. | |||
11592 | DeclContext *DC = S.CurContext, *Prev = nullptr; | |||
11593 | // Decide whether the first capture was for a block or a lambda. | |||
11594 | while (DC) { | |||
11595 | // For init-capture, it is possible that the variable belongs to the | |||
11596 | // template pattern of the current context. | |||
11597 | if (auto *FD = dyn_cast<FunctionDecl>(DC)) | |||
11598 | if (var->isInitCapture() && | |||
11599 | FD->getTemplateInstantiationPattern() == var->getDeclContext()) | |||
11600 | break; | |||
11601 | if (DC == var->getDeclContext()) | |||
11602 | break; | |||
11603 | Prev = DC; | |||
11604 | DC = DC->getParent(); | |||
11605 | } | |||
11606 | // Unless we have an init-capture, we've gone one step too far. | |||
11607 | if (!var->isInitCapture()) | |||
11608 | DC = Prev; | |||
11609 | return (isa<BlockDecl>(DC) ? NCCK_Block : NCCK_Lambda); | |||
11610 | } | |||
11611 | ||||
11612 | static bool IsTypeModifiable(QualType Ty, bool IsDereference) { | |||
11613 | Ty = Ty.getNonReferenceType(); | |||
11614 | if (IsDereference && Ty->isPointerType()) | |||
11615 | Ty = Ty->getPointeeType(); | |||
11616 | return !Ty.isConstQualified(); | |||
11617 | } | |||
11618 | ||||
11619 | // Update err_typecheck_assign_const and note_typecheck_assign_const | |||
11620 | // when this enum is changed. | |||
11621 | enum { | |||
11622 | ConstFunction, | |||
11623 | ConstVariable, | |||
11624 | ConstMember, | |||
11625 | ConstMethod, | |||
11626 | NestedConstMember, | |||
11627 | ConstUnknown, // Keep as last element | |||
11628 | }; | |||
11629 | ||||
11630 | /// Emit the "read-only variable not assignable" error and print notes to give | |||
11631 | /// more information about why the variable is not assignable, such as pointing | |||
11632 | /// to the declaration of a const variable, showing that a method is const, or | |||
11633 | /// that the function is returning a const reference. | |||
11634 | static void DiagnoseConstAssignment(Sema &S, const Expr *E, | |||
11635 | SourceLocation Loc) { | |||
11636 | SourceRange ExprRange = E->getSourceRange(); | |||
11637 | ||||
11638 | // Only emit one error on the first const found. All other consts will emit | |||
11639 | // a note to the error. | |||
11640 | bool DiagnosticEmitted = false; | |||
11641 | ||||
11642 | // Track if the current expression is the result of a dereference, and if the | |||
11643 | // next checked expression is the result of a dereference. | |||
11644 | bool IsDereference = false; | |||
11645 | bool NextIsDereference = false; | |||
11646 | ||||
11647 | // Loop to process MemberExpr chains. | |||
11648 | while (true) { | |||
11649 | IsDereference = NextIsDereference; | |||
11650 | ||||
11651 | E = E->IgnoreImplicit()->IgnoreParenImpCasts(); | |||
11652 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { | |||
11653 | NextIsDereference = ME->isArrow(); | |||
11654 | const ValueDecl *VD = ME->getMemberDecl(); | |||
11655 | if (const FieldDecl *Field = dyn_cast<FieldDecl>(VD)) { | |||
11656 | // Mutable fields can be modified even if the class is const. | |||
11657 | if (Field->isMutable()) { | |||
11658 | assert(DiagnosticEmitted && "Expected diagnostic not emitted.")((DiagnosticEmitted && "Expected diagnostic not emitted." ) ? static_cast<void> (0) : __assert_fail ("DiagnosticEmitted && \"Expected diagnostic not emitted.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 11658, __PRETTY_FUNCTION__)); | |||
11659 | break; | |||
11660 | } | |||
11661 | ||||
11662 | if (!IsTypeModifiable(Field->getType(), IsDereference)) { | |||
11663 | if (!DiagnosticEmitted) { | |||
11664 | S.Diag(Loc, diag::err_typecheck_assign_const) | |||
11665 | << ExprRange << ConstMember << false /*static*/ << Field | |||
11666 | << Field->getType(); | |||
11667 | DiagnosticEmitted = true; | |||
11668 | } | |||
11669 | S.Diag(VD->getLocation(), diag::note_typecheck_assign_const) | |||
11670 | << ConstMember << false /*static*/ << Field << Field->getType() | |||
11671 | << Field->getSourceRange(); | |||
11672 | } | |||
11673 | E = ME->getBase(); | |||
11674 | continue; | |||
11675 | } else if (const VarDecl *VDecl = dyn_cast<VarDecl>(VD)) { | |||
11676 | if (VDecl->getType().isConstQualified()) { | |||
11677 | if (!DiagnosticEmitted) { | |||
11678 | S.Diag(Loc, diag::err_typecheck_assign_const) | |||
11679 | << ExprRange << ConstMember << true /*static*/ << VDecl | |||
11680 | << VDecl->getType(); | |||
11681 | DiagnosticEmitted = true; | |||
11682 | } | |||
11683 | S.Diag(VD->getLocation(), diag::note_typecheck_assign_const) | |||
11684 | << ConstMember << true /*static*/ << VDecl << VDecl->getType() | |||
11685 | << VDecl->getSourceRange(); | |||
11686 | } | |||
11687 | // Static fields do not inherit constness from parents. | |||
11688 | break; | |||
11689 | } | |||
11690 | break; // End MemberExpr | |||
11691 | } else if (const ArraySubscriptExpr *ASE = | |||
11692 | dyn_cast<ArraySubscriptExpr>(E)) { | |||
11693 | E = ASE->getBase()->IgnoreParenImpCasts(); | |||
11694 | continue; | |||
11695 | } else if (const ExtVectorElementExpr *EVE = | |||
11696 | dyn_cast<ExtVectorElementExpr>(E)) { | |||
11697 | E = EVE->getBase()->IgnoreParenImpCasts(); | |||
11698 | continue; | |||
11699 | } | |||
11700 | break; | |||
11701 | } | |||
11702 | ||||
11703 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { | |||
11704 | // Function calls | |||
11705 | const FunctionDecl *FD = CE->getDirectCallee(); | |||
11706 | if (FD && !IsTypeModifiable(FD->getReturnType(), IsDereference)) { | |||
11707 | if (!DiagnosticEmitted) { | |||
11708 | S.Diag(Loc, diag::err_typecheck_assign_const) << ExprRange | |||
11709 | << ConstFunction << FD; | |||
11710 | DiagnosticEmitted = true; | |||
11711 | } | |||
11712 | S.Diag(FD->getReturnTypeSourceRange().getBegin(), | |||
11713 | diag::note_typecheck_assign_const) | |||
11714 | << ConstFunction << FD << FD->getReturnType() | |||
11715 | << FD->getReturnTypeSourceRange(); | |||
11716 | } | |||
11717 | } else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { | |||
11718 | // Point to variable declaration. | |||
11719 | if (const ValueDecl *VD = DRE->getDecl()) { | |||
11720 | if (!IsTypeModifiable(VD->getType(), IsDereference)) { | |||
11721 | if (!DiagnosticEmitted) { | |||
11722 | S.Diag(Loc, diag::err_typecheck_assign_const) | |||
11723 | << ExprRange << ConstVariable << VD << VD->getType(); | |||
11724 | DiagnosticEmitted = true; | |||
11725 | } | |||
11726 | S.Diag(VD->getLocation(), diag::note_typecheck_assign_const) | |||
11727 | << ConstVariable << VD << VD->getType() << VD->getSourceRange(); | |||
11728 | } | |||
11729 | } | |||
11730 | } else if (isa<CXXThisExpr>(E)) { | |||
11731 | if (const DeclContext *DC = S.getFunctionLevelDeclContext()) { | |||
11732 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) { | |||
11733 | if (MD->isConst()) { | |||
11734 | if (!DiagnosticEmitted) { | |||
11735 | S.Diag(Loc, diag::err_typecheck_assign_const) << ExprRange | |||
11736 | << ConstMethod << MD; | |||
11737 | DiagnosticEmitted = true; | |||
11738 | } | |||
11739 | S.Diag(MD->getLocation(), diag::note_typecheck_assign_const) | |||
11740 | << ConstMethod << MD << MD->getSourceRange(); | |||
11741 | } | |||
11742 | } | |||
11743 | } | |||
11744 | } | |||
11745 | ||||
11746 | if (DiagnosticEmitted) | |||
11747 | return; | |||
11748 | ||||
11749 | // Can't determine a more specific message, so display the generic error. | |||
11750 | S.Diag(Loc, diag::err_typecheck_assign_const) << ExprRange << ConstUnknown; | |||
11751 | } | |||
11752 | ||||
11753 | enum OriginalExprKind { | |||
11754 | OEK_Variable, | |||
11755 | OEK_Member, | |||
11756 | OEK_LValue | |||
11757 | }; | |||
11758 | ||||
11759 | static void DiagnoseRecursiveConstFields(Sema &S, const ValueDecl *VD, | |||
11760 | const RecordType *Ty, | |||
11761 | SourceLocation Loc, SourceRange Range, | |||
11762 | OriginalExprKind OEK, | |||
11763 | bool &DiagnosticEmitted) { | |||
11764 | std::vector<const RecordType *> RecordTypeList; | |||
11765 | RecordTypeList.push_back(Ty); | |||
11766 | unsigned NextToCheckIndex = 0; | |||
11767 | // We walk the record hierarchy breadth-first to ensure that we print | |||
11768 | // diagnostics in field nesting order. | |||
11769 | while (RecordTypeList.size() > NextToCheckIndex) { | |||
11770 | bool IsNested = NextToCheckIndex > 0; | |||
11771 | for (const FieldDecl *Field : | |||
11772 | RecordTypeList[NextToCheckIndex]->getDecl()->fields()) { | |||
11773 | // First, check every field for constness. | |||
11774 | QualType FieldTy = Field->getType(); | |||
11775 | if (FieldTy.isConstQualified()) { | |||
11776 | if (!DiagnosticEmitted) { | |||
11777 | S.Diag(Loc, diag::err_typecheck_assign_const) | |||
11778 | << Range << NestedConstMember << OEK << VD | |||
11779 | << IsNested << Field; | |||
11780 | DiagnosticEmitted = true; | |||
11781 | } | |||
11782 | S.Diag(Field->getLocation(), diag::note_typecheck_assign_const) | |||
11783 | << NestedConstMember << IsNested << Field | |||
11784 | << FieldTy << Field->getSourceRange(); | |||
11785 | } | |||
11786 | ||||
11787 | // Then we append it to the list to check next in order. | |||
11788 | FieldTy = FieldTy.getCanonicalType(); | |||
11789 | if (const auto *FieldRecTy = FieldTy->getAs<RecordType>()) { | |||
11790 | if (llvm::find(RecordTypeList, FieldRecTy) == RecordTypeList.end()) | |||
11791 | RecordTypeList.push_back(FieldRecTy); | |||
11792 | } | |||
11793 | } | |||
11794 | ++NextToCheckIndex; | |||
11795 | } | |||
11796 | } | |||
11797 | ||||
11798 | /// Emit an error for the case where a record we are trying to assign to has a | |||
11799 | /// const-qualified field somewhere in its hierarchy. | |||
11800 | static void DiagnoseRecursiveConstFields(Sema &S, const Expr *E, | |||
11801 | SourceLocation Loc) { | |||
11802 | QualType Ty = E->getType(); | |||
11803 | assert(Ty->isRecordType() && "lvalue was not record?")((Ty->isRecordType() && "lvalue was not record?") ? static_cast<void> (0) : __assert_fail ("Ty->isRecordType() && \"lvalue was not record?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 11803, __PRETTY_FUNCTION__)); | |||
11804 | SourceRange Range = E->getSourceRange(); | |||
11805 | const RecordType *RTy = Ty.getCanonicalType()->getAs<RecordType>(); | |||
11806 | bool DiagEmitted = false; | |||
11807 | ||||
11808 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) | |||
11809 | DiagnoseRecursiveConstFields(S, ME->getMemberDecl(), RTy, Loc, | |||
11810 | Range, OEK_Member, DiagEmitted); | |||
11811 | else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) | |||
11812 | DiagnoseRecursiveConstFields(S, DRE->getDecl(), RTy, Loc, | |||
11813 | Range, OEK_Variable, DiagEmitted); | |||
11814 | else | |||
11815 | DiagnoseRecursiveConstFields(S, nullptr, RTy, Loc, | |||
11816 | Range, OEK_LValue, DiagEmitted); | |||
11817 | if (!DiagEmitted) | |||
11818 | DiagnoseConstAssignment(S, E, Loc); | |||
11819 | } | |||
11820 | ||||
11821 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, | |||
11822 | /// emit an error and return true. If so, return false. | |||
11823 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { | |||
11824 | assert(!E->hasPlaceholderType(BuiltinType::PseudoObject))((!E->hasPlaceholderType(BuiltinType::PseudoObject)) ? static_cast <void> (0) : __assert_fail ("!E->hasPlaceholderType(BuiltinType::PseudoObject)" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 11824, __PRETTY_FUNCTION__)); | |||
11825 | ||||
11826 | S.CheckShadowingDeclModification(E, Loc); | |||
11827 | ||||
11828 | SourceLocation OrigLoc = Loc; | |||
11829 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, | |||
11830 | &Loc); | |||
11831 | if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) | |||
11832 | IsLV = Expr::MLV_InvalidMessageExpression; | |||
11833 | if (IsLV == Expr::MLV_Valid) | |||
11834 | return false; | |||
11835 | ||||
11836 | unsigned DiagID = 0; | |||
11837 | bool NeedType = false; | |||
11838 | switch (IsLV) { // C99 6.5.16p2 | |||
11839 | case Expr::MLV_ConstQualified: | |||
11840 | // Use a specialized diagnostic when we're assigning to an object | |||
11841 | // from an enclosing function or block. | |||
11842 | if (NonConstCaptureKind NCCK = isReferenceToNonConstCapture(S, E)) { | |||
11843 | if (NCCK == NCCK_Block) | |||
11844 | DiagID = diag::err_block_decl_ref_not_modifiable_lvalue; | |||
11845 | else | |||
11846 | DiagID = diag::err_lambda_decl_ref_not_modifiable_lvalue; | |||
11847 | break; | |||
11848 | } | |||
11849 | ||||
11850 | // In ARC, use some specialized diagnostics for occasions where we | |||
11851 | // infer 'const'. These are always pseudo-strong variables. | |||
11852 | if (S.getLangOpts().ObjCAutoRefCount) { | |||
11853 | DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); | |||
11854 | if (declRef && isa<VarDecl>(declRef->getDecl())) { | |||
11855 | VarDecl *var = cast<VarDecl>(declRef->getDecl()); | |||
11856 | ||||
11857 | // Use the normal diagnostic if it's pseudo-__strong but the | |||
11858 | // user actually wrote 'const'. | |||
11859 | if (var->isARCPseudoStrong() && | |||
11860 | (!var->getTypeSourceInfo() || | |||
11861 | !var->getTypeSourceInfo()->getType().isConstQualified())) { | |||
11862 | // There are three pseudo-strong cases: | |||
11863 | // - self | |||
11864 | ObjCMethodDecl *method = S.getCurMethodDecl(); | |||
11865 | if (method && var == method->getSelfDecl()) { | |||
11866 | DiagID = method->isClassMethod() | |||
11867 | ? diag::err_typecheck_arc_assign_self_class_method | |||
11868 | : diag::err_typecheck_arc_assign_self; | |||
11869 | ||||
11870 | // - Objective-C externally_retained attribute. | |||
11871 | } else if (var->hasAttr<ObjCExternallyRetainedAttr>() || | |||
11872 | isa<ParmVarDecl>(var)) { | |||
11873 | DiagID = diag::err_typecheck_arc_assign_externally_retained; | |||
11874 | ||||
11875 | // - fast enumeration variables | |||
11876 | } else { | |||
11877 | DiagID = diag::err_typecheck_arr_assign_enumeration; | |||
11878 | } | |||
11879 | ||||
11880 | SourceRange Assign; | |||
11881 | if (Loc != OrigLoc) | |||
11882 | Assign = SourceRange(OrigLoc, OrigLoc); | |||
11883 | S.Diag(Loc, DiagID) << E->getSourceRange() << Assign; | |||
11884 | // We need to preserve the AST regardless, so migration tool | |||
11885 | // can do its job. | |||
11886 | return false; | |||
11887 | } | |||
11888 | } | |||
11889 | } | |||
11890 | ||||
11891 | // If none of the special cases above are triggered, then this is a | |||
11892 | // simple const assignment. | |||
11893 | if (DiagID == 0) { | |||
11894 | DiagnoseConstAssignment(S, E, Loc); | |||
11895 | return true; | |||
11896 | } | |||
11897 | ||||
11898 | break; | |||
11899 | case Expr::MLV_ConstAddrSpace: | |||
11900 | DiagnoseConstAssignment(S, E, Loc); | |||
11901 | return true; | |||
11902 | case Expr::MLV_ConstQualifiedField: | |||
11903 | DiagnoseRecursiveConstFields(S, E, Loc); | |||
11904 | return true; | |||
11905 | case Expr::MLV_ArrayType: | |||
11906 | case Expr::MLV_ArrayTemporary: | |||
11907 | DiagID = diag::err_typecheck_array_not_modifiable_lvalue; | |||
11908 | NeedType = true; | |||
11909 | break; | |||
11910 | case Expr::MLV_NotObjectType: | |||
11911 | DiagID = diag::err_typecheck_non_object_not_modifiable_lvalue; | |||
11912 | NeedType = true; | |||
11913 | break; | |||
11914 | case Expr::MLV_LValueCast: | |||
11915 | DiagID = diag::err_typecheck_lvalue_casts_not_supported; | |||
11916 | break; | |||
11917 | case Expr::MLV_Valid: | |||
11918 | llvm_unreachable("did not take early return for MLV_Valid")::llvm::llvm_unreachable_internal("did not take early return for MLV_Valid" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 11918); | |||
11919 | case Expr::MLV_InvalidExpression: | |||
11920 | case Expr::MLV_MemberFunction: | |||
11921 | case Expr::MLV_ClassTemporary: | |||
11922 | DiagID = diag::err_typecheck_expression_not_modifiable_lvalue; | |||
11923 | break; | |||
11924 | case Expr::MLV_IncompleteType: | |||
11925 | case Expr::MLV_IncompleteVoidType: | |||
11926 | return S.RequireCompleteType(Loc, E->getType(), | |||
11927 | diag::err_typecheck_incomplete_type_not_modifiable_lvalue, E); | |||
11928 | case Expr::MLV_DuplicateVectorComponents: | |||
11929 | DiagID = diag::err_typecheck_duplicate_vector_components_not_mlvalue; | |||
11930 | break; | |||
11931 | case Expr::MLV_NoSetterProperty: | |||
11932 | llvm_unreachable("readonly properties should be processed differently")::llvm::llvm_unreachable_internal("readonly properties should be processed differently" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 11932); | |||
11933 | case Expr::MLV_InvalidMessageExpression: | |||
11934 | DiagID = diag::err_readonly_message_assignment; | |||
11935 | break; | |||
11936 | case Expr::MLV_SubObjCPropertySetting: | |||
11937 | DiagID = diag::err_no_subobject_property_setting; | |||
11938 | break; | |||
11939 | } | |||
11940 | ||||
11941 | SourceRange Assign; | |||
11942 | if (Loc != OrigLoc) | |||
11943 | Assign = SourceRange(OrigLoc, OrigLoc); | |||
11944 | if (NeedType) | |||
11945 | S.Diag(Loc, DiagID) << E->getType() << E->getSourceRange() << Assign; | |||
11946 | else | |||
11947 | S.Diag(Loc, DiagID) << E->getSourceRange() << Assign; | |||
11948 | return true; | |||
11949 | } | |||
11950 | ||||
11951 | static void CheckIdentityFieldAssignment(Expr *LHSExpr, Expr *RHSExpr, | |||
11952 | SourceLocation Loc, | |||
11953 | Sema &Sema) { | |||
11954 | if (Sema.inTemplateInstantiation()) | |||
11955 | return; | |||
11956 | if (Sema.isUnevaluatedContext()) | |||
11957 | return; | |||
11958 | if (Loc.isInvalid() || Loc.isMacroID()) | |||
11959 | return; | |||
11960 | if (LHSExpr->getExprLoc().isMacroID() || RHSExpr->getExprLoc().isMacroID()) | |||
11961 | return; | |||
11962 | ||||
11963 | // C / C++ fields | |||
11964 | MemberExpr *ML = dyn_cast<MemberExpr>(LHSExpr); | |||
11965 | MemberExpr *MR = dyn_cast<MemberExpr>(RHSExpr); | |||
11966 | if (ML && MR) { | |||
11967 | if (!(isa<CXXThisExpr>(ML->getBase()) && isa<CXXThisExpr>(MR->getBase()))) | |||
11968 | return; | |||
11969 | const ValueDecl *LHSDecl = | |||
11970 | cast<ValueDecl>(ML->getMemberDecl()->getCanonicalDecl()); | |||
11971 | const ValueDecl *RHSDecl = | |||
11972 | cast<ValueDecl>(MR->getMemberDecl()->getCanonicalDecl()); | |||
11973 | if (LHSDecl != RHSDecl) | |||
11974 | return; | |||
11975 | if (LHSDecl->getType().isVolatileQualified()) | |||
11976 | return; | |||
11977 | if (const ReferenceType *RefTy = LHSDecl->getType()->getAs<ReferenceType>()) | |||
11978 | if (RefTy->getPointeeType().isVolatileQualified()) | |||
11979 | return; | |||
11980 | ||||
11981 | Sema.Diag(Loc, diag::warn_identity_field_assign) << 0; | |||
11982 | } | |||
11983 | ||||
11984 | // Objective-C instance variables | |||
11985 | ObjCIvarRefExpr *OL = dyn_cast<ObjCIvarRefExpr>(LHSExpr); | |||
11986 | ObjCIvarRefExpr *OR = dyn_cast<ObjCIvarRefExpr>(RHSExpr); | |||
11987 | if (OL && OR && OL->getDecl() == OR->getDecl()) { | |||
11988 | DeclRefExpr *RL = dyn_cast<DeclRefExpr>(OL->getBase()->IgnoreImpCasts()); | |||
11989 | DeclRefExpr *RR = dyn_cast<DeclRefExpr>(OR->getBase()->IgnoreImpCasts()); | |||
11990 | if (RL && RR && RL->getDecl() == RR->getDecl()) | |||
11991 | Sema.Diag(Loc, diag::warn_identity_field_assign) << 1; | |||
11992 | } | |||
11993 | } | |||
11994 | ||||
11995 | // C99 6.5.16.1 | |||
11996 | QualType Sema::CheckAssignmentOperands(Expr *LHSExpr, ExprResult &RHS, | |||
11997 | SourceLocation Loc, | |||
11998 | QualType CompoundType) { | |||
11999 | assert(!LHSExpr->hasPlaceholderType(BuiltinType::PseudoObject))((!LHSExpr->hasPlaceholderType(BuiltinType::PseudoObject)) ? static_cast<void> (0) : __assert_fail ("!LHSExpr->hasPlaceholderType(BuiltinType::PseudoObject)" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 11999, __PRETTY_FUNCTION__)); | |||
12000 | ||||
12001 | // Verify that LHS is a modifiable lvalue, and emit error if not. | |||
12002 | if (CheckForModifiableLvalue(LHSExpr, Loc, *this)) | |||
12003 | return QualType(); | |||
12004 | ||||
12005 | QualType LHSType = LHSExpr->getType(); | |||
12006 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : | |||
12007 | CompoundType; | |||
12008 | // OpenCL v1.2 s6.1.1.1 p2: | |||
12009 | // The half data type can only be used to declare a pointer to a buffer that | |||
12010 | // contains half values | |||
12011 | if (getLangOpts().OpenCL && !getOpenCLOptions().isEnabled("cl_khr_fp16") && | |||
12012 | LHSType->isHalfType()) { | |||
12013 | Diag(Loc, diag::err_opencl_half_load_store) << 1 | |||
12014 | << LHSType.getUnqualifiedType(); | |||
12015 | return QualType(); | |||
12016 | } | |||
12017 | ||||
12018 | AssignConvertType ConvTy; | |||
12019 | if (CompoundType.isNull()) { | |||
12020 | Expr *RHSCheck = RHS.get(); | |||
12021 | ||||
12022 | CheckIdentityFieldAssignment(LHSExpr, RHSCheck, Loc, *this); | |||
12023 | ||||
12024 | QualType LHSTy(LHSType); | |||
12025 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); | |||
12026 | if (RHS.isInvalid()) | |||
12027 | return QualType(); | |||
12028 | // Special case of NSObject attributes on c-style pointer types. | |||
12029 | if (ConvTy == IncompatiblePointer && | |||
12030 | ((Context.isObjCNSObjectType(LHSType) && | |||
12031 | RHSType->isObjCObjectPointerType()) || | |||
12032 | (Context.isObjCNSObjectType(RHSType) && | |||
12033 | LHSType->isObjCObjectPointerType()))) | |||
12034 | ConvTy = Compatible; | |||
12035 | ||||
12036 | if (ConvTy == Compatible && | |||
12037 | LHSType->isObjCObjectType()) | |||
12038 | Diag(Loc, diag::err_objc_object_assignment) | |||
12039 | << LHSType; | |||
12040 | ||||
12041 | // If the RHS is a unary plus or minus, check to see if they = and + are | |||
12042 | // right next to each other. If so, the user may have typo'd "x =+ 4" | |||
12043 | // instead of "x += 4". | |||
12044 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) | |||
12045 | RHSCheck = ICE->getSubExpr(); | |||
12046 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { | |||
12047 | if ((UO->getOpcode() == UO_Plus || UO->getOpcode() == UO_Minus) && | |||
12048 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && | |||
12049 | // Only if the two operators are exactly adjacent. | |||
12050 | Loc.getLocWithOffset(1) == UO->getOperatorLoc() && | |||
12051 | // And there is a space or other character before the subexpr of the | |||
12052 | // unary +/-. We don't want to warn on "x=-1". | |||
12053 | Loc.getLocWithOffset(2) != UO->getSubExpr()->getBeginLoc() && | |||
12054 | UO->getSubExpr()->getBeginLoc().isFileID()) { | |||
12055 | Diag(Loc, diag::warn_not_compound_assign) | |||
12056 | << (UO->getOpcode() == UO_Plus ? "+" : "-") | |||
12057 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); | |||
12058 | } | |||
12059 | } | |||
12060 | ||||
12061 | if (ConvTy == Compatible) { | |||
12062 | if (LHSType.getObjCLifetime() == Qualifiers::OCL_Strong) { | |||
12063 | // Warn about retain cycles where a block captures the LHS, but | |||
12064 | // not if the LHS is a simple variable into which the block is | |||
12065 | // being stored...unless that variable can be captured by reference! | |||
12066 | const Expr *InnerLHS = LHSExpr->IgnoreParenCasts(); | |||
12067 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InnerLHS); | |||
12068 | if (!DRE || DRE->getDecl()->hasAttr<BlocksAttr>()) | |||
12069 | checkRetainCycles(LHSExpr, RHS.get()); | |||
12070 | } | |||
12071 | ||||
12072 | if (LHSType.getObjCLifetime() == Qualifiers::OCL_Strong || | |||
12073 | LHSType.isNonWeakInMRRWithObjCWeak(Context)) { | |||
12074 | // It is safe to assign a weak reference into a strong variable. | |||
12075 | // Although this code can still have problems: | |||
12076 | // id x = self.weakProp; | |||
12077 | // id y = self.weakProp; | |||
12078 | // we do not warn to warn spuriously when 'x' and 'y' are on separate | |||
12079 | // paths through the function. This should be revisited if | |||
12080 | // -Wrepeated-use-of-weak is made flow-sensitive. | |||
12081 | // For ObjCWeak only, we do not warn if the assign is to a non-weak | |||
12082 | // variable, which will be valid for the current autorelease scope. | |||
12083 | if (!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, | |||
12084 | RHS.get()->getBeginLoc())) | |||
12085 | getCurFunction()->markSafeWeakUse(RHS.get()); | |||
12086 | ||||
12087 | } else if (getLangOpts().ObjCAutoRefCount || getLangOpts().ObjCWeak) { | |||
12088 | checkUnsafeExprAssigns(Loc, LHSExpr, RHS.get()); | |||
12089 | } | |||
12090 | } | |||
12091 | } else { | |||
12092 | // Compound assignment "x += y" | |||
12093 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); | |||
12094 | } | |||
12095 | ||||
12096 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, | |||
12097 | RHS.get(), AA_Assigning)) | |||
12098 | return QualType(); | |||
12099 | ||||
12100 | CheckForNullPointerDereference(*this, LHSExpr); | |||
12101 | ||||
12102 | if (getLangOpts().CPlusPlus2a && LHSType.isVolatileQualified()) { | |||
12103 | if (CompoundType.isNull()) { | |||
12104 | // C++2a [expr.ass]p5: | |||
12105 | // A simple-assignment whose left operand is of a volatile-qualified | |||
12106 | // type is deprecated unless the assignment is either a discarded-value | |||
12107 | // expression or an unevaluated operand | |||
12108 | ExprEvalContexts.back().VolatileAssignmentLHSs.push_back(LHSExpr); | |||
12109 | } else { | |||
12110 | // C++2a [expr.ass]p6: | |||
12111 | // [Compound-assignment] expressions are deprecated if E1 has | |||
12112 | // volatile-qualified type | |||
12113 | Diag(Loc, diag::warn_deprecated_compound_assign_volatile) << LHSType; | |||
12114 | } | |||
12115 | } | |||
12116 | ||||
12117 | // C99 6.5.16p3: The type of an assignment expression is the type of the | |||
12118 | // left operand unless the left operand has qualified type, in which case | |||
12119 | // it is the unqualified version of the type of the left operand. | |||
12120 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand | |||
12121 | // is converted to the type of the assignment expression (above). | |||
12122 | // C++ 5.17p1: the type of the assignment expression is that of its left | |||
12123 | // operand. | |||
12124 | return (getLangOpts().CPlusPlus | |||
12125 | ? LHSType : LHSType.getUnqualifiedType()); | |||
12126 | } | |||
12127 | ||||
12128 | // Only ignore explicit casts to void. | |||
12129 | static bool IgnoreCommaOperand(const Expr *E) { | |||
12130 | E = E->IgnoreParens(); | |||
12131 | ||||
12132 | if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { | |||
12133 | if (CE->getCastKind() == CK_ToVoid) { | |||
12134 | return true; | |||
12135 | } | |||
12136 | ||||
12137 | // static_cast<void> on a dependent type will not show up as CK_ToVoid. | |||
12138 | if (CE->getCastKind() == CK_Dependent && E->getType()->isVoidType() && | |||
12139 | CE->getSubExpr()->getType()->isDependentType()) { | |||
12140 | return true; | |||
12141 | } | |||
12142 | } | |||
12143 | ||||
12144 | return false; | |||
12145 | } | |||
12146 | ||||
12147 | // Look for instances where it is likely the comma operator is confused with | |||
12148 | // another operator. There is a whitelist of acceptable expressions for the | |||
12149 | // left hand side of the comma operator, otherwise emit a warning. | |||
12150 | void Sema::DiagnoseCommaOperator(const Expr *LHS, SourceLocation Loc) { | |||
12151 | // No warnings in macros | |||
12152 | if (Loc.isMacroID()) | |||
12153 | return; | |||
12154 | ||||
12155 | // Don't warn in template instantiations. | |||
12156 | if (inTemplateInstantiation()) | |||
12157 | return; | |||
12158 | ||||
12159 | // Scope isn't fine-grained enough to whitelist the specific cases, so | |||
12160 | // instead, skip more than needed, then call back into here with the | |||
12161 | // CommaVisitor in SemaStmt.cpp. | |||
12162 | // The whitelisted locations are the initialization and increment portions | |||
12163 | // of a for loop. The additional checks are on the condition of | |||
12164 | // if statements, do/while loops, and for loops. | |||
12165 | // Differences in scope flags for C89 mode requires the extra logic. | |||
12166 | const unsigned ForIncrementFlags = | |||
12167 | getLangOpts().C99 || getLangOpts().CPlusPlus | |||
12168 | ? Scope::ControlScope | Scope::ContinueScope | Scope::BreakScope | |||
12169 | : Scope::ContinueScope | Scope::BreakScope; | |||
12170 | const unsigned ForInitFlags = Scope::ControlScope | Scope::DeclScope; | |||
12171 | const unsigned ScopeFlags = getCurScope()->getFlags(); | |||
12172 | if ((ScopeFlags & ForIncrementFlags) == ForIncrementFlags || | |||
12173 | (ScopeFlags & ForInitFlags) == ForInitFlags) | |||
12174 | return; | |||
12175 | ||||
12176 | // If there are multiple comma operators used together, get the RHS of the | |||
12177 | // of the comma operator as the LHS. | |||
12178 | while (const BinaryOperator *BO = dyn_cast<BinaryOperator>(LHS)) { | |||
12179 | if (BO->getOpcode() != BO_Comma) | |||
12180 | break; | |||
12181 | LHS = BO->getRHS(); | |||
12182 | } | |||
12183 | ||||
12184 | // Only allow some expressions on LHS to not warn. | |||
12185 | if (IgnoreCommaOperand(LHS)) | |||
12186 | return; | |||
12187 | ||||
12188 | Diag(Loc, diag::warn_comma_operator); | |||
12189 | Diag(LHS->getBeginLoc(), diag::note_cast_to_void) | |||
12190 | << LHS->getSourceRange() | |||
12191 | << FixItHint::CreateInsertion(LHS->getBeginLoc(), | |||
12192 | LangOpts.CPlusPlus ? "static_cast<void>(" | |||
12193 | : "(void)(") | |||
12194 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(LHS->getEndLoc()), | |||
12195 | ")"); | |||
12196 | } | |||
12197 | ||||
12198 | // C99 6.5.17 | |||
12199 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, | |||
12200 | SourceLocation Loc) { | |||
12201 | LHS = S.CheckPlaceholderExpr(LHS.get()); | |||
12202 | RHS = S.CheckPlaceholderExpr(RHS.get()); | |||
12203 | if (LHS.isInvalid() || RHS.isInvalid()) | |||
12204 | return QualType(); | |||
12205 | ||||
12206 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its | |||
12207 | // operands, but not unary promotions. | |||
12208 | // C++'s comma does not do any conversions at all (C++ [expr.comma]p1). | |||
12209 | ||||
12210 | // So we treat the LHS as a ignored value, and in C++ we allow the | |||
12211 | // containing site to determine what should be done with the RHS. | |||
12212 | LHS = S.IgnoredValueConversions(LHS.get()); | |||
12213 | if (LHS.isInvalid()) | |||
12214 | return QualType(); | |||
12215 | ||||
12216 | S.DiagnoseUnusedExprResult(LHS.get()); | |||
12217 | ||||
12218 | if (!S.getLangOpts().CPlusPlus) { | |||
12219 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.get()); | |||
12220 | if (RHS.isInvalid()) | |||
12221 | return QualType(); | |||
12222 | if (!RHS.get()->getType()->isVoidType()) | |||
12223 | S.RequireCompleteType(Loc, RHS.get()->getType(), | |||
12224 | diag::err_incomplete_type); | |||
12225 | } | |||
12226 | ||||
12227 | if (!S.getDiagnostics().isIgnored(diag::warn_comma_operator, Loc)) | |||
12228 | S.DiagnoseCommaOperator(LHS.get(), Loc); | |||
12229 | ||||
12230 | return RHS.get()->getType(); | |||
12231 | } | |||
12232 | ||||
12233 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine | |||
12234 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. | |||
12235 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, | |||
12236 | ExprValueKind &VK, | |||
12237 | ExprObjectKind &OK, | |||
12238 | SourceLocation OpLoc, | |||
12239 | bool IsInc, bool IsPrefix) { | |||
12240 | if (Op->isTypeDependent()) | |||
12241 | return S.Context.DependentTy; | |||
12242 | ||||
12243 | QualType ResType = Op->getType(); | |||
12244 | // Atomic types can be used for increment / decrement where the non-atomic | |||
12245 | // versions can, so ignore the _Atomic() specifier for the purpose of | |||
12246 | // checking. | |||
12247 | if (const AtomicType *ResAtomicType = ResType->getAs<AtomicType>()) | |||
12248 | ResType = ResAtomicType->getValueType(); | |||
12249 | ||||
12250 | assert(!ResType.isNull() && "no type for increment/decrement expression")((!ResType.isNull() && "no type for increment/decrement expression" ) ? static_cast<void> (0) : __assert_fail ("!ResType.isNull() && \"no type for increment/decrement expression\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 12250, __PRETTY_FUNCTION__)); | |||
12251 | ||||
12252 | if (S.getLangOpts().CPlusPlus && ResType->isBooleanType()) { | |||
12253 | // Decrement of bool is not allowed. | |||
12254 | if (!IsInc) { | |||
12255 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); | |||
12256 | return QualType(); | |||
12257 | } | |||
12258 | // Increment of bool sets it to true, but is deprecated. | |||
12259 | S.Diag(OpLoc, S.getLangOpts().CPlusPlus17 ? diag::ext_increment_bool | |||
12260 | : diag::warn_increment_bool) | |||
12261 | << Op->getSourceRange(); | |||
12262 | } else if (S.getLangOpts().CPlusPlus && ResType->isEnumeralType()) { | |||
12263 | // Error on enum increments and decrements in C++ mode | |||
12264 | S.Diag(OpLoc, diag::err_increment_decrement_enum) << IsInc << ResType; | |||
12265 | return QualType(); | |||
12266 | } else if (ResType->isRealType()) { | |||
12267 | // OK! | |||
12268 | } else if (ResType->isPointerType()) { | |||
12269 | // C99 6.5.2.4p2, 6.5.6p2 | |||
12270 | if (!checkArithmeticOpPointerOperand(S, OpLoc, Op)) | |||
12271 | return QualType(); | |||
12272 | } else if (ResType->isObjCObjectPointerType()) { | |||
12273 | // On modern runtimes, ObjC pointer arithmetic is forbidden. | |||
12274 | // Otherwise, we just need a complete type. | |||
12275 | if (checkArithmeticIncompletePointerType(S, OpLoc, Op) || | |||
12276 | checkArithmeticOnObjCPointer(S, OpLoc, Op)) | |||
12277 | return QualType(); | |||
12278 | } else if (ResType->isAnyComplexType()) { | |||
12279 | // C99 does not support ++/-- on complex types, we allow as an extension. | |||
12280 | S.Diag(OpLoc, diag::ext_integer_increment_complex) | |||
12281 | << ResType << Op->getSourceRange(); | |||
12282 | } else if (ResType->isPlaceholderType()) { | |||
12283 | ExprResult PR = S.CheckPlaceholderExpr(Op); | |||
12284 | if (PR.isInvalid()) return QualType(); | |||
12285 | return CheckIncrementDecrementOperand(S, PR.get(), VK, OK, OpLoc, | |||
12286 | IsInc, IsPrefix); | |||
12287 | } else if (S.getLangOpts().AltiVec && ResType->isVectorType()) { | |||
12288 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) | |||
12289 | } else if (S.getLangOpts().ZVector && ResType->isVectorType() && | |||
12290 | (ResType->castAs<VectorType>()->getVectorKind() != | |||
12291 | VectorType::AltiVecBool)) { | |||
12292 | // The z vector extensions allow ++ and -- for non-bool vectors. | |||
12293 | } else if(S.getLangOpts().OpenCL && ResType->isVectorType() && | |||
12294 | ResType->castAs<VectorType>()->getElementType()->isIntegerType()) { | |||
12295 | // OpenCL V1.2 6.3 says dec/inc ops operate on integer vector types. | |||
12296 | } else { | |||
12297 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) | |||
12298 | << ResType << int(IsInc) << Op->getSourceRange(); | |||
12299 | return QualType(); | |||
12300 | } | |||
12301 | // At this point, we know we have a real, complex or pointer type. | |||
12302 | // Now make sure the operand is a modifiable lvalue. | |||
12303 | if (CheckForModifiableLvalue(Op, OpLoc, S)) | |||
12304 | return QualType(); | |||
12305 | if (S.getLangOpts().CPlusPlus2a && ResType.isVolatileQualified()) { | |||
12306 | // C++2a [expr.pre.inc]p1, [expr.post.inc]p1: | |||
12307 | // An operand with volatile-qualified type is deprecated | |||
12308 | S.Diag(OpLoc, diag::warn_deprecated_increment_decrement_volatile) | |||
12309 | << IsInc << ResType; | |||
12310 | } | |||
12311 | // In C++, a prefix increment is the same type as the operand. Otherwise | |||
12312 | // (in C or with postfix), the increment is the unqualified type of the | |||
12313 | // operand. | |||
12314 | if (IsPrefix && S.getLangOpts().CPlusPlus) { | |||
12315 | VK = VK_LValue; | |||
12316 | OK = Op->getObjectKind(); | |||
12317 | return ResType; | |||
12318 | } else { | |||
12319 | VK = VK_RValue; | |||
12320 | return ResType.getUnqualifiedType(); | |||
12321 | } | |||
12322 | } | |||
12323 | ||||
12324 | ||||
12325 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). | |||
12326 | /// This routine allows us to typecheck complex/recursive expressions | |||
12327 | /// where the declaration is needed for type checking. We only need to | |||
12328 | /// handle cases when the expression references a function designator | |||
12329 | /// or is an lvalue. Here are some examples: | |||
12330 | /// - &(x) => x | |||
12331 | /// - &*****f => f for f a function designator. | |||
12332 | /// - &s.xx => s | |||
12333 | /// - &s.zz[1].yy -> s, if zz is an array | |||
12334 | /// - *(x + 1) -> x, if x is an array | |||
12335 | /// - &"123"[2] -> 0 | |||
12336 | /// - & __real__ x -> x | |||
12337 | static ValueDecl *getPrimaryDecl(Expr *E) { | |||
12338 | switch (E->getStmtClass()) { | |||
12339 | case Stmt::DeclRefExprClass: | |||
12340 | return cast<DeclRefExpr>(E)->getDecl(); | |||
12341 | case Stmt::MemberExprClass: | |||
12342 | // If this is an arrow operator, the address is an offset from | |||
12343 | // the base's value, so the object the base refers to is | |||
12344 | // irrelevant. | |||
12345 | if (cast<MemberExpr>(E)->isArrow()) | |||
12346 | return nullptr; | |||
12347 | // Otherwise, the expression refers to a part of the base | |||
12348 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); | |||
12349 | case Stmt::ArraySubscriptExprClass: { | |||
12350 | // FIXME: This code shouldn't be necessary! We should catch the implicit | |||
12351 | // promotion of register arrays earlier. | |||
12352 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); | |||
12353 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { | |||
12354 | if (ICE->getSubExpr()->getType()->isArrayType()) | |||
12355 | return getPrimaryDecl(ICE->getSubExpr()); | |||
12356 | } | |||
12357 | return nullptr; | |||
12358 | } | |||
12359 | case Stmt::UnaryOperatorClass: { | |||
12360 | UnaryOperator *UO = cast<UnaryOperator>(E); | |||
12361 | ||||
12362 | switch(UO->getOpcode()) { | |||
12363 | case UO_Real: | |||
12364 | case UO_Imag: | |||
12365 | case UO_Extension: | |||
12366 | return getPrimaryDecl(UO->getSubExpr()); | |||
12367 | default: | |||
12368 | return nullptr; | |||
12369 | } | |||
12370 | } | |||
12371 | case Stmt::ParenExprClass: | |||
12372 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); | |||
12373 | case Stmt::ImplicitCastExprClass: | |||
12374 | // If the result of an implicit cast is an l-value, we care about | |||
12375 | // the sub-expression; otherwise, the result here doesn't matter. | |||
12376 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); | |||
12377 | default: | |||
12378 | return nullptr; | |||
12379 | } | |||
12380 | } | |||
12381 | ||||
12382 | namespace { | |||
12383 | enum { | |||
12384 | AO_Bit_Field = 0, | |||
12385 | AO_Vector_Element = 1, | |||
12386 | AO_Property_Expansion = 2, | |||
12387 | AO_Register_Variable = 3, | |||
12388 | AO_No_Error = 4 | |||
12389 | }; | |||
12390 | } | |||
12391 | /// Diagnose invalid operand for address of operations. | |||
12392 | /// | |||
12393 | /// \param Type The type of operand which cannot have its address taken. | |||
12394 | static void diagnoseAddressOfInvalidType(Sema &S, SourceLocation Loc, | |||
12395 | Expr *E, unsigned Type) { | |||
12396 | S.Diag(Loc, diag::err_typecheck_address_of) << Type << E->getSourceRange(); | |||
12397 | } | |||
12398 | ||||
12399 | /// CheckAddressOfOperand - The operand of & must be either a function | |||
12400 | /// designator or an lvalue designating an object. If it is an lvalue, the | |||
12401 | /// object cannot be declared with storage class register or be a bit field. | |||
12402 | /// Note: The usual conversions are *not* applied to the operand of the & | |||
12403 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. | |||
12404 | /// In C++, the operand might be an overloaded function name, in which case | |||
12405 | /// we allow the '&' but retain the overloaded-function type. | |||
12406 | QualType Sema::CheckAddressOfOperand(ExprResult &OrigOp, SourceLocation OpLoc) { | |||
12407 | if (const BuiltinType *PTy = OrigOp.get()->getType()->getAsPlaceholderType()){ | |||
12408 | if (PTy->getKind() == BuiltinType::Overload) { | |||
12409 | Expr *E = OrigOp.get()->IgnoreParens(); | |||
12410 | if (!isa<OverloadExpr>(E)) { | |||
12411 | assert(cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf)((cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf) ? static_cast<void> (0) : __assert_fail ("cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 12411, __PRETTY_FUNCTION__)); | |||
12412 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof_addrof_function) | |||
12413 | << OrigOp.get()->getSourceRange(); | |||
12414 | return QualType(); | |||
12415 | } | |||
12416 | ||||
12417 | OverloadExpr *Ovl = cast<OverloadExpr>(E); | |||
12418 | if (isa<UnresolvedMemberExpr>(Ovl)) | |||
12419 | if (!ResolveSingleFunctionTemplateSpecialization(Ovl)) { | |||
12420 | Diag(OpLoc, diag::err_invalid_form_pointer_member_function) | |||
12421 | << OrigOp.get()->getSourceRange(); | |||
12422 | return QualType(); | |||
12423 | } | |||
12424 | ||||
12425 | return Context.OverloadTy; | |||
12426 | } | |||
12427 | ||||
12428 | if (PTy->getKind() == BuiltinType::UnknownAny) | |||
12429 | return Context.UnknownAnyTy; | |||
12430 | ||||
12431 | if (PTy->getKind() == BuiltinType::BoundMember) { | |||
12432 | Diag(OpLoc, diag::err_invalid_form_pointer_member_function) | |||
12433 | << OrigOp.get()->getSourceRange(); | |||
12434 | return QualType(); | |||
12435 | } | |||
12436 | ||||
12437 | OrigOp = CheckPlaceholderExpr(OrigOp.get()); | |||
12438 | if (OrigOp.isInvalid()) return QualType(); | |||
12439 | } | |||
12440 | ||||
12441 | if (OrigOp.get()->isTypeDependent()) | |||
12442 | return Context.DependentTy; | |||
12443 | ||||
12444 | assert(!OrigOp.get()->getType()->isPlaceholderType())((!OrigOp.get()->getType()->isPlaceholderType()) ? static_cast <void> (0) : __assert_fail ("!OrigOp.get()->getType()->isPlaceholderType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 12444, __PRETTY_FUNCTION__)); | |||
12445 | ||||
12446 | // Make sure to ignore parentheses in subsequent checks | |||
12447 | Expr *op = OrigOp.get()->IgnoreParens(); | |||
12448 | ||||
12449 | // In OpenCL captures for blocks called as lambda functions | |||
12450 | // are located in the private address space. Blocks used in | |||
12451 | // enqueue_kernel can be located in a different address space | |||
12452 | // depending on a vendor implementation. Thus preventing | |||
12453 | // taking an address of the capture to avoid invalid AS casts. | |||
12454 | if (LangOpts.OpenCL) { | |||
12455 | auto* VarRef = dyn_cast<DeclRefExpr>(op); | |||
12456 | if (VarRef && VarRef->refersToEnclosingVariableOrCapture()) { | |||
12457 | Diag(op->getExprLoc(), diag::err_opencl_taking_address_capture); | |||
12458 | return QualType(); | |||
12459 | } | |||
12460 | } | |||
12461 | ||||
12462 | if (getLangOpts().C99) { | |||
12463 | // Implement C99-only parts of addressof rules. | |||
12464 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { | |||
12465 | if (uOp->getOpcode() == UO_Deref) | |||
12466 | // Per C99 6.5.3.2, the address of a deref always returns a valid result | |||
12467 | // (assuming the deref expression is valid). | |||
12468 | return uOp->getSubExpr()->getType(); | |||
12469 | } | |||
12470 | // Technically, there should be a check for array subscript | |||
12471 | // expressions here, but the result of one is always an lvalue anyway. | |||
12472 | } | |||
12473 | ValueDecl *dcl = getPrimaryDecl(op); | |||
12474 | ||||
12475 | if (auto *FD = dyn_cast_or_null<FunctionDecl>(dcl)) | |||
12476 | if (!checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, | |||
12477 | op->getBeginLoc())) | |||
12478 | return QualType(); | |||
12479 | ||||
12480 | Expr::LValueClassification lval = op->ClassifyLValue(Context); | |||
12481 | unsigned AddressOfError = AO_No_Error; | |||
12482 | ||||
12483 | if (lval == Expr::LV_ClassTemporary || lval == Expr::LV_ArrayTemporary) { | |||
12484 | bool sfinae = (bool)isSFINAEContext(); | |||
12485 | Diag(OpLoc, isSFINAEContext() ? diag::err_typecheck_addrof_temporary | |||
12486 | : diag::ext_typecheck_addrof_temporary) | |||
12487 | << op->getType() << op->getSourceRange(); | |||
12488 | if (sfinae) | |||
12489 | return QualType(); | |||
12490 | // Materialize the temporary as an lvalue so that we can take its address. | |||
12491 | OrigOp = op = | |||
12492 | CreateMaterializeTemporaryExpr(op->getType(), OrigOp.get(), true); | |||
12493 | } else if (isa<ObjCSelectorExpr>(op)) { | |||
12494 | return Context.getPointerType(op->getType()); | |||
12495 | } else if (lval == Expr::LV_MemberFunction) { | |||
12496 | // If it's an instance method, make a member pointer. | |||
12497 | // The expression must have exactly the form &A::foo. | |||
12498 | ||||
12499 | // If the underlying expression isn't a decl ref, give up. | |||
12500 | if (!isa<DeclRefExpr>(op)) { | |||
12501 | Diag(OpLoc, diag::err_invalid_form_pointer_member_function) | |||
12502 | << OrigOp.get()->getSourceRange(); | |||
12503 | return QualType(); | |||
12504 | } | |||
12505 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); | |||
12506 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); | |||
12507 | ||||
12508 | // The id-expression was parenthesized. | |||
12509 | if (OrigOp.get() != DRE) { | |||
12510 | Diag(OpLoc, diag::err_parens_pointer_member_function) | |||
12511 | << OrigOp.get()->getSourceRange(); | |||
12512 | ||||
12513 | // The method was named without a qualifier. | |||
12514 | } else if (!DRE->getQualifier()) { | |||
12515 | if (MD->getParent()->getName().empty()) | |||
12516 | Diag(OpLoc, diag::err_unqualified_pointer_member_function) | |||
12517 | << op->getSourceRange(); | |||
12518 | else { | |||
12519 | SmallString<32> Str; | |||
12520 | StringRef Qual = (MD->getParent()->getName() + "::").toStringRef(Str); | |||
12521 | Diag(OpLoc, diag::err_unqualified_pointer_member_function) | |||
12522 | << op->getSourceRange() | |||
12523 | << FixItHint::CreateInsertion(op->getSourceRange().getBegin(), Qual); | |||
12524 | } | |||
12525 | } | |||
12526 | ||||
12527 | // Taking the address of a dtor is illegal per C++ [class.dtor]p2. | |||
12528 | if (isa<CXXDestructorDecl>(MD)) | |||
12529 | Diag(OpLoc, diag::err_typecheck_addrof_dtor) << op->getSourceRange(); | |||
12530 | ||||
12531 | QualType MPTy = Context.getMemberPointerType( | |||
12532 | op->getType(), Context.getTypeDeclType(MD->getParent()).getTypePtr()); | |||
12533 | // Under the MS ABI, lock down the inheritance model now. | |||
12534 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) | |||
12535 | (void)isCompleteType(OpLoc, MPTy); | |||
12536 | return MPTy; | |||
12537 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { | |||
12538 | // C99 6.5.3.2p1 | |||
12539 | // The operand must be either an l-value or a function designator | |||
12540 | if (!op->getType()->isFunctionType()) { | |||
12541 | // Use a special diagnostic for loads from property references. | |||
12542 | if (isa<PseudoObjectExpr>(op)) { | |||
12543 | AddressOfError = AO_Property_Expansion; | |||
12544 | } else { | |||
12545 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) | |||
12546 | << op->getType() << op->getSourceRange(); | |||
12547 | return QualType(); | |||
12548 | } | |||
12549 | } | |||
12550 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 | |||
12551 | // The operand cannot be a bit-field | |||
12552 | AddressOfError = AO_Bit_Field; | |||
12553 | } else if (op->getObjectKind() == OK_VectorComponent) { | |||
12554 | // The operand cannot be an element of a vector | |||
12555 | AddressOfError = AO_Vector_Element; | |||
12556 | } else if (dcl) { // C99 6.5.3.2p1 | |||
12557 | // We have an lvalue with a decl. Make sure the decl is not declared | |||
12558 | // with the register storage-class specifier. | |||
12559 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { | |||
12560 | // in C++ it is not error to take address of a register | |||
12561 | // variable (c++03 7.1.1P3) | |||
12562 | if (vd->getStorageClass() == SC_Register && | |||
12563 | !getLangOpts().CPlusPlus) { | |||
12564 | AddressOfError = AO_Register_Variable; | |||
12565 | } | |||
12566 | } else if (isa<MSPropertyDecl>(dcl)) { | |||
12567 | AddressOfError = AO_Property_Expansion; | |||
12568 | } else if (isa<FunctionTemplateDecl>(dcl)) { | |||
12569 | return Context.OverloadTy; | |||
12570 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { | |||
12571 | // Okay: we can take the address of a field. | |||
12572 | // Could be a pointer to member, though, if there is an explicit | |||
12573 | // scope qualifier for the class. | |||
12574 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { | |||
12575 | DeclContext *Ctx = dcl->getDeclContext(); | |||
12576 | if (Ctx && Ctx->isRecord()) { | |||
12577 | if (dcl->getType()->isReferenceType()) { | |||
12578 | Diag(OpLoc, | |||
12579 | diag::err_cannot_form_pointer_to_member_of_reference_type) | |||
12580 | << dcl->getDeclName() << dcl->getType(); | |||
12581 | return QualType(); | |||
12582 | } | |||
12583 | ||||
12584 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) | |||
12585 | Ctx = Ctx->getParent(); | |||
12586 | ||||
12587 | QualType MPTy = Context.getMemberPointerType( | |||
12588 | op->getType(), | |||
12589 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); | |||
12590 | // Under the MS ABI, lock down the inheritance model now. | |||
12591 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) | |||
12592 | (void)isCompleteType(OpLoc, MPTy); | |||
12593 | return MPTy; | |||
12594 | } | |||
12595 | } | |||
12596 | } else if (!isa<FunctionDecl>(dcl) && !isa<NonTypeTemplateParmDecl>(dcl) && | |||
12597 | !isa<BindingDecl>(dcl)) | |||
12598 | llvm_unreachable("Unknown/unexpected decl type")::llvm::llvm_unreachable_internal("Unknown/unexpected decl type" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 12598); | |||
12599 | } | |||
12600 | ||||
12601 | if (AddressOfError != AO_No_Error) { | |||
12602 | diagnoseAddressOfInvalidType(*this, OpLoc, op, AddressOfError); | |||
12603 | return QualType(); | |||
12604 | } | |||
12605 | ||||
12606 | if (lval == Expr::LV_IncompleteVoidType) { | |||
12607 | // Taking the address of a void variable is technically illegal, but we | |||
12608 | // allow it in cases which are otherwise valid. | |||
12609 | // Example: "extern void x; void* y = &x;". | |||
12610 | Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); | |||
12611 | } | |||
12612 | ||||
12613 | // If the operand has type "type", the result has type "pointer to type". | |||
12614 | if (op->getType()->isObjCObjectType()) | |||
12615 | return Context.getObjCObjectPointerType(op->getType()); | |||
12616 | ||||
12617 | CheckAddressOfPackedMember(op); | |||
12618 | ||||
12619 | return Context.getPointerType(op->getType()); | |||
12620 | } | |||
12621 | ||||
12622 | static void RecordModifiableNonNullParam(Sema &S, const Expr *Exp) { | |||
12623 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp); | |||
12624 | if (!DRE) | |||
12625 | return; | |||
12626 | const Decl *D = DRE->getDecl(); | |||
12627 | if (!D) | |||
12628 | return; | |||
12629 | const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D); | |||
12630 | if (!Param) | |||
12631 | return; | |||
12632 | if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(Param->getDeclContext())) | |||
12633 | if (!FD->hasAttr<NonNullAttr>() && !Param->hasAttr<NonNullAttr>()) | |||
12634 | return; | |||
12635 | if (FunctionScopeInfo *FD = S.getCurFunction()) | |||
12636 | if (!FD->ModifiedNonNullParams.count(Param)) | |||
12637 | FD->ModifiedNonNullParams.insert(Param); | |||
12638 | } | |||
12639 | ||||
12640 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). | |||
12641 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, | |||
12642 | SourceLocation OpLoc) { | |||
12643 | if (Op->isTypeDependent()) | |||
12644 | return S.Context.DependentTy; | |||
12645 | ||||
12646 | ExprResult ConvResult = S.UsualUnaryConversions(Op); | |||
12647 | if (ConvResult.isInvalid()) | |||
12648 | return QualType(); | |||
12649 | Op = ConvResult.get(); | |||
12650 | QualType OpTy = Op->getType(); | |||
12651 | QualType Result; | |||
12652 | ||||
12653 | if (isa<CXXReinterpretCastExpr>(Op)) { | |||
12654 | QualType OpOrigType = Op->IgnoreParenCasts()->getType(); | |||
12655 | S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true, | |||
12656 | Op->getSourceRange()); | |||
12657 | } | |||
12658 | ||||
12659 | if (const PointerType *PT = OpTy->getAs<PointerType>()) | |||
12660 | { | |||
12661 | Result = PT->getPointeeType(); | |||
12662 | } | |||
12663 | else if (const ObjCObjectPointerType *OPT = | |||
12664 | OpTy->getAs<ObjCObjectPointerType>()) | |||
12665 | Result = OPT->getPointeeType(); | |||
12666 | else { | |||
12667 | ExprResult PR = S.CheckPlaceholderExpr(Op); | |||
12668 | if (PR.isInvalid()) return QualType(); | |||
12669 | if (PR.get() != Op) | |||
12670 | return CheckIndirectionOperand(S, PR.get(), VK, OpLoc); | |||
12671 | } | |||
12672 | ||||
12673 | if (Result.isNull()) { | |||
12674 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) | |||
12675 | << OpTy << Op->getSourceRange(); | |||
12676 | return QualType(); | |||
12677 | } | |||
12678 | ||||
12679 | // Note that per both C89 and C99, indirection is always legal, even if Result | |||
12680 | // is an incomplete type or void. It would be possible to warn about | |||
12681 | // dereferencing a void pointer, but it's completely well-defined, and such a | |||
12682 | // warning is unlikely to catch any mistakes. In C++, indirection is not valid | |||
12683 | // for pointers to 'void' but is fine for any other pointer type: | |||
12684 | // | |||
12685 | // C++ [expr.unary.op]p1: | |||
12686 | // [...] the expression to which [the unary * operator] is applied shall | |||
12687 | // be a pointer to an object type, or a pointer to a function type | |||
12688 | if (S.getLangOpts().CPlusPlus && Result->isVoidType()) | |||
12689 | S.Diag(OpLoc, diag::ext_typecheck_indirection_through_void_pointer) | |||
12690 | << OpTy << Op->getSourceRange(); | |||
12691 | ||||
12692 | // Dereferences are usually l-values... | |||
12693 | VK = VK_LValue; | |||
12694 | ||||
12695 | // ...except that certain expressions are never l-values in C. | |||
12696 | if (!S.getLangOpts().CPlusPlus && Result.isCForbiddenLValueType()) | |||
12697 | VK = VK_RValue; | |||
12698 | ||||
12699 | return Result; | |||
12700 | } | |||
12701 | ||||
12702 | BinaryOperatorKind Sema::ConvertTokenKindToBinaryOpcode(tok::TokenKind Kind) { | |||
12703 | BinaryOperatorKind Opc; | |||
12704 | switch (Kind) { | |||
12705 | default: llvm_unreachable("Unknown binop!")::llvm::llvm_unreachable_internal("Unknown binop!", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 12705); | |||
12706 | case tok::periodstar: Opc = BO_PtrMemD; break; | |||
12707 | case tok::arrowstar: Opc = BO_PtrMemI; break; | |||
12708 | case tok::star: Opc = BO_Mul; break; | |||
12709 | case tok::slash: Opc = BO_Div; break; | |||
12710 | case tok::percent: Opc = BO_Rem; break; | |||
12711 | case tok::plus: Opc = BO_Add; break; | |||
12712 | case tok::minus: Opc = BO_Sub; break; | |||
12713 | case tok::lessless: Opc = BO_Shl; break; | |||
12714 | case tok::greatergreater: Opc = BO_Shr; break; | |||
12715 | case tok::lessequal: Opc = BO_LE; break; | |||
12716 | case tok::less: Opc = BO_LT; break; | |||
12717 | case tok::greaterequal: Opc = BO_GE; break; | |||
12718 | case tok::greater: Opc = BO_GT; break; | |||
12719 | case tok::exclaimequal: Opc = BO_NE; break; | |||
12720 | case tok::equalequal: Opc = BO_EQ; break; | |||
12721 | case tok::spaceship: Opc = BO_Cmp; break; | |||
12722 | case tok::amp: Opc = BO_And; break; | |||
12723 | case tok::caret: Opc = BO_Xor; break; | |||
12724 | case tok::pipe: Opc = BO_Or; break; | |||
12725 | case tok::ampamp: Opc = BO_LAnd; break; | |||
12726 | case tok::pipepipe: Opc = BO_LOr; break; | |||
12727 | case tok::equal: Opc = BO_Assign; break; | |||
12728 | case tok::starequal: Opc = BO_MulAssign; break; | |||
12729 | case tok::slashequal: Opc = BO_DivAssign; break; | |||
12730 | case tok::percentequal: Opc = BO_RemAssign; break; | |||
12731 | case tok::plusequal: Opc = BO_AddAssign; break; | |||
12732 | case tok::minusequal: Opc = BO_SubAssign; break; | |||
12733 | case tok::lesslessequal: Opc = BO_ShlAssign; break; | |||
12734 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; | |||
12735 | case tok::ampequal: Opc = BO_AndAssign; break; | |||
12736 | case tok::caretequal: Opc = BO_XorAssign; break; | |||
12737 | case tok::pipeequal: Opc = BO_OrAssign; break; | |||
12738 | case tok::comma: Opc = BO_Comma; break; | |||
12739 | } | |||
12740 | return Opc; | |||
12741 | } | |||
12742 | ||||
12743 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( | |||
12744 | tok::TokenKind Kind) { | |||
12745 | UnaryOperatorKind Opc; | |||
12746 | switch (Kind) { | |||
12747 | default: llvm_unreachable("Unknown unary op!")::llvm::llvm_unreachable_internal("Unknown unary op!", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 12747); | |||
12748 | case tok::plusplus: Opc = UO_PreInc; break; | |||
12749 | case tok::minusminus: Opc = UO_PreDec; break; | |||
12750 | case tok::amp: Opc = UO_AddrOf; break; | |||
12751 | case tok::star: Opc = UO_Deref; break; | |||
12752 | case tok::plus: Opc = UO_Plus; break; | |||
12753 | case tok::minus: Opc = UO_Minus; break; | |||
12754 | case tok::tilde: Opc = UO_Not; break; | |||
12755 | case tok::exclaim: Opc = UO_LNot; break; | |||
12756 | case tok::kw___real: Opc = UO_Real; break; | |||
12757 | case tok::kw___imag: Opc = UO_Imag; break; | |||
12758 | case tok::kw___extension__: Opc = UO_Extension; break; | |||
12759 | } | |||
12760 | return Opc; | |||
12761 | } | |||
12762 | ||||
12763 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. | |||
12764 | /// This warning suppressed in the event of macro expansions. | |||
12765 | static void DiagnoseSelfAssignment(Sema &S, Expr *LHSExpr, Expr *RHSExpr, | |||
12766 | SourceLocation OpLoc, bool IsBuiltin) { | |||
12767 | if (S.inTemplateInstantiation()) | |||
12768 | return; | |||
12769 | if (S.isUnevaluatedContext()) | |||
12770 | return; | |||
12771 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) | |||
12772 | return; | |||
12773 | LHSExpr = LHSExpr->IgnoreParenImpCasts(); | |||
12774 | RHSExpr = RHSExpr->IgnoreParenImpCasts(); | |||
12775 | const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr); | |||
12776 | const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr); | |||
12777 | if (!LHSDeclRef || !RHSDeclRef || | |||
12778 | LHSDeclRef->getLocation().isMacroID() || | |||
12779 | RHSDeclRef->getLocation().isMacroID()) | |||
12780 | return; | |||
12781 | const ValueDecl *LHSDecl = | |||
12782 | cast<ValueDecl>(LHSDeclRef->getDecl()->getCanonicalDecl()); | |||
12783 | const ValueDecl *RHSDecl = | |||
12784 | cast<ValueDecl>(RHSDeclRef->getDecl()->getCanonicalDecl()); | |||
12785 | if (LHSDecl != RHSDecl) | |||
12786 | return; | |||
12787 | if (LHSDecl->getType().isVolatileQualified()) | |||
12788 | return; | |||
12789 | if (const ReferenceType *RefTy = LHSDecl->getType()->getAs<ReferenceType>()) | |||
12790 | if (RefTy->getPointeeType().isVolatileQualified()) | |||
12791 | return; | |||
12792 | ||||
12793 | S.Diag(OpLoc, IsBuiltin ? diag::warn_self_assignment_builtin | |||
12794 | : diag::warn_self_assignment_overloaded) | |||
12795 | << LHSDeclRef->getType() << LHSExpr->getSourceRange() | |||
12796 | << RHSExpr->getSourceRange(); | |||
12797 | } | |||
12798 | ||||
12799 | /// Check if a bitwise-& is performed on an Objective-C pointer. This | |||
12800 | /// is usually indicative of introspection within the Objective-C pointer. | |||
12801 | static void checkObjCPointerIntrospection(Sema &S, ExprResult &L, ExprResult &R, | |||
12802 | SourceLocation OpLoc) { | |||
12803 | if (!S.getLangOpts().ObjC) | |||
12804 | return; | |||
12805 | ||||
12806 | const Expr *ObjCPointerExpr = nullptr, *OtherExpr = nullptr; | |||
12807 | const Expr *LHS = L.get(); | |||
12808 | const Expr *RHS = R.get(); | |||
12809 | ||||
12810 | if (LHS->IgnoreParenCasts()->getType()->isObjCObjectPointerType()) { | |||
12811 | ObjCPointerExpr = LHS; | |||
12812 | OtherExpr = RHS; | |||
12813 | } | |||
12814 | else if (RHS->IgnoreParenCasts()->getType()->isObjCObjectPointerType()) { | |||
12815 | ObjCPointerExpr = RHS; | |||
12816 | OtherExpr = LHS; | |||
12817 | } | |||
12818 | ||||
12819 | // This warning is deliberately made very specific to reduce false | |||
12820 | // positives with logic that uses '&' for hashing. This logic mainly | |||
12821 | // looks for code trying to introspect into tagged pointers, which | |||
12822 | // code should generally never do. | |||
12823 | if (ObjCPointerExpr && isa<IntegerLiteral>(OtherExpr->IgnoreParenCasts())) { | |||
12824 | unsigned Diag = diag::warn_objc_pointer_masking; | |||
12825 | // Determine if we are introspecting the result of performSelectorXXX. | |||
12826 | const Expr *Ex = ObjCPointerExpr->IgnoreParenCasts(); | |||
12827 | // Special case messages to -performSelector and friends, which | |||
12828 | // can return non-pointer values boxed in a pointer value. | |||
12829 | // Some clients may wish to silence warnings in this subcase. | |||
12830 | if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(Ex)) { | |||
12831 | Selector S = ME->getSelector(); | |||
12832 | StringRef SelArg0 = S.getNameForSlot(0); | |||
12833 | if (SelArg0.startswith("performSelector")) | |||
12834 | Diag = diag::warn_objc_pointer_masking_performSelector; | |||
12835 | } | |||
12836 | ||||
12837 | S.Diag(OpLoc, Diag) | |||
12838 | << ObjCPointerExpr->getSourceRange(); | |||
12839 | } | |||
12840 | } | |||
12841 | ||||
12842 | static NamedDecl *getDeclFromExpr(Expr *E) { | |||
12843 | if (!E) | |||
12844 | return nullptr; | |||
12845 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) | |||
12846 | return DRE->getDecl(); | |||
12847 | if (auto *ME = dyn_cast<MemberExpr>(E)) | |||
12848 | return ME->getMemberDecl(); | |||
12849 | if (auto *IRE = dyn_cast<ObjCIvarRefExpr>(E)) | |||
12850 | return IRE->getDecl(); | |||
12851 | return nullptr; | |||
12852 | } | |||
12853 | ||||
12854 | // This helper function promotes a binary operator's operands (which are of a | |||
12855 | // half vector type) to a vector of floats and then truncates the result to | |||
12856 | // a vector of either half or short. | |||
12857 | static ExprResult convertHalfVecBinOp(Sema &S, ExprResult LHS, ExprResult RHS, | |||
12858 | BinaryOperatorKind Opc, QualType ResultTy, | |||
12859 | ExprValueKind VK, ExprObjectKind OK, | |||
12860 | bool IsCompAssign, SourceLocation OpLoc, | |||
12861 | FPOptions FPFeatures) { | |||
12862 | auto &Context = S.getASTContext(); | |||
12863 | assert((isVector(ResultTy, Context.HalfTy) ||(((isVector(ResultTy, Context.HalfTy) || isVector(ResultTy, Context .ShortTy)) && "Result must be a vector of half or short" ) ? static_cast<void> (0) : __assert_fail ("(isVector(ResultTy, Context.HalfTy) || isVector(ResultTy, Context.ShortTy)) && \"Result must be a vector of half or short\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 12865, __PRETTY_FUNCTION__)) | |||
12864 | isVector(ResultTy, Context.ShortTy)) &&(((isVector(ResultTy, Context.HalfTy) || isVector(ResultTy, Context .ShortTy)) && "Result must be a vector of half or short" ) ? static_cast<void> (0) : __assert_fail ("(isVector(ResultTy, Context.HalfTy) || isVector(ResultTy, Context.ShortTy)) && \"Result must be a vector of half or short\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 12865, __PRETTY_FUNCTION__)) | |||
12865 | "Result must be a vector of half or short")(((isVector(ResultTy, Context.HalfTy) || isVector(ResultTy, Context .ShortTy)) && "Result must be a vector of half or short" ) ? static_cast<void> (0) : __assert_fail ("(isVector(ResultTy, Context.HalfTy) || isVector(ResultTy, Context.ShortTy)) && \"Result must be a vector of half or short\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 12865, __PRETTY_FUNCTION__)); | |||
12866 | assert(isVector(LHS.get()->getType(), Context.HalfTy) &&((isVector(LHS.get()->getType(), Context.HalfTy) && isVector(RHS.get()->getType(), Context.HalfTy) && "both operands expected to be a half vector") ? static_cast< void> (0) : __assert_fail ("isVector(LHS.get()->getType(), Context.HalfTy) && isVector(RHS.get()->getType(), Context.HalfTy) && \"both operands expected to be a half vector\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 12868, __PRETTY_FUNCTION__)) | |||
12867 | isVector(RHS.get()->getType(), Context.HalfTy) &&((isVector(LHS.get()->getType(), Context.HalfTy) && isVector(RHS.get()->getType(), Context.HalfTy) && "both operands expected to be a half vector") ? static_cast< void> (0) : __assert_fail ("isVector(LHS.get()->getType(), Context.HalfTy) && isVector(RHS.get()->getType(), Context.HalfTy) && \"both operands expected to be a half vector\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 12868, __PRETTY_FUNCTION__)) | |||
12868 | "both operands expected to be a half vector")((isVector(LHS.get()->getType(), Context.HalfTy) && isVector(RHS.get()->getType(), Context.HalfTy) && "both operands expected to be a half vector") ? static_cast< void> (0) : __assert_fail ("isVector(LHS.get()->getType(), Context.HalfTy) && isVector(RHS.get()->getType(), Context.HalfTy) && \"both operands expected to be a half vector\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 12868, __PRETTY_FUNCTION__)); | |||
12869 | ||||
12870 | RHS = convertVector(RHS.get(), Context.FloatTy, S); | |||
12871 | QualType BinOpResTy = RHS.get()->getType(); | |||
12872 | ||||
12873 | // If Opc is a comparison, ResultType is a vector of shorts. In that case, | |||
12874 | // change BinOpResTy to a vector of ints. | |||
12875 | if (isVector(ResultTy, Context.ShortTy)) | |||
12876 | BinOpResTy = S.GetSignedVectorType(BinOpResTy); | |||
12877 | ||||
12878 | if (IsCompAssign) | |||
12879 | return new (Context) CompoundAssignOperator( | |||
12880 | LHS.get(), RHS.get(), Opc, ResultTy, VK, OK, BinOpResTy, BinOpResTy, | |||
12881 | OpLoc, FPFeatures); | |||
12882 | ||||
12883 | LHS = convertVector(LHS.get(), Context.FloatTy, S); | |||
12884 | auto *BO = new (Context) BinaryOperator(LHS.get(), RHS.get(), Opc, BinOpResTy, | |||
12885 | VK, OK, OpLoc, FPFeatures); | |||
12886 | return convertVector(BO, ResultTy->castAs<VectorType>()->getElementType(), S); | |||
12887 | } | |||
12888 | ||||
12889 | static std::pair<ExprResult, ExprResult> | |||
12890 | CorrectDelayedTyposInBinOp(Sema &S, BinaryOperatorKind Opc, Expr *LHSExpr, | |||
12891 | Expr *RHSExpr) { | |||
12892 | ExprResult LHS = LHSExpr, RHS = RHSExpr; | |||
12893 | if (!S.getLangOpts().CPlusPlus) { | |||
12894 | // C cannot handle TypoExpr nodes on either side of a binop because it | |||
12895 | // doesn't handle dependent types properly, so make sure any TypoExprs have | |||
12896 | // been dealt with before checking the operands. | |||
12897 | LHS = S.CorrectDelayedTyposInExpr(LHS); | |||
12898 | RHS = S.CorrectDelayedTyposInExpr(RHS, [Opc, LHS](Expr *E) { | |||
12899 | if (Opc != BO_Assign) | |||
12900 | return ExprResult(E); | |||
12901 | // Avoid correcting the RHS to the same Expr as the LHS. | |||
12902 | Decl *D = getDeclFromExpr(E); | |||
12903 | return (D && D == getDeclFromExpr(LHS.get())) ? ExprError() : E; | |||
12904 | }); | |||
12905 | } | |||
12906 | return std::make_pair(LHS, RHS); | |||
12907 | } | |||
12908 | ||||
12909 | /// Returns true if conversion between vectors of halfs and vectors of floats | |||
12910 | /// is needed. | |||
12911 | static bool needsConversionOfHalfVec(bool OpRequiresConversion, ASTContext &Ctx, | |||
12912 | QualType SrcType) { | |||
12913 | return OpRequiresConversion && !Ctx.getLangOpts().NativeHalfType && | |||
12914 | !Ctx.getTargetInfo().useFP16ConversionIntrinsics() && | |||
12915 | isVector(SrcType, Ctx.HalfTy); | |||
12916 | } | |||
12917 | ||||
12918 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with | |||
12919 | /// operator @p Opc at location @c TokLoc. This routine only supports | |||
12920 | /// built-in operations; ActOnBinOp handles overloaded operators. | |||
12921 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, | |||
12922 | BinaryOperatorKind Opc, | |||
12923 | Expr *LHSExpr, Expr *RHSExpr) { | |||
12924 | if (getLangOpts().CPlusPlus11 && isa<InitListExpr>(RHSExpr)) { | |||
12925 | // The syntax only allows initializer lists on the RHS of assignment, | |||
12926 | // so we don't need to worry about accepting invalid code for | |||
12927 | // non-assignment operators. | |||
12928 | // C++11 5.17p9: | |||
12929 | // The meaning of x = {v} [...] is that of x = T(v) [...]. The meaning | |||
12930 | // of x = {} is x = T(). | |||
12931 | InitializationKind Kind = InitializationKind::CreateDirectList( | |||
12932 | RHSExpr->getBeginLoc(), RHSExpr->getBeginLoc(), RHSExpr->getEndLoc()); | |||
12933 | InitializedEntity Entity = | |||
12934 | InitializedEntity::InitializeTemporary(LHSExpr->getType()); | |||
12935 | InitializationSequence InitSeq(*this, Entity, Kind, RHSExpr); | |||
12936 | ExprResult Init = InitSeq.Perform(*this, Entity, Kind, RHSExpr); | |||
12937 | if (Init.isInvalid()) | |||
12938 | return Init; | |||
12939 | RHSExpr = Init.get(); | |||
12940 | } | |||
12941 | ||||
12942 | ExprResult LHS = LHSExpr, RHS = RHSExpr; | |||
12943 | QualType ResultTy; // Result type of the binary operator. | |||
12944 | // The following two variables are used for compound assignment operators | |||
12945 | QualType CompLHSTy; // Type of LHS after promotions for computation | |||
12946 | QualType CompResultTy; // Type of computation result | |||
12947 | ExprValueKind VK = VK_RValue; | |||
12948 | ExprObjectKind OK = OK_Ordinary; | |||
12949 | bool ConvertHalfVec = false; | |||
12950 | ||||
12951 | std::tie(LHS, RHS) = CorrectDelayedTyposInBinOp(*this, Opc, LHSExpr, RHSExpr); | |||
12952 | if (!LHS.isUsable() || !RHS.isUsable()) | |||
12953 | return ExprError(); | |||
12954 | ||||
12955 | if (getLangOpts().OpenCL) { | |||
12956 | QualType LHSTy = LHSExpr->getType(); | |||
12957 | QualType RHSTy = RHSExpr->getType(); | |||
12958 | // OpenCLC v2.0 s6.13.11.1 allows atomic variables to be initialized by | |||
12959 | // the ATOMIC_VAR_INIT macro. | |||
12960 | if (LHSTy->isAtomicType() || RHSTy->isAtomicType()) { | |||
12961 | SourceRange SR(LHSExpr->getBeginLoc(), RHSExpr->getEndLoc()); | |||
12962 | if (BO_Assign == Opc) | |||
12963 | Diag(OpLoc, diag::err_opencl_atomic_init) << 0 << SR; | |||
12964 | else | |||
12965 | ResultTy = InvalidOperands(OpLoc, LHS, RHS); | |||
12966 | return ExprError(); | |||
12967 | } | |||
12968 | ||||
12969 | // OpenCL special types - image, sampler, pipe, and blocks are to be used | |||
12970 | // only with a builtin functions and therefore should be disallowed here. | |||
12971 | if (LHSTy->isImageType() || RHSTy->isImageType() || | |||
12972 | LHSTy->isSamplerT() || RHSTy->isSamplerT() || | |||
12973 | LHSTy->isPipeType() || RHSTy->isPipeType() || | |||
12974 | LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { | |||
12975 | ResultTy = InvalidOperands(OpLoc, LHS, RHS); | |||
12976 | return ExprError(); | |||
12977 | } | |||
12978 | } | |||
12979 | ||||
12980 | // Diagnose operations on the unsupported types for OpenMP device compilation. | |||
12981 | if (getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice) { | |||
12982 | if (Opc != BO_Assign && Opc != BO_Comma) { | |||
12983 | checkOpenMPDeviceExpr(LHSExpr); | |||
12984 | checkOpenMPDeviceExpr(RHSExpr); | |||
12985 | } | |||
12986 | } | |||
12987 | ||||
12988 | switch (Opc) { | |||
12989 | case BO_Assign: | |||
12990 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, QualType()); | |||
12991 | if (getLangOpts().CPlusPlus && | |||
12992 | LHS.get()->getObjectKind() != OK_ObjCProperty) { | |||
12993 | VK = LHS.get()->getValueKind(); | |||
12994 | OK = LHS.get()->getObjectKind(); | |||
12995 | } | |||
12996 | if (!ResultTy.isNull()) { | |||
12997 | DiagnoseSelfAssignment(*this, LHS.get(), RHS.get(), OpLoc, true); | |||
12998 | DiagnoseSelfMove(LHS.get(), RHS.get(), OpLoc); | |||
12999 | ||||
13000 | // Avoid copying a block to the heap if the block is assigned to a local | |||
13001 | // auto variable that is declared in the same scope as the block. This | |||
13002 | // optimization is unsafe if the local variable is declared in an outer | |||
13003 | // scope. For example: | |||
13004 | // | |||
13005 | // BlockTy b; | |||
13006 | // { | |||
13007 | // b = ^{...}; | |||
13008 | // } | |||
13009 | // // It is unsafe to invoke the block here if it wasn't copied to the | |||
13010 | // // heap. | |||
13011 | // b(); | |||
13012 | ||||
13013 | if (auto *BE = dyn_cast<BlockExpr>(RHS.get()->IgnoreParens())) | |||
13014 | if (auto *DRE = dyn_cast<DeclRefExpr>(LHS.get()->IgnoreParens())) | |||
13015 | if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) | |||
13016 | if (VD->hasLocalStorage() && getCurScope()->isDeclScope(VD)) | |||
13017 | BE->getBlockDecl()->setCanAvoidCopyToHeap(); | |||
13018 | ||||
13019 | if (LHS.get()->getType().hasNonTrivialToPrimitiveCopyCUnion()) | |||
13020 | checkNonTrivialCUnion(LHS.get()->getType(), LHS.get()->getExprLoc(), | |||
13021 | NTCUC_Assignment, NTCUK_Copy); | |||
13022 | } | |||
13023 | RecordModifiableNonNullParam(*this, LHS.get()); | |||
13024 | break; | |||
13025 | case BO_PtrMemD: | |||
13026 | case BO_PtrMemI: | |||
13027 | ResultTy = CheckPointerToMemberOperands(LHS, RHS, VK, OpLoc, | |||
13028 | Opc == BO_PtrMemI); | |||
13029 | break; | |||
13030 | case BO_Mul: | |||
13031 | case BO_Div: | |||
13032 | ConvertHalfVec = true; | |||
13033 | ResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, false, | |||
13034 | Opc == BO_Div); | |||
13035 | break; | |||
13036 | case BO_Rem: | |||
13037 | ResultTy = CheckRemainderOperands(LHS, RHS, OpLoc); | |||
13038 | break; | |||
13039 | case BO_Add: | |||
13040 | ConvertHalfVec = true; | |||
13041 | ResultTy = CheckAdditionOperands(LHS, RHS, OpLoc, Opc); | |||
13042 | break; | |||
13043 | case BO_Sub: | |||
13044 | ConvertHalfVec = true; | |||
13045 | ResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc); | |||
13046 | break; | |||
13047 | case BO_Shl: | |||
13048 | case BO_Shr: | |||
13049 | ResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc); | |||
13050 | break; | |||
13051 | case BO_LE: | |||
13052 | case BO_LT: | |||
13053 | case BO_GE: | |||
13054 | case BO_GT: | |||
13055 | ConvertHalfVec = true; | |||
13056 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc); | |||
13057 | break; | |||
13058 | case BO_EQ: | |||
13059 | case BO_NE: | |||
13060 | ConvertHalfVec = true; | |||
13061 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc); | |||
13062 | break; | |||
13063 | case BO_Cmp: | |||
13064 | ConvertHalfVec = true; | |||
13065 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc); | |||
13066 | assert(ResultTy.isNull() || ResultTy->getAsCXXRecordDecl())((ResultTy.isNull() || ResultTy->getAsCXXRecordDecl()) ? static_cast <void> (0) : __assert_fail ("ResultTy.isNull() || ResultTy->getAsCXXRecordDecl()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 13066, __PRETTY_FUNCTION__)); | |||
13067 | break; | |||
13068 | case BO_And: | |||
13069 | checkObjCPointerIntrospection(*this, LHS, RHS, OpLoc); | |||
13070 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
13071 | case BO_Xor: | |||
13072 | case BO_Or: | |||
13073 | ResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc, Opc); | |||
13074 | break; | |||
13075 | case BO_LAnd: | |||
13076 | case BO_LOr: | |||
13077 | ConvertHalfVec = true; | |||
13078 | ResultTy = CheckLogicalOperands(LHS, RHS, OpLoc, Opc); | |||
13079 | break; | |||
13080 | case BO_MulAssign: | |||
13081 | case BO_DivAssign: | |||
13082 | ConvertHalfVec = true; | |||
13083 | CompResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, true, | |||
13084 | Opc == BO_DivAssign); | |||
13085 | CompLHSTy = CompResultTy; | |||
13086 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) | |||
13087 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); | |||
13088 | break; | |||
13089 | case BO_RemAssign: | |||
13090 | CompResultTy = CheckRemainderOperands(LHS, RHS, OpLoc, true); | |||
13091 | CompLHSTy = CompResultTy; | |||
13092 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) | |||
13093 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); | |||
13094 | break; | |||
13095 | case BO_AddAssign: | |||
13096 | ConvertHalfVec = true; | |||
13097 | CompResultTy = CheckAdditionOperands(LHS, RHS, OpLoc, Opc, &CompLHSTy); | |||
13098 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) | |||
13099 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); | |||
13100 | break; | |||
13101 | case BO_SubAssign: | |||
13102 | ConvertHalfVec = true; | |||
13103 | CompResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc, &CompLHSTy); | |||
13104 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) | |||
13105 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); | |||
13106 | break; | |||
13107 | case BO_ShlAssign: | |||
13108 | case BO_ShrAssign: | |||
13109 | CompResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc, true); | |||
13110 | CompLHSTy = CompResultTy; | |||
13111 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) | |||
13112 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); | |||
13113 | break; | |||
13114 | case BO_AndAssign: | |||
13115 | case BO_OrAssign: // fallthrough | |||
13116 | DiagnoseSelfAssignment(*this, LHS.get(), RHS.get(), OpLoc, true); | |||
13117 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
13118 | case BO_XorAssign: | |||
13119 | CompResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc, Opc); | |||
13120 | CompLHSTy = CompResultTy; | |||
13121 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) | |||
13122 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); | |||
13123 | break; | |||
13124 | case BO_Comma: | |||
13125 | ResultTy = CheckCommaOperands(*this, LHS, RHS, OpLoc); | |||
13126 | if (getLangOpts().CPlusPlus && !RHS.isInvalid()) { | |||
13127 | VK = RHS.get()->getValueKind(); | |||
13128 | OK = RHS.get()->getObjectKind(); | |||
13129 | } | |||
13130 | break; | |||
13131 | } | |||
13132 | if (ResultTy.isNull() || LHS.isInvalid() || RHS.isInvalid()) | |||
13133 | return ExprError(); | |||
13134 | ||||
13135 | if (ResultTy->isRealFloatingType() && | |||
13136 | (getLangOpts().getFPRoundingMode() != LangOptions::FPR_ToNearest || | |||
13137 | getLangOpts().getFPExceptionMode() != LangOptions::FPE_Ignore)) | |||
13138 | // Mark the current function as usng floating point constrained intrinsics | |||
13139 | if (FunctionDecl *F = dyn_cast<FunctionDecl>(CurContext)) { | |||
13140 | F->setUsesFPIntrin(true); | |||
13141 | } | |||
13142 | ||||
13143 | // Some of the binary operations require promoting operands of half vector to | |||
13144 | // float vectors and truncating the result back to half vector. For now, we do | |||
13145 | // this only when HalfArgsAndReturn is set (that is, when the target is arm or | |||
13146 | // arm64). | |||
13147 | assert(isVector(RHS.get()->getType(), Context.HalfTy) ==((isVector(RHS.get()->getType(), Context.HalfTy) == isVector (LHS.get()->getType(), Context.HalfTy) && "both sides are half vectors or neither sides are" ) ? static_cast<void> (0) : __assert_fail ("isVector(RHS.get()->getType(), Context.HalfTy) == isVector(LHS.get()->getType(), Context.HalfTy) && \"both sides are half vectors or neither sides are\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 13149, __PRETTY_FUNCTION__)) | |||
13148 | isVector(LHS.get()->getType(), Context.HalfTy) &&((isVector(RHS.get()->getType(), Context.HalfTy) == isVector (LHS.get()->getType(), Context.HalfTy) && "both sides are half vectors or neither sides are" ) ? static_cast<void> (0) : __assert_fail ("isVector(RHS.get()->getType(), Context.HalfTy) == isVector(LHS.get()->getType(), Context.HalfTy) && \"both sides are half vectors or neither sides are\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 13149, __PRETTY_FUNCTION__)) | |||
13149 | "both sides are half vectors or neither sides are")((isVector(RHS.get()->getType(), Context.HalfTy) == isVector (LHS.get()->getType(), Context.HalfTy) && "both sides are half vectors or neither sides are" ) ? static_cast<void> (0) : __assert_fail ("isVector(RHS.get()->getType(), Context.HalfTy) == isVector(LHS.get()->getType(), Context.HalfTy) && \"both sides are half vectors or neither sides are\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 13149, __PRETTY_FUNCTION__)); | |||
13150 | ConvertHalfVec = needsConversionOfHalfVec(ConvertHalfVec, Context, | |||
13151 | LHS.get()->getType()); | |||
13152 | ||||
13153 | // Check for array bounds violations for both sides of the BinaryOperator | |||
13154 | CheckArrayAccess(LHS.get()); | |||
13155 | CheckArrayAccess(RHS.get()); | |||
13156 | ||||
13157 | if (const ObjCIsaExpr *OISA = dyn_cast<ObjCIsaExpr>(LHS.get()->IgnoreParenCasts())) { | |||
13158 | NamedDecl *ObjectSetClass = LookupSingleName(TUScope, | |||
13159 | &Context.Idents.get("object_setClass"), | |||
13160 | SourceLocation(), LookupOrdinaryName); | |||
13161 | if (ObjectSetClass && isa<ObjCIsaExpr>(LHS.get())) { | |||
13162 | SourceLocation RHSLocEnd = getLocForEndOfToken(RHS.get()->getEndLoc()); | |||
13163 | Diag(LHS.get()->getExprLoc(), diag::warn_objc_isa_assign) | |||
13164 | << FixItHint::CreateInsertion(LHS.get()->getBeginLoc(), | |||
13165 | "object_setClass(") | |||
13166 | << FixItHint::CreateReplacement(SourceRange(OISA->getOpLoc(), OpLoc), | |||
13167 | ",") | |||
13168 | << FixItHint::CreateInsertion(RHSLocEnd, ")"); | |||
13169 | } | |||
13170 | else | |||
13171 | Diag(LHS.get()->getExprLoc(), diag::warn_objc_isa_assign); | |||
13172 | } | |||
13173 | else if (const ObjCIvarRefExpr *OIRE = | |||
13174 | dyn_cast<ObjCIvarRefExpr>(LHS.get()->IgnoreParenCasts())) | |||
13175 | DiagnoseDirectIsaAccess(*this, OIRE, OpLoc, RHS.get()); | |||
13176 | ||||
13177 | // Opc is not a compound assignment if CompResultTy is null. | |||
13178 | if (CompResultTy.isNull()) { | |||
13179 | if (ConvertHalfVec) | |||
13180 | return convertHalfVecBinOp(*this, LHS, RHS, Opc, ResultTy, VK, OK, false, | |||
13181 | OpLoc, FPFeatures); | |||
13182 | return new (Context) BinaryOperator(LHS.get(), RHS.get(), Opc, ResultTy, VK, | |||
13183 | OK, OpLoc, FPFeatures); | |||
13184 | } | |||
13185 | ||||
13186 | // Handle compound assignments. | |||
13187 | if (getLangOpts().CPlusPlus && LHS.get()->getObjectKind() != | |||
13188 | OK_ObjCProperty) { | |||
13189 | VK = VK_LValue; | |||
13190 | OK = LHS.get()->getObjectKind(); | |||
13191 | } | |||
13192 | ||||
13193 | if (ConvertHalfVec) | |||
13194 | return convertHalfVecBinOp(*this, LHS, RHS, Opc, ResultTy, VK, OK, true, | |||
13195 | OpLoc, FPFeatures); | |||
13196 | ||||
13197 | return new (Context) CompoundAssignOperator( | |||
13198 | LHS.get(), RHS.get(), Opc, ResultTy, VK, OK, CompLHSTy, CompResultTy, | |||
13199 | OpLoc, FPFeatures); | |||
13200 | } | |||
13201 | ||||
13202 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison | |||
13203 | /// operators are mixed in a way that suggests that the programmer forgot that | |||
13204 | /// comparison operators have higher precedence. The most typical example of | |||
13205 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". | |||
13206 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, | |||
13207 | SourceLocation OpLoc, Expr *LHSExpr, | |||
13208 | Expr *RHSExpr) { | |||
13209 | BinaryOperator *LHSBO = dyn_cast<BinaryOperator>(LHSExpr); | |||
13210 | BinaryOperator *RHSBO = dyn_cast<BinaryOperator>(RHSExpr); | |||
13211 | ||||
13212 | // Check that one of the sides is a comparison operator and the other isn't. | |||
13213 | bool isLeftComp = LHSBO && LHSBO->isComparisonOp(); | |||
13214 | bool isRightComp = RHSBO && RHSBO->isComparisonOp(); | |||
13215 | if (isLeftComp == isRightComp) | |||
13216 | return; | |||
13217 | ||||
13218 | // Bitwise operations are sometimes used as eager logical ops. | |||
13219 | // Don't diagnose this. | |||
13220 | bool isLeftBitwise = LHSBO && LHSBO->isBitwiseOp(); | |||
13221 | bool isRightBitwise = RHSBO && RHSBO->isBitwiseOp(); | |||
13222 | if (isLeftBitwise || isRightBitwise) | |||
13223 | return; | |||
13224 | ||||
13225 | SourceRange DiagRange = isLeftComp | |||
13226 | ? SourceRange(LHSExpr->getBeginLoc(), OpLoc) | |||
13227 | : SourceRange(OpLoc, RHSExpr->getEndLoc()); | |||
13228 | StringRef OpStr = isLeftComp ? LHSBO->getOpcodeStr() : RHSBO->getOpcodeStr(); | |||
13229 | SourceRange ParensRange = | |||
13230 | isLeftComp | |||
13231 | ? SourceRange(LHSBO->getRHS()->getBeginLoc(), RHSExpr->getEndLoc()) | |||
13232 | : SourceRange(LHSExpr->getBeginLoc(), RHSBO->getLHS()->getEndLoc()); | |||
13233 | ||||
13234 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) | |||
13235 | << DiagRange << BinaryOperator::getOpcodeStr(Opc) << OpStr; | |||
13236 | SuggestParentheses(Self, OpLoc, | |||
13237 | Self.PDiag(diag::note_precedence_silence) << OpStr, | |||
13238 | (isLeftComp ? LHSExpr : RHSExpr)->getSourceRange()); | |||
13239 | SuggestParentheses(Self, OpLoc, | |||
13240 | Self.PDiag(diag::note_precedence_bitwise_first) | |||
13241 | << BinaryOperator::getOpcodeStr(Opc), | |||
13242 | ParensRange); | |||
13243 | } | |||
13244 | ||||
13245 | /// It accepts a '&&' expr that is inside a '||' one. | |||
13246 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression | |||
13247 | /// in parentheses. | |||
13248 | static void | |||
13249 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, | |||
13250 | BinaryOperator *Bop) { | |||
13251 | assert(Bop->getOpcode() == BO_LAnd)((Bop->getOpcode() == BO_LAnd) ? static_cast<void> ( 0) : __assert_fail ("Bop->getOpcode() == BO_LAnd", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 13251, __PRETTY_FUNCTION__)); | |||
13252 | Self.Diag(Bop->getOperatorLoc(), diag::warn_logical_and_in_logical_or) | |||
13253 | << Bop->getSourceRange() << OpLoc; | |||
13254 | SuggestParentheses(Self, Bop->getOperatorLoc(), | |||
13255 | Self.PDiag(diag::note_precedence_silence) | |||
13256 | << Bop->getOpcodeStr(), | |||
13257 | Bop->getSourceRange()); | |||
13258 | } | |||
13259 | ||||
13260 | /// Returns true if the given expression can be evaluated as a constant | |||
13261 | /// 'true'. | |||
13262 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { | |||
13263 | bool Res; | |||
13264 | return !E->isValueDependent() && | |||
13265 | E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; | |||
13266 | } | |||
13267 | ||||
13268 | /// Returns true if the given expression can be evaluated as a constant | |||
13269 | /// 'false'. | |||
13270 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { | |||
13271 | bool Res; | |||
13272 | return !E->isValueDependent() && | |||
13273 | E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; | |||
13274 | } | |||
13275 | ||||
13276 | /// Look for '&&' in the left hand of a '||' expr. | |||
13277 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, | |||
13278 | Expr *LHSExpr, Expr *RHSExpr) { | |||
13279 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(LHSExpr)) { | |||
13280 | if (Bop->getOpcode() == BO_LAnd) { | |||
13281 | // If it's "a && b || 0" don't warn since the precedence doesn't matter. | |||
13282 | if (EvaluatesAsFalse(S, RHSExpr)) | |||
13283 | return; | |||
13284 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. | |||
13285 | if (!EvaluatesAsTrue(S, Bop->getLHS())) | |||
13286 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); | |||
13287 | } else if (Bop->getOpcode() == BO_LOr) { | |||
13288 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { | |||
13289 | // If it's "a || b && 1 || c" we didn't warn earlier for | |||
13290 | // "a || b && 1", but warn now. | |||
13291 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) | |||
13292 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); | |||
13293 | } | |||
13294 | } | |||
13295 | } | |||
13296 | } | |||
13297 | ||||
13298 | /// Look for '&&' in the right hand of a '||' expr. | |||
13299 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, | |||
13300 | Expr *LHSExpr, Expr *RHSExpr) { | |||
13301 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(RHSExpr)) { | |||
13302 | if (Bop->getOpcode() == BO_LAnd) { | |||
13303 | // If it's "0 || a && b" don't warn since the precedence doesn't matter. | |||
13304 | if (EvaluatesAsFalse(S, LHSExpr)) | |||
13305 | return; | |||
13306 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. | |||
13307 | if (!EvaluatesAsTrue(S, Bop->getRHS())) | |||
13308 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); | |||
13309 | } | |||
13310 | } | |||
13311 | } | |||
13312 | ||||
13313 | /// Look for bitwise op in the left or right hand of a bitwise op with | |||
13314 | /// lower precedence and emit a diagnostic together with a fixit hint that wraps | |||
13315 | /// the '&' expression in parentheses. | |||
13316 | static void DiagnoseBitwiseOpInBitwiseOp(Sema &S, BinaryOperatorKind Opc, | |||
13317 | SourceLocation OpLoc, Expr *SubExpr) { | |||
13318 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(SubExpr)) { | |||
13319 | if (Bop->isBitwiseOp() && Bop->getOpcode() < Opc) { | |||
13320 | S.Diag(Bop->getOperatorLoc(), diag::warn_bitwise_op_in_bitwise_op) | |||
13321 | << Bop->getOpcodeStr() << BinaryOperator::getOpcodeStr(Opc) | |||
13322 | << Bop->getSourceRange() << OpLoc; | |||
13323 | SuggestParentheses(S, Bop->getOperatorLoc(), | |||
13324 | S.PDiag(diag::note_precedence_silence) | |||
13325 | << Bop->getOpcodeStr(), | |||
13326 | Bop->getSourceRange()); | |||
13327 | } | |||
13328 | } | |||
13329 | } | |||
13330 | ||||
13331 | static void DiagnoseAdditionInShift(Sema &S, SourceLocation OpLoc, | |||
13332 | Expr *SubExpr, StringRef Shift) { | |||
13333 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(SubExpr)) { | |||
13334 | if (Bop->getOpcode() == BO_Add || Bop->getOpcode() == BO_Sub) { | |||
13335 | StringRef Op = Bop->getOpcodeStr(); | |||
13336 | S.Diag(Bop->getOperatorLoc(), diag::warn_addition_in_bitshift) | |||
13337 | << Bop->getSourceRange() << OpLoc << Shift << Op; | |||
13338 | SuggestParentheses(S, Bop->getOperatorLoc(), | |||
13339 | S.PDiag(diag::note_precedence_silence) << Op, | |||
13340 | Bop->getSourceRange()); | |||
13341 | } | |||
13342 | } | |||
13343 | } | |||
13344 | ||||
13345 | static void DiagnoseShiftCompare(Sema &S, SourceLocation OpLoc, | |||
13346 | Expr *LHSExpr, Expr *RHSExpr) { | |||
13347 | CXXOperatorCallExpr *OCE = dyn_cast<CXXOperatorCallExpr>(LHSExpr); | |||
13348 | if (!OCE) | |||
13349 | return; | |||
13350 | ||||
13351 | FunctionDecl *FD = OCE->getDirectCallee(); | |||
13352 | if (!FD || !FD->isOverloadedOperator()) | |||
13353 | return; | |||
13354 | ||||
13355 | OverloadedOperatorKind Kind = FD->getOverloadedOperator(); | |||
13356 | if (Kind != OO_LessLess && Kind != OO_GreaterGreater) | |||
13357 | return; | |||
13358 | ||||
13359 | S.Diag(OpLoc, diag::warn_overloaded_shift_in_comparison) | |||
13360 | << LHSExpr->getSourceRange() << RHSExpr->getSourceRange() | |||
13361 | << (Kind == OO_LessLess); | |||
13362 | SuggestParentheses(S, OCE->getOperatorLoc(), | |||
13363 | S.PDiag(diag::note_precedence_silence) | |||
13364 | << (Kind == OO_LessLess ? "<<" : ">>"), | |||
13365 | OCE->getSourceRange()); | |||
13366 | SuggestParentheses( | |||
13367 | S, OpLoc, S.PDiag(diag::note_evaluate_comparison_first), | |||
13368 | SourceRange(OCE->getArg(1)->getBeginLoc(), RHSExpr->getEndLoc())); | |||
13369 | } | |||
13370 | ||||
13371 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky | |||
13372 | /// precedence. | |||
13373 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, | |||
13374 | SourceLocation OpLoc, Expr *LHSExpr, | |||
13375 | Expr *RHSExpr){ | |||
13376 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". | |||
13377 | if (BinaryOperator::isBitwiseOp(Opc)) | |||
13378 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, LHSExpr, RHSExpr); | |||
13379 | ||||
13380 | // Diagnose "arg1 & arg2 | arg3" | |||
13381 | if ((Opc == BO_Or || Opc == BO_Xor) && | |||
13382 | !OpLoc.isMacroID()/* Don't warn in macros. */) { | |||
13383 | DiagnoseBitwiseOpInBitwiseOp(Self, Opc, OpLoc, LHSExpr); | |||
13384 | DiagnoseBitwiseOpInBitwiseOp(Self, Opc, OpLoc, RHSExpr); | |||
13385 | } | |||
13386 | ||||
13387 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. | |||
13388 | // We don't warn for 'assert(a || b && "bad")' since this is safe. | |||
13389 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { | |||
13390 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, LHSExpr, RHSExpr); | |||
13391 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, LHSExpr, RHSExpr); | |||
13392 | } | |||
13393 | ||||
13394 | if ((Opc == BO_Shl && LHSExpr->getType()->isIntegralType(Self.getASTContext())) | |||
13395 | || Opc == BO_Shr) { | |||
13396 | StringRef Shift = BinaryOperator::getOpcodeStr(Opc); | |||
13397 | DiagnoseAdditionInShift(Self, OpLoc, LHSExpr, Shift); | |||
13398 | DiagnoseAdditionInShift(Self, OpLoc, RHSExpr, Shift); | |||
13399 | } | |||
13400 | ||||
13401 | // Warn on overloaded shift operators and comparisons, such as: | |||
13402 | // cout << 5 == 4; | |||
13403 | if (BinaryOperator::isComparisonOp(Opc)) | |||
13404 | DiagnoseShiftCompare(Self, OpLoc, LHSExpr, RHSExpr); | |||
13405 | } | |||
13406 | ||||
13407 | // Binary Operators. 'Tok' is the token for the operator. | |||
13408 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, | |||
13409 | tok::TokenKind Kind, | |||
13410 | Expr *LHSExpr, Expr *RHSExpr) { | |||
13411 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); | |||
13412 | assert(LHSExpr && "ActOnBinOp(): missing left expression")((LHSExpr && "ActOnBinOp(): missing left expression") ? static_cast<void> (0) : __assert_fail ("LHSExpr && \"ActOnBinOp(): missing left expression\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 13412, __PRETTY_FUNCTION__)); | |||
13413 | assert(RHSExpr && "ActOnBinOp(): missing right expression")((RHSExpr && "ActOnBinOp(): missing right expression" ) ? static_cast<void> (0) : __assert_fail ("RHSExpr && \"ActOnBinOp(): missing right expression\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 13413, __PRETTY_FUNCTION__)); | |||
13414 | ||||
13415 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" | |||
13416 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, LHSExpr, RHSExpr); | |||
13417 | ||||
13418 | return BuildBinOp(S, TokLoc, Opc, LHSExpr, RHSExpr); | |||
13419 | } | |||
13420 | ||||
13421 | /// Build an overloaded binary operator expression in the given scope. | |||
13422 | static ExprResult BuildOverloadedBinOp(Sema &S, Scope *Sc, SourceLocation OpLoc, | |||
13423 | BinaryOperatorKind Opc, | |||
13424 | Expr *LHS, Expr *RHS) { | |||
13425 | switch (Opc) { | |||
13426 | case BO_Assign: | |||
13427 | case BO_DivAssign: | |||
13428 | case BO_RemAssign: | |||
13429 | case BO_SubAssign: | |||
13430 | case BO_AndAssign: | |||
13431 | case BO_OrAssign: | |||
13432 | case BO_XorAssign: | |||
13433 | DiagnoseSelfAssignment(S, LHS, RHS, OpLoc, false); | |||
13434 | CheckIdentityFieldAssignment(LHS, RHS, OpLoc, S); | |||
13435 | break; | |||
13436 | default: | |||
13437 | break; | |||
13438 | } | |||
13439 | ||||
13440 | // Find all of the overloaded operators visible from this | |||
13441 | // point. We perform both an operator-name lookup from the local | |||
13442 | // scope and an argument-dependent lookup based on the types of | |||
13443 | // the arguments. | |||
13444 | UnresolvedSet<16> Functions; | |||
13445 | OverloadedOperatorKind OverOp | |||
13446 | = BinaryOperator::getOverloadedOperator(Opc); | |||
13447 | if (Sc && OverOp != OO_None && OverOp != OO_Equal) | |||
13448 | S.LookupOverloadedOperatorName(OverOp, Sc, LHS->getType(), | |||
13449 | RHS->getType(), Functions); | |||
13450 | ||||
13451 | // In C++20 onwards, we may have a second operator to look up. | |||
13452 | if (S.getLangOpts().CPlusPlus2a) { | |||
13453 | if (OverloadedOperatorKind ExtraOp = getRewrittenOverloadedOperator(OverOp)) | |||
13454 | S.LookupOverloadedOperatorName(ExtraOp, Sc, LHS->getType(), | |||
13455 | RHS->getType(), Functions); | |||
13456 | } | |||
13457 | ||||
13458 | // Build the (potentially-overloaded, potentially-dependent) | |||
13459 | // binary operation. | |||
13460 | return S.CreateOverloadedBinOp(OpLoc, Opc, Functions, LHS, RHS); | |||
13461 | } | |||
13462 | ||||
13463 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, | |||
13464 | BinaryOperatorKind Opc, | |||
13465 | Expr *LHSExpr, Expr *RHSExpr) { | |||
13466 | ExprResult LHS, RHS; | |||
13467 | std::tie(LHS, RHS) = CorrectDelayedTyposInBinOp(*this, Opc, LHSExpr, RHSExpr); | |||
13468 | if (!LHS.isUsable() || !RHS.isUsable()) | |||
13469 | return ExprError(); | |||
13470 | LHSExpr = LHS.get(); | |||
13471 | RHSExpr = RHS.get(); | |||
13472 | ||||
13473 | // We want to end up calling one of checkPseudoObjectAssignment | |||
13474 | // (if the LHS is a pseudo-object), BuildOverloadedBinOp (if | |||
13475 | // both expressions are overloadable or either is type-dependent), | |||
13476 | // or CreateBuiltinBinOp (in any other case). We also want to get | |||
13477 | // any placeholder types out of the way. | |||
13478 | ||||
13479 | // Handle pseudo-objects in the LHS. | |||
13480 | if (const BuiltinType *pty = LHSExpr->getType()->getAsPlaceholderType()) { | |||
13481 | // Assignments with a pseudo-object l-value need special analysis. | |||
13482 | if (pty->getKind() == BuiltinType::PseudoObject && | |||
13483 | BinaryOperator::isAssignmentOp(Opc)) | |||
13484 | return checkPseudoObjectAssignment(S, OpLoc, Opc, LHSExpr, RHSExpr); | |||
13485 | ||||
13486 | // Don't resolve overloads if the other type is overloadable. | |||
13487 | if (getLangOpts().CPlusPlus && pty->getKind() == BuiltinType::Overload) { | |||
13488 | // We can't actually test that if we still have a placeholder, | |||
13489 | // though. Fortunately, none of the exceptions we see in that | |||
13490 | // code below are valid when the LHS is an overload set. Note | |||
13491 | // that an overload set can be dependently-typed, but it never | |||
13492 | // instantiates to having an overloadable type. | |||
13493 | ExprResult resolvedRHS = CheckPlaceholderExpr(RHSExpr); | |||
13494 | if (resolvedRHS.isInvalid()) return ExprError(); | |||
13495 | RHSExpr = resolvedRHS.get(); | |||
13496 | ||||
13497 | if (RHSExpr->isTypeDependent() || | |||
13498 | RHSExpr->getType()->isOverloadableType()) | |||
13499 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); | |||
13500 | } | |||
13501 | ||||
13502 | // If we're instantiating "a.x < b" or "A::x < b" and 'x' names a function | |||
13503 | // template, diagnose the missing 'template' keyword instead of diagnosing | |||
13504 | // an invalid use of a bound member function. | |||
13505 | // | |||
13506 | // Note that "A::x < b" might be valid if 'b' has an overloadable type due | |||
13507 | // to C++1z [over.over]/1.4, but we already checked for that case above. | |||
13508 | if (Opc == BO_LT && inTemplateInstantiation() && | |||
13509 | (pty->getKind() == BuiltinType::BoundMember || | |||
13510 | pty->getKind() == BuiltinType::Overload)) { | |||
13511 | auto *OE = dyn_cast<OverloadExpr>(LHSExpr); | |||
13512 | if (OE && !OE->hasTemplateKeyword() && !OE->hasExplicitTemplateArgs() && | |||
13513 | std::any_of(OE->decls_begin(), OE->decls_end(), [](NamedDecl *ND) { | |||
13514 | return isa<FunctionTemplateDecl>(ND); | |||
13515 | })) { | |||
13516 | Diag(OE->getQualifier() ? OE->getQualifierLoc().getBeginLoc() | |||
13517 | : OE->getNameLoc(), | |||
13518 | diag::err_template_kw_missing) | |||
13519 | << OE->getName().getAsString() << ""; | |||
13520 | return ExprError(); | |||
13521 | } | |||
13522 | } | |||
13523 | ||||
13524 | ExprResult LHS = CheckPlaceholderExpr(LHSExpr); | |||
13525 | if (LHS.isInvalid()) return ExprError(); | |||
13526 | LHSExpr = LHS.get(); | |||
13527 | } | |||
13528 | ||||
13529 | // Handle pseudo-objects in the RHS. | |||
13530 | if (const BuiltinType *pty = RHSExpr->getType()->getAsPlaceholderType()) { | |||
13531 | // An overload in the RHS can potentially be resolved by the type | |||
13532 | // being assigned to. | |||
13533 | if (Opc == BO_Assign && pty->getKind() == BuiltinType::Overload) { | |||
13534 | if (getLangOpts().CPlusPlus && | |||
13535 | (LHSExpr->isTypeDependent() || RHSExpr->isTypeDependent() || | |||
13536 | LHSExpr->getType()->isOverloadableType())) | |||
13537 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); | |||
13538 | ||||
13539 | return CreateBuiltinBinOp(OpLoc, Opc, LHSExpr, RHSExpr); | |||
13540 | } | |||
13541 | ||||
13542 | // Don't resolve overloads if the other type is overloadable. | |||
13543 | if (getLangOpts().CPlusPlus && pty->getKind() == BuiltinType::Overload && | |||
13544 | LHSExpr->getType()->isOverloadableType()) | |||
13545 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); | |||
13546 | ||||
13547 | ExprResult resolvedRHS = CheckPlaceholderExpr(RHSExpr); | |||
13548 | if (!resolvedRHS.isUsable()) return ExprError(); | |||
13549 | RHSExpr = resolvedRHS.get(); | |||
13550 | } | |||
13551 | ||||
13552 | if (getLangOpts().CPlusPlus) { | |||
13553 | // If either expression is type-dependent, always build an | |||
13554 | // overloaded op. | |||
13555 | if (LHSExpr->isTypeDependent() || RHSExpr->isTypeDependent()) | |||
13556 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); | |||
13557 | ||||
13558 | // Otherwise, build an overloaded op if either expression has an | |||
13559 | // overloadable type. | |||
13560 | if (LHSExpr->getType()->isOverloadableType() || | |||
13561 | RHSExpr->getType()->isOverloadableType()) | |||
13562 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); | |||
13563 | } | |||
13564 | ||||
13565 | // Build a built-in binary operation. | |||
13566 | return CreateBuiltinBinOp(OpLoc, Opc, LHSExpr, RHSExpr); | |||
13567 | } | |||
13568 | ||||
13569 | static bool isOverflowingIntegerType(ASTContext &Ctx, QualType T) { | |||
13570 | if (T.isNull() || T->isDependentType()) | |||
13571 | return false; | |||
13572 | ||||
13573 | if (!T->isPromotableIntegerType()) | |||
13574 | return true; | |||
13575 | ||||
13576 | return Ctx.getIntWidth(T) >= Ctx.getIntWidth(Ctx.IntTy); | |||
13577 | } | |||
13578 | ||||
13579 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, | |||
13580 | UnaryOperatorKind Opc, | |||
13581 | Expr *InputExpr) { | |||
13582 | ExprResult Input = InputExpr; | |||
13583 | ExprValueKind VK = VK_RValue; | |||
13584 | ExprObjectKind OK = OK_Ordinary; | |||
13585 | QualType resultType; | |||
13586 | bool CanOverflow = false; | |||
13587 | ||||
13588 | bool ConvertHalfVec = false; | |||
13589 | if (getLangOpts().OpenCL) { | |||
13590 | QualType Ty = InputExpr->getType(); | |||
13591 | // The only legal unary operation for atomics is '&'. | |||
13592 | if ((Opc != UO_AddrOf && Ty->isAtomicType()) || | |||
13593 | // OpenCL special types - image, sampler, pipe, and blocks are to be used | |||
13594 | // only with a builtin functions and therefore should be disallowed here. | |||
13595 | (Ty->isImageType() || Ty->isSamplerT() || Ty->isPipeType() | |||
13596 | || Ty->isBlockPointerType())) { | |||
13597 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) | |||
13598 | << InputExpr->getType() | |||
13599 | << Input.get()->getSourceRange()); | |||
13600 | } | |||
13601 | } | |||
13602 | // Diagnose operations on the unsupported types for OpenMP device compilation. | |||
13603 | if (getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice) { | |||
13604 | if (UnaryOperator::isIncrementDecrementOp(Opc) || | |||
13605 | UnaryOperator::isArithmeticOp(Opc)) | |||
13606 | checkOpenMPDeviceExpr(InputExpr); | |||
13607 | } | |||
13608 | ||||
13609 | switch (Opc) { | |||
13610 | case UO_PreInc: | |||
13611 | case UO_PreDec: | |||
13612 | case UO_PostInc: | |||
13613 | case UO_PostDec: | |||
13614 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OK, | |||
13615 | OpLoc, | |||
13616 | Opc == UO_PreInc || | |||
13617 | Opc == UO_PostInc, | |||
13618 | Opc == UO_PreInc || | |||
13619 | Opc == UO_PreDec); | |||
13620 | CanOverflow = isOverflowingIntegerType(Context, resultType); | |||
13621 | break; | |||
13622 | case UO_AddrOf: | |||
13623 | resultType = CheckAddressOfOperand(Input, OpLoc); | |||
13624 | CheckAddressOfNoDeref(InputExpr); | |||
13625 | RecordModifiableNonNullParam(*this, InputExpr); | |||
13626 | break; | |||
13627 | case UO_Deref: { | |||
13628 | Input = DefaultFunctionArrayLvalueConversion(Input.get()); | |||
13629 | if (Input.isInvalid()) return ExprError(); | |||
13630 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); | |||
13631 | break; | |||
13632 | } | |||
13633 | case UO_Plus: | |||
13634 | case UO_Minus: | |||
13635 | CanOverflow = Opc == UO_Minus && | |||
13636 | isOverflowingIntegerType(Context, Input.get()->getType()); | |||
13637 | Input = UsualUnaryConversions(Input.get()); | |||
13638 | if (Input.isInvalid()) return ExprError(); | |||
13639 | // Unary plus and minus require promoting an operand of half vector to a | |||
13640 | // float vector and truncating the result back to a half vector. For now, we | |||
13641 | // do this only when HalfArgsAndReturns is set (that is, when the target is | |||
13642 | // arm or arm64). | |||
13643 | ConvertHalfVec = | |||
13644 | needsConversionOfHalfVec(true, Context, Input.get()->getType()); | |||
13645 | ||||
13646 | // If the operand is a half vector, promote it to a float vector. | |||
13647 | if (ConvertHalfVec) | |||
13648 | Input = convertVector(Input.get(), Context.FloatTy, *this); | |||
13649 | resultType = Input.get()->getType(); | |||
13650 | if (resultType->isDependentType()) | |||
13651 | break; | |||
13652 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 | |||
13653 | break; | |||
13654 | else if (resultType->isVectorType() && | |||
13655 | // The z vector extensions don't allow + or - with bool vectors. | |||
13656 | (!Context.getLangOpts().ZVector || | |||
13657 | resultType->castAs<VectorType>()->getVectorKind() != | |||
13658 | VectorType::AltiVecBool)) | |||
13659 | break; | |||
13660 | else if (getLangOpts().CPlusPlus && // C++ [expr.unary.op]p6 | |||
13661 | Opc == UO_Plus && | |||
13662 | resultType->isPointerType()) | |||
13663 | break; | |||
13664 | ||||
13665 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) | |||
13666 | << resultType << Input.get()->getSourceRange()); | |||
13667 | ||||
13668 | case UO_Not: // bitwise complement | |||
13669 | Input = UsualUnaryConversions(Input.get()); | |||
13670 | if (Input.isInvalid()) | |||
13671 | return ExprError(); | |||
13672 | resultType = Input.get()->getType(); | |||
13673 | if (resultType->isDependentType()) | |||
13674 | break; | |||
13675 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. | |||
13676 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) | |||
13677 | // C99 does not support '~' for complex conjugation. | |||
13678 | Diag(OpLoc, diag::ext_integer_complement_complex) | |||
13679 | << resultType << Input.get()->getSourceRange(); | |||
13680 | else if (resultType->hasIntegerRepresentation()) | |||
13681 | break; | |||
13682 | else if (resultType->isExtVectorType() && Context.getLangOpts().OpenCL) { | |||
13683 | // OpenCL v1.1 s6.3.f: The bitwise operator not (~) does not operate | |||
13684 | // on vector float types. | |||
13685 | QualType T = resultType->castAs<ExtVectorType>()->getElementType(); | |||
13686 | if (!T->isIntegerType()) | |||
13687 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) | |||
13688 | << resultType << Input.get()->getSourceRange()); | |||
13689 | } else { | |||
13690 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) | |||
13691 | << resultType << Input.get()->getSourceRange()); | |||
13692 | } | |||
13693 | break; | |||
13694 | ||||
13695 | case UO_LNot: // logical negation | |||
13696 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). | |||
13697 | Input = DefaultFunctionArrayLvalueConversion(Input.get()); | |||
13698 | if (Input.isInvalid()) return ExprError(); | |||
13699 | resultType = Input.get()->getType(); | |||
13700 | ||||
13701 | // Though we still have to promote half FP to float... | |||
13702 | if (resultType->isHalfType() && !Context.getLangOpts().NativeHalfType) { | |||
13703 | Input = ImpCastExprToType(Input.get(), Context.FloatTy, CK_FloatingCast).get(); | |||
13704 | resultType = Context.FloatTy; | |||
13705 | } | |||
13706 | ||||
13707 | if (resultType->isDependentType()) | |||
13708 | break; | |||
13709 | if (resultType->isScalarType() && !isScopedEnumerationType(resultType)) { | |||
13710 | // C99 6.5.3.3p1: ok, fallthrough; | |||
13711 | if (Context.getLangOpts().CPlusPlus) { | |||
13712 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: | |||
13713 | // operand contextually converted to bool. | |||
13714 | Input = ImpCastExprToType(Input.get(), Context.BoolTy, | |||
13715 | ScalarTypeToBooleanCastKind(resultType)); | |||
13716 | } else if (Context.getLangOpts().OpenCL && | |||
13717 | Context.getLangOpts().OpenCLVersion < 120) { | |||
13718 | // OpenCL v1.1 6.3.h: The logical operator not (!) does not | |||
13719 | // operate on scalar float types. | |||
13720 | if (!resultType->isIntegerType() && !resultType->isPointerType()) | |||
13721 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) | |||
13722 | << resultType << Input.get()->getSourceRange()); | |||
13723 | } | |||
13724 | } else if (resultType->isExtVectorType()) { | |||
13725 | if (Context.getLangOpts().OpenCL && | |||
13726 | Context.getLangOpts().OpenCLVersion < 120 && | |||
13727 | !Context.getLangOpts().OpenCLCPlusPlus) { | |||
13728 | // OpenCL v1.1 6.3.h: The logical operator not (!) does not | |||
13729 | // operate on vector float types. | |||
13730 | QualType T = resultType->castAs<ExtVectorType>()->getElementType(); | |||
13731 | if (!T->isIntegerType()) | |||
13732 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) | |||
13733 | << resultType << Input.get()->getSourceRange()); | |||
13734 | } | |||
13735 | // Vector logical not returns the signed variant of the operand type. | |||
13736 | resultType = GetSignedVectorType(resultType); | |||
13737 | break; | |||
13738 | } else { | |||
13739 | // FIXME: GCC's vector extension permits the usage of '!' with a vector | |||
13740 | // type in C++. We should allow that here too. | |||
13741 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) | |||
13742 | << resultType << Input.get()->getSourceRange()); | |||
13743 | } | |||
13744 | ||||
13745 | // LNot always has type int. C99 6.5.3.3p5. | |||
13746 | // In C++, it's bool. C++ 5.3.1p8 | |||
13747 | resultType = Context.getLogicalOperationType(); | |||
13748 | break; | |||
13749 | case UO_Real: | |||
13750 | case UO_Imag: | |||
13751 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); | |||
13752 | // _Real maps ordinary l-values into ordinary l-values. _Imag maps ordinary | |||
13753 | // complex l-values to ordinary l-values and all other values to r-values. | |||
13754 | if (Input.isInvalid()) return ExprError(); | |||
13755 | if (Opc == UO_Real || Input.get()->getType()->isAnyComplexType()) { | |||
13756 | if (Input.get()->getValueKind() != VK_RValue && | |||
13757 | Input.get()->getObjectKind() == OK_Ordinary) | |||
13758 | VK = Input.get()->getValueKind(); | |||
13759 | } else if (!getLangOpts().CPlusPlus) { | |||
13760 | // In C, a volatile scalar is read by __imag. In C++, it is not. | |||
13761 | Input = DefaultLvalueConversion(Input.get()); | |||
13762 | } | |||
13763 | break; | |||
13764 | case UO_Extension: | |||
13765 | resultType = Input.get()->getType(); | |||
13766 | VK = Input.get()->getValueKind(); | |||
13767 | OK = Input.get()->getObjectKind(); | |||
13768 | break; | |||
13769 | case UO_Coawait: | |||
13770 | // It's unnecessary to represent the pass-through operator co_await in the | |||
13771 | // AST; just return the input expression instead. | |||
13772 | assert(!Input.get()->getType()->isDependentType() &&((!Input.get()->getType()->isDependentType() && "the co_await expression must be non-dependant before " "building operator co_await" ) ? static_cast<void> (0) : __assert_fail ("!Input.get()->getType()->isDependentType() && \"the co_await expression must be non-dependant before \" \"building operator co_await\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 13774, __PRETTY_FUNCTION__)) | |||
13773 | "the co_await expression must be non-dependant before "((!Input.get()->getType()->isDependentType() && "the co_await expression must be non-dependant before " "building operator co_await" ) ? static_cast<void> (0) : __assert_fail ("!Input.get()->getType()->isDependentType() && \"the co_await expression must be non-dependant before \" \"building operator co_await\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 13774, __PRETTY_FUNCTION__)) | |||
13774 | "building operator co_await")((!Input.get()->getType()->isDependentType() && "the co_await expression must be non-dependant before " "building operator co_await" ) ? static_cast<void> (0) : __assert_fail ("!Input.get()->getType()->isDependentType() && \"the co_await expression must be non-dependant before \" \"building operator co_await\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 13774, __PRETTY_FUNCTION__)); | |||
13775 | return Input; | |||
13776 | } | |||
13777 | if (resultType.isNull() || Input.isInvalid()) | |||
13778 | return ExprError(); | |||
13779 | ||||
13780 | // Check for array bounds violations in the operand of the UnaryOperator, | |||
13781 | // except for the '*' and '&' operators that have to be handled specially | |||
13782 | // by CheckArrayAccess (as there are special cases like &array[arraysize] | |||
13783 | // that are explicitly defined as valid by the standard). | |||
13784 | if (Opc != UO_AddrOf && Opc != UO_Deref) | |||
13785 | CheckArrayAccess(Input.get()); | |||
13786 | ||||
13787 | auto *UO = new (Context) | |||
13788 | UnaryOperator(Input.get(), Opc, resultType, VK, OK, OpLoc, CanOverflow); | |||
13789 | ||||
13790 | if (Opc == UO_Deref && UO->getType()->hasAttr(attr::NoDeref) && | |||
13791 | !isa<ArrayType>(UO->getType().getDesugaredType(Context))) | |||
13792 | ExprEvalContexts.back().PossibleDerefs.insert(UO); | |||
13793 | ||||
13794 | // Convert the result back to a half vector. | |||
13795 | if (ConvertHalfVec) | |||
13796 | return convertVector(UO, Context.HalfTy, *this); | |||
13797 | return UO; | |||
13798 | } | |||
13799 | ||||
13800 | /// Determine whether the given expression is a qualified member | |||
13801 | /// access expression, of a form that could be turned into a pointer to member | |||
13802 | /// with the address-of operator. | |||
13803 | bool Sema::isQualifiedMemberAccess(Expr *E) { | |||
13804 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { | |||
13805 | if (!DRE->getQualifier()) | |||
13806 | return false; | |||
13807 | ||||
13808 | ValueDecl *VD = DRE->getDecl(); | |||
13809 | if (!VD->isCXXClassMember()) | |||
13810 | return false; | |||
13811 | ||||
13812 | if (isa<FieldDecl>(VD) || isa<IndirectFieldDecl>(VD)) | |||
13813 | return true; | |||
13814 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(VD)) | |||
13815 | return Method->isInstance(); | |||
13816 | ||||
13817 | return false; | |||
13818 | } | |||
13819 | ||||
13820 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { | |||
13821 | if (!ULE->getQualifier()) | |||
13822 | return false; | |||
13823 | ||||
13824 | for (NamedDecl *D : ULE->decls()) { | |||
13825 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { | |||
13826 | if (Method->isInstance()) | |||
13827 | return true; | |||
13828 | } else { | |||
13829 | // Overload set does not contain methods. | |||
13830 | break; | |||
13831 | } | |||
13832 | } | |||
13833 | ||||
13834 | return false; | |||
13835 | } | |||
13836 | ||||
13837 | return false; | |||
13838 | } | |||
13839 | ||||
13840 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, | |||
13841 | UnaryOperatorKind Opc, Expr *Input) { | |||
13842 | // First things first: handle placeholders so that the | |||
13843 | // overloaded-operator check considers the right type. | |||
13844 | if (const BuiltinType *pty = Input->getType()->getAsPlaceholderType()) { | |||
13845 | // Increment and decrement of pseudo-object references. | |||
13846 | if (pty->getKind() == BuiltinType::PseudoObject && | |||
13847 | UnaryOperator::isIncrementDecrementOp(Opc)) | |||
13848 | return checkPseudoObjectIncDec(S, OpLoc, Opc, Input); | |||
13849 | ||||
13850 | // extension is always a builtin operator. | |||
13851 | if (Opc == UO_Extension) | |||
13852 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); | |||
13853 | ||||
13854 | // & gets special logic for several kinds of placeholder. | |||
13855 | // The builtin code knows what to do. | |||
13856 | if (Opc == UO_AddrOf && | |||
13857 | (pty->getKind() == BuiltinType::Overload || | |||
13858 | pty->getKind() == BuiltinType::UnknownAny || | |||
13859 | pty->getKind() == BuiltinType::BoundMember)) | |||
13860 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); | |||
13861 | ||||
13862 | // Anything else needs to be handled now. | |||
13863 | ExprResult Result = CheckPlaceholderExpr(Input); | |||
13864 | if (Result.isInvalid()) return ExprError(); | |||
13865 | Input = Result.get(); | |||
13866 | } | |||
13867 | ||||
13868 | if (getLangOpts().CPlusPlus && Input->getType()->isOverloadableType() && | |||
13869 | UnaryOperator::getOverloadedOperator(Opc) != OO_None && | |||
13870 | !(Opc == UO_AddrOf && isQualifiedMemberAccess(Input))) { | |||
13871 | // Find all of the overloaded operators visible from this | |||
13872 | // point. We perform both an operator-name lookup from the local | |||
13873 | // scope and an argument-dependent lookup based on the types of | |||
13874 | // the arguments. | |||
13875 | UnresolvedSet<16> Functions; | |||
13876 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); | |||
13877 | if (S && OverOp != OO_None) | |||
13878 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), | |||
13879 | Functions); | |||
13880 | ||||
13881 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); | |||
13882 | } | |||
13883 | ||||
13884 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); | |||
13885 | } | |||
13886 | ||||
13887 | // Unary Operators. 'Tok' is the token for the operator. | |||
13888 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, | |||
13889 | tok::TokenKind Op, Expr *Input) { | |||
13890 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); | |||
13891 | } | |||
13892 | ||||
13893 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". | |||
13894 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, | |||
13895 | LabelDecl *TheDecl) { | |||
13896 | TheDecl->markUsed(Context); | |||
13897 | // Create the AST node. The address of a label always has type 'void*'. | |||
13898 | return new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, | |||
13899 | Context.getPointerType(Context.VoidTy)); | |||
13900 | } | |||
13901 | ||||
13902 | void Sema::ActOnStartStmtExpr() { | |||
13903 | PushExpressionEvaluationContext(ExprEvalContexts.back().Context); | |||
13904 | } | |||
13905 | ||||
13906 | void Sema::ActOnStmtExprError() { | |||
13907 | // Note that function is also called by TreeTransform when leaving a | |||
13908 | // StmtExpr scope without rebuilding anything. | |||
13909 | ||||
13910 | DiscardCleanupsInEvaluationContext(); | |||
13911 | PopExpressionEvaluationContext(); | |||
13912 | } | |||
13913 | ||||
13914 | ExprResult | |||
13915 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, | |||
13916 | SourceLocation RPLoc) { // "({..})" | |||
13917 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!")((SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!") ? static_cast<void> (0) : __assert_fail ("SubStmt && isa<CompoundStmt>(SubStmt) && \"Invalid action invocation!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 13917, __PRETTY_FUNCTION__)); | |||
13918 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); | |||
13919 | ||||
13920 | if (hasAnyUnrecoverableErrorsInThisFunction()) | |||
13921 | DiscardCleanupsInEvaluationContext(); | |||
13922 | assert(!Cleanup.exprNeedsCleanups() &&((!Cleanup.exprNeedsCleanups() && "cleanups within StmtExpr not correctly bound!" ) ? static_cast<void> (0) : __assert_fail ("!Cleanup.exprNeedsCleanups() && \"cleanups within StmtExpr not correctly bound!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 13923, __PRETTY_FUNCTION__)) | |||
13923 | "cleanups within StmtExpr not correctly bound!")((!Cleanup.exprNeedsCleanups() && "cleanups within StmtExpr not correctly bound!" ) ? static_cast<void> (0) : __assert_fail ("!Cleanup.exprNeedsCleanups() && \"cleanups within StmtExpr not correctly bound!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 13923, __PRETTY_FUNCTION__)); | |||
13924 | PopExpressionEvaluationContext(); | |||
13925 | ||||
13926 | // FIXME: there are a variety of strange constraints to enforce here, for | |||
13927 | // example, it is not possible to goto into a stmt expression apparently. | |||
13928 | // More semantic analysis is needed. | |||
13929 | ||||
13930 | // If there are sub-stmts in the compound stmt, take the type of the last one | |||
13931 | // as the type of the stmtexpr. | |||
13932 | QualType Ty = Context.VoidTy; | |||
13933 | bool StmtExprMayBindToTemp = false; | |||
13934 | if (!Compound->body_empty()) { | |||
13935 | // For GCC compatibility we get the last Stmt excluding trailing NullStmts. | |||
13936 | if (const auto *LastStmt = | |||
13937 | dyn_cast<ValueStmt>(Compound->getStmtExprResult())) { | |||
13938 | if (const Expr *Value = LastStmt->getExprStmt()) { | |||
13939 | StmtExprMayBindToTemp = true; | |||
13940 | Ty = Value->getType(); | |||
13941 | } | |||
13942 | } | |||
13943 | } | |||
13944 | ||||
13945 | // FIXME: Check that expression type is complete/non-abstract; statement | |||
13946 | // expressions are not lvalues. | |||
13947 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); | |||
13948 | if (StmtExprMayBindToTemp) | |||
13949 | return MaybeBindToTemporary(ResStmtExpr); | |||
13950 | return ResStmtExpr; | |||
13951 | } | |||
13952 | ||||
13953 | ExprResult Sema::ActOnStmtExprResult(ExprResult ER) { | |||
13954 | if (ER.isInvalid()) | |||
13955 | return ExprError(); | |||
13956 | ||||
13957 | // Do function/array conversion on the last expression, but not | |||
13958 | // lvalue-to-rvalue. However, initialize an unqualified type. | |||
13959 | ER = DefaultFunctionArrayConversion(ER.get()); | |||
13960 | if (ER.isInvalid()) | |||
13961 | return ExprError(); | |||
13962 | Expr *E = ER.get(); | |||
13963 | ||||
13964 | if (E->isTypeDependent()) | |||
13965 | return E; | |||
13966 | ||||
13967 | // In ARC, if the final expression ends in a consume, splice | |||
13968 | // the consume out and bind it later. In the alternate case | |||
13969 | // (when dealing with a retainable type), the result | |||
13970 | // initialization will create a produce. In both cases the | |||
13971 | // result will be +1, and we'll need to balance that out with | |||
13972 | // a bind. | |||
13973 | auto *Cast = dyn_cast<ImplicitCastExpr>(E); | |||
13974 | if (Cast && Cast->getCastKind() == CK_ARCConsumeObject) | |||
13975 | return Cast->getSubExpr(); | |||
13976 | ||||
13977 | // FIXME: Provide a better location for the initialization. | |||
13978 | return PerformCopyInitialization( | |||
13979 | InitializedEntity::InitializeStmtExprResult( | |||
13980 | E->getBeginLoc(), E->getType().getUnqualifiedType()), | |||
13981 | SourceLocation(), E); | |||
13982 | } | |||
13983 | ||||
13984 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, | |||
13985 | TypeSourceInfo *TInfo, | |||
13986 | ArrayRef<OffsetOfComponent> Components, | |||
13987 | SourceLocation RParenLoc) { | |||
13988 | QualType ArgTy = TInfo->getType(); | |||
13989 | bool Dependent = ArgTy->isDependentType(); | |||
13990 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); | |||
13991 | ||||
13992 | // We must have at least one component that refers to the type, and the first | |||
13993 | // one is known to be a field designator. Verify that the ArgTy represents | |||
13994 | // a struct/union/class. | |||
13995 | if (!Dependent && !ArgTy->isRecordType()) | |||
13996 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) | |||
13997 | << ArgTy << TypeRange); | |||
13998 | ||||
13999 | // Type must be complete per C99 7.17p3 because a declaring a variable | |||
14000 | // with an incomplete type would be ill-formed. | |||
14001 | if (!Dependent | |||
14002 | && RequireCompleteType(BuiltinLoc, ArgTy, | |||
14003 | diag::err_offsetof_incomplete_type, TypeRange)) | |||
14004 | return ExprError(); | |||
14005 | ||||
14006 | bool DidWarnAboutNonPOD = false; | |||
14007 | QualType CurrentType = ArgTy; | |||
14008 | SmallVector<OffsetOfNode, 4> Comps; | |||
14009 | SmallVector<Expr*, 4> Exprs; | |||
14010 | for (const OffsetOfComponent &OC : Components) { | |||
14011 | if (OC.isBrackets) { | |||
14012 | // Offset of an array sub-field. TODO: Should we allow vector elements? | |||
14013 | if (!CurrentType->isDependentType()) { | |||
14014 | const ArrayType *AT = Context.getAsArrayType(CurrentType); | |||
14015 | if(!AT) | |||
14016 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) | |||
14017 | << CurrentType); | |||
14018 | CurrentType = AT->getElementType(); | |||
14019 | } else | |||
14020 | CurrentType = Context.DependentTy; | |||
14021 | ||||
14022 | ExprResult IdxRval = DefaultLvalueConversion(static_cast<Expr*>(OC.U.E)); | |||
14023 | if (IdxRval.isInvalid()) | |||
14024 | return ExprError(); | |||
14025 | Expr *Idx = IdxRval.get(); | |||
14026 | ||||
14027 | // The expression must be an integral expression. | |||
14028 | // FIXME: An integral constant expression? | |||
14029 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && | |||
14030 | !Idx->getType()->isIntegerType()) | |||
14031 | return ExprError( | |||
14032 | Diag(Idx->getBeginLoc(), diag::err_typecheck_subscript_not_integer) | |||
14033 | << Idx->getSourceRange()); | |||
14034 | ||||
14035 | // Record this array index. | |||
14036 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); | |||
14037 | Exprs.push_back(Idx); | |||
14038 | continue; | |||
14039 | } | |||
14040 | ||||
14041 | // Offset of a field. | |||
14042 | if (CurrentType->isDependentType()) { | |||
14043 | // We have the offset of a field, but we can't look into the dependent | |||
14044 | // type. Just record the identifier of the field. | |||
14045 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); | |||
14046 | CurrentType = Context.DependentTy; | |||
14047 | continue; | |||
14048 | } | |||
14049 | ||||
14050 | // We need to have a complete type to look into. | |||
14051 | if (RequireCompleteType(OC.LocStart, CurrentType, | |||
14052 | diag::err_offsetof_incomplete_type)) | |||
14053 | return ExprError(); | |||
14054 | ||||
14055 | // Look for the designated field. | |||
14056 | const RecordType *RC = CurrentType->getAs<RecordType>(); | |||
14057 | if (!RC) | |||
14058 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) | |||
14059 | << CurrentType); | |||
14060 | RecordDecl *RD = RC->getDecl(); | |||
14061 | ||||
14062 | // C++ [lib.support.types]p5: | |||
14063 | // The macro offsetof accepts a restricted set of type arguments in this | |||
14064 | // International Standard. type shall be a POD structure or a POD union | |||
14065 | // (clause 9). | |||
14066 | // C++11 [support.types]p4: | |||
14067 | // If type is not a standard-layout class (Clause 9), the results are | |||
14068 | // undefined. | |||
14069 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { | |||
14070 | bool IsSafe = LangOpts.CPlusPlus11? CRD->isStandardLayout() : CRD->isPOD(); | |||
14071 | unsigned DiagID = | |||
14072 | LangOpts.CPlusPlus11? diag::ext_offsetof_non_standardlayout_type | |||
14073 | : diag::ext_offsetof_non_pod_type; | |||
14074 | ||||
14075 | if (!IsSafe && !DidWarnAboutNonPOD && | |||
14076 | DiagRuntimeBehavior(BuiltinLoc, nullptr, | |||
14077 | PDiag(DiagID) | |||
14078 | << SourceRange(Components[0].LocStart, OC.LocEnd) | |||
14079 | << CurrentType)) | |||
14080 | DidWarnAboutNonPOD = true; | |||
14081 | } | |||
14082 | ||||
14083 | // Look for the field. | |||
14084 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); | |||
14085 | LookupQualifiedName(R, RD); | |||
14086 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); | |||
14087 | IndirectFieldDecl *IndirectMemberDecl = nullptr; | |||
14088 | if (!MemberDecl) { | |||
14089 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) | |||
14090 | MemberDecl = IndirectMemberDecl->getAnonField(); | |||
14091 | } | |||
14092 | ||||
14093 | if (!MemberDecl) | |||
14094 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) | |||
14095 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, | |||
14096 | OC.LocEnd)); | |||
14097 | ||||
14098 | // C99 7.17p3: | |||
14099 | // (If the specified member is a bit-field, the behavior is undefined.) | |||
14100 | // | |||
14101 | // We diagnose this as an error. | |||
14102 | if (MemberDecl->isBitField()) { | |||
14103 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) | |||
14104 | << MemberDecl->getDeclName() | |||
14105 | << SourceRange(BuiltinLoc, RParenLoc); | |||
14106 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); | |||
14107 | return ExprError(); | |||
14108 | } | |||
14109 | ||||
14110 | RecordDecl *Parent = MemberDecl->getParent(); | |||
14111 | if (IndirectMemberDecl) | |||
14112 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); | |||
14113 | ||||
14114 | // If the member was found in a base class, introduce OffsetOfNodes for | |||
14115 | // the base class indirections. | |||
14116 | CXXBasePaths Paths; | |||
14117 | if (IsDerivedFrom(OC.LocStart, CurrentType, Context.getTypeDeclType(Parent), | |||
14118 | Paths)) { | |||
14119 | if (Paths.getDetectedVirtual()) { | |||
14120 | Diag(OC.LocEnd, diag::err_offsetof_field_of_virtual_base) | |||
14121 | << MemberDecl->getDeclName() | |||
14122 | << SourceRange(BuiltinLoc, RParenLoc); | |||
14123 | return ExprError(); | |||
14124 | } | |||
14125 | ||||
14126 | CXXBasePath &Path = Paths.front(); | |||
14127 | for (const CXXBasePathElement &B : Path) | |||
14128 | Comps.push_back(OffsetOfNode(B.Base)); | |||
14129 | } | |||
14130 | ||||
14131 | if (IndirectMemberDecl) { | |||
14132 | for (auto *FI : IndirectMemberDecl->chain()) { | |||
14133 | assert(isa<FieldDecl>(FI))((isa<FieldDecl>(FI)) ? static_cast<void> (0) : __assert_fail ("isa<FieldDecl>(FI)", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 14133, __PRETTY_FUNCTION__)); | |||
14134 | Comps.push_back(OffsetOfNode(OC.LocStart, | |||
14135 | cast<FieldDecl>(FI), OC.LocEnd)); | |||
14136 | } | |||
14137 | } else | |||
14138 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); | |||
14139 | ||||
14140 | CurrentType = MemberDecl->getType().getNonReferenceType(); | |||
14141 | } | |||
14142 | ||||
14143 | return OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, TInfo, | |||
14144 | Comps, Exprs, RParenLoc); | |||
14145 | } | |||
14146 | ||||
14147 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, | |||
14148 | SourceLocation BuiltinLoc, | |||
14149 | SourceLocation TypeLoc, | |||
14150 | ParsedType ParsedArgTy, | |||
14151 | ArrayRef<OffsetOfComponent> Components, | |||
14152 | SourceLocation RParenLoc) { | |||
14153 | ||||
14154 | TypeSourceInfo *ArgTInfo; | |||
14155 | QualType ArgTy = GetTypeFromParser(ParsedArgTy, &ArgTInfo); | |||
14156 | if (ArgTy.isNull()) | |||
14157 | return ExprError(); | |||
14158 | ||||
14159 | if (!ArgTInfo) | |||
14160 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); | |||
14161 | ||||
14162 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, Components, RParenLoc); | |||
14163 | } | |||
14164 | ||||
14165 | ||||
14166 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, | |||
14167 | Expr *CondExpr, | |||
14168 | Expr *LHSExpr, Expr *RHSExpr, | |||
14169 | SourceLocation RPLoc) { | |||
14170 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)")(((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)") ? static_cast<void> (0) : __assert_fail ("(CondExpr && LHSExpr && RHSExpr) && \"Missing type argument(s)\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 14170, __PRETTY_FUNCTION__)); | |||
14171 | ||||
14172 | ExprValueKind VK = VK_RValue; | |||
14173 | ExprObjectKind OK = OK_Ordinary; | |||
14174 | QualType resType; | |||
14175 | bool ValueDependent = false; | |||
14176 | bool CondIsTrue = false; | |||
14177 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { | |||
14178 | resType = Context.DependentTy; | |||
14179 | ValueDependent = true; | |||
14180 | } else { | |||
14181 | // The conditional expression is required to be a constant expression. | |||
14182 | llvm::APSInt condEval(32); | |||
14183 | ExprResult CondICE | |||
14184 | = VerifyIntegerConstantExpression(CondExpr, &condEval, | |||
14185 | diag::err_typecheck_choose_expr_requires_constant, false); | |||
14186 | if (CondICE.isInvalid()) | |||
14187 | return ExprError(); | |||
14188 | CondExpr = CondICE.get(); | |||
14189 | CondIsTrue = condEval.getZExtValue(); | |||
14190 | ||||
14191 | // If the condition is > zero, then the AST type is the same as the LHSExpr. | |||
14192 | Expr *ActiveExpr = CondIsTrue ? LHSExpr : RHSExpr; | |||
14193 | ||||
14194 | resType = ActiveExpr->getType(); | |||
14195 | ValueDependent = ActiveExpr->isValueDependent(); | |||
14196 | VK = ActiveExpr->getValueKind(); | |||
14197 | OK = ActiveExpr->getObjectKind(); | |||
14198 | } | |||
14199 | ||||
14200 | return new (Context) | |||
14201 | ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, VK, OK, RPLoc, | |||
14202 | CondIsTrue, resType->isDependentType(), ValueDependent); | |||
14203 | } | |||
14204 | ||||
14205 | //===----------------------------------------------------------------------===// | |||
14206 | // Clang Extensions. | |||
14207 | //===----------------------------------------------------------------------===// | |||
14208 | ||||
14209 | /// ActOnBlockStart - This callback is invoked when a block literal is started. | |||
14210 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *CurScope) { | |||
14211 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); | |||
14212 | ||||
14213 | if (LangOpts.CPlusPlus) { | |||
14214 | MangleNumberingContext *MCtx; | |||
14215 | Decl *ManglingContextDecl; | |||
14216 | std::tie(MCtx, ManglingContextDecl) = | |||
14217 | getCurrentMangleNumberContext(Block->getDeclContext()); | |||
14218 | if (MCtx) { | |||
14219 | unsigned ManglingNumber = MCtx->getManglingNumber(Block); | |||
14220 | Block->setBlockMangling(ManglingNumber, ManglingContextDecl); | |||
14221 | } | |||
14222 | } | |||
14223 | ||||
14224 | PushBlockScope(CurScope, Block); | |||
14225 | CurContext->addDecl(Block); | |||
14226 | if (CurScope) | |||
14227 | PushDeclContext(CurScope, Block); | |||
14228 | else | |||
14229 | CurContext = Block; | |||
14230 | ||||
14231 | getCurBlock()->HasImplicitReturnType = true; | |||
14232 | ||||
14233 | // Enter a new evaluation context to insulate the block from any | |||
14234 | // cleanups from the enclosing full-expression. | |||
14235 | PushExpressionEvaluationContext( | |||
14236 | ExpressionEvaluationContext::PotentiallyEvaluated); | |||
14237 | } | |||
14238 | ||||
14239 | void Sema::ActOnBlockArguments(SourceLocation CaretLoc, Declarator &ParamInfo, | |||
14240 | Scope *CurScope) { | |||
14241 | assert(ParamInfo.getIdentifier() == nullptr &&((ParamInfo.getIdentifier() == nullptr && "block-id should have no identifier!" ) ? static_cast<void> (0) : __assert_fail ("ParamInfo.getIdentifier() == nullptr && \"block-id should have no identifier!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 14242, __PRETTY_FUNCTION__)) | |||
14242 | "block-id should have no identifier!")((ParamInfo.getIdentifier() == nullptr && "block-id should have no identifier!" ) ? static_cast<void> (0) : __assert_fail ("ParamInfo.getIdentifier() == nullptr && \"block-id should have no identifier!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 14242, __PRETTY_FUNCTION__)); | |||
14243 | assert(ParamInfo.getContext() == DeclaratorContext::BlockLiteralContext)((ParamInfo.getContext() == DeclaratorContext::BlockLiteralContext ) ? static_cast<void> (0) : __assert_fail ("ParamInfo.getContext() == DeclaratorContext::BlockLiteralContext" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 14243, __PRETTY_FUNCTION__)); | |||
14244 | BlockScopeInfo *CurBlock = getCurBlock(); | |||
14245 | ||||
14246 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); | |||
14247 | QualType T = Sig->getType(); | |||
14248 | ||||
14249 | // FIXME: We should allow unexpanded parameter packs here, but that would, | |||
14250 | // in turn, make the block expression contain unexpanded parameter packs. | |||
14251 | if (DiagnoseUnexpandedParameterPack(CaretLoc, Sig, UPPC_Block)) { | |||
14252 | // Drop the parameters. | |||
14253 | FunctionProtoType::ExtProtoInfo EPI; | |||
14254 | EPI.HasTrailingReturn = false; | |||
14255 | EPI.TypeQuals.addConst(); | |||
14256 | T = Context.getFunctionType(Context.DependentTy, None, EPI); | |||
14257 | Sig = Context.getTrivialTypeSourceInfo(T); | |||
14258 | } | |||
14259 | ||||
14260 | // GetTypeForDeclarator always produces a function type for a block | |||
14261 | // literal signature. Furthermore, it is always a FunctionProtoType | |||
14262 | // unless the function was written with a typedef. | |||
14263 | assert(T->isFunctionType() &&((T->isFunctionType() && "GetTypeForDeclarator made a non-function block signature" ) ? static_cast<void> (0) : __assert_fail ("T->isFunctionType() && \"GetTypeForDeclarator made a non-function block signature\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 14264, __PRETTY_FUNCTION__)) | |||
14264 | "GetTypeForDeclarator made a non-function block signature")((T->isFunctionType() && "GetTypeForDeclarator made a non-function block signature" ) ? static_cast<void> (0) : __assert_fail ("T->isFunctionType() && \"GetTypeForDeclarator made a non-function block signature\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 14264, __PRETTY_FUNCTION__)); | |||
14265 | ||||
14266 | // Look for an explicit signature in that function type. | |||
14267 | FunctionProtoTypeLoc ExplicitSignature; | |||
14268 | ||||
14269 | if ((ExplicitSignature = Sig->getTypeLoc() | |||
14270 | .getAsAdjusted<FunctionProtoTypeLoc>())) { | |||
14271 | ||||
14272 | // Check whether that explicit signature was synthesized by | |||
14273 | // GetTypeForDeclarator. If so, don't save that as part of the | |||
14274 | // written signature. | |||
14275 | if (ExplicitSignature.getLocalRangeBegin() == | |||
14276 | ExplicitSignature.getLocalRangeEnd()) { | |||
14277 | // This would be much cheaper if we stored TypeLocs instead of | |||
14278 | // TypeSourceInfos. | |||
14279 | TypeLoc Result = ExplicitSignature.getReturnLoc(); | |||
14280 | unsigned Size = Result.getFullDataSize(); | |||
14281 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); | |||
14282 | Sig->getTypeLoc().initializeFullCopy(Result, Size); | |||
14283 | ||||
14284 | ExplicitSignature = FunctionProtoTypeLoc(); | |||
14285 | } | |||
14286 | } | |||
14287 | ||||
14288 | CurBlock->TheDecl->setSignatureAsWritten(Sig); | |||
14289 | CurBlock->FunctionType = T; | |||
14290 | ||||
14291 | const FunctionType *Fn = T->getAs<FunctionType>(); | |||
14292 | QualType RetTy = Fn->getReturnType(); | |||
14293 | bool isVariadic = | |||
14294 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); | |||
14295 | ||||
14296 | CurBlock->TheDecl->setIsVariadic(isVariadic); | |||
14297 | ||||
14298 | // Context.DependentTy is used as a placeholder for a missing block | |||
14299 | // return type. TODO: what should we do with declarators like: | |||
14300 | // ^ * { ... } | |||
14301 | // If the answer is "apply template argument deduction".... | |||
14302 | if (RetTy != Context.DependentTy) { | |||
14303 | CurBlock->ReturnType = RetTy; | |||
14304 | CurBlock->TheDecl->setBlockMissingReturnType(false); | |||
14305 | CurBlock->HasImplicitReturnType = false; | |||
14306 | } | |||
14307 | ||||
14308 | // Push block parameters from the declarator if we had them. | |||
14309 | SmallVector<ParmVarDecl*, 8> Params; | |||
14310 | if (ExplicitSignature) { | |||
14311 | for (unsigned I = 0, E = ExplicitSignature.getNumParams(); I != E; ++I) { | |||
14312 | ParmVarDecl *Param = ExplicitSignature.getParam(I); | |||
14313 | if (Param->getIdentifier() == nullptr && | |||
14314 | !Param->isImplicit() && | |||
14315 | !Param->isInvalidDecl() && | |||
14316 | !getLangOpts().CPlusPlus) | |||
14317 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); | |||
14318 | Params.push_back(Param); | |||
14319 | } | |||
14320 | ||||
14321 | // Fake up parameter variables if we have a typedef, like | |||
14322 | // ^ fntype { ... } | |||
14323 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { | |||
14324 | for (const auto &I : Fn->param_types()) { | |||
14325 | ParmVarDecl *Param = BuildParmVarDeclForTypedef( | |||
14326 | CurBlock->TheDecl, ParamInfo.getBeginLoc(), I); | |||
14327 | Params.push_back(Param); | |||
14328 | } | |||
14329 | } | |||
14330 | ||||
14331 | // Set the parameters on the block decl. | |||
14332 | if (!Params.empty()) { | |||
14333 | CurBlock->TheDecl->setParams(Params); | |||
14334 | CheckParmsForFunctionDef(CurBlock->TheDecl->parameters(), | |||
14335 | /*CheckParameterNames=*/false); | |||
14336 | } | |||
14337 | ||||
14338 | // Finally we can process decl attributes. | |||
14339 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); | |||
14340 | ||||
14341 | // Put the parameter variables in scope. | |||
14342 | for (auto AI : CurBlock->TheDecl->parameters()) { | |||
14343 | AI->setOwningFunction(CurBlock->TheDecl); | |||
14344 | ||||
14345 | // If this has an identifier, add it to the scope stack. | |||
14346 | if (AI->getIdentifier()) { | |||
14347 | CheckShadow(CurBlock->TheScope, AI); | |||
14348 | ||||
14349 | PushOnScopeChains(AI, CurBlock->TheScope); | |||
14350 | } | |||
14351 | } | |||
14352 | } | |||
14353 | ||||
14354 | /// ActOnBlockError - If there is an error parsing a block, this callback | |||
14355 | /// is invoked to pop the information about the block from the action impl. | |||
14356 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { | |||
14357 | // Leave the expression-evaluation context. | |||
14358 | DiscardCleanupsInEvaluationContext(); | |||
14359 | PopExpressionEvaluationContext(); | |||
14360 | ||||
14361 | // Pop off CurBlock, handle nested blocks. | |||
14362 | PopDeclContext(); | |||
14363 | PopFunctionScopeInfo(); | |||
14364 | } | |||
14365 | ||||
14366 | /// ActOnBlockStmtExpr - This is called when the body of a block statement | |||
14367 | /// literal was successfully completed. ^(int x){...} | |||
14368 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, | |||
14369 | Stmt *Body, Scope *CurScope) { | |||
14370 | // If blocks are disabled, emit an error. | |||
14371 | if (!LangOpts.Blocks) | |||
14372 | Diag(CaretLoc, diag::err_blocks_disable) << LangOpts.OpenCL; | |||
14373 | ||||
14374 | // Leave the expression-evaluation context. | |||
14375 | if (hasAnyUnrecoverableErrorsInThisFunction()) | |||
14376 | DiscardCleanupsInEvaluationContext(); | |||
14377 | assert(!Cleanup.exprNeedsCleanups() &&((!Cleanup.exprNeedsCleanups() && "cleanups within block not correctly bound!" ) ? static_cast<void> (0) : __assert_fail ("!Cleanup.exprNeedsCleanups() && \"cleanups within block not correctly bound!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 14378, __PRETTY_FUNCTION__)) | |||
14378 | "cleanups within block not correctly bound!")((!Cleanup.exprNeedsCleanups() && "cleanups within block not correctly bound!" ) ? static_cast<void> (0) : __assert_fail ("!Cleanup.exprNeedsCleanups() && \"cleanups within block not correctly bound!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 14378, __PRETTY_FUNCTION__)); | |||
14379 | PopExpressionEvaluationContext(); | |||
14380 | ||||
14381 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); | |||
14382 | BlockDecl *BD = BSI->TheDecl; | |||
14383 | ||||
14384 | if (BSI->HasImplicitReturnType) | |||
14385 | deduceClosureReturnType(*BSI); | |||
14386 | ||||
14387 | QualType RetTy = Context.VoidTy; | |||
14388 | if (!BSI->ReturnType.isNull()) | |||
14389 | RetTy = BSI->ReturnType; | |||
14390 | ||||
14391 | bool NoReturn = BD->hasAttr<NoReturnAttr>(); | |||
14392 | QualType BlockTy; | |||
14393 | ||||
14394 | // If the user wrote a function type in some form, try to use that. | |||
14395 | if (!BSI->FunctionType.isNull()) { | |||
14396 | const FunctionType *FTy = BSI->FunctionType->castAs<FunctionType>(); | |||
14397 | ||||
14398 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); | |||
14399 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); | |||
14400 | ||||
14401 | // Turn protoless block types into nullary block types. | |||
14402 | if (isa<FunctionNoProtoType>(FTy)) { | |||
14403 | FunctionProtoType::ExtProtoInfo EPI; | |||
14404 | EPI.ExtInfo = Ext; | |||
14405 | BlockTy = Context.getFunctionType(RetTy, None, EPI); | |||
14406 | ||||
14407 | // Otherwise, if we don't need to change anything about the function type, | |||
14408 | // preserve its sugar structure. | |||
14409 | } else if (FTy->getReturnType() == RetTy && | |||
14410 | (!NoReturn || FTy->getNoReturnAttr())) { | |||
14411 | BlockTy = BSI->FunctionType; | |||
14412 | ||||
14413 | // Otherwise, make the minimal modifications to the function type. | |||
14414 | } else { | |||
14415 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); | |||
14416 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); | |||
14417 | EPI.TypeQuals = Qualifiers(); | |||
14418 | EPI.ExtInfo = Ext; | |||
14419 | BlockTy = Context.getFunctionType(RetTy, FPT->getParamTypes(), EPI); | |||
14420 | } | |||
14421 | ||||
14422 | // If we don't have a function type, just build one from nothing. | |||
14423 | } else { | |||
14424 | FunctionProtoType::ExtProtoInfo EPI; | |||
14425 | EPI.ExtInfo = FunctionType::ExtInfo().withNoReturn(NoReturn); | |||
14426 | BlockTy = Context.getFunctionType(RetTy, None, EPI); | |||
14427 | } | |||
14428 | ||||
14429 | DiagnoseUnusedParameters(BD->parameters()); | |||
14430 | BlockTy = Context.getBlockPointerType(BlockTy); | |||
14431 | ||||
14432 | // If needed, diagnose invalid gotos and switches in the block. | |||
14433 | if (getCurFunction()->NeedsScopeChecking() && | |||
14434 | !PP.isCodeCompletionEnabled()) | |||
14435 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); | |||
14436 | ||||
14437 | BD->setBody(cast<CompoundStmt>(Body)); | |||
14438 | ||||
14439 | if (Body && getCurFunction()->HasPotentialAvailabilityViolations) | |||
14440 | DiagnoseUnguardedAvailabilityViolations(BD); | |||
14441 | ||||
14442 | // Try to apply the named return value optimization. We have to check again | |||
14443 | // if we can do this, though, because blocks keep return statements around | |||
14444 | // to deduce an implicit return type. | |||
14445 | if (getLangOpts().CPlusPlus && RetTy->isRecordType() && | |||
14446 | !BD->isDependentContext()) | |||
14447 | computeNRVO(Body, BSI); | |||
14448 | ||||
14449 | if (RetTy.hasNonTrivialToPrimitiveDestructCUnion() || | |||
14450 | RetTy.hasNonTrivialToPrimitiveCopyCUnion()) | |||
14451 | checkNonTrivialCUnion(RetTy, BD->getCaretLocation(), NTCUC_FunctionReturn, | |||
14452 | NTCUK_Destruct|NTCUK_Copy); | |||
14453 | ||||
14454 | PopDeclContext(); | |||
14455 | ||||
14456 | // Pop the block scope now but keep it alive to the end of this function. | |||
14457 | AnalysisBasedWarnings::Policy WP = AnalysisWarnings.getDefaultPolicy(); | |||
14458 | PoppedFunctionScopePtr ScopeRAII = PopFunctionScopeInfo(&WP, BD, BlockTy); | |||
14459 | ||||
14460 | // Set the captured variables on the block. | |||
14461 | SmallVector<BlockDecl::Capture, 4> Captures; | |||
14462 | for (Capture &Cap : BSI->Captures) { | |||
14463 | if (Cap.isInvalid() || Cap.isThisCapture()) | |||
14464 | continue; | |||
14465 | ||||
14466 | VarDecl *Var = Cap.getVariable(); | |||
14467 | Expr *CopyExpr = nullptr; | |||
14468 | if (getLangOpts().CPlusPlus && Cap.isCopyCapture()) { | |||
14469 | if (const RecordType *Record = | |||
14470 | Cap.getCaptureType()->getAs<RecordType>()) { | |||
14471 | // The capture logic needs the destructor, so make sure we mark it. | |||
14472 | // Usually this is unnecessary because most local variables have | |||
14473 | // their destructors marked at declaration time, but parameters are | |||
14474 | // an exception because it's technically only the call site that | |||
14475 | // actually requires the destructor. | |||
14476 | if (isa<ParmVarDecl>(Var)) | |||
14477 | FinalizeVarWithDestructor(Var, Record); | |||
14478 | ||||
14479 | // Enter a separate potentially-evaluated context while building block | |||
14480 | // initializers to isolate their cleanups from those of the block | |||
14481 | // itself. | |||
14482 | // FIXME: Is this appropriate even when the block itself occurs in an | |||
14483 | // unevaluated operand? | |||
14484 | EnterExpressionEvaluationContext EvalContext( | |||
14485 | *this, ExpressionEvaluationContext::PotentiallyEvaluated); | |||
14486 | ||||
14487 | SourceLocation Loc = Cap.getLocation(); | |||
14488 | ||||
14489 | ExprResult Result = BuildDeclarationNameExpr( | |||
14490 | CXXScopeSpec(), DeclarationNameInfo(Var->getDeclName(), Loc), Var); | |||
14491 | ||||
14492 | // According to the blocks spec, the capture of a variable from | |||
14493 | // the stack requires a const copy constructor. This is not true | |||
14494 | // of the copy/move done to move a __block variable to the heap. | |||
14495 | if (!Result.isInvalid() && | |||
14496 | !Result.get()->getType().isConstQualified()) { | |||
14497 | Result = ImpCastExprToType(Result.get(), | |||
14498 | Result.get()->getType().withConst(), | |||
14499 | CK_NoOp, VK_LValue); | |||
14500 | } | |||
14501 | ||||
14502 | if (!Result.isInvalid()) { | |||
14503 | Result = PerformCopyInitialization( | |||
14504 | InitializedEntity::InitializeBlock(Var->getLocation(), | |||
14505 | Cap.getCaptureType(), false), | |||
14506 | Loc, Result.get()); | |||
14507 | } | |||
14508 | ||||
14509 | // Build a full-expression copy expression if initialization | |||
14510 | // succeeded and used a non-trivial constructor. Recover from | |||
14511 | // errors by pretending that the copy isn't necessary. | |||
14512 | if (!Result.isInvalid() && | |||
14513 | !cast<CXXConstructExpr>(Result.get())->getConstructor() | |||
14514 | ->isTrivial()) { | |||
14515 | Result = MaybeCreateExprWithCleanups(Result); | |||
14516 | CopyExpr = Result.get(); | |||
14517 | } | |||
14518 | } | |||
14519 | } | |||
14520 | ||||
14521 | BlockDecl::Capture NewCap(Var, Cap.isBlockCapture(), Cap.isNested(), | |||
14522 | CopyExpr); | |||
14523 | Captures.push_back(NewCap); | |||
14524 | } | |||
14525 | BD->setCaptures(Context, Captures, BSI->CXXThisCaptureIndex != 0); | |||
14526 | ||||
14527 | BlockExpr *Result = new (Context) BlockExpr(BD, BlockTy); | |||
14528 | ||||
14529 | // If the block isn't obviously global, i.e. it captures anything at | |||
14530 | // all, then we need to do a few things in the surrounding context: | |||
14531 | if (Result->getBlockDecl()->hasCaptures()) { | |||
14532 | // First, this expression has a new cleanup object. | |||
14533 | ExprCleanupObjects.push_back(Result->getBlockDecl()); | |||
14534 | Cleanup.setExprNeedsCleanups(true); | |||
14535 | ||||
14536 | // It also gets a branch-protected scope if any of the captured | |||
14537 | // variables needs destruction. | |||
14538 | for (const auto &CI : Result->getBlockDecl()->captures()) { | |||
14539 | const VarDecl *var = CI.getVariable(); | |||
14540 | if (var->getType().isDestructedType() != QualType::DK_none) { | |||
14541 | setFunctionHasBranchProtectedScope(); | |||
14542 | break; | |||
14543 | } | |||
14544 | } | |||
14545 | } | |||
14546 | ||||
14547 | if (getCurFunction()) | |||
14548 | getCurFunction()->addBlock(BD); | |||
14549 | ||||
14550 | return Result; | |||
14551 | } | |||
14552 | ||||
14553 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, Expr *E, ParsedType Ty, | |||
14554 | SourceLocation RPLoc) { | |||
14555 | TypeSourceInfo *TInfo; | |||
14556 | GetTypeFromParser(Ty, &TInfo); | |||
14557 | return BuildVAArgExpr(BuiltinLoc, E, TInfo, RPLoc); | |||
14558 | } | |||
14559 | ||||
14560 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, | |||
14561 | Expr *E, TypeSourceInfo *TInfo, | |||
14562 | SourceLocation RPLoc) { | |||
14563 | Expr *OrigExpr = E; | |||
14564 | bool IsMS = false; | |||
14565 | ||||
14566 | // CUDA device code does not support varargs. | |||
14567 | if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice) { | |||
14568 | if (const FunctionDecl *F = dyn_cast<FunctionDecl>(CurContext)) { | |||
14569 | CUDAFunctionTarget T = IdentifyCUDATarget(F); | |||
14570 | if (T == CFT_Global || T == CFT_Device || T == CFT_HostDevice) | |||
14571 | return ExprError(Diag(E->getBeginLoc(), diag::err_va_arg_in_device)); | |||
14572 | } | |||
14573 | } | |||
14574 | ||||
14575 | // NVPTX does not support va_arg expression. | |||
14576 | if (getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && | |||
14577 | Context.getTargetInfo().getTriple().isNVPTX()) | |||
14578 | targetDiag(E->getBeginLoc(), diag::err_va_arg_in_device); | |||
14579 | ||||
14580 | // It might be a __builtin_ms_va_list. (But don't ever mark a va_arg() | |||
14581 | // as Microsoft ABI on an actual Microsoft platform, where | |||
14582 | // __builtin_ms_va_list and __builtin_va_list are the same.) | |||
14583 | if (!E->isTypeDependent() && Context.getTargetInfo().hasBuiltinMSVaList() && | |||
14584 | Context.getTargetInfo().getBuiltinVaListKind() != TargetInfo::CharPtrBuiltinVaList) { | |||
14585 | QualType MSVaListType = Context.getBuiltinMSVaListType(); | |||
14586 | if (Context.hasSameType(MSVaListType, E->getType())) { | |||
14587 | if (CheckForModifiableLvalue(E, BuiltinLoc, *this)) | |||
14588 | return ExprError(); | |||
14589 | IsMS = true; | |||
14590 | } | |||
14591 | } | |||
14592 | ||||
14593 | // Get the va_list type | |||
14594 | QualType VaListType = Context.getBuiltinVaListType(); | |||
14595 | if (!IsMS) { | |||
14596 | if (VaListType->isArrayType()) { | |||
14597 | // Deal with implicit array decay; for example, on x86-64, | |||
14598 | // va_list is an array, but it's supposed to decay to | |||
14599 | // a pointer for va_arg. | |||
14600 | VaListType = Context.getArrayDecayedType(VaListType); | |||
14601 | // Make sure the input expression also decays appropriately. | |||
14602 | ExprResult Result = UsualUnaryConversions(E); | |||
14603 | if (Result.isInvalid()) | |||
14604 | return ExprError(); | |||
14605 | E = Result.get(); | |||
14606 | } else if (VaListType->isRecordType() && getLangOpts().CPlusPlus) { | |||
14607 | // If va_list is a record type and we are compiling in C++ mode, | |||
14608 | // check the argument using reference binding. | |||
14609 | InitializedEntity Entity = InitializedEntity::InitializeParameter( | |||
14610 | Context, Context.getLValueReferenceType(VaListType), false); | |||
14611 | ExprResult Init = PerformCopyInitialization(Entity, SourceLocation(), E); | |||
14612 | if (Init.isInvalid()) | |||
14613 | return ExprError(); | |||
14614 | E = Init.getAs<Expr>(); | |||
14615 | } else { | |||
14616 | // Otherwise, the va_list argument must be an l-value because | |||
14617 | // it is modified by va_arg. | |||
14618 | if (!E->isTypeDependent() && | |||
14619 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) | |||
14620 | return ExprError(); | |||
14621 | } | |||
14622 | } | |||
14623 | ||||
14624 | if (!IsMS && !E->isTypeDependent() && | |||
14625 | !Context.hasSameType(VaListType, E->getType())) | |||
14626 | return ExprError( | |||
14627 | Diag(E->getBeginLoc(), | |||
14628 | diag::err_first_argument_to_va_arg_not_of_type_va_list) | |||
14629 | << OrigExpr->getType() << E->getSourceRange()); | |||
14630 | ||||
14631 | if (!TInfo->getType()->isDependentType()) { | |||
14632 | if (RequireCompleteType(TInfo->getTypeLoc().getBeginLoc(), TInfo->getType(), | |||
14633 | diag::err_second_parameter_to_va_arg_incomplete, | |||
14634 | TInfo->getTypeLoc())) | |||
14635 | return ExprError(); | |||
14636 | ||||
14637 | if (RequireNonAbstractType(TInfo->getTypeLoc().getBeginLoc(), | |||
14638 | TInfo->getType(), | |||
14639 | diag::err_second_parameter_to_va_arg_abstract, | |||
14640 | TInfo->getTypeLoc())) | |||
14641 | return ExprError(); | |||
14642 | ||||
14643 | if (!TInfo->getType().isPODType(Context)) { | |||
14644 | Diag(TInfo->getTypeLoc().getBeginLoc(), | |||
14645 | TInfo->getType()->isObjCLifetimeType() | |||
14646 | ? diag::warn_second_parameter_to_va_arg_ownership_qualified | |||
14647 | : diag::warn_second_parameter_to_va_arg_not_pod) | |||
14648 | << TInfo->getType() | |||
14649 | << TInfo->getTypeLoc().getSourceRange(); | |||
14650 | } | |||
14651 | ||||
14652 | // Check for va_arg where arguments of the given type will be promoted | |||
14653 | // (i.e. this va_arg is guaranteed to have undefined behavior). | |||
14654 | QualType PromoteType; | |||
14655 | if (TInfo->getType()->isPromotableIntegerType()) { | |||
14656 | PromoteType = Context.getPromotedIntegerType(TInfo->getType()); | |||
14657 | if (Context.typesAreCompatible(PromoteType, TInfo->getType())) | |||
14658 | PromoteType = QualType(); | |||
14659 | } | |||
14660 | if (TInfo->getType()->isSpecificBuiltinType(BuiltinType::Float)) | |||
14661 | PromoteType = Context.DoubleTy; | |||
14662 | if (!PromoteType.isNull()) | |||
14663 | DiagRuntimeBehavior(TInfo->getTypeLoc().getBeginLoc(), E, | |||
14664 | PDiag(diag::warn_second_parameter_to_va_arg_never_compatible) | |||
14665 | << TInfo->getType() | |||
14666 | << PromoteType | |||
14667 | << TInfo->getTypeLoc().getSourceRange()); | |||
14668 | } | |||
14669 | ||||
14670 | QualType T = TInfo->getType().getNonLValueExprType(Context); | |||
14671 | return new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T, IsMS); | |||
14672 | } | |||
14673 | ||||
14674 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { | |||
14675 | // The type of __null will be int or long, depending on the size of | |||
14676 | // pointers on the target. | |||
14677 | QualType Ty; | |||
14678 | unsigned pw = Context.getTargetInfo().getPointerWidth(0); | |||
14679 | if (pw == Context.getTargetInfo().getIntWidth()) | |||
14680 | Ty = Context.IntTy; | |||
14681 | else if (pw == Context.getTargetInfo().getLongWidth()) | |||
14682 | Ty = Context.LongTy; | |||
14683 | else if (pw == Context.getTargetInfo().getLongLongWidth()) | |||
14684 | Ty = Context.LongLongTy; | |||
14685 | else { | |||
14686 | llvm_unreachable("I don't know size of pointer!")::llvm::llvm_unreachable_internal("I don't know size of pointer!" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 14686); | |||
14687 | } | |||
14688 | ||||
14689 | return new (Context) GNUNullExpr(Ty, TokenLoc); | |||
14690 | } | |||
14691 | ||||
14692 | ExprResult Sema::ActOnSourceLocExpr(SourceLocExpr::IdentKind Kind, | |||
14693 | SourceLocation BuiltinLoc, | |||
14694 | SourceLocation RPLoc) { | |||
14695 | return BuildSourceLocExpr(Kind, BuiltinLoc, RPLoc, CurContext); | |||
14696 | } | |||
14697 | ||||
14698 | ExprResult Sema::BuildSourceLocExpr(SourceLocExpr::IdentKind Kind, | |||
14699 | SourceLocation BuiltinLoc, | |||
14700 | SourceLocation RPLoc, | |||
14701 | DeclContext *ParentContext) { | |||
14702 | return new (Context) | |||
14703 | SourceLocExpr(Context, Kind, BuiltinLoc, RPLoc, ParentContext); | |||
14704 | } | |||
14705 | ||||
14706 | bool Sema::ConversionToObjCStringLiteralCheck(QualType DstType, Expr *&Exp, | |||
14707 | bool Diagnose) { | |||
14708 | if (!getLangOpts().ObjC) | |||
14709 | return false; | |||
14710 | ||||
14711 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); | |||
14712 | if (!PT) | |||
14713 | return false; | |||
14714 | ||||
14715 | if (!PT->isObjCIdType()) { | |||
14716 | // Check if the destination is the 'NSString' interface. | |||
14717 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); | |||
14718 | if (!ID || !ID->getIdentifier()->isStr("NSString")) | |||
14719 | return false; | |||
14720 | } | |||
14721 | ||||
14722 | // Ignore any parens, implicit casts (should only be | |||
14723 | // array-to-pointer decays), and not-so-opaque values. The last is | |||
14724 | // important for making this trigger for property assignments. | |||
14725 | Expr *SrcExpr = Exp->IgnoreParenImpCasts(); | |||
14726 | if (OpaqueValueExpr *OV = dyn_cast<OpaqueValueExpr>(SrcExpr)) | |||
14727 | if (OV->getSourceExpr()) | |||
14728 | SrcExpr = OV->getSourceExpr()->IgnoreParenImpCasts(); | |||
14729 | ||||
14730 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr); | |||
14731 | if (!SL || !SL->isAscii()) | |||
14732 | return false; | |||
14733 | if (Diagnose) { | |||
14734 | Diag(SL->getBeginLoc(), diag::err_missing_atsign_prefix) | |||
14735 | << FixItHint::CreateInsertion(SL->getBeginLoc(), "@"); | |||
14736 | Exp = BuildObjCStringLiteral(SL->getBeginLoc(), SL).get(); | |||
14737 | } | |||
14738 | return true; | |||
14739 | } | |||
14740 | ||||
14741 | static bool maybeDiagnoseAssignmentToFunction(Sema &S, QualType DstType, | |||
14742 | const Expr *SrcExpr) { | |||
14743 | if (!DstType->isFunctionPointerType() || | |||
14744 | !SrcExpr->getType()->isFunctionType()) | |||
14745 | return false; | |||
14746 | ||||
14747 | auto *DRE = dyn_cast<DeclRefExpr>(SrcExpr->IgnoreParenImpCasts()); | |||
14748 | if (!DRE) | |||
14749 | return false; | |||
14750 | ||||
14751 | auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl()); | |||
14752 | if (!FD) | |||
14753 | return false; | |||
14754 | ||||
14755 | return !S.checkAddressOfFunctionIsAvailable(FD, | |||
14756 | /*Complain=*/true, | |||
14757 | SrcExpr->getBeginLoc()); | |||
14758 | } | |||
14759 | ||||
14760 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, | |||
14761 | SourceLocation Loc, | |||
14762 | QualType DstType, QualType SrcType, | |||
14763 | Expr *SrcExpr, AssignmentAction Action, | |||
14764 | bool *Complained) { | |||
14765 | if (Complained) | |||
14766 | *Complained = false; | |||
14767 | ||||
14768 | // Decode the result (notice that AST's are still created for extensions). | |||
14769 | bool CheckInferredResultType = false; | |||
14770 | bool isInvalid = false; | |||
14771 | unsigned DiagKind = 0; | |||
14772 | FixItHint Hint; | |||
14773 | ConversionFixItGenerator ConvHints; | |||
14774 | bool MayHaveConvFixit = false; | |||
14775 | bool MayHaveFunctionDiff = false; | |||
14776 | const ObjCInterfaceDecl *IFace = nullptr; | |||
14777 | const ObjCProtocolDecl *PDecl = nullptr; | |||
14778 | ||||
14779 | switch (ConvTy) { | |||
14780 | case Compatible: | |||
14781 | DiagnoseAssignmentEnum(DstType, SrcType, SrcExpr); | |||
14782 | return false; | |||
14783 | ||||
14784 | case PointerToInt: | |||
14785 | if (getLangOpts().CPlusPlus) { | |||
14786 | DiagKind = diag::err_typecheck_convert_pointer_int; | |||
14787 | isInvalid = true; | |||
14788 | } else { | |||
14789 | DiagKind = diag::ext_typecheck_convert_pointer_int; | |||
14790 | } | |||
14791 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); | |||
14792 | MayHaveConvFixit = true; | |||
14793 | break; | |||
14794 | case IntToPointer: | |||
14795 | if (getLangOpts().CPlusPlus) { | |||
14796 | DiagKind = diag::err_typecheck_convert_int_pointer; | |||
14797 | isInvalid = true; | |||
14798 | } else { | |||
14799 | DiagKind = diag::ext_typecheck_convert_int_pointer; | |||
14800 | } | |||
14801 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); | |||
14802 | MayHaveConvFixit = true; | |||
14803 | break; | |||
14804 | case IncompatibleFunctionPointer: | |||
14805 | if (getLangOpts().CPlusPlus) { | |||
14806 | DiagKind = diag::err_typecheck_convert_incompatible_function_pointer; | |||
14807 | isInvalid = true; | |||
14808 | } else { | |||
14809 | DiagKind = diag::ext_typecheck_convert_incompatible_function_pointer; | |||
14810 | } | |||
14811 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); | |||
14812 | MayHaveConvFixit = true; | |||
14813 | break; | |||
14814 | case IncompatiblePointer: | |||
14815 | if (Action == AA_Passing_CFAudited) { | |||
14816 | DiagKind = diag::err_arc_typecheck_convert_incompatible_pointer; | |||
14817 | } else if (getLangOpts().CPlusPlus) { | |||
14818 | DiagKind = diag::err_typecheck_convert_incompatible_pointer; | |||
14819 | isInvalid = true; | |||
14820 | } else { | |||
14821 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; | |||
14822 | } | |||
14823 | CheckInferredResultType = DstType->isObjCObjectPointerType() && | |||
14824 | SrcType->isObjCObjectPointerType(); | |||
14825 | if (Hint.isNull() && !CheckInferredResultType) { | |||
14826 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); | |||
14827 | } | |||
14828 | else if (CheckInferredResultType) { | |||
14829 | SrcType = SrcType.getUnqualifiedType(); | |||
14830 | DstType = DstType.getUnqualifiedType(); | |||
14831 | } | |||
14832 | MayHaveConvFixit = true; | |||
14833 | break; | |||
14834 | case IncompatiblePointerSign: | |||
14835 | if (getLangOpts().CPlusPlus) { | |||
14836 | DiagKind = diag::err_typecheck_convert_incompatible_pointer_sign; | |||
14837 | isInvalid = true; | |||
14838 | } else { | |||
14839 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; | |||
14840 | } | |||
14841 | break; | |||
14842 | case FunctionVoidPointer: | |||
14843 | if (getLangOpts().CPlusPlus) { | |||
14844 | DiagKind = diag::err_typecheck_convert_pointer_void_func; | |||
14845 | isInvalid = true; | |||
14846 | } else { | |||
14847 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; | |||
14848 | } | |||
14849 | break; | |||
14850 | case IncompatiblePointerDiscardsQualifiers: { | |||
14851 | // Perform array-to-pointer decay if necessary. | |||
14852 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); | |||
14853 | ||||
14854 | isInvalid = true; | |||
14855 | ||||
14856 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); | |||
14857 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); | |||
14858 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { | |||
14859 | DiagKind = diag::err_typecheck_incompatible_address_space; | |||
14860 | break; | |||
14861 | ||||
14862 | } else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) { | |||
14863 | DiagKind = diag::err_typecheck_incompatible_ownership; | |||
14864 | break; | |||
14865 | } | |||
14866 | ||||
14867 | llvm_unreachable("unknown error case for discarding qualifiers!")::llvm::llvm_unreachable_internal("unknown error case for discarding qualifiers!" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 14867); | |||
14868 | // fallthrough | |||
14869 | } | |||
14870 | case CompatiblePointerDiscardsQualifiers: | |||
14871 | // If the qualifiers lost were because we were applying the | |||
14872 | // (deprecated) C++ conversion from a string literal to a char* | |||
14873 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: | |||
14874 | // Ideally, this check would be performed in | |||
14875 | // checkPointerTypesForAssignment. However, that would require a | |||
14876 | // bit of refactoring (so that the second argument is an | |||
14877 | // expression, rather than a type), which should be done as part | |||
14878 | // of a larger effort to fix checkPointerTypesForAssignment for | |||
14879 | // C++ semantics. | |||
14880 | if (getLangOpts().CPlusPlus && | |||
14881 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) | |||
14882 | return false; | |||
14883 | if (getLangOpts().CPlusPlus) { | |||
14884 | DiagKind = diag::err_typecheck_convert_discards_qualifiers; | |||
14885 | isInvalid = true; | |||
14886 | } else { | |||
14887 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; | |||
14888 | } | |||
14889 | ||||
14890 | break; | |||
14891 | case IncompatibleNestedPointerQualifiers: | |||
14892 | if (getLangOpts().CPlusPlus) { | |||
14893 | isInvalid = true; | |||
14894 | DiagKind = diag::err_nested_pointer_qualifier_mismatch; | |||
14895 | } else { | |||
14896 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; | |||
14897 | } | |||
14898 | break; | |||
14899 | case IncompatibleNestedPointerAddressSpaceMismatch: | |||
14900 | DiagKind = diag::err_typecheck_incompatible_nested_address_space; | |||
14901 | isInvalid = true; | |||
14902 | break; | |||
14903 | case IntToBlockPointer: | |||
14904 | DiagKind = diag::err_int_to_block_pointer; | |||
14905 | isInvalid = true; | |||
14906 | break; | |||
14907 | case IncompatibleBlockPointer: | |||
14908 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; | |||
14909 | isInvalid = true; | |||
14910 | break; | |||
14911 | case IncompatibleObjCQualifiedId: { | |||
14912 | if (SrcType->isObjCQualifiedIdType()) { | |||
14913 | const ObjCObjectPointerType *srcOPT = | |||
14914 | SrcType->castAs<ObjCObjectPointerType>(); | |||
14915 | for (auto *srcProto : srcOPT->quals()) { | |||
14916 | PDecl = srcProto; | |||
14917 | break; | |||
14918 | } | |||
14919 | if (const ObjCInterfaceType *IFaceT = | |||
14920 | DstType->castAs<ObjCObjectPointerType>()->getInterfaceType()) | |||
14921 | IFace = IFaceT->getDecl(); | |||
14922 | } | |||
14923 | else if (DstType->isObjCQualifiedIdType()) { | |||
14924 | const ObjCObjectPointerType *dstOPT = | |||
14925 | DstType->castAs<ObjCObjectPointerType>(); | |||
14926 | for (auto *dstProto : dstOPT->quals()) { | |||
14927 | PDecl = dstProto; | |||
14928 | break; | |||
14929 | } | |||
14930 | if (const ObjCInterfaceType *IFaceT = | |||
14931 | SrcType->castAs<ObjCObjectPointerType>()->getInterfaceType()) | |||
14932 | IFace = IFaceT->getDecl(); | |||
14933 | } | |||
14934 | if (getLangOpts().CPlusPlus) { | |||
14935 | DiagKind = diag::err_incompatible_qualified_id; | |||
14936 | isInvalid = true; | |||
14937 | } else { | |||
14938 | DiagKind = diag::warn_incompatible_qualified_id; | |||
14939 | } | |||
14940 | break; | |||
14941 | } | |||
14942 | case IncompatibleVectors: | |||
14943 | if (getLangOpts().CPlusPlus) { | |||
14944 | DiagKind = diag::err_incompatible_vectors; | |||
14945 | isInvalid = true; | |||
14946 | } else { | |||
14947 | DiagKind = diag::warn_incompatible_vectors; | |||
14948 | } | |||
14949 | break; | |||
14950 | case IncompatibleObjCWeakRef: | |||
14951 | DiagKind = diag::err_arc_weak_unavailable_assign; | |||
14952 | isInvalid = true; | |||
14953 | break; | |||
14954 | case Incompatible: | |||
14955 | if (maybeDiagnoseAssignmentToFunction(*this, DstType, SrcExpr)) { | |||
14956 | if (Complained) | |||
14957 | *Complained = true; | |||
14958 | return true; | |||
14959 | } | |||
14960 | ||||
14961 | DiagKind = diag::err_typecheck_convert_incompatible; | |||
14962 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); | |||
14963 | MayHaveConvFixit = true; | |||
14964 | isInvalid = true; | |||
14965 | MayHaveFunctionDiff = true; | |||
14966 | break; | |||
14967 | } | |||
14968 | ||||
14969 | QualType FirstType, SecondType; | |||
14970 | switch (Action) { | |||
14971 | case AA_Assigning: | |||
14972 | case AA_Initializing: | |||
14973 | // The destination type comes first. | |||
14974 | FirstType = DstType; | |||
14975 | SecondType = SrcType; | |||
14976 | break; | |||
14977 | ||||
14978 | case AA_Returning: | |||
14979 | case AA_Passing: | |||
14980 | case AA_Passing_CFAudited: | |||
14981 | case AA_Converting: | |||
14982 | case AA_Sending: | |||
14983 | case AA_Casting: | |||
14984 | // The source type comes first. | |||
14985 | FirstType = SrcType; | |||
14986 | SecondType = DstType; | |||
14987 | break; | |||
14988 | } | |||
14989 | ||||
14990 | PartialDiagnostic FDiag = PDiag(DiagKind); | |||
14991 | if (Action == AA_Passing_CFAudited) | |||
14992 | FDiag << FirstType << SecondType << AA_Passing << SrcExpr->getSourceRange(); | |||
14993 | else | |||
14994 | FDiag << FirstType << SecondType << Action << SrcExpr->getSourceRange(); | |||
14995 | ||||
14996 | // If we can fix the conversion, suggest the FixIts. | |||
14997 | assert(ConvHints.isNull() || Hint.isNull())((ConvHints.isNull() || Hint.isNull()) ? static_cast<void> (0) : __assert_fail ("ConvHints.isNull() || Hint.isNull()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 14997, __PRETTY_FUNCTION__)); | |||
14998 | if (!ConvHints.isNull()) { | |||
14999 | for (FixItHint &H : ConvHints.Hints) | |||
15000 | FDiag << H; | |||
15001 | } else { | |||
15002 | FDiag << Hint; | |||
15003 | } | |||
15004 | if (MayHaveConvFixit) { FDiag << (unsigned) (ConvHints.Kind); } | |||
15005 | ||||
15006 | if (MayHaveFunctionDiff) | |||
15007 | HandleFunctionTypeMismatch(FDiag, SecondType, FirstType); | |||
15008 | ||||
15009 | Diag(Loc, FDiag); | |||
15010 | if ((DiagKind == diag::warn_incompatible_qualified_id || | |||
15011 | DiagKind == diag::err_incompatible_qualified_id) && | |||
15012 | PDecl && IFace && !IFace->hasDefinition()) | |||
15013 | Diag(IFace->getLocation(), diag::note_incomplete_class_and_qualified_id) | |||
15014 | << IFace << PDecl; | |||
15015 | ||||
15016 | if (SecondType == Context.OverloadTy) | |||
15017 | NoteAllOverloadCandidates(OverloadExpr::find(SrcExpr).Expression, | |||
15018 | FirstType, /*TakingAddress=*/true); | |||
15019 | ||||
15020 | if (CheckInferredResultType) | |||
15021 | EmitRelatedResultTypeNote(SrcExpr); | |||
15022 | ||||
15023 | if (Action == AA_Returning && ConvTy == IncompatiblePointer) | |||
15024 | EmitRelatedResultTypeNoteForReturn(DstType); | |||
15025 | ||||
15026 | if (Complained) | |||
15027 | *Complained = true; | |||
15028 | return isInvalid; | |||
15029 | } | |||
15030 | ||||
15031 | ExprResult Sema::VerifyIntegerConstantExpression(Expr *E, | |||
15032 | llvm::APSInt *Result) { | |||
15033 | class SimpleICEDiagnoser : public VerifyICEDiagnoser { | |||
15034 | public: | |||
15035 | void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) override { | |||
15036 | S.Diag(Loc, diag::err_expr_not_ice) << S.LangOpts.CPlusPlus << SR; | |||
15037 | } | |||
15038 | } Diagnoser; | |||
15039 | ||||
15040 | return VerifyIntegerConstantExpression(E, Result, Diagnoser); | |||
15041 | } | |||
15042 | ||||
15043 | ExprResult Sema::VerifyIntegerConstantExpression(Expr *E, | |||
15044 | llvm::APSInt *Result, | |||
15045 | unsigned DiagID, | |||
15046 | bool AllowFold) { | |||
15047 | class IDDiagnoser : public VerifyICEDiagnoser { | |||
15048 | unsigned DiagID; | |||
15049 | ||||
15050 | public: | |||
15051 | IDDiagnoser(unsigned DiagID) | |||
15052 | : VerifyICEDiagnoser(DiagID == 0), DiagID(DiagID) { } | |||
15053 | ||||
15054 | void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) override { | |||
15055 | S.Diag(Loc, DiagID) << SR; | |||
15056 | } | |||
15057 | } Diagnoser(DiagID); | |||
15058 | ||||
15059 | return VerifyIntegerConstantExpression(E, Result, Diagnoser, AllowFold); | |||
15060 | } | |||
15061 | ||||
15062 | void Sema::VerifyICEDiagnoser::diagnoseFold(Sema &S, SourceLocation Loc, | |||
15063 | SourceRange SR) { | |||
15064 | S.Diag(Loc, diag::ext_expr_not_ice) << SR << S.LangOpts.CPlusPlus; | |||
15065 | } | |||
15066 | ||||
15067 | ExprResult | |||
15068 | Sema::VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result, | |||
15069 | VerifyICEDiagnoser &Diagnoser, | |||
15070 | bool AllowFold) { | |||
15071 | SourceLocation DiagLoc = E->getBeginLoc(); | |||
15072 | ||||
15073 | if (getLangOpts().CPlusPlus11) { | |||
15074 | // C++11 [expr.const]p5: | |||
15075 | // If an expression of literal class type is used in a context where an | |||
15076 | // integral constant expression is required, then that class type shall | |||
15077 | // have a single non-explicit conversion function to an integral or | |||
15078 | // unscoped enumeration type | |||
15079 | ExprResult Converted; | |||
15080 | class CXX11ConvertDiagnoser : public ICEConvertDiagnoser { | |||
15081 | public: | |||
15082 | CXX11ConvertDiagnoser(bool Silent) | |||
15083 | : ICEConvertDiagnoser(/*AllowScopedEnumerations*/false, | |||
15084 | Silent, true) {} | |||
15085 | ||||
15086 | SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, | |||
15087 | QualType T) override { | |||
15088 | return S.Diag(Loc, diag::err_ice_not_integral) << T; | |||
15089 | } | |||
15090 | ||||
15091 | SemaDiagnosticBuilder diagnoseIncomplete( | |||
15092 | Sema &S, SourceLocation Loc, QualType T) override { | |||
15093 | return S.Diag(Loc, diag::err_ice_incomplete_type) << T; | |||
15094 | } | |||
15095 | ||||
15096 | SemaDiagnosticBuilder diagnoseExplicitConv( | |||
15097 | Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override { | |||
15098 | return S.Diag(Loc, diag::err_ice_explicit_conversion) << T << ConvTy; | |||
15099 | } | |||
15100 | ||||
15101 | SemaDiagnosticBuilder noteExplicitConv( | |||
15102 | Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { | |||
15103 | return S.Diag(Conv->getLocation(), diag::note_ice_conversion_here) | |||
15104 | << ConvTy->isEnumeralType() << ConvTy; | |||
15105 | } | |||
15106 | ||||
15107 | SemaDiagnosticBuilder diagnoseAmbiguous( | |||
15108 | Sema &S, SourceLocation Loc, QualType T) override { | |||
15109 | return S.Diag(Loc, diag::err_ice_ambiguous_conversion) << T; | |||
15110 | } | |||
15111 | ||||
15112 | SemaDiagnosticBuilder noteAmbiguous( | |||
15113 | Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { | |||
15114 | return S.Diag(Conv->getLocation(), diag::note_ice_conversion_here) | |||
15115 | << ConvTy->isEnumeralType() << ConvTy; | |||
15116 | } | |||
15117 | ||||
15118 | SemaDiagnosticBuilder diagnoseConversion( | |||
15119 | Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override { | |||
15120 | llvm_unreachable("conversion functions are permitted")::llvm::llvm_unreachable_internal("conversion functions are permitted" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 15120); | |||
15121 | } | |||
15122 | } ConvertDiagnoser(Diagnoser.Suppress); | |||
15123 | ||||
15124 | Converted = PerformContextualImplicitConversion(DiagLoc, E, | |||
15125 | ConvertDiagnoser); | |||
15126 | if (Converted.isInvalid()) | |||
15127 | return Converted; | |||
15128 | E = Converted.get(); | |||
15129 | if (!E->getType()->isIntegralOrUnscopedEnumerationType()) | |||
15130 | return ExprError(); | |||
15131 | } else if (!E->getType()->isIntegralOrUnscopedEnumerationType()) { | |||
15132 | // An ICE must be of integral or unscoped enumeration type. | |||
15133 | if (!Diagnoser.Suppress) | |||
15134 | Diagnoser.diagnoseNotICE(*this, DiagLoc, E->getSourceRange()); | |||
15135 | return ExprError(); | |||
15136 | } | |||
15137 | ||||
15138 | // Circumvent ICE checking in C++11 to avoid evaluating the expression twice | |||
15139 | // in the non-ICE case. | |||
15140 | if (!getLangOpts().CPlusPlus11 && E->isIntegerConstantExpr(Context)) { | |||
15141 | if (Result) | |||
15142 | *Result = E->EvaluateKnownConstIntCheckOverflow(Context); | |||
15143 | if (!isa<ConstantExpr>(E)) | |||
15144 | E = ConstantExpr::Create(Context, E); | |||
15145 | return E; | |||
15146 | } | |||
15147 | ||||
15148 | Expr::EvalResult EvalResult; | |||
15149 | SmallVector<PartialDiagnosticAt, 8> Notes; | |||
15150 | EvalResult.Diag = &Notes; | |||
15151 | ||||
15152 | // Try to evaluate the expression, and produce diagnostics explaining why it's | |||
15153 | // not a constant expression as a side-effect. | |||
15154 | bool Folded = | |||
15155 | E->EvaluateAsRValue(EvalResult, Context, /*isConstantContext*/ true) && | |||
15156 | EvalResult.Val.isInt() && !EvalResult.HasSideEffects; | |||
15157 | ||||
15158 | if (!isa<ConstantExpr>(E)) | |||
15159 | E = ConstantExpr::Create(Context, E, EvalResult.Val); | |||
15160 | ||||
15161 | // In C++11, we can rely on diagnostics being produced for any expression | |||
15162 | // which is not a constant expression. If no diagnostics were produced, then | |||
15163 | // this is a constant expression. | |||
15164 | if (Folded && getLangOpts().CPlusPlus11 && Notes.empty()) { | |||
15165 | if (Result) | |||
15166 | *Result = EvalResult.Val.getInt(); | |||
15167 | return E; | |||
15168 | } | |||
15169 | ||||
15170 | // If our only note is the usual "invalid subexpression" note, just point | |||
15171 | // the caret at its location rather than producing an essentially | |||
15172 | // redundant note. | |||
15173 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == | |||
15174 | diag::note_invalid_subexpr_in_const_expr) { | |||
15175 | DiagLoc = Notes[0].first; | |||
15176 | Notes.clear(); | |||
15177 | } | |||
15178 | ||||
15179 | if (!Folded || !AllowFold) { | |||
15180 | if (!Diagnoser.Suppress) { | |||
15181 | Diagnoser.diagnoseNotICE(*this, DiagLoc, E->getSourceRange()); | |||
15182 | for (const PartialDiagnosticAt &Note : Notes) | |||
15183 | Diag(Note.first, Note.second); | |||
15184 | } | |||
15185 | ||||
15186 | return ExprError(); | |||
15187 | } | |||
15188 | ||||
15189 | Diagnoser.diagnoseFold(*this, DiagLoc, E->getSourceRange()); | |||
15190 | for (const PartialDiagnosticAt &Note : Notes) | |||
15191 | Diag(Note.first, Note.second); | |||
15192 | ||||
15193 | if (Result) | |||
15194 | *Result = EvalResult.Val.getInt(); | |||
15195 | return E; | |||
15196 | } | |||
15197 | ||||
15198 | namespace { | |||
15199 | // Handle the case where we conclude a expression which we speculatively | |||
15200 | // considered to be unevaluated is actually evaluated. | |||
15201 | class TransformToPE : public TreeTransform<TransformToPE> { | |||
15202 | typedef TreeTransform<TransformToPE> BaseTransform; | |||
15203 | ||||
15204 | public: | |||
15205 | TransformToPE(Sema &SemaRef) : BaseTransform(SemaRef) { } | |||
15206 | ||||
15207 | // Make sure we redo semantic analysis | |||
15208 | bool AlwaysRebuild() { return true; } | |||
15209 | bool ReplacingOriginal() { return true; } | |||
15210 | ||||
15211 | // We need to special-case DeclRefExprs referring to FieldDecls which | |||
15212 | // are not part of a member pointer formation; normal TreeTransforming | |||
15213 | // doesn't catch this case because of the way we represent them in the AST. | |||
15214 | // FIXME: This is a bit ugly; is it really the best way to handle this | |||
15215 | // case? | |||
15216 | // | |||
15217 | // Error on DeclRefExprs referring to FieldDecls. | |||
15218 | ExprResult TransformDeclRefExpr(DeclRefExpr *E) { | |||
15219 | if (isa<FieldDecl>(E->getDecl()) && | |||
15220 | !SemaRef.isUnevaluatedContext()) | |||
15221 | return SemaRef.Diag(E->getLocation(), | |||
15222 | diag::err_invalid_non_static_member_use) | |||
15223 | << E->getDecl() << E->getSourceRange(); | |||
15224 | ||||
15225 | return BaseTransform::TransformDeclRefExpr(E); | |||
15226 | } | |||
15227 | ||||
15228 | // Exception: filter out member pointer formation | |||
15229 | ExprResult TransformUnaryOperator(UnaryOperator *E) { | |||
15230 | if (E->getOpcode() == UO_AddrOf && E->getType()->isMemberPointerType()) | |||
15231 | return E; | |||
15232 | ||||
15233 | return BaseTransform::TransformUnaryOperator(E); | |||
15234 | } | |||
15235 | ||||
15236 | // The body of a lambda-expression is in a separate expression evaluation | |||
15237 | // context so never needs to be transformed. | |||
15238 | // FIXME: Ideally we wouldn't transform the closure type either, and would | |||
15239 | // just recreate the capture expressions and lambda expression. | |||
15240 | StmtResult TransformLambdaBody(LambdaExpr *E, Stmt *Body) { | |||
15241 | return SkipLambdaBody(E, Body); | |||
15242 | } | |||
15243 | }; | |||
15244 | } | |||
15245 | ||||
15246 | ExprResult Sema::TransformToPotentiallyEvaluated(Expr *E) { | |||
15247 | assert(isUnevaluatedContext() &&((isUnevaluatedContext() && "Should only transform unevaluated expressions" ) ? static_cast<void> (0) : __assert_fail ("isUnevaluatedContext() && \"Should only transform unevaluated expressions\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 15248, __PRETTY_FUNCTION__)) | |||
15248 | "Should only transform unevaluated expressions")((isUnevaluatedContext() && "Should only transform unevaluated expressions" ) ? static_cast<void> (0) : __assert_fail ("isUnevaluatedContext() && \"Should only transform unevaluated expressions\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 15248, __PRETTY_FUNCTION__)); | |||
15249 | ExprEvalContexts.back().Context = | |||
15250 | ExprEvalContexts[ExprEvalContexts.size()-2].Context; | |||
15251 | if (isUnevaluatedContext()) | |||
15252 | return E; | |||
15253 | return TransformToPE(*this).TransformExpr(E); | |||
15254 | } | |||
15255 | ||||
15256 | void | |||
15257 | Sema::PushExpressionEvaluationContext( | |||
15258 | ExpressionEvaluationContext NewContext, Decl *LambdaContextDecl, | |||
15259 | ExpressionEvaluationContextRecord::ExpressionKind ExprContext) { | |||
15260 | ExprEvalContexts.emplace_back(NewContext, ExprCleanupObjects.size(), Cleanup, | |||
15261 | LambdaContextDecl, ExprContext); | |||
15262 | Cleanup.reset(); | |||
15263 | if (!MaybeODRUseExprs.empty()) | |||
15264 | std::swap(MaybeODRUseExprs, ExprEvalContexts.back().SavedMaybeODRUseExprs); | |||
15265 | } | |||
15266 | ||||
15267 | void | |||
15268 | Sema::PushExpressionEvaluationContext( | |||
15269 | ExpressionEvaluationContext NewContext, ReuseLambdaContextDecl_t, | |||
15270 | ExpressionEvaluationContextRecord::ExpressionKind ExprContext) { | |||
15271 | Decl *ClosureContextDecl = ExprEvalContexts.back().ManglingContextDecl; | |||
15272 | PushExpressionEvaluationContext(NewContext, ClosureContextDecl, ExprContext); | |||
15273 | } | |||
15274 | ||||
15275 | namespace { | |||
15276 | ||||
15277 | const DeclRefExpr *CheckPossibleDeref(Sema &S, const Expr *PossibleDeref) { | |||
15278 | PossibleDeref = PossibleDeref->IgnoreParenImpCasts(); | |||
15279 | if (const auto *E = dyn_cast<UnaryOperator>(PossibleDeref)) { | |||
15280 | if (E->getOpcode() == UO_Deref) | |||
15281 | return CheckPossibleDeref(S, E->getSubExpr()); | |||
15282 | } else if (const auto *E = dyn_cast<ArraySubscriptExpr>(PossibleDeref)) { | |||
15283 | return CheckPossibleDeref(S, E->getBase()); | |||
15284 | } else if (const auto *E = dyn_cast<MemberExpr>(PossibleDeref)) { | |||
15285 | return CheckPossibleDeref(S, E->getBase()); | |||
15286 | } else if (const auto E = dyn_cast<DeclRefExpr>(PossibleDeref)) { | |||
15287 | QualType Inner; | |||
15288 | QualType Ty = E->getType(); | |||
15289 | if (const auto *Ptr = Ty->getAs<PointerType>()) | |||
15290 | Inner = Ptr->getPointeeType(); | |||
15291 | else if (const auto *Arr = S.Context.getAsArrayType(Ty)) | |||
15292 | Inner = Arr->getElementType(); | |||
15293 | else | |||
15294 | return nullptr; | |||
15295 | ||||
15296 | if (Inner->hasAttr(attr::NoDeref)) | |||
15297 | return E; | |||
15298 | } | |||
15299 | return nullptr; | |||
15300 | } | |||
15301 | ||||
15302 | } // namespace | |||
15303 | ||||
15304 | void Sema::WarnOnPendingNoDerefs(ExpressionEvaluationContextRecord &Rec) { | |||
15305 | for (const Expr *E : Rec.PossibleDerefs) { | |||
15306 | const DeclRefExpr *DeclRef = CheckPossibleDeref(*this, E); | |||
15307 | if (DeclRef) { | |||
15308 | const ValueDecl *Decl = DeclRef->getDecl(); | |||
15309 | Diag(E->getExprLoc(), diag::warn_dereference_of_noderef_type) | |||
15310 | << Decl->getName() << E->getSourceRange(); | |||
15311 | Diag(Decl->getLocation(), diag::note_previous_decl) << Decl->getName(); | |||
15312 | } else { | |||
15313 | Diag(E->getExprLoc(), diag::warn_dereference_of_noderef_type_no_decl) | |||
15314 | << E->getSourceRange(); | |||
15315 | } | |||
15316 | } | |||
15317 | Rec.PossibleDerefs.clear(); | |||
15318 | } | |||
15319 | ||||
15320 | /// Check whether E, which is either a discarded-value expression or an | |||
15321 | /// unevaluated operand, is a simple-assignment to a volatlie-qualified lvalue, | |||
15322 | /// and if so, remove it from the list of volatile-qualified assignments that | |||
15323 | /// we are going to warn are deprecated. | |||
15324 | void Sema::CheckUnusedVolatileAssignment(Expr *E) { | |||
15325 | if (!E->getType().isVolatileQualified() || !getLangOpts().CPlusPlus2a) | |||
15326 | return; | |||
15327 | ||||
15328 | // Note: ignoring parens here is not justified by the standard rules, but | |||
15329 | // ignoring parentheses seems like a more reasonable approach, and this only | |||
15330 | // drives a deprecation warning so doesn't affect conformance. | |||
15331 | if (auto *BO = dyn_cast<BinaryOperator>(E->IgnoreParenImpCasts())) { | |||
15332 | if (BO->getOpcode() == BO_Assign) { | |||
15333 | auto &LHSs = ExprEvalContexts.back().VolatileAssignmentLHSs; | |||
15334 | LHSs.erase(std::remove(LHSs.begin(), LHSs.end(), BO->getLHS()), | |||
15335 | LHSs.end()); | |||
15336 | } | |||
15337 | } | |||
15338 | } | |||
15339 | ||||
15340 | ExprResult Sema::CheckForImmediateInvocation(ExprResult E, FunctionDecl *Decl) { | |||
15341 | if (!E.isUsable() || !Decl || !Decl->isConsteval() || isConstantEvaluated() || | |||
15342 | RebuildingImmediateInvocation) | |||
15343 | return E; | |||
15344 | ||||
15345 | /// Opportunistically remove the callee from ReferencesToConsteval if we can. | |||
15346 | /// It's OK if this fails; we'll also remove this in | |||
15347 | /// HandleImmediateInvocations, but catching it here allows us to avoid | |||
15348 | /// walking the AST looking for it in simple cases. | |||
15349 | if (auto *Call = dyn_cast<CallExpr>(E.get()->IgnoreImplicit())) | |||
15350 | if (auto *DeclRef = | |||
15351 | dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) | |||
15352 | ExprEvalContexts.back().ReferenceToConsteval.erase(DeclRef); | |||
15353 | ||||
15354 | E = MaybeCreateExprWithCleanups(E); | |||
15355 | ||||
15356 | ConstantExpr *Res = ConstantExpr::Create( | |||
15357 | getASTContext(), E.get(), | |||
15358 | ConstantExpr::getStorageKind(E.get()->getType().getTypePtr(), | |||
15359 | getASTContext()), | |||
15360 | /*IsImmediateInvocation*/ true); | |||
15361 | ExprEvalContexts.back().ImmediateInvocationCandidates.emplace_back(Res, 0); | |||
15362 | return Res; | |||
15363 | } | |||
15364 | ||||
15365 | static void EvaluateAndDiagnoseImmediateInvocation( | |||
15366 | Sema &SemaRef, Sema::ImmediateInvocationCandidate Candidate) { | |||
15367 | llvm::SmallVector<PartialDiagnosticAt, 8> Notes; | |||
15368 | Expr::EvalResult Eval; | |||
15369 | Eval.Diag = &Notes; | |||
15370 | ConstantExpr *CE = Candidate.getPointer(); | |||
15371 | bool Result = CE->EvaluateAsConstantExpr(Eval, Expr::EvaluateForCodeGen, | |||
15372 | SemaRef.getASTContext(), true); | |||
15373 | if (!Result || !Notes.empty()) { | |||
15374 | Expr *InnerExpr = CE->getSubExpr()->IgnoreImplicit(); | |||
15375 | FunctionDecl *FD = nullptr; | |||
15376 | if (auto *Call = dyn_cast<CallExpr>(InnerExpr)) | |||
15377 | FD = cast<FunctionDecl>(Call->getCalleeDecl()); | |||
15378 | else if (auto *Call = dyn_cast<CXXConstructExpr>(InnerExpr)) | |||
15379 | FD = Call->getConstructor(); | |||
15380 | else | |||
15381 | llvm_unreachable("unhandled decl kind")::llvm::llvm_unreachable_internal("unhandled decl kind", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 15381); | |||
15382 | assert(FD->isConsteval())((FD->isConsteval()) ? static_cast<void> (0) : __assert_fail ("FD->isConsteval()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 15382, __PRETTY_FUNCTION__)); | |||
15383 | SemaRef.Diag(CE->getBeginLoc(), diag::err_invalid_consteval_call) << FD; | |||
15384 | for (auto &Note : Notes) | |||
15385 | SemaRef.Diag(Note.first, Note.second); | |||
15386 | return; | |||
15387 | } | |||
15388 | CE->MoveIntoResult(Eval.Val, SemaRef.getASTContext()); | |||
15389 | } | |||
15390 | ||||
15391 | static void RemoveNestedImmediateInvocation( | |||
15392 | Sema &SemaRef, Sema::ExpressionEvaluationContextRecord &Rec, | |||
15393 | SmallVector<Sema::ImmediateInvocationCandidate, 4>::reverse_iterator It) { | |||
15394 | struct ComplexRemove : TreeTransform<ComplexRemove> { | |||
15395 | using Base = TreeTransform<ComplexRemove>; | |||
15396 | llvm::SmallPtrSetImpl<DeclRefExpr *> &DRSet; | |||
15397 | SmallVector<Sema::ImmediateInvocationCandidate, 4> &IISet; | |||
15398 | SmallVector<Sema::ImmediateInvocationCandidate, 4>::reverse_iterator | |||
15399 | CurrentII; | |||
15400 | ComplexRemove(Sema &SemaRef, llvm::SmallPtrSetImpl<DeclRefExpr *> &DR, | |||
15401 | SmallVector<Sema::ImmediateInvocationCandidate, 4> &II, | |||
15402 | SmallVector<Sema::ImmediateInvocationCandidate, | |||
15403 | 4>::reverse_iterator Current) | |||
15404 | : Base(SemaRef), DRSet(DR), IISet(II), CurrentII(Current) {} | |||
15405 | void RemoveImmediateInvocation(ConstantExpr* E) { | |||
15406 | auto It = std::find_if(CurrentII, IISet.rend(), | |||
15407 | [E](Sema::ImmediateInvocationCandidate Elem) { | |||
15408 | return Elem.getPointer() == E; | |||
15409 | }); | |||
15410 | assert(It != IISet.rend() &&((It != IISet.rend() && "ConstantExpr marked IsImmediateInvocation should " "be present") ? static_cast<void> (0) : __assert_fail ( "It != IISet.rend() && \"ConstantExpr marked IsImmediateInvocation should \" \"be present\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 15412, __PRETTY_FUNCTION__)) | |||
15411 | "ConstantExpr marked IsImmediateInvocation should "((It != IISet.rend() && "ConstantExpr marked IsImmediateInvocation should " "be present") ? static_cast<void> (0) : __assert_fail ( "It != IISet.rend() && \"ConstantExpr marked IsImmediateInvocation should \" \"be present\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 15412, __PRETTY_FUNCTION__)) | |||
15412 | "be present")((It != IISet.rend() && "ConstantExpr marked IsImmediateInvocation should " "be present") ? static_cast<void> (0) : __assert_fail ( "It != IISet.rend() && \"ConstantExpr marked IsImmediateInvocation should \" \"be present\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 15412, __PRETTY_FUNCTION__)); | |||
15413 | It->setInt(1); // Mark as deleted | |||
15414 | } | |||
15415 | ExprResult TransformConstantExpr(ConstantExpr *E) { | |||
15416 | if (!E->isImmediateInvocation()) | |||
15417 | return Base::TransformConstantExpr(E); | |||
15418 | RemoveImmediateInvocation(E); | |||
15419 | return Base::TransformExpr(E->getSubExpr()); | |||
15420 | } | |||
15421 | /// Base::TransfromCXXOperatorCallExpr doesn't traverse the callee so | |||
15422 | /// we need to remove its DeclRefExpr from the DRSet. | |||
15423 | ExprResult TransformCXXOperatorCallExpr(CXXOperatorCallExpr *E) { | |||
15424 | DRSet.erase(cast<DeclRefExpr>(E->getCallee()->IgnoreImplicit())); | |||
| ||||
15425 | return Base::TransformCXXOperatorCallExpr(E); | |||
15426 | } | |||
15427 | /// Base::TransformInitializer skip ConstantExpr so we need to visit them | |||
15428 | /// here. | |||
15429 | ExprResult TransformInitializer(Expr *Init, bool NotCopyInit) { | |||
15430 | if (!Init) | |||
15431 | return Init; | |||
15432 | /// ConstantExpr are the first layer of implicit node to be removed so if | |||
15433 | /// Init isn't a ConstantExpr, no ConstantExpr will be skipped. | |||
15434 | if (auto *CE = dyn_cast<ConstantExpr>(Init)) | |||
15435 | if (CE->isImmediateInvocation()) | |||
15436 | RemoveImmediateInvocation(CE); | |||
15437 | return Base::TransformInitializer(Init, NotCopyInit); | |||
15438 | } | |||
15439 | ExprResult TransformDeclRefExpr(DeclRefExpr *E) { | |||
15440 | DRSet.erase(E); | |||
15441 | return E; | |||
15442 | } | |||
15443 | bool AlwaysRebuild() { return false; } | |||
15444 | bool ReplacingOriginal() { return true; } | |||
15445 | } Transformer(SemaRef, Rec.ReferenceToConsteval, | |||
15446 | Rec.ImmediateInvocationCandidates, It); | |||
15447 | ExprResult Res = Transformer.TransformExpr(It->getPointer()->getSubExpr()); | |||
15448 | assert(Res.isUsable())((Res.isUsable()) ? static_cast<void> (0) : __assert_fail ("Res.isUsable()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 15448, __PRETTY_FUNCTION__)); | |||
15449 | Res = SemaRef.MaybeCreateExprWithCleanups(Res); | |||
15450 | It->getPointer()->setSubExpr(Res.get()); | |||
15451 | } | |||
15452 | ||||
15453 | static void | |||
15454 | HandleImmediateInvocations(Sema &SemaRef, | |||
15455 | Sema::ExpressionEvaluationContextRecord &Rec) { | |||
15456 | if ((Rec.ImmediateInvocationCandidates.size() == 0 && | |||
15457 | Rec.ReferenceToConsteval.size() == 0) || | |||
15458 | SemaRef.RebuildingImmediateInvocation) | |||
15459 | return; | |||
15460 | ||||
15461 | /// When we have more then 1 ImmediateInvocationCandidates we need to check | |||
15462 | /// for nested ImmediateInvocationCandidates. when we have only 1 we only | |||
15463 | /// need to remove ReferenceToConsteval in the immediate invocation. | |||
15464 | if (Rec.ImmediateInvocationCandidates.size() > 1) { | |||
15465 | ||||
15466 | /// Prevent sema calls during the tree transform from adding pointers that | |||
15467 | /// are already in the sets. | |||
15468 | llvm::SaveAndRestore<bool> DisableIITracking( | |||
15469 | SemaRef.RebuildingImmediateInvocation, true); | |||
15470 | ||||
15471 | /// Prevent diagnostic during tree transfrom as they are duplicates | |||
15472 | Sema::TentativeAnalysisScope DisableDiag(SemaRef); | |||
15473 | ||||
15474 | for (auto It = Rec.ImmediateInvocationCandidates.rbegin(); | |||
15475 | It != Rec.ImmediateInvocationCandidates.rend(); It++) | |||
15476 | if (!It->getInt()) | |||
15477 | RemoveNestedImmediateInvocation(SemaRef, Rec, It); | |||
15478 | } else if (Rec.ImmediateInvocationCandidates.size() == 1 && | |||
15479 | Rec.ReferenceToConsteval.size()) { | |||
15480 | struct SimpleRemove : RecursiveASTVisitor<SimpleRemove> { | |||
15481 | llvm::SmallPtrSetImpl<DeclRefExpr *> &DRSet; | |||
15482 | SimpleRemove(llvm::SmallPtrSetImpl<DeclRefExpr *> &S) : DRSet(S) {} | |||
15483 | bool VisitDeclRefExpr(DeclRefExpr *E) { | |||
15484 | DRSet.erase(E); | |||
15485 | return DRSet.size(); | |||
15486 | } | |||
15487 | } Visitor(Rec.ReferenceToConsteval); | |||
15488 | Visitor.TraverseStmt( | |||
15489 | Rec.ImmediateInvocationCandidates.front().getPointer()->getSubExpr()); | |||
15490 | } | |||
15491 | for (auto CE : Rec.ImmediateInvocationCandidates) | |||
15492 | if (!CE.getInt()) | |||
15493 | EvaluateAndDiagnoseImmediateInvocation(SemaRef, CE); | |||
15494 | for (auto DR : Rec.ReferenceToConsteval) { | |||
15495 | auto *FD = cast<FunctionDecl>(DR->getDecl()); | |||
15496 | SemaRef.Diag(DR->getBeginLoc(), diag::err_invalid_consteval_take_address) | |||
15497 | << FD; | |||
15498 | SemaRef.Diag(FD->getLocation(), diag::note_declared_at); | |||
15499 | } | |||
15500 | } | |||
15501 | ||||
15502 | void Sema::PopExpressionEvaluationContext() { | |||
15503 | ExpressionEvaluationContextRecord& Rec = ExprEvalContexts.back(); | |||
15504 | unsigned NumTypos = Rec.NumTypos; | |||
15505 | ||||
15506 | if (!Rec.Lambdas.empty()) { | |||
15507 | using ExpressionKind = ExpressionEvaluationContextRecord::ExpressionKind; | |||
15508 | if (Rec.ExprContext == ExpressionKind::EK_TemplateArgument || Rec.isUnevaluated() || | |||
15509 | (Rec.isConstantEvaluated() && !getLangOpts().CPlusPlus17)) { | |||
15510 | unsigned D; | |||
15511 | if (Rec.isUnevaluated()) { | |||
15512 | // C++11 [expr.prim.lambda]p2: | |||
15513 | // A lambda-expression shall not appear in an unevaluated operand | |||
15514 | // (Clause 5). | |||
15515 | D = diag::err_lambda_unevaluated_operand; | |||
15516 | } else if (Rec.isConstantEvaluated() && !getLangOpts().CPlusPlus17) { | |||
15517 | // C++1y [expr.const]p2: | |||
15518 | // A conditional-expression e is a core constant expression unless the | |||
15519 | // evaluation of e, following the rules of the abstract machine, would | |||
15520 | // evaluate [...] a lambda-expression. | |||
15521 | D = diag::err_lambda_in_constant_expression; | |||
15522 | } else if (Rec.ExprContext == ExpressionKind::EK_TemplateArgument) { | |||
15523 | // C++17 [expr.prim.lamda]p2: | |||
15524 | // A lambda-expression shall not appear [...] in a template-argument. | |||
15525 | D = diag::err_lambda_in_invalid_context; | |||
15526 | } else | |||
15527 | llvm_unreachable("Couldn't infer lambda error message.")::llvm::llvm_unreachable_internal("Couldn't infer lambda error message." , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 15527); | |||
15528 | ||||
15529 | for (const auto *L : Rec.Lambdas) | |||
15530 | Diag(L->getBeginLoc(), D); | |||
15531 | } | |||
15532 | } | |||
15533 | ||||
15534 | WarnOnPendingNoDerefs(Rec); | |||
15535 | HandleImmediateInvocations(*this, Rec); | |||
15536 | ||||
15537 | // Warn on any volatile-qualified simple-assignments that are not discarded- | |||
15538 | // value expressions nor unevaluated operands (those cases get removed from | |||
15539 | // this list by CheckUnusedVolatileAssignment). | |||
15540 | for (auto *BO : Rec.VolatileAssignmentLHSs) | |||
15541 | Diag(BO->getBeginLoc(), diag::warn_deprecated_simple_assign_volatile) | |||
15542 | << BO->getType(); | |||
15543 | ||||
15544 | // When are coming out of an unevaluated context, clear out any | |||
15545 | // temporaries that we may have created as part of the evaluation of | |||
15546 | // the expression in that context: they aren't relevant because they | |||
15547 | // will never be constructed. | |||
15548 | if (Rec.isUnevaluated() || Rec.isConstantEvaluated()) { | |||
15549 | ExprCleanupObjects.erase(ExprCleanupObjects.begin() + Rec.NumCleanupObjects, | |||
15550 | ExprCleanupObjects.end()); | |||
15551 | Cleanup = Rec.ParentCleanup; | |||
15552 | CleanupVarDeclMarking(); | |||
15553 | std::swap(MaybeODRUseExprs, Rec.SavedMaybeODRUseExprs); | |||
15554 | // Otherwise, merge the contexts together. | |||
15555 | } else { | |||
15556 | Cleanup.mergeFrom(Rec.ParentCleanup); | |||
15557 | MaybeODRUseExprs.insert(Rec.SavedMaybeODRUseExprs.begin(), | |||
15558 | Rec.SavedMaybeODRUseExprs.end()); | |||
15559 | } | |||
15560 | ||||
15561 | // Pop the current expression evaluation context off the stack. | |||
15562 | ExprEvalContexts.pop_back(); | |||
15563 | ||||
15564 | // The global expression evaluation context record is never popped. | |||
15565 | ExprEvalContexts.back().NumTypos += NumTypos; | |||
15566 | } | |||
15567 | ||||
15568 | void Sema::DiscardCleanupsInEvaluationContext() { | |||
15569 | ExprCleanupObjects.erase( | |||
15570 | ExprCleanupObjects.begin() + ExprEvalContexts.back().NumCleanupObjects, | |||
15571 | ExprCleanupObjects.end()); | |||
15572 | Cleanup.reset(); | |||
15573 | MaybeODRUseExprs.clear(); | |||
15574 | } | |||
15575 | ||||
15576 | ExprResult Sema::HandleExprEvaluationContextForTypeof(Expr *E) { | |||
15577 | ExprResult Result = CheckPlaceholderExpr(E); | |||
15578 | if (Result.isInvalid()) | |||
15579 | return ExprError(); | |||
15580 | E = Result.get(); | |||
15581 | if (!E->getType()->isVariablyModifiedType()) | |||
15582 | return E; | |||
15583 | return TransformToPotentiallyEvaluated(E); | |||
15584 | } | |||
15585 | ||||
15586 | /// Are we in a context that is potentially constant evaluated per C++20 | |||
15587 | /// [expr.const]p12? | |||
15588 | static bool isPotentiallyConstantEvaluatedContext(Sema &SemaRef) { | |||
15589 | /// C++2a [expr.const]p12: | |||
15590 | // An expression or conversion is potentially constant evaluated if it is | |||
15591 | switch (SemaRef.ExprEvalContexts.back().Context) { | |||
15592 | case Sema::ExpressionEvaluationContext::ConstantEvaluated: | |||
15593 | // -- a manifestly constant-evaluated expression, | |||
15594 | case Sema::ExpressionEvaluationContext::PotentiallyEvaluated: | |||
15595 | case Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed: | |||
15596 | case Sema::ExpressionEvaluationContext::DiscardedStatement: | |||
15597 | // -- a potentially-evaluated expression, | |||
15598 | case Sema::ExpressionEvaluationContext::UnevaluatedList: | |||
15599 | // -- an immediate subexpression of a braced-init-list, | |||
15600 | ||||
15601 | // -- [FIXME] an expression of the form & cast-expression that occurs | |||
15602 | // within a templated entity | |||
15603 | // -- a subexpression of one of the above that is not a subexpression of | |||
15604 | // a nested unevaluated operand. | |||
15605 | return true; | |||
15606 | ||||
15607 | case Sema::ExpressionEvaluationContext::Unevaluated: | |||
15608 | case Sema::ExpressionEvaluationContext::UnevaluatedAbstract: | |||
15609 | // Expressions in this context are never evaluated. | |||
15610 | return false; | |||
15611 | } | |||
15612 | llvm_unreachable("Invalid context")::llvm::llvm_unreachable_internal("Invalid context", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 15612); | |||
15613 | } | |||
15614 | ||||
15615 | /// Return true if this function has a calling convention that requires mangling | |||
15616 | /// in the size of the parameter pack. | |||
15617 | static bool funcHasParameterSizeMangling(Sema &S, FunctionDecl *FD) { | |||
15618 | // These manglings don't do anything on non-Windows or non-x86 platforms, so | |||
15619 | // we don't need parameter type sizes. | |||
15620 | const llvm::Triple &TT = S.Context.getTargetInfo().getTriple(); | |||
15621 | if (!TT.isOSWindows() || !TT.isX86()) | |||
15622 | return false; | |||
15623 | ||||
15624 | // If this is C++ and this isn't an extern "C" function, parameters do not | |||
15625 | // need to be complete. In this case, C++ mangling will apply, which doesn't | |||
15626 | // use the size of the parameters. | |||
15627 | if (S.getLangOpts().CPlusPlus && !FD->isExternC()) | |||
15628 | return false; | |||
15629 | ||||
15630 | // Stdcall, fastcall, and vectorcall need this special treatment. | |||
15631 | CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv(); | |||
15632 | switch (CC) { | |||
15633 | case CC_X86StdCall: | |||
15634 | case CC_X86FastCall: | |||
15635 | case CC_X86VectorCall: | |||
15636 | return true; | |||
15637 | default: | |||
15638 | break; | |||
15639 | } | |||
15640 | return false; | |||
15641 | } | |||
15642 | ||||
15643 | /// Require that all of the parameter types of function be complete. Normally, | |||
15644 | /// parameter types are only required to be complete when a function is called | |||
15645 | /// or defined, but to mangle functions with certain calling conventions, the | |||
15646 | /// mangler needs to know the size of the parameter list. In this situation, | |||
15647 | /// MSVC doesn't emit an error or instantiate templates. Instead, MSVC mangles | |||
15648 | /// the function as _foo@0, i.e. zero bytes of parameters, which will usually | |||
15649 | /// result in a linker error. Clang doesn't implement this behavior, and instead | |||
15650 | /// attempts to error at compile time. | |||
15651 | static void CheckCompleteParameterTypesForMangler(Sema &S, FunctionDecl *FD, | |||
15652 | SourceLocation Loc) { | |||
15653 | class ParamIncompleteTypeDiagnoser : public Sema::TypeDiagnoser { | |||
15654 | FunctionDecl *FD; | |||
15655 | ParmVarDecl *Param; | |||
15656 | ||||
15657 | public: | |||
15658 | ParamIncompleteTypeDiagnoser(FunctionDecl *FD, ParmVarDecl *Param) | |||
15659 | : FD(FD), Param(Param) {} | |||
15660 | ||||
15661 | void diagnose(Sema &S, SourceLocation Loc, QualType T) override { | |||
15662 | CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv(); | |||
15663 | StringRef CCName; | |||
15664 | switch (CC) { | |||
15665 | case CC_X86StdCall: | |||
15666 | CCName = "stdcall"; | |||
15667 | break; | |||
15668 | case CC_X86FastCall: | |||
15669 | CCName = "fastcall"; | |||
15670 | break; | |||
15671 | case CC_X86VectorCall: | |||
15672 | CCName = "vectorcall"; | |||
15673 | break; | |||
15674 | default: | |||
15675 | llvm_unreachable("CC does not need mangling")::llvm::llvm_unreachable_internal("CC does not need mangling" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 15675); | |||
15676 | } | |||
15677 | ||||
15678 | S.Diag(Loc, diag::err_cconv_incomplete_param_type) | |||
15679 | << Param->getDeclName() << FD->getDeclName() << CCName; | |||
15680 | } | |||
15681 | }; | |||
15682 | ||||
15683 | for (ParmVarDecl *Param : FD->parameters()) { | |||
15684 | ParamIncompleteTypeDiagnoser Diagnoser(FD, Param); | |||
15685 | S.RequireCompleteType(Loc, Param->getType(), Diagnoser); | |||
15686 | } | |||
15687 | } | |||
15688 | ||||
15689 | namespace { | |||
15690 | enum class OdrUseContext { | |||
15691 | /// Declarations in this context are not odr-used. | |||
15692 | None, | |||
15693 | /// Declarations in this context are formally odr-used, but this is a | |||
15694 | /// dependent context. | |||
15695 | Dependent, | |||
15696 | /// Declarations in this context are odr-used but not actually used (yet). | |||
15697 | FormallyOdrUsed, | |||
15698 | /// Declarations in this context are used. | |||
15699 | Used | |||
15700 | }; | |||
15701 | } | |||
15702 | ||||
15703 | /// Are we within a context in which references to resolved functions or to | |||
15704 | /// variables result in odr-use? | |||
15705 | static OdrUseContext isOdrUseContext(Sema &SemaRef) { | |||
15706 | OdrUseContext Result; | |||
15707 | ||||
15708 | switch (SemaRef.ExprEvalContexts.back().Context) { | |||
15709 | case Sema::ExpressionEvaluationContext::Unevaluated: | |||
15710 | case Sema::ExpressionEvaluationContext::UnevaluatedList: | |||
15711 | case Sema::ExpressionEvaluationContext::UnevaluatedAbstract: | |||
15712 | return OdrUseContext::None; | |||
15713 | ||||
15714 | case Sema::ExpressionEvaluationContext::ConstantEvaluated: | |||
15715 | case Sema::ExpressionEvaluationContext::PotentiallyEvaluated: | |||
15716 | Result = OdrUseContext::Used; | |||
15717 | break; | |||
15718 | ||||
15719 | case Sema::ExpressionEvaluationContext::DiscardedStatement: | |||
15720 | Result = OdrUseContext::FormallyOdrUsed; | |||
15721 | break; | |||
15722 | ||||
15723 | case Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed: | |||
15724 | // A default argument formally results in odr-use, but doesn't actually | |||
15725 | // result in a use in any real sense until it itself is used. | |||
15726 | Result = OdrUseContext::FormallyOdrUsed; | |||
15727 | break; | |||
15728 | } | |||
15729 | ||||
15730 | if (SemaRef.CurContext->isDependentContext()) | |||
15731 | return OdrUseContext::Dependent; | |||
15732 | ||||
15733 | return Result; | |||
15734 | } | |||
15735 | ||||
15736 | static bool isImplicitlyDefinableConstexprFunction(FunctionDecl *Func) { | |||
15737 | return Func->isConstexpr() && | |||
15738 | (Func->isImplicitlyInstantiable() || !Func->isUserProvided()); | |||
15739 | } | |||
15740 | ||||
15741 | /// Mark a function referenced, and check whether it is odr-used | |||
15742 | /// (C++ [basic.def.odr]p2, C99 6.9p3) | |||
15743 | void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func, | |||
15744 | bool MightBeOdrUse) { | |||
15745 | assert(Func && "No function?")((Func && "No function?") ? static_cast<void> ( 0) : __assert_fail ("Func && \"No function?\"", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 15745, __PRETTY_FUNCTION__)); | |||
15746 | ||||
15747 | Func->setReferenced(); | |||
15748 | ||||
15749 | // Recursive functions aren't really used until they're used from some other | |||
15750 | // context. | |||
15751 | bool IsRecursiveCall = CurContext == Func; | |||
15752 | ||||
15753 | // C++11 [basic.def.odr]p3: | |||
15754 | // A function whose name appears as a potentially-evaluated expression is | |||
15755 | // odr-used if it is the unique lookup result or the selected member of a | |||
15756 | // set of overloaded functions [...]. | |||
15757 | // | |||
15758 | // We (incorrectly) mark overload resolution as an unevaluated context, so we | |||
15759 | // can just check that here. | |||
15760 | OdrUseContext OdrUse = | |||
15761 | MightBeOdrUse ? isOdrUseContext(*this) : OdrUseContext::None; | |||
15762 | if (IsRecursiveCall && OdrUse == OdrUseContext::Used) | |||
15763 | OdrUse = OdrUseContext::FormallyOdrUsed; | |||
15764 | ||||
15765 | // Trivial default constructors and destructors are never actually used. | |||
15766 | // FIXME: What about other special members? | |||
15767 | if (Func->isTrivial() && !Func->hasAttr<DLLExportAttr>() && | |||
15768 | OdrUse == OdrUseContext::Used) { | |||
15769 | if (auto *Constructor = dyn_cast<CXXConstructorDecl>(Func)) | |||
15770 | if (Constructor->isDefaultConstructor()) | |||
15771 | OdrUse = OdrUseContext::FormallyOdrUsed; | |||
15772 | if (isa<CXXDestructorDecl>(Func)) | |||
15773 | OdrUse = OdrUseContext::FormallyOdrUsed; | |||
15774 | } | |||
15775 | ||||
15776 | // C++20 [expr.const]p12: | |||
15777 | // A function [...] is needed for constant evaluation if it is [...] a | |||
15778 | // constexpr function that is named by an expression that is potentially | |||
15779 | // constant evaluated | |||
15780 | bool NeededForConstantEvaluation = | |||
15781 | isPotentiallyConstantEvaluatedContext(*this) && | |||
15782 | isImplicitlyDefinableConstexprFunction(Func); | |||
15783 | ||||
15784 | // Determine whether we require a function definition to exist, per | |||
15785 | // C++11 [temp.inst]p3: | |||
15786 | // Unless a function template specialization has been explicitly | |||
15787 | // instantiated or explicitly specialized, the function template | |||
15788 | // specialization is implicitly instantiated when the specialization is | |||
15789 | // referenced in a context that requires a function definition to exist. | |||
15790 | // C++20 [temp.inst]p7: | |||
15791 | // The existence of a definition of a [...] function is considered to | |||
15792 | // affect the semantics of the program if the [...] function is needed for | |||
15793 | // constant evaluation by an expression | |||
15794 | // C++20 [basic.def.odr]p10: | |||
15795 | // Every program shall contain exactly one definition of every non-inline | |||
15796 | // function or variable that is odr-used in that program outside of a | |||
15797 | // discarded statement | |||
15798 | // C++20 [special]p1: | |||
15799 | // The implementation will implicitly define [defaulted special members] | |||
15800 | // if they are odr-used or needed for constant evaluation. | |||
15801 | // | |||
15802 | // Note that we skip the implicit instantiation of templates that are only | |||
15803 | // used in unused default arguments or by recursive calls to themselves. | |||
15804 | // This is formally non-conforming, but seems reasonable in practice. | |||
15805 | bool NeedDefinition = !IsRecursiveCall && (OdrUse == OdrUseContext::Used || | |||
15806 | NeededForConstantEvaluation); | |||
15807 | ||||
15808 | // C++14 [temp.expl.spec]p6: | |||
15809 | // If a template [...] is explicitly specialized then that specialization | |||
15810 | // shall be declared before the first use of that specialization that would | |||
15811 | // cause an implicit instantiation to take place, in every translation unit | |||
15812 | // in which such a use occurs | |||
15813 | if (NeedDefinition && | |||
15814 | (Func->getTemplateSpecializationKind() != TSK_Undeclared || | |||
15815 | Func->getMemberSpecializationInfo())) | |||
15816 | checkSpecializationVisibility(Loc, Func); | |||
15817 | ||||
15818 | if (getLangOpts().CUDA) | |||
15819 | CheckCUDACall(Loc, Func); | |||
15820 | ||||
15821 | // If we need a definition, try to create one. | |||
15822 | if (NeedDefinition && !Func->getBody()) { | |||
15823 | runWithSufficientStackSpace(Loc, [&] { | |||
15824 | if (CXXConstructorDecl *Constructor = | |||
15825 | dyn_cast<CXXConstructorDecl>(Func)) { | |||
15826 | Constructor = cast<CXXConstructorDecl>(Constructor->getFirstDecl()); | |||
15827 | if (Constructor->isDefaulted() && !Constructor->isDeleted()) { | |||
15828 | if (Constructor->isDefaultConstructor()) { | |||
15829 | if (Constructor->isTrivial() && | |||
15830 | !Constructor->hasAttr<DLLExportAttr>()) | |||
15831 | return; | |||
15832 | DefineImplicitDefaultConstructor(Loc, Constructor); | |||
15833 | } else if (Constructor->isCopyConstructor()) { | |||
15834 | DefineImplicitCopyConstructor(Loc, Constructor); | |||
15835 | } else if (Constructor->isMoveConstructor()) { | |||
15836 | DefineImplicitMoveConstructor(Loc, Constructor); | |||
15837 | } | |||
15838 | } else if (Constructor->getInheritedConstructor()) { | |||
15839 | DefineInheritingConstructor(Loc, Constructor); | |||
15840 | } | |||
15841 | } else if (CXXDestructorDecl *Destructor = | |||
15842 | dyn_cast<CXXDestructorDecl>(Func)) { | |||
15843 | Destructor = cast<CXXDestructorDecl>(Destructor->getFirstDecl()); | |||
15844 | if (Destructor->isDefaulted() && !Destructor->isDeleted()) { | |||
15845 | if (Destructor->isTrivial() && !Destructor->hasAttr<DLLExportAttr>()) | |||
15846 | return; | |||
15847 | DefineImplicitDestructor(Loc, Destructor); | |||
15848 | } | |||
15849 | if (Destructor->isVirtual() && getLangOpts().AppleKext) | |||
15850 | MarkVTableUsed(Loc, Destructor->getParent()); | |||
15851 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(Func)) { | |||
15852 | if (MethodDecl->isOverloadedOperator() && | |||
15853 | MethodDecl->getOverloadedOperator() == OO_Equal) { | |||
15854 | MethodDecl = cast<CXXMethodDecl>(MethodDecl->getFirstDecl()); | |||
15855 | if (MethodDecl->isDefaulted() && !MethodDecl->isDeleted()) { | |||
15856 | if (MethodDecl->isCopyAssignmentOperator()) | |||
15857 | DefineImplicitCopyAssignment(Loc, MethodDecl); | |||
15858 | else if (MethodDecl->isMoveAssignmentOperator()) | |||
15859 | DefineImplicitMoveAssignment(Loc, MethodDecl); | |||
15860 | } | |||
15861 | } else if (isa<CXXConversionDecl>(MethodDecl) && | |||
15862 | MethodDecl->getParent()->isLambda()) { | |||
15863 | CXXConversionDecl *Conversion = | |||
15864 | cast<CXXConversionDecl>(MethodDecl->getFirstDecl()); | |||
15865 | if (Conversion->isLambdaToBlockPointerConversion()) | |||
15866 | DefineImplicitLambdaToBlockPointerConversion(Loc, Conversion); | |||
15867 | else | |||
15868 | DefineImplicitLambdaToFunctionPointerConversion(Loc, Conversion); | |||
15869 | } else if (MethodDecl->isVirtual() && getLangOpts().AppleKext) | |||
15870 | MarkVTableUsed(Loc, MethodDecl->getParent()); | |||
15871 | } | |||
15872 | ||||
15873 | if (Func->isDefaulted() && !Func->isDeleted()) { | |||
15874 | DefaultedComparisonKind DCK = getDefaultedComparisonKind(Func); | |||
15875 | if (DCK != DefaultedComparisonKind::None) | |||
15876 | DefineDefaultedComparison(Loc, Func, DCK); | |||
15877 | } | |||
15878 | ||||
15879 | // Implicit instantiation of function templates and member functions of | |||
15880 | // class templates. | |||
15881 | if (Func->isImplicitlyInstantiable()) { | |||
15882 | TemplateSpecializationKind TSK = | |||
15883 | Func->getTemplateSpecializationKindForInstantiation(); | |||
15884 | SourceLocation PointOfInstantiation = Func->getPointOfInstantiation(); | |||
15885 | bool FirstInstantiation = PointOfInstantiation.isInvalid(); | |||
15886 | if (FirstInstantiation) { | |||
15887 | PointOfInstantiation = Loc; | |||
15888 | Func->setTemplateSpecializationKind(TSK, PointOfInstantiation); | |||
15889 | } else if (TSK != TSK_ImplicitInstantiation) { | |||
15890 | // Use the point of use as the point of instantiation, instead of the | |||
15891 | // point of explicit instantiation (which we track as the actual point | |||
15892 | // of instantiation). This gives better backtraces in diagnostics. | |||
15893 | PointOfInstantiation = Loc; | |||
15894 | } | |||
15895 | ||||
15896 | if (FirstInstantiation || TSK != TSK_ImplicitInstantiation || | |||
15897 | Func->isConstexpr()) { | |||
15898 | if (isa<CXXRecordDecl>(Func->getDeclContext()) && | |||
15899 | cast<CXXRecordDecl>(Func->getDeclContext())->isLocalClass() && | |||
15900 | CodeSynthesisContexts.size()) | |||
15901 | PendingLocalImplicitInstantiations.push_back( | |||
15902 | std::make_pair(Func, PointOfInstantiation)); | |||
15903 | else if (Func->isConstexpr()) | |||
15904 | // Do not defer instantiations of constexpr functions, to avoid the | |||
15905 | // expression evaluator needing to call back into Sema if it sees a | |||
15906 | // call to such a function. | |||
15907 | InstantiateFunctionDefinition(PointOfInstantiation, Func); | |||
15908 | else { | |||
15909 | Func->setInstantiationIsPending(true); | |||
15910 | PendingInstantiations.push_back( | |||
15911 | std::make_pair(Func, PointOfInstantiation)); | |||
15912 | // Notify the consumer that a function was implicitly instantiated. | |||
15913 | Consumer.HandleCXXImplicitFunctionInstantiation(Func); | |||
15914 | } | |||
15915 | } | |||
15916 | } else { | |||
15917 | // Walk redefinitions, as some of them may be instantiable. | |||
15918 | for (auto i : Func->redecls()) { | |||
15919 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) | |||
15920 | MarkFunctionReferenced(Loc, i, MightBeOdrUse); | |||
15921 | } | |||
15922 | } | |||
15923 | }); | |||
15924 | } | |||
15925 | ||||
15926 | // C++14 [except.spec]p17: | |||
15927 | // An exception-specification is considered to be needed when: | |||
15928 | // - the function is odr-used or, if it appears in an unevaluated operand, | |||
15929 | // would be odr-used if the expression were potentially-evaluated; | |||
15930 | // | |||
15931 | // Note, we do this even if MightBeOdrUse is false. That indicates that the | |||
15932 | // function is a pure virtual function we're calling, and in that case the | |||
15933 | // function was selected by overload resolution and we need to resolve its | |||
15934 | // exception specification for a different reason. | |||
15935 | const FunctionProtoType *FPT = Func->getType()->getAs<FunctionProtoType>(); | |||
15936 | if (FPT && isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) | |||
15937 | ResolveExceptionSpec(Loc, FPT); | |||
15938 | ||||
15939 | // If this is the first "real" use, act on that. | |||
15940 | if (OdrUse == OdrUseContext::Used && !Func->isUsed(/*CheckUsedAttr=*/false)) { | |||
15941 | // Keep track of used but undefined functions. | |||
15942 | if (!Func->isDefined()) { | |||
15943 | if (mightHaveNonExternalLinkage(Func)) | |||
15944 | UndefinedButUsed.insert(std::make_pair(Func->getCanonicalDecl(), Loc)); | |||
15945 | else if (Func->getMostRecentDecl()->isInlined() && | |||
15946 | !LangOpts.GNUInline && | |||
15947 | !Func->getMostRecentDecl()->hasAttr<GNUInlineAttr>()) | |||
15948 | UndefinedButUsed.insert(std::make_pair(Func->getCanonicalDecl(), Loc)); | |||
15949 | else if (isExternalWithNoLinkageType(Func)) | |||
15950 | UndefinedButUsed.insert(std::make_pair(Func->getCanonicalDecl(), Loc)); | |||
15951 | } | |||
15952 | ||||
15953 | // Some x86 Windows calling conventions mangle the size of the parameter | |||
15954 | // pack into the name. Computing the size of the parameters requires the | |||
15955 | // parameter types to be complete. Check that now. | |||
15956 | if (funcHasParameterSizeMangling(*this, Func)) | |||
15957 | CheckCompleteParameterTypesForMangler(*this, Func, Loc); | |||
15958 | ||||
15959 | Func->markUsed(Context); | |||
15960 | } | |||
15961 | ||||
15962 | if (LangOpts.OpenMP) { | |||
15963 | markOpenMPDeclareVariantFuncsReferenced(Loc, Func, MightBeOdrUse); | |||
15964 | if (LangOpts.OpenMPIsDevice) | |||
15965 | checkOpenMPDeviceFunction(Loc, Func); | |||
15966 | else | |||
15967 | checkOpenMPHostFunction(Loc, Func); | |||
15968 | } | |||
15969 | } | |||
15970 | ||||
15971 | /// Directly mark a variable odr-used. Given a choice, prefer to use | |||
15972 | /// MarkVariableReferenced since it does additional checks and then | |||
15973 | /// calls MarkVarDeclODRUsed. | |||
15974 | /// If the variable must be captured: | |||
15975 | /// - if FunctionScopeIndexToStopAt is null, capture it in the CurContext | |||
15976 | /// - else capture it in the DeclContext that maps to the | |||
15977 | /// *FunctionScopeIndexToStopAt on the FunctionScopeInfo stack. | |||
15978 | static void | |||
15979 | MarkVarDeclODRUsed(VarDecl *Var, SourceLocation Loc, Sema &SemaRef, | |||
15980 | const unsigned *const FunctionScopeIndexToStopAt = nullptr) { | |||
15981 | // Keep track of used but undefined variables. | |||
15982 | // FIXME: We shouldn't suppress this warning for static data members. | |||
15983 | if (Var->hasDefinition(SemaRef.Context) == VarDecl::DeclarationOnly && | |||
15984 | (!Var->isExternallyVisible() || Var->isInline() || | |||
15985 | SemaRef.isExternalWithNoLinkageType(Var)) && | |||
15986 | !(Var->isStaticDataMember() && Var->hasInit())) { | |||
15987 | SourceLocation &old = SemaRef.UndefinedButUsed[Var->getCanonicalDecl()]; | |||
15988 | if (old.isInvalid()) | |||
15989 | old = Loc; | |||
15990 | } | |||
15991 | QualType CaptureType, DeclRefType; | |||
15992 | if (SemaRef.LangOpts.OpenMP) | |||
15993 | SemaRef.tryCaptureOpenMPLambdas(Var); | |||
15994 | SemaRef.tryCaptureVariable(Var, Loc, Sema::TryCapture_Implicit, | |||
15995 | /*EllipsisLoc*/ SourceLocation(), | |||
15996 | /*BuildAndDiagnose*/ true, | |||
15997 | CaptureType, DeclRefType, | |||
15998 | FunctionScopeIndexToStopAt); | |||
15999 | ||||
16000 | Var->markUsed(SemaRef.Context); | |||
16001 | } | |||
16002 | ||||
16003 | void Sema::MarkCaptureUsedInEnclosingContext(VarDecl *Capture, | |||
16004 | SourceLocation Loc, | |||
16005 | unsigned CapturingScopeIndex) { | |||
16006 | MarkVarDeclODRUsed(Capture, Loc, *this, &CapturingScopeIndex); | |||
16007 | } | |||
16008 | ||||
16009 | static void | |||
16010 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, | |||
16011 | ValueDecl *var, DeclContext *DC) { | |||
16012 | DeclContext *VarDC = var->getDeclContext(); | |||
16013 | ||||
16014 | // If the parameter still belongs to the translation unit, then | |||
16015 | // we're actually just using one parameter in the declaration of | |||
16016 | // the next. | |||
16017 | if (isa<ParmVarDecl>(var) && | |||
16018 | isa<TranslationUnitDecl>(VarDC)) | |||
16019 | return; | |||
16020 | ||||
16021 | // For C code, don't diagnose about capture if we're not actually in code | |||
16022 | // right now; it's impossible to write a non-constant expression outside of | |||
16023 | // function context, so we'll get other (more useful) diagnostics later. | |||
16024 | // | |||
16025 | // For C++, things get a bit more nasty... it would be nice to suppress this | |||
16026 | // diagnostic for certain cases like using a local variable in an array bound | |||
16027 | // for a member of a local class, but the correct predicate is not obvious. | |||
16028 | if (!S.getLangOpts().CPlusPlus && !S.CurContext->isFunctionOrMethod()) | |||
16029 | return; | |||
16030 | ||||
16031 | unsigned ValueKind = isa<BindingDecl>(var) ? 1 : 0; | |||
16032 | unsigned ContextKind = 3; // unknown | |||
16033 | if (isa<CXXMethodDecl>(VarDC) && | |||
16034 | cast<CXXRecordDecl>(VarDC->getParent())->isLambda()) { | |||
16035 | ContextKind = 2; | |||
16036 | } else if (isa<FunctionDecl>(VarDC)) { | |||
16037 | ContextKind = 0; | |||
16038 | } else if (isa<BlockDecl>(VarDC)) { | |||
16039 | ContextKind = 1; | |||
16040 | } | |||
16041 | ||||
16042 | S.Diag(loc, diag::err_reference_to_local_in_enclosing_context) | |||
16043 | << var << ValueKind << ContextKind << VarDC; | |||
16044 | S.Diag(var->getLocation(), diag::note_entity_declared_at) | |||
16045 | << var; | |||
16046 | ||||
16047 | // FIXME: Add additional diagnostic info about class etc. which prevents | |||
16048 | // capture. | |||
16049 | } | |||
16050 | ||||
16051 | ||||
16052 | static bool isVariableAlreadyCapturedInScopeInfo(CapturingScopeInfo *CSI, VarDecl *Var, | |||
16053 | bool &SubCapturesAreNested, | |||
16054 | QualType &CaptureType, | |||
16055 | QualType &DeclRefType) { | |||
16056 | // Check whether we've already captured it. | |||
16057 | if (CSI->CaptureMap.count(Var)) { | |||
16058 | // If we found a capture, any subcaptures are nested. | |||
16059 | SubCapturesAreNested = true; | |||
16060 | ||||
16061 | // Retrieve the capture type for this variable. | |||
16062 | CaptureType = CSI->getCapture(Var).getCaptureType(); | |||
16063 | ||||
16064 | // Compute the type of an expression that refers to this variable. | |||
16065 | DeclRefType = CaptureType.getNonReferenceType(); | |||
16066 | ||||
16067 | // Similarly to mutable captures in lambda, all the OpenMP captures by copy | |||
16068 | // are mutable in the sense that user can change their value - they are | |||
16069 | // private instances of the captured declarations. | |||
16070 | const Capture &Cap = CSI->getCapture(Var); | |||
16071 | if (Cap.isCopyCapture() && | |||
16072 | !(isa<LambdaScopeInfo>(CSI) && cast<LambdaScopeInfo>(CSI)->Mutable) && | |||
16073 | !(isa<CapturedRegionScopeInfo>(CSI) && | |||
16074 | cast<CapturedRegionScopeInfo>(CSI)->CapRegionKind == CR_OpenMP)) | |||
16075 | DeclRefType.addConst(); | |||
16076 | return true; | |||
16077 | } | |||
16078 | return false; | |||
16079 | } | |||
16080 | ||||
16081 | // Only block literals, captured statements, and lambda expressions can | |||
16082 | // capture; other scopes don't work. | |||
16083 | static DeclContext *getParentOfCapturingContextOrNull(DeclContext *DC, VarDecl *Var, | |||
16084 | SourceLocation Loc, | |||
16085 | const bool Diagnose, Sema &S) { | |||
16086 | if (isa<BlockDecl>(DC) || isa<CapturedDecl>(DC) || isLambdaCallOperator(DC)) | |||
16087 | return getLambdaAwareParentOfDeclContext(DC); | |||
16088 | else if (Var->hasLocalStorage()) { | |||
16089 | if (Diagnose) | |||
16090 | diagnoseUncapturableValueReference(S, Loc, Var, DC); | |||
16091 | } | |||
16092 | return nullptr; | |||
16093 | } | |||
16094 | ||||
16095 | // Certain capturing entities (lambdas, blocks etc.) are not allowed to capture | |||
16096 | // certain types of variables (unnamed, variably modified types etc.) | |||
16097 | // so check for eligibility. | |||
16098 | static bool isVariableCapturable(CapturingScopeInfo *CSI, VarDecl *Var, | |||
16099 | SourceLocation Loc, | |||
16100 | const bool Diagnose, Sema &S) { | |||
16101 | ||||
16102 | bool IsBlock = isa<BlockScopeInfo>(CSI); | |||
16103 | bool IsLambda = isa<LambdaScopeInfo>(CSI); | |||
16104 | ||||
16105 | // Lambdas are not allowed to capture unnamed variables | |||
16106 | // (e.g. anonymous unions). | |||
16107 | // FIXME: The C++11 rule don't actually state this explicitly, but I'm | |||
16108 | // assuming that's the intent. | |||
16109 | if (IsLambda && !Var->getDeclName()) { | |||
16110 | if (Diagnose) { | |||
16111 | S.Diag(Loc, diag::err_lambda_capture_anonymous_var); | |||
16112 | S.Diag(Var->getLocation(), diag::note_declared_at); | |||
16113 | } | |||
16114 | return false; | |||
16115 | } | |||
16116 | ||||
16117 | // Prohibit variably-modified types in blocks; they're difficult to deal with. | |||
16118 | if (Var->getType()->isVariablyModifiedType() && IsBlock) { | |||
16119 | if (Diagnose) { | |||
16120 | S.Diag(Loc, diag::err_ref_vm_type); | |||
16121 | S.Diag(Var->getLocation(), diag::note_previous_decl) | |||
16122 | << Var->getDeclName(); | |||
16123 | } | |||
16124 | return false; | |||
16125 | } | |||
16126 | // Prohibit structs with flexible array members too. | |||
16127 | // We cannot capture what is in the tail end of the struct. | |||
16128 | if (const RecordType *VTTy = Var->getType()->getAs<RecordType>()) { | |||
16129 | if (VTTy->getDecl()->hasFlexibleArrayMember()) { | |||
16130 | if (Diagnose) { | |||
16131 | if (IsBlock) | |||
16132 | S.Diag(Loc, diag::err_ref_flexarray_type); | |||
16133 | else | |||
16134 | S.Diag(Loc, diag::err_lambda_capture_flexarray_type) | |||
16135 | << Var->getDeclName(); | |||
16136 | S.Diag(Var->getLocation(), diag::note_previous_decl) | |||
16137 | << Var->getDeclName(); | |||
16138 | } | |||
16139 | return false; | |||
16140 | } | |||
16141 | } | |||
16142 | const bool HasBlocksAttr = Var->hasAttr<BlocksAttr>(); | |||
16143 | // Lambdas and captured statements are not allowed to capture __block | |||
16144 | // variables; they don't support the expected semantics. | |||
16145 | if (HasBlocksAttr && (IsLambda || isa<CapturedRegionScopeInfo>(CSI))) { | |||
16146 | if (Diagnose) { | |||
16147 | S.Diag(Loc, diag::err_capture_block_variable) | |||
16148 | << Var->getDeclName() << !IsLambda; | |||
16149 | S.Diag(Var->getLocation(), diag::note_previous_decl) | |||
16150 | << Var->getDeclName(); | |||
16151 | } | |||
16152 | return false; | |||
16153 | } | |||
16154 | // OpenCL v2.0 s6.12.5: Blocks cannot reference/capture other blocks | |||
16155 | if (S.getLangOpts().OpenCL && IsBlock && | |||
16156 | Var->getType()->isBlockPointerType()) { | |||
16157 | if (Diagnose) | |||
16158 | S.Diag(Loc, diag::err_opencl_block_ref_block); | |||
16159 | return false; | |||
16160 | } | |||
16161 | ||||
16162 | return true; | |||
16163 | } | |||
16164 | ||||
16165 | // Returns true if the capture by block was successful. | |||
16166 | static bool captureInBlock(BlockScopeInfo *BSI, VarDecl *Var, | |||
16167 | SourceLocation Loc, | |||
16168 | const bool BuildAndDiagnose, | |||
16169 | QualType &CaptureType, | |||
16170 | QualType &DeclRefType, | |||
16171 | const bool Nested, | |||
16172 | Sema &S, bool Invalid) { | |||
16173 | bool ByRef = false; | |||
16174 | ||||
16175 | // Blocks are not allowed to capture arrays, excepting OpenCL. | |||
16176 | // OpenCL v2.0 s1.12.5 (revision 40): arrays are captured by reference | |||
16177 | // (decayed to pointers). | |||
16178 | if (!Invalid && !S.getLangOpts().OpenCL && CaptureType->isArrayType()) { | |||
16179 | if (BuildAndDiagnose) { | |||
16180 | S.Diag(Loc, diag::err_ref_array_type); | |||
16181 | S.Diag(Var->getLocation(), diag::note_previous_decl) | |||
16182 | << Var->getDeclName(); | |||
16183 | Invalid = true; | |||
16184 | } else { | |||
16185 | return false; | |||
16186 | } | |||
16187 | } | |||
16188 | ||||
16189 | // Forbid the block-capture of autoreleasing variables. | |||
16190 | if (!Invalid && | |||
16191 | CaptureType.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) { | |||
16192 | if (BuildAndDiagnose) { | |||
16193 | S.Diag(Loc, diag::err_arc_autoreleasing_capture) | |||
16194 | << /*block*/ 0; | |||
16195 | S.Diag(Var->getLocation(), diag::note_previous_decl) | |||
16196 | << Var->getDeclName(); | |||
16197 | Invalid = true; | |||
16198 | } else { | |||
16199 | return false; | |||
16200 | } | |||
16201 | } | |||
16202 | ||||
16203 | // Warn about implicitly autoreleasing indirect parameters captured by blocks. | |||
16204 | if (const auto *PT = CaptureType->getAs<PointerType>()) { | |||
16205 | QualType PointeeTy = PT->getPointeeType(); | |||
16206 | ||||
16207 | if (!Invalid && PointeeTy->getAs<ObjCObjectPointerType>() && | |||
16208 | PointeeTy.getObjCLifetime() == Qualifiers::OCL_Autoreleasing && | |||
16209 | !S.Context.hasDirectOwnershipQualifier(PointeeTy)) { | |||
16210 | if (BuildAndDiagnose) { | |||
16211 | SourceLocation VarLoc = Var->getLocation(); | |||
16212 | S.Diag(Loc, diag::warn_block_capture_autoreleasing); | |||
16213 | S.Diag(VarLoc, diag::note_declare_parameter_strong); | |||
16214 | } | |||
16215 | } | |||
16216 | } | |||
16217 | ||||
16218 | const bool HasBlocksAttr = Var->hasAttr<BlocksAttr>(); | |||
16219 | if (HasBlocksAttr || CaptureType->isReferenceType() || | |||
16220 | (S.getLangOpts().OpenMP && S.isOpenMPCapturedDecl(Var))) { | |||
16221 | // Block capture by reference does not change the capture or | |||
16222 | // declaration reference types. | |||
16223 | ByRef = true; | |||
16224 | } else { | |||
16225 | // Block capture by copy introduces 'const'. | |||
16226 | CaptureType = CaptureType.getNonReferenceType().withConst(); | |||
16227 | DeclRefType = CaptureType; | |||
16228 | } | |||
16229 | ||||
16230 | // Actually capture the variable. | |||
16231 | if (BuildAndDiagnose) | |||
16232 | BSI->addCapture(Var, HasBlocksAttr, ByRef, Nested, Loc, SourceLocation(), | |||
16233 | CaptureType, Invalid); | |||
16234 | ||||
16235 | return !Invalid; | |||
16236 | } | |||
16237 | ||||
16238 | ||||
16239 | /// Capture the given variable in the captured region. | |||
16240 | static bool captureInCapturedRegion(CapturedRegionScopeInfo *RSI, | |||
16241 | VarDecl *Var, | |||
16242 | SourceLocation Loc, | |||
16243 | const bool BuildAndDiagnose, | |||
16244 | QualType &CaptureType, | |||
16245 | QualType &DeclRefType, | |||
16246 | const bool RefersToCapturedVariable, | |||
16247 | Sema &S, bool Invalid) { | |||
16248 | // By default, capture variables by reference. | |||
16249 | bool ByRef = true; | |||
16250 | // Using an LValue reference type is consistent with Lambdas (see below). | |||
16251 | if (S.getLangOpts().OpenMP && RSI->CapRegionKind == CR_OpenMP) { | |||
16252 | if (S.isOpenMPCapturedDecl(Var)) { | |||
16253 | bool HasConst = DeclRefType.isConstQualified(); | |||
16254 | DeclRefType = DeclRefType.getUnqualifiedType(); | |||
16255 | // Don't lose diagnostics about assignments to const. | |||
16256 | if (HasConst) | |||
16257 | DeclRefType.addConst(); | |||
16258 | } | |||
16259 | ByRef = S.isOpenMPCapturedByRef(Var, RSI->OpenMPLevel, | |||
16260 | RSI->OpenMPCaptureLevel); | |||
16261 | } | |||
16262 | ||||
16263 | if (ByRef) | |||
16264 | CaptureType = S.Context.getLValueReferenceType(DeclRefType); | |||
16265 | else | |||
16266 | CaptureType = DeclRefType; | |||
16267 | ||||
16268 | // Actually capture the variable. | |||
16269 | if (BuildAndDiagnose) | |||
16270 | RSI->addCapture(Var, /*isBlock*/ false, ByRef, RefersToCapturedVariable, | |||
16271 | Loc, SourceLocation(), CaptureType, Invalid); | |||
16272 | ||||
16273 | return !Invalid; | |||
16274 | } | |||
16275 | ||||
16276 | /// Capture the given variable in the lambda. | |||
16277 | static bool captureInLambda(LambdaScopeInfo *LSI, | |||
16278 | VarDecl *Var, | |||
16279 | SourceLocation Loc, | |||
16280 | const bool BuildAndDiagnose, | |||
16281 | QualType &CaptureType, | |||
16282 | QualType &DeclRefType, | |||
16283 | const bool RefersToCapturedVariable, | |||
16284 | const Sema::TryCaptureKind Kind, | |||
16285 | SourceLocation EllipsisLoc, | |||
16286 | const bool IsTopScope, | |||
16287 | Sema &S, bool Invalid) { | |||
16288 | // Determine whether we are capturing by reference or by value. | |||
16289 | bool ByRef = false; | |||
16290 | if (IsTopScope && Kind != Sema::TryCapture_Implicit) { | |||
16291 | ByRef = (Kind == Sema::TryCapture_ExplicitByRef); | |||
16292 | } else { | |||
16293 | ByRef = (LSI->ImpCaptureStyle == LambdaScopeInfo::ImpCap_LambdaByref); | |||
16294 | } | |||
16295 | ||||
16296 | // Compute the type of the field that will capture this variable. | |||
16297 | if (ByRef) { | |||
16298 | // C++11 [expr.prim.lambda]p15: | |||
16299 | // An entity is captured by reference if it is implicitly or | |||
16300 | // explicitly captured but not captured by copy. It is | |||
16301 | // unspecified whether additional unnamed non-static data | |||
16302 | // members are declared in the closure type for entities | |||
16303 | // captured by reference. | |||
16304 | // | |||
16305 | // FIXME: It is not clear whether we want to build an lvalue reference | |||
16306 | // to the DeclRefType or to CaptureType.getNonReferenceType(). GCC appears | |||
16307 | // to do the former, while EDG does the latter. Core issue 1249 will | |||
16308 | // clarify, but for now we follow GCC because it's a more permissive and | |||
16309 | // easily defensible position. | |||
16310 | CaptureType = S.Context.getLValueReferenceType(DeclRefType); | |||
16311 | } else { | |||
16312 | // C++11 [expr.prim.lambda]p14: | |||
16313 | // For each entity captured by copy, an unnamed non-static | |||
16314 | // data member is declared in the closure type. The | |||
16315 | // declaration order of these members is unspecified. The type | |||
16316 | // of such a data member is the type of the corresponding | |||
16317 | // captured entity if the entity is not a reference to an | |||
16318 | // object, or the referenced type otherwise. [Note: If the | |||
16319 | // captured entity is a reference to a function, the | |||
16320 | // corresponding data member is also a reference to a | |||
16321 | // function. - end note ] | |||
16322 | if (const ReferenceType *RefType = CaptureType->getAs<ReferenceType>()){ | |||
16323 | if (!RefType->getPointeeType()->isFunctionType()) | |||
16324 | CaptureType = RefType->getPointeeType(); | |||
16325 | } | |||
16326 | ||||
16327 | // Forbid the lambda copy-capture of autoreleasing variables. | |||
16328 | if (!Invalid && | |||
16329 | CaptureType.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) { | |||
16330 | if (BuildAndDiagnose) { | |||
16331 | S.Diag(Loc, diag::err_arc_autoreleasing_capture) << /*lambda*/ 1; | |||
16332 | S.Diag(Var->getLocation(), diag::note_previous_decl) | |||
16333 | << Var->getDeclName(); | |||
16334 | Invalid = true; | |||
16335 | } else { | |||
16336 | return false; | |||
16337 | } | |||
16338 | } | |||
16339 | ||||
16340 | // Make sure that by-copy captures are of a complete and non-abstract type. | |||
16341 | if (!Invalid && BuildAndDiagnose) { | |||
16342 | if (!CaptureType->isDependentType() && | |||
16343 | S.RequireCompleteType(Loc, CaptureType, | |||
16344 | diag::err_capture_of_incomplete_type, | |||
16345 | Var->getDeclName())) | |||
16346 | Invalid = true; | |||
16347 | else if (S.RequireNonAbstractType(Loc, CaptureType, | |||
16348 | diag::err_capture_of_abstract_type)) | |||
16349 | Invalid = true; | |||
16350 | } | |||
16351 | } | |||
16352 | ||||
16353 | // Compute the type of a reference to this captured variable. | |||
16354 | if (ByRef) | |||
16355 | DeclRefType = CaptureType.getNonReferenceType(); | |||
16356 | else { | |||
16357 | // C++ [expr.prim.lambda]p5: | |||
16358 | // The closure type for a lambda-expression has a public inline | |||
16359 | // function call operator [...]. This function call operator is | |||
16360 | // declared const (9.3.1) if and only if the lambda-expression's | |||
16361 | // parameter-declaration-clause is not followed by mutable. | |||
16362 | DeclRefType = CaptureType.getNonReferenceType(); | |||
16363 | if (!LSI->Mutable && !CaptureType->isReferenceType()) | |||
16364 | DeclRefType.addConst(); | |||
16365 | } | |||
16366 | ||||
16367 | // Add the capture. | |||
16368 | if (BuildAndDiagnose) | |||
16369 | LSI->addCapture(Var, /*isBlock=*/false, ByRef, RefersToCapturedVariable, | |||
16370 | Loc, EllipsisLoc, CaptureType, Invalid); | |||
16371 | ||||
16372 | return !Invalid; | |||
16373 | } | |||
16374 | ||||
16375 | bool Sema::tryCaptureVariable( | |||
16376 | VarDecl *Var, SourceLocation ExprLoc, TryCaptureKind Kind, | |||
16377 | SourceLocation EllipsisLoc, bool BuildAndDiagnose, QualType &CaptureType, | |||
16378 | QualType &DeclRefType, const unsigned *const FunctionScopeIndexToStopAt) { | |||
16379 | // An init-capture is notionally from the context surrounding its | |||
16380 | // declaration, but its parent DC is the lambda class. | |||
16381 | DeclContext *VarDC = Var->getDeclContext(); | |||
16382 | if (Var->isInitCapture()) | |||
16383 | VarDC = VarDC->getParent(); | |||
16384 | ||||
16385 | DeclContext *DC = CurContext; | |||
16386 | const unsigned MaxFunctionScopesIndex = FunctionScopeIndexToStopAt | |||
16387 | ? *FunctionScopeIndexToStopAt : FunctionScopes.size() - 1; | |||
16388 | // We need to sync up the Declaration Context with the | |||
16389 | // FunctionScopeIndexToStopAt | |||
16390 | if (FunctionScopeIndexToStopAt) { | |||
16391 | unsigned FSIndex = FunctionScopes.size() - 1; | |||
16392 | while (FSIndex != MaxFunctionScopesIndex) { | |||
16393 | DC = getLambdaAwareParentOfDeclContext(DC); | |||
16394 | --FSIndex; | |||
16395 | } | |||
16396 | } | |||
16397 | ||||
16398 | ||||
16399 | // If the variable is declared in the current context, there is no need to | |||
16400 | // capture it. | |||
16401 | if (VarDC == DC) return true; | |||
16402 | ||||
16403 | // Capture global variables if it is required to use private copy of this | |||
16404 | // variable. | |||
16405 | bool IsGlobal = !Var->hasLocalStorage(); | |||
16406 | if (IsGlobal && | |||
16407 | !(LangOpts.OpenMP && isOpenMPCapturedDecl(Var, /*CheckScopeInfo=*/true, | |||
16408 | MaxFunctionScopesIndex))) | |||
16409 | return true; | |||
16410 | Var = Var->getCanonicalDecl(); | |||
16411 | ||||
16412 | // Walk up the stack to determine whether we can capture the variable, | |||
16413 | // performing the "simple" checks that don't depend on type. We stop when | |||
16414 | // we've either hit the declared scope of the variable or find an existing | |||
16415 | // capture of that variable. We start from the innermost capturing-entity | |||
16416 | // (the DC) and ensure that all intervening capturing-entities | |||
16417 | // (blocks/lambdas etc.) between the innermost capturer and the variable`s | |||
16418 | // declcontext can either capture the variable or have already captured | |||
16419 | // the variable. | |||
16420 | CaptureType = Var->getType(); | |||
16421 | DeclRefType = CaptureType.getNonReferenceType(); | |||
16422 | bool Nested = false; | |||
16423 | bool Explicit = (Kind != TryCapture_Implicit); | |||
16424 | unsigned FunctionScopesIndex = MaxFunctionScopesIndex; | |||
16425 | do { | |||
16426 | // Only block literals, captured statements, and lambda expressions can | |||
16427 | // capture; other scopes don't work. | |||
16428 | DeclContext *ParentDC = getParentOfCapturingContextOrNull(DC, Var, | |||
16429 | ExprLoc, | |||
16430 | BuildAndDiagnose, | |||
16431 | *this); | |||
16432 | // We need to check for the parent *first* because, if we *have* | |||
16433 | // private-captured a global variable, we need to recursively capture it in | |||
16434 | // intermediate blocks, lambdas, etc. | |||
16435 | if (!ParentDC) { | |||
16436 | if (IsGlobal) { | |||
16437 | FunctionScopesIndex = MaxFunctionScopesIndex - 1; | |||
16438 | break; | |||
16439 | } | |||
16440 | return true; | |||
16441 | } | |||
16442 | ||||
16443 | FunctionScopeInfo *FSI = FunctionScopes[FunctionScopesIndex]; | |||
16444 | CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FSI); | |||
16445 | ||||
16446 | ||||
16447 | // Check whether we've already captured it. | |||
16448 | if (isVariableAlreadyCapturedInScopeInfo(CSI, Var, Nested, CaptureType, | |||
16449 | DeclRefType)) { | |||
16450 | CSI->getCapture(Var).markUsed(BuildAndDiagnose); | |||
16451 | break; | |||
16452 | } | |||
16453 | // If we are instantiating a generic lambda call operator body, | |||
16454 | // we do not want to capture new variables. What was captured | |||
16455 | // during either a lambdas transformation or initial parsing | |||
16456 | // should be used. | |||
16457 | if (isGenericLambdaCallOperatorSpecialization(DC)) { | |||
16458 | if (BuildAndDiagnose) { | |||
16459 | LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(CSI); | |||
16460 | if (LSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_None) { | |||
16461 | Diag(ExprLoc, diag::err_lambda_impcap) << Var->getDeclName(); | |||
16462 | Diag(Var->getLocation(), diag::note_previous_decl) | |||
16463 | << Var->getDeclName(); | |||
16464 | Diag(LSI->Lambda->getBeginLoc(), diag::note_lambda_decl); | |||
16465 | } else | |||
16466 | diagnoseUncapturableValueReference(*this, ExprLoc, Var, DC); | |||
16467 | } | |||
16468 | return true; | |||
16469 | } | |||
16470 | ||||
16471 | // Try to capture variable-length arrays types. | |||
16472 | if (Var->getType()->isVariablyModifiedType()) { | |||
16473 | // We're going to walk down into the type and look for VLA | |||
16474 | // expressions. | |||
16475 | QualType QTy = Var->getType(); | |||
16476 | if (ParmVarDecl *PVD = dyn_cast_or_null<ParmVarDecl>(Var)) | |||
16477 | QTy = PVD->getOriginalType(); | |||
16478 | captureVariablyModifiedType(Context, QTy, CSI); | |||
16479 | } | |||
16480 | ||||
16481 | if (getLangOpts().OpenMP) { | |||
16482 | if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(CSI)) { | |||
16483 | // OpenMP private variables should not be captured in outer scope, so | |||
16484 | // just break here. Similarly, global variables that are captured in a | |||
16485 | // target region should not be captured outside the scope of the region. | |||
16486 | if (RSI->CapRegionKind == CR_OpenMP) { | |||
16487 | bool IsOpenMPPrivateDecl = isOpenMPPrivateDecl(Var, RSI->OpenMPLevel); | |||
16488 | // If the variable is private (i.e. not captured) and has variably | |||
16489 | // modified type, we still need to capture the type for correct | |||
16490 | // codegen in all regions, associated with the construct. Currently, | |||
16491 | // it is captured in the innermost captured region only. | |||
16492 | if (IsOpenMPPrivateDecl && Var->getType()->isVariablyModifiedType()) { | |||
16493 | QualType QTy = Var->getType(); | |||
16494 | if (ParmVarDecl *PVD = dyn_cast_or_null<ParmVarDecl>(Var)) | |||
16495 | QTy = PVD->getOriginalType(); | |||
16496 | for (int I = 1, E = getNumberOfConstructScopes(RSI->OpenMPLevel); | |||
16497 | I < E; ++I) { | |||
16498 | auto *OuterRSI = cast<CapturedRegionScopeInfo>( | |||
16499 | FunctionScopes[FunctionScopesIndex - I]); | |||
16500 | assert(RSI->OpenMPLevel == OuterRSI->OpenMPLevel &&((RSI->OpenMPLevel == OuterRSI->OpenMPLevel && "Wrong number of captured regions associated with the " "OpenMP construct.") ? static_cast<void> (0) : __assert_fail ("RSI->OpenMPLevel == OuterRSI->OpenMPLevel && \"Wrong number of captured regions associated with the \" \"OpenMP construct.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 16502, __PRETTY_FUNCTION__)) | |||
16501 | "Wrong number of captured regions associated with the "((RSI->OpenMPLevel == OuterRSI->OpenMPLevel && "Wrong number of captured regions associated with the " "OpenMP construct.") ? static_cast<void> (0) : __assert_fail ("RSI->OpenMPLevel == OuterRSI->OpenMPLevel && \"Wrong number of captured regions associated with the \" \"OpenMP construct.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 16502, __PRETTY_FUNCTION__)) | |||
16502 | "OpenMP construct.")((RSI->OpenMPLevel == OuterRSI->OpenMPLevel && "Wrong number of captured regions associated with the " "OpenMP construct.") ? static_cast<void> (0) : __assert_fail ("RSI->OpenMPLevel == OuterRSI->OpenMPLevel && \"Wrong number of captured regions associated with the \" \"OpenMP construct.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 16502, __PRETTY_FUNCTION__)); | |||
16503 | captureVariablyModifiedType(Context, QTy, OuterRSI); | |||
16504 | } | |||
16505 | } | |||
16506 | bool IsTargetCap = | |||
16507 | !IsOpenMPPrivateDecl && | |||
16508 | isOpenMPTargetCapturedDecl(Var, RSI->OpenMPLevel, | |||
16509 | RSI->OpenMPCaptureLevel); | |||
16510 | // When we detect target captures we are looking from inside the | |||
16511 | // target region, therefore we need to propagate the capture from the | |||
16512 | // enclosing region. Therefore, the capture is not initially nested. | |||
16513 | if (IsTargetCap) | |||
16514 | adjustOpenMPTargetScopeIndex(FunctionScopesIndex, RSI->OpenMPLevel); | |||
16515 | ||||
16516 | if (IsTargetCap || IsOpenMPPrivateDecl) { | |||
16517 | Nested = !IsTargetCap; | |||
16518 | DeclRefType = DeclRefType.getUnqualifiedType(); | |||
16519 | CaptureType = Context.getLValueReferenceType(DeclRefType); | |||
16520 | break; | |||
16521 | } | |||
16522 | } | |||
16523 | } | |||
16524 | } | |||
16525 | if (CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_None && !Explicit) { | |||
16526 | // No capture-default, and this is not an explicit capture | |||
16527 | // so cannot capture this variable. | |||
16528 | if (BuildAndDiagnose) { | |||
16529 | Diag(ExprLoc, diag::err_lambda_impcap) << Var->getDeclName(); | |||
16530 | Diag(Var->getLocation(), diag::note_previous_decl) | |||
16531 | << Var->getDeclName(); | |||
16532 | if (cast<LambdaScopeInfo>(CSI)->Lambda) | |||
16533 | Diag(cast<LambdaScopeInfo>(CSI)->Lambda->getBeginLoc(), | |||
16534 | diag::note_lambda_decl); | |||
16535 | // FIXME: If we error out because an outer lambda can not implicitly | |||
16536 | // capture a variable that an inner lambda explicitly captures, we | |||
16537 | // should have the inner lambda do the explicit capture - because | |||
16538 | // it makes for cleaner diagnostics later. This would purely be done | |||
16539 | // so that the diagnostic does not misleadingly claim that a variable | |||
16540 | // can not be captured by a lambda implicitly even though it is captured | |||
16541 | // explicitly. Suggestion: | |||
16542 | // - create const bool VariableCaptureWasInitiallyExplicit = Explicit | |||
16543 | // at the function head | |||
16544 | // - cache the StartingDeclContext - this must be a lambda | |||
16545 | // - captureInLambda in the innermost lambda the variable. | |||
16546 | } | |||
16547 | return true; | |||
16548 | } | |||
16549 | ||||
16550 | FunctionScopesIndex--; | |||
16551 | DC = ParentDC; | |||
16552 | Explicit = false; | |||
16553 | } while (!VarDC->Equals(DC)); | |||
16554 | ||||
16555 | // Walk back down the scope stack, (e.g. from outer lambda to inner lambda) | |||
16556 | // computing the type of the capture at each step, checking type-specific | |||
16557 | // requirements, and adding captures if requested. | |||
16558 | // If the variable had already been captured previously, we start capturing | |||
16559 | // at the lambda nested within that one. | |||
16560 | bool Invalid = false; | |||
16561 | for (unsigned I = ++FunctionScopesIndex, N = MaxFunctionScopesIndex + 1; I != N; | |||
16562 | ++I) { | |||
16563 | CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FunctionScopes[I]); | |||
16564 | ||||
16565 | // Certain capturing entities (lambdas, blocks etc.) are not allowed to capture | |||
16566 | // certain types of variables (unnamed, variably modified types etc.) | |||
16567 | // so check for eligibility. | |||
16568 | if (!Invalid) | |||
16569 | Invalid = | |||
16570 | !isVariableCapturable(CSI, Var, ExprLoc, BuildAndDiagnose, *this); | |||
16571 | ||||
16572 | // After encountering an error, if we're actually supposed to capture, keep | |||
16573 | // capturing in nested contexts to suppress any follow-on diagnostics. | |||
16574 | if (Invalid && !BuildAndDiagnose) | |||
16575 | return true; | |||
16576 | ||||
16577 | if (BlockScopeInfo *BSI = dyn_cast<BlockScopeInfo>(CSI)) { | |||
16578 | Invalid = !captureInBlock(BSI, Var, ExprLoc, BuildAndDiagnose, CaptureType, | |||
16579 | DeclRefType, Nested, *this, Invalid); | |||
16580 | Nested = true; | |||
16581 | } else if (CapturedRegionScopeInfo *RSI = dyn_cast<CapturedRegionScopeInfo>(CSI)) { | |||
16582 | Invalid = !captureInCapturedRegion(RSI, Var, ExprLoc, BuildAndDiagnose, | |||
16583 | CaptureType, DeclRefType, Nested, | |||
16584 | *this, Invalid); | |||
16585 | Nested = true; | |||
16586 | } else { | |||
16587 | LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(CSI); | |||
16588 | Invalid = | |||
16589 | !captureInLambda(LSI, Var, ExprLoc, BuildAndDiagnose, CaptureType, | |||
16590 | DeclRefType, Nested, Kind, EllipsisLoc, | |||
16591 | /*IsTopScope*/ I == N - 1, *this, Invalid); | |||
16592 | Nested = true; | |||
16593 | } | |||
16594 | ||||
16595 | if (Invalid && !BuildAndDiagnose) | |||
16596 | return true; | |||
16597 | } | |||
16598 | return Invalid; | |||
16599 | } | |||
16600 | ||||
16601 | bool Sema::tryCaptureVariable(VarDecl *Var, SourceLocation Loc, | |||
16602 | TryCaptureKind Kind, SourceLocation EllipsisLoc) { | |||
16603 | QualType CaptureType; | |||
16604 | QualType DeclRefType; | |||
16605 | return tryCaptureVariable(Var, Loc, Kind, EllipsisLoc, | |||
16606 | /*BuildAndDiagnose=*/true, CaptureType, | |||
16607 | DeclRefType, nullptr); | |||
16608 | } | |||
16609 | ||||
16610 | bool Sema::NeedToCaptureVariable(VarDecl *Var, SourceLocation Loc) { | |||
16611 | QualType CaptureType; | |||
16612 | QualType DeclRefType; | |||
16613 | return !tryCaptureVariable(Var, Loc, TryCapture_Implicit, SourceLocation(), | |||
16614 | /*BuildAndDiagnose=*/false, CaptureType, | |||
16615 | DeclRefType, nullptr); | |||
16616 | } | |||
16617 | ||||
16618 | QualType Sema::getCapturedDeclRefType(VarDecl *Var, SourceLocation Loc) { | |||
16619 | QualType CaptureType; | |||
16620 | QualType DeclRefType; | |||
16621 | ||||
16622 | // Determine whether we can capture this variable. | |||
16623 | if (tryCaptureVariable(Var, Loc, TryCapture_Implicit, SourceLocation(), | |||
16624 | /*BuildAndDiagnose=*/false, CaptureType, | |||
16625 | DeclRefType, nullptr)) | |||
16626 | return QualType(); | |||
16627 | ||||
16628 | return DeclRefType; | |||
16629 | } | |||
16630 | ||||
16631 | namespace { | |||
16632 | // Helper to copy the template arguments from a DeclRefExpr or MemberExpr. | |||
16633 | // The produced TemplateArgumentListInfo* points to data stored within this | |||
16634 | // object, so should only be used in contexts where the pointer will not be | |||
16635 | // used after the CopiedTemplateArgs object is destroyed. | |||
16636 | class CopiedTemplateArgs { | |||
16637 | bool HasArgs; | |||
16638 | TemplateArgumentListInfo TemplateArgStorage; | |||
16639 | public: | |||
16640 | template<typename RefExpr> | |||
16641 | CopiedTemplateArgs(RefExpr *E) : HasArgs(E->hasExplicitTemplateArgs()) { | |||
16642 | if (HasArgs) | |||
16643 | E->copyTemplateArgumentsInto(TemplateArgStorage); | |||
16644 | } | |||
16645 | operator TemplateArgumentListInfo*() | |||
16646 | #ifdef __has_cpp_attribute | |||
16647 | #if0 __has_cpp_attribute(clang::lifetimebound)1 | |||
16648 | [[clang::lifetimebound]] | |||
16649 | #endif | |||
16650 | #endif | |||
16651 | { | |||
16652 | return HasArgs ? &TemplateArgStorage : nullptr; | |||
16653 | } | |||
16654 | }; | |||
16655 | } | |||
16656 | ||||
16657 | /// Walk the set of potential results of an expression and mark them all as | |||
16658 | /// non-odr-uses if they satisfy the side-conditions of the NonOdrUseReason. | |||
16659 | /// | |||
16660 | /// \return A new expression if we found any potential results, ExprEmpty() if | |||
16661 | /// not, and ExprError() if we diagnosed an error. | |||
16662 | static ExprResult rebuildPotentialResultsAsNonOdrUsed(Sema &S, Expr *E, | |||
16663 | NonOdrUseReason NOUR) { | |||
16664 | // Per C++11 [basic.def.odr], a variable is odr-used "unless it is | |||
16665 | // an object that satisfies the requirements for appearing in a | |||
16666 | // constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) | |||
16667 | // is immediately applied." This function handles the lvalue-to-rvalue | |||
16668 | // conversion part. | |||
16669 | // | |||
16670 | // If we encounter a node that claims to be an odr-use but shouldn't be, we | |||
16671 | // transform it into the relevant kind of non-odr-use node and rebuild the | |||
16672 | // tree of nodes leading to it. | |||
16673 | // | |||
16674 | // This is a mini-TreeTransform that only transforms a restricted subset of | |||
16675 | // nodes (and only certain operands of them). | |||
16676 | ||||
16677 | // Rebuild a subexpression. | |||
16678 | auto Rebuild = [&](Expr *Sub) { | |||
16679 | return rebuildPotentialResultsAsNonOdrUsed(S, Sub, NOUR); | |||
16680 | }; | |||
16681 | ||||
16682 | // Check whether a potential result satisfies the requirements of NOUR. | |||
16683 | auto IsPotentialResultOdrUsed = [&](NamedDecl *D) { | |||
16684 | // Any entity other than a VarDecl is always odr-used whenever it's named | |||
16685 | // in a potentially-evaluated expression. | |||
16686 | auto *VD = dyn_cast<VarDecl>(D); | |||
16687 | if (!VD) | |||
16688 | return true; | |||
16689 | ||||
16690 | // C++2a [basic.def.odr]p4: | |||
16691 | // A variable x whose name appears as a potentially-evalauted expression | |||
16692 | // e is odr-used by e unless | |||
16693 | // -- x is a reference that is usable in constant expressions, or | |||
16694 | // -- x is a variable of non-reference type that is usable in constant | |||
16695 | // expressions and has no mutable subobjects, and e is an element of | |||
16696 | // the set of potential results of an expression of | |||
16697 | // non-volatile-qualified non-class type to which the lvalue-to-rvalue | |||
16698 | // conversion is applied, or | |||
16699 | // -- x is a variable of non-reference type, and e is an element of the | |||
16700 | // set of potential results of a discarded-value expression to which | |||
16701 | // the lvalue-to-rvalue conversion is not applied | |||
16702 | // | |||
16703 | // We check the first bullet and the "potentially-evaluated" condition in | |||
16704 | // BuildDeclRefExpr. We check the type requirements in the second bullet | |||
16705 | // in CheckLValueToRValueConversionOperand below. | |||
16706 | switch (NOUR) { | |||
16707 | case NOUR_None: | |||
16708 | case NOUR_Unevaluated: | |||
16709 | llvm_unreachable("unexpected non-odr-use-reason")::llvm::llvm_unreachable_internal("unexpected non-odr-use-reason" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 16709); | |||
16710 | ||||
16711 | case NOUR_Constant: | |||
16712 | // Constant references were handled when they were built. | |||
16713 | if (VD->getType()->isReferenceType()) | |||
16714 | return true; | |||
16715 | if (auto *RD = VD->getType()->getAsCXXRecordDecl()) | |||
16716 | if (RD->hasMutableFields()) | |||
16717 | return true; | |||
16718 | if (!VD->isUsableInConstantExpressions(S.Context)) | |||
16719 | return true; | |||
16720 | break; | |||
16721 | ||||
16722 | case NOUR_Discarded: | |||
16723 | if (VD->getType()->isReferenceType()) | |||
16724 | return true; | |||
16725 | break; | |||
16726 | } | |||
16727 | return false; | |||
16728 | }; | |||
16729 | ||||
16730 | // Mark that this expression does not constitute an odr-use. | |||
16731 | auto MarkNotOdrUsed = [&] { | |||
16732 | S.MaybeODRUseExprs.erase(E); | |||
16733 | if (LambdaScopeInfo *LSI = S.getCurLambda()) | |||
16734 | LSI->markVariableExprAsNonODRUsed(E); | |||
16735 | }; | |||
16736 | ||||
16737 | // C++2a [basic.def.odr]p2: | |||
16738 | // The set of potential results of an expression e is defined as follows: | |||
16739 | switch (E->getStmtClass()) { | |||
16740 | // -- If e is an id-expression, ... | |||
16741 | case Expr::DeclRefExprClass: { | |||
16742 | auto *DRE = cast<DeclRefExpr>(E); | |||
16743 | if (DRE->isNonOdrUse() || IsPotentialResultOdrUsed(DRE->getDecl())) | |||
16744 | break; | |||
16745 | ||||
16746 | // Rebuild as a non-odr-use DeclRefExpr. | |||
16747 | MarkNotOdrUsed(); | |||
16748 | return DeclRefExpr::Create( | |||
16749 | S.Context, DRE->getQualifierLoc(), DRE->getTemplateKeywordLoc(), | |||
16750 | DRE->getDecl(), DRE->refersToEnclosingVariableOrCapture(), | |||
16751 | DRE->getNameInfo(), DRE->getType(), DRE->getValueKind(), | |||
16752 | DRE->getFoundDecl(), CopiedTemplateArgs(DRE), NOUR); | |||
16753 | } | |||
16754 | ||||
16755 | case Expr::FunctionParmPackExprClass: { | |||
16756 | auto *FPPE = cast<FunctionParmPackExpr>(E); | |||
16757 | // If any of the declarations in the pack is odr-used, then the expression | |||
16758 | // as a whole constitutes an odr-use. | |||
16759 | for (VarDecl *D : *FPPE) | |||
16760 | if (IsPotentialResultOdrUsed(D)) | |||
16761 | return ExprEmpty(); | |||
16762 | ||||
16763 | // FIXME: Rebuild as a non-odr-use FunctionParmPackExpr? In practice, | |||
16764 | // nothing cares about whether we marked this as an odr-use, but it might | |||
16765 | // be useful for non-compiler tools. | |||
16766 | MarkNotOdrUsed(); | |||
16767 | break; | |||
16768 | } | |||
16769 | ||||
16770 | // -- If e is a subscripting operation with an array operand... | |||
16771 | case Expr::ArraySubscriptExprClass: { | |||
16772 | auto *ASE = cast<ArraySubscriptExpr>(E); | |||
16773 | Expr *OldBase = ASE->getBase()->IgnoreImplicit(); | |||
16774 | if (!OldBase->getType()->isArrayType()) | |||
16775 | break; | |||
16776 | ExprResult Base = Rebuild(OldBase); | |||
16777 | if (!Base.isUsable()) | |||
16778 | return Base; | |||
16779 | Expr *LHS = ASE->getBase() == ASE->getLHS() ? Base.get() : ASE->getLHS(); | |||
16780 | Expr *RHS = ASE->getBase() == ASE->getRHS() ? Base.get() : ASE->getRHS(); | |||
16781 | SourceLocation LBracketLoc = ASE->getBeginLoc(); // FIXME: Not stored. | |||
16782 | return S.ActOnArraySubscriptExpr(nullptr, LHS, LBracketLoc, RHS, | |||
16783 | ASE->getRBracketLoc()); | |||
16784 | } | |||
16785 | ||||
16786 | case Expr::MemberExprClass: { | |||
16787 | auto *ME = cast<MemberExpr>(E); | |||
16788 | // -- If e is a class member access expression [...] naming a non-static | |||
16789 | // data member... | |||
16790 | if (isa<FieldDecl>(ME->getMemberDecl())) { | |||
16791 | ExprResult Base = Rebuild(ME->getBase()); | |||
16792 | if (!Base.isUsable()) | |||
16793 | return Base; | |||
16794 | return MemberExpr::Create( | |||
16795 | S.Context, Base.get(), ME->isArrow(), ME->getOperatorLoc(), | |||
16796 | ME->getQualifierLoc(), ME->getTemplateKeywordLoc(), | |||
16797 | ME->getMemberDecl(), ME->getFoundDecl(), ME->getMemberNameInfo(), | |||
16798 | CopiedTemplateArgs(ME), ME->getType(), ME->getValueKind(), | |||
16799 | ME->getObjectKind(), ME->isNonOdrUse()); | |||
16800 | } | |||
16801 | ||||
16802 | if (ME->getMemberDecl()->isCXXInstanceMember()) | |||
16803 | break; | |||
16804 | ||||
16805 | // -- If e is a class member access expression naming a static data member, | |||
16806 | // ... | |||
16807 | if (ME->isNonOdrUse() || IsPotentialResultOdrUsed(ME->getMemberDecl())) | |||
16808 | break; | |||
16809 | ||||
16810 | // Rebuild as a non-odr-use MemberExpr. | |||
16811 | MarkNotOdrUsed(); | |||
16812 | return MemberExpr::Create( | |||
16813 | S.Context, ME->getBase(), ME->isArrow(), ME->getOperatorLoc(), | |||
16814 | ME->getQualifierLoc(), ME->getTemplateKeywordLoc(), ME->getMemberDecl(), | |||
16815 | ME->getFoundDecl(), ME->getMemberNameInfo(), CopiedTemplateArgs(ME), | |||
16816 | ME->getType(), ME->getValueKind(), ME->getObjectKind(), NOUR); | |||
16817 | return ExprEmpty(); | |||
16818 | } | |||
16819 | ||||
16820 | case Expr::BinaryOperatorClass: { | |||
16821 | auto *BO = cast<BinaryOperator>(E); | |||
16822 | Expr *LHS = BO->getLHS(); | |||
16823 | Expr *RHS = BO->getRHS(); | |||
16824 | // -- If e is a pointer-to-member expression of the form e1 .* e2 ... | |||
16825 | if (BO->getOpcode() == BO_PtrMemD) { | |||
16826 | ExprResult Sub = Rebuild(LHS); | |||
16827 | if (!Sub.isUsable()) | |||
16828 | return Sub; | |||
16829 | LHS = Sub.get(); | |||
16830 | // -- If e is a comma expression, ... | |||
16831 | } else if (BO->getOpcode() == BO_Comma) { | |||
16832 | ExprResult Sub = Rebuild(RHS); | |||
16833 | if (!Sub.isUsable()) | |||
16834 | return Sub; | |||
16835 | RHS = Sub.get(); | |||
16836 | } else { | |||
16837 | break; | |||
16838 | } | |||
16839 | return S.BuildBinOp(nullptr, BO->getOperatorLoc(), BO->getOpcode(), | |||
16840 | LHS, RHS); | |||
16841 | } | |||
16842 | ||||
16843 | // -- If e has the form (e1)... | |||
16844 | case Expr::ParenExprClass: { | |||
16845 | auto *PE = cast<ParenExpr>(E); | |||
16846 | ExprResult Sub = Rebuild(PE->getSubExpr()); | |||
16847 | if (!Sub.isUsable()) | |||
16848 | return Sub; | |||
16849 | return S.ActOnParenExpr(PE->getLParen(), PE->getRParen(), Sub.get()); | |||
16850 | } | |||
16851 | ||||
16852 | // -- If e is a glvalue conditional expression, ... | |||
16853 | // We don't apply this to a binary conditional operator. FIXME: Should we? | |||
16854 | case Expr::ConditionalOperatorClass: { | |||
16855 | auto *CO = cast<ConditionalOperator>(E); | |||
16856 | ExprResult LHS = Rebuild(CO->getLHS()); | |||
16857 | if (LHS.isInvalid()) | |||
16858 | return ExprError(); | |||
16859 | ExprResult RHS = Rebuild(CO->getRHS()); | |||
16860 | if (RHS.isInvalid()) | |||
16861 | return ExprError(); | |||
16862 | if (!LHS.isUsable() && !RHS.isUsable()) | |||
16863 | return ExprEmpty(); | |||
16864 | if (!LHS.isUsable()) | |||
16865 | LHS = CO->getLHS(); | |||
16866 | if (!RHS.isUsable()) | |||
16867 | RHS = CO->getRHS(); | |||
16868 | return S.ActOnConditionalOp(CO->getQuestionLoc(), CO->getColonLoc(), | |||
16869 | CO->getCond(), LHS.get(), RHS.get()); | |||
16870 | } | |||
16871 | ||||
16872 | // [Clang extension] | |||
16873 | // -- If e has the form __extension__ e1... | |||
16874 | case Expr::UnaryOperatorClass: { | |||
16875 | auto *UO = cast<UnaryOperator>(E); | |||
16876 | if (UO->getOpcode() != UO_Extension) | |||
16877 | break; | |||
16878 | ExprResult Sub = Rebuild(UO->getSubExpr()); | |||
16879 | if (!Sub.isUsable()) | |||
16880 | return Sub; | |||
16881 | return S.BuildUnaryOp(nullptr, UO->getOperatorLoc(), UO_Extension, | |||
16882 | Sub.get()); | |||
16883 | } | |||
16884 | ||||
16885 | // [Clang extension] | |||
16886 | // -- If e has the form _Generic(...), the set of potential results is the | |||
16887 | // union of the sets of potential results of the associated expressions. | |||
16888 | case Expr::GenericSelectionExprClass: { | |||
16889 | auto *GSE = cast<GenericSelectionExpr>(E); | |||
16890 | ||||
16891 | SmallVector<Expr *, 4> AssocExprs; | |||
16892 | bool AnyChanged = false; | |||
16893 | for (Expr *OrigAssocExpr : GSE->getAssocExprs()) { | |||
16894 | ExprResult AssocExpr = Rebuild(OrigAssocExpr); | |||
16895 | if (AssocExpr.isInvalid()) | |||
16896 | return ExprError(); | |||
16897 | if (AssocExpr.isUsable()) { | |||
16898 | AssocExprs.push_back(AssocExpr.get()); | |||
16899 | AnyChanged = true; | |||
16900 | } else { | |||
16901 | AssocExprs.push_back(OrigAssocExpr); | |||
16902 | } | |||
16903 | } | |||
16904 | ||||
16905 | return AnyChanged ? S.CreateGenericSelectionExpr( | |||
16906 | GSE->getGenericLoc(), GSE->getDefaultLoc(), | |||
16907 | GSE->getRParenLoc(), GSE->getControllingExpr(), | |||
16908 | GSE->getAssocTypeSourceInfos(), AssocExprs) | |||
16909 | : ExprEmpty(); | |||
16910 | } | |||
16911 | ||||
16912 | // [Clang extension] | |||
16913 | // -- If e has the form __builtin_choose_expr(...), the set of potential | |||
16914 | // results is the union of the sets of potential results of the | |||
16915 | // second and third subexpressions. | |||
16916 | case Expr::ChooseExprClass: { | |||
16917 | auto *CE = cast<ChooseExpr>(E); | |||
16918 | ||||
16919 | ExprResult LHS = Rebuild(CE->getLHS()); | |||
16920 | if (LHS.isInvalid()) | |||
16921 | return ExprError(); | |||
16922 | ||||
16923 | ExprResult RHS = Rebuild(CE->getLHS()); | |||
16924 | if (RHS.isInvalid()) | |||
16925 | return ExprError(); | |||
16926 | ||||
16927 | if (!LHS.get() && !RHS.get()) | |||
16928 | return ExprEmpty(); | |||
16929 | if (!LHS.isUsable()) | |||
16930 | LHS = CE->getLHS(); | |||
16931 | if (!RHS.isUsable()) | |||
16932 | RHS = CE->getRHS(); | |||
16933 | ||||
16934 | return S.ActOnChooseExpr(CE->getBuiltinLoc(), CE->getCond(), LHS.get(), | |||
16935 | RHS.get(), CE->getRParenLoc()); | |||
16936 | } | |||
16937 | ||||
16938 | // Step through non-syntactic nodes. | |||
16939 | case Expr::ConstantExprClass: { | |||
16940 | auto *CE = cast<ConstantExpr>(E); | |||
16941 | ExprResult Sub = Rebuild(CE->getSubExpr()); | |||
16942 | if (!Sub.isUsable()) | |||
16943 | return Sub; | |||
16944 | return ConstantExpr::Create(S.Context, Sub.get()); | |||
16945 | } | |||
16946 | ||||
16947 | // We could mostly rely on the recursive rebuilding to rebuild implicit | |||
16948 | // casts, but not at the top level, so rebuild them here. | |||
16949 | case Expr::ImplicitCastExprClass: { | |||
16950 | auto *ICE = cast<ImplicitCastExpr>(E); | |||
16951 | // Only step through the narrow set of cast kinds we expect to encounter. | |||
16952 | // Anything else suggests we've left the region in which potential results | |||
16953 | // can be found. | |||
16954 | switch (ICE->getCastKind()) { | |||
16955 | case CK_NoOp: | |||
16956 | case CK_DerivedToBase: | |||
16957 | case CK_UncheckedDerivedToBase: { | |||
16958 | ExprResult Sub = Rebuild(ICE->getSubExpr()); | |||
16959 | if (!Sub.isUsable()) | |||
16960 | return Sub; | |||
16961 | CXXCastPath Path(ICE->path()); | |||
16962 | return S.ImpCastExprToType(Sub.get(), ICE->getType(), ICE->getCastKind(), | |||
16963 | ICE->getValueKind(), &Path); | |||
16964 | } | |||
16965 | ||||
16966 | default: | |||
16967 | break; | |||
16968 | } | |||
16969 | break; | |||
16970 | } | |||
16971 | ||||
16972 | default: | |||
16973 | break; | |||
16974 | } | |||
16975 | ||||
16976 | // Can't traverse through this node. Nothing to do. | |||
16977 | return ExprEmpty(); | |||
16978 | } | |||
16979 | ||||
16980 | ExprResult Sema::CheckLValueToRValueConversionOperand(Expr *E) { | |||
16981 | // Check whether the operand is or contains an object of non-trivial C union | |||
16982 | // type. | |||
16983 | if (E->getType().isVolatileQualified() && | |||
16984 | (E->getType().hasNonTrivialToPrimitiveDestructCUnion() || | |||
16985 | E->getType().hasNonTrivialToPrimitiveCopyCUnion())) | |||
16986 | checkNonTrivialCUnion(E->getType(), E->getExprLoc(), | |||
16987 | Sema::NTCUC_LValueToRValueVolatile, | |||
16988 | NTCUK_Destruct|NTCUK_Copy); | |||
16989 | ||||
16990 | // C++2a [basic.def.odr]p4: | |||
16991 | // [...] an expression of non-volatile-qualified non-class type to which | |||
16992 | // the lvalue-to-rvalue conversion is applied [...] | |||
16993 | if (E->getType().isVolatileQualified() || E->getType()->getAs<RecordType>()) | |||
16994 | return E; | |||
16995 | ||||
16996 | ExprResult Result = | |||
16997 | rebuildPotentialResultsAsNonOdrUsed(*this, E, NOUR_Constant); | |||
16998 | if (Result.isInvalid()) | |||
16999 | return ExprError(); | |||
17000 | return Result.get() ? Result : E; | |||
17001 | } | |||
17002 | ||||
17003 | ExprResult Sema::ActOnConstantExpression(ExprResult Res) { | |||
17004 | Res = CorrectDelayedTyposInExpr(Res); | |||
17005 | ||||
17006 | if (!Res.isUsable()) | |||
17007 | return Res; | |||
17008 | ||||
17009 | // If a constant-expression is a reference to a variable where we delay | |||
17010 | // deciding whether it is an odr-use, just assume we will apply the | |||
17011 | // lvalue-to-rvalue conversion. In the one case where this doesn't happen | |||
17012 | // (a non-type template argument), we have special handling anyway. | |||
17013 | return CheckLValueToRValueConversionOperand(Res.get()); | |||
17014 | } | |||
17015 | ||||
17016 | void Sema::CleanupVarDeclMarking() { | |||
17017 | // Iterate through a local copy in case MarkVarDeclODRUsed makes a recursive | |||
17018 | // call. | |||
17019 | MaybeODRUseExprSet LocalMaybeODRUseExprs; | |||
17020 | std::swap(LocalMaybeODRUseExprs, MaybeODRUseExprs); | |||
17021 | ||||
17022 | for (Expr *E : LocalMaybeODRUseExprs) { | |||
17023 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) { | |||
17024 | MarkVarDeclODRUsed(cast<VarDecl>(DRE->getDecl()), | |||
17025 | DRE->getLocation(), *this); | |||
17026 | } else if (auto *ME = dyn_cast<MemberExpr>(E)) { | |||
17027 | MarkVarDeclODRUsed(cast<VarDecl>(ME->getMemberDecl()), ME->getMemberLoc(), | |||
17028 | *this); | |||
17029 | } else if (auto *FP = dyn_cast<FunctionParmPackExpr>(E)) { | |||
17030 | for (VarDecl *VD : *FP) | |||
17031 | MarkVarDeclODRUsed(VD, FP->getParameterPackLocation(), *this); | |||
17032 | } else { | |||
17033 | llvm_unreachable("Unexpected expression")::llvm::llvm_unreachable_internal("Unexpected expression", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17033); | |||
17034 | } | |||
17035 | } | |||
17036 | ||||
17037 | assert(MaybeODRUseExprs.empty() &&((MaybeODRUseExprs.empty() && "MarkVarDeclODRUsed failed to cleanup MaybeODRUseExprs?" ) ? static_cast<void> (0) : __assert_fail ("MaybeODRUseExprs.empty() && \"MarkVarDeclODRUsed failed to cleanup MaybeODRUseExprs?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17038, __PRETTY_FUNCTION__)) | |||
17038 | "MarkVarDeclODRUsed failed to cleanup MaybeODRUseExprs?")((MaybeODRUseExprs.empty() && "MarkVarDeclODRUsed failed to cleanup MaybeODRUseExprs?" ) ? static_cast<void> (0) : __assert_fail ("MaybeODRUseExprs.empty() && \"MarkVarDeclODRUsed failed to cleanup MaybeODRUseExprs?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17038, __PRETTY_FUNCTION__)); | |||
17039 | } | |||
17040 | ||||
17041 | static void DoMarkVarDeclReferenced(Sema &SemaRef, SourceLocation Loc, | |||
17042 | VarDecl *Var, Expr *E) { | |||
17043 | assert((!E || isa<DeclRefExpr>(E) || isa<MemberExpr>(E) ||(((!E || isa<DeclRefExpr>(E) || isa<MemberExpr>(E ) || isa<FunctionParmPackExpr>(E)) && "Invalid Expr argument to DoMarkVarDeclReferenced" ) ? static_cast<void> (0) : __assert_fail ("(!E || isa<DeclRefExpr>(E) || isa<MemberExpr>(E) || isa<FunctionParmPackExpr>(E)) && \"Invalid Expr argument to DoMarkVarDeclReferenced\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17045, __PRETTY_FUNCTION__)) | |||
17044 | isa<FunctionParmPackExpr>(E)) &&(((!E || isa<DeclRefExpr>(E) || isa<MemberExpr>(E ) || isa<FunctionParmPackExpr>(E)) && "Invalid Expr argument to DoMarkVarDeclReferenced" ) ? static_cast<void> (0) : __assert_fail ("(!E || isa<DeclRefExpr>(E) || isa<MemberExpr>(E) || isa<FunctionParmPackExpr>(E)) && \"Invalid Expr argument to DoMarkVarDeclReferenced\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17045, __PRETTY_FUNCTION__)) | |||
17045 | "Invalid Expr argument to DoMarkVarDeclReferenced")(((!E || isa<DeclRefExpr>(E) || isa<MemberExpr>(E ) || isa<FunctionParmPackExpr>(E)) && "Invalid Expr argument to DoMarkVarDeclReferenced" ) ? static_cast<void> (0) : __assert_fail ("(!E || isa<DeclRefExpr>(E) || isa<MemberExpr>(E) || isa<FunctionParmPackExpr>(E)) && \"Invalid Expr argument to DoMarkVarDeclReferenced\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17045, __PRETTY_FUNCTION__)); | |||
17046 | Var->setReferenced(); | |||
17047 | ||||
17048 | if (Var->isInvalidDecl()) | |||
17049 | return; | |||
17050 | ||||
17051 | auto *MSI = Var->getMemberSpecializationInfo(); | |||
17052 | TemplateSpecializationKind TSK = MSI ? MSI->getTemplateSpecializationKind() | |||
17053 | : Var->getTemplateSpecializationKind(); | |||
17054 | ||||
17055 | OdrUseContext OdrUse = isOdrUseContext(SemaRef); | |||
17056 | bool UsableInConstantExpr = | |||
17057 | Var->mightBeUsableInConstantExpressions(SemaRef.Context); | |||
17058 | ||||
17059 | // C++20 [expr.const]p12: | |||
17060 | // A variable [...] is needed for constant evaluation if it is [...] a | |||
17061 | // variable whose name appears as a potentially constant evaluated | |||
17062 | // expression that is either a contexpr variable or is of non-volatile | |||
17063 | // const-qualified integral type or of reference type | |||
17064 | bool NeededForConstantEvaluation = | |||
17065 | isPotentiallyConstantEvaluatedContext(SemaRef) && UsableInConstantExpr; | |||
17066 | ||||
17067 | bool NeedDefinition = | |||
17068 | OdrUse == OdrUseContext::Used || NeededForConstantEvaluation; | |||
17069 | ||||
17070 | VarTemplateSpecializationDecl *VarSpec = | |||
17071 | dyn_cast<VarTemplateSpecializationDecl>(Var); | |||
17072 | assert(!isa<VarTemplatePartialSpecializationDecl>(Var) &&((!isa<VarTemplatePartialSpecializationDecl>(Var) && "Can't instantiate a partial template specialization.") ? static_cast <void> (0) : __assert_fail ("!isa<VarTemplatePartialSpecializationDecl>(Var) && \"Can't instantiate a partial template specialization.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17073, __PRETTY_FUNCTION__)) | |||
17073 | "Can't instantiate a partial template specialization.")((!isa<VarTemplatePartialSpecializationDecl>(Var) && "Can't instantiate a partial template specialization.") ? static_cast <void> (0) : __assert_fail ("!isa<VarTemplatePartialSpecializationDecl>(Var) && \"Can't instantiate a partial template specialization.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17073, __PRETTY_FUNCTION__)); | |||
17074 | ||||
17075 | // If this might be a member specialization of a static data member, check | |||
17076 | // the specialization is visible. We already did the checks for variable | |||
17077 | // template specializations when we created them. | |||
17078 | if (NeedDefinition && TSK != TSK_Undeclared && | |||
17079 | !isa<VarTemplateSpecializationDecl>(Var)) | |||
17080 | SemaRef.checkSpecializationVisibility(Loc, Var); | |||
17081 | ||||
17082 | // Perform implicit instantiation of static data members, static data member | |||
17083 | // templates of class templates, and variable template specializations. Delay | |||
17084 | // instantiations of variable templates, except for those that could be used | |||
17085 | // in a constant expression. | |||
17086 | if (NeedDefinition && isTemplateInstantiation(TSK)) { | |||
17087 | // Per C++17 [temp.explicit]p10, we may instantiate despite an explicit | |||
17088 | // instantiation declaration if a variable is usable in a constant | |||
17089 | // expression (among other cases). | |||
17090 | bool TryInstantiating = | |||
17091 | TSK == TSK_ImplicitInstantiation || | |||
17092 | (TSK == TSK_ExplicitInstantiationDeclaration && UsableInConstantExpr); | |||
17093 | ||||
17094 | if (TryInstantiating) { | |||
17095 | SourceLocation PointOfInstantiation = | |||
17096 | MSI ? MSI->getPointOfInstantiation() : Var->getPointOfInstantiation(); | |||
17097 | bool FirstInstantiation = PointOfInstantiation.isInvalid(); | |||
17098 | if (FirstInstantiation) { | |||
17099 | PointOfInstantiation = Loc; | |||
17100 | if (MSI) | |||
17101 | MSI->setPointOfInstantiation(PointOfInstantiation); | |||
17102 | else | |||
17103 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); | |||
17104 | } | |||
17105 | ||||
17106 | bool InstantiationDependent = false; | |||
17107 | bool IsNonDependent = | |||
17108 | VarSpec ? !TemplateSpecializationType::anyDependentTemplateArguments( | |||
17109 | VarSpec->getTemplateArgsInfo(), InstantiationDependent) | |||
17110 | : true; | |||
17111 | ||||
17112 | // Do not instantiate specializations that are still type-dependent. | |||
17113 | if (IsNonDependent) { | |||
17114 | if (UsableInConstantExpr) { | |||
17115 | // Do not defer instantiations of variables that could be used in a | |||
17116 | // constant expression. | |||
17117 | SemaRef.runWithSufficientStackSpace(PointOfInstantiation, [&] { | |||
17118 | SemaRef.InstantiateVariableDefinition(PointOfInstantiation, Var); | |||
17119 | }); | |||
17120 | } else if (FirstInstantiation || | |||
17121 | isa<VarTemplateSpecializationDecl>(Var)) { | |||
17122 | // FIXME: For a specialization of a variable template, we don't | |||
17123 | // distinguish between "declaration and type implicitly instantiated" | |||
17124 | // and "implicit instantiation of definition requested", so we have | |||
17125 | // no direct way to avoid enqueueing the pending instantiation | |||
17126 | // multiple times. | |||
17127 | SemaRef.PendingInstantiations | |||
17128 | .push_back(std::make_pair(Var, PointOfInstantiation)); | |||
17129 | } | |||
17130 | } | |||
17131 | } | |||
17132 | } | |||
17133 | ||||
17134 | // C++2a [basic.def.odr]p4: | |||
17135 | // A variable x whose name appears as a potentially-evaluated expression e | |||
17136 | // is odr-used by e unless | |||
17137 | // -- x is a reference that is usable in constant expressions | |||
17138 | // -- x is a variable of non-reference type that is usable in constant | |||
17139 | // expressions and has no mutable subobjects [FIXME], and e is an | |||
17140 | // element of the set of potential results of an expression of | |||
17141 | // non-volatile-qualified non-class type to which the lvalue-to-rvalue | |||
17142 | // conversion is applied | |||
17143 | // -- x is a variable of non-reference type, and e is an element of the set | |||
17144 | // of potential results of a discarded-value expression to which the | |||
17145 | // lvalue-to-rvalue conversion is not applied [FIXME] | |||
17146 | // | |||
17147 | // We check the first part of the second bullet here, and | |||
17148 | // Sema::CheckLValueToRValueConversionOperand deals with the second part. | |||
17149 | // FIXME: To get the third bullet right, we need to delay this even for | |||
17150 | // variables that are not usable in constant expressions. | |||
17151 | ||||
17152 | // If we already know this isn't an odr-use, there's nothing more to do. | |||
17153 | if (DeclRefExpr *DRE = dyn_cast_or_null<DeclRefExpr>(E)) | |||
17154 | if (DRE->isNonOdrUse()) | |||
17155 | return; | |||
17156 | if (MemberExpr *ME = dyn_cast_or_null<MemberExpr>(E)) | |||
17157 | if (ME->isNonOdrUse()) | |||
17158 | return; | |||
17159 | ||||
17160 | switch (OdrUse) { | |||
17161 | case OdrUseContext::None: | |||
17162 | assert((!E || isa<FunctionParmPackExpr>(E)) &&(((!E || isa<FunctionParmPackExpr>(E)) && "missing non-odr-use marking for unevaluated decl ref" ) ? static_cast<void> (0) : __assert_fail ("(!E || isa<FunctionParmPackExpr>(E)) && \"missing non-odr-use marking for unevaluated decl ref\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17163, __PRETTY_FUNCTION__)) | |||
17163 | "missing non-odr-use marking for unevaluated decl ref")(((!E || isa<FunctionParmPackExpr>(E)) && "missing non-odr-use marking for unevaluated decl ref" ) ? static_cast<void> (0) : __assert_fail ("(!E || isa<FunctionParmPackExpr>(E)) && \"missing non-odr-use marking for unevaluated decl ref\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17163, __PRETTY_FUNCTION__)); | |||
17164 | break; | |||
17165 | ||||
17166 | case OdrUseContext::FormallyOdrUsed: | |||
17167 | // FIXME: Ignoring formal odr-uses results in incorrect lambda capture | |||
17168 | // behavior. | |||
17169 | break; | |||
17170 | ||||
17171 | case OdrUseContext::Used: | |||
17172 | // If we might later find that this expression isn't actually an odr-use, | |||
17173 | // delay the marking. | |||
17174 | if (E && Var->isUsableInConstantExpressions(SemaRef.Context)) | |||
17175 | SemaRef.MaybeODRUseExprs.insert(E); | |||
17176 | else | |||
17177 | MarkVarDeclODRUsed(Var, Loc, SemaRef); | |||
17178 | break; | |||
17179 | ||||
17180 | case OdrUseContext::Dependent: | |||
17181 | // If this is a dependent context, we don't need to mark variables as | |||
17182 | // odr-used, but we may still need to track them for lambda capture. | |||
17183 | // FIXME: Do we also need to do this inside dependent typeid expressions | |||
17184 | // (which are modeled as unevaluated at this point)? | |||
17185 | const bool RefersToEnclosingScope = | |||
17186 | (SemaRef.CurContext != Var->getDeclContext() && | |||
17187 | Var->getDeclContext()->isFunctionOrMethod() && Var->hasLocalStorage()); | |||
17188 | if (RefersToEnclosingScope) { | |||
17189 | LambdaScopeInfo *const LSI = | |||
17190 | SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true); | |||
17191 | if (LSI && (!LSI->CallOperator || | |||
17192 | !LSI->CallOperator->Encloses(Var->getDeclContext()))) { | |||
17193 | // If a variable could potentially be odr-used, defer marking it so | |||
17194 | // until we finish analyzing the full expression for any | |||
17195 | // lvalue-to-rvalue | |||
17196 | // or discarded value conversions that would obviate odr-use. | |||
17197 | // Add it to the list of potential captures that will be analyzed | |||
17198 | // later (ActOnFinishFullExpr) for eventual capture and odr-use marking | |||
17199 | // unless the variable is a reference that was initialized by a constant | |||
17200 | // expression (this will never need to be captured or odr-used). | |||
17201 | // | |||
17202 | // FIXME: We can simplify this a lot after implementing P0588R1. | |||
17203 | assert(E && "Capture variable should be used in an expression.")((E && "Capture variable should be used in an expression." ) ? static_cast<void> (0) : __assert_fail ("E && \"Capture variable should be used in an expression.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17203, __PRETTY_FUNCTION__)); | |||
17204 | if (!Var->getType()->isReferenceType() || | |||
17205 | !Var->isUsableInConstantExpressions(SemaRef.Context)) | |||
17206 | LSI->addPotentialCapture(E->IgnoreParens()); | |||
17207 | } | |||
17208 | } | |||
17209 | break; | |||
17210 | } | |||
17211 | } | |||
17212 | ||||
17213 | /// Mark a variable referenced, and check whether it is odr-used | |||
17214 | /// (C++ [basic.def.odr]p2, C99 6.9p3). Note that this should not be | |||
17215 | /// used directly for normal expressions referring to VarDecl. | |||
17216 | void Sema::MarkVariableReferenced(SourceLocation Loc, VarDecl *Var) { | |||
17217 | DoMarkVarDeclReferenced(*this, Loc, Var, nullptr); | |||
17218 | } | |||
17219 | ||||
17220 | static void MarkExprReferenced(Sema &SemaRef, SourceLocation Loc, | |||
17221 | Decl *D, Expr *E, bool MightBeOdrUse) { | |||
17222 | if (SemaRef.isInOpenMPDeclareTargetContext()) | |||
17223 | SemaRef.checkDeclIsAllowedInOpenMPTarget(E, D); | |||
17224 | ||||
17225 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { | |||
17226 | DoMarkVarDeclReferenced(SemaRef, Loc, Var, E); | |||
17227 | return; | |||
17228 | } | |||
17229 | ||||
17230 | SemaRef.MarkAnyDeclReferenced(Loc, D, MightBeOdrUse); | |||
17231 | ||||
17232 | // If this is a call to a method via a cast, also mark the method in the | |||
17233 | // derived class used in case codegen can devirtualize the call. | |||
17234 | const MemberExpr *ME = dyn_cast<MemberExpr>(E); | |||
17235 | if (!ME) | |||
17236 | return; | |||
17237 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ME->getMemberDecl()); | |||
17238 | if (!MD) | |||
17239 | return; | |||
17240 | // Only attempt to devirtualize if this is truly a virtual call. | |||
17241 | bool IsVirtualCall = MD->isVirtual() && | |||
17242 | ME->performsVirtualDispatch(SemaRef.getLangOpts()); | |||
17243 | if (!IsVirtualCall) | |||
17244 | return; | |||
17245 | ||||
17246 | // If it's possible to devirtualize the call, mark the called function | |||
17247 | // referenced. | |||
17248 | CXXMethodDecl *DM = MD->getDevirtualizedMethod( | |||
17249 | ME->getBase(), SemaRef.getLangOpts().AppleKext); | |||
17250 | if (DM) | |||
17251 | SemaRef.MarkAnyDeclReferenced(Loc, DM, MightBeOdrUse); | |||
17252 | } | |||
17253 | ||||
17254 | /// Perform reference-marking and odr-use handling for a DeclRefExpr. | |||
17255 | void Sema::MarkDeclRefReferenced(DeclRefExpr *E, const Expr *Base) { | |||
17256 | // TODO: update this with DR# once a defect report is filed. | |||
17257 | // C++11 defect. The address of a pure member should not be an ODR use, even | |||
17258 | // if it's a qualified reference. | |||
17259 | bool OdrUse = true; | |||
17260 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(E->getDecl())) | |||
17261 | if (Method->isVirtual() && | |||
17262 | !Method->getDevirtualizedMethod(Base, getLangOpts().AppleKext)) | |||
17263 | OdrUse = false; | |||
17264 | ||||
17265 | if (auto *FD = dyn_cast<FunctionDecl>(E->getDecl())) | |||
17266 | if (!isConstantEvaluated() && FD->isConsteval() && | |||
17267 | !RebuildingImmediateInvocation) | |||
17268 | ExprEvalContexts.back().ReferenceToConsteval.insert(E); | |||
17269 | MarkExprReferenced(*this, E->getLocation(), E->getDecl(), E, OdrUse); | |||
17270 | } | |||
17271 | ||||
17272 | /// Perform reference-marking and odr-use handling for a MemberExpr. | |||
17273 | void Sema::MarkMemberReferenced(MemberExpr *E) { | |||
17274 | // C++11 [basic.def.odr]p2: | |||
17275 | // A non-overloaded function whose name appears as a potentially-evaluated | |||
17276 | // expression or a member of a set of candidate functions, if selected by | |||
17277 | // overload resolution when referred to from a potentially-evaluated | |||
17278 | // expression, is odr-used, unless it is a pure virtual function and its | |||
17279 | // name is not explicitly qualified. | |||
17280 | bool MightBeOdrUse = true; | |||
17281 | if (E->performsVirtualDispatch(getLangOpts())) { | |||
17282 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) | |||
17283 | if (Method->isPure()) | |||
17284 | MightBeOdrUse = false; | |||
17285 | } | |||
17286 | SourceLocation Loc = | |||
17287 | E->getMemberLoc().isValid() ? E->getMemberLoc() : E->getBeginLoc(); | |||
17288 | MarkExprReferenced(*this, Loc, E->getMemberDecl(), E, MightBeOdrUse); | |||
17289 | } | |||
17290 | ||||
17291 | /// Perform reference-marking and odr-use handling for a FunctionParmPackExpr. | |||
17292 | void Sema::MarkFunctionParmPackReferenced(FunctionParmPackExpr *E) { | |||
17293 | for (VarDecl *VD : *E) | |||
17294 | MarkExprReferenced(*this, E->getParameterPackLocation(), VD, E, true); | |||
17295 | } | |||
17296 | ||||
17297 | /// Perform marking for a reference to an arbitrary declaration. It | |||
17298 | /// marks the declaration referenced, and performs odr-use checking for | |||
17299 | /// functions and variables. This method should not be used when building a | |||
17300 | /// normal expression which refers to a variable. | |||
17301 | void Sema::MarkAnyDeclReferenced(SourceLocation Loc, Decl *D, | |||
17302 | bool MightBeOdrUse) { | |||
17303 | if (MightBeOdrUse) { | |||
17304 | if (auto *VD = dyn_cast<VarDecl>(D)) { | |||
17305 | MarkVariableReferenced(Loc, VD); | |||
17306 | return; | |||
17307 | } | |||
17308 | } | |||
17309 | if (auto *FD = dyn_cast<FunctionDecl>(D)) { | |||
17310 | MarkFunctionReferenced(Loc, FD, MightBeOdrUse); | |||
17311 | return; | |||
17312 | } | |||
17313 | D->setReferenced(); | |||
17314 | } | |||
17315 | ||||
17316 | namespace { | |||
17317 | // Mark all of the declarations used by a type as referenced. | |||
17318 | // FIXME: Not fully implemented yet! We need to have a better understanding | |||
17319 | // of when we're entering a context we should not recurse into. | |||
17320 | // FIXME: This is and EvaluatedExprMarker are more-or-less equivalent to | |||
17321 | // TreeTransforms rebuilding the type in a new context. Rather than | |||
17322 | // duplicating the TreeTransform logic, we should consider reusing it here. | |||
17323 | // Currently that causes problems when rebuilding LambdaExprs. | |||
17324 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { | |||
17325 | Sema &S; | |||
17326 | SourceLocation Loc; | |||
17327 | ||||
17328 | public: | |||
17329 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; | |||
17330 | ||||
17331 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } | |||
17332 | ||||
17333 | bool TraverseTemplateArgument(const TemplateArgument &Arg); | |||
17334 | }; | |||
17335 | } | |||
17336 | ||||
17337 | bool MarkReferencedDecls::TraverseTemplateArgument( | |||
17338 | const TemplateArgument &Arg) { | |||
17339 | { | |||
17340 | // A non-type template argument is a constant-evaluated context. | |||
17341 | EnterExpressionEvaluationContext Evaluated( | |||
17342 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); | |||
17343 | if (Arg.getKind() == TemplateArgument::Declaration) { | |||
17344 | if (Decl *D = Arg.getAsDecl()) | |||
17345 | S.MarkAnyDeclReferenced(Loc, D, true); | |||
17346 | } else if (Arg.getKind() == TemplateArgument::Expression) { | |||
17347 | S.MarkDeclarationsReferencedInExpr(Arg.getAsExpr(), false); | |||
17348 | } | |||
17349 | } | |||
17350 | ||||
17351 | return Inherited::TraverseTemplateArgument(Arg); | |||
17352 | } | |||
17353 | ||||
17354 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { | |||
17355 | MarkReferencedDecls Marker(*this, Loc); | |||
17356 | Marker.TraverseType(T); | |||
17357 | } | |||
17358 | ||||
17359 | namespace { | |||
17360 | /// Helper class that marks all of the declarations referenced by | |||
17361 | /// potentially-evaluated subexpressions as "referenced". | |||
17362 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { | |||
17363 | Sema &S; | |||
17364 | bool SkipLocalVariables; | |||
17365 | ||||
17366 | public: | |||
17367 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; | |||
17368 | ||||
17369 | EvaluatedExprMarker(Sema &S, bool SkipLocalVariables) | |||
17370 | : Inherited(S.Context), S(S), SkipLocalVariables(SkipLocalVariables) { } | |||
17371 | ||||
17372 | void VisitDeclRefExpr(DeclRefExpr *E) { | |||
17373 | // If we were asked not to visit local variables, don't. | |||
17374 | if (SkipLocalVariables) { | |||
17375 | if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) | |||
17376 | if (VD->hasLocalStorage()) | |||
17377 | return; | |||
17378 | } | |||
17379 | ||||
17380 | S.MarkDeclRefReferenced(E); | |||
17381 | } | |||
17382 | ||||
17383 | void VisitMemberExpr(MemberExpr *E) { | |||
17384 | S.MarkMemberReferenced(E); | |||
17385 | Inherited::VisitMemberExpr(E); | |||
17386 | } | |||
17387 | ||||
17388 | void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { | |||
17389 | S.MarkFunctionReferenced( | |||
17390 | E->getBeginLoc(), | |||
17391 | const_cast<CXXDestructorDecl *>(E->getTemporary()->getDestructor())); | |||
17392 | Visit(E->getSubExpr()); | |||
17393 | } | |||
17394 | ||||
17395 | void VisitCXXNewExpr(CXXNewExpr *E) { | |||
17396 | if (E->getOperatorNew()) | |||
17397 | S.MarkFunctionReferenced(E->getBeginLoc(), E->getOperatorNew()); | |||
17398 | if (E->getOperatorDelete()) | |||
17399 | S.MarkFunctionReferenced(E->getBeginLoc(), E->getOperatorDelete()); | |||
17400 | Inherited::VisitCXXNewExpr(E); | |||
17401 | } | |||
17402 | ||||
17403 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { | |||
17404 | if (E->getOperatorDelete()) | |||
17405 | S.MarkFunctionReferenced(E->getBeginLoc(), E->getOperatorDelete()); | |||
17406 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); | |||
17407 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { | |||
17408 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); | |||
17409 | S.MarkFunctionReferenced(E->getBeginLoc(), S.LookupDestructor(Record)); | |||
17410 | } | |||
17411 | ||||
17412 | Inherited::VisitCXXDeleteExpr(E); | |||
17413 | } | |||
17414 | ||||
17415 | void VisitCXXConstructExpr(CXXConstructExpr *E) { | |||
17416 | S.MarkFunctionReferenced(E->getBeginLoc(), E->getConstructor()); | |||
17417 | Inherited::VisitCXXConstructExpr(E); | |||
17418 | } | |||
17419 | ||||
17420 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { | |||
17421 | Visit(E->getExpr()); | |||
17422 | } | |||
17423 | }; | |||
17424 | } | |||
17425 | ||||
17426 | /// Mark any declarations that appear within this expression or any | |||
17427 | /// potentially-evaluated subexpressions as "referenced". | |||
17428 | /// | |||
17429 | /// \param SkipLocalVariables If true, don't mark local variables as | |||
17430 | /// 'referenced'. | |||
17431 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E, | |||
17432 | bool SkipLocalVariables) { | |||
17433 | EvaluatedExprMarker(*this, SkipLocalVariables).Visit(E); | |||
17434 | } | |||
17435 | ||||
17436 | /// Emit a diagnostic that describes an effect on the run-time behavior | |||
17437 | /// of the program being compiled. | |||
17438 | /// | |||
17439 | /// This routine emits the given diagnostic when the code currently being | |||
17440 | /// type-checked is "potentially evaluated", meaning that there is a | |||
17441 | /// possibility that the code will actually be executable. Code in sizeof() | |||
17442 | /// expressions, code used only during overload resolution, etc., are not | |||
17443 | /// potentially evaluated. This routine will suppress such diagnostics or, | |||
17444 | /// in the absolutely nutty case of potentially potentially evaluated | |||
17445 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it | |||
17446 | /// later. | |||
17447 | /// | |||
17448 | /// This routine should be used for all diagnostics that describe the run-time | |||
17449 | /// behavior of a program, such as passing a non-POD value through an ellipsis. | |||
17450 | /// Failure to do so will likely result in spurious diagnostics or failures | |||
17451 | /// during overload resolution or within sizeof/alignof/typeof/typeid. | |||
17452 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, ArrayRef<const Stmt*> Stmts, | |||
17453 | const PartialDiagnostic &PD) { | |||
17454 | switch (ExprEvalContexts.back().Context) { | |||
17455 | case ExpressionEvaluationContext::Unevaluated: | |||
17456 | case ExpressionEvaluationContext::UnevaluatedList: | |||
17457 | case ExpressionEvaluationContext::UnevaluatedAbstract: | |||
17458 | case ExpressionEvaluationContext::DiscardedStatement: | |||
17459 | // The argument will never be evaluated, so don't complain. | |||
17460 | break; | |||
17461 | ||||
17462 | case ExpressionEvaluationContext::ConstantEvaluated: | |||
17463 | // Relevant diagnostics should be produced by constant evaluation. | |||
17464 | break; | |||
17465 | ||||
17466 | case ExpressionEvaluationContext::PotentiallyEvaluated: | |||
17467 | case ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed: | |||
17468 | if (!Stmts.empty() && getCurFunctionOrMethodDecl()) { | |||
17469 | FunctionScopes.back()->PossiblyUnreachableDiags. | |||
17470 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, Stmts)); | |||
17471 | return true; | |||
17472 | } | |||
17473 | ||||
17474 | // The initializer of a constexpr variable or of the first declaration of a | |||
17475 | // static data member is not syntactically a constant evaluated constant, | |||
17476 | // but nonetheless is always required to be a constant expression, so we | |||
17477 | // can skip diagnosing. | |||
17478 | // FIXME: Using the mangling context here is a hack. | |||
17479 | if (auto *VD = dyn_cast_or_null<VarDecl>( | |||
17480 | ExprEvalContexts.back().ManglingContextDecl)) { | |||
17481 | if (VD->isConstexpr() || | |||
17482 | (VD->isStaticDataMember() && VD->isFirstDecl() && !VD->isInline())) | |||
17483 | break; | |||
17484 | // FIXME: For any other kind of variable, we should build a CFG for its | |||
17485 | // initializer and check whether the context in question is reachable. | |||
17486 | } | |||
17487 | ||||
17488 | Diag(Loc, PD); | |||
17489 | return true; | |||
17490 | } | |||
17491 | ||||
17492 | return false; | |||
17493 | } | |||
17494 | ||||
17495 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *Statement, | |||
17496 | const PartialDiagnostic &PD) { | |||
17497 | return DiagRuntimeBehavior( | |||
17498 | Loc, Statement ? llvm::makeArrayRef(Statement) : llvm::None, PD); | |||
17499 | } | |||
17500 | ||||
17501 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, | |||
17502 | CallExpr *CE, FunctionDecl *FD) { | |||
17503 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) | |||
17504 | return false; | |||
17505 | ||||
17506 | // If we're inside a decltype's expression, don't check for a valid return | |||
17507 | // type or construct temporaries until we know whether this is the last call. | |||
17508 | if (ExprEvalContexts.back().ExprContext == | |||
17509 | ExpressionEvaluationContextRecord::EK_Decltype) { | |||
17510 | ExprEvalContexts.back().DelayedDecltypeCalls.push_back(CE); | |||
17511 | return false; | |||
17512 | } | |||
17513 | ||||
17514 | class CallReturnIncompleteDiagnoser : public TypeDiagnoser { | |||
17515 | FunctionDecl *FD; | |||
17516 | CallExpr *CE; | |||
17517 | ||||
17518 | public: | |||
17519 | CallReturnIncompleteDiagnoser(FunctionDecl *FD, CallExpr *CE) | |||
17520 | : FD(FD), CE(CE) { } | |||
17521 | ||||
17522 | void diagnose(Sema &S, SourceLocation Loc, QualType T) override { | |||
17523 | if (!FD) { | |||
17524 | S.Diag(Loc, diag::err_call_incomplete_return) | |||
17525 | << T << CE->getSourceRange(); | |||
17526 | return; | |||
17527 | } | |||
17528 | ||||
17529 | S.Diag(Loc, diag::err_call_function_incomplete_return) | |||
17530 | << CE->getSourceRange() << FD->getDeclName() << T; | |||
17531 | S.Diag(FD->getLocation(), diag::note_entity_declared_at) | |||
17532 | << FD->getDeclName(); | |||
17533 | } | |||
17534 | } Diagnoser(FD, CE); | |||
17535 | ||||
17536 | if (RequireCompleteType(Loc, ReturnType, Diagnoser)) | |||
17537 | return true; | |||
17538 | ||||
17539 | return false; | |||
17540 | } | |||
17541 | ||||
17542 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses | |||
17543 | // will prevent this condition from triggering, which is what we want. | |||
17544 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { | |||
17545 | SourceLocation Loc; | |||
17546 | ||||
17547 | unsigned diagnostic = diag::warn_condition_is_assignment; | |||
17548 | bool IsOrAssign = false; | |||
17549 | ||||
17550 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) { | |||
17551 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) | |||
17552 | return; | |||
17553 | ||||
17554 | IsOrAssign = Op->getOpcode() == BO_OrAssign; | |||
17555 | ||||
17556 | // Greylist some idioms by putting them into a warning subcategory. | |||
17557 | if (ObjCMessageExpr *ME | |||
17558 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { | |||
17559 | Selector Sel = ME->getSelector(); | |||
17560 | ||||
17561 | // self = [<foo> init...] | |||
17562 | if (isSelfExpr(Op->getLHS()) && ME->getMethodFamily() == OMF_init) | |||
17563 | diagnostic = diag::warn_condition_is_idiomatic_assignment; | |||
17564 | ||||
17565 | // <foo> = [<bar> nextObject] | |||
17566 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") | |||
17567 | diagnostic = diag::warn_condition_is_idiomatic_assignment; | |||
17568 | } | |||
17569 | ||||
17570 | Loc = Op->getOperatorLoc(); | |||
17571 | } else if (CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) { | |||
17572 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) | |||
17573 | return; | |||
17574 | ||||
17575 | IsOrAssign = Op->getOperator() == OO_PipeEqual; | |||
17576 | Loc = Op->getOperatorLoc(); | |||
17577 | } else if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) | |||
17578 | return DiagnoseAssignmentAsCondition(POE->getSyntacticForm()); | |||
17579 | else { | |||
17580 | // Not an assignment. | |||
17581 | return; | |||
17582 | } | |||
17583 | ||||
17584 | Diag(Loc, diagnostic) << E->getSourceRange(); | |||
17585 | ||||
17586 | SourceLocation Open = E->getBeginLoc(); | |||
17587 | SourceLocation Close = getLocForEndOfToken(E->getSourceRange().getEnd()); | |||
17588 | Diag(Loc, diag::note_condition_assign_silence) | |||
17589 | << FixItHint::CreateInsertion(Open, "(") | |||
17590 | << FixItHint::CreateInsertion(Close, ")"); | |||
17591 | ||||
17592 | if (IsOrAssign) | |||
17593 | Diag(Loc, diag::note_condition_or_assign_to_comparison) | |||
17594 | << FixItHint::CreateReplacement(Loc, "!="); | |||
17595 | else | |||
17596 | Diag(Loc, diag::note_condition_assign_to_comparison) | |||
17597 | << FixItHint::CreateReplacement(Loc, "=="); | |||
17598 | } | |||
17599 | ||||
17600 | /// Redundant parentheses over an equality comparison can indicate | |||
17601 | /// that the user intended an assignment used as condition. | |||
17602 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *ParenE) { | |||
17603 | // Don't warn if the parens came from a macro. | |||
17604 | SourceLocation parenLoc = ParenE->getBeginLoc(); | |||
17605 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) | |||
17606 | return; | |||
17607 | // Don't warn for dependent expressions. | |||
17608 | if (ParenE->isTypeDependent()) | |||
17609 | return; | |||
17610 | ||||
17611 | Expr *E = ParenE->IgnoreParens(); | |||
17612 | ||||
17613 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) | |||
17614 | if (opE->getOpcode() == BO_EQ && | |||
17615 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) | |||
17616 | == Expr::MLV_Valid) { | |||
17617 | SourceLocation Loc = opE->getOperatorLoc(); | |||
17618 | ||||
17619 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); | |||
17620 | SourceRange ParenERange = ParenE->getSourceRange(); | |||
17621 | Diag(Loc, diag::note_equality_comparison_silence) | |||
17622 | << FixItHint::CreateRemoval(ParenERange.getBegin()) | |||
17623 | << FixItHint::CreateRemoval(ParenERange.getEnd()); | |||
17624 | Diag(Loc, diag::note_equality_comparison_to_assign) | |||
17625 | << FixItHint::CreateReplacement(Loc, "="); | |||
17626 | } | |||
17627 | } | |||
17628 | ||||
17629 | ExprResult Sema::CheckBooleanCondition(SourceLocation Loc, Expr *E, | |||
17630 | bool IsConstexpr) { | |||
17631 | DiagnoseAssignmentAsCondition(E); | |||
17632 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) | |||
17633 | DiagnoseEqualityWithExtraParens(parenE); | |||
17634 | ||||
17635 | ExprResult result = CheckPlaceholderExpr(E); | |||
17636 | if (result.isInvalid()) return ExprError(); | |||
17637 | E = result.get(); | |||
17638 | ||||
17639 | if (!E->isTypeDependent()) { | |||
17640 | if (getLangOpts().CPlusPlus) | |||
17641 | return CheckCXXBooleanCondition(E, IsConstexpr); // C++ 6.4p4 | |||
17642 | ||||
17643 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); | |||
17644 | if (ERes.isInvalid()) | |||
17645 | return ExprError(); | |||
17646 | E = ERes.get(); | |||
17647 | ||||
17648 | QualType T = E->getType(); | |||
17649 | if (!T->isScalarType()) { // C99 6.8.4.1p1 | |||
17650 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) | |||
17651 | << T << E->getSourceRange(); | |||
17652 | return ExprError(); | |||
17653 | } | |||
17654 | CheckBoolLikeConversion(E, Loc); | |||
17655 | } | |||
17656 | ||||
17657 | return E; | |||
17658 | } | |||
17659 | ||||
17660 | Sema::ConditionResult Sema::ActOnCondition(Scope *S, SourceLocation Loc, | |||
17661 | Expr *SubExpr, ConditionKind CK) { | |||
17662 | // Empty conditions are valid in for-statements. | |||
17663 | if (!SubExpr) | |||
17664 | return ConditionResult(); | |||
17665 | ||||
17666 | ExprResult Cond; | |||
17667 | switch (CK) { | |||
17668 | case ConditionKind::Boolean: | |||
17669 | Cond = CheckBooleanCondition(Loc, SubExpr); | |||
17670 | break; | |||
17671 | ||||
17672 | case ConditionKind::ConstexprIf: | |||
17673 | Cond = CheckBooleanCondition(Loc, SubExpr, true); | |||
17674 | break; | |||
17675 | ||||
17676 | case ConditionKind::Switch: | |||
17677 | Cond = CheckSwitchCondition(Loc, SubExpr); | |||
17678 | break; | |||
17679 | } | |||
17680 | if (Cond.isInvalid()) | |||
17681 | return ConditionError(); | |||
17682 | ||||
17683 | // FIXME: FullExprArg doesn't have an invalid bit, so check nullness instead. | |||
17684 | FullExprArg FullExpr = MakeFullExpr(Cond.get(), Loc); | |||
17685 | if (!FullExpr.get()) | |||
17686 | return ConditionError(); | |||
17687 | ||||
17688 | return ConditionResult(*this, nullptr, FullExpr, | |||
17689 | CK == ConditionKind::ConstexprIf); | |||
17690 | } | |||
17691 | ||||
17692 | namespace { | |||
17693 | /// A visitor for rebuilding a call to an __unknown_any expression | |||
17694 | /// to have an appropriate type. | |||
17695 | struct RebuildUnknownAnyFunction | |||
17696 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { | |||
17697 | ||||
17698 | Sema &S; | |||
17699 | ||||
17700 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} | |||
17701 | ||||
17702 | ExprResult VisitStmt(Stmt *S) { | |||
17703 | llvm_unreachable("unexpected statement!")::llvm::llvm_unreachable_internal("unexpected statement!", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17703); | |||
17704 | } | |||
17705 | ||||
17706 | ExprResult VisitExpr(Expr *E) { | |||
17707 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_call) | |||
17708 | << E->getSourceRange(); | |||
17709 | return ExprError(); | |||
17710 | } | |||
17711 | ||||
17712 | /// Rebuild an expression which simply semantically wraps another | |||
17713 | /// expression which it shares the type and value kind of. | |||
17714 | template <class T> ExprResult rebuildSugarExpr(T *E) { | |||
17715 | ExprResult SubResult = Visit(E->getSubExpr()); | |||
17716 | if (SubResult.isInvalid()) return ExprError(); | |||
17717 | ||||
17718 | Expr *SubExpr = SubResult.get(); | |||
17719 | E->setSubExpr(SubExpr); | |||
17720 | E->setType(SubExpr->getType()); | |||
17721 | E->setValueKind(SubExpr->getValueKind()); | |||
17722 | assert(E->getObjectKind() == OK_Ordinary)((E->getObjectKind() == OK_Ordinary) ? static_cast<void > (0) : __assert_fail ("E->getObjectKind() == OK_Ordinary" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17722, __PRETTY_FUNCTION__)); | |||
17723 | return E; | |||
17724 | } | |||
17725 | ||||
17726 | ExprResult VisitParenExpr(ParenExpr *E) { | |||
17727 | return rebuildSugarExpr(E); | |||
17728 | } | |||
17729 | ||||
17730 | ExprResult VisitUnaryExtension(UnaryOperator *E) { | |||
17731 | return rebuildSugarExpr(E); | |||
17732 | } | |||
17733 | ||||
17734 | ExprResult VisitUnaryAddrOf(UnaryOperator *E) { | |||
17735 | ExprResult SubResult = Visit(E->getSubExpr()); | |||
17736 | if (SubResult.isInvalid()) return ExprError(); | |||
17737 | ||||
17738 | Expr *SubExpr = SubResult.get(); | |||
17739 | E->setSubExpr(SubExpr); | |||
17740 | E->setType(S.Context.getPointerType(SubExpr->getType())); | |||
17741 | assert(E->getValueKind() == VK_RValue)((E->getValueKind() == VK_RValue) ? static_cast<void> (0) : __assert_fail ("E->getValueKind() == VK_RValue", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17741, __PRETTY_FUNCTION__)); | |||
17742 | assert(E->getObjectKind() == OK_Ordinary)((E->getObjectKind() == OK_Ordinary) ? static_cast<void > (0) : __assert_fail ("E->getObjectKind() == OK_Ordinary" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17742, __PRETTY_FUNCTION__)); | |||
17743 | return E; | |||
17744 | } | |||
17745 | ||||
17746 | ExprResult resolveDecl(Expr *E, ValueDecl *VD) { | |||
17747 | if (!isa<FunctionDecl>(VD)) return VisitExpr(E); | |||
17748 | ||||
17749 | E->setType(VD->getType()); | |||
17750 | ||||
17751 | assert(E->getValueKind() == VK_RValue)((E->getValueKind() == VK_RValue) ? static_cast<void> (0) : __assert_fail ("E->getValueKind() == VK_RValue", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17751, __PRETTY_FUNCTION__)); | |||
17752 | if (S.getLangOpts().CPlusPlus && | |||
17753 | !(isa<CXXMethodDecl>(VD) && | |||
17754 | cast<CXXMethodDecl>(VD)->isInstance())) | |||
17755 | E->setValueKind(VK_LValue); | |||
17756 | ||||
17757 | return E; | |||
17758 | } | |||
17759 | ||||
17760 | ExprResult VisitMemberExpr(MemberExpr *E) { | |||
17761 | return resolveDecl(E, E->getMemberDecl()); | |||
17762 | } | |||
17763 | ||||
17764 | ExprResult VisitDeclRefExpr(DeclRefExpr *E) { | |||
17765 | return resolveDecl(E, E->getDecl()); | |||
17766 | } | |||
17767 | }; | |||
17768 | } | |||
17769 | ||||
17770 | /// Given a function expression of unknown-any type, try to rebuild it | |||
17771 | /// to have a function type. | |||
17772 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *FunctionExpr) { | |||
17773 | ExprResult Result = RebuildUnknownAnyFunction(S).Visit(FunctionExpr); | |||
17774 | if (Result.isInvalid()) return ExprError(); | |||
17775 | return S.DefaultFunctionArrayConversion(Result.get()); | |||
17776 | } | |||
17777 | ||||
17778 | namespace { | |||
17779 | /// A visitor for rebuilding an expression of type __unknown_anytype | |||
17780 | /// into one which resolves the type directly on the referring | |||
17781 | /// expression. Strict preservation of the original source | |||
17782 | /// structure is not a goal. | |||
17783 | struct RebuildUnknownAnyExpr | |||
17784 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { | |||
17785 | ||||
17786 | Sema &S; | |||
17787 | ||||
17788 | /// The current destination type. | |||
17789 | QualType DestType; | |||
17790 | ||||
17791 | RebuildUnknownAnyExpr(Sema &S, QualType CastType) | |||
17792 | : S(S), DestType(CastType) {} | |||
17793 | ||||
17794 | ExprResult VisitStmt(Stmt *S) { | |||
17795 | llvm_unreachable("unexpected statement!")::llvm::llvm_unreachable_internal("unexpected statement!", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17795); | |||
17796 | } | |||
17797 | ||||
17798 | ExprResult VisitExpr(Expr *E) { | |||
17799 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) | |||
17800 | << E->getSourceRange(); | |||
17801 | return ExprError(); | |||
17802 | } | |||
17803 | ||||
17804 | ExprResult VisitCallExpr(CallExpr *E); | |||
17805 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *E); | |||
17806 | ||||
17807 | /// Rebuild an expression which simply semantically wraps another | |||
17808 | /// expression which it shares the type and value kind of. | |||
17809 | template <class T> ExprResult rebuildSugarExpr(T *E) { | |||
17810 | ExprResult SubResult = Visit(E->getSubExpr()); | |||
17811 | if (SubResult.isInvalid()) return ExprError(); | |||
17812 | Expr *SubExpr = SubResult.get(); | |||
17813 | E->setSubExpr(SubExpr); | |||
17814 | E->setType(SubExpr->getType()); | |||
17815 | E->setValueKind(SubExpr->getValueKind()); | |||
17816 | assert(E->getObjectKind() == OK_Ordinary)((E->getObjectKind() == OK_Ordinary) ? static_cast<void > (0) : __assert_fail ("E->getObjectKind() == OK_Ordinary" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17816, __PRETTY_FUNCTION__)); | |||
17817 | return E; | |||
17818 | } | |||
17819 | ||||
17820 | ExprResult VisitParenExpr(ParenExpr *E) { | |||
17821 | return rebuildSugarExpr(E); | |||
17822 | } | |||
17823 | ||||
17824 | ExprResult VisitUnaryExtension(UnaryOperator *E) { | |||
17825 | return rebuildSugarExpr(E); | |||
17826 | } | |||
17827 | ||||
17828 | ExprResult VisitUnaryAddrOf(UnaryOperator *E) { | |||
17829 | const PointerType *Ptr = DestType->getAs<PointerType>(); | |||
17830 | if (!Ptr) { | |||
17831 | S.Diag(E->getOperatorLoc(), diag::err_unknown_any_addrof) | |||
17832 | << E->getSourceRange(); | |||
17833 | return ExprError(); | |||
17834 | } | |||
17835 | ||||
17836 | if (isa<CallExpr>(E->getSubExpr())) { | |||
17837 | S.Diag(E->getOperatorLoc(), diag::err_unknown_any_addrof_call) | |||
17838 | << E->getSourceRange(); | |||
17839 | return ExprError(); | |||
17840 | } | |||
17841 | ||||
17842 | assert(E->getValueKind() == VK_RValue)((E->getValueKind() == VK_RValue) ? static_cast<void> (0) : __assert_fail ("E->getValueKind() == VK_RValue", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17842, __PRETTY_FUNCTION__)); | |||
17843 | assert(E->getObjectKind() == OK_Ordinary)((E->getObjectKind() == OK_Ordinary) ? static_cast<void > (0) : __assert_fail ("E->getObjectKind() == OK_Ordinary" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17843, __PRETTY_FUNCTION__)); | |||
17844 | E->setType(DestType); | |||
17845 | ||||
17846 | // Build the sub-expression as if it were an object of the pointee type. | |||
17847 | DestType = Ptr->getPointeeType(); | |||
17848 | ExprResult SubResult = Visit(E->getSubExpr()); | |||
17849 | if (SubResult.isInvalid()) return ExprError(); | |||
17850 | E->setSubExpr(SubResult.get()); | |||
17851 | return E; | |||
17852 | } | |||
17853 | ||||
17854 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *E); | |||
17855 | ||||
17856 | ExprResult resolveDecl(Expr *E, ValueDecl *VD); | |||
17857 | ||||
17858 | ExprResult VisitMemberExpr(MemberExpr *E) { | |||
17859 | return resolveDecl(E, E->getMemberDecl()); | |||
17860 | } | |||
17861 | ||||
17862 | ExprResult VisitDeclRefExpr(DeclRefExpr *E) { | |||
17863 | return resolveDecl(E, E->getDecl()); | |||
17864 | } | |||
17865 | }; | |||
17866 | } | |||
17867 | ||||
17868 | /// Rebuilds a call expression which yielded __unknown_anytype. | |||
17869 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *E) { | |||
17870 | Expr *CalleeExpr = E->getCallee(); | |||
17871 | ||||
17872 | enum FnKind { | |||
17873 | FK_MemberFunction, | |||
17874 | FK_FunctionPointer, | |||
17875 | FK_BlockPointer | |||
17876 | }; | |||
17877 | ||||
17878 | FnKind Kind; | |||
17879 | QualType CalleeType = CalleeExpr->getType(); | |||
17880 | if (CalleeType == S.Context.BoundMemberTy) { | |||
17881 | assert(isa<CXXMemberCallExpr>(E) || isa<CXXOperatorCallExpr>(E))((isa<CXXMemberCallExpr>(E) || isa<CXXOperatorCallExpr >(E)) ? static_cast<void> (0) : __assert_fail ("isa<CXXMemberCallExpr>(E) || isa<CXXOperatorCallExpr>(E)" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17881, __PRETTY_FUNCTION__)); | |||
17882 | Kind = FK_MemberFunction; | |||
17883 | CalleeType = Expr::findBoundMemberType(CalleeExpr); | |||
17884 | } else if (const PointerType *Ptr = CalleeType->getAs<PointerType>()) { | |||
17885 | CalleeType = Ptr->getPointeeType(); | |||
17886 | Kind = FK_FunctionPointer; | |||
17887 | } else { | |||
17888 | CalleeType = CalleeType->castAs<BlockPointerType>()->getPointeeType(); | |||
17889 | Kind = FK_BlockPointer; | |||
17890 | } | |||
17891 | const FunctionType *FnType = CalleeType->castAs<FunctionType>(); | |||
17892 | ||||
17893 | // Verify that this is a legal result type of a function. | |||
17894 | if (DestType->isArrayType() || DestType->isFunctionType()) { | |||
17895 | unsigned diagID = diag::err_func_returning_array_function; | |||
17896 | if (Kind == FK_BlockPointer) | |||
17897 | diagID = diag::err_block_returning_array_function; | |||
17898 | ||||
17899 | S.Diag(E->getExprLoc(), diagID) | |||
17900 | << DestType->isFunctionType() << DestType; | |||
17901 | return ExprError(); | |||
17902 | } | |||
17903 | ||||
17904 | // Otherwise, go ahead and set DestType as the call's result. | |||
17905 | E->setType(DestType.getNonLValueExprType(S.Context)); | |||
17906 | E->setValueKind(Expr::getValueKindForType(DestType)); | |||
17907 | assert(E->getObjectKind() == OK_Ordinary)((E->getObjectKind() == OK_Ordinary) ? static_cast<void > (0) : __assert_fail ("E->getObjectKind() == OK_Ordinary" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17907, __PRETTY_FUNCTION__)); | |||
17908 | ||||
17909 | // Rebuild the function type, replacing the result type with DestType. | |||
17910 | const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FnType); | |||
17911 | if (Proto) { | |||
17912 | // __unknown_anytype(...) is a special case used by the debugger when | |||
17913 | // it has no idea what a function's signature is. | |||
17914 | // | |||
17915 | // We want to build this call essentially under the K&R | |||
17916 | // unprototyped rules, but making a FunctionNoProtoType in C++ | |||
17917 | // would foul up all sorts of assumptions. However, we cannot | |||
17918 | // simply pass all arguments as variadic arguments, nor can we | |||
17919 | // portably just call the function under a non-variadic type; see | |||
17920 | // the comment on IR-gen's TargetInfo::isNoProtoCallVariadic. | |||
17921 | // However, it turns out that in practice it is generally safe to | |||
17922 | // call a function declared as "A foo(B,C,D);" under the prototype | |||
17923 | // "A foo(B,C,D,...);". The only known exception is with the | |||
17924 | // Windows ABI, where any variadic function is implicitly cdecl | |||
17925 | // regardless of its normal CC. Therefore we change the parameter | |||
17926 | // types to match the types of the arguments. | |||
17927 | // | |||
17928 | // This is a hack, but it is far superior to moving the | |||
17929 | // corresponding target-specific code from IR-gen to Sema/AST. | |||
17930 | ||||
17931 | ArrayRef<QualType> ParamTypes = Proto->getParamTypes(); | |||
17932 | SmallVector<QualType, 8> ArgTypes; | |||
17933 | if (ParamTypes.empty() && Proto->isVariadic()) { // the special case | |||
17934 | ArgTypes.reserve(E->getNumArgs()); | |||
17935 | for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { | |||
17936 | Expr *Arg = E->getArg(i); | |||
17937 | QualType ArgType = Arg->getType(); | |||
17938 | if (E->isLValue()) { | |||
17939 | ArgType = S.Context.getLValueReferenceType(ArgType); | |||
17940 | } else if (E->isXValue()) { | |||
17941 | ArgType = S.Context.getRValueReferenceType(ArgType); | |||
17942 | } | |||
17943 | ArgTypes.push_back(ArgType); | |||
17944 | } | |||
17945 | ParamTypes = ArgTypes; | |||
17946 | } | |||
17947 | DestType = S.Context.getFunctionType(DestType, ParamTypes, | |||
17948 | Proto->getExtProtoInfo()); | |||
17949 | } else { | |||
17950 | DestType = S.Context.getFunctionNoProtoType(DestType, | |||
17951 | FnType->getExtInfo()); | |||
17952 | } | |||
17953 | ||||
17954 | // Rebuild the appropriate pointer-to-function type. | |||
17955 | switch (Kind) { | |||
17956 | case FK_MemberFunction: | |||
17957 | // Nothing to do. | |||
17958 | break; | |||
17959 | ||||
17960 | case FK_FunctionPointer: | |||
17961 | DestType = S.Context.getPointerType(DestType); | |||
17962 | break; | |||
17963 | ||||
17964 | case FK_BlockPointer: | |||
17965 | DestType = S.Context.getBlockPointerType(DestType); | |||
17966 | break; | |||
17967 | } | |||
17968 | ||||
17969 | // Finally, we can recurse. | |||
17970 | ExprResult CalleeResult = Visit(CalleeExpr); | |||
17971 | if (!CalleeResult.isUsable()) return ExprError(); | |||
17972 | E->setCallee(CalleeResult.get()); | |||
17973 | ||||
17974 | // Bind a temporary if necessary. | |||
17975 | return S.MaybeBindToTemporary(E); | |||
17976 | } | |||
17977 | ||||
17978 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *E) { | |||
17979 | // Verify that this is a legal result type of a call. | |||
17980 | if (DestType->isArrayType() || DestType->isFunctionType()) { | |||
17981 | S.Diag(E->getExprLoc(), diag::err_func_returning_array_function) | |||
17982 | << DestType->isFunctionType() << DestType; | |||
17983 | return ExprError(); | |||
17984 | } | |||
17985 | ||||
17986 | // Rewrite the method result type if available. | |||
17987 | if (ObjCMethodDecl *Method = E->getMethodDecl()) { | |||
17988 | assert(Method->getReturnType() == S.Context.UnknownAnyTy)((Method->getReturnType() == S.Context.UnknownAnyTy) ? static_cast <void> (0) : __assert_fail ("Method->getReturnType() == S.Context.UnknownAnyTy" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 17988, __PRETTY_FUNCTION__)); | |||
17989 | Method->setReturnType(DestType); | |||
17990 | } | |||
17991 | ||||
17992 | // Change the type of the message. | |||
17993 | E->setType(DestType.getNonReferenceType()); | |||
17994 | E->setValueKind(Expr::getValueKindForType(DestType)); | |||
17995 | ||||
17996 | return S.MaybeBindToTemporary(E); | |||
17997 | } | |||
17998 | ||||
17999 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *E) { | |||
18000 | // The only case we should ever see here is a function-to-pointer decay. | |||
18001 | if (E->getCastKind() == CK_FunctionToPointerDecay) { | |||
18002 | assert(E->getValueKind() == VK_RValue)((E->getValueKind() == VK_RValue) ? static_cast<void> (0) : __assert_fail ("E->getValueKind() == VK_RValue", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 18002, __PRETTY_FUNCTION__)); | |||
18003 | assert(E->getObjectKind() == OK_Ordinary)((E->getObjectKind() == OK_Ordinary) ? static_cast<void > (0) : __assert_fail ("E->getObjectKind() == OK_Ordinary" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 18003, __PRETTY_FUNCTION__)); | |||
18004 | ||||
18005 | E->setType(DestType); | |||
18006 | ||||
18007 | // Rebuild the sub-expression as the pointee (function) type. | |||
18008 | DestType = DestType->castAs<PointerType>()->getPointeeType(); | |||
18009 | ||||
18010 | ExprResult Result = Visit(E->getSubExpr()); | |||
18011 | if (!Result.isUsable()) return ExprError(); | |||
18012 | ||||
18013 | E->setSubExpr(Result.get()); | |||
18014 | return E; | |||
18015 | } else if (E->getCastKind() == CK_LValueToRValue) { | |||
18016 | assert(E->getValueKind() == VK_RValue)((E->getValueKind() == VK_RValue) ? static_cast<void> (0) : __assert_fail ("E->getValueKind() == VK_RValue", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 18016, __PRETTY_FUNCTION__)); | |||
18017 | assert(E->getObjectKind() == OK_Ordinary)((E->getObjectKind() == OK_Ordinary) ? static_cast<void > (0) : __assert_fail ("E->getObjectKind() == OK_Ordinary" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 18017, __PRETTY_FUNCTION__)); | |||
18018 | ||||
18019 | assert(isa<BlockPointerType>(E->getType()))((isa<BlockPointerType>(E->getType())) ? static_cast <void> (0) : __assert_fail ("isa<BlockPointerType>(E->getType())" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 18019, __PRETTY_FUNCTION__)); | |||
18020 | ||||
18021 | E->setType(DestType); | |||
18022 | ||||
18023 | // The sub-expression has to be a lvalue reference, so rebuild it as such. | |||
18024 | DestType = S.Context.getLValueReferenceType(DestType); | |||
18025 | ||||
18026 | ExprResult Result = Visit(E->getSubExpr()); | |||
18027 | if (!Result.isUsable()) return ExprError(); | |||
18028 | ||||
18029 | E->setSubExpr(Result.get()); | |||
18030 | return E; | |||
18031 | } else { | |||
18032 | llvm_unreachable("Unhandled cast type!")::llvm::llvm_unreachable_internal("Unhandled cast type!", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 18032); | |||
18033 | } | |||
18034 | } | |||
18035 | ||||
18036 | ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *E, ValueDecl *VD) { | |||
18037 | ExprValueKind ValueKind = VK_LValue; | |||
18038 | QualType Type = DestType; | |||
18039 | ||||
18040 | // We know how to make this work for certain kinds of decls: | |||
18041 | ||||
18042 | // - functions | |||
18043 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(VD)) { | |||
18044 | if (const PointerType *Ptr = Type->getAs<PointerType>()) { | |||
18045 | DestType = Ptr->getPointeeType(); | |||
18046 | ExprResult Result = resolveDecl(E, VD); | |||
18047 | if (Result.isInvalid()) return ExprError(); | |||
18048 | return S.ImpCastExprToType(Result.get(), Type, | |||
18049 | CK_FunctionToPointerDecay, VK_RValue); | |||
18050 | } | |||
18051 | ||||
18052 | if (!Type->isFunctionType()) { | |||
18053 | S.Diag(E->getExprLoc(), diag::err_unknown_any_function) | |||
18054 | << VD << E->getSourceRange(); | |||
18055 | return ExprError(); | |||
18056 | } | |||
18057 | if (const FunctionProtoType *FT = Type->getAs<FunctionProtoType>()) { | |||
18058 | // We must match the FunctionDecl's type to the hack introduced in | |||
18059 | // RebuildUnknownAnyExpr::VisitCallExpr to vararg functions of unknown | |||
18060 | // type. See the lengthy commentary in that routine. | |||
18061 | QualType FDT = FD->getType(); | |||
18062 | const FunctionType *FnType = FDT->castAs<FunctionType>(); | |||
18063 | const FunctionProtoType *Proto = dyn_cast_or_null<FunctionProtoType>(FnType); | |||
18064 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); | |||
18065 | if (DRE && Proto && Proto->getParamTypes().empty() && Proto->isVariadic()) { | |||
18066 | SourceLocation Loc = FD->getLocation(); | |||
18067 | FunctionDecl *NewFD = FunctionDecl::Create( | |||
18068 | S.Context, FD->getDeclContext(), Loc, Loc, | |||
18069 | FD->getNameInfo().getName(), DestType, FD->getTypeSourceInfo(), | |||
18070 | SC_None, false /*isInlineSpecified*/, FD->hasPrototype(), | |||
18071 | /*ConstexprKind*/ CSK_unspecified); | |||
18072 | ||||
18073 | if (FD->getQualifier()) | |||
18074 | NewFD->setQualifierInfo(FD->getQualifierLoc()); | |||
18075 | ||||
18076 | SmallVector<ParmVarDecl*, 16> Params; | |||
18077 | for (const auto &AI : FT->param_types()) { | |||
18078 | ParmVarDecl *Param = | |||
18079 | S.BuildParmVarDeclForTypedef(FD, Loc, AI); | |||
18080 | Param->setScopeInfo(0, Params.size()); | |||
18081 | Params.push_back(Param); | |||
18082 | } | |||
18083 | NewFD->setParams(Params); | |||
18084 | DRE->setDecl(NewFD); | |||
18085 | VD = DRE->getDecl(); | |||
18086 | } | |||
18087 | } | |||
18088 | ||||
18089 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) | |||
18090 | if (MD->isInstance()) { | |||
18091 | ValueKind = VK_RValue; | |||
18092 | Type = S.Context.BoundMemberTy; | |||
18093 | } | |||
18094 | ||||
18095 | // Function references aren't l-values in C. | |||
18096 | if (!S.getLangOpts().CPlusPlus) | |||
18097 | ValueKind = VK_RValue; | |||
18098 | ||||
18099 | // - variables | |||
18100 | } else if (isa<VarDecl>(VD)) { | |||
18101 | if (const ReferenceType *RefTy = Type->getAs<ReferenceType>()) { | |||
18102 | Type = RefTy->getPointeeType(); | |||
18103 | } else if (Type->isFunctionType()) { | |||
18104 | S.Diag(E->getExprLoc(), diag::err_unknown_any_var_function_type) | |||
18105 | << VD << E->getSourceRange(); | |||
18106 | return ExprError(); | |||
18107 | } | |||
18108 | ||||
18109 | // - nothing else | |||
18110 | } else { | |||
18111 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_decl) | |||
18112 | << VD << E->getSourceRange(); | |||
18113 | return ExprError(); | |||
18114 | } | |||
18115 | ||||
18116 | // Modifying the declaration like this is friendly to IR-gen but | |||
18117 | // also really dangerous. | |||
18118 | VD->setType(DestType); | |||
18119 | E->setType(Type); | |||
18120 | E->setValueKind(ValueKind); | |||
18121 | return E; | |||
18122 | } | |||
18123 | ||||
18124 | /// Check a cast of an unknown-any type. We intentionally only | |||
18125 | /// trigger this for C-style casts. | |||
18126 | ExprResult Sema::checkUnknownAnyCast(SourceRange TypeRange, QualType CastType, | |||
18127 | Expr *CastExpr, CastKind &CastKind, | |||
18128 | ExprValueKind &VK, CXXCastPath &Path) { | |||
18129 | // The type we're casting to must be either void or complete. | |||
18130 | if (!CastType->isVoidType() && | |||
18131 | RequireCompleteType(TypeRange.getBegin(), CastType, | |||
18132 | diag::err_typecheck_cast_to_incomplete)) | |||
18133 | return ExprError(); | |||
18134 | ||||
18135 | // Rewrite the casted expression from scratch. | |||
18136 | ExprResult result = RebuildUnknownAnyExpr(*this, CastType).Visit(CastExpr); | |||
18137 | if (!result.isUsable()) return ExprError(); | |||
18138 | ||||
18139 | CastExpr = result.get(); | |||
18140 | VK = CastExpr->getValueKind(); | |||
18141 | CastKind = CK_NoOp; | |||
18142 | ||||
18143 | return CastExpr; | |||
18144 | } | |||
18145 | ||||
18146 | ExprResult Sema::forceUnknownAnyToType(Expr *E, QualType ToType) { | |||
18147 | return RebuildUnknownAnyExpr(*this, ToType).Visit(E); | |||
18148 | } | |||
18149 | ||||
18150 | ExprResult Sema::checkUnknownAnyArg(SourceLocation callLoc, | |||
18151 | Expr *arg, QualType ¶mType) { | |||
18152 | // If the syntactic form of the argument is not an explicit cast of | |||
18153 | // any sort, just do default argument promotion. | |||
18154 | ExplicitCastExpr *castArg = dyn_cast<ExplicitCastExpr>(arg->IgnoreParens()); | |||
18155 | if (!castArg) { | |||
18156 | ExprResult result = DefaultArgumentPromotion(arg); | |||
18157 | if (result.isInvalid()) return ExprError(); | |||
18158 | paramType = result.get()->getType(); | |||
18159 | return result; | |||
18160 | } | |||
18161 | ||||
18162 | // Otherwise, use the type that was written in the explicit cast. | |||
18163 | assert(!arg->hasPlaceholderType())((!arg->hasPlaceholderType()) ? static_cast<void> (0 ) : __assert_fail ("!arg->hasPlaceholderType()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 18163, __PRETTY_FUNCTION__)); | |||
18164 | paramType = castArg->getTypeAsWritten(); | |||
18165 | ||||
18166 | // Copy-initialize a parameter of that type. | |||
18167 | InitializedEntity entity = | |||
18168 | InitializedEntity::InitializeParameter(Context, paramType, | |||
18169 | /*consumed*/ false); | |||
18170 | return PerformCopyInitialization(entity, callLoc, arg); | |||
18171 | } | |||
18172 | ||||
18173 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *E) { | |||
18174 | Expr *orig = E; | |||
18175 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; | |||
18176 | while (true) { | |||
18177 | E = E->IgnoreParenImpCasts(); | |||
18178 | if (CallExpr *call = dyn_cast<CallExpr>(E)) { | |||
18179 | E = call->getCallee(); | |||
18180 | diagID = diag::err_uncasted_call_of_unknown_any; | |||
18181 | } else { | |||
18182 | break; | |||
18183 | } | |||
18184 | } | |||
18185 | ||||
18186 | SourceLocation loc; | |||
18187 | NamedDecl *d; | |||
18188 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(E)) { | |||
18189 | loc = ref->getLocation(); | |||
18190 | d = ref->getDecl(); | |||
18191 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(E)) { | |||
18192 | loc = mem->getMemberLoc(); | |||
18193 | d = mem->getMemberDecl(); | |||
18194 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(E)) { | |||
18195 | diagID = diag::err_uncasted_call_of_unknown_any; | |||
18196 | loc = msg->getSelectorStartLoc(); | |||
18197 | d = msg->getMethodDecl(); | |||
18198 | if (!d) { | |||
18199 | S.Diag(loc, diag::err_uncasted_send_to_unknown_any_method) | |||
18200 | << static_cast<unsigned>(msg->isClassMessage()) << msg->getSelector() | |||
18201 | << orig->getSourceRange(); | |||
18202 | return ExprError(); | |||
18203 | } | |||
18204 | } else { | |||
18205 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) | |||
18206 | << E->getSourceRange(); | |||
18207 | return ExprError(); | |||
18208 | } | |||
18209 | ||||
18210 | S.Diag(loc, diagID) << d << orig->getSourceRange(); | |||
18211 | ||||
18212 | // Never recoverable. | |||
18213 | return ExprError(); | |||
18214 | } | |||
18215 | ||||
18216 | /// Check for operands with placeholder types and complain if found. | |||
18217 | /// Returns ExprError() if there was an error and no recovery was possible. | |||
18218 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { | |||
18219 | if (!getLangOpts().CPlusPlus) { | |||
18220 | // C cannot handle TypoExpr nodes on either side of a binop because it | |||
18221 | // doesn't handle dependent types properly, so make sure any TypoExprs have | |||
18222 | // been dealt with before checking the operands. | |||
18223 | ExprResult Result = CorrectDelayedTyposInExpr(E); | |||
18224 | if (!Result.isUsable()) return ExprError(); | |||
18225 | E = Result.get(); | |||
18226 | } | |||
18227 | ||||
18228 | const BuiltinType *placeholderType = E->getType()->getAsPlaceholderType(); | |||
18229 | if (!placeholderType) return E; | |||
18230 | ||||
18231 | switch (placeholderType->getKind()) { | |||
18232 | ||||
18233 | // Overloaded expressions. | |||
18234 | case BuiltinType::Overload: { | |||
18235 | // Try to resolve a single function template specialization. | |||
18236 | // This is obligatory. | |||
18237 | ExprResult Result = E; | |||
18238 | if (ResolveAndFixSingleFunctionTemplateSpecialization(Result, false)) | |||
18239 | return Result; | |||
18240 | ||||
18241 | // No guarantees that ResolveAndFixSingleFunctionTemplateSpecialization | |||
18242 | // leaves Result unchanged on failure. | |||
18243 | Result = E; | |||
18244 | if (resolveAndFixAddressOfSingleOverloadCandidate(Result)) | |||
18245 | return Result; | |||
18246 | ||||
18247 | // If that failed, try to recover with a call. | |||
18248 | tryToRecoverWithCall(Result, PDiag(diag::err_ovl_unresolvable), | |||
18249 | /*complain*/ true); | |||
18250 | return Result; | |||
18251 | } | |||
18252 | ||||
18253 | // Bound member functions. | |||
18254 | case BuiltinType::BoundMember: { | |||
18255 | ExprResult result = E; | |||
18256 | const Expr *BME = E->IgnoreParens(); | |||
18257 | PartialDiagnostic PD = PDiag(diag::err_bound_member_function); | |||
18258 | // Try to give a nicer diagnostic if it is a bound member that we recognize. | |||
18259 | if (isa<CXXPseudoDestructorExpr>(BME)) { | |||
18260 | PD = PDiag(diag::err_dtor_expr_without_call) << /*pseudo-destructor*/ 1; | |||
18261 | } else if (const auto *ME = dyn_cast<MemberExpr>(BME)) { | |||
18262 | if (ME->getMemberNameInfo().getName().getNameKind() == | |||
18263 | DeclarationName::CXXDestructorName) | |||
18264 | PD = PDiag(diag::err_dtor_expr_without_call) << /*destructor*/ 0; | |||
18265 | } | |||
18266 | tryToRecoverWithCall(result, PD, | |||
18267 | /*complain*/ true); | |||
18268 | return result; | |||
18269 | } | |||
18270 | ||||
18271 | // ARC unbridged casts. | |||
18272 | case BuiltinType::ARCUnbridgedCast: { | |||
18273 | Expr *realCast = stripARCUnbridgedCast(E); | |||
18274 | diagnoseARCUnbridgedCast(realCast); | |||
18275 | return realCast; | |||
18276 | } | |||
18277 | ||||
18278 | // Expressions of unknown type. | |||
18279 | case BuiltinType::UnknownAny: | |||
18280 | return diagnoseUnknownAnyExpr(*this, E); | |||
18281 | ||||
18282 | // Pseudo-objects. | |||
18283 | case BuiltinType::PseudoObject: | |||
18284 | return checkPseudoObjectRValue(E); | |||
18285 | ||||
18286 | case BuiltinType::BuiltinFn: { | |||
18287 | // Accept __noop without parens by implicitly converting it to a call expr. | |||
18288 | auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()); | |||
18289 | if (DRE) { | |||
18290 | auto *FD = cast<FunctionDecl>(DRE->getDecl()); | |||
18291 | if (FD->getBuiltinID() == Builtin::BI__noop) { | |||
18292 | E = ImpCastExprToType(E, Context.getPointerType(FD->getType()), | |||
18293 | CK_BuiltinFnToFnPtr) | |||
18294 | .get(); | |||
18295 | return CallExpr::Create(Context, E, /*Args=*/{}, Context.IntTy, | |||
18296 | VK_RValue, SourceLocation()); | |||
18297 | } | |||
18298 | } | |||
18299 | ||||
18300 | Diag(E->getBeginLoc(), diag::err_builtin_fn_use); | |||
18301 | return ExprError(); | |||
18302 | } | |||
18303 | ||||
18304 | // Expressions of unknown type. | |||
18305 | case BuiltinType::OMPArraySection: | |||
18306 | Diag(E->getBeginLoc(), diag::err_omp_array_section_use); | |||
18307 | return ExprError(); | |||
18308 | ||||
18309 | // Everything else should be impossible. | |||
18310 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ | |||
18311 | case BuiltinType::Id: | |||
18312 | #include "clang/Basic/OpenCLImageTypes.def" | |||
18313 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ | |||
18314 | case BuiltinType::Id: | |||
18315 | #include "clang/Basic/OpenCLExtensionTypes.def" | |||
18316 | #define SVE_TYPE(Name, Id, SingletonId) \ | |||
18317 | case BuiltinType::Id: | |||
18318 | #include "clang/Basic/AArch64SVEACLETypes.def" | |||
18319 | #define BUILTIN_TYPE(Id, SingletonId) case BuiltinType::Id: | |||
18320 | #define PLACEHOLDER_TYPE(Id, SingletonId) | |||
18321 | #include "clang/AST/BuiltinTypes.def" | |||
18322 | break; | |||
18323 | } | |||
18324 | ||||
18325 | llvm_unreachable("invalid placeholder type!")::llvm::llvm_unreachable_internal("invalid placeholder type!" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 18325); | |||
18326 | } | |||
18327 | ||||
18328 | bool Sema::CheckCaseExpression(Expr *E) { | |||
18329 | if (E->isTypeDependent()) | |||
18330 | return true; | |||
18331 | if (E->isValueDependent() || E->isIntegerConstantExpr(Context)) | |||
18332 | return E->getType()->isIntegralOrEnumerationType(); | |||
18333 | return false; | |||
18334 | } | |||
18335 | ||||
18336 | /// ActOnObjCBoolLiteral - Parse {__objc_yes,__objc_no} literals. | |||
18337 | ExprResult | |||
18338 | Sema::ActOnObjCBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { | |||
18339 | assert((Kind == tok::kw___objc_yes || Kind == tok::kw___objc_no) &&(((Kind == tok::kw___objc_yes || Kind == tok::kw___objc_no) && "Unknown Objective-C Boolean value!") ? static_cast<void> (0) : __assert_fail ("(Kind == tok::kw___objc_yes || Kind == tok::kw___objc_no) && \"Unknown Objective-C Boolean value!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 18340, __PRETTY_FUNCTION__)) | |||
18340 | "Unknown Objective-C Boolean value!")(((Kind == tok::kw___objc_yes || Kind == tok::kw___objc_no) && "Unknown Objective-C Boolean value!") ? static_cast<void> (0) : __assert_fail ("(Kind == tok::kw___objc_yes || Kind == tok::kw___objc_no) && \"Unknown Objective-C Boolean value!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 18340, __PRETTY_FUNCTION__)); | |||
18341 | QualType BoolT = Context.ObjCBuiltinBoolTy; | |||
18342 | if (!Context.getBOOLDecl()) { | |||
18343 | LookupResult Result(*this, &Context.Idents.get("BOOL"), OpLoc, | |||
18344 | Sema::LookupOrdinaryName); | |||
18345 | if (LookupName(Result, getCurScope()) && Result.isSingleResult()) { | |||
18346 | NamedDecl *ND = Result.getFoundDecl(); | |||
18347 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) | |||
18348 | Context.setBOOLDecl(TD); | |||
18349 | } | |||
18350 | } | |||
18351 | if (Context.getBOOLDecl()) | |||
18352 | BoolT = Context.getBOOLType(); | |||
18353 | return new (Context) | |||
18354 | ObjCBoolLiteralExpr(Kind == tok::kw___objc_yes, BoolT, OpLoc); | |||
18355 | } | |||
18356 | ||||
18357 | ExprResult Sema::ActOnObjCAvailabilityCheckExpr( | |||
18358 | llvm::ArrayRef<AvailabilitySpec> AvailSpecs, SourceLocation AtLoc, | |||
18359 | SourceLocation RParen) { | |||
18360 | ||||
18361 | StringRef Platform = getASTContext().getTargetInfo().getPlatformName(); | |||
18362 | ||||
18363 | auto Spec = llvm::find_if(AvailSpecs, [&](const AvailabilitySpec &Spec) { | |||
18364 | return Spec.getPlatform() == Platform; | |||
18365 | }); | |||
18366 | ||||
18367 | VersionTuple Version; | |||
18368 | if (Spec != AvailSpecs.end()) | |||
18369 | Version = Spec->getVersion(); | |||
18370 | ||||
18371 | // The use of `@available` in the enclosing function should be analyzed to | |||
18372 | // warn when it's used inappropriately (i.e. not if(@available)). | |||
18373 | if (getCurFunctionOrMethodDecl()) | |||
18374 | getEnclosingFunction()->HasPotentialAvailabilityViolations = true; | |||
18375 | else if (getCurBlock() || getCurLambda()) | |||
18376 | getCurFunction()->HasPotentialAvailabilityViolations = true; | |||
18377 | ||||
18378 | return new (Context) | |||
18379 | ObjCAvailabilityCheckExpr(Version, AtLoc, RParen, Context.BoolTy); | |||
18380 | } | |||
18381 | ||||
18382 | bool Sema::IsDependentFunctionNameExpr(Expr *E) { | |||
18383 | assert(E->isTypeDependent())((E->isTypeDependent()) ? static_cast<void> (0) : __assert_fail ("E->isTypeDependent()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/SemaExpr.cpp" , 18383, __PRETTY_FUNCTION__)); | |||
18384 | return isa<UnresolvedLookupExpr>(E); | |||
18385 | } |
1 | //===------- TreeTransform.h - Semantic Tree Transformation -----*- C++ -*-===// | ||||||||
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 | // This file implements a semantic tree transformation that takes a given | ||||||||
9 | // AST and rebuilds it, possibly transforming some nodes in the process. | ||||||||
10 | // | ||||||||
11 | //===----------------------------------------------------------------------===// | ||||||||
12 | |||||||||
13 | #ifndef LLVM_CLANG_LIB_SEMA_TREETRANSFORM_H | ||||||||
14 | #define LLVM_CLANG_LIB_SEMA_TREETRANSFORM_H | ||||||||
15 | |||||||||
16 | #include "CoroutineStmtBuilder.h" | ||||||||
17 | #include "TypeLocBuilder.h" | ||||||||
18 | #include "clang/AST/Decl.h" | ||||||||
19 | #include "clang/AST/DeclObjC.h" | ||||||||
20 | #include "clang/AST/DeclTemplate.h" | ||||||||
21 | #include "clang/AST/Expr.h" | ||||||||
22 | #include "clang/AST/ExprConcepts.h" | ||||||||
23 | #include "clang/AST/ExprCXX.h" | ||||||||
24 | #include "clang/AST/ExprObjC.h" | ||||||||
25 | #include "clang/AST/ExprOpenMP.h" | ||||||||
26 | #include "clang/AST/OpenMPClause.h" | ||||||||
27 | #include "clang/AST/Stmt.h" | ||||||||
28 | #include "clang/AST/StmtCXX.h" | ||||||||
29 | #include "clang/AST/StmtObjC.h" | ||||||||
30 | #include "clang/AST/StmtOpenMP.h" | ||||||||
31 | #include "clang/Sema/Designator.h" | ||||||||
32 | #include "clang/Sema/Lookup.h" | ||||||||
33 | #include "clang/Sema/Ownership.h" | ||||||||
34 | #include "clang/Sema/ParsedTemplate.h" | ||||||||
35 | #include "clang/Sema/ScopeInfo.h" | ||||||||
36 | #include "clang/Sema/SemaDiagnostic.h" | ||||||||
37 | #include "clang/Sema/SemaInternal.h" | ||||||||
38 | #include "llvm/ADT/ArrayRef.h" | ||||||||
39 | #include "llvm/Support/ErrorHandling.h" | ||||||||
40 | #include <algorithm> | ||||||||
41 | |||||||||
42 | using namespace llvm::omp; | ||||||||
43 | |||||||||
44 | namespace clang { | ||||||||
45 | using namespace sema; | ||||||||
46 | |||||||||
47 | /// A semantic tree transformation that allows one to transform one | ||||||||
48 | /// abstract syntax tree into another. | ||||||||
49 | /// | ||||||||
50 | /// A new tree transformation is defined by creating a new subclass \c X of | ||||||||
51 | /// \c TreeTransform<X> and then overriding certain operations to provide | ||||||||
52 | /// behavior specific to that transformation. For example, template | ||||||||
53 | /// instantiation is implemented as a tree transformation where the | ||||||||
54 | /// transformation of TemplateTypeParmType nodes involves substituting the | ||||||||
55 | /// template arguments for their corresponding template parameters; a similar | ||||||||
56 | /// transformation is performed for non-type template parameters and | ||||||||
57 | /// template template parameters. | ||||||||
58 | /// | ||||||||
59 | /// This tree-transformation template uses static polymorphism to allow | ||||||||
60 | /// subclasses to customize any of its operations. Thus, a subclass can | ||||||||
61 | /// override any of the transformation or rebuild operators by providing an | ||||||||
62 | /// operation with the same signature as the default implementation. The | ||||||||
63 | /// overriding function should not be virtual. | ||||||||
64 | /// | ||||||||
65 | /// Semantic tree transformations are split into two stages, either of which | ||||||||
66 | /// can be replaced by a subclass. The "transform" step transforms an AST node | ||||||||
67 | /// or the parts of an AST node using the various transformation functions, | ||||||||
68 | /// then passes the pieces on to the "rebuild" step, which constructs a new AST | ||||||||
69 | /// node of the appropriate kind from the pieces. The default transformation | ||||||||
70 | /// routines recursively transform the operands to composite AST nodes (e.g., | ||||||||
71 | /// the pointee type of a PointerType node) and, if any of those operand nodes | ||||||||
72 | /// were changed by the transformation, invokes the rebuild operation to create | ||||||||
73 | /// a new AST node. | ||||||||
74 | /// | ||||||||
75 | /// Subclasses can customize the transformation at various levels. The | ||||||||
76 | /// most coarse-grained transformations involve replacing TransformType(), | ||||||||
77 | /// TransformExpr(), TransformDecl(), TransformNestedNameSpecifierLoc(), | ||||||||
78 | /// TransformTemplateName(), or TransformTemplateArgument() with entirely | ||||||||
79 | /// new implementations. | ||||||||
80 | /// | ||||||||
81 | /// For more fine-grained transformations, subclasses can replace any of the | ||||||||
82 | /// \c TransformXXX functions (where XXX is the name of an AST node, e.g., | ||||||||
83 | /// PointerType, StmtExpr) to alter the transformation. As mentioned previously, | ||||||||
84 | /// replacing TransformTemplateTypeParmType() allows template instantiation | ||||||||
85 | /// to substitute template arguments for their corresponding template | ||||||||
86 | /// parameters. Additionally, subclasses can override the \c RebuildXXX | ||||||||
87 | /// functions to control how AST nodes are rebuilt when their operands change. | ||||||||
88 | /// By default, \c TreeTransform will invoke semantic analysis to rebuild | ||||||||
89 | /// AST nodes. However, certain other tree transformations (e.g, cloning) may | ||||||||
90 | /// be able to use more efficient rebuild steps. | ||||||||
91 | /// | ||||||||
92 | /// There are a handful of other functions that can be overridden, allowing one | ||||||||
93 | /// to avoid traversing nodes that don't need any transformation | ||||||||
94 | /// (\c AlreadyTransformed()), force rebuilding AST nodes even when their | ||||||||
95 | /// operands have not changed (\c AlwaysRebuild()), and customize the | ||||||||
96 | /// default locations and entity names used for type-checking | ||||||||
97 | /// (\c getBaseLocation(), \c getBaseEntity()). | ||||||||
98 | template<typename Derived> | ||||||||
99 | class TreeTransform { | ||||||||
100 | /// Private RAII object that helps us forget and then re-remember | ||||||||
101 | /// the template argument corresponding to a partially-substituted parameter | ||||||||
102 | /// pack. | ||||||||
103 | class ForgetPartiallySubstitutedPackRAII { | ||||||||
104 | Derived &Self; | ||||||||
105 | TemplateArgument Old; | ||||||||
106 | |||||||||
107 | public: | ||||||||
108 | ForgetPartiallySubstitutedPackRAII(Derived &Self) : Self(Self) { | ||||||||
109 | Old = Self.ForgetPartiallySubstitutedPack(); | ||||||||
110 | } | ||||||||
111 | |||||||||
112 | ~ForgetPartiallySubstitutedPackRAII() { | ||||||||
113 | Self.RememberPartiallySubstitutedPack(Old); | ||||||||
114 | } | ||||||||
115 | }; | ||||||||
116 | |||||||||
117 | protected: | ||||||||
118 | Sema &SemaRef; | ||||||||
119 | |||||||||
120 | /// The set of local declarations that have been transformed, for | ||||||||
121 | /// cases where we are forced to build new declarations within the transformer | ||||||||
122 | /// rather than in the subclass (e.g., lambda closure types). | ||||||||
123 | llvm::DenseMap<Decl *, Decl *> TransformedLocalDecls; | ||||||||
124 | |||||||||
125 | public: | ||||||||
126 | /// Initializes a new tree transformer. | ||||||||
127 | TreeTransform(Sema &SemaRef) : SemaRef(SemaRef) { } | ||||||||
128 | |||||||||
129 | /// Retrieves a reference to the derived class. | ||||||||
130 | Derived &getDerived() { return static_cast<Derived&>(*this); } | ||||||||
131 | |||||||||
132 | /// Retrieves a reference to the derived class. | ||||||||
133 | const Derived &getDerived() const { | ||||||||
134 | return static_cast<const Derived&>(*this); | ||||||||
135 | } | ||||||||
136 | |||||||||
137 | static inline ExprResult Owned(Expr *E) { return E; } | ||||||||
138 | static inline StmtResult Owned(Stmt *S) { return S; } | ||||||||
139 | |||||||||
140 | /// Retrieves a reference to the semantic analysis object used for | ||||||||
141 | /// this tree transform. | ||||||||
142 | Sema &getSema() const { return SemaRef; } | ||||||||
143 | |||||||||
144 | /// Whether the transformation should always rebuild AST nodes, even | ||||||||
145 | /// if none of the children have changed. | ||||||||
146 | /// | ||||||||
147 | /// Subclasses may override this function to specify when the transformation | ||||||||
148 | /// should rebuild all AST nodes. | ||||||||
149 | /// | ||||||||
150 | /// We must always rebuild all AST nodes when performing variadic template | ||||||||
151 | /// pack expansion, in order to avoid violating the AST invariant that each | ||||||||
152 | /// statement node appears at most once in its containing declaration. | ||||||||
153 | bool AlwaysRebuild() { return SemaRef.ArgumentPackSubstitutionIndex != -1; } | ||||||||
154 | |||||||||
155 | /// Whether the transformation is forming an expression or statement that | ||||||||
156 | /// replaces the original. In this case, we'll reuse mangling numbers from | ||||||||
157 | /// existing lambdas. | ||||||||
158 | bool ReplacingOriginal() { return false; } | ||||||||
159 | |||||||||
160 | /// Returns the location of the entity being transformed, if that | ||||||||
161 | /// information was not available elsewhere in the AST. | ||||||||
162 | /// | ||||||||
163 | /// By default, returns no source-location information. Subclasses can | ||||||||
164 | /// provide an alternative implementation that provides better location | ||||||||
165 | /// information. | ||||||||
166 | SourceLocation getBaseLocation() { return SourceLocation(); } | ||||||||
167 | |||||||||
168 | /// Returns the name of the entity being transformed, if that | ||||||||
169 | /// information was not available elsewhere in the AST. | ||||||||
170 | /// | ||||||||
171 | /// By default, returns an empty name. Subclasses can provide an alternative | ||||||||
172 | /// implementation with a more precise name. | ||||||||
173 | DeclarationName getBaseEntity() { return DeclarationName(); } | ||||||||
174 | |||||||||
175 | /// Sets the "base" location and entity when that | ||||||||
176 | /// information is known based on another transformation. | ||||||||
177 | /// | ||||||||
178 | /// By default, the source location and entity are ignored. Subclasses can | ||||||||
179 | /// override this function to provide a customized implementation. | ||||||||
180 | void setBase(SourceLocation Loc, DeclarationName Entity) { } | ||||||||
181 | |||||||||
182 | /// RAII object that temporarily sets the base location and entity | ||||||||
183 | /// used for reporting diagnostics in types. | ||||||||
184 | class TemporaryBase { | ||||||||
185 | TreeTransform &Self; | ||||||||
186 | SourceLocation OldLocation; | ||||||||
187 | DeclarationName OldEntity; | ||||||||
188 | |||||||||
189 | public: | ||||||||
190 | TemporaryBase(TreeTransform &Self, SourceLocation Location, | ||||||||
191 | DeclarationName Entity) : Self(Self) { | ||||||||
192 | OldLocation = Self.getDerived().getBaseLocation(); | ||||||||
193 | OldEntity = Self.getDerived().getBaseEntity(); | ||||||||
194 | |||||||||
195 | if (Location.isValid()) | ||||||||
196 | Self.getDerived().setBase(Location, Entity); | ||||||||
197 | } | ||||||||
198 | |||||||||
199 | ~TemporaryBase() { | ||||||||
200 | Self.getDerived().setBase(OldLocation, OldEntity); | ||||||||
201 | } | ||||||||
202 | }; | ||||||||
203 | |||||||||
204 | /// Determine whether the given type \p T has already been | ||||||||
205 | /// transformed. | ||||||||
206 | /// | ||||||||
207 | /// Subclasses can provide an alternative implementation of this routine | ||||||||
208 | /// to short-circuit evaluation when it is known that a given type will | ||||||||
209 | /// not change. For example, template instantiation need not traverse | ||||||||
210 | /// non-dependent types. | ||||||||
211 | bool AlreadyTransformed(QualType T) { | ||||||||
212 | return T.isNull(); | ||||||||
213 | } | ||||||||
214 | |||||||||
215 | /// Determine whether the given call argument should be dropped, e.g., | ||||||||
216 | /// because it is a default argument. | ||||||||
217 | /// | ||||||||
218 | /// Subclasses can provide an alternative implementation of this routine to | ||||||||
219 | /// determine which kinds of call arguments get dropped. By default, | ||||||||
220 | /// CXXDefaultArgument nodes are dropped (prior to transformation). | ||||||||
221 | bool DropCallArgument(Expr *E) { | ||||||||
222 | return E->isDefaultArgument(); | ||||||||
223 | } | ||||||||
224 | |||||||||
225 | /// Determine whether we should expand a pack expansion with the | ||||||||
226 | /// given set of parameter packs into separate arguments by repeatedly | ||||||||
227 | /// transforming the pattern. | ||||||||
228 | /// | ||||||||
229 | /// By default, the transformer never tries to expand pack expansions. | ||||||||
230 | /// Subclasses can override this routine to provide different behavior. | ||||||||
231 | /// | ||||||||
232 | /// \param EllipsisLoc The location of the ellipsis that identifies the | ||||||||
233 | /// pack expansion. | ||||||||
234 | /// | ||||||||
235 | /// \param PatternRange The source range that covers the entire pattern of | ||||||||
236 | /// the pack expansion. | ||||||||
237 | /// | ||||||||
238 | /// \param Unexpanded The set of unexpanded parameter packs within the | ||||||||
239 | /// pattern. | ||||||||
240 | /// | ||||||||
241 | /// \param ShouldExpand Will be set to \c true if the transformer should | ||||||||
242 | /// expand the corresponding pack expansions into separate arguments. When | ||||||||
243 | /// set, \c NumExpansions must also be set. | ||||||||
244 | /// | ||||||||
245 | /// \param RetainExpansion Whether the caller should add an unexpanded | ||||||||
246 | /// pack expansion after all of the expanded arguments. This is used | ||||||||
247 | /// when extending explicitly-specified template argument packs per | ||||||||
248 | /// C++0x [temp.arg.explicit]p9. | ||||||||
249 | /// | ||||||||
250 | /// \param NumExpansions The number of separate arguments that will be in | ||||||||
251 | /// the expanded form of the corresponding pack expansion. This is both an | ||||||||
252 | /// input and an output parameter, which can be set by the caller if the | ||||||||
253 | /// number of expansions is known a priori (e.g., due to a prior substitution) | ||||||||
254 | /// and will be set by the callee when the number of expansions is known. | ||||||||
255 | /// The callee must set this value when \c ShouldExpand is \c true; it may | ||||||||
256 | /// set this value in other cases. | ||||||||
257 | /// | ||||||||
258 | /// \returns true if an error occurred (e.g., because the parameter packs | ||||||||
259 | /// are to be instantiated with arguments of different lengths), false | ||||||||
260 | /// otherwise. If false, \c ShouldExpand (and possibly \c NumExpansions) | ||||||||
261 | /// must be set. | ||||||||
262 | bool TryExpandParameterPacks(SourceLocation EllipsisLoc, | ||||||||
263 | SourceRange PatternRange, | ||||||||
264 | ArrayRef<UnexpandedParameterPack> Unexpanded, | ||||||||
265 | bool &ShouldExpand, | ||||||||
266 | bool &RetainExpansion, | ||||||||
267 | Optional<unsigned> &NumExpansions) { | ||||||||
268 | ShouldExpand = false; | ||||||||
269 | return false; | ||||||||
270 | } | ||||||||
271 | |||||||||
272 | /// "Forget" about the partially-substituted pack template argument, | ||||||||
273 | /// when performing an instantiation that must preserve the parameter pack | ||||||||
274 | /// use. | ||||||||
275 | /// | ||||||||
276 | /// This routine is meant to be overridden by the template instantiator. | ||||||||
277 | TemplateArgument ForgetPartiallySubstitutedPack() { | ||||||||
278 | return TemplateArgument(); | ||||||||
279 | } | ||||||||
280 | |||||||||
281 | /// "Remember" the partially-substituted pack template argument | ||||||||
282 | /// after performing an instantiation that must preserve the parameter pack | ||||||||
283 | /// use. | ||||||||
284 | /// | ||||||||
285 | /// This routine is meant to be overridden by the template instantiator. | ||||||||
286 | void RememberPartiallySubstitutedPack(TemplateArgument Arg) { } | ||||||||
287 | |||||||||
288 | /// Note to the derived class when a function parameter pack is | ||||||||
289 | /// being expanded. | ||||||||
290 | void ExpandingFunctionParameterPack(ParmVarDecl *Pack) { } | ||||||||
291 | |||||||||
292 | /// Transforms the given type into another type. | ||||||||
293 | /// | ||||||||
294 | /// By default, this routine transforms a type by creating a | ||||||||
295 | /// TypeSourceInfo for it and delegating to the appropriate | ||||||||
296 | /// function. This is expensive, but we don't mind, because | ||||||||
297 | /// this method is deprecated anyway; all users should be | ||||||||
298 | /// switched to storing TypeSourceInfos. | ||||||||
299 | /// | ||||||||
300 | /// \returns the transformed type. | ||||||||
301 | QualType TransformType(QualType T); | ||||||||
302 | |||||||||
303 | /// Transforms the given type-with-location into a new | ||||||||
304 | /// type-with-location. | ||||||||
305 | /// | ||||||||
306 | /// By default, this routine transforms a type by delegating to the | ||||||||
307 | /// appropriate TransformXXXType to build a new type. Subclasses | ||||||||
308 | /// may override this function (to take over all type | ||||||||
309 | /// transformations) or some set of the TransformXXXType functions | ||||||||
310 | /// to alter the transformation. | ||||||||
311 | TypeSourceInfo *TransformType(TypeSourceInfo *DI); | ||||||||
312 | |||||||||
313 | /// Transform the given type-with-location into a new | ||||||||
314 | /// type, collecting location information in the given builder | ||||||||
315 | /// as necessary. | ||||||||
316 | /// | ||||||||
317 | QualType TransformType(TypeLocBuilder &TLB, TypeLoc TL); | ||||||||
318 | |||||||||
319 | /// Transform a type that is permitted to produce a | ||||||||
320 | /// DeducedTemplateSpecializationType. | ||||||||
321 | /// | ||||||||
322 | /// This is used in the (relatively rare) contexts where it is acceptable | ||||||||
323 | /// for transformation to produce a class template type with deduced | ||||||||
324 | /// template arguments. | ||||||||
325 | /// @{ | ||||||||
326 | QualType TransformTypeWithDeducedTST(QualType T); | ||||||||
327 | TypeSourceInfo *TransformTypeWithDeducedTST(TypeSourceInfo *DI); | ||||||||
328 | /// @} | ||||||||
329 | |||||||||
330 | /// The reason why the value of a statement is not discarded, if any. | ||||||||
331 | enum StmtDiscardKind { | ||||||||
332 | SDK_Discarded, | ||||||||
333 | SDK_NotDiscarded, | ||||||||
334 | SDK_StmtExprResult, | ||||||||
335 | }; | ||||||||
336 | |||||||||
337 | /// Transform the given statement. | ||||||||
338 | /// | ||||||||
339 | /// By default, this routine transforms a statement by delegating to the | ||||||||
340 | /// appropriate TransformXXXStmt function to transform a specific kind of | ||||||||
341 | /// statement or the TransformExpr() function to transform an expression. | ||||||||
342 | /// Subclasses may override this function to transform statements using some | ||||||||
343 | /// other mechanism. | ||||||||
344 | /// | ||||||||
345 | /// \returns the transformed statement. | ||||||||
346 | StmtResult TransformStmt(Stmt *S, StmtDiscardKind SDK = SDK_Discarded); | ||||||||
347 | |||||||||
348 | /// Transform the given statement. | ||||||||
349 | /// | ||||||||
350 | /// By default, this routine transforms a statement by delegating to the | ||||||||
351 | /// appropriate TransformOMPXXXClause function to transform a specific kind | ||||||||
352 | /// of clause. Subclasses may override this function to transform statements | ||||||||
353 | /// using some other mechanism. | ||||||||
354 | /// | ||||||||
355 | /// \returns the transformed OpenMP clause. | ||||||||
356 | OMPClause *TransformOMPClause(OMPClause *S); | ||||||||
357 | |||||||||
358 | /// Transform the given attribute. | ||||||||
359 | /// | ||||||||
360 | /// By default, this routine transforms a statement by delegating to the | ||||||||
361 | /// appropriate TransformXXXAttr function to transform a specific kind | ||||||||
362 | /// of attribute. Subclasses may override this function to transform | ||||||||
363 | /// attributed statements using some other mechanism. | ||||||||
364 | /// | ||||||||
365 | /// \returns the transformed attribute | ||||||||
366 | const Attr *TransformAttr(const Attr *S); | ||||||||
367 | |||||||||
368 | /// Transform the specified attribute. | ||||||||
369 | /// | ||||||||
370 | /// Subclasses should override the transformation of attributes with a pragma | ||||||||
371 | /// spelling to transform expressions stored within the attribute. | ||||||||
372 | /// | ||||||||
373 | /// \returns the transformed attribute. | ||||||||
374 | #define ATTR(X) | ||||||||
375 | #define PRAGMA_SPELLING_ATTR(X) \ | ||||||||
376 | const X##Attr *Transform##X##Attr(const X##Attr *R) { return R; } | ||||||||
377 | #include "clang/Basic/AttrList.inc" | ||||||||
378 | |||||||||
379 | /// Transform the given expression. | ||||||||
380 | /// | ||||||||
381 | /// By default, this routine transforms an expression by delegating to the | ||||||||
382 | /// appropriate TransformXXXExpr function to build a new expression. | ||||||||
383 | /// Subclasses may override this function to transform expressions using some | ||||||||
384 | /// other mechanism. | ||||||||
385 | /// | ||||||||
386 | /// \returns the transformed expression. | ||||||||
387 | ExprResult TransformExpr(Expr *E); | ||||||||
388 | |||||||||
389 | /// Transform the given initializer. | ||||||||
390 | /// | ||||||||
391 | /// By default, this routine transforms an initializer by stripping off the | ||||||||
392 | /// semantic nodes added by initialization, then passing the result to | ||||||||
393 | /// TransformExpr or TransformExprs. | ||||||||
394 | /// | ||||||||
395 | /// \returns the transformed initializer. | ||||||||
396 | ExprResult TransformInitializer(Expr *Init, bool NotCopyInit); | ||||||||
397 | |||||||||
398 | /// Transform the given list of expressions. | ||||||||
399 | /// | ||||||||
400 | /// This routine transforms a list of expressions by invoking | ||||||||
401 | /// \c TransformExpr() for each subexpression. However, it also provides | ||||||||
402 | /// support for variadic templates by expanding any pack expansions (if the | ||||||||
403 | /// derived class permits such expansion) along the way. When pack expansions | ||||||||
404 | /// are present, the number of outputs may not equal the number of inputs. | ||||||||
405 | /// | ||||||||
406 | /// \param Inputs The set of expressions to be transformed. | ||||||||
407 | /// | ||||||||
408 | /// \param NumInputs The number of expressions in \c Inputs. | ||||||||
409 | /// | ||||||||
410 | /// \param IsCall If \c true, then this transform is being performed on | ||||||||
411 | /// function-call arguments, and any arguments that should be dropped, will | ||||||||
412 | /// be. | ||||||||
413 | /// | ||||||||
414 | /// \param Outputs The transformed input expressions will be added to this | ||||||||
415 | /// vector. | ||||||||
416 | /// | ||||||||
417 | /// \param ArgChanged If non-NULL, will be set \c true if any argument changed | ||||||||
418 | /// due to transformation. | ||||||||
419 | /// | ||||||||
420 | /// \returns true if an error occurred, false otherwise. | ||||||||
421 | bool TransformExprs(Expr *const *Inputs, unsigned NumInputs, bool IsCall, | ||||||||
422 | SmallVectorImpl<Expr *> &Outputs, | ||||||||
423 | bool *ArgChanged = nullptr); | ||||||||
424 | |||||||||
425 | /// Transform the given declaration, which is referenced from a type | ||||||||
426 | /// or expression. | ||||||||
427 | /// | ||||||||
428 | /// By default, acts as the identity function on declarations, unless the | ||||||||
429 | /// transformer has had to transform the declaration itself. Subclasses | ||||||||
430 | /// may override this function to provide alternate behavior. | ||||||||
431 | Decl *TransformDecl(SourceLocation Loc, Decl *D) { | ||||||||
432 | llvm::DenseMap<Decl *, Decl *>::iterator Known | ||||||||
433 | = TransformedLocalDecls.find(D); | ||||||||
434 | if (Known != TransformedLocalDecls.end()) | ||||||||
435 | return Known->second; | ||||||||
436 | |||||||||
437 | return D; | ||||||||
438 | } | ||||||||
439 | |||||||||
440 | /// Transform the specified condition. | ||||||||
441 | /// | ||||||||
442 | /// By default, this transforms the variable and expression and rebuilds | ||||||||
443 | /// the condition. | ||||||||
444 | Sema::ConditionResult TransformCondition(SourceLocation Loc, VarDecl *Var, | ||||||||
445 | Expr *Expr, | ||||||||
446 | Sema::ConditionKind Kind); | ||||||||
447 | |||||||||
448 | /// Transform the attributes associated with the given declaration and | ||||||||
449 | /// place them on the new declaration. | ||||||||
450 | /// | ||||||||
451 | /// By default, this operation does nothing. Subclasses may override this | ||||||||
452 | /// behavior to transform attributes. | ||||||||
453 | void transformAttrs(Decl *Old, Decl *New) { } | ||||||||
454 | |||||||||
455 | /// Note that a local declaration has been transformed by this | ||||||||
456 | /// transformer. | ||||||||
457 | /// | ||||||||
458 | /// Local declarations are typically transformed via a call to | ||||||||
459 | /// TransformDefinition. However, in some cases (e.g., lambda expressions), | ||||||||
460 | /// the transformer itself has to transform the declarations. This routine | ||||||||
461 | /// can be overridden by a subclass that keeps track of such mappings. | ||||||||
462 | void transformedLocalDecl(Decl *Old, ArrayRef<Decl *> New) { | ||||||||
463 | assert(New.size() == 1 &&((New.size() == 1 && "must override transformedLocalDecl if performing pack expansion" ) ? static_cast<void> (0) : __assert_fail ("New.size() == 1 && \"must override transformedLocalDecl if performing pack expansion\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 464, __PRETTY_FUNCTION__)) | ||||||||
464 | "must override transformedLocalDecl if performing pack expansion")((New.size() == 1 && "must override transformedLocalDecl if performing pack expansion" ) ? static_cast<void> (0) : __assert_fail ("New.size() == 1 && \"must override transformedLocalDecl if performing pack expansion\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 464, __PRETTY_FUNCTION__)); | ||||||||
465 | TransformedLocalDecls[Old] = New.front(); | ||||||||
466 | } | ||||||||
467 | |||||||||
468 | /// Transform the definition of the given declaration. | ||||||||
469 | /// | ||||||||
470 | /// By default, invokes TransformDecl() to transform the declaration. | ||||||||
471 | /// Subclasses may override this function to provide alternate behavior. | ||||||||
472 | Decl *TransformDefinition(SourceLocation Loc, Decl *D) { | ||||||||
473 | return getDerived().TransformDecl(Loc, D); | ||||||||
474 | } | ||||||||
475 | |||||||||
476 | /// Transform the given declaration, which was the first part of a | ||||||||
477 | /// nested-name-specifier in a member access expression. | ||||||||
478 | /// | ||||||||
479 | /// This specific declaration transformation only applies to the first | ||||||||
480 | /// identifier in a nested-name-specifier of a member access expression, e.g., | ||||||||
481 | /// the \c T in \c x->T::member | ||||||||
482 | /// | ||||||||
483 | /// By default, invokes TransformDecl() to transform the declaration. | ||||||||
484 | /// Subclasses may override this function to provide alternate behavior. | ||||||||
485 | NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc) { | ||||||||
486 | return cast_or_null<NamedDecl>(getDerived().TransformDecl(Loc, D)); | ||||||||
487 | } | ||||||||
488 | |||||||||
489 | /// Transform the set of declarations in an OverloadExpr. | ||||||||
490 | bool TransformOverloadExprDecls(OverloadExpr *Old, bool RequiresADL, | ||||||||
491 | LookupResult &R); | ||||||||
492 | |||||||||
493 | /// Transform the given nested-name-specifier with source-location | ||||||||
494 | /// information. | ||||||||
495 | /// | ||||||||
496 | /// By default, transforms all of the types and declarations within the | ||||||||
497 | /// nested-name-specifier. Subclasses may override this function to provide | ||||||||
498 | /// alternate behavior. | ||||||||
499 | NestedNameSpecifierLoc | ||||||||
500 | TransformNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, | ||||||||
501 | QualType ObjectType = QualType(), | ||||||||
502 | NamedDecl *FirstQualifierInScope = nullptr); | ||||||||
503 | |||||||||
504 | /// Transform the given declaration name. | ||||||||
505 | /// | ||||||||
506 | /// By default, transforms the types of conversion function, constructor, | ||||||||
507 | /// and destructor names and then (if needed) rebuilds the declaration name. | ||||||||
508 | /// Identifiers and selectors are returned unmodified. Sublcasses may | ||||||||
509 | /// override this function to provide alternate behavior. | ||||||||
510 | DeclarationNameInfo | ||||||||
511 | TransformDeclarationNameInfo(const DeclarationNameInfo &NameInfo); | ||||||||
512 | |||||||||
513 | bool TransformRequiresExprRequirements(ArrayRef<concepts::Requirement *> Reqs, | ||||||||
514 | llvm::SmallVectorImpl<concepts::Requirement *> &Transformed); | ||||||||
515 | concepts::TypeRequirement * | ||||||||
516 | TransformTypeRequirement(concepts::TypeRequirement *Req); | ||||||||
517 | concepts::ExprRequirement * | ||||||||
518 | TransformExprRequirement(concepts::ExprRequirement *Req); | ||||||||
519 | concepts::NestedRequirement * | ||||||||
520 | TransformNestedRequirement(concepts::NestedRequirement *Req); | ||||||||
521 | |||||||||
522 | /// Transform the given template name. | ||||||||
523 | /// | ||||||||
524 | /// \param SS The nested-name-specifier that qualifies the template | ||||||||
525 | /// name. This nested-name-specifier must already have been transformed. | ||||||||
526 | /// | ||||||||
527 | /// \param Name The template name to transform. | ||||||||
528 | /// | ||||||||
529 | /// \param NameLoc The source location of the template name. | ||||||||
530 | /// | ||||||||
531 | /// \param ObjectType If we're translating a template name within a member | ||||||||
532 | /// access expression, this is the type of the object whose member template | ||||||||
533 | /// is being referenced. | ||||||||
534 | /// | ||||||||
535 | /// \param FirstQualifierInScope If the first part of a nested-name-specifier | ||||||||
536 | /// also refers to a name within the current (lexical) scope, this is the | ||||||||
537 | /// declaration it refers to. | ||||||||
538 | /// | ||||||||
539 | /// By default, transforms the template name by transforming the declarations | ||||||||
540 | /// and nested-name-specifiers that occur within the template name. | ||||||||
541 | /// Subclasses may override this function to provide alternate behavior. | ||||||||
542 | TemplateName | ||||||||
543 | TransformTemplateName(CXXScopeSpec &SS, TemplateName Name, | ||||||||
544 | SourceLocation NameLoc, | ||||||||
545 | QualType ObjectType = QualType(), | ||||||||
546 | NamedDecl *FirstQualifierInScope = nullptr, | ||||||||
547 | bool AllowInjectedClassName = false); | ||||||||
548 | |||||||||
549 | /// Transform the given template argument. | ||||||||
550 | /// | ||||||||
551 | /// By default, this operation transforms the type, expression, or | ||||||||
552 | /// declaration stored within the template argument and constructs a | ||||||||
553 | /// new template argument from the transformed result. Subclasses may | ||||||||
554 | /// override this function to provide alternate behavior. | ||||||||
555 | /// | ||||||||
556 | /// Returns true if there was an error. | ||||||||
557 | bool TransformTemplateArgument(const TemplateArgumentLoc &Input, | ||||||||
558 | TemplateArgumentLoc &Output, | ||||||||
559 | bool Uneval = false); | ||||||||
560 | |||||||||
561 | /// Transform the given set of template arguments. | ||||||||
562 | /// | ||||||||
563 | /// By default, this operation transforms all of the template arguments | ||||||||
564 | /// in the input set using \c TransformTemplateArgument(), and appends | ||||||||
565 | /// the transformed arguments to the output list. | ||||||||
566 | /// | ||||||||
567 | /// Note that this overload of \c TransformTemplateArguments() is merely | ||||||||
568 | /// a convenience function. Subclasses that wish to override this behavior | ||||||||
569 | /// should override the iterator-based member template version. | ||||||||
570 | /// | ||||||||
571 | /// \param Inputs The set of template arguments to be transformed. | ||||||||
572 | /// | ||||||||
573 | /// \param NumInputs The number of template arguments in \p Inputs. | ||||||||
574 | /// | ||||||||
575 | /// \param Outputs The set of transformed template arguments output by this | ||||||||
576 | /// routine. | ||||||||
577 | /// | ||||||||
578 | /// Returns true if an error occurred. | ||||||||
579 | bool TransformTemplateArguments(const TemplateArgumentLoc *Inputs, | ||||||||
580 | unsigned NumInputs, | ||||||||
581 | TemplateArgumentListInfo &Outputs, | ||||||||
582 | bool Uneval = false) { | ||||||||
583 | return TransformTemplateArguments(Inputs, Inputs + NumInputs, Outputs, | ||||||||
584 | Uneval); | ||||||||
585 | } | ||||||||
586 | |||||||||
587 | /// Transform the given set of template arguments. | ||||||||
588 | /// | ||||||||
589 | /// By default, this operation transforms all of the template arguments | ||||||||
590 | /// in the input set using \c TransformTemplateArgument(), and appends | ||||||||
591 | /// the transformed arguments to the output list. | ||||||||
592 | /// | ||||||||
593 | /// \param First An iterator to the first template argument. | ||||||||
594 | /// | ||||||||
595 | /// \param Last An iterator one step past the last template argument. | ||||||||
596 | /// | ||||||||
597 | /// \param Outputs The set of transformed template arguments output by this | ||||||||
598 | /// routine. | ||||||||
599 | /// | ||||||||
600 | /// Returns true if an error occurred. | ||||||||
601 | template<typename InputIterator> | ||||||||
602 | bool TransformTemplateArguments(InputIterator First, | ||||||||
603 | InputIterator Last, | ||||||||
604 | TemplateArgumentListInfo &Outputs, | ||||||||
605 | bool Uneval = false); | ||||||||
606 | |||||||||
607 | /// Fakes up a TemplateArgumentLoc for a given TemplateArgument. | ||||||||
608 | void InventTemplateArgumentLoc(const TemplateArgument &Arg, | ||||||||
609 | TemplateArgumentLoc &ArgLoc); | ||||||||
610 | |||||||||
611 | /// Fakes up a TypeSourceInfo for a type. | ||||||||
612 | TypeSourceInfo *InventTypeSourceInfo(QualType T) { | ||||||||
613 | return SemaRef.Context.getTrivialTypeSourceInfo(T, | ||||||||
614 | getDerived().getBaseLocation()); | ||||||||
615 | } | ||||||||
616 | |||||||||
617 | #define ABSTRACT_TYPELOC(CLASS, PARENT) | ||||||||
618 | #define TYPELOC(CLASS, PARENT) \ | ||||||||
619 | QualType Transform##CLASS##Type(TypeLocBuilder &TLB, CLASS##TypeLoc T); | ||||||||
620 | #include "clang/AST/TypeLocNodes.def" | ||||||||
621 | |||||||||
622 | template<typename Fn> | ||||||||
623 | QualType TransformFunctionProtoType(TypeLocBuilder &TLB, | ||||||||
624 | FunctionProtoTypeLoc TL, | ||||||||
625 | CXXRecordDecl *ThisContext, | ||||||||
626 | Qualifiers ThisTypeQuals, | ||||||||
627 | Fn TransformExceptionSpec); | ||||||||
628 | |||||||||
629 | bool TransformExceptionSpec(SourceLocation Loc, | ||||||||
630 | FunctionProtoType::ExceptionSpecInfo &ESI, | ||||||||
631 | SmallVectorImpl<QualType> &Exceptions, | ||||||||
632 | bool &Changed); | ||||||||
633 | |||||||||
634 | StmtResult TransformSEHHandler(Stmt *Handler); | ||||||||
635 | |||||||||
636 | QualType | ||||||||
637 | TransformTemplateSpecializationType(TypeLocBuilder &TLB, | ||||||||
638 | TemplateSpecializationTypeLoc TL, | ||||||||
639 | TemplateName Template); | ||||||||
640 | |||||||||
641 | QualType | ||||||||
642 | TransformDependentTemplateSpecializationType(TypeLocBuilder &TLB, | ||||||||
643 | DependentTemplateSpecializationTypeLoc TL, | ||||||||
644 | TemplateName Template, | ||||||||
645 | CXXScopeSpec &SS); | ||||||||
646 | |||||||||
647 | QualType TransformDependentTemplateSpecializationType( | ||||||||
648 | TypeLocBuilder &TLB, DependentTemplateSpecializationTypeLoc TL, | ||||||||
649 | NestedNameSpecifierLoc QualifierLoc); | ||||||||
650 | |||||||||
651 | /// Transforms the parameters of a function type into the | ||||||||
652 | /// given vectors. | ||||||||
653 | /// | ||||||||
654 | /// The result vectors should be kept in sync; null entries in the | ||||||||
655 | /// variables vector are acceptable. | ||||||||
656 | /// | ||||||||
657 | /// Return true on error. | ||||||||
658 | bool TransformFunctionTypeParams( | ||||||||
659 | SourceLocation Loc, ArrayRef<ParmVarDecl *> Params, | ||||||||
660 | const QualType *ParamTypes, | ||||||||
661 | const FunctionProtoType::ExtParameterInfo *ParamInfos, | ||||||||
662 | SmallVectorImpl<QualType> &PTypes, SmallVectorImpl<ParmVarDecl *> *PVars, | ||||||||
663 | Sema::ExtParameterInfoBuilder &PInfos); | ||||||||
664 | |||||||||
665 | /// Transforms a single function-type parameter. Return null | ||||||||
666 | /// on error. | ||||||||
667 | /// | ||||||||
668 | /// \param indexAdjustment - A number to add to the parameter's | ||||||||
669 | /// scope index; can be negative | ||||||||
670 | ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm, | ||||||||
671 | int indexAdjustment, | ||||||||
672 | Optional<unsigned> NumExpansions, | ||||||||
673 | bool ExpectParameterPack); | ||||||||
674 | |||||||||
675 | /// Transform the body of a lambda-expression. | ||||||||
676 | StmtResult TransformLambdaBody(LambdaExpr *E, Stmt *Body); | ||||||||
677 | /// Alternative implementation of TransformLambdaBody that skips transforming | ||||||||
678 | /// the body. | ||||||||
679 | StmtResult SkipLambdaBody(LambdaExpr *E, Stmt *Body); | ||||||||
680 | |||||||||
681 | QualType TransformReferenceType(TypeLocBuilder &TLB, ReferenceTypeLoc TL); | ||||||||
682 | |||||||||
683 | StmtResult TransformCompoundStmt(CompoundStmt *S, bool IsStmtExpr); | ||||||||
684 | ExprResult TransformCXXNamedCastExpr(CXXNamedCastExpr *E); | ||||||||
685 | |||||||||
686 | TemplateParameterList *TransformTemplateParameterList( | ||||||||
687 | TemplateParameterList *TPL) { | ||||||||
688 | return TPL; | ||||||||
689 | } | ||||||||
690 | |||||||||
691 | ExprResult TransformAddressOfOperand(Expr *E); | ||||||||
692 | |||||||||
693 | ExprResult TransformDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E, | ||||||||
694 | bool IsAddressOfOperand, | ||||||||
695 | TypeSourceInfo **RecoveryTSI); | ||||||||
696 | |||||||||
697 | ExprResult TransformParenDependentScopeDeclRefExpr( | ||||||||
698 | ParenExpr *PE, DependentScopeDeclRefExpr *DRE, bool IsAddressOfOperand, | ||||||||
699 | TypeSourceInfo **RecoveryTSI); | ||||||||
700 | |||||||||
701 | StmtResult TransformOMPExecutableDirective(OMPExecutableDirective *S); | ||||||||
702 | |||||||||
703 | // FIXME: We use LLVM_ATTRIBUTE_NOINLINE because inlining causes a ridiculous | ||||||||
704 | // amount of stack usage with clang. | ||||||||
705 | #define STMT(Node, Parent) \ | ||||||||
706 | LLVM_ATTRIBUTE_NOINLINE__attribute__((noinline)) \ | ||||||||
707 | StmtResult Transform##Node(Node *S); | ||||||||
708 | #define VALUESTMT(Node, Parent) \ | ||||||||
709 | LLVM_ATTRIBUTE_NOINLINE__attribute__((noinline)) \ | ||||||||
710 | StmtResult Transform##Node(Node *S, StmtDiscardKind SDK); | ||||||||
711 | #define EXPR(Node, Parent) \ | ||||||||
712 | LLVM_ATTRIBUTE_NOINLINE__attribute__((noinline)) \ | ||||||||
713 | ExprResult Transform##Node(Node *E); | ||||||||
714 | #define ABSTRACT_STMT(Stmt) | ||||||||
715 | #include "clang/AST/StmtNodes.inc" | ||||||||
716 | |||||||||
717 | #define OPENMP_CLAUSE(Name, Class) \ | ||||||||
718 | LLVM_ATTRIBUTE_NOINLINE__attribute__((noinline)) \ | ||||||||
719 | OMPClause *Transform ## Class(Class *S); | ||||||||
720 | #include "clang/Basic/OpenMPKinds.def" | ||||||||
721 | |||||||||
722 | /// Build a new qualified type given its unqualified type and type location. | ||||||||
723 | /// | ||||||||
724 | /// By default, this routine adds type qualifiers only to types that can | ||||||||
725 | /// have qualifiers, and silently suppresses those qualifiers that are not | ||||||||
726 | /// permitted. Subclasses may override this routine to provide different | ||||||||
727 | /// behavior. | ||||||||
728 | QualType RebuildQualifiedType(QualType T, QualifiedTypeLoc TL); | ||||||||
729 | |||||||||
730 | /// Build a new pointer type given its pointee type. | ||||||||
731 | /// | ||||||||
732 | /// By default, performs semantic analysis when building the pointer type. | ||||||||
733 | /// Subclasses may override this routine to provide different behavior. | ||||||||
734 | QualType RebuildPointerType(QualType PointeeType, SourceLocation Sigil); | ||||||||
735 | |||||||||
736 | /// Build a new block pointer type given its pointee type. | ||||||||
737 | /// | ||||||||
738 | /// By default, performs semantic analysis when building the block pointer | ||||||||
739 | /// type. Subclasses may override this routine to provide different behavior. | ||||||||
740 | QualType RebuildBlockPointerType(QualType PointeeType, SourceLocation Sigil); | ||||||||
741 | |||||||||
742 | /// Build a new reference type given the type it references. | ||||||||
743 | /// | ||||||||
744 | /// By default, performs semantic analysis when building the | ||||||||
745 | /// reference type. Subclasses may override this routine to provide | ||||||||
746 | /// different behavior. | ||||||||
747 | /// | ||||||||
748 | /// \param LValue whether the type was written with an lvalue sigil | ||||||||
749 | /// or an rvalue sigil. | ||||||||
750 | QualType RebuildReferenceType(QualType ReferentType, | ||||||||
751 | bool LValue, | ||||||||
752 | SourceLocation Sigil); | ||||||||
753 | |||||||||
754 | /// Build a new member pointer type given the pointee type and the | ||||||||
755 | /// class type it refers into. | ||||||||
756 | /// | ||||||||
757 | /// By default, performs semantic analysis when building the member pointer | ||||||||
758 | /// type. Subclasses may override this routine to provide different behavior. | ||||||||
759 | QualType RebuildMemberPointerType(QualType PointeeType, QualType ClassType, | ||||||||
760 | SourceLocation Sigil); | ||||||||
761 | |||||||||
762 | QualType RebuildObjCTypeParamType(const ObjCTypeParamDecl *Decl, | ||||||||
763 | SourceLocation ProtocolLAngleLoc, | ||||||||
764 | ArrayRef<ObjCProtocolDecl *> Protocols, | ||||||||
765 | ArrayRef<SourceLocation> ProtocolLocs, | ||||||||
766 | SourceLocation ProtocolRAngleLoc); | ||||||||
767 | |||||||||
768 | /// Build an Objective-C object type. | ||||||||
769 | /// | ||||||||
770 | /// By default, performs semantic analysis when building the object type. | ||||||||
771 | /// Subclasses may override this routine to provide different behavior. | ||||||||
772 | QualType RebuildObjCObjectType(QualType BaseType, | ||||||||
773 | SourceLocation Loc, | ||||||||
774 | SourceLocation TypeArgsLAngleLoc, | ||||||||
775 | ArrayRef<TypeSourceInfo *> TypeArgs, | ||||||||
776 | SourceLocation TypeArgsRAngleLoc, | ||||||||
777 | SourceLocation ProtocolLAngleLoc, | ||||||||
778 | ArrayRef<ObjCProtocolDecl *> Protocols, | ||||||||
779 | ArrayRef<SourceLocation> ProtocolLocs, | ||||||||
780 | SourceLocation ProtocolRAngleLoc); | ||||||||
781 | |||||||||
782 | /// Build a new Objective-C object pointer type given the pointee type. | ||||||||
783 | /// | ||||||||
784 | /// By default, directly builds the pointer type, with no additional semantic | ||||||||
785 | /// analysis. | ||||||||
786 | QualType RebuildObjCObjectPointerType(QualType PointeeType, | ||||||||
787 | SourceLocation Star); | ||||||||
788 | |||||||||
789 | /// Build a new array type given the element type, size | ||||||||
790 | /// modifier, size of the array (if known), size expression, and index type | ||||||||
791 | /// qualifiers. | ||||||||
792 | /// | ||||||||
793 | /// By default, performs semantic analysis when building the array type. | ||||||||
794 | /// Subclasses may override this routine to provide different behavior. | ||||||||
795 | /// Also by default, all of the other Rebuild*Array | ||||||||
796 | QualType RebuildArrayType(QualType ElementType, | ||||||||
797 | ArrayType::ArraySizeModifier SizeMod, | ||||||||
798 | const llvm::APInt *Size, | ||||||||
799 | Expr *SizeExpr, | ||||||||
800 | unsigned IndexTypeQuals, | ||||||||
801 | SourceRange BracketsRange); | ||||||||
802 | |||||||||
803 | /// Build a new constant array type given the element type, size | ||||||||
804 | /// modifier, (known) size of the array, and index type qualifiers. | ||||||||
805 | /// | ||||||||
806 | /// By default, performs semantic analysis when building the array type. | ||||||||
807 | /// Subclasses may override this routine to provide different behavior. | ||||||||
808 | QualType RebuildConstantArrayType(QualType ElementType, | ||||||||
809 | ArrayType::ArraySizeModifier SizeMod, | ||||||||
810 | const llvm::APInt &Size, | ||||||||
811 | Expr *SizeExpr, | ||||||||
812 | unsigned IndexTypeQuals, | ||||||||
813 | SourceRange BracketsRange); | ||||||||
814 | |||||||||
815 | /// Build a new incomplete array type given the element type, size | ||||||||
816 | /// modifier, and index type qualifiers. | ||||||||
817 | /// | ||||||||
818 | /// By default, performs semantic analysis when building the array type. | ||||||||
819 | /// Subclasses may override this routine to provide different behavior. | ||||||||
820 | QualType RebuildIncompleteArrayType(QualType ElementType, | ||||||||
821 | ArrayType::ArraySizeModifier SizeMod, | ||||||||
822 | unsigned IndexTypeQuals, | ||||||||
823 | SourceRange BracketsRange); | ||||||||
824 | |||||||||
825 | /// Build a new variable-length array type given the element type, | ||||||||
826 | /// size modifier, size expression, and index type qualifiers. | ||||||||
827 | /// | ||||||||
828 | /// By default, performs semantic analysis when building the array type. | ||||||||
829 | /// Subclasses may override this routine to provide different behavior. | ||||||||
830 | QualType RebuildVariableArrayType(QualType ElementType, | ||||||||
831 | ArrayType::ArraySizeModifier SizeMod, | ||||||||
832 | Expr *SizeExpr, | ||||||||
833 | unsigned IndexTypeQuals, | ||||||||
834 | SourceRange BracketsRange); | ||||||||
835 | |||||||||
836 | /// Build a new dependent-sized array type given the element type, | ||||||||
837 | /// size modifier, size expression, and index type qualifiers. | ||||||||
838 | /// | ||||||||
839 | /// By default, performs semantic analysis when building the array type. | ||||||||
840 | /// Subclasses may override this routine to provide different behavior. | ||||||||
841 | QualType RebuildDependentSizedArrayType(QualType ElementType, | ||||||||
842 | ArrayType::ArraySizeModifier SizeMod, | ||||||||
843 | Expr *SizeExpr, | ||||||||
844 | unsigned IndexTypeQuals, | ||||||||
845 | SourceRange BracketsRange); | ||||||||
846 | |||||||||
847 | /// Build a new vector type given the element type and | ||||||||
848 | /// number of elements. | ||||||||
849 | /// | ||||||||
850 | /// By default, performs semantic analysis when building the vector type. | ||||||||
851 | /// Subclasses may override this routine to provide different behavior. | ||||||||
852 | QualType RebuildVectorType(QualType ElementType, unsigned NumElements, | ||||||||
853 | VectorType::VectorKind VecKind); | ||||||||
854 | |||||||||
855 | /// Build a new potentially dependently-sized extended vector type | ||||||||
856 | /// given the element type and number of elements. | ||||||||
857 | /// | ||||||||
858 | /// By default, performs semantic analysis when building the vector type. | ||||||||
859 | /// Subclasses may override this routine to provide different behavior. | ||||||||
860 | QualType RebuildDependentVectorType(QualType ElementType, Expr *SizeExpr, | ||||||||
861 | SourceLocation AttributeLoc, | ||||||||
862 | VectorType::VectorKind); | ||||||||
863 | |||||||||
864 | /// Build a new extended vector type given the element type and | ||||||||
865 | /// number of elements. | ||||||||
866 | /// | ||||||||
867 | /// By default, performs semantic analysis when building the vector type. | ||||||||
868 | /// Subclasses may override this routine to provide different behavior. | ||||||||
869 | QualType RebuildExtVectorType(QualType ElementType, unsigned NumElements, | ||||||||
870 | SourceLocation AttributeLoc); | ||||||||
871 | |||||||||
872 | /// Build a new potentially dependently-sized extended vector type | ||||||||
873 | /// given the element type and number of elements. | ||||||||
874 | /// | ||||||||
875 | /// By default, performs semantic analysis when building the vector type. | ||||||||
876 | /// Subclasses may override this routine to provide different behavior. | ||||||||
877 | QualType RebuildDependentSizedExtVectorType(QualType ElementType, | ||||||||
878 | Expr *SizeExpr, | ||||||||
879 | SourceLocation AttributeLoc); | ||||||||
880 | |||||||||
881 | /// Build a new DependentAddressSpaceType or return the pointee | ||||||||
882 | /// type variable with the correct address space (retrieved from | ||||||||
883 | /// AddrSpaceExpr) applied to it. The former will be returned in cases | ||||||||
884 | /// where the address space remains dependent. | ||||||||
885 | /// | ||||||||
886 | /// By default, performs semantic analysis when building the type with address | ||||||||
887 | /// space applied. Subclasses may override this routine to provide different | ||||||||
888 | /// behavior. | ||||||||
889 | QualType RebuildDependentAddressSpaceType(QualType PointeeType, | ||||||||
890 | Expr *AddrSpaceExpr, | ||||||||
891 | SourceLocation AttributeLoc); | ||||||||
892 | |||||||||
893 | /// Build a new function type. | ||||||||
894 | /// | ||||||||
895 | /// By default, performs semantic analysis when building the function type. | ||||||||
896 | /// Subclasses may override this routine to provide different behavior. | ||||||||
897 | QualType RebuildFunctionProtoType(QualType T, | ||||||||
898 | MutableArrayRef<QualType> ParamTypes, | ||||||||
899 | const FunctionProtoType::ExtProtoInfo &EPI); | ||||||||
900 | |||||||||
901 | /// Build a new unprototyped function type. | ||||||||
902 | QualType RebuildFunctionNoProtoType(QualType ResultType); | ||||||||
903 | |||||||||
904 | /// Rebuild an unresolved typename type, given the decl that | ||||||||
905 | /// the UnresolvedUsingTypenameDecl was transformed to. | ||||||||
906 | QualType RebuildUnresolvedUsingType(SourceLocation NameLoc, Decl *D); | ||||||||
907 | |||||||||
908 | /// Build a new typedef type. | ||||||||
909 | QualType RebuildTypedefType(TypedefNameDecl *Typedef) { | ||||||||
910 | return SemaRef.Context.getTypeDeclType(Typedef); | ||||||||
911 | } | ||||||||
912 | |||||||||
913 | /// Build a new MacroDefined type. | ||||||||
914 | QualType RebuildMacroQualifiedType(QualType T, | ||||||||
915 | const IdentifierInfo *MacroII) { | ||||||||
916 | return SemaRef.Context.getMacroQualifiedType(T, MacroII); | ||||||||
917 | } | ||||||||
918 | |||||||||
919 | /// Build a new class/struct/union type. | ||||||||
920 | QualType RebuildRecordType(RecordDecl *Record) { | ||||||||
921 | return SemaRef.Context.getTypeDeclType(Record); | ||||||||
922 | } | ||||||||
923 | |||||||||
924 | /// Build a new Enum type. | ||||||||
925 | QualType RebuildEnumType(EnumDecl *Enum) { | ||||||||
926 | return SemaRef.Context.getTypeDeclType(Enum); | ||||||||
927 | } | ||||||||
928 | |||||||||
929 | /// Build a new typeof(expr) type. | ||||||||
930 | /// | ||||||||
931 | /// By default, performs semantic analysis when building the typeof type. | ||||||||
932 | /// Subclasses may override this routine to provide different behavior. | ||||||||
933 | QualType RebuildTypeOfExprType(Expr *Underlying, SourceLocation Loc); | ||||||||
934 | |||||||||
935 | /// Build a new typeof(type) type. | ||||||||
936 | /// | ||||||||
937 | /// By default, builds a new TypeOfType with the given underlying type. | ||||||||
938 | QualType RebuildTypeOfType(QualType Underlying); | ||||||||
939 | |||||||||
940 | /// Build a new unary transform type. | ||||||||
941 | QualType RebuildUnaryTransformType(QualType BaseType, | ||||||||
942 | UnaryTransformType::UTTKind UKind, | ||||||||
943 | SourceLocation Loc); | ||||||||
944 | |||||||||
945 | /// Build a new C++11 decltype type. | ||||||||
946 | /// | ||||||||
947 | /// By default, performs semantic analysis when building the decltype type. | ||||||||
948 | /// Subclasses may override this routine to provide different behavior. | ||||||||
949 | QualType RebuildDecltypeType(Expr *Underlying, SourceLocation Loc); | ||||||||
950 | |||||||||
951 | /// Build a new C++11 auto type. | ||||||||
952 | /// | ||||||||
953 | /// By default, builds a new AutoType with the given deduced type. | ||||||||
954 | QualType RebuildAutoType(QualType Deduced, AutoTypeKeyword Keyword, | ||||||||
955 | ConceptDecl *TypeConstraintConcept, | ||||||||
956 | ArrayRef<TemplateArgument> TypeConstraintArgs) { | ||||||||
957 | // Note, IsDependent is always false here: we implicitly convert an 'auto' | ||||||||
958 | // which has been deduced to a dependent type into an undeduced 'auto', so | ||||||||
959 | // that we'll retry deduction after the transformation. | ||||||||
960 | return SemaRef.Context.getAutoType(Deduced, Keyword, | ||||||||
961 | /*IsDependent*/ false, /*IsPack=*/false, | ||||||||
962 | TypeConstraintConcept, | ||||||||
963 | TypeConstraintArgs); | ||||||||
964 | } | ||||||||
965 | |||||||||
966 | /// By default, builds a new DeducedTemplateSpecializationType with the given | ||||||||
967 | /// deduced type. | ||||||||
968 | QualType RebuildDeducedTemplateSpecializationType(TemplateName Template, | ||||||||
969 | QualType Deduced) { | ||||||||
970 | return SemaRef.Context.getDeducedTemplateSpecializationType( | ||||||||
971 | Template, Deduced, /*IsDependent*/ false); | ||||||||
972 | } | ||||||||
973 | |||||||||
974 | /// Build a new template specialization type. | ||||||||
975 | /// | ||||||||
976 | /// By default, performs semantic analysis when building the template | ||||||||
977 | /// specialization type. Subclasses may override this routine to provide | ||||||||
978 | /// different behavior. | ||||||||
979 | QualType RebuildTemplateSpecializationType(TemplateName Template, | ||||||||
980 | SourceLocation TemplateLoc, | ||||||||
981 | TemplateArgumentListInfo &Args); | ||||||||
982 | |||||||||
983 | /// Build a new parenthesized type. | ||||||||
984 | /// | ||||||||
985 | /// By default, builds a new ParenType type from the inner type. | ||||||||
986 | /// Subclasses may override this routine to provide different behavior. | ||||||||
987 | QualType RebuildParenType(QualType InnerType) { | ||||||||
988 | return SemaRef.BuildParenType(InnerType); | ||||||||
989 | } | ||||||||
990 | |||||||||
991 | /// Build a new qualified name type. | ||||||||
992 | /// | ||||||||
993 | /// By default, builds a new ElaboratedType type from the keyword, | ||||||||
994 | /// the nested-name-specifier and the named type. | ||||||||
995 | /// Subclasses may override this routine to provide different behavior. | ||||||||
996 | QualType RebuildElaboratedType(SourceLocation KeywordLoc, | ||||||||
997 | ElaboratedTypeKeyword Keyword, | ||||||||
998 | NestedNameSpecifierLoc QualifierLoc, | ||||||||
999 | QualType Named) { | ||||||||
1000 | return SemaRef.Context.getElaboratedType(Keyword, | ||||||||
1001 | QualifierLoc.getNestedNameSpecifier(), | ||||||||
1002 | Named); | ||||||||
1003 | } | ||||||||
1004 | |||||||||
1005 | /// Build a new typename type that refers to a template-id. | ||||||||
1006 | /// | ||||||||
1007 | /// By default, builds a new DependentNameType type from the | ||||||||
1008 | /// nested-name-specifier and the given type. Subclasses may override | ||||||||
1009 | /// this routine to provide different behavior. | ||||||||
1010 | QualType RebuildDependentTemplateSpecializationType( | ||||||||
1011 | ElaboratedTypeKeyword Keyword, | ||||||||
1012 | NestedNameSpecifierLoc QualifierLoc, | ||||||||
1013 | SourceLocation TemplateKWLoc, | ||||||||
1014 | const IdentifierInfo *Name, | ||||||||
1015 | SourceLocation NameLoc, | ||||||||
1016 | TemplateArgumentListInfo &Args, | ||||||||
1017 | bool AllowInjectedClassName) { | ||||||||
1018 | // Rebuild the template name. | ||||||||
1019 | // TODO: avoid TemplateName abstraction | ||||||||
1020 | CXXScopeSpec SS; | ||||||||
1021 | SS.Adopt(QualifierLoc); | ||||||||
1022 | TemplateName InstName = getDerived().RebuildTemplateName( | ||||||||
1023 | SS, TemplateKWLoc, *Name, NameLoc, QualType(), nullptr, | ||||||||
1024 | AllowInjectedClassName); | ||||||||
1025 | |||||||||
1026 | if (InstName.isNull()) | ||||||||
1027 | return QualType(); | ||||||||
1028 | |||||||||
1029 | // If it's still dependent, make a dependent specialization. | ||||||||
1030 | if (InstName.getAsDependentTemplateName()) | ||||||||
1031 | return SemaRef.Context.getDependentTemplateSpecializationType(Keyword, | ||||||||
1032 | QualifierLoc.getNestedNameSpecifier(), | ||||||||
1033 | Name, | ||||||||
1034 | Args); | ||||||||
1035 | |||||||||
1036 | // Otherwise, make an elaborated type wrapping a non-dependent | ||||||||
1037 | // specialization. | ||||||||
1038 | QualType T = | ||||||||
1039 | getDerived().RebuildTemplateSpecializationType(InstName, NameLoc, Args); | ||||||||
1040 | if (T.isNull()) return QualType(); | ||||||||
1041 | |||||||||
1042 | if (Keyword == ETK_None && QualifierLoc.getNestedNameSpecifier() == nullptr) | ||||||||
1043 | return T; | ||||||||
1044 | |||||||||
1045 | return SemaRef.Context.getElaboratedType(Keyword, | ||||||||
1046 | QualifierLoc.getNestedNameSpecifier(), | ||||||||
1047 | T); | ||||||||
1048 | } | ||||||||
1049 | |||||||||
1050 | /// Build a new typename type that refers to an identifier. | ||||||||
1051 | /// | ||||||||
1052 | /// By default, performs semantic analysis when building the typename type | ||||||||
1053 | /// (or elaborated type). Subclasses may override this routine to provide | ||||||||
1054 | /// different behavior. | ||||||||
1055 | QualType RebuildDependentNameType(ElaboratedTypeKeyword Keyword, | ||||||||
1056 | SourceLocation KeywordLoc, | ||||||||
1057 | NestedNameSpecifierLoc QualifierLoc, | ||||||||
1058 | const IdentifierInfo *Id, | ||||||||
1059 | SourceLocation IdLoc, | ||||||||
1060 | bool DeducedTSTContext) { | ||||||||
1061 | CXXScopeSpec SS; | ||||||||
1062 | SS.Adopt(QualifierLoc); | ||||||||
1063 | |||||||||
1064 | if (QualifierLoc.getNestedNameSpecifier()->isDependent()) { | ||||||||
1065 | // If the name is still dependent, just build a new dependent name type. | ||||||||
1066 | if (!SemaRef.computeDeclContext(SS)) | ||||||||
1067 | return SemaRef.Context.getDependentNameType(Keyword, | ||||||||
1068 | QualifierLoc.getNestedNameSpecifier(), | ||||||||
1069 | Id); | ||||||||
1070 | } | ||||||||
1071 | |||||||||
1072 | if (Keyword == ETK_None || Keyword == ETK_Typename) { | ||||||||
1073 | return SemaRef.CheckTypenameType(Keyword, KeywordLoc, QualifierLoc, | ||||||||
1074 | *Id, IdLoc, DeducedTSTContext); | ||||||||
1075 | } | ||||||||
1076 | |||||||||
1077 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword); | ||||||||
1078 | |||||||||
1079 | // We had a dependent elaborated-type-specifier that has been transformed | ||||||||
1080 | // into a non-dependent elaborated-type-specifier. Find the tag we're | ||||||||
1081 | // referring to. | ||||||||
1082 | LookupResult Result(SemaRef, Id, IdLoc, Sema::LookupTagName); | ||||||||
1083 | DeclContext *DC = SemaRef.computeDeclContext(SS, false); | ||||||||
1084 | if (!DC) | ||||||||
1085 | return QualType(); | ||||||||
1086 | |||||||||
1087 | if (SemaRef.RequireCompleteDeclContext(SS, DC)) | ||||||||
1088 | return QualType(); | ||||||||
1089 | |||||||||
1090 | TagDecl *Tag = nullptr; | ||||||||
1091 | SemaRef.LookupQualifiedName(Result, DC); | ||||||||
1092 | switch (Result.getResultKind()) { | ||||||||
1093 | case LookupResult::NotFound: | ||||||||
1094 | case LookupResult::NotFoundInCurrentInstantiation: | ||||||||
1095 | break; | ||||||||
1096 | |||||||||
1097 | case LookupResult::Found: | ||||||||
1098 | Tag = Result.getAsSingle<TagDecl>(); | ||||||||
1099 | break; | ||||||||
1100 | |||||||||
1101 | case LookupResult::FoundOverloaded: | ||||||||
1102 | case LookupResult::FoundUnresolvedValue: | ||||||||
1103 | llvm_unreachable("Tag lookup cannot find non-tags")::llvm::llvm_unreachable_internal("Tag lookup cannot find non-tags" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 1103); | ||||||||
1104 | |||||||||
1105 | case LookupResult::Ambiguous: | ||||||||
1106 | // Let the LookupResult structure handle ambiguities. | ||||||||
1107 | return QualType(); | ||||||||
1108 | } | ||||||||
1109 | |||||||||
1110 | if (!Tag) { | ||||||||
1111 | // Check where the name exists but isn't a tag type and use that to emit | ||||||||
1112 | // better diagnostics. | ||||||||
1113 | LookupResult Result(SemaRef, Id, IdLoc, Sema::LookupTagName); | ||||||||
1114 | SemaRef.LookupQualifiedName(Result, DC); | ||||||||
1115 | switch (Result.getResultKind()) { | ||||||||
1116 | case LookupResult::Found: | ||||||||
1117 | case LookupResult::FoundOverloaded: | ||||||||
1118 | case LookupResult::FoundUnresolvedValue: { | ||||||||
1119 | NamedDecl *SomeDecl = Result.getRepresentativeDecl(); | ||||||||
1120 | Sema::NonTagKind NTK = SemaRef.getNonTagTypeDeclKind(SomeDecl, Kind); | ||||||||
1121 | SemaRef.Diag(IdLoc, diag::err_tag_reference_non_tag) << SomeDecl | ||||||||
1122 | << NTK << Kind; | ||||||||
1123 | SemaRef.Diag(SomeDecl->getLocation(), diag::note_declared_at); | ||||||||
1124 | break; | ||||||||
1125 | } | ||||||||
1126 | default: | ||||||||
1127 | SemaRef.Diag(IdLoc, diag::err_not_tag_in_scope) | ||||||||
1128 | << Kind << Id << DC << QualifierLoc.getSourceRange(); | ||||||||
1129 | break; | ||||||||
1130 | } | ||||||||
1131 | return QualType(); | ||||||||
1132 | } | ||||||||
1133 | |||||||||
1134 | if (!SemaRef.isAcceptableTagRedeclaration(Tag, Kind, /*isDefinition*/false, | ||||||||
1135 | IdLoc, Id)) { | ||||||||
1136 | SemaRef.Diag(KeywordLoc, diag::err_use_with_wrong_tag) << Id; | ||||||||
1137 | SemaRef.Diag(Tag->getLocation(), diag::note_previous_use); | ||||||||
1138 | return QualType(); | ||||||||
1139 | } | ||||||||
1140 | |||||||||
1141 | // Build the elaborated-type-specifier type. | ||||||||
1142 | QualType T = SemaRef.Context.getTypeDeclType(Tag); | ||||||||
1143 | return SemaRef.Context.getElaboratedType(Keyword, | ||||||||
1144 | QualifierLoc.getNestedNameSpecifier(), | ||||||||
1145 | T); | ||||||||
1146 | } | ||||||||
1147 | |||||||||
1148 | /// Build a new pack expansion type. | ||||||||
1149 | /// | ||||||||
1150 | /// By default, builds a new PackExpansionType type from the given pattern. | ||||||||
1151 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1152 | QualType RebuildPackExpansionType(QualType Pattern, | ||||||||
1153 | SourceRange PatternRange, | ||||||||
1154 | SourceLocation EllipsisLoc, | ||||||||
1155 | Optional<unsigned> NumExpansions) { | ||||||||
1156 | return getSema().CheckPackExpansion(Pattern, PatternRange, EllipsisLoc, | ||||||||
1157 | NumExpansions); | ||||||||
1158 | } | ||||||||
1159 | |||||||||
1160 | /// Build a new atomic type given its value type. | ||||||||
1161 | /// | ||||||||
1162 | /// By default, performs semantic analysis when building the atomic type. | ||||||||
1163 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1164 | QualType RebuildAtomicType(QualType ValueType, SourceLocation KWLoc); | ||||||||
1165 | |||||||||
1166 | /// Build a new pipe type given its value type. | ||||||||
1167 | QualType RebuildPipeType(QualType ValueType, SourceLocation KWLoc, | ||||||||
1168 | bool isReadPipe); | ||||||||
1169 | |||||||||
1170 | /// Build a new template name given a nested name specifier, a flag | ||||||||
1171 | /// indicating whether the "template" keyword was provided, and the template | ||||||||
1172 | /// that the template name refers to. | ||||||||
1173 | /// | ||||||||
1174 | /// By default, builds the new template name directly. Subclasses may override | ||||||||
1175 | /// this routine to provide different behavior. | ||||||||
1176 | TemplateName RebuildTemplateName(CXXScopeSpec &SS, | ||||||||
1177 | bool TemplateKW, | ||||||||
1178 | TemplateDecl *Template); | ||||||||
1179 | |||||||||
1180 | /// Build a new template name given a nested name specifier and the | ||||||||
1181 | /// name that is referred to as a template. | ||||||||
1182 | /// | ||||||||
1183 | /// By default, performs semantic analysis to determine whether the name can | ||||||||
1184 | /// be resolved to a specific template, then builds the appropriate kind of | ||||||||
1185 | /// template name. Subclasses may override this routine to provide different | ||||||||
1186 | /// behavior. | ||||||||
1187 | TemplateName RebuildTemplateName(CXXScopeSpec &SS, | ||||||||
1188 | SourceLocation TemplateKWLoc, | ||||||||
1189 | const IdentifierInfo &Name, | ||||||||
1190 | SourceLocation NameLoc, QualType ObjectType, | ||||||||
1191 | NamedDecl *FirstQualifierInScope, | ||||||||
1192 | bool AllowInjectedClassName); | ||||||||
1193 | |||||||||
1194 | /// Build a new template name given a nested name specifier and the | ||||||||
1195 | /// overloaded operator name that is referred to as a template. | ||||||||
1196 | /// | ||||||||
1197 | /// By default, performs semantic analysis to determine whether the name can | ||||||||
1198 | /// be resolved to a specific template, then builds the appropriate kind of | ||||||||
1199 | /// template name. Subclasses may override this routine to provide different | ||||||||
1200 | /// behavior. | ||||||||
1201 | TemplateName RebuildTemplateName(CXXScopeSpec &SS, | ||||||||
1202 | SourceLocation TemplateKWLoc, | ||||||||
1203 | OverloadedOperatorKind Operator, | ||||||||
1204 | SourceLocation NameLoc, QualType ObjectType, | ||||||||
1205 | bool AllowInjectedClassName); | ||||||||
1206 | |||||||||
1207 | /// Build a new template name given a template template parameter pack | ||||||||
1208 | /// and the | ||||||||
1209 | /// | ||||||||
1210 | /// By default, performs semantic analysis to determine whether the name can | ||||||||
1211 | /// be resolved to a specific template, then builds the appropriate kind of | ||||||||
1212 | /// template name. Subclasses may override this routine to provide different | ||||||||
1213 | /// behavior. | ||||||||
1214 | TemplateName RebuildTemplateName(TemplateTemplateParmDecl *Param, | ||||||||
1215 | const TemplateArgument &ArgPack) { | ||||||||
1216 | return getSema().Context.getSubstTemplateTemplateParmPack(Param, ArgPack); | ||||||||
1217 | } | ||||||||
1218 | |||||||||
1219 | /// Build a new compound statement. | ||||||||
1220 | /// | ||||||||
1221 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1222 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1223 | StmtResult RebuildCompoundStmt(SourceLocation LBraceLoc, | ||||||||
1224 | MultiStmtArg Statements, | ||||||||
1225 | SourceLocation RBraceLoc, | ||||||||
1226 | bool IsStmtExpr) { | ||||||||
1227 | return getSema().ActOnCompoundStmt(LBraceLoc, RBraceLoc, Statements, | ||||||||
1228 | IsStmtExpr); | ||||||||
1229 | } | ||||||||
1230 | |||||||||
1231 | /// Build a new case statement. | ||||||||
1232 | /// | ||||||||
1233 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1234 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1235 | StmtResult RebuildCaseStmt(SourceLocation CaseLoc, | ||||||||
1236 | Expr *LHS, | ||||||||
1237 | SourceLocation EllipsisLoc, | ||||||||
1238 | Expr *RHS, | ||||||||
1239 | SourceLocation ColonLoc) { | ||||||||
1240 | return getSema().ActOnCaseStmt(CaseLoc, LHS, EllipsisLoc, RHS, | ||||||||
1241 | ColonLoc); | ||||||||
1242 | } | ||||||||
1243 | |||||||||
1244 | /// Attach the body to a new case statement. | ||||||||
1245 | /// | ||||||||
1246 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1247 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1248 | StmtResult RebuildCaseStmtBody(Stmt *S, Stmt *Body) { | ||||||||
1249 | getSema().ActOnCaseStmtBody(S, Body); | ||||||||
1250 | return S; | ||||||||
1251 | } | ||||||||
1252 | |||||||||
1253 | /// Build a new default statement. | ||||||||
1254 | /// | ||||||||
1255 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1256 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1257 | StmtResult RebuildDefaultStmt(SourceLocation DefaultLoc, | ||||||||
1258 | SourceLocation ColonLoc, | ||||||||
1259 | Stmt *SubStmt) { | ||||||||
1260 | return getSema().ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt, | ||||||||
1261 | /*CurScope=*/nullptr); | ||||||||
1262 | } | ||||||||
1263 | |||||||||
1264 | /// Build a new label statement. | ||||||||
1265 | /// | ||||||||
1266 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1267 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1268 | StmtResult RebuildLabelStmt(SourceLocation IdentLoc, LabelDecl *L, | ||||||||
1269 | SourceLocation ColonLoc, Stmt *SubStmt) { | ||||||||
1270 | return SemaRef.ActOnLabelStmt(IdentLoc, L, ColonLoc, SubStmt); | ||||||||
1271 | } | ||||||||
1272 | |||||||||
1273 | /// Build a new label statement. | ||||||||
1274 | /// | ||||||||
1275 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1276 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1277 | StmtResult RebuildAttributedStmt(SourceLocation AttrLoc, | ||||||||
1278 | ArrayRef<const Attr*> Attrs, | ||||||||
1279 | Stmt *SubStmt) { | ||||||||
1280 | return SemaRef.ActOnAttributedStmt(AttrLoc, Attrs, SubStmt); | ||||||||
1281 | } | ||||||||
1282 | |||||||||
1283 | /// Build a new "if" statement. | ||||||||
1284 | /// | ||||||||
1285 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1286 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1287 | StmtResult RebuildIfStmt(SourceLocation IfLoc, bool IsConstexpr, | ||||||||
1288 | Sema::ConditionResult Cond, Stmt *Init, Stmt *Then, | ||||||||
1289 | SourceLocation ElseLoc, Stmt *Else) { | ||||||||
1290 | return getSema().ActOnIfStmt(IfLoc, IsConstexpr, Init, Cond, Then, | ||||||||
1291 | ElseLoc, Else); | ||||||||
1292 | } | ||||||||
1293 | |||||||||
1294 | /// Start building a new switch statement. | ||||||||
1295 | /// | ||||||||
1296 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1297 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1298 | StmtResult RebuildSwitchStmtStart(SourceLocation SwitchLoc, Stmt *Init, | ||||||||
1299 | Sema::ConditionResult Cond) { | ||||||||
1300 | return getSema().ActOnStartOfSwitchStmt(SwitchLoc, Init, Cond); | ||||||||
1301 | } | ||||||||
1302 | |||||||||
1303 | /// Attach the body to the switch statement. | ||||||||
1304 | /// | ||||||||
1305 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1306 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1307 | StmtResult RebuildSwitchStmtBody(SourceLocation SwitchLoc, | ||||||||
1308 | Stmt *Switch, Stmt *Body) { | ||||||||
1309 | return getSema().ActOnFinishSwitchStmt(SwitchLoc, Switch, Body); | ||||||||
1310 | } | ||||||||
1311 | |||||||||
1312 | /// Build a new while statement. | ||||||||
1313 | /// | ||||||||
1314 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1315 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1316 | StmtResult RebuildWhileStmt(SourceLocation WhileLoc, | ||||||||
1317 | Sema::ConditionResult Cond, Stmt *Body) { | ||||||||
1318 | return getSema().ActOnWhileStmt(WhileLoc, Cond, Body); | ||||||||
1319 | } | ||||||||
1320 | |||||||||
1321 | /// Build a new do-while statement. | ||||||||
1322 | /// | ||||||||
1323 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1324 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1325 | StmtResult RebuildDoStmt(SourceLocation DoLoc, Stmt *Body, | ||||||||
1326 | SourceLocation WhileLoc, SourceLocation LParenLoc, | ||||||||
1327 | Expr *Cond, SourceLocation RParenLoc) { | ||||||||
1328 | return getSema().ActOnDoStmt(DoLoc, Body, WhileLoc, LParenLoc, | ||||||||
1329 | Cond, RParenLoc); | ||||||||
1330 | } | ||||||||
1331 | |||||||||
1332 | /// Build a new for statement. | ||||||||
1333 | /// | ||||||||
1334 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1335 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1336 | StmtResult RebuildForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, | ||||||||
1337 | Stmt *Init, Sema::ConditionResult Cond, | ||||||||
1338 | Sema::FullExprArg Inc, SourceLocation RParenLoc, | ||||||||
1339 | Stmt *Body) { | ||||||||
1340 | return getSema().ActOnForStmt(ForLoc, LParenLoc, Init, Cond, | ||||||||
1341 | Inc, RParenLoc, Body); | ||||||||
1342 | } | ||||||||
1343 | |||||||||
1344 | /// Build a new goto statement. | ||||||||
1345 | /// | ||||||||
1346 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1347 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1348 | StmtResult RebuildGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, | ||||||||
1349 | LabelDecl *Label) { | ||||||||
1350 | return getSema().ActOnGotoStmt(GotoLoc, LabelLoc, Label); | ||||||||
1351 | } | ||||||||
1352 | |||||||||
1353 | /// Build a new indirect goto statement. | ||||||||
1354 | /// | ||||||||
1355 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1356 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1357 | StmtResult RebuildIndirectGotoStmt(SourceLocation GotoLoc, | ||||||||
1358 | SourceLocation StarLoc, | ||||||||
1359 | Expr *Target) { | ||||||||
1360 | return getSema().ActOnIndirectGotoStmt(GotoLoc, StarLoc, Target); | ||||||||
1361 | } | ||||||||
1362 | |||||||||
1363 | /// Build a new return statement. | ||||||||
1364 | /// | ||||||||
1365 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1366 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1367 | StmtResult RebuildReturnStmt(SourceLocation ReturnLoc, Expr *Result) { | ||||||||
1368 | return getSema().BuildReturnStmt(ReturnLoc, Result); | ||||||||
1369 | } | ||||||||
1370 | |||||||||
1371 | /// Build a new declaration statement. | ||||||||
1372 | /// | ||||||||
1373 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1374 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1375 | StmtResult RebuildDeclStmt(MutableArrayRef<Decl *> Decls, | ||||||||
1376 | SourceLocation StartLoc, SourceLocation EndLoc) { | ||||||||
1377 | Sema::DeclGroupPtrTy DG = getSema().BuildDeclaratorGroup(Decls); | ||||||||
1378 | return getSema().ActOnDeclStmt(DG, StartLoc, EndLoc); | ||||||||
1379 | } | ||||||||
1380 | |||||||||
1381 | /// Build a new inline asm statement. | ||||||||
1382 | /// | ||||||||
1383 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1384 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1385 | StmtResult RebuildGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple, | ||||||||
1386 | bool IsVolatile, unsigned NumOutputs, | ||||||||
1387 | unsigned NumInputs, IdentifierInfo **Names, | ||||||||
1388 | MultiExprArg Constraints, MultiExprArg Exprs, | ||||||||
1389 | Expr *AsmString, MultiExprArg Clobbers, | ||||||||
1390 | unsigned NumLabels, | ||||||||
1391 | SourceLocation RParenLoc) { | ||||||||
1392 | return getSema().ActOnGCCAsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs, | ||||||||
1393 | NumInputs, Names, Constraints, Exprs, | ||||||||
1394 | AsmString, Clobbers, NumLabels, RParenLoc); | ||||||||
1395 | } | ||||||||
1396 | |||||||||
1397 | /// Build a new MS style inline asm statement. | ||||||||
1398 | /// | ||||||||
1399 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1400 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1401 | StmtResult RebuildMSAsmStmt(SourceLocation AsmLoc, SourceLocation LBraceLoc, | ||||||||
1402 | ArrayRef<Token> AsmToks, | ||||||||
1403 | StringRef AsmString, | ||||||||
1404 | unsigned NumOutputs, unsigned NumInputs, | ||||||||
1405 | ArrayRef<StringRef> Constraints, | ||||||||
1406 | ArrayRef<StringRef> Clobbers, | ||||||||
1407 | ArrayRef<Expr*> Exprs, | ||||||||
1408 | SourceLocation EndLoc) { | ||||||||
1409 | return getSema().ActOnMSAsmStmt(AsmLoc, LBraceLoc, AsmToks, AsmString, | ||||||||
1410 | NumOutputs, NumInputs, | ||||||||
1411 | Constraints, Clobbers, Exprs, EndLoc); | ||||||||
1412 | } | ||||||||
1413 | |||||||||
1414 | /// Build a new co_return statement. | ||||||||
1415 | /// | ||||||||
1416 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1417 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1418 | StmtResult RebuildCoreturnStmt(SourceLocation CoreturnLoc, Expr *Result, | ||||||||
1419 | bool IsImplicit) { | ||||||||
1420 | return getSema().BuildCoreturnStmt(CoreturnLoc, Result, IsImplicit); | ||||||||
1421 | } | ||||||||
1422 | |||||||||
1423 | /// Build a new co_await expression. | ||||||||
1424 | /// | ||||||||
1425 | /// By default, performs semantic analysis to build the new expression. | ||||||||
1426 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1427 | ExprResult RebuildCoawaitExpr(SourceLocation CoawaitLoc, Expr *Result, | ||||||||
1428 | bool IsImplicit) { | ||||||||
1429 | return getSema().BuildResolvedCoawaitExpr(CoawaitLoc, Result, IsImplicit); | ||||||||
1430 | } | ||||||||
1431 | |||||||||
1432 | /// Build a new co_await expression. | ||||||||
1433 | /// | ||||||||
1434 | /// By default, performs semantic analysis to build the new expression. | ||||||||
1435 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1436 | ExprResult RebuildDependentCoawaitExpr(SourceLocation CoawaitLoc, | ||||||||
1437 | Expr *Result, | ||||||||
1438 | UnresolvedLookupExpr *Lookup) { | ||||||||
1439 | return getSema().BuildUnresolvedCoawaitExpr(CoawaitLoc, Result, Lookup); | ||||||||
1440 | } | ||||||||
1441 | |||||||||
1442 | /// Build a new co_yield expression. | ||||||||
1443 | /// | ||||||||
1444 | /// By default, performs semantic analysis to build the new expression. | ||||||||
1445 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1446 | ExprResult RebuildCoyieldExpr(SourceLocation CoyieldLoc, Expr *Result) { | ||||||||
1447 | return getSema().BuildCoyieldExpr(CoyieldLoc, Result); | ||||||||
1448 | } | ||||||||
1449 | |||||||||
1450 | StmtResult RebuildCoroutineBodyStmt(CoroutineBodyStmt::CtorArgs Args) { | ||||||||
1451 | return getSema().BuildCoroutineBodyStmt(Args); | ||||||||
1452 | } | ||||||||
1453 | |||||||||
1454 | /// Build a new Objective-C \@try statement. | ||||||||
1455 | /// | ||||||||
1456 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1457 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1458 | StmtResult RebuildObjCAtTryStmt(SourceLocation AtLoc, | ||||||||
1459 | Stmt *TryBody, | ||||||||
1460 | MultiStmtArg CatchStmts, | ||||||||
1461 | Stmt *Finally) { | ||||||||
1462 | return getSema().ActOnObjCAtTryStmt(AtLoc, TryBody, CatchStmts, | ||||||||
1463 | Finally); | ||||||||
1464 | } | ||||||||
1465 | |||||||||
1466 | /// Rebuild an Objective-C exception declaration. | ||||||||
1467 | /// | ||||||||
1468 | /// By default, performs semantic analysis to build the new declaration. | ||||||||
1469 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1470 | VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl, | ||||||||
1471 | TypeSourceInfo *TInfo, QualType T) { | ||||||||
1472 | return getSema().BuildObjCExceptionDecl(TInfo, T, | ||||||||
1473 | ExceptionDecl->getInnerLocStart(), | ||||||||
1474 | ExceptionDecl->getLocation(), | ||||||||
1475 | ExceptionDecl->getIdentifier()); | ||||||||
1476 | } | ||||||||
1477 | |||||||||
1478 | /// Build a new Objective-C \@catch statement. | ||||||||
1479 | /// | ||||||||
1480 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1481 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1482 | StmtResult RebuildObjCAtCatchStmt(SourceLocation AtLoc, | ||||||||
1483 | SourceLocation RParenLoc, | ||||||||
1484 | VarDecl *Var, | ||||||||
1485 | Stmt *Body) { | ||||||||
1486 | return getSema().ActOnObjCAtCatchStmt(AtLoc, RParenLoc, | ||||||||
1487 | Var, Body); | ||||||||
1488 | } | ||||||||
1489 | |||||||||
1490 | /// Build a new Objective-C \@finally statement. | ||||||||
1491 | /// | ||||||||
1492 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1493 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1494 | StmtResult RebuildObjCAtFinallyStmt(SourceLocation AtLoc, | ||||||||
1495 | Stmt *Body) { | ||||||||
1496 | return getSema().ActOnObjCAtFinallyStmt(AtLoc, Body); | ||||||||
1497 | } | ||||||||
1498 | |||||||||
1499 | /// Build a new Objective-C \@throw statement. | ||||||||
1500 | /// | ||||||||
1501 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1502 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1503 | StmtResult RebuildObjCAtThrowStmt(SourceLocation AtLoc, | ||||||||
1504 | Expr *Operand) { | ||||||||
1505 | return getSema().BuildObjCAtThrowStmt(AtLoc, Operand); | ||||||||
1506 | } | ||||||||
1507 | |||||||||
1508 | /// Build a new OpenMP executable directive. | ||||||||
1509 | /// | ||||||||
1510 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1511 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1512 | StmtResult RebuildOMPExecutableDirective(OpenMPDirectiveKind Kind, | ||||||||
1513 | DeclarationNameInfo DirName, | ||||||||
1514 | OpenMPDirectiveKind CancelRegion, | ||||||||
1515 | ArrayRef<OMPClause *> Clauses, | ||||||||
1516 | Stmt *AStmt, SourceLocation StartLoc, | ||||||||
1517 | SourceLocation EndLoc) { | ||||||||
1518 | return getSema().ActOnOpenMPExecutableDirective( | ||||||||
1519 | Kind, DirName, CancelRegion, Clauses, AStmt, StartLoc, EndLoc); | ||||||||
1520 | } | ||||||||
1521 | |||||||||
1522 | /// Build a new OpenMP 'if' clause. | ||||||||
1523 | /// | ||||||||
1524 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1525 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1526 | OMPClause *RebuildOMPIfClause(OpenMPDirectiveKind NameModifier, | ||||||||
1527 | Expr *Condition, SourceLocation StartLoc, | ||||||||
1528 | SourceLocation LParenLoc, | ||||||||
1529 | SourceLocation NameModifierLoc, | ||||||||
1530 | SourceLocation ColonLoc, | ||||||||
1531 | SourceLocation EndLoc) { | ||||||||
1532 | return getSema().ActOnOpenMPIfClause(NameModifier, Condition, StartLoc, | ||||||||
1533 | LParenLoc, NameModifierLoc, ColonLoc, | ||||||||
1534 | EndLoc); | ||||||||
1535 | } | ||||||||
1536 | |||||||||
1537 | /// Build a new OpenMP 'final' clause. | ||||||||
1538 | /// | ||||||||
1539 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1540 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1541 | OMPClause *RebuildOMPFinalClause(Expr *Condition, SourceLocation StartLoc, | ||||||||
1542 | SourceLocation LParenLoc, | ||||||||
1543 | SourceLocation EndLoc) { | ||||||||
1544 | return getSema().ActOnOpenMPFinalClause(Condition, StartLoc, LParenLoc, | ||||||||
1545 | EndLoc); | ||||||||
1546 | } | ||||||||
1547 | |||||||||
1548 | /// Build a new OpenMP 'num_threads' clause. | ||||||||
1549 | /// | ||||||||
1550 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1551 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1552 | OMPClause *RebuildOMPNumThreadsClause(Expr *NumThreads, | ||||||||
1553 | SourceLocation StartLoc, | ||||||||
1554 | SourceLocation LParenLoc, | ||||||||
1555 | SourceLocation EndLoc) { | ||||||||
1556 | return getSema().ActOnOpenMPNumThreadsClause(NumThreads, StartLoc, | ||||||||
1557 | LParenLoc, EndLoc); | ||||||||
1558 | } | ||||||||
1559 | |||||||||
1560 | /// Build a new OpenMP 'safelen' clause. | ||||||||
1561 | /// | ||||||||
1562 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1563 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1564 | OMPClause *RebuildOMPSafelenClause(Expr *Len, SourceLocation StartLoc, | ||||||||
1565 | SourceLocation LParenLoc, | ||||||||
1566 | SourceLocation EndLoc) { | ||||||||
1567 | return getSema().ActOnOpenMPSafelenClause(Len, StartLoc, LParenLoc, EndLoc); | ||||||||
1568 | } | ||||||||
1569 | |||||||||
1570 | /// Build a new OpenMP 'simdlen' clause. | ||||||||
1571 | /// | ||||||||
1572 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1573 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1574 | OMPClause *RebuildOMPSimdlenClause(Expr *Len, SourceLocation StartLoc, | ||||||||
1575 | SourceLocation LParenLoc, | ||||||||
1576 | SourceLocation EndLoc) { | ||||||||
1577 | return getSema().ActOnOpenMPSimdlenClause(Len, StartLoc, LParenLoc, EndLoc); | ||||||||
1578 | } | ||||||||
1579 | |||||||||
1580 | /// Build a new OpenMP 'allocator' clause. | ||||||||
1581 | /// | ||||||||
1582 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1583 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1584 | OMPClause *RebuildOMPAllocatorClause(Expr *A, SourceLocation StartLoc, | ||||||||
1585 | SourceLocation LParenLoc, | ||||||||
1586 | SourceLocation EndLoc) { | ||||||||
1587 | return getSema().ActOnOpenMPAllocatorClause(A, StartLoc, LParenLoc, EndLoc); | ||||||||
1588 | } | ||||||||
1589 | |||||||||
1590 | /// Build a new OpenMP 'collapse' clause. | ||||||||
1591 | /// | ||||||||
1592 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1593 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1594 | OMPClause *RebuildOMPCollapseClause(Expr *Num, SourceLocation StartLoc, | ||||||||
1595 | SourceLocation LParenLoc, | ||||||||
1596 | SourceLocation EndLoc) { | ||||||||
1597 | return getSema().ActOnOpenMPCollapseClause(Num, StartLoc, LParenLoc, | ||||||||
1598 | EndLoc); | ||||||||
1599 | } | ||||||||
1600 | |||||||||
1601 | /// Build a new OpenMP 'default' clause. | ||||||||
1602 | /// | ||||||||
1603 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1604 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1605 | OMPClause *RebuildOMPDefaultClause(DefaultKind Kind, SourceLocation KindKwLoc, | ||||||||
1606 | SourceLocation StartLoc, | ||||||||
1607 | SourceLocation LParenLoc, | ||||||||
1608 | SourceLocation EndLoc) { | ||||||||
1609 | return getSema().ActOnOpenMPDefaultClause(Kind, KindKwLoc, | ||||||||
1610 | StartLoc, LParenLoc, EndLoc); | ||||||||
1611 | } | ||||||||
1612 | |||||||||
1613 | /// Build a new OpenMP 'proc_bind' clause. | ||||||||
1614 | /// | ||||||||
1615 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1616 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1617 | OMPClause *RebuildOMPProcBindClause(ProcBindKind Kind, | ||||||||
1618 | SourceLocation KindKwLoc, | ||||||||
1619 | SourceLocation StartLoc, | ||||||||
1620 | SourceLocation LParenLoc, | ||||||||
1621 | SourceLocation EndLoc) { | ||||||||
1622 | return getSema().ActOnOpenMPProcBindClause(Kind, KindKwLoc, | ||||||||
1623 | StartLoc, LParenLoc, EndLoc); | ||||||||
1624 | } | ||||||||
1625 | |||||||||
1626 | /// Build a new OpenMP 'schedule' clause. | ||||||||
1627 | /// | ||||||||
1628 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1629 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1630 | OMPClause *RebuildOMPScheduleClause( | ||||||||
1631 | OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, | ||||||||
1632 | OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, | ||||||||
1633 | SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, | ||||||||
1634 | SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { | ||||||||
1635 | return getSema().ActOnOpenMPScheduleClause( | ||||||||
1636 | M1, M2, Kind, ChunkSize, StartLoc, LParenLoc, M1Loc, M2Loc, KindLoc, | ||||||||
1637 | CommaLoc, EndLoc); | ||||||||
1638 | } | ||||||||
1639 | |||||||||
1640 | /// Build a new OpenMP 'ordered' clause. | ||||||||
1641 | /// | ||||||||
1642 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1643 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1644 | OMPClause *RebuildOMPOrderedClause(SourceLocation StartLoc, | ||||||||
1645 | SourceLocation EndLoc, | ||||||||
1646 | SourceLocation LParenLoc, Expr *Num) { | ||||||||
1647 | return getSema().ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Num); | ||||||||
1648 | } | ||||||||
1649 | |||||||||
1650 | /// Build a new OpenMP 'private' clause. | ||||||||
1651 | /// | ||||||||
1652 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1653 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1654 | OMPClause *RebuildOMPPrivateClause(ArrayRef<Expr *> VarList, | ||||||||
1655 | SourceLocation StartLoc, | ||||||||
1656 | SourceLocation LParenLoc, | ||||||||
1657 | SourceLocation EndLoc) { | ||||||||
1658 | return getSema().ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, | ||||||||
1659 | EndLoc); | ||||||||
1660 | } | ||||||||
1661 | |||||||||
1662 | /// Build a new OpenMP 'firstprivate' clause. | ||||||||
1663 | /// | ||||||||
1664 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1665 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1666 | OMPClause *RebuildOMPFirstprivateClause(ArrayRef<Expr *> VarList, | ||||||||
1667 | SourceLocation StartLoc, | ||||||||
1668 | SourceLocation LParenLoc, | ||||||||
1669 | SourceLocation EndLoc) { | ||||||||
1670 | return getSema().ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, | ||||||||
1671 | EndLoc); | ||||||||
1672 | } | ||||||||
1673 | |||||||||
1674 | /// Build a new OpenMP 'lastprivate' clause. | ||||||||
1675 | /// | ||||||||
1676 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1677 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1678 | OMPClause *RebuildOMPLastprivateClause(ArrayRef<Expr *> VarList, | ||||||||
1679 | OpenMPLastprivateModifier LPKind, | ||||||||
1680 | SourceLocation LPKindLoc, | ||||||||
1681 | SourceLocation ColonLoc, | ||||||||
1682 | SourceLocation StartLoc, | ||||||||
1683 | SourceLocation LParenLoc, | ||||||||
1684 | SourceLocation EndLoc) { | ||||||||
1685 | return getSema().ActOnOpenMPLastprivateClause( | ||||||||
1686 | VarList, LPKind, LPKindLoc, ColonLoc, StartLoc, LParenLoc, EndLoc); | ||||||||
1687 | } | ||||||||
1688 | |||||||||
1689 | /// Build a new OpenMP 'shared' clause. | ||||||||
1690 | /// | ||||||||
1691 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1692 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1693 | OMPClause *RebuildOMPSharedClause(ArrayRef<Expr *> VarList, | ||||||||
1694 | SourceLocation StartLoc, | ||||||||
1695 | SourceLocation LParenLoc, | ||||||||
1696 | SourceLocation EndLoc) { | ||||||||
1697 | return getSema().ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, | ||||||||
1698 | EndLoc); | ||||||||
1699 | } | ||||||||
1700 | |||||||||
1701 | /// Build a new OpenMP 'reduction' clause. | ||||||||
1702 | /// | ||||||||
1703 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1704 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1705 | OMPClause *RebuildOMPReductionClause(ArrayRef<Expr *> VarList, | ||||||||
1706 | SourceLocation StartLoc, | ||||||||
1707 | SourceLocation LParenLoc, | ||||||||
1708 | SourceLocation ColonLoc, | ||||||||
1709 | SourceLocation EndLoc, | ||||||||
1710 | CXXScopeSpec &ReductionIdScopeSpec, | ||||||||
1711 | const DeclarationNameInfo &ReductionId, | ||||||||
1712 | ArrayRef<Expr *> UnresolvedReductions) { | ||||||||
1713 | return getSema().ActOnOpenMPReductionClause( | ||||||||
1714 | VarList, StartLoc, LParenLoc, ColonLoc, EndLoc, ReductionIdScopeSpec, | ||||||||
1715 | ReductionId, UnresolvedReductions); | ||||||||
1716 | } | ||||||||
1717 | |||||||||
1718 | /// Build a new OpenMP 'task_reduction' clause. | ||||||||
1719 | /// | ||||||||
1720 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1721 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1722 | OMPClause *RebuildOMPTaskReductionClause( | ||||||||
1723 | ArrayRef<Expr *> VarList, SourceLocation StartLoc, | ||||||||
1724 | SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc, | ||||||||
1725 | CXXScopeSpec &ReductionIdScopeSpec, | ||||||||
1726 | const DeclarationNameInfo &ReductionId, | ||||||||
1727 | ArrayRef<Expr *> UnresolvedReductions) { | ||||||||
1728 | return getSema().ActOnOpenMPTaskReductionClause( | ||||||||
1729 | VarList, StartLoc, LParenLoc, ColonLoc, EndLoc, ReductionIdScopeSpec, | ||||||||
1730 | ReductionId, UnresolvedReductions); | ||||||||
1731 | } | ||||||||
1732 | |||||||||
1733 | /// Build a new OpenMP 'in_reduction' clause. | ||||||||
1734 | /// | ||||||||
1735 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1736 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1737 | OMPClause * | ||||||||
1738 | RebuildOMPInReductionClause(ArrayRef<Expr *> VarList, SourceLocation StartLoc, | ||||||||
1739 | SourceLocation LParenLoc, SourceLocation ColonLoc, | ||||||||
1740 | SourceLocation EndLoc, | ||||||||
1741 | CXXScopeSpec &ReductionIdScopeSpec, | ||||||||
1742 | const DeclarationNameInfo &ReductionId, | ||||||||
1743 | ArrayRef<Expr *> UnresolvedReductions) { | ||||||||
1744 | return getSema().ActOnOpenMPInReductionClause( | ||||||||
1745 | VarList, StartLoc, LParenLoc, ColonLoc, EndLoc, ReductionIdScopeSpec, | ||||||||
1746 | ReductionId, UnresolvedReductions); | ||||||||
1747 | } | ||||||||
1748 | |||||||||
1749 | /// Build a new OpenMP 'linear' clause. | ||||||||
1750 | /// | ||||||||
1751 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1752 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1753 | OMPClause *RebuildOMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step, | ||||||||
1754 | SourceLocation StartLoc, | ||||||||
1755 | SourceLocation LParenLoc, | ||||||||
1756 | OpenMPLinearClauseKind Modifier, | ||||||||
1757 | SourceLocation ModifierLoc, | ||||||||
1758 | SourceLocation ColonLoc, | ||||||||
1759 | SourceLocation EndLoc) { | ||||||||
1760 | return getSema().ActOnOpenMPLinearClause(VarList, Step, StartLoc, LParenLoc, | ||||||||
1761 | Modifier, ModifierLoc, ColonLoc, | ||||||||
1762 | EndLoc); | ||||||||
1763 | } | ||||||||
1764 | |||||||||
1765 | /// Build a new OpenMP 'aligned' clause. | ||||||||
1766 | /// | ||||||||
1767 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1768 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1769 | OMPClause *RebuildOMPAlignedClause(ArrayRef<Expr *> VarList, Expr *Alignment, | ||||||||
1770 | SourceLocation StartLoc, | ||||||||
1771 | SourceLocation LParenLoc, | ||||||||
1772 | SourceLocation ColonLoc, | ||||||||
1773 | SourceLocation EndLoc) { | ||||||||
1774 | return getSema().ActOnOpenMPAlignedClause(VarList, Alignment, StartLoc, | ||||||||
1775 | LParenLoc, ColonLoc, EndLoc); | ||||||||
1776 | } | ||||||||
1777 | |||||||||
1778 | /// Build a new OpenMP 'copyin' clause. | ||||||||
1779 | /// | ||||||||
1780 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1781 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1782 | OMPClause *RebuildOMPCopyinClause(ArrayRef<Expr *> VarList, | ||||||||
1783 | SourceLocation StartLoc, | ||||||||
1784 | SourceLocation LParenLoc, | ||||||||
1785 | SourceLocation EndLoc) { | ||||||||
1786 | return getSema().ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, | ||||||||
1787 | EndLoc); | ||||||||
1788 | } | ||||||||
1789 | |||||||||
1790 | /// Build a new OpenMP 'copyprivate' clause. | ||||||||
1791 | /// | ||||||||
1792 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1793 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1794 | OMPClause *RebuildOMPCopyprivateClause(ArrayRef<Expr *> VarList, | ||||||||
1795 | SourceLocation StartLoc, | ||||||||
1796 | SourceLocation LParenLoc, | ||||||||
1797 | SourceLocation EndLoc) { | ||||||||
1798 | return getSema().ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, | ||||||||
1799 | EndLoc); | ||||||||
1800 | } | ||||||||
1801 | |||||||||
1802 | /// Build a new OpenMP 'flush' pseudo clause. | ||||||||
1803 | /// | ||||||||
1804 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1805 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1806 | OMPClause *RebuildOMPFlushClause(ArrayRef<Expr *> VarList, | ||||||||
1807 | SourceLocation StartLoc, | ||||||||
1808 | SourceLocation LParenLoc, | ||||||||
1809 | SourceLocation EndLoc) { | ||||||||
1810 | return getSema().ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, | ||||||||
1811 | EndLoc); | ||||||||
1812 | } | ||||||||
1813 | |||||||||
1814 | /// Build a new OpenMP 'depobj' pseudo clause. | ||||||||
1815 | /// | ||||||||
1816 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1817 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1818 | OMPClause *RebuildOMPDepobjClause(Expr *Depobj, SourceLocation StartLoc, | ||||||||
1819 | SourceLocation LParenLoc, | ||||||||
1820 | SourceLocation EndLoc) { | ||||||||
1821 | return getSema().ActOnOpenMPDepobjClause(Depobj, StartLoc, LParenLoc, | ||||||||
1822 | EndLoc); | ||||||||
1823 | } | ||||||||
1824 | |||||||||
1825 | /// Build a new OpenMP 'depend' pseudo clause. | ||||||||
1826 | /// | ||||||||
1827 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1828 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1829 | OMPClause * | ||||||||
1830 | RebuildOMPDependClause(OpenMPDependClauseKind DepKind, SourceLocation DepLoc, | ||||||||
1831 | SourceLocation ColonLoc, ArrayRef<Expr *> VarList, | ||||||||
1832 | SourceLocation StartLoc, SourceLocation LParenLoc, | ||||||||
1833 | SourceLocation EndLoc) { | ||||||||
1834 | return getSema().ActOnOpenMPDependClause(DepKind, DepLoc, ColonLoc, VarList, | ||||||||
1835 | StartLoc, LParenLoc, EndLoc); | ||||||||
1836 | } | ||||||||
1837 | |||||||||
1838 | /// Build a new OpenMP 'device' clause. | ||||||||
1839 | /// | ||||||||
1840 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1841 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1842 | OMPClause *RebuildOMPDeviceClause(Expr *Device, SourceLocation StartLoc, | ||||||||
1843 | SourceLocation LParenLoc, | ||||||||
1844 | SourceLocation EndLoc) { | ||||||||
1845 | return getSema().ActOnOpenMPDeviceClause(Device, StartLoc, LParenLoc, | ||||||||
1846 | EndLoc); | ||||||||
1847 | } | ||||||||
1848 | |||||||||
1849 | /// Build a new OpenMP 'map' clause. | ||||||||
1850 | /// | ||||||||
1851 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1852 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1853 | OMPClause *RebuildOMPMapClause( | ||||||||
1854 | ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, | ||||||||
1855 | ArrayRef<SourceLocation> MapTypeModifiersLoc, | ||||||||
1856 | CXXScopeSpec MapperIdScopeSpec, DeclarationNameInfo MapperId, | ||||||||
1857 | OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, | ||||||||
1858 | SourceLocation MapLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VarList, | ||||||||
1859 | const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { | ||||||||
1860 | return getSema().ActOnOpenMPMapClause(MapTypeModifiers, MapTypeModifiersLoc, | ||||||||
1861 | MapperIdScopeSpec, MapperId, MapType, | ||||||||
1862 | IsMapTypeImplicit, MapLoc, ColonLoc, | ||||||||
1863 | VarList, Locs, UnresolvedMappers); | ||||||||
1864 | } | ||||||||
1865 | |||||||||
1866 | /// Build a new OpenMP 'allocate' clause. | ||||||||
1867 | /// | ||||||||
1868 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1869 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1870 | OMPClause *RebuildOMPAllocateClause(Expr *Allocate, ArrayRef<Expr *> VarList, | ||||||||
1871 | SourceLocation StartLoc, | ||||||||
1872 | SourceLocation LParenLoc, | ||||||||
1873 | SourceLocation ColonLoc, | ||||||||
1874 | SourceLocation EndLoc) { | ||||||||
1875 | return getSema().ActOnOpenMPAllocateClause(Allocate, VarList, StartLoc, | ||||||||
1876 | LParenLoc, ColonLoc, EndLoc); | ||||||||
1877 | } | ||||||||
1878 | |||||||||
1879 | /// Build a new OpenMP 'num_teams' clause. | ||||||||
1880 | /// | ||||||||
1881 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1882 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1883 | OMPClause *RebuildOMPNumTeamsClause(Expr *NumTeams, SourceLocation StartLoc, | ||||||||
1884 | SourceLocation LParenLoc, | ||||||||
1885 | SourceLocation EndLoc) { | ||||||||
1886 | return getSema().ActOnOpenMPNumTeamsClause(NumTeams, StartLoc, LParenLoc, | ||||||||
1887 | EndLoc); | ||||||||
1888 | } | ||||||||
1889 | |||||||||
1890 | /// Build a new OpenMP 'thread_limit' clause. | ||||||||
1891 | /// | ||||||||
1892 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1893 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1894 | OMPClause *RebuildOMPThreadLimitClause(Expr *ThreadLimit, | ||||||||
1895 | SourceLocation StartLoc, | ||||||||
1896 | SourceLocation LParenLoc, | ||||||||
1897 | SourceLocation EndLoc) { | ||||||||
1898 | return getSema().ActOnOpenMPThreadLimitClause(ThreadLimit, StartLoc, | ||||||||
1899 | LParenLoc, EndLoc); | ||||||||
1900 | } | ||||||||
1901 | |||||||||
1902 | /// Build a new OpenMP 'priority' clause. | ||||||||
1903 | /// | ||||||||
1904 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1905 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1906 | OMPClause *RebuildOMPPriorityClause(Expr *Priority, SourceLocation StartLoc, | ||||||||
1907 | SourceLocation LParenLoc, | ||||||||
1908 | SourceLocation EndLoc) { | ||||||||
1909 | return getSema().ActOnOpenMPPriorityClause(Priority, StartLoc, LParenLoc, | ||||||||
1910 | EndLoc); | ||||||||
1911 | } | ||||||||
1912 | |||||||||
1913 | /// Build a new OpenMP 'grainsize' clause. | ||||||||
1914 | /// | ||||||||
1915 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1916 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1917 | OMPClause *RebuildOMPGrainsizeClause(Expr *Grainsize, SourceLocation StartLoc, | ||||||||
1918 | SourceLocation LParenLoc, | ||||||||
1919 | SourceLocation EndLoc) { | ||||||||
1920 | return getSema().ActOnOpenMPGrainsizeClause(Grainsize, StartLoc, LParenLoc, | ||||||||
1921 | EndLoc); | ||||||||
1922 | } | ||||||||
1923 | |||||||||
1924 | /// Build a new OpenMP 'num_tasks' clause. | ||||||||
1925 | /// | ||||||||
1926 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1927 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1928 | OMPClause *RebuildOMPNumTasksClause(Expr *NumTasks, SourceLocation StartLoc, | ||||||||
1929 | SourceLocation LParenLoc, | ||||||||
1930 | SourceLocation EndLoc) { | ||||||||
1931 | return getSema().ActOnOpenMPNumTasksClause(NumTasks, StartLoc, LParenLoc, | ||||||||
1932 | EndLoc); | ||||||||
1933 | } | ||||||||
1934 | |||||||||
1935 | /// Build a new OpenMP 'hint' clause. | ||||||||
1936 | /// | ||||||||
1937 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1938 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1939 | OMPClause *RebuildOMPHintClause(Expr *Hint, SourceLocation StartLoc, | ||||||||
1940 | SourceLocation LParenLoc, | ||||||||
1941 | SourceLocation EndLoc) { | ||||||||
1942 | return getSema().ActOnOpenMPHintClause(Hint, StartLoc, LParenLoc, EndLoc); | ||||||||
1943 | } | ||||||||
1944 | |||||||||
1945 | /// Build a new OpenMP 'dist_schedule' clause. | ||||||||
1946 | /// | ||||||||
1947 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1948 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1949 | OMPClause * | ||||||||
1950 | RebuildOMPDistScheduleClause(OpenMPDistScheduleClauseKind Kind, | ||||||||
1951 | Expr *ChunkSize, SourceLocation StartLoc, | ||||||||
1952 | SourceLocation LParenLoc, SourceLocation KindLoc, | ||||||||
1953 | SourceLocation CommaLoc, SourceLocation EndLoc) { | ||||||||
1954 | return getSema().ActOnOpenMPDistScheduleClause( | ||||||||
1955 | Kind, ChunkSize, StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc); | ||||||||
1956 | } | ||||||||
1957 | |||||||||
1958 | /// Build a new OpenMP 'to' clause. | ||||||||
1959 | /// | ||||||||
1960 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1961 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1962 | OMPClause *RebuildOMPToClause(ArrayRef<Expr *> VarList, | ||||||||
1963 | CXXScopeSpec &MapperIdScopeSpec, | ||||||||
1964 | DeclarationNameInfo &MapperId, | ||||||||
1965 | const OMPVarListLocTy &Locs, | ||||||||
1966 | ArrayRef<Expr *> UnresolvedMappers) { | ||||||||
1967 | return getSema().ActOnOpenMPToClause(VarList, MapperIdScopeSpec, MapperId, | ||||||||
1968 | Locs, UnresolvedMappers); | ||||||||
1969 | } | ||||||||
1970 | |||||||||
1971 | /// Build a new OpenMP 'from' clause. | ||||||||
1972 | /// | ||||||||
1973 | /// By default, performs semantic analysis to build the new statement. | ||||||||
1974 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1975 | OMPClause *RebuildOMPFromClause(ArrayRef<Expr *> VarList, | ||||||||
1976 | CXXScopeSpec &MapperIdScopeSpec, | ||||||||
1977 | DeclarationNameInfo &MapperId, | ||||||||
1978 | const OMPVarListLocTy &Locs, | ||||||||
1979 | ArrayRef<Expr *> UnresolvedMappers) { | ||||||||
1980 | return getSema().ActOnOpenMPFromClause(VarList, MapperIdScopeSpec, MapperId, | ||||||||
1981 | Locs, UnresolvedMappers); | ||||||||
1982 | } | ||||||||
1983 | |||||||||
1984 | /// Build a new OpenMP 'use_device_ptr' clause. | ||||||||
1985 | /// | ||||||||
1986 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1987 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1988 | OMPClause *RebuildOMPUseDevicePtrClause(ArrayRef<Expr *> VarList, | ||||||||
1989 | const OMPVarListLocTy &Locs) { | ||||||||
1990 | return getSema().ActOnOpenMPUseDevicePtrClause(VarList, Locs); | ||||||||
1991 | } | ||||||||
1992 | |||||||||
1993 | /// Build a new OpenMP 'is_device_ptr' clause. | ||||||||
1994 | /// | ||||||||
1995 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
1996 | /// Subclasses may override this routine to provide different behavior. | ||||||||
1997 | OMPClause *RebuildOMPIsDevicePtrClause(ArrayRef<Expr *> VarList, | ||||||||
1998 | const OMPVarListLocTy &Locs) { | ||||||||
1999 | return getSema().ActOnOpenMPIsDevicePtrClause(VarList, Locs); | ||||||||
2000 | } | ||||||||
2001 | |||||||||
2002 | /// Build a new OpenMP 'defaultmap' clause. | ||||||||
2003 | /// | ||||||||
2004 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
2005 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2006 | OMPClause *RebuildOMPDefaultmapClause(OpenMPDefaultmapClauseModifier M, | ||||||||
2007 | OpenMPDefaultmapClauseKind Kind, | ||||||||
2008 | SourceLocation StartLoc, | ||||||||
2009 | SourceLocation LParenLoc, | ||||||||
2010 | SourceLocation MLoc, | ||||||||
2011 | SourceLocation KindLoc, | ||||||||
2012 | SourceLocation EndLoc) { | ||||||||
2013 | return getSema().ActOnOpenMPDefaultmapClause(M, Kind, StartLoc, LParenLoc, | ||||||||
2014 | MLoc, KindLoc, EndLoc); | ||||||||
2015 | } | ||||||||
2016 | |||||||||
2017 | /// Build a new OpenMP 'nontemporal' clause. | ||||||||
2018 | /// | ||||||||
2019 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
2020 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2021 | OMPClause *RebuildOMPNontemporalClause(ArrayRef<Expr *> VarList, | ||||||||
2022 | SourceLocation StartLoc, | ||||||||
2023 | SourceLocation LParenLoc, | ||||||||
2024 | SourceLocation EndLoc) { | ||||||||
2025 | return getSema().ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, | ||||||||
2026 | EndLoc); | ||||||||
2027 | } | ||||||||
2028 | |||||||||
2029 | /// Build a new OpenMP 'order' clause. | ||||||||
2030 | /// | ||||||||
2031 | /// By default, performs semantic analysis to build the new OpenMP clause. | ||||||||
2032 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2033 | OMPClause *RebuildOMPOrderClause(OpenMPOrderClauseKind Kind, | ||||||||
2034 | SourceLocation KindKwLoc, | ||||||||
2035 | SourceLocation StartLoc, | ||||||||
2036 | SourceLocation LParenLoc, | ||||||||
2037 | SourceLocation EndLoc) { | ||||||||
2038 | return getSema().ActOnOpenMPOrderClause(Kind, KindKwLoc, StartLoc, | ||||||||
2039 | LParenLoc, EndLoc); | ||||||||
2040 | } | ||||||||
2041 | |||||||||
2042 | /// Rebuild the operand to an Objective-C \@synchronized statement. | ||||||||
2043 | /// | ||||||||
2044 | /// By default, performs semantic analysis to build the new statement. | ||||||||
2045 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2046 | ExprResult RebuildObjCAtSynchronizedOperand(SourceLocation atLoc, | ||||||||
2047 | Expr *object) { | ||||||||
2048 | return getSema().ActOnObjCAtSynchronizedOperand(atLoc, object); | ||||||||
2049 | } | ||||||||
2050 | |||||||||
2051 | /// Build a new Objective-C \@synchronized statement. | ||||||||
2052 | /// | ||||||||
2053 | /// By default, performs semantic analysis to build the new statement. | ||||||||
2054 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2055 | StmtResult RebuildObjCAtSynchronizedStmt(SourceLocation AtLoc, | ||||||||
2056 | Expr *Object, Stmt *Body) { | ||||||||
2057 | return getSema().ActOnObjCAtSynchronizedStmt(AtLoc, Object, Body); | ||||||||
2058 | } | ||||||||
2059 | |||||||||
2060 | /// Build a new Objective-C \@autoreleasepool statement. | ||||||||
2061 | /// | ||||||||
2062 | /// By default, performs semantic analysis to build the new statement. | ||||||||
2063 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2064 | StmtResult RebuildObjCAutoreleasePoolStmt(SourceLocation AtLoc, | ||||||||
2065 | Stmt *Body) { | ||||||||
2066 | return getSema().ActOnObjCAutoreleasePoolStmt(AtLoc, Body); | ||||||||
2067 | } | ||||||||
2068 | |||||||||
2069 | /// Build a new Objective-C fast enumeration statement. | ||||||||
2070 | /// | ||||||||
2071 | /// By default, performs semantic analysis to build the new statement. | ||||||||
2072 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2073 | StmtResult RebuildObjCForCollectionStmt(SourceLocation ForLoc, | ||||||||
2074 | Stmt *Element, | ||||||||
2075 | Expr *Collection, | ||||||||
2076 | SourceLocation RParenLoc, | ||||||||
2077 | Stmt *Body) { | ||||||||
2078 | StmtResult ForEachStmt = getSema().ActOnObjCForCollectionStmt(ForLoc, | ||||||||
2079 | Element, | ||||||||
2080 | Collection, | ||||||||
2081 | RParenLoc); | ||||||||
2082 | if (ForEachStmt.isInvalid()) | ||||||||
2083 | return StmtError(); | ||||||||
2084 | |||||||||
2085 | return getSema().FinishObjCForCollectionStmt(ForEachStmt.get(), Body); | ||||||||
2086 | } | ||||||||
2087 | |||||||||
2088 | /// Build a new C++ exception declaration. | ||||||||
2089 | /// | ||||||||
2090 | /// By default, performs semantic analysis to build the new decaration. | ||||||||
2091 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2092 | VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, | ||||||||
2093 | TypeSourceInfo *Declarator, | ||||||||
2094 | SourceLocation StartLoc, | ||||||||
2095 | SourceLocation IdLoc, | ||||||||
2096 | IdentifierInfo *Id) { | ||||||||
2097 | VarDecl *Var = getSema().BuildExceptionDeclaration(nullptr, Declarator, | ||||||||
2098 | StartLoc, IdLoc, Id); | ||||||||
2099 | if (Var) | ||||||||
2100 | getSema().CurContext->addDecl(Var); | ||||||||
2101 | return Var; | ||||||||
2102 | } | ||||||||
2103 | |||||||||
2104 | /// Build a new C++ catch statement. | ||||||||
2105 | /// | ||||||||
2106 | /// By default, performs semantic analysis to build the new statement. | ||||||||
2107 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2108 | StmtResult RebuildCXXCatchStmt(SourceLocation CatchLoc, | ||||||||
2109 | VarDecl *ExceptionDecl, | ||||||||
2110 | Stmt *Handler) { | ||||||||
2111 | return Owned(new (getSema().Context) CXXCatchStmt(CatchLoc, ExceptionDecl, | ||||||||
2112 | Handler)); | ||||||||
2113 | } | ||||||||
2114 | |||||||||
2115 | /// Build a new C++ try statement. | ||||||||
2116 | /// | ||||||||
2117 | /// By default, performs semantic analysis to build the new statement. | ||||||||
2118 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2119 | StmtResult RebuildCXXTryStmt(SourceLocation TryLoc, Stmt *TryBlock, | ||||||||
2120 | ArrayRef<Stmt *> Handlers) { | ||||||||
2121 | return getSema().ActOnCXXTryBlock(TryLoc, TryBlock, Handlers); | ||||||||
2122 | } | ||||||||
2123 | |||||||||
2124 | /// Build a new C++0x range-based for statement. | ||||||||
2125 | /// | ||||||||
2126 | /// By default, performs semantic analysis to build the new statement. | ||||||||
2127 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2128 | StmtResult RebuildCXXForRangeStmt(SourceLocation ForLoc, | ||||||||
2129 | SourceLocation CoawaitLoc, Stmt *Init, | ||||||||
2130 | SourceLocation ColonLoc, Stmt *Range, | ||||||||
2131 | Stmt *Begin, Stmt *End, Expr *Cond, | ||||||||
2132 | Expr *Inc, Stmt *LoopVar, | ||||||||
2133 | SourceLocation RParenLoc) { | ||||||||
2134 | // If we've just learned that the range is actually an Objective-C | ||||||||
2135 | // collection, treat this as an Objective-C fast enumeration loop. | ||||||||
2136 | if (DeclStmt *RangeStmt = dyn_cast<DeclStmt>(Range)) { | ||||||||
2137 | if (RangeStmt->isSingleDecl()) { | ||||||||
2138 | if (VarDecl *RangeVar = dyn_cast<VarDecl>(RangeStmt->getSingleDecl())) { | ||||||||
2139 | if (RangeVar->isInvalidDecl()) | ||||||||
2140 | return StmtError(); | ||||||||
2141 | |||||||||
2142 | Expr *RangeExpr = RangeVar->getInit(); | ||||||||
2143 | if (!RangeExpr->isTypeDependent() && | ||||||||
2144 | RangeExpr->getType()->isObjCObjectPointerType()) { | ||||||||
2145 | // FIXME: Support init-statements in Objective-C++20 ranged for | ||||||||
2146 | // statement. | ||||||||
2147 | if (Init) { | ||||||||
2148 | return SemaRef.Diag(Init->getBeginLoc(), | ||||||||
2149 | diag::err_objc_for_range_init_stmt) | ||||||||
2150 | << Init->getSourceRange(); | ||||||||
2151 | } | ||||||||
2152 | return getSema().ActOnObjCForCollectionStmt(ForLoc, LoopVar, | ||||||||
2153 | RangeExpr, RParenLoc); | ||||||||
2154 | } | ||||||||
2155 | } | ||||||||
2156 | } | ||||||||
2157 | } | ||||||||
2158 | |||||||||
2159 | return getSema().BuildCXXForRangeStmt(ForLoc, CoawaitLoc, Init, ColonLoc, | ||||||||
2160 | Range, Begin, End, Cond, Inc, LoopVar, | ||||||||
2161 | RParenLoc, Sema::BFRK_Rebuild); | ||||||||
2162 | } | ||||||||
2163 | |||||||||
2164 | /// Build a new C++0x range-based for statement. | ||||||||
2165 | /// | ||||||||
2166 | /// By default, performs semantic analysis to build the new statement. | ||||||||
2167 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2168 | StmtResult RebuildMSDependentExistsStmt(SourceLocation KeywordLoc, | ||||||||
2169 | bool IsIfExists, | ||||||||
2170 | NestedNameSpecifierLoc QualifierLoc, | ||||||||
2171 | DeclarationNameInfo NameInfo, | ||||||||
2172 | Stmt *Nested) { | ||||||||
2173 | return getSema().BuildMSDependentExistsStmt(KeywordLoc, IsIfExists, | ||||||||
2174 | QualifierLoc, NameInfo, Nested); | ||||||||
2175 | } | ||||||||
2176 | |||||||||
2177 | /// Attach body to a C++0x range-based for statement. | ||||||||
2178 | /// | ||||||||
2179 | /// By default, performs semantic analysis to finish the new statement. | ||||||||
2180 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2181 | StmtResult FinishCXXForRangeStmt(Stmt *ForRange, Stmt *Body) { | ||||||||
2182 | return getSema().FinishCXXForRangeStmt(ForRange, Body); | ||||||||
2183 | } | ||||||||
2184 | |||||||||
2185 | StmtResult RebuildSEHTryStmt(bool IsCXXTry, SourceLocation TryLoc, | ||||||||
2186 | Stmt *TryBlock, Stmt *Handler) { | ||||||||
2187 | return getSema().ActOnSEHTryBlock(IsCXXTry, TryLoc, TryBlock, Handler); | ||||||||
2188 | } | ||||||||
2189 | |||||||||
2190 | StmtResult RebuildSEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, | ||||||||
2191 | Stmt *Block) { | ||||||||
2192 | return getSema().ActOnSEHExceptBlock(Loc, FilterExpr, Block); | ||||||||
2193 | } | ||||||||
2194 | |||||||||
2195 | StmtResult RebuildSEHFinallyStmt(SourceLocation Loc, Stmt *Block) { | ||||||||
2196 | return SEHFinallyStmt::Create(getSema().getASTContext(), Loc, Block); | ||||||||
2197 | } | ||||||||
2198 | |||||||||
2199 | /// Build a new predefined expression. | ||||||||
2200 | /// | ||||||||
2201 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2202 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2203 | ExprResult RebuildPredefinedExpr(SourceLocation Loc, | ||||||||
2204 | PredefinedExpr::IdentKind IK) { | ||||||||
2205 | return getSema().BuildPredefinedExpr(Loc, IK); | ||||||||
2206 | } | ||||||||
2207 | |||||||||
2208 | /// Build a new expression that references a declaration. | ||||||||
2209 | /// | ||||||||
2210 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2211 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2212 | ExprResult RebuildDeclarationNameExpr(const CXXScopeSpec &SS, | ||||||||
2213 | LookupResult &R, | ||||||||
2214 | bool RequiresADL) { | ||||||||
2215 | return getSema().BuildDeclarationNameExpr(SS, R, RequiresADL); | ||||||||
2216 | } | ||||||||
2217 | |||||||||
2218 | |||||||||
2219 | /// Build a new expression that references a declaration. | ||||||||
2220 | /// | ||||||||
2221 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2222 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2223 | ExprResult RebuildDeclRefExpr(NestedNameSpecifierLoc QualifierLoc, | ||||||||
2224 | ValueDecl *VD, | ||||||||
2225 | const DeclarationNameInfo &NameInfo, | ||||||||
2226 | NamedDecl *Found, | ||||||||
2227 | TemplateArgumentListInfo *TemplateArgs) { | ||||||||
2228 | CXXScopeSpec SS; | ||||||||
2229 | SS.Adopt(QualifierLoc); | ||||||||
2230 | return getSema().BuildDeclarationNameExpr(SS, NameInfo, VD, Found, | ||||||||
2231 | TemplateArgs); | ||||||||
2232 | } | ||||||||
2233 | |||||||||
2234 | /// Build a new expression in parentheses. | ||||||||
2235 | /// | ||||||||
2236 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2237 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2238 | ExprResult RebuildParenExpr(Expr *SubExpr, SourceLocation LParen, | ||||||||
2239 | SourceLocation RParen) { | ||||||||
2240 | return getSema().ActOnParenExpr(LParen, RParen, SubExpr); | ||||||||
2241 | } | ||||||||
2242 | |||||||||
2243 | /// Build a new pseudo-destructor expression. | ||||||||
2244 | /// | ||||||||
2245 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2246 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2247 | ExprResult RebuildCXXPseudoDestructorExpr(Expr *Base, | ||||||||
2248 | SourceLocation OperatorLoc, | ||||||||
2249 | bool isArrow, | ||||||||
2250 | CXXScopeSpec &SS, | ||||||||
2251 | TypeSourceInfo *ScopeType, | ||||||||
2252 | SourceLocation CCLoc, | ||||||||
2253 | SourceLocation TildeLoc, | ||||||||
2254 | PseudoDestructorTypeStorage Destroyed); | ||||||||
2255 | |||||||||
2256 | /// Build a new unary operator expression. | ||||||||
2257 | /// | ||||||||
2258 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2259 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2260 | ExprResult RebuildUnaryOperator(SourceLocation OpLoc, | ||||||||
2261 | UnaryOperatorKind Opc, | ||||||||
2262 | Expr *SubExpr) { | ||||||||
2263 | return getSema().BuildUnaryOp(/*Scope=*/nullptr, OpLoc, Opc, SubExpr); | ||||||||
2264 | } | ||||||||
2265 | |||||||||
2266 | /// Build a new builtin offsetof expression. | ||||||||
2267 | /// | ||||||||
2268 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2269 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2270 | ExprResult RebuildOffsetOfExpr(SourceLocation OperatorLoc, | ||||||||
2271 | TypeSourceInfo *Type, | ||||||||
2272 | ArrayRef<Sema::OffsetOfComponent> Components, | ||||||||
2273 | SourceLocation RParenLoc) { | ||||||||
2274 | return getSema().BuildBuiltinOffsetOf(OperatorLoc, Type, Components, | ||||||||
2275 | RParenLoc); | ||||||||
2276 | } | ||||||||
2277 | |||||||||
2278 | /// Build a new sizeof, alignof or vec_step expression with a | ||||||||
2279 | /// type argument. | ||||||||
2280 | /// | ||||||||
2281 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2282 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2283 | ExprResult RebuildUnaryExprOrTypeTrait(TypeSourceInfo *TInfo, | ||||||||
2284 | SourceLocation OpLoc, | ||||||||
2285 | UnaryExprOrTypeTrait ExprKind, | ||||||||
2286 | SourceRange R) { | ||||||||
2287 | return getSema().CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, R); | ||||||||
2288 | } | ||||||||
2289 | |||||||||
2290 | /// Build a new sizeof, alignof or vec step expression with an | ||||||||
2291 | /// expression argument. | ||||||||
2292 | /// | ||||||||
2293 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2294 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2295 | ExprResult RebuildUnaryExprOrTypeTrait(Expr *SubExpr, SourceLocation OpLoc, | ||||||||
2296 | UnaryExprOrTypeTrait ExprKind, | ||||||||
2297 | SourceRange R) { | ||||||||
2298 | ExprResult Result | ||||||||
2299 | = getSema().CreateUnaryExprOrTypeTraitExpr(SubExpr, OpLoc, ExprKind); | ||||||||
2300 | if (Result.isInvalid()) | ||||||||
2301 | return ExprError(); | ||||||||
2302 | |||||||||
2303 | return Result; | ||||||||
2304 | } | ||||||||
2305 | |||||||||
2306 | /// Build a new array subscript expression. | ||||||||
2307 | /// | ||||||||
2308 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2309 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2310 | ExprResult RebuildArraySubscriptExpr(Expr *LHS, | ||||||||
2311 | SourceLocation LBracketLoc, | ||||||||
2312 | Expr *RHS, | ||||||||
2313 | SourceLocation RBracketLoc) { | ||||||||
2314 | return getSema().ActOnArraySubscriptExpr(/*Scope=*/nullptr, LHS, | ||||||||
2315 | LBracketLoc, RHS, | ||||||||
2316 | RBracketLoc); | ||||||||
2317 | } | ||||||||
2318 | |||||||||
2319 | /// Build a new array section expression. | ||||||||
2320 | /// | ||||||||
2321 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2322 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2323 | ExprResult RebuildOMPArraySectionExpr(Expr *Base, SourceLocation LBracketLoc, | ||||||||
2324 | Expr *LowerBound, | ||||||||
2325 | SourceLocation ColonLoc, Expr *Length, | ||||||||
2326 | SourceLocation RBracketLoc) { | ||||||||
2327 | return getSema().ActOnOMPArraySectionExpr(Base, LBracketLoc, LowerBound, | ||||||||
2328 | ColonLoc, Length, RBracketLoc); | ||||||||
2329 | } | ||||||||
2330 | |||||||||
2331 | /// Build a new call expression. | ||||||||
2332 | /// | ||||||||
2333 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2334 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2335 | ExprResult RebuildCallExpr(Expr *Callee, SourceLocation LParenLoc, | ||||||||
2336 | MultiExprArg Args, | ||||||||
2337 | SourceLocation RParenLoc, | ||||||||
2338 | Expr *ExecConfig = nullptr) { | ||||||||
2339 | return getSema().BuildCallExpr(/*Scope=*/nullptr, Callee, LParenLoc, Args, | ||||||||
2340 | RParenLoc, ExecConfig); | ||||||||
2341 | } | ||||||||
2342 | |||||||||
2343 | /// Build a new member access expression. | ||||||||
2344 | /// | ||||||||
2345 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2346 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2347 | ExprResult RebuildMemberExpr(Expr *Base, SourceLocation OpLoc, | ||||||||
2348 | bool isArrow, | ||||||||
2349 | NestedNameSpecifierLoc QualifierLoc, | ||||||||
2350 | SourceLocation TemplateKWLoc, | ||||||||
2351 | const DeclarationNameInfo &MemberNameInfo, | ||||||||
2352 | ValueDecl *Member, | ||||||||
2353 | NamedDecl *FoundDecl, | ||||||||
2354 | const TemplateArgumentListInfo *ExplicitTemplateArgs, | ||||||||
2355 | NamedDecl *FirstQualifierInScope) { | ||||||||
2356 | ExprResult BaseResult = getSema().PerformMemberExprBaseConversion(Base, | ||||||||
2357 | isArrow); | ||||||||
2358 | if (!Member->getDeclName()) { | ||||||||
2359 | // We have a reference to an unnamed field. This is always the | ||||||||
2360 | // base of an anonymous struct/union member access, i.e. the | ||||||||
2361 | // field is always of record type. | ||||||||
2362 | assert(Member->getType()->isRecordType() &&((Member->getType()->isRecordType() && "unnamed member not of record type?" ) ? static_cast<void> (0) : __assert_fail ("Member->getType()->isRecordType() && \"unnamed member not of record type?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 2363, __PRETTY_FUNCTION__)) | ||||||||
2363 | "unnamed member not of record type?")((Member->getType()->isRecordType() && "unnamed member not of record type?" ) ? static_cast<void> (0) : __assert_fail ("Member->getType()->isRecordType() && \"unnamed member not of record type?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 2363, __PRETTY_FUNCTION__)); | ||||||||
2364 | |||||||||
2365 | BaseResult = | ||||||||
2366 | getSema().PerformObjectMemberConversion(BaseResult.get(), | ||||||||
2367 | QualifierLoc.getNestedNameSpecifier(), | ||||||||
2368 | FoundDecl, Member); | ||||||||
2369 | if (BaseResult.isInvalid()) | ||||||||
2370 | return ExprError(); | ||||||||
2371 | Base = BaseResult.get(); | ||||||||
2372 | |||||||||
2373 | CXXScopeSpec EmptySS; | ||||||||
2374 | return getSema().BuildFieldReferenceExpr( | ||||||||
2375 | Base, isArrow, OpLoc, EmptySS, cast<FieldDecl>(Member), | ||||||||
2376 | DeclAccessPair::make(FoundDecl, FoundDecl->getAccess()), MemberNameInfo); | ||||||||
2377 | } | ||||||||
2378 | |||||||||
2379 | CXXScopeSpec SS; | ||||||||
2380 | SS.Adopt(QualifierLoc); | ||||||||
2381 | |||||||||
2382 | Base = BaseResult.get(); | ||||||||
2383 | QualType BaseType = Base->getType(); | ||||||||
2384 | |||||||||
2385 | if (isArrow && !BaseType->isPointerType()) | ||||||||
2386 | return ExprError(); | ||||||||
2387 | |||||||||
2388 | // FIXME: this involves duplicating earlier analysis in a lot of | ||||||||
2389 | // cases; we should avoid this when possible. | ||||||||
2390 | LookupResult R(getSema(), MemberNameInfo, Sema::LookupMemberName); | ||||||||
2391 | R.addDecl(FoundDecl); | ||||||||
2392 | R.resolveKind(); | ||||||||
2393 | |||||||||
2394 | return getSema().BuildMemberReferenceExpr(Base, BaseType, OpLoc, isArrow, | ||||||||
2395 | SS, TemplateKWLoc, | ||||||||
2396 | FirstQualifierInScope, | ||||||||
2397 | R, ExplicitTemplateArgs, | ||||||||
2398 | /*S*/nullptr); | ||||||||
2399 | } | ||||||||
2400 | |||||||||
2401 | /// Build a new binary operator expression. | ||||||||
2402 | /// | ||||||||
2403 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2404 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2405 | ExprResult RebuildBinaryOperator(SourceLocation OpLoc, | ||||||||
2406 | BinaryOperatorKind Opc, | ||||||||
2407 | Expr *LHS, Expr *RHS) { | ||||||||
2408 | return getSema().BuildBinOp(/*Scope=*/nullptr, OpLoc, Opc, LHS, RHS); | ||||||||
2409 | } | ||||||||
2410 | |||||||||
2411 | /// Build a new rewritten operator expression. | ||||||||
2412 | /// | ||||||||
2413 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2414 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2415 | ExprResult RebuildCXXRewrittenBinaryOperator( | ||||||||
2416 | SourceLocation OpLoc, BinaryOperatorKind Opcode, | ||||||||
2417 | const UnresolvedSetImpl &UnqualLookups, Expr *LHS, Expr *RHS) { | ||||||||
2418 | return getSema().CreateOverloadedBinOp(OpLoc, Opcode, UnqualLookups, LHS, | ||||||||
2419 | RHS, /*RequiresADL*/false); | ||||||||
2420 | } | ||||||||
2421 | |||||||||
2422 | /// Build a new conditional operator expression. | ||||||||
2423 | /// | ||||||||
2424 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2425 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2426 | ExprResult RebuildConditionalOperator(Expr *Cond, | ||||||||
2427 | SourceLocation QuestionLoc, | ||||||||
2428 | Expr *LHS, | ||||||||
2429 | SourceLocation ColonLoc, | ||||||||
2430 | Expr *RHS) { | ||||||||
2431 | return getSema().ActOnConditionalOp(QuestionLoc, ColonLoc, Cond, | ||||||||
2432 | LHS, RHS); | ||||||||
2433 | } | ||||||||
2434 | |||||||||
2435 | /// Build a new C-style cast expression. | ||||||||
2436 | /// | ||||||||
2437 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2438 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2439 | ExprResult RebuildCStyleCastExpr(SourceLocation LParenLoc, | ||||||||
2440 | TypeSourceInfo *TInfo, | ||||||||
2441 | SourceLocation RParenLoc, | ||||||||
2442 | Expr *SubExpr) { | ||||||||
2443 | return getSema().BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, | ||||||||
2444 | SubExpr); | ||||||||
2445 | } | ||||||||
2446 | |||||||||
2447 | /// Build a new compound literal expression. | ||||||||
2448 | /// | ||||||||
2449 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2450 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2451 | ExprResult RebuildCompoundLiteralExpr(SourceLocation LParenLoc, | ||||||||
2452 | TypeSourceInfo *TInfo, | ||||||||
2453 | SourceLocation RParenLoc, | ||||||||
2454 | Expr *Init) { | ||||||||
2455 | return getSema().BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, | ||||||||
2456 | Init); | ||||||||
2457 | } | ||||||||
2458 | |||||||||
2459 | /// Build a new extended vector element access expression. | ||||||||
2460 | /// | ||||||||
2461 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2462 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2463 | ExprResult RebuildExtVectorElementExpr(Expr *Base, | ||||||||
2464 | SourceLocation OpLoc, | ||||||||
2465 | SourceLocation AccessorLoc, | ||||||||
2466 | IdentifierInfo &Accessor) { | ||||||||
2467 | |||||||||
2468 | CXXScopeSpec SS; | ||||||||
2469 | DeclarationNameInfo NameInfo(&Accessor, AccessorLoc); | ||||||||
2470 | return getSema().BuildMemberReferenceExpr(Base, Base->getType(), | ||||||||
2471 | OpLoc, /*IsArrow*/ false, | ||||||||
2472 | SS, SourceLocation(), | ||||||||
2473 | /*FirstQualifierInScope*/ nullptr, | ||||||||
2474 | NameInfo, | ||||||||
2475 | /* TemplateArgs */ nullptr, | ||||||||
2476 | /*S*/ nullptr); | ||||||||
2477 | } | ||||||||
2478 | |||||||||
2479 | /// Build a new initializer list expression. | ||||||||
2480 | /// | ||||||||
2481 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2482 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2483 | ExprResult RebuildInitList(SourceLocation LBraceLoc, | ||||||||
2484 | MultiExprArg Inits, | ||||||||
2485 | SourceLocation RBraceLoc) { | ||||||||
2486 | return SemaRef.BuildInitList(LBraceLoc, Inits, RBraceLoc); | ||||||||
2487 | } | ||||||||
2488 | |||||||||
2489 | /// Build a new designated initializer expression. | ||||||||
2490 | /// | ||||||||
2491 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2492 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2493 | ExprResult RebuildDesignatedInitExpr(Designation &Desig, | ||||||||
2494 | MultiExprArg ArrayExprs, | ||||||||
2495 | SourceLocation EqualOrColonLoc, | ||||||||
2496 | bool GNUSyntax, | ||||||||
2497 | Expr *Init) { | ||||||||
2498 | ExprResult Result | ||||||||
2499 | = SemaRef.ActOnDesignatedInitializer(Desig, EqualOrColonLoc, GNUSyntax, | ||||||||
2500 | Init); | ||||||||
2501 | if (Result.isInvalid()) | ||||||||
2502 | return ExprError(); | ||||||||
2503 | |||||||||
2504 | return Result; | ||||||||
2505 | } | ||||||||
2506 | |||||||||
2507 | /// Build a new value-initialized expression. | ||||||||
2508 | /// | ||||||||
2509 | /// By default, builds the implicit value initialization without performing | ||||||||
2510 | /// any semantic analysis. Subclasses may override this routine to provide | ||||||||
2511 | /// different behavior. | ||||||||
2512 | ExprResult RebuildImplicitValueInitExpr(QualType T) { | ||||||||
2513 | return new (SemaRef.Context) ImplicitValueInitExpr(T); | ||||||||
2514 | } | ||||||||
2515 | |||||||||
2516 | /// Build a new \c va_arg expression. | ||||||||
2517 | /// | ||||||||
2518 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2519 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2520 | ExprResult RebuildVAArgExpr(SourceLocation BuiltinLoc, | ||||||||
2521 | Expr *SubExpr, TypeSourceInfo *TInfo, | ||||||||
2522 | SourceLocation RParenLoc) { | ||||||||
2523 | return getSema().BuildVAArgExpr(BuiltinLoc, | ||||||||
2524 | SubExpr, TInfo, | ||||||||
2525 | RParenLoc); | ||||||||
2526 | } | ||||||||
2527 | |||||||||
2528 | /// Build a new expression list in parentheses. | ||||||||
2529 | /// | ||||||||
2530 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2531 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2532 | ExprResult RebuildParenListExpr(SourceLocation LParenLoc, | ||||||||
2533 | MultiExprArg SubExprs, | ||||||||
2534 | SourceLocation RParenLoc) { | ||||||||
2535 | return getSema().ActOnParenListExpr(LParenLoc, RParenLoc, SubExprs); | ||||||||
2536 | } | ||||||||
2537 | |||||||||
2538 | /// Build a new address-of-label expression. | ||||||||
2539 | /// | ||||||||
2540 | /// By default, performs semantic analysis, using the name of the label | ||||||||
2541 | /// rather than attempting to map the label statement itself. | ||||||||
2542 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2543 | ExprResult RebuildAddrLabelExpr(SourceLocation AmpAmpLoc, | ||||||||
2544 | SourceLocation LabelLoc, LabelDecl *Label) { | ||||||||
2545 | return getSema().ActOnAddrLabel(AmpAmpLoc, LabelLoc, Label); | ||||||||
2546 | } | ||||||||
2547 | |||||||||
2548 | /// Build a new GNU statement expression. | ||||||||
2549 | /// | ||||||||
2550 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2551 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2552 | ExprResult RebuildStmtExpr(SourceLocation LParenLoc, | ||||||||
2553 | Stmt *SubStmt, | ||||||||
2554 | SourceLocation RParenLoc) { | ||||||||
2555 | return getSema().ActOnStmtExpr(LParenLoc, SubStmt, RParenLoc); | ||||||||
2556 | } | ||||||||
2557 | |||||||||
2558 | /// Build a new __builtin_choose_expr expression. | ||||||||
2559 | /// | ||||||||
2560 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2561 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2562 | ExprResult RebuildChooseExpr(SourceLocation BuiltinLoc, | ||||||||
2563 | Expr *Cond, Expr *LHS, Expr *RHS, | ||||||||
2564 | SourceLocation RParenLoc) { | ||||||||
2565 | return SemaRef.ActOnChooseExpr(BuiltinLoc, | ||||||||
2566 | Cond, LHS, RHS, | ||||||||
2567 | RParenLoc); | ||||||||
2568 | } | ||||||||
2569 | |||||||||
2570 | /// Build a new generic selection expression. | ||||||||
2571 | /// | ||||||||
2572 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2573 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2574 | ExprResult RebuildGenericSelectionExpr(SourceLocation KeyLoc, | ||||||||
2575 | SourceLocation DefaultLoc, | ||||||||
2576 | SourceLocation RParenLoc, | ||||||||
2577 | Expr *ControllingExpr, | ||||||||
2578 | ArrayRef<TypeSourceInfo *> Types, | ||||||||
2579 | ArrayRef<Expr *> Exprs) { | ||||||||
2580 | return getSema().CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc, | ||||||||
2581 | ControllingExpr, Types, Exprs); | ||||||||
2582 | } | ||||||||
2583 | |||||||||
2584 | /// Build a new overloaded operator call expression. | ||||||||
2585 | /// | ||||||||
2586 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2587 | /// The semantic analysis provides the behavior of template instantiation, | ||||||||
2588 | /// copying with transformations that turn what looks like an overloaded | ||||||||
2589 | /// operator call into a use of a builtin operator, performing | ||||||||
2590 | /// argument-dependent lookup, etc. Subclasses may override this routine to | ||||||||
2591 | /// provide different behavior. | ||||||||
2592 | ExprResult RebuildCXXOperatorCallExpr(OverloadedOperatorKind Op, | ||||||||
2593 | SourceLocation OpLoc, | ||||||||
2594 | Expr *Callee, | ||||||||
2595 | Expr *First, | ||||||||
2596 | Expr *Second); | ||||||||
2597 | |||||||||
2598 | /// Build a new C++ "named" cast expression, such as static_cast or | ||||||||
2599 | /// reinterpret_cast. | ||||||||
2600 | /// | ||||||||
2601 | /// By default, this routine dispatches to one of the more-specific routines | ||||||||
2602 | /// for a particular named case, e.g., RebuildCXXStaticCastExpr(). | ||||||||
2603 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2604 | ExprResult RebuildCXXNamedCastExpr(SourceLocation OpLoc, | ||||||||
2605 | Stmt::StmtClass Class, | ||||||||
2606 | SourceLocation LAngleLoc, | ||||||||
2607 | TypeSourceInfo *TInfo, | ||||||||
2608 | SourceLocation RAngleLoc, | ||||||||
2609 | SourceLocation LParenLoc, | ||||||||
2610 | Expr *SubExpr, | ||||||||
2611 | SourceLocation RParenLoc) { | ||||||||
2612 | switch (Class) { | ||||||||
2613 | case Stmt::CXXStaticCastExprClass: | ||||||||
2614 | return getDerived().RebuildCXXStaticCastExpr(OpLoc, LAngleLoc, TInfo, | ||||||||
2615 | RAngleLoc, LParenLoc, | ||||||||
2616 | SubExpr, RParenLoc); | ||||||||
2617 | |||||||||
2618 | case Stmt::CXXDynamicCastExprClass: | ||||||||
2619 | return getDerived().RebuildCXXDynamicCastExpr(OpLoc, LAngleLoc, TInfo, | ||||||||
2620 | RAngleLoc, LParenLoc, | ||||||||
2621 | SubExpr, RParenLoc); | ||||||||
2622 | |||||||||
2623 | case Stmt::CXXReinterpretCastExprClass: | ||||||||
2624 | return getDerived().RebuildCXXReinterpretCastExpr(OpLoc, LAngleLoc, TInfo, | ||||||||
2625 | RAngleLoc, LParenLoc, | ||||||||
2626 | SubExpr, | ||||||||
2627 | RParenLoc); | ||||||||
2628 | |||||||||
2629 | case Stmt::CXXConstCastExprClass: | ||||||||
2630 | return getDerived().RebuildCXXConstCastExpr(OpLoc, LAngleLoc, TInfo, | ||||||||
2631 | RAngleLoc, LParenLoc, | ||||||||
2632 | SubExpr, RParenLoc); | ||||||||
2633 | |||||||||
2634 | default: | ||||||||
2635 | llvm_unreachable("Invalid C++ named cast")::llvm::llvm_unreachable_internal("Invalid C++ named cast", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 2635); | ||||||||
2636 | } | ||||||||
2637 | } | ||||||||
2638 | |||||||||
2639 | /// Build a new C++ static_cast expression. | ||||||||
2640 | /// | ||||||||
2641 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2642 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2643 | ExprResult RebuildCXXStaticCastExpr(SourceLocation OpLoc, | ||||||||
2644 | SourceLocation LAngleLoc, | ||||||||
2645 | TypeSourceInfo *TInfo, | ||||||||
2646 | SourceLocation RAngleLoc, | ||||||||
2647 | SourceLocation LParenLoc, | ||||||||
2648 | Expr *SubExpr, | ||||||||
2649 | SourceLocation RParenLoc) { | ||||||||
2650 | return getSema().BuildCXXNamedCast(OpLoc, tok::kw_static_cast, | ||||||||
2651 | TInfo, SubExpr, | ||||||||
2652 | SourceRange(LAngleLoc, RAngleLoc), | ||||||||
2653 | SourceRange(LParenLoc, RParenLoc)); | ||||||||
2654 | } | ||||||||
2655 | |||||||||
2656 | /// Build a new C++ dynamic_cast expression. | ||||||||
2657 | /// | ||||||||
2658 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2659 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2660 | ExprResult RebuildCXXDynamicCastExpr(SourceLocation OpLoc, | ||||||||
2661 | SourceLocation LAngleLoc, | ||||||||
2662 | TypeSourceInfo *TInfo, | ||||||||
2663 | SourceLocation RAngleLoc, | ||||||||
2664 | SourceLocation LParenLoc, | ||||||||
2665 | Expr *SubExpr, | ||||||||
2666 | SourceLocation RParenLoc) { | ||||||||
2667 | return getSema().BuildCXXNamedCast(OpLoc, tok::kw_dynamic_cast, | ||||||||
2668 | TInfo, SubExpr, | ||||||||
2669 | SourceRange(LAngleLoc, RAngleLoc), | ||||||||
2670 | SourceRange(LParenLoc, RParenLoc)); | ||||||||
2671 | } | ||||||||
2672 | |||||||||
2673 | /// Build a new C++ reinterpret_cast expression. | ||||||||
2674 | /// | ||||||||
2675 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2676 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2677 | ExprResult RebuildCXXReinterpretCastExpr(SourceLocation OpLoc, | ||||||||
2678 | SourceLocation LAngleLoc, | ||||||||
2679 | TypeSourceInfo *TInfo, | ||||||||
2680 | SourceLocation RAngleLoc, | ||||||||
2681 | SourceLocation LParenLoc, | ||||||||
2682 | Expr *SubExpr, | ||||||||
2683 | SourceLocation RParenLoc) { | ||||||||
2684 | return getSema().BuildCXXNamedCast(OpLoc, tok::kw_reinterpret_cast, | ||||||||
2685 | TInfo, SubExpr, | ||||||||
2686 | SourceRange(LAngleLoc, RAngleLoc), | ||||||||
2687 | SourceRange(LParenLoc, RParenLoc)); | ||||||||
2688 | } | ||||||||
2689 | |||||||||
2690 | /// Build a new C++ const_cast expression. | ||||||||
2691 | /// | ||||||||
2692 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2693 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2694 | ExprResult RebuildCXXConstCastExpr(SourceLocation OpLoc, | ||||||||
2695 | SourceLocation LAngleLoc, | ||||||||
2696 | TypeSourceInfo *TInfo, | ||||||||
2697 | SourceLocation RAngleLoc, | ||||||||
2698 | SourceLocation LParenLoc, | ||||||||
2699 | Expr *SubExpr, | ||||||||
2700 | SourceLocation RParenLoc) { | ||||||||
2701 | return getSema().BuildCXXNamedCast(OpLoc, tok::kw_const_cast, | ||||||||
2702 | TInfo, SubExpr, | ||||||||
2703 | SourceRange(LAngleLoc, RAngleLoc), | ||||||||
2704 | SourceRange(LParenLoc, RParenLoc)); | ||||||||
2705 | } | ||||||||
2706 | |||||||||
2707 | /// Build a new C++ functional-style cast expression. | ||||||||
2708 | /// | ||||||||
2709 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2710 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2711 | ExprResult RebuildCXXFunctionalCastExpr(TypeSourceInfo *TInfo, | ||||||||
2712 | SourceLocation LParenLoc, | ||||||||
2713 | Expr *Sub, | ||||||||
2714 | SourceLocation RParenLoc, | ||||||||
2715 | bool ListInitialization) { | ||||||||
2716 | return getSema().BuildCXXTypeConstructExpr(TInfo, LParenLoc, | ||||||||
2717 | MultiExprArg(&Sub, 1), RParenLoc, | ||||||||
2718 | ListInitialization); | ||||||||
2719 | } | ||||||||
2720 | |||||||||
2721 | /// Build a new C++ __builtin_bit_cast expression. | ||||||||
2722 | /// | ||||||||
2723 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2724 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2725 | ExprResult RebuildBuiltinBitCastExpr(SourceLocation KWLoc, | ||||||||
2726 | TypeSourceInfo *TSI, Expr *Sub, | ||||||||
2727 | SourceLocation RParenLoc) { | ||||||||
2728 | return getSema().BuildBuiltinBitCastExpr(KWLoc, TSI, Sub, RParenLoc); | ||||||||
2729 | } | ||||||||
2730 | |||||||||
2731 | /// Build a new C++ typeid(type) expression. | ||||||||
2732 | /// | ||||||||
2733 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2734 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2735 | ExprResult RebuildCXXTypeidExpr(QualType TypeInfoType, | ||||||||
2736 | SourceLocation TypeidLoc, | ||||||||
2737 | TypeSourceInfo *Operand, | ||||||||
2738 | SourceLocation RParenLoc) { | ||||||||
2739 | return getSema().BuildCXXTypeId(TypeInfoType, TypeidLoc, Operand, | ||||||||
2740 | RParenLoc); | ||||||||
2741 | } | ||||||||
2742 | |||||||||
2743 | |||||||||
2744 | /// Build a new C++ typeid(expr) expression. | ||||||||
2745 | /// | ||||||||
2746 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2747 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2748 | ExprResult RebuildCXXTypeidExpr(QualType TypeInfoType, | ||||||||
2749 | SourceLocation TypeidLoc, | ||||||||
2750 | Expr *Operand, | ||||||||
2751 | SourceLocation RParenLoc) { | ||||||||
2752 | return getSema().BuildCXXTypeId(TypeInfoType, TypeidLoc, Operand, | ||||||||
2753 | RParenLoc); | ||||||||
2754 | } | ||||||||
2755 | |||||||||
2756 | /// Build a new C++ __uuidof(type) expression. | ||||||||
2757 | /// | ||||||||
2758 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2759 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2760 | ExprResult RebuildCXXUuidofExpr(QualType TypeInfoType, | ||||||||
2761 | SourceLocation TypeidLoc, | ||||||||
2762 | TypeSourceInfo *Operand, | ||||||||
2763 | SourceLocation RParenLoc) { | ||||||||
2764 | return getSema().BuildCXXUuidof(TypeInfoType, TypeidLoc, Operand, | ||||||||
2765 | RParenLoc); | ||||||||
2766 | } | ||||||||
2767 | |||||||||
2768 | /// Build a new C++ __uuidof(expr) expression. | ||||||||
2769 | /// | ||||||||
2770 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2771 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2772 | ExprResult RebuildCXXUuidofExpr(QualType TypeInfoType, | ||||||||
2773 | SourceLocation TypeidLoc, | ||||||||
2774 | Expr *Operand, | ||||||||
2775 | SourceLocation RParenLoc) { | ||||||||
2776 | return getSema().BuildCXXUuidof(TypeInfoType, TypeidLoc, Operand, | ||||||||
2777 | RParenLoc); | ||||||||
2778 | } | ||||||||
2779 | |||||||||
2780 | /// Build a new C++ "this" expression. | ||||||||
2781 | /// | ||||||||
2782 | /// By default, builds a new "this" expression without performing any | ||||||||
2783 | /// semantic analysis. Subclasses may override this routine to provide | ||||||||
2784 | /// different behavior. | ||||||||
2785 | ExprResult RebuildCXXThisExpr(SourceLocation ThisLoc, | ||||||||
2786 | QualType ThisType, | ||||||||
2787 | bool isImplicit) { | ||||||||
2788 | return getSema().BuildCXXThisExpr(ThisLoc, ThisType, isImplicit); | ||||||||
2789 | } | ||||||||
2790 | |||||||||
2791 | /// Build a new C++ throw expression. | ||||||||
2792 | /// | ||||||||
2793 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2794 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2795 | ExprResult RebuildCXXThrowExpr(SourceLocation ThrowLoc, Expr *Sub, | ||||||||
2796 | bool IsThrownVariableInScope) { | ||||||||
2797 | return getSema().BuildCXXThrow(ThrowLoc, Sub, IsThrownVariableInScope); | ||||||||
2798 | } | ||||||||
2799 | |||||||||
2800 | /// Build a new C++ default-argument expression. | ||||||||
2801 | /// | ||||||||
2802 | /// By default, builds a new default-argument expression, which does not | ||||||||
2803 | /// require any semantic analysis. Subclasses may override this routine to | ||||||||
2804 | /// provide different behavior. | ||||||||
2805 | ExprResult RebuildCXXDefaultArgExpr(SourceLocation Loc, ParmVarDecl *Param) { | ||||||||
2806 | return CXXDefaultArgExpr::Create(getSema().Context, Loc, Param, | ||||||||
2807 | getSema().CurContext); | ||||||||
2808 | } | ||||||||
2809 | |||||||||
2810 | /// Build a new C++11 default-initialization expression. | ||||||||
2811 | /// | ||||||||
2812 | /// By default, builds a new default field initialization expression, which | ||||||||
2813 | /// does not require any semantic analysis. Subclasses may override this | ||||||||
2814 | /// routine to provide different behavior. | ||||||||
2815 | ExprResult RebuildCXXDefaultInitExpr(SourceLocation Loc, | ||||||||
2816 | FieldDecl *Field) { | ||||||||
2817 | return CXXDefaultInitExpr::Create(getSema().Context, Loc, Field, | ||||||||
2818 | getSema().CurContext); | ||||||||
2819 | } | ||||||||
2820 | |||||||||
2821 | /// Build a new C++ zero-initialization expression. | ||||||||
2822 | /// | ||||||||
2823 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2824 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2825 | ExprResult RebuildCXXScalarValueInitExpr(TypeSourceInfo *TSInfo, | ||||||||
2826 | SourceLocation LParenLoc, | ||||||||
2827 | SourceLocation RParenLoc) { | ||||||||
2828 | return getSema().BuildCXXTypeConstructExpr( | ||||||||
2829 | TSInfo, LParenLoc, None, RParenLoc, /*ListInitialization=*/false); | ||||||||
2830 | } | ||||||||
2831 | |||||||||
2832 | /// Build a new C++ "new" expression. | ||||||||
2833 | /// | ||||||||
2834 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2835 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2836 | ExprResult RebuildCXXNewExpr(SourceLocation StartLoc, | ||||||||
2837 | bool UseGlobal, | ||||||||
2838 | SourceLocation PlacementLParen, | ||||||||
2839 | MultiExprArg PlacementArgs, | ||||||||
2840 | SourceLocation PlacementRParen, | ||||||||
2841 | SourceRange TypeIdParens, | ||||||||
2842 | QualType AllocatedType, | ||||||||
2843 | TypeSourceInfo *AllocatedTypeInfo, | ||||||||
2844 | Optional<Expr *> ArraySize, | ||||||||
2845 | SourceRange DirectInitRange, | ||||||||
2846 | Expr *Initializer) { | ||||||||
2847 | return getSema().BuildCXXNew(StartLoc, UseGlobal, | ||||||||
2848 | PlacementLParen, | ||||||||
2849 | PlacementArgs, | ||||||||
2850 | PlacementRParen, | ||||||||
2851 | TypeIdParens, | ||||||||
2852 | AllocatedType, | ||||||||
2853 | AllocatedTypeInfo, | ||||||||
2854 | ArraySize, | ||||||||
2855 | DirectInitRange, | ||||||||
2856 | Initializer); | ||||||||
2857 | } | ||||||||
2858 | |||||||||
2859 | /// Build a new C++ "delete" expression. | ||||||||
2860 | /// | ||||||||
2861 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2862 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2863 | ExprResult RebuildCXXDeleteExpr(SourceLocation StartLoc, | ||||||||
2864 | bool IsGlobalDelete, | ||||||||
2865 | bool IsArrayForm, | ||||||||
2866 | Expr *Operand) { | ||||||||
2867 | return getSema().ActOnCXXDelete(StartLoc, IsGlobalDelete, IsArrayForm, | ||||||||
2868 | Operand); | ||||||||
2869 | } | ||||||||
2870 | |||||||||
2871 | /// Build a new type trait expression. | ||||||||
2872 | /// | ||||||||
2873 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2874 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2875 | ExprResult RebuildTypeTrait(TypeTrait Trait, | ||||||||
2876 | SourceLocation StartLoc, | ||||||||
2877 | ArrayRef<TypeSourceInfo *> Args, | ||||||||
2878 | SourceLocation RParenLoc) { | ||||||||
2879 | return getSema().BuildTypeTrait(Trait, StartLoc, Args, RParenLoc); | ||||||||
2880 | } | ||||||||
2881 | |||||||||
2882 | /// Build a new array type trait expression. | ||||||||
2883 | /// | ||||||||
2884 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2885 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2886 | ExprResult RebuildArrayTypeTrait(ArrayTypeTrait Trait, | ||||||||
2887 | SourceLocation StartLoc, | ||||||||
2888 | TypeSourceInfo *TSInfo, | ||||||||
2889 | Expr *DimExpr, | ||||||||
2890 | SourceLocation RParenLoc) { | ||||||||
2891 | return getSema().BuildArrayTypeTrait(Trait, StartLoc, TSInfo, DimExpr, RParenLoc); | ||||||||
2892 | } | ||||||||
2893 | |||||||||
2894 | /// Build a new expression trait expression. | ||||||||
2895 | /// | ||||||||
2896 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2897 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2898 | ExprResult RebuildExpressionTrait(ExpressionTrait Trait, | ||||||||
2899 | SourceLocation StartLoc, | ||||||||
2900 | Expr *Queried, | ||||||||
2901 | SourceLocation RParenLoc) { | ||||||||
2902 | return getSema().BuildExpressionTrait(Trait, StartLoc, Queried, RParenLoc); | ||||||||
2903 | } | ||||||||
2904 | |||||||||
2905 | /// Build a new (previously unresolved) declaration reference | ||||||||
2906 | /// expression. | ||||||||
2907 | /// | ||||||||
2908 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2909 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2910 | ExprResult RebuildDependentScopeDeclRefExpr( | ||||||||
2911 | NestedNameSpecifierLoc QualifierLoc, | ||||||||
2912 | SourceLocation TemplateKWLoc, | ||||||||
2913 | const DeclarationNameInfo &NameInfo, | ||||||||
2914 | const TemplateArgumentListInfo *TemplateArgs, | ||||||||
2915 | bool IsAddressOfOperand, | ||||||||
2916 | TypeSourceInfo **RecoveryTSI) { | ||||||||
2917 | CXXScopeSpec SS; | ||||||||
2918 | SS.Adopt(QualifierLoc); | ||||||||
2919 | |||||||||
2920 | if (TemplateArgs || TemplateKWLoc.isValid()) | ||||||||
2921 | return getSema().BuildQualifiedTemplateIdExpr(SS, TemplateKWLoc, NameInfo, | ||||||||
2922 | TemplateArgs); | ||||||||
2923 | |||||||||
2924 | return getSema().BuildQualifiedDeclarationNameExpr( | ||||||||
2925 | SS, NameInfo, IsAddressOfOperand, /*S*/nullptr, RecoveryTSI); | ||||||||
2926 | } | ||||||||
2927 | |||||||||
2928 | /// Build a new template-id expression. | ||||||||
2929 | /// | ||||||||
2930 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2931 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2932 | ExprResult RebuildTemplateIdExpr(const CXXScopeSpec &SS, | ||||||||
2933 | SourceLocation TemplateKWLoc, | ||||||||
2934 | LookupResult &R, | ||||||||
2935 | bool RequiresADL, | ||||||||
2936 | const TemplateArgumentListInfo *TemplateArgs) { | ||||||||
2937 | return getSema().BuildTemplateIdExpr(SS, TemplateKWLoc, R, RequiresADL, | ||||||||
2938 | TemplateArgs); | ||||||||
2939 | } | ||||||||
2940 | |||||||||
2941 | /// Build a new object-construction expression. | ||||||||
2942 | /// | ||||||||
2943 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2944 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2945 | ExprResult RebuildCXXConstructExpr(QualType T, | ||||||||
2946 | SourceLocation Loc, | ||||||||
2947 | CXXConstructorDecl *Constructor, | ||||||||
2948 | bool IsElidable, | ||||||||
2949 | MultiExprArg Args, | ||||||||
2950 | bool HadMultipleCandidates, | ||||||||
2951 | bool ListInitialization, | ||||||||
2952 | bool StdInitListInitialization, | ||||||||
2953 | bool RequiresZeroInit, | ||||||||
2954 | CXXConstructExpr::ConstructionKind ConstructKind, | ||||||||
2955 | SourceRange ParenRange) { | ||||||||
2956 | SmallVector<Expr*, 8> ConvertedArgs; | ||||||||
2957 | if (getSema().CompleteConstructorCall(Constructor, Args, Loc, | ||||||||
2958 | ConvertedArgs)) | ||||||||
2959 | return ExprError(); | ||||||||
2960 | |||||||||
2961 | return getSema().BuildCXXConstructExpr(Loc, T, Constructor, | ||||||||
2962 | IsElidable, | ||||||||
2963 | ConvertedArgs, | ||||||||
2964 | HadMultipleCandidates, | ||||||||
2965 | ListInitialization, | ||||||||
2966 | StdInitListInitialization, | ||||||||
2967 | RequiresZeroInit, ConstructKind, | ||||||||
2968 | ParenRange); | ||||||||
2969 | } | ||||||||
2970 | |||||||||
2971 | /// Build a new implicit construction via inherited constructor | ||||||||
2972 | /// expression. | ||||||||
2973 | ExprResult RebuildCXXInheritedCtorInitExpr(QualType T, SourceLocation Loc, | ||||||||
2974 | CXXConstructorDecl *Constructor, | ||||||||
2975 | bool ConstructsVBase, | ||||||||
2976 | bool InheritedFromVBase) { | ||||||||
2977 | return new (getSema().Context) CXXInheritedCtorInitExpr( | ||||||||
2978 | Loc, T, Constructor, ConstructsVBase, InheritedFromVBase); | ||||||||
2979 | } | ||||||||
2980 | |||||||||
2981 | /// Build a new object-construction expression. | ||||||||
2982 | /// | ||||||||
2983 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2984 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2985 | ExprResult RebuildCXXTemporaryObjectExpr(TypeSourceInfo *TSInfo, | ||||||||
2986 | SourceLocation LParenOrBraceLoc, | ||||||||
2987 | MultiExprArg Args, | ||||||||
2988 | SourceLocation RParenOrBraceLoc, | ||||||||
2989 | bool ListInitialization) { | ||||||||
2990 | return getSema().BuildCXXTypeConstructExpr( | ||||||||
2991 | TSInfo, LParenOrBraceLoc, Args, RParenOrBraceLoc, ListInitialization); | ||||||||
2992 | } | ||||||||
2993 | |||||||||
2994 | /// Build a new object-construction expression. | ||||||||
2995 | /// | ||||||||
2996 | /// By default, performs semantic analysis to build the new expression. | ||||||||
2997 | /// Subclasses may override this routine to provide different behavior. | ||||||||
2998 | ExprResult RebuildCXXUnresolvedConstructExpr(TypeSourceInfo *TSInfo, | ||||||||
2999 | SourceLocation LParenLoc, | ||||||||
3000 | MultiExprArg Args, | ||||||||
3001 | SourceLocation RParenLoc, | ||||||||
3002 | bool ListInitialization) { | ||||||||
3003 | return getSema().BuildCXXTypeConstructExpr(TSInfo, LParenLoc, Args, | ||||||||
3004 | RParenLoc, ListInitialization); | ||||||||
3005 | } | ||||||||
3006 | |||||||||
3007 | /// Build a new member reference expression. | ||||||||
3008 | /// | ||||||||
3009 | /// By default, performs semantic analysis to build the new expression. | ||||||||
3010 | /// Subclasses may override this routine to provide different behavior. | ||||||||
3011 | ExprResult RebuildCXXDependentScopeMemberExpr(Expr *BaseE, | ||||||||
3012 | QualType BaseType, | ||||||||
3013 | bool IsArrow, | ||||||||
3014 | SourceLocation OperatorLoc, | ||||||||
3015 | NestedNameSpecifierLoc QualifierLoc, | ||||||||
3016 | SourceLocation TemplateKWLoc, | ||||||||
3017 | NamedDecl *FirstQualifierInScope, | ||||||||
3018 | const DeclarationNameInfo &MemberNameInfo, | ||||||||
3019 | const TemplateArgumentListInfo *TemplateArgs) { | ||||||||
3020 | CXXScopeSpec SS; | ||||||||
3021 | SS.Adopt(QualifierLoc); | ||||||||
3022 | |||||||||
3023 | return SemaRef.BuildMemberReferenceExpr(BaseE, BaseType, | ||||||||
3024 | OperatorLoc, IsArrow, | ||||||||
3025 | SS, TemplateKWLoc, | ||||||||
3026 | FirstQualifierInScope, | ||||||||
3027 | MemberNameInfo, | ||||||||
3028 | TemplateArgs, /*S*/nullptr); | ||||||||
3029 | } | ||||||||
3030 | |||||||||
3031 | /// Build a new member reference expression. | ||||||||
3032 | /// | ||||||||
3033 | /// By default, performs semantic analysis to build the new expression. | ||||||||
3034 | /// Subclasses may override this routine to provide different behavior. | ||||||||
3035 | ExprResult RebuildUnresolvedMemberExpr(Expr *BaseE, QualType BaseType, | ||||||||
3036 | SourceLocation OperatorLoc, | ||||||||
3037 | bool IsArrow, | ||||||||
3038 | NestedNameSpecifierLoc QualifierLoc, | ||||||||
3039 | SourceLocation TemplateKWLoc, | ||||||||
3040 | NamedDecl *FirstQualifierInScope, | ||||||||
3041 | LookupResult &R, | ||||||||
3042 | const TemplateArgumentListInfo *TemplateArgs) { | ||||||||
3043 | CXXScopeSpec SS; | ||||||||
3044 | SS.Adopt(QualifierLoc); | ||||||||
3045 | |||||||||
3046 | return SemaRef.BuildMemberReferenceExpr(BaseE, BaseType, | ||||||||
3047 | OperatorLoc, IsArrow, | ||||||||
3048 | SS, TemplateKWLoc, | ||||||||
3049 | FirstQualifierInScope, | ||||||||
3050 | R, TemplateArgs, /*S*/nullptr); | ||||||||
3051 | } | ||||||||
3052 | |||||||||
3053 | /// Build a new noexcept expression. | ||||||||
3054 | /// | ||||||||
3055 | /// By default, performs semantic analysis to build the new expression. | ||||||||
3056 | /// Subclasses may override this routine to provide different behavior. | ||||||||
3057 | ExprResult RebuildCXXNoexceptExpr(SourceRange Range, Expr *Arg) { | ||||||||
3058 | return SemaRef.BuildCXXNoexceptExpr(Range.getBegin(), Arg, Range.getEnd()); | ||||||||
3059 | } | ||||||||
3060 | |||||||||
3061 | /// Build a new expression to compute the length of a parameter pack. | ||||||||
3062 | ExprResult RebuildSizeOfPackExpr(SourceLocation OperatorLoc, | ||||||||
3063 | NamedDecl *Pack, | ||||||||
3064 | SourceLocation PackLoc, | ||||||||
3065 | SourceLocation RParenLoc, | ||||||||
3066 | Optional<unsigned> Length, | ||||||||
3067 | ArrayRef<TemplateArgument> PartialArgs) { | ||||||||
3068 | return SizeOfPackExpr::Create(SemaRef.Context, OperatorLoc, Pack, PackLoc, | ||||||||
3069 | RParenLoc, Length, PartialArgs); | ||||||||
3070 | } | ||||||||
3071 | |||||||||
3072 | /// Build a new expression representing a call to a source location | ||||||||
3073 | /// builtin. | ||||||||
3074 | /// | ||||||||
3075 | /// By default, performs semantic analysis to build the new expression. | ||||||||
3076 | /// Subclasses may override this routine to provide different behavior. | ||||||||
3077 | ExprResult RebuildSourceLocExpr(SourceLocExpr::IdentKind Kind, | ||||||||
3078 | SourceLocation BuiltinLoc, | ||||||||
3079 | SourceLocation RPLoc, | ||||||||
3080 | DeclContext *ParentContext) { | ||||||||
3081 | return getSema().BuildSourceLocExpr(Kind, BuiltinLoc, RPLoc, ParentContext); | ||||||||
3082 | } | ||||||||
3083 | |||||||||
3084 | /// Build a new Objective-C boxed expression. | ||||||||
3085 | /// | ||||||||
3086 | /// By default, performs semantic analysis to build the new expression. | ||||||||
3087 | /// Subclasses may override this routine to provide different behavior. | ||||||||
3088 | ExprResult RebuildConceptSpecializationExpr(NestedNameSpecifierLoc NNS, | ||||||||
3089 | SourceLocation TemplateKWLoc, DeclarationNameInfo ConceptNameInfo, | ||||||||
3090 | NamedDecl *FoundDecl, ConceptDecl *NamedConcept, | ||||||||
3091 | TemplateArgumentListInfo *TALI) { | ||||||||
3092 | CXXScopeSpec SS; | ||||||||
3093 | SS.Adopt(NNS); | ||||||||
3094 | ExprResult Result = getSema().CheckConceptTemplateId(SS, TemplateKWLoc, | ||||||||
3095 | ConceptNameInfo, | ||||||||
3096 | FoundDecl, | ||||||||
3097 | NamedConcept, TALI); | ||||||||
3098 | if (Result.isInvalid()) | ||||||||
3099 | return ExprError(); | ||||||||
3100 | return Result; | ||||||||
3101 | } | ||||||||
3102 | |||||||||
3103 | /// \brief Build a new requires expression. | ||||||||
3104 | /// | ||||||||
3105 | /// By default, performs semantic analysis to build the new expression. | ||||||||
3106 | /// Subclasses may override this routine to provide different behavior. | ||||||||
3107 | ExprResult RebuildRequiresExpr(SourceLocation RequiresKWLoc, | ||||||||
3108 | RequiresExprBodyDecl *Body, | ||||||||
3109 | ArrayRef<ParmVarDecl *> LocalParameters, | ||||||||
3110 | ArrayRef<concepts::Requirement *> Requirements, | ||||||||
3111 | SourceLocation ClosingBraceLoc) { | ||||||||
3112 | return RequiresExpr::Create(SemaRef.Context, RequiresKWLoc, Body, | ||||||||
3113 | LocalParameters, Requirements, ClosingBraceLoc); | ||||||||
3114 | } | ||||||||
3115 | |||||||||
3116 | concepts::TypeRequirement * | ||||||||
3117 | RebuildTypeRequirement( | ||||||||
3118 | concepts::Requirement::SubstitutionDiagnostic *SubstDiag) { | ||||||||
3119 | return SemaRef.BuildTypeRequirement(SubstDiag); | ||||||||
3120 | } | ||||||||
3121 | |||||||||
3122 | concepts::TypeRequirement *RebuildTypeRequirement(TypeSourceInfo *T) { | ||||||||
3123 | return SemaRef.BuildTypeRequirement(T); | ||||||||
3124 | } | ||||||||
3125 | |||||||||
3126 | concepts::ExprRequirement * | ||||||||
3127 | RebuildExprRequirement( | ||||||||
3128 | concepts::Requirement::SubstitutionDiagnostic *SubstDiag, bool IsSimple, | ||||||||
3129 | SourceLocation NoexceptLoc, | ||||||||
3130 | concepts::ExprRequirement::ReturnTypeRequirement Ret) { | ||||||||
3131 | return SemaRef.BuildExprRequirement(SubstDiag, IsSimple, NoexceptLoc, | ||||||||
3132 | std::move(Ret)); | ||||||||
3133 | } | ||||||||
3134 | |||||||||
3135 | concepts::ExprRequirement * | ||||||||
3136 | RebuildExprRequirement(Expr *E, bool IsSimple, SourceLocation NoexceptLoc, | ||||||||
3137 | concepts::ExprRequirement::ReturnTypeRequirement Ret) { | ||||||||
3138 | return SemaRef.BuildExprRequirement(E, IsSimple, NoexceptLoc, | ||||||||
3139 | std::move(Ret)); | ||||||||
3140 | } | ||||||||
3141 | |||||||||
3142 | concepts::NestedRequirement * | ||||||||
3143 | RebuildNestedRequirement( | ||||||||
3144 | concepts::Requirement::SubstitutionDiagnostic *SubstDiag) { | ||||||||
3145 | return SemaRef.BuildNestedRequirement(SubstDiag); | ||||||||
3146 | } | ||||||||
3147 | |||||||||
3148 | concepts::NestedRequirement *RebuildNestedRequirement(Expr *Constraint) { | ||||||||
3149 | return SemaRef.BuildNestedRequirement(Constraint); | ||||||||
3150 | } | ||||||||
3151 | |||||||||
3152 | /// \brief Build a new Objective-C boxed expression. | ||||||||
3153 | /// | ||||||||
3154 | /// By default, performs semantic analysis to build the new expression. | ||||||||
3155 | /// Subclasses may override this routine to provide different behavior. | ||||||||
3156 | ExprResult RebuildObjCBoxedExpr(SourceRange SR, Expr *ValueExpr) { | ||||||||
3157 | return getSema().BuildObjCBoxedExpr(SR, ValueExpr); | ||||||||
3158 | } | ||||||||
3159 | |||||||||
3160 | /// Build a new Objective-C array literal. | ||||||||
3161 | /// | ||||||||
3162 | /// By default, performs semantic analysis to build the new expression. | ||||||||
3163 | /// Subclasses may override this routine to provide different behavior. | ||||||||
3164 | ExprResult RebuildObjCArrayLiteral(SourceRange Range, | ||||||||
3165 | Expr **Elements, unsigned NumElements) { | ||||||||
3166 | return getSema().BuildObjCArrayLiteral(Range, | ||||||||
3167 | MultiExprArg(Elements, NumElements)); | ||||||||
3168 | } | ||||||||
3169 | |||||||||
3170 | ExprResult RebuildObjCSubscriptRefExpr(SourceLocation RB, | ||||||||
3171 | Expr *Base, Expr *Key, | ||||||||
3172 | ObjCMethodDecl *getterMethod, | ||||||||
3173 | ObjCMethodDecl *setterMethod) { | ||||||||
3174 | return getSema().BuildObjCSubscriptExpression(RB, Base, Key, | ||||||||
3175 | getterMethod, setterMethod); | ||||||||
3176 | } | ||||||||
3177 | |||||||||
3178 | /// Build a new Objective-C dictionary literal. | ||||||||
3179 | /// | ||||||||
3180 | /// By default, performs semantic analysis to build the new expression. | ||||||||
3181 | /// Subclasses may override this routine to provide different behavior. | ||||||||
3182 | ExprResult RebuildObjCDictionaryLiteral(SourceRange Range, | ||||||||
3183 | MutableArrayRef<ObjCDictionaryElement> Elements) { | ||||||||
3184 | return getSema().BuildObjCDictionaryLiteral(Range, Elements); | ||||||||
3185 | } | ||||||||
3186 | |||||||||
3187 | /// Build a new Objective-C \@encode expression. | ||||||||
3188 | /// | ||||||||
3189 | /// By default, performs semantic analysis to build the new expression. | ||||||||
3190 | /// Subclasses may override this routine to provide different behavior. | ||||||||
3191 | ExprResult RebuildObjCEncodeExpr(SourceLocation AtLoc, | ||||||||
3192 | TypeSourceInfo *EncodeTypeInfo, | ||||||||
3193 | SourceLocation RParenLoc) { | ||||||||
3194 | return SemaRef.BuildObjCEncodeExpression(AtLoc, EncodeTypeInfo, RParenLoc); | ||||||||
3195 | } | ||||||||
3196 | |||||||||
3197 | /// Build a new Objective-C class message. | ||||||||
3198 | ExprResult RebuildObjCMessageExpr(TypeSourceInfo *ReceiverTypeInfo, | ||||||||
3199 | Selector Sel, | ||||||||
3200 | ArrayRef<SourceLocation> SelectorLocs, | ||||||||
3201 | ObjCMethodDecl *Method, | ||||||||
3202 | SourceLocation LBracLoc, | ||||||||
3203 | MultiExprArg Args, | ||||||||
3204 | SourceLocation RBracLoc) { | ||||||||
3205 | return SemaRef.BuildClassMessage(ReceiverTypeInfo, | ||||||||
3206 | ReceiverTypeInfo->getType(), | ||||||||
3207 | /*SuperLoc=*/SourceLocation(), | ||||||||
3208 | Sel, Method, LBracLoc, SelectorLocs, | ||||||||
3209 | RBracLoc, Args); | ||||||||
3210 | } | ||||||||
3211 | |||||||||
3212 | /// Build a new Objective-C instance message. | ||||||||
3213 | ExprResult RebuildObjCMessageExpr(Expr *Receiver, | ||||||||
3214 | Selector Sel, | ||||||||
3215 | ArrayRef<SourceLocation> SelectorLocs, | ||||||||
3216 | ObjCMethodDecl *Method, | ||||||||
3217 | SourceLocation LBracLoc, | ||||||||
3218 | MultiExprArg Args, | ||||||||
3219 | SourceLocation RBracLoc) { | ||||||||
3220 | return SemaRef.BuildInstanceMessage(Receiver, | ||||||||
3221 | Receiver->getType(), | ||||||||
3222 | /*SuperLoc=*/SourceLocation(), | ||||||||
3223 | Sel, Method, LBracLoc, SelectorLocs, | ||||||||
3224 | RBracLoc, Args); | ||||||||
3225 | } | ||||||||
3226 | |||||||||
3227 | /// Build a new Objective-C instance/class message to 'super'. | ||||||||
3228 | ExprResult RebuildObjCMessageExpr(SourceLocation SuperLoc, | ||||||||
3229 | Selector Sel, | ||||||||
3230 | ArrayRef<SourceLocation> SelectorLocs, | ||||||||
3231 | QualType SuperType, | ||||||||
3232 | ObjCMethodDecl *Method, | ||||||||
3233 | SourceLocation LBracLoc, | ||||||||
3234 | MultiExprArg Args, | ||||||||
3235 | SourceLocation RBracLoc) { | ||||||||
3236 | return Method->isInstanceMethod() ? SemaRef.BuildInstanceMessage(nullptr, | ||||||||
3237 | SuperType, | ||||||||
3238 | SuperLoc, | ||||||||
3239 | Sel, Method, LBracLoc, SelectorLocs, | ||||||||
3240 | RBracLoc, Args) | ||||||||
3241 | : SemaRef.BuildClassMessage(nullptr, | ||||||||
3242 | SuperType, | ||||||||
3243 | SuperLoc, | ||||||||
3244 | Sel, Method, LBracLoc, SelectorLocs, | ||||||||
3245 | RBracLoc, Args); | ||||||||
3246 | |||||||||
3247 | |||||||||
3248 | } | ||||||||
3249 | |||||||||
3250 | /// Build a new Objective-C ivar reference expression. | ||||||||
3251 | /// | ||||||||
3252 | /// By default, performs semantic analysis to build the new expression. | ||||||||
3253 | /// Subclasses may override this routine to provide different behavior. | ||||||||
3254 | ExprResult RebuildObjCIvarRefExpr(Expr *BaseArg, ObjCIvarDecl *Ivar, | ||||||||
3255 | SourceLocation IvarLoc, | ||||||||
3256 | bool IsArrow, bool IsFreeIvar) { | ||||||||
3257 | CXXScopeSpec SS; | ||||||||
3258 | DeclarationNameInfo NameInfo(Ivar->getDeclName(), IvarLoc); | ||||||||
3259 | ExprResult Result = getSema().BuildMemberReferenceExpr( | ||||||||
3260 | BaseArg, BaseArg->getType(), | ||||||||
3261 | /*FIXME:*/ IvarLoc, IsArrow, SS, SourceLocation(), | ||||||||
3262 | /*FirstQualifierInScope=*/nullptr, NameInfo, | ||||||||
3263 | /*TemplateArgs=*/nullptr, | ||||||||
3264 | /*S=*/nullptr); | ||||||||
3265 | if (IsFreeIvar && Result.isUsable()) | ||||||||
3266 | cast<ObjCIvarRefExpr>(Result.get())->setIsFreeIvar(IsFreeIvar); | ||||||||
3267 | return Result; | ||||||||
3268 | } | ||||||||
3269 | |||||||||
3270 | /// Build a new Objective-C property reference expression. | ||||||||
3271 | /// | ||||||||
3272 | /// By default, performs semantic analysis to build the new expression. | ||||||||
3273 | /// Subclasses may override this routine to provide different behavior. | ||||||||
3274 | ExprResult RebuildObjCPropertyRefExpr(Expr *BaseArg, | ||||||||
3275 | ObjCPropertyDecl *Property, | ||||||||
3276 | SourceLocation PropertyLoc) { | ||||||||
3277 | CXXScopeSpec SS; | ||||||||
3278 | DeclarationNameInfo NameInfo(Property->getDeclName(), PropertyLoc); | ||||||||
3279 | return getSema().BuildMemberReferenceExpr(BaseArg, BaseArg->getType(), | ||||||||
3280 | /*FIXME:*/PropertyLoc, | ||||||||
3281 | /*IsArrow=*/false, | ||||||||
3282 | SS, SourceLocation(), | ||||||||
3283 | /*FirstQualifierInScope=*/nullptr, | ||||||||
3284 | NameInfo, | ||||||||
3285 | /*TemplateArgs=*/nullptr, | ||||||||
3286 | /*S=*/nullptr); | ||||||||
3287 | } | ||||||||
3288 | |||||||||
3289 | /// Build a new Objective-C property reference expression. | ||||||||
3290 | /// | ||||||||
3291 | /// By default, performs semantic analysis to build the new expression. | ||||||||
3292 | /// Subclasses may override this routine to provide different behavior. | ||||||||
3293 | ExprResult RebuildObjCPropertyRefExpr(Expr *Base, QualType T, | ||||||||
3294 | ObjCMethodDecl *Getter, | ||||||||
3295 | ObjCMethodDecl *Setter, | ||||||||
3296 | SourceLocation PropertyLoc) { | ||||||||
3297 | // Since these expressions can only be value-dependent, we do not | ||||||||
3298 | // need to perform semantic analysis again. | ||||||||
3299 | return Owned( | ||||||||
3300 | new (getSema().Context) ObjCPropertyRefExpr(Getter, Setter, T, | ||||||||
3301 | VK_LValue, OK_ObjCProperty, | ||||||||
3302 | PropertyLoc, Base)); | ||||||||
3303 | } | ||||||||
3304 | |||||||||
3305 | /// Build a new Objective-C "isa" expression. | ||||||||
3306 | /// | ||||||||
3307 | /// By default, performs semantic analysis to build the new expression. | ||||||||
3308 | /// Subclasses may override this routine to provide different behavior. | ||||||||
3309 | ExprResult RebuildObjCIsaExpr(Expr *BaseArg, SourceLocation IsaLoc, | ||||||||
3310 | SourceLocation OpLoc, bool IsArrow) { | ||||||||
3311 | CXXScopeSpec SS; | ||||||||
3312 | DeclarationNameInfo NameInfo(&getSema().Context.Idents.get("isa"), IsaLoc); | ||||||||
3313 | return getSema().BuildMemberReferenceExpr(BaseArg, BaseArg->getType(), | ||||||||
3314 | OpLoc, IsArrow, | ||||||||
3315 | SS, SourceLocation(), | ||||||||
3316 | /*FirstQualifierInScope=*/nullptr, | ||||||||
3317 | NameInfo, | ||||||||
3318 | /*TemplateArgs=*/nullptr, | ||||||||
3319 | /*S=*/nullptr); | ||||||||
3320 | } | ||||||||
3321 | |||||||||
3322 | /// Build a new shuffle vector expression. | ||||||||
3323 | /// | ||||||||
3324 | /// By default, performs semantic analysis to build the new expression. | ||||||||
3325 | /// Subclasses may override this routine to provide different behavior. | ||||||||
3326 | ExprResult RebuildShuffleVectorExpr(SourceLocation BuiltinLoc, | ||||||||
3327 | MultiExprArg SubExprs, | ||||||||
3328 | SourceLocation RParenLoc) { | ||||||||
3329 | // Find the declaration for __builtin_shufflevector | ||||||||
3330 | const IdentifierInfo &Name | ||||||||
3331 | = SemaRef.Context.Idents.get("__builtin_shufflevector"); | ||||||||
3332 | TranslationUnitDecl *TUDecl = SemaRef.Context.getTranslationUnitDecl(); | ||||||||
3333 | DeclContext::lookup_result Lookup = TUDecl->lookup(DeclarationName(&Name)); | ||||||||
3334 | assert(!Lookup.empty() && "No __builtin_shufflevector?")((!Lookup.empty() && "No __builtin_shufflevector?") ? static_cast<void> (0) : __assert_fail ("!Lookup.empty() && \"No __builtin_shufflevector?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 3334, __PRETTY_FUNCTION__)); | ||||||||
3335 | |||||||||
3336 | // Build a reference to the __builtin_shufflevector builtin | ||||||||
3337 | FunctionDecl *Builtin = cast<FunctionDecl>(Lookup.front()); | ||||||||
3338 | Expr *Callee = new (SemaRef.Context) | ||||||||
3339 | DeclRefExpr(SemaRef.Context, Builtin, false, | ||||||||
3340 | SemaRef.Context.BuiltinFnTy, VK_RValue, BuiltinLoc); | ||||||||
3341 | QualType CalleePtrTy = SemaRef.Context.getPointerType(Builtin->getType()); | ||||||||
3342 | Callee = SemaRef.ImpCastExprToType(Callee, CalleePtrTy, | ||||||||
3343 | CK_BuiltinFnToFnPtr).get(); | ||||||||
3344 | |||||||||
3345 | // Build the CallExpr | ||||||||
3346 | ExprResult TheCall = CallExpr::Create( | ||||||||
3347 | SemaRef.Context, Callee, SubExprs, Builtin->getCallResultType(), | ||||||||
3348 | Expr::getValueKindForType(Builtin->getReturnType()), RParenLoc); | ||||||||
3349 | |||||||||
3350 | // Type-check the __builtin_shufflevector expression. | ||||||||
3351 | return SemaRef.SemaBuiltinShuffleVector(cast<CallExpr>(TheCall.get())); | ||||||||
3352 | } | ||||||||
3353 | |||||||||
3354 | /// Build a new convert vector expression. | ||||||||
3355 | ExprResult RebuildConvertVectorExpr(SourceLocation BuiltinLoc, | ||||||||
3356 | Expr *SrcExpr, TypeSourceInfo *DstTInfo, | ||||||||
3357 | SourceLocation RParenLoc) { | ||||||||
3358 | return SemaRef.SemaConvertVectorExpr(SrcExpr, DstTInfo, | ||||||||
3359 | BuiltinLoc, RParenLoc); | ||||||||
3360 | } | ||||||||
3361 | |||||||||
3362 | /// Build a new template argument pack expansion. | ||||||||
3363 | /// | ||||||||
3364 | /// By default, performs semantic analysis to build a new pack expansion | ||||||||
3365 | /// for a template argument. Subclasses may override this routine to provide | ||||||||
3366 | /// different behavior. | ||||||||
3367 | TemplateArgumentLoc RebuildPackExpansion(TemplateArgumentLoc Pattern, | ||||||||
3368 | SourceLocation EllipsisLoc, | ||||||||
3369 | Optional<unsigned> NumExpansions) { | ||||||||
3370 | switch (Pattern.getArgument().getKind()) { | ||||||||
3371 | case TemplateArgument::Expression: { | ||||||||
3372 | ExprResult Result | ||||||||
3373 | = getSema().CheckPackExpansion(Pattern.getSourceExpression(), | ||||||||
3374 | EllipsisLoc, NumExpansions); | ||||||||
3375 | if (Result.isInvalid()) | ||||||||
3376 | return TemplateArgumentLoc(); | ||||||||
3377 | |||||||||
3378 | return TemplateArgumentLoc(Result.get(), Result.get()); | ||||||||
3379 | } | ||||||||
3380 | |||||||||
3381 | case TemplateArgument::Template: | ||||||||
3382 | return TemplateArgumentLoc(TemplateArgument( | ||||||||
3383 | Pattern.getArgument().getAsTemplate(), | ||||||||
3384 | NumExpansions), | ||||||||
3385 | Pattern.getTemplateQualifierLoc(), | ||||||||
3386 | Pattern.getTemplateNameLoc(), | ||||||||
3387 | EllipsisLoc); | ||||||||
3388 | |||||||||
3389 | case TemplateArgument::Null: | ||||||||
3390 | case TemplateArgument::Integral: | ||||||||
3391 | case TemplateArgument::Declaration: | ||||||||
3392 | case TemplateArgument::Pack: | ||||||||
3393 | case TemplateArgument::TemplateExpansion: | ||||||||
3394 | case TemplateArgument::NullPtr: | ||||||||
3395 | llvm_unreachable("Pack expansion pattern has no parameter packs")::llvm::llvm_unreachable_internal("Pack expansion pattern has no parameter packs" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 3395); | ||||||||
3396 | |||||||||
3397 | case TemplateArgument::Type: | ||||||||
3398 | if (TypeSourceInfo *Expansion | ||||||||
3399 | = getSema().CheckPackExpansion(Pattern.getTypeSourceInfo(), | ||||||||
3400 | EllipsisLoc, | ||||||||
3401 | NumExpansions)) | ||||||||
3402 | return TemplateArgumentLoc(TemplateArgument(Expansion->getType()), | ||||||||
3403 | Expansion); | ||||||||
3404 | break; | ||||||||
3405 | } | ||||||||
3406 | |||||||||
3407 | return TemplateArgumentLoc(); | ||||||||
3408 | } | ||||||||
3409 | |||||||||
3410 | /// Build a new expression pack expansion. | ||||||||
3411 | /// | ||||||||
3412 | /// By default, performs semantic analysis to build a new pack expansion | ||||||||
3413 | /// for an expression. Subclasses may override this routine to provide | ||||||||
3414 | /// different behavior. | ||||||||
3415 | ExprResult RebuildPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc, | ||||||||
3416 | Optional<unsigned> NumExpansions) { | ||||||||
3417 | return getSema().CheckPackExpansion(Pattern, EllipsisLoc, NumExpansions); | ||||||||
3418 | } | ||||||||
3419 | |||||||||
3420 | /// Build a new C++1z fold-expression. | ||||||||
3421 | /// | ||||||||
3422 | /// By default, performs semantic analysis in order to build a new fold | ||||||||
3423 | /// expression. | ||||||||
3424 | ExprResult RebuildCXXFoldExpr(SourceLocation LParenLoc, Expr *LHS, | ||||||||
3425 | BinaryOperatorKind Operator, | ||||||||
3426 | SourceLocation EllipsisLoc, Expr *RHS, | ||||||||
3427 | SourceLocation RParenLoc, | ||||||||
3428 | Optional<unsigned> NumExpansions) { | ||||||||
3429 | return getSema().BuildCXXFoldExpr(LParenLoc, LHS, Operator, EllipsisLoc, | ||||||||
3430 | RHS, RParenLoc, NumExpansions); | ||||||||
3431 | } | ||||||||
3432 | |||||||||
3433 | /// Build an empty C++1z fold-expression with the given operator. | ||||||||
3434 | /// | ||||||||
3435 | /// By default, produces the fallback value for the fold-expression, or | ||||||||
3436 | /// produce an error if there is no fallback value. | ||||||||
3437 | ExprResult RebuildEmptyCXXFoldExpr(SourceLocation EllipsisLoc, | ||||||||
3438 | BinaryOperatorKind Operator) { | ||||||||
3439 | return getSema().BuildEmptyCXXFoldExpr(EllipsisLoc, Operator); | ||||||||
3440 | } | ||||||||
3441 | |||||||||
3442 | /// Build a new atomic operation expression. | ||||||||
3443 | /// | ||||||||
3444 | /// By default, performs semantic analysis to build the new expression. | ||||||||
3445 | /// Subclasses may override this routine to provide different behavior. | ||||||||
3446 | ExprResult RebuildAtomicExpr(SourceLocation BuiltinLoc, MultiExprArg SubExprs, | ||||||||
3447 | AtomicExpr::AtomicOp Op, | ||||||||
3448 | SourceLocation RParenLoc) { | ||||||||
3449 | // Use this for all of the locations, since we don't know the difference | ||||||||
3450 | // between the call and the expr at this point. | ||||||||
3451 | SourceRange Range{BuiltinLoc, RParenLoc}; | ||||||||
3452 | return getSema().BuildAtomicExpr(Range, Range, RParenLoc, SubExprs, Op, | ||||||||
3453 | Sema::AtomicArgumentOrder::AST); | ||||||||
3454 | } | ||||||||
3455 | |||||||||
3456 | private: | ||||||||
3457 | TypeLoc TransformTypeInObjectScope(TypeLoc TL, | ||||||||
3458 | QualType ObjectType, | ||||||||
3459 | NamedDecl *FirstQualifierInScope, | ||||||||
3460 | CXXScopeSpec &SS); | ||||||||
3461 | |||||||||
3462 | TypeSourceInfo *TransformTypeInObjectScope(TypeSourceInfo *TSInfo, | ||||||||
3463 | QualType ObjectType, | ||||||||
3464 | NamedDecl *FirstQualifierInScope, | ||||||||
3465 | CXXScopeSpec &SS); | ||||||||
3466 | |||||||||
3467 | TypeSourceInfo *TransformTSIInObjectScope(TypeLoc TL, QualType ObjectType, | ||||||||
3468 | NamedDecl *FirstQualifierInScope, | ||||||||
3469 | CXXScopeSpec &SS); | ||||||||
3470 | |||||||||
3471 | QualType TransformDependentNameType(TypeLocBuilder &TLB, | ||||||||
3472 | DependentNameTypeLoc TL, | ||||||||
3473 | bool DeducibleTSTContext); | ||||||||
3474 | }; | ||||||||
3475 | |||||||||
3476 | template <typename Derived> | ||||||||
3477 | StmtResult TreeTransform<Derived>::TransformStmt(Stmt *S, StmtDiscardKind SDK) { | ||||||||
3478 | if (!S) | ||||||||
3479 | return S; | ||||||||
3480 | |||||||||
3481 | switch (S->getStmtClass()) { | ||||||||
3482 | case Stmt::NoStmtClass: break; | ||||||||
3483 | |||||||||
3484 | // Transform individual statement nodes | ||||||||
3485 | // Pass SDK into statements that can produce a value | ||||||||
3486 | #define STMT(Node, Parent) \ | ||||||||
3487 | case Stmt::Node##Class: return getDerived().Transform##Node(cast<Node>(S)); | ||||||||
3488 | #define VALUESTMT(Node, Parent) \ | ||||||||
3489 | case Stmt::Node##Class: \ | ||||||||
3490 | return getDerived().Transform##Node(cast<Node>(S), SDK); | ||||||||
3491 | #define ABSTRACT_STMT(Node) | ||||||||
3492 | #define EXPR(Node, Parent) | ||||||||
3493 | #include "clang/AST/StmtNodes.inc" | ||||||||
3494 | |||||||||
3495 | // Transform expressions by calling TransformExpr. | ||||||||
3496 | #define STMT(Node, Parent) | ||||||||
3497 | #define ABSTRACT_STMT(Stmt) | ||||||||
3498 | #define EXPR(Node, Parent) case Stmt::Node##Class: | ||||||||
3499 | #include "clang/AST/StmtNodes.inc" | ||||||||
3500 | { | ||||||||
3501 | ExprResult E = getDerived().TransformExpr(cast<Expr>(S)); | ||||||||
3502 | |||||||||
3503 | if (SDK == SDK_StmtExprResult) | ||||||||
3504 | E = getSema().ActOnStmtExprResult(E); | ||||||||
3505 | return getSema().ActOnExprStmt(E, SDK == SDK_Discarded); | ||||||||
3506 | } | ||||||||
3507 | } | ||||||||
3508 | |||||||||
3509 | return S; | ||||||||
3510 | } | ||||||||
3511 | |||||||||
3512 | template<typename Derived> | ||||||||
3513 | OMPClause *TreeTransform<Derived>::TransformOMPClause(OMPClause *S) { | ||||||||
3514 | if (!S) | ||||||||
3515 | return S; | ||||||||
3516 | |||||||||
3517 | switch (S->getClauseKind()) { | ||||||||
3518 | default: break; | ||||||||
3519 | // Transform individual clause nodes | ||||||||
3520 | #define OPENMP_CLAUSE(Name, Class) \ | ||||||||
3521 | case OMPC_ ## Name : \ | ||||||||
3522 | return getDerived().Transform ## Class(cast<Class>(S)); | ||||||||
3523 | #include "clang/Basic/OpenMPKinds.def" | ||||||||
3524 | } | ||||||||
3525 | |||||||||
3526 | return S; | ||||||||
3527 | } | ||||||||
3528 | |||||||||
3529 | |||||||||
3530 | template<typename Derived> | ||||||||
3531 | ExprResult TreeTransform<Derived>::TransformExpr(Expr *E) { | ||||||||
3532 | if (!E) | ||||||||
3533 | return E; | ||||||||
3534 | |||||||||
3535 | switch (E->getStmtClass()) { | ||||||||
3536 | case Stmt::NoStmtClass: break; | ||||||||
3537 | #define STMT(Node, Parent) case Stmt::Node##Class: break; | ||||||||
3538 | #define ABSTRACT_STMT(Stmt) | ||||||||
3539 | #define EXPR(Node, Parent) \ | ||||||||
3540 | case Stmt::Node##Class: return getDerived().Transform##Node(cast<Node>(E)); | ||||||||
3541 | #include "clang/AST/StmtNodes.inc" | ||||||||
3542 | } | ||||||||
3543 | |||||||||
3544 | return E; | ||||||||
3545 | } | ||||||||
3546 | |||||||||
3547 | template<typename Derived> | ||||||||
3548 | ExprResult TreeTransform<Derived>::TransformInitializer(Expr *Init, | ||||||||
3549 | bool NotCopyInit) { | ||||||||
3550 | // Initializers are instantiated like expressions, except that various outer | ||||||||
3551 | // layers are stripped. | ||||||||
3552 | if (!Init) | ||||||||
3553 | return Init; | ||||||||
3554 | |||||||||
3555 | if (auto *FE = dyn_cast<FullExpr>(Init)) | ||||||||
3556 | Init = FE->getSubExpr(); | ||||||||
3557 | |||||||||
3558 | if (auto *AIL = dyn_cast<ArrayInitLoopExpr>(Init)) | ||||||||
3559 | Init = AIL->getCommonExpr(); | ||||||||
3560 | |||||||||
3561 | if (MaterializeTemporaryExpr *MTE = dyn_cast<MaterializeTemporaryExpr>(Init)) | ||||||||
3562 | Init = MTE->getSubExpr(); | ||||||||
3563 | |||||||||
3564 | while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init)) | ||||||||
3565 | Init = Binder->getSubExpr(); | ||||||||
3566 | |||||||||
3567 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init)) | ||||||||
3568 | Init = ICE->getSubExprAsWritten(); | ||||||||
3569 | |||||||||
3570 | if (CXXStdInitializerListExpr *ILE = | ||||||||
3571 | dyn_cast<CXXStdInitializerListExpr>(Init)) | ||||||||
3572 | return TransformInitializer(ILE->getSubExpr(), NotCopyInit); | ||||||||
3573 | |||||||||
3574 | // If this is copy-initialization, we only need to reconstruct | ||||||||
3575 | // InitListExprs. Other forms of copy-initialization will be a no-op if | ||||||||
3576 | // the initializer is already the right type. | ||||||||
3577 | CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init); | ||||||||
3578 | if (!NotCopyInit && !(Construct && Construct->isListInitialization())) | ||||||||
3579 | return getDerived().TransformExpr(Init); | ||||||||
3580 | |||||||||
3581 | // Revert value-initialization back to empty parens. | ||||||||
3582 | if (CXXScalarValueInitExpr *VIE = dyn_cast<CXXScalarValueInitExpr>(Init)) { | ||||||||
3583 | SourceRange Parens = VIE->getSourceRange(); | ||||||||
3584 | return getDerived().RebuildParenListExpr(Parens.getBegin(), None, | ||||||||
3585 | Parens.getEnd()); | ||||||||
3586 | } | ||||||||
3587 | |||||||||
3588 | // FIXME: We shouldn't build ImplicitValueInitExprs for direct-initialization. | ||||||||
3589 | if (isa<ImplicitValueInitExpr>(Init)) | ||||||||
3590 | return getDerived().RebuildParenListExpr(SourceLocation(), None, | ||||||||
3591 | SourceLocation()); | ||||||||
3592 | |||||||||
3593 | // Revert initialization by constructor back to a parenthesized or braced list | ||||||||
3594 | // of expressions. Any other form of initializer can just be reused directly. | ||||||||
3595 | if (!Construct || isa<CXXTemporaryObjectExpr>(Construct)) | ||||||||
3596 | return getDerived().TransformExpr(Init); | ||||||||
3597 | |||||||||
3598 | // If the initialization implicitly converted an initializer list to a | ||||||||
3599 | // std::initializer_list object, unwrap the std::initializer_list too. | ||||||||
3600 | if (Construct && Construct->isStdInitListInitialization()) | ||||||||
3601 | return TransformInitializer(Construct->getArg(0), NotCopyInit); | ||||||||
3602 | |||||||||
3603 | // Enter a list-init context if this was list initialization. | ||||||||
3604 | EnterExpressionEvaluationContext Context( | ||||||||
3605 | getSema(), EnterExpressionEvaluationContext::InitList, | ||||||||
3606 | Construct->isListInitialization()); | ||||||||
3607 | |||||||||
3608 | SmallVector<Expr*, 8> NewArgs; | ||||||||
3609 | bool ArgChanged = false; | ||||||||
3610 | if (getDerived().TransformExprs(Construct->getArgs(), Construct->getNumArgs(), | ||||||||
3611 | /*IsCall*/true, NewArgs, &ArgChanged)) | ||||||||
3612 | return ExprError(); | ||||||||
3613 | |||||||||
3614 | // If this was list initialization, revert to syntactic list form. | ||||||||
3615 | if (Construct->isListInitialization()) | ||||||||
3616 | return getDerived().RebuildInitList(Construct->getBeginLoc(), NewArgs, | ||||||||
3617 | Construct->getEndLoc()); | ||||||||
3618 | |||||||||
3619 | // Build a ParenListExpr to represent anything else. | ||||||||
3620 | SourceRange Parens = Construct->getParenOrBraceRange(); | ||||||||
3621 | if (Parens.isInvalid()) { | ||||||||
3622 | // This was a variable declaration's initialization for which no initializer | ||||||||
3623 | // was specified. | ||||||||
3624 | assert(NewArgs.empty() &&((NewArgs.empty() && "no parens or braces but have direct init with arguments?" ) ? static_cast<void> (0) : __assert_fail ("NewArgs.empty() && \"no parens or braces but have direct init with arguments?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 3625, __PRETTY_FUNCTION__)) | ||||||||
3625 | "no parens or braces but have direct init with arguments?")((NewArgs.empty() && "no parens or braces but have direct init with arguments?" ) ? static_cast<void> (0) : __assert_fail ("NewArgs.empty() && \"no parens or braces but have direct init with arguments?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 3625, __PRETTY_FUNCTION__)); | ||||||||
3626 | return ExprEmpty(); | ||||||||
3627 | } | ||||||||
3628 | return getDerived().RebuildParenListExpr(Parens.getBegin(), NewArgs, | ||||||||
3629 | Parens.getEnd()); | ||||||||
3630 | } | ||||||||
3631 | |||||||||
3632 | template<typename Derived> | ||||||||
3633 | bool TreeTransform<Derived>::TransformExprs(Expr *const *Inputs, | ||||||||
3634 | unsigned NumInputs, | ||||||||
3635 | bool IsCall, | ||||||||
3636 | SmallVectorImpl<Expr *> &Outputs, | ||||||||
3637 | bool *ArgChanged) { | ||||||||
3638 | for (unsigned I = 0; I != NumInputs; ++I) { | ||||||||
3639 | // If requested, drop call arguments that need to be dropped. | ||||||||
3640 | if (IsCall && getDerived().DropCallArgument(Inputs[I])) { | ||||||||
3641 | if (ArgChanged) | ||||||||
3642 | *ArgChanged = true; | ||||||||
3643 | |||||||||
3644 | break; | ||||||||
3645 | } | ||||||||
3646 | |||||||||
3647 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(Inputs[I])) { | ||||||||
3648 | Expr *Pattern = Expansion->getPattern(); | ||||||||
3649 | |||||||||
3650 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | ||||||||
3651 | getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded); | ||||||||
3652 | assert(!Unexpanded.empty() && "Pack expansion without parameter packs?")((!Unexpanded.empty() && "Pack expansion without parameter packs?" ) ? static_cast<void> (0) : __assert_fail ("!Unexpanded.empty() && \"Pack expansion without parameter packs?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 3652, __PRETTY_FUNCTION__)); | ||||||||
3653 | |||||||||
3654 | // Determine whether the set of unexpanded parameter packs can and should | ||||||||
3655 | // be expanded. | ||||||||
3656 | bool Expand = true; | ||||||||
3657 | bool RetainExpansion = false; | ||||||||
3658 | Optional<unsigned> OrigNumExpansions = Expansion->getNumExpansions(); | ||||||||
3659 | Optional<unsigned> NumExpansions = OrigNumExpansions; | ||||||||
3660 | if (getDerived().TryExpandParameterPacks(Expansion->getEllipsisLoc(), | ||||||||
3661 | Pattern->getSourceRange(), | ||||||||
3662 | Unexpanded, | ||||||||
3663 | Expand, RetainExpansion, | ||||||||
3664 | NumExpansions)) | ||||||||
3665 | return true; | ||||||||
3666 | |||||||||
3667 | if (!Expand) { | ||||||||
3668 | // The transform has determined that we should perform a simple | ||||||||
3669 | // transformation on the pack expansion, producing another pack | ||||||||
3670 | // expansion. | ||||||||
3671 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1); | ||||||||
3672 | ExprResult OutPattern = getDerived().TransformExpr(Pattern); | ||||||||
3673 | if (OutPattern.isInvalid()) | ||||||||
3674 | return true; | ||||||||
3675 | |||||||||
3676 | ExprResult Out = getDerived().RebuildPackExpansion(OutPattern.get(), | ||||||||
3677 | Expansion->getEllipsisLoc(), | ||||||||
3678 | NumExpansions); | ||||||||
3679 | if (Out.isInvalid()) | ||||||||
3680 | return true; | ||||||||
3681 | |||||||||
3682 | if (ArgChanged) | ||||||||
3683 | *ArgChanged = true; | ||||||||
3684 | Outputs.push_back(Out.get()); | ||||||||
3685 | continue; | ||||||||
3686 | } | ||||||||
3687 | |||||||||
3688 | // Record right away that the argument was changed. This needs | ||||||||
3689 | // to happen even if the array expands to nothing. | ||||||||
3690 | if (ArgChanged) *ArgChanged = true; | ||||||||
3691 | |||||||||
3692 | // The transform has determined that we should perform an elementwise | ||||||||
3693 | // expansion of the pattern. Do so. | ||||||||
3694 | for (unsigned I = 0; I != *NumExpansions; ++I) { | ||||||||
3695 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I); | ||||||||
3696 | ExprResult Out = getDerived().TransformExpr(Pattern); | ||||||||
3697 | if (Out.isInvalid()) | ||||||||
3698 | return true; | ||||||||
3699 | |||||||||
3700 | if (Out.get()->containsUnexpandedParameterPack()) { | ||||||||
3701 | Out = getDerived().RebuildPackExpansion( | ||||||||
3702 | Out.get(), Expansion->getEllipsisLoc(), OrigNumExpansions); | ||||||||
3703 | if (Out.isInvalid()) | ||||||||
3704 | return true; | ||||||||
3705 | } | ||||||||
3706 | |||||||||
3707 | Outputs.push_back(Out.get()); | ||||||||
3708 | } | ||||||||
3709 | |||||||||
3710 | // If we're supposed to retain a pack expansion, do so by temporarily | ||||||||
3711 | // forgetting the partially-substituted parameter pack. | ||||||||
3712 | if (RetainExpansion) { | ||||||||
3713 | ForgetPartiallySubstitutedPackRAII Forget(getDerived()); | ||||||||
3714 | |||||||||
3715 | ExprResult Out = getDerived().TransformExpr(Pattern); | ||||||||
3716 | if (Out.isInvalid()) | ||||||||
3717 | return true; | ||||||||
3718 | |||||||||
3719 | Out = getDerived().RebuildPackExpansion( | ||||||||
3720 | Out.get(), Expansion->getEllipsisLoc(), OrigNumExpansions); | ||||||||
3721 | if (Out.isInvalid()) | ||||||||
3722 | return true; | ||||||||
3723 | |||||||||
3724 | Outputs.push_back(Out.get()); | ||||||||
3725 | } | ||||||||
3726 | |||||||||
3727 | continue; | ||||||||
3728 | } | ||||||||
3729 | |||||||||
3730 | ExprResult Result = | ||||||||
3731 | IsCall ? getDerived().TransformInitializer(Inputs[I], /*DirectInit*/false) | ||||||||
3732 | : getDerived().TransformExpr(Inputs[I]); | ||||||||
3733 | if (Result.isInvalid()) | ||||||||
3734 | return true; | ||||||||
3735 | |||||||||
3736 | if (Result.get() != Inputs[I] && ArgChanged) | ||||||||
3737 | *ArgChanged = true; | ||||||||
3738 | |||||||||
3739 | Outputs.push_back(Result.get()); | ||||||||
3740 | } | ||||||||
3741 | |||||||||
3742 | return false; | ||||||||
3743 | } | ||||||||
3744 | |||||||||
3745 | template <typename Derived> | ||||||||
3746 | Sema::ConditionResult TreeTransform<Derived>::TransformCondition( | ||||||||
3747 | SourceLocation Loc, VarDecl *Var, Expr *Expr, Sema::ConditionKind Kind) { | ||||||||
3748 | if (Var) { | ||||||||
3749 | VarDecl *ConditionVar = cast_or_null<VarDecl>( | ||||||||
3750 | getDerived().TransformDefinition(Var->getLocation(), Var)); | ||||||||
3751 | |||||||||
3752 | if (!ConditionVar) | ||||||||
3753 | return Sema::ConditionError(); | ||||||||
3754 | |||||||||
3755 | return getSema().ActOnConditionVariable(ConditionVar, Loc, Kind); | ||||||||
3756 | } | ||||||||
3757 | |||||||||
3758 | if (Expr) { | ||||||||
3759 | ExprResult CondExpr = getDerived().TransformExpr(Expr); | ||||||||
3760 | |||||||||
3761 | if (CondExpr.isInvalid()) | ||||||||
3762 | return Sema::ConditionError(); | ||||||||
3763 | |||||||||
3764 | return getSema().ActOnCondition(nullptr, Loc, CondExpr.get(), Kind); | ||||||||
3765 | } | ||||||||
3766 | |||||||||
3767 | return Sema::ConditionResult(); | ||||||||
3768 | } | ||||||||
3769 | |||||||||
3770 | template<typename Derived> | ||||||||
3771 | NestedNameSpecifierLoc | ||||||||
3772 | TreeTransform<Derived>::TransformNestedNameSpecifierLoc( | ||||||||
3773 | NestedNameSpecifierLoc NNS, | ||||||||
3774 | QualType ObjectType, | ||||||||
3775 | NamedDecl *FirstQualifierInScope) { | ||||||||
3776 | SmallVector<NestedNameSpecifierLoc, 4> Qualifiers; | ||||||||
3777 | for (NestedNameSpecifierLoc Qualifier = NNS; Qualifier; | ||||||||
3778 | Qualifier = Qualifier.getPrefix()) | ||||||||
3779 | Qualifiers.push_back(Qualifier); | ||||||||
3780 | |||||||||
3781 | CXXScopeSpec SS; | ||||||||
3782 | while (!Qualifiers.empty()) { | ||||||||
3783 | NestedNameSpecifierLoc Q = Qualifiers.pop_back_val(); | ||||||||
3784 | NestedNameSpecifier *QNNS = Q.getNestedNameSpecifier(); | ||||||||
3785 | |||||||||
3786 | switch (QNNS->getKind()) { | ||||||||
3787 | case NestedNameSpecifier::Identifier: { | ||||||||
3788 | Sema::NestedNameSpecInfo IdInfo(QNNS->getAsIdentifier(), | ||||||||
3789 | Q.getLocalBeginLoc(), Q.getLocalEndLoc(), ObjectType); | ||||||||
3790 | if (SemaRef.BuildCXXNestedNameSpecifier(/*Scope=*/nullptr, IdInfo, false, | ||||||||
3791 | SS, FirstQualifierInScope, false)) | ||||||||
3792 | return NestedNameSpecifierLoc(); | ||||||||
3793 | } | ||||||||
3794 | break; | ||||||||
3795 | |||||||||
3796 | case NestedNameSpecifier::Namespace: { | ||||||||
3797 | NamespaceDecl *NS | ||||||||
3798 | = cast_or_null<NamespaceDecl>( | ||||||||
3799 | getDerived().TransformDecl( | ||||||||
3800 | Q.getLocalBeginLoc(), | ||||||||
3801 | QNNS->getAsNamespace())); | ||||||||
3802 | SS.Extend(SemaRef.Context, NS, Q.getLocalBeginLoc(), Q.getLocalEndLoc()); | ||||||||
3803 | break; | ||||||||
3804 | } | ||||||||
3805 | |||||||||
3806 | case NestedNameSpecifier::NamespaceAlias: { | ||||||||
3807 | NamespaceAliasDecl *Alias | ||||||||
3808 | = cast_or_null<NamespaceAliasDecl>( | ||||||||
3809 | getDerived().TransformDecl(Q.getLocalBeginLoc(), | ||||||||
3810 | QNNS->getAsNamespaceAlias())); | ||||||||
3811 | SS.Extend(SemaRef.Context, Alias, Q.getLocalBeginLoc(), | ||||||||
3812 | Q.getLocalEndLoc()); | ||||||||
3813 | break; | ||||||||
3814 | } | ||||||||
3815 | |||||||||
3816 | case NestedNameSpecifier::Global: | ||||||||
3817 | // There is no meaningful transformation that one could perform on the | ||||||||
3818 | // global scope. | ||||||||
3819 | SS.MakeGlobal(SemaRef.Context, Q.getBeginLoc()); | ||||||||
3820 | break; | ||||||||
3821 | |||||||||
3822 | case NestedNameSpecifier::Super: { | ||||||||
3823 | CXXRecordDecl *RD = | ||||||||
3824 | cast_or_null<CXXRecordDecl>(getDerived().TransformDecl( | ||||||||
3825 | SourceLocation(), QNNS->getAsRecordDecl())); | ||||||||
3826 | SS.MakeSuper(SemaRef.Context, RD, Q.getBeginLoc(), Q.getEndLoc()); | ||||||||
3827 | break; | ||||||||
3828 | } | ||||||||
3829 | |||||||||
3830 | case NestedNameSpecifier::TypeSpecWithTemplate: | ||||||||
3831 | case NestedNameSpecifier::TypeSpec: { | ||||||||
3832 | TypeLoc TL = TransformTypeInObjectScope(Q.getTypeLoc(), ObjectType, | ||||||||
3833 | FirstQualifierInScope, SS); | ||||||||
3834 | |||||||||
3835 | if (!TL) | ||||||||
3836 | return NestedNameSpecifierLoc(); | ||||||||
3837 | |||||||||
3838 | if (TL.getType()->isDependentType() || TL.getType()->isRecordType() || | ||||||||
3839 | (SemaRef.getLangOpts().CPlusPlus11 && | ||||||||
3840 | TL.getType()->isEnumeralType())) { | ||||||||
3841 | assert(!TL.getType().hasLocalQualifiers() &&((!TL.getType().hasLocalQualifiers() && "Can't get cv-qualifiers here" ) ? static_cast<void> (0) : __assert_fail ("!TL.getType().hasLocalQualifiers() && \"Can't get cv-qualifiers here\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 3842, __PRETTY_FUNCTION__)) | ||||||||
3842 | "Can't get cv-qualifiers here")((!TL.getType().hasLocalQualifiers() && "Can't get cv-qualifiers here" ) ? static_cast<void> (0) : __assert_fail ("!TL.getType().hasLocalQualifiers() && \"Can't get cv-qualifiers here\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 3842, __PRETTY_FUNCTION__)); | ||||||||
3843 | if (TL.getType()->isEnumeralType()) | ||||||||
3844 | SemaRef.Diag(TL.getBeginLoc(), | ||||||||
3845 | diag::warn_cxx98_compat_enum_nested_name_spec); | ||||||||
3846 | SS.Extend(SemaRef.Context, /*FIXME:*/SourceLocation(), TL, | ||||||||
3847 | Q.getLocalEndLoc()); | ||||||||
3848 | break; | ||||||||
3849 | } | ||||||||
3850 | // If the nested-name-specifier is an invalid type def, don't emit an | ||||||||
3851 | // error because a previous error should have already been emitted. | ||||||||
3852 | TypedefTypeLoc TTL = TL.getAs<TypedefTypeLoc>(); | ||||||||
3853 | if (!TTL || !TTL.getTypedefNameDecl()->isInvalidDecl()) { | ||||||||
3854 | SemaRef.Diag(TL.getBeginLoc(), diag::err_nested_name_spec_non_tag) | ||||||||
3855 | << TL.getType() << SS.getRange(); | ||||||||
3856 | } | ||||||||
3857 | return NestedNameSpecifierLoc(); | ||||||||
3858 | } | ||||||||
3859 | } | ||||||||
3860 | |||||||||
3861 | // The qualifier-in-scope and object type only apply to the leftmost entity. | ||||||||
3862 | FirstQualifierInScope = nullptr; | ||||||||
3863 | ObjectType = QualType(); | ||||||||
3864 | } | ||||||||
3865 | |||||||||
3866 | // Don't rebuild the nested-name-specifier if we don't have to. | ||||||||
3867 | if (SS.getScopeRep() == NNS.getNestedNameSpecifier() && | ||||||||
3868 | !getDerived().AlwaysRebuild()) | ||||||||
3869 | return NNS; | ||||||||
3870 | |||||||||
3871 | // If we can re-use the source-location data from the original | ||||||||
3872 | // nested-name-specifier, do so. | ||||||||
3873 | if (SS.location_size() == NNS.getDataLength() && | ||||||||
3874 | memcmp(SS.location_data(), NNS.getOpaqueData(), SS.location_size()) == 0) | ||||||||
3875 | return NestedNameSpecifierLoc(SS.getScopeRep(), NNS.getOpaqueData()); | ||||||||
3876 | |||||||||
3877 | // Allocate new nested-name-specifier location information. | ||||||||
3878 | return SS.getWithLocInContext(SemaRef.Context); | ||||||||
3879 | } | ||||||||
3880 | |||||||||
3881 | template<typename Derived> | ||||||||
3882 | DeclarationNameInfo | ||||||||
3883 | TreeTransform<Derived> | ||||||||
3884 | ::TransformDeclarationNameInfo(const DeclarationNameInfo &NameInfo) { | ||||||||
3885 | DeclarationName Name = NameInfo.getName(); | ||||||||
3886 | if (!Name) | ||||||||
3887 | return DeclarationNameInfo(); | ||||||||
3888 | |||||||||
3889 | switch (Name.getNameKind()) { | ||||||||
3890 | case DeclarationName::Identifier: | ||||||||
3891 | case DeclarationName::ObjCZeroArgSelector: | ||||||||
3892 | case DeclarationName::ObjCOneArgSelector: | ||||||||
3893 | case DeclarationName::ObjCMultiArgSelector: | ||||||||
3894 | case DeclarationName::CXXOperatorName: | ||||||||
3895 | case DeclarationName::CXXLiteralOperatorName: | ||||||||
3896 | case DeclarationName::CXXUsingDirective: | ||||||||
3897 | return NameInfo; | ||||||||
3898 | |||||||||
3899 | case DeclarationName::CXXDeductionGuideName: { | ||||||||
3900 | TemplateDecl *OldTemplate = Name.getCXXDeductionGuideTemplate(); | ||||||||
3901 | TemplateDecl *NewTemplate = cast_or_null<TemplateDecl>( | ||||||||
3902 | getDerived().TransformDecl(NameInfo.getLoc(), OldTemplate)); | ||||||||
3903 | if (!NewTemplate) | ||||||||
3904 | return DeclarationNameInfo(); | ||||||||
3905 | |||||||||
3906 | DeclarationNameInfo NewNameInfo(NameInfo); | ||||||||
3907 | NewNameInfo.setName( | ||||||||
3908 | SemaRef.Context.DeclarationNames.getCXXDeductionGuideName(NewTemplate)); | ||||||||
3909 | return NewNameInfo; | ||||||||
3910 | } | ||||||||
3911 | |||||||||
3912 | case DeclarationName::CXXConstructorName: | ||||||||
3913 | case DeclarationName::CXXDestructorName: | ||||||||
3914 | case DeclarationName::CXXConversionFunctionName: { | ||||||||
3915 | TypeSourceInfo *NewTInfo; | ||||||||
3916 | CanQualType NewCanTy; | ||||||||
3917 | if (TypeSourceInfo *OldTInfo = NameInfo.getNamedTypeInfo()) { | ||||||||
3918 | NewTInfo = getDerived().TransformType(OldTInfo); | ||||||||
3919 | if (!NewTInfo) | ||||||||
3920 | return DeclarationNameInfo(); | ||||||||
3921 | NewCanTy = SemaRef.Context.getCanonicalType(NewTInfo->getType()); | ||||||||
3922 | } | ||||||||
3923 | else { | ||||||||
3924 | NewTInfo = nullptr; | ||||||||
3925 | TemporaryBase Rebase(*this, NameInfo.getLoc(), Name); | ||||||||
3926 | QualType NewT = getDerived().TransformType(Name.getCXXNameType()); | ||||||||
3927 | if (NewT.isNull()) | ||||||||
3928 | return DeclarationNameInfo(); | ||||||||
3929 | NewCanTy = SemaRef.Context.getCanonicalType(NewT); | ||||||||
3930 | } | ||||||||
3931 | |||||||||
3932 | DeclarationName NewName | ||||||||
3933 | = SemaRef.Context.DeclarationNames.getCXXSpecialName(Name.getNameKind(), | ||||||||
3934 | NewCanTy); | ||||||||
3935 | DeclarationNameInfo NewNameInfo(NameInfo); | ||||||||
3936 | NewNameInfo.setName(NewName); | ||||||||
3937 | NewNameInfo.setNamedTypeInfo(NewTInfo); | ||||||||
3938 | return NewNameInfo; | ||||||||
3939 | } | ||||||||
3940 | } | ||||||||
3941 | |||||||||
3942 | llvm_unreachable("Unknown name kind.")::llvm::llvm_unreachable_internal("Unknown name kind.", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 3942); | ||||||||
3943 | } | ||||||||
3944 | |||||||||
3945 | template<typename Derived> | ||||||||
3946 | TemplateName | ||||||||
3947 | TreeTransform<Derived>::TransformTemplateName(CXXScopeSpec &SS, | ||||||||
3948 | TemplateName Name, | ||||||||
3949 | SourceLocation NameLoc, | ||||||||
3950 | QualType ObjectType, | ||||||||
3951 | NamedDecl *FirstQualifierInScope, | ||||||||
3952 | bool AllowInjectedClassName) { | ||||||||
3953 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) { | ||||||||
3954 | TemplateDecl *Template = QTN->getTemplateDecl(); | ||||||||
3955 | assert(Template && "qualified template name must refer to a template")((Template && "qualified template name must refer to a template" ) ? static_cast<void> (0) : __assert_fail ("Template && \"qualified template name must refer to a template\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 3955, __PRETTY_FUNCTION__)); | ||||||||
3956 | |||||||||
3957 | TemplateDecl *TransTemplate | ||||||||
3958 | = cast_or_null<TemplateDecl>(getDerived().TransformDecl(NameLoc, | ||||||||
3959 | Template)); | ||||||||
3960 | if (!TransTemplate) | ||||||||
3961 | return TemplateName(); | ||||||||
3962 | |||||||||
3963 | if (!getDerived().AlwaysRebuild() && | ||||||||
3964 | SS.getScopeRep() == QTN->getQualifier() && | ||||||||
3965 | TransTemplate == Template) | ||||||||
3966 | return Name; | ||||||||
3967 | |||||||||
3968 | return getDerived().RebuildTemplateName(SS, QTN->hasTemplateKeyword(), | ||||||||
3969 | TransTemplate); | ||||||||
3970 | } | ||||||||
3971 | |||||||||
3972 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) { | ||||||||
3973 | if (SS.getScopeRep()) { | ||||||||
3974 | // These apply to the scope specifier, not the template. | ||||||||
3975 | ObjectType = QualType(); | ||||||||
3976 | FirstQualifierInScope = nullptr; | ||||||||
3977 | } | ||||||||
3978 | |||||||||
3979 | if (!getDerived().AlwaysRebuild() && | ||||||||
3980 | SS.getScopeRep() == DTN->getQualifier() && | ||||||||
3981 | ObjectType.isNull()) | ||||||||
3982 | return Name; | ||||||||
3983 | |||||||||
3984 | // FIXME: Preserve the location of the "template" keyword. | ||||||||
3985 | SourceLocation TemplateKWLoc = NameLoc; | ||||||||
3986 | |||||||||
3987 | if (DTN->isIdentifier()) { | ||||||||
3988 | return getDerived().RebuildTemplateName(SS, | ||||||||
3989 | TemplateKWLoc, | ||||||||
3990 | *DTN->getIdentifier(), | ||||||||
3991 | NameLoc, | ||||||||
3992 | ObjectType, | ||||||||
3993 | FirstQualifierInScope, | ||||||||
3994 | AllowInjectedClassName); | ||||||||
3995 | } | ||||||||
3996 | |||||||||
3997 | return getDerived().RebuildTemplateName(SS, TemplateKWLoc, | ||||||||
3998 | DTN->getOperator(), NameLoc, | ||||||||
3999 | ObjectType, AllowInjectedClassName); | ||||||||
4000 | } | ||||||||
4001 | |||||||||
4002 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { | ||||||||
4003 | TemplateDecl *TransTemplate | ||||||||
4004 | = cast_or_null<TemplateDecl>(getDerived().TransformDecl(NameLoc, | ||||||||
4005 | Template)); | ||||||||
4006 | if (!TransTemplate) | ||||||||
4007 | return TemplateName(); | ||||||||
4008 | |||||||||
4009 | if (!getDerived().AlwaysRebuild() && | ||||||||
4010 | TransTemplate == Template) | ||||||||
4011 | return Name; | ||||||||
4012 | |||||||||
4013 | return TemplateName(TransTemplate); | ||||||||
4014 | } | ||||||||
4015 | |||||||||
4016 | if (SubstTemplateTemplateParmPackStorage *SubstPack | ||||||||
4017 | = Name.getAsSubstTemplateTemplateParmPack()) { | ||||||||
4018 | TemplateTemplateParmDecl *TransParam | ||||||||
4019 | = cast_or_null<TemplateTemplateParmDecl>( | ||||||||
4020 | getDerived().TransformDecl(NameLoc, SubstPack->getParameterPack())); | ||||||||
4021 | if (!TransParam) | ||||||||
4022 | return TemplateName(); | ||||||||
4023 | |||||||||
4024 | if (!getDerived().AlwaysRebuild() && | ||||||||
4025 | TransParam == SubstPack->getParameterPack()) | ||||||||
4026 | return Name; | ||||||||
4027 | |||||||||
4028 | return getDerived().RebuildTemplateName(TransParam, | ||||||||
4029 | SubstPack->getArgumentPack()); | ||||||||
4030 | } | ||||||||
4031 | |||||||||
4032 | // These should be getting filtered out before they reach the AST. | ||||||||
4033 | llvm_unreachable("overloaded function decl survived to here")::llvm::llvm_unreachable_internal("overloaded function decl survived to here" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 4033); | ||||||||
4034 | } | ||||||||
4035 | |||||||||
4036 | template<typename Derived> | ||||||||
4037 | void TreeTransform<Derived>::InventTemplateArgumentLoc( | ||||||||
4038 | const TemplateArgument &Arg, | ||||||||
4039 | TemplateArgumentLoc &Output) { | ||||||||
4040 | SourceLocation Loc = getDerived().getBaseLocation(); | ||||||||
4041 | switch (Arg.getKind()) { | ||||||||
4042 | case TemplateArgument::Null: | ||||||||
4043 | llvm_unreachable("null template argument in TreeTransform")::llvm::llvm_unreachable_internal("null template argument in TreeTransform" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 4043); | ||||||||
4044 | break; | ||||||||
4045 | |||||||||
4046 | case TemplateArgument::Type: | ||||||||
4047 | Output = TemplateArgumentLoc(Arg, | ||||||||
4048 | SemaRef.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); | ||||||||
4049 | |||||||||
4050 | break; | ||||||||
4051 | |||||||||
4052 | case TemplateArgument::Template: | ||||||||
4053 | case TemplateArgument::TemplateExpansion: { | ||||||||
4054 | NestedNameSpecifierLocBuilder Builder; | ||||||||
4055 | TemplateName Template = Arg.getAsTemplateOrTemplatePattern(); | ||||||||
4056 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) | ||||||||
4057 | Builder.MakeTrivial(SemaRef.Context, DTN->getQualifier(), Loc); | ||||||||
4058 | else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) | ||||||||
4059 | Builder.MakeTrivial(SemaRef.Context, QTN->getQualifier(), Loc); | ||||||||
4060 | |||||||||
4061 | if (Arg.getKind() == TemplateArgument::Template) | ||||||||
4062 | Output = TemplateArgumentLoc(Arg, | ||||||||
4063 | Builder.getWithLocInContext(SemaRef.Context), | ||||||||
4064 | Loc); | ||||||||
4065 | else | ||||||||
4066 | Output = TemplateArgumentLoc(Arg, | ||||||||
4067 | Builder.getWithLocInContext(SemaRef.Context), | ||||||||
4068 | Loc, Loc); | ||||||||
4069 | |||||||||
4070 | break; | ||||||||
4071 | } | ||||||||
4072 | |||||||||
4073 | case TemplateArgument::Expression: | ||||||||
4074 | Output = TemplateArgumentLoc(Arg, Arg.getAsExpr()); | ||||||||
4075 | break; | ||||||||
4076 | |||||||||
4077 | case TemplateArgument::Declaration: | ||||||||
4078 | case TemplateArgument::Integral: | ||||||||
4079 | case TemplateArgument::Pack: | ||||||||
4080 | case TemplateArgument::NullPtr: | ||||||||
4081 | Output = TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); | ||||||||
4082 | break; | ||||||||
4083 | } | ||||||||
4084 | } | ||||||||
4085 | |||||||||
4086 | template<typename Derived> | ||||||||
4087 | bool TreeTransform<Derived>::TransformTemplateArgument( | ||||||||
4088 | const TemplateArgumentLoc &Input, | ||||||||
4089 | TemplateArgumentLoc &Output, bool Uneval) { | ||||||||
4090 | const TemplateArgument &Arg = Input.getArgument(); | ||||||||
4091 | switch (Arg.getKind()) { | ||||||||
4092 | case TemplateArgument::Null: | ||||||||
4093 | case TemplateArgument::Integral: | ||||||||
4094 | case TemplateArgument::Pack: | ||||||||
4095 | case TemplateArgument::Declaration: | ||||||||
4096 | case TemplateArgument::NullPtr: | ||||||||
4097 | llvm_unreachable("Unexpected TemplateArgument")::llvm::llvm_unreachable_internal("Unexpected TemplateArgument" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 4097); | ||||||||
4098 | |||||||||
4099 | case TemplateArgument::Type: { | ||||||||
4100 | TypeSourceInfo *DI = Input.getTypeSourceInfo(); | ||||||||
4101 | if (!DI) | ||||||||
4102 | DI = InventTypeSourceInfo(Input.getArgument().getAsType()); | ||||||||
4103 | |||||||||
4104 | DI = getDerived().TransformType(DI); | ||||||||
4105 | if (!DI) return true; | ||||||||
4106 | |||||||||
4107 | Output = TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); | ||||||||
4108 | return false; | ||||||||
4109 | } | ||||||||
4110 | |||||||||
4111 | case TemplateArgument::Template: { | ||||||||
4112 | NestedNameSpecifierLoc QualifierLoc = Input.getTemplateQualifierLoc(); | ||||||||
4113 | if (QualifierLoc) { | ||||||||
4114 | QualifierLoc = getDerived().TransformNestedNameSpecifierLoc(QualifierLoc); | ||||||||
4115 | if (!QualifierLoc) | ||||||||
4116 | return true; | ||||||||
4117 | } | ||||||||
4118 | |||||||||
4119 | CXXScopeSpec SS; | ||||||||
4120 | SS.Adopt(QualifierLoc); | ||||||||
4121 | TemplateName Template | ||||||||
4122 | = getDerived().TransformTemplateName(SS, Arg.getAsTemplate(), | ||||||||
4123 | Input.getTemplateNameLoc()); | ||||||||
4124 | if (Template.isNull()) | ||||||||
4125 | return true; | ||||||||
4126 | |||||||||
4127 | Output = TemplateArgumentLoc(TemplateArgument(Template), QualifierLoc, | ||||||||
4128 | Input.getTemplateNameLoc()); | ||||||||
4129 | return false; | ||||||||
4130 | } | ||||||||
4131 | |||||||||
4132 | case TemplateArgument::TemplateExpansion: | ||||||||
4133 | llvm_unreachable("Caller should expand pack expansions")::llvm::llvm_unreachable_internal("Caller should expand pack expansions" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 4133); | ||||||||
4134 | |||||||||
4135 | case TemplateArgument::Expression: { | ||||||||
4136 | // Template argument expressions are constant expressions. | ||||||||
4137 | EnterExpressionEvaluationContext Unevaluated( | ||||||||
4138 | getSema(), | ||||||||
4139 | Uneval ? Sema::ExpressionEvaluationContext::Unevaluated | ||||||||
4140 | : Sema::ExpressionEvaluationContext::ConstantEvaluated, | ||||||||
4141 | /*LambdaContextDecl=*/nullptr, /*ExprContext=*/ | ||||||||
4142 | Sema::ExpressionEvaluationContextRecord::EK_TemplateArgument); | ||||||||
4143 | |||||||||
4144 | Expr *InputExpr = Input.getSourceExpression(); | ||||||||
4145 | if (!InputExpr) InputExpr = Input.getArgument().getAsExpr(); | ||||||||
4146 | |||||||||
4147 | ExprResult E = getDerived().TransformExpr(InputExpr); | ||||||||
4148 | E = SemaRef.ActOnConstantExpression(E); | ||||||||
4149 | if (E.isInvalid()) return true; | ||||||||
4150 | Output = TemplateArgumentLoc(TemplateArgument(E.get()), E.get()); | ||||||||
4151 | return false; | ||||||||
4152 | } | ||||||||
4153 | } | ||||||||
4154 | |||||||||
4155 | // Work around bogus GCC warning | ||||||||
4156 | return true; | ||||||||
4157 | } | ||||||||
4158 | |||||||||
4159 | /// Iterator adaptor that invents template argument location information | ||||||||
4160 | /// for each of the template arguments in its underlying iterator. | ||||||||
4161 | template<typename Derived, typename InputIterator> | ||||||||
4162 | class TemplateArgumentLocInventIterator { | ||||||||
4163 | TreeTransform<Derived> &Self; | ||||||||
4164 | InputIterator Iter; | ||||||||
4165 | |||||||||
4166 | public: | ||||||||
4167 | typedef TemplateArgumentLoc value_type; | ||||||||
4168 | typedef TemplateArgumentLoc reference; | ||||||||
4169 | typedef typename std::iterator_traits<InputIterator>::difference_type | ||||||||
4170 | difference_type; | ||||||||
4171 | typedef std::input_iterator_tag iterator_category; | ||||||||
4172 | |||||||||
4173 | class pointer { | ||||||||
4174 | TemplateArgumentLoc Arg; | ||||||||
4175 | |||||||||
4176 | public: | ||||||||
4177 | explicit pointer(TemplateArgumentLoc Arg) : Arg(Arg) { } | ||||||||
4178 | |||||||||
4179 | const TemplateArgumentLoc *operator->() const { return &Arg; } | ||||||||
4180 | }; | ||||||||
4181 | |||||||||
4182 | TemplateArgumentLocInventIterator() { } | ||||||||
4183 | |||||||||
4184 | explicit TemplateArgumentLocInventIterator(TreeTransform<Derived> &Self, | ||||||||
4185 | InputIterator Iter) | ||||||||
4186 | : Self(Self), Iter(Iter) { } | ||||||||
4187 | |||||||||
4188 | TemplateArgumentLocInventIterator &operator++() { | ||||||||
4189 | ++Iter; | ||||||||
4190 | return *this; | ||||||||
4191 | } | ||||||||
4192 | |||||||||
4193 | TemplateArgumentLocInventIterator operator++(int) { | ||||||||
4194 | TemplateArgumentLocInventIterator Old(*this); | ||||||||
4195 | ++(*this); | ||||||||
4196 | return Old; | ||||||||
4197 | } | ||||||||
4198 | |||||||||
4199 | reference operator*() const { | ||||||||
4200 | TemplateArgumentLoc Result; | ||||||||
4201 | Self.InventTemplateArgumentLoc(*Iter, Result); | ||||||||
4202 | return Result; | ||||||||
4203 | } | ||||||||
4204 | |||||||||
4205 | pointer operator->() const { return pointer(**this); } | ||||||||
4206 | |||||||||
4207 | friend bool operator==(const TemplateArgumentLocInventIterator &X, | ||||||||
4208 | const TemplateArgumentLocInventIterator &Y) { | ||||||||
4209 | return X.Iter == Y.Iter; | ||||||||
4210 | } | ||||||||
4211 | |||||||||
4212 | friend bool operator!=(const TemplateArgumentLocInventIterator &X, | ||||||||
4213 | const TemplateArgumentLocInventIterator &Y) { | ||||||||
4214 | return X.Iter != Y.Iter; | ||||||||
4215 | } | ||||||||
4216 | }; | ||||||||
4217 | |||||||||
4218 | template<typename Derived> | ||||||||
4219 | template<typename InputIterator> | ||||||||
4220 | bool TreeTransform<Derived>::TransformTemplateArguments( | ||||||||
4221 | InputIterator First, InputIterator Last, TemplateArgumentListInfo &Outputs, | ||||||||
4222 | bool Uneval) { | ||||||||
4223 | for (; First != Last; ++First) { | ||||||||
4224 | TemplateArgumentLoc Out; | ||||||||
4225 | TemplateArgumentLoc In = *First; | ||||||||
4226 | |||||||||
4227 | if (In.getArgument().getKind() == TemplateArgument::Pack) { | ||||||||
4228 | // Unpack argument packs, which we translate them into separate | ||||||||
4229 | // arguments. | ||||||||
4230 | // FIXME: We could do much better if we could guarantee that the | ||||||||
4231 | // TemplateArgumentLocInfo for the pack expansion would be usable for | ||||||||
4232 | // all of the template arguments in the argument pack. | ||||||||
4233 | typedef TemplateArgumentLocInventIterator<Derived, | ||||||||
4234 | TemplateArgument::pack_iterator> | ||||||||
4235 | PackLocIterator; | ||||||||
4236 | if (TransformTemplateArguments(PackLocIterator(*this, | ||||||||
4237 | In.getArgument().pack_begin()), | ||||||||
4238 | PackLocIterator(*this, | ||||||||
4239 | In.getArgument().pack_end()), | ||||||||
4240 | Outputs, Uneval)) | ||||||||
4241 | return true; | ||||||||
4242 | |||||||||
4243 | continue; | ||||||||
4244 | } | ||||||||
4245 | |||||||||
4246 | if (In.getArgument().isPackExpansion()) { | ||||||||
4247 | // We have a pack expansion, for which we will be substituting into | ||||||||
4248 | // the pattern. | ||||||||
4249 | SourceLocation Ellipsis; | ||||||||
4250 | Optional<unsigned> OrigNumExpansions; | ||||||||
4251 | TemplateArgumentLoc Pattern | ||||||||
4252 | = getSema().getTemplateArgumentPackExpansionPattern( | ||||||||
4253 | In, Ellipsis, OrigNumExpansions); | ||||||||
4254 | |||||||||
4255 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | ||||||||
4256 | getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded); | ||||||||
4257 | assert(!Unexpanded.empty() && "Pack expansion without parameter packs?")((!Unexpanded.empty() && "Pack expansion without parameter packs?" ) ? static_cast<void> (0) : __assert_fail ("!Unexpanded.empty() && \"Pack expansion without parameter packs?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 4257, __PRETTY_FUNCTION__)); | ||||||||
4258 | |||||||||
4259 | // Determine whether the set of unexpanded parameter packs can and should | ||||||||
4260 | // be expanded. | ||||||||
4261 | bool Expand = true; | ||||||||
4262 | bool RetainExpansion = false; | ||||||||
4263 | Optional<unsigned> NumExpansions = OrigNumExpansions; | ||||||||
4264 | if (getDerived().TryExpandParameterPacks(Ellipsis, | ||||||||
4265 | Pattern.getSourceRange(), | ||||||||
4266 | Unexpanded, | ||||||||
4267 | Expand, | ||||||||
4268 | RetainExpansion, | ||||||||
4269 | NumExpansions)) | ||||||||
4270 | return true; | ||||||||
4271 | |||||||||
4272 | if (!Expand) { | ||||||||
4273 | // The transform has determined that we should perform a simple | ||||||||
4274 | // transformation on the pack expansion, producing another pack | ||||||||
4275 | // expansion. | ||||||||
4276 | TemplateArgumentLoc OutPattern; | ||||||||
4277 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1); | ||||||||
4278 | if (getDerived().TransformTemplateArgument(Pattern, OutPattern, Uneval)) | ||||||||
4279 | return true; | ||||||||
4280 | |||||||||
4281 | Out = getDerived().RebuildPackExpansion(OutPattern, Ellipsis, | ||||||||
4282 | NumExpansions); | ||||||||
4283 | if (Out.getArgument().isNull()) | ||||||||
4284 | return true; | ||||||||
4285 | |||||||||
4286 | Outputs.addArgument(Out); | ||||||||
4287 | continue; | ||||||||
4288 | } | ||||||||
4289 | |||||||||
4290 | // The transform has determined that we should perform an elementwise | ||||||||
4291 | // expansion of the pattern. Do so. | ||||||||
4292 | for (unsigned I = 0; I != *NumExpansions; ++I) { | ||||||||
4293 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I); | ||||||||
4294 | |||||||||
4295 | if (getDerived().TransformTemplateArgument(Pattern, Out, Uneval)) | ||||||||
4296 | return true; | ||||||||
4297 | |||||||||
4298 | if (Out.getArgument().containsUnexpandedParameterPack()) { | ||||||||
4299 | Out = getDerived().RebuildPackExpansion(Out, Ellipsis, | ||||||||
4300 | OrigNumExpansions); | ||||||||
4301 | if (Out.getArgument().isNull()) | ||||||||
4302 | return true; | ||||||||
4303 | } | ||||||||
4304 | |||||||||
4305 | Outputs.addArgument(Out); | ||||||||
4306 | } | ||||||||
4307 | |||||||||
4308 | // If we're supposed to retain a pack expansion, do so by temporarily | ||||||||
4309 | // forgetting the partially-substituted parameter pack. | ||||||||
4310 | if (RetainExpansion) { | ||||||||
4311 | ForgetPartiallySubstitutedPackRAII Forget(getDerived()); | ||||||||
4312 | |||||||||
4313 | if (getDerived().TransformTemplateArgument(Pattern, Out, Uneval)) | ||||||||
4314 | return true; | ||||||||
4315 | |||||||||
4316 | Out = getDerived().RebuildPackExpansion(Out, Ellipsis, | ||||||||
4317 | OrigNumExpansions); | ||||||||
4318 | if (Out.getArgument().isNull()) | ||||||||
4319 | return true; | ||||||||
4320 | |||||||||
4321 | Outputs.addArgument(Out); | ||||||||
4322 | } | ||||||||
4323 | |||||||||
4324 | continue; | ||||||||
4325 | } | ||||||||
4326 | |||||||||
4327 | // The simple case: | ||||||||
4328 | if (getDerived().TransformTemplateArgument(In, Out, Uneval)) | ||||||||
4329 | return true; | ||||||||
4330 | |||||||||
4331 | Outputs.addArgument(Out); | ||||||||
4332 | } | ||||||||
4333 | |||||||||
4334 | return false; | ||||||||
4335 | |||||||||
4336 | } | ||||||||
4337 | |||||||||
4338 | //===----------------------------------------------------------------------===// | ||||||||
4339 | // Type transformation | ||||||||
4340 | //===----------------------------------------------------------------------===// | ||||||||
4341 | |||||||||
4342 | template<typename Derived> | ||||||||
4343 | QualType TreeTransform<Derived>::TransformType(QualType T) { | ||||||||
4344 | if (getDerived().AlreadyTransformed(T)) | ||||||||
4345 | return T; | ||||||||
4346 | |||||||||
4347 | // Temporary workaround. All of these transformations should | ||||||||
4348 | // eventually turn into transformations on TypeLocs. | ||||||||
4349 | TypeSourceInfo *DI = getSema().Context.getTrivialTypeSourceInfo(T, | ||||||||
4350 | getDerived().getBaseLocation()); | ||||||||
4351 | |||||||||
4352 | TypeSourceInfo *NewDI = getDerived().TransformType(DI); | ||||||||
4353 | |||||||||
4354 | if (!NewDI) | ||||||||
4355 | return QualType(); | ||||||||
4356 | |||||||||
4357 | return NewDI->getType(); | ||||||||
4358 | } | ||||||||
4359 | |||||||||
4360 | template<typename Derived> | ||||||||
4361 | TypeSourceInfo *TreeTransform<Derived>::TransformType(TypeSourceInfo *DI) { | ||||||||
4362 | // Refine the base location to the type's location. | ||||||||
4363 | TemporaryBase Rebase(*this, DI->getTypeLoc().getBeginLoc(), | ||||||||
4364 | getDerived().getBaseEntity()); | ||||||||
4365 | if (getDerived().AlreadyTransformed(DI->getType())) | ||||||||
4366 | return DI; | ||||||||
4367 | |||||||||
4368 | TypeLocBuilder TLB; | ||||||||
4369 | |||||||||
4370 | TypeLoc TL = DI->getTypeLoc(); | ||||||||
4371 | TLB.reserve(TL.getFullDataSize()); | ||||||||
4372 | |||||||||
4373 | QualType Result = getDerived().TransformType(TLB, TL); | ||||||||
4374 | if (Result.isNull()) | ||||||||
4375 | return nullptr; | ||||||||
4376 | |||||||||
4377 | return TLB.getTypeSourceInfo(SemaRef.Context, Result); | ||||||||
4378 | } | ||||||||
4379 | |||||||||
4380 | template<typename Derived> | ||||||||
4381 | QualType | ||||||||
4382 | TreeTransform<Derived>::TransformType(TypeLocBuilder &TLB, TypeLoc T) { | ||||||||
4383 | switch (T.getTypeLocClass()) { | ||||||||
4384 | #define ABSTRACT_TYPELOC(CLASS, PARENT) | ||||||||
4385 | #define TYPELOC(CLASS, PARENT) \ | ||||||||
4386 | case TypeLoc::CLASS: \ | ||||||||
4387 | return getDerived().Transform##CLASS##Type(TLB, \ | ||||||||
4388 | T.castAs<CLASS##TypeLoc>()); | ||||||||
4389 | #include "clang/AST/TypeLocNodes.def" | ||||||||
4390 | } | ||||||||
4391 | |||||||||
4392 | llvm_unreachable("unhandled type loc!")::llvm::llvm_unreachable_internal("unhandled type loc!", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 4392); | ||||||||
4393 | } | ||||||||
4394 | |||||||||
4395 | template<typename Derived> | ||||||||
4396 | QualType TreeTransform<Derived>::TransformTypeWithDeducedTST(QualType T) { | ||||||||
4397 | if (!isa<DependentNameType>(T)) | ||||||||
4398 | return TransformType(T); | ||||||||
4399 | |||||||||
4400 | if (getDerived().AlreadyTransformed(T)) | ||||||||
4401 | return T; | ||||||||
4402 | TypeSourceInfo *DI = getSema().Context.getTrivialTypeSourceInfo(T, | ||||||||
4403 | getDerived().getBaseLocation()); | ||||||||
4404 | TypeSourceInfo *NewDI = getDerived().TransformTypeWithDeducedTST(DI); | ||||||||
4405 | return NewDI ? NewDI->getType() : QualType(); | ||||||||
4406 | } | ||||||||
4407 | |||||||||
4408 | template<typename Derived> | ||||||||
4409 | TypeSourceInfo * | ||||||||
4410 | TreeTransform<Derived>::TransformTypeWithDeducedTST(TypeSourceInfo *DI) { | ||||||||
4411 | if (!isa<DependentNameType>(DI->getType())) | ||||||||
4412 | return TransformType(DI); | ||||||||
4413 | |||||||||
4414 | // Refine the base location to the type's location. | ||||||||
4415 | TemporaryBase Rebase(*this, DI->getTypeLoc().getBeginLoc(), | ||||||||
4416 | getDerived().getBaseEntity()); | ||||||||
4417 | if (getDerived().AlreadyTransformed(DI->getType())) | ||||||||
4418 | return DI; | ||||||||
4419 | |||||||||
4420 | TypeLocBuilder TLB; | ||||||||
4421 | |||||||||
4422 | TypeLoc TL = DI->getTypeLoc(); | ||||||||
4423 | TLB.reserve(TL.getFullDataSize()); | ||||||||
4424 | |||||||||
4425 | auto QTL = TL.getAs<QualifiedTypeLoc>(); | ||||||||
4426 | if (QTL) | ||||||||
4427 | TL = QTL.getUnqualifiedLoc(); | ||||||||
4428 | |||||||||
4429 | auto DNTL = TL.castAs<DependentNameTypeLoc>(); | ||||||||
4430 | |||||||||
4431 | QualType Result = getDerived().TransformDependentNameType( | ||||||||
4432 | TLB, DNTL, /*DeducedTSTContext*/true); | ||||||||
4433 | if (Result.isNull()) | ||||||||
4434 | return nullptr; | ||||||||
4435 | |||||||||
4436 | if (QTL) { | ||||||||
4437 | Result = getDerived().RebuildQualifiedType(Result, QTL); | ||||||||
4438 | if (Result.isNull()) | ||||||||
4439 | return nullptr; | ||||||||
4440 | TLB.TypeWasModifiedSafely(Result); | ||||||||
4441 | } | ||||||||
4442 | |||||||||
4443 | return TLB.getTypeSourceInfo(SemaRef.Context, Result); | ||||||||
4444 | } | ||||||||
4445 | |||||||||
4446 | template<typename Derived> | ||||||||
4447 | QualType | ||||||||
4448 | TreeTransform<Derived>::TransformQualifiedType(TypeLocBuilder &TLB, | ||||||||
4449 | QualifiedTypeLoc T) { | ||||||||
4450 | QualType Result = getDerived().TransformType(TLB, T.getUnqualifiedLoc()); | ||||||||
4451 | if (Result.isNull()) | ||||||||
4452 | return QualType(); | ||||||||
4453 | |||||||||
4454 | Result = getDerived().RebuildQualifiedType(Result, T); | ||||||||
4455 | |||||||||
4456 | if (Result.isNull()) | ||||||||
4457 | return QualType(); | ||||||||
4458 | |||||||||
4459 | // RebuildQualifiedType might have updated the type, but not in a way | ||||||||
4460 | // that invalidates the TypeLoc. (There's no location information for | ||||||||
4461 | // qualifiers.) | ||||||||
4462 | TLB.TypeWasModifiedSafely(Result); | ||||||||
4463 | |||||||||
4464 | return Result; | ||||||||
4465 | } | ||||||||
4466 | |||||||||
4467 | template <typename Derived> | ||||||||
4468 | QualType TreeTransform<Derived>::RebuildQualifiedType(QualType T, | ||||||||
4469 | QualifiedTypeLoc TL) { | ||||||||
4470 | |||||||||
4471 | SourceLocation Loc = TL.getBeginLoc(); | ||||||||
4472 | Qualifiers Quals = TL.getType().getLocalQualifiers(); | ||||||||
4473 | |||||||||
4474 | if (((T.getAddressSpace() != LangAS::Default && | ||||||||
4475 | Quals.getAddressSpace() != LangAS::Default)) && | ||||||||
4476 | T.getAddressSpace() != Quals.getAddressSpace()) { | ||||||||
4477 | SemaRef.Diag(Loc, diag::err_address_space_mismatch_templ_inst) | ||||||||
4478 | << TL.getType() << T; | ||||||||
4479 | return QualType(); | ||||||||
4480 | } | ||||||||
4481 | |||||||||
4482 | // C++ [dcl.fct]p7: | ||||||||
4483 | // [When] adding cv-qualifications on top of the function type [...] the | ||||||||
4484 | // cv-qualifiers are ignored. | ||||||||
4485 | if (T->isFunctionType()) { | ||||||||
4486 | T = SemaRef.getASTContext().getAddrSpaceQualType(T, | ||||||||
4487 | Quals.getAddressSpace()); | ||||||||
4488 | return T; | ||||||||
4489 | } | ||||||||
4490 | |||||||||
4491 | // C++ [dcl.ref]p1: | ||||||||
4492 | // when the cv-qualifiers are introduced through the use of a typedef-name | ||||||||
4493 | // or decltype-specifier [...] the cv-qualifiers are ignored. | ||||||||
4494 | // Note that [dcl.ref]p1 lists all cases in which cv-qualifiers can be | ||||||||
4495 | // applied to a reference type. | ||||||||
4496 | if (T->isReferenceType()) { | ||||||||
4497 | // The only qualifier that applies to a reference type is restrict. | ||||||||
4498 | if (!Quals.hasRestrict()) | ||||||||
4499 | return T; | ||||||||
4500 | Quals = Qualifiers::fromCVRMask(Qualifiers::Restrict); | ||||||||
4501 | } | ||||||||
4502 | |||||||||
4503 | // Suppress Objective-C lifetime qualifiers if they don't make sense for the | ||||||||
4504 | // resulting type. | ||||||||
4505 | if (Quals.hasObjCLifetime()) { | ||||||||
4506 | if (!T->isObjCLifetimeType() && !T->isDependentType()) | ||||||||
4507 | Quals.removeObjCLifetime(); | ||||||||
4508 | else if (T.getObjCLifetime()) { | ||||||||
4509 | // Objective-C ARC: | ||||||||
4510 | // A lifetime qualifier applied to a substituted template parameter | ||||||||
4511 | // overrides the lifetime qualifier from the template argument. | ||||||||
4512 | const AutoType *AutoTy; | ||||||||
4513 | if (const SubstTemplateTypeParmType *SubstTypeParam | ||||||||
4514 | = dyn_cast<SubstTemplateTypeParmType>(T)) { | ||||||||
4515 | QualType Replacement = SubstTypeParam->getReplacementType(); | ||||||||
4516 | Qualifiers Qs = Replacement.getQualifiers(); | ||||||||
4517 | Qs.removeObjCLifetime(); | ||||||||
4518 | Replacement = SemaRef.Context.getQualifiedType( | ||||||||
4519 | Replacement.getUnqualifiedType(), Qs); | ||||||||
4520 | T = SemaRef.Context.getSubstTemplateTypeParmType( | ||||||||
4521 | SubstTypeParam->getReplacedParameter(), Replacement); | ||||||||
4522 | } else if ((AutoTy = dyn_cast<AutoType>(T)) && AutoTy->isDeduced()) { | ||||||||
4523 | // 'auto' types behave the same way as template parameters. | ||||||||
4524 | QualType Deduced = AutoTy->getDeducedType(); | ||||||||
4525 | Qualifiers Qs = Deduced.getQualifiers(); | ||||||||
4526 | Qs.removeObjCLifetime(); | ||||||||
4527 | Deduced = | ||||||||
4528 | SemaRef.Context.getQualifiedType(Deduced.getUnqualifiedType(), Qs); | ||||||||
4529 | T = SemaRef.Context.getAutoType(Deduced, AutoTy->getKeyword(), | ||||||||
4530 | AutoTy->isDependentType(), | ||||||||
4531 | /*isPack=*/false, | ||||||||
4532 | AutoTy->getTypeConstraintConcept(), | ||||||||
4533 | AutoTy->getTypeConstraintArguments()); | ||||||||
4534 | } else { | ||||||||
4535 | // Otherwise, complain about the addition of a qualifier to an | ||||||||
4536 | // already-qualified type. | ||||||||
4537 | // FIXME: Why is this check not in Sema::BuildQualifiedType? | ||||||||
4538 | SemaRef.Diag(Loc, diag::err_attr_objc_ownership_redundant) << T; | ||||||||
4539 | Quals.removeObjCLifetime(); | ||||||||
4540 | } | ||||||||
4541 | } | ||||||||
4542 | } | ||||||||
4543 | |||||||||
4544 | return SemaRef.BuildQualifiedType(T, Loc, Quals); | ||||||||
4545 | } | ||||||||
4546 | |||||||||
4547 | template<typename Derived> | ||||||||
4548 | TypeLoc | ||||||||
4549 | TreeTransform<Derived>::TransformTypeInObjectScope(TypeLoc TL, | ||||||||
4550 | QualType ObjectType, | ||||||||
4551 | NamedDecl *UnqualLookup, | ||||||||
4552 | CXXScopeSpec &SS) { | ||||||||
4553 | if (getDerived().AlreadyTransformed(TL.getType())) | ||||||||
4554 | return TL; | ||||||||
4555 | |||||||||
4556 | TypeSourceInfo *TSI = | ||||||||
4557 | TransformTSIInObjectScope(TL, ObjectType, UnqualLookup, SS); | ||||||||
4558 | if (TSI) | ||||||||
4559 | return TSI->getTypeLoc(); | ||||||||
4560 | return TypeLoc(); | ||||||||
4561 | } | ||||||||
4562 | |||||||||
4563 | template<typename Derived> | ||||||||
4564 | TypeSourceInfo * | ||||||||
4565 | TreeTransform<Derived>::TransformTypeInObjectScope(TypeSourceInfo *TSInfo, | ||||||||
4566 | QualType ObjectType, | ||||||||
4567 | NamedDecl *UnqualLookup, | ||||||||
4568 | CXXScopeSpec &SS) { | ||||||||
4569 | if (getDerived().AlreadyTransformed(TSInfo->getType())) | ||||||||
4570 | return TSInfo; | ||||||||
4571 | |||||||||
4572 | return TransformTSIInObjectScope(TSInfo->getTypeLoc(), ObjectType, | ||||||||
4573 | UnqualLookup, SS); | ||||||||
4574 | } | ||||||||
4575 | |||||||||
4576 | template <typename Derived> | ||||||||
4577 | TypeSourceInfo *TreeTransform<Derived>::TransformTSIInObjectScope( | ||||||||
4578 | TypeLoc TL, QualType ObjectType, NamedDecl *UnqualLookup, | ||||||||
4579 | CXXScopeSpec &SS) { | ||||||||
4580 | QualType T = TL.getType(); | ||||||||
4581 | assert(!getDerived().AlreadyTransformed(T))((!getDerived().AlreadyTransformed(T)) ? static_cast<void> (0) : __assert_fail ("!getDerived().AlreadyTransformed(T)", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 4581, __PRETTY_FUNCTION__)); | ||||||||
4582 | |||||||||
4583 | TypeLocBuilder TLB; | ||||||||
4584 | QualType Result; | ||||||||
4585 | |||||||||
4586 | if (isa<TemplateSpecializationType>(T)) { | ||||||||
4587 | TemplateSpecializationTypeLoc SpecTL = | ||||||||
4588 | TL.castAs<TemplateSpecializationTypeLoc>(); | ||||||||
4589 | |||||||||
4590 | TemplateName Template = getDerived().TransformTemplateName( | ||||||||
4591 | SS, SpecTL.getTypePtr()->getTemplateName(), SpecTL.getTemplateNameLoc(), | ||||||||
4592 | ObjectType, UnqualLookup, /*AllowInjectedClassName*/true); | ||||||||
4593 | if (Template.isNull()) | ||||||||
4594 | return nullptr; | ||||||||
4595 | |||||||||
4596 | Result = getDerived().TransformTemplateSpecializationType(TLB, SpecTL, | ||||||||
4597 | Template); | ||||||||
4598 | } else if (isa<DependentTemplateSpecializationType>(T)) { | ||||||||
4599 | DependentTemplateSpecializationTypeLoc SpecTL = | ||||||||
4600 | TL.castAs<DependentTemplateSpecializationTypeLoc>(); | ||||||||
4601 | |||||||||
4602 | TemplateName Template | ||||||||
4603 | = getDerived().RebuildTemplateName(SS, | ||||||||
4604 | SpecTL.getTemplateKeywordLoc(), | ||||||||
4605 | *SpecTL.getTypePtr()->getIdentifier(), | ||||||||
4606 | SpecTL.getTemplateNameLoc(), | ||||||||
4607 | ObjectType, UnqualLookup, | ||||||||
4608 | /*AllowInjectedClassName*/true); | ||||||||
4609 | if (Template.isNull()) | ||||||||
4610 | return nullptr; | ||||||||
4611 | |||||||||
4612 | Result = getDerived().TransformDependentTemplateSpecializationType(TLB, | ||||||||
4613 | SpecTL, | ||||||||
4614 | Template, | ||||||||
4615 | SS); | ||||||||
4616 | } else { | ||||||||
4617 | // Nothing special needs to be done for these. | ||||||||
4618 | Result = getDerived().TransformType(TLB, TL); | ||||||||
4619 | } | ||||||||
4620 | |||||||||
4621 | if (Result.isNull()) | ||||||||
4622 | return nullptr; | ||||||||
4623 | |||||||||
4624 | return TLB.getTypeSourceInfo(SemaRef.Context, Result); | ||||||||
4625 | } | ||||||||
4626 | |||||||||
4627 | template <class TyLoc> static inline | ||||||||
4628 | QualType TransformTypeSpecType(TypeLocBuilder &TLB, TyLoc T) { | ||||||||
4629 | TyLoc NewT = TLB.push<TyLoc>(T.getType()); | ||||||||
4630 | NewT.setNameLoc(T.getNameLoc()); | ||||||||
4631 | return T.getType(); | ||||||||
4632 | } | ||||||||
4633 | |||||||||
4634 | template<typename Derived> | ||||||||
4635 | QualType TreeTransform<Derived>::TransformBuiltinType(TypeLocBuilder &TLB, | ||||||||
4636 | BuiltinTypeLoc T) { | ||||||||
4637 | BuiltinTypeLoc NewT = TLB.push<BuiltinTypeLoc>(T.getType()); | ||||||||
4638 | NewT.setBuiltinLoc(T.getBuiltinLoc()); | ||||||||
4639 | if (T.needsExtraLocalData()) | ||||||||
4640 | NewT.getWrittenBuiltinSpecs() = T.getWrittenBuiltinSpecs(); | ||||||||
4641 | return T.getType(); | ||||||||
4642 | } | ||||||||
4643 | |||||||||
4644 | template<typename Derived> | ||||||||
4645 | QualType TreeTransform<Derived>::TransformComplexType(TypeLocBuilder &TLB, | ||||||||
4646 | ComplexTypeLoc T) { | ||||||||
4647 | // FIXME: recurse? | ||||||||
4648 | return TransformTypeSpecType(TLB, T); | ||||||||
4649 | } | ||||||||
4650 | |||||||||
4651 | template <typename Derived> | ||||||||
4652 | QualType TreeTransform<Derived>::TransformAdjustedType(TypeLocBuilder &TLB, | ||||||||
4653 | AdjustedTypeLoc TL) { | ||||||||
4654 | // Adjustments applied during transformation are handled elsewhere. | ||||||||
4655 | return getDerived().TransformType(TLB, TL.getOriginalLoc()); | ||||||||
4656 | } | ||||||||
4657 | |||||||||
4658 | template<typename Derived> | ||||||||
4659 | QualType TreeTransform<Derived>::TransformDecayedType(TypeLocBuilder &TLB, | ||||||||
4660 | DecayedTypeLoc TL) { | ||||||||
4661 | QualType OriginalType = getDerived().TransformType(TLB, TL.getOriginalLoc()); | ||||||||
4662 | if (OriginalType.isNull()) | ||||||||
4663 | return QualType(); | ||||||||
4664 | |||||||||
4665 | QualType Result = TL.getType(); | ||||||||
4666 | if (getDerived().AlwaysRebuild() || | ||||||||
4667 | OriginalType != TL.getOriginalLoc().getType()) | ||||||||
4668 | Result = SemaRef.Context.getDecayedType(OriginalType); | ||||||||
4669 | TLB.push<DecayedTypeLoc>(Result); | ||||||||
4670 | // Nothing to set for DecayedTypeLoc. | ||||||||
4671 | return Result; | ||||||||
4672 | } | ||||||||
4673 | |||||||||
4674 | template<typename Derived> | ||||||||
4675 | QualType TreeTransform<Derived>::TransformPointerType(TypeLocBuilder &TLB, | ||||||||
4676 | PointerTypeLoc TL) { | ||||||||
4677 | QualType PointeeType | ||||||||
4678 | = getDerived().TransformType(TLB, TL.getPointeeLoc()); | ||||||||
4679 | if (PointeeType.isNull()) | ||||||||
4680 | return QualType(); | ||||||||
4681 | |||||||||
4682 | QualType Result = TL.getType(); | ||||||||
4683 | if (PointeeType->getAs<ObjCObjectType>()) { | ||||||||
4684 | // A dependent pointer type 'T *' has is being transformed such | ||||||||
4685 | // that an Objective-C class type is being replaced for 'T'. The | ||||||||
4686 | // resulting pointer type is an ObjCObjectPointerType, not a | ||||||||
4687 | // PointerType. | ||||||||
4688 | Result = SemaRef.Context.getObjCObjectPointerType(PointeeType); | ||||||||
4689 | |||||||||
4690 | ObjCObjectPointerTypeLoc NewT = TLB.push<ObjCObjectPointerTypeLoc>(Result); | ||||||||
4691 | NewT.setStarLoc(TL.getStarLoc()); | ||||||||
4692 | return Result; | ||||||||
4693 | } | ||||||||
4694 | |||||||||
4695 | if (getDerived().AlwaysRebuild() || | ||||||||
4696 | PointeeType != TL.getPointeeLoc().getType()) { | ||||||||
4697 | Result = getDerived().RebuildPointerType(PointeeType, TL.getSigilLoc()); | ||||||||
4698 | if (Result.isNull()) | ||||||||
4699 | return QualType(); | ||||||||
4700 | } | ||||||||
4701 | |||||||||
4702 | // Objective-C ARC can add lifetime qualifiers to the type that we're | ||||||||
4703 | // pointing to. | ||||||||
4704 | TLB.TypeWasModifiedSafely(Result->getPointeeType()); | ||||||||
4705 | |||||||||
4706 | PointerTypeLoc NewT = TLB.push<PointerTypeLoc>(Result); | ||||||||
4707 | NewT.setSigilLoc(TL.getSigilLoc()); | ||||||||
4708 | return Result; | ||||||||
4709 | } | ||||||||
4710 | |||||||||
4711 | template<typename Derived> | ||||||||
4712 | QualType | ||||||||
4713 | TreeTransform<Derived>::TransformBlockPointerType(TypeLocBuilder &TLB, | ||||||||
4714 | BlockPointerTypeLoc TL) { | ||||||||
4715 | QualType PointeeType | ||||||||
4716 | = getDerived().TransformType(TLB, TL.getPointeeLoc()); | ||||||||
4717 | if (PointeeType.isNull()) | ||||||||
4718 | return QualType(); | ||||||||
4719 | |||||||||
4720 | QualType Result = TL.getType(); | ||||||||
4721 | if (getDerived().AlwaysRebuild() || | ||||||||
4722 | PointeeType != TL.getPointeeLoc().getType()) { | ||||||||
4723 | Result = getDerived().RebuildBlockPointerType(PointeeType, | ||||||||
4724 | TL.getSigilLoc()); | ||||||||
4725 | if (Result.isNull()) | ||||||||
4726 | return QualType(); | ||||||||
4727 | } | ||||||||
4728 | |||||||||
4729 | BlockPointerTypeLoc NewT = TLB.push<BlockPointerTypeLoc>(Result); | ||||||||
4730 | NewT.setSigilLoc(TL.getSigilLoc()); | ||||||||
4731 | return Result; | ||||||||
4732 | } | ||||||||
4733 | |||||||||
4734 | /// Transforms a reference type. Note that somewhat paradoxically we | ||||||||
4735 | /// don't care whether the type itself is an l-value type or an r-value | ||||||||
4736 | /// type; we only care if the type was *written* as an l-value type | ||||||||
4737 | /// or an r-value type. | ||||||||
4738 | template<typename Derived> | ||||||||
4739 | QualType | ||||||||
4740 | TreeTransform<Derived>::TransformReferenceType(TypeLocBuilder &TLB, | ||||||||
4741 | ReferenceTypeLoc TL) { | ||||||||
4742 | const ReferenceType *T = TL.getTypePtr(); | ||||||||
4743 | |||||||||
4744 | // Note that this works with the pointee-as-written. | ||||||||
4745 | QualType PointeeType = getDerived().TransformType(TLB, TL.getPointeeLoc()); | ||||||||
4746 | if (PointeeType.isNull()) | ||||||||
4747 | return QualType(); | ||||||||
4748 | |||||||||
4749 | QualType Result = TL.getType(); | ||||||||
4750 | if (getDerived().AlwaysRebuild() || | ||||||||
4751 | PointeeType != T->getPointeeTypeAsWritten()) { | ||||||||
4752 | Result = getDerived().RebuildReferenceType(PointeeType, | ||||||||
4753 | T->isSpelledAsLValue(), | ||||||||
4754 | TL.getSigilLoc()); | ||||||||
4755 | if (Result.isNull()) | ||||||||
4756 | return QualType(); | ||||||||
4757 | } | ||||||||
4758 | |||||||||
4759 | // Objective-C ARC can add lifetime qualifiers to the type that we're | ||||||||
4760 | // referring to. | ||||||||
4761 | TLB.TypeWasModifiedSafely( | ||||||||
4762 | Result->castAs<ReferenceType>()->getPointeeTypeAsWritten()); | ||||||||
4763 | |||||||||
4764 | // r-value references can be rebuilt as l-value references. | ||||||||
4765 | ReferenceTypeLoc NewTL; | ||||||||
4766 | if (isa<LValueReferenceType>(Result)) | ||||||||
4767 | NewTL = TLB.push<LValueReferenceTypeLoc>(Result); | ||||||||
4768 | else | ||||||||
4769 | NewTL = TLB.push<RValueReferenceTypeLoc>(Result); | ||||||||
4770 | NewTL.setSigilLoc(TL.getSigilLoc()); | ||||||||
4771 | |||||||||
4772 | return Result; | ||||||||
4773 | } | ||||||||
4774 | |||||||||
4775 | template<typename Derived> | ||||||||
4776 | QualType | ||||||||
4777 | TreeTransform<Derived>::TransformLValueReferenceType(TypeLocBuilder &TLB, | ||||||||
4778 | LValueReferenceTypeLoc TL) { | ||||||||
4779 | return TransformReferenceType(TLB, TL); | ||||||||
4780 | } | ||||||||
4781 | |||||||||
4782 | template<typename Derived> | ||||||||
4783 | QualType | ||||||||
4784 | TreeTransform<Derived>::TransformRValueReferenceType(TypeLocBuilder &TLB, | ||||||||
4785 | RValueReferenceTypeLoc TL) { | ||||||||
4786 | return TransformReferenceType(TLB, TL); | ||||||||
4787 | } | ||||||||
4788 | |||||||||
4789 | template<typename Derived> | ||||||||
4790 | QualType | ||||||||
4791 | TreeTransform<Derived>::TransformMemberPointerType(TypeLocBuilder &TLB, | ||||||||
4792 | MemberPointerTypeLoc TL) { | ||||||||
4793 | QualType PointeeType = getDerived().TransformType(TLB, TL.getPointeeLoc()); | ||||||||
4794 | if (PointeeType.isNull()) | ||||||||
4795 | return QualType(); | ||||||||
4796 | |||||||||
4797 | TypeSourceInfo* OldClsTInfo = TL.getClassTInfo(); | ||||||||
4798 | TypeSourceInfo *NewClsTInfo = nullptr; | ||||||||
4799 | if (OldClsTInfo) { | ||||||||
4800 | NewClsTInfo = getDerived().TransformType(OldClsTInfo); | ||||||||
4801 | if (!NewClsTInfo) | ||||||||
4802 | return QualType(); | ||||||||
4803 | } | ||||||||
4804 | |||||||||
4805 | const MemberPointerType *T = TL.getTypePtr(); | ||||||||
4806 | QualType OldClsType = QualType(T->getClass(), 0); | ||||||||
4807 | QualType NewClsType; | ||||||||
4808 | if (NewClsTInfo) | ||||||||
4809 | NewClsType = NewClsTInfo->getType(); | ||||||||
4810 | else { | ||||||||
4811 | NewClsType = getDerived().TransformType(OldClsType); | ||||||||
4812 | if (NewClsType.isNull()) | ||||||||
4813 | return QualType(); | ||||||||
4814 | } | ||||||||
4815 | |||||||||
4816 | QualType Result = TL.getType(); | ||||||||
4817 | if (getDerived().AlwaysRebuild() || | ||||||||
4818 | PointeeType != T->getPointeeType() || | ||||||||
4819 | NewClsType != OldClsType) { | ||||||||
4820 | Result = getDerived().RebuildMemberPointerType(PointeeType, NewClsType, | ||||||||
4821 | TL.getStarLoc()); | ||||||||
4822 | if (Result.isNull()) | ||||||||
4823 | return QualType(); | ||||||||
4824 | } | ||||||||
4825 | |||||||||
4826 | // If we had to adjust the pointee type when building a member pointer, make | ||||||||
4827 | // sure to push TypeLoc info for it. | ||||||||
4828 | const MemberPointerType *MPT = Result->getAs<MemberPointerType>(); | ||||||||
4829 | if (MPT && PointeeType != MPT->getPointeeType()) { | ||||||||
4830 | assert(isa<AdjustedType>(MPT->getPointeeType()))((isa<AdjustedType>(MPT->getPointeeType())) ? static_cast <void> (0) : __assert_fail ("isa<AdjustedType>(MPT->getPointeeType())" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 4830, __PRETTY_FUNCTION__)); | ||||||||
4831 | TLB.push<AdjustedTypeLoc>(MPT->getPointeeType()); | ||||||||
4832 | } | ||||||||
4833 | |||||||||
4834 | MemberPointerTypeLoc NewTL = TLB.push<MemberPointerTypeLoc>(Result); | ||||||||
4835 | NewTL.setSigilLoc(TL.getSigilLoc()); | ||||||||
4836 | NewTL.setClassTInfo(NewClsTInfo); | ||||||||
4837 | |||||||||
4838 | return Result; | ||||||||
4839 | } | ||||||||
4840 | |||||||||
4841 | template<typename Derived> | ||||||||
4842 | QualType | ||||||||
4843 | TreeTransform<Derived>::TransformConstantArrayType(TypeLocBuilder &TLB, | ||||||||
4844 | ConstantArrayTypeLoc TL) { | ||||||||
4845 | const ConstantArrayType *T = TL.getTypePtr(); | ||||||||
4846 | QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc()); | ||||||||
4847 | if (ElementType.isNull()) | ||||||||
4848 | return QualType(); | ||||||||
4849 | |||||||||
4850 | // Prefer the expression from the TypeLoc; the other may have been uniqued. | ||||||||
4851 | Expr *OldSize = TL.getSizeExpr(); | ||||||||
4852 | if (!OldSize) | ||||||||
4853 | OldSize = const_cast<Expr*>(T->getSizeExpr()); | ||||||||
4854 | Expr *NewSize = nullptr; | ||||||||
4855 | if (OldSize) { | ||||||||
4856 | EnterExpressionEvaluationContext Unevaluated( | ||||||||
4857 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||
4858 | NewSize = getDerived().TransformExpr(OldSize).template getAs<Expr>(); | ||||||||
4859 | NewSize = SemaRef.ActOnConstantExpression(NewSize).get(); | ||||||||
4860 | } | ||||||||
4861 | |||||||||
4862 | QualType Result = TL.getType(); | ||||||||
4863 | if (getDerived().AlwaysRebuild() || | ||||||||
4864 | ElementType != T->getElementType() || | ||||||||
4865 | (T->getSizeExpr() && NewSize != OldSize)) { | ||||||||
4866 | Result = getDerived().RebuildConstantArrayType(ElementType, | ||||||||
4867 | T->getSizeModifier(), | ||||||||
4868 | T->getSize(), NewSize, | ||||||||
4869 | T->getIndexTypeCVRQualifiers(), | ||||||||
4870 | TL.getBracketsRange()); | ||||||||
4871 | if (Result.isNull()) | ||||||||
4872 | return QualType(); | ||||||||
4873 | } | ||||||||
4874 | |||||||||
4875 | // We might have either a ConstantArrayType or a VariableArrayType now: | ||||||||
4876 | // a ConstantArrayType is allowed to have an element type which is a | ||||||||
4877 | // VariableArrayType if the type is dependent. Fortunately, all array | ||||||||
4878 | // types have the same location layout. | ||||||||
4879 | ArrayTypeLoc NewTL = TLB.push<ArrayTypeLoc>(Result); | ||||||||
4880 | NewTL.setLBracketLoc(TL.getLBracketLoc()); | ||||||||
4881 | NewTL.setRBracketLoc(TL.getRBracketLoc()); | ||||||||
4882 | NewTL.setSizeExpr(NewSize); | ||||||||
4883 | |||||||||
4884 | return Result; | ||||||||
4885 | } | ||||||||
4886 | |||||||||
4887 | template<typename Derived> | ||||||||
4888 | QualType TreeTransform<Derived>::TransformIncompleteArrayType( | ||||||||
4889 | TypeLocBuilder &TLB, | ||||||||
4890 | IncompleteArrayTypeLoc TL) { | ||||||||
4891 | const IncompleteArrayType *T = TL.getTypePtr(); | ||||||||
4892 | QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc()); | ||||||||
4893 | if (ElementType.isNull()) | ||||||||
4894 | return QualType(); | ||||||||
4895 | |||||||||
4896 | QualType Result = TL.getType(); | ||||||||
4897 | if (getDerived().AlwaysRebuild() || | ||||||||
4898 | ElementType != T->getElementType()) { | ||||||||
4899 | Result = getDerived().RebuildIncompleteArrayType(ElementType, | ||||||||
4900 | T->getSizeModifier(), | ||||||||
4901 | T->getIndexTypeCVRQualifiers(), | ||||||||
4902 | TL.getBracketsRange()); | ||||||||
4903 | if (Result.isNull()) | ||||||||
4904 | return QualType(); | ||||||||
4905 | } | ||||||||
4906 | |||||||||
4907 | IncompleteArrayTypeLoc NewTL = TLB.push<IncompleteArrayTypeLoc>(Result); | ||||||||
4908 | NewTL.setLBracketLoc(TL.getLBracketLoc()); | ||||||||
4909 | NewTL.setRBracketLoc(TL.getRBracketLoc()); | ||||||||
4910 | NewTL.setSizeExpr(nullptr); | ||||||||
4911 | |||||||||
4912 | return Result; | ||||||||
4913 | } | ||||||||
4914 | |||||||||
4915 | template<typename Derived> | ||||||||
4916 | QualType | ||||||||
4917 | TreeTransform<Derived>::TransformVariableArrayType(TypeLocBuilder &TLB, | ||||||||
4918 | VariableArrayTypeLoc TL) { | ||||||||
4919 | const VariableArrayType *T = TL.getTypePtr(); | ||||||||
4920 | QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc()); | ||||||||
4921 | if (ElementType.isNull()) | ||||||||
4922 | return QualType(); | ||||||||
4923 | |||||||||
4924 | ExprResult SizeResult; | ||||||||
4925 | { | ||||||||
4926 | EnterExpressionEvaluationContext Context( | ||||||||
4927 | SemaRef, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); | ||||||||
4928 | SizeResult = getDerived().TransformExpr(T->getSizeExpr()); | ||||||||
4929 | } | ||||||||
4930 | if (SizeResult.isInvalid()) | ||||||||
4931 | return QualType(); | ||||||||
4932 | SizeResult = | ||||||||
4933 | SemaRef.ActOnFinishFullExpr(SizeResult.get(), /*DiscardedValue*/ false); | ||||||||
4934 | if (SizeResult.isInvalid()) | ||||||||
4935 | return QualType(); | ||||||||
4936 | |||||||||
4937 | Expr *Size = SizeResult.get(); | ||||||||
4938 | |||||||||
4939 | QualType Result = TL.getType(); | ||||||||
4940 | if (getDerived().AlwaysRebuild() || | ||||||||
4941 | ElementType != T->getElementType() || | ||||||||
4942 | Size != T->getSizeExpr()) { | ||||||||
4943 | Result = getDerived().RebuildVariableArrayType(ElementType, | ||||||||
4944 | T->getSizeModifier(), | ||||||||
4945 | Size, | ||||||||
4946 | T->getIndexTypeCVRQualifiers(), | ||||||||
4947 | TL.getBracketsRange()); | ||||||||
4948 | if (Result.isNull()) | ||||||||
4949 | return QualType(); | ||||||||
4950 | } | ||||||||
4951 | |||||||||
4952 | // We might have constant size array now, but fortunately it has the same | ||||||||
4953 | // location layout. | ||||||||
4954 | ArrayTypeLoc NewTL = TLB.push<ArrayTypeLoc>(Result); | ||||||||
4955 | NewTL.setLBracketLoc(TL.getLBracketLoc()); | ||||||||
4956 | NewTL.setRBracketLoc(TL.getRBracketLoc()); | ||||||||
4957 | NewTL.setSizeExpr(Size); | ||||||||
4958 | |||||||||
4959 | return Result; | ||||||||
4960 | } | ||||||||
4961 | |||||||||
4962 | template<typename Derived> | ||||||||
4963 | QualType | ||||||||
4964 | TreeTransform<Derived>::TransformDependentSizedArrayType(TypeLocBuilder &TLB, | ||||||||
4965 | DependentSizedArrayTypeLoc TL) { | ||||||||
4966 | const DependentSizedArrayType *T = TL.getTypePtr(); | ||||||||
4967 | QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc()); | ||||||||
4968 | if (ElementType.isNull()) | ||||||||
4969 | return QualType(); | ||||||||
4970 | |||||||||
4971 | // Array bounds are constant expressions. | ||||||||
4972 | EnterExpressionEvaluationContext Unevaluated( | ||||||||
4973 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||
4974 | |||||||||
4975 | // Prefer the expression from the TypeLoc; the other may have been uniqued. | ||||||||
4976 | Expr *origSize = TL.getSizeExpr(); | ||||||||
4977 | if (!origSize) origSize = T->getSizeExpr(); | ||||||||
4978 | |||||||||
4979 | ExprResult sizeResult | ||||||||
4980 | = getDerived().TransformExpr(origSize); | ||||||||
4981 | sizeResult = SemaRef.ActOnConstantExpression(sizeResult); | ||||||||
4982 | if (sizeResult.isInvalid()) | ||||||||
4983 | return QualType(); | ||||||||
4984 | |||||||||
4985 | Expr *size = sizeResult.get(); | ||||||||
4986 | |||||||||
4987 | QualType Result = TL.getType(); | ||||||||
4988 | if (getDerived().AlwaysRebuild() || | ||||||||
4989 | ElementType != T->getElementType() || | ||||||||
4990 | size != origSize) { | ||||||||
4991 | Result = getDerived().RebuildDependentSizedArrayType(ElementType, | ||||||||
4992 | T->getSizeModifier(), | ||||||||
4993 | size, | ||||||||
4994 | T->getIndexTypeCVRQualifiers(), | ||||||||
4995 | TL.getBracketsRange()); | ||||||||
4996 | if (Result.isNull()) | ||||||||
4997 | return QualType(); | ||||||||
4998 | } | ||||||||
4999 | |||||||||
5000 | // We might have any sort of array type now, but fortunately they | ||||||||
5001 | // all have the same location layout. | ||||||||
5002 | ArrayTypeLoc NewTL = TLB.push<ArrayTypeLoc>(Result); | ||||||||
5003 | NewTL.setLBracketLoc(TL.getLBracketLoc()); | ||||||||
5004 | NewTL.setRBracketLoc(TL.getRBracketLoc()); | ||||||||
5005 | NewTL.setSizeExpr(size); | ||||||||
5006 | |||||||||
5007 | return Result; | ||||||||
5008 | } | ||||||||
5009 | |||||||||
5010 | template <typename Derived> | ||||||||
5011 | QualType TreeTransform<Derived>::TransformDependentVectorType( | ||||||||
5012 | TypeLocBuilder &TLB, DependentVectorTypeLoc TL) { | ||||||||
5013 | const DependentVectorType *T = TL.getTypePtr(); | ||||||||
5014 | QualType ElementType = getDerived().TransformType(T->getElementType()); | ||||||||
5015 | if (ElementType.isNull()) | ||||||||
5016 | return QualType(); | ||||||||
5017 | |||||||||
5018 | EnterExpressionEvaluationContext Unevaluated( | ||||||||
5019 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||
5020 | |||||||||
5021 | ExprResult Size = getDerived().TransformExpr(T->getSizeExpr()); | ||||||||
5022 | Size = SemaRef.ActOnConstantExpression(Size); | ||||||||
5023 | if (Size.isInvalid()) | ||||||||
5024 | return QualType(); | ||||||||
5025 | |||||||||
5026 | QualType Result = TL.getType(); | ||||||||
5027 | if (getDerived().AlwaysRebuild() || ElementType != T->getElementType() || | ||||||||
5028 | Size.get() != T->getSizeExpr()) { | ||||||||
5029 | Result = getDerived().RebuildDependentVectorType( | ||||||||
5030 | ElementType, Size.get(), T->getAttributeLoc(), T->getVectorKind()); | ||||||||
5031 | if (Result.isNull()) | ||||||||
5032 | return QualType(); | ||||||||
5033 | } | ||||||||
5034 | |||||||||
5035 | // Result might be dependent or not. | ||||||||
5036 | if (isa<DependentVectorType>(Result)) { | ||||||||
5037 | DependentVectorTypeLoc NewTL = | ||||||||
5038 | TLB.push<DependentVectorTypeLoc>(Result); | ||||||||
5039 | NewTL.setNameLoc(TL.getNameLoc()); | ||||||||
5040 | } else { | ||||||||
5041 | VectorTypeLoc NewTL = TLB.push<VectorTypeLoc>(Result); | ||||||||
5042 | NewTL.setNameLoc(TL.getNameLoc()); | ||||||||
5043 | } | ||||||||
5044 | |||||||||
5045 | return Result; | ||||||||
5046 | } | ||||||||
5047 | |||||||||
5048 | template<typename Derived> | ||||||||
5049 | QualType TreeTransform<Derived>::TransformDependentSizedExtVectorType( | ||||||||
5050 | TypeLocBuilder &TLB, | ||||||||
5051 | DependentSizedExtVectorTypeLoc TL) { | ||||||||
5052 | const DependentSizedExtVectorType *T = TL.getTypePtr(); | ||||||||
5053 | |||||||||
5054 | // FIXME: ext vector locs should be nested | ||||||||
5055 | QualType ElementType = getDerived().TransformType(T->getElementType()); | ||||||||
5056 | if (ElementType.isNull()) | ||||||||
5057 | return QualType(); | ||||||||
5058 | |||||||||
5059 | // Vector sizes are constant expressions. | ||||||||
5060 | EnterExpressionEvaluationContext Unevaluated( | ||||||||
5061 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||
5062 | |||||||||
5063 | ExprResult Size = getDerived().TransformExpr(T->getSizeExpr()); | ||||||||
5064 | Size = SemaRef.ActOnConstantExpression(Size); | ||||||||
5065 | if (Size.isInvalid()) | ||||||||
5066 | return QualType(); | ||||||||
5067 | |||||||||
5068 | QualType Result = TL.getType(); | ||||||||
5069 | if (getDerived().AlwaysRebuild() || | ||||||||
5070 | ElementType != T->getElementType() || | ||||||||
5071 | Size.get() != T->getSizeExpr()) { | ||||||||
5072 | Result = getDerived().RebuildDependentSizedExtVectorType(ElementType, | ||||||||
5073 | Size.get(), | ||||||||
5074 | T->getAttributeLoc()); | ||||||||
5075 | if (Result.isNull()) | ||||||||
5076 | return QualType(); | ||||||||
5077 | } | ||||||||
5078 | |||||||||
5079 | // Result might be dependent or not. | ||||||||
5080 | if (isa<DependentSizedExtVectorType>(Result)) { | ||||||||
5081 | DependentSizedExtVectorTypeLoc NewTL | ||||||||
5082 | = TLB.push<DependentSizedExtVectorTypeLoc>(Result); | ||||||||
5083 | NewTL.setNameLoc(TL.getNameLoc()); | ||||||||
5084 | } else { | ||||||||
5085 | ExtVectorTypeLoc NewTL = TLB.push<ExtVectorTypeLoc>(Result); | ||||||||
5086 | NewTL.setNameLoc(TL.getNameLoc()); | ||||||||
5087 | } | ||||||||
5088 | |||||||||
5089 | return Result; | ||||||||
5090 | } | ||||||||
5091 | |||||||||
5092 | template <typename Derived> | ||||||||
5093 | QualType TreeTransform<Derived>::TransformDependentAddressSpaceType( | ||||||||
5094 | TypeLocBuilder &TLB, DependentAddressSpaceTypeLoc TL) { | ||||||||
5095 | const DependentAddressSpaceType *T = TL.getTypePtr(); | ||||||||
5096 | |||||||||
5097 | QualType pointeeType = getDerived().TransformType(T->getPointeeType()); | ||||||||
5098 | |||||||||
5099 | if (pointeeType.isNull()) | ||||||||
5100 | return QualType(); | ||||||||
5101 | |||||||||
5102 | // Address spaces are constant expressions. | ||||||||
5103 | EnterExpressionEvaluationContext Unevaluated( | ||||||||
5104 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||
5105 | |||||||||
5106 | ExprResult AddrSpace = getDerived().TransformExpr(T->getAddrSpaceExpr()); | ||||||||
5107 | AddrSpace = SemaRef.ActOnConstantExpression(AddrSpace); | ||||||||
5108 | if (AddrSpace.isInvalid()) | ||||||||
5109 | return QualType(); | ||||||||
5110 | |||||||||
5111 | QualType Result = TL.getType(); | ||||||||
5112 | if (getDerived().AlwaysRebuild() || pointeeType != T->getPointeeType() || | ||||||||
5113 | AddrSpace.get() != T->getAddrSpaceExpr()) { | ||||||||
5114 | Result = getDerived().RebuildDependentAddressSpaceType( | ||||||||
5115 | pointeeType, AddrSpace.get(), T->getAttributeLoc()); | ||||||||
5116 | if (Result.isNull()) | ||||||||
5117 | return QualType(); | ||||||||
5118 | } | ||||||||
5119 | |||||||||
5120 | // Result might be dependent or not. | ||||||||
5121 | if (isa<DependentAddressSpaceType>(Result)) { | ||||||||
5122 | DependentAddressSpaceTypeLoc NewTL = | ||||||||
5123 | TLB.push<DependentAddressSpaceTypeLoc>(Result); | ||||||||
5124 | |||||||||
5125 | NewTL.setAttrOperandParensRange(TL.getAttrOperandParensRange()); | ||||||||
5126 | NewTL.setAttrExprOperand(TL.getAttrExprOperand()); | ||||||||
5127 | NewTL.setAttrNameLoc(TL.getAttrNameLoc()); | ||||||||
5128 | |||||||||
5129 | } else { | ||||||||
5130 | TypeSourceInfo *DI = getSema().Context.getTrivialTypeSourceInfo( | ||||||||
5131 | Result, getDerived().getBaseLocation()); | ||||||||
5132 | TransformType(TLB, DI->getTypeLoc()); | ||||||||
5133 | } | ||||||||
5134 | |||||||||
5135 | return Result; | ||||||||
5136 | } | ||||||||
5137 | |||||||||
5138 | template <typename Derived> | ||||||||
5139 | QualType TreeTransform<Derived>::TransformVectorType(TypeLocBuilder &TLB, | ||||||||
5140 | VectorTypeLoc TL) { | ||||||||
5141 | const VectorType *T = TL.getTypePtr(); | ||||||||
5142 | QualType ElementType = getDerived().TransformType(T->getElementType()); | ||||||||
5143 | if (ElementType.isNull()) | ||||||||
5144 | return QualType(); | ||||||||
5145 | |||||||||
5146 | QualType Result = TL.getType(); | ||||||||
5147 | if (getDerived().AlwaysRebuild() || | ||||||||
5148 | ElementType != T->getElementType()) { | ||||||||
5149 | Result = getDerived().RebuildVectorType(ElementType, T->getNumElements(), | ||||||||
5150 | T->getVectorKind()); | ||||||||
5151 | if (Result.isNull()) | ||||||||
5152 | return QualType(); | ||||||||
5153 | } | ||||||||
5154 | |||||||||
5155 | VectorTypeLoc NewTL = TLB.push<VectorTypeLoc>(Result); | ||||||||
5156 | NewTL.setNameLoc(TL.getNameLoc()); | ||||||||
5157 | |||||||||
5158 | return Result; | ||||||||
5159 | } | ||||||||
5160 | |||||||||
5161 | template<typename Derived> | ||||||||
5162 | QualType TreeTransform<Derived>::TransformExtVectorType(TypeLocBuilder &TLB, | ||||||||
5163 | ExtVectorTypeLoc TL) { | ||||||||
5164 | const VectorType *T = TL.getTypePtr(); | ||||||||
5165 | QualType ElementType = getDerived().TransformType(T->getElementType()); | ||||||||
5166 | if (ElementType.isNull()) | ||||||||
5167 | return QualType(); | ||||||||
5168 | |||||||||
5169 | QualType Result = TL.getType(); | ||||||||
5170 | if (getDerived().AlwaysRebuild() || | ||||||||
5171 | ElementType != T->getElementType()) { | ||||||||
5172 | Result = getDerived().RebuildExtVectorType(ElementType, | ||||||||
5173 | T->getNumElements(), | ||||||||
5174 | /*FIXME*/ SourceLocation()); | ||||||||
5175 | if (Result.isNull()) | ||||||||
5176 | return QualType(); | ||||||||
5177 | } | ||||||||
5178 | |||||||||
5179 | ExtVectorTypeLoc NewTL = TLB.push<ExtVectorTypeLoc>(Result); | ||||||||
5180 | NewTL.setNameLoc(TL.getNameLoc()); | ||||||||
5181 | |||||||||
5182 | return Result; | ||||||||
5183 | } | ||||||||
5184 | |||||||||
5185 | template <typename Derived> | ||||||||
5186 | ParmVarDecl *TreeTransform<Derived>::TransformFunctionTypeParam( | ||||||||
5187 | ParmVarDecl *OldParm, int indexAdjustment, Optional<unsigned> NumExpansions, | ||||||||
5188 | bool ExpectParameterPack) { | ||||||||
5189 | TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo(); | ||||||||
5190 | TypeSourceInfo *NewDI = nullptr; | ||||||||
5191 | |||||||||
5192 | if (NumExpansions && isa<PackExpansionType>(OldDI->getType())) { | ||||||||
5193 | // If we're substituting into a pack expansion type and we know the | ||||||||
5194 | // length we want to expand to, just substitute for the pattern. | ||||||||
5195 | TypeLoc OldTL = OldDI->getTypeLoc(); | ||||||||
5196 | PackExpansionTypeLoc OldExpansionTL = OldTL.castAs<PackExpansionTypeLoc>(); | ||||||||
5197 | |||||||||
5198 | TypeLocBuilder TLB; | ||||||||
5199 | TypeLoc NewTL = OldDI->getTypeLoc(); | ||||||||
5200 | TLB.reserve(NewTL.getFullDataSize()); | ||||||||
5201 | |||||||||
5202 | QualType Result = getDerived().TransformType(TLB, | ||||||||
5203 | OldExpansionTL.getPatternLoc()); | ||||||||
5204 | if (Result.isNull()) | ||||||||
5205 | return nullptr; | ||||||||
5206 | |||||||||
5207 | Result = RebuildPackExpansionType(Result, | ||||||||
5208 | OldExpansionTL.getPatternLoc().getSourceRange(), | ||||||||
5209 | OldExpansionTL.getEllipsisLoc(), | ||||||||
5210 | NumExpansions); | ||||||||
5211 | if (Result.isNull()) | ||||||||
5212 | return nullptr; | ||||||||
5213 | |||||||||
5214 | PackExpansionTypeLoc NewExpansionTL | ||||||||
5215 | = TLB.push<PackExpansionTypeLoc>(Result); | ||||||||
5216 | NewExpansionTL.setEllipsisLoc(OldExpansionTL.getEllipsisLoc()); | ||||||||
5217 | NewDI = TLB.getTypeSourceInfo(SemaRef.Context, Result); | ||||||||
5218 | } else | ||||||||
5219 | NewDI = getDerived().TransformType(OldDI); | ||||||||
5220 | if (!NewDI) | ||||||||
5221 | return nullptr; | ||||||||
5222 | |||||||||
5223 | if (NewDI == OldDI && indexAdjustment == 0) | ||||||||
5224 | return OldParm; | ||||||||
5225 | |||||||||
5226 | ParmVarDecl *newParm = ParmVarDecl::Create(SemaRef.Context, | ||||||||
5227 | OldParm->getDeclContext(), | ||||||||
5228 | OldParm->getInnerLocStart(), | ||||||||
5229 | OldParm->getLocation(), | ||||||||
5230 | OldParm->getIdentifier(), | ||||||||
5231 | NewDI->getType(), | ||||||||
5232 | NewDI, | ||||||||
5233 | OldParm->getStorageClass(), | ||||||||
5234 | /* DefArg */ nullptr); | ||||||||
5235 | newParm->setScopeInfo(OldParm->getFunctionScopeDepth(), | ||||||||
5236 | OldParm->getFunctionScopeIndex() + indexAdjustment); | ||||||||
5237 | return newParm; | ||||||||
5238 | } | ||||||||
5239 | |||||||||
5240 | template <typename Derived> | ||||||||
5241 | bool TreeTransform<Derived>::TransformFunctionTypeParams( | ||||||||
5242 | SourceLocation Loc, ArrayRef<ParmVarDecl *> Params, | ||||||||
5243 | const QualType *ParamTypes, | ||||||||
5244 | const FunctionProtoType::ExtParameterInfo *ParamInfos, | ||||||||
5245 | SmallVectorImpl<QualType> &OutParamTypes, | ||||||||
5246 | SmallVectorImpl<ParmVarDecl *> *PVars, | ||||||||
5247 | Sema::ExtParameterInfoBuilder &PInfos) { | ||||||||
5248 | int indexAdjustment = 0; | ||||||||
5249 | |||||||||
5250 | unsigned NumParams = Params.size(); | ||||||||
5251 | for (unsigned i = 0; i != NumParams; ++i) { | ||||||||
5252 | if (ParmVarDecl *OldParm = Params[i]) { | ||||||||
5253 | assert(OldParm->getFunctionScopeIndex() == i)((OldParm->getFunctionScopeIndex() == i) ? static_cast< void> (0) : __assert_fail ("OldParm->getFunctionScopeIndex() == i" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 5253, __PRETTY_FUNCTION__)); | ||||||||
5254 | |||||||||
5255 | Optional<unsigned> NumExpansions; | ||||||||
5256 | ParmVarDecl *NewParm = nullptr; | ||||||||
5257 | if (OldParm->isParameterPack()) { | ||||||||
5258 | // We have a function parameter pack that may need to be expanded. | ||||||||
5259 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | ||||||||
5260 | |||||||||
5261 | // Find the parameter packs that could be expanded. | ||||||||
5262 | TypeLoc TL = OldParm->getTypeSourceInfo()->getTypeLoc(); | ||||||||
5263 | PackExpansionTypeLoc ExpansionTL = TL.castAs<PackExpansionTypeLoc>(); | ||||||||
5264 | TypeLoc Pattern = ExpansionTL.getPatternLoc(); | ||||||||
5265 | SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded); | ||||||||
5266 | |||||||||
5267 | // Determine whether we should expand the parameter packs. | ||||||||
5268 | bool ShouldExpand = false; | ||||||||
5269 | bool RetainExpansion = false; | ||||||||
5270 | Optional<unsigned> OrigNumExpansions; | ||||||||
5271 | if (Unexpanded.size() > 0) { | ||||||||
5272 | OrigNumExpansions = ExpansionTL.getTypePtr()->getNumExpansions(); | ||||||||
5273 | NumExpansions = OrigNumExpansions; | ||||||||
5274 | if (getDerived().TryExpandParameterPacks(ExpansionTL.getEllipsisLoc(), | ||||||||
5275 | Pattern.getSourceRange(), | ||||||||
5276 | Unexpanded, | ||||||||
5277 | ShouldExpand, | ||||||||
5278 | RetainExpansion, | ||||||||
5279 | NumExpansions)) { | ||||||||
5280 | return true; | ||||||||
5281 | } | ||||||||
5282 | } else { | ||||||||
5283 | #ifndef NDEBUG | ||||||||
5284 | const AutoType *AT = | ||||||||
5285 | Pattern.getType().getTypePtr()->getContainedAutoType(); | ||||||||
5286 | assert((AT && (!AT->isDeduced() || AT->getDeducedType().isNull())) &&(((AT && (!AT->isDeduced() || AT->getDeducedType ().isNull())) && "Could not find parameter packs or undeduced auto type!" ) ? static_cast<void> (0) : __assert_fail ("(AT && (!AT->isDeduced() || AT->getDeducedType().isNull())) && \"Could not find parameter packs or undeduced auto type!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 5287, __PRETTY_FUNCTION__)) | ||||||||
5287 | "Could not find parameter packs or undeduced auto type!")(((AT && (!AT->isDeduced() || AT->getDeducedType ().isNull())) && "Could not find parameter packs or undeduced auto type!" ) ? static_cast<void> (0) : __assert_fail ("(AT && (!AT->isDeduced() || AT->getDeducedType().isNull())) && \"Could not find parameter packs or undeduced auto type!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 5287, __PRETTY_FUNCTION__)); | ||||||||
5288 | #endif | ||||||||
5289 | } | ||||||||
5290 | |||||||||
5291 | if (ShouldExpand) { | ||||||||
5292 | // Expand the function parameter pack into multiple, separate | ||||||||
5293 | // parameters. | ||||||||
5294 | getDerived().ExpandingFunctionParameterPack(OldParm); | ||||||||
5295 | for (unsigned I = 0; I != *NumExpansions; ++I) { | ||||||||
5296 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I); | ||||||||
5297 | ParmVarDecl *NewParm | ||||||||
5298 | = getDerived().TransformFunctionTypeParam(OldParm, | ||||||||
5299 | indexAdjustment++, | ||||||||
5300 | OrigNumExpansions, | ||||||||
5301 | /*ExpectParameterPack=*/false); | ||||||||
5302 | if (!NewParm) | ||||||||
5303 | return true; | ||||||||
5304 | |||||||||
5305 | if (ParamInfos) | ||||||||
5306 | PInfos.set(OutParamTypes.size(), ParamInfos[i]); | ||||||||
5307 | OutParamTypes.push_back(NewParm->getType()); | ||||||||
5308 | if (PVars) | ||||||||
5309 | PVars->push_back(NewParm); | ||||||||
5310 | } | ||||||||
5311 | |||||||||
5312 | // If we're supposed to retain a pack expansion, do so by temporarily | ||||||||
5313 | // forgetting the partially-substituted parameter pack. | ||||||||
5314 | if (RetainExpansion) { | ||||||||
5315 | ForgetPartiallySubstitutedPackRAII Forget(getDerived()); | ||||||||
5316 | ParmVarDecl *NewParm | ||||||||
5317 | = getDerived().TransformFunctionTypeParam(OldParm, | ||||||||
5318 | indexAdjustment++, | ||||||||
5319 | OrigNumExpansions, | ||||||||
5320 | /*ExpectParameterPack=*/false); | ||||||||
5321 | if (!NewParm) | ||||||||
5322 | return true; | ||||||||
5323 | |||||||||
5324 | if (ParamInfos) | ||||||||
5325 | PInfos.set(OutParamTypes.size(), ParamInfos[i]); | ||||||||
5326 | OutParamTypes.push_back(NewParm->getType()); | ||||||||
5327 | if (PVars) | ||||||||
5328 | PVars->push_back(NewParm); | ||||||||
5329 | } | ||||||||
5330 | |||||||||
5331 | // The next parameter should have the same adjustment as the | ||||||||
5332 | // last thing we pushed, but we post-incremented indexAdjustment | ||||||||
5333 | // on every push. Also, if we push nothing, the adjustment should | ||||||||
5334 | // go down by one. | ||||||||
5335 | indexAdjustment--; | ||||||||
5336 | |||||||||
5337 | // We're done with the pack expansion. | ||||||||
5338 | continue; | ||||||||
5339 | } | ||||||||
5340 | |||||||||
5341 | // We'll substitute the parameter now without expanding the pack | ||||||||
5342 | // expansion. | ||||||||
5343 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1); | ||||||||
5344 | NewParm = getDerived().TransformFunctionTypeParam(OldParm, | ||||||||
5345 | indexAdjustment, | ||||||||
5346 | NumExpansions, | ||||||||
5347 | /*ExpectParameterPack=*/true); | ||||||||
5348 | assert(NewParm->isParameterPack() &&((NewParm->isParameterPack() && "Parameter pack no longer a parameter pack after " "transformation.") ? static_cast<void> (0) : __assert_fail ("NewParm->isParameterPack() && \"Parameter pack no longer a parameter pack after \" \"transformation.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 5350, __PRETTY_FUNCTION__)) | ||||||||
5349 | "Parameter pack no longer a parameter pack after "((NewParm->isParameterPack() && "Parameter pack no longer a parameter pack after " "transformation.") ? static_cast<void> (0) : __assert_fail ("NewParm->isParameterPack() && \"Parameter pack no longer a parameter pack after \" \"transformation.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 5350, __PRETTY_FUNCTION__)) | ||||||||
5350 | "transformation.")((NewParm->isParameterPack() && "Parameter pack no longer a parameter pack after " "transformation.") ? static_cast<void> (0) : __assert_fail ("NewParm->isParameterPack() && \"Parameter pack no longer a parameter pack after \" \"transformation.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 5350, __PRETTY_FUNCTION__)); | ||||||||
5351 | } else { | ||||||||
5352 | NewParm = getDerived().TransformFunctionTypeParam( | ||||||||
5353 | OldParm, indexAdjustment, None, /*ExpectParameterPack=*/ false); | ||||||||
5354 | } | ||||||||
5355 | |||||||||
5356 | if (!NewParm) | ||||||||
5357 | return true; | ||||||||
5358 | |||||||||
5359 | if (ParamInfos) | ||||||||
5360 | PInfos.set(OutParamTypes.size(), ParamInfos[i]); | ||||||||
5361 | OutParamTypes.push_back(NewParm->getType()); | ||||||||
5362 | if (PVars) | ||||||||
5363 | PVars->push_back(NewParm); | ||||||||
5364 | continue; | ||||||||
5365 | } | ||||||||
5366 | |||||||||
5367 | // Deal with the possibility that we don't have a parameter | ||||||||
5368 | // declaration for this parameter. | ||||||||
5369 | QualType OldType = ParamTypes[i]; | ||||||||
5370 | bool IsPackExpansion = false; | ||||||||
5371 | Optional<unsigned> NumExpansions; | ||||||||
5372 | QualType NewType; | ||||||||
5373 | if (const PackExpansionType *Expansion | ||||||||
5374 | = dyn_cast<PackExpansionType>(OldType)) { | ||||||||
5375 | // We have a function parameter pack that may need to be expanded. | ||||||||
5376 | QualType Pattern = Expansion->getPattern(); | ||||||||
5377 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | ||||||||
5378 | getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded); | ||||||||
5379 | |||||||||
5380 | // Determine whether we should expand the parameter packs. | ||||||||
5381 | bool ShouldExpand = false; | ||||||||
5382 | bool RetainExpansion = false; | ||||||||
5383 | if (getDerived().TryExpandParameterPacks(Loc, SourceRange(), | ||||||||
5384 | Unexpanded, | ||||||||
5385 | ShouldExpand, | ||||||||
5386 | RetainExpansion, | ||||||||
5387 | NumExpansions)) { | ||||||||
5388 | return true; | ||||||||
5389 | } | ||||||||
5390 | |||||||||
5391 | if (ShouldExpand) { | ||||||||
5392 | // Expand the function parameter pack into multiple, separate | ||||||||
5393 | // parameters. | ||||||||
5394 | for (unsigned I = 0; I != *NumExpansions; ++I) { | ||||||||
5395 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I); | ||||||||
5396 | QualType NewType = getDerived().TransformType(Pattern); | ||||||||
5397 | if (NewType.isNull()) | ||||||||
5398 | return true; | ||||||||
5399 | |||||||||
5400 | if (NewType->containsUnexpandedParameterPack()) { | ||||||||
5401 | NewType = | ||||||||
5402 | getSema().getASTContext().getPackExpansionType(NewType, None); | ||||||||
5403 | |||||||||
5404 | if (NewType.isNull()) | ||||||||
5405 | return true; | ||||||||
5406 | } | ||||||||
5407 | |||||||||
5408 | if (ParamInfos) | ||||||||
5409 | PInfos.set(OutParamTypes.size(), ParamInfos[i]); | ||||||||
5410 | OutParamTypes.push_back(NewType); | ||||||||
5411 | if (PVars) | ||||||||
5412 | PVars->push_back(nullptr); | ||||||||
5413 | } | ||||||||
5414 | |||||||||
5415 | // We're done with the pack expansion. | ||||||||
5416 | continue; | ||||||||
5417 | } | ||||||||
5418 | |||||||||
5419 | // If we're supposed to retain a pack expansion, do so by temporarily | ||||||||
5420 | // forgetting the partially-substituted parameter pack. | ||||||||
5421 | if (RetainExpansion) { | ||||||||
5422 | ForgetPartiallySubstitutedPackRAII Forget(getDerived()); | ||||||||
5423 | QualType NewType = getDerived().TransformType(Pattern); | ||||||||
5424 | if (NewType.isNull()) | ||||||||
5425 | return true; | ||||||||
5426 | |||||||||
5427 | if (ParamInfos) | ||||||||
5428 | PInfos.set(OutParamTypes.size(), ParamInfos[i]); | ||||||||
5429 | OutParamTypes.push_back(NewType); | ||||||||
5430 | if (PVars) | ||||||||
5431 | PVars->push_back(nullptr); | ||||||||
5432 | } | ||||||||
5433 | |||||||||
5434 | // We'll substitute the parameter now without expanding the pack | ||||||||
5435 | // expansion. | ||||||||
5436 | OldType = Expansion->getPattern(); | ||||||||
5437 | IsPackExpansion = true; | ||||||||
5438 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1); | ||||||||
5439 | NewType = getDerived().TransformType(OldType); | ||||||||
5440 | } else { | ||||||||
5441 | NewType = getDerived().TransformType(OldType); | ||||||||
5442 | } | ||||||||
5443 | |||||||||
5444 | if (NewType.isNull()) | ||||||||
5445 | return true; | ||||||||
5446 | |||||||||
5447 | if (IsPackExpansion) | ||||||||
5448 | NewType = getSema().Context.getPackExpansionType(NewType, | ||||||||
5449 | NumExpansions); | ||||||||
5450 | |||||||||
5451 | if (ParamInfos) | ||||||||
5452 | PInfos.set(OutParamTypes.size(), ParamInfos[i]); | ||||||||
5453 | OutParamTypes.push_back(NewType); | ||||||||
5454 | if (PVars) | ||||||||
5455 | PVars->push_back(nullptr); | ||||||||
5456 | } | ||||||||
5457 | |||||||||
5458 | #ifndef NDEBUG | ||||||||
5459 | if (PVars) { | ||||||||
5460 | for (unsigned i = 0, e = PVars->size(); i != e; ++i) | ||||||||
5461 | if (ParmVarDecl *parm = (*PVars)[i]) | ||||||||
5462 | assert(parm->getFunctionScopeIndex() == i)((parm->getFunctionScopeIndex() == i) ? static_cast<void > (0) : __assert_fail ("parm->getFunctionScopeIndex() == i" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 5462, __PRETTY_FUNCTION__)); | ||||||||
5463 | } | ||||||||
5464 | #endif | ||||||||
5465 | |||||||||
5466 | return false; | ||||||||
5467 | } | ||||||||
5468 | |||||||||
5469 | template<typename Derived> | ||||||||
5470 | QualType | ||||||||
5471 | TreeTransform<Derived>::TransformFunctionProtoType(TypeLocBuilder &TLB, | ||||||||
5472 | FunctionProtoTypeLoc TL) { | ||||||||
5473 | SmallVector<QualType, 4> ExceptionStorage; | ||||||||
5474 | TreeTransform *This = this; // Work around gcc.gnu.org/PR56135. | ||||||||
5475 | return getDerived().TransformFunctionProtoType( | ||||||||
5476 | TLB, TL, nullptr, Qualifiers(), | ||||||||
5477 | [&](FunctionProtoType::ExceptionSpecInfo &ESI, bool &Changed) { | ||||||||
5478 | return This->TransformExceptionSpec(TL.getBeginLoc(), ESI, | ||||||||
5479 | ExceptionStorage, Changed); | ||||||||
5480 | }); | ||||||||
5481 | } | ||||||||
5482 | |||||||||
5483 | template<typename Derived> template<typename Fn> | ||||||||
5484 | QualType TreeTransform<Derived>::TransformFunctionProtoType( | ||||||||
5485 | TypeLocBuilder &TLB, FunctionProtoTypeLoc TL, CXXRecordDecl *ThisContext, | ||||||||
5486 | Qualifiers ThisTypeQuals, Fn TransformExceptionSpec) { | ||||||||
5487 | |||||||||
5488 | // Transform the parameters and return type. | ||||||||
5489 | // | ||||||||
5490 | // We are required to instantiate the params and return type in source order. | ||||||||
5491 | // When the function has a trailing return type, we instantiate the | ||||||||
5492 | // parameters before the return type, since the return type can then refer | ||||||||
5493 | // to the parameters themselves (via decltype, sizeof, etc.). | ||||||||
5494 | // | ||||||||
5495 | SmallVector<QualType, 4> ParamTypes; | ||||||||
5496 | SmallVector<ParmVarDecl*, 4> ParamDecls; | ||||||||
5497 | Sema::ExtParameterInfoBuilder ExtParamInfos; | ||||||||
5498 | const FunctionProtoType *T = TL.getTypePtr(); | ||||||||
5499 | |||||||||
5500 | QualType ResultType; | ||||||||
5501 | |||||||||
5502 | if (T->hasTrailingReturn()) { | ||||||||
5503 | if (getDerived().TransformFunctionTypeParams( | ||||||||
5504 | TL.getBeginLoc(), TL.getParams(), | ||||||||
5505 | TL.getTypePtr()->param_type_begin(), | ||||||||
5506 | T->getExtParameterInfosOrNull(), | ||||||||
5507 | ParamTypes, &ParamDecls, ExtParamInfos)) | ||||||||
5508 | return QualType(); | ||||||||
5509 | |||||||||
5510 | { | ||||||||
5511 | // C++11 [expr.prim.general]p3: | ||||||||
5512 | // If a declaration declares a member function or member function | ||||||||
5513 | // template of a class X, the expression this is a prvalue of type | ||||||||
5514 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq | ||||||||
5515 | // and the end of the function-definition, member-declarator, or | ||||||||
5516 | // declarator. | ||||||||
5517 | Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals); | ||||||||
5518 | |||||||||
5519 | ResultType = getDerived().TransformType(TLB, TL.getReturnLoc()); | ||||||||
5520 | if (ResultType.isNull()) | ||||||||
5521 | return QualType(); | ||||||||
5522 | } | ||||||||
5523 | } | ||||||||
5524 | else { | ||||||||
5525 | ResultType = getDerived().TransformType(TLB, TL.getReturnLoc()); | ||||||||
5526 | if (ResultType.isNull()) | ||||||||
5527 | return QualType(); | ||||||||
5528 | |||||||||
5529 | if (getDerived().TransformFunctionTypeParams( | ||||||||
5530 | TL.getBeginLoc(), TL.getParams(), | ||||||||
5531 | TL.getTypePtr()->param_type_begin(), | ||||||||
5532 | T->getExtParameterInfosOrNull(), | ||||||||
5533 | ParamTypes, &ParamDecls, ExtParamInfos)) | ||||||||
5534 | return QualType(); | ||||||||
5535 | } | ||||||||
5536 | |||||||||
5537 | FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo(); | ||||||||
5538 | |||||||||
5539 | bool EPIChanged = false; | ||||||||
5540 | if (TransformExceptionSpec(EPI.ExceptionSpec, EPIChanged)) | ||||||||
5541 | return QualType(); | ||||||||
5542 | |||||||||
5543 | // Handle extended parameter information. | ||||||||
5544 | if (auto NewExtParamInfos = | ||||||||
5545 | ExtParamInfos.getPointerOrNull(ParamTypes.size())) { | ||||||||
5546 | if (!EPI.ExtParameterInfos || | ||||||||
5547 | llvm::makeArrayRef(EPI.ExtParameterInfos, TL.getNumParams()) | ||||||||
5548 | != llvm::makeArrayRef(NewExtParamInfos, ParamTypes.size())) { | ||||||||
5549 | EPIChanged = true; | ||||||||
5550 | } | ||||||||
5551 | EPI.ExtParameterInfos = NewExtParamInfos; | ||||||||
5552 | } else if (EPI.ExtParameterInfos) { | ||||||||
5553 | EPIChanged = true; | ||||||||
5554 | EPI.ExtParameterInfos = nullptr; | ||||||||
5555 | } | ||||||||
5556 | |||||||||
5557 | QualType Result = TL.getType(); | ||||||||
5558 | if (getDerived().AlwaysRebuild() || ResultType != T->getReturnType() || | ||||||||
5559 | T->getParamTypes() != llvm::makeArrayRef(ParamTypes) || EPIChanged) { | ||||||||
5560 | Result = getDerived().RebuildFunctionProtoType(ResultType, ParamTypes, EPI); | ||||||||
5561 | if (Result.isNull()) | ||||||||
5562 | return QualType(); | ||||||||
5563 | } | ||||||||
5564 | |||||||||
5565 | FunctionProtoTypeLoc NewTL = TLB.push<FunctionProtoTypeLoc>(Result); | ||||||||
5566 | NewTL.setLocalRangeBegin(TL.getLocalRangeBegin()); | ||||||||
5567 | NewTL.setLParenLoc(TL.getLParenLoc()); | ||||||||
5568 | NewTL.setRParenLoc(TL.getRParenLoc()); | ||||||||
5569 | NewTL.setExceptionSpecRange(TL.getExceptionSpecRange()); | ||||||||
5570 | NewTL.setLocalRangeEnd(TL.getLocalRangeEnd()); | ||||||||
5571 | for (unsigned i = 0, e = NewTL.getNumParams(); i != e; ++i) | ||||||||
5572 | NewTL.setParam(i, ParamDecls[i]); | ||||||||
5573 | |||||||||
5574 | return Result; | ||||||||
5575 | } | ||||||||
5576 | |||||||||
5577 | template<typename Derived> | ||||||||
5578 | bool TreeTransform<Derived>::TransformExceptionSpec( | ||||||||
5579 | SourceLocation Loc, FunctionProtoType::ExceptionSpecInfo &ESI, | ||||||||
5580 | SmallVectorImpl<QualType> &Exceptions, bool &Changed) { | ||||||||
5581 | assert(ESI.Type != EST_Uninstantiated && ESI.Type != EST_Unevaluated)((ESI.Type != EST_Uninstantiated && ESI.Type != EST_Unevaluated ) ? static_cast<void> (0) : __assert_fail ("ESI.Type != EST_Uninstantiated && ESI.Type != EST_Unevaluated" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 5581, __PRETTY_FUNCTION__)); | ||||||||
5582 | |||||||||
5583 | // Instantiate a dynamic noexcept expression, if any. | ||||||||
5584 | if (isComputedNoexcept(ESI.Type)) { | ||||||||
5585 | EnterExpressionEvaluationContext Unevaluated( | ||||||||
5586 | getSema(), Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||
5587 | ExprResult NoexceptExpr = getDerived().TransformExpr(ESI.NoexceptExpr); | ||||||||
5588 | if (NoexceptExpr.isInvalid()) | ||||||||
5589 | return true; | ||||||||
5590 | |||||||||
5591 | ExceptionSpecificationType EST = ESI.Type; | ||||||||
5592 | NoexceptExpr = | ||||||||
5593 | getSema().ActOnNoexceptSpec(Loc, NoexceptExpr.get(), EST); | ||||||||
5594 | if (NoexceptExpr.isInvalid()) | ||||||||
5595 | return true; | ||||||||
5596 | |||||||||
5597 | if (ESI.NoexceptExpr != NoexceptExpr.get() || EST != ESI.Type) | ||||||||
5598 | Changed = true; | ||||||||
5599 | ESI.NoexceptExpr = NoexceptExpr.get(); | ||||||||
5600 | ESI.Type = EST; | ||||||||
5601 | } | ||||||||
5602 | |||||||||
5603 | if (ESI.Type != EST_Dynamic) | ||||||||
5604 | return false; | ||||||||
5605 | |||||||||
5606 | // Instantiate a dynamic exception specification's type. | ||||||||
5607 | for (QualType T : ESI.Exceptions) { | ||||||||
5608 | if (const PackExpansionType *PackExpansion = | ||||||||
5609 | T->getAs<PackExpansionType>()) { | ||||||||
5610 | Changed = true; | ||||||||
5611 | |||||||||
5612 | // We have a pack expansion. Instantiate it. | ||||||||
5613 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | ||||||||
5614 | SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(), | ||||||||
5615 | Unexpanded); | ||||||||
5616 | assert(!Unexpanded.empty() && "Pack expansion without parameter packs?")((!Unexpanded.empty() && "Pack expansion without parameter packs?" ) ? static_cast<void> (0) : __assert_fail ("!Unexpanded.empty() && \"Pack expansion without parameter packs?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 5616, __PRETTY_FUNCTION__)); | ||||||||
5617 | |||||||||
5618 | // Determine whether the set of unexpanded parameter packs can and | ||||||||
5619 | // should | ||||||||
5620 | // be expanded. | ||||||||
5621 | bool Expand = false; | ||||||||
5622 | bool RetainExpansion = false; | ||||||||
5623 | Optional<unsigned> NumExpansions = PackExpansion->getNumExpansions(); | ||||||||
5624 | // FIXME: Track the location of the ellipsis (and track source location | ||||||||
5625 | // information for the types in the exception specification in general). | ||||||||
5626 | if (getDerived().TryExpandParameterPacks( | ||||||||
5627 | Loc, SourceRange(), Unexpanded, Expand, | ||||||||
5628 | RetainExpansion, NumExpansions)) | ||||||||
5629 | return true; | ||||||||
5630 | |||||||||
5631 | if (!Expand) { | ||||||||
5632 | // We can't expand this pack expansion into separate arguments yet; | ||||||||
5633 | // just substitute into the pattern and create a new pack expansion | ||||||||
5634 | // type. | ||||||||
5635 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1); | ||||||||
5636 | QualType U = getDerived().TransformType(PackExpansion->getPattern()); | ||||||||
5637 | if (U.isNull()) | ||||||||
5638 | return true; | ||||||||
5639 | |||||||||
5640 | U = SemaRef.Context.getPackExpansionType(U, NumExpansions); | ||||||||
5641 | Exceptions.push_back(U); | ||||||||
5642 | continue; | ||||||||
5643 | } | ||||||||
5644 | |||||||||
5645 | // Substitute into the pack expansion pattern for each slice of the | ||||||||
5646 | // pack. | ||||||||
5647 | for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) { | ||||||||
5648 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), ArgIdx); | ||||||||
5649 | |||||||||
5650 | QualType U = getDerived().TransformType(PackExpansion->getPattern()); | ||||||||
5651 | if (U.isNull() || SemaRef.CheckSpecifiedExceptionType(U, Loc)) | ||||||||
5652 | return true; | ||||||||
5653 | |||||||||
5654 | Exceptions.push_back(U); | ||||||||
5655 | } | ||||||||
5656 | } else { | ||||||||
5657 | QualType U = getDerived().TransformType(T); | ||||||||
5658 | if (U.isNull() || SemaRef.CheckSpecifiedExceptionType(U, Loc)) | ||||||||
5659 | return true; | ||||||||
5660 | if (T != U) | ||||||||
5661 | Changed = true; | ||||||||
5662 | |||||||||
5663 | Exceptions.push_back(U); | ||||||||
5664 | } | ||||||||
5665 | } | ||||||||
5666 | |||||||||
5667 | ESI.Exceptions = Exceptions; | ||||||||
5668 | if (ESI.Exceptions.empty()) | ||||||||
5669 | ESI.Type = EST_DynamicNone; | ||||||||
5670 | return false; | ||||||||
5671 | } | ||||||||
5672 | |||||||||
5673 | template<typename Derived> | ||||||||
5674 | QualType TreeTransform<Derived>::TransformFunctionNoProtoType( | ||||||||
5675 | TypeLocBuilder &TLB, | ||||||||
5676 | FunctionNoProtoTypeLoc TL) { | ||||||||
5677 | const FunctionNoProtoType *T = TL.getTypePtr(); | ||||||||
5678 | QualType ResultType = getDerived().TransformType(TLB, TL.getReturnLoc()); | ||||||||
5679 | if (ResultType.isNull()) | ||||||||
5680 | return QualType(); | ||||||||
5681 | |||||||||
5682 | QualType Result = TL.getType(); | ||||||||
5683 | if (getDerived().AlwaysRebuild() || ResultType != T->getReturnType()) | ||||||||
5684 | Result = getDerived().RebuildFunctionNoProtoType(ResultType); | ||||||||
5685 | |||||||||
5686 | FunctionNoProtoTypeLoc NewTL = TLB.push<FunctionNoProtoTypeLoc>(Result); | ||||||||
5687 | NewTL.setLocalRangeBegin(TL.getLocalRangeBegin()); | ||||||||
5688 | NewTL.setLParenLoc(TL.getLParenLoc()); | ||||||||
5689 | NewTL.setRParenLoc(TL.getRParenLoc()); | ||||||||
5690 | NewTL.setLocalRangeEnd(TL.getLocalRangeEnd()); | ||||||||
5691 | |||||||||
5692 | return Result; | ||||||||
5693 | } | ||||||||
5694 | |||||||||
5695 | template<typename Derived> QualType | ||||||||
5696 | TreeTransform<Derived>::TransformUnresolvedUsingType(TypeLocBuilder &TLB, | ||||||||
5697 | UnresolvedUsingTypeLoc TL) { | ||||||||
5698 | const UnresolvedUsingType *T = TL.getTypePtr(); | ||||||||
5699 | Decl *D = getDerived().TransformDecl(TL.getNameLoc(), T->getDecl()); | ||||||||
5700 | if (!D) | ||||||||
5701 | return QualType(); | ||||||||
5702 | |||||||||
5703 | QualType Result = TL.getType(); | ||||||||
5704 | if (getDerived().AlwaysRebuild() || D != T->getDecl()) { | ||||||||
5705 | Result = getDerived().RebuildUnresolvedUsingType(TL.getNameLoc(), D); | ||||||||
5706 | if (Result.isNull()) | ||||||||
5707 | return QualType(); | ||||||||
5708 | } | ||||||||
5709 | |||||||||
5710 | // We might get an arbitrary type spec type back. We should at | ||||||||
5711 | // least always get a type spec type, though. | ||||||||
5712 | TypeSpecTypeLoc NewTL = TLB.pushTypeSpec(Result); | ||||||||
5713 | NewTL.setNameLoc(TL.getNameLoc()); | ||||||||
5714 | |||||||||
5715 | return Result; | ||||||||
5716 | } | ||||||||
5717 | |||||||||
5718 | template<typename Derived> | ||||||||
5719 | QualType TreeTransform<Derived>::TransformTypedefType(TypeLocBuilder &TLB, | ||||||||
5720 | TypedefTypeLoc TL) { | ||||||||
5721 | const TypedefType *T = TL.getTypePtr(); | ||||||||
5722 | TypedefNameDecl *Typedef | ||||||||
5723 | = cast_or_null<TypedefNameDecl>(getDerived().TransformDecl(TL.getNameLoc(), | ||||||||
5724 | T->getDecl())); | ||||||||
5725 | if (!Typedef) | ||||||||
5726 | return QualType(); | ||||||||
5727 | |||||||||
5728 | QualType Result = TL.getType(); | ||||||||
5729 | if (getDerived().AlwaysRebuild() || | ||||||||
5730 | Typedef != T->getDecl()) { | ||||||||
5731 | Result = getDerived().RebuildTypedefType(Typedef); | ||||||||
5732 | if (Result.isNull()) | ||||||||
5733 | return QualType(); | ||||||||
5734 | } | ||||||||
5735 | |||||||||
5736 | TypedefTypeLoc NewTL = TLB.push<TypedefTypeLoc>(Result); | ||||||||
5737 | NewTL.setNameLoc(TL.getNameLoc()); | ||||||||
5738 | |||||||||
5739 | return Result; | ||||||||
5740 | } | ||||||||
5741 | |||||||||
5742 | template<typename Derived> | ||||||||
5743 | QualType TreeTransform<Derived>::TransformTypeOfExprType(TypeLocBuilder &TLB, | ||||||||
5744 | TypeOfExprTypeLoc TL) { | ||||||||
5745 | // typeof expressions are not potentially evaluated contexts | ||||||||
5746 | EnterExpressionEvaluationContext Unevaluated( | ||||||||
5747 | SemaRef, Sema::ExpressionEvaluationContext::Unevaluated, | ||||||||
5748 | Sema::ReuseLambdaContextDecl); | ||||||||
5749 | |||||||||
5750 | ExprResult E = getDerived().TransformExpr(TL.getUnderlyingExpr()); | ||||||||
5751 | if (E.isInvalid()) | ||||||||
5752 | return QualType(); | ||||||||
5753 | |||||||||
5754 | E = SemaRef.HandleExprEvaluationContextForTypeof(E.get()); | ||||||||
5755 | if (E.isInvalid()) | ||||||||
5756 | return QualType(); | ||||||||
5757 | |||||||||
5758 | QualType Result = TL.getType(); | ||||||||
5759 | if (getDerived().AlwaysRebuild() || | ||||||||
5760 | E.get() != TL.getUnderlyingExpr()) { | ||||||||
5761 | Result = getDerived().RebuildTypeOfExprType(E.get(), TL.getTypeofLoc()); | ||||||||
5762 | if (Result.isNull()) | ||||||||
5763 | return QualType(); | ||||||||
5764 | } | ||||||||
5765 | else E.get(); | ||||||||
5766 | |||||||||
5767 | TypeOfExprTypeLoc NewTL = TLB.push<TypeOfExprTypeLoc>(Result); | ||||||||
5768 | NewTL.setTypeofLoc(TL.getTypeofLoc()); | ||||||||
5769 | NewTL.setLParenLoc(TL.getLParenLoc()); | ||||||||
5770 | NewTL.setRParenLoc(TL.getRParenLoc()); | ||||||||
5771 | |||||||||
5772 | return Result; | ||||||||
5773 | } | ||||||||
5774 | |||||||||
5775 | template<typename Derived> | ||||||||
5776 | QualType TreeTransform<Derived>::TransformTypeOfType(TypeLocBuilder &TLB, | ||||||||
5777 | TypeOfTypeLoc TL) { | ||||||||
5778 | TypeSourceInfo* Old_Under_TI = TL.getUnderlyingTInfo(); | ||||||||
5779 | TypeSourceInfo* New_Under_TI = getDerived().TransformType(Old_Under_TI); | ||||||||
5780 | if (!New_Under_TI) | ||||||||
5781 | return QualType(); | ||||||||
5782 | |||||||||
5783 | QualType Result = TL.getType(); | ||||||||
5784 | if (getDerived().AlwaysRebuild() || New_Under_TI != Old_Under_TI) { | ||||||||
5785 | Result = getDerived().RebuildTypeOfType(New_Under_TI->getType()); | ||||||||
5786 | if (Result.isNull()) | ||||||||
5787 | return QualType(); | ||||||||
5788 | } | ||||||||
5789 | |||||||||
5790 | TypeOfTypeLoc NewTL = TLB.push<TypeOfTypeLoc>(Result); | ||||||||
5791 | NewTL.setTypeofLoc(TL.getTypeofLoc()); | ||||||||
5792 | NewTL.setLParenLoc(TL.getLParenLoc()); | ||||||||
5793 | NewTL.setRParenLoc(TL.getRParenLoc()); | ||||||||
5794 | NewTL.setUnderlyingTInfo(New_Under_TI); | ||||||||
5795 | |||||||||
5796 | return Result; | ||||||||
5797 | } | ||||||||
5798 | |||||||||
5799 | template<typename Derived> | ||||||||
5800 | QualType TreeTransform<Derived>::TransformDecltypeType(TypeLocBuilder &TLB, | ||||||||
5801 | DecltypeTypeLoc TL) { | ||||||||
5802 | const DecltypeType *T = TL.getTypePtr(); | ||||||||
5803 | |||||||||
5804 | // decltype expressions are not potentially evaluated contexts | ||||||||
5805 | EnterExpressionEvaluationContext Unevaluated( | ||||||||
5806 | SemaRef, Sema::ExpressionEvaluationContext::Unevaluated, nullptr, | ||||||||
5807 | Sema::ExpressionEvaluationContextRecord::EK_Decltype); | ||||||||
5808 | |||||||||
5809 | ExprResult E = getDerived().TransformExpr(T->getUnderlyingExpr()); | ||||||||
5810 | if (E.isInvalid()) | ||||||||
5811 | return QualType(); | ||||||||
5812 | |||||||||
5813 | E = getSema().ActOnDecltypeExpression(E.get()); | ||||||||
5814 | if (E.isInvalid()) | ||||||||
5815 | return QualType(); | ||||||||
5816 | |||||||||
5817 | QualType Result = TL.getType(); | ||||||||
5818 | if (getDerived().AlwaysRebuild() || | ||||||||
5819 | E.get() != T->getUnderlyingExpr()) { | ||||||||
5820 | Result = getDerived().RebuildDecltypeType(E.get(), TL.getNameLoc()); | ||||||||
5821 | if (Result.isNull()) | ||||||||
5822 | return QualType(); | ||||||||
5823 | } | ||||||||
5824 | else E.get(); | ||||||||
5825 | |||||||||
5826 | DecltypeTypeLoc NewTL = TLB.push<DecltypeTypeLoc>(Result); | ||||||||
5827 | NewTL.setNameLoc(TL.getNameLoc()); | ||||||||
5828 | |||||||||
5829 | return Result; | ||||||||
5830 | } | ||||||||
5831 | |||||||||
5832 | template<typename Derived> | ||||||||
5833 | QualType TreeTransform<Derived>::TransformUnaryTransformType( | ||||||||
5834 | TypeLocBuilder &TLB, | ||||||||
5835 | UnaryTransformTypeLoc TL) { | ||||||||
5836 | QualType Result = TL.getType(); | ||||||||
5837 | if (Result->isDependentType()) { | ||||||||
5838 | const UnaryTransformType *T = TL.getTypePtr(); | ||||||||
5839 | QualType NewBase = | ||||||||
5840 | getDerived().TransformType(TL.getUnderlyingTInfo())->getType(); | ||||||||
5841 | Result = getDerived().RebuildUnaryTransformType(NewBase, | ||||||||
5842 | T->getUTTKind(), | ||||||||
5843 | TL.getKWLoc()); | ||||||||
5844 | if (Result.isNull()) | ||||||||
5845 | return QualType(); | ||||||||
5846 | } | ||||||||
5847 | |||||||||
5848 | UnaryTransformTypeLoc NewTL = TLB.push<UnaryTransformTypeLoc>(Result); | ||||||||
5849 | NewTL.setKWLoc(TL.getKWLoc()); | ||||||||
5850 | NewTL.setParensRange(TL.getParensRange()); | ||||||||
5851 | NewTL.setUnderlyingTInfo(TL.getUnderlyingTInfo()); | ||||||||
5852 | return Result; | ||||||||
5853 | } | ||||||||
5854 | |||||||||
5855 | template<typename Derived> | ||||||||
5856 | QualType TreeTransform<Derived>::TransformDeducedTemplateSpecializationType( | ||||||||
5857 | TypeLocBuilder &TLB, DeducedTemplateSpecializationTypeLoc TL) { | ||||||||
5858 | const DeducedTemplateSpecializationType *T = TL.getTypePtr(); | ||||||||
5859 | |||||||||
5860 | CXXScopeSpec SS; | ||||||||
5861 | TemplateName TemplateName = getDerived().TransformTemplateName( | ||||||||
5862 | SS, T->getTemplateName(), TL.getTemplateNameLoc()); | ||||||||
5863 | if (TemplateName.isNull()) | ||||||||
5864 | return QualType(); | ||||||||
5865 | |||||||||
5866 | QualType OldDeduced = T->getDeducedType(); | ||||||||
5867 | QualType NewDeduced; | ||||||||
5868 | if (!OldDeduced.isNull()) { | ||||||||
5869 | NewDeduced = getDerived().TransformType(OldDeduced); | ||||||||
5870 | if (NewDeduced.isNull()) | ||||||||
5871 | return QualType(); | ||||||||
5872 | } | ||||||||
5873 | |||||||||
5874 | QualType Result = getDerived().RebuildDeducedTemplateSpecializationType( | ||||||||
5875 | TemplateName, NewDeduced); | ||||||||
5876 | if (Result.isNull()) | ||||||||
5877 | return QualType(); | ||||||||
5878 | |||||||||
5879 | DeducedTemplateSpecializationTypeLoc NewTL = | ||||||||
5880 | TLB.push<DeducedTemplateSpecializationTypeLoc>(Result); | ||||||||
5881 | NewTL.setTemplateNameLoc(TL.getTemplateNameLoc()); | ||||||||
5882 | |||||||||
5883 | return Result; | ||||||||
5884 | } | ||||||||
5885 | |||||||||
5886 | template<typename Derived> | ||||||||
5887 | QualType TreeTransform<Derived>::TransformRecordType(TypeLocBuilder &TLB, | ||||||||
5888 | RecordTypeLoc TL) { | ||||||||
5889 | const RecordType *T = TL.getTypePtr(); | ||||||||
5890 | RecordDecl *Record | ||||||||
5891 | = cast_or_null<RecordDecl>(getDerived().TransformDecl(TL.getNameLoc(), | ||||||||
5892 | T->getDecl())); | ||||||||
5893 | if (!Record) | ||||||||
5894 | return QualType(); | ||||||||
5895 | |||||||||
5896 | QualType Result = TL.getType(); | ||||||||
5897 | if (getDerived().AlwaysRebuild() || | ||||||||
5898 | Record != T->getDecl()) { | ||||||||
5899 | Result = getDerived().RebuildRecordType(Record); | ||||||||
5900 | if (Result.isNull()) | ||||||||
5901 | return QualType(); | ||||||||
5902 | } | ||||||||
5903 | |||||||||
5904 | RecordTypeLoc NewTL = TLB.push<RecordTypeLoc>(Result); | ||||||||
5905 | NewTL.setNameLoc(TL.getNameLoc()); | ||||||||
5906 | |||||||||
5907 | return Result; | ||||||||
5908 | } | ||||||||
5909 | |||||||||
5910 | template<typename Derived> | ||||||||
5911 | QualType TreeTransform<Derived>::TransformEnumType(TypeLocBuilder &TLB, | ||||||||
5912 | EnumTypeLoc TL) { | ||||||||
5913 | const EnumType *T = TL.getTypePtr(); | ||||||||
5914 | EnumDecl *Enum | ||||||||
5915 | = cast_or_null<EnumDecl>(getDerived().TransformDecl(TL.getNameLoc(), | ||||||||
5916 | T->getDecl())); | ||||||||
5917 | if (!Enum) | ||||||||
5918 | return QualType(); | ||||||||
5919 | |||||||||
5920 | QualType Result = TL.getType(); | ||||||||
5921 | if (getDerived().AlwaysRebuild() || | ||||||||
5922 | Enum != T->getDecl()) { | ||||||||
5923 | Result = getDerived().RebuildEnumType(Enum); | ||||||||
5924 | if (Result.isNull()) | ||||||||
5925 | return QualType(); | ||||||||
5926 | } | ||||||||
5927 | |||||||||
5928 | EnumTypeLoc NewTL = TLB.push<EnumTypeLoc>(Result); | ||||||||
5929 | NewTL.setNameLoc(TL.getNameLoc()); | ||||||||
5930 | |||||||||
5931 | return Result; | ||||||||
5932 | } | ||||||||
5933 | |||||||||
5934 | template<typename Derived> | ||||||||
5935 | QualType TreeTransform<Derived>::TransformInjectedClassNameType( | ||||||||
5936 | TypeLocBuilder &TLB, | ||||||||
5937 | InjectedClassNameTypeLoc TL) { | ||||||||
5938 | Decl *D = getDerived().TransformDecl(TL.getNameLoc(), | ||||||||
5939 | TL.getTypePtr()->getDecl()); | ||||||||
5940 | if (!D) return QualType(); | ||||||||
5941 | |||||||||
5942 | QualType T = SemaRef.Context.getTypeDeclType(cast<TypeDecl>(D)); | ||||||||
5943 | TLB.pushTypeSpec(T).setNameLoc(TL.getNameLoc()); | ||||||||
5944 | return T; | ||||||||
5945 | } | ||||||||
5946 | |||||||||
5947 | template<typename Derived> | ||||||||
5948 | QualType TreeTransform<Derived>::TransformTemplateTypeParmType( | ||||||||
5949 | TypeLocBuilder &TLB, | ||||||||
5950 | TemplateTypeParmTypeLoc TL) { | ||||||||
5951 | return TransformTypeSpecType(TLB, TL); | ||||||||
5952 | } | ||||||||
5953 | |||||||||
5954 | template<typename Derived> | ||||||||
5955 | QualType TreeTransform<Derived>::TransformSubstTemplateTypeParmType( | ||||||||
5956 | TypeLocBuilder &TLB, | ||||||||
5957 | SubstTemplateTypeParmTypeLoc TL) { | ||||||||
5958 | const SubstTemplateTypeParmType *T = TL.getTypePtr(); | ||||||||
5959 | |||||||||
5960 | // Substitute into the replacement type, which itself might involve something | ||||||||
5961 | // that needs to be transformed. This only tends to occur with default | ||||||||
5962 | // template arguments of template template parameters. | ||||||||
5963 | TemporaryBase Rebase(*this, TL.getNameLoc(), DeclarationName()); | ||||||||
5964 | QualType Replacement = getDerived().TransformType(T->getReplacementType()); | ||||||||
5965 | if (Replacement.isNull()) | ||||||||
5966 | return QualType(); | ||||||||
5967 | |||||||||
5968 | // Always canonicalize the replacement type. | ||||||||
5969 | Replacement = SemaRef.Context.getCanonicalType(Replacement); | ||||||||
5970 | QualType Result | ||||||||
5971 | = SemaRef.Context.getSubstTemplateTypeParmType(T->getReplacedParameter(), | ||||||||
5972 | Replacement); | ||||||||
5973 | |||||||||
5974 | // Propagate type-source information. | ||||||||
5975 | SubstTemplateTypeParmTypeLoc NewTL | ||||||||
5976 | = TLB.push<SubstTemplateTypeParmTypeLoc>(Result); | ||||||||
5977 | NewTL.setNameLoc(TL.getNameLoc()); | ||||||||
5978 | return Result; | ||||||||
5979 | |||||||||
5980 | } | ||||||||
5981 | |||||||||
5982 | template<typename Derived> | ||||||||
5983 | QualType TreeTransform<Derived>::TransformSubstTemplateTypeParmPackType( | ||||||||
5984 | TypeLocBuilder &TLB, | ||||||||
5985 | SubstTemplateTypeParmPackTypeLoc TL) { | ||||||||
5986 | return TransformTypeSpecType(TLB, TL); | ||||||||
5987 | } | ||||||||
5988 | |||||||||
5989 | template<typename Derived> | ||||||||
5990 | QualType TreeTransform<Derived>::TransformTemplateSpecializationType( | ||||||||
5991 | TypeLocBuilder &TLB, | ||||||||
5992 | TemplateSpecializationTypeLoc TL) { | ||||||||
5993 | const TemplateSpecializationType *T = TL.getTypePtr(); | ||||||||
5994 | |||||||||
5995 | // The nested-name-specifier never matters in a TemplateSpecializationType, | ||||||||
5996 | // because we can't have a dependent nested-name-specifier anyway. | ||||||||
5997 | CXXScopeSpec SS; | ||||||||
5998 | TemplateName Template | ||||||||
5999 | = getDerived().TransformTemplateName(SS, T->getTemplateName(), | ||||||||
6000 | TL.getTemplateNameLoc()); | ||||||||
6001 | if (Template.isNull()) | ||||||||
6002 | return QualType(); | ||||||||
6003 | |||||||||
6004 | return getDerived().TransformTemplateSpecializationType(TLB, TL, Template); | ||||||||
6005 | } | ||||||||
6006 | |||||||||
6007 | template<typename Derived> | ||||||||
6008 | QualType TreeTransform<Derived>::TransformAtomicType(TypeLocBuilder &TLB, | ||||||||
6009 | AtomicTypeLoc TL) { | ||||||||
6010 | QualType ValueType = getDerived().TransformType(TLB, TL.getValueLoc()); | ||||||||
6011 | if (ValueType.isNull()) | ||||||||
6012 | return QualType(); | ||||||||
6013 | |||||||||
6014 | QualType Result = TL.getType(); | ||||||||
6015 | if (getDerived().AlwaysRebuild() || | ||||||||
6016 | ValueType != TL.getValueLoc().getType()) { | ||||||||
6017 | Result = getDerived().RebuildAtomicType(ValueType, TL.getKWLoc()); | ||||||||
6018 | if (Result.isNull()) | ||||||||
6019 | return QualType(); | ||||||||
6020 | } | ||||||||
6021 | |||||||||
6022 | AtomicTypeLoc NewTL = TLB.push<AtomicTypeLoc>(Result); | ||||||||
6023 | NewTL.setKWLoc(TL.getKWLoc()); | ||||||||
6024 | NewTL.setLParenLoc(TL.getLParenLoc()); | ||||||||
6025 | NewTL.setRParenLoc(TL.getRParenLoc()); | ||||||||
6026 | |||||||||
6027 | return Result; | ||||||||
6028 | } | ||||||||
6029 | |||||||||
6030 | template <typename Derived> | ||||||||
6031 | QualType TreeTransform<Derived>::TransformPipeType(TypeLocBuilder &TLB, | ||||||||
6032 | PipeTypeLoc TL) { | ||||||||
6033 | QualType ValueType = getDerived().TransformType(TLB, TL.getValueLoc()); | ||||||||
6034 | if (ValueType.isNull()) | ||||||||
6035 | return QualType(); | ||||||||
6036 | |||||||||
6037 | QualType Result = TL.getType(); | ||||||||
6038 | if (getDerived().AlwaysRebuild() || ValueType != TL.getValueLoc().getType()) { | ||||||||
6039 | const PipeType *PT = Result->castAs<PipeType>(); | ||||||||
6040 | bool isReadPipe = PT->isReadOnly(); | ||||||||
6041 | Result = getDerived().RebuildPipeType(ValueType, TL.getKWLoc(), isReadPipe); | ||||||||
6042 | if (Result.isNull()) | ||||||||
6043 | return QualType(); | ||||||||
6044 | } | ||||||||
6045 | |||||||||
6046 | PipeTypeLoc NewTL = TLB.push<PipeTypeLoc>(Result); | ||||||||
6047 | NewTL.setKWLoc(TL.getKWLoc()); | ||||||||
6048 | |||||||||
6049 | return Result; | ||||||||
6050 | } | ||||||||
6051 | |||||||||
6052 | /// Simple iterator that traverses the template arguments in a | ||||||||
6053 | /// container that provides a \c getArgLoc() member function. | ||||||||
6054 | /// | ||||||||
6055 | /// This iterator is intended to be used with the iterator form of | ||||||||
6056 | /// \c TreeTransform<Derived>::TransformTemplateArguments(). | ||||||||
6057 | template<typename ArgLocContainer> | ||||||||
6058 | class TemplateArgumentLocContainerIterator { | ||||||||
6059 | ArgLocContainer *Container; | ||||||||
6060 | unsigned Index; | ||||||||
6061 | |||||||||
6062 | public: | ||||||||
6063 | typedef TemplateArgumentLoc value_type; | ||||||||
6064 | typedef TemplateArgumentLoc reference; | ||||||||
6065 | typedef int difference_type; | ||||||||
6066 | typedef std::input_iterator_tag iterator_category; | ||||||||
6067 | |||||||||
6068 | class pointer { | ||||||||
6069 | TemplateArgumentLoc Arg; | ||||||||
6070 | |||||||||
6071 | public: | ||||||||
6072 | explicit pointer(TemplateArgumentLoc Arg) : Arg(Arg) { } | ||||||||
6073 | |||||||||
6074 | const TemplateArgumentLoc *operator->() const { | ||||||||
6075 | return &Arg; | ||||||||
6076 | } | ||||||||
6077 | }; | ||||||||
6078 | |||||||||
6079 | |||||||||
6080 | TemplateArgumentLocContainerIterator() {} | ||||||||
6081 | |||||||||
6082 | TemplateArgumentLocContainerIterator(ArgLocContainer &Container, | ||||||||
6083 | unsigned Index) | ||||||||
6084 | : Container(&Container), Index(Index) { } | ||||||||
6085 | |||||||||
6086 | TemplateArgumentLocContainerIterator &operator++() { | ||||||||
6087 | ++Index; | ||||||||
6088 | return *this; | ||||||||
6089 | } | ||||||||
6090 | |||||||||
6091 | TemplateArgumentLocContainerIterator operator++(int) { | ||||||||
6092 | TemplateArgumentLocContainerIterator Old(*this); | ||||||||
6093 | ++(*this); | ||||||||
6094 | return Old; | ||||||||
6095 | } | ||||||||
6096 | |||||||||
6097 | TemplateArgumentLoc operator*() const { | ||||||||
6098 | return Container->getArgLoc(Index); | ||||||||
6099 | } | ||||||||
6100 | |||||||||
6101 | pointer operator->() const { | ||||||||
6102 | return pointer(Container->getArgLoc(Index)); | ||||||||
6103 | } | ||||||||
6104 | |||||||||
6105 | friend bool operator==(const TemplateArgumentLocContainerIterator &X, | ||||||||
6106 | const TemplateArgumentLocContainerIterator &Y) { | ||||||||
6107 | return X.Container == Y.Container && X.Index == Y.Index; | ||||||||
6108 | } | ||||||||
6109 | |||||||||
6110 | friend bool operator!=(const TemplateArgumentLocContainerIterator &X, | ||||||||
6111 | const TemplateArgumentLocContainerIterator &Y) { | ||||||||
6112 | return !(X == Y); | ||||||||
6113 | } | ||||||||
6114 | }; | ||||||||
6115 | |||||||||
6116 | template<typename Derived> | ||||||||
6117 | QualType TreeTransform<Derived>::TransformAutoType(TypeLocBuilder &TLB, | ||||||||
6118 | AutoTypeLoc TL) { | ||||||||
6119 | const AutoType *T = TL.getTypePtr(); | ||||||||
6120 | QualType OldDeduced = T->getDeducedType(); | ||||||||
6121 | QualType NewDeduced; | ||||||||
6122 | if (!OldDeduced.isNull()) { | ||||||||
6123 | NewDeduced = getDerived().TransformType(OldDeduced); | ||||||||
6124 | if (NewDeduced.isNull()) | ||||||||
6125 | return QualType(); | ||||||||
6126 | } | ||||||||
6127 | |||||||||
6128 | ConceptDecl *NewCD = nullptr; | ||||||||
6129 | TemplateArgumentListInfo NewTemplateArgs; | ||||||||
6130 | NestedNameSpecifierLoc NewNestedNameSpec; | ||||||||
6131 | if (TL.getTypePtr()->isConstrained()) { | ||||||||
6132 | NewCD = cast_or_null<ConceptDecl>( | ||||||||
6133 | getDerived().TransformDecl( | ||||||||
6134 | TL.getConceptNameLoc(), | ||||||||
6135 | TL.getTypePtr()->getTypeConstraintConcept())); | ||||||||
6136 | |||||||||
6137 | NewTemplateArgs.setLAngleLoc(TL.getLAngleLoc()); | ||||||||
6138 | NewTemplateArgs.setRAngleLoc(TL.getRAngleLoc()); | ||||||||
6139 | typedef TemplateArgumentLocContainerIterator<AutoTypeLoc> ArgIterator; | ||||||||
6140 | if (getDerived().TransformTemplateArguments(ArgIterator(TL, 0), | ||||||||
6141 | ArgIterator(TL, | ||||||||
6142 | TL.getNumArgs()), | ||||||||
6143 | NewTemplateArgs)) | ||||||||
6144 | return QualType(); | ||||||||
6145 | |||||||||
6146 | if (TL.getNestedNameSpecifierLoc()) { | ||||||||
6147 | NewNestedNameSpec | ||||||||
6148 | = getDerived().TransformNestedNameSpecifierLoc( | ||||||||
6149 | TL.getNestedNameSpecifierLoc()); | ||||||||
6150 | if (!NewNestedNameSpec) | ||||||||
6151 | return QualType(); | ||||||||
6152 | } | ||||||||
6153 | } | ||||||||
6154 | |||||||||
6155 | QualType Result = TL.getType(); | ||||||||
6156 | if (getDerived().AlwaysRebuild() || NewDeduced != OldDeduced || | ||||||||
6157 | T->isDependentType()) { | ||||||||
6158 | llvm::SmallVector<TemplateArgument, 4> NewArgList; | ||||||||
6159 | NewArgList.reserve(NewArgList.size()); | ||||||||
6160 | for (const auto &ArgLoc : NewTemplateArgs.arguments()) | ||||||||
6161 | NewArgList.push_back(ArgLoc.getArgument()); | ||||||||
6162 | Result = getDerived().RebuildAutoType(NewDeduced, T->getKeyword(), NewCD, | ||||||||
6163 | NewArgList); | ||||||||
6164 | if (Result.isNull()) | ||||||||
6165 | return QualType(); | ||||||||
6166 | } | ||||||||
6167 | |||||||||
6168 | AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result); | ||||||||
6169 | NewTL.setNameLoc(TL.getNameLoc()); | ||||||||
6170 | NewTL.setNestedNameSpecifierLoc(NewNestedNameSpec); | ||||||||
6171 | NewTL.setTemplateKWLoc(TL.getTemplateKWLoc()); | ||||||||
6172 | NewTL.setConceptNameLoc(TL.getConceptNameLoc()); | ||||||||
6173 | NewTL.setFoundDecl(TL.getFoundDecl()); | ||||||||
6174 | NewTL.setLAngleLoc(TL.getLAngleLoc()); | ||||||||
6175 | NewTL.setRAngleLoc(TL.getRAngleLoc()); | ||||||||
6176 | for (unsigned I = 0; I < TL.getNumArgs(); ++I) | ||||||||
6177 | NewTL.setArgLocInfo(I, NewTemplateArgs.arguments()[I].getLocInfo()); | ||||||||
6178 | |||||||||
6179 | return Result; | ||||||||
6180 | } | ||||||||
6181 | |||||||||
6182 | template <typename Derived> | ||||||||
6183 | QualType TreeTransform<Derived>::TransformTemplateSpecializationType( | ||||||||
6184 | TypeLocBuilder &TLB, | ||||||||
6185 | TemplateSpecializationTypeLoc TL, | ||||||||
6186 | TemplateName Template) { | ||||||||
6187 | TemplateArgumentListInfo NewTemplateArgs; | ||||||||
6188 | NewTemplateArgs.setLAngleLoc(TL.getLAngleLoc()); | ||||||||
6189 | NewTemplateArgs.setRAngleLoc(TL.getRAngleLoc()); | ||||||||
6190 | typedef TemplateArgumentLocContainerIterator<TemplateSpecializationTypeLoc> | ||||||||
6191 | ArgIterator; | ||||||||
6192 | if (getDerived().TransformTemplateArguments(ArgIterator(TL, 0), | ||||||||
6193 | ArgIterator(TL, TL.getNumArgs()), | ||||||||
6194 | NewTemplateArgs)) | ||||||||
6195 | return QualType(); | ||||||||
6196 | |||||||||
6197 | // FIXME: maybe don't rebuild if all the template arguments are the same. | ||||||||
6198 | |||||||||
6199 | QualType Result = | ||||||||
6200 | getDerived().RebuildTemplateSpecializationType(Template, | ||||||||
6201 | TL.getTemplateNameLoc(), | ||||||||
6202 | NewTemplateArgs); | ||||||||
6203 | |||||||||
6204 | if (!Result.isNull()) { | ||||||||
6205 | // Specializations of template template parameters are represented as | ||||||||
6206 | // TemplateSpecializationTypes, and substitution of type alias templates | ||||||||
6207 | // within a dependent context can transform them into | ||||||||
6208 | // DependentTemplateSpecializationTypes. | ||||||||
6209 | if (isa<DependentTemplateSpecializationType>(Result)) { | ||||||||
6210 | DependentTemplateSpecializationTypeLoc NewTL | ||||||||
6211 | = TLB.push<DependentTemplateSpecializationTypeLoc>(Result); | ||||||||
6212 | NewTL.setElaboratedKeywordLoc(SourceLocation()); | ||||||||
6213 | NewTL.setQualifierLoc(NestedNameSpecifierLoc()); | ||||||||
6214 | NewTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc()); | ||||||||
6215 | NewTL.setTemplateNameLoc(TL.getTemplateNameLoc()); | ||||||||
6216 | NewTL.setLAngleLoc(TL.getLAngleLoc()); | ||||||||
6217 | NewTL.setRAngleLoc(TL.getRAngleLoc()); | ||||||||
6218 | for (unsigned i = 0, e = NewTemplateArgs.size(); i != e; ++i) | ||||||||
6219 | NewTL.setArgLocInfo(i, NewTemplateArgs[i].getLocInfo()); | ||||||||
6220 | return Result; | ||||||||
6221 | } | ||||||||
6222 | |||||||||
6223 | TemplateSpecializationTypeLoc NewTL | ||||||||
6224 | = TLB.push<TemplateSpecializationTypeLoc>(Result); | ||||||||
6225 | NewTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc()); | ||||||||
6226 | NewTL.setTemplateNameLoc(TL.getTemplateNameLoc()); | ||||||||
6227 | NewTL.setLAngleLoc(TL.getLAngleLoc()); | ||||||||
6228 | NewTL.setRAngleLoc(TL.getRAngleLoc()); | ||||||||
6229 | for (unsigned i = 0, e = NewTemplateArgs.size(); i != e; ++i) | ||||||||
6230 | NewTL.setArgLocInfo(i, NewTemplateArgs[i].getLocInfo()); | ||||||||
6231 | } | ||||||||
6232 | |||||||||
6233 | return Result; | ||||||||
6234 | } | ||||||||
6235 | |||||||||
6236 | template <typename Derived> | ||||||||
6237 | QualType TreeTransform<Derived>::TransformDependentTemplateSpecializationType( | ||||||||
6238 | TypeLocBuilder &TLB, | ||||||||
6239 | DependentTemplateSpecializationTypeLoc TL, | ||||||||
6240 | TemplateName Template, | ||||||||
6241 | CXXScopeSpec &SS) { | ||||||||
6242 | TemplateArgumentListInfo NewTemplateArgs; | ||||||||
6243 | NewTemplateArgs.setLAngleLoc(TL.getLAngleLoc()); | ||||||||
6244 | NewTemplateArgs.setRAngleLoc(TL.getRAngleLoc()); | ||||||||
6245 | typedef TemplateArgumentLocContainerIterator< | ||||||||
6246 | DependentTemplateSpecializationTypeLoc> ArgIterator; | ||||||||
6247 | if (getDerived().TransformTemplateArguments(ArgIterator(TL, 0), | ||||||||
6248 | ArgIterator(TL, TL.getNumArgs()), | ||||||||
6249 | NewTemplateArgs)) | ||||||||
6250 | return QualType(); | ||||||||
6251 | |||||||||
6252 | // FIXME: maybe don't rebuild if all the template arguments are the same. | ||||||||
6253 | |||||||||
6254 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { | ||||||||
6255 | QualType Result | ||||||||
6256 | = getSema().Context.getDependentTemplateSpecializationType( | ||||||||
6257 | TL.getTypePtr()->getKeyword(), | ||||||||
6258 | DTN->getQualifier(), | ||||||||
6259 | DTN->getIdentifier(), | ||||||||
6260 | NewTemplateArgs); | ||||||||
6261 | |||||||||
6262 | DependentTemplateSpecializationTypeLoc NewTL | ||||||||
6263 | = TLB.push<DependentTemplateSpecializationTypeLoc>(Result); | ||||||||
6264 | NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc()); | ||||||||
6265 | NewTL.setQualifierLoc(SS.getWithLocInContext(SemaRef.Context)); | ||||||||
6266 | NewTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc()); | ||||||||
6267 | NewTL.setTemplateNameLoc(TL.getTemplateNameLoc()); | ||||||||
6268 | NewTL.setLAngleLoc(TL.getLAngleLoc()); | ||||||||
6269 | NewTL.setRAngleLoc(TL.getRAngleLoc()); | ||||||||
6270 | for (unsigned i = 0, e = NewTemplateArgs.size(); i != e; ++i) | ||||||||
6271 | NewTL.setArgLocInfo(i, NewTemplateArgs[i].getLocInfo()); | ||||||||
6272 | return Result; | ||||||||
6273 | } | ||||||||
6274 | |||||||||
6275 | QualType Result | ||||||||
6276 | = getDerived().RebuildTemplateSpecializationType(Template, | ||||||||
6277 | TL.getTemplateNameLoc(), | ||||||||
6278 | NewTemplateArgs); | ||||||||
6279 | |||||||||
6280 | if (!Result.isNull()) { | ||||||||
6281 | /// FIXME: Wrap this in an elaborated-type-specifier? | ||||||||
6282 | TemplateSpecializationTypeLoc NewTL | ||||||||
6283 | = TLB.push<TemplateSpecializationTypeLoc>(Result); | ||||||||
6284 | NewTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc()); | ||||||||
6285 | NewTL.setTemplateNameLoc(TL.getTemplateNameLoc()); | ||||||||
6286 | NewTL.setLAngleLoc(TL.getLAngleLoc()); | ||||||||
6287 | NewTL.setRAngleLoc(TL.getRAngleLoc()); | ||||||||
6288 | for (unsigned i = 0, e = NewTemplateArgs.size(); i != e; ++i) | ||||||||
6289 | NewTL.setArgLocInfo(i, NewTemplateArgs[i].getLocInfo()); | ||||||||
6290 | } | ||||||||
6291 | |||||||||
6292 | return Result; | ||||||||
6293 | } | ||||||||
6294 | |||||||||
6295 | template<typename Derived> | ||||||||
6296 | QualType | ||||||||
6297 | TreeTransform<Derived>::TransformElaboratedType(TypeLocBuilder &TLB, | ||||||||
6298 | ElaboratedTypeLoc TL) { | ||||||||
6299 | const ElaboratedType *T = TL.getTypePtr(); | ||||||||
6300 | |||||||||
6301 | NestedNameSpecifierLoc QualifierLoc; | ||||||||
6302 | // NOTE: the qualifier in an ElaboratedType is optional. | ||||||||
6303 | if (TL.getQualifierLoc()) { | ||||||||
6304 | QualifierLoc | ||||||||
6305 | = getDerived().TransformNestedNameSpecifierLoc(TL.getQualifierLoc()); | ||||||||
6306 | if (!QualifierLoc) | ||||||||
6307 | return QualType(); | ||||||||
6308 | } | ||||||||
6309 | |||||||||
6310 | QualType NamedT = getDerived().TransformType(TLB, TL.getNamedTypeLoc()); | ||||||||
6311 | if (NamedT.isNull()) | ||||||||
6312 | return QualType(); | ||||||||
6313 | |||||||||
6314 | // C++0x [dcl.type.elab]p2: | ||||||||
6315 | // If the identifier resolves to a typedef-name or the simple-template-id | ||||||||
6316 | // resolves to an alias template specialization, the | ||||||||
6317 | // elaborated-type-specifier is ill-formed. | ||||||||
6318 | if (T->getKeyword() != ETK_None && T->getKeyword() != ETK_Typename) { | ||||||||
6319 | if (const TemplateSpecializationType *TST = | ||||||||
6320 | NamedT->getAs<TemplateSpecializationType>()) { | ||||||||
6321 | TemplateName Template = TST->getTemplateName(); | ||||||||
6322 | if (TypeAliasTemplateDecl *TAT = dyn_cast_or_null<TypeAliasTemplateDecl>( | ||||||||
6323 | Template.getAsTemplateDecl())) { | ||||||||
6324 | SemaRef.Diag(TL.getNamedTypeLoc().getBeginLoc(), | ||||||||
6325 | diag::err_tag_reference_non_tag) | ||||||||
6326 | << TAT << Sema::NTK_TypeAliasTemplate | ||||||||
6327 | << ElaboratedType::getTagTypeKindForKeyword(T->getKeyword()); | ||||||||
6328 | SemaRef.Diag(TAT->getLocation(), diag::note_declared_at); | ||||||||
6329 | } | ||||||||
6330 | } | ||||||||
6331 | } | ||||||||
6332 | |||||||||
6333 | QualType Result = TL.getType(); | ||||||||
6334 | if (getDerived().AlwaysRebuild() || | ||||||||
6335 | QualifierLoc != TL.getQualifierLoc() || | ||||||||
6336 | NamedT != T->getNamedType()) { | ||||||||
6337 | Result = getDerived().RebuildElaboratedType(TL.getElaboratedKeywordLoc(), | ||||||||
6338 | T->getKeyword(), | ||||||||
6339 | QualifierLoc, NamedT); | ||||||||
6340 | if (Result.isNull()) | ||||||||
6341 | return QualType(); | ||||||||
6342 | } | ||||||||
6343 | |||||||||
6344 | ElaboratedTypeLoc NewTL = TLB.push<ElaboratedTypeLoc>(Result); | ||||||||
6345 | NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc()); | ||||||||
6346 | NewTL.setQualifierLoc(QualifierLoc); | ||||||||
6347 | return Result; | ||||||||
6348 | } | ||||||||
6349 | |||||||||
6350 | template<typename Derived> | ||||||||
6351 | QualType TreeTransform<Derived>::TransformAttributedType( | ||||||||
6352 | TypeLocBuilder &TLB, | ||||||||
6353 | AttributedTypeLoc TL) { | ||||||||
6354 | const AttributedType *oldType = TL.getTypePtr(); | ||||||||
6355 | QualType modifiedType = getDerived().TransformType(TLB, TL.getModifiedLoc()); | ||||||||
6356 | if (modifiedType.isNull()) | ||||||||
6357 | return QualType(); | ||||||||
6358 | |||||||||
6359 | // oldAttr can be null if we started with a QualType rather than a TypeLoc. | ||||||||
6360 | const Attr *oldAttr = TL.getAttr(); | ||||||||
6361 | const Attr *newAttr = oldAttr ? getDerived().TransformAttr(oldAttr) : nullptr; | ||||||||
6362 | if (oldAttr && !newAttr) | ||||||||
6363 | return QualType(); | ||||||||
6364 | |||||||||
6365 | QualType result = TL.getType(); | ||||||||
6366 | |||||||||
6367 | // FIXME: dependent operand expressions? | ||||||||
6368 | if (getDerived().AlwaysRebuild() || | ||||||||
6369 | modifiedType != oldType->getModifiedType()) { | ||||||||
6370 | // TODO: this is really lame; we should really be rebuilding the | ||||||||
6371 | // equivalent type from first principles. | ||||||||
6372 | QualType equivalentType | ||||||||
6373 | = getDerived().TransformType(oldType->getEquivalentType()); | ||||||||
6374 | if (equivalentType.isNull()) | ||||||||
6375 | return QualType(); | ||||||||
6376 | |||||||||
6377 | // Check whether we can add nullability; it is only represented as | ||||||||
6378 | // type sugar, and therefore cannot be diagnosed in any other way. | ||||||||
6379 | if (auto nullability = oldType->getImmediateNullability()) { | ||||||||
6380 | if (!modifiedType->canHaveNullability()) { | ||||||||
6381 | SemaRef.Diag(TL.getAttr()->getLocation(), | ||||||||
6382 | diag::err_nullability_nonpointer) | ||||||||
6383 | << DiagNullabilityKind(*nullability, false) << modifiedType; | ||||||||
6384 | return QualType(); | ||||||||
6385 | } | ||||||||
6386 | } | ||||||||
6387 | |||||||||
6388 | result = SemaRef.Context.getAttributedType(TL.getAttrKind(), | ||||||||
6389 | modifiedType, | ||||||||
6390 | equivalentType); | ||||||||
6391 | } | ||||||||
6392 | |||||||||
6393 | AttributedTypeLoc newTL = TLB.push<AttributedTypeLoc>(result); | ||||||||
6394 | newTL.setAttr(newAttr); | ||||||||
6395 | return result; | ||||||||
6396 | } | ||||||||
6397 | |||||||||
6398 | template<typename Derived> | ||||||||
6399 | QualType | ||||||||
6400 | TreeTransform<Derived>::TransformParenType(TypeLocBuilder &TLB, | ||||||||
6401 | ParenTypeLoc TL) { | ||||||||
6402 | QualType Inner = getDerived().TransformType(TLB, TL.getInnerLoc()); | ||||||||
6403 | if (Inner.isNull()) | ||||||||
6404 | return QualType(); | ||||||||
6405 | |||||||||
6406 | QualType Result = TL.getType(); | ||||||||
6407 | if (getDerived().AlwaysRebuild() || | ||||||||
6408 | Inner != TL.getInnerLoc().getType()) { | ||||||||
6409 | Result = getDerived().RebuildParenType(Inner); | ||||||||
6410 | if (Result.isNull()) | ||||||||
6411 | return QualType(); | ||||||||
6412 | } | ||||||||
6413 | |||||||||
6414 | ParenTypeLoc NewTL = TLB.push<ParenTypeLoc>(Result); | ||||||||
6415 | NewTL.setLParenLoc(TL.getLParenLoc()); | ||||||||
6416 | NewTL.setRParenLoc(TL.getRParenLoc()); | ||||||||
6417 | return Result; | ||||||||
6418 | } | ||||||||
6419 | |||||||||
6420 | template <typename Derived> | ||||||||
6421 | QualType | ||||||||
6422 | TreeTransform<Derived>::TransformMacroQualifiedType(TypeLocBuilder &TLB, | ||||||||
6423 | MacroQualifiedTypeLoc TL) { | ||||||||
6424 | QualType Inner = getDerived().TransformType(TLB, TL.getInnerLoc()); | ||||||||
6425 | if (Inner.isNull()) | ||||||||
6426 | return QualType(); | ||||||||
6427 | |||||||||
6428 | QualType Result = TL.getType(); | ||||||||
6429 | if (getDerived().AlwaysRebuild() || Inner != TL.getInnerLoc().getType()) { | ||||||||
6430 | Result = | ||||||||
6431 | getDerived().RebuildMacroQualifiedType(Inner, TL.getMacroIdentifier()); | ||||||||
6432 | if (Result.isNull()) | ||||||||
6433 | return QualType(); | ||||||||
6434 | } | ||||||||
6435 | |||||||||
6436 | MacroQualifiedTypeLoc NewTL = TLB.push<MacroQualifiedTypeLoc>(Result); | ||||||||
6437 | NewTL.setExpansionLoc(TL.getExpansionLoc()); | ||||||||
6438 | return Result; | ||||||||
6439 | } | ||||||||
6440 | |||||||||
6441 | template<typename Derived> | ||||||||
6442 | QualType TreeTransform<Derived>::TransformDependentNameType( | ||||||||
6443 | TypeLocBuilder &TLB, DependentNameTypeLoc TL) { | ||||||||
6444 | return TransformDependentNameType(TLB, TL, false); | ||||||||
6445 | } | ||||||||
6446 | |||||||||
6447 | template<typename Derived> | ||||||||
6448 | QualType TreeTransform<Derived>::TransformDependentNameType( | ||||||||
6449 | TypeLocBuilder &TLB, DependentNameTypeLoc TL, bool DeducedTSTContext) { | ||||||||
6450 | const DependentNameType *T = TL.getTypePtr(); | ||||||||
6451 | |||||||||
6452 | NestedNameSpecifierLoc QualifierLoc | ||||||||
6453 | = getDerived().TransformNestedNameSpecifierLoc(TL.getQualifierLoc()); | ||||||||
6454 | if (!QualifierLoc) | ||||||||
6455 | return QualType(); | ||||||||
6456 | |||||||||
6457 | QualType Result | ||||||||
6458 | = getDerived().RebuildDependentNameType(T->getKeyword(), | ||||||||
6459 | TL.getElaboratedKeywordLoc(), | ||||||||
6460 | QualifierLoc, | ||||||||
6461 | T->getIdentifier(), | ||||||||
6462 | TL.getNameLoc(), | ||||||||
6463 | DeducedTSTContext); | ||||||||
6464 | if (Result.isNull()) | ||||||||
6465 | return QualType(); | ||||||||
6466 | |||||||||
6467 | if (const ElaboratedType* ElabT = Result->getAs<ElaboratedType>()) { | ||||||||
6468 | QualType NamedT = ElabT->getNamedType(); | ||||||||
6469 | TLB.pushTypeSpec(NamedT).setNameLoc(TL.getNameLoc()); | ||||||||
6470 | |||||||||
6471 | ElaboratedTypeLoc NewTL = TLB.push<ElaboratedTypeLoc>(Result); | ||||||||
6472 | NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc()); | ||||||||
6473 | NewTL.setQualifierLoc(QualifierLoc); | ||||||||
6474 | } else { | ||||||||
6475 | DependentNameTypeLoc NewTL = TLB.push<DependentNameTypeLoc>(Result); | ||||||||
6476 | NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc()); | ||||||||
6477 | NewTL.setQualifierLoc(QualifierLoc); | ||||||||
6478 | NewTL.setNameLoc(TL.getNameLoc()); | ||||||||
6479 | } | ||||||||
6480 | return Result; | ||||||||
6481 | } | ||||||||
6482 | |||||||||
6483 | template<typename Derived> | ||||||||
6484 | QualType TreeTransform<Derived>:: | ||||||||
6485 | TransformDependentTemplateSpecializationType(TypeLocBuilder &TLB, | ||||||||
6486 | DependentTemplateSpecializationTypeLoc TL) { | ||||||||
6487 | NestedNameSpecifierLoc QualifierLoc; | ||||||||
6488 | if (TL.getQualifierLoc()) { | ||||||||
6489 | QualifierLoc | ||||||||
6490 | = getDerived().TransformNestedNameSpecifierLoc(TL.getQualifierLoc()); | ||||||||
6491 | if (!QualifierLoc) | ||||||||
6492 | return QualType(); | ||||||||
6493 | } | ||||||||
6494 | |||||||||
6495 | return getDerived() | ||||||||
6496 | .TransformDependentTemplateSpecializationType(TLB, TL, QualifierLoc); | ||||||||
6497 | } | ||||||||
6498 | |||||||||
6499 | template<typename Derived> | ||||||||
6500 | QualType TreeTransform<Derived>:: | ||||||||
6501 | TransformDependentTemplateSpecializationType(TypeLocBuilder &TLB, | ||||||||
6502 | DependentTemplateSpecializationTypeLoc TL, | ||||||||
6503 | NestedNameSpecifierLoc QualifierLoc) { | ||||||||
6504 | const DependentTemplateSpecializationType *T = TL.getTypePtr(); | ||||||||
6505 | |||||||||
6506 | TemplateArgumentListInfo NewTemplateArgs; | ||||||||
6507 | NewTemplateArgs.setLAngleLoc(TL.getLAngleLoc()); | ||||||||
6508 | NewTemplateArgs.setRAngleLoc(TL.getRAngleLoc()); | ||||||||
6509 | |||||||||
6510 | typedef TemplateArgumentLocContainerIterator< | ||||||||
6511 | DependentTemplateSpecializationTypeLoc> ArgIterator; | ||||||||
6512 | if (getDerived().TransformTemplateArguments(ArgIterator(TL, 0), | ||||||||
6513 | ArgIterator(TL, TL.getNumArgs()), | ||||||||
6514 | NewTemplateArgs)) | ||||||||
6515 | return QualType(); | ||||||||
6516 | |||||||||
6517 | QualType Result = getDerived().RebuildDependentTemplateSpecializationType( | ||||||||
6518 | T->getKeyword(), QualifierLoc, TL.getTemplateKeywordLoc(), | ||||||||
6519 | T->getIdentifier(), TL.getTemplateNameLoc(), NewTemplateArgs, | ||||||||
6520 | /*AllowInjectedClassName*/ false); | ||||||||
6521 | if (Result.isNull()) | ||||||||
6522 | return QualType(); | ||||||||
6523 | |||||||||
6524 | if (const ElaboratedType *ElabT = dyn_cast<ElaboratedType>(Result)) { | ||||||||
6525 | QualType NamedT = ElabT->getNamedType(); | ||||||||
6526 | |||||||||
6527 | // Copy information relevant to the template specialization. | ||||||||
6528 | TemplateSpecializationTypeLoc NamedTL | ||||||||
6529 | = TLB.push<TemplateSpecializationTypeLoc>(NamedT); | ||||||||
6530 | NamedTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc()); | ||||||||
6531 | NamedTL.setTemplateNameLoc(TL.getTemplateNameLoc()); | ||||||||
6532 | NamedTL.setLAngleLoc(TL.getLAngleLoc()); | ||||||||
6533 | NamedTL.setRAngleLoc(TL.getRAngleLoc()); | ||||||||
6534 | for (unsigned I = 0, E = NewTemplateArgs.size(); I != E; ++I) | ||||||||
6535 | NamedTL.setArgLocInfo(I, NewTemplateArgs[I].getLocInfo()); | ||||||||
6536 | |||||||||
6537 | // Copy information relevant to the elaborated type. | ||||||||
6538 | ElaboratedTypeLoc NewTL = TLB.push<ElaboratedTypeLoc>(Result); | ||||||||
6539 | NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc()); | ||||||||
6540 | NewTL.setQualifierLoc(QualifierLoc); | ||||||||
6541 | } else if (isa<DependentTemplateSpecializationType>(Result)) { | ||||||||
6542 | DependentTemplateSpecializationTypeLoc SpecTL | ||||||||
6543 | = TLB.push<DependentTemplateSpecializationTypeLoc>(Result); | ||||||||
6544 | SpecTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc()); | ||||||||
6545 | SpecTL.setQualifierLoc(QualifierLoc); | ||||||||
6546 | SpecTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc()); | ||||||||
6547 | SpecTL.setTemplateNameLoc(TL.getTemplateNameLoc()); | ||||||||
6548 | SpecTL.setLAngleLoc(TL.getLAngleLoc()); | ||||||||
6549 | SpecTL.setRAngleLoc(TL.getRAngleLoc()); | ||||||||
6550 | for (unsigned I = 0, E = NewTemplateArgs.size(); I != E; ++I) | ||||||||
6551 | SpecTL.setArgLocInfo(I, NewTemplateArgs[I].getLocInfo()); | ||||||||
6552 | } else { | ||||||||
6553 | TemplateSpecializationTypeLoc SpecTL | ||||||||
6554 | = TLB.push<TemplateSpecializationTypeLoc>(Result); | ||||||||
6555 | SpecTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc()); | ||||||||
6556 | SpecTL.setTemplateNameLoc(TL.getTemplateNameLoc()); | ||||||||
6557 | SpecTL.setLAngleLoc(TL.getLAngleLoc()); | ||||||||
6558 | SpecTL.setRAngleLoc(TL.getRAngleLoc()); | ||||||||
6559 | for (unsigned I = 0, E = NewTemplateArgs.size(); I != E; ++I) | ||||||||
6560 | SpecTL.setArgLocInfo(I, NewTemplateArgs[I].getLocInfo()); | ||||||||
6561 | } | ||||||||
6562 | return Result; | ||||||||
6563 | } | ||||||||
6564 | |||||||||
6565 | template<typename Derived> | ||||||||
6566 | QualType TreeTransform<Derived>::TransformPackExpansionType(TypeLocBuilder &TLB, | ||||||||
6567 | PackExpansionTypeLoc TL) { | ||||||||
6568 | QualType Pattern | ||||||||
6569 | = getDerived().TransformType(TLB, TL.getPatternLoc()); | ||||||||
6570 | if (Pattern.isNull()) | ||||||||
6571 | return QualType(); | ||||||||
6572 | |||||||||
6573 | QualType Result = TL.getType(); | ||||||||
6574 | if (getDerived().AlwaysRebuild() || | ||||||||
6575 | Pattern != TL.getPatternLoc().getType()) { | ||||||||
6576 | Result = getDerived().RebuildPackExpansionType(Pattern, | ||||||||
6577 | TL.getPatternLoc().getSourceRange(), | ||||||||
6578 | TL.getEllipsisLoc(), | ||||||||
6579 | TL.getTypePtr()->getNumExpansions()); | ||||||||
6580 | if (Result.isNull()) | ||||||||
6581 | return QualType(); | ||||||||
6582 | } | ||||||||
6583 | |||||||||
6584 | PackExpansionTypeLoc NewT = TLB.push<PackExpansionTypeLoc>(Result); | ||||||||
6585 | NewT.setEllipsisLoc(TL.getEllipsisLoc()); | ||||||||
6586 | return Result; | ||||||||
6587 | } | ||||||||
6588 | |||||||||
6589 | template<typename Derived> | ||||||||
6590 | QualType | ||||||||
6591 | TreeTransform<Derived>::TransformObjCInterfaceType(TypeLocBuilder &TLB, | ||||||||
6592 | ObjCInterfaceTypeLoc TL) { | ||||||||
6593 | // ObjCInterfaceType is never dependent. | ||||||||
6594 | TLB.pushFullCopy(TL); | ||||||||
6595 | return TL.getType(); | ||||||||
6596 | } | ||||||||
6597 | |||||||||
6598 | template<typename Derived> | ||||||||
6599 | QualType | ||||||||
6600 | TreeTransform<Derived>::TransformObjCTypeParamType(TypeLocBuilder &TLB, | ||||||||
6601 | ObjCTypeParamTypeLoc TL) { | ||||||||
6602 | const ObjCTypeParamType *T = TL.getTypePtr(); | ||||||||
6603 | ObjCTypeParamDecl *OTP = cast_or_null<ObjCTypeParamDecl>( | ||||||||
6604 | getDerived().TransformDecl(T->getDecl()->getLocation(), T->getDecl())); | ||||||||
6605 | if (!OTP) | ||||||||
6606 | return QualType(); | ||||||||
6607 | |||||||||
6608 | QualType Result = TL.getType(); | ||||||||
6609 | if (getDerived().AlwaysRebuild() || | ||||||||
6610 | OTP != T->getDecl()) { | ||||||||
6611 | Result = getDerived().RebuildObjCTypeParamType(OTP, | ||||||||
6612 | TL.getProtocolLAngleLoc(), | ||||||||
6613 | llvm::makeArrayRef(TL.getTypePtr()->qual_begin(), | ||||||||
6614 | TL.getNumProtocols()), | ||||||||
6615 | TL.getProtocolLocs(), | ||||||||
6616 | TL.getProtocolRAngleLoc()); | ||||||||
6617 | if (Result.isNull()) | ||||||||
6618 | return QualType(); | ||||||||
6619 | } | ||||||||
6620 | |||||||||
6621 | ObjCTypeParamTypeLoc NewTL = TLB.push<ObjCTypeParamTypeLoc>(Result); | ||||||||
6622 | if (TL.getNumProtocols()) { | ||||||||
6623 | NewTL.setProtocolLAngleLoc(TL.getProtocolLAngleLoc()); | ||||||||
6624 | for (unsigned i = 0, n = TL.getNumProtocols(); i != n; ++i) | ||||||||
6625 | NewTL.setProtocolLoc(i, TL.getProtocolLoc(i)); | ||||||||
6626 | NewTL.setProtocolRAngleLoc(TL.getProtocolRAngleLoc()); | ||||||||
6627 | } | ||||||||
6628 | return Result; | ||||||||
6629 | } | ||||||||
6630 | |||||||||
6631 | template<typename Derived> | ||||||||
6632 | QualType | ||||||||
6633 | TreeTransform<Derived>::TransformObjCObjectType(TypeLocBuilder &TLB, | ||||||||
6634 | ObjCObjectTypeLoc TL) { | ||||||||
6635 | // Transform base type. | ||||||||
6636 | QualType BaseType = getDerived().TransformType(TLB, TL.getBaseLoc()); | ||||||||
6637 | if (BaseType.isNull()) | ||||||||
6638 | return QualType(); | ||||||||
6639 | |||||||||
6640 | bool AnyChanged = BaseType != TL.getBaseLoc().getType(); | ||||||||
6641 | |||||||||
6642 | // Transform type arguments. | ||||||||
6643 | SmallVector<TypeSourceInfo *, 4> NewTypeArgInfos; | ||||||||
6644 | for (unsigned i = 0, n = TL.getNumTypeArgs(); i != n; ++i) { | ||||||||
6645 | TypeSourceInfo *TypeArgInfo = TL.getTypeArgTInfo(i); | ||||||||
6646 | TypeLoc TypeArgLoc = TypeArgInfo->getTypeLoc(); | ||||||||
6647 | QualType TypeArg = TypeArgInfo->getType(); | ||||||||
6648 | if (auto PackExpansionLoc = TypeArgLoc.getAs<PackExpansionTypeLoc>()) { | ||||||||
6649 | AnyChanged = true; | ||||||||
6650 | |||||||||
6651 | // We have a pack expansion. Instantiate it. | ||||||||
6652 | const auto *PackExpansion = PackExpansionLoc.getType() | ||||||||
6653 | ->castAs<PackExpansionType>(); | ||||||||
6654 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | ||||||||
6655 | SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(), | ||||||||
6656 | Unexpanded); | ||||||||
6657 | assert(!Unexpanded.empty() && "Pack expansion without parameter packs?")((!Unexpanded.empty() && "Pack expansion without parameter packs?" ) ? static_cast<void> (0) : __assert_fail ("!Unexpanded.empty() && \"Pack expansion without parameter packs?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 6657, __PRETTY_FUNCTION__)); | ||||||||
6658 | |||||||||
6659 | // Determine whether the set of unexpanded parameter packs can | ||||||||
6660 | // and should be expanded. | ||||||||
6661 | TypeLoc PatternLoc = PackExpansionLoc.getPatternLoc(); | ||||||||
6662 | bool Expand = false; | ||||||||
6663 | bool RetainExpansion = false; | ||||||||
6664 | Optional<unsigned> NumExpansions = PackExpansion->getNumExpansions(); | ||||||||
6665 | if (getDerived().TryExpandParameterPacks( | ||||||||
6666 | PackExpansionLoc.getEllipsisLoc(), PatternLoc.getSourceRange(), | ||||||||
6667 | Unexpanded, Expand, RetainExpansion, NumExpansions)) | ||||||||
6668 | return QualType(); | ||||||||
6669 | |||||||||
6670 | if (!Expand) { | ||||||||
6671 | // We can't expand this pack expansion into separate arguments yet; | ||||||||
6672 | // just substitute into the pattern and create a new pack expansion | ||||||||
6673 | // type. | ||||||||
6674 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1); | ||||||||
6675 | |||||||||
6676 | TypeLocBuilder TypeArgBuilder; | ||||||||
6677 | TypeArgBuilder.reserve(PatternLoc.getFullDataSize()); | ||||||||
6678 | QualType NewPatternType = getDerived().TransformType(TypeArgBuilder, | ||||||||
6679 | PatternLoc); | ||||||||
6680 | if (NewPatternType.isNull()) | ||||||||
6681 | return QualType(); | ||||||||
6682 | |||||||||
6683 | QualType NewExpansionType = SemaRef.Context.getPackExpansionType( | ||||||||
6684 | NewPatternType, NumExpansions); | ||||||||
6685 | auto NewExpansionLoc = TLB.push<PackExpansionTypeLoc>(NewExpansionType); | ||||||||
6686 | NewExpansionLoc.setEllipsisLoc(PackExpansionLoc.getEllipsisLoc()); | ||||||||
6687 | NewTypeArgInfos.push_back( | ||||||||
6688 | TypeArgBuilder.getTypeSourceInfo(SemaRef.Context, NewExpansionType)); | ||||||||
6689 | continue; | ||||||||
6690 | } | ||||||||
6691 | |||||||||
6692 | // Substitute into the pack expansion pattern for each slice of the | ||||||||
6693 | // pack. | ||||||||
6694 | for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) { | ||||||||
6695 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), ArgIdx); | ||||||||
6696 | |||||||||
6697 | TypeLocBuilder TypeArgBuilder; | ||||||||
6698 | TypeArgBuilder.reserve(PatternLoc.getFullDataSize()); | ||||||||
6699 | |||||||||
6700 | QualType NewTypeArg = getDerived().TransformType(TypeArgBuilder, | ||||||||
6701 | PatternLoc); | ||||||||
6702 | if (NewTypeArg.isNull()) | ||||||||
6703 | return QualType(); | ||||||||
6704 | |||||||||
6705 | NewTypeArgInfos.push_back( | ||||||||
6706 | TypeArgBuilder.getTypeSourceInfo(SemaRef.Context, NewTypeArg)); | ||||||||
6707 | } | ||||||||
6708 | |||||||||
6709 | continue; | ||||||||
6710 | } | ||||||||
6711 | |||||||||
6712 | TypeLocBuilder TypeArgBuilder; | ||||||||
6713 | TypeArgBuilder.reserve(TypeArgLoc.getFullDataSize()); | ||||||||
6714 | QualType NewTypeArg = getDerived().TransformType(TypeArgBuilder, TypeArgLoc); | ||||||||
6715 | if (NewTypeArg.isNull()) | ||||||||
6716 | return QualType(); | ||||||||
6717 | |||||||||
6718 | // If nothing changed, just keep the old TypeSourceInfo. | ||||||||
6719 | if (NewTypeArg == TypeArg) { | ||||||||
6720 | NewTypeArgInfos.push_back(TypeArgInfo); | ||||||||
6721 | continue; | ||||||||
6722 | } | ||||||||
6723 | |||||||||
6724 | NewTypeArgInfos.push_back( | ||||||||
6725 | TypeArgBuilder.getTypeSourceInfo(SemaRef.Context, NewTypeArg)); | ||||||||
6726 | AnyChanged = true; | ||||||||
6727 | } | ||||||||
6728 | |||||||||
6729 | QualType Result = TL.getType(); | ||||||||
6730 | if (getDerived().AlwaysRebuild() || AnyChanged) { | ||||||||
6731 | // Rebuild the type. | ||||||||
6732 | Result = getDerived().RebuildObjCObjectType( | ||||||||
6733 | BaseType, TL.getBeginLoc(), TL.getTypeArgsLAngleLoc(), NewTypeArgInfos, | ||||||||
6734 | TL.getTypeArgsRAngleLoc(), TL.getProtocolLAngleLoc(), | ||||||||
6735 | llvm::makeArrayRef(TL.getTypePtr()->qual_begin(), TL.getNumProtocols()), | ||||||||
6736 | TL.getProtocolLocs(), TL.getProtocolRAngleLoc()); | ||||||||
6737 | |||||||||
6738 | if (Result.isNull()) | ||||||||
6739 | return QualType(); | ||||||||
6740 | } | ||||||||
6741 | |||||||||
6742 | ObjCObjectTypeLoc NewT = TLB.push<ObjCObjectTypeLoc>(Result); | ||||||||
6743 | NewT.setHasBaseTypeAsWritten(true); | ||||||||
6744 | NewT.setTypeArgsLAngleLoc(TL.getTypeArgsLAngleLoc()); | ||||||||
6745 | for (unsigned i = 0, n = TL.getNumTypeArgs(); i != n; ++i) | ||||||||
6746 | NewT.setTypeArgTInfo(i, NewTypeArgInfos[i]); | ||||||||
6747 | NewT.setTypeArgsRAngleLoc(TL.getTypeArgsRAngleLoc()); | ||||||||
6748 | NewT.setProtocolLAngleLoc(TL.getProtocolLAngleLoc()); | ||||||||
6749 | for (unsigned i = 0, n = TL.getNumProtocols(); i != n; ++i) | ||||||||
6750 | NewT.setProtocolLoc(i, TL.getProtocolLoc(i)); | ||||||||
6751 | NewT.setProtocolRAngleLoc(TL.getProtocolRAngleLoc()); | ||||||||
6752 | return Result; | ||||||||
6753 | } | ||||||||
6754 | |||||||||
6755 | template<typename Derived> | ||||||||
6756 | QualType | ||||||||
6757 | TreeTransform<Derived>::TransformObjCObjectPointerType(TypeLocBuilder &TLB, | ||||||||
6758 | ObjCObjectPointerTypeLoc TL) { | ||||||||
6759 | QualType PointeeType = getDerived().TransformType(TLB, TL.getPointeeLoc()); | ||||||||
6760 | if (PointeeType.isNull()) | ||||||||
6761 | return QualType(); | ||||||||
6762 | |||||||||
6763 | QualType Result = TL.getType(); | ||||||||
6764 | if (getDerived().AlwaysRebuild() || | ||||||||
6765 | PointeeType != TL.getPointeeLoc().getType()) { | ||||||||
6766 | Result = getDerived().RebuildObjCObjectPointerType(PointeeType, | ||||||||
6767 | TL.getStarLoc()); | ||||||||
6768 | if (Result.isNull()) | ||||||||
6769 | return QualType(); | ||||||||
6770 | } | ||||||||
6771 | |||||||||
6772 | ObjCObjectPointerTypeLoc NewT = TLB.push<ObjCObjectPointerTypeLoc>(Result); | ||||||||
6773 | NewT.setStarLoc(TL.getStarLoc()); | ||||||||
6774 | return Result; | ||||||||
6775 | } | ||||||||
6776 | |||||||||
6777 | //===----------------------------------------------------------------------===// | ||||||||
6778 | // Statement transformation | ||||||||
6779 | //===----------------------------------------------------------------------===// | ||||||||
6780 | template<typename Derived> | ||||||||
6781 | StmtResult | ||||||||
6782 | TreeTransform<Derived>::TransformNullStmt(NullStmt *S) { | ||||||||
6783 | return S; | ||||||||
6784 | } | ||||||||
6785 | |||||||||
6786 | template<typename Derived> | ||||||||
6787 | StmtResult | ||||||||
6788 | TreeTransform<Derived>::TransformCompoundStmt(CompoundStmt *S) { | ||||||||
6789 | return getDerived().TransformCompoundStmt(S, false); | ||||||||
6790 | } | ||||||||
6791 | |||||||||
6792 | template<typename Derived> | ||||||||
6793 | StmtResult | ||||||||
6794 | TreeTransform<Derived>::TransformCompoundStmt(CompoundStmt *S, | ||||||||
6795 | bool IsStmtExpr) { | ||||||||
6796 | Sema::CompoundScopeRAII CompoundScope(getSema()); | ||||||||
6797 | |||||||||
6798 | const Stmt *ExprResult = S->getStmtExprResult(); | ||||||||
6799 | bool SubStmtInvalid = false; | ||||||||
6800 | bool SubStmtChanged = false; | ||||||||
6801 | SmallVector<Stmt*, 8> Statements; | ||||||||
6802 | for (auto *B : S->body()) { | ||||||||
6803 | StmtResult Result = getDerived().TransformStmt( | ||||||||
6804 | B, IsStmtExpr && B == ExprResult ? SDK_StmtExprResult : SDK_Discarded); | ||||||||
6805 | |||||||||
6806 | if (Result.isInvalid()) { | ||||||||
6807 | // Immediately fail if this was a DeclStmt, since it's very | ||||||||
6808 | // likely that this will cause problems for future statements. | ||||||||
6809 | if (isa<DeclStmt>(B)) | ||||||||
6810 | return StmtError(); | ||||||||
6811 | |||||||||
6812 | // Otherwise, just keep processing substatements and fail later. | ||||||||
6813 | SubStmtInvalid = true; | ||||||||
6814 | continue; | ||||||||
6815 | } | ||||||||
6816 | |||||||||
6817 | SubStmtChanged = SubStmtChanged || Result.get() != B; | ||||||||
6818 | Statements.push_back(Result.getAs<Stmt>()); | ||||||||
6819 | } | ||||||||
6820 | |||||||||
6821 | if (SubStmtInvalid) | ||||||||
6822 | return StmtError(); | ||||||||
6823 | |||||||||
6824 | if (!getDerived().AlwaysRebuild() && | ||||||||
6825 | !SubStmtChanged) | ||||||||
6826 | return S; | ||||||||
6827 | |||||||||
6828 | return getDerived().RebuildCompoundStmt(S->getLBracLoc(), | ||||||||
6829 | Statements, | ||||||||
6830 | S->getRBracLoc(), | ||||||||
6831 | IsStmtExpr); | ||||||||
6832 | } | ||||||||
6833 | |||||||||
6834 | template<typename Derived> | ||||||||
6835 | StmtResult | ||||||||
6836 | TreeTransform<Derived>::TransformCaseStmt(CaseStmt *S) { | ||||||||
6837 | ExprResult LHS, RHS; | ||||||||
6838 | { | ||||||||
6839 | EnterExpressionEvaluationContext Unevaluated( | ||||||||
6840 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||
6841 | |||||||||
6842 | // Transform the left-hand case value. | ||||||||
6843 | LHS = getDerived().TransformExpr(S->getLHS()); | ||||||||
6844 | LHS = SemaRef.ActOnCaseExpr(S->getCaseLoc(), LHS); | ||||||||
6845 | if (LHS.isInvalid()) | ||||||||
6846 | return StmtError(); | ||||||||
6847 | |||||||||
6848 | // Transform the right-hand case value (for the GNU case-range extension). | ||||||||
6849 | RHS = getDerived().TransformExpr(S->getRHS()); | ||||||||
6850 | RHS = SemaRef.ActOnCaseExpr(S->getCaseLoc(), RHS); | ||||||||
6851 | if (RHS.isInvalid()) | ||||||||
6852 | return StmtError(); | ||||||||
6853 | } | ||||||||
6854 | |||||||||
6855 | // Build the case statement. | ||||||||
6856 | // Case statements are always rebuilt so that they will attached to their | ||||||||
6857 | // transformed switch statement. | ||||||||
6858 | StmtResult Case = getDerived().RebuildCaseStmt(S->getCaseLoc(), | ||||||||
6859 | LHS.get(), | ||||||||
6860 | S->getEllipsisLoc(), | ||||||||
6861 | RHS.get(), | ||||||||
6862 | S->getColonLoc()); | ||||||||
6863 | if (Case.isInvalid()) | ||||||||
6864 | return StmtError(); | ||||||||
6865 | |||||||||
6866 | // Transform the statement following the case | ||||||||
6867 | StmtResult SubStmt = | ||||||||
6868 | getDerived().TransformStmt(S->getSubStmt()); | ||||||||
6869 | if (SubStmt.isInvalid()) | ||||||||
6870 | return StmtError(); | ||||||||
6871 | |||||||||
6872 | // Attach the body to the case statement | ||||||||
6873 | return getDerived().RebuildCaseStmtBody(Case.get(), SubStmt.get()); | ||||||||
6874 | } | ||||||||
6875 | |||||||||
6876 | template <typename Derived> | ||||||||
6877 | StmtResult TreeTransform<Derived>::TransformDefaultStmt(DefaultStmt *S) { | ||||||||
6878 | // Transform the statement following the default case | ||||||||
6879 | StmtResult SubStmt = | ||||||||
6880 | getDerived().TransformStmt(S->getSubStmt()); | ||||||||
6881 | if (SubStmt.isInvalid()) | ||||||||
6882 | return StmtError(); | ||||||||
6883 | |||||||||
6884 | // Default statements are always rebuilt | ||||||||
6885 | return getDerived().RebuildDefaultStmt(S->getDefaultLoc(), S->getColonLoc(), | ||||||||
6886 | SubStmt.get()); | ||||||||
6887 | } | ||||||||
6888 | |||||||||
6889 | template<typename Derived> | ||||||||
6890 | StmtResult | ||||||||
6891 | TreeTransform<Derived>::TransformLabelStmt(LabelStmt *S, StmtDiscardKind SDK) { | ||||||||
6892 | StmtResult SubStmt = getDerived().TransformStmt(S->getSubStmt(), SDK); | ||||||||
6893 | if (SubStmt.isInvalid()) | ||||||||
6894 | return StmtError(); | ||||||||
6895 | |||||||||
6896 | Decl *LD = getDerived().TransformDecl(S->getDecl()->getLocation(), | ||||||||
6897 | S->getDecl()); | ||||||||
6898 | if (!LD) | ||||||||
6899 | return StmtError(); | ||||||||
6900 | |||||||||
6901 | // If we're transforming "in-place" (we're not creating new local | ||||||||
6902 | // declarations), assume we're replacing the old label statement | ||||||||
6903 | // and clear out the reference to it. | ||||||||
6904 | if (LD == S->getDecl()) | ||||||||
6905 | S->getDecl()->setStmt(nullptr); | ||||||||
6906 | |||||||||
6907 | // FIXME: Pass the real colon location in. | ||||||||
6908 | return getDerived().RebuildLabelStmt(S->getIdentLoc(), | ||||||||
6909 | cast<LabelDecl>(LD), SourceLocation(), | ||||||||
6910 | SubStmt.get()); | ||||||||
6911 | } | ||||||||
6912 | |||||||||
6913 | template <typename Derived> | ||||||||
6914 | const Attr *TreeTransform<Derived>::TransformAttr(const Attr *R) { | ||||||||
6915 | if (!R) | ||||||||
6916 | return R; | ||||||||
6917 | |||||||||
6918 | switch (R->getKind()) { | ||||||||
6919 | // Transform attributes with a pragma spelling by calling TransformXXXAttr. | ||||||||
6920 | #define ATTR(X) | ||||||||
6921 | #define PRAGMA_SPELLING_ATTR(X) \ | ||||||||
6922 | case attr::X: \ | ||||||||
6923 | return getDerived().Transform##X##Attr(cast<X##Attr>(R)); | ||||||||
6924 | #include "clang/Basic/AttrList.inc" | ||||||||
6925 | default: | ||||||||
6926 | return R; | ||||||||
6927 | } | ||||||||
6928 | } | ||||||||
6929 | |||||||||
6930 | template <typename Derived> | ||||||||
6931 | StmtResult | ||||||||
6932 | TreeTransform<Derived>::TransformAttributedStmt(AttributedStmt *S, | ||||||||
6933 | StmtDiscardKind SDK) { | ||||||||
6934 | bool AttrsChanged = false; | ||||||||
6935 | SmallVector<const Attr *, 1> Attrs; | ||||||||
6936 | |||||||||
6937 | // Visit attributes and keep track if any are transformed. | ||||||||
6938 | for (const auto *I : S->getAttrs()) { | ||||||||
6939 | const Attr *R = getDerived().TransformAttr(I); | ||||||||
6940 | AttrsChanged |= (I != R); | ||||||||
6941 | Attrs.push_back(R); | ||||||||
6942 | } | ||||||||
6943 | |||||||||
6944 | StmtResult SubStmt = getDerived().TransformStmt(S->getSubStmt(), SDK); | ||||||||
6945 | if (SubStmt.isInvalid()) | ||||||||
6946 | return StmtError(); | ||||||||
6947 | |||||||||
6948 | if (SubStmt.get() == S->getSubStmt() && !AttrsChanged) | ||||||||
6949 | return S; | ||||||||
6950 | |||||||||
6951 | return getDerived().RebuildAttributedStmt(S->getAttrLoc(), Attrs, | ||||||||
6952 | SubStmt.get()); | ||||||||
6953 | } | ||||||||
6954 | |||||||||
6955 | template<typename Derived> | ||||||||
6956 | StmtResult | ||||||||
6957 | TreeTransform<Derived>::TransformIfStmt(IfStmt *S) { | ||||||||
6958 | // Transform the initialization statement | ||||||||
6959 | StmtResult Init = getDerived().TransformStmt(S->getInit()); | ||||||||
6960 | if (Init.isInvalid()) | ||||||||
6961 | return StmtError(); | ||||||||
6962 | |||||||||
6963 | // Transform the condition | ||||||||
6964 | Sema::ConditionResult Cond = getDerived().TransformCondition( | ||||||||
6965 | S->getIfLoc(), S->getConditionVariable(), S->getCond(), | ||||||||
6966 | S->isConstexpr() ? Sema::ConditionKind::ConstexprIf | ||||||||
6967 | : Sema::ConditionKind::Boolean); | ||||||||
6968 | if (Cond.isInvalid()) | ||||||||
6969 | return StmtError(); | ||||||||
6970 | |||||||||
6971 | // If this is a constexpr if, determine which arm we should instantiate. | ||||||||
6972 | llvm::Optional<bool> ConstexprConditionValue; | ||||||||
6973 | if (S->isConstexpr()) | ||||||||
6974 | ConstexprConditionValue = Cond.getKnownValue(); | ||||||||
6975 | |||||||||
6976 | // Transform the "then" branch. | ||||||||
6977 | StmtResult Then; | ||||||||
6978 | if (!ConstexprConditionValue || *ConstexprConditionValue) { | ||||||||
6979 | Then = getDerived().TransformStmt(S->getThen()); | ||||||||
6980 | if (Then.isInvalid()) | ||||||||
6981 | return StmtError(); | ||||||||
6982 | } else { | ||||||||
6983 | Then = new (getSema().Context) NullStmt(S->getThen()->getBeginLoc()); | ||||||||
6984 | } | ||||||||
6985 | |||||||||
6986 | // Transform the "else" branch. | ||||||||
6987 | StmtResult Else; | ||||||||
6988 | if (!ConstexprConditionValue || !*ConstexprConditionValue) { | ||||||||
6989 | Else = getDerived().TransformStmt(S->getElse()); | ||||||||
6990 | if (Else.isInvalid()) | ||||||||
6991 | return StmtError(); | ||||||||
6992 | } | ||||||||
6993 | |||||||||
6994 | if (!getDerived().AlwaysRebuild() && | ||||||||
6995 | Init.get() == S->getInit() && | ||||||||
6996 | Cond.get() == std::make_pair(S->getConditionVariable(), S->getCond()) && | ||||||||
6997 | Then.get() == S->getThen() && | ||||||||
6998 | Else.get() == S->getElse()) | ||||||||
6999 | return S; | ||||||||
7000 | |||||||||
7001 | return getDerived().RebuildIfStmt(S->getIfLoc(), S->isConstexpr(), Cond, | ||||||||
7002 | Init.get(), Then.get(), S->getElseLoc(), | ||||||||
7003 | Else.get()); | ||||||||
7004 | } | ||||||||
7005 | |||||||||
7006 | template<typename Derived> | ||||||||
7007 | StmtResult | ||||||||
7008 | TreeTransform<Derived>::TransformSwitchStmt(SwitchStmt *S) { | ||||||||
7009 | // Transform the initialization statement | ||||||||
7010 | StmtResult Init = getDerived().TransformStmt(S->getInit()); | ||||||||
7011 | if (Init.isInvalid()) | ||||||||
7012 | return StmtError(); | ||||||||
7013 | |||||||||
7014 | // Transform the condition. | ||||||||
7015 | Sema::ConditionResult Cond = getDerived().TransformCondition( | ||||||||
7016 | S->getSwitchLoc(), S->getConditionVariable(), S->getCond(), | ||||||||
7017 | Sema::ConditionKind::Switch); | ||||||||
7018 | if (Cond.isInvalid()) | ||||||||
7019 | return StmtError(); | ||||||||
7020 | |||||||||
7021 | // Rebuild the switch statement. | ||||||||
7022 | StmtResult Switch | ||||||||
7023 | = getDerived().RebuildSwitchStmtStart(S->getSwitchLoc(), Init.get(), Cond); | ||||||||
7024 | if (Switch.isInvalid()) | ||||||||
7025 | return StmtError(); | ||||||||
7026 | |||||||||
7027 | // Transform the body of the switch statement. | ||||||||
7028 | StmtResult Body = getDerived().TransformStmt(S->getBody()); | ||||||||
7029 | if (Body.isInvalid()) | ||||||||
7030 | return StmtError(); | ||||||||
7031 | |||||||||
7032 | // Complete the switch statement. | ||||||||
7033 | return getDerived().RebuildSwitchStmtBody(S->getSwitchLoc(), Switch.get(), | ||||||||
7034 | Body.get()); | ||||||||
7035 | } | ||||||||
7036 | |||||||||
7037 | template<typename Derived> | ||||||||
7038 | StmtResult | ||||||||
7039 | TreeTransform<Derived>::TransformWhileStmt(WhileStmt *S) { | ||||||||
7040 | // Transform the condition | ||||||||
7041 | Sema::ConditionResult Cond = getDerived().TransformCondition( | ||||||||
7042 | S->getWhileLoc(), S->getConditionVariable(), S->getCond(), | ||||||||
7043 | Sema::ConditionKind::Boolean); | ||||||||
7044 | if (Cond.isInvalid()) | ||||||||
7045 | return StmtError(); | ||||||||
7046 | |||||||||
7047 | // Transform the body | ||||||||
7048 | StmtResult Body = getDerived().TransformStmt(S->getBody()); | ||||||||
7049 | if (Body.isInvalid()) | ||||||||
7050 | return StmtError(); | ||||||||
7051 | |||||||||
7052 | if (!getDerived().AlwaysRebuild() && | ||||||||
7053 | Cond.get() == std::make_pair(S->getConditionVariable(), S->getCond()) && | ||||||||
7054 | Body.get() == S->getBody()) | ||||||||
7055 | return Owned(S); | ||||||||
7056 | |||||||||
7057 | return getDerived().RebuildWhileStmt(S->getWhileLoc(), Cond, Body.get()); | ||||||||
7058 | } | ||||||||
7059 | |||||||||
7060 | template<typename Derived> | ||||||||
7061 | StmtResult | ||||||||
7062 | TreeTransform<Derived>::TransformDoStmt(DoStmt *S) { | ||||||||
7063 | // Transform the body | ||||||||
7064 | StmtResult Body = getDerived().TransformStmt(S->getBody()); | ||||||||
7065 | if (Body.isInvalid()) | ||||||||
7066 | return StmtError(); | ||||||||
7067 | |||||||||
7068 | // Transform the condition | ||||||||
7069 | ExprResult Cond = getDerived().TransformExpr(S->getCond()); | ||||||||
7070 | if (Cond.isInvalid()) | ||||||||
7071 | return StmtError(); | ||||||||
7072 | |||||||||
7073 | if (!getDerived().AlwaysRebuild() && | ||||||||
7074 | Cond.get() == S->getCond() && | ||||||||
7075 | Body.get() == S->getBody()) | ||||||||
7076 | return S; | ||||||||
7077 | |||||||||
7078 | return getDerived().RebuildDoStmt(S->getDoLoc(), Body.get(), S->getWhileLoc(), | ||||||||
7079 | /*FIXME:*/S->getWhileLoc(), Cond.get(), | ||||||||
7080 | S->getRParenLoc()); | ||||||||
7081 | } | ||||||||
7082 | |||||||||
7083 | template<typename Derived> | ||||||||
7084 | StmtResult | ||||||||
7085 | TreeTransform<Derived>::TransformForStmt(ForStmt *S) { | ||||||||
7086 | if (getSema().getLangOpts().OpenMP) | ||||||||
7087 | getSema().startOpenMPLoop(); | ||||||||
7088 | |||||||||
7089 | // Transform the initialization statement | ||||||||
7090 | StmtResult Init = getDerived().TransformStmt(S->getInit()); | ||||||||
7091 | if (Init.isInvalid()) | ||||||||
7092 | return StmtError(); | ||||||||
7093 | |||||||||
7094 | // In OpenMP loop region loop control variable must be captured and be | ||||||||
7095 | // private. Perform analysis of first part (if any). | ||||||||
7096 | if (getSema().getLangOpts().OpenMP && Init.isUsable()) | ||||||||
7097 | getSema().ActOnOpenMPLoopInitialization(S->getForLoc(), Init.get()); | ||||||||
7098 | |||||||||
7099 | // Transform the condition | ||||||||
7100 | Sema::ConditionResult Cond = getDerived().TransformCondition( | ||||||||
7101 | S->getForLoc(), S->getConditionVariable(), S->getCond(), | ||||||||
7102 | Sema::ConditionKind::Boolean); | ||||||||
7103 | if (Cond.isInvalid()) | ||||||||
7104 | return StmtError(); | ||||||||
7105 | |||||||||
7106 | // Transform the increment | ||||||||
7107 | ExprResult Inc = getDerived().TransformExpr(S->getInc()); | ||||||||
7108 | if (Inc.isInvalid()) | ||||||||
7109 | return StmtError(); | ||||||||
7110 | |||||||||
7111 | Sema::FullExprArg FullInc(getSema().MakeFullDiscardedValueExpr(Inc.get())); | ||||||||
7112 | if (S->getInc() && !FullInc.get()) | ||||||||
7113 | return StmtError(); | ||||||||
7114 | |||||||||
7115 | // Transform the body | ||||||||
7116 | StmtResult Body = getDerived().TransformStmt(S->getBody()); | ||||||||
7117 | if (Body.isInvalid()) | ||||||||
7118 | return StmtError(); | ||||||||
7119 | |||||||||
7120 | if (!getDerived().AlwaysRebuild() && | ||||||||
7121 | Init.get() == S->getInit() && | ||||||||
7122 | Cond.get() == std::make_pair(S->getConditionVariable(), S->getCond()) && | ||||||||
7123 | Inc.get() == S->getInc() && | ||||||||
7124 | Body.get() == S->getBody()) | ||||||||
7125 | return S; | ||||||||
7126 | |||||||||
7127 | return getDerived().RebuildForStmt(S->getForLoc(), S->getLParenLoc(), | ||||||||
7128 | Init.get(), Cond, FullInc, | ||||||||
7129 | S->getRParenLoc(), Body.get()); | ||||||||
7130 | } | ||||||||
7131 | |||||||||
7132 | template<typename Derived> | ||||||||
7133 | StmtResult | ||||||||
7134 | TreeTransform<Derived>::TransformGotoStmt(GotoStmt *S) { | ||||||||
7135 | Decl *LD = getDerived().TransformDecl(S->getLabel()->getLocation(), | ||||||||
7136 | S->getLabel()); | ||||||||
7137 | if (!LD) | ||||||||
7138 | return StmtError(); | ||||||||
7139 | |||||||||
7140 | // Goto statements must always be rebuilt, to resolve the label. | ||||||||
7141 | return getDerived().RebuildGotoStmt(S->getGotoLoc(), S->getLabelLoc(), | ||||||||
7142 | cast<LabelDecl>(LD)); | ||||||||
7143 | } | ||||||||
7144 | |||||||||
7145 | template<typename Derived> | ||||||||
7146 | StmtResult | ||||||||
7147 | TreeTransform<Derived>::TransformIndirectGotoStmt(IndirectGotoStmt *S) { | ||||||||
7148 | ExprResult Target = getDerived().TransformExpr(S->getTarget()); | ||||||||
7149 | if (Target.isInvalid()) | ||||||||
7150 | return StmtError(); | ||||||||
7151 | Target = SemaRef.MaybeCreateExprWithCleanups(Target.get()); | ||||||||
7152 | |||||||||
7153 | if (!getDerived().AlwaysRebuild() && | ||||||||
7154 | Target.get() == S->getTarget()) | ||||||||
7155 | return S; | ||||||||
7156 | |||||||||
7157 | return getDerived().RebuildIndirectGotoStmt(S->getGotoLoc(), S->getStarLoc(), | ||||||||
7158 | Target.get()); | ||||||||
7159 | } | ||||||||
7160 | |||||||||
7161 | template<typename Derived> | ||||||||
7162 | StmtResult | ||||||||
7163 | TreeTransform<Derived>::TransformContinueStmt(ContinueStmt *S) { | ||||||||
7164 | return S; | ||||||||
7165 | } | ||||||||
7166 | |||||||||
7167 | template<typename Derived> | ||||||||
7168 | StmtResult | ||||||||
7169 | TreeTransform<Derived>::TransformBreakStmt(BreakStmt *S) { | ||||||||
7170 | return S; | ||||||||
7171 | } | ||||||||
7172 | |||||||||
7173 | template<typename Derived> | ||||||||
7174 | StmtResult | ||||||||
7175 | TreeTransform<Derived>::TransformReturnStmt(ReturnStmt *S) { | ||||||||
7176 | ExprResult Result = getDerived().TransformInitializer(S->getRetValue(), | ||||||||
7177 | /*NotCopyInit*/false); | ||||||||
7178 | if (Result.isInvalid()) | ||||||||
7179 | return StmtError(); | ||||||||
7180 | |||||||||
7181 | // FIXME: We always rebuild the return statement because there is no way | ||||||||
7182 | // to tell whether the return type of the function has changed. | ||||||||
7183 | return getDerived().RebuildReturnStmt(S->getReturnLoc(), Result.get()); | ||||||||
7184 | } | ||||||||
7185 | |||||||||
7186 | template<typename Derived> | ||||||||
7187 | StmtResult | ||||||||
7188 | TreeTransform<Derived>::TransformDeclStmt(DeclStmt *S) { | ||||||||
7189 | bool DeclChanged = false; | ||||||||
7190 | SmallVector<Decl *, 4> Decls; | ||||||||
7191 | for (auto *D : S->decls()) { | ||||||||
7192 | Decl *Transformed = getDerived().TransformDefinition(D->getLocation(), D); | ||||||||
7193 | if (!Transformed) | ||||||||
7194 | return StmtError(); | ||||||||
7195 | |||||||||
7196 | if (Transformed != D) | ||||||||
7197 | DeclChanged = true; | ||||||||
7198 | |||||||||
7199 | Decls.push_back(Transformed); | ||||||||
7200 | } | ||||||||
7201 | |||||||||
7202 | if (!getDerived().AlwaysRebuild() && !DeclChanged) | ||||||||
7203 | return S; | ||||||||
7204 | |||||||||
7205 | return getDerived().RebuildDeclStmt(Decls, S->getBeginLoc(), S->getEndLoc()); | ||||||||
7206 | } | ||||||||
7207 | |||||||||
7208 | template<typename Derived> | ||||||||
7209 | StmtResult | ||||||||
7210 | TreeTransform<Derived>::TransformGCCAsmStmt(GCCAsmStmt *S) { | ||||||||
7211 | |||||||||
7212 | SmallVector<Expr*, 8> Constraints; | ||||||||
7213 | SmallVector<Expr*, 8> Exprs; | ||||||||
7214 | SmallVector<IdentifierInfo *, 4> Names; | ||||||||
7215 | |||||||||
7216 | ExprResult AsmString; | ||||||||
7217 | SmallVector<Expr*, 8> Clobbers; | ||||||||
7218 | |||||||||
7219 | bool ExprsChanged = false; | ||||||||
7220 | |||||||||
7221 | // Go through the outputs. | ||||||||
7222 | for (unsigned I = 0, E = S->getNumOutputs(); I != E; ++I) { | ||||||||
7223 | Names.push_back(S->getOutputIdentifier(I)); | ||||||||
7224 | |||||||||
7225 | // No need to transform the constraint literal. | ||||||||
7226 | Constraints.push_back(S->getOutputConstraintLiteral(I)); | ||||||||
7227 | |||||||||
7228 | // Transform the output expr. | ||||||||
7229 | Expr *OutputExpr = S->getOutputExpr(I); | ||||||||
7230 | ExprResult Result = getDerived().TransformExpr(OutputExpr); | ||||||||
7231 | if (Result.isInvalid()) | ||||||||
7232 | return StmtError(); | ||||||||
7233 | |||||||||
7234 | ExprsChanged |= Result.get() != OutputExpr; | ||||||||
7235 | |||||||||
7236 | Exprs.push_back(Result.get()); | ||||||||
7237 | } | ||||||||
7238 | |||||||||
7239 | // Go through the inputs. | ||||||||
7240 | for (unsigned I = 0, E = S->getNumInputs(); I != E; ++I) { | ||||||||
7241 | Names.push_back(S->getInputIdentifier(I)); | ||||||||
7242 | |||||||||
7243 | // No need to transform the constraint literal. | ||||||||
7244 | Constraints.push_back(S->getInputConstraintLiteral(I)); | ||||||||
7245 | |||||||||
7246 | // Transform the input expr. | ||||||||
7247 | Expr *InputExpr = S->getInputExpr(I); | ||||||||
7248 | ExprResult Result = getDerived().TransformExpr(InputExpr); | ||||||||
7249 | if (Result.isInvalid()) | ||||||||
7250 | return StmtError(); | ||||||||
7251 | |||||||||
7252 | ExprsChanged |= Result.get() != InputExpr; | ||||||||
7253 | |||||||||
7254 | Exprs.push_back(Result.get()); | ||||||||
7255 | } | ||||||||
7256 | |||||||||
7257 | // Go through the Labels. | ||||||||
7258 | for (unsigned I = 0, E = S->getNumLabels(); I != E; ++I) { | ||||||||
7259 | Names.push_back(S->getLabelIdentifier(I)); | ||||||||
7260 | |||||||||
7261 | ExprResult Result = getDerived().TransformExpr(S->getLabelExpr(I)); | ||||||||
7262 | if (Result.isInvalid()) | ||||||||
7263 | return StmtError(); | ||||||||
7264 | ExprsChanged |= Result.get() != S->getLabelExpr(I); | ||||||||
7265 | Exprs.push_back(Result.get()); | ||||||||
7266 | } | ||||||||
7267 | if (!getDerived().AlwaysRebuild() && !ExprsChanged) | ||||||||
7268 | return S; | ||||||||
7269 | |||||||||
7270 | // Go through the clobbers. | ||||||||
7271 | for (unsigned I = 0, E = S->getNumClobbers(); I != E; ++I) | ||||||||
7272 | Clobbers.push_back(S->getClobberStringLiteral(I)); | ||||||||
7273 | |||||||||
7274 | // No need to transform the asm string literal. | ||||||||
7275 | AsmString = S->getAsmString(); | ||||||||
7276 | return getDerived().RebuildGCCAsmStmt(S->getAsmLoc(), S->isSimple(), | ||||||||
7277 | S->isVolatile(), S->getNumOutputs(), | ||||||||
7278 | S->getNumInputs(), Names.data(), | ||||||||
7279 | Constraints, Exprs, AsmString.get(), | ||||||||
7280 | Clobbers, S->getNumLabels(), | ||||||||
7281 | S->getRParenLoc()); | ||||||||
7282 | } | ||||||||
7283 | |||||||||
7284 | template<typename Derived> | ||||||||
7285 | StmtResult | ||||||||
7286 | TreeTransform<Derived>::TransformMSAsmStmt(MSAsmStmt *S) { | ||||||||
7287 | ArrayRef<Token> AsmToks = | ||||||||
7288 | llvm::makeArrayRef(S->getAsmToks(), S->getNumAsmToks()); | ||||||||
7289 | |||||||||
7290 | bool HadError = false, HadChange = false; | ||||||||
7291 | |||||||||
7292 | ArrayRef<Expr*> SrcExprs = S->getAllExprs(); | ||||||||
7293 | SmallVector<Expr*, 8> TransformedExprs; | ||||||||
7294 | TransformedExprs.reserve(SrcExprs.size()); | ||||||||
7295 | for (unsigned i = 0, e = SrcExprs.size(); i != e; ++i) { | ||||||||
7296 | ExprResult Result = getDerived().TransformExpr(SrcExprs[i]); | ||||||||
7297 | if (!Result.isUsable()) { | ||||||||
7298 | HadError = true; | ||||||||
7299 | } else { | ||||||||
7300 | HadChange |= (Result.get() != SrcExprs[i]); | ||||||||
7301 | TransformedExprs.push_back(Result.get()); | ||||||||
7302 | } | ||||||||
7303 | } | ||||||||
7304 | |||||||||
7305 | if (HadError) return StmtError(); | ||||||||
7306 | if (!HadChange && !getDerived().AlwaysRebuild()) | ||||||||
7307 | return Owned(S); | ||||||||
7308 | |||||||||
7309 | return getDerived().RebuildMSAsmStmt(S->getAsmLoc(), S->getLBraceLoc(), | ||||||||
7310 | AsmToks, S->getAsmString(), | ||||||||
7311 | S->getNumOutputs(), S->getNumInputs(), | ||||||||
7312 | S->getAllConstraints(), S->getClobbers(), | ||||||||
7313 | TransformedExprs, S->getEndLoc()); | ||||||||
7314 | } | ||||||||
7315 | |||||||||
7316 | // C++ Coroutines TS | ||||||||
7317 | |||||||||
7318 | template<typename Derived> | ||||||||
7319 | StmtResult | ||||||||
7320 | TreeTransform<Derived>::TransformCoroutineBodyStmt(CoroutineBodyStmt *S) { | ||||||||
7321 | auto *ScopeInfo = SemaRef.getCurFunction(); | ||||||||
7322 | auto *FD = cast<FunctionDecl>(SemaRef.CurContext); | ||||||||
7323 | assert(FD && ScopeInfo && !ScopeInfo->CoroutinePromise &&((FD && ScopeInfo && !ScopeInfo->CoroutinePromise && ScopeInfo->NeedsCoroutineSuspends && ScopeInfo ->CoroutineSuspends.first == nullptr && ScopeInfo-> CoroutineSuspends.second == nullptr && "expected clean scope info" ) ? static_cast<void> (0) : __assert_fail ("FD && ScopeInfo && !ScopeInfo->CoroutinePromise && ScopeInfo->NeedsCoroutineSuspends && ScopeInfo->CoroutineSuspends.first == nullptr && ScopeInfo->CoroutineSuspends.second == nullptr && \"expected clean scope info\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 7327, __PRETTY_FUNCTION__)) | ||||||||
7324 | ScopeInfo->NeedsCoroutineSuspends &&((FD && ScopeInfo && !ScopeInfo->CoroutinePromise && ScopeInfo->NeedsCoroutineSuspends && ScopeInfo ->CoroutineSuspends.first == nullptr && ScopeInfo-> CoroutineSuspends.second == nullptr && "expected clean scope info" ) ? static_cast<void> (0) : __assert_fail ("FD && ScopeInfo && !ScopeInfo->CoroutinePromise && ScopeInfo->NeedsCoroutineSuspends && ScopeInfo->CoroutineSuspends.first == nullptr && ScopeInfo->CoroutineSuspends.second == nullptr && \"expected clean scope info\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 7327, __PRETTY_FUNCTION__)) | ||||||||
7325 | ScopeInfo->CoroutineSuspends.first == nullptr &&((FD && ScopeInfo && !ScopeInfo->CoroutinePromise && ScopeInfo->NeedsCoroutineSuspends && ScopeInfo ->CoroutineSuspends.first == nullptr && ScopeInfo-> CoroutineSuspends.second == nullptr && "expected clean scope info" ) ? static_cast<void> (0) : __assert_fail ("FD && ScopeInfo && !ScopeInfo->CoroutinePromise && ScopeInfo->NeedsCoroutineSuspends && ScopeInfo->CoroutineSuspends.first == nullptr && ScopeInfo->CoroutineSuspends.second == nullptr && \"expected clean scope info\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 7327, __PRETTY_FUNCTION__)) | ||||||||
7326 | ScopeInfo->CoroutineSuspends.second == nullptr &&((FD && ScopeInfo && !ScopeInfo->CoroutinePromise && ScopeInfo->NeedsCoroutineSuspends && ScopeInfo ->CoroutineSuspends.first == nullptr && ScopeInfo-> CoroutineSuspends.second == nullptr && "expected clean scope info" ) ? static_cast<void> (0) : __assert_fail ("FD && ScopeInfo && !ScopeInfo->CoroutinePromise && ScopeInfo->NeedsCoroutineSuspends && ScopeInfo->CoroutineSuspends.first == nullptr && ScopeInfo->CoroutineSuspends.second == nullptr && \"expected clean scope info\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 7327, __PRETTY_FUNCTION__)) | ||||||||
7327 | "expected clean scope info")((FD && ScopeInfo && !ScopeInfo->CoroutinePromise && ScopeInfo->NeedsCoroutineSuspends && ScopeInfo ->CoroutineSuspends.first == nullptr && ScopeInfo-> CoroutineSuspends.second == nullptr && "expected clean scope info" ) ? static_cast<void> (0) : __assert_fail ("FD && ScopeInfo && !ScopeInfo->CoroutinePromise && ScopeInfo->NeedsCoroutineSuspends && ScopeInfo->CoroutineSuspends.first == nullptr && ScopeInfo->CoroutineSuspends.second == nullptr && \"expected clean scope info\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 7327, __PRETTY_FUNCTION__)); | ||||||||
7328 | |||||||||
7329 | // Set that we have (possibly-invalid) suspend points before we do anything | ||||||||
7330 | // that may fail. | ||||||||
7331 | ScopeInfo->setNeedsCoroutineSuspends(false); | ||||||||
7332 | |||||||||
7333 | // We re-build the coroutine promise object (and the coroutine parameters its | ||||||||
7334 | // type and constructor depend on) based on the types used in our current | ||||||||
7335 | // function. We must do so, and set it on the current FunctionScopeInfo, | ||||||||
7336 | // before attempting to transform the other parts of the coroutine body | ||||||||
7337 | // statement, such as the implicit suspend statements (because those | ||||||||
7338 | // statements reference the FunctionScopeInfo::CoroutinePromise). | ||||||||
7339 | if (!SemaRef.buildCoroutineParameterMoves(FD->getLocation())) | ||||||||
7340 | return StmtError(); | ||||||||
7341 | auto *Promise = SemaRef.buildCoroutinePromise(FD->getLocation()); | ||||||||
7342 | if (!Promise) | ||||||||
7343 | return StmtError(); | ||||||||
7344 | getDerived().transformedLocalDecl(S->getPromiseDecl(), {Promise}); | ||||||||
7345 | ScopeInfo->CoroutinePromise = Promise; | ||||||||
7346 | |||||||||
7347 | // Transform the implicit coroutine statements constructed using dependent | ||||||||
7348 | // types during the previous parse: initial and final suspensions, the return | ||||||||
7349 | // object, and others. We also transform the coroutine function's body. | ||||||||
7350 | StmtResult InitSuspend = getDerived().TransformStmt(S->getInitSuspendStmt()); | ||||||||
7351 | if (InitSuspend.isInvalid()) | ||||||||
7352 | return StmtError(); | ||||||||
7353 | StmtResult FinalSuspend = | ||||||||
7354 | getDerived().TransformStmt(S->getFinalSuspendStmt()); | ||||||||
7355 | if (FinalSuspend.isInvalid()) | ||||||||
7356 | return StmtError(); | ||||||||
7357 | ScopeInfo->setCoroutineSuspends(InitSuspend.get(), FinalSuspend.get()); | ||||||||
7358 | assert(isa<Expr>(InitSuspend.get()) && isa<Expr>(FinalSuspend.get()))((isa<Expr>(InitSuspend.get()) && isa<Expr> (FinalSuspend.get())) ? static_cast<void> (0) : __assert_fail ("isa<Expr>(InitSuspend.get()) && isa<Expr>(FinalSuspend.get())" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 7358, __PRETTY_FUNCTION__)); | ||||||||
7359 | |||||||||
7360 | StmtResult BodyRes = getDerived().TransformStmt(S->getBody()); | ||||||||
7361 | if (BodyRes.isInvalid()) | ||||||||
7362 | return StmtError(); | ||||||||
7363 | |||||||||
7364 | CoroutineStmtBuilder Builder(SemaRef, *FD, *ScopeInfo, BodyRes.get()); | ||||||||
7365 | if (Builder.isInvalid()) | ||||||||
7366 | return StmtError(); | ||||||||
7367 | |||||||||
7368 | Expr *ReturnObject = S->getReturnValueInit(); | ||||||||
7369 | assert(ReturnObject && "the return object is expected to be valid")((ReturnObject && "the return object is expected to be valid" ) ? static_cast<void> (0) : __assert_fail ("ReturnObject && \"the return object is expected to be valid\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 7369, __PRETTY_FUNCTION__)); | ||||||||
7370 | ExprResult Res = getDerived().TransformInitializer(ReturnObject, | ||||||||
7371 | /*NoCopyInit*/ false); | ||||||||
7372 | if (Res.isInvalid()) | ||||||||
7373 | return StmtError(); | ||||||||
7374 | Builder.ReturnValue = Res.get(); | ||||||||
7375 | |||||||||
7376 | // If during the previous parse the coroutine still had a dependent promise | ||||||||
7377 | // statement, we may need to build some implicit coroutine statements | ||||||||
7378 | // (such as exception and fallthrough handlers) for the first time. | ||||||||
7379 | if (S->hasDependentPromiseType()) { | ||||||||
7380 | // We can only build these statements, however, if the current promise type | ||||||||
7381 | // is not dependent. | ||||||||
7382 | if (!Promise->getType()->isDependentType()) { | ||||||||
7383 | assert(!S->getFallthroughHandler() && !S->getExceptionHandler() &&((!S->getFallthroughHandler() && !S->getExceptionHandler () && !S->getReturnStmtOnAllocFailure() && !S->getDeallocate() && "these nodes should not have been built yet" ) ? static_cast<void> (0) : __assert_fail ("!S->getFallthroughHandler() && !S->getExceptionHandler() && !S->getReturnStmtOnAllocFailure() && !S->getDeallocate() && \"these nodes should not have been built yet\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 7385, __PRETTY_FUNCTION__)) | ||||||||
7384 | !S->getReturnStmtOnAllocFailure() && !S->getDeallocate() &&((!S->getFallthroughHandler() && !S->getExceptionHandler () && !S->getReturnStmtOnAllocFailure() && !S->getDeallocate() && "these nodes should not have been built yet" ) ? static_cast<void> (0) : __assert_fail ("!S->getFallthroughHandler() && !S->getExceptionHandler() && !S->getReturnStmtOnAllocFailure() && !S->getDeallocate() && \"these nodes should not have been built yet\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 7385, __PRETTY_FUNCTION__)) | ||||||||
7385 | "these nodes should not have been built yet")((!S->getFallthroughHandler() && !S->getExceptionHandler () && !S->getReturnStmtOnAllocFailure() && !S->getDeallocate() && "these nodes should not have been built yet" ) ? static_cast<void> (0) : __assert_fail ("!S->getFallthroughHandler() && !S->getExceptionHandler() && !S->getReturnStmtOnAllocFailure() && !S->getDeallocate() && \"these nodes should not have been built yet\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 7385, __PRETTY_FUNCTION__)); | ||||||||
7386 | if (!Builder.buildDependentStatements()) | ||||||||
7387 | return StmtError(); | ||||||||
7388 | } | ||||||||
7389 | } else { | ||||||||
7390 | if (auto *OnFallthrough = S->getFallthroughHandler()) { | ||||||||
7391 | StmtResult Res = getDerived().TransformStmt(OnFallthrough); | ||||||||
7392 | if (Res.isInvalid()) | ||||||||
7393 | return StmtError(); | ||||||||
7394 | Builder.OnFallthrough = Res.get(); | ||||||||
7395 | } | ||||||||
7396 | |||||||||
7397 | if (auto *OnException = S->getExceptionHandler()) { | ||||||||
7398 | StmtResult Res = getDerived().TransformStmt(OnException); | ||||||||
7399 | if (Res.isInvalid()) | ||||||||
7400 | return StmtError(); | ||||||||
7401 | Builder.OnException = Res.get(); | ||||||||
7402 | } | ||||||||
7403 | |||||||||
7404 | if (auto *OnAllocFailure = S->getReturnStmtOnAllocFailure()) { | ||||||||
7405 | StmtResult Res = getDerived().TransformStmt(OnAllocFailure); | ||||||||
7406 | if (Res.isInvalid()) | ||||||||
7407 | return StmtError(); | ||||||||
7408 | Builder.ReturnStmtOnAllocFailure = Res.get(); | ||||||||
7409 | } | ||||||||
7410 | |||||||||
7411 | // Transform any additional statements we may have already built | ||||||||
7412 | assert(S->getAllocate() && S->getDeallocate() &&((S->getAllocate() && S->getDeallocate() && "allocation and deallocation calls must already be built") ? static_cast<void> (0) : __assert_fail ("S->getAllocate() && S->getDeallocate() && \"allocation and deallocation calls must already be built\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 7413, __PRETTY_FUNCTION__)) | ||||||||
7413 | "allocation and deallocation calls must already be built")((S->getAllocate() && S->getDeallocate() && "allocation and deallocation calls must already be built") ? static_cast<void> (0) : __assert_fail ("S->getAllocate() && S->getDeallocate() && \"allocation and deallocation calls must already be built\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 7413, __PRETTY_FUNCTION__)); | ||||||||
7414 | ExprResult AllocRes = getDerived().TransformExpr(S->getAllocate()); | ||||||||
7415 | if (AllocRes.isInvalid()) | ||||||||
7416 | return StmtError(); | ||||||||
7417 | Builder.Allocate = AllocRes.get(); | ||||||||
7418 | |||||||||
7419 | ExprResult DeallocRes = getDerived().TransformExpr(S->getDeallocate()); | ||||||||
7420 | if (DeallocRes.isInvalid()) | ||||||||
7421 | return StmtError(); | ||||||||
7422 | Builder.Deallocate = DeallocRes.get(); | ||||||||
7423 | |||||||||
7424 | assert(S->getResultDecl() && "ResultDecl must already be built")((S->getResultDecl() && "ResultDecl must already be built" ) ? static_cast<void> (0) : __assert_fail ("S->getResultDecl() && \"ResultDecl must already be built\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 7424, __PRETTY_FUNCTION__)); | ||||||||
7425 | StmtResult ResultDecl = getDerived().TransformStmt(S->getResultDecl()); | ||||||||
7426 | if (ResultDecl.isInvalid()) | ||||||||
7427 | return StmtError(); | ||||||||
7428 | Builder.ResultDecl = ResultDecl.get(); | ||||||||
7429 | |||||||||
7430 | if (auto *ReturnStmt = S->getReturnStmt()) { | ||||||||
7431 | StmtResult Res = getDerived().TransformStmt(ReturnStmt); | ||||||||
7432 | if (Res.isInvalid()) | ||||||||
7433 | return StmtError(); | ||||||||
7434 | Builder.ReturnStmt = Res.get(); | ||||||||
7435 | } | ||||||||
7436 | } | ||||||||
7437 | |||||||||
7438 | return getDerived().RebuildCoroutineBodyStmt(Builder); | ||||||||
7439 | } | ||||||||
7440 | |||||||||
7441 | template<typename Derived> | ||||||||
7442 | StmtResult | ||||||||
7443 | TreeTransform<Derived>::TransformCoreturnStmt(CoreturnStmt *S) { | ||||||||
7444 | ExprResult Result = getDerived().TransformInitializer(S->getOperand(), | ||||||||
7445 | /*NotCopyInit*/false); | ||||||||
7446 | if (Result.isInvalid()) | ||||||||
7447 | return StmtError(); | ||||||||
7448 | |||||||||
7449 | // Always rebuild; we don't know if this needs to be injected into a new | ||||||||
7450 | // context or if the promise type has changed. | ||||||||
7451 | return getDerived().RebuildCoreturnStmt(S->getKeywordLoc(), Result.get(), | ||||||||
7452 | S->isImplicit()); | ||||||||
7453 | } | ||||||||
7454 | |||||||||
7455 | template<typename Derived> | ||||||||
7456 | ExprResult | ||||||||
7457 | TreeTransform<Derived>::TransformCoawaitExpr(CoawaitExpr *E) { | ||||||||
7458 | ExprResult Result = getDerived().TransformInitializer(E->getOperand(), | ||||||||
7459 | /*NotCopyInit*/false); | ||||||||
7460 | if (Result.isInvalid()) | ||||||||
7461 | return ExprError(); | ||||||||
7462 | |||||||||
7463 | // Always rebuild; we don't know if this needs to be injected into a new | ||||||||
7464 | // context or if the promise type has changed. | ||||||||
7465 | return getDerived().RebuildCoawaitExpr(E->getKeywordLoc(), Result.get(), | ||||||||
7466 | E->isImplicit()); | ||||||||
7467 | } | ||||||||
7468 | |||||||||
7469 | template <typename Derived> | ||||||||
7470 | ExprResult | ||||||||
7471 | TreeTransform<Derived>::TransformDependentCoawaitExpr(DependentCoawaitExpr *E) { | ||||||||
7472 | ExprResult OperandResult = getDerived().TransformInitializer(E->getOperand(), | ||||||||
7473 | /*NotCopyInit*/ false); | ||||||||
7474 | if (OperandResult.isInvalid()) | ||||||||
7475 | return ExprError(); | ||||||||
7476 | |||||||||
7477 | ExprResult LookupResult = getDerived().TransformUnresolvedLookupExpr( | ||||||||
7478 | E->getOperatorCoawaitLookup()); | ||||||||
7479 | |||||||||
7480 | if (LookupResult.isInvalid()) | ||||||||
7481 | return ExprError(); | ||||||||
7482 | |||||||||
7483 | // Always rebuild; we don't know if this needs to be injected into a new | ||||||||
7484 | // context or if the promise type has changed. | ||||||||
7485 | return getDerived().RebuildDependentCoawaitExpr( | ||||||||
7486 | E->getKeywordLoc(), OperandResult.get(), | ||||||||
7487 | cast<UnresolvedLookupExpr>(LookupResult.get())); | ||||||||
7488 | } | ||||||||
7489 | |||||||||
7490 | template<typename Derived> | ||||||||
7491 | ExprResult | ||||||||
7492 | TreeTransform<Derived>::TransformCoyieldExpr(CoyieldExpr *E) { | ||||||||
7493 | ExprResult Result = getDerived().TransformInitializer(E->getOperand(), | ||||||||
7494 | /*NotCopyInit*/false); | ||||||||
7495 | if (Result.isInvalid()) | ||||||||
7496 | return ExprError(); | ||||||||
7497 | |||||||||
7498 | // Always rebuild; we don't know if this needs to be injected into a new | ||||||||
7499 | // context or if the promise type has changed. | ||||||||
7500 | return getDerived().RebuildCoyieldExpr(E->getKeywordLoc(), Result.get()); | ||||||||
7501 | } | ||||||||
7502 | |||||||||
7503 | // Objective-C Statements. | ||||||||
7504 | |||||||||
7505 | template<typename Derived> | ||||||||
7506 | StmtResult | ||||||||
7507 | TreeTransform<Derived>::TransformObjCAtTryStmt(ObjCAtTryStmt *S) { | ||||||||
7508 | // Transform the body of the @try. | ||||||||
7509 | StmtResult TryBody = getDerived().TransformStmt(S->getTryBody()); | ||||||||
7510 | if (TryBody.isInvalid()) | ||||||||
7511 | return StmtError(); | ||||||||
7512 | |||||||||
7513 | // Transform the @catch statements (if present). | ||||||||
7514 | bool AnyCatchChanged = false; | ||||||||
7515 | SmallVector<Stmt*, 8> CatchStmts; | ||||||||
7516 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { | ||||||||
7517 | StmtResult Catch = getDerived().TransformStmt(S->getCatchStmt(I)); | ||||||||
7518 | if (Catch.isInvalid()) | ||||||||
7519 | return StmtError(); | ||||||||
7520 | if (Catch.get() != S->getCatchStmt(I)) | ||||||||
7521 | AnyCatchChanged = true; | ||||||||
7522 | CatchStmts.push_back(Catch.get()); | ||||||||
7523 | } | ||||||||
7524 | |||||||||
7525 | // Transform the @finally statement (if present). | ||||||||
7526 | StmtResult Finally; | ||||||||
7527 | if (S->getFinallyStmt()) { | ||||||||
7528 | Finally = getDerived().TransformStmt(S->getFinallyStmt()); | ||||||||
7529 | if (Finally.isInvalid()) | ||||||||
7530 | return StmtError(); | ||||||||
7531 | } | ||||||||
7532 | |||||||||
7533 | // If nothing changed, just retain this statement. | ||||||||
7534 | if (!getDerived().AlwaysRebuild() && | ||||||||
7535 | TryBody.get() == S->getTryBody() && | ||||||||
7536 | !AnyCatchChanged && | ||||||||
7537 | Finally.get() == S->getFinallyStmt()) | ||||||||
7538 | return S; | ||||||||
7539 | |||||||||
7540 | // Build a new statement. | ||||||||
7541 | return getDerived().RebuildObjCAtTryStmt(S->getAtTryLoc(), TryBody.get(), | ||||||||
7542 | CatchStmts, Finally.get()); | ||||||||
7543 | } | ||||||||
7544 | |||||||||
7545 | template<typename Derived> | ||||||||
7546 | StmtResult | ||||||||
7547 | TreeTransform<Derived>::TransformObjCAtCatchStmt(ObjCAtCatchStmt *S) { | ||||||||
7548 | // Transform the @catch parameter, if there is one. | ||||||||
7549 | VarDecl *Var = nullptr; | ||||||||
7550 | if (VarDecl *FromVar = S->getCatchParamDecl()) { | ||||||||
7551 | TypeSourceInfo *TSInfo = nullptr; | ||||||||
7552 | if (FromVar->getTypeSourceInfo()) { | ||||||||
7553 | TSInfo = getDerived().TransformType(FromVar->getTypeSourceInfo()); | ||||||||
7554 | if (!TSInfo) | ||||||||
7555 | return StmtError(); | ||||||||
7556 | } | ||||||||
7557 | |||||||||
7558 | QualType T; | ||||||||
7559 | if (TSInfo) | ||||||||
7560 | T = TSInfo->getType(); | ||||||||
7561 | else { | ||||||||
7562 | T = getDerived().TransformType(FromVar->getType()); | ||||||||
7563 | if (T.isNull()) | ||||||||
7564 | return StmtError(); | ||||||||
7565 | } | ||||||||
7566 | |||||||||
7567 | Var = getDerived().RebuildObjCExceptionDecl(FromVar, TSInfo, T); | ||||||||
7568 | if (!Var) | ||||||||
7569 | return StmtError(); | ||||||||
7570 | } | ||||||||
7571 | |||||||||
7572 | StmtResult Body = getDerived().TransformStmt(S->getCatchBody()); | ||||||||
7573 | if (Body.isInvalid()) | ||||||||
7574 | return StmtError(); | ||||||||
7575 | |||||||||
7576 | return getDerived().RebuildObjCAtCatchStmt(S->getAtCatchLoc(), | ||||||||
7577 | S->getRParenLoc(), | ||||||||
7578 | Var, Body.get()); | ||||||||
7579 | } | ||||||||
7580 | |||||||||
7581 | template<typename Derived> | ||||||||
7582 | StmtResult | ||||||||
7583 | TreeTransform<Derived>::TransformObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { | ||||||||
7584 | // Transform the body. | ||||||||
7585 | StmtResult Body = getDerived().TransformStmt(S->getFinallyBody()); | ||||||||
7586 | if (Body.isInvalid()) | ||||||||
7587 | return StmtError(); | ||||||||
7588 | |||||||||
7589 | // If nothing changed, just retain this statement. | ||||||||
7590 | if (!getDerived().AlwaysRebuild() && | ||||||||
7591 | Body.get() == S->getFinallyBody()) | ||||||||
7592 | return S; | ||||||||
7593 | |||||||||
7594 | // Build a new statement. | ||||||||
7595 | return getDerived().RebuildObjCAtFinallyStmt(S->getAtFinallyLoc(), | ||||||||
7596 | Body.get()); | ||||||||
7597 | } | ||||||||
7598 | |||||||||
7599 | template<typename Derived> | ||||||||
7600 | StmtResult | ||||||||
7601 | TreeTransform<Derived>::TransformObjCAtThrowStmt(ObjCAtThrowStmt *S) { | ||||||||
7602 | ExprResult Operand; | ||||||||
7603 | if (S->getThrowExpr()) { | ||||||||
7604 | Operand = getDerived().TransformExpr(S->getThrowExpr()); | ||||||||
7605 | if (Operand.isInvalid()) | ||||||||
7606 | return StmtError(); | ||||||||
7607 | } | ||||||||
7608 | |||||||||
7609 | if (!getDerived().AlwaysRebuild() && | ||||||||
7610 | Operand.get() == S->getThrowExpr()) | ||||||||
7611 | return S; | ||||||||
7612 | |||||||||
7613 | return getDerived().RebuildObjCAtThrowStmt(S->getThrowLoc(), Operand.get()); | ||||||||
7614 | } | ||||||||
7615 | |||||||||
7616 | template<typename Derived> | ||||||||
7617 | StmtResult | ||||||||
7618 | TreeTransform<Derived>::TransformObjCAtSynchronizedStmt( | ||||||||
7619 | ObjCAtSynchronizedStmt *S) { | ||||||||
7620 | // Transform the object we are locking. | ||||||||
7621 | ExprResult Object = getDerived().TransformExpr(S->getSynchExpr()); | ||||||||
7622 | if (Object.isInvalid()) | ||||||||
7623 | return StmtError(); | ||||||||
7624 | Object = | ||||||||
7625 | getDerived().RebuildObjCAtSynchronizedOperand(S->getAtSynchronizedLoc(), | ||||||||
7626 | Object.get()); | ||||||||
7627 | if (Object.isInvalid()) | ||||||||
7628 | return StmtError(); | ||||||||
7629 | |||||||||
7630 | // Transform the body. | ||||||||
7631 | StmtResult Body = getDerived().TransformStmt(S->getSynchBody()); | ||||||||
7632 | if (Body.isInvalid()) | ||||||||
7633 | return StmtError(); | ||||||||
7634 | |||||||||
7635 | // If nothing change, just retain the current statement. | ||||||||
7636 | if (!getDerived().AlwaysRebuild() && | ||||||||
7637 | Object.get() == S->getSynchExpr() && | ||||||||
7638 | Body.get() == S->getSynchBody()) | ||||||||
7639 | return S; | ||||||||
7640 | |||||||||
7641 | // Build a new statement. | ||||||||
7642 | return getDerived().RebuildObjCAtSynchronizedStmt(S->getAtSynchronizedLoc(), | ||||||||
7643 | Object.get(), Body.get()); | ||||||||
7644 | } | ||||||||
7645 | |||||||||
7646 | template<typename Derived> | ||||||||
7647 | StmtResult | ||||||||
7648 | TreeTransform<Derived>::TransformObjCAutoreleasePoolStmt( | ||||||||
7649 | ObjCAutoreleasePoolStmt *S) { | ||||||||
7650 | // Transform the body. | ||||||||
7651 | StmtResult Body = getDerived().TransformStmt(S->getSubStmt()); | ||||||||
7652 | if (Body.isInvalid()) | ||||||||
7653 | return StmtError(); | ||||||||
7654 | |||||||||
7655 | // If nothing changed, just retain this statement. | ||||||||
7656 | if (!getDerived().AlwaysRebuild() && | ||||||||
7657 | Body.get() == S->getSubStmt()) | ||||||||
7658 | return S; | ||||||||
7659 | |||||||||
7660 | // Build a new statement. | ||||||||
7661 | return getDerived().RebuildObjCAutoreleasePoolStmt( | ||||||||
7662 | S->getAtLoc(), Body.get()); | ||||||||
7663 | } | ||||||||
7664 | |||||||||
7665 | template<typename Derived> | ||||||||
7666 | StmtResult | ||||||||
7667 | TreeTransform<Derived>::TransformObjCForCollectionStmt( | ||||||||
7668 | ObjCForCollectionStmt *S) { | ||||||||
7669 | // Transform the element statement. | ||||||||
7670 | StmtResult Element = | ||||||||
7671 | getDerived().TransformStmt(S->getElement(), SDK_NotDiscarded); | ||||||||
7672 | if (Element.isInvalid()) | ||||||||
7673 | return StmtError(); | ||||||||
7674 | |||||||||
7675 | // Transform the collection expression. | ||||||||
7676 | ExprResult Collection = getDerived().TransformExpr(S->getCollection()); | ||||||||
7677 | if (Collection.isInvalid()) | ||||||||
7678 | return StmtError(); | ||||||||
7679 | |||||||||
7680 | // Transform the body. | ||||||||
7681 | StmtResult Body = getDerived().TransformStmt(S->getBody()); | ||||||||
7682 | if (Body.isInvalid()) | ||||||||
7683 | return StmtError(); | ||||||||
7684 | |||||||||
7685 | // If nothing changed, just retain this statement. | ||||||||
7686 | if (!getDerived().AlwaysRebuild() && | ||||||||
7687 | Element.get() == S->getElement() && | ||||||||
7688 | Collection.get() == S->getCollection() && | ||||||||
7689 | Body.get() == S->getBody()) | ||||||||
7690 | return S; | ||||||||
7691 | |||||||||
7692 | // Build a new statement. | ||||||||
7693 | return getDerived().RebuildObjCForCollectionStmt(S->getForLoc(), | ||||||||
7694 | Element.get(), | ||||||||
7695 | Collection.get(), | ||||||||
7696 | S->getRParenLoc(), | ||||||||
7697 | Body.get()); | ||||||||
7698 | } | ||||||||
7699 | |||||||||
7700 | template <typename Derived> | ||||||||
7701 | StmtResult TreeTransform<Derived>::TransformCXXCatchStmt(CXXCatchStmt *S) { | ||||||||
7702 | // Transform the exception declaration, if any. | ||||||||
7703 | VarDecl *Var = nullptr; | ||||||||
7704 | if (VarDecl *ExceptionDecl = S->getExceptionDecl()) { | ||||||||
7705 | TypeSourceInfo *T = | ||||||||
7706 | getDerived().TransformType(ExceptionDecl->getTypeSourceInfo()); | ||||||||
7707 | if (!T) | ||||||||
7708 | return StmtError(); | ||||||||
7709 | |||||||||
7710 | Var = getDerived().RebuildExceptionDecl( | ||||||||
7711 | ExceptionDecl, T, ExceptionDecl->getInnerLocStart(), | ||||||||
7712 | ExceptionDecl->getLocation(), ExceptionDecl->getIdentifier()); | ||||||||
7713 | if (!Var || Var->isInvalidDecl()) | ||||||||
7714 | return StmtError(); | ||||||||
7715 | } | ||||||||
7716 | |||||||||
7717 | // Transform the actual exception handler. | ||||||||
7718 | StmtResult Handler = getDerived().TransformStmt(S->getHandlerBlock()); | ||||||||
7719 | if (Handler.isInvalid()) | ||||||||
7720 | return StmtError(); | ||||||||
7721 | |||||||||
7722 | if (!getDerived().AlwaysRebuild() && !Var && | ||||||||
7723 | Handler.get() == S->getHandlerBlock()) | ||||||||
7724 | return S; | ||||||||
7725 | |||||||||
7726 | return getDerived().RebuildCXXCatchStmt(S->getCatchLoc(), Var, Handler.get()); | ||||||||
7727 | } | ||||||||
7728 | |||||||||
7729 | template <typename Derived> | ||||||||
7730 | StmtResult TreeTransform<Derived>::TransformCXXTryStmt(CXXTryStmt *S) { | ||||||||
7731 | // Transform the try block itself. | ||||||||
7732 | StmtResult TryBlock = getDerived().TransformCompoundStmt(S->getTryBlock()); | ||||||||
7733 | if (TryBlock.isInvalid()) | ||||||||
7734 | return StmtError(); | ||||||||
7735 | |||||||||
7736 | // Transform the handlers. | ||||||||
7737 | bool HandlerChanged = false; | ||||||||
7738 | SmallVector<Stmt *, 8> Handlers; | ||||||||
7739 | for (unsigned I = 0, N = S->getNumHandlers(); I != N; ++I) { | ||||||||
7740 | StmtResult Handler = getDerived().TransformCXXCatchStmt(S->getHandler(I)); | ||||||||
7741 | if (Handler.isInvalid()) | ||||||||
7742 | return StmtError(); | ||||||||
7743 | |||||||||
7744 | HandlerChanged = HandlerChanged || Handler.get() != S->getHandler(I); | ||||||||
7745 | Handlers.push_back(Handler.getAs<Stmt>()); | ||||||||
7746 | } | ||||||||
7747 | |||||||||
7748 | if (!getDerived().AlwaysRebuild() && TryBlock.get() == S->getTryBlock() && | ||||||||
7749 | !HandlerChanged) | ||||||||
7750 | return S; | ||||||||
7751 | |||||||||
7752 | return getDerived().RebuildCXXTryStmt(S->getTryLoc(), TryBlock.get(), | ||||||||
7753 | Handlers); | ||||||||
7754 | } | ||||||||
7755 | |||||||||
7756 | template<typename Derived> | ||||||||
7757 | StmtResult | ||||||||
7758 | TreeTransform<Derived>::TransformCXXForRangeStmt(CXXForRangeStmt *S) { | ||||||||
7759 | StmtResult Init = | ||||||||
7760 | S->getInit() ? getDerived().TransformStmt(S->getInit()) : StmtResult(); | ||||||||
7761 | if (Init.isInvalid()) | ||||||||
7762 | return StmtError(); | ||||||||
7763 | |||||||||
7764 | StmtResult Range = getDerived().TransformStmt(S->getRangeStmt()); | ||||||||
7765 | if (Range.isInvalid()) | ||||||||
7766 | return StmtError(); | ||||||||
7767 | |||||||||
7768 | StmtResult Begin = getDerived().TransformStmt(S->getBeginStmt()); | ||||||||
7769 | if (Begin.isInvalid()) | ||||||||
7770 | return StmtError(); | ||||||||
7771 | StmtResult End = getDerived().TransformStmt(S->getEndStmt()); | ||||||||
7772 | if (End.isInvalid()) | ||||||||
7773 | return StmtError(); | ||||||||
7774 | |||||||||
7775 | ExprResult Cond = getDerived().TransformExpr(S->getCond()); | ||||||||
7776 | if (Cond.isInvalid()) | ||||||||
7777 | return StmtError(); | ||||||||
7778 | if (Cond.get()) | ||||||||
7779 | Cond = SemaRef.CheckBooleanCondition(S->getColonLoc(), Cond.get()); | ||||||||
7780 | if (Cond.isInvalid()) | ||||||||
7781 | return StmtError(); | ||||||||
7782 | if (Cond.get()) | ||||||||
7783 | Cond = SemaRef.MaybeCreateExprWithCleanups(Cond.get()); | ||||||||
7784 | |||||||||
7785 | ExprResult Inc = getDerived().TransformExpr(S->getInc()); | ||||||||
7786 | if (Inc.isInvalid()) | ||||||||
7787 | return StmtError(); | ||||||||
7788 | if (Inc.get()) | ||||||||
7789 | Inc = SemaRef.MaybeCreateExprWithCleanups(Inc.get()); | ||||||||
7790 | |||||||||
7791 | StmtResult LoopVar = getDerived().TransformStmt(S->getLoopVarStmt()); | ||||||||
7792 | if (LoopVar.isInvalid()) | ||||||||
7793 | return StmtError(); | ||||||||
7794 | |||||||||
7795 | StmtResult NewStmt = S; | ||||||||
7796 | if (getDerived().AlwaysRebuild() || | ||||||||
7797 | Init.get() != S->getInit() || | ||||||||
7798 | Range.get() != S->getRangeStmt() || | ||||||||
7799 | Begin.get() != S->getBeginStmt() || | ||||||||
7800 | End.get() != S->getEndStmt() || | ||||||||
7801 | Cond.get() != S->getCond() || | ||||||||
7802 | Inc.get() != S->getInc() || | ||||||||
7803 | LoopVar.get() != S->getLoopVarStmt()) { | ||||||||
7804 | NewStmt = getDerived().RebuildCXXForRangeStmt(S->getForLoc(), | ||||||||
7805 | S->getCoawaitLoc(), Init.get(), | ||||||||
7806 | S->getColonLoc(), Range.get(), | ||||||||
7807 | Begin.get(), End.get(), | ||||||||
7808 | Cond.get(), | ||||||||
7809 | Inc.get(), LoopVar.get(), | ||||||||
7810 | S->getRParenLoc()); | ||||||||
7811 | if (NewStmt.isInvalid()) | ||||||||
7812 | return StmtError(); | ||||||||
7813 | } | ||||||||
7814 | |||||||||
7815 | StmtResult Body = getDerived().TransformStmt(S->getBody()); | ||||||||
7816 | if (Body.isInvalid()) | ||||||||
7817 | return StmtError(); | ||||||||
7818 | |||||||||
7819 | // Body has changed but we didn't rebuild the for-range statement. Rebuild | ||||||||
7820 | // it now so we have a new statement to attach the body to. | ||||||||
7821 | if (Body.get() != S->getBody() && NewStmt.get() == S) { | ||||||||
7822 | NewStmt = getDerived().RebuildCXXForRangeStmt(S->getForLoc(), | ||||||||
7823 | S->getCoawaitLoc(), Init.get(), | ||||||||
7824 | S->getColonLoc(), Range.get(), | ||||||||
7825 | Begin.get(), End.get(), | ||||||||
7826 | Cond.get(), | ||||||||
7827 | Inc.get(), LoopVar.get(), | ||||||||
7828 | S->getRParenLoc()); | ||||||||
7829 | if (NewStmt.isInvalid()) | ||||||||
7830 | return StmtError(); | ||||||||
7831 | } | ||||||||
7832 | |||||||||
7833 | if (NewStmt.get() == S) | ||||||||
7834 | return S; | ||||||||
7835 | |||||||||
7836 | return FinishCXXForRangeStmt(NewStmt.get(), Body.get()); | ||||||||
7837 | } | ||||||||
7838 | |||||||||
7839 | template<typename Derived> | ||||||||
7840 | StmtResult | ||||||||
7841 | TreeTransform<Derived>::TransformMSDependentExistsStmt( | ||||||||
7842 | MSDependentExistsStmt *S) { | ||||||||
7843 | // Transform the nested-name-specifier, if any. | ||||||||
7844 | NestedNameSpecifierLoc QualifierLoc; | ||||||||
7845 | if (S->getQualifierLoc()) { | ||||||||
7846 | QualifierLoc | ||||||||
7847 | = getDerived().TransformNestedNameSpecifierLoc(S->getQualifierLoc()); | ||||||||
7848 | if (!QualifierLoc) | ||||||||
7849 | return StmtError(); | ||||||||
7850 | } | ||||||||
7851 | |||||||||
7852 | // Transform the declaration name. | ||||||||
7853 | DeclarationNameInfo NameInfo = S->getNameInfo(); | ||||||||
7854 | if (NameInfo.getName()) { | ||||||||
7855 | NameInfo = getDerived().TransformDeclarationNameInfo(NameInfo); | ||||||||
7856 | if (!NameInfo.getName()) | ||||||||
7857 | return StmtError(); | ||||||||
7858 | } | ||||||||
7859 | |||||||||
7860 | // Check whether anything changed. | ||||||||
7861 | if (!getDerived().AlwaysRebuild() && | ||||||||
7862 | QualifierLoc == S->getQualifierLoc() && | ||||||||
7863 | NameInfo.getName() == S->getNameInfo().getName()) | ||||||||
7864 | return S; | ||||||||
7865 | |||||||||
7866 | // Determine whether this name exists, if we can. | ||||||||
7867 | CXXScopeSpec SS; | ||||||||
7868 | SS.Adopt(QualifierLoc); | ||||||||
7869 | bool Dependent = false; | ||||||||
7870 | switch (getSema().CheckMicrosoftIfExistsSymbol(/*S=*/nullptr, SS, NameInfo)) { | ||||||||
7871 | case Sema::IER_Exists: | ||||||||
7872 | if (S->isIfExists()) | ||||||||
7873 | break; | ||||||||
7874 | |||||||||
7875 | return new (getSema().Context) NullStmt(S->getKeywordLoc()); | ||||||||
7876 | |||||||||
7877 | case Sema::IER_DoesNotExist: | ||||||||
7878 | if (S->isIfNotExists()) | ||||||||
7879 | break; | ||||||||
7880 | |||||||||
7881 | return new (getSema().Context) NullStmt(S->getKeywordLoc()); | ||||||||
7882 | |||||||||
7883 | case Sema::IER_Dependent: | ||||||||
7884 | Dependent = true; | ||||||||
7885 | break; | ||||||||
7886 | |||||||||
7887 | case Sema::IER_Error: | ||||||||
7888 | return StmtError(); | ||||||||
7889 | } | ||||||||
7890 | |||||||||
7891 | // We need to continue with the instantiation, so do so now. | ||||||||
7892 | StmtResult SubStmt = getDerived().TransformCompoundStmt(S->getSubStmt()); | ||||||||
7893 | if (SubStmt.isInvalid()) | ||||||||
7894 | return StmtError(); | ||||||||
7895 | |||||||||
7896 | // If we have resolved the name, just transform to the substatement. | ||||||||
7897 | if (!Dependent) | ||||||||
7898 | return SubStmt; | ||||||||
7899 | |||||||||
7900 | // The name is still dependent, so build a dependent expression again. | ||||||||
7901 | return getDerived().RebuildMSDependentExistsStmt(S->getKeywordLoc(), | ||||||||
7902 | S->isIfExists(), | ||||||||
7903 | QualifierLoc, | ||||||||
7904 | NameInfo, | ||||||||
7905 | SubStmt.get()); | ||||||||
7906 | } | ||||||||
7907 | |||||||||
7908 | template<typename Derived> | ||||||||
7909 | ExprResult | ||||||||
7910 | TreeTransform<Derived>::TransformMSPropertyRefExpr(MSPropertyRefExpr *E) { | ||||||||
7911 | NestedNameSpecifierLoc QualifierLoc; | ||||||||
7912 | if (E->getQualifierLoc()) { | ||||||||
7913 | QualifierLoc | ||||||||
7914 | = getDerived().TransformNestedNameSpecifierLoc(E->getQualifierLoc()); | ||||||||
7915 | if (!QualifierLoc) | ||||||||
7916 | return ExprError(); | ||||||||
7917 | } | ||||||||
7918 | |||||||||
7919 | MSPropertyDecl *PD = cast_or_null<MSPropertyDecl>( | ||||||||
7920 | getDerived().TransformDecl(E->getMemberLoc(), E->getPropertyDecl())); | ||||||||
7921 | if (!PD) | ||||||||
7922 | return ExprError(); | ||||||||
7923 | |||||||||
7924 | ExprResult Base = getDerived().TransformExpr(E->getBaseExpr()); | ||||||||
7925 | if (Base.isInvalid()) | ||||||||
7926 | return ExprError(); | ||||||||
7927 | |||||||||
7928 | return new (SemaRef.getASTContext()) | ||||||||
7929 | MSPropertyRefExpr(Base.get(), PD, E->isArrow(), | ||||||||
7930 | SemaRef.getASTContext().PseudoObjectTy, VK_LValue, | ||||||||
7931 | QualifierLoc, E->getMemberLoc()); | ||||||||
7932 | } | ||||||||
7933 | |||||||||
7934 | template <typename Derived> | ||||||||
7935 | ExprResult TreeTransform<Derived>::TransformMSPropertySubscriptExpr( | ||||||||
7936 | MSPropertySubscriptExpr *E) { | ||||||||
7937 | auto BaseRes = getDerived().TransformExpr(E->getBase()); | ||||||||
7938 | if (BaseRes.isInvalid()) | ||||||||
7939 | return ExprError(); | ||||||||
7940 | auto IdxRes = getDerived().TransformExpr(E->getIdx()); | ||||||||
7941 | if (IdxRes.isInvalid()) | ||||||||
7942 | return ExprError(); | ||||||||
7943 | |||||||||
7944 | if (!getDerived().AlwaysRebuild() && | ||||||||
7945 | BaseRes.get() == E->getBase() && | ||||||||
7946 | IdxRes.get() == E->getIdx()) | ||||||||
7947 | return E; | ||||||||
7948 | |||||||||
7949 | return getDerived().RebuildArraySubscriptExpr( | ||||||||
7950 | BaseRes.get(), SourceLocation(), IdxRes.get(), E->getRBracketLoc()); | ||||||||
7951 | } | ||||||||
7952 | |||||||||
7953 | template <typename Derived> | ||||||||
7954 | StmtResult TreeTransform<Derived>::TransformSEHTryStmt(SEHTryStmt *S) { | ||||||||
7955 | StmtResult TryBlock = getDerived().TransformCompoundStmt(S->getTryBlock()); | ||||||||
7956 | if (TryBlock.isInvalid()) | ||||||||
7957 | return StmtError(); | ||||||||
7958 | |||||||||
7959 | StmtResult Handler = getDerived().TransformSEHHandler(S->getHandler()); | ||||||||
7960 | if (Handler.isInvalid()) | ||||||||
7961 | return StmtError(); | ||||||||
7962 | |||||||||
7963 | if (!getDerived().AlwaysRebuild() && TryBlock.get() == S->getTryBlock() && | ||||||||
7964 | Handler.get() == S->getHandler()) | ||||||||
7965 | return S; | ||||||||
7966 | |||||||||
7967 | return getDerived().RebuildSEHTryStmt(S->getIsCXXTry(), S->getTryLoc(), | ||||||||
7968 | TryBlock.get(), Handler.get()); | ||||||||
7969 | } | ||||||||
7970 | |||||||||
7971 | template <typename Derived> | ||||||||
7972 | StmtResult TreeTransform<Derived>::TransformSEHFinallyStmt(SEHFinallyStmt *S) { | ||||||||
7973 | StmtResult Block = getDerived().TransformCompoundStmt(S->getBlock()); | ||||||||
7974 | if (Block.isInvalid()) | ||||||||
7975 | return StmtError(); | ||||||||
7976 | |||||||||
7977 | return getDerived().RebuildSEHFinallyStmt(S->getFinallyLoc(), Block.get()); | ||||||||
7978 | } | ||||||||
7979 | |||||||||
7980 | template <typename Derived> | ||||||||
7981 | StmtResult TreeTransform<Derived>::TransformSEHExceptStmt(SEHExceptStmt *S) { | ||||||||
7982 | ExprResult FilterExpr = getDerived().TransformExpr(S->getFilterExpr()); | ||||||||
7983 | if (FilterExpr.isInvalid()) | ||||||||
7984 | return StmtError(); | ||||||||
7985 | |||||||||
7986 | StmtResult Block = getDerived().TransformCompoundStmt(S->getBlock()); | ||||||||
7987 | if (Block.isInvalid()) | ||||||||
7988 | return StmtError(); | ||||||||
7989 | |||||||||
7990 | return getDerived().RebuildSEHExceptStmt(S->getExceptLoc(), FilterExpr.get(), | ||||||||
7991 | Block.get()); | ||||||||
7992 | } | ||||||||
7993 | |||||||||
7994 | template <typename Derived> | ||||||||
7995 | StmtResult TreeTransform<Derived>::TransformSEHHandler(Stmt *Handler) { | ||||||||
7996 | if (isa<SEHFinallyStmt>(Handler)) | ||||||||
7997 | return getDerived().TransformSEHFinallyStmt(cast<SEHFinallyStmt>(Handler)); | ||||||||
7998 | else | ||||||||
7999 | return getDerived().TransformSEHExceptStmt(cast<SEHExceptStmt>(Handler)); | ||||||||
8000 | } | ||||||||
8001 | |||||||||
8002 | template<typename Derived> | ||||||||
8003 | StmtResult | ||||||||
8004 | TreeTransform<Derived>::TransformSEHLeaveStmt(SEHLeaveStmt *S) { | ||||||||
8005 | return S; | ||||||||
8006 | } | ||||||||
8007 | |||||||||
8008 | //===----------------------------------------------------------------------===// | ||||||||
8009 | // OpenMP directive transformation | ||||||||
8010 | //===----------------------------------------------------------------------===// | ||||||||
8011 | template <typename Derived> | ||||||||
8012 | StmtResult TreeTransform<Derived>::TransformOMPExecutableDirective( | ||||||||
8013 | OMPExecutableDirective *D) { | ||||||||
8014 | |||||||||
8015 | // Transform the clauses | ||||||||
8016 | llvm::SmallVector<OMPClause *, 16> TClauses; | ||||||||
8017 | ArrayRef<OMPClause *> Clauses = D->clauses(); | ||||||||
8018 | TClauses.reserve(Clauses.size()); | ||||||||
8019 | for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end(); | ||||||||
8020 | I != E; ++I) { | ||||||||
8021 | if (*I) { | ||||||||
8022 | getDerived().getSema().StartOpenMPClause((*I)->getClauseKind()); | ||||||||
8023 | OMPClause *Clause = getDerived().TransformOMPClause(*I); | ||||||||
8024 | getDerived().getSema().EndOpenMPClause(); | ||||||||
8025 | if (Clause) | ||||||||
8026 | TClauses.push_back(Clause); | ||||||||
8027 | } else { | ||||||||
8028 | TClauses.push_back(nullptr); | ||||||||
8029 | } | ||||||||
8030 | } | ||||||||
8031 | StmtResult AssociatedStmt; | ||||||||
8032 | if (D->hasAssociatedStmt() && D->getAssociatedStmt()) { | ||||||||
8033 | getDerived().getSema().ActOnOpenMPRegionStart(D->getDirectiveKind(), | ||||||||
8034 | /*CurScope=*/nullptr); | ||||||||
8035 | StmtResult Body; | ||||||||
8036 | { | ||||||||
8037 | Sema::CompoundScopeRAII CompoundScope(getSema()); | ||||||||
8038 | Stmt *CS = D->getInnermostCapturedStmt()->getCapturedStmt(); | ||||||||
8039 | Body = getDerived().TransformStmt(CS); | ||||||||
8040 | } | ||||||||
8041 | AssociatedStmt = | ||||||||
8042 | getDerived().getSema().ActOnOpenMPRegionEnd(Body, TClauses); | ||||||||
8043 | if (AssociatedStmt.isInvalid()) { | ||||||||
8044 | return StmtError(); | ||||||||
8045 | } | ||||||||
8046 | } | ||||||||
8047 | if (TClauses.size() != Clauses.size()) { | ||||||||
8048 | return StmtError(); | ||||||||
8049 | } | ||||||||
8050 | |||||||||
8051 | // Transform directive name for 'omp critical' directive. | ||||||||
8052 | DeclarationNameInfo DirName; | ||||||||
8053 | if (D->getDirectiveKind() == OMPD_critical) { | ||||||||
8054 | DirName = cast<OMPCriticalDirective>(D)->getDirectiveName(); | ||||||||
8055 | DirName = getDerived().TransformDeclarationNameInfo(DirName); | ||||||||
8056 | } | ||||||||
8057 | OpenMPDirectiveKind CancelRegion = OMPD_unknown; | ||||||||
8058 | if (D->getDirectiveKind() == OMPD_cancellation_point) { | ||||||||
8059 | CancelRegion = cast<OMPCancellationPointDirective>(D)->getCancelRegion(); | ||||||||
8060 | } else if (D->getDirectiveKind() == OMPD_cancel) { | ||||||||
8061 | CancelRegion = cast<OMPCancelDirective>(D)->getCancelRegion(); | ||||||||
8062 | } | ||||||||
8063 | |||||||||
8064 | return getDerived().RebuildOMPExecutableDirective( | ||||||||
8065 | D->getDirectiveKind(), DirName, CancelRegion, TClauses, | ||||||||
8066 | AssociatedStmt.get(), D->getBeginLoc(), D->getEndLoc()); | ||||||||
8067 | } | ||||||||
8068 | |||||||||
8069 | template <typename Derived> | ||||||||
8070 | StmtResult | ||||||||
8071 | TreeTransform<Derived>::TransformOMPParallelDirective(OMPParallelDirective *D) { | ||||||||
8072 | DeclarationNameInfo DirName; | ||||||||
8073 | getDerived().getSema().StartOpenMPDSABlock(OMPD_parallel, DirName, nullptr, | ||||||||
8074 | D->getBeginLoc()); | ||||||||
8075 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8076 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8077 | return Res; | ||||||||
8078 | } | ||||||||
8079 | |||||||||
8080 | template <typename Derived> | ||||||||
8081 | StmtResult | ||||||||
8082 | TreeTransform<Derived>::TransformOMPSimdDirective(OMPSimdDirective *D) { | ||||||||
8083 | DeclarationNameInfo DirName; | ||||||||
8084 | getDerived().getSema().StartOpenMPDSABlock(OMPD_simd, DirName, nullptr, | ||||||||
8085 | D->getBeginLoc()); | ||||||||
8086 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8087 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8088 | return Res; | ||||||||
8089 | } | ||||||||
8090 | |||||||||
8091 | template <typename Derived> | ||||||||
8092 | StmtResult | ||||||||
8093 | TreeTransform<Derived>::TransformOMPForDirective(OMPForDirective *D) { | ||||||||
8094 | DeclarationNameInfo DirName; | ||||||||
8095 | getDerived().getSema().StartOpenMPDSABlock(OMPD_for, DirName, nullptr, | ||||||||
8096 | D->getBeginLoc()); | ||||||||
8097 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8098 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8099 | return Res; | ||||||||
8100 | } | ||||||||
8101 | |||||||||
8102 | template <typename Derived> | ||||||||
8103 | StmtResult | ||||||||
8104 | TreeTransform<Derived>::TransformOMPForSimdDirective(OMPForSimdDirective *D) { | ||||||||
8105 | DeclarationNameInfo DirName; | ||||||||
8106 | getDerived().getSema().StartOpenMPDSABlock(OMPD_for_simd, DirName, nullptr, | ||||||||
8107 | D->getBeginLoc()); | ||||||||
8108 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8109 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8110 | return Res; | ||||||||
8111 | } | ||||||||
8112 | |||||||||
8113 | template <typename Derived> | ||||||||
8114 | StmtResult | ||||||||
8115 | TreeTransform<Derived>::TransformOMPSectionsDirective(OMPSectionsDirective *D) { | ||||||||
8116 | DeclarationNameInfo DirName; | ||||||||
8117 | getDerived().getSema().StartOpenMPDSABlock(OMPD_sections, DirName, nullptr, | ||||||||
8118 | D->getBeginLoc()); | ||||||||
8119 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8120 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8121 | return Res; | ||||||||
8122 | } | ||||||||
8123 | |||||||||
8124 | template <typename Derived> | ||||||||
8125 | StmtResult | ||||||||
8126 | TreeTransform<Derived>::TransformOMPSectionDirective(OMPSectionDirective *D) { | ||||||||
8127 | DeclarationNameInfo DirName; | ||||||||
8128 | getDerived().getSema().StartOpenMPDSABlock(OMPD_section, DirName, nullptr, | ||||||||
8129 | D->getBeginLoc()); | ||||||||
8130 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8131 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8132 | return Res; | ||||||||
8133 | } | ||||||||
8134 | |||||||||
8135 | template <typename Derived> | ||||||||
8136 | StmtResult | ||||||||
8137 | TreeTransform<Derived>::TransformOMPSingleDirective(OMPSingleDirective *D) { | ||||||||
8138 | DeclarationNameInfo DirName; | ||||||||
8139 | getDerived().getSema().StartOpenMPDSABlock(OMPD_single, DirName, nullptr, | ||||||||
8140 | D->getBeginLoc()); | ||||||||
8141 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8142 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8143 | return Res; | ||||||||
8144 | } | ||||||||
8145 | |||||||||
8146 | template <typename Derived> | ||||||||
8147 | StmtResult | ||||||||
8148 | TreeTransform<Derived>::TransformOMPMasterDirective(OMPMasterDirective *D) { | ||||||||
8149 | DeclarationNameInfo DirName; | ||||||||
8150 | getDerived().getSema().StartOpenMPDSABlock(OMPD_master, DirName, nullptr, | ||||||||
8151 | D->getBeginLoc()); | ||||||||
8152 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8153 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8154 | return Res; | ||||||||
8155 | } | ||||||||
8156 | |||||||||
8157 | template <typename Derived> | ||||||||
8158 | StmtResult | ||||||||
8159 | TreeTransform<Derived>::TransformOMPCriticalDirective(OMPCriticalDirective *D) { | ||||||||
8160 | getDerived().getSema().StartOpenMPDSABlock( | ||||||||
8161 | OMPD_critical, D->getDirectiveName(), nullptr, D->getBeginLoc()); | ||||||||
8162 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8163 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8164 | return Res; | ||||||||
8165 | } | ||||||||
8166 | |||||||||
8167 | template <typename Derived> | ||||||||
8168 | StmtResult TreeTransform<Derived>::TransformOMPParallelForDirective( | ||||||||
8169 | OMPParallelForDirective *D) { | ||||||||
8170 | DeclarationNameInfo DirName; | ||||||||
8171 | getDerived().getSema().StartOpenMPDSABlock(OMPD_parallel_for, DirName, | ||||||||
8172 | nullptr, D->getBeginLoc()); | ||||||||
8173 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8174 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8175 | return Res; | ||||||||
8176 | } | ||||||||
8177 | |||||||||
8178 | template <typename Derived> | ||||||||
8179 | StmtResult TreeTransform<Derived>::TransformOMPParallelForSimdDirective( | ||||||||
8180 | OMPParallelForSimdDirective *D) { | ||||||||
8181 | DeclarationNameInfo DirName; | ||||||||
8182 | getDerived().getSema().StartOpenMPDSABlock(OMPD_parallel_for_simd, DirName, | ||||||||
8183 | nullptr, D->getBeginLoc()); | ||||||||
8184 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8185 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8186 | return Res; | ||||||||
8187 | } | ||||||||
8188 | |||||||||
8189 | template <typename Derived> | ||||||||
8190 | StmtResult TreeTransform<Derived>::TransformOMPParallelMasterDirective( | ||||||||
8191 | OMPParallelMasterDirective *D) { | ||||||||
8192 | DeclarationNameInfo DirName; | ||||||||
8193 | getDerived().getSema().StartOpenMPDSABlock(OMPD_parallel_master, DirName, | ||||||||
8194 | nullptr, D->getBeginLoc()); | ||||||||
8195 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8196 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8197 | return Res; | ||||||||
8198 | } | ||||||||
8199 | |||||||||
8200 | template <typename Derived> | ||||||||
8201 | StmtResult TreeTransform<Derived>::TransformOMPParallelSectionsDirective( | ||||||||
8202 | OMPParallelSectionsDirective *D) { | ||||||||
8203 | DeclarationNameInfo DirName; | ||||||||
8204 | getDerived().getSema().StartOpenMPDSABlock(OMPD_parallel_sections, DirName, | ||||||||
8205 | nullptr, D->getBeginLoc()); | ||||||||
8206 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8207 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8208 | return Res; | ||||||||
8209 | } | ||||||||
8210 | |||||||||
8211 | template <typename Derived> | ||||||||
8212 | StmtResult | ||||||||
8213 | TreeTransform<Derived>::TransformOMPTaskDirective(OMPTaskDirective *D) { | ||||||||
8214 | DeclarationNameInfo DirName; | ||||||||
8215 | getDerived().getSema().StartOpenMPDSABlock(OMPD_task, DirName, nullptr, | ||||||||
8216 | D->getBeginLoc()); | ||||||||
8217 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8218 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8219 | return Res; | ||||||||
8220 | } | ||||||||
8221 | |||||||||
8222 | template <typename Derived> | ||||||||
8223 | StmtResult TreeTransform<Derived>::TransformOMPTaskyieldDirective( | ||||||||
8224 | OMPTaskyieldDirective *D) { | ||||||||
8225 | DeclarationNameInfo DirName; | ||||||||
8226 | getDerived().getSema().StartOpenMPDSABlock(OMPD_taskyield, DirName, nullptr, | ||||||||
8227 | D->getBeginLoc()); | ||||||||
8228 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8229 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8230 | return Res; | ||||||||
8231 | } | ||||||||
8232 | |||||||||
8233 | template <typename Derived> | ||||||||
8234 | StmtResult | ||||||||
8235 | TreeTransform<Derived>::TransformOMPBarrierDirective(OMPBarrierDirective *D) { | ||||||||
8236 | DeclarationNameInfo DirName; | ||||||||
8237 | getDerived().getSema().StartOpenMPDSABlock(OMPD_barrier, DirName, nullptr, | ||||||||
8238 | D->getBeginLoc()); | ||||||||
8239 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8240 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8241 | return Res; | ||||||||
8242 | } | ||||||||
8243 | |||||||||
8244 | template <typename Derived> | ||||||||
8245 | StmtResult | ||||||||
8246 | TreeTransform<Derived>::TransformOMPTaskwaitDirective(OMPTaskwaitDirective *D) { | ||||||||
8247 | DeclarationNameInfo DirName; | ||||||||
8248 | getDerived().getSema().StartOpenMPDSABlock(OMPD_taskwait, DirName, nullptr, | ||||||||
8249 | D->getBeginLoc()); | ||||||||
8250 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8251 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8252 | return Res; | ||||||||
8253 | } | ||||||||
8254 | |||||||||
8255 | template <typename Derived> | ||||||||
8256 | StmtResult TreeTransform<Derived>::TransformOMPTaskgroupDirective( | ||||||||
8257 | OMPTaskgroupDirective *D) { | ||||||||
8258 | DeclarationNameInfo DirName; | ||||||||
8259 | getDerived().getSema().StartOpenMPDSABlock(OMPD_taskgroup, DirName, nullptr, | ||||||||
8260 | D->getBeginLoc()); | ||||||||
8261 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8262 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8263 | return Res; | ||||||||
8264 | } | ||||||||
8265 | |||||||||
8266 | template <typename Derived> | ||||||||
8267 | StmtResult | ||||||||
8268 | TreeTransform<Derived>::TransformOMPFlushDirective(OMPFlushDirective *D) { | ||||||||
8269 | DeclarationNameInfo DirName; | ||||||||
8270 | getDerived().getSema().StartOpenMPDSABlock(OMPD_flush, DirName, nullptr, | ||||||||
8271 | D->getBeginLoc()); | ||||||||
8272 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8273 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8274 | return Res; | ||||||||
8275 | } | ||||||||
8276 | |||||||||
8277 | template <typename Derived> | ||||||||
8278 | StmtResult | ||||||||
8279 | TreeTransform<Derived>::TransformOMPDepobjDirective(OMPDepobjDirective *D) { | ||||||||
8280 | DeclarationNameInfo DirName; | ||||||||
8281 | getDerived().getSema().StartOpenMPDSABlock(OMPD_depobj, DirName, nullptr, | ||||||||
8282 | D->getBeginLoc()); | ||||||||
8283 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8284 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8285 | return Res; | ||||||||
8286 | } | ||||||||
8287 | |||||||||
8288 | template <typename Derived> | ||||||||
8289 | StmtResult | ||||||||
8290 | TreeTransform<Derived>::TransformOMPOrderedDirective(OMPOrderedDirective *D) { | ||||||||
8291 | DeclarationNameInfo DirName; | ||||||||
8292 | getDerived().getSema().StartOpenMPDSABlock(OMPD_ordered, DirName, nullptr, | ||||||||
8293 | D->getBeginLoc()); | ||||||||
8294 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8295 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8296 | return Res; | ||||||||
8297 | } | ||||||||
8298 | |||||||||
8299 | template <typename Derived> | ||||||||
8300 | StmtResult | ||||||||
8301 | TreeTransform<Derived>::TransformOMPAtomicDirective(OMPAtomicDirective *D) { | ||||||||
8302 | DeclarationNameInfo DirName; | ||||||||
8303 | getDerived().getSema().StartOpenMPDSABlock(OMPD_atomic, DirName, nullptr, | ||||||||
8304 | D->getBeginLoc()); | ||||||||
8305 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8306 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8307 | return Res; | ||||||||
8308 | } | ||||||||
8309 | |||||||||
8310 | template <typename Derived> | ||||||||
8311 | StmtResult | ||||||||
8312 | TreeTransform<Derived>::TransformOMPTargetDirective(OMPTargetDirective *D) { | ||||||||
8313 | DeclarationNameInfo DirName; | ||||||||
8314 | getDerived().getSema().StartOpenMPDSABlock(OMPD_target, DirName, nullptr, | ||||||||
8315 | D->getBeginLoc()); | ||||||||
8316 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8317 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8318 | return Res; | ||||||||
8319 | } | ||||||||
8320 | |||||||||
8321 | template <typename Derived> | ||||||||
8322 | StmtResult TreeTransform<Derived>::TransformOMPTargetDataDirective( | ||||||||
8323 | OMPTargetDataDirective *D) { | ||||||||
8324 | DeclarationNameInfo DirName; | ||||||||
8325 | getDerived().getSema().StartOpenMPDSABlock(OMPD_target_data, DirName, nullptr, | ||||||||
8326 | D->getBeginLoc()); | ||||||||
8327 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8328 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8329 | return Res; | ||||||||
8330 | } | ||||||||
8331 | |||||||||
8332 | template <typename Derived> | ||||||||
8333 | StmtResult TreeTransform<Derived>::TransformOMPTargetEnterDataDirective( | ||||||||
8334 | OMPTargetEnterDataDirective *D) { | ||||||||
8335 | DeclarationNameInfo DirName; | ||||||||
8336 | getDerived().getSema().StartOpenMPDSABlock(OMPD_target_enter_data, DirName, | ||||||||
8337 | nullptr, D->getBeginLoc()); | ||||||||
8338 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8339 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8340 | return Res; | ||||||||
8341 | } | ||||||||
8342 | |||||||||
8343 | template <typename Derived> | ||||||||
8344 | StmtResult TreeTransform<Derived>::TransformOMPTargetExitDataDirective( | ||||||||
8345 | OMPTargetExitDataDirective *D) { | ||||||||
8346 | DeclarationNameInfo DirName; | ||||||||
8347 | getDerived().getSema().StartOpenMPDSABlock(OMPD_target_exit_data, DirName, | ||||||||
8348 | nullptr, D->getBeginLoc()); | ||||||||
8349 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8350 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8351 | return Res; | ||||||||
8352 | } | ||||||||
8353 | |||||||||
8354 | template <typename Derived> | ||||||||
8355 | StmtResult TreeTransform<Derived>::TransformOMPTargetParallelDirective( | ||||||||
8356 | OMPTargetParallelDirective *D) { | ||||||||
8357 | DeclarationNameInfo DirName; | ||||||||
8358 | getDerived().getSema().StartOpenMPDSABlock(OMPD_target_parallel, DirName, | ||||||||
8359 | nullptr, D->getBeginLoc()); | ||||||||
8360 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8361 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8362 | return Res; | ||||||||
8363 | } | ||||||||
8364 | |||||||||
8365 | template <typename Derived> | ||||||||
8366 | StmtResult TreeTransform<Derived>::TransformOMPTargetParallelForDirective( | ||||||||
8367 | OMPTargetParallelForDirective *D) { | ||||||||
8368 | DeclarationNameInfo DirName; | ||||||||
8369 | getDerived().getSema().StartOpenMPDSABlock(OMPD_target_parallel_for, DirName, | ||||||||
8370 | nullptr, D->getBeginLoc()); | ||||||||
8371 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8372 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8373 | return Res; | ||||||||
8374 | } | ||||||||
8375 | |||||||||
8376 | template <typename Derived> | ||||||||
8377 | StmtResult TreeTransform<Derived>::TransformOMPTargetUpdateDirective( | ||||||||
8378 | OMPTargetUpdateDirective *D) { | ||||||||
8379 | DeclarationNameInfo DirName; | ||||||||
8380 | getDerived().getSema().StartOpenMPDSABlock(OMPD_target_update, DirName, | ||||||||
8381 | nullptr, D->getBeginLoc()); | ||||||||
8382 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8383 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8384 | return Res; | ||||||||
8385 | } | ||||||||
8386 | |||||||||
8387 | template <typename Derived> | ||||||||
8388 | StmtResult | ||||||||
8389 | TreeTransform<Derived>::TransformOMPTeamsDirective(OMPTeamsDirective *D) { | ||||||||
8390 | DeclarationNameInfo DirName; | ||||||||
8391 | getDerived().getSema().StartOpenMPDSABlock(OMPD_teams, DirName, nullptr, | ||||||||
8392 | D->getBeginLoc()); | ||||||||
8393 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8394 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8395 | return Res; | ||||||||
8396 | } | ||||||||
8397 | |||||||||
8398 | template <typename Derived> | ||||||||
8399 | StmtResult TreeTransform<Derived>::TransformOMPCancellationPointDirective( | ||||||||
8400 | OMPCancellationPointDirective *D) { | ||||||||
8401 | DeclarationNameInfo DirName; | ||||||||
8402 | getDerived().getSema().StartOpenMPDSABlock(OMPD_cancellation_point, DirName, | ||||||||
8403 | nullptr, D->getBeginLoc()); | ||||||||
8404 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8405 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8406 | return Res; | ||||||||
8407 | } | ||||||||
8408 | |||||||||
8409 | template <typename Derived> | ||||||||
8410 | StmtResult | ||||||||
8411 | TreeTransform<Derived>::TransformOMPCancelDirective(OMPCancelDirective *D) { | ||||||||
8412 | DeclarationNameInfo DirName; | ||||||||
8413 | getDerived().getSema().StartOpenMPDSABlock(OMPD_cancel, DirName, nullptr, | ||||||||
8414 | D->getBeginLoc()); | ||||||||
8415 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8416 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8417 | return Res; | ||||||||
8418 | } | ||||||||
8419 | |||||||||
8420 | template <typename Derived> | ||||||||
8421 | StmtResult | ||||||||
8422 | TreeTransform<Derived>::TransformOMPTaskLoopDirective(OMPTaskLoopDirective *D) { | ||||||||
8423 | DeclarationNameInfo DirName; | ||||||||
8424 | getDerived().getSema().StartOpenMPDSABlock(OMPD_taskloop, DirName, nullptr, | ||||||||
8425 | D->getBeginLoc()); | ||||||||
8426 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8427 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8428 | return Res; | ||||||||
8429 | } | ||||||||
8430 | |||||||||
8431 | template <typename Derived> | ||||||||
8432 | StmtResult TreeTransform<Derived>::TransformOMPTaskLoopSimdDirective( | ||||||||
8433 | OMPTaskLoopSimdDirective *D) { | ||||||||
8434 | DeclarationNameInfo DirName; | ||||||||
8435 | getDerived().getSema().StartOpenMPDSABlock(OMPD_taskloop_simd, DirName, | ||||||||
8436 | nullptr, D->getBeginLoc()); | ||||||||
8437 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8438 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8439 | return Res; | ||||||||
8440 | } | ||||||||
8441 | |||||||||
8442 | template <typename Derived> | ||||||||
8443 | StmtResult TreeTransform<Derived>::TransformOMPMasterTaskLoopDirective( | ||||||||
8444 | OMPMasterTaskLoopDirective *D) { | ||||||||
8445 | DeclarationNameInfo DirName; | ||||||||
8446 | getDerived().getSema().StartOpenMPDSABlock(OMPD_master_taskloop, DirName, | ||||||||
8447 | nullptr, D->getBeginLoc()); | ||||||||
8448 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8449 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8450 | return Res; | ||||||||
8451 | } | ||||||||
8452 | |||||||||
8453 | template <typename Derived> | ||||||||
8454 | StmtResult TreeTransform<Derived>::TransformOMPMasterTaskLoopSimdDirective( | ||||||||
8455 | OMPMasterTaskLoopSimdDirective *D) { | ||||||||
8456 | DeclarationNameInfo DirName; | ||||||||
8457 | getDerived().getSema().StartOpenMPDSABlock(OMPD_master_taskloop_simd, DirName, | ||||||||
8458 | nullptr, D->getBeginLoc()); | ||||||||
8459 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8460 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8461 | return Res; | ||||||||
8462 | } | ||||||||
8463 | |||||||||
8464 | template <typename Derived> | ||||||||
8465 | StmtResult TreeTransform<Derived>::TransformOMPParallelMasterTaskLoopDirective( | ||||||||
8466 | OMPParallelMasterTaskLoopDirective *D) { | ||||||||
8467 | DeclarationNameInfo DirName; | ||||||||
8468 | getDerived().getSema().StartOpenMPDSABlock( | ||||||||
8469 | OMPD_parallel_master_taskloop, DirName, nullptr, D->getBeginLoc()); | ||||||||
8470 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8471 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8472 | return Res; | ||||||||
8473 | } | ||||||||
8474 | |||||||||
8475 | template <typename Derived> | ||||||||
8476 | StmtResult | ||||||||
8477 | TreeTransform<Derived>::TransformOMPParallelMasterTaskLoopSimdDirective( | ||||||||
8478 | OMPParallelMasterTaskLoopSimdDirective *D) { | ||||||||
8479 | DeclarationNameInfo DirName; | ||||||||
8480 | getDerived().getSema().StartOpenMPDSABlock( | ||||||||
8481 | OMPD_parallel_master_taskloop_simd, DirName, nullptr, D->getBeginLoc()); | ||||||||
8482 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8483 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8484 | return Res; | ||||||||
8485 | } | ||||||||
8486 | |||||||||
8487 | template <typename Derived> | ||||||||
8488 | StmtResult TreeTransform<Derived>::TransformOMPDistributeDirective( | ||||||||
8489 | OMPDistributeDirective *D) { | ||||||||
8490 | DeclarationNameInfo DirName; | ||||||||
8491 | getDerived().getSema().StartOpenMPDSABlock(OMPD_distribute, DirName, nullptr, | ||||||||
8492 | D->getBeginLoc()); | ||||||||
8493 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8494 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8495 | return Res; | ||||||||
8496 | } | ||||||||
8497 | |||||||||
8498 | template <typename Derived> | ||||||||
8499 | StmtResult TreeTransform<Derived>::TransformOMPDistributeParallelForDirective( | ||||||||
8500 | OMPDistributeParallelForDirective *D) { | ||||||||
8501 | DeclarationNameInfo DirName; | ||||||||
8502 | getDerived().getSema().StartOpenMPDSABlock( | ||||||||
8503 | OMPD_distribute_parallel_for, DirName, nullptr, D->getBeginLoc()); | ||||||||
8504 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8505 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8506 | return Res; | ||||||||
8507 | } | ||||||||
8508 | |||||||||
8509 | template <typename Derived> | ||||||||
8510 | StmtResult | ||||||||
8511 | TreeTransform<Derived>::TransformOMPDistributeParallelForSimdDirective( | ||||||||
8512 | OMPDistributeParallelForSimdDirective *D) { | ||||||||
8513 | DeclarationNameInfo DirName; | ||||||||
8514 | getDerived().getSema().StartOpenMPDSABlock( | ||||||||
8515 | OMPD_distribute_parallel_for_simd, DirName, nullptr, D->getBeginLoc()); | ||||||||
8516 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8517 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8518 | return Res; | ||||||||
8519 | } | ||||||||
8520 | |||||||||
8521 | template <typename Derived> | ||||||||
8522 | StmtResult TreeTransform<Derived>::TransformOMPDistributeSimdDirective( | ||||||||
8523 | OMPDistributeSimdDirective *D) { | ||||||||
8524 | DeclarationNameInfo DirName; | ||||||||
8525 | getDerived().getSema().StartOpenMPDSABlock(OMPD_distribute_simd, DirName, | ||||||||
8526 | nullptr, D->getBeginLoc()); | ||||||||
8527 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8528 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8529 | return Res; | ||||||||
8530 | } | ||||||||
8531 | |||||||||
8532 | template <typename Derived> | ||||||||
8533 | StmtResult TreeTransform<Derived>::TransformOMPTargetParallelForSimdDirective( | ||||||||
8534 | OMPTargetParallelForSimdDirective *D) { | ||||||||
8535 | DeclarationNameInfo DirName; | ||||||||
8536 | getDerived().getSema().StartOpenMPDSABlock( | ||||||||
8537 | OMPD_target_parallel_for_simd, DirName, nullptr, D->getBeginLoc()); | ||||||||
8538 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8539 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8540 | return Res; | ||||||||
8541 | } | ||||||||
8542 | |||||||||
8543 | template <typename Derived> | ||||||||
8544 | StmtResult TreeTransform<Derived>::TransformOMPTargetSimdDirective( | ||||||||
8545 | OMPTargetSimdDirective *D) { | ||||||||
8546 | DeclarationNameInfo DirName; | ||||||||
8547 | getDerived().getSema().StartOpenMPDSABlock(OMPD_target_simd, DirName, nullptr, | ||||||||
8548 | D->getBeginLoc()); | ||||||||
8549 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8550 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8551 | return Res; | ||||||||
8552 | } | ||||||||
8553 | |||||||||
8554 | template <typename Derived> | ||||||||
8555 | StmtResult TreeTransform<Derived>::TransformOMPTeamsDistributeDirective( | ||||||||
8556 | OMPTeamsDistributeDirective *D) { | ||||||||
8557 | DeclarationNameInfo DirName; | ||||||||
8558 | getDerived().getSema().StartOpenMPDSABlock(OMPD_teams_distribute, DirName, | ||||||||
8559 | nullptr, D->getBeginLoc()); | ||||||||
8560 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8561 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8562 | return Res; | ||||||||
8563 | } | ||||||||
8564 | |||||||||
8565 | template <typename Derived> | ||||||||
8566 | StmtResult TreeTransform<Derived>::TransformOMPTeamsDistributeSimdDirective( | ||||||||
8567 | OMPTeamsDistributeSimdDirective *D) { | ||||||||
8568 | DeclarationNameInfo DirName; | ||||||||
8569 | getDerived().getSema().StartOpenMPDSABlock( | ||||||||
8570 | OMPD_teams_distribute_simd, DirName, nullptr, D->getBeginLoc()); | ||||||||
8571 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8572 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8573 | return Res; | ||||||||
8574 | } | ||||||||
8575 | |||||||||
8576 | template <typename Derived> | ||||||||
8577 | StmtResult TreeTransform<Derived>::TransformOMPTeamsDistributeParallelForSimdDirective( | ||||||||
8578 | OMPTeamsDistributeParallelForSimdDirective *D) { | ||||||||
8579 | DeclarationNameInfo DirName; | ||||||||
8580 | getDerived().getSema().StartOpenMPDSABlock( | ||||||||
8581 | OMPD_teams_distribute_parallel_for_simd, DirName, nullptr, | ||||||||
8582 | D->getBeginLoc()); | ||||||||
8583 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8584 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8585 | return Res; | ||||||||
8586 | } | ||||||||
8587 | |||||||||
8588 | template <typename Derived> | ||||||||
8589 | StmtResult TreeTransform<Derived>::TransformOMPTeamsDistributeParallelForDirective( | ||||||||
8590 | OMPTeamsDistributeParallelForDirective *D) { | ||||||||
8591 | DeclarationNameInfo DirName; | ||||||||
8592 | getDerived().getSema().StartOpenMPDSABlock( | ||||||||
8593 | OMPD_teams_distribute_parallel_for, DirName, nullptr, D->getBeginLoc()); | ||||||||
8594 | StmtResult Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8595 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8596 | return Res; | ||||||||
8597 | } | ||||||||
8598 | |||||||||
8599 | template <typename Derived> | ||||||||
8600 | StmtResult TreeTransform<Derived>::TransformOMPTargetTeamsDirective( | ||||||||
8601 | OMPTargetTeamsDirective *D) { | ||||||||
8602 | DeclarationNameInfo DirName; | ||||||||
8603 | getDerived().getSema().StartOpenMPDSABlock(OMPD_target_teams, DirName, | ||||||||
8604 | nullptr, D->getBeginLoc()); | ||||||||
8605 | auto Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8606 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8607 | return Res; | ||||||||
8608 | } | ||||||||
8609 | |||||||||
8610 | template <typename Derived> | ||||||||
8611 | StmtResult TreeTransform<Derived>::TransformOMPTargetTeamsDistributeDirective( | ||||||||
8612 | OMPTargetTeamsDistributeDirective *D) { | ||||||||
8613 | DeclarationNameInfo DirName; | ||||||||
8614 | getDerived().getSema().StartOpenMPDSABlock( | ||||||||
8615 | OMPD_target_teams_distribute, DirName, nullptr, D->getBeginLoc()); | ||||||||
8616 | auto Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8617 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8618 | return Res; | ||||||||
8619 | } | ||||||||
8620 | |||||||||
8621 | template <typename Derived> | ||||||||
8622 | StmtResult | ||||||||
8623 | TreeTransform<Derived>::TransformOMPTargetTeamsDistributeParallelForDirective( | ||||||||
8624 | OMPTargetTeamsDistributeParallelForDirective *D) { | ||||||||
8625 | DeclarationNameInfo DirName; | ||||||||
8626 | getDerived().getSema().StartOpenMPDSABlock( | ||||||||
8627 | OMPD_target_teams_distribute_parallel_for, DirName, nullptr, | ||||||||
8628 | D->getBeginLoc()); | ||||||||
8629 | auto Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8630 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8631 | return Res; | ||||||||
8632 | } | ||||||||
8633 | |||||||||
8634 | template <typename Derived> | ||||||||
8635 | StmtResult TreeTransform<Derived>:: | ||||||||
8636 | TransformOMPTargetTeamsDistributeParallelForSimdDirective( | ||||||||
8637 | OMPTargetTeamsDistributeParallelForSimdDirective *D) { | ||||||||
8638 | DeclarationNameInfo DirName; | ||||||||
8639 | getDerived().getSema().StartOpenMPDSABlock( | ||||||||
8640 | OMPD_target_teams_distribute_parallel_for_simd, DirName, nullptr, | ||||||||
8641 | D->getBeginLoc()); | ||||||||
8642 | auto Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8643 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8644 | return Res; | ||||||||
8645 | } | ||||||||
8646 | |||||||||
8647 | template <typename Derived> | ||||||||
8648 | StmtResult | ||||||||
8649 | TreeTransform<Derived>::TransformOMPTargetTeamsDistributeSimdDirective( | ||||||||
8650 | OMPTargetTeamsDistributeSimdDirective *D) { | ||||||||
8651 | DeclarationNameInfo DirName; | ||||||||
8652 | getDerived().getSema().StartOpenMPDSABlock( | ||||||||
8653 | OMPD_target_teams_distribute_simd, DirName, nullptr, D->getBeginLoc()); | ||||||||
8654 | auto Res = getDerived().TransformOMPExecutableDirective(D); | ||||||||
8655 | getDerived().getSema().EndOpenMPDSABlock(Res.get()); | ||||||||
8656 | return Res; | ||||||||
8657 | } | ||||||||
8658 | |||||||||
8659 | |||||||||
8660 | //===----------------------------------------------------------------------===// | ||||||||
8661 | // OpenMP clause transformation | ||||||||
8662 | //===----------------------------------------------------------------------===// | ||||||||
8663 | template <typename Derived> | ||||||||
8664 | OMPClause *TreeTransform<Derived>::TransformOMPIfClause(OMPIfClause *C) { | ||||||||
8665 | ExprResult Cond = getDerived().TransformExpr(C->getCondition()); | ||||||||
8666 | if (Cond.isInvalid()) | ||||||||
8667 | return nullptr; | ||||||||
8668 | return getDerived().RebuildOMPIfClause( | ||||||||
8669 | C->getNameModifier(), Cond.get(), C->getBeginLoc(), C->getLParenLoc(), | ||||||||
8670 | C->getNameModifierLoc(), C->getColonLoc(), C->getEndLoc()); | ||||||||
8671 | } | ||||||||
8672 | |||||||||
8673 | template <typename Derived> | ||||||||
8674 | OMPClause *TreeTransform<Derived>::TransformOMPFinalClause(OMPFinalClause *C) { | ||||||||
8675 | ExprResult Cond = getDerived().TransformExpr(C->getCondition()); | ||||||||
8676 | if (Cond.isInvalid()) | ||||||||
8677 | return nullptr; | ||||||||
8678 | return getDerived().RebuildOMPFinalClause(Cond.get(), C->getBeginLoc(), | ||||||||
8679 | C->getLParenLoc(), C->getEndLoc()); | ||||||||
8680 | } | ||||||||
8681 | |||||||||
8682 | template <typename Derived> | ||||||||
8683 | OMPClause * | ||||||||
8684 | TreeTransform<Derived>::TransformOMPNumThreadsClause(OMPNumThreadsClause *C) { | ||||||||
8685 | ExprResult NumThreads = getDerived().TransformExpr(C->getNumThreads()); | ||||||||
8686 | if (NumThreads.isInvalid()) | ||||||||
8687 | return nullptr; | ||||||||
8688 | return getDerived().RebuildOMPNumThreadsClause( | ||||||||
8689 | NumThreads.get(), C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
8690 | } | ||||||||
8691 | |||||||||
8692 | template <typename Derived> | ||||||||
8693 | OMPClause * | ||||||||
8694 | TreeTransform<Derived>::TransformOMPSafelenClause(OMPSafelenClause *C) { | ||||||||
8695 | ExprResult E = getDerived().TransformExpr(C->getSafelen()); | ||||||||
8696 | if (E.isInvalid()) | ||||||||
8697 | return nullptr; | ||||||||
8698 | return getDerived().RebuildOMPSafelenClause( | ||||||||
8699 | E.get(), C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
8700 | } | ||||||||
8701 | |||||||||
8702 | template <typename Derived> | ||||||||
8703 | OMPClause * | ||||||||
8704 | TreeTransform<Derived>::TransformOMPAllocatorClause(OMPAllocatorClause *C) { | ||||||||
8705 | ExprResult E = getDerived().TransformExpr(C->getAllocator()); | ||||||||
8706 | if (E.isInvalid()) | ||||||||
8707 | return nullptr; | ||||||||
8708 | return getDerived().RebuildOMPAllocatorClause( | ||||||||
8709 | E.get(), C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
8710 | } | ||||||||
8711 | |||||||||
8712 | template <typename Derived> | ||||||||
8713 | OMPClause * | ||||||||
8714 | TreeTransform<Derived>::TransformOMPSimdlenClause(OMPSimdlenClause *C) { | ||||||||
8715 | ExprResult E = getDerived().TransformExpr(C->getSimdlen()); | ||||||||
8716 | if (E.isInvalid()) | ||||||||
8717 | return nullptr; | ||||||||
8718 | return getDerived().RebuildOMPSimdlenClause( | ||||||||
8719 | E.get(), C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
8720 | } | ||||||||
8721 | |||||||||
8722 | template <typename Derived> | ||||||||
8723 | OMPClause * | ||||||||
8724 | TreeTransform<Derived>::TransformOMPCollapseClause(OMPCollapseClause *C) { | ||||||||
8725 | ExprResult E = getDerived().TransformExpr(C->getNumForLoops()); | ||||||||
8726 | if (E.isInvalid()) | ||||||||
8727 | return nullptr; | ||||||||
8728 | return getDerived().RebuildOMPCollapseClause( | ||||||||
8729 | E.get(), C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
8730 | } | ||||||||
8731 | |||||||||
8732 | template <typename Derived> | ||||||||
8733 | OMPClause * | ||||||||
8734 | TreeTransform<Derived>::TransformOMPDefaultClause(OMPDefaultClause *C) { | ||||||||
8735 | return getDerived().RebuildOMPDefaultClause( | ||||||||
8736 | C->getDefaultKind(), C->getDefaultKindKwLoc(), C->getBeginLoc(), | ||||||||
8737 | C->getLParenLoc(), C->getEndLoc()); | ||||||||
8738 | } | ||||||||
8739 | |||||||||
8740 | template <typename Derived> | ||||||||
8741 | OMPClause * | ||||||||
8742 | TreeTransform<Derived>::TransformOMPProcBindClause(OMPProcBindClause *C) { | ||||||||
8743 | return getDerived().RebuildOMPProcBindClause( | ||||||||
8744 | C->getProcBindKind(), C->getProcBindKindKwLoc(), C->getBeginLoc(), | ||||||||
8745 | C->getLParenLoc(), C->getEndLoc()); | ||||||||
8746 | } | ||||||||
8747 | |||||||||
8748 | template <typename Derived> | ||||||||
8749 | OMPClause * | ||||||||
8750 | TreeTransform<Derived>::TransformOMPScheduleClause(OMPScheduleClause *C) { | ||||||||
8751 | ExprResult E = getDerived().TransformExpr(C->getChunkSize()); | ||||||||
8752 | if (E.isInvalid()) | ||||||||
8753 | return nullptr; | ||||||||
8754 | return getDerived().RebuildOMPScheduleClause( | ||||||||
8755 | C->getFirstScheduleModifier(), C->getSecondScheduleModifier(), | ||||||||
8756 | C->getScheduleKind(), E.get(), C->getBeginLoc(), C->getLParenLoc(), | ||||||||
8757 | C->getFirstScheduleModifierLoc(), C->getSecondScheduleModifierLoc(), | ||||||||
8758 | C->getScheduleKindLoc(), C->getCommaLoc(), C->getEndLoc()); | ||||||||
8759 | } | ||||||||
8760 | |||||||||
8761 | template <typename Derived> | ||||||||
8762 | OMPClause * | ||||||||
8763 | TreeTransform<Derived>::TransformOMPOrderedClause(OMPOrderedClause *C) { | ||||||||
8764 | ExprResult E; | ||||||||
8765 | if (auto *Num = C->getNumForLoops()) { | ||||||||
8766 | E = getDerived().TransformExpr(Num); | ||||||||
8767 | if (E.isInvalid()) | ||||||||
8768 | return nullptr; | ||||||||
8769 | } | ||||||||
8770 | return getDerived().RebuildOMPOrderedClause(C->getBeginLoc(), C->getEndLoc(), | ||||||||
8771 | C->getLParenLoc(), E.get()); | ||||||||
8772 | } | ||||||||
8773 | |||||||||
8774 | template <typename Derived> | ||||||||
8775 | OMPClause * | ||||||||
8776 | TreeTransform<Derived>::TransformOMPNowaitClause(OMPNowaitClause *C) { | ||||||||
8777 | // No need to rebuild this clause, no template-dependent parameters. | ||||||||
8778 | return C; | ||||||||
8779 | } | ||||||||
8780 | |||||||||
8781 | template <typename Derived> | ||||||||
8782 | OMPClause * | ||||||||
8783 | TreeTransform<Derived>::TransformOMPUntiedClause(OMPUntiedClause *C) { | ||||||||
8784 | // No need to rebuild this clause, no template-dependent parameters. | ||||||||
8785 | return C; | ||||||||
8786 | } | ||||||||
8787 | |||||||||
8788 | template <typename Derived> | ||||||||
8789 | OMPClause * | ||||||||
8790 | TreeTransform<Derived>::TransformOMPMergeableClause(OMPMergeableClause *C) { | ||||||||
8791 | // No need to rebuild this clause, no template-dependent parameters. | ||||||||
8792 | return C; | ||||||||
8793 | } | ||||||||
8794 | |||||||||
8795 | template <typename Derived> | ||||||||
8796 | OMPClause *TreeTransform<Derived>::TransformOMPReadClause(OMPReadClause *C) { | ||||||||
8797 | // No need to rebuild this clause, no template-dependent parameters. | ||||||||
8798 | return C; | ||||||||
8799 | } | ||||||||
8800 | |||||||||
8801 | template <typename Derived> | ||||||||
8802 | OMPClause *TreeTransform<Derived>::TransformOMPWriteClause(OMPWriteClause *C) { | ||||||||
8803 | // No need to rebuild this clause, no template-dependent parameters. | ||||||||
8804 | return C; | ||||||||
8805 | } | ||||||||
8806 | |||||||||
8807 | template <typename Derived> | ||||||||
8808 | OMPClause * | ||||||||
8809 | TreeTransform<Derived>::TransformOMPUpdateClause(OMPUpdateClause *C) { | ||||||||
8810 | // No need to rebuild this clause, no template-dependent parameters. | ||||||||
8811 | return C; | ||||||||
8812 | } | ||||||||
8813 | |||||||||
8814 | template <typename Derived> | ||||||||
8815 | OMPClause * | ||||||||
8816 | TreeTransform<Derived>::TransformOMPCaptureClause(OMPCaptureClause *C) { | ||||||||
8817 | // No need to rebuild this clause, no template-dependent parameters. | ||||||||
8818 | return C; | ||||||||
8819 | } | ||||||||
8820 | |||||||||
8821 | template <typename Derived> | ||||||||
8822 | OMPClause * | ||||||||
8823 | TreeTransform<Derived>::TransformOMPSeqCstClause(OMPSeqCstClause *C) { | ||||||||
8824 | // No need to rebuild this clause, no template-dependent parameters. | ||||||||
8825 | return C; | ||||||||
8826 | } | ||||||||
8827 | |||||||||
8828 | template <typename Derived> | ||||||||
8829 | OMPClause * | ||||||||
8830 | TreeTransform<Derived>::TransformOMPAcqRelClause(OMPAcqRelClause *C) { | ||||||||
8831 | // No need to rebuild this clause, no template-dependent parameters. | ||||||||
8832 | return C; | ||||||||
8833 | } | ||||||||
8834 | |||||||||
8835 | template <typename Derived> | ||||||||
8836 | OMPClause * | ||||||||
8837 | TreeTransform<Derived>::TransformOMPAcquireClause(OMPAcquireClause *C) { | ||||||||
8838 | // No need to rebuild this clause, no template-dependent parameters. | ||||||||
8839 | return C; | ||||||||
8840 | } | ||||||||
8841 | |||||||||
8842 | template <typename Derived> | ||||||||
8843 | OMPClause * | ||||||||
8844 | TreeTransform<Derived>::TransformOMPReleaseClause(OMPReleaseClause *C) { | ||||||||
8845 | // No need to rebuild this clause, no template-dependent parameters. | ||||||||
8846 | return C; | ||||||||
8847 | } | ||||||||
8848 | |||||||||
8849 | template <typename Derived> | ||||||||
8850 | OMPClause * | ||||||||
8851 | TreeTransform<Derived>::TransformOMPRelaxedClause(OMPRelaxedClause *C) { | ||||||||
8852 | // No need to rebuild this clause, no template-dependent parameters. | ||||||||
8853 | return C; | ||||||||
8854 | } | ||||||||
8855 | |||||||||
8856 | template <typename Derived> | ||||||||
8857 | OMPClause * | ||||||||
8858 | TreeTransform<Derived>::TransformOMPThreadsClause(OMPThreadsClause *C) { | ||||||||
8859 | // No need to rebuild this clause, no template-dependent parameters. | ||||||||
8860 | return C; | ||||||||
8861 | } | ||||||||
8862 | |||||||||
8863 | template <typename Derived> | ||||||||
8864 | OMPClause *TreeTransform<Derived>::TransformOMPSIMDClause(OMPSIMDClause *C) { | ||||||||
8865 | // No need to rebuild this clause, no template-dependent parameters. | ||||||||
8866 | return C; | ||||||||
8867 | } | ||||||||
8868 | |||||||||
8869 | template <typename Derived> | ||||||||
8870 | OMPClause * | ||||||||
8871 | TreeTransform<Derived>::TransformOMPNogroupClause(OMPNogroupClause *C) { | ||||||||
8872 | // No need to rebuild this clause, no template-dependent parameters. | ||||||||
8873 | return C; | ||||||||
8874 | } | ||||||||
8875 | |||||||||
8876 | template <typename Derived> | ||||||||
8877 | OMPClause * | ||||||||
8878 | TreeTransform<Derived>::TransformOMPDestroyClause(OMPDestroyClause *C) { | ||||||||
8879 | // No need to rebuild this clause, no template-dependent parameters. | ||||||||
8880 | return C; | ||||||||
8881 | } | ||||||||
8882 | |||||||||
8883 | template <typename Derived> | ||||||||
8884 | OMPClause *TreeTransform<Derived>::TransformOMPUnifiedAddressClause( | ||||||||
8885 | OMPUnifiedAddressClause *C) { | ||||||||
8886 | llvm_unreachable("unified_address clause cannot appear in dependent context")::llvm::llvm_unreachable_internal("unified_address clause cannot appear in dependent context" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 8886); | ||||||||
8887 | } | ||||||||
8888 | |||||||||
8889 | template <typename Derived> | ||||||||
8890 | OMPClause *TreeTransform<Derived>::TransformOMPUnifiedSharedMemoryClause( | ||||||||
8891 | OMPUnifiedSharedMemoryClause *C) { | ||||||||
8892 | llvm_unreachable(::llvm::llvm_unreachable_internal("unified_shared_memory clause cannot appear in dependent context" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 8893) | ||||||||
8893 | "unified_shared_memory clause cannot appear in dependent context")::llvm::llvm_unreachable_internal("unified_shared_memory clause cannot appear in dependent context" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 8893); | ||||||||
8894 | } | ||||||||
8895 | |||||||||
8896 | template <typename Derived> | ||||||||
8897 | OMPClause *TreeTransform<Derived>::TransformOMPReverseOffloadClause( | ||||||||
8898 | OMPReverseOffloadClause *C) { | ||||||||
8899 | llvm_unreachable("reverse_offload clause cannot appear in dependent context")::llvm::llvm_unreachable_internal("reverse_offload clause cannot appear in dependent context" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 8899); | ||||||||
8900 | } | ||||||||
8901 | |||||||||
8902 | template <typename Derived> | ||||||||
8903 | OMPClause *TreeTransform<Derived>::TransformOMPDynamicAllocatorsClause( | ||||||||
8904 | OMPDynamicAllocatorsClause *C) { | ||||||||
8905 | llvm_unreachable(::llvm::llvm_unreachable_internal("dynamic_allocators clause cannot appear in dependent context" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 8906) | ||||||||
8906 | "dynamic_allocators clause cannot appear in dependent context")::llvm::llvm_unreachable_internal("dynamic_allocators clause cannot appear in dependent context" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 8906); | ||||||||
8907 | } | ||||||||
8908 | |||||||||
8909 | template <typename Derived> | ||||||||
8910 | OMPClause *TreeTransform<Derived>::TransformOMPAtomicDefaultMemOrderClause( | ||||||||
8911 | OMPAtomicDefaultMemOrderClause *C) { | ||||||||
8912 | llvm_unreachable(::llvm::llvm_unreachable_internal("atomic_default_mem_order clause cannot appear in dependent context" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 8913) | ||||||||
8913 | "atomic_default_mem_order clause cannot appear in dependent context")::llvm::llvm_unreachable_internal("atomic_default_mem_order clause cannot appear in dependent context" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 8913); | ||||||||
8914 | } | ||||||||
8915 | |||||||||
8916 | template <typename Derived> | ||||||||
8917 | OMPClause * | ||||||||
8918 | TreeTransform<Derived>::TransformOMPPrivateClause(OMPPrivateClause *C) { | ||||||||
8919 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
8920 | Vars.reserve(C->varlist_size()); | ||||||||
8921 | for (auto *VE : C->varlists()) { | ||||||||
8922 | ExprResult EVar = getDerived().TransformExpr(cast<Expr>(VE)); | ||||||||
8923 | if (EVar.isInvalid()) | ||||||||
8924 | return nullptr; | ||||||||
8925 | Vars.push_back(EVar.get()); | ||||||||
8926 | } | ||||||||
8927 | return getDerived().RebuildOMPPrivateClause( | ||||||||
8928 | Vars, C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
8929 | } | ||||||||
8930 | |||||||||
8931 | template <typename Derived> | ||||||||
8932 | OMPClause *TreeTransform<Derived>::TransformOMPFirstprivateClause( | ||||||||
8933 | OMPFirstprivateClause *C) { | ||||||||
8934 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
8935 | Vars.reserve(C->varlist_size()); | ||||||||
8936 | for (auto *VE : C->varlists()) { | ||||||||
8937 | ExprResult EVar = getDerived().TransformExpr(cast<Expr>(VE)); | ||||||||
8938 | if (EVar.isInvalid()) | ||||||||
8939 | return nullptr; | ||||||||
8940 | Vars.push_back(EVar.get()); | ||||||||
8941 | } | ||||||||
8942 | return getDerived().RebuildOMPFirstprivateClause( | ||||||||
8943 | Vars, C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
8944 | } | ||||||||
8945 | |||||||||
8946 | template <typename Derived> | ||||||||
8947 | OMPClause * | ||||||||
8948 | TreeTransform<Derived>::TransformOMPLastprivateClause(OMPLastprivateClause *C) { | ||||||||
8949 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
8950 | Vars.reserve(C->varlist_size()); | ||||||||
8951 | for (auto *VE : C->varlists()) { | ||||||||
8952 | ExprResult EVar = getDerived().TransformExpr(cast<Expr>(VE)); | ||||||||
8953 | if (EVar.isInvalid()) | ||||||||
8954 | return nullptr; | ||||||||
8955 | Vars.push_back(EVar.get()); | ||||||||
8956 | } | ||||||||
8957 | return getDerived().RebuildOMPLastprivateClause( | ||||||||
8958 | Vars, C->getKind(), C->getKindLoc(), C->getColonLoc(), C->getBeginLoc(), | ||||||||
8959 | C->getLParenLoc(), C->getEndLoc()); | ||||||||
8960 | } | ||||||||
8961 | |||||||||
8962 | template <typename Derived> | ||||||||
8963 | OMPClause * | ||||||||
8964 | TreeTransform<Derived>::TransformOMPSharedClause(OMPSharedClause *C) { | ||||||||
8965 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
8966 | Vars.reserve(C->varlist_size()); | ||||||||
8967 | for (auto *VE : C->varlists()) { | ||||||||
8968 | ExprResult EVar = getDerived().TransformExpr(cast<Expr>(VE)); | ||||||||
8969 | if (EVar.isInvalid()) | ||||||||
8970 | return nullptr; | ||||||||
8971 | Vars.push_back(EVar.get()); | ||||||||
8972 | } | ||||||||
8973 | return getDerived().RebuildOMPSharedClause(Vars, C->getBeginLoc(), | ||||||||
8974 | C->getLParenLoc(), C->getEndLoc()); | ||||||||
8975 | } | ||||||||
8976 | |||||||||
8977 | template <typename Derived> | ||||||||
8978 | OMPClause * | ||||||||
8979 | TreeTransform<Derived>::TransformOMPReductionClause(OMPReductionClause *C) { | ||||||||
8980 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
8981 | Vars.reserve(C->varlist_size()); | ||||||||
8982 | for (auto *VE : C->varlists()) { | ||||||||
8983 | ExprResult EVar = getDerived().TransformExpr(cast<Expr>(VE)); | ||||||||
8984 | if (EVar.isInvalid()) | ||||||||
8985 | return nullptr; | ||||||||
8986 | Vars.push_back(EVar.get()); | ||||||||
8987 | } | ||||||||
8988 | CXXScopeSpec ReductionIdScopeSpec; | ||||||||
8989 | ReductionIdScopeSpec.Adopt(C->getQualifierLoc()); | ||||||||
8990 | |||||||||
8991 | DeclarationNameInfo NameInfo = C->getNameInfo(); | ||||||||
8992 | if (NameInfo.getName()) { | ||||||||
8993 | NameInfo = getDerived().TransformDeclarationNameInfo(NameInfo); | ||||||||
8994 | if (!NameInfo.getName()) | ||||||||
8995 | return nullptr; | ||||||||
8996 | } | ||||||||
8997 | // Build a list of all UDR decls with the same names ranged by the Scopes. | ||||||||
8998 | // The Scope boundary is a duplication of the previous decl. | ||||||||
8999 | llvm::SmallVector<Expr *, 16> UnresolvedReductions; | ||||||||
9000 | for (auto *E : C->reduction_ops()) { | ||||||||
9001 | // Transform all the decls. | ||||||||
9002 | if (E) { | ||||||||
9003 | auto *ULE = cast<UnresolvedLookupExpr>(E); | ||||||||
9004 | UnresolvedSet<8> Decls; | ||||||||
9005 | for (auto *D : ULE->decls()) { | ||||||||
9006 | NamedDecl *InstD = | ||||||||
9007 | cast<NamedDecl>(getDerived().TransformDecl(E->getExprLoc(), D)); | ||||||||
9008 | Decls.addDecl(InstD, InstD->getAccess()); | ||||||||
9009 | } | ||||||||
9010 | UnresolvedReductions.push_back( | ||||||||
9011 | UnresolvedLookupExpr::Create( | ||||||||
9012 | SemaRef.Context, /*NamingClass=*/nullptr, | ||||||||
9013 | ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), | ||||||||
9014 | NameInfo, /*ADL=*/true, ULE->isOverloaded(), | ||||||||
9015 | Decls.begin(), Decls.end())); | ||||||||
9016 | } else | ||||||||
9017 | UnresolvedReductions.push_back(nullptr); | ||||||||
9018 | } | ||||||||
9019 | return getDerived().RebuildOMPReductionClause( | ||||||||
9020 | Vars, C->getBeginLoc(), C->getLParenLoc(), C->getColonLoc(), | ||||||||
9021 | C->getEndLoc(), ReductionIdScopeSpec, NameInfo, UnresolvedReductions); | ||||||||
9022 | } | ||||||||
9023 | |||||||||
9024 | template <typename Derived> | ||||||||
9025 | OMPClause *TreeTransform<Derived>::TransformOMPTaskReductionClause( | ||||||||
9026 | OMPTaskReductionClause *C) { | ||||||||
9027 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
9028 | Vars.reserve(C->varlist_size()); | ||||||||
9029 | for (auto *VE : C->varlists()) { | ||||||||
9030 | ExprResult EVar = getDerived().TransformExpr(cast<Expr>(VE)); | ||||||||
9031 | if (EVar.isInvalid()) | ||||||||
9032 | return nullptr; | ||||||||
9033 | Vars.push_back(EVar.get()); | ||||||||
9034 | } | ||||||||
9035 | CXXScopeSpec ReductionIdScopeSpec; | ||||||||
9036 | ReductionIdScopeSpec.Adopt(C->getQualifierLoc()); | ||||||||
9037 | |||||||||
9038 | DeclarationNameInfo NameInfo = C->getNameInfo(); | ||||||||
9039 | if (NameInfo.getName()) { | ||||||||
9040 | NameInfo = getDerived().TransformDeclarationNameInfo(NameInfo); | ||||||||
9041 | if (!NameInfo.getName()) | ||||||||
9042 | return nullptr; | ||||||||
9043 | } | ||||||||
9044 | // Build a list of all UDR decls with the same names ranged by the Scopes. | ||||||||
9045 | // The Scope boundary is a duplication of the previous decl. | ||||||||
9046 | llvm::SmallVector<Expr *, 16> UnresolvedReductions; | ||||||||
9047 | for (auto *E : C->reduction_ops()) { | ||||||||
9048 | // Transform all the decls. | ||||||||
9049 | if (E) { | ||||||||
9050 | auto *ULE = cast<UnresolvedLookupExpr>(E); | ||||||||
9051 | UnresolvedSet<8> Decls; | ||||||||
9052 | for (auto *D : ULE->decls()) { | ||||||||
9053 | NamedDecl *InstD = | ||||||||
9054 | cast<NamedDecl>(getDerived().TransformDecl(E->getExprLoc(), D)); | ||||||||
9055 | Decls.addDecl(InstD, InstD->getAccess()); | ||||||||
9056 | } | ||||||||
9057 | UnresolvedReductions.push_back(UnresolvedLookupExpr::Create( | ||||||||
9058 | SemaRef.Context, /*NamingClass=*/nullptr, | ||||||||
9059 | ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), NameInfo, | ||||||||
9060 | /*ADL=*/true, ULE->isOverloaded(), Decls.begin(), Decls.end())); | ||||||||
9061 | } else | ||||||||
9062 | UnresolvedReductions.push_back(nullptr); | ||||||||
9063 | } | ||||||||
9064 | return getDerived().RebuildOMPTaskReductionClause( | ||||||||
9065 | Vars, C->getBeginLoc(), C->getLParenLoc(), C->getColonLoc(), | ||||||||
9066 | C->getEndLoc(), ReductionIdScopeSpec, NameInfo, UnresolvedReductions); | ||||||||
9067 | } | ||||||||
9068 | |||||||||
9069 | template <typename Derived> | ||||||||
9070 | OMPClause * | ||||||||
9071 | TreeTransform<Derived>::TransformOMPInReductionClause(OMPInReductionClause *C) { | ||||||||
9072 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
9073 | Vars.reserve(C->varlist_size()); | ||||||||
9074 | for (auto *VE : C->varlists()) { | ||||||||
9075 | ExprResult EVar = getDerived().TransformExpr(cast<Expr>(VE)); | ||||||||
9076 | if (EVar.isInvalid()) | ||||||||
9077 | return nullptr; | ||||||||
9078 | Vars.push_back(EVar.get()); | ||||||||
9079 | } | ||||||||
9080 | CXXScopeSpec ReductionIdScopeSpec; | ||||||||
9081 | ReductionIdScopeSpec.Adopt(C->getQualifierLoc()); | ||||||||
9082 | |||||||||
9083 | DeclarationNameInfo NameInfo = C->getNameInfo(); | ||||||||
9084 | if (NameInfo.getName()) { | ||||||||
9085 | NameInfo = getDerived().TransformDeclarationNameInfo(NameInfo); | ||||||||
9086 | if (!NameInfo.getName()) | ||||||||
9087 | return nullptr; | ||||||||
9088 | } | ||||||||
9089 | // Build a list of all UDR decls with the same names ranged by the Scopes. | ||||||||
9090 | // The Scope boundary is a duplication of the previous decl. | ||||||||
9091 | llvm::SmallVector<Expr *, 16> UnresolvedReductions; | ||||||||
9092 | for (auto *E : C->reduction_ops()) { | ||||||||
9093 | // Transform all the decls. | ||||||||
9094 | if (E) { | ||||||||
9095 | auto *ULE = cast<UnresolvedLookupExpr>(E); | ||||||||
9096 | UnresolvedSet<8> Decls; | ||||||||
9097 | for (auto *D : ULE->decls()) { | ||||||||
9098 | NamedDecl *InstD = | ||||||||
9099 | cast<NamedDecl>(getDerived().TransformDecl(E->getExprLoc(), D)); | ||||||||
9100 | Decls.addDecl(InstD, InstD->getAccess()); | ||||||||
9101 | } | ||||||||
9102 | UnresolvedReductions.push_back(UnresolvedLookupExpr::Create( | ||||||||
9103 | SemaRef.Context, /*NamingClass=*/nullptr, | ||||||||
9104 | ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), NameInfo, | ||||||||
9105 | /*ADL=*/true, ULE->isOverloaded(), Decls.begin(), Decls.end())); | ||||||||
9106 | } else | ||||||||
9107 | UnresolvedReductions.push_back(nullptr); | ||||||||
9108 | } | ||||||||
9109 | return getDerived().RebuildOMPInReductionClause( | ||||||||
9110 | Vars, C->getBeginLoc(), C->getLParenLoc(), C->getColonLoc(), | ||||||||
9111 | C->getEndLoc(), ReductionIdScopeSpec, NameInfo, UnresolvedReductions); | ||||||||
9112 | } | ||||||||
9113 | |||||||||
9114 | template <typename Derived> | ||||||||
9115 | OMPClause * | ||||||||
9116 | TreeTransform<Derived>::TransformOMPLinearClause(OMPLinearClause *C) { | ||||||||
9117 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
9118 | Vars.reserve(C->varlist_size()); | ||||||||
9119 | for (auto *VE : C->varlists()) { | ||||||||
9120 | ExprResult EVar = getDerived().TransformExpr(cast<Expr>(VE)); | ||||||||
9121 | if (EVar.isInvalid()) | ||||||||
9122 | return nullptr; | ||||||||
9123 | Vars.push_back(EVar.get()); | ||||||||
9124 | } | ||||||||
9125 | ExprResult Step = getDerived().TransformExpr(C->getStep()); | ||||||||
9126 | if (Step.isInvalid()) | ||||||||
9127 | return nullptr; | ||||||||
9128 | return getDerived().RebuildOMPLinearClause( | ||||||||
9129 | Vars, Step.get(), C->getBeginLoc(), C->getLParenLoc(), C->getModifier(), | ||||||||
9130 | C->getModifierLoc(), C->getColonLoc(), C->getEndLoc()); | ||||||||
9131 | } | ||||||||
9132 | |||||||||
9133 | template <typename Derived> | ||||||||
9134 | OMPClause * | ||||||||
9135 | TreeTransform<Derived>::TransformOMPAlignedClause(OMPAlignedClause *C) { | ||||||||
9136 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
9137 | Vars.reserve(C->varlist_size()); | ||||||||
9138 | for (auto *VE : C->varlists()) { | ||||||||
9139 | ExprResult EVar = getDerived().TransformExpr(cast<Expr>(VE)); | ||||||||
9140 | if (EVar.isInvalid()) | ||||||||
9141 | return nullptr; | ||||||||
9142 | Vars.push_back(EVar.get()); | ||||||||
9143 | } | ||||||||
9144 | ExprResult Alignment = getDerived().TransformExpr(C->getAlignment()); | ||||||||
9145 | if (Alignment.isInvalid()) | ||||||||
9146 | return nullptr; | ||||||||
9147 | return getDerived().RebuildOMPAlignedClause( | ||||||||
9148 | Vars, Alignment.get(), C->getBeginLoc(), C->getLParenLoc(), | ||||||||
9149 | C->getColonLoc(), C->getEndLoc()); | ||||||||
9150 | } | ||||||||
9151 | |||||||||
9152 | template <typename Derived> | ||||||||
9153 | OMPClause * | ||||||||
9154 | TreeTransform<Derived>::TransformOMPCopyinClause(OMPCopyinClause *C) { | ||||||||
9155 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
9156 | Vars.reserve(C->varlist_size()); | ||||||||
9157 | for (auto *VE : C->varlists()) { | ||||||||
9158 | ExprResult EVar = getDerived().TransformExpr(cast<Expr>(VE)); | ||||||||
9159 | if (EVar.isInvalid()) | ||||||||
9160 | return nullptr; | ||||||||
9161 | Vars.push_back(EVar.get()); | ||||||||
9162 | } | ||||||||
9163 | return getDerived().RebuildOMPCopyinClause(Vars, C->getBeginLoc(), | ||||||||
9164 | C->getLParenLoc(), C->getEndLoc()); | ||||||||
9165 | } | ||||||||
9166 | |||||||||
9167 | template <typename Derived> | ||||||||
9168 | OMPClause * | ||||||||
9169 | TreeTransform<Derived>::TransformOMPCopyprivateClause(OMPCopyprivateClause *C) { | ||||||||
9170 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
9171 | Vars.reserve(C->varlist_size()); | ||||||||
9172 | for (auto *VE : C->varlists()) { | ||||||||
9173 | ExprResult EVar = getDerived().TransformExpr(cast<Expr>(VE)); | ||||||||
9174 | if (EVar.isInvalid()) | ||||||||
9175 | return nullptr; | ||||||||
9176 | Vars.push_back(EVar.get()); | ||||||||
9177 | } | ||||||||
9178 | return getDerived().RebuildOMPCopyprivateClause( | ||||||||
9179 | Vars, C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
9180 | } | ||||||||
9181 | |||||||||
9182 | template <typename Derived> | ||||||||
9183 | OMPClause *TreeTransform<Derived>::TransformOMPFlushClause(OMPFlushClause *C) { | ||||||||
9184 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
9185 | Vars.reserve(C->varlist_size()); | ||||||||
9186 | for (auto *VE : C->varlists()) { | ||||||||
9187 | ExprResult EVar = getDerived().TransformExpr(cast<Expr>(VE)); | ||||||||
9188 | if (EVar.isInvalid()) | ||||||||
9189 | return nullptr; | ||||||||
9190 | Vars.push_back(EVar.get()); | ||||||||
9191 | } | ||||||||
9192 | return getDerived().RebuildOMPFlushClause(Vars, C->getBeginLoc(), | ||||||||
9193 | C->getLParenLoc(), C->getEndLoc()); | ||||||||
9194 | } | ||||||||
9195 | |||||||||
9196 | template <typename Derived> | ||||||||
9197 | OMPClause * | ||||||||
9198 | TreeTransform<Derived>::TransformOMPDepobjClause(OMPDepobjClause *C) { | ||||||||
9199 | ExprResult E = getDerived().TransformExpr(C->getDepobj()); | ||||||||
9200 | if (E.isInvalid()) | ||||||||
9201 | return nullptr; | ||||||||
9202 | return getDerived().RebuildOMPDepobjClause(E.get(), C->getBeginLoc(), | ||||||||
9203 | C->getLParenLoc(), C->getEndLoc()); | ||||||||
9204 | } | ||||||||
9205 | |||||||||
9206 | template <typename Derived> | ||||||||
9207 | OMPClause * | ||||||||
9208 | TreeTransform<Derived>::TransformOMPDependClause(OMPDependClause *C) { | ||||||||
9209 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
9210 | Vars.reserve(C->varlist_size()); | ||||||||
9211 | for (auto *VE : C->varlists()) { | ||||||||
9212 | ExprResult EVar = getDerived().TransformExpr(cast<Expr>(VE)); | ||||||||
9213 | if (EVar.isInvalid()) | ||||||||
9214 | return nullptr; | ||||||||
9215 | Vars.push_back(EVar.get()); | ||||||||
9216 | } | ||||||||
9217 | return getDerived().RebuildOMPDependClause( | ||||||||
9218 | C->getDependencyKind(), C->getDependencyLoc(), C->getColonLoc(), Vars, | ||||||||
9219 | C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
9220 | } | ||||||||
9221 | |||||||||
9222 | template <typename Derived> | ||||||||
9223 | OMPClause * | ||||||||
9224 | TreeTransform<Derived>::TransformOMPDeviceClause(OMPDeviceClause *C) { | ||||||||
9225 | ExprResult E = getDerived().TransformExpr(C->getDevice()); | ||||||||
9226 | if (E.isInvalid()) | ||||||||
9227 | return nullptr; | ||||||||
9228 | return getDerived().RebuildOMPDeviceClause(E.get(), C->getBeginLoc(), | ||||||||
9229 | C->getLParenLoc(), C->getEndLoc()); | ||||||||
9230 | } | ||||||||
9231 | |||||||||
9232 | template <typename Derived, class T> | ||||||||
9233 | bool transformOMPMappableExprListClause( | ||||||||
9234 | TreeTransform<Derived> &TT, OMPMappableExprListClause<T> *C, | ||||||||
9235 | llvm::SmallVectorImpl<Expr *> &Vars, CXXScopeSpec &MapperIdScopeSpec, | ||||||||
9236 | DeclarationNameInfo &MapperIdInfo, | ||||||||
9237 | llvm::SmallVectorImpl<Expr *> &UnresolvedMappers) { | ||||||||
9238 | // Transform expressions in the list. | ||||||||
9239 | Vars.reserve(C->varlist_size()); | ||||||||
9240 | for (auto *VE : C->varlists()) { | ||||||||
9241 | ExprResult EVar = TT.getDerived().TransformExpr(cast<Expr>(VE)); | ||||||||
9242 | if (EVar.isInvalid()) | ||||||||
9243 | return true; | ||||||||
9244 | Vars.push_back(EVar.get()); | ||||||||
9245 | } | ||||||||
9246 | // Transform mapper scope specifier and identifier. | ||||||||
9247 | NestedNameSpecifierLoc QualifierLoc; | ||||||||
9248 | if (C->getMapperQualifierLoc()) { | ||||||||
9249 | QualifierLoc = TT.getDerived().TransformNestedNameSpecifierLoc( | ||||||||
9250 | C->getMapperQualifierLoc()); | ||||||||
9251 | if (!QualifierLoc) | ||||||||
9252 | return true; | ||||||||
9253 | } | ||||||||
9254 | MapperIdScopeSpec.Adopt(QualifierLoc); | ||||||||
9255 | MapperIdInfo = C->getMapperIdInfo(); | ||||||||
9256 | if (MapperIdInfo.getName()) { | ||||||||
9257 | MapperIdInfo = TT.getDerived().TransformDeclarationNameInfo(MapperIdInfo); | ||||||||
9258 | if (!MapperIdInfo.getName()) | ||||||||
9259 | return true; | ||||||||
9260 | } | ||||||||
9261 | // Build a list of all candidate OMPDeclareMapperDecls, which is provided by | ||||||||
9262 | // the previous user-defined mapper lookup in dependent environment. | ||||||||
9263 | for (auto *E : C->mapperlists()) { | ||||||||
9264 | // Transform all the decls. | ||||||||
9265 | if (E) { | ||||||||
9266 | auto *ULE = cast<UnresolvedLookupExpr>(E); | ||||||||
9267 | UnresolvedSet<8> Decls; | ||||||||
9268 | for (auto *D : ULE->decls()) { | ||||||||
9269 | NamedDecl *InstD = | ||||||||
9270 | cast<NamedDecl>(TT.getDerived().TransformDecl(E->getExprLoc(), D)); | ||||||||
9271 | Decls.addDecl(InstD, InstD->getAccess()); | ||||||||
9272 | } | ||||||||
9273 | UnresolvedMappers.push_back(UnresolvedLookupExpr::Create( | ||||||||
9274 | TT.getSema().Context, /*NamingClass=*/nullptr, | ||||||||
9275 | MapperIdScopeSpec.getWithLocInContext(TT.getSema().Context), | ||||||||
9276 | MapperIdInfo, /*ADL=*/true, ULE->isOverloaded(), Decls.begin(), | ||||||||
9277 | Decls.end())); | ||||||||
9278 | } else { | ||||||||
9279 | UnresolvedMappers.push_back(nullptr); | ||||||||
9280 | } | ||||||||
9281 | } | ||||||||
9282 | return false; | ||||||||
9283 | } | ||||||||
9284 | |||||||||
9285 | template <typename Derived> | ||||||||
9286 | OMPClause *TreeTransform<Derived>::TransformOMPMapClause(OMPMapClause *C) { | ||||||||
9287 | OMPVarListLocTy Locs(C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
9288 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
9289 | CXXScopeSpec MapperIdScopeSpec; | ||||||||
9290 | DeclarationNameInfo MapperIdInfo; | ||||||||
9291 | llvm::SmallVector<Expr *, 16> UnresolvedMappers; | ||||||||
9292 | if (transformOMPMappableExprListClause<Derived, OMPMapClause>( | ||||||||
9293 | *this, C, Vars, MapperIdScopeSpec, MapperIdInfo, UnresolvedMappers)) | ||||||||
9294 | return nullptr; | ||||||||
9295 | return getDerived().RebuildOMPMapClause( | ||||||||
9296 | C->getMapTypeModifiers(), C->getMapTypeModifiersLoc(), MapperIdScopeSpec, | ||||||||
9297 | MapperIdInfo, C->getMapType(), C->isImplicitMapType(), C->getMapLoc(), | ||||||||
9298 | C->getColonLoc(), Vars, Locs, UnresolvedMappers); | ||||||||
9299 | } | ||||||||
9300 | |||||||||
9301 | template <typename Derived> | ||||||||
9302 | OMPClause * | ||||||||
9303 | TreeTransform<Derived>::TransformOMPAllocateClause(OMPAllocateClause *C) { | ||||||||
9304 | Expr *Allocator = C->getAllocator(); | ||||||||
9305 | if (Allocator) { | ||||||||
9306 | ExprResult AllocatorRes = getDerived().TransformExpr(Allocator); | ||||||||
9307 | if (AllocatorRes.isInvalid()) | ||||||||
9308 | return nullptr; | ||||||||
9309 | Allocator = AllocatorRes.get(); | ||||||||
9310 | } | ||||||||
9311 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
9312 | Vars.reserve(C->varlist_size()); | ||||||||
9313 | for (auto *VE : C->varlists()) { | ||||||||
9314 | ExprResult EVar = getDerived().TransformExpr(cast<Expr>(VE)); | ||||||||
9315 | if (EVar.isInvalid()) | ||||||||
9316 | return nullptr; | ||||||||
9317 | Vars.push_back(EVar.get()); | ||||||||
9318 | } | ||||||||
9319 | return getDerived().RebuildOMPAllocateClause( | ||||||||
9320 | Allocator, Vars, C->getBeginLoc(), C->getLParenLoc(), C->getColonLoc(), | ||||||||
9321 | C->getEndLoc()); | ||||||||
9322 | } | ||||||||
9323 | |||||||||
9324 | template <typename Derived> | ||||||||
9325 | OMPClause * | ||||||||
9326 | TreeTransform<Derived>::TransformOMPNumTeamsClause(OMPNumTeamsClause *C) { | ||||||||
9327 | ExprResult E = getDerived().TransformExpr(C->getNumTeams()); | ||||||||
9328 | if (E.isInvalid()) | ||||||||
9329 | return nullptr; | ||||||||
9330 | return getDerived().RebuildOMPNumTeamsClause( | ||||||||
9331 | E.get(), C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
9332 | } | ||||||||
9333 | |||||||||
9334 | template <typename Derived> | ||||||||
9335 | OMPClause * | ||||||||
9336 | TreeTransform<Derived>::TransformOMPThreadLimitClause(OMPThreadLimitClause *C) { | ||||||||
9337 | ExprResult E = getDerived().TransformExpr(C->getThreadLimit()); | ||||||||
9338 | if (E.isInvalid()) | ||||||||
9339 | return nullptr; | ||||||||
9340 | return getDerived().RebuildOMPThreadLimitClause( | ||||||||
9341 | E.get(), C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
9342 | } | ||||||||
9343 | |||||||||
9344 | template <typename Derived> | ||||||||
9345 | OMPClause * | ||||||||
9346 | TreeTransform<Derived>::TransformOMPPriorityClause(OMPPriorityClause *C) { | ||||||||
9347 | ExprResult E = getDerived().TransformExpr(C->getPriority()); | ||||||||
9348 | if (E.isInvalid()) | ||||||||
9349 | return nullptr; | ||||||||
9350 | return getDerived().RebuildOMPPriorityClause( | ||||||||
9351 | E.get(), C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
9352 | } | ||||||||
9353 | |||||||||
9354 | template <typename Derived> | ||||||||
9355 | OMPClause * | ||||||||
9356 | TreeTransform<Derived>::TransformOMPGrainsizeClause(OMPGrainsizeClause *C) { | ||||||||
9357 | ExprResult E = getDerived().TransformExpr(C->getGrainsize()); | ||||||||
9358 | if (E.isInvalid()) | ||||||||
9359 | return nullptr; | ||||||||
9360 | return getDerived().RebuildOMPGrainsizeClause( | ||||||||
9361 | E.get(), C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
9362 | } | ||||||||
9363 | |||||||||
9364 | template <typename Derived> | ||||||||
9365 | OMPClause * | ||||||||
9366 | TreeTransform<Derived>::TransformOMPNumTasksClause(OMPNumTasksClause *C) { | ||||||||
9367 | ExprResult E = getDerived().TransformExpr(C->getNumTasks()); | ||||||||
9368 | if (E.isInvalid()) | ||||||||
9369 | return nullptr; | ||||||||
9370 | return getDerived().RebuildOMPNumTasksClause( | ||||||||
9371 | E.get(), C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
9372 | } | ||||||||
9373 | |||||||||
9374 | template <typename Derived> | ||||||||
9375 | OMPClause *TreeTransform<Derived>::TransformOMPHintClause(OMPHintClause *C) { | ||||||||
9376 | ExprResult E = getDerived().TransformExpr(C->getHint()); | ||||||||
9377 | if (E.isInvalid()) | ||||||||
9378 | return nullptr; | ||||||||
9379 | return getDerived().RebuildOMPHintClause(E.get(), C->getBeginLoc(), | ||||||||
9380 | C->getLParenLoc(), C->getEndLoc()); | ||||||||
9381 | } | ||||||||
9382 | |||||||||
9383 | template <typename Derived> | ||||||||
9384 | OMPClause *TreeTransform<Derived>::TransformOMPDistScheduleClause( | ||||||||
9385 | OMPDistScheduleClause *C) { | ||||||||
9386 | ExprResult E = getDerived().TransformExpr(C->getChunkSize()); | ||||||||
9387 | if (E.isInvalid()) | ||||||||
9388 | return nullptr; | ||||||||
9389 | return getDerived().RebuildOMPDistScheduleClause( | ||||||||
9390 | C->getDistScheduleKind(), E.get(), C->getBeginLoc(), C->getLParenLoc(), | ||||||||
9391 | C->getDistScheduleKindLoc(), C->getCommaLoc(), C->getEndLoc()); | ||||||||
9392 | } | ||||||||
9393 | |||||||||
9394 | template <typename Derived> | ||||||||
9395 | OMPClause * | ||||||||
9396 | TreeTransform<Derived>::TransformOMPDefaultmapClause(OMPDefaultmapClause *C) { | ||||||||
9397 | // Rebuild Defaultmap Clause since we need to invoke the checking of | ||||||||
9398 | // defaultmap(none:variable-category) after template initialization. | ||||||||
9399 | return getDerived().RebuildOMPDefaultmapClause(C->getDefaultmapModifier(), | ||||||||
9400 | C->getDefaultmapKind(), | ||||||||
9401 | C->getBeginLoc(), | ||||||||
9402 | C->getLParenLoc(), | ||||||||
9403 | C->getDefaultmapModifierLoc(), | ||||||||
9404 | C->getDefaultmapKindLoc(), | ||||||||
9405 | C->getEndLoc()); | ||||||||
9406 | } | ||||||||
9407 | |||||||||
9408 | template <typename Derived> | ||||||||
9409 | OMPClause *TreeTransform<Derived>::TransformOMPToClause(OMPToClause *C) { | ||||||||
9410 | OMPVarListLocTy Locs(C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
9411 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
9412 | CXXScopeSpec MapperIdScopeSpec; | ||||||||
9413 | DeclarationNameInfo MapperIdInfo; | ||||||||
9414 | llvm::SmallVector<Expr *, 16> UnresolvedMappers; | ||||||||
9415 | if (transformOMPMappableExprListClause<Derived, OMPToClause>( | ||||||||
9416 | *this, C, Vars, MapperIdScopeSpec, MapperIdInfo, UnresolvedMappers)) | ||||||||
9417 | return nullptr; | ||||||||
9418 | return getDerived().RebuildOMPToClause(Vars, MapperIdScopeSpec, MapperIdInfo, | ||||||||
9419 | Locs, UnresolvedMappers); | ||||||||
9420 | } | ||||||||
9421 | |||||||||
9422 | template <typename Derived> | ||||||||
9423 | OMPClause *TreeTransform<Derived>::TransformOMPFromClause(OMPFromClause *C) { | ||||||||
9424 | OMPVarListLocTy Locs(C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
9425 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
9426 | CXXScopeSpec MapperIdScopeSpec; | ||||||||
9427 | DeclarationNameInfo MapperIdInfo; | ||||||||
9428 | llvm::SmallVector<Expr *, 16> UnresolvedMappers; | ||||||||
9429 | if (transformOMPMappableExprListClause<Derived, OMPFromClause>( | ||||||||
9430 | *this, C, Vars, MapperIdScopeSpec, MapperIdInfo, UnresolvedMappers)) | ||||||||
9431 | return nullptr; | ||||||||
9432 | return getDerived().RebuildOMPFromClause( | ||||||||
9433 | Vars, MapperIdScopeSpec, MapperIdInfo, Locs, UnresolvedMappers); | ||||||||
9434 | } | ||||||||
9435 | |||||||||
9436 | template <typename Derived> | ||||||||
9437 | OMPClause *TreeTransform<Derived>::TransformOMPUseDevicePtrClause( | ||||||||
9438 | OMPUseDevicePtrClause *C) { | ||||||||
9439 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
9440 | Vars.reserve(C->varlist_size()); | ||||||||
9441 | for (auto *VE : C->varlists()) { | ||||||||
9442 | ExprResult EVar = getDerived().TransformExpr(cast<Expr>(VE)); | ||||||||
9443 | if (EVar.isInvalid()) | ||||||||
9444 | return nullptr; | ||||||||
9445 | Vars.push_back(EVar.get()); | ||||||||
9446 | } | ||||||||
9447 | OMPVarListLocTy Locs(C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
9448 | return getDerived().RebuildOMPUseDevicePtrClause(Vars, Locs); | ||||||||
9449 | } | ||||||||
9450 | |||||||||
9451 | template <typename Derived> | ||||||||
9452 | OMPClause * | ||||||||
9453 | TreeTransform<Derived>::TransformOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) { | ||||||||
9454 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
9455 | Vars.reserve(C->varlist_size()); | ||||||||
9456 | for (auto *VE : C->varlists()) { | ||||||||
9457 | ExprResult EVar = getDerived().TransformExpr(cast<Expr>(VE)); | ||||||||
9458 | if (EVar.isInvalid()) | ||||||||
9459 | return nullptr; | ||||||||
9460 | Vars.push_back(EVar.get()); | ||||||||
9461 | } | ||||||||
9462 | OMPVarListLocTy Locs(C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
9463 | return getDerived().RebuildOMPIsDevicePtrClause(Vars, Locs); | ||||||||
9464 | } | ||||||||
9465 | |||||||||
9466 | template <typename Derived> | ||||||||
9467 | OMPClause * | ||||||||
9468 | TreeTransform<Derived>::TransformOMPNontemporalClause(OMPNontemporalClause *C) { | ||||||||
9469 | llvm::SmallVector<Expr *, 16> Vars; | ||||||||
9470 | Vars.reserve(C->varlist_size()); | ||||||||
9471 | for (auto *VE : C->varlists()) { | ||||||||
9472 | ExprResult EVar = getDerived().TransformExpr(cast<Expr>(VE)); | ||||||||
9473 | if (EVar.isInvalid()) | ||||||||
9474 | return nullptr; | ||||||||
9475 | Vars.push_back(EVar.get()); | ||||||||
9476 | } | ||||||||
9477 | return getDerived().RebuildOMPNontemporalClause( | ||||||||
9478 | Vars, C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc()); | ||||||||
9479 | } | ||||||||
9480 | |||||||||
9481 | template <typename Derived> | ||||||||
9482 | OMPClause * | ||||||||
9483 | TreeTransform<Derived>::TransformOMPOrderClause(OMPOrderClause *C) { | ||||||||
9484 | return getDerived().RebuildOMPOrderClause(C->getKind(), C->getKindKwLoc(), | ||||||||
9485 | C->getBeginLoc(), C->getLParenLoc(), | ||||||||
9486 | C->getEndLoc()); | ||||||||
9487 | } | ||||||||
9488 | |||||||||
9489 | //===----------------------------------------------------------------------===// | ||||||||
9490 | // Expression transformation | ||||||||
9491 | //===----------------------------------------------------------------------===// | ||||||||
9492 | template<typename Derived> | ||||||||
9493 | ExprResult | ||||||||
9494 | TreeTransform<Derived>::TransformConstantExpr(ConstantExpr *E) { | ||||||||
9495 | return TransformExpr(E->getSubExpr()); | ||||||||
9496 | } | ||||||||
9497 | |||||||||
9498 | template<typename Derived> | ||||||||
9499 | ExprResult | ||||||||
9500 | TreeTransform<Derived>::TransformPredefinedExpr(PredefinedExpr *E) { | ||||||||
9501 | if (!E->isTypeDependent()) | ||||||||
9502 | return E; | ||||||||
9503 | |||||||||
9504 | return getDerived().RebuildPredefinedExpr(E->getLocation(), | ||||||||
9505 | E->getIdentKind()); | ||||||||
9506 | } | ||||||||
9507 | |||||||||
9508 | template<typename Derived> | ||||||||
9509 | ExprResult | ||||||||
9510 | TreeTransform<Derived>::TransformDeclRefExpr(DeclRefExpr *E) { | ||||||||
9511 | NestedNameSpecifierLoc QualifierLoc; | ||||||||
9512 | if (E->getQualifierLoc()) { | ||||||||
9513 | QualifierLoc | ||||||||
9514 | = getDerived().TransformNestedNameSpecifierLoc(E->getQualifierLoc()); | ||||||||
9515 | if (!QualifierLoc) | ||||||||
9516 | return ExprError(); | ||||||||
9517 | } | ||||||||
9518 | |||||||||
9519 | ValueDecl *ND | ||||||||
9520 | = cast_or_null<ValueDecl>(getDerived().TransformDecl(E->getLocation(), | ||||||||
9521 | E->getDecl())); | ||||||||
9522 | if (!ND) | ||||||||
9523 | return ExprError(); | ||||||||
9524 | |||||||||
9525 | NamedDecl *Found = ND; | ||||||||
9526 | if (E->getFoundDecl() != E->getDecl()) { | ||||||||
9527 | Found = cast_or_null<NamedDecl>( | ||||||||
9528 | getDerived().TransformDecl(E->getLocation(), E->getFoundDecl())); | ||||||||
9529 | if (!Found) | ||||||||
9530 | return ExprError(); | ||||||||
9531 | } | ||||||||
9532 | |||||||||
9533 | DeclarationNameInfo NameInfo = E->getNameInfo(); | ||||||||
9534 | if (NameInfo.getName()) { | ||||||||
9535 | NameInfo = getDerived().TransformDeclarationNameInfo(NameInfo); | ||||||||
9536 | if (!NameInfo.getName()) | ||||||||
9537 | return ExprError(); | ||||||||
9538 | } | ||||||||
9539 | |||||||||
9540 | if (!getDerived().AlwaysRebuild() && | ||||||||
9541 | QualifierLoc == E->getQualifierLoc() && | ||||||||
9542 | ND == E->getDecl() && | ||||||||
9543 | Found == E->getFoundDecl() && | ||||||||
9544 | NameInfo.getName() == E->getDecl()->getDeclName() && | ||||||||
9545 | !E->hasExplicitTemplateArgs()) { | ||||||||
9546 | |||||||||
9547 | // Mark it referenced in the new context regardless. | ||||||||
9548 | // FIXME: this is a bit instantiation-specific. | ||||||||
9549 | SemaRef.MarkDeclRefReferenced(E); | ||||||||
9550 | |||||||||
9551 | return E; | ||||||||
9552 | } | ||||||||
9553 | |||||||||
9554 | TemplateArgumentListInfo TransArgs, *TemplateArgs = nullptr; | ||||||||
9555 | if (E->hasExplicitTemplateArgs()) { | ||||||||
9556 | TemplateArgs = &TransArgs; | ||||||||
9557 | TransArgs.setLAngleLoc(E->getLAngleLoc()); | ||||||||
9558 | TransArgs.setRAngleLoc(E->getRAngleLoc()); | ||||||||
9559 | if (getDerived().TransformTemplateArguments(E->getTemplateArgs(), | ||||||||
9560 | E->getNumTemplateArgs(), | ||||||||
9561 | TransArgs)) | ||||||||
9562 | return ExprError(); | ||||||||
9563 | } | ||||||||
9564 | |||||||||
9565 | return getDerived().RebuildDeclRefExpr(QualifierLoc, ND, NameInfo, | ||||||||
9566 | Found, TemplateArgs); | ||||||||
9567 | } | ||||||||
9568 | |||||||||
9569 | template<typename Derived> | ||||||||
9570 | ExprResult | ||||||||
9571 | TreeTransform<Derived>::TransformIntegerLiteral(IntegerLiteral *E) { | ||||||||
9572 | return E; | ||||||||
9573 | } | ||||||||
9574 | |||||||||
9575 | template <typename Derived> | ||||||||
9576 | ExprResult TreeTransform<Derived>::TransformFixedPointLiteral( | ||||||||
9577 | FixedPointLiteral *E) { | ||||||||
9578 | return E; | ||||||||
9579 | } | ||||||||
9580 | |||||||||
9581 | template<typename Derived> | ||||||||
9582 | ExprResult | ||||||||
9583 | TreeTransform<Derived>::TransformFloatingLiteral(FloatingLiteral *E) { | ||||||||
9584 | return E; | ||||||||
9585 | } | ||||||||
9586 | |||||||||
9587 | template<typename Derived> | ||||||||
9588 | ExprResult | ||||||||
9589 | TreeTransform<Derived>::TransformImaginaryLiteral(ImaginaryLiteral *E) { | ||||||||
9590 | return E; | ||||||||
9591 | } | ||||||||
9592 | |||||||||
9593 | template<typename Derived> | ||||||||
9594 | ExprResult | ||||||||
9595 | TreeTransform<Derived>::TransformStringLiteral(StringLiteral *E) { | ||||||||
9596 | return E; | ||||||||
9597 | } | ||||||||
9598 | |||||||||
9599 | template<typename Derived> | ||||||||
9600 | ExprResult | ||||||||
9601 | TreeTransform<Derived>::TransformCharacterLiteral(CharacterLiteral *E) { | ||||||||
9602 | return E; | ||||||||
9603 | } | ||||||||
9604 | |||||||||
9605 | template<typename Derived> | ||||||||
9606 | ExprResult | ||||||||
9607 | TreeTransform<Derived>::TransformUserDefinedLiteral(UserDefinedLiteral *E) { | ||||||||
9608 | if (FunctionDecl *FD = E->getDirectCallee()) | ||||||||
9609 | SemaRef.MarkFunctionReferenced(E->getBeginLoc(), FD); | ||||||||
9610 | return SemaRef.MaybeBindToTemporary(E); | ||||||||
9611 | } | ||||||||
9612 | |||||||||
9613 | template<typename Derived> | ||||||||
9614 | ExprResult | ||||||||
9615 | TreeTransform<Derived>::TransformGenericSelectionExpr(GenericSelectionExpr *E) { | ||||||||
9616 | ExprResult ControllingExpr = | ||||||||
9617 | getDerived().TransformExpr(E->getControllingExpr()); | ||||||||
9618 | if (ControllingExpr.isInvalid()) | ||||||||
9619 | return ExprError(); | ||||||||
9620 | |||||||||
9621 | SmallVector<Expr *, 4> AssocExprs; | ||||||||
9622 | SmallVector<TypeSourceInfo *, 4> AssocTypes; | ||||||||
9623 | for (const GenericSelectionExpr::Association Assoc : E->associations()) { | ||||||||
9624 | TypeSourceInfo *TSI = Assoc.getTypeSourceInfo(); | ||||||||
9625 | if (TSI) { | ||||||||
9626 | TypeSourceInfo *AssocType = getDerived().TransformType(TSI); | ||||||||
9627 | if (!AssocType) | ||||||||
9628 | return ExprError(); | ||||||||
9629 | AssocTypes.push_back(AssocType); | ||||||||
9630 | } else { | ||||||||
9631 | AssocTypes.push_back(nullptr); | ||||||||
9632 | } | ||||||||
9633 | |||||||||
9634 | ExprResult AssocExpr = | ||||||||
9635 | getDerived().TransformExpr(Assoc.getAssociationExpr()); | ||||||||
9636 | if (AssocExpr.isInvalid()) | ||||||||
9637 | return ExprError(); | ||||||||
9638 | AssocExprs.push_back(AssocExpr.get()); | ||||||||
9639 | } | ||||||||
9640 | |||||||||
9641 | return getDerived().RebuildGenericSelectionExpr(E->getGenericLoc(), | ||||||||
9642 | E->getDefaultLoc(), | ||||||||
9643 | E->getRParenLoc(), | ||||||||
9644 | ControllingExpr.get(), | ||||||||
9645 | AssocTypes, | ||||||||
9646 | AssocExprs); | ||||||||
9647 | } | ||||||||
9648 | |||||||||
9649 | template<typename Derived> | ||||||||
9650 | ExprResult | ||||||||
9651 | TreeTransform<Derived>::TransformParenExpr(ParenExpr *E) { | ||||||||
9652 | ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr()); | ||||||||
9653 | if (SubExpr.isInvalid()) | ||||||||
9654 | return ExprError(); | ||||||||
9655 | |||||||||
9656 | if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getSubExpr()) | ||||||||
9657 | return E; | ||||||||
9658 | |||||||||
9659 | return getDerived().RebuildParenExpr(SubExpr.get(), E->getLParen(), | ||||||||
9660 | E->getRParen()); | ||||||||
9661 | } | ||||||||
9662 | |||||||||
9663 | /// The operand of a unary address-of operator has special rules: it's | ||||||||
9664 | /// allowed to refer to a non-static member of a class even if there's no 'this' | ||||||||
9665 | /// object available. | ||||||||
9666 | template<typename Derived> | ||||||||
9667 | ExprResult | ||||||||
9668 | TreeTransform<Derived>::TransformAddressOfOperand(Expr *E) { | ||||||||
9669 | if (DependentScopeDeclRefExpr *DRE = dyn_cast<DependentScopeDeclRefExpr>(E)) | ||||||||
9670 | return getDerived().TransformDependentScopeDeclRefExpr(DRE, true, nullptr); | ||||||||
9671 | else | ||||||||
9672 | return getDerived().TransformExpr(E); | ||||||||
9673 | } | ||||||||
9674 | |||||||||
9675 | template<typename Derived> | ||||||||
9676 | ExprResult | ||||||||
9677 | TreeTransform<Derived>::TransformUnaryOperator(UnaryOperator *E) { | ||||||||
9678 | ExprResult SubExpr; | ||||||||
9679 | if (E->getOpcode() == UO_AddrOf) | ||||||||
9680 | SubExpr = TransformAddressOfOperand(E->getSubExpr()); | ||||||||
9681 | else | ||||||||
9682 | SubExpr = TransformExpr(E->getSubExpr()); | ||||||||
9683 | if (SubExpr.isInvalid()) | ||||||||
9684 | return ExprError(); | ||||||||
9685 | |||||||||
9686 | if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getSubExpr()) | ||||||||
9687 | return E; | ||||||||
9688 | |||||||||
9689 | return getDerived().RebuildUnaryOperator(E->getOperatorLoc(), | ||||||||
9690 | E->getOpcode(), | ||||||||
9691 | SubExpr.get()); | ||||||||
9692 | } | ||||||||
9693 | |||||||||
9694 | template<typename Derived> | ||||||||
9695 | ExprResult | ||||||||
9696 | TreeTransform<Derived>::TransformOffsetOfExpr(OffsetOfExpr *E) { | ||||||||
9697 | // Transform the type. | ||||||||
9698 | TypeSourceInfo *Type = getDerived().TransformType(E->getTypeSourceInfo()); | ||||||||
9699 | if (!Type) | ||||||||
9700 | return ExprError(); | ||||||||
9701 | |||||||||
9702 | // Transform all of the components into components similar to what the | ||||||||
9703 | // parser uses. | ||||||||
9704 | // FIXME: It would be slightly more efficient in the non-dependent case to | ||||||||
9705 | // just map FieldDecls, rather than requiring the rebuilder to look for | ||||||||
9706 | // the fields again. However, __builtin_offsetof is rare enough in | ||||||||
9707 | // template code that we don't care. | ||||||||
9708 | bool ExprChanged = false; | ||||||||
9709 | typedef Sema::OffsetOfComponent Component; | ||||||||
9710 | SmallVector<Component, 4> Components; | ||||||||
9711 | for (unsigned I = 0, N = E->getNumComponents(); I != N; ++I) { | ||||||||
9712 | const OffsetOfNode &ON = E->getComponent(I); | ||||||||
9713 | Component Comp; | ||||||||
9714 | Comp.isBrackets = true; | ||||||||
9715 | Comp.LocStart = ON.getSourceRange().getBegin(); | ||||||||
9716 | Comp.LocEnd = ON.getSourceRange().getEnd(); | ||||||||
9717 | switch (ON.getKind()) { | ||||||||
9718 | case OffsetOfNode::Array: { | ||||||||
9719 | Expr *FromIndex = E->getIndexExpr(ON.getArrayExprIndex()); | ||||||||
9720 | ExprResult Index = getDerived().TransformExpr(FromIndex); | ||||||||
9721 | if (Index.isInvalid()) | ||||||||
9722 | return ExprError(); | ||||||||
9723 | |||||||||
9724 | ExprChanged = ExprChanged || Index.get() != FromIndex; | ||||||||
9725 | Comp.isBrackets = true; | ||||||||
9726 | Comp.U.E = Index.get(); | ||||||||
9727 | break; | ||||||||
9728 | } | ||||||||
9729 | |||||||||
9730 | case OffsetOfNode::Field: | ||||||||
9731 | case OffsetOfNode::Identifier: | ||||||||
9732 | Comp.isBrackets = false; | ||||||||
9733 | Comp.U.IdentInfo = ON.getFieldName(); | ||||||||
9734 | if (!Comp.U.IdentInfo) | ||||||||
9735 | continue; | ||||||||
9736 | |||||||||
9737 | break; | ||||||||
9738 | |||||||||
9739 | case OffsetOfNode::Base: | ||||||||
9740 | // Will be recomputed during the rebuild. | ||||||||
9741 | continue; | ||||||||
9742 | } | ||||||||
9743 | |||||||||
9744 | Components.push_back(Comp); | ||||||||
9745 | } | ||||||||
9746 | |||||||||
9747 | // If nothing changed, retain the existing expression. | ||||||||
9748 | if (!getDerived().AlwaysRebuild() && | ||||||||
9749 | Type == E->getTypeSourceInfo() && | ||||||||
9750 | !ExprChanged) | ||||||||
9751 | return E; | ||||||||
9752 | |||||||||
9753 | // Build a new offsetof expression. | ||||||||
9754 | return getDerived().RebuildOffsetOfExpr(E->getOperatorLoc(), Type, | ||||||||
9755 | Components, E->getRParenLoc()); | ||||||||
9756 | } | ||||||||
9757 | |||||||||
9758 | template<typename Derived> | ||||||||
9759 | ExprResult | ||||||||
9760 | TreeTransform<Derived>::TransformOpaqueValueExpr(OpaqueValueExpr *E) { | ||||||||
9761 | assert((!E->getSourceExpr() || getDerived().AlreadyTransformed(E->getType())) &&(((!E->getSourceExpr() || getDerived().AlreadyTransformed( E->getType())) && "opaque value expression requires transformation" ) ? static_cast<void> (0) : __assert_fail ("(!E->getSourceExpr() || getDerived().AlreadyTransformed(E->getType())) && \"opaque value expression requires transformation\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 9762, __PRETTY_FUNCTION__)) | ||||||||
9762 | "opaque value expression requires transformation")(((!E->getSourceExpr() || getDerived().AlreadyTransformed( E->getType())) && "opaque value expression requires transformation" ) ? static_cast<void> (0) : __assert_fail ("(!E->getSourceExpr() || getDerived().AlreadyTransformed(E->getType())) && \"opaque value expression requires transformation\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 9762, __PRETTY_FUNCTION__)); | ||||||||
9763 | return E; | ||||||||
9764 | } | ||||||||
9765 | |||||||||
9766 | template<typename Derived> | ||||||||
9767 | ExprResult | ||||||||
9768 | TreeTransform<Derived>::TransformTypoExpr(TypoExpr *E) { | ||||||||
9769 | return E; | ||||||||
9770 | } | ||||||||
9771 | |||||||||
9772 | template<typename Derived> | ||||||||
9773 | ExprResult | ||||||||
9774 | TreeTransform<Derived>::TransformPseudoObjectExpr(PseudoObjectExpr *E) { | ||||||||
9775 | // Rebuild the syntactic form. The original syntactic form has | ||||||||
9776 | // opaque-value expressions in it, so strip those away and rebuild | ||||||||
9777 | // the result. This is a really awful way of doing this, but the | ||||||||
9778 | // better solution (rebuilding the semantic expressions and | ||||||||
9779 | // rebinding OVEs as necessary) doesn't work; we'd need | ||||||||
9780 | // TreeTransform to not strip away implicit conversions. | ||||||||
9781 | Expr *newSyntacticForm = SemaRef.recreateSyntacticForm(E); | ||||||||
9782 | ExprResult result = getDerived().TransformExpr(newSyntacticForm); | ||||||||
9783 | if (result.isInvalid()) return ExprError(); | ||||||||
9784 | |||||||||
9785 | // If that gives us a pseudo-object result back, the pseudo-object | ||||||||
9786 | // expression must have been an lvalue-to-rvalue conversion which we | ||||||||
9787 | // should reapply. | ||||||||
9788 | if (result.get()->hasPlaceholderType(BuiltinType::PseudoObject)) | ||||||||
9789 | result = SemaRef.checkPseudoObjectRValue(result.get()); | ||||||||
9790 | |||||||||
9791 | return result; | ||||||||
9792 | } | ||||||||
9793 | |||||||||
9794 | template<typename Derived> | ||||||||
9795 | ExprResult | ||||||||
9796 | TreeTransform<Derived>::TransformUnaryExprOrTypeTraitExpr( | ||||||||
9797 | UnaryExprOrTypeTraitExpr *E) { | ||||||||
9798 | if (E->isArgumentType()) { | ||||||||
9799 | TypeSourceInfo *OldT = E->getArgumentTypeInfo(); | ||||||||
9800 | |||||||||
9801 | TypeSourceInfo *NewT = getDerived().TransformType(OldT); | ||||||||
9802 | if (!NewT) | ||||||||
9803 | return ExprError(); | ||||||||
9804 | |||||||||
9805 | if (!getDerived().AlwaysRebuild() && OldT == NewT) | ||||||||
9806 | return E; | ||||||||
9807 | |||||||||
9808 | return getDerived().RebuildUnaryExprOrTypeTrait(NewT, E->getOperatorLoc(), | ||||||||
9809 | E->getKind(), | ||||||||
9810 | E->getSourceRange()); | ||||||||
9811 | } | ||||||||
9812 | |||||||||
9813 | // C++0x [expr.sizeof]p1: | ||||||||
9814 | // The operand is either an expression, which is an unevaluated operand | ||||||||
9815 | // [...] | ||||||||
9816 | EnterExpressionEvaluationContext Unevaluated( | ||||||||
9817 | SemaRef, Sema::ExpressionEvaluationContext::Unevaluated, | ||||||||
9818 | Sema::ReuseLambdaContextDecl); | ||||||||
9819 | |||||||||
9820 | // Try to recover if we have something like sizeof(T::X) where X is a type. | ||||||||
9821 | // Notably, there must be *exactly* one set of parens if X is a type. | ||||||||
9822 | TypeSourceInfo *RecoveryTSI = nullptr; | ||||||||
9823 | ExprResult SubExpr; | ||||||||
9824 | auto *PE = dyn_cast<ParenExpr>(E->getArgumentExpr()); | ||||||||
9825 | if (auto *DRE = | ||||||||
9826 | PE ? dyn_cast<DependentScopeDeclRefExpr>(PE->getSubExpr()) : nullptr) | ||||||||
9827 | SubExpr = getDerived().TransformParenDependentScopeDeclRefExpr( | ||||||||
9828 | PE, DRE, false, &RecoveryTSI); | ||||||||
9829 | else | ||||||||
9830 | SubExpr = getDerived().TransformExpr(E->getArgumentExpr()); | ||||||||
9831 | |||||||||
9832 | if (RecoveryTSI) { | ||||||||
9833 | return getDerived().RebuildUnaryExprOrTypeTrait( | ||||||||
9834 | RecoveryTSI, E->getOperatorLoc(), E->getKind(), E->getSourceRange()); | ||||||||
9835 | } else if (SubExpr.isInvalid()) | ||||||||
9836 | return ExprError(); | ||||||||
9837 | |||||||||
9838 | if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getArgumentExpr()) | ||||||||
9839 | return E; | ||||||||
9840 | |||||||||
9841 | return getDerived().RebuildUnaryExprOrTypeTrait(SubExpr.get(), | ||||||||
9842 | E->getOperatorLoc(), | ||||||||
9843 | E->getKind(), | ||||||||
9844 | E->getSourceRange()); | ||||||||
9845 | } | ||||||||
9846 | |||||||||
9847 | template<typename Derived> | ||||||||
9848 | ExprResult | ||||||||
9849 | TreeTransform<Derived>::TransformArraySubscriptExpr(ArraySubscriptExpr *E) { | ||||||||
9850 | ExprResult LHS = getDerived().TransformExpr(E->getLHS()); | ||||||||
9851 | if (LHS.isInvalid()) | ||||||||
9852 | return ExprError(); | ||||||||
9853 | |||||||||
9854 | ExprResult RHS = getDerived().TransformExpr(E->getRHS()); | ||||||||
9855 | if (RHS.isInvalid()) | ||||||||
9856 | return ExprError(); | ||||||||
9857 | |||||||||
9858 | |||||||||
9859 | if (!getDerived().AlwaysRebuild() && | ||||||||
9860 | LHS.get() == E->getLHS() && | ||||||||
9861 | RHS.get() == E->getRHS()) | ||||||||
9862 | return E; | ||||||||
9863 | |||||||||
9864 | return getDerived().RebuildArraySubscriptExpr( | ||||||||
9865 | LHS.get(), | ||||||||
9866 | /*FIXME:*/ E->getLHS()->getBeginLoc(), RHS.get(), E->getRBracketLoc()); | ||||||||
9867 | } | ||||||||
9868 | |||||||||
9869 | template <typename Derived> | ||||||||
9870 | ExprResult | ||||||||
9871 | TreeTransform<Derived>::TransformOMPArraySectionExpr(OMPArraySectionExpr *E) { | ||||||||
9872 | ExprResult Base = getDerived().TransformExpr(E->getBase()); | ||||||||
9873 | if (Base.isInvalid()) | ||||||||
9874 | return ExprError(); | ||||||||
9875 | |||||||||
9876 | ExprResult LowerBound; | ||||||||
9877 | if (E->getLowerBound()) { | ||||||||
9878 | LowerBound = getDerived().TransformExpr(E->getLowerBound()); | ||||||||
9879 | if (LowerBound.isInvalid()) | ||||||||
9880 | return ExprError(); | ||||||||
9881 | } | ||||||||
9882 | |||||||||
9883 | ExprResult Length; | ||||||||
9884 | if (E->getLength()) { | ||||||||
9885 | Length = getDerived().TransformExpr(E->getLength()); | ||||||||
9886 | if (Length.isInvalid()) | ||||||||
9887 | return ExprError(); | ||||||||
9888 | } | ||||||||
9889 | |||||||||
9890 | if (!getDerived().AlwaysRebuild() && Base.get() == E->getBase() && | ||||||||
9891 | LowerBound.get() == E->getLowerBound() && Length.get() == E->getLength()) | ||||||||
9892 | return E; | ||||||||
9893 | |||||||||
9894 | return getDerived().RebuildOMPArraySectionExpr( | ||||||||
9895 | Base.get(), E->getBase()->getEndLoc(), LowerBound.get(), E->getColonLoc(), | ||||||||
9896 | Length.get(), E->getRBracketLoc()); | ||||||||
9897 | } | ||||||||
9898 | |||||||||
9899 | template<typename Derived> | ||||||||
9900 | ExprResult | ||||||||
9901 | TreeTransform<Derived>::TransformCallExpr(CallExpr *E) { | ||||||||
9902 | // Transform the callee. | ||||||||
9903 | ExprResult Callee = getDerived().TransformExpr(E->getCallee()); | ||||||||
9904 | if (Callee.isInvalid()) | ||||||||
9905 | return ExprError(); | ||||||||
9906 | |||||||||
9907 | // Transform arguments. | ||||||||
9908 | bool ArgChanged = false; | ||||||||
9909 | SmallVector<Expr*, 8> Args; | ||||||||
9910 | if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), true, Args, | ||||||||
9911 | &ArgChanged)) | ||||||||
9912 | return ExprError(); | ||||||||
9913 | |||||||||
9914 | if (!getDerived().AlwaysRebuild() && | ||||||||
9915 | Callee.get() == E->getCallee() && | ||||||||
9916 | !ArgChanged) | ||||||||
9917 | return SemaRef.MaybeBindToTemporary(E); | ||||||||
9918 | |||||||||
9919 | // FIXME: Wrong source location information for the '('. | ||||||||
9920 | SourceLocation FakeLParenLoc | ||||||||
9921 | = ((Expr *)Callee.get())->getSourceRange().getBegin(); | ||||||||
9922 | return getDerived().RebuildCallExpr(Callee.get(), FakeLParenLoc, | ||||||||
9923 | Args, | ||||||||
9924 | E->getRParenLoc()); | ||||||||
9925 | } | ||||||||
9926 | |||||||||
9927 | template<typename Derived> | ||||||||
9928 | ExprResult | ||||||||
9929 | TreeTransform<Derived>::TransformMemberExpr(MemberExpr *E) { | ||||||||
9930 | ExprResult Base = getDerived().TransformExpr(E->getBase()); | ||||||||
9931 | if (Base.isInvalid()) | ||||||||
9932 | return ExprError(); | ||||||||
9933 | |||||||||
9934 | NestedNameSpecifierLoc QualifierLoc; | ||||||||
9935 | if (E->hasQualifier()) { | ||||||||
9936 | QualifierLoc | ||||||||
9937 | = getDerived().TransformNestedNameSpecifierLoc(E->getQualifierLoc()); | ||||||||
9938 | |||||||||
9939 | if (!QualifierLoc) | ||||||||
9940 | return ExprError(); | ||||||||
9941 | } | ||||||||
9942 | SourceLocation TemplateKWLoc = E->getTemplateKeywordLoc(); | ||||||||
9943 | |||||||||
9944 | ValueDecl *Member | ||||||||
9945 | = cast_or_null<ValueDecl>(getDerived().TransformDecl(E->getMemberLoc(), | ||||||||
9946 | E->getMemberDecl())); | ||||||||
9947 | if (!Member) | ||||||||
9948 | return ExprError(); | ||||||||
9949 | |||||||||
9950 | NamedDecl *FoundDecl = E->getFoundDecl(); | ||||||||
9951 | if (FoundDecl == E->getMemberDecl()) { | ||||||||
9952 | FoundDecl = Member; | ||||||||
9953 | } else { | ||||||||
9954 | FoundDecl = cast_or_null<NamedDecl>( | ||||||||
9955 | getDerived().TransformDecl(E->getMemberLoc(), FoundDecl)); | ||||||||
9956 | if (!FoundDecl) | ||||||||
9957 | return ExprError(); | ||||||||
9958 | } | ||||||||
9959 | |||||||||
9960 | if (!getDerived().AlwaysRebuild() && | ||||||||
9961 | Base.get() == E->getBase() && | ||||||||
9962 | QualifierLoc == E->getQualifierLoc() && | ||||||||
9963 | Member == E->getMemberDecl() && | ||||||||
9964 | FoundDecl == E->getFoundDecl() && | ||||||||
9965 | !E->hasExplicitTemplateArgs()) { | ||||||||
9966 | |||||||||
9967 | // Mark it referenced in the new context regardless. | ||||||||
9968 | // FIXME: this is a bit instantiation-specific. | ||||||||
9969 | SemaRef.MarkMemberReferenced(E); | ||||||||
9970 | |||||||||
9971 | return E; | ||||||||
9972 | } | ||||||||
9973 | |||||||||
9974 | TemplateArgumentListInfo TransArgs; | ||||||||
9975 | if (E->hasExplicitTemplateArgs()) { | ||||||||
9976 | TransArgs.setLAngleLoc(E->getLAngleLoc()); | ||||||||
9977 | TransArgs.setRAngleLoc(E->getRAngleLoc()); | ||||||||
9978 | if (getDerived().TransformTemplateArguments(E->getTemplateArgs(), | ||||||||
9979 | E->getNumTemplateArgs(), | ||||||||
9980 | TransArgs)) | ||||||||
9981 | return ExprError(); | ||||||||
9982 | } | ||||||||
9983 | |||||||||
9984 | // FIXME: Bogus source location for the operator | ||||||||
9985 | SourceLocation FakeOperatorLoc = | ||||||||
9986 | SemaRef.getLocForEndOfToken(E->getBase()->getSourceRange().getEnd()); | ||||||||
9987 | |||||||||
9988 | // FIXME: to do this check properly, we will need to preserve the | ||||||||
9989 | // first-qualifier-in-scope here, just in case we had a dependent | ||||||||
9990 | // base (and therefore couldn't do the check) and a | ||||||||
9991 | // nested-name-qualifier (and therefore could do the lookup). | ||||||||
9992 | NamedDecl *FirstQualifierInScope = nullptr; | ||||||||
9993 | DeclarationNameInfo MemberNameInfo = E->getMemberNameInfo(); | ||||||||
9994 | if (MemberNameInfo.getName()) { | ||||||||
9995 | MemberNameInfo = getDerived().TransformDeclarationNameInfo(MemberNameInfo); | ||||||||
9996 | if (!MemberNameInfo.getName()) | ||||||||
9997 | return ExprError(); | ||||||||
9998 | } | ||||||||
9999 | |||||||||
10000 | return getDerived().RebuildMemberExpr(Base.get(), FakeOperatorLoc, | ||||||||
10001 | E->isArrow(), | ||||||||
10002 | QualifierLoc, | ||||||||
10003 | TemplateKWLoc, | ||||||||
10004 | MemberNameInfo, | ||||||||
10005 | Member, | ||||||||
10006 | FoundDecl, | ||||||||
10007 | (E->hasExplicitTemplateArgs() | ||||||||
10008 | ? &TransArgs : nullptr), | ||||||||
10009 | FirstQualifierInScope); | ||||||||
10010 | } | ||||||||
10011 | |||||||||
10012 | template<typename Derived> | ||||||||
10013 | ExprResult | ||||||||
10014 | TreeTransform<Derived>::TransformBinaryOperator(BinaryOperator *E) { | ||||||||
10015 | ExprResult LHS = getDerived().TransformExpr(E->getLHS()); | ||||||||
10016 | if (LHS.isInvalid()) | ||||||||
10017 | return ExprError(); | ||||||||
10018 | |||||||||
10019 | ExprResult RHS = getDerived().TransformExpr(E->getRHS()); | ||||||||
10020 | if (RHS.isInvalid()) | ||||||||
10021 | return ExprError(); | ||||||||
10022 | |||||||||
10023 | if (!getDerived().AlwaysRebuild() && | ||||||||
10024 | LHS.get() == E->getLHS() && | ||||||||
10025 | RHS.get() == E->getRHS()) | ||||||||
10026 | return E; | ||||||||
10027 | |||||||||
10028 | Sema::FPFeaturesStateRAII FPFeaturesState(getSema()); | ||||||||
10029 | getSema().FPFeatures = E->getFPFeatures(); | ||||||||
10030 | |||||||||
10031 | return getDerived().RebuildBinaryOperator(E->getOperatorLoc(), E->getOpcode(), | ||||||||
10032 | LHS.get(), RHS.get()); | ||||||||
10033 | } | ||||||||
10034 | |||||||||
10035 | template <typename Derived> | ||||||||
10036 | ExprResult TreeTransform<Derived>::TransformCXXRewrittenBinaryOperator( | ||||||||
10037 | CXXRewrittenBinaryOperator *E) { | ||||||||
10038 | CXXRewrittenBinaryOperator::DecomposedForm Decomp = E->getDecomposedForm(); | ||||||||
10039 | |||||||||
10040 | ExprResult LHS = getDerived().TransformExpr(const_cast<Expr*>(Decomp.LHS)); | ||||||||
10041 | if (LHS.isInvalid()) | ||||||||
10042 | return ExprError(); | ||||||||
10043 | |||||||||
10044 | ExprResult RHS = getDerived().TransformExpr(const_cast<Expr*>(Decomp.RHS)); | ||||||||
10045 | if (RHS.isInvalid()) | ||||||||
10046 | return ExprError(); | ||||||||
10047 | |||||||||
10048 | if (!getDerived().AlwaysRebuild() && | ||||||||
10049 | LHS.get() == Decomp.LHS && | ||||||||
10050 | RHS.get() == Decomp.RHS) | ||||||||
10051 | return E; | ||||||||
10052 | |||||||||
10053 | // Extract the already-resolved callee declarations so that we can restrict | ||||||||
10054 | // ourselves to using them as the unqualified lookup results when rebuilding. | ||||||||
10055 | UnresolvedSet<2> UnqualLookups; | ||||||||
10056 | Expr *PossibleBinOps[] = {E->getSemanticForm(), | ||||||||
10057 | const_cast<Expr *>(Decomp.InnerBinOp)}; | ||||||||
10058 | for (Expr *PossibleBinOp : PossibleBinOps) { | ||||||||
10059 | auto *Op = dyn_cast<CXXOperatorCallExpr>(PossibleBinOp->IgnoreImplicit()); | ||||||||
10060 | if (!Op) | ||||||||
10061 | continue; | ||||||||
10062 | auto *Callee = dyn_cast<DeclRefExpr>(Op->getCallee()->IgnoreImplicit()); | ||||||||
10063 | if (!Callee || isa<CXXMethodDecl>(Callee->getDecl())) | ||||||||
10064 | continue; | ||||||||
10065 | |||||||||
10066 | // Transform the callee in case we built a call to a local extern | ||||||||
10067 | // declaration. | ||||||||
10068 | NamedDecl *Found = cast_or_null<NamedDecl>(getDerived().TransformDecl( | ||||||||
10069 | E->getOperatorLoc(), Callee->getFoundDecl())); | ||||||||
10070 | if (!Found) | ||||||||
10071 | return ExprError(); | ||||||||
10072 | UnqualLookups.addDecl(Found); | ||||||||
10073 | } | ||||||||
10074 | |||||||||
10075 | return getDerived().RebuildCXXRewrittenBinaryOperator( | ||||||||
10076 | E->getOperatorLoc(), Decomp.Opcode, UnqualLookups, LHS.get(), RHS.get()); | ||||||||
10077 | } | ||||||||
10078 | |||||||||
10079 | template<typename Derived> | ||||||||
10080 | ExprResult | ||||||||
10081 | TreeTransform<Derived>::TransformCompoundAssignOperator( | ||||||||
10082 | CompoundAssignOperator *E) { | ||||||||
10083 | return getDerived().TransformBinaryOperator(E); | ||||||||
10084 | } | ||||||||
10085 | |||||||||
10086 | template<typename Derived> | ||||||||
10087 | ExprResult TreeTransform<Derived>:: | ||||||||
10088 | TransformBinaryConditionalOperator(BinaryConditionalOperator *e) { | ||||||||
10089 | // Just rebuild the common and RHS expressions and see whether we | ||||||||
10090 | // get any changes. | ||||||||
10091 | |||||||||
10092 | ExprResult commonExpr = getDerived().TransformExpr(e->getCommon()); | ||||||||
10093 | if (commonExpr.isInvalid()) | ||||||||
10094 | return ExprError(); | ||||||||
10095 | |||||||||
10096 | ExprResult rhs = getDerived().TransformExpr(e->getFalseExpr()); | ||||||||
10097 | if (rhs.isInvalid()) | ||||||||
10098 | return ExprError(); | ||||||||
10099 | |||||||||
10100 | if (!getDerived().AlwaysRebuild() && | ||||||||
10101 | commonExpr.get() == e->getCommon() && | ||||||||
10102 | rhs.get() == e->getFalseExpr()) | ||||||||
10103 | return e; | ||||||||
10104 | |||||||||
10105 | return getDerived().RebuildConditionalOperator(commonExpr.get(), | ||||||||
10106 | e->getQuestionLoc(), | ||||||||
10107 | nullptr, | ||||||||
10108 | e->getColonLoc(), | ||||||||
10109 | rhs.get()); | ||||||||
10110 | } | ||||||||
10111 | |||||||||
10112 | template<typename Derived> | ||||||||
10113 | ExprResult | ||||||||
10114 | TreeTransform<Derived>::TransformConditionalOperator(ConditionalOperator *E) { | ||||||||
10115 | ExprResult Cond = getDerived().TransformExpr(E->getCond()); | ||||||||
10116 | if (Cond.isInvalid()) | ||||||||
10117 | return ExprError(); | ||||||||
10118 | |||||||||
10119 | ExprResult LHS = getDerived().TransformExpr(E->getLHS()); | ||||||||
10120 | if (LHS.isInvalid()) | ||||||||
10121 | return ExprError(); | ||||||||
10122 | |||||||||
10123 | ExprResult RHS = getDerived().TransformExpr(E->getRHS()); | ||||||||
10124 | if (RHS.isInvalid()) | ||||||||
10125 | return ExprError(); | ||||||||
10126 | |||||||||
10127 | if (!getDerived().AlwaysRebuild() && | ||||||||
10128 | Cond.get() == E->getCond() && | ||||||||
10129 | LHS.get() == E->getLHS() && | ||||||||
10130 | RHS.get() == E->getRHS()) | ||||||||
10131 | return E; | ||||||||
10132 | |||||||||
10133 | return getDerived().RebuildConditionalOperator(Cond.get(), | ||||||||
10134 | E->getQuestionLoc(), | ||||||||
10135 | LHS.get(), | ||||||||
10136 | E->getColonLoc(), | ||||||||
10137 | RHS.get()); | ||||||||
10138 | } | ||||||||
10139 | |||||||||
10140 | template<typename Derived> | ||||||||
10141 | ExprResult | ||||||||
10142 | TreeTransform<Derived>::TransformImplicitCastExpr(ImplicitCastExpr *E) { | ||||||||
10143 | // Implicit casts are eliminated during transformation, since they | ||||||||
10144 | // will be recomputed by semantic analysis after transformation. | ||||||||
10145 | return getDerived().TransformExpr(E->getSubExprAsWritten()); | ||||||||
10146 | } | ||||||||
10147 | |||||||||
10148 | template<typename Derived> | ||||||||
10149 | ExprResult | ||||||||
10150 | TreeTransform<Derived>::TransformCStyleCastExpr(CStyleCastExpr *E) { | ||||||||
10151 | TypeSourceInfo *Type = getDerived().TransformType(E->getTypeInfoAsWritten()); | ||||||||
10152 | if (!Type) | ||||||||
10153 | return ExprError(); | ||||||||
10154 | |||||||||
10155 | ExprResult SubExpr | ||||||||
10156 | = getDerived().TransformExpr(E->getSubExprAsWritten()); | ||||||||
10157 | if (SubExpr.isInvalid()) | ||||||||
10158 | return ExprError(); | ||||||||
10159 | |||||||||
10160 | if (!getDerived().AlwaysRebuild() && | ||||||||
10161 | Type == E->getTypeInfoAsWritten() && | ||||||||
10162 | SubExpr.get() == E->getSubExpr()) | ||||||||
10163 | return E; | ||||||||
10164 | |||||||||
10165 | return getDerived().RebuildCStyleCastExpr(E->getLParenLoc(), | ||||||||
10166 | Type, | ||||||||
10167 | E->getRParenLoc(), | ||||||||
10168 | SubExpr.get()); | ||||||||
10169 | } | ||||||||
10170 | |||||||||
10171 | template<typename Derived> | ||||||||
10172 | ExprResult | ||||||||
10173 | TreeTransform<Derived>::TransformCompoundLiteralExpr(CompoundLiteralExpr *E) { | ||||||||
10174 | TypeSourceInfo *OldT = E->getTypeSourceInfo(); | ||||||||
10175 | TypeSourceInfo *NewT = getDerived().TransformType(OldT); | ||||||||
10176 | if (!NewT) | ||||||||
10177 | return ExprError(); | ||||||||
10178 | |||||||||
10179 | ExprResult Init = getDerived().TransformExpr(E->getInitializer()); | ||||||||
10180 | if (Init.isInvalid()) | ||||||||
10181 | return ExprError(); | ||||||||
10182 | |||||||||
10183 | if (!getDerived().AlwaysRebuild() && | ||||||||
10184 | OldT == NewT && | ||||||||
10185 | Init.get() == E->getInitializer()) | ||||||||
10186 | return SemaRef.MaybeBindToTemporary(E); | ||||||||
10187 | |||||||||
10188 | // Note: the expression type doesn't necessarily match the | ||||||||
10189 | // type-as-written, but that's okay, because it should always be | ||||||||
10190 | // derivable from the initializer. | ||||||||
10191 | |||||||||
10192 | return getDerived().RebuildCompoundLiteralExpr( | ||||||||
10193 | E->getLParenLoc(), NewT, | ||||||||
10194 | /*FIXME:*/ E->getInitializer()->getEndLoc(), Init.get()); | ||||||||
10195 | } | ||||||||
10196 | |||||||||
10197 | template<typename Derived> | ||||||||
10198 | ExprResult | ||||||||
10199 | TreeTransform<Derived>::TransformExtVectorElementExpr(ExtVectorElementExpr *E) { | ||||||||
10200 | ExprResult Base = getDerived().TransformExpr(E->getBase()); | ||||||||
10201 | if (Base.isInvalid()) | ||||||||
10202 | return ExprError(); | ||||||||
10203 | |||||||||
10204 | if (!getDerived().AlwaysRebuild() && | ||||||||
10205 | Base.get() == E->getBase()) | ||||||||
10206 | return E; | ||||||||
10207 | |||||||||
10208 | // FIXME: Bad source location | ||||||||
10209 | SourceLocation FakeOperatorLoc = | ||||||||
10210 | SemaRef.getLocForEndOfToken(E->getBase()->getEndLoc()); | ||||||||
10211 | return getDerived().RebuildExtVectorElementExpr(Base.get(), FakeOperatorLoc, | ||||||||
10212 | E->getAccessorLoc(), | ||||||||
10213 | E->getAccessor()); | ||||||||
10214 | } | ||||||||
10215 | |||||||||
10216 | template<typename Derived> | ||||||||
10217 | ExprResult | ||||||||
10218 | TreeTransform<Derived>::TransformInitListExpr(InitListExpr *E) { | ||||||||
10219 | if (InitListExpr *Syntactic = E->getSyntacticForm()) | ||||||||
10220 | E = Syntactic; | ||||||||
10221 | |||||||||
10222 | bool InitChanged = false; | ||||||||
10223 | |||||||||
10224 | EnterExpressionEvaluationContext Context( | ||||||||
10225 | getSema(), EnterExpressionEvaluationContext::InitList); | ||||||||
10226 | |||||||||
10227 | SmallVector<Expr*, 4> Inits; | ||||||||
10228 | if (getDerived().TransformExprs(E->getInits(), E->getNumInits(), false, | ||||||||
10229 | Inits, &InitChanged)) | ||||||||
10230 | return ExprError(); | ||||||||
10231 | |||||||||
10232 | if (!getDerived().AlwaysRebuild() && !InitChanged) { | ||||||||
10233 | // FIXME: Attempt to reuse the existing syntactic form of the InitListExpr | ||||||||
10234 | // in some cases. We can't reuse it in general, because the syntactic and | ||||||||
10235 | // semantic forms are linked, and we can't know that semantic form will | ||||||||
10236 | // match even if the syntactic form does. | ||||||||
10237 | } | ||||||||
10238 | |||||||||
10239 | return getDerived().RebuildInitList(E->getLBraceLoc(), Inits, | ||||||||
10240 | E->getRBraceLoc()); | ||||||||
10241 | } | ||||||||
10242 | |||||||||
10243 | template<typename Derived> | ||||||||
10244 | ExprResult | ||||||||
10245 | TreeTransform<Derived>::TransformDesignatedInitExpr(DesignatedInitExpr *E) { | ||||||||
10246 | Designation Desig; | ||||||||
10247 | |||||||||
10248 | // transform the initializer value | ||||||||
10249 | ExprResult Init = getDerived().TransformExpr(E->getInit()); | ||||||||
10250 | if (Init.isInvalid()) | ||||||||
10251 | return ExprError(); | ||||||||
10252 | |||||||||
10253 | // transform the designators. | ||||||||
10254 | SmallVector<Expr*, 4> ArrayExprs; | ||||||||
10255 | bool ExprChanged = false; | ||||||||
10256 | for (const DesignatedInitExpr::Designator &D : E->designators()) { | ||||||||
10257 | if (D.isFieldDesignator()) { | ||||||||
10258 | Desig.AddDesignator(Designator::getField(D.getFieldName(), | ||||||||
10259 | D.getDotLoc(), | ||||||||
10260 | D.getFieldLoc())); | ||||||||
10261 | if (D.getField()) { | ||||||||
10262 | FieldDecl *Field = cast_or_null<FieldDecl>( | ||||||||
10263 | getDerived().TransformDecl(D.getFieldLoc(), D.getField())); | ||||||||
10264 | if (Field != D.getField()) | ||||||||
10265 | // Rebuild the expression when the transformed FieldDecl is | ||||||||
10266 | // different to the already assigned FieldDecl. | ||||||||
10267 | ExprChanged = true; | ||||||||
10268 | } else { | ||||||||
10269 | // Ensure that the designator expression is rebuilt when there isn't | ||||||||
10270 | // a resolved FieldDecl in the designator as we don't want to assign | ||||||||
10271 | // a FieldDecl to a pattern designator that will be instantiated again. | ||||||||
10272 | ExprChanged = true; | ||||||||
10273 | } | ||||||||
10274 | continue; | ||||||||
10275 | } | ||||||||
10276 | |||||||||
10277 | if (D.isArrayDesignator()) { | ||||||||
10278 | ExprResult Index = getDerived().TransformExpr(E->getArrayIndex(D)); | ||||||||
10279 | if (Index.isInvalid()) | ||||||||
10280 | return ExprError(); | ||||||||
10281 | |||||||||
10282 | Desig.AddDesignator( | ||||||||
10283 | Designator::getArray(Index.get(), D.getLBracketLoc())); | ||||||||
10284 | |||||||||
10285 | ExprChanged = ExprChanged || Init.get() != E->getArrayIndex(D); | ||||||||
10286 | ArrayExprs.push_back(Index.get()); | ||||||||
10287 | continue; | ||||||||
10288 | } | ||||||||
10289 | |||||||||
10290 | assert(D.isArrayRangeDesignator() && "New kind of designator?")((D.isArrayRangeDesignator() && "New kind of designator?" ) ? static_cast<void> (0) : __assert_fail ("D.isArrayRangeDesignator() && \"New kind of designator?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 10290, __PRETTY_FUNCTION__)); | ||||||||
10291 | ExprResult Start | ||||||||
10292 | = getDerived().TransformExpr(E->getArrayRangeStart(D)); | ||||||||
10293 | if (Start.isInvalid()) | ||||||||
10294 | return ExprError(); | ||||||||
10295 | |||||||||
10296 | ExprResult End = getDerived().TransformExpr(E->getArrayRangeEnd(D)); | ||||||||
10297 | if (End.isInvalid()) | ||||||||
10298 | return ExprError(); | ||||||||
10299 | |||||||||
10300 | Desig.AddDesignator(Designator::getArrayRange(Start.get(), | ||||||||
10301 | End.get(), | ||||||||
10302 | D.getLBracketLoc(), | ||||||||
10303 | D.getEllipsisLoc())); | ||||||||
10304 | |||||||||
10305 | ExprChanged = ExprChanged || Start.get() != E->getArrayRangeStart(D) || | ||||||||
10306 | End.get() != E->getArrayRangeEnd(D); | ||||||||
10307 | |||||||||
10308 | ArrayExprs.push_back(Start.get()); | ||||||||
10309 | ArrayExprs.push_back(End.get()); | ||||||||
10310 | } | ||||||||
10311 | |||||||||
10312 | if (!getDerived().AlwaysRebuild() && | ||||||||
10313 | Init.get() == E->getInit() && | ||||||||
10314 | !ExprChanged) | ||||||||
10315 | return E; | ||||||||
10316 | |||||||||
10317 | return getDerived().RebuildDesignatedInitExpr(Desig, ArrayExprs, | ||||||||
10318 | E->getEqualOrColonLoc(), | ||||||||
10319 | E->usesGNUSyntax(), Init.get()); | ||||||||
10320 | } | ||||||||
10321 | |||||||||
10322 | // Seems that if TransformInitListExpr() only works on the syntactic form of an | ||||||||
10323 | // InitListExpr, then a DesignatedInitUpdateExpr is not encountered. | ||||||||
10324 | template<typename Derived> | ||||||||
10325 | ExprResult | ||||||||
10326 | TreeTransform<Derived>::TransformDesignatedInitUpdateExpr( | ||||||||
10327 | DesignatedInitUpdateExpr *E) { | ||||||||
10328 | llvm_unreachable("Unexpected DesignatedInitUpdateExpr in syntactic form of "::llvm::llvm_unreachable_internal("Unexpected DesignatedInitUpdateExpr in syntactic form of " "initializer", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 10329) | ||||||||
10329 | "initializer")::llvm::llvm_unreachable_internal("Unexpected DesignatedInitUpdateExpr in syntactic form of " "initializer", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 10329); | ||||||||
10330 | return ExprError(); | ||||||||
10331 | } | ||||||||
10332 | |||||||||
10333 | template<typename Derived> | ||||||||
10334 | ExprResult | ||||||||
10335 | TreeTransform<Derived>::TransformNoInitExpr( | ||||||||
10336 | NoInitExpr *E) { | ||||||||
10337 | llvm_unreachable("Unexpected NoInitExpr in syntactic form of initializer")::llvm::llvm_unreachable_internal("Unexpected NoInitExpr in syntactic form of initializer" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 10337); | ||||||||
10338 | return ExprError(); | ||||||||
10339 | } | ||||||||
10340 | |||||||||
10341 | template<typename Derived> | ||||||||
10342 | ExprResult | ||||||||
10343 | TreeTransform<Derived>::TransformArrayInitLoopExpr(ArrayInitLoopExpr *E) { | ||||||||
10344 | llvm_unreachable("Unexpected ArrayInitLoopExpr outside of initializer")::llvm::llvm_unreachable_internal("Unexpected ArrayInitLoopExpr outside of initializer" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 10344); | ||||||||
10345 | return ExprError(); | ||||||||
10346 | } | ||||||||
10347 | |||||||||
10348 | template<typename Derived> | ||||||||
10349 | ExprResult | ||||||||
10350 | TreeTransform<Derived>::TransformArrayInitIndexExpr(ArrayInitIndexExpr *E) { | ||||||||
10351 | llvm_unreachable("Unexpected ArrayInitIndexExpr outside of initializer")::llvm::llvm_unreachable_internal("Unexpected ArrayInitIndexExpr outside of initializer" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 10351); | ||||||||
10352 | return ExprError(); | ||||||||
10353 | } | ||||||||
10354 | |||||||||
10355 | template<typename Derived> | ||||||||
10356 | ExprResult | ||||||||
10357 | TreeTransform<Derived>::TransformImplicitValueInitExpr( | ||||||||
10358 | ImplicitValueInitExpr *E) { | ||||||||
10359 | TemporaryBase Rebase(*this, E->getBeginLoc(), DeclarationName()); | ||||||||
10360 | |||||||||
10361 | // FIXME: Will we ever have proper type location here? Will we actually | ||||||||
10362 | // need to transform the type? | ||||||||
10363 | QualType T = getDerived().TransformType(E->getType()); | ||||||||
10364 | if (T.isNull()) | ||||||||
10365 | return ExprError(); | ||||||||
10366 | |||||||||
10367 | if (!getDerived().AlwaysRebuild() && | ||||||||
10368 | T == E->getType()) | ||||||||
10369 | return E; | ||||||||
10370 | |||||||||
10371 | return getDerived().RebuildImplicitValueInitExpr(T); | ||||||||
10372 | } | ||||||||
10373 | |||||||||
10374 | template<typename Derived> | ||||||||
10375 | ExprResult | ||||||||
10376 | TreeTransform<Derived>::TransformVAArgExpr(VAArgExpr *E) { | ||||||||
10377 | TypeSourceInfo *TInfo = getDerived().TransformType(E->getWrittenTypeInfo()); | ||||||||
10378 | if (!TInfo) | ||||||||
10379 | return ExprError(); | ||||||||
10380 | |||||||||
10381 | ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr()); | ||||||||
10382 | if (SubExpr.isInvalid()) | ||||||||
10383 | return ExprError(); | ||||||||
10384 | |||||||||
10385 | if (!getDerived().AlwaysRebuild() && | ||||||||
10386 | TInfo == E->getWrittenTypeInfo() && | ||||||||
10387 | SubExpr.get() == E->getSubExpr()) | ||||||||
10388 | return E; | ||||||||
10389 | |||||||||
10390 | return getDerived().RebuildVAArgExpr(E->getBuiltinLoc(), SubExpr.get(), | ||||||||
10391 | TInfo, E->getRParenLoc()); | ||||||||
10392 | } | ||||||||
10393 | |||||||||
10394 | template<typename Derived> | ||||||||
10395 | ExprResult | ||||||||
10396 | TreeTransform<Derived>::TransformParenListExpr(ParenListExpr *E) { | ||||||||
10397 | bool ArgumentChanged = false; | ||||||||
10398 | SmallVector<Expr*, 4> Inits; | ||||||||
10399 | if (TransformExprs(E->getExprs(), E->getNumExprs(), true, Inits, | ||||||||
10400 | &ArgumentChanged)) | ||||||||
10401 | return ExprError(); | ||||||||
10402 | |||||||||
10403 | return getDerived().RebuildParenListExpr(E->getLParenLoc(), | ||||||||
10404 | Inits, | ||||||||
10405 | E->getRParenLoc()); | ||||||||
10406 | } | ||||||||
10407 | |||||||||
10408 | /// Transform an address-of-label expression. | ||||||||
10409 | /// | ||||||||
10410 | /// By default, the transformation of an address-of-label expression always | ||||||||
10411 | /// rebuilds the expression, so that the label identifier can be resolved to | ||||||||
10412 | /// the corresponding label statement by semantic analysis. | ||||||||
10413 | template<typename Derived> | ||||||||
10414 | ExprResult | ||||||||
10415 | TreeTransform<Derived>::TransformAddrLabelExpr(AddrLabelExpr *E) { | ||||||||
10416 | Decl *LD = getDerived().TransformDecl(E->getLabel()->getLocation(), | ||||||||
10417 | E->getLabel()); | ||||||||
10418 | if (!LD) | ||||||||
10419 | return ExprError(); | ||||||||
10420 | |||||||||
10421 | return getDerived().RebuildAddrLabelExpr(E->getAmpAmpLoc(), E->getLabelLoc(), | ||||||||
10422 | cast<LabelDecl>(LD)); | ||||||||
10423 | } | ||||||||
10424 | |||||||||
10425 | template<typename Derived> | ||||||||
10426 | ExprResult | ||||||||
10427 | TreeTransform<Derived>::TransformStmtExpr(StmtExpr *E) { | ||||||||
10428 | SemaRef.ActOnStartStmtExpr(); | ||||||||
10429 | StmtResult SubStmt | ||||||||
10430 | = getDerived().TransformCompoundStmt(E->getSubStmt(), true); | ||||||||
10431 | if (SubStmt.isInvalid()) { | ||||||||
10432 | SemaRef.ActOnStmtExprError(); | ||||||||
10433 | return ExprError(); | ||||||||
10434 | } | ||||||||
10435 | |||||||||
10436 | if (!getDerived().AlwaysRebuild() && | ||||||||
10437 | SubStmt.get() == E->getSubStmt()) { | ||||||||
10438 | // Calling this an 'error' is unintuitive, but it does the right thing. | ||||||||
10439 | SemaRef.ActOnStmtExprError(); | ||||||||
10440 | return SemaRef.MaybeBindToTemporary(E); | ||||||||
10441 | } | ||||||||
10442 | |||||||||
10443 | return getDerived().RebuildStmtExpr(E->getLParenLoc(), | ||||||||
10444 | SubStmt.get(), | ||||||||
10445 | E->getRParenLoc()); | ||||||||
10446 | } | ||||||||
10447 | |||||||||
10448 | template<typename Derived> | ||||||||
10449 | ExprResult | ||||||||
10450 | TreeTransform<Derived>::TransformChooseExpr(ChooseExpr *E) { | ||||||||
10451 | ExprResult Cond = getDerived().TransformExpr(E->getCond()); | ||||||||
10452 | if (Cond.isInvalid()) | ||||||||
10453 | return ExprError(); | ||||||||
10454 | |||||||||
10455 | ExprResult LHS = getDerived().TransformExpr(E->getLHS()); | ||||||||
10456 | if (LHS.isInvalid()) | ||||||||
10457 | return ExprError(); | ||||||||
10458 | |||||||||
10459 | ExprResult RHS = getDerived().TransformExpr(E->getRHS()); | ||||||||
10460 | if (RHS.isInvalid()) | ||||||||
10461 | return ExprError(); | ||||||||
10462 | |||||||||
10463 | if (!getDerived().AlwaysRebuild() && | ||||||||
10464 | Cond.get() == E->getCond() && | ||||||||
10465 | LHS.get() == E->getLHS() && | ||||||||
10466 | RHS.get() == E->getRHS()) | ||||||||
10467 | return E; | ||||||||
10468 | |||||||||
10469 | return getDerived().RebuildChooseExpr(E->getBuiltinLoc(), | ||||||||
10470 | Cond.get(), LHS.get(), RHS.get(), | ||||||||
10471 | E->getRParenLoc()); | ||||||||
10472 | } | ||||||||
10473 | |||||||||
10474 | template<typename Derived> | ||||||||
10475 | ExprResult | ||||||||
10476 | TreeTransform<Derived>::TransformGNUNullExpr(GNUNullExpr *E) { | ||||||||
10477 | return E; | ||||||||
10478 | } | ||||||||
10479 | |||||||||
10480 | template<typename Derived> | ||||||||
10481 | ExprResult | ||||||||
10482 | TreeTransform<Derived>::TransformCXXOperatorCallExpr(CXXOperatorCallExpr *E) { | ||||||||
10483 | switch (E->getOperator()) { | ||||||||
10484 | case OO_New: | ||||||||
10485 | case OO_Delete: | ||||||||
10486 | case OO_Array_New: | ||||||||
10487 | case OO_Array_Delete: | ||||||||
10488 | llvm_unreachable("new and delete operators cannot use CXXOperatorCallExpr")::llvm::llvm_unreachable_internal("new and delete operators cannot use CXXOperatorCallExpr" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 10488); | ||||||||
10489 | |||||||||
10490 | case OO_Call: { | ||||||||
10491 | // This is a call to an object's operator(). | ||||||||
10492 | assert(E->getNumArgs() >= 1 && "Object call is missing arguments")((E->getNumArgs() >= 1 && "Object call is missing arguments" ) ? static_cast<void> (0) : __assert_fail ("E->getNumArgs() >= 1 && \"Object call is missing arguments\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 10492, __PRETTY_FUNCTION__)); | ||||||||
10493 | |||||||||
10494 | // Transform the object itself. | ||||||||
10495 | ExprResult Object = getDerived().TransformExpr(E->getArg(0)); | ||||||||
10496 | if (Object.isInvalid()) | ||||||||
10497 | return ExprError(); | ||||||||
10498 | |||||||||
10499 | // FIXME: Poor location information | ||||||||
10500 | SourceLocation FakeLParenLoc = SemaRef.getLocForEndOfToken( | ||||||||
10501 | static_cast<Expr *>(Object.get())->getEndLoc()); | ||||||||
10502 | |||||||||
10503 | // Transform the call arguments. | ||||||||
10504 | SmallVector<Expr*, 8> Args; | ||||||||
10505 | if (getDerived().TransformExprs(E->getArgs() + 1, E->getNumArgs() - 1, true, | ||||||||
10506 | Args)) | ||||||||
10507 | return ExprError(); | ||||||||
10508 | |||||||||
10509 | return getDerived().RebuildCallExpr(Object.get(), FakeLParenLoc, Args, | ||||||||
10510 | E->getEndLoc()); | ||||||||
10511 | } | ||||||||
10512 | |||||||||
10513 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ | ||||||||
10514 | case OO_##Name: | ||||||||
10515 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) | ||||||||
10516 | #include "clang/Basic/OperatorKinds.def" | ||||||||
10517 | case OO_Subscript: | ||||||||
10518 | // Handled below. | ||||||||
10519 | break; | ||||||||
10520 | |||||||||
10521 | case OO_Conditional: | ||||||||
10522 | llvm_unreachable("conditional operator is not actually overloadable")::llvm::llvm_unreachable_internal("conditional operator is not actually overloadable" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 10522); | ||||||||
10523 | |||||||||
10524 | case OO_None: | ||||||||
10525 | case NUM_OVERLOADED_OPERATORS: | ||||||||
10526 | llvm_unreachable("not an overloaded operator?")::llvm::llvm_unreachable_internal("not an overloaded operator?" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 10526); | ||||||||
10527 | } | ||||||||
10528 | |||||||||
10529 | ExprResult Callee = getDerived().TransformExpr(E->getCallee()); | ||||||||
10530 | if (Callee.isInvalid()) | ||||||||
10531 | return ExprError(); | ||||||||
10532 | |||||||||
10533 | ExprResult First; | ||||||||
10534 | if (E->getOperator() == OO_Amp) | ||||||||
10535 | First = getDerived().TransformAddressOfOperand(E->getArg(0)); | ||||||||
10536 | else | ||||||||
10537 | First = getDerived().TransformExpr(E->getArg(0)); | ||||||||
10538 | if (First.isInvalid()) | ||||||||
10539 | return ExprError(); | ||||||||
10540 | |||||||||
10541 | ExprResult Second; | ||||||||
10542 | if (E->getNumArgs() == 2) { | ||||||||
10543 | Second = getDerived().TransformExpr(E->getArg(1)); | ||||||||
10544 | if (Second.isInvalid()) | ||||||||
10545 | return ExprError(); | ||||||||
10546 | } | ||||||||
10547 | |||||||||
10548 | if (!getDerived().AlwaysRebuild() && | ||||||||
10549 | Callee.get() == E->getCallee() && | ||||||||
10550 | First.get() == E->getArg(0) && | ||||||||
10551 | (E->getNumArgs() != 2 || Second.get() == E->getArg(1))) | ||||||||
10552 | return SemaRef.MaybeBindToTemporary(E); | ||||||||
10553 | |||||||||
10554 | Sema::FPFeaturesStateRAII FPFeaturesState(getSema()); | ||||||||
10555 | getSema().FPFeatures = E->getFPFeatures(); | ||||||||
10556 | |||||||||
10557 | return getDerived().RebuildCXXOperatorCallExpr(E->getOperator(), | ||||||||
10558 | E->getOperatorLoc(), | ||||||||
10559 | Callee.get(), | ||||||||
10560 | First.get(), | ||||||||
10561 | Second.get()); | ||||||||
10562 | } | ||||||||
10563 | |||||||||
10564 | template<typename Derived> | ||||||||
10565 | ExprResult | ||||||||
10566 | TreeTransform<Derived>::TransformCXXMemberCallExpr(CXXMemberCallExpr *E) { | ||||||||
10567 | return getDerived().TransformCallExpr(E); | ||||||||
10568 | } | ||||||||
10569 | |||||||||
10570 | template <typename Derived> | ||||||||
10571 | ExprResult TreeTransform<Derived>::TransformSourceLocExpr(SourceLocExpr *E) { | ||||||||
10572 | bool NeedRebuildFunc = E->getIdentKind() == SourceLocExpr::Function && | ||||||||
10573 | getSema().CurContext != E->getParentContext(); | ||||||||
10574 | |||||||||
10575 | if (!getDerived().AlwaysRebuild() && !NeedRebuildFunc) | ||||||||
10576 | return E; | ||||||||
10577 | |||||||||
10578 | return getDerived().RebuildSourceLocExpr(E->getIdentKind(), E->getBeginLoc(), | ||||||||
10579 | E->getEndLoc(), | ||||||||
10580 | getSema().CurContext); | ||||||||
10581 | } | ||||||||
10582 | |||||||||
10583 | template<typename Derived> | ||||||||
10584 | ExprResult | ||||||||
10585 | TreeTransform<Derived>::TransformCUDAKernelCallExpr(CUDAKernelCallExpr *E) { | ||||||||
10586 | // Transform the callee. | ||||||||
10587 | ExprResult Callee = getDerived().TransformExpr(E->getCallee()); | ||||||||
10588 | if (Callee.isInvalid()) | ||||||||
10589 | return ExprError(); | ||||||||
10590 | |||||||||
10591 | // Transform exec config. | ||||||||
10592 | ExprResult EC = getDerived().TransformCallExpr(E->getConfig()); | ||||||||
10593 | if (EC.isInvalid()) | ||||||||
10594 | return ExprError(); | ||||||||
10595 | |||||||||
10596 | // Transform arguments. | ||||||||
10597 | bool ArgChanged = false; | ||||||||
10598 | SmallVector<Expr*, 8> Args; | ||||||||
10599 | if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), true, Args, | ||||||||
10600 | &ArgChanged)) | ||||||||
10601 | return ExprError(); | ||||||||
10602 | |||||||||
10603 | if (!getDerived().AlwaysRebuild() && | ||||||||
10604 | Callee.get() == E->getCallee() && | ||||||||
10605 | !ArgChanged) | ||||||||
10606 | return SemaRef.MaybeBindToTemporary(E); | ||||||||
10607 | |||||||||
10608 | // FIXME: Wrong source location information for the '('. | ||||||||
10609 | SourceLocation FakeLParenLoc | ||||||||
10610 | = ((Expr *)Callee.get())->getSourceRange().getBegin(); | ||||||||
10611 | return getDerived().RebuildCallExpr(Callee.get(), FakeLParenLoc, | ||||||||
10612 | Args, | ||||||||
10613 | E->getRParenLoc(), EC.get()); | ||||||||
10614 | } | ||||||||
10615 | |||||||||
10616 | template<typename Derived> | ||||||||
10617 | ExprResult | ||||||||
10618 | TreeTransform<Derived>::TransformCXXNamedCastExpr(CXXNamedCastExpr *E) { | ||||||||
10619 | TypeSourceInfo *Type = getDerived().TransformType(E->getTypeInfoAsWritten()); | ||||||||
10620 | if (!Type) | ||||||||
10621 | return ExprError(); | ||||||||
10622 | |||||||||
10623 | ExprResult SubExpr | ||||||||
10624 | = getDerived().TransformExpr(E->getSubExprAsWritten()); | ||||||||
10625 | if (SubExpr.isInvalid()) | ||||||||
10626 | return ExprError(); | ||||||||
10627 | |||||||||
10628 | if (!getDerived().AlwaysRebuild() && | ||||||||
10629 | Type == E->getTypeInfoAsWritten() && | ||||||||
10630 | SubExpr.get() == E->getSubExpr()) | ||||||||
10631 | return E; | ||||||||
10632 | return getDerived().RebuildCXXNamedCastExpr( | ||||||||
10633 | E->getOperatorLoc(), E->getStmtClass(), E->getAngleBrackets().getBegin(), | ||||||||
10634 | Type, E->getAngleBrackets().getEnd(), | ||||||||
10635 | // FIXME. this should be '(' location | ||||||||
10636 | E->getAngleBrackets().getEnd(), SubExpr.get(), E->getRParenLoc()); | ||||||||
10637 | } | ||||||||
10638 | |||||||||
10639 | template<typename Derived> | ||||||||
10640 | ExprResult | ||||||||
10641 | TreeTransform<Derived>::TransformBuiltinBitCastExpr(BuiltinBitCastExpr *BCE) { | ||||||||
10642 | TypeSourceInfo *TSI = | ||||||||
10643 | getDerived().TransformType(BCE->getTypeInfoAsWritten()); | ||||||||
10644 | if (!TSI) | ||||||||
10645 | return ExprError(); | ||||||||
10646 | |||||||||
10647 | ExprResult Sub = getDerived().TransformExpr(BCE->getSubExpr()); | ||||||||
10648 | if (Sub.isInvalid()) | ||||||||
10649 | return ExprError(); | ||||||||
10650 | |||||||||
10651 | return getDerived().RebuildBuiltinBitCastExpr(BCE->getBeginLoc(), TSI, | ||||||||
10652 | Sub.get(), BCE->getEndLoc()); | ||||||||
10653 | } | ||||||||
10654 | |||||||||
10655 | template<typename Derived> | ||||||||
10656 | ExprResult | ||||||||
10657 | TreeTransform<Derived>::TransformCXXStaticCastExpr(CXXStaticCastExpr *E) { | ||||||||
10658 | return getDerived().TransformCXXNamedCastExpr(E); | ||||||||
10659 | } | ||||||||
10660 | |||||||||
10661 | template<typename Derived> | ||||||||
10662 | ExprResult | ||||||||
10663 | TreeTransform<Derived>::TransformCXXDynamicCastExpr(CXXDynamicCastExpr *E) { | ||||||||
10664 | return getDerived().TransformCXXNamedCastExpr(E); | ||||||||
10665 | } | ||||||||
10666 | |||||||||
10667 | template<typename Derived> | ||||||||
10668 | ExprResult | ||||||||
10669 | TreeTransform<Derived>::TransformCXXReinterpretCastExpr( | ||||||||
10670 | CXXReinterpretCastExpr *E) { | ||||||||
10671 | return getDerived().TransformCXXNamedCastExpr(E); | ||||||||
10672 | } | ||||||||
10673 | |||||||||
10674 | template<typename Derived> | ||||||||
10675 | ExprResult | ||||||||
10676 | TreeTransform<Derived>::TransformCXXConstCastExpr(CXXConstCastExpr *E) { | ||||||||
10677 | return getDerived().TransformCXXNamedCastExpr(E); | ||||||||
10678 | } | ||||||||
10679 | |||||||||
10680 | template<typename Derived> | ||||||||
10681 | ExprResult | ||||||||
10682 | TreeTransform<Derived>::TransformCXXFunctionalCastExpr( | ||||||||
10683 | CXXFunctionalCastExpr *E) { | ||||||||
10684 | TypeSourceInfo *Type = | ||||||||
10685 | getDerived().TransformTypeWithDeducedTST(E->getTypeInfoAsWritten()); | ||||||||
10686 | if (!Type) | ||||||||
10687 | return ExprError(); | ||||||||
10688 | |||||||||
10689 | ExprResult SubExpr | ||||||||
10690 | = getDerived().TransformExpr(E->getSubExprAsWritten()); | ||||||||
10691 | if (SubExpr.isInvalid()) | ||||||||
10692 | return ExprError(); | ||||||||
10693 | |||||||||
10694 | if (!getDerived().AlwaysRebuild() && | ||||||||
10695 | Type == E->getTypeInfoAsWritten() && | ||||||||
10696 | SubExpr.get() == E->getSubExpr()) | ||||||||
10697 | return E; | ||||||||
10698 | |||||||||
10699 | return getDerived().RebuildCXXFunctionalCastExpr(Type, | ||||||||
10700 | E->getLParenLoc(), | ||||||||
10701 | SubExpr.get(), | ||||||||
10702 | E->getRParenLoc(), | ||||||||
10703 | E->isListInitialization()); | ||||||||
10704 | } | ||||||||
10705 | |||||||||
10706 | template<typename Derived> | ||||||||
10707 | ExprResult | ||||||||
10708 | TreeTransform<Derived>::TransformCXXTypeidExpr(CXXTypeidExpr *E) { | ||||||||
10709 | if (E->isTypeOperand()) { | ||||||||
10710 | TypeSourceInfo *TInfo | ||||||||
10711 | = getDerived().TransformType(E->getTypeOperandSourceInfo()); | ||||||||
10712 | if (!TInfo) | ||||||||
10713 | return ExprError(); | ||||||||
10714 | |||||||||
10715 | if (!getDerived().AlwaysRebuild() && | ||||||||
10716 | TInfo == E->getTypeOperandSourceInfo()) | ||||||||
10717 | return E; | ||||||||
10718 | |||||||||
10719 | return getDerived().RebuildCXXTypeidExpr(E->getType(), E->getBeginLoc(), | ||||||||
10720 | TInfo, E->getEndLoc()); | ||||||||
10721 | } | ||||||||
10722 | |||||||||
10723 | // We don't know whether the subexpression is potentially evaluated until | ||||||||
10724 | // after we perform semantic analysis. We speculatively assume it is | ||||||||
10725 | // unevaluated; it will get fixed later if the subexpression is in fact | ||||||||
10726 | // potentially evaluated. | ||||||||
10727 | EnterExpressionEvaluationContext Unevaluated( | ||||||||
10728 | SemaRef, Sema::ExpressionEvaluationContext::Unevaluated, | ||||||||
10729 | Sema::ReuseLambdaContextDecl); | ||||||||
10730 | |||||||||
10731 | ExprResult SubExpr = getDerived().TransformExpr(E->getExprOperand()); | ||||||||
10732 | if (SubExpr.isInvalid()) | ||||||||
10733 | return ExprError(); | ||||||||
10734 | |||||||||
10735 | if (!getDerived().AlwaysRebuild() && | ||||||||
10736 | SubExpr.get() == E->getExprOperand()) | ||||||||
10737 | return E; | ||||||||
10738 | |||||||||
10739 | return getDerived().RebuildCXXTypeidExpr(E->getType(), E->getBeginLoc(), | ||||||||
10740 | SubExpr.get(), E->getEndLoc()); | ||||||||
10741 | } | ||||||||
10742 | |||||||||
10743 | template<typename Derived> | ||||||||
10744 | ExprResult | ||||||||
10745 | TreeTransform<Derived>::TransformCXXUuidofExpr(CXXUuidofExpr *E) { | ||||||||
10746 | if (E->isTypeOperand()) { | ||||||||
10747 | TypeSourceInfo *TInfo | ||||||||
10748 | = getDerived().TransformType(E->getTypeOperandSourceInfo()); | ||||||||
10749 | if (!TInfo) | ||||||||
10750 | return ExprError(); | ||||||||
10751 | |||||||||
10752 | if (!getDerived().AlwaysRebuild() && | ||||||||
10753 | TInfo == E->getTypeOperandSourceInfo()) | ||||||||
10754 | return E; | ||||||||
10755 | |||||||||
10756 | return getDerived().RebuildCXXUuidofExpr(E->getType(), E->getBeginLoc(), | ||||||||
10757 | TInfo, E->getEndLoc()); | ||||||||
10758 | } | ||||||||
10759 | |||||||||
10760 | EnterExpressionEvaluationContext Unevaluated( | ||||||||
10761 | SemaRef, Sema::ExpressionEvaluationContext::Unevaluated); | ||||||||
10762 | |||||||||
10763 | ExprResult SubExpr = getDerived().TransformExpr(E->getExprOperand()); | ||||||||
10764 | if (SubExpr.isInvalid()) | ||||||||
10765 | return ExprError(); | ||||||||
10766 | |||||||||
10767 | if (!getDerived().AlwaysRebuild() && | ||||||||
10768 | SubExpr.get() == E->getExprOperand()) | ||||||||
10769 | return E; | ||||||||
10770 | |||||||||
10771 | return getDerived().RebuildCXXUuidofExpr(E->getType(), E->getBeginLoc(), | ||||||||
10772 | SubExpr.get(), E->getEndLoc()); | ||||||||
10773 | } | ||||||||
10774 | |||||||||
10775 | template<typename Derived> | ||||||||
10776 | ExprResult | ||||||||
10777 | TreeTransform<Derived>::TransformCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { | ||||||||
10778 | return E; | ||||||||
10779 | } | ||||||||
10780 | |||||||||
10781 | template<typename Derived> | ||||||||
10782 | ExprResult | ||||||||
10783 | TreeTransform<Derived>::TransformCXXNullPtrLiteralExpr( | ||||||||
10784 | CXXNullPtrLiteralExpr *E) { | ||||||||
10785 | return E; | ||||||||
10786 | } | ||||||||
10787 | |||||||||
10788 | template<typename Derived> | ||||||||
10789 | ExprResult | ||||||||
10790 | TreeTransform<Derived>::TransformCXXThisExpr(CXXThisExpr *E) { | ||||||||
10791 | QualType T = getSema().getCurrentThisType(); | ||||||||
10792 | |||||||||
10793 | if (!getDerived().AlwaysRebuild() && T == E->getType()) { | ||||||||
10794 | // Mark it referenced in the new context regardless. | ||||||||
10795 | // FIXME: this is a bit instantiation-specific. | ||||||||
10796 | getSema().MarkThisReferenced(E); | ||||||||
10797 | return E; | ||||||||
10798 | } | ||||||||
10799 | |||||||||
10800 | return getDerived().RebuildCXXThisExpr(E->getBeginLoc(), T, E->isImplicit()); | ||||||||
10801 | } | ||||||||
10802 | |||||||||
10803 | template<typename Derived> | ||||||||
10804 | ExprResult | ||||||||
10805 | TreeTransform<Derived>::TransformCXXThrowExpr(CXXThrowExpr *E) { | ||||||||
10806 | ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr()); | ||||||||
10807 | if (SubExpr.isInvalid()) | ||||||||
10808 | return ExprError(); | ||||||||
10809 | |||||||||
10810 | if (!getDerived().AlwaysRebuild() && | ||||||||
10811 | SubExpr.get() == E->getSubExpr()) | ||||||||
10812 | return E; | ||||||||
10813 | |||||||||
10814 | return getDerived().RebuildCXXThrowExpr(E->getThrowLoc(), SubExpr.get(), | ||||||||
10815 | E->isThrownVariableInScope()); | ||||||||
10816 | } | ||||||||
10817 | |||||||||
10818 | template<typename Derived> | ||||||||
10819 | ExprResult | ||||||||
10820 | TreeTransform<Derived>::TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E) { | ||||||||
10821 | ParmVarDecl *Param = cast_or_null<ParmVarDecl>( | ||||||||
10822 | getDerived().TransformDecl(E->getBeginLoc(), E->getParam())); | ||||||||
10823 | if (!Param) | ||||||||
10824 | return ExprError(); | ||||||||
10825 | |||||||||
10826 | if (!getDerived().AlwaysRebuild() && Param == E->getParam() && | ||||||||
10827 | E->getUsedContext() == SemaRef.CurContext) | ||||||||
10828 | return E; | ||||||||
10829 | |||||||||
10830 | return getDerived().RebuildCXXDefaultArgExpr(E->getUsedLocation(), Param); | ||||||||
10831 | } | ||||||||
10832 | |||||||||
10833 | template<typename Derived> | ||||||||
10834 | ExprResult | ||||||||
10835 | TreeTransform<Derived>::TransformCXXDefaultInitExpr(CXXDefaultInitExpr *E) { | ||||||||
10836 | FieldDecl *Field = cast_or_null<FieldDecl>( | ||||||||
10837 | getDerived().TransformDecl(E->getBeginLoc(), E->getField())); | ||||||||
10838 | if (!Field) | ||||||||
10839 | return ExprError(); | ||||||||
10840 | |||||||||
10841 | if (!getDerived().AlwaysRebuild() && Field == E->getField() && | ||||||||
10842 | E->getUsedContext() == SemaRef.CurContext) | ||||||||
10843 | return E; | ||||||||
10844 | |||||||||
10845 | return getDerived().RebuildCXXDefaultInitExpr(E->getExprLoc(), Field); | ||||||||
10846 | } | ||||||||
10847 | |||||||||
10848 | template<typename Derived> | ||||||||
10849 | ExprResult | ||||||||
10850 | TreeTransform<Derived>::TransformCXXScalarValueInitExpr( | ||||||||
10851 | CXXScalarValueInitExpr *E) { | ||||||||
10852 | TypeSourceInfo *T = getDerived().TransformType(E->getTypeSourceInfo()); | ||||||||
10853 | if (!T) | ||||||||
10854 | return ExprError(); | ||||||||
10855 | |||||||||
10856 | if (!getDerived().AlwaysRebuild() && | ||||||||
10857 | T == E->getTypeSourceInfo()) | ||||||||
10858 | return E; | ||||||||
10859 | |||||||||
10860 | return getDerived().RebuildCXXScalarValueInitExpr(T, | ||||||||
10861 | /*FIXME:*/T->getTypeLoc().getEndLoc(), | ||||||||
10862 | E->getRParenLoc()); | ||||||||
10863 | } | ||||||||
10864 | |||||||||
10865 | template<typename Derived> | ||||||||
10866 | ExprResult | ||||||||
10867 | TreeTransform<Derived>::TransformCXXNewExpr(CXXNewExpr *E) { | ||||||||
10868 | // Transform the type that we're allocating | ||||||||
10869 | TypeSourceInfo *AllocTypeInfo = | ||||||||
10870 | getDerived().TransformTypeWithDeducedTST(E->getAllocatedTypeSourceInfo()); | ||||||||
10871 | if (!AllocTypeInfo) | ||||||||
10872 | return ExprError(); | ||||||||
10873 | |||||||||
10874 | // Transform the size of the array we're allocating (if any). | ||||||||
10875 | Optional<Expr *> ArraySize; | ||||||||
10876 | if (Optional<Expr *> OldArraySize = E->getArraySize()) { | ||||||||
10877 | ExprResult NewArraySize; | ||||||||
10878 | if (*OldArraySize) { | ||||||||
10879 | NewArraySize = getDerived().TransformExpr(*OldArraySize); | ||||||||
10880 | if (NewArraySize.isInvalid()) | ||||||||
10881 | return ExprError(); | ||||||||
10882 | } | ||||||||
10883 | ArraySize = NewArraySize.get(); | ||||||||
10884 | } | ||||||||
10885 | |||||||||
10886 | // Transform the placement arguments (if any). | ||||||||
10887 | bool ArgumentChanged = false; | ||||||||
10888 | SmallVector<Expr*, 8> PlacementArgs; | ||||||||
10889 | if (getDerived().TransformExprs(E->getPlacementArgs(), | ||||||||
10890 | E->getNumPlacementArgs(), true, | ||||||||
10891 | PlacementArgs, &ArgumentChanged)) | ||||||||
10892 | return ExprError(); | ||||||||
10893 | |||||||||
10894 | // Transform the initializer (if any). | ||||||||
10895 | Expr *OldInit = E->getInitializer(); | ||||||||
10896 | ExprResult NewInit; | ||||||||
10897 | if (OldInit) | ||||||||
10898 | NewInit = getDerived().TransformInitializer(OldInit, true); | ||||||||
10899 | if (NewInit.isInvalid()) | ||||||||
10900 | return ExprError(); | ||||||||
10901 | |||||||||
10902 | // Transform new operator and delete operator. | ||||||||
10903 | FunctionDecl *OperatorNew = nullptr; | ||||||||
10904 | if (E->getOperatorNew()) { | ||||||||
10905 | OperatorNew = cast_or_null<FunctionDecl>( | ||||||||
10906 | getDerived().TransformDecl(E->getBeginLoc(), E->getOperatorNew())); | ||||||||
10907 | if (!OperatorNew) | ||||||||
10908 | return ExprError(); | ||||||||
10909 | } | ||||||||
10910 | |||||||||
10911 | FunctionDecl *OperatorDelete = nullptr; | ||||||||
10912 | if (E->getOperatorDelete()) { | ||||||||
10913 | OperatorDelete = cast_or_null<FunctionDecl>( | ||||||||
10914 | getDerived().TransformDecl(E->getBeginLoc(), E->getOperatorDelete())); | ||||||||
10915 | if (!OperatorDelete) | ||||||||
10916 | return ExprError(); | ||||||||
10917 | } | ||||||||
10918 | |||||||||
10919 | if (!getDerived().AlwaysRebuild() && | ||||||||
10920 | AllocTypeInfo == E->getAllocatedTypeSourceInfo() && | ||||||||
10921 | ArraySize == E->getArraySize() && | ||||||||
10922 | NewInit.get() == OldInit && | ||||||||
10923 | OperatorNew == E->getOperatorNew() && | ||||||||
10924 | OperatorDelete == E->getOperatorDelete() && | ||||||||
10925 | !ArgumentChanged) { | ||||||||
10926 | // Mark any declarations we need as referenced. | ||||||||
10927 | // FIXME: instantiation-specific. | ||||||||
10928 | if (OperatorNew) | ||||||||
10929 | SemaRef.MarkFunctionReferenced(E->getBeginLoc(), OperatorNew); | ||||||||
10930 | if (OperatorDelete) | ||||||||
10931 | SemaRef.MarkFunctionReferenced(E->getBeginLoc(), OperatorDelete); | ||||||||
10932 | |||||||||
10933 | if (E->isArray() && !E->getAllocatedType()->isDependentType()) { | ||||||||
10934 | QualType ElementType | ||||||||
10935 | = SemaRef.Context.getBaseElementType(E->getAllocatedType()); | ||||||||
10936 | if (const RecordType *RecordT = ElementType->getAs<RecordType>()) { | ||||||||
10937 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordT->getDecl()); | ||||||||
10938 | if (CXXDestructorDecl *Destructor = SemaRef.LookupDestructor(Record)) { | ||||||||
10939 | SemaRef.MarkFunctionReferenced(E->getBeginLoc(), Destructor); | ||||||||
10940 | } | ||||||||
10941 | } | ||||||||
10942 | } | ||||||||
10943 | |||||||||
10944 | return E; | ||||||||
10945 | } | ||||||||
10946 | |||||||||
10947 | QualType AllocType = AllocTypeInfo->getType(); | ||||||||
10948 | if (!ArraySize) { | ||||||||
10949 | // If no array size was specified, but the new expression was | ||||||||
10950 | // instantiated with an array type (e.g., "new T" where T is | ||||||||
10951 | // instantiated with "int[4]"), extract the outer bound from the | ||||||||
10952 | // array type as our array size. We do this with constant and | ||||||||
10953 | // dependently-sized array types. | ||||||||
10954 | const ArrayType *ArrayT = SemaRef.Context.getAsArrayType(AllocType); | ||||||||
10955 | if (!ArrayT) { | ||||||||
10956 | // Do nothing | ||||||||
10957 | } else if (const ConstantArrayType *ConsArrayT | ||||||||
10958 | = dyn_cast<ConstantArrayType>(ArrayT)) { | ||||||||
10959 | ArraySize = IntegerLiteral::Create(SemaRef.Context, ConsArrayT->getSize(), | ||||||||
10960 | SemaRef.Context.getSizeType(), | ||||||||
10961 | /*FIXME:*/ E->getBeginLoc()); | ||||||||
10962 | AllocType = ConsArrayT->getElementType(); | ||||||||
10963 | } else if (const DependentSizedArrayType *DepArrayT | ||||||||
10964 | = dyn_cast<DependentSizedArrayType>(ArrayT)) { | ||||||||
10965 | if (DepArrayT->getSizeExpr()) { | ||||||||
10966 | ArraySize = DepArrayT->getSizeExpr(); | ||||||||
10967 | AllocType = DepArrayT->getElementType(); | ||||||||
10968 | } | ||||||||
10969 | } | ||||||||
10970 | } | ||||||||
10971 | |||||||||
10972 | return getDerived().RebuildCXXNewExpr( | ||||||||
10973 | E->getBeginLoc(), E->isGlobalNew(), | ||||||||
10974 | /*FIXME:*/ E->getBeginLoc(), PlacementArgs, | ||||||||
10975 | /*FIXME:*/ E->getBeginLoc(), E->getTypeIdParens(), AllocType, | ||||||||
10976 | AllocTypeInfo, ArraySize, E->getDirectInitRange(), NewInit.get()); | ||||||||
10977 | } | ||||||||
10978 | |||||||||
10979 | template<typename Derived> | ||||||||
10980 | ExprResult | ||||||||
10981 | TreeTransform<Derived>::TransformCXXDeleteExpr(CXXDeleteExpr *E) { | ||||||||
10982 | ExprResult Operand = getDerived().TransformExpr(E->getArgument()); | ||||||||
10983 | if (Operand.isInvalid()) | ||||||||
10984 | return ExprError(); | ||||||||
10985 | |||||||||
10986 | // Transform the delete operator, if known. | ||||||||
10987 | FunctionDecl *OperatorDelete = nullptr; | ||||||||
10988 | if (E->getOperatorDelete()) { | ||||||||
10989 | OperatorDelete = cast_or_null<FunctionDecl>( | ||||||||
10990 | getDerived().TransformDecl(E->getBeginLoc(), E->getOperatorDelete())); | ||||||||
10991 | if (!OperatorDelete) | ||||||||
10992 | return ExprError(); | ||||||||
10993 | } | ||||||||
10994 | |||||||||
10995 | if (!getDerived().AlwaysRebuild() && | ||||||||
10996 | Operand.get() == E->getArgument() && | ||||||||
10997 | OperatorDelete == E->getOperatorDelete()) { | ||||||||
10998 | // Mark any declarations we need as referenced. | ||||||||
10999 | // FIXME: instantiation-specific. | ||||||||
11000 | if (OperatorDelete) | ||||||||
11001 | SemaRef.MarkFunctionReferenced(E->getBeginLoc(), OperatorDelete); | ||||||||
11002 | |||||||||
11003 | if (!E->getArgument()->isTypeDependent()) { | ||||||||
11004 | QualType Destroyed = SemaRef.Context.getBaseElementType( | ||||||||
11005 | E->getDestroyedType()); | ||||||||
11006 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { | ||||||||
11007 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); | ||||||||
11008 | SemaRef.MarkFunctionReferenced(E->getBeginLoc(), | ||||||||
11009 | SemaRef.LookupDestructor(Record)); | ||||||||
11010 | } | ||||||||
11011 | } | ||||||||
11012 | |||||||||
11013 | return E; | ||||||||
11014 | } | ||||||||
11015 | |||||||||
11016 | return getDerived().RebuildCXXDeleteExpr( | ||||||||
11017 | E->getBeginLoc(), E->isGlobalDelete(), E->isArrayForm(), Operand.get()); | ||||||||
11018 | } | ||||||||
11019 | |||||||||
11020 | template<typename Derived> | ||||||||
11021 | ExprResult | ||||||||
11022 | TreeTransform<Derived>::TransformCXXPseudoDestructorExpr( | ||||||||
11023 | CXXPseudoDestructorExpr *E) { | ||||||||
11024 | ExprResult Base = getDerived().TransformExpr(E->getBase()); | ||||||||
11025 | if (Base.isInvalid()) | ||||||||
11026 | return ExprError(); | ||||||||
11027 | |||||||||
11028 | ParsedType ObjectTypePtr; | ||||||||
11029 | bool MayBePseudoDestructor = false; | ||||||||
11030 | Base = SemaRef.ActOnStartCXXMemberReference(nullptr, Base.get(), | ||||||||
11031 | E->getOperatorLoc(), | ||||||||
11032 | E->isArrow()? tok::arrow : tok::period, | ||||||||
11033 | ObjectTypePtr, | ||||||||
11034 | MayBePseudoDestructor); | ||||||||
11035 | if (Base.isInvalid()) | ||||||||
11036 | return ExprError(); | ||||||||
11037 | |||||||||
11038 | QualType ObjectType = ObjectTypePtr.get(); | ||||||||
11039 | NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc(); | ||||||||
11040 | if (QualifierLoc) { | ||||||||
11041 | QualifierLoc | ||||||||
11042 | = getDerived().TransformNestedNameSpecifierLoc(QualifierLoc, ObjectType); | ||||||||
11043 | if (!QualifierLoc) | ||||||||
11044 | return ExprError(); | ||||||||
11045 | } | ||||||||
11046 | CXXScopeSpec SS; | ||||||||
11047 | SS.Adopt(QualifierLoc); | ||||||||
11048 | |||||||||
11049 | PseudoDestructorTypeStorage Destroyed; | ||||||||
11050 | if (E->getDestroyedTypeInfo()) { | ||||||||
11051 | TypeSourceInfo *DestroyedTypeInfo | ||||||||
11052 | = getDerived().TransformTypeInObjectScope(E->getDestroyedTypeInfo(), | ||||||||
11053 | ObjectType, nullptr, SS); | ||||||||
11054 | if (!DestroyedTypeInfo) | ||||||||
11055 | return ExprError(); | ||||||||
11056 | Destroyed = DestroyedTypeInfo; | ||||||||
11057 | } else if (!ObjectType.isNull() && ObjectType->isDependentType()) { | ||||||||
11058 | // We aren't likely to be able to resolve the identifier down to a type | ||||||||
11059 | // now anyway, so just retain the identifier. | ||||||||
11060 | Destroyed = PseudoDestructorTypeStorage(E->getDestroyedTypeIdentifier(), | ||||||||
11061 | E->getDestroyedTypeLoc()); | ||||||||
11062 | } else { | ||||||||
11063 | // Look for a destructor known with the given name. | ||||||||
11064 | ParsedType T = SemaRef.getDestructorName(E->getTildeLoc(), | ||||||||
11065 | *E->getDestroyedTypeIdentifier(), | ||||||||
11066 | E->getDestroyedTypeLoc(), | ||||||||
11067 | /*Scope=*/nullptr, | ||||||||
11068 | SS, ObjectTypePtr, | ||||||||
11069 | false); | ||||||||
11070 | if (!T) | ||||||||
11071 | return ExprError(); | ||||||||
11072 | |||||||||
11073 | Destroyed | ||||||||
11074 | = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.GetTypeFromParser(T), | ||||||||
11075 | E->getDestroyedTypeLoc()); | ||||||||
11076 | } | ||||||||
11077 | |||||||||
11078 | TypeSourceInfo *ScopeTypeInfo = nullptr; | ||||||||
11079 | if (E->getScopeTypeInfo()) { | ||||||||
11080 | CXXScopeSpec EmptySS; | ||||||||
11081 | ScopeTypeInfo = getDerived().TransformTypeInObjectScope( | ||||||||
11082 | E->getScopeTypeInfo(), ObjectType, nullptr, EmptySS); | ||||||||
11083 | if (!ScopeTypeInfo) | ||||||||
11084 | return ExprError(); | ||||||||
11085 | } | ||||||||
11086 | |||||||||
11087 | return getDerived().RebuildCXXPseudoDestructorExpr(Base.get(), | ||||||||
11088 | E->getOperatorLoc(), | ||||||||
11089 | E->isArrow(), | ||||||||
11090 | SS, | ||||||||
11091 | ScopeTypeInfo, | ||||||||
11092 | E->getColonColonLoc(), | ||||||||
11093 | E->getTildeLoc(), | ||||||||
11094 | Destroyed); | ||||||||
11095 | } | ||||||||
11096 | |||||||||
11097 | template <typename Derived> | ||||||||
11098 | bool TreeTransform<Derived>::TransformOverloadExprDecls(OverloadExpr *Old, | ||||||||
11099 | bool RequiresADL, | ||||||||
11100 | LookupResult &R) { | ||||||||
11101 | // Transform all the decls. | ||||||||
11102 | bool AllEmptyPacks = true; | ||||||||
11103 | for (auto *OldD : Old->decls()) { | ||||||||
11104 | Decl *InstD = getDerived().TransformDecl(Old->getNameLoc(), OldD); | ||||||||
11105 | if (!InstD) { | ||||||||
11106 | // Silently ignore these if a UsingShadowDecl instantiated to nothing. | ||||||||
11107 | // This can happen because of dependent hiding. | ||||||||
11108 | if (isa<UsingShadowDecl>(OldD)) | ||||||||
11109 | continue; | ||||||||
11110 | else { | ||||||||
11111 | R.clear(); | ||||||||
11112 | return true; | ||||||||
11113 | } | ||||||||
11114 | } | ||||||||
11115 | |||||||||
11116 | // Expand using pack declarations. | ||||||||
11117 | NamedDecl *SingleDecl = cast<NamedDecl>(InstD); | ||||||||
11118 | ArrayRef<NamedDecl*> Decls = SingleDecl; | ||||||||
11119 | if (auto *UPD = dyn_cast<UsingPackDecl>(InstD)) | ||||||||
11120 | Decls = UPD->expansions(); | ||||||||
11121 | |||||||||
11122 | // Expand using declarations. | ||||||||
11123 | for (auto *D : Decls) { | ||||||||
11124 | if (auto *UD = dyn_cast<UsingDecl>(D)) { | ||||||||
11125 | for (auto *SD : UD->shadows()) | ||||||||
11126 | R.addDecl(SD); | ||||||||
11127 | } else { | ||||||||
11128 | R.addDecl(D); | ||||||||
11129 | } | ||||||||
11130 | } | ||||||||
11131 | |||||||||
11132 | AllEmptyPacks &= Decls.empty(); | ||||||||
11133 | }; | ||||||||
11134 | |||||||||
11135 | // C++ [temp.res]/8.4.2: | ||||||||
11136 | // The program is ill-formed, no diagnostic required, if [...] lookup for | ||||||||
11137 | // a name in the template definition found a using-declaration, but the | ||||||||
11138 | // lookup in the corresponding scope in the instantiation odoes not find | ||||||||
11139 | // any declarations because the using-declaration was a pack expansion and | ||||||||
11140 | // the corresponding pack is empty | ||||||||
11141 | if (AllEmptyPacks && !RequiresADL) { | ||||||||
11142 | getSema().Diag(Old->getNameLoc(), diag::err_using_pack_expansion_empty) | ||||||||
11143 | << isa<UnresolvedMemberExpr>(Old) << Old->getName(); | ||||||||
11144 | return true; | ||||||||
11145 | } | ||||||||
11146 | |||||||||
11147 | // Resolve a kind, but don't do any further analysis. If it's | ||||||||
11148 | // ambiguous, the callee needs to deal with it. | ||||||||
11149 | R.resolveKind(); | ||||||||
11150 | return false; | ||||||||
11151 | } | ||||||||
11152 | |||||||||
11153 | template<typename Derived> | ||||||||
11154 | ExprResult | ||||||||
11155 | TreeTransform<Derived>::TransformUnresolvedLookupExpr( | ||||||||
11156 | UnresolvedLookupExpr *Old) { | ||||||||
11157 | LookupResult R(SemaRef, Old->getName(), Old->getNameLoc(), | ||||||||
11158 | Sema::LookupOrdinaryName); | ||||||||
11159 | |||||||||
11160 | // Transform the declaration set. | ||||||||
11161 | if (TransformOverloadExprDecls(Old, Old->requiresADL(), R)) | ||||||||
11162 | return ExprError(); | ||||||||
11163 | |||||||||
11164 | // Rebuild the nested-name qualifier, if present. | ||||||||
11165 | CXXScopeSpec SS; | ||||||||
11166 | if (Old->getQualifierLoc()) { | ||||||||
11167 | NestedNameSpecifierLoc QualifierLoc | ||||||||
11168 | = getDerived().TransformNestedNameSpecifierLoc(Old->getQualifierLoc()); | ||||||||
11169 | if (!QualifierLoc) | ||||||||
11170 | return ExprError(); | ||||||||
11171 | |||||||||
11172 | SS.Adopt(QualifierLoc); | ||||||||
11173 | } | ||||||||
11174 | |||||||||
11175 | if (Old->getNamingClass()) { | ||||||||
11176 | CXXRecordDecl *NamingClass | ||||||||
11177 | = cast_or_null<CXXRecordDecl>(getDerived().TransformDecl( | ||||||||
11178 | Old->getNameLoc(), | ||||||||
11179 | Old->getNamingClass())); | ||||||||
11180 | if (!NamingClass) { | ||||||||
11181 | R.clear(); | ||||||||
11182 | return ExprError(); | ||||||||
11183 | } | ||||||||
11184 | |||||||||
11185 | R.setNamingClass(NamingClass); | ||||||||
11186 | } | ||||||||
11187 | |||||||||
11188 | SourceLocation TemplateKWLoc = Old->getTemplateKeywordLoc(); | ||||||||
11189 | |||||||||
11190 | // If we have neither explicit template arguments, nor the template keyword, | ||||||||
11191 | // it's a normal declaration name or member reference. | ||||||||
11192 | if (!Old->hasExplicitTemplateArgs() && !TemplateKWLoc.isValid()) { | ||||||||
11193 | NamedDecl *D = R.getAsSingle<NamedDecl>(); | ||||||||
11194 | // In a C++11 unevaluated context, an UnresolvedLookupExpr might refer to an | ||||||||
11195 | // instance member. In other contexts, BuildPossibleImplicitMemberExpr will | ||||||||
11196 | // give a good diagnostic. | ||||||||
11197 | if (D && D->isCXXInstanceMember()) { | ||||||||
11198 | return SemaRef.BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, R, | ||||||||
11199 | /*TemplateArgs=*/nullptr, | ||||||||
11200 | /*Scope=*/nullptr); | ||||||||
11201 | } | ||||||||
11202 | |||||||||
11203 | return getDerived().RebuildDeclarationNameExpr(SS, R, Old->requiresADL()); | ||||||||
11204 | } | ||||||||
11205 | |||||||||
11206 | // If we have template arguments, rebuild them, then rebuild the | ||||||||
11207 | // templateid expression. | ||||||||
11208 | TemplateArgumentListInfo TransArgs(Old->getLAngleLoc(), Old->getRAngleLoc()); | ||||||||
11209 | if (Old->hasExplicitTemplateArgs() && | ||||||||
11210 | getDerived().TransformTemplateArguments(Old->getTemplateArgs(), | ||||||||
11211 | Old->getNumTemplateArgs(), | ||||||||
11212 | TransArgs)) { | ||||||||
11213 | R.clear(); | ||||||||
11214 | return ExprError(); | ||||||||
11215 | } | ||||||||
11216 | |||||||||
11217 | return getDerived().RebuildTemplateIdExpr(SS, TemplateKWLoc, R, | ||||||||
11218 | Old->requiresADL(), &TransArgs); | ||||||||
11219 | } | ||||||||
11220 | |||||||||
11221 | template<typename Derived> | ||||||||
11222 | ExprResult | ||||||||
11223 | TreeTransform<Derived>::TransformTypeTraitExpr(TypeTraitExpr *E) { | ||||||||
11224 | bool ArgChanged = false; | ||||||||
11225 | SmallVector<TypeSourceInfo *, 4> Args; | ||||||||
11226 | for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) { | ||||||||
11227 | TypeSourceInfo *From = E->getArg(I); | ||||||||
11228 | TypeLoc FromTL = From->getTypeLoc(); | ||||||||
11229 | if (!FromTL.getAs<PackExpansionTypeLoc>()) { | ||||||||
11230 | TypeLocBuilder TLB; | ||||||||
11231 | TLB.reserve(FromTL.getFullDataSize()); | ||||||||
11232 | QualType To = getDerived().TransformType(TLB, FromTL); | ||||||||
11233 | if (To.isNull()) | ||||||||
11234 | return ExprError(); | ||||||||
11235 | |||||||||
11236 | if (To == From->getType()) | ||||||||
11237 | Args.push_back(From); | ||||||||
11238 | else { | ||||||||
11239 | Args.push_back(TLB.getTypeSourceInfo(SemaRef.Context, To)); | ||||||||
11240 | ArgChanged = true; | ||||||||
11241 | } | ||||||||
11242 | continue; | ||||||||
11243 | } | ||||||||
11244 | |||||||||
11245 | ArgChanged = true; | ||||||||
11246 | |||||||||
11247 | // We have a pack expansion. Instantiate it. | ||||||||
11248 | PackExpansionTypeLoc ExpansionTL = FromTL.castAs<PackExpansionTypeLoc>(); | ||||||||
11249 | TypeLoc PatternTL = ExpansionTL.getPatternLoc(); | ||||||||
11250 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | ||||||||
11251 | SemaRef.collectUnexpandedParameterPacks(PatternTL, Unexpanded); | ||||||||
11252 | |||||||||
11253 | // Determine whether the set of unexpanded parameter packs can and should | ||||||||
11254 | // be expanded. | ||||||||
11255 | bool Expand = true; | ||||||||
11256 | bool RetainExpansion = false; | ||||||||
11257 | Optional<unsigned> OrigNumExpansions = | ||||||||
11258 | ExpansionTL.getTypePtr()->getNumExpansions(); | ||||||||
11259 | Optional<unsigned> NumExpansions = OrigNumExpansions; | ||||||||
11260 | if (getDerived().TryExpandParameterPacks(ExpansionTL.getEllipsisLoc(), | ||||||||
11261 | PatternTL.getSourceRange(), | ||||||||
11262 | Unexpanded, | ||||||||
11263 | Expand, RetainExpansion, | ||||||||
11264 | NumExpansions)) | ||||||||
11265 | return ExprError(); | ||||||||
11266 | |||||||||
11267 | if (!Expand) { | ||||||||
11268 | // The transform has determined that we should perform a simple | ||||||||
11269 | // transformation on the pack expansion, producing another pack | ||||||||
11270 | // expansion. | ||||||||
11271 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1); | ||||||||
11272 | |||||||||
11273 | TypeLocBuilder TLB; | ||||||||
11274 | TLB.reserve(From->getTypeLoc().getFullDataSize()); | ||||||||
11275 | |||||||||
11276 | QualType To = getDerived().TransformType(TLB, PatternTL); | ||||||||
11277 | if (To.isNull()) | ||||||||
11278 | return ExprError(); | ||||||||
11279 | |||||||||
11280 | To = getDerived().RebuildPackExpansionType(To, | ||||||||
11281 | PatternTL.getSourceRange(), | ||||||||
11282 | ExpansionTL.getEllipsisLoc(), | ||||||||
11283 | NumExpansions); | ||||||||
11284 | if (To.isNull()) | ||||||||
11285 | return ExprError(); | ||||||||
11286 | |||||||||
11287 | PackExpansionTypeLoc ToExpansionTL | ||||||||
11288 | = TLB.push<PackExpansionTypeLoc>(To); | ||||||||
11289 | ToExpansionTL.setEllipsisLoc(ExpansionTL.getEllipsisLoc()); | ||||||||
11290 | Args.push_back(TLB.getTypeSourceInfo(SemaRef.Context, To)); | ||||||||
11291 | continue; | ||||||||
11292 | } | ||||||||
11293 | |||||||||
11294 | // Expand the pack expansion by substituting for each argument in the | ||||||||
11295 | // pack(s). | ||||||||
11296 | for (unsigned I = 0; I != *NumExpansions; ++I) { | ||||||||
11297 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); | ||||||||
11298 | TypeLocBuilder TLB; | ||||||||
11299 | TLB.reserve(PatternTL.getFullDataSize()); | ||||||||
11300 | QualType To = getDerived().TransformType(TLB, PatternTL); | ||||||||
11301 | if (To.isNull()) | ||||||||
11302 | return ExprError(); | ||||||||
11303 | |||||||||
11304 | if (To->containsUnexpandedParameterPack()) { | ||||||||
11305 | To = getDerived().RebuildPackExpansionType(To, | ||||||||
11306 | PatternTL.getSourceRange(), | ||||||||
11307 | ExpansionTL.getEllipsisLoc(), | ||||||||
11308 | NumExpansions); | ||||||||
11309 | if (To.isNull()) | ||||||||
11310 | return ExprError(); | ||||||||
11311 | |||||||||
11312 | PackExpansionTypeLoc ToExpansionTL | ||||||||
11313 | = TLB.push<PackExpansionTypeLoc>(To); | ||||||||
11314 | ToExpansionTL.setEllipsisLoc(ExpansionTL.getEllipsisLoc()); | ||||||||
11315 | } | ||||||||
11316 | |||||||||
11317 | Args.push_back(TLB.getTypeSourceInfo(SemaRef.Context, To)); | ||||||||
11318 | } | ||||||||
11319 | |||||||||
11320 | if (!RetainExpansion) | ||||||||
11321 | continue; | ||||||||
11322 | |||||||||
11323 | // If we're supposed to retain a pack expansion, do so by temporarily | ||||||||
11324 | // forgetting the partially-substituted parameter pack. | ||||||||
11325 | ForgetPartiallySubstitutedPackRAII Forget(getDerived()); | ||||||||
11326 | |||||||||
11327 | TypeLocBuilder TLB; | ||||||||
11328 | TLB.reserve(From->getTypeLoc().getFullDataSize()); | ||||||||
11329 | |||||||||
11330 | QualType To = getDerived().TransformType(TLB, PatternTL); | ||||||||
11331 | if (To.isNull()) | ||||||||
11332 | return ExprError(); | ||||||||
11333 | |||||||||
11334 | To = getDerived().RebuildPackExpansionType(To, | ||||||||
11335 | PatternTL.getSourceRange(), | ||||||||
11336 | ExpansionTL.getEllipsisLoc(), | ||||||||
11337 | NumExpansions); | ||||||||
11338 | if (To.isNull()) | ||||||||
11339 | return ExprError(); | ||||||||
11340 | |||||||||
11341 | PackExpansionTypeLoc ToExpansionTL | ||||||||
11342 | = TLB.push<PackExpansionTypeLoc>(To); | ||||||||
11343 | ToExpansionTL.setEllipsisLoc(ExpansionTL.getEllipsisLoc()); | ||||||||
11344 | Args.push_back(TLB.getTypeSourceInfo(SemaRef.Context, To)); | ||||||||
11345 | } | ||||||||
11346 | |||||||||
11347 | if (!getDerived().AlwaysRebuild() && !ArgChanged) | ||||||||
11348 | return E; | ||||||||
11349 | |||||||||
11350 | return getDerived().RebuildTypeTrait(E->getTrait(), E->getBeginLoc(), Args, | ||||||||
11351 | E->getEndLoc()); | ||||||||
11352 | } | ||||||||
11353 | |||||||||
11354 | template<typename Derived> | ||||||||
11355 | ExprResult | ||||||||
11356 | TreeTransform<Derived>::TransformConceptSpecializationExpr( | ||||||||
11357 | ConceptSpecializationExpr *E) { | ||||||||
11358 | const ASTTemplateArgumentListInfo *Old = E->getTemplateArgsAsWritten(); | ||||||||
11359 | TemplateArgumentListInfo TransArgs(Old->LAngleLoc, Old->RAngleLoc); | ||||||||
11360 | if (getDerived().TransformTemplateArguments(Old->getTemplateArgs(), | ||||||||
11361 | Old->NumTemplateArgs, TransArgs)) | ||||||||
11362 | return ExprError(); | ||||||||
11363 | |||||||||
11364 | return getDerived().RebuildConceptSpecializationExpr( | ||||||||
11365 | E->getNestedNameSpecifierLoc(), E->getTemplateKWLoc(), | ||||||||
11366 | E->getConceptNameInfo(), E->getFoundDecl(), E->getNamedConcept(), | ||||||||
11367 | &TransArgs); | ||||||||
11368 | } | ||||||||
11369 | |||||||||
11370 | template<typename Derived> | ||||||||
11371 | ExprResult | ||||||||
11372 | TreeTransform<Derived>::TransformRequiresExpr(RequiresExpr *E) { | ||||||||
11373 | SmallVector<ParmVarDecl*, 4> TransParams; | ||||||||
11374 | SmallVector<QualType, 4> TransParamTypes; | ||||||||
11375 | Sema::ExtParameterInfoBuilder ExtParamInfos; | ||||||||
11376 | |||||||||
11377 | // C++2a [expr.prim.req]p2 | ||||||||
11378 | // Expressions appearing within a requirement-body are unevaluated operands. | ||||||||
11379 | EnterExpressionEvaluationContext Ctx( | ||||||||
11380 | SemaRef, Sema::ExpressionEvaluationContext::Unevaluated); | ||||||||
11381 | |||||||||
11382 | RequiresExprBodyDecl *Body = RequiresExprBodyDecl::Create( | ||||||||
11383 | getSema().Context, E->getBody()->getDeclContext(), | ||||||||
11384 | E->getBody()->getBeginLoc()); | ||||||||
11385 | |||||||||
11386 | Sema::ContextRAII SavedContext(getSema(), Body, /*NewThisContext*/false); | ||||||||
11387 | |||||||||
11388 | if (getDerived().TransformFunctionTypeParams(E->getRequiresKWLoc(), | ||||||||
11389 | E->getLocalParameters(), | ||||||||
11390 | /*ParamTypes=*/nullptr, | ||||||||
11391 | /*ParamInfos=*/nullptr, | ||||||||
11392 | TransParamTypes, &TransParams, | ||||||||
11393 | ExtParamInfos)) | ||||||||
11394 | return ExprError(); | ||||||||
11395 | |||||||||
11396 | for (ParmVarDecl *Param : TransParams) | ||||||||
11397 | Param->setDeclContext(Body); | ||||||||
11398 | |||||||||
11399 | SmallVector<concepts::Requirement *, 4> TransReqs; | ||||||||
11400 | if (getDerived().TransformRequiresExprRequirements(E->getRequirements(), | ||||||||
11401 | TransReqs)) | ||||||||
11402 | return ExprError(); | ||||||||
11403 | |||||||||
11404 | for (concepts::Requirement *Req : TransReqs) { | ||||||||
11405 | if (auto *ER = dyn_cast<concepts::ExprRequirement>(Req)) { | ||||||||
11406 | if (ER->getReturnTypeRequirement().isTypeConstraint()) { | ||||||||
11407 | ER->getReturnTypeRequirement() | ||||||||
11408 | .getTypeConstraintTemplateParameterList()->getParam(0) | ||||||||
11409 | ->setDeclContext(Body); | ||||||||
11410 | } | ||||||||
11411 | } | ||||||||
11412 | } | ||||||||
11413 | |||||||||
11414 | return getDerived().RebuildRequiresExpr(E->getRequiresKWLoc(), Body, | ||||||||
11415 | TransParams, TransReqs, | ||||||||
11416 | E->getRBraceLoc()); | ||||||||
11417 | } | ||||||||
11418 | |||||||||
11419 | template<typename Derived> | ||||||||
11420 | bool TreeTransform<Derived>::TransformRequiresExprRequirements( | ||||||||
11421 | ArrayRef<concepts::Requirement *> Reqs, | ||||||||
11422 | SmallVectorImpl<concepts::Requirement *> &Transformed) { | ||||||||
11423 | for (concepts::Requirement *Req : Reqs) { | ||||||||
11424 | concepts::Requirement *TransReq = nullptr; | ||||||||
11425 | if (auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req)) | ||||||||
11426 | TransReq = getDerived().TransformTypeRequirement(TypeReq); | ||||||||
11427 | else if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req)) | ||||||||
11428 | TransReq = getDerived().TransformExprRequirement(ExprReq); | ||||||||
11429 | else | ||||||||
11430 | TransReq = getDerived().TransformNestedRequirement( | ||||||||
11431 | cast<concepts::NestedRequirement>(Req)); | ||||||||
11432 | if (!TransReq) | ||||||||
11433 | return true; | ||||||||
11434 | Transformed.push_back(TransReq); | ||||||||
11435 | } | ||||||||
11436 | return false; | ||||||||
11437 | } | ||||||||
11438 | |||||||||
11439 | template<typename Derived> | ||||||||
11440 | concepts::TypeRequirement * | ||||||||
11441 | TreeTransform<Derived>::TransformTypeRequirement( | ||||||||
11442 | concepts::TypeRequirement *Req) { | ||||||||
11443 | if (Req->isSubstitutionFailure()) { | ||||||||
11444 | if (getDerived().AlwaysRebuild()) | ||||||||
11445 | return getDerived().RebuildTypeRequirement( | ||||||||
11446 | Req->getSubstitutionDiagnostic()); | ||||||||
11447 | return Req; | ||||||||
11448 | } | ||||||||
11449 | TypeSourceInfo *TransType = getDerived().TransformType(Req->getType()); | ||||||||
11450 | if (!TransType) | ||||||||
11451 | return nullptr; | ||||||||
11452 | return getDerived().RebuildTypeRequirement(TransType); | ||||||||
11453 | } | ||||||||
11454 | |||||||||
11455 | template<typename Derived> | ||||||||
11456 | concepts::ExprRequirement * | ||||||||
11457 | TreeTransform<Derived>::TransformExprRequirement(concepts::ExprRequirement *Req) { | ||||||||
11458 | llvm::PointerUnion<Expr *, concepts::Requirement::SubstitutionDiagnostic *> TransExpr; | ||||||||
11459 | if (Req->isExprSubstitutionFailure()) | ||||||||
11460 | TransExpr = Req->getExprSubstitutionDiagnostic(); | ||||||||
11461 | else { | ||||||||
11462 | ExprResult TransExprRes = getDerived().TransformExpr(Req->getExpr()); | ||||||||
11463 | if (TransExprRes.isInvalid()) | ||||||||
11464 | return nullptr; | ||||||||
11465 | TransExpr = TransExprRes.get(); | ||||||||
11466 | } | ||||||||
11467 | |||||||||
11468 | llvm::Optional<concepts::ExprRequirement::ReturnTypeRequirement> TransRetReq; | ||||||||
11469 | const auto &RetReq = Req->getReturnTypeRequirement(); | ||||||||
11470 | if (RetReq.isEmpty()) | ||||||||
11471 | TransRetReq.emplace(); | ||||||||
11472 | else if (RetReq.isSubstitutionFailure()) | ||||||||
11473 | TransRetReq.emplace(RetReq.getSubstitutionDiagnostic()); | ||||||||
11474 | else if (RetReq.isTypeConstraint()) { | ||||||||
11475 | TemplateParameterList *OrigTPL = | ||||||||
11476 | RetReq.getTypeConstraintTemplateParameterList(); | ||||||||
11477 | TemplateParameterList *TPL = | ||||||||
11478 | getDerived().TransformTemplateParameterList(OrigTPL); | ||||||||
11479 | if (!TPL) | ||||||||
11480 | return nullptr; | ||||||||
11481 | TransRetReq.emplace(TPL); | ||||||||
11482 | } | ||||||||
11483 | assert(TransRetReq.hasValue() &&((TransRetReq.hasValue() && "All code paths leading here must set TransRetReq" ) ? static_cast<void> (0) : __assert_fail ("TransRetReq.hasValue() && \"All code paths leading here must set TransRetReq\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 11484, __PRETTY_FUNCTION__)) | ||||||||
11484 | "All code paths leading here must set TransRetReq")((TransRetReq.hasValue() && "All code paths leading here must set TransRetReq" ) ? static_cast<void> (0) : __assert_fail ("TransRetReq.hasValue() && \"All code paths leading here must set TransRetReq\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 11484, __PRETTY_FUNCTION__)); | ||||||||
11485 | if (Expr *E = TransExpr.dyn_cast<Expr *>()) | ||||||||
11486 | return getDerived().RebuildExprRequirement(E, Req->isSimple(), | ||||||||
11487 | Req->getNoexceptLoc(), | ||||||||
11488 | std::move(*TransRetReq)); | ||||||||
11489 | return getDerived().RebuildExprRequirement( | ||||||||
11490 | TransExpr.get<concepts::Requirement::SubstitutionDiagnostic *>(), | ||||||||
11491 | Req->isSimple(), Req->getNoexceptLoc(), std::move(*TransRetReq)); | ||||||||
11492 | } | ||||||||
11493 | |||||||||
11494 | template<typename Derived> | ||||||||
11495 | concepts::NestedRequirement * | ||||||||
11496 | TreeTransform<Derived>::TransformNestedRequirement( | ||||||||
11497 | concepts::NestedRequirement *Req) { | ||||||||
11498 | if (Req->isSubstitutionFailure()) { | ||||||||
11499 | if (getDerived().AlwaysRebuild()) | ||||||||
11500 | return getDerived().RebuildNestedRequirement( | ||||||||
11501 | Req->getSubstitutionDiagnostic()); | ||||||||
11502 | return Req; | ||||||||
11503 | } | ||||||||
11504 | ExprResult TransConstraint = | ||||||||
11505 | getDerived().TransformExpr(Req->getConstraintExpr()); | ||||||||
11506 | if (TransConstraint.isInvalid()) | ||||||||
11507 | return nullptr; | ||||||||
11508 | return getDerived().RebuildNestedRequirement(TransConstraint.get()); | ||||||||
11509 | } | ||||||||
11510 | |||||||||
11511 | template<typename Derived> | ||||||||
11512 | ExprResult | ||||||||
11513 | TreeTransform<Derived>::TransformArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { | ||||||||
11514 | TypeSourceInfo *T = getDerived().TransformType(E->getQueriedTypeSourceInfo()); | ||||||||
11515 | if (!T) | ||||||||
11516 | return ExprError(); | ||||||||
11517 | |||||||||
11518 | if (!getDerived().AlwaysRebuild() && | ||||||||
11519 | T == E->getQueriedTypeSourceInfo()) | ||||||||
11520 | return E; | ||||||||
11521 | |||||||||
11522 | ExprResult SubExpr; | ||||||||
11523 | { | ||||||||
11524 | EnterExpressionEvaluationContext Unevaluated( | ||||||||
11525 | SemaRef, Sema::ExpressionEvaluationContext::Unevaluated); | ||||||||
11526 | SubExpr = getDerived().TransformExpr(E->getDimensionExpression()); | ||||||||
11527 | if (SubExpr.isInvalid()) | ||||||||
11528 | return ExprError(); | ||||||||
11529 | |||||||||
11530 | if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getDimensionExpression()) | ||||||||
11531 | return E; | ||||||||
11532 | } | ||||||||
11533 | |||||||||
11534 | return getDerived().RebuildArrayTypeTrait(E->getTrait(), E->getBeginLoc(), T, | ||||||||
11535 | SubExpr.get(), E->getEndLoc()); | ||||||||
11536 | } | ||||||||
11537 | |||||||||
11538 | template<typename Derived> | ||||||||
11539 | ExprResult | ||||||||
11540 | TreeTransform<Derived>::TransformExpressionTraitExpr(ExpressionTraitExpr *E) { | ||||||||
11541 | ExprResult SubExpr; | ||||||||
11542 | { | ||||||||
11543 | EnterExpressionEvaluationContext Unevaluated( | ||||||||
11544 | SemaRef, Sema::ExpressionEvaluationContext::Unevaluated); | ||||||||
11545 | SubExpr = getDerived().TransformExpr(E->getQueriedExpression()); | ||||||||
11546 | if (SubExpr.isInvalid()) | ||||||||
11547 | return ExprError(); | ||||||||
11548 | |||||||||
11549 | if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getQueriedExpression()) | ||||||||
11550 | return E; | ||||||||
11551 | } | ||||||||
11552 | |||||||||
11553 | return getDerived().RebuildExpressionTrait(E->getTrait(), E->getBeginLoc(), | ||||||||
11554 | SubExpr.get(), E->getEndLoc()); | ||||||||
11555 | } | ||||||||
11556 | |||||||||
11557 | template <typename Derived> | ||||||||
11558 | ExprResult TreeTransform<Derived>::TransformParenDependentScopeDeclRefExpr( | ||||||||
11559 | ParenExpr *PE, DependentScopeDeclRefExpr *DRE, bool AddrTaken, | ||||||||
11560 | TypeSourceInfo **RecoveryTSI) { | ||||||||
11561 | ExprResult NewDRE = getDerived().TransformDependentScopeDeclRefExpr( | ||||||||
11562 | DRE, AddrTaken, RecoveryTSI); | ||||||||
11563 | |||||||||
11564 | // Propagate both errors and recovered types, which return ExprEmpty. | ||||||||
11565 | if (!NewDRE.isUsable()) | ||||||||
11566 | return NewDRE; | ||||||||
11567 | |||||||||
11568 | // We got an expr, wrap it up in parens. | ||||||||
11569 | if (!getDerived().AlwaysRebuild() && NewDRE.get() == DRE) | ||||||||
11570 | return PE; | ||||||||
11571 | return getDerived().RebuildParenExpr(NewDRE.get(), PE->getLParen(), | ||||||||
11572 | PE->getRParen()); | ||||||||
11573 | } | ||||||||
11574 | |||||||||
11575 | template <typename Derived> | ||||||||
11576 | ExprResult TreeTransform<Derived>::TransformDependentScopeDeclRefExpr( | ||||||||
11577 | DependentScopeDeclRefExpr *E) { | ||||||||
11578 | return TransformDependentScopeDeclRefExpr(E, /*IsAddressOfOperand=*/false, | ||||||||
11579 | nullptr); | ||||||||
11580 | } | ||||||||
11581 | |||||||||
11582 | template<typename Derived> | ||||||||
11583 | ExprResult | ||||||||
11584 | TreeTransform<Derived>::TransformDependentScopeDeclRefExpr( | ||||||||
11585 | DependentScopeDeclRefExpr *E, | ||||||||
11586 | bool IsAddressOfOperand, | ||||||||
11587 | TypeSourceInfo **RecoveryTSI) { | ||||||||
11588 | assert(E->getQualifierLoc())((E->getQualifierLoc()) ? static_cast<void> (0) : __assert_fail ("E->getQualifierLoc()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 11588, __PRETTY_FUNCTION__)); | ||||||||
11589 | NestedNameSpecifierLoc QualifierLoc | ||||||||
11590 | = getDerived().TransformNestedNameSpecifierLoc(E->getQualifierLoc()); | ||||||||
11591 | if (!QualifierLoc) | ||||||||
11592 | return ExprError(); | ||||||||
11593 | SourceLocation TemplateKWLoc = E->getTemplateKeywordLoc(); | ||||||||
11594 | |||||||||
11595 | // TODO: If this is a conversion-function-id, verify that the | ||||||||
11596 | // destination type name (if present) resolves the same way after | ||||||||
11597 | // instantiation as it did in the local scope. | ||||||||
11598 | |||||||||
11599 | DeclarationNameInfo NameInfo | ||||||||
11600 | = getDerived().TransformDeclarationNameInfo(E->getNameInfo()); | ||||||||
11601 | if (!NameInfo.getName()) | ||||||||
11602 | return ExprError(); | ||||||||
11603 | |||||||||
11604 | if (!E->hasExplicitTemplateArgs()) { | ||||||||
11605 | if (!getDerived().AlwaysRebuild() && | ||||||||
11606 | QualifierLoc == E->getQualifierLoc() && | ||||||||
11607 | // Note: it is sufficient to compare the Name component of NameInfo: | ||||||||
11608 | // if name has not changed, DNLoc has not changed either. | ||||||||
11609 | NameInfo.getName() == E->getDeclName()) | ||||||||
11610 | return E; | ||||||||
11611 | |||||||||
11612 | return getDerived().RebuildDependentScopeDeclRefExpr( | ||||||||
11613 | QualifierLoc, TemplateKWLoc, NameInfo, /*TemplateArgs=*/nullptr, | ||||||||
11614 | IsAddressOfOperand, RecoveryTSI); | ||||||||
11615 | } | ||||||||
11616 | |||||||||
11617 | TemplateArgumentListInfo TransArgs(E->getLAngleLoc(), E->getRAngleLoc()); | ||||||||
11618 | if (getDerived().TransformTemplateArguments(E->getTemplateArgs(), | ||||||||
11619 | E->getNumTemplateArgs(), | ||||||||
11620 | TransArgs)) | ||||||||
11621 | return ExprError(); | ||||||||
11622 | |||||||||
11623 | return getDerived().RebuildDependentScopeDeclRefExpr( | ||||||||
11624 | QualifierLoc, TemplateKWLoc, NameInfo, &TransArgs, IsAddressOfOperand, | ||||||||
11625 | RecoveryTSI); | ||||||||
11626 | } | ||||||||
11627 | |||||||||
11628 | template<typename Derived> | ||||||||
11629 | ExprResult | ||||||||
11630 | TreeTransform<Derived>::TransformCXXConstructExpr(CXXConstructExpr *E) { | ||||||||
11631 | // CXXConstructExprs other than for list-initialization and | ||||||||
11632 | // CXXTemporaryObjectExpr are always implicit, so when we have | ||||||||
11633 | // a 1-argument construction we just transform that argument. | ||||||||
11634 | if ((E->getNumArgs() == 1 || | ||||||||
11635 | (E->getNumArgs() > 1 && getDerived().DropCallArgument(E->getArg(1)))) && | ||||||||
11636 | (!getDerived().DropCallArgument(E->getArg(0))) && | ||||||||
11637 | !E->isListInitialization()) | ||||||||
11638 | return getDerived().TransformExpr(E->getArg(0)); | ||||||||
11639 | |||||||||
11640 | TemporaryBase Rebase(*this, /*FIXME*/ E->getBeginLoc(), DeclarationName()); | ||||||||
11641 | |||||||||
11642 | QualType T = getDerived().TransformType(E->getType()); | ||||||||
11643 | if (T.isNull()) | ||||||||
11644 | return ExprError(); | ||||||||
11645 | |||||||||
11646 | CXXConstructorDecl *Constructor = cast_or_null<CXXConstructorDecl>( | ||||||||
11647 | getDerived().TransformDecl(E->getBeginLoc(), E->getConstructor())); | ||||||||
11648 | if (!Constructor) | ||||||||
11649 | return ExprError(); | ||||||||
11650 | |||||||||
11651 | bool ArgumentChanged = false; | ||||||||
11652 | SmallVector<Expr*, 8> Args; | ||||||||
11653 | { | ||||||||
11654 | EnterExpressionEvaluationContext Context( | ||||||||
11655 | getSema(), EnterExpressionEvaluationContext::InitList, | ||||||||
11656 | E->isListInitialization()); | ||||||||
11657 | if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), true, Args, | ||||||||
11658 | &ArgumentChanged)) | ||||||||
11659 | return ExprError(); | ||||||||
11660 | } | ||||||||
11661 | |||||||||
11662 | if (!getDerived().AlwaysRebuild() && | ||||||||
11663 | T == E->getType() && | ||||||||
11664 | Constructor == E->getConstructor() && | ||||||||
11665 | !ArgumentChanged) { | ||||||||
11666 | // Mark the constructor as referenced. | ||||||||
11667 | // FIXME: Instantiation-specific | ||||||||
11668 | SemaRef.MarkFunctionReferenced(E->getBeginLoc(), Constructor); | ||||||||
11669 | return E; | ||||||||
11670 | } | ||||||||
11671 | |||||||||
11672 | return getDerived().RebuildCXXConstructExpr( | ||||||||
11673 | T, /*FIXME:*/ E->getBeginLoc(), Constructor, E->isElidable(), Args, | ||||||||
11674 | E->hadMultipleCandidates(), E->isListInitialization(), | ||||||||
11675 | E->isStdInitListInitialization(), E->requiresZeroInitialization(), | ||||||||
11676 | E->getConstructionKind(), E->getParenOrBraceRange()); | ||||||||
11677 | } | ||||||||
11678 | |||||||||
11679 | template<typename Derived> | ||||||||
11680 | ExprResult TreeTransform<Derived>::TransformCXXInheritedCtorInitExpr( | ||||||||
11681 | CXXInheritedCtorInitExpr *E) { | ||||||||
11682 | QualType T = getDerived().TransformType(E->getType()); | ||||||||
11683 | if (T.isNull()) | ||||||||
11684 | return ExprError(); | ||||||||
11685 | |||||||||
11686 | CXXConstructorDecl *Constructor = cast_or_null<CXXConstructorDecl>( | ||||||||
11687 | getDerived().TransformDecl(E->getBeginLoc(), E->getConstructor())); | ||||||||
11688 | if (!Constructor) | ||||||||
11689 | return ExprError(); | ||||||||
11690 | |||||||||
11691 | if (!getDerived().AlwaysRebuild() && | ||||||||
11692 | T == E->getType() && | ||||||||
11693 | Constructor == E->getConstructor()) { | ||||||||
11694 | // Mark the constructor as referenced. | ||||||||
11695 | // FIXME: Instantiation-specific | ||||||||
11696 | SemaRef.MarkFunctionReferenced(E->getBeginLoc(), Constructor); | ||||||||
11697 | return E; | ||||||||
11698 | } | ||||||||
11699 | |||||||||
11700 | return getDerived().RebuildCXXInheritedCtorInitExpr( | ||||||||
11701 | T, E->getLocation(), Constructor, | ||||||||
11702 | E->constructsVBase(), E->inheritedFromVBase()); | ||||||||
11703 | } | ||||||||
11704 | |||||||||
11705 | /// Transform a C++ temporary-binding expression. | ||||||||
11706 | /// | ||||||||
11707 | /// Since CXXBindTemporaryExpr nodes are implicitly generated, we just | ||||||||
11708 | /// transform the subexpression and return that. | ||||||||
11709 | template<typename Derived> | ||||||||
11710 | ExprResult | ||||||||
11711 | TreeTransform<Derived>::TransformCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { | ||||||||
11712 | return getDerived().TransformExpr(E->getSubExpr()); | ||||||||
11713 | } | ||||||||
11714 | |||||||||
11715 | /// Transform a C++ expression that contains cleanups that should | ||||||||
11716 | /// be run after the expression is evaluated. | ||||||||
11717 | /// | ||||||||
11718 | /// Since ExprWithCleanups nodes are implicitly generated, we | ||||||||
11719 | /// just transform the subexpression and return that. | ||||||||
11720 | template<typename Derived> | ||||||||
11721 | ExprResult | ||||||||
11722 | TreeTransform<Derived>::TransformExprWithCleanups(ExprWithCleanups *E) { | ||||||||
11723 | return getDerived().TransformExpr(E->getSubExpr()); | ||||||||
11724 | } | ||||||||
11725 | |||||||||
11726 | template<typename Derived> | ||||||||
11727 | ExprResult | ||||||||
11728 | TreeTransform<Derived>::TransformCXXTemporaryObjectExpr( | ||||||||
11729 | CXXTemporaryObjectExpr *E) { | ||||||||
11730 | TypeSourceInfo *T = | ||||||||
11731 | getDerived().TransformTypeWithDeducedTST(E->getTypeSourceInfo()); | ||||||||
11732 | if (!T) | ||||||||
11733 | return ExprError(); | ||||||||
11734 | |||||||||
11735 | CXXConstructorDecl *Constructor = cast_or_null<CXXConstructorDecl>( | ||||||||
11736 | getDerived().TransformDecl(E->getBeginLoc(), E->getConstructor())); | ||||||||
11737 | if (!Constructor) | ||||||||
11738 | return ExprError(); | ||||||||
11739 | |||||||||
11740 | bool ArgumentChanged = false; | ||||||||
11741 | SmallVector<Expr*, 8> Args; | ||||||||
11742 | Args.reserve(E->getNumArgs()); | ||||||||
11743 | { | ||||||||
11744 | EnterExpressionEvaluationContext Context( | ||||||||
11745 | getSema(), EnterExpressionEvaluationContext::InitList, | ||||||||
11746 | E->isListInitialization()); | ||||||||
11747 | if (TransformExprs(E->getArgs(), E->getNumArgs(), true, Args, | ||||||||
11748 | &ArgumentChanged)) | ||||||||
11749 | return ExprError(); | ||||||||
11750 | } | ||||||||
11751 | |||||||||
11752 | if (!getDerived().AlwaysRebuild() && | ||||||||
11753 | T == E->getTypeSourceInfo() && | ||||||||
11754 | Constructor == E->getConstructor() && | ||||||||
11755 | !ArgumentChanged) { | ||||||||
11756 | // FIXME: Instantiation-specific | ||||||||
11757 | SemaRef.MarkFunctionReferenced(E->getBeginLoc(), Constructor); | ||||||||
11758 | return SemaRef.MaybeBindToTemporary(E); | ||||||||
11759 | } | ||||||||
11760 | |||||||||
11761 | // FIXME: We should just pass E->isListInitialization(), but we're not | ||||||||
11762 | // prepared to handle list-initialization without a child InitListExpr. | ||||||||
11763 | SourceLocation LParenLoc = T->getTypeLoc().getEndLoc(); | ||||||||
11764 | return getDerived().RebuildCXXTemporaryObjectExpr( | ||||||||
11765 | T, LParenLoc, Args, E->getEndLoc(), | ||||||||
11766 | /*ListInitialization=*/LParenLoc.isInvalid()); | ||||||||
11767 | } | ||||||||
11768 | |||||||||
11769 | template<typename Derived> | ||||||||
11770 | ExprResult | ||||||||
11771 | TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) { | ||||||||
11772 | // Transform any init-capture expressions before entering the scope of the | ||||||||
11773 | // lambda body, because they are not semantically within that scope. | ||||||||
11774 | typedef std::pair<ExprResult, QualType> InitCaptureInfoTy; | ||||||||
11775 | struct TransformedInitCapture { | ||||||||
11776 | // The location of the ... if the result is retaining a pack expansion. | ||||||||
11777 | SourceLocation EllipsisLoc; | ||||||||
11778 | // Zero or more expansions of the init-capture. | ||||||||
11779 | SmallVector<InitCaptureInfoTy, 4> Expansions; | ||||||||
11780 | }; | ||||||||
11781 | SmallVector<TransformedInitCapture, 4> InitCaptures; | ||||||||
11782 | InitCaptures.resize(E->explicit_capture_end() - E->explicit_capture_begin()); | ||||||||
11783 | for (LambdaExpr::capture_iterator C = E->capture_begin(), | ||||||||
11784 | CEnd = E->capture_end(); | ||||||||
11785 | C != CEnd; ++C) { | ||||||||
11786 | if (!E->isInitCapture(C)) | ||||||||
11787 | continue; | ||||||||
11788 | |||||||||
11789 | TransformedInitCapture &Result = InitCaptures[C - E->capture_begin()]; | ||||||||
11790 | VarDecl *OldVD = C->getCapturedVar(); | ||||||||
11791 | |||||||||
11792 | auto SubstInitCapture = [&](SourceLocation EllipsisLoc, | ||||||||
11793 | Optional<unsigned> NumExpansions) { | ||||||||
11794 | ExprResult NewExprInitResult = getDerived().TransformInitializer( | ||||||||
11795 | OldVD->getInit(), OldVD->getInitStyle() == VarDecl::CallInit); | ||||||||
11796 | |||||||||
11797 | if (NewExprInitResult.isInvalid()) { | ||||||||
11798 | Result.Expansions.push_back(InitCaptureInfoTy(ExprError(), QualType())); | ||||||||
11799 | return; | ||||||||
11800 | } | ||||||||
11801 | Expr *NewExprInit = NewExprInitResult.get(); | ||||||||
11802 | |||||||||
11803 | QualType NewInitCaptureType = | ||||||||
11804 | getSema().buildLambdaInitCaptureInitialization( | ||||||||
11805 | C->getLocation(), OldVD->getType()->isReferenceType(), | ||||||||
11806 | EllipsisLoc, NumExpansions, OldVD->getIdentifier(), | ||||||||
11807 | C->getCapturedVar()->getInitStyle() != VarDecl::CInit, | ||||||||
11808 | NewExprInit); | ||||||||
11809 | Result.Expansions.push_back( | ||||||||
11810 | InitCaptureInfoTy(NewExprInit, NewInitCaptureType)); | ||||||||
11811 | }; | ||||||||
11812 | |||||||||
11813 | // If this is an init-capture pack, consider expanding the pack now. | ||||||||
11814 | if (OldVD->isParameterPack()) { | ||||||||
11815 | PackExpansionTypeLoc ExpansionTL = OldVD->getTypeSourceInfo() | ||||||||
11816 | ->getTypeLoc() | ||||||||
11817 | .castAs<PackExpansionTypeLoc>(); | ||||||||
11818 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | ||||||||
11819 | SemaRef.collectUnexpandedParameterPacks(OldVD->getInit(), Unexpanded); | ||||||||
11820 | |||||||||
11821 | // Determine whether the set of unexpanded parameter packs can and should | ||||||||
11822 | // be expanded. | ||||||||
11823 | bool Expand = true; | ||||||||
11824 | bool RetainExpansion = false; | ||||||||
11825 | Optional<unsigned> OrigNumExpansions = | ||||||||
11826 | ExpansionTL.getTypePtr()->getNumExpansions(); | ||||||||
11827 | Optional<unsigned> NumExpansions = OrigNumExpansions; | ||||||||
11828 | if (getDerived().TryExpandParameterPacks( | ||||||||
11829 | ExpansionTL.getEllipsisLoc(), | ||||||||
11830 | OldVD->getInit()->getSourceRange(), Unexpanded, Expand, | ||||||||
11831 | RetainExpansion, NumExpansions)) | ||||||||
11832 | return ExprError(); | ||||||||
11833 | if (Expand) { | ||||||||
11834 | for (unsigned I = 0; I != *NumExpansions; ++I) { | ||||||||
11835 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I); | ||||||||
11836 | SubstInitCapture(SourceLocation(), None); | ||||||||
11837 | } | ||||||||
11838 | } | ||||||||
11839 | if (!Expand || RetainExpansion) { | ||||||||
11840 | ForgetPartiallySubstitutedPackRAII Forget(getDerived()); | ||||||||
11841 | SubstInitCapture(ExpansionTL.getEllipsisLoc(), NumExpansions); | ||||||||
11842 | Result.EllipsisLoc = ExpansionTL.getEllipsisLoc(); | ||||||||
11843 | } | ||||||||
11844 | } else { | ||||||||
11845 | SubstInitCapture(SourceLocation(), None); | ||||||||
11846 | } | ||||||||
11847 | } | ||||||||
11848 | |||||||||
11849 | LambdaScopeInfo *LSI = getSema().PushLambdaScope(); | ||||||||
11850 | Sema::FunctionScopeRAII FuncScopeCleanup(getSema()); | ||||||||
11851 | |||||||||
11852 | // Transform the template parameters, and add them to the current | ||||||||
11853 | // instantiation scope. The null case is handled correctly. | ||||||||
11854 | auto TPL = getDerived().TransformTemplateParameterList( | ||||||||
11855 | E->getTemplateParameterList()); | ||||||||
11856 | LSI->GLTemplateParameterList = TPL; | ||||||||
11857 | |||||||||
11858 | // Transform the type of the original lambda's call operator. | ||||||||
11859 | // The transformation MUST be done in the CurrentInstantiationScope since | ||||||||
11860 | // it introduces a mapping of the original to the newly created | ||||||||
11861 | // transformed parameters. | ||||||||
11862 | TypeSourceInfo *NewCallOpTSI = nullptr; | ||||||||
11863 | { | ||||||||
11864 | TypeSourceInfo *OldCallOpTSI = E->getCallOperator()->getTypeSourceInfo(); | ||||||||
11865 | FunctionProtoTypeLoc OldCallOpFPTL = | ||||||||
11866 | OldCallOpTSI->getTypeLoc().getAs<FunctionProtoTypeLoc>(); | ||||||||
11867 | |||||||||
11868 | TypeLocBuilder NewCallOpTLBuilder; | ||||||||
11869 | SmallVector<QualType, 4> ExceptionStorage; | ||||||||
11870 | TreeTransform *This = this; // Work around gcc.gnu.org/PR56135. | ||||||||
11871 | QualType NewCallOpType = TransformFunctionProtoType( | ||||||||
11872 | NewCallOpTLBuilder, OldCallOpFPTL, nullptr, Qualifiers(), | ||||||||
11873 | [&](FunctionProtoType::ExceptionSpecInfo &ESI, bool &Changed) { | ||||||||
11874 | return This->TransformExceptionSpec(OldCallOpFPTL.getBeginLoc(), ESI, | ||||||||
11875 | ExceptionStorage, Changed); | ||||||||
11876 | }); | ||||||||
11877 | if (NewCallOpType.isNull()) | ||||||||
11878 | return ExprError(); | ||||||||
11879 | NewCallOpTSI = NewCallOpTLBuilder.getTypeSourceInfo(getSema().Context, | ||||||||
11880 | NewCallOpType); | ||||||||
11881 | } | ||||||||
11882 | |||||||||
11883 | // Transform the trailing requires clause | ||||||||
11884 | ExprResult NewTrailingRequiresClause; | ||||||||
11885 | if (Expr *TRC = E->getCallOperator()->getTrailingRequiresClause()) | ||||||||
11886 | // FIXME: Concepts: Substitution into requires clause should only happen | ||||||||
11887 | // when checking satisfaction. | ||||||||
11888 | NewTrailingRequiresClause = getDerived().TransformExpr(TRC); | ||||||||
11889 | |||||||||
11890 | // Create the local class that will describe the lambda. | ||||||||
11891 | CXXRecordDecl *OldClass = E->getLambdaClass(); | ||||||||
11892 | CXXRecordDecl *Class | ||||||||
11893 | = getSema().createLambdaClosureType(E->getIntroducerRange(), | ||||||||
11894 | NewCallOpTSI, | ||||||||
11895 | /*KnownDependent=*/false, | ||||||||
11896 | E->getCaptureDefault()); | ||||||||
11897 | getDerived().transformedLocalDecl(OldClass, {Class}); | ||||||||
11898 | |||||||||
11899 | Optional<std::tuple<unsigned, bool, Decl *>> Mangling; | ||||||||
11900 | if (getDerived().ReplacingOriginal()) | ||||||||
11901 | Mangling = std::make_tuple(OldClass->getLambdaManglingNumber(), | ||||||||
11902 | OldClass->hasKnownLambdaInternalLinkage(), | ||||||||
11903 | OldClass->getLambdaContextDecl()); | ||||||||
11904 | |||||||||
11905 | // Build the call operator. | ||||||||
11906 | CXXMethodDecl *NewCallOperator = getSema().startLambdaDefinition( | ||||||||
11907 | Class, E->getIntroducerRange(), NewCallOpTSI, | ||||||||
11908 | E->getCallOperator()->getEndLoc(), | ||||||||
11909 | NewCallOpTSI->getTypeLoc().castAs<FunctionProtoTypeLoc>().getParams(), | ||||||||
11910 | E->getCallOperator()->getConstexprKind(), | ||||||||
11911 | NewTrailingRequiresClause.get()); | ||||||||
11912 | |||||||||
11913 | LSI->CallOperator = NewCallOperator; | ||||||||
11914 | |||||||||
11915 | for (unsigned I = 0, NumParams = NewCallOperator->getNumParams(); | ||||||||
11916 | I != NumParams; ++I) { | ||||||||
11917 | auto *P = NewCallOperator->getParamDecl(I); | ||||||||
11918 | if (P->hasUninstantiatedDefaultArg()) { | ||||||||
11919 | EnterExpressionEvaluationContext Eval( | ||||||||
11920 | getSema(), | ||||||||
11921 | Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed, P); | ||||||||
11922 | ExprResult R = getDerived().TransformExpr( | ||||||||
11923 | E->getCallOperator()->getParamDecl(I)->getDefaultArg()); | ||||||||
11924 | P->setDefaultArg(R.get()); | ||||||||
11925 | } | ||||||||
11926 | } | ||||||||
11927 | |||||||||
11928 | getDerived().transformAttrs(E->getCallOperator(), NewCallOperator); | ||||||||
11929 | getDerived().transformedLocalDecl(E->getCallOperator(), {NewCallOperator}); | ||||||||
11930 | |||||||||
11931 | // Number the lambda for linkage purposes if necessary. | ||||||||
11932 | getSema().handleLambdaNumbering(Class, NewCallOperator, Mangling); | ||||||||
11933 | |||||||||
11934 | // Introduce the context of the call operator. | ||||||||
11935 | Sema::ContextRAII SavedContext(getSema(), NewCallOperator, | ||||||||
11936 | /*NewThisContext*/false); | ||||||||
11937 | |||||||||
11938 | // Enter the scope of the lambda. | ||||||||
11939 | getSema().buildLambdaScope(LSI, NewCallOperator, | ||||||||
11940 | E->getIntroducerRange(), | ||||||||
11941 | E->getCaptureDefault(), | ||||||||
11942 | E->getCaptureDefaultLoc(), | ||||||||
11943 | E->hasExplicitParameters(), | ||||||||
11944 | E->hasExplicitResultType(), | ||||||||
11945 | E->isMutable()); | ||||||||
11946 | |||||||||
11947 | bool Invalid = false; | ||||||||
11948 | |||||||||
11949 | // Transform captures. | ||||||||
11950 | for (LambdaExpr::capture_iterator C = E->capture_begin(), | ||||||||
11951 | CEnd = E->capture_end(); | ||||||||
11952 | C != CEnd; ++C) { | ||||||||
11953 | // When we hit the first implicit capture, tell Sema that we've finished | ||||||||
11954 | // the list of explicit captures. | ||||||||
11955 | if (C->isImplicit()) | ||||||||
11956 | break; | ||||||||
11957 | |||||||||
11958 | // Capturing 'this' is trivial. | ||||||||
11959 | if (C->capturesThis()) { | ||||||||
11960 | getSema().CheckCXXThisCapture(C->getLocation(), C->isExplicit(), | ||||||||
11961 | /*BuildAndDiagnose*/ true, nullptr, | ||||||||
11962 | C->getCaptureKind() == LCK_StarThis); | ||||||||
11963 | continue; | ||||||||
11964 | } | ||||||||
11965 | // Captured expression will be recaptured during captured variables | ||||||||
11966 | // rebuilding. | ||||||||
11967 | if (C->capturesVLAType()) | ||||||||
11968 | continue; | ||||||||
11969 | |||||||||
11970 | // Rebuild init-captures, including the implied field declaration. | ||||||||
11971 | if (E->isInitCapture(C)) { | ||||||||
11972 | TransformedInitCapture &NewC = InitCaptures[C - E->capture_begin()]; | ||||||||
11973 | |||||||||
11974 | VarDecl *OldVD = C->getCapturedVar(); | ||||||||
11975 | llvm::SmallVector<Decl*, 4> NewVDs; | ||||||||
11976 | |||||||||
11977 | for (InitCaptureInfoTy &Info : NewC.Expansions) { | ||||||||
11978 | ExprResult Init = Info.first; | ||||||||
11979 | QualType InitQualType = Info.second; | ||||||||
11980 | if (Init.isInvalid() || InitQualType.isNull()) { | ||||||||
11981 | Invalid = true; | ||||||||
11982 | break; | ||||||||
11983 | } | ||||||||
11984 | VarDecl *NewVD = getSema().createLambdaInitCaptureVarDecl( | ||||||||
11985 | OldVD->getLocation(), InitQualType, NewC.EllipsisLoc, | ||||||||
11986 | OldVD->getIdentifier(), OldVD->getInitStyle(), Init.get()); | ||||||||
11987 | if (!NewVD) { | ||||||||
11988 | Invalid = true; | ||||||||
11989 | break; | ||||||||
11990 | } | ||||||||
11991 | NewVDs.push_back(NewVD); | ||||||||
11992 | getSema().addInitCapture(LSI, NewVD); | ||||||||
11993 | } | ||||||||
11994 | |||||||||
11995 | if (Invalid) | ||||||||
11996 | break; | ||||||||
11997 | |||||||||
11998 | getDerived().transformedLocalDecl(OldVD, NewVDs); | ||||||||
11999 | continue; | ||||||||
12000 | } | ||||||||
12001 | |||||||||
12002 | assert(C->capturesVariable() && "unexpected kind of lambda capture")((C->capturesVariable() && "unexpected kind of lambda capture" ) ? static_cast<void> (0) : __assert_fail ("C->capturesVariable() && \"unexpected kind of lambda capture\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 12002, __PRETTY_FUNCTION__)); | ||||||||
12003 | |||||||||
12004 | // Determine the capture kind for Sema. | ||||||||
12005 | Sema::TryCaptureKind Kind | ||||||||
12006 | = C->isImplicit()? Sema::TryCapture_Implicit | ||||||||
12007 | : C->getCaptureKind() == LCK_ByCopy | ||||||||
12008 | ? Sema::TryCapture_ExplicitByVal | ||||||||
12009 | : Sema::TryCapture_ExplicitByRef; | ||||||||
12010 | SourceLocation EllipsisLoc; | ||||||||
12011 | if (C->isPackExpansion()) { | ||||||||
12012 | UnexpandedParameterPack Unexpanded(C->getCapturedVar(), C->getLocation()); | ||||||||
12013 | bool ShouldExpand = false; | ||||||||
12014 | bool RetainExpansion = false; | ||||||||
12015 | Optional<unsigned> NumExpansions; | ||||||||
12016 | if (getDerived().TryExpandParameterPacks(C->getEllipsisLoc(), | ||||||||
12017 | C->getLocation(), | ||||||||
12018 | Unexpanded, | ||||||||
12019 | ShouldExpand, RetainExpansion, | ||||||||
12020 | NumExpansions)) { | ||||||||
12021 | Invalid = true; | ||||||||
12022 | continue; | ||||||||
12023 | } | ||||||||
12024 | |||||||||
12025 | if (ShouldExpand) { | ||||||||
12026 | // The transform has determined that we should perform an expansion; | ||||||||
12027 | // transform and capture each of the arguments. | ||||||||
12028 | // expansion of the pattern. Do so. | ||||||||
12029 | VarDecl *Pack = C->getCapturedVar(); | ||||||||
12030 | for (unsigned I = 0; I != *NumExpansions; ++I) { | ||||||||
12031 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I); | ||||||||
12032 | VarDecl *CapturedVar | ||||||||
12033 | = cast_or_null<VarDecl>(getDerived().TransformDecl(C->getLocation(), | ||||||||
12034 | Pack)); | ||||||||
12035 | if (!CapturedVar) { | ||||||||
12036 | Invalid = true; | ||||||||
12037 | continue; | ||||||||
12038 | } | ||||||||
12039 | |||||||||
12040 | // Capture the transformed variable. | ||||||||
12041 | getSema().tryCaptureVariable(CapturedVar, C->getLocation(), Kind); | ||||||||
12042 | } | ||||||||
12043 | |||||||||
12044 | // FIXME: Retain a pack expansion if RetainExpansion is true. | ||||||||
12045 | |||||||||
12046 | continue; | ||||||||
12047 | } | ||||||||
12048 | |||||||||
12049 | EllipsisLoc = C->getEllipsisLoc(); | ||||||||
12050 | } | ||||||||
12051 | |||||||||
12052 | // Transform the captured variable. | ||||||||
12053 | VarDecl *CapturedVar | ||||||||
12054 | = cast_or_null<VarDecl>(getDerived().TransformDecl(C->getLocation(), | ||||||||
12055 | C->getCapturedVar())); | ||||||||
12056 | if (!CapturedVar || CapturedVar->isInvalidDecl()) { | ||||||||
12057 | Invalid = true; | ||||||||
12058 | continue; | ||||||||
12059 | } | ||||||||
12060 | |||||||||
12061 | // Capture the transformed variable. | ||||||||
12062 | getSema().tryCaptureVariable(CapturedVar, C->getLocation(), Kind, | ||||||||
12063 | EllipsisLoc); | ||||||||
12064 | } | ||||||||
12065 | getSema().finishLambdaExplicitCaptures(LSI); | ||||||||
12066 | |||||||||
12067 | // FIXME: Sema's lambda-building mechanism expects us to push an expression | ||||||||
12068 | // evaluation context even if we're not transforming the function body. | ||||||||
12069 | getSema().PushExpressionEvaluationContext( | ||||||||
12070 | Sema::ExpressionEvaluationContext::PotentiallyEvaluated); | ||||||||
12071 | |||||||||
12072 | // Instantiate the body of the lambda expression. | ||||||||
12073 | StmtResult Body = | ||||||||
12074 | Invalid ? StmtError() : getDerived().TransformLambdaBody(E, E->getBody()); | ||||||||
12075 | |||||||||
12076 | // ActOnLambda* will pop the function scope for us. | ||||||||
12077 | FuncScopeCleanup.disable(); | ||||||||
12078 | |||||||||
12079 | if (Body.isInvalid()) { | ||||||||
12080 | SavedContext.pop(); | ||||||||
12081 | getSema().ActOnLambdaError(E->getBeginLoc(), /*CurScope=*/nullptr, | ||||||||
12082 | /*IsInstantiation=*/true); | ||||||||
12083 | return ExprError(); | ||||||||
12084 | } | ||||||||
12085 | |||||||||
12086 | // Copy the LSI before ActOnFinishFunctionBody removes it. | ||||||||
12087 | // FIXME: This is dumb. Store the lambda information somewhere that outlives | ||||||||
12088 | // the call operator. | ||||||||
12089 | auto LSICopy = *LSI; | ||||||||
12090 | getSema().ActOnFinishFunctionBody(NewCallOperator, Body.get(), | ||||||||
12091 | /*IsInstantiation*/ true); | ||||||||
12092 | SavedContext.pop(); | ||||||||
12093 | |||||||||
12094 | return getSema().BuildLambdaExpr(E->getBeginLoc(), Body.get()->getEndLoc(), | ||||||||
12095 | &LSICopy); | ||||||||
12096 | } | ||||||||
12097 | |||||||||
12098 | template<typename Derived> | ||||||||
12099 | StmtResult | ||||||||
12100 | TreeTransform<Derived>::TransformLambdaBody(LambdaExpr *E, Stmt *S) { | ||||||||
12101 | return TransformStmt(S); | ||||||||
12102 | } | ||||||||
12103 | |||||||||
12104 | template<typename Derived> | ||||||||
12105 | StmtResult | ||||||||
12106 | TreeTransform<Derived>::SkipLambdaBody(LambdaExpr *E, Stmt *S) { | ||||||||
12107 | // Transform captures. | ||||||||
12108 | for (LambdaExpr::capture_iterator C = E->capture_begin(), | ||||||||
12109 | CEnd = E->capture_end(); | ||||||||
12110 | C != CEnd; ++C) { | ||||||||
12111 | // When we hit the first implicit capture, tell Sema that we've finished | ||||||||
12112 | // the list of explicit captures. | ||||||||
12113 | if (!C->isImplicit()) | ||||||||
12114 | continue; | ||||||||
12115 | |||||||||
12116 | // Capturing 'this' is trivial. | ||||||||
12117 | if (C->capturesThis()) { | ||||||||
12118 | getSema().CheckCXXThisCapture(C->getLocation(), C->isExplicit(), | ||||||||
12119 | /*BuildAndDiagnose*/ true, nullptr, | ||||||||
12120 | C->getCaptureKind() == LCK_StarThis); | ||||||||
12121 | continue; | ||||||||
12122 | } | ||||||||
12123 | // Captured expression will be recaptured during captured variables | ||||||||
12124 | // rebuilding. | ||||||||
12125 | if (C->capturesVLAType()) | ||||||||
12126 | continue; | ||||||||
12127 | |||||||||
12128 | assert(C->capturesVariable() && "unexpected kind of lambda capture")((C->capturesVariable() && "unexpected kind of lambda capture" ) ? static_cast<void> (0) : __assert_fail ("C->capturesVariable() && \"unexpected kind of lambda capture\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 12128, __PRETTY_FUNCTION__)); | ||||||||
12129 | assert(!E->isInitCapture(C) && "implicit init-capture?")((!E->isInitCapture(C) && "implicit init-capture?" ) ? static_cast<void> (0) : __assert_fail ("!E->isInitCapture(C) && \"implicit init-capture?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 12129, __PRETTY_FUNCTION__)); | ||||||||
12130 | |||||||||
12131 | // Transform the captured variable. | ||||||||
12132 | VarDecl *CapturedVar = cast_or_null<VarDecl>( | ||||||||
12133 | getDerived().TransformDecl(C->getLocation(), C->getCapturedVar())); | ||||||||
12134 | if (!CapturedVar || CapturedVar->isInvalidDecl()) | ||||||||
12135 | return StmtError(); | ||||||||
12136 | |||||||||
12137 | // Capture the transformed variable. | ||||||||
12138 | getSema().tryCaptureVariable(CapturedVar, C->getLocation()); | ||||||||
12139 | } | ||||||||
12140 | |||||||||
12141 | return S; | ||||||||
12142 | } | ||||||||
12143 | |||||||||
12144 | template<typename Derived> | ||||||||
12145 | ExprResult | ||||||||
12146 | TreeTransform<Derived>::TransformCXXUnresolvedConstructExpr( | ||||||||
12147 | CXXUnresolvedConstructExpr *E) { | ||||||||
12148 | TypeSourceInfo *T = | ||||||||
12149 | getDerived().TransformTypeWithDeducedTST(E->getTypeSourceInfo()); | ||||||||
12150 | if (!T) | ||||||||
12151 | return ExprError(); | ||||||||
12152 | |||||||||
12153 | bool ArgumentChanged = false; | ||||||||
12154 | SmallVector<Expr*, 8> Args; | ||||||||
12155 | Args.reserve(E->arg_size()); | ||||||||
12156 | { | ||||||||
12157 | EnterExpressionEvaluationContext Context( | ||||||||
12158 | getSema(), EnterExpressionEvaluationContext::InitList, | ||||||||
12159 | E->isListInitialization()); | ||||||||
12160 | if (getDerived().TransformExprs(E->arg_begin(), E->arg_size(), true, Args, | ||||||||
12161 | &ArgumentChanged)) | ||||||||
12162 | return ExprError(); | ||||||||
12163 | } | ||||||||
12164 | |||||||||
12165 | if (!getDerived().AlwaysRebuild() && | ||||||||
12166 | T == E->getTypeSourceInfo() && | ||||||||
12167 | !ArgumentChanged) | ||||||||
12168 | return E; | ||||||||
12169 | |||||||||
12170 | // FIXME: we're faking the locations of the commas | ||||||||
12171 | return getDerived().RebuildCXXUnresolvedConstructExpr( | ||||||||
12172 | T, E->getLParenLoc(), Args, E->getRParenLoc(), E->isListInitialization()); | ||||||||
12173 | } | ||||||||
12174 | |||||||||
12175 | template<typename Derived> | ||||||||
12176 | ExprResult | ||||||||
12177 | TreeTransform<Derived>::TransformCXXDependentScopeMemberExpr( | ||||||||
12178 | CXXDependentScopeMemberExpr *E) { | ||||||||
12179 | // Transform the base of the expression. | ||||||||
12180 | ExprResult Base((Expr*) nullptr); | ||||||||
12181 | Expr *OldBase; | ||||||||
12182 | QualType BaseType; | ||||||||
12183 | QualType ObjectType; | ||||||||
12184 | if (!E->isImplicitAccess()) { | ||||||||
12185 | OldBase = E->getBase(); | ||||||||
12186 | Base = getDerived().TransformExpr(OldBase); | ||||||||
12187 | if (Base.isInvalid()) | ||||||||
12188 | return ExprError(); | ||||||||
12189 | |||||||||
12190 | // Start the member reference and compute the object's type. | ||||||||
12191 | ParsedType ObjectTy; | ||||||||
12192 | bool MayBePseudoDestructor = false; | ||||||||
12193 | Base = SemaRef.ActOnStartCXXMemberReference(nullptr, Base.get(), | ||||||||
12194 | E->getOperatorLoc(), | ||||||||
12195 | E->isArrow()? tok::arrow : tok::period, | ||||||||
12196 | ObjectTy, | ||||||||
12197 | MayBePseudoDestructor); | ||||||||
12198 | if (Base.isInvalid()) | ||||||||
12199 | return ExprError(); | ||||||||
12200 | |||||||||
12201 | ObjectType = ObjectTy.get(); | ||||||||
12202 | BaseType = ((Expr*) Base.get())->getType(); | ||||||||
12203 | } else { | ||||||||
12204 | OldBase = nullptr; | ||||||||
12205 | BaseType = getDerived().TransformType(E->getBaseType()); | ||||||||
12206 | ObjectType = BaseType->castAs<PointerType>()->getPointeeType(); | ||||||||
12207 | } | ||||||||
12208 | |||||||||
12209 | // Transform the first part of the nested-name-specifier that qualifies | ||||||||
12210 | // the member name. | ||||||||
12211 | NamedDecl *FirstQualifierInScope | ||||||||
12212 | = getDerived().TransformFirstQualifierInScope( | ||||||||
12213 | E->getFirstQualifierFoundInScope(), | ||||||||
12214 | E->getQualifierLoc().getBeginLoc()); | ||||||||
12215 | |||||||||
12216 | NestedNameSpecifierLoc QualifierLoc; | ||||||||
12217 | if (E->getQualifier()) { | ||||||||
12218 | QualifierLoc | ||||||||
12219 | = getDerived().TransformNestedNameSpecifierLoc(E->getQualifierLoc(), | ||||||||
12220 | ObjectType, | ||||||||
12221 | FirstQualifierInScope); | ||||||||
12222 | if (!QualifierLoc) | ||||||||
12223 | return ExprError(); | ||||||||
12224 | } | ||||||||
12225 | |||||||||
12226 | SourceLocation TemplateKWLoc = E->getTemplateKeywordLoc(); | ||||||||
12227 | |||||||||
12228 | // TODO: If this is a conversion-function-id, verify that the | ||||||||
12229 | // destination type name (if present) resolves the same way after | ||||||||
12230 | // instantiation as it did in the local scope. | ||||||||
12231 | |||||||||
12232 | DeclarationNameInfo NameInfo | ||||||||
12233 | = getDerived().TransformDeclarationNameInfo(E->getMemberNameInfo()); | ||||||||
12234 | if (!NameInfo.getName()) | ||||||||
12235 | return ExprError(); | ||||||||
12236 | |||||||||
12237 | if (!E->hasExplicitTemplateArgs()) { | ||||||||
12238 | // This is a reference to a member without an explicitly-specified | ||||||||
12239 | // template argument list. Optimize for this common case. | ||||||||
12240 | if (!getDerived().AlwaysRebuild() && | ||||||||
12241 | Base.get() == OldBase && | ||||||||
12242 | BaseType == E->getBaseType() && | ||||||||
12243 | QualifierLoc == E->getQualifierLoc() && | ||||||||
12244 | NameInfo.getName() == E->getMember() && | ||||||||
12245 | FirstQualifierInScope == E->getFirstQualifierFoundInScope()) | ||||||||
12246 | return E; | ||||||||
12247 | |||||||||
12248 | return getDerived().RebuildCXXDependentScopeMemberExpr(Base.get(), | ||||||||
12249 | BaseType, | ||||||||
12250 | E->isArrow(), | ||||||||
12251 | E->getOperatorLoc(), | ||||||||
12252 | QualifierLoc, | ||||||||
12253 | TemplateKWLoc, | ||||||||
12254 | FirstQualifierInScope, | ||||||||
12255 | NameInfo, | ||||||||
12256 | /*TemplateArgs*/nullptr); | ||||||||
12257 | } | ||||||||
12258 | |||||||||
12259 | TemplateArgumentListInfo TransArgs(E->getLAngleLoc(), E->getRAngleLoc()); | ||||||||
12260 | if (getDerived().TransformTemplateArguments(E->getTemplateArgs(), | ||||||||
12261 | E->getNumTemplateArgs(), | ||||||||
12262 | TransArgs)) | ||||||||
12263 | return ExprError(); | ||||||||
12264 | |||||||||
12265 | return getDerived().RebuildCXXDependentScopeMemberExpr(Base.get(), | ||||||||
12266 | BaseType, | ||||||||
12267 | E->isArrow(), | ||||||||
12268 | E->getOperatorLoc(), | ||||||||
12269 | QualifierLoc, | ||||||||
12270 | TemplateKWLoc, | ||||||||
12271 | FirstQualifierInScope, | ||||||||
12272 | NameInfo, | ||||||||
12273 | &TransArgs); | ||||||||
12274 | } | ||||||||
12275 | |||||||||
12276 | template<typename Derived> | ||||||||
12277 | ExprResult | ||||||||
12278 | TreeTransform<Derived>::TransformUnresolvedMemberExpr(UnresolvedMemberExpr *Old) { | ||||||||
12279 | // Transform the base of the expression. | ||||||||
12280 | ExprResult Base((Expr*) nullptr); | ||||||||
12281 | QualType BaseType; | ||||||||
12282 | if (!Old->isImplicitAccess()) { | ||||||||
12283 | Base = getDerived().TransformExpr(Old->getBase()); | ||||||||
12284 | if (Base.isInvalid()) | ||||||||
12285 | return ExprError(); | ||||||||
12286 | Base = getSema().PerformMemberExprBaseConversion(Base.get(), | ||||||||
12287 | Old->isArrow()); | ||||||||
12288 | if (Base.isInvalid()) | ||||||||
12289 | return ExprError(); | ||||||||
12290 | BaseType = Base.get()->getType(); | ||||||||
12291 | } else { | ||||||||
12292 | BaseType = getDerived().TransformType(Old->getBaseType()); | ||||||||
12293 | } | ||||||||
12294 | |||||||||
12295 | NestedNameSpecifierLoc QualifierLoc; | ||||||||
12296 | if (Old->getQualifierLoc()) { | ||||||||
12297 | QualifierLoc | ||||||||
12298 | = getDerived().TransformNestedNameSpecifierLoc(Old->getQualifierLoc()); | ||||||||
12299 | if (!QualifierLoc) | ||||||||
12300 | return ExprError(); | ||||||||
12301 | } | ||||||||
12302 | |||||||||
12303 | SourceLocation TemplateKWLoc = Old->getTemplateKeywordLoc(); | ||||||||
12304 | |||||||||
12305 | LookupResult R(SemaRef, Old->getMemberNameInfo(), | ||||||||
12306 | Sema::LookupOrdinaryName); | ||||||||
12307 | |||||||||
12308 | // Transform the declaration set. | ||||||||
12309 | if (TransformOverloadExprDecls(Old, /*RequiresADL*/false, R)) | ||||||||
12310 | return ExprError(); | ||||||||
12311 | |||||||||
12312 | // Determine the naming class. | ||||||||
12313 | if (Old->getNamingClass()) { | ||||||||
12314 | CXXRecordDecl *NamingClass | ||||||||
12315 | = cast_or_null<CXXRecordDecl>(getDerived().TransformDecl( | ||||||||
12316 | Old->getMemberLoc(), | ||||||||
12317 | Old->getNamingClass())); | ||||||||
12318 | if (!NamingClass) | ||||||||
12319 | return ExprError(); | ||||||||
12320 | |||||||||
12321 | R.setNamingClass(NamingClass); | ||||||||
12322 | } | ||||||||
12323 | |||||||||
12324 | TemplateArgumentListInfo TransArgs; | ||||||||
12325 | if (Old->hasExplicitTemplateArgs()) { | ||||||||
12326 | TransArgs.setLAngleLoc(Old->getLAngleLoc()); | ||||||||
12327 | TransArgs.setRAngleLoc(Old->getRAngleLoc()); | ||||||||
12328 | if (getDerived().TransformTemplateArguments(Old->getTemplateArgs(), | ||||||||
12329 | Old->getNumTemplateArgs(), | ||||||||
12330 | TransArgs)) | ||||||||
12331 | return ExprError(); | ||||||||
12332 | } | ||||||||
12333 | |||||||||
12334 | // FIXME: to do this check properly, we will need to preserve the | ||||||||
12335 | // first-qualifier-in-scope here, just in case we had a dependent | ||||||||
12336 | // base (and therefore couldn't do the check) and a | ||||||||
12337 | // nested-name-qualifier (and therefore could do the lookup). | ||||||||
12338 | NamedDecl *FirstQualifierInScope = nullptr; | ||||||||
12339 | |||||||||
12340 | return getDerived().RebuildUnresolvedMemberExpr(Base.get(), | ||||||||
12341 | BaseType, | ||||||||
12342 | Old->getOperatorLoc(), | ||||||||
12343 | Old->isArrow(), | ||||||||
12344 | QualifierLoc, | ||||||||
12345 | TemplateKWLoc, | ||||||||
12346 | FirstQualifierInScope, | ||||||||
12347 | R, | ||||||||
12348 | (Old->hasExplicitTemplateArgs() | ||||||||
12349 | ? &TransArgs : nullptr)); | ||||||||
12350 | } | ||||||||
12351 | |||||||||
12352 | template<typename Derived> | ||||||||
12353 | ExprResult | ||||||||
12354 | TreeTransform<Derived>::TransformCXXNoexceptExpr(CXXNoexceptExpr *E) { | ||||||||
12355 | EnterExpressionEvaluationContext Unevaluated( | ||||||||
12356 | SemaRef, Sema::ExpressionEvaluationContext::Unevaluated); | ||||||||
12357 | ExprResult SubExpr = getDerived().TransformExpr(E->getOperand()); | ||||||||
12358 | if (SubExpr.isInvalid()) | ||||||||
12359 | return ExprError(); | ||||||||
12360 | |||||||||
12361 | if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getOperand()) | ||||||||
12362 | return E; | ||||||||
12363 | |||||||||
12364 | return getDerived().RebuildCXXNoexceptExpr(E->getSourceRange(),SubExpr.get()); | ||||||||
12365 | } | ||||||||
12366 | |||||||||
12367 | template<typename Derived> | ||||||||
12368 | ExprResult | ||||||||
12369 | TreeTransform<Derived>::TransformPackExpansionExpr(PackExpansionExpr *E) { | ||||||||
12370 | ExprResult Pattern = getDerived().TransformExpr(E->getPattern()); | ||||||||
12371 | if (Pattern.isInvalid()) | ||||||||
12372 | return ExprError(); | ||||||||
12373 | |||||||||
12374 | if (!getDerived().AlwaysRebuild() && Pattern.get() == E->getPattern()) | ||||||||
12375 | return E; | ||||||||
12376 | |||||||||
12377 | return getDerived().RebuildPackExpansion(Pattern.get(), E->getEllipsisLoc(), | ||||||||
12378 | E->getNumExpansions()); | ||||||||
12379 | } | ||||||||
12380 | |||||||||
12381 | template<typename Derived> | ||||||||
12382 | ExprResult | ||||||||
12383 | TreeTransform<Derived>::TransformSizeOfPackExpr(SizeOfPackExpr *E) { | ||||||||
12384 | // If E is not value-dependent, then nothing will change when we transform it. | ||||||||
12385 | // Note: This is an instantiation-centric view. | ||||||||
12386 | if (!E->isValueDependent()) | ||||||||
12387 | return E; | ||||||||
12388 | |||||||||
12389 | EnterExpressionEvaluationContext Unevaluated( | ||||||||
12390 | getSema(), Sema::ExpressionEvaluationContext::Unevaluated); | ||||||||
12391 | |||||||||
12392 | ArrayRef<TemplateArgument> PackArgs; | ||||||||
12393 | TemplateArgument ArgStorage; | ||||||||
12394 | |||||||||
12395 | // Find the argument list to transform. | ||||||||
12396 | if (E->isPartiallySubstituted()) { | ||||||||
12397 | PackArgs = E->getPartialArguments(); | ||||||||
12398 | } else if (E->isValueDependent()) { | ||||||||
12399 | UnexpandedParameterPack Unexpanded(E->getPack(), E->getPackLoc()); | ||||||||
12400 | bool ShouldExpand = false; | ||||||||
12401 | bool RetainExpansion = false; | ||||||||
12402 | Optional<unsigned> NumExpansions; | ||||||||
12403 | if (getDerived().TryExpandParameterPacks(E->getOperatorLoc(), E->getPackLoc(), | ||||||||
12404 | Unexpanded, | ||||||||
12405 | ShouldExpand, RetainExpansion, | ||||||||
12406 | NumExpansions)) | ||||||||
12407 | return ExprError(); | ||||||||
12408 | |||||||||
12409 | // If we need to expand the pack, build a template argument from it and | ||||||||
12410 | // expand that. | ||||||||
12411 | if (ShouldExpand) { | ||||||||
12412 | auto *Pack = E->getPack(); | ||||||||
12413 | if (auto *TTPD = dyn_cast<TemplateTypeParmDecl>(Pack)) { | ||||||||
12414 | ArgStorage = getSema().Context.getPackExpansionType( | ||||||||
12415 | getSema().Context.getTypeDeclType(TTPD), None); | ||||||||
12416 | } else if (auto *TTPD = dyn_cast<TemplateTemplateParmDecl>(Pack)) { | ||||||||
12417 | ArgStorage = TemplateArgument(TemplateName(TTPD), None); | ||||||||
12418 | } else { | ||||||||
12419 | auto *VD = cast<ValueDecl>(Pack); | ||||||||
12420 | ExprResult DRE = getSema().BuildDeclRefExpr( | ||||||||
12421 | VD, VD->getType().getNonLValueExprType(getSema().Context), | ||||||||
12422 | VD->getType()->isReferenceType() ? VK_LValue : VK_RValue, | ||||||||
12423 | E->getPackLoc()); | ||||||||
12424 | if (DRE.isInvalid()) | ||||||||
12425 | return ExprError(); | ||||||||
12426 | ArgStorage = new (getSema().Context) PackExpansionExpr( | ||||||||
12427 | getSema().Context.DependentTy, DRE.get(), E->getPackLoc(), None); | ||||||||
12428 | } | ||||||||
12429 | PackArgs = ArgStorage; | ||||||||
12430 | } | ||||||||
12431 | } | ||||||||
12432 | |||||||||
12433 | // If we're not expanding the pack, just transform the decl. | ||||||||
12434 | if (!PackArgs.size()) { | ||||||||
12435 | auto *Pack = cast_or_null<NamedDecl>( | ||||||||
12436 | getDerived().TransformDecl(E->getPackLoc(), E->getPack())); | ||||||||
12437 | if (!Pack) | ||||||||
12438 | return ExprError(); | ||||||||
12439 | return getDerived().RebuildSizeOfPackExpr(E->getOperatorLoc(), Pack, | ||||||||
12440 | E->getPackLoc(), | ||||||||
12441 | E->getRParenLoc(), None, None); | ||||||||
12442 | } | ||||||||
12443 | |||||||||
12444 | // Try to compute the result without performing a partial substitution. | ||||||||
12445 | Optional<unsigned> Result = 0; | ||||||||
12446 | for (const TemplateArgument &Arg : PackArgs) { | ||||||||
12447 | if (!Arg.isPackExpansion()) { | ||||||||
12448 | Result = *Result + 1; | ||||||||
12449 | continue; | ||||||||
12450 | } | ||||||||
12451 | |||||||||
12452 | TemplateArgumentLoc ArgLoc; | ||||||||
12453 | InventTemplateArgumentLoc(Arg, ArgLoc); | ||||||||
12454 | |||||||||
12455 | // Find the pattern of the pack expansion. | ||||||||
12456 | SourceLocation Ellipsis; | ||||||||
12457 | Optional<unsigned> OrigNumExpansions; | ||||||||
12458 | TemplateArgumentLoc Pattern = | ||||||||
12459 | getSema().getTemplateArgumentPackExpansionPattern(ArgLoc, Ellipsis, | ||||||||
12460 | OrigNumExpansions); | ||||||||
12461 | |||||||||
12462 | // Substitute under the pack expansion. Do not expand the pack (yet). | ||||||||
12463 | TemplateArgumentLoc OutPattern; | ||||||||
12464 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1); | ||||||||
12465 | if (getDerived().TransformTemplateArgument(Pattern, OutPattern, | ||||||||
12466 | /*Uneval*/ true)) | ||||||||
12467 | return true; | ||||||||
12468 | |||||||||
12469 | // See if we can determine the number of arguments from the result. | ||||||||
12470 | Optional<unsigned> NumExpansions = | ||||||||
12471 | getSema().getFullyPackExpandedSize(OutPattern.getArgument()); | ||||||||
12472 | if (!NumExpansions) { | ||||||||
12473 | // No: we must be in an alias template expansion, and we're going to need | ||||||||
12474 | // to actually expand the packs. | ||||||||
12475 | Result = None; | ||||||||
12476 | break; | ||||||||
12477 | } | ||||||||
12478 | |||||||||
12479 | Result = *Result + *NumExpansions; | ||||||||
12480 | } | ||||||||
12481 | |||||||||
12482 | // Common case: we could determine the number of expansions without | ||||||||
12483 | // substituting. | ||||||||
12484 | if (Result) | ||||||||
12485 | return getDerived().RebuildSizeOfPackExpr(E->getOperatorLoc(), E->getPack(), | ||||||||
12486 | E->getPackLoc(), | ||||||||
12487 | E->getRParenLoc(), *Result, None); | ||||||||
12488 | |||||||||
12489 | TemplateArgumentListInfo TransformedPackArgs(E->getPackLoc(), | ||||||||
12490 | E->getPackLoc()); | ||||||||
12491 | { | ||||||||
12492 | TemporaryBase Rebase(*this, E->getPackLoc(), getBaseEntity()); | ||||||||
12493 | typedef TemplateArgumentLocInventIterator< | ||||||||
12494 | Derived, const TemplateArgument*> PackLocIterator; | ||||||||
12495 | if (TransformTemplateArguments(PackLocIterator(*this, PackArgs.begin()), | ||||||||
12496 | PackLocIterator(*this, PackArgs.end()), | ||||||||
12497 | TransformedPackArgs, /*Uneval*/true)) | ||||||||
12498 | return ExprError(); | ||||||||
12499 | } | ||||||||
12500 | |||||||||
12501 | // Check whether we managed to fully-expand the pack. | ||||||||
12502 | // FIXME: Is it possible for us to do so and not hit the early exit path? | ||||||||
12503 | SmallVector<TemplateArgument, 8> Args; | ||||||||
12504 | bool PartialSubstitution = false; | ||||||||
12505 | for (auto &Loc : TransformedPackArgs.arguments()) { | ||||||||
12506 | Args.push_back(Loc.getArgument()); | ||||||||
12507 | if (Loc.getArgument().isPackExpansion()) | ||||||||
12508 | PartialSubstitution = true; | ||||||||
12509 | } | ||||||||
12510 | |||||||||
12511 | if (PartialSubstitution) | ||||||||
12512 | return getDerived().RebuildSizeOfPackExpr(E->getOperatorLoc(), E->getPack(), | ||||||||
12513 | E->getPackLoc(), | ||||||||
12514 | E->getRParenLoc(), None, Args); | ||||||||
12515 | |||||||||
12516 | return getDerived().RebuildSizeOfPackExpr(E->getOperatorLoc(), E->getPack(), | ||||||||
12517 | E->getPackLoc(), E->getRParenLoc(), | ||||||||
12518 | Args.size(), None); | ||||||||
12519 | } | ||||||||
12520 | |||||||||
12521 | template<typename Derived> | ||||||||
12522 | ExprResult | ||||||||
12523 | TreeTransform<Derived>::TransformSubstNonTypeTemplateParmPackExpr( | ||||||||
12524 | SubstNonTypeTemplateParmPackExpr *E) { | ||||||||
12525 | // Default behavior is to do nothing with this transformation. | ||||||||
12526 | return E; | ||||||||
12527 | } | ||||||||
12528 | |||||||||
12529 | template<typename Derived> | ||||||||
12530 | ExprResult | ||||||||
12531 | TreeTransform<Derived>::TransformSubstNonTypeTemplateParmExpr( | ||||||||
12532 | SubstNonTypeTemplateParmExpr *E) { | ||||||||
12533 | // Default behavior is to do nothing with this transformation. | ||||||||
12534 | return E; | ||||||||
12535 | } | ||||||||
12536 | |||||||||
12537 | template<typename Derived> | ||||||||
12538 | ExprResult | ||||||||
12539 | TreeTransform<Derived>::TransformFunctionParmPackExpr(FunctionParmPackExpr *E) { | ||||||||
12540 | // Default behavior is to do nothing with this transformation. | ||||||||
12541 | return E; | ||||||||
12542 | } | ||||||||
12543 | |||||||||
12544 | template<typename Derived> | ||||||||
12545 | ExprResult | ||||||||
12546 | TreeTransform<Derived>::TransformMaterializeTemporaryExpr( | ||||||||
12547 | MaterializeTemporaryExpr *E) { | ||||||||
12548 | return getDerived().TransformExpr(E->getSubExpr()); | ||||||||
12549 | } | ||||||||
12550 | |||||||||
12551 | template<typename Derived> | ||||||||
12552 | ExprResult | ||||||||
12553 | TreeTransform<Derived>::TransformCXXFoldExpr(CXXFoldExpr *E) { | ||||||||
12554 | Expr *Pattern = E->getPattern(); | ||||||||
12555 | |||||||||
12556 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | ||||||||
12557 | getSema().collectUnexpandedParameterPacks(Pattern, Unexpanded); | ||||||||
12558 | assert(!Unexpanded.empty() && "Pack expansion without parameter packs?")((!Unexpanded.empty() && "Pack expansion without parameter packs?" ) ? static_cast<void> (0) : __assert_fail ("!Unexpanded.empty() && \"Pack expansion without parameter packs?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 12558, __PRETTY_FUNCTION__)); | ||||||||
12559 | |||||||||
12560 | // Determine whether the set of unexpanded parameter packs can and should | ||||||||
12561 | // be expanded. | ||||||||
12562 | bool Expand = true; | ||||||||
12563 | bool RetainExpansion = false; | ||||||||
12564 | Optional<unsigned> OrigNumExpansions = E->getNumExpansions(), | ||||||||
12565 | NumExpansions = OrigNumExpansions; | ||||||||
12566 | if (getDerived().TryExpandParameterPacks(E->getEllipsisLoc(), | ||||||||
12567 | Pattern->getSourceRange(), | ||||||||
12568 | Unexpanded, | ||||||||
12569 | Expand, RetainExpansion, | ||||||||
12570 | NumExpansions)) | ||||||||
12571 | return true; | ||||||||
12572 | |||||||||
12573 | if (!Expand) { | ||||||||
12574 | // Do not expand any packs here, just transform and rebuild a fold | ||||||||
12575 | // expression. | ||||||||
12576 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1); | ||||||||
12577 | |||||||||
12578 | ExprResult LHS = | ||||||||
12579 | E->getLHS() ? getDerived().TransformExpr(E->getLHS()) : ExprResult(); | ||||||||
12580 | if (LHS.isInvalid()) | ||||||||
12581 | return true; | ||||||||
12582 | |||||||||
12583 | ExprResult RHS = | ||||||||
12584 | E->getRHS() ? getDerived().TransformExpr(E->getRHS()) : ExprResult(); | ||||||||
12585 | if (RHS.isInvalid()) | ||||||||
12586 | return true; | ||||||||
12587 | |||||||||
12588 | if (!getDerived().AlwaysRebuild() && | ||||||||
12589 | LHS.get() == E->getLHS() && RHS.get() == E->getRHS()) | ||||||||
12590 | return E; | ||||||||
12591 | |||||||||
12592 | return getDerived().RebuildCXXFoldExpr( | ||||||||
12593 | E->getBeginLoc(), LHS.get(), E->getOperator(), E->getEllipsisLoc(), | ||||||||
12594 | RHS.get(), E->getEndLoc(), NumExpansions); | ||||||||
12595 | } | ||||||||
12596 | |||||||||
12597 | // The transform has determined that we should perform an elementwise | ||||||||
12598 | // expansion of the pattern. Do so. | ||||||||
12599 | ExprResult Result = getDerived().TransformExpr(E->getInit()); | ||||||||
12600 | if (Result.isInvalid()) | ||||||||
12601 | return true; | ||||||||
12602 | bool LeftFold = E->isLeftFold(); | ||||||||
12603 | |||||||||
12604 | // If we're retaining an expansion for a right fold, it is the innermost | ||||||||
12605 | // component and takes the init (if any). | ||||||||
12606 | if (!LeftFold && RetainExpansion) { | ||||||||
12607 | ForgetPartiallySubstitutedPackRAII Forget(getDerived()); | ||||||||
12608 | |||||||||
12609 | ExprResult Out = getDerived().TransformExpr(Pattern); | ||||||||
12610 | if (Out.isInvalid()) | ||||||||
12611 | return true; | ||||||||
12612 | |||||||||
12613 | Result = getDerived().RebuildCXXFoldExpr( | ||||||||
12614 | E->getBeginLoc(), Out.get(), E->getOperator(), E->getEllipsisLoc(), | ||||||||
12615 | Result.get(), E->getEndLoc(), OrigNumExpansions); | ||||||||
12616 | if (Result.isInvalid()) | ||||||||
12617 | return true; | ||||||||
12618 | } | ||||||||
12619 | |||||||||
12620 | for (unsigned I = 0; I != *NumExpansions; ++I) { | ||||||||
12621 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex( | ||||||||
12622 | getSema(), LeftFold ? I : *NumExpansions - I - 1); | ||||||||
12623 | ExprResult Out = getDerived().TransformExpr(Pattern); | ||||||||
12624 | if (Out.isInvalid()) | ||||||||
12625 | return true; | ||||||||
12626 | |||||||||
12627 | if (Out.get()->containsUnexpandedParameterPack()) { | ||||||||
12628 | // We still have a pack; retain a pack expansion for this slice. | ||||||||
12629 | Result = getDerived().RebuildCXXFoldExpr( | ||||||||
12630 | E->getBeginLoc(), LeftFold ? Result.get() : Out.get(), | ||||||||
12631 | E->getOperator(), E->getEllipsisLoc(), | ||||||||
12632 | LeftFold ? Out.get() : Result.get(), E->getEndLoc(), | ||||||||
12633 | OrigNumExpansions); | ||||||||
12634 | } else if (Result.isUsable()) { | ||||||||
12635 | // We've got down to a single element; build a binary operator. | ||||||||
12636 | Result = getDerived().RebuildBinaryOperator( | ||||||||
12637 | E->getEllipsisLoc(), E->getOperator(), | ||||||||
12638 | LeftFold ? Result.get() : Out.get(), | ||||||||
12639 | LeftFold ? Out.get() : Result.get()); | ||||||||
12640 | } else | ||||||||
12641 | Result = Out; | ||||||||
12642 | |||||||||
12643 | if (Result.isInvalid()) | ||||||||
12644 | return true; | ||||||||
12645 | } | ||||||||
12646 | |||||||||
12647 | // If we're retaining an expansion for a left fold, it is the outermost | ||||||||
12648 | // component and takes the complete expansion so far as its init (if any). | ||||||||
12649 | if (LeftFold && RetainExpansion) { | ||||||||
12650 | ForgetPartiallySubstitutedPackRAII Forget(getDerived()); | ||||||||
12651 | |||||||||
12652 | ExprResult Out = getDerived().TransformExpr(Pattern); | ||||||||
12653 | if (Out.isInvalid()) | ||||||||
12654 | return true; | ||||||||
12655 | |||||||||
12656 | Result = getDerived().RebuildCXXFoldExpr( | ||||||||
12657 | E->getBeginLoc(), Result.get(), E->getOperator(), E->getEllipsisLoc(), | ||||||||
12658 | Out.get(), E->getEndLoc(), OrigNumExpansions); | ||||||||
12659 | if (Result.isInvalid()) | ||||||||
12660 | return true; | ||||||||
12661 | } | ||||||||
12662 | |||||||||
12663 | // If we had no init and an empty pack, and we're not retaining an expansion, | ||||||||
12664 | // then produce a fallback value or error. | ||||||||
12665 | if (Result.isUnset()) | ||||||||
12666 | return getDerived().RebuildEmptyCXXFoldExpr(E->getEllipsisLoc(), | ||||||||
12667 | E->getOperator()); | ||||||||
12668 | |||||||||
12669 | return Result; | ||||||||
12670 | } | ||||||||
12671 | |||||||||
12672 | template<typename Derived> | ||||||||
12673 | ExprResult | ||||||||
12674 | TreeTransform<Derived>::TransformCXXStdInitializerListExpr( | ||||||||
12675 | CXXStdInitializerListExpr *E) { | ||||||||
12676 | return getDerived().TransformExpr(E->getSubExpr()); | ||||||||
12677 | } | ||||||||
12678 | |||||||||
12679 | template<typename Derived> | ||||||||
12680 | ExprResult | ||||||||
12681 | TreeTransform<Derived>::TransformObjCStringLiteral(ObjCStringLiteral *E) { | ||||||||
12682 | return SemaRef.MaybeBindToTemporary(E); | ||||||||
12683 | } | ||||||||
12684 | |||||||||
12685 | template<typename Derived> | ||||||||
12686 | ExprResult | ||||||||
12687 | TreeTransform<Derived>::TransformObjCBoolLiteralExpr(ObjCBoolLiteralExpr *E) { | ||||||||
12688 | return E; | ||||||||
12689 | } | ||||||||
12690 | |||||||||
12691 | template<typename Derived> | ||||||||
12692 | ExprResult | ||||||||
12693 | TreeTransform<Derived>::TransformObjCBoxedExpr(ObjCBoxedExpr *E) { | ||||||||
12694 | ExprResult SubExpr = getDerived().TransformExpr(E->getSubExpr()); | ||||||||
12695 | if (SubExpr.isInvalid()) | ||||||||
12696 | return ExprError(); | ||||||||
12697 | |||||||||
12698 | if (!getDerived().AlwaysRebuild() && | ||||||||
12699 | SubExpr.get() == E->getSubExpr()) | ||||||||
12700 | return E; | ||||||||
12701 | |||||||||
12702 | return getDerived().RebuildObjCBoxedExpr(E->getSourceRange(), SubExpr.get()); | ||||||||
12703 | } | ||||||||
12704 | |||||||||
12705 | template<typename Derived> | ||||||||
12706 | ExprResult | ||||||||
12707 | TreeTransform<Derived>::TransformObjCArrayLiteral(ObjCArrayLiteral *E) { | ||||||||
12708 | // Transform each of the elements. | ||||||||
12709 | SmallVector<Expr *, 8> Elements; | ||||||||
12710 | bool ArgChanged = false; | ||||||||
12711 | if (getDerived().TransformExprs(E->getElements(), E->getNumElements(), | ||||||||
12712 | /*IsCall=*/false, Elements, &ArgChanged)) | ||||||||
12713 | return ExprError(); | ||||||||
12714 | |||||||||
12715 | if (!getDerived().AlwaysRebuild() && !ArgChanged) | ||||||||
12716 | return SemaRef.MaybeBindToTemporary(E); | ||||||||
12717 | |||||||||
12718 | return getDerived().RebuildObjCArrayLiteral(E->getSourceRange(), | ||||||||
12719 | Elements.data(), | ||||||||
12720 | Elements.size()); | ||||||||
12721 | } | ||||||||
12722 | |||||||||
12723 | template<typename Derived> | ||||||||
12724 | ExprResult | ||||||||
12725 | TreeTransform<Derived>::TransformObjCDictionaryLiteral( | ||||||||
12726 | ObjCDictionaryLiteral *E) { | ||||||||
12727 | // Transform each of the elements. | ||||||||
12728 | SmallVector<ObjCDictionaryElement, 8> Elements; | ||||||||
12729 | bool ArgChanged = false; | ||||||||
12730 | for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { | ||||||||
12731 | ObjCDictionaryElement OrigElement = E->getKeyValueElement(I); | ||||||||
12732 | |||||||||
12733 | if (OrigElement.isPackExpansion()) { | ||||||||
12734 | // This key/value element is a pack expansion. | ||||||||
12735 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | ||||||||
12736 | getSema().collectUnexpandedParameterPacks(OrigElement.Key, Unexpanded); | ||||||||
12737 | getSema().collectUnexpandedParameterPacks(OrigElement.Value, Unexpanded); | ||||||||
12738 | assert(!Unexpanded.empty() && "Pack expansion without parameter packs?")((!Unexpanded.empty() && "Pack expansion without parameter packs?" ) ? static_cast<void> (0) : __assert_fail ("!Unexpanded.empty() && \"Pack expansion without parameter packs?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 12738, __PRETTY_FUNCTION__)); | ||||||||
12739 | |||||||||
12740 | // Determine whether the set of unexpanded parameter packs can | ||||||||
12741 | // and should be expanded. | ||||||||
12742 | bool Expand = true; | ||||||||
12743 | bool RetainExpansion = false; | ||||||||
12744 | Optional<unsigned> OrigNumExpansions = OrigElement.NumExpansions; | ||||||||
12745 | Optional<unsigned> NumExpansions = OrigNumExpansions; | ||||||||
12746 | SourceRange PatternRange(OrigElement.Key->getBeginLoc(), | ||||||||
12747 | OrigElement.Value->getEndLoc()); | ||||||||
12748 | if (getDerived().TryExpandParameterPacks(OrigElement.EllipsisLoc, | ||||||||
12749 | PatternRange, Unexpanded, Expand, | ||||||||
12750 | RetainExpansion, NumExpansions)) | ||||||||
12751 | return ExprError(); | ||||||||
12752 | |||||||||
12753 | if (!Expand) { | ||||||||
12754 | // The transform has determined that we should perform a simple | ||||||||
12755 | // transformation on the pack expansion, producing another pack | ||||||||
12756 | // expansion. | ||||||||
12757 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1); | ||||||||
12758 | ExprResult Key = getDerived().TransformExpr(OrigElement.Key); | ||||||||
12759 | if (Key.isInvalid()) | ||||||||
12760 | return ExprError(); | ||||||||
12761 | |||||||||
12762 | if (Key.get() != OrigElement.Key) | ||||||||
12763 | ArgChanged = true; | ||||||||
12764 | |||||||||
12765 | ExprResult Value = getDerived().TransformExpr(OrigElement.Value); | ||||||||
12766 | if (Value.isInvalid()) | ||||||||
12767 | return ExprError(); | ||||||||
12768 | |||||||||
12769 | if (Value.get() != OrigElement.Value) | ||||||||
12770 | ArgChanged = true; | ||||||||
12771 | |||||||||
12772 | ObjCDictionaryElement Expansion = { | ||||||||
12773 | Key.get(), Value.get(), OrigElement.EllipsisLoc, NumExpansions | ||||||||
12774 | }; | ||||||||
12775 | Elements.push_back(Expansion); | ||||||||
12776 | continue; | ||||||||
12777 | } | ||||||||
12778 | |||||||||
12779 | // Record right away that the argument was changed. This needs | ||||||||
12780 | // to happen even if the array expands to nothing. | ||||||||
12781 | ArgChanged = true; | ||||||||
12782 | |||||||||
12783 | // The transform has determined that we should perform an elementwise | ||||||||
12784 | // expansion of the pattern. Do so. | ||||||||
12785 | for (unsigned I = 0; I != *NumExpansions; ++I) { | ||||||||
12786 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I); | ||||||||
12787 | ExprResult Key = getDerived().TransformExpr(OrigElement.Key); | ||||||||
12788 | if (Key.isInvalid()) | ||||||||
12789 | return ExprError(); | ||||||||
12790 | |||||||||
12791 | ExprResult Value = getDerived().TransformExpr(OrigElement.Value); | ||||||||
12792 | if (Value.isInvalid()) | ||||||||
12793 | return ExprError(); | ||||||||
12794 | |||||||||
12795 | ObjCDictionaryElement Element = { | ||||||||
12796 | Key.get(), Value.get(), SourceLocation(), NumExpansions | ||||||||
12797 | }; | ||||||||
12798 | |||||||||
12799 | // If any unexpanded parameter packs remain, we still have a | ||||||||
12800 | // pack expansion. | ||||||||
12801 | // FIXME: Can this really happen? | ||||||||
12802 | if (Key.get()->containsUnexpandedParameterPack() || | ||||||||
12803 | Value.get()->containsUnexpandedParameterPack()) | ||||||||
12804 | Element.EllipsisLoc = OrigElement.EllipsisLoc; | ||||||||
12805 | |||||||||
12806 | Elements.push_back(Element); | ||||||||
12807 | } | ||||||||
12808 | |||||||||
12809 | // FIXME: Retain a pack expansion if RetainExpansion is true. | ||||||||
12810 | |||||||||
12811 | // We've finished with this pack expansion. | ||||||||
12812 | continue; | ||||||||
12813 | } | ||||||||
12814 | |||||||||
12815 | // Transform and check key. | ||||||||
12816 | ExprResult Key = getDerived().TransformExpr(OrigElement.Key); | ||||||||
12817 | if (Key.isInvalid()) | ||||||||
12818 | return ExprError(); | ||||||||
12819 | |||||||||
12820 | if (Key.get() != OrigElement.Key) | ||||||||
12821 | ArgChanged = true; | ||||||||
12822 | |||||||||
12823 | // Transform and check value. | ||||||||
12824 | ExprResult Value | ||||||||
12825 | = getDerived().TransformExpr(OrigElement.Value); | ||||||||
12826 | if (Value.isInvalid()) | ||||||||
12827 | return ExprError(); | ||||||||
12828 | |||||||||
12829 | if (Value.get() != OrigElement.Value) | ||||||||
12830 | ArgChanged = true; | ||||||||
12831 | |||||||||
12832 | ObjCDictionaryElement Element = { | ||||||||
12833 | Key.get(), Value.get(), SourceLocation(), None | ||||||||
12834 | }; | ||||||||
12835 | Elements.push_back(Element); | ||||||||
12836 | } | ||||||||
12837 | |||||||||
12838 | if (!getDerived().AlwaysRebuild() && !ArgChanged) | ||||||||
12839 | return SemaRef.MaybeBindToTemporary(E); | ||||||||
12840 | |||||||||
12841 | return getDerived().RebuildObjCDictionaryLiteral(E->getSourceRange(), | ||||||||
12842 | Elements); | ||||||||
12843 | } | ||||||||
12844 | |||||||||
12845 | template<typename Derived> | ||||||||
12846 | ExprResult | ||||||||
12847 | TreeTransform<Derived>::TransformObjCEncodeExpr(ObjCEncodeExpr *E) { | ||||||||
12848 | TypeSourceInfo *EncodedTypeInfo | ||||||||
12849 | = getDerived().TransformType(E->getEncodedTypeSourceInfo()); | ||||||||
12850 | if (!EncodedTypeInfo) | ||||||||
12851 | return ExprError(); | ||||||||
12852 | |||||||||
12853 | if (!getDerived().AlwaysRebuild() && | ||||||||
12854 | EncodedTypeInfo == E->getEncodedTypeSourceInfo()) | ||||||||
12855 | return E; | ||||||||
12856 | |||||||||
12857 | return getDerived().RebuildObjCEncodeExpr(E->getAtLoc(), | ||||||||
12858 | EncodedTypeInfo, | ||||||||
12859 | E->getRParenLoc()); | ||||||||
12860 | } | ||||||||
12861 | |||||||||
12862 | template<typename Derived> | ||||||||
12863 | ExprResult TreeTransform<Derived>:: | ||||||||
12864 | TransformObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) { | ||||||||
12865 | // This is a kind of implicit conversion, and it needs to get dropped | ||||||||
12866 | // and recomputed for the same general reasons that ImplicitCastExprs | ||||||||
12867 | // do, as well a more specific one: this expression is only valid when | ||||||||
12868 | // it appears *immediately* as an argument expression. | ||||||||
12869 | return getDerived().TransformExpr(E->getSubExpr()); | ||||||||
12870 | } | ||||||||
12871 | |||||||||
12872 | template<typename Derived> | ||||||||
12873 | ExprResult TreeTransform<Derived>:: | ||||||||
12874 | TransformObjCBridgedCastExpr(ObjCBridgedCastExpr *E) { | ||||||||
12875 | TypeSourceInfo *TSInfo | ||||||||
12876 | = getDerived().TransformType(E->getTypeInfoAsWritten()); | ||||||||
12877 | if (!TSInfo) | ||||||||
12878 | return ExprError(); | ||||||||
12879 | |||||||||
12880 | ExprResult Result = getDerived().TransformExpr(E->getSubExpr()); | ||||||||
12881 | if (Result.isInvalid()) | ||||||||
12882 | return ExprError(); | ||||||||
12883 | |||||||||
12884 | if (!getDerived().AlwaysRebuild() && | ||||||||
12885 | TSInfo == E->getTypeInfoAsWritten() && | ||||||||
12886 | Result.get() == E->getSubExpr()) | ||||||||
12887 | return E; | ||||||||
12888 | |||||||||
12889 | return SemaRef.BuildObjCBridgedCast(E->getLParenLoc(), E->getBridgeKind(), | ||||||||
12890 | E->getBridgeKeywordLoc(), TSInfo, | ||||||||
12891 | Result.get()); | ||||||||
12892 | } | ||||||||
12893 | |||||||||
12894 | template <typename Derived> | ||||||||
12895 | ExprResult TreeTransform<Derived>::TransformObjCAvailabilityCheckExpr( | ||||||||
12896 | ObjCAvailabilityCheckExpr *E) { | ||||||||
12897 | return E; | ||||||||
12898 | } | ||||||||
12899 | |||||||||
12900 | template<typename Derived> | ||||||||
12901 | ExprResult | ||||||||
12902 | TreeTransform<Derived>::TransformObjCMessageExpr(ObjCMessageExpr *E) { | ||||||||
12903 | // Transform arguments. | ||||||||
12904 | bool ArgChanged = false; | ||||||||
12905 | SmallVector<Expr*, 8> Args; | ||||||||
12906 | Args.reserve(E->getNumArgs()); | ||||||||
12907 | if (getDerived().TransformExprs(E->getArgs(), E->getNumArgs(), false, Args, | ||||||||
12908 | &ArgChanged)) | ||||||||
12909 | return ExprError(); | ||||||||
12910 | |||||||||
12911 | if (E->getReceiverKind() == ObjCMessageExpr::Class) { | ||||||||
12912 | // Class message: transform the receiver type. | ||||||||
12913 | TypeSourceInfo *ReceiverTypeInfo | ||||||||
12914 | = getDerived().TransformType(E->getClassReceiverTypeInfo()); | ||||||||
12915 | if (!ReceiverTypeInfo) | ||||||||
12916 | return ExprError(); | ||||||||
12917 | |||||||||
12918 | // If nothing changed, just retain the existing message send. | ||||||||
12919 | if (!getDerived().AlwaysRebuild() && | ||||||||
12920 | ReceiverTypeInfo == E->getClassReceiverTypeInfo() && !ArgChanged) | ||||||||
12921 | return SemaRef.MaybeBindToTemporary(E); | ||||||||
12922 | |||||||||
12923 | // Build a new class message send. | ||||||||
12924 | SmallVector<SourceLocation, 16> SelLocs; | ||||||||
12925 | E->getSelectorLocs(SelLocs); | ||||||||
12926 | return getDerived().RebuildObjCMessageExpr(ReceiverTypeInfo, | ||||||||
12927 | E->getSelector(), | ||||||||
12928 | SelLocs, | ||||||||
12929 | E->getMethodDecl(), | ||||||||
12930 | E->getLeftLoc(), | ||||||||
12931 | Args, | ||||||||
12932 | E->getRightLoc()); | ||||||||
12933 | } | ||||||||
12934 | else if (E->getReceiverKind() == ObjCMessageExpr::SuperClass || | ||||||||
12935 | E->getReceiverKind() == ObjCMessageExpr::SuperInstance) { | ||||||||
12936 | if (!E->getMethodDecl()) | ||||||||
12937 | return ExprError(); | ||||||||
12938 | |||||||||
12939 | // Build a new class message send to 'super'. | ||||||||
12940 | SmallVector<SourceLocation, 16> SelLocs; | ||||||||
12941 | E->getSelectorLocs(SelLocs); | ||||||||
12942 | return getDerived().RebuildObjCMessageExpr(E->getSuperLoc(), | ||||||||
12943 | E->getSelector(), | ||||||||
12944 | SelLocs, | ||||||||
12945 | E->getReceiverType(), | ||||||||
12946 | E->getMethodDecl(), | ||||||||
12947 | E->getLeftLoc(), | ||||||||
12948 | Args, | ||||||||
12949 | E->getRightLoc()); | ||||||||
12950 | } | ||||||||
12951 | |||||||||
12952 | // Instance message: transform the receiver | ||||||||
12953 | assert(E->getReceiverKind() == ObjCMessageExpr::Instance &&((E->getReceiverKind() == ObjCMessageExpr::Instance && "Only class and instance messages may be instantiated") ? static_cast <void> (0) : __assert_fail ("E->getReceiverKind() == ObjCMessageExpr::Instance && \"Only class and instance messages may be instantiated\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 12954, __PRETTY_FUNCTION__)) | ||||||||
12954 | "Only class and instance messages may be instantiated")((E->getReceiverKind() == ObjCMessageExpr::Instance && "Only class and instance messages may be instantiated") ? static_cast <void> (0) : __assert_fail ("E->getReceiverKind() == ObjCMessageExpr::Instance && \"Only class and instance messages may be instantiated\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 12954, __PRETTY_FUNCTION__)); | ||||||||
12955 | ExprResult Receiver | ||||||||
12956 | = getDerived().TransformExpr(E->getInstanceReceiver()); | ||||||||
12957 | if (Receiver.isInvalid()) | ||||||||
12958 | return ExprError(); | ||||||||
12959 | |||||||||
12960 | // If nothing changed, just retain the existing message send. | ||||||||
12961 | if (!getDerived().AlwaysRebuild() && | ||||||||
12962 | Receiver.get() == E->getInstanceReceiver() && !ArgChanged) | ||||||||
12963 | return SemaRef.MaybeBindToTemporary(E); | ||||||||
12964 | |||||||||
12965 | // Build a new instance message send. | ||||||||
12966 | SmallVector<SourceLocation, 16> SelLocs; | ||||||||
12967 | E->getSelectorLocs(SelLocs); | ||||||||
12968 | return getDerived().RebuildObjCMessageExpr(Receiver.get(), | ||||||||
12969 | E->getSelector(), | ||||||||
12970 | SelLocs, | ||||||||
12971 | E->getMethodDecl(), | ||||||||
12972 | E->getLeftLoc(), | ||||||||
12973 | Args, | ||||||||
12974 | E->getRightLoc()); | ||||||||
12975 | } | ||||||||
12976 | |||||||||
12977 | template<typename Derived> | ||||||||
12978 | ExprResult | ||||||||
12979 | TreeTransform<Derived>::TransformObjCSelectorExpr(ObjCSelectorExpr *E) { | ||||||||
12980 | return E; | ||||||||
12981 | } | ||||||||
12982 | |||||||||
12983 | template<typename Derived> | ||||||||
12984 | ExprResult | ||||||||
12985 | TreeTransform<Derived>::TransformObjCProtocolExpr(ObjCProtocolExpr *E) { | ||||||||
12986 | return E; | ||||||||
12987 | } | ||||||||
12988 | |||||||||
12989 | template<typename Derived> | ||||||||
12990 | ExprResult | ||||||||
12991 | TreeTransform<Derived>::TransformObjCIvarRefExpr(ObjCIvarRefExpr *E) { | ||||||||
12992 | // Transform the base expression. | ||||||||
12993 | ExprResult Base = getDerived().TransformExpr(E->getBase()); | ||||||||
12994 | if (Base.isInvalid()) | ||||||||
12995 | return ExprError(); | ||||||||
12996 | |||||||||
12997 | // We don't need to transform the ivar; it will never change. | ||||||||
12998 | |||||||||
12999 | // If nothing changed, just retain the existing expression. | ||||||||
13000 | if (!getDerived().AlwaysRebuild() && | ||||||||
13001 | Base.get() == E->getBase()) | ||||||||
13002 | return E; | ||||||||
13003 | |||||||||
13004 | return getDerived().RebuildObjCIvarRefExpr(Base.get(), E->getDecl(), | ||||||||
13005 | E->getLocation(), | ||||||||
13006 | E->isArrow(), E->isFreeIvar()); | ||||||||
13007 | } | ||||||||
13008 | |||||||||
13009 | template<typename Derived> | ||||||||
13010 | ExprResult | ||||||||
13011 | TreeTransform<Derived>::TransformObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { | ||||||||
13012 | // 'super' and types never change. Property never changes. Just | ||||||||
13013 | // retain the existing expression. | ||||||||
13014 | if (!E->isObjectReceiver()) | ||||||||
13015 | return E; | ||||||||
13016 | |||||||||
13017 | // Transform the base expression. | ||||||||
13018 | ExprResult Base = getDerived().TransformExpr(E->getBase()); | ||||||||
13019 | if (Base.isInvalid()) | ||||||||
13020 | return ExprError(); | ||||||||
13021 | |||||||||
13022 | // We don't need to transform the property; it will never change. | ||||||||
13023 | |||||||||
13024 | // If nothing changed, just retain the existing expression. | ||||||||
13025 | if (!getDerived().AlwaysRebuild() && | ||||||||
13026 | Base.get() == E->getBase()) | ||||||||
13027 | return E; | ||||||||
13028 | |||||||||
13029 | if (E->isExplicitProperty()) | ||||||||
13030 | return getDerived().RebuildObjCPropertyRefExpr(Base.get(), | ||||||||
13031 | E->getExplicitProperty(), | ||||||||
13032 | E->getLocation()); | ||||||||
13033 | |||||||||
13034 | return getDerived().RebuildObjCPropertyRefExpr(Base.get(), | ||||||||
13035 | SemaRef.Context.PseudoObjectTy, | ||||||||
13036 | E->getImplicitPropertyGetter(), | ||||||||
13037 | E->getImplicitPropertySetter(), | ||||||||
13038 | E->getLocation()); | ||||||||
13039 | } | ||||||||
13040 | |||||||||
13041 | template<typename Derived> | ||||||||
13042 | ExprResult | ||||||||
13043 | TreeTransform<Derived>::TransformObjCSubscriptRefExpr(ObjCSubscriptRefExpr *E) { | ||||||||
13044 | // Transform the base expression. | ||||||||
13045 | ExprResult Base = getDerived().TransformExpr(E->getBaseExpr()); | ||||||||
13046 | if (Base.isInvalid()) | ||||||||
13047 | return ExprError(); | ||||||||
13048 | |||||||||
13049 | // Transform the key expression. | ||||||||
13050 | ExprResult Key = getDerived().TransformExpr(E->getKeyExpr()); | ||||||||
13051 | if (Key.isInvalid()) | ||||||||
13052 | return ExprError(); | ||||||||
13053 | |||||||||
13054 | // If nothing changed, just retain the existing expression. | ||||||||
13055 | if (!getDerived().AlwaysRebuild() && | ||||||||
13056 | Key.get() == E->getKeyExpr() && Base.get() == E->getBaseExpr()) | ||||||||
13057 | return E; | ||||||||
13058 | |||||||||
13059 | return getDerived().RebuildObjCSubscriptRefExpr(E->getRBracket(), | ||||||||
13060 | Base.get(), Key.get(), | ||||||||
13061 | E->getAtIndexMethodDecl(), | ||||||||
13062 | E->setAtIndexMethodDecl()); | ||||||||
13063 | } | ||||||||
13064 | |||||||||
13065 | template<typename Derived> | ||||||||
13066 | ExprResult | ||||||||
13067 | TreeTransform<Derived>::TransformObjCIsaExpr(ObjCIsaExpr *E) { | ||||||||
13068 | // Transform the base expression. | ||||||||
13069 | ExprResult Base = getDerived().TransformExpr(E->getBase()); | ||||||||
13070 | if (Base.isInvalid()) | ||||||||
13071 | return ExprError(); | ||||||||
13072 | |||||||||
13073 | // If nothing changed, just retain the existing expression. | ||||||||
13074 | if (!getDerived().AlwaysRebuild() && | ||||||||
13075 | Base.get() == E->getBase()) | ||||||||
13076 | return E; | ||||||||
13077 | |||||||||
13078 | return getDerived().RebuildObjCIsaExpr(Base.get(), E->getIsaMemberLoc(), | ||||||||
13079 | E->getOpLoc(), | ||||||||
13080 | E->isArrow()); | ||||||||
13081 | } | ||||||||
13082 | |||||||||
13083 | template<typename Derived> | ||||||||
13084 | ExprResult | ||||||||
13085 | TreeTransform<Derived>::TransformShuffleVectorExpr(ShuffleVectorExpr *E) { | ||||||||
13086 | bool ArgumentChanged = false; | ||||||||
13087 | SmallVector<Expr*, 8> SubExprs; | ||||||||
13088 | SubExprs.reserve(E->getNumSubExprs()); | ||||||||
13089 | if (getDerived().TransformExprs(E->getSubExprs(), E->getNumSubExprs(), false, | ||||||||
13090 | SubExprs, &ArgumentChanged)) | ||||||||
13091 | return ExprError(); | ||||||||
13092 | |||||||||
13093 | if (!getDerived().AlwaysRebuild() && | ||||||||
13094 | !ArgumentChanged) | ||||||||
13095 | return E; | ||||||||
13096 | |||||||||
13097 | return getDerived().RebuildShuffleVectorExpr(E->getBuiltinLoc(), | ||||||||
13098 | SubExprs, | ||||||||
13099 | E->getRParenLoc()); | ||||||||
13100 | } | ||||||||
13101 | |||||||||
13102 | template<typename Derived> | ||||||||
13103 | ExprResult | ||||||||
13104 | TreeTransform<Derived>::TransformConvertVectorExpr(ConvertVectorExpr *E) { | ||||||||
13105 | ExprResult SrcExpr = getDerived().TransformExpr(E->getSrcExpr()); | ||||||||
13106 | if (SrcExpr.isInvalid()) | ||||||||
13107 | return ExprError(); | ||||||||
13108 | |||||||||
13109 | TypeSourceInfo *Type = getDerived().TransformType(E->getTypeSourceInfo()); | ||||||||
13110 | if (!Type) | ||||||||
13111 | return ExprError(); | ||||||||
13112 | |||||||||
13113 | if (!getDerived().AlwaysRebuild() && | ||||||||
13114 | Type == E->getTypeSourceInfo() && | ||||||||
13115 | SrcExpr.get() == E->getSrcExpr()) | ||||||||
13116 | return E; | ||||||||
13117 | |||||||||
13118 | return getDerived().RebuildConvertVectorExpr(E->getBuiltinLoc(), | ||||||||
13119 | SrcExpr.get(), Type, | ||||||||
13120 | E->getRParenLoc()); | ||||||||
13121 | } | ||||||||
13122 | |||||||||
13123 | template<typename Derived> | ||||||||
13124 | ExprResult | ||||||||
13125 | TreeTransform<Derived>::TransformBlockExpr(BlockExpr *E) { | ||||||||
13126 | BlockDecl *oldBlock = E->getBlockDecl(); | ||||||||
13127 | |||||||||
13128 | SemaRef.ActOnBlockStart(E->getCaretLocation(), /*Scope=*/nullptr); | ||||||||
13129 | BlockScopeInfo *blockScope = SemaRef.getCurBlock(); | ||||||||
13130 | |||||||||
13131 | blockScope->TheDecl->setIsVariadic(oldBlock->isVariadic()); | ||||||||
13132 | blockScope->TheDecl->setBlockMissingReturnType( | ||||||||
13133 | oldBlock->blockMissingReturnType()); | ||||||||
13134 | |||||||||
13135 | SmallVector<ParmVarDecl*, 4> params; | ||||||||
13136 | SmallVector<QualType, 4> paramTypes; | ||||||||
13137 | |||||||||
13138 | const FunctionProtoType *exprFunctionType = E->getFunctionType(); | ||||||||
13139 | |||||||||
13140 | // Parameter substitution. | ||||||||
13141 | Sema::ExtParameterInfoBuilder extParamInfos; | ||||||||
13142 | if (getDerived().TransformFunctionTypeParams( | ||||||||
13143 | E->getCaretLocation(), oldBlock->parameters(), nullptr, | ||||||||
13144 | exprFunctionType->getExtParameterInfosOrNull(), paramTypes, ¶ms, | ||||||||
13145 | extParamInfos)) { | ||||||||
13146 | getSema().ActOnBlockError(E->getCaretLocation(), /*Scope=*/nullptr); | ||||||||
13147 | return ExprError(); | ||||||||
13148 | } | ||||||||
13149 | |||||||||
13150 | QualType exprResultType = | ||||||||
13151 | getDerived().TransformType(exprFunctionType->getReturnType()); | ||||||||
13152 | |||||||||
13153 | auto epi = exprFunctionType->getExtProtoInfo(); | ||||||||
13154 | epi.ExtParameterInfos = extParamInfos.getPointerOrNull(paramTypes.size()); | ||||||||
13155 | |||||||||
13156 | QualType functionType = | ||||||||
13157 | getDerived().RebuildFunctionProtoType(exprResultType, paramTypes, epi); | ||||||||
13158 | blockScope->FunctionType = functionType; | ||||||||
13159 | |||||||||
13160 | // Set the parameters on the block decl. | ||||||||
13161 | if (!params.empty()) | ||||||||
13162 | blockScope->TheDecl->setParams(params); | ||||||||
13163 | |||||||||
13164 | if (!oldBlock->blockMissingReturnType()) { | ||||||||
13165 | blockScope->HasImplicitReturnType = false; | ||||||||
13166 | blockScope->ReturnType = exprResultType; | ||||||||
13167 | } | ||||||||
13168 | |||||||||
13169 | // Transform the body | ||||||||
13170 | StmtResult body = getDerived().TransformStmt(E->getBody()); | ||||||||
13171 | if (body.isInvalid()) { | ||||||||
13172 | getSema().ActOnBlockError(E->getCaretLocation(), /*Scope=*/nullptr); | ||||||||
13173 | return ExprError(); | ||||||||
13174 | } | ||||||||
13175 | |||||||||
13176 | #ifndef NDEBUG | ||||||||
13177 | // In builds with assertions, make sure that we captured everything we | ||||||||
13178 | // captured before. | ||||||||
13179 | if (!SemaRef.getDiagnostics().hasErrorOccurred()) { | ||||||||
13180 | for (const auto &I : oldBlock->captures()) { | ||||||||
13181 | VarDecl *oldCapture = I.getVariable(); | ||||||||
13182 | |||||||||
13183 | // Ignore parameter packs. | ||||||||
13184 | if (oldCapture->isParameterPack()) | ||||||||
13185 | continue; | ||||||||
13186 | |||||||||
13187 | VarDecl *newCapture = | ||||||||
13188 | cast<VarDecl>(getDerived().TransformDecl(E->getCaretLocation(), | ||||||||
13189 | oldCapture)); | ||||||||
13190 | assert(blockScope->CaptureMap.count(newCapture))((blockScope->CaptureMap.count(newCapture)) ? static_cast< void> (0) : __assert_fail ("blockScope->CaptureMap.count(newCapture)" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 13190, __PRETTY_FUNCTION__)); | ||||||||
13191 | } | ||||||||
13192 | assert(oldBlock->capturesCXXThis() == blockScope->isCXXThisCaptured())((oldBlock->capturesCXXThis() == blockScope->isCXXThisCaptured ()) ? static_cast<void> (0) : __assert_fail ("oldBlock->capturesCXXThis() == blockScope->isCXXThisCaptured()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 13192, __PRETTY_FUNCTION__)); | ||||||||
13193 | } | ||||||||
13194 | #endif | ||||||||
13195 | |||||||||
13196 | return SemaRef.ActOnBlockStmtExpr(E->getCaretLocation(), body.get(), | ||||||||
13197 | /*Scope=*/nullptr); | ||||||||
13198 | } | ||||||||
13199 | |||||||||
13200 | template<typename Derived> | ||||||||
13201 | ExprResult | ||||||||
13202 | TreeTransform<Derived>::TransformAsTypeExpr(AsTypeExpr *E) { | ||||||||
13203 | llvm_unreachable("Cannot transform asType expressions yet")::llvm::llvm_unreachable_internal("Cannot transform asType expressions yet" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 13203); | ||||||||
13204 | } | ||||||||
13205 | |||||||||
13206 | template<typename Derived> | ||||||||
13207 | ExprResult | ||||||||
13208 | TreeTransform<Derived>::TransformAtomicExpr(AtomicExpr *E) { | ||||||||
13209 | bool ArgumentChanged = false; | ||||||||
13210 | SmallVector<Expr*, 8> SubExprs; | ||||||||
13211 | SubExprs.reserve(E->getNumSubExprs()); | ||||||||
13212 | if (getDerived().TransformExprs(E->getSubExprs(), E->getNumSubExprs(), false, | ||||||||
13213 | SubExprs, &ArgumentChanged)) | ||||||||
13214 | return ExprError(); | ||||||||
13215 | |||||||||
13216 | if (!getDerived().AlwaysRebuild() && | ||||||||
13217 | !ArgumentChanged) | ||||||||
13218 | return E; | ||||||||
13219 | |||||||||
13220 | return getDerived().RebuildAtomicExpr(E->getBuiltinLoc(), SubExprs, | ||||||||
13221 | E->getOp(), E->getRParenLoc()); | ||||||||
13222 | } | ||||||||
13223 | |||||||||
13224 | //===----------------------------------------------------------------------===// | ||||||||
13225 | // Type reconstruction | ||||||||
13226 | //===----------------------------------------------------------------------===// | ||||||||
13227 | |||||||||
13228 | template<typename Derived> | ||||||||
13229 | QualType TreeTransform<Derived>::RebuildPointerType(QualType PointeeType, | ||||||||
13230 | SourceLocation Star) { | ||||||||
13231 | return SemaRef.BuildPointerType(PointeeType, Star, | ||||||||
13232 | getDerived().getBaseEntity()); | ||||||||
13233 | } | ||||||||
13234 | |||||||||
13235 | template<typename Derived> | ||||||||
13236 | QualType TreeTransform<Derived>::RebuildBlockPointerType(QualType PointeeType, | ||||||||
13237 | SourceLocation Star) { | ||||||||
13238 | return SemaRef.BuildBlockPointerType(PointeeType, Star, | ||||||||
13239 | getDerived().getBaseEntity()); | ||||||||
13240 | } | ||||||||
13241 | |||||||||
13242 | template<typename Derived> | ||||||||
13243 | QualType | ||||||||
13244 | TreeTransform<Derived>::RebuildReferenceType(QualType ReferentType, | ||||||||
13245 | bool WrittenAsLValue, | ||||||||
13246 | SourceLocation Sigil) { | ||||||||
13247 | return SemaRef.BuildReferenceType(ReferentType, WrittenAsLValue, | ||||||||
13248 | Sigil, getDerived().getBaseEntity()); | ||||||||
13249 | } | ||||||||
13250 | |||||||||
13251 | template<typename Derived> | ||||||||
13252 | QualType | ||||||||
13253 | TreeTransform<Derived>::RebuildMemberPointerType(QualType PointeeType, | ||||||||
13254 | QualType ClassType, | ||||||||
13255 | SourceLocation Sigil) { | ||||||||
13256 | return SemaRef.BuildMemberPointerType(PointeeType, ClassType, Sigil, | ||||||||
13257 | getDerived().getBaseEntity()); | ||||||||
13258 | } | ||||||||
13259 | |||||||||
13260 | template<typename Derived> | ||||||||
13261 | QualType TreeTransform<Derived>::RebuildObjCTypeParamType( | ||||||||
13262 | const ObjCTypeParamDecl *Decl, | ||||||||
13263 | SourceLocation ProtocolLAngleLoc, | ||||||||
13264 | ArrayRef<ObjCProtocolDecl *> Protocols, | ||||||||
13265 | ArrayRef<SourceLocation> ProtocolLocs, | ||||||||
13266 | SourceLocation ProtocolRAngleLoc) { | ||||||||
13267 | return SemaRef.BuildObjCTypeParamType(Decl, | ||||||||
13268 | ProtocolLAngleLoc, Protocols, | ||||||||
13269 | ProtocolLocs, ProtocolRAngleLoc, | ||||||||
13270 | /*FailOnError=*/true); | ||||||||
13271 | } | ||||||||
13272 | |||||||||
13273 | template<typename Derived> | ||||||||
13274 | QualType TreeTransform<Derived>::RebuildObjCObjectType( | ||||||||
13275 | QualType BaseType, | ||||||||
13276 | SourceLocation Loc, | ||||||||
13277 | SourceLocation TypeArgsLAngleLoc, | ||||||||
13278 | ArrayRef<TypeSourceInfo *> TypeArgs, | ||||||||
13279 | SourceLocation TypeArgsRAngleLoc, | ||||||||
13280 | SourceLocation ProtocolLAngleLoc, | ||||||||
13281 | ArrayRef<ObjCProtocolDecl *> Protocols, | ||||||||
13282 | ArrayRef<SourceLocation> ProtocolLocs, | ||||||||
13283 | SourceLocation ProtocolRAngleLoc) { | ||||||||
13284 | return SemaRef.BuildObjCObjectType(BaseType, Loc, TypeArgsLAngleLoc, | ||||||||
13285 | TypeArgs, TypeArgsRAngleLoc, | ||||||||
13286 | ProtocolLAngleLoc, Protocols, ProtocolLocs, | ||||||||
13287 | ProtocolRAngleLoc, | ||||||||
13288 | /*FailOnError=*/true); | ||||||||
13289 | } | ||||||||
13290 | |||||||||
13291 | template<typename Derived> | ||||||||
13292 | QualType TreeTransform<Derived>::RebuildObjCObjectPointerType( | ||||||||
13293 | QualType PointeeType, | ||||||||
13294 | SourceLocation Star) { | ||||||||
13295 | return SemaRef.Context.getObjCObjectPointerType(PointeeType); | ||||||||
13296 | } | ||||||||
13297 | |||||||||
13298 | template<typename Derived> | ||||||||
13299 | QualType | ||||||||
13300 | TreeTransform<Derived>::RebuildArrayType(QualType ElementType, | ||||||||
13301 | ArrayType::ArraySizeModifier SizeMod, | ||||||||
13302 | const llvm::APInt *Size, | ||||||||
13303 | Expr *SizeExpr, | ||||||||
13304 | unsigned IndexTypeQuals, | ||||||||
13305 | SourceRange BracketsRange) { | ||||||||
13306 | if (SizeExpr || !Size) | ||||||||
13307 | return SemaRef.BuildArrayType(ElementType, SizeMod, SizeExpr, | ||||||||
13308 | IndexTypeQuals, BracketsRange, | ||||||||
13309 | getDerived().getBaseEntity()); | ||||||||
13310 | |||||||||
13311 | QualType Types[] = { | ||||||||
13312 | SemaRef.Context.UnsignedCharTy, SemaRef.Context.UnsignedShortTy, | ||||||||
13313 | SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy, | ||||||||
13314 | SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty | ||||||||
13315 | }; | ||||||||
13316 | const unsigned NumTypes = llvm::array_lengthof(Types); | ||||||||
13317 | QualType SizeType; | ||||||||
13318 | for (unsigned I = 0; I != NumTypes; ++I) | ||||||||
13319 | if (Size->getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) { | ||||||||
13320 | SizeType = Types[I]; | ||||||||
13321 | break; | ||||||||
13322 | } | ||||||||
13323 | |||||||||
13324 | // Note that we can return a VariableArrayType here in the case where | ||||||||
13325 | // the element type was a dependent VariableArrayType. | ||||||||
13326 | IntegerLiteral *ArraySize | ||||||||
13327 | = IntegerLiteral::Create(SemaRef.Context, *Size, SizeType, | ||||||||
13328 | /*FIXME*/BracketsRange.getBegin()); | ||||||||
13329 | return SemaRef.BuildArrayType(ElementType, SizeMod, ArraySize, | ||||||||
13330 | IndexTypeQuals, BracketsRange, | ||||||||
13331 | getDerived().getBaseEntity()); | ||||||||
13332 | } | ||||||||
13333 | |||||||||
13334 | template<typename Derived> | ||||||||
13335 | QualType | ||||||||
13336 | TreeTransform<Derived>::RebuildConstantArrayType(QualType ElementType, | ||||||||
13337 | ArrayType::ArraySizeModifier SizeMod, | ||||||||
13338 | const llvm::APInt &Size, | ||||||||
13339 | Expr *SizeExpr, | ||||||||
13340 | unsigned IndexTypeQuals, | ||||||||
13341 | SourceRange BracketsRange) { | ||||||||
13342 | return getDerived().RebuildArrayType(ElementType, SizeMod, &Size, SizeExpr, | ||||||||
13343 | IndexTypeQuals, BracketsRange); | ||||||||
13344 | } | ||||||||
13345 | |||||||||
13346 | template<typename Derived> | ||||||||
13347 | QualType | ||||||||
13348 | TreeTransform<Derived>::RebuildIncompleteArrayType(QualType ElementType, | ||||||||
13349 | ArrayType::ArraySizeModifier SizeMod, | ||||||||
13350 | unsigned IndexTypeQuals, | ||||||||
13351 | SourceRange BracketsRange) { | ||||||||
13352 | return getDerived().RebuildArrayType(ElementType, SizeMod, nullptr, nullptr, | ||||||||
13353 | IndexTypeQuals, BracketsRange); | ||||||||
13354 | } | ||||||||
13355 | |||||||||
13356 | template<typename Derived> | ||||||||
13357 | QualType | ||||||||
13358 | TreeTransform<Derived>::RebuildVariableArrayType(QualType ElementType, | ||||||||
13359 | ArrayType::ArraySizeModifier SizeMod, | ||||||||
13360 | Expr *SizeExpr, | ||||||||
13361 | unsigned IndexTypeQuals, | ||||||||
13362 | SourceRange BracketsRange) { | ||||||||
13363 | return getDerived().RebuildArrayType(ElementType, SizeMod, nullptr, | ||||||||
13364 | SizeExpr, | ||||||||
13365 | IndexTypeQuals, BracketsRange); | ||||||||
13366 | } | ||||||||
13367 | |||||||||
13368 | template<typename Derived> | ||||||||
13369 | QualType | ||||||||
13370 | TreeTransform<Derived>::RebuildDependentSizedArrayType(QualType ElementType, | ||||||||
13371 | ArrayType::ArraySizeModifier SizeMod, | ||||||||
13372 | Expr *SizeExpr, | ||||||||
13373 | unsigned IndexTypeQuals, | ||||||||
13374 | SourceRange BracketsRange) { | ||||||||
13375 | return getDerived().RebuildArrayType(ElementType, SizeMod, nullptr, | ||||||||
13376 | SizeExpr, | ||||||||
13377 | IndexTypeQuals, BracketsRange); | ||||||||
13378 | } | ||||||||
13379 | |||||||||
13380 | template <typename Derived> | ||||||||
13381 | QualType TreeTransform<Derived>::RebuildDependentAddressSpaceType( | ||||||||
13382 | QualType PointeeType, Expr *AddrSpaceExpr, SourceLocation AttributeLoc) { | ||||||||
13383 | return SemaRef.BuildAddressSpaceAttr(PointeeType, AddrSpaceExpr, | ||||||||
13384 | AttributeLoc); | ||||||||
13385 | } | ||||||||
13386 | |||||||||
13387 | template <typename Derived> | ||||||||
13388 | QualType | ||||||||
13389 | TreeTransform<Derived>::RebuildVectorType(QualType ElementType, | ||||||||
13390 | unsigned NumElements, | ||||||||
13391 | VectorType::VectorKind VecKind) { | ||||||||
13392 | // FIXME: semantic checking! | ||||||||
13393 | return SemaRef.Context.getVectorType(ElementType, NumElements, VecKind); | ||||||||
13394 | } | ||||||||
13395 | |||||||||
13396 | template <typename Derived> | ||||||||
13397 | QualType TreeTransform<Derived>::RebuildDependentVectorType( | ||||||||
13398 | QualType ElementType, Expr *SizeExpr, SourceLocation AttributeLoc, | ||||||||
13399 | VectorType::VectorKind VecKind) { | ||||||||
13400 | return SemaRef.BuildVectorType(ElementType, SizeExpr, AttributeLoc); | ||||||||
13401 | } | ||||||||
13402 | |||||||||
13403 | template<typename Derived> | ||||||||
13404 | QualType TreeTransform<Derived>::RebuildExtVectorType(QualType ElementType, | ||||||||
13405 | unsigned NumElements, | ||||||||
13406 | SourceLocation AttributeLoc) { | ||||||||
13407 | llvm::APInt numElements(SemaRef.Context.getIntWidth(SemaRef.Context.IntTy), | ||||||||
13408 | NumElements, true); | ||||||||
13409 | IntegerLiteral *VectorSize | ||||||||
13410 | = IntegerLiteral::Create(SemaRef.Context, numElements, SemaRef.Context.IntTy, | ||||||||
13411 | AttributeLoc); | ||||||||
13412 | return SemaRef.BuildExtVectorType(ElementType, VectorSize, AttributeLoc); | ||||||||
13413 | } | ||||||||
13414 | |||||||||
13415 | template<typename Derived> | ||||||||
13416 | QualType | ||||||||
13417 | TreeTransform<Derived>::RebuildDependentSizedExtVectorType(QualType ElementType, | ||||||||
13418 | Expr *SizeExpr, | ||||||||
13419 | SourceLocation AttributeLoc) { | ||||||||
13420 | return SemaRef.BuildExtVectorType(ElementType, SizeExpr, AttributeLoc); | ||||||||
13421 | } | ||||||||
13422 | |||||||||
13423 | template<typename Derived> | ||||||||
13424 | QualType TreeTransform<Derived>::RebuildFunctionProtoType( | ||||||||
13425 | QualType T, | ||||||||
13426 | MutableArrayRef<QualType> ParamTypes, | ||||||||
13427 | const FunctionProtoType::ExtProtoInfo &EPI) { | ||||||||
13428 | return SemaRef.BuildFunctionType(T, ParamTypes, | ||||||||
13429 | getDerived().getBaseLocation(), | ||||||||
13430 | getDerived().getBaseEntity(), | ||||||||
13431 | EPI); | ||||||||
13432 | } | ||||||||
13433 | |||||||||
13434 | template<typename Derived> | ||||||||
13435 | QualType TreeTransform<Derived>::RebuildFunctionNoProtoType(QualType T) { | ||||||||
13436 | return SemaRef.Context.getFunctionNoProtoType(T); | ||||||||
13437 | } | ||||||||
13438 | |||||||||
13439 | template<typename Derived> | ||||||||
13440 | QualType TreeTransform<Derived>::RebuildUnresolvedUsingType(SourceLocation Loc, | ||||||||
13441 | Decl *D) { | ||||||||
13442 | assert(D && "no decl found")((D && "no decl found") ? static_cast<void> (0) : __assert_fail ("D && \"no decl found\"", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 13442, __PRETTY_FUNCTION__)); | ||||||||
13443 | if (D->isInvalidDecl()) return QualType(); | ||||||||
13444 | |||||||||
13445 | // FIXME: Doesn't account for ObjCInterfaceDecl! | ||||||||
13446 | TypeDecl *Ty; | ||||||||
13447 | if (auto *UPD = dyn_cast<UsingPackDecl>(D)) { | ||||||||
13448 | // A valid resolved using typename pack expansion decl can have multiple | ||||||||
13449 | // UsingDecls, but they must each have exactly one type, and it must be | ||||||||
13450 | // the same type in every case. But we must have at least one expansion! | ||||||||
13451 | if (UPD->expansions().empty()) { | ||||||||
13452 | getSema().Diag(Loc, diag::err_using_pack_expansion_empty) | ||||||||
13453 | << UPD->isCXXClassMember() << UPD; | ||||||||
13454 | return QualType(); | ||||||||
13455 | } | ||||||||
13456 | |||||||||
13457 | // We might still have some unresolved types. Try to pick a resolved type | ||||||||
13458 | // if we can. The final instantiation will check that the remaining | ||||||||
13459 | // unresolved types instantiate to the type we pick. | ||||||||
13460 | QualType FallbackT; | ||||||||
13461 | QualType T; | ||||||||
13462 | for (auto *E : UPD->expansions()) { | ||||||||
13463 | QualType ThisT = RebuildUnresolvedUsingType(Loc, E); | ||||||||
13464 | if (ThisT.isNull()) | ||||||||
13465 | continue; | ||||||||
13466 | else if (ThisT->getAs<UnresolvedUsingType>()) | ||||||||
13467 | FallbackT = ThisT; | ||||||||
13468 | else if (T.isNull()) | ||||||||
13469 | T = ThisT; | ||||||||
13470 | else | ||||||||
13471 | assert(getSema().Context.hasSameType(ThisT, T) &&((getSema().Context.hasSameType(ThisT, T) && "mismatched resolved types in using pack expansion" ) ? static_cast<void> (0) : __assert_fail ("getSema().Context.hasSameType(ThisT, T) && \"mismatched resolved types in using pack expansion\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 13472, __PRETTY_FUNCTION__)) | ||||||||
13472 | "mismatched resolved types in using pack expansion")((getSema().Context.hasSameType(ThisT, T) && "mismatched resolved types in using pack expansion" ) ? static_cast<void> (0) : __assert_fail ("getSema().Context.hasSameType(ThisT, T) && \"mismatched resolved types in using pack expansion\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 13472, __PRETTY_FUNCTION__)); | ||||||||
13473 | } | ||||||||
13474 | return T.isNull() ? FallbackT : T; | ||||||||
13475 | } else if (auto *Using = dyn_cast<UsingDecl>(D)) { | ||||||||
13476 | assert(Using->hasTypename() &&((Using->hasTypename() && "UnresolvedUsingTypenameDecl transformed to non-typename using" ) ? static_cast<void> (0) : __assert_fail ("Using->hasTypename() && \"UnresolvedUsingTypenameDecl transformed to non-typename using\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 13477, __PRETTY_FUNCTION__)) | ||||||||
13477 | "UnresolvedUsingTypenameDecl transformed to non-typename using")((Using->hasTypename() && "UnresolvedUsingTypenameDecl transformed to non-typename using" ) ? static_cast<void> (0) : __assert_fail ("Using->hasTypename() && \"UnresolvedUsingTypenameDecl transformed to non-typename using\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 13477, __PRETTY_FUNCTION__)); | ||||||||
13478 | |||||||||
13479 | // A valid resolved using typename decl points to exactly one type decl. | ||||||||
13480 | assert(++Using->shadow_begin() == Using->shadow_end())((++Using->shadow_begin() == Using->shadow_end()) ? static_cast <void> (0) : __assert_fail ("++Using->shadow_begin() == Using->shadow_end()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 13480, __PRETTY_FUNCTION__)); | ||||||||
13481 | Ty = cast<TypeDecl>((*Using->shadow_begin())->getTargetDecl()); | ||||||||
13482 | } else { | ||||||||
13483 | assert(isa<UnresolvedUsingTypenameDecl>(D) &&((isa<UnresolvedUsingTypenameDecl>(D) && "UnresolvedUsingTypenameDecl transformed to non-using decl" ) ? static_cast<void> (0) : __assert_fail ("isa<UnresolvedUsingTypenameDecl>(D) && \"UnresolvedUsingTypenameDecl transformed to non-using decl\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 13484, __PRETTY_FUNCTION__)) | ||||||||
13484 | "UnresolvedUsingTypenameDecl transformed to non-using decl")((isa<UnresolvedUsingTypenameDecl>(D) && "UnresolvedUsingTypenameDecl transformed to non-using decl" ) ? static_cast<void> (0) : __assert_fail ("isa<UnresolvedUsingTypenameDecl>(D) && \"UnresolvedUsingTypenameDecl transformed to non-using decl\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Sema/TreeTransform.h" , 13484, __PRETTY_FUNCTION__)); | ||||||||
13485 | Ty = cast<UnresolvedUsingTypenameDecl>(D); | ||||||||
13486 | } | ||||||||
13487 | |||||||||
13488 | return SemaRef.Context.getTypeDeclType(Ty); | ||||||||
13489 | } | ||||||||
13490 | |||||||||
13491 | template<typename Derived> | ||||||||
13492 | QualType TreeTransform<Derived>::RebuildTypeOfExprType(Expr *E, | ||||||||
13493 | SourceLocation Loc) { | ||||||||
13494 | return SemaRef.BuildTypeofExprType(E, Loc); | ||||||||
13495 | } | ||||||||
13496 | |||||||||
13497 | template<typename Derived> | ||||||||
13498 | QualType TreeTransform<Derived>::RebuildTypeOfType(QualType Underlying) { | ||||||||
13499 | return SemaRef.Context.getTypeOfType(Underlying); | ||||||||
13500 | } | ||||||||
13501 | |||||||||
13502 | template<typename Derived> | ||||||||
13503 | QualType TreeTransform<Derived>::RebuildDecltypeType(Expr *E, | ||||||||
13504 | SourceLocation Loc) { | ||||||||
13505 | return SemaRef.BuildDecltypeType(E, Loc); | ||||||||
13506 | } | ||||||||
13507 | |||||||||
13508 | template<typename Derived> | ||||||||
13509 | QualType TreeTransform<Derived>::RebuildUnaryTransformType(QualType BaseType, | ||||||||
13510 | UnaryTransformType::UTTKind UKind, | ||||||||
13511 | SourceLocation Loc) { | ||||||||
13512 | return SemaRef.BuildUnaryTransformType(BaseType, UKind, Loc); | ||||||||
13513 | } | ||||||||
13514 | |||||||||
13515 | template<typename Derived> | ||||||||
13516 | QualType TreeTransform<Derived>::RebuildTemplateSpecializationType( | ||||||||
13517 | TemplateName Template, | ||||||||
13518 | SourceLocation TemplateNameLoc, | ||||||||
13519 | TemplateArgumentListInfo &TemplateArgs) { | ||||||||
13520 | return SemaRef.CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); | ||||||||
13521 | } | ||||||||
13522 | |||||||||
13523 | template<typename Derived> | ||||||||
13524 | QualType TreeTransform<Derived>::RebuildAtomicType(QualType ValueType, | ||||||||
13525 | SourceLocation KWLoc) { | ||||||||
13526 | return SemaRef.BuildAtomicType(ValueType, KWLoc); | ||||||||
13527 | } | ||||||||
13528 | |||||||||
13529 | template<typename Derived> | ||||||||
13530 | QualType TreeTransform<Derived>::RebuildPipeType(QualType ValueType, | ||||||||
13531 | SourceLocation KWLoc, | ||||||||
13532 | bool isReadPipe) { | ||||||||
13533 | return isReadPipe ? SemaRef.BuildReadPipeType(ValueType, KWLoc) | ||||||||
13534 | : SemaRef.BuildWritePipeType(ValueType, KWLoc); | ||||||||
13535 | } | ||||||||
13536 | |||||||||
13537 | template<typename Derived> | ||||||||
13538 | TemplateName | ||||||||
13539 | TreeTransform<Derived>::RebuildTemplateName(CXXScopeSpec &SS, | ||||||||
13540 | bool TemplateKW, | ||||||||
13541 | TemplateDecl *Template) { | ||||||||
13542 | return SemaRef.Context.getQualifiedTemplateName(SS.getScopeRep(), TemplateKW, | ||||||||
13543 | Template); | ||||||||
13544 | } | ||||||||
13545 | |||||||||
13546 | template<typename Derived> | ||||||||
13547 | TemplateName | ||||||||
13548 | TreeTransform<Derived>::RebuildTemplateName(CXXScopeSpec &SS, | ||||||||
13549 | SourceLocation TemplateKWLoc, | ||||||||
13550 | const IdentifierInfo &Name, | ||||||||
13551 | SourceLocation NameLoc, | ||||||||
13552 | QualType ObjectType, | ||||||||
13553 | NamedDecl *FirstQualifierInScope, | ||||||||
13554 | bool AllowInjectedClassName) { | ||||||||
13555 | UnqualifiedId TemplateName; | ||||||||
13556 | TemplateName.setIdentifier(&Name, NameLoc); | ||||||||
13557 | Sema::TemplateTy Template; | ||||||||
13558 | getSema().ActOnDependentTemplateName(/*Scope=*/nullptr, | ||||||||
13559 | SS, TemplateKWLoc, TemplateName, | ||||||||
13560 | ParsedType::make(ObjectType), | ||||||||
13561 | /*EnteringContext=*/false, | ||||||||
13562 | Template, AllowInjectedClassName); | ||||||||
13563 | return Template.get(); | ||||||||
13564 | } | ||||||||
13565 | |||||||||
13566 | template<typename Derived> | ||||||||
13567 | TemplateName | ||||||||
13568 | TreeTransform<Derived>::RebuildTemplateName(CXXScopeSpec &SS, | ||||||||
13569 | SourceLocation TemplateKWLoc, | ||||||||
13570 | OverloadedOperatorKind Operator, | ||||||||
13571 | SourceLocation NameLoc, | ||||||||
13572 | QualType ObjectType, | ||||||||
13573 | bool AllowInjectedClassName) { | ||||||||
13574 | UnqualifiedId Name; | ||||||||
13575 | // FIXME: Bogus location information. | ||||||||
13576 | SourceLocation SymbolLocations[3] = { NameLoc, NameLoc, NameLoc }; | ||||||||
13577 | Name.setOperatorFunctionId(NameLoc, Operator, SymbolLocations); | ||||||||
13578 | Sema::TemplateTy Template; | ||||||||
13579 | getSema().ActOnDependentTemplateName(/*Scope=*/nullptr, | ||||||||
13580 | SS, TemplateKWLoc, Name, | ||||||||
13581 | ParsedType::make(ObjectType), | ||||||||
13582 | /*EnteringContext=*/false, | ||||||||
13583 | Template, AllowInjectedClassName); | ||||||||
13584 | return Template.get(); | ||||||||
13585 | } | ||||||||
13586 | |||||||||
13587 | template<typename Derived> | ||||||||
13588 | ExprResult | ||||||||
13589 | TreeTransform<Derived>::RebuildCXXOperatorCallExpr(OverloadedOperatorKind Op, | ||||||||
13590 | SourceLocation OpLoc, | ||||||||
13591 | Expr *OrigCallee, | ||||||||
13592 | Expr *First, | ||||||||
13593 | Expr *Second) { | ||||||||
13594 | Expr *Callee = OrigCallee->IgnoreParenCasts(); | ||||||||
13595 | bool isPostIncDec = Second && (Op == OO_PlusPlus || Op == OO_MinusMinus); | ||||||||
13596 | |||||||||
13597 | if (First->getObjectKind() == OK_ObjCProperty) { | ||||||||
13598 | BinaryOperatorKind Opc = BinaryOperator::getOverloadedOpcode(Op); | ||||||||
13599 | if (BinaryOperator::isAssignmentOp(Opc)) | ||||||||
13600 | return SemaRef.checkPseudoObjectAssignment(/*Scope=*/nullptr, OpLoc, Opc, | ||||||||
13601 | First, Second); | ||||||||
13602 | ExprResult Result = SemaRef.CheckPlaceholderExpr(First); | ||||||||
13603 | if (Result.isInvalid()) | ||||||||
13604 | return ExprError(); | ||||||||
13605 | First = Result.get(); | ||||||||
13606 | } | ||||||||
13607 | |||||||||
13608 | if (Second
| ||||||||
13609 | ExprResult Result = SemaRef.CheckPlaceholderExpr(Second); | ||||||||
13610 | if (Result.isInvalid()) | ||||||||
13611 | return ExprError(); | ||||||||
13612 | Second = Result.get(); | ||||||||
13613 | } | ||||||||
13614 | |||||||||
13615 | // Determine whether this should be a builtin operation. | ||||||||
13616 | if (Op
| ||||||||
13617 | if (!First->getType()->isOverloadableType() && | ||||||||
13618 | !Second->getType()->isOverloadableType()) | ||||||||
| |||||||||
13619 | return getSema().CreateBuiltinArraySubscriptExpr( | ||||||||
13620 | First, Callee->getBeginLoc(), Second, OpLoc); | ||||||||
13621 | } else if (Op == OO_Arrow) { | ||||||||
13622 | // -> is never a builtin operation. | ||||||||
13623 | return SemaRef.BuildOverloadedArrowExpr(nullptr, First, OpLoc); | ||||||||
13624 | } else if (Second == nullptr || isPostIncDec) { | ||||||||
13625 | if (!First->getType()->isOverloadableType() || | ||||||||
13626 | (Op == OO_Amp && getSema().isQualifiedMemberAccess(First))) { | ||||||||
13627 | // The argument is not of overloadable type, or this is an expression | ||||||||
13628 | // of the form &Class::member, so try to create a built-in unary | ||||||||
13629 | // operation. | ||||||||
13630 | UnaryOperatorKind Opc | ||||||||
13631 | = UnaryOperator::getOverloadedOpcode(Op, isPostIncDec); | ||||||||
13632 | |||||||||
13633 | return getSema().CreateBuiltinUnaryOp(OpLoc, Opc, First); | ||||||||
13634 | } | ||||||||
13635 | } else { | ||||||||
13636 | if (!First->getType()->isOverloadableType() && | ||||||||
13637 | !Second->getType()->isOverloadableType()) { | ||||||||
13638 | // Neither of the arguments is an overloadable type, so try to | ||||||||
13639 | // create a built-in binary operation. | ||||||||
13640 | BinaryOperatorKind Opc = BinaryOperator::getOverloadedOpcode(Op); | ||||||||
13641 | ExprResult Result | ||||||||
13642 | = SemaRef.CreateBuiltinBinOp(OpLoc, Opc, First, Second); | ||||||||
13643 | if (Result.isInvalid()) | ||||||||
13644 | return ExprError(); | ||||||||
13645 | |||||||||
13646 | return Result; | ||||||||
13647 | } | ||||||||
13648 | } | ||||||||
13649 | |||||||||
13650 | // Compute the transformed set of functions (and function templates) to be | ||||||||
13651 | // used during overload resolution. | ||||||||
13652 | UnresolvedSet<16> Functions; | ||||||||
13653 | bool RequiresADL; | ||||||||
13654 | |||||||||
13655 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(Callee)) { | ||||||||
13656 | Functions.append(ULE->decls_begin(), ULE->decls_end()); | ||||||||
13657 | // If the overload could not be resolved in the template definition | ||||||||
13658 | // (because we had a dependent argument), ADL is performed as part of | ||||||||
13659 | // template instantiation. | ||||||||
13660 | RequiresADL = ULE->requiresADL(); | ||||||||
13661 | } else { | ||||||||
13662 | // If we've resolved this to a particular non-member function, just call | ||||||||
13663 | // that function. If we resolved it to a member function, | ||||||||
13664 | // CreateOverloaded* will find that function for us. | ||||||||
13665 | NamedDecl *ND = cast<DeclRefExpr>(Callee)->getDecl(); | ||||||||
13666 | if (!isa<CXXMethodDecl>(ND)) | ||||||||
13667 | Functions.addDecl(ND); | ||||||||
13668 | RequiresADL = false; | ||||||||
13669 | } | ||||||||
13670 | |||||||||
13671 | // Add any functions found via argument-dependent lookup. | ||||||||
13672 | Expr *Args[2] = { First, Second }; | ||||||||
13673 | unsigned NumArgs = 1 + (Second != nullptr); | ||||||||
13674 | |||||||||
13675 | // Create the overloaded operator invocation for unary operators. | ||||||||
13676 | if (NumArgs == 1 || isPostIncDec) { | ||||||||
13677 | UnaryOperatorKind Opc | ||||||||
13678 | = UnaryOperator::getOverloadedOpcode(Op, isPostIncDec); | ||||||||
13679 | return SemaRef.CreateOverloadedUnaryOp(OpLoc, Opc, Functions, First, | ||||||||
13680 | RequiresADL); | ||||||||
13681 | } | ||||||||
13682 | |||||||||
13683 | if (Op == OO_Subscript) { | ||||||||
13684 | SourceLocation LBrace; | ||||||||
13685 | SourceLocation RBrace; | ||||||||
13686 | |||||||||
13687 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) { | ||||||||
13688 | DeclarationNameLoc NameLoc = DRE->getNameInfo().getInfo(); | ||||||||
13689 | LBrace = SourceLocation::getFromRawEncoding( | ||||||||
13690 | NameLoc.CXXOperatorName.BeginOpNameLoc); | ||||||||
13691 | RBrace = SourceLocation::getFromRawEncoding( | ||||||||
13692 | NameLoc.CXXOperatorName.EndOpNameLoc); | ||||||||
13693 | } else { | ||||||||
13694 | LBrace = Callee->getBeginLoc(); | ||||||||
13695 | RBrace = OpLoc; | ||||||||
13696 | } | ||||||||
13697 | |||||||||
13698 | return SemaRef.CreateOverloadedArraySubscriptExpr(LBrace, RBrace, | ||||||||
13699 | First, Second); | ||||||||
13700 | } | ||||||||
13701 | |||||||||
13702 | // Create the overloaded operator invocation for binary operators. | ||||||||
13703 | BinaryOperatorKind Opc = BinaryOperator::getOverloadedOpcode(Op); | ||||||||
13704 | ExprResult Result = SemaRef.CreateOverloadedBinOp( | ||||||||
13705 | OpLoc, Opc, Functions, Args[0], Args[1], RequiresADL); | ||||||||
13706 | if (Result.isInvalid()) | ||||||||
13707 | return ExprError(); | ||||||||
13708 | |||||||||
13709 | return Result; | ||||||||
13710 | } | ||||||||
13711 | |||||||||
13712 | template<typename Derived> | ||||||||
13713 | ExprResult | ||||||||
13714 | TreeTransform<Derived>::RebuildCXXPseudoDestructorExpr(Expr *Base, | ||||||||
13715 | SourceLocation OperatorLoc, | ||||||||
13716 | bool isArrow, | ||||||||
13717 | CXXScopeSpec &SS, | ||||||||
13718 | TypeSourceInfo *ScopeType, | ||||||||
13719 | SourceLocation CCLoc, | ||||||||
13720 | SourceLocation TildeLoc, | ||||||||
13721 | PseudoDestructorTypeStorage Destroyed) { | ||||||||
13722 | QualType BaseType = Base->getType(); | ||||||||
13723 | if (Base->isTypeDependent() || Destroyed.getIdentifier() || | ||||||||
13724 | (!isArrow && !BaseType->getAs<RecordType>()) || | ||||||||
13725 | (isArrow && BaseType->getAs<PointerType>() && | ||||||||
13726 | !BaseType->castAs<PointerType>()->getPointeeType() | ||||||||
13727 | ->template getAs<RecordType>())){ | ||||||||
13728 | // This pseudo-destructor expression is still a pseudo-destructor. | ||||||||
13729 | return SemaRef.BuildPseudoDestructorExpr( | ||||||||
13730 | Base, OperatorLoc, isArrow ? tok::arrow : tok::period, SS, ScopeType, | ||||||||
13731 | CCLoc, TildeLoc, Destroyed); | ||||||||
13732 | } | ||||||||
13733 | |||||||||
13734 | TypeSourceInfo *DestroyedType = Destroyed.getTypeSourceInfo(); | ||||||||
13735 | DeclarationName Name(SemaRef.Context.DeclarationNames.getCXXDestructorName( | ||||||||
13736 | SemaRef.Context.getCanonicalType(DestroyedType->getType()))); | ||||||||
13737 | DeclarationNameInfo NameInfo(Name, Destroyed.getLocation()); | ||||||||
13738 | NameInfo.setNamedTypeInfo(DestroyedType); | ||||||||
13739 | |||||||||
13740 | // The scope type is now known to be a valid nested name specifier | ||||||||
13741 | // component. Tack it on to the end of the nested name specifier. | ||||||||
13742 | if (ScopeType) { | ||||||||
13743 | if (!ScopeType->getType()->getAs<TagType>()) { | ||||||||
13744 | getSema().Diag(ScopeType->getTypeLoc().getBeginLoc(), | ||||||||
13745 | diag::err_expected_class_or_namespace) | ||||||||
13746 | << ScopeType->getType() << getSema().getLangOpts().CPlusPlus; | ||||||||
13747 | return ExprError(); | ||||||||
13748 | } | ||||||||
13749 | SS.Extend(SemaRef.Context, SourceLocation(), ScopeType->getTypeLoc(), | ||||||||
13750 | CCLoc); | ||||||||
13751 | } | ||||||||
13752 | |||||||||
13753 | SourceLocation TemplateKWLoc; // FIXME: retrieve it from caller. | ||||||||
13754 | return getSema().BuildMemberReferenceExpr(Base, BaseType, | ||||||||
13755 | OperatorLoc, isArrow, | ||||||||
13756 | SS, TemplateKWLoc, | ||||||||
13757 | /*FIXME: FirstQualifier*/ nullptr, | ||||||||
13758 | NameInfo, | ||||||||
13759 | /*TemplateArgs*/ nullptr, | ||||||||
13760 | /*S*/nullptr); | ||||||||
13761 | } | ||||||||
13762 | |||||||||
13763 | template<typename Derived> | ||||||||
13764 | StmtResult | ||||||||
13765 | TreeTransform<Derived>::TransformCapturedStmt(CapturedStmt *S) { | ||||||||
13766 | SourceLocation Loc = S->getBeginLoc(); | ||||||||
13767 | CapturedDecl *CD = S->getCapturedDecl(); | ||||||||
13768 | unsigned NumParams = CD->getNumParams(); | ||||||||
13769 | unsigned ContextParamPos = CD->getContextParamPosition(); | ||||||||
13770 | SmallVector<Sema::CapturedParamNameType, 4> Params; | ||||||||
13771 | for (unsigned I = 0; I < NumParams; ++I) { | ||||||||
13772 | if (I != ContextParamPos) { | ||||||||
13773 | Params.push_back( | ||||||||
13774 | std::make_pair( | ||||||||
13775 | CD->getParam(I)->getName(), | ||||||||
13776 | getDerived().TransformType(CD->getParam(I)->getType()))); | ||||||||
13777 | } else { | ||||||||
13778 | Params.push_back(std::make_pair(StringRef(), QualType())); | ||||||||
13779 | } | ||||||||
13780 | } | ||||||||
13781 | getSema().ActOnCapturedRegionStart(Loc, /*CurScope*/nullptr, | ||||||||
13782 | S->getCapturedRegionKind(), Params); | ||||||||
13783 | StmtResult Body; | ||||||||
13784 | { | ||||||||
13785 | Sema::CompoundScopeRAII CompoundScope(getSema()); | ||||||||
13786 | Body = getDerived().TransformStmt(S->getCapturedStmt()); | ||||||||
13787 | } | ||||||||
13788 | |||||||||
13789 | if (Body.isInvalid()) { | ||||||||
13790 | getSema().ActOnCapturedRegionError(); | ||||||||
13791 | return StmtError(); | ||||||||
13792 | } | ||||||||
13793 | |||||||||
13794 | return getSema().ActOnCapturedRegionEnd(Body.get()); | ||||||||
13795 | } | ||||||||
13796 | |||||||||
13797 | } // end namespace clang | ||||||||
13798 | |||||||||
13799 | #endif // LLVM_CLANG_LIB_SEMA_TREETRANSFORM_H |
1 | //===- Ownership.h - Parser ownership helpers -------------------*- C++ -*-===// |
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 | // |
9 | // This file contains classes for managing ownership of Stmt and Expr nodes. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_CLANG_SEMA_OWNERSHIP_H |
14 | #define LLVM_CLANG_SEMA_OWNERSHIP_H |
15 | |
16 | #include "clang/AST/Expr.h" |
17 | #include "clang/Basic/LLVM.h" |
18 | #include "llvm/ADT/ArrayRef.h" |
19 | #include "llvm/Support/PointerLikeTypeTraits.h" |
20 | #include "llvm/Support/type_traits.h" |
21 | #include <cassert> |
22 | #include <cstddef> |
23 | #include <cstdint> |
24 | |
25 | //===----------------------------------------------------------------------===// |
26 | // OpaquePtr |
27 | //===----------------------------------------------------------------------===// |
28 | |
29 | namespace clang { |
30 | |
31 | class CXXBaseSpecifier; |
32 | class CXXCtorInitializer; |
33 | class Decl; |
34 | class Expr; |
35 | class ParsedTemplateArgument; |
36 | class QualType; |
37 | class Stmt; |
38 | class TemplateName; |
39 | class TemplateParameterList; |
40 | |
41 | /// Wrapper for void* pointer. |
42 | /// \tparam PtrTy Either a pointer type like 'T*' or a type that behaves like |
43 | /// a pointer. |
44 | /// |
45 | /// This is a very simple POD type that wraps a pointer that the Parser |
46 | /// doesn't know about but that Sema or another client does. The PtrTy |
47 | /// template argument is used to make sure that "Decl" pointers are not |
48 | /// compatible with "Type" pointers for example. |
49 | template <class PtrTy> |
50 | class OpaquePtr { |
51 | void *Ptr = nullptr; |
52 | |
53 | explicit OpaquePtr(void *Ptr) : Ptr(Ptr) {} |
54 | |
55 | using Traits = llvm::PointerLikeTypeTraits<PtrTy>; |
56 | |
57 | public: |
58 | OpaquePtr(std::nullptr_t = nullptr) {} |
59 | |
60 | static OpaquePtr make(PtrTy P) { OpaquePtr OP; OP.set(P); return OP; } |
61 | |
62 | /// Returns plain pointer to the entity pointed by this wrapper. |
63 | /// \tparam PointeeT Type of pointed entity. |
64 | /// |
65 | /// It is identical to getPtrAs<PointeeT*>. |
66 | template <typename PointeeT> PointeeT* getPtrTo() const { |
67 | return get(); |
68 | } |
69 | |
70 | /// Returns pointer converted to the specified type. |
71 | /// \tparam PtrT Result pointer type. There must be implicit conversion |
72 | /// from PtrTy to PtrT. |
73 | /// |
74 | /// In contrast to getPtrTo, this method allows the return type to be |
75 | /// a smart pointer. |
76 | template <typename PtrT> PtrT getPtrAs() const { |
77 | return get(); |
78 | } |
79 | |
80 | PtrTy get() const { |
81 | return Traits::getFromVoidPointer(Ptr); |
82 | } |
83 | |
84 | void set(PtrTy P) { |
85 | Ptr = Traits::getAsVoidPointer(P); |
86 | } |
87 | |
88 | explicit operator bool() const { return Ptr != nullptr; } |
89 | |
90 | void *getAsOpaquePtr() const { return Ptr; } |
91 | static OpaquePtr getFromOpaquePtr(void *P) { return OpaquePtr(P); } |
92 | }; |
93 | |
94 | /// UnionOpaquePtr - A version of OpaquePtr suitable for membership |
95 | /// in a union. |
96 | template <class T> struct UnionOpaquePtr { |
97 | void *Ptr; |
98 | |
99 | static UnionOpaquePtr make(OpaquePtr<T> P) { |
100 | UnionOpaquePtr OP = { P.getAsOpaquePtr() }; |
101 | return OP; |
102 | } |
103 | |
104 | OpaquePtr<T> get() const { return OpaquePtr<T>::getFromOpaquePtr(Ptr); } |
105 | operator OpaquePtr<T>() const { return get(); } |
106 | |
107 | UnionOpaquePtr &operator=(OpaquePtr<T> P) { |
108 | Ptr = P.getAsOpaquePtr(); |
109 | return *this; |
110 | } |
111 | }; |
112 | |
113 | } // namespace clang |
114 | |
115 | namespace llvm { |
116 | |
117 | template <class T> |
118 | struct PointerLikeTypeTraits<clang::OpaquePtr<T>> { |
119 | static constexpr int NumLowBitsAvailable = 0; |
120 | |
121 | static inline void *getAsVoidPointer(clang::OpaquePtr<T> P) { |
122 | // FIXME: Doesn't work? return P.getAs< void >(); |
123 | return P.getAsOpaquePtr(); |
124 | } |
125 | |
126 | static inline clang::OpaquePtr<T> getFromVoidPointer(void *P) { |
127 | return clang::OpaquePtr<T>::getFromOpaquePtr(P); |
128 | } |
129 | }; |
130 | |
131 | } // namespace llvm |
132 | |
133 | namespace clang { |
134 | |
135 | // Basic |
136 | class DiagnosticBuilder; |
137 | |
138 | // Determines whether the low bit of the result pointer for the |
139 | // given UID is always zero. If so, ActionResult will use that bit |
140 | // for it's "invalid" flag. |
141 | template<class Ptr> |
142 | struct IsResultPtrLowBitFree { |
143 | static const bool value = false; |
144 | }; |
145 | |
146 | /// ActionResult - This structure is used while parsing/acting on |
147 | /// expressions, stmts, etc. It encapsulates both the object returned by |
148 | /// the action, plus a sense of whether or not it is valid. |
149 | /// When CompressInvalid is true, the "invalid" flag will be |
150 | /// stored in the low bit of the Val pointer. |
151 | template<class PtrTy, |
152 | bool CompressInvalid = IsResultPtrLowBitFree<PtrTy>::value> |
153 | class ActionResult { |
154 | PtrTy Val; |
155 | bool Invalid; |
156 | |
157 | public: |
158 | ActionResult(bool Invalid = false) : Val(PtrTy()), Invalid(Invalid) {} |
159 | ActionResult(PtrTy val) : Val(val), Invalid(false) {} |
160 | ActionResult(const DiagnosticBuilder &) : Val(PtrTy()), Invalid(true) {} |
161 | |
162 | // These two overloads prevent void* -> bool conversions. |
163 | ActionResult(const void *) = delete; |
164 | ActionResult(volatile void *) = delete; |
165 | |
166 | bool isInvalid() const { return Invalid; } |
167 | bool isUsable() const { return !Invalid && Val; } |
168 | bool isUnset() const { return !Invalid && !Val; } |
169 | |
170 | PtrTy get() const { return Val; } |
171 | template <typename T> T *getAs() { return static_cast<T*>(get()); } |
172 | |
173 | void set(PtrTy V) { Val = V; } |
174 | |
175 | const ActionResult &operator=(PtrTy RHS) { |
176 | Val = RHS; |
177 | Invalid = false; |
178 | return *this; |
179 | } |
180 | }; |
181 | |
182 | // This ActionResult partial specialization places the "invalid" |
183 | // flag into the low bit of the pointer. |
184 | template<typename PtrTy> |
185 | class ActionResult<PtrTy, true> { |
186 | // A pointer whose low bit is 1 if this result is invalid, 0 |
187 | // otherwise. |
188 | uintptr_t PtrWithInvalid; |
189 | |
190 | using PtrTraits = llvm::PointerLikeTypeTraits<PtrTy>; |
191 | |
192 | public: |
193 | ActionResult(bool Invalid = false) |
194 | : PtrWithInvalid(static_cast<uintptr_t>(Invalid)) {} |
195 | |
196 | ActionResult(PtrTy V) { |
197 | void *VP = PtrTraits::getAsVoidPointer(V); |
198 | PtrWithInvalid = reinterpret_cast<uintptr_t>(VP); |
199 | assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer")(((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer" ) ? static_cast<void> (0) : __assert_fail ("(PtrWithInvalid & 0x01) == 0 && \"Badly aligned pointer\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/Sema/Ownership.h" , 199, __PRETTY_FUNCTION__)); |
200 | } |
201 | |
202 | ActionResult(const DiagnosticBuilder &) : PtrWithInvalid(0x01) {} |
203 | |
204 | // These two overloads prevent void* -> bool conversions. |
205 | ActionResult(const void *) = delete; |
206 | ActionResult(volatile void *) = delete; |
207 | |
208 | bool isInvalid() const { return PtrWithInvalid & 0x01; } |
209 | bool isUsable() const { return PtrWithInvalid > 0x01; } |
210 | bool isUnset() const { return PtrWithInvalid == 0; } |
211 | |
212 | PtrTy get() const { |
213 | void *VP = reinterpret_cast<void *>(PtrWithInvalid & ~0x01); |
214 | return PtrTraits::getFromVoidPointer(VP); |
215 | } |
216 | |
217 | template <typename T> T *getAs() { return static_cast<T*>(get()); } |
218 | |
219 | void set(PtrTy V) { |
220 | void *VP = PtrTraits::getAsVoidPointer(V); |
221 | PtrWithInvalid = reinterpret_cast<uintptr_t>(VP); |
222 | assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer")(((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer" ) ? static_cast<void> (0) : __assert_fail ("(PtrWithInvalid & 0x01) == 0 && \"Badly aligned pointer\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/Sema/Ownership.h" , 222, __PRETTY_FUNCTION__)); |
223 | } |
224 | |
225 | const ActionResult &operator=(PtrTy RHS) { |
226 | void *VP = PtrTraits::getAsVoidPointer(RHS); |
227 | PtrWithInvalid = reinterpret_cast<uintptr_t>(VP); |
228 | assert((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer")(((PtrWithInvalid & 0x01) == 0 && "Badly aligned pointer" ) ? static_cast<void> (0) : __assert_fail ("(PtrWithInvalid & 0x01) == 0 && \"Badly aligned pointer\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/Sema/Ownership.h" , 228, __PRETTY_FUNCTION__)); |
229 | return *this; |
230 | } |
231 | |
232 | // For types where we can fit a flag in with the pointer, provide |
233 | // conversions to/from pointer type. |
234 | static ActionResult getFromOpaquePointer(void *P) { |
235 | ActionResult Result; |
236 | Result.PtrWithInvalid = (uintptr_t)P; |
237 | return Result; |
238 | } |
239 | void *getAsOpaquePointer() const { return (void*)PtrWithInvalid; } |
240 | }; |
241 | |
242 | /// An opaque type for threading parsed type information through the |
243 | /// parser. |
244 | using ParsedType = OpaquePtr<QualType>; |
245 | using UnionParsedType = UnionOpaquePtr<QualType>; |
246 | |
247 | // We can re-use the low bit of expression, statement, base, and |
248 | // member-initializer pointers for the "invalid" flag of |
249 | // ActionResult. |
250 | template<> struct IsResultPtrLowBitFree<Expr*> { |
251 | static const bool value = true; |
252 | }; |
253 | template<> struct IsResultPtrLowBitFree<Stmt*> { |
254 | static const bool value = true; |
255 | }; |
256 | template<> struct IsResultPtrLowBitFree<CXXBaseSpecifier*> { |
257 | static const bool value = true; |
258 | }; |
259 | template<> struct IsResultPtrLowBitFree<CXXCtorInitializer*> { |
260 | static const bool value = true; |
261 | }; |
262 | |
263 | using ExprResult = ActionResult<Expr *>; |
264 | using StmtResult = ActionResult<Stmt *>; |
265 | using TypeResult = ActionResult<ParsedType>; |
266 | using BaseResult = ActionResult<CXXBaseSpecifier *>; |
267 | using MemInitResult = ActionResult<CXXCtorInitializer *>; |
268 | |
269 | using DeclResult = ActionResult<Decl *>; |
270 | using ParsedTemplateTy = OpaquePtr<TemplateName>; |
271 | using UnionParsedTemplateTy = UnionOpaquePtr<TemplateName>; |
272 | |
273 | using MultiExprArg = MutableArrayRef<Expr *>; |
274 | using MultiStmtArg = MutableArrayRef<Stmt *>; |
275 | using ASTTemplateArgsPtr = MutableArrayRef<ParsedTemplateArgument>; |
276 | using MultiTypeArg = MutableArrayRef<ParsedType>; |
277 | using MultiTemplateParamsArg = MutableArrayRef<TemplateParameterList *>; |
278 | |
279 | inline ExprResult ExprError() { return ExprResult(true); } |
280 | inline StmtResult StmtError() { return StmtResult(true); } |
281 | |
282 | inline ExprResult ExprError(const DiagnosticBuilder&) { return ExprError(); } |
283 | inline StmtResult StmtError(const DiagnosticBuilder&) { return StmtError(); } |
284 | |
285 | inline ExprResult ExprEmpty() { return ExprResult(false); } |
286 | inline StmtResult StmtEmpty() { return StmtResult(false); } |
287 | |
288 | inline Expr *AssertSuccess(ExprResult R) { |
289 | assert(!R.isInvalid() && "operation was asserted to never fail!")((!R.isInvalid() && "operation was asserted to never fail!" ) ? static_cast<void> (0) : __assert_fail ("!R.isInvalid() && \"operation was asserted to never fail!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/Sema/Ownership.h" , 289, __PRETTY_FUNCTION__)); |
290 | return R.get(); |
291 | } |
292 | |
293 | inline Stmt *AssertSuccess(StmtResult R) { |
294 | assert(!R.isInvalid() && "operation was asserted to never fail!")((!R.isInvalid() && "operation was asserted to never fail!" ) ? static_cast<void> (0) : __assert_fail ("!R.isInvalid() && \"operation was asserted to never fail!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/Sema/Ownership.h" , 294, __PRETTY_FUNCTION__)); |
295 | return R.get(); |
296 | } |
297 | |
298 | } // namespace clang |
299 | |
300 | #endif // LLVM_CLANG_SEMA_OWNERSHIP_H |
1 | //===- Type.h - C Language Family Type Representation -----------*- C++ -*-===// |
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 | // |
9 | /// \file |
10 | /// C Language Family Type Representation |
11 | /// |
12 | /// This file defines the clang::Type interface and subclasses, used to |
13 | /// represent types for languages in the C family. |
14 | // |
15 | //===----------------------------------------------------------------------===// |
16 | |
17 | #ifndef LLVM_CLANG_AST_TYPE_H |
18 | #define LLVM_CLANG_AST_TYPE_H |
19 | |
20 | #include "clang/AST/DependencyFlags.h" |
21 | #include "clang/AST/NestedNameSpecifier.h" |
22 | #include "clang/AST/TemplateName.h" |
23 | #include "clang/Basic/AddressSpaces.h" |
24 | #include "clang/Basic/AttrKinds.h" |
25 | #include "clang/Basic/Diagnostic.h" |
26 | #include "clang/Basic/ExceptionSpecificationType.h" |
27 | #include "clang/Basic/LLVM.h" |
28 | #include "clang/Basic/Linkage.h" |
29 | #include "clang/Basic/PartialDiagnostic.h" |
30 | #include "clang/Basic/SourceLocation.h" |
31 | #include "clang/Basic/Specifiers.h" |
32 | #include "clang/Basic/Visibility.h" |
33 | #include "llvm/ADT/APInt.h" |
34 | #include "llvm/ADT/APSInt.h" |
35 | #include "llvm/ADT/ArrayRef.h" |
36 | #include "llvm/ADT/FoldingSet.h" |
37 | #include "llvm/ADT/None.h" |
38 | #include "llvm/ADT/Optional.h" |
39 | #include "llvm/ADT/PointerIntPair.h" |
40 | #include "llvm/ADT/PointerUnion.h" |
41 | #include "llvm/ADT/StringRef.h" |
42 | #include "llvm/ADT/Twine.h" |
43 | #include "llvm/ADT/iterator_range.h" |
44 | #include "llvm/Support/Casting.h" |
45 | #include "llvm/Support/Compiler.h" |
46 | #include "llvm/Support/ErrorHandling.h" |
47 | #include "llvm/Support/PointerLikeTypeTraits.h" |
48 | #include "llvm/Support/TrailingObjects.h" |
49 | #include "llvm/Support/type_traits.h" |
50 | #include <cassert> |
51 | #include <cstddef> |
52 | #include <cstdint> |
53 | #include <cstring> |
54 | #include <string> |
55 | #include <type_traits> |
56 | #include <utility> |
57 | |
58 | namespace clang { |
59 | |
60 | class ExtQuals; |
61 | class QualType; |
62 | class ConceptDecl; |
63 | class TagDecl; |
64 | class Type; |
65 | |
66 | enum { |
67 | TypeAlignmentInBits = 4, |
68 | TypeAlignment = 1 << TypeAlignmentInBits |
69 | }; |
70 | |
71 | namespace serialization { |
72 | template <class T> class AbstractTypeReader; |
73 | template <class T> class AbstractTypeWriter; |
74 | } |
75 | |
76 | } // namespace clang |
77 | |
78 | namespace llvm { |
79 | |
80 | template <typename T> |
81 | struct PointerLikeTypeTraits; |
82 | template<> |
83 | struct PointerLikeTypeTraits< ::clang::Type*> { |
84 | static inline void *getAsVoidPointer(::clang::Type *P) { return P; } |
85 | |
86 | static inline ::clang::Type *getFromVoidPointer(void *P) { |
87 | return static_cast< ::clang::Type*>(P); |
88 | } |
89 | |
90 | static constexpr int NumLowBitsAvailable = clang::TypeAlignmentInBits; |
91 | }; |
92 | |
93 | template<> |
94 | struct PointerLikeTypeTraits< ::clang::ExtQuals*> { |
95 | static inline void *getAsVoidPointer(::clang::ExtQuals *P) { return P; } |
96 | |
97 | static inline ::clang::ExtQuals *getFromVoidPointer(void *P) { |
98 | return static_cast< ::clang::ExtQuals*>(P); |
99 | } |
100 | |
101 | static constexpr int NumLowBitsAvailable = clang::TypeAlignmentInBits; |
102 | }; |
103 | |
104 | } // namespace llvm |
105 | |
106 | namespace clang { |
107 | |
108 | class ASTContext; |
109 | template <typename> class CanQual; |
110 | class CXXRecordDecl; |
111 | class DeclContext; |
112 | class EnumDecl; |
113 | class Expr; |
114 | class ExtQualsTypeCommonBase; |
115 | class FunctionDecl; |
116 | class IdentifierInfo; |
117 | class NamedDecl; |
118 | class ObjCInterfaceDecl; |
119 | class ObjCProtocolDecl; |
120 | class ObjCTypeParamDecl; |
121 | struct PrintingPolicy; |
122 | class RecordDecl; |
123 | class Stmt; |
124 | class TagDecl; |
125 | class TemplateArgument; |
126 | class TemplateArgumentListInfo; |
127 | class TemplateArgumentLoc; |
128 | class TemplateTypeParmDecl; |
129 | class TypedefNameDecl; |
130 | class UnresolvedUsingTypenameDecl; |
131 | |
132 | using CanQualType = CanQual<Type>; |
133 | |
134 | // Provide forward declarations for all of the *Type classes. |
135 | #define TYPE(Class, Base) class Class##Type; |
136 | #include "clang/AST/TypeNodes.inc" |
137 | |
138 | /// The collection of all-type qualifiers we support. |
139 | /// Clang supports five independent qualifiers: |
140 | /// * C99: const, volatile, and restrict |
141 | /// * MS: __unaligned |
142 | /// * Embedded C (TR18037): address spaces |
143 | /// * Objective C: the GC attributes (none, weak, or strong) |
144 | class Qualifiers { |
145 | public: |
146 | enum TQ { // NOTE: These flags must be kept in sync with DeclSpec::TQ. |
147 | Const = 0x1, |
148 | Restrict = 0x2, |
149 | Volatile = 0x4, |
150 | CVRMask = Const | Volatile | Restrict |
151 | }; |
152 | |
153 | enum GC { |
154 | GCNone = 0, |
155 | Weak, |
156 | Strong |
157 | }; |
158 | |
159 | enum ObjCLifetime { |
160 | /// There is no lifetime qualification on this type. |
161 | OCL_None, |
162 | |
163 | /// This object can be modified without requiring retains or |
164 | /// releases. |
165 | OCL_ExplicitNone, |
166 | |
167 | /// Assigning into this object requires the old value to be |
168 | /// released and the new value to be retained. The timing of the |
169 | /// release of the old value is inexact: it may be moved to |
170 | /// immediately after the last known point where the value is |
171 | /// live. |
172 | OCL_Strong, |
173 | |
174 | /// Reading or writing from this object requires a barrier call. |
175 | OCL_Weak, |
176 | |
177 | /// Assigning into this object requires a lifetime extension. |
178 | OCL_Autoreleasing |
179 | }; |
180 | |
181 | enum { |
182 | /// The maximum supported address space number. |
183 | /// 23 bits should be enough for anyone. |
184 | MaxAddressSpace = 0x7fffffu, |
185 | |
186 | /// The width of the "fast" qualifier mask. |
187 | FastWidth = 3, |
188 | |
189 | /// The fast qualifier mask. |
190 | FastMask = (1 << FastWidth) - 1 |
191 | }; |
192 | |
193 | /// Returns the common set of qualifiers while removing them from |
194 | /// the given sets. |
195 | static Qualifiers removeCommonQualifiers(Qualifiers &L, Qualifiers &R) { |
196 | // If both are only CVR-qualified, bit operations are sufficient. |
197 | if (!(L.Mask & ~CVRMask) && !(R.Mask & ~CVRMask)) { |
198 | Qualifiers Q; |
199 | Q.Mask = L.Mask & R.Mask; |
200 | L.Mask &= ~Q.Mask; |
201 | R.Mask &= ~Q.Mask; |
202 | return Q; |
203 | } |
204 | |
205 | Qualifiers Q; |
206 | unsigned CommonCRV = L.getCVRQualifiers() & R.getCVRQualifiers(); |
207 | Q.addCVRQualifiers(CommonCRV); |
208 | L.removeCVRQualifiers(CommonCRV); |
209 | R.removeCVRQualifiers(CommonCRV); |
210 | |
211 | if (L.getObjCGCAttr() == R.getObjCGCAttr()) { |
212 | Q.setObjCGCAttr(L.getObjCGCAttr()); |
213 | L.removeObjCGCAttr(); |
214 | R.removeObjCGCAttr(); |
215 | } |
216 | |
217 | if (L.getObjCLifetime() == R.getObjCLifetime()) { |
218 | Q.setObjCLifetime(L.getObjCLifetime()); |
219 | L.removeObjCLifetime(); |
220 | R.removeObjCLifetime(); |
221 | } |
222 | |
223 | if (L.getAddressSpace() == R.getAddressSpace()) { |
224 | Q.setAddressSpace(L.getAddressSpace()); |
225 | L.removeAddressSpace(); |
226 | R.removeAddressSpace(); |
227 | } |
228 | return Q; |
229 | } |
230 | |
231 | static Qualifiers fromFastMask(unsigned Mask) { |
232 | Qualifiers Qs; |
233 | Qs.addFastQualifiers(Mask); |
234 | return Qs; |
235 | } |
236 | |
237 | static Qualifiers fromCVRMask(unsigned CVR) { |
238 | Qualifiers Qs; |
239 | Qs.addCVRQualifiers(CVR); |
240 | return Qs; |
241 | } |
242 | |
243 | static Qualifiers fromCVRUMask(unsigned CVRU) { |
244 | Qualifiers Qs; |
245 | Qs.addCVRUQualifiers(CVRU); |
246 | return Qs; |
247 | } |
248 | |
249 | // Deserialize qualifiers from an opaque representation. |
250 | static Qualifiers fromOpaqueValue(unsigned opaque) { |
251 | Qualifiers Qs; |
252 | Qs.Mask = opaque; |
253 | return Qs; |
254 | } |
255 | |
256 | // Serialize these qualifiers into an opaque representation. |
257 | unsigned getAsOpaqueValue() const { |
258 | return Mask; |
259 | } |
260 | |
261 | bool hasConst() const { return Mask & Const; } |
262 | bool hasOnlyConst() const { return Mask == Const; } |
263 | void removeConst() { Mask &= ~Const; } |
264 | void addConst() { Mask |= Const; } |
265 | |
266 | bool hasVolatile() const { return Mask & Volatile; } |
267 | bool hasOnlyVolatile() const { return Mask == Volatile; } |
268 | void removeVolatile() { Mask &= ~Volatile; } |
269 | void addVolatile() { Mask |= Volatile; } |
270 | |
271 | bool hasRestrict() const { return Mask & Restrict; } |
272 | bool hasOnlyRestrict() const { return Mask == Restrict; } |
273 | void removeRestrict() { Mask &= ~Restrict; } |
274 | void addRestrict() { Mask |= Restrict; } |
275 | |
276 | bool hasCVRQualifiers() const { return getCVRQualifiers(); } |
277 | unsigned getCVRQualifiers() const { return Mask & CVRMask; } |
278 | unsigned getCVRUQualifiers() const { return Mask & (CVRMask | UMask); } |
279 | |
280 | void setCVRQualifiers(unsigned mask) { |
281 | assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits")((!(mask & ~CVRMask) && "bitmask contains non-CVR bits" ) ? static_cast<void> (0) : __assert_fail ("!(mask & ~CVRMask) && \"bitmask contains non-CVR bits\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 281, __PRETTY_FUNCTION__)); |
282 | Mask = (Mask & ~CVRMask) | mask; |
283 | } |
284 | void removeCVRQualifiers(unsigned mask) { |
285 | assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits")((!(mask & ~CVRMask) && "bitmask contains non-CVR bits" ) ? static_cast<void> (0) : __assert_fail ("!(mask & ~CVRMask) && \"bitmask contains non-CVR bits\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 285, __PRETTY_FUNCTION__)); |
286 | Mask &= ~mask; |
287 | } |
288 | void removeCVRQualifiers() { |
289 | removeCVRQualifiers(CVRMask); |
290 | } |
291 | void addCVRQualifiers(unsigned mask) { |
292 | assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits")((!(mask & ~CVRMask) && "bitmask contains non-CVR bits" ) ? static_cast<void> (0) : __assert_fail ("!(mask & ~CVRMask) && \"bitmask contains non-CVR bits\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 292, __PRETTY_FUNCTION__)); |
293 | Mask |= mask; |
294 | } |
295 | void addCVRUQualifiers(unsigned mask) { |
296 | assert(!(mask & ~CVRMask & ~UMask) && "bitmask contains non-CVRU bits")((!(mask & ~CVRMask & ~UMask) && "bitmask contains non-CVRU bits" ) ? static_cast<void> (0) : __assert_fail ("!(mask & ~CVRMask & ~UMask) && \"bitmask contains non-CVRU bits\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 296, __PRETTY_FUNCTION__)); |
297 | Mask |= mask; |
298 | } |
299 | |
300 | bool hasUnaligned() const { return Mask & UMask; } |
301 | void setUnaligned(bool flag) { |
302 | Mask = (Mask & ~UMask) | (flag ? UMask : 0); |
303 | } |
304 | void removeUnaligned() { Mask &= ~UMask; } |
305 | void addUnaligned() { Mask |= UMask; } |
306 | |
307 | bool hasObjCGCAttr() const { return Mask & GCAttrMask; } |
308 | GC getObjCGCAttr() const { return GC((Mask & GCAttrMask) >> GCAttrShift); } |
309 | void setObjCGCAttr(GC type) { |
310 | Mask = (Mask & ~GCAttrMask) | (type << GCAttrShift); |
311 | } |
312 | void removeObjCGCAttr() { setObjCGCAttr(GCNone); } |
313 | void addObjCGCAttr(GC type) { |
314 | assert(type)((type) ? static_cast<void> (0) : __assert_fail ("type" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 314, __PRETTY_FUNCTION__)); |
315 | setObjCGCAttr(type); |
316 | } |
317 | Qualifiers withoutObjCGCAttr() const { |
318 | Qualifiers qs = *this; |
319 | qs.removeObjCGCAttr(); |
320 | return qs; |
321 | } |
322 | Qualifiers withoutObjCLifetime() const { |
323 | Qualifiers qs = *this; |
324 | qs.removeObjCLifetime(); |
325 | return qs; |
326 | } |
327 | Qualifiers withoutAddressSpace() const { |
328 | Qualifiers qs = *this; |
329 | qs.removeAddressSpace(); |
330 | return qs; |
331 | } |
332 | |
333 | bool hasObjCLifetime() const { return Mask & LifetimeMask; } |
334 | ObjCLifetime getObjCLifetime() const { |
335 | return ObjCLifetime((Mask & LifetimeMask) >> LifetimeShift); |
336 | } |
337 | void setObjCLifetime(ObjCLifetime type) { |
338 | Mask = (Mask & ~LifetimeMask) | (type << LifetimeShift); |
339 | } |
340 | void removeObjCLifetime() { setObjCLifetime(OCL_None); } |
341 | void addObjCLifetime(ObjCLifetime type) { |
342 | assert(type)((type) ? static_cast<void> (0) : __assert_fail ("type" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 342, __PRETTY_FUNCTION__)); |
343 | assert(!hasObjCLifetime())((!hasObjCLifetime()) ? static_cast<void> (0) : __assert_fail ("!hasObjCLifetime()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 343, __PRETTY_FUNCTION__)); |
344 | Mask |= (type << LifetimeShift); |
345 | } |
346 | |
347 | /// True if the lifetime is neither None or ExplicitNone. |
348 | bool hasNonTrivialObjCLifetime() const { |
349 | ObjCLifetime lifetime = getObjCLifetime(); |
350 | return (lifetime > OCL_ExplicitNone); |
351 | } |
352 | |
353 | /// True if the lifetime is either strong or weak. |
354 | bool hasStrongOrWeakObjCLifetime() const { |
355 | ObjCLifetime lifetime = getObjCLifetime(); |
356 | return (lifetime == OCL_Strong || lifetime == OCL_Weak); |
357 | } |
358 | |
359 | bool hasAddressSpace() const { return Mask & AddressSpaceMask; } |
360 | LangAS getAddressSpace() const { |
361 | return static_cast<LangAS>(Mask >> AddressSpaceShift); |
362 | } |
363 | bool hasTargetSpecificAddressSpace() const { |
364 | return isTargetAddressSpace(getAddressSpace()); |
365 | } |
366 | /// Get the address space attribute value to be printed by diagnostics. |
367 | unsigned getAddressSpaceAttributePrintValue() const { |
368 | auto Addr = getAddressSpace(); |
369 | // This function is not supposed to be used with language specific |
370 | // address spaces. If that happens, the diagnostic message should consider |
371 | // printing the QualType instead of the address space value. |
372 | assert(Addr == LangAS::Default || hasTargetSpecificAddressSpace())((Addr == LangAS::Default || hasTargetSpecificAddressSpace()) ? static_cast<void> (0) : __assert_fail ("Addr == LangAS::Default || hasTargetSpecificAddressSpace()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 372, __PRETTY_FUNCTION__)); |
373 | if (Addr != LangAS::Default) |
374 | return toTargetAddressSpace(Addr); |
375 | // TODO: The diagnostic messages where Addr may be 0 should be fixed |
376 | // since it cannot differentiate the situation where 0 denotes the default |
377 | // address space or user specified __attribute__((address_space(0))). |
378 | return 0; |
379 | } |
380 | void setAddressSpace(LangAS space) { |
381 | assert((unsigned)space <= MaxAddressSpace)(((unsigned)space <= MaxAddressSpace) ? static_cast<void > (0) : __assert_fail ("(unsigned)space <= MaxAddressSpace" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 381, __PRETTY_FUNCTION__)); |
382 | Mask = (Mask & ~AddressSpaceMask) |
383 | | (((uint32_t) space) << AddressSpaceShift); |
384 | } |
385 | void removeAddressSpace() { setAddressSpace(LangAS::Default); } |
386 | void addAddressSpace(LangAS space) { |
387 | assert(space != LangAS::Default)((space != LangAS::Default) ? static_cast<void> (0) : __assert_fail ("space != LangAS::Default", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 387, __PRETTY_FUNCTION__)); |
388 | setAddressSpace(space); |
389 | } |
390 | |
391 | // Fast qualifiers are those that can be allocated directly |
392 | // on a QualType object. |
393 | bool hasFastQualifiers() const { return getFastQualifiers(); } |
394 | unsigned getFastQualifiers() const { return Mask & FastMask; } |
395 | void setFastQualifiers(unsigned mask) { |
396 | assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits")((!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits" ) ? static_cast<void> (0) : __assert_fail ("!(mask & ~FastMask) && \"bitmask contains non-fast qualifier bits\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 396, __PRETTY_FUNCTION__)); |
397 | Mask = (Mask & ~FastMask) | mask; |
398 | } |
399 | void removeFastQualifiers(unsigned mask) { |
400 | assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits")((!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits" ) ? static_cast<void> (0) : __assert_fail ("!(mask & ~FastMask) && \"bitmask contains non-fast qualifier bits\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 400, __PRETTY_FUNCTION__)); |
401 | Mask &= ~mask; |
402 | } |
403 | void removeFastQualifiers() { |
404 | removeFastQualifiers(FastMask); |
405 | } |
406 | void addFastQualifiers(unsigned mask) { |
407 | assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits")((!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits" ) ? static_cast<void> (0) : __assert_fail ("!(mask & ~FastMask) && \"bitmask contains non-fast qualifier bits\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 407, __PRETTY_FUNCTION__)); |
408 | Mask |= mask; |
409 | } |
410 | |
411 | /// Return true if the set contains any qualifiers which require an ExtQuals |
412 | /// node to be allocated. |
413 | bool hasNonFastQualifiers() const { return Mask & ~FastMask; } |
414 | Qualifiers getNonFastQualifiers() const { |
415 | Qualifiers Quals = *this; |
416 | Quals.setFastQualifiers(0); |
417 | return Quals; |
418 | } |
419 | |
420 | /// Return true if the set contains any qualifiers. |
421 | bool hasQualifiers() const { return Mask; } |
422 | bool empty() const { return !Mask; } |
423 | |
424 | /// Add the qualifiers from the given set to this set. |
425 | void addQualifiers(Qualifiers Q) { |
426 | // If the other set doesn't have any non-boolean qualifiers, just |
427 | // bit-or it in. |
428 | if (!(Q.Mask & ~CVRMask)) |
429 | Mask |= Q.Mask; |
430 | else { |
431 | Mask |= (Q.Mask & CVRMask); |
432 | if (Q.hasAddressSpace()) |
433 | addAddressSpace(Q.getAddressSpace()); |
434 | if (Q.hasObjCGCAttr()) |
435 | addObjCGCAttr(Q.getObjCGCAttr()); |
436 | if (Q.hasObjCLifetime()) |
437 | addObjCLifetime(Q.getObjCLifetime()); |
438 | } |
439 | } |
440 | |
441 | /// Remove the qualifiers from the given set from this set. |
442 | void removeQualifiers(Qualifiers Q) { |
443 | // If the other set doesn't have any non-boolean qualifiers, just |
444 | // bit-and the inverse in. |
445 | if (!(Q.Mask & ~CVRMask)) |
446 | Mask &= ~Q.Mask; |
447 | else { |
448 | Mask &= ~(Q.Mask & CVRMask); |
449 | if (getObjCGCAttr() == Q.getObjCGCAttr()) |
450 | removeObjCGCAttr(); |
451 | if (getObjCLifetime() == Q.getObjCLifetime()) |
452 | removeObjCLifetime(); |
453 | if (getAddressSpace() == Q.getAddressSpace()) |
454 | removeAddressSpace(); |
455 | } |
456 | } |
457 | |
458 | /// Add the qualifiers from the given set to this set, given that |
459 | /// they don't conflict. |
460 | void addConsistentQualifiers(Qualifiers qs) { |
461 | assert(getAddressSpace() == qs.getAddressSpace() ||((getAddressSpace() == qs.getAddressSpace() || !hasAddressSpace () || !qs.hasAddressSpace()) ? static_cast<void> (0) : __assert_fail ("getAddressSpace() == qs.getAddressSpace() || !hasAddressSpace() || !qs.hasAddressSpace()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 462, __PRETTY_FUNCTION__)) |
462 | !hasAddressSpace() || !qs.hasAddressSpace())((getAddressSpace() == qs.getAddressSpace() || !hasAddressSpace () || !qs.hasAddressSpace()) ? static_cast<void> (0) : __assert_fail ("getAddressSpace() == qs.getAddressSpace() || !hasAddressSpace() || !qs.hasAddressSpace()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 462, __PRETTY_FUNCTION__)); |
463 | assert(getObjCGCAttr() == qs.getObjCGCAttr() ||((getObjCGCAttr() == qs.getObjCGCAttr() || !hasObjCGCAttr() || !qs.hasObjCGCAttr()) ? static_cast<void> (0) : __assert_fail ("getObjCGCAttr() == qs.getObjCGCAttr() || !hasObjCGCAttr() || !qs.hasObjCGCAttr()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 464, __PRETTY_FUNCTION__)) |
464 | !hasObjCGCAttr() || !qs.hasObjCGCAttr())((getObjCGCAttr() == qs.getObjCGCAttr() || !hasObjCGCAttr() || !qs.hasObjCGCAttr()) ? static_cast<void> (0) : __assert_fail ("getObjCGCAttr() == qs.getObjCGCAttr() || !hasObjCGCAttr() || !qs.hasObjCGCAttr()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 464, __PRETTY_FUNCTION__)); |
465 | assert(getObjCLifetime() == qs.getObjCLifetime() ||((getObjCLifetime() == qs.getObjCLifetime() || !hasObjCLifetime () || !qs.hasObjCLifetime()) ? static_cast<void> (0) : __assert_fail ("getObjCLifetime() == qs.getObjCLifetime() || !hasObjCLifetime() || !qs.hasObjCLifetime()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 466, __PRETTY_FUNCTION__)) |
466 | !hasObjCLifetime() || !qs.hasObjCLifetime())((getObjCLifetime() == qs.getObjCLifetime() || !hasObjCLifetime () || !qs.hasObjCLifetime()) ? static_cast<void> (0) : __assert_fail ("getObjCLifetime() == qs.getObjCLifetime() || !hasObjCLifetime() || !qs.hasObjCLifetime()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 466, __PRETTY_FUNCTION__)); |
467 | Mask |= qs.Mask; |
468 | } |
469 | |
470 | /// Returns true if address space A is equal to or a superset of B. |
471 | /// OpenCL v2.0 defines conversion rules (OpenCLC v2.0 s6.5.5) and notion of |
472 | /// overlapping address spaces. |
473 | /// CL1.1 or CL1.2: |
474 | /// every address space is a superset of itself. |
475 | /// CL2.0 adds: |
476 | /// __generic is a superset of any address space except for __constant. |
477 | static bool isAddressSpaceSupersetOf(LangAS A, LangAS B) { |
478 | // Address spaces must match exactly. |
479 | return A == B || |
480 | // Otherwise in OpenCLC v2.0 s6.5.5: every address space except |
481 | // for __constant can be used as __generic. |
482 | (A == LangAS::opencl_generic && B != LangAS::opencl_constant) || |
483 | // Consider pointer size address spaces to be equivalent to default. |
484 | ((isPtrSizeAddressSpace(A) || A == LangAS::Default) && |
485 | (isPtrSizeAddressSpace(B) || B == LangAS::Default)); |
486 | } |
487 | |
488 | /// Returns true if the address space in these qualifiers is equal to or |
489 | /// a superset of the address space in the argument qualifiers. |
490 | bool isAddressSpaceSupersetOf(Qualifiers other) const { |
491 | return isAddressSpaceSupersetOf(getAddressSpace(), other.getAddressSpace()); |
492 | } |
493 | |
494 | /// Determines if these qualifiers compatibly include another set. |
495 | /// Generally this answers the question of whether an object with the other |
496 | /// qualifiers can be safely used as an object with these qualifiers. |
497 | bool compatiblyIncludes(Qualifiers other) const { |
498 | return isAddressSpaceSupersetOf(other) && |
499 | // ObjC GC qualifiers can match, be added, or be removed, but can't |
500 | // be changed. |
501 | (getObjCGCAttr() == other.getObjCGCAttr() || !hasObjCGCAttr() || |
502 | !other.hasObjCGCAttr()) && |
503 | // ObjC lifetime qualifiers must match exactly. |
504 | getObjCLifetime() == other.getObjCLifetime() && |
505 | // CVR qualifiers may subset. |
506 | (((Mask & CVRMask) | (other.Mask & CVRMask)) == (Mask & CVRMask)) && |
507 | // U qualifier may superset. |
508 | (!other.hasUnaligned() || hasUnaligned()); |
509 | } |
510 | |
511 | /// Determines if these qualifiers compatibly include another set of |
512 | /// qualifiers from the narrow perspective of Objective-C ARC lifetime. |
513 | /// |
514 | /// One set of Objective-C lifetime qualifiers compatibly includes the other |
515 | /// if the lifetime qualifiers match, or if both are non-__weak and the |
516 | /// including set also contains the 'const' qualifier, or both are non-__weak |
517 | /// and one is None (which can only happen in non-ARC modes). |
518 | bool compatiblyIncludesObjCLifetime(Qualifiers other) const { |
519 | if (getObjCLifetime() == other.getObjCLifetime()) |
520 | return true; |
521 | |
522 | if (getObjCLifetime() == OCL_Weak || other.getObjCLifetime() == OCL_Weak) |
523 | return false; |
524 | |
525 | if (getObjCLifetime() == OCL_None || other.getObjCLifetime() == OCL_None) |
526 | return true; |
527 | |
528 | return hasConst(); |
529 | } |
530 | |
531 | /// Determine whether this set of qualifiers is a strict superset of |
532 | /// another set of qualifiers, not considering qualifier compatibility. |
533 | bool isStrictSupersetOf(Qualifiers Other) const; |
534 | |
535 | bool operator==(Qualifiers Other) const { return Mask == Other.Mask; } |
536 | bool operator!=(Qualifiers Other) const { return Mask != Other.Mask; } |
537 | |
538 | explicit operator bool() const { return hasQualifiers(); } |
539 | |
540 | Qualifiers &operator+=(Qualifiers R) { |
541 | addQualifiers(R); |
542 | return *this; |
543 | } |
544 | |
545 | // Union two qualifier sets. If an enumerated qualifier appears |
546 | // in both sets, use the one from the right. |
547 | friend Qualifiers operator+(Qualifiers L, Qualifiers R) { |
548 | L += R; |
549 | return L; |
550 | } |
551 | |
552 | Qualifiers &operator-=(Qualifiers R) { |
553 | removeQualifiers(R); |
554 | return *this; |
555 | } |
556 | |
557 | /// Compute the difference between two qualifier sets. |
558 | friend Qualifiers operator-(Qualifiers L, Qualifiers R) { |
559 | L -= R; |
560 | return L; |
561 | } |
562 | |
563 | std::string getAsString() const; |
564 | std::string getAsString(const PrintingPolicy &Policy) const; |
565 | |
566 | static std::string getAddrSpaceAsString(LangAS AS); |
567 | |
568 | bool isEmptyWhenPrinted(const PrintingPolicy &Policy) const; |
569 | void print(raw_ostream &OS, const PrintingPolicy &Policy, |
570 | bool appendSpaceIfNonEmpty = false) const; |
571 | |
572 | void Profile(llvm::FoldingSetNodeID &ID) const { |
573 | ID.AddInteger(Mask); |
574 | } |
575 | |
576 | private: |
577 | // bits: |0 1 2|3|4 .. 5|6 .. 8|9 ... 31| |
578 | // |C R V|U|GCAttr|Lifetime|AddressSpace| |
579 | uint32_t Mask = 0; |
580 | |
581 | static const uint32_t UMask = 0x8; |
582 | static const uint32_t UShift = 3; |
583 | static const uint32_t GCAttrMask = 0x30; |
584 | static const uint32_t GCAttrShift = 4; |
585 | static const uint32_t LifetimeMask = 0x1C0; |
586 | static const uint32_t LifetimeShift = 6; |
587 | static const uint32_t AddressSpaceMask = |
588 | ~(CVRMask | UMask | GCAttrMask | LifetimeMask); |
589 | static const uint32_t AddressSpaceShift = 9; |
590 | }; |
591 | |
592 | /// A std::pair-like structure for storing a qualified type split |
593 | /// into its local qualifiers and its locally-unqualified type. |
594 | struct SplitQualType { |
595 | /// The locally-unqualified type. |
596 | const Type *Ty = nullptr; |
597 | |
598 | /// The local qualifiers. |
599 | Qualifiers Quals; |
600 | |
601 | SplitQualType() = default; |
602 | SplitQualType(const Type *ty, Qualifiers qs) : Ty(ty), Quals(qs) {} |
603 | |
604 | SplitQualType getSingleStepDesugaredType() const; // end of this file |
605 | |
606 | // Make std::tie work. |
607 | std::pair<const Type *,Qualifiers> asPair() const { |
608 | return std::pair<const Type *, Qualifiers>(Ty, Quals); |
609 | } |
610 | |
611 | friend bool operator==(SplitQualType a, SplitQualType b) { |
612 | return a.Ty == b.Ty && a.Quals == b.Quals; |
613 | } |
614 | friend bool operator!=(SplitQualType a, SplitQualType b) { |
615 | return a.Ty != b.Ty || a.Quals != b.Quals; |
616 | } |
617 | }; |
618 | |
619 | /// The kind of type we are substituting Objective-C type arguments into. |
620 | /// |
621 | /// The kind of substitution affects the replacement of type parameters when |
622 | /// no concrete type information is provided, e.g., when dealing with an |
623 | /// unspecialized type. |
624 | enum class ObjCSubstitutionContext { |
625 | /// An ordinary type. |
626 | Ordinary, |
627 | |
628 | /// The result type of a method or function. |
629 | Result, |
630 | |
631 | /// The parameter type of a method or function. |
632 | Parameter, |
633 | |
634 | /// The type of a property. |
635 | Property, |
636 | |
637 | /// The superclass of a type. |
638 | Superclass, |
639 | }; |
640 | |
641 | /// A (possibly-)qualified type. |
642 | /// |
643 | /// For efficiency, we don't store CV-qualified types as nodes on their |
644 | /// own: instead each reference to a type stores the qualifiers. This |
645 | /// greatly reduces the number of nodes we need to allocate for types (for |
646 | /// example we only need one for 'int', 'const int', 'volatile int', |
647 | /// 'const volatile int', etc). |
648 | /// |
649 | /// As an added efficiency bonus, instead of making this a pair, we |
650 | /// just store the two bits we care about in the low bits of the |
651 | /// pointer. To handle the packing/unpacking, we make QualType be a |
652 | /// simple wrapper class that acts like a smart pointer. A third bit |
653 | /// indicates whether there are extended qualifiers present, in which |
654 | /// case the pointer points to a special structure. |
655 | class QualType { |
656 | friend class QualifierCollector; |
657 | |
658 | // Thankfully, these are efficiently composable. |
659 | llvm::PointerIntPair<llvm::PointerUnion<const Type *, const ExtQuals *>, |
660 | Qualifiers::FastWidth> Value; |
661 | |
662 | const ExtQuals *getExtQualsUnsafe() const { |
663 | return Value.getPointer().get<const ExtQuals*>(); |
664 | } |
665 | |
666 | const Type *getTypePtrUnsafe() const { |
667 | return Value.getPointer().get<const Type*>(); |
668 | } |
669 | |
670 | const ExtQualsTypeCommonBase *getCommonPtr() const { |
671 | assert(!isNull() && "Cannot retrieve a NULL type pointer")((!isNull() && "Cannot retrieve a NULL type pointer") ? static_cast<void> (0) : __assert_fail ("!isNull() && \"Cannot retrieve a NULL type pointer\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 671, __PRETTY_FUNCTION__)); |
672 | auto CommonPtrVal = reinterpret_cast<uintptr_t>(Value.getOpaqueValue()); |
673 | CommonPtrVal &= ~(uintptr_t)((1 << TypeAlignmentInBits) - 1); |
674 | return reinterpret_cast<ExtQualsTypeCommonBase*>(CommonPtrVal); |
675 | } |
676 | |
677 | public: |
678 | QualType() = default; |
679 | QualType(const Type *Ptr, unsigned Quals) : Value(Ptr, Quals) {} |
680 | QualType(const ExtQuals *Ptr, unsigned Quals) : Value(Ptr, Quals) {} |
681 | |
682 | unsigned getLocalFastQualifiers() const { return Value.getInt(); } |
683 | void setLocalFastQualifiers(unsigned Quals) { Value.setInt(Quals); } |
684 | |
685 | /// Retrieves a pointer to the underlying (unqualified) type. |
686 | /// |
687 | /// This function requires that the type not be NULL. If the type might be |
688 | /// NULL, use the (slightly less efficient) \c getTypePtrOrNull(). |
689 | const Type *getTypePtr() const; |
690 | |
691 | const Type *getTypePtrOrNull() const; |
692 | |
693 | /// Retrieves a pointer to the name of the base type. |
694 | const IdentifierInfo *getBaseTypeIdentifier() const; |
695 | |
696 | /// Divides a QualType into its unqualified type and a set of local |
697 | /// qualifiers. |
698 | SplitQualType split() const; |
699 | |
700 | void *getAsOpaquePtr() const { return Value.getOpaqueValue(); } |
701 | |
702 | static QualType getFromOpaquePtr(const void *Ptr) { |
703 | QualType T; |
704 | T.Value.setFromOpaqueValue(const_cast<void*>(Ptr)); |
705 | return T; |
706 | } |
707 | |
708 | const Type &operator*() const { |
709 | return *getTypePtr(); |
710 | } |
711 | |
712 | const Type *operator->() const { |
713 | return getTypePtr(); |
714 | } |
715 | |
716 | bool isCanonical() const; |
717 | bool isCanonicalAsParam() const; |
718 | |
719 | /// Return true if this QualType doesn't point to a type yet. |
720 | bool isNull() const { |
721 | return Value.getPointer().isNull(); |
722 | } |
723 | |
724 | /// Determine whether this particular QualType instance has the |
725 | /// "const" qualifier set, without looking through typedefs that may have |
726 | /// added "const" at a different level. |
727 | bool isLocalConstQualified() const { |
728 | return (getLocalFastQualifiers() & Qualifiers::Const); |
729 | } |
730 | |
731 | /// Determine whether this type is const-qualified. |
732 | bool isConstQualified() const; |
733 | |
734 | /// Determine whether this particular QualType instance has the |
735 | /// "restrict" qualifier set, without looking through typedefs that may have |
736 | /// added "restrict" at a different level. |
737 | bool isLocalRestrictQualified() const { |
738 | return (getLocalFastQualifiers() & Qualifiers::Restrict); |
739 | } |
740 | |
741 | /// Determine whether this type is restrict-qualified. |
742 | bool isRestrictQualified() const; |
743 | |
744 | /// Determine whether this particular QualType instance has the |
745 | /// "volatile" qualifier set, without looking through typedefs that may have |
746 | /// added "volatile" at a different level. |
747 | bool isLocalVolatileQualified() const { |
748 | return (getLocalFastQualifiers() & Qualifiers::Volatile); |
749 | } |
750 | |
751 | /// Determine whether this type is volatile-qualified. |
752 | bool isVolatileQualified() const; |
753 | |
754 | /// Determine whether this particular QualType instance has any |
755 | /// qualifiers, without looking through any typedefs that might add |
756 | /// qualifiers at a different level. |
757 | bool hasLocalQualifiers() const { |
758 | return getLocalFastQualifiers() || hasLocalNonFastQualifiers(); |
759 | } |
760 | |
761 | /// Determine whether this type has any qualifiers. |
762 | bool hasQualifiers() const; |
763 | |
764 | /// Determine whether this particular QualType instance has any |
765 | /// "non-fast" qualifiers, e.g., those that are stored in an ExtQualType |
766 | /// instance. |
767 | bool hasLocalNonFastQualifiers() const { |
768 | return Value.getPointer().is<const ExtQuals*>(); |
769 | } |
770 | |
771 | /// Retrieve the set of qualifiers local to this particular QualType |
772 | /// instance, not including any qualifiers acquired through typedefs or |
773 | /// other sugar. |
774 | Qualifiers getLocalQualifiers() const; |
775 | |
776 | /// Retrieve the set of qualifiers applied to this type. |
777 | Qualifiers getQualifiers() const; |
778 | |
779 | /// Retrieve the set of CVR (const-volatile-restrict) qualifiers |
780 | /// local to this particular QualType instance, not including any qualifiers |
781 | /// acquired through typedefs or other sugar. |
782 | unsigned getLocalCVRQualifiers() const { |
783 | return getLocalFastQualifiers(); |
784 | } |
785 | |
786 | /// Retrieve the set of CVR (const-volatile-restrict) qualifiers |
787 | /// applied to this type. |
788 | unsigned getCVRQualifiers() const; |
789 | |
790 | bool isConstant(const ASTContext& Ctx) const { |
791 | return QualType::isConstant(*this, Ctx); |
792 | } |
793 | |
794 | /// Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10). |
795 | bool isPODType(const ASTContext &Context) const; |
796 | |
797 | /// Return true if this is a POD type according to the rules of the C++98 |
798 | /// standard, regardless of the current compilation's language. |
799 | bool isCXX98PODType(const ASTContext &Context) const; |
800 | |
801 | /// Return true if this is a POD type according to the more relaxed rules |
802 | /// of the C++11 standard, regardless of the current compilation's language. |
803 | /// (C++0x [basic.types]p9). Note that, unlike |
804 | /// CXXRecordDecl::isCXX11StandardLayout, this takes DRs into account. |
805 | bool isCXX11PODType(const ASTContext &Context) const; |
806 | |
807 | /// Return true if this is a trivial type per (C++0x [basic.types]p9) |
808 | bool isTrivialType(const ASTContext &Context) const; |
809 | |
810 | /// Return true if this is a trivially copyable type (C++0x [basic.types]p9) |
811 | bool isTriviallyCopyableType(const ASTContext &Context) const; |
812 | |
813 | |
814 | /// Returns true if it is a class and it might be dynamic. |
815 | bool mayBeDynamicClass() const; |
816 | |
817 | /// Returns true if it is not a class or if the class might not be dynamic. |
818 | bool mayBeNotDynamicClass() const; |
819 | |
820 | // Don't promise in the API that anything besides 'const' can be |
821 | // easily added. |
822 | |
823 | /// Add the `const` type qualifier to this QualType. |
824 | void addConst() { |
825 | addFastQualifiers(Qualifiers::Const); |
826 | } |
827 | QualType withConst() const { |
828 | return withFastQualifiers(Qualifiers::Const); |
829 | } |
830 | |
831 | /// Add the `volatile` type qualifier to this QualType. |
832 | void addVolatile() { |
833 | addFastQualifiers(Qualifiers::Volatile); |
834 | } |
835 | QualType withVolatile() const { |
836 | return withFastQualifiers(Qualifiers::Volatile); |
837 | } |
838 | |
839 | /// Add the `restrict` qualifier to this QualType. |
840 | void addRestrict() { |
841 | addFastQualifiers(Qualifiers::Restrict); |
842 | } |
843 | QualType withRestrict() const { |
844 | return withFastQualifiers(Qualifiers::Restrict); |
845 | } |
846 | |
847 | QualType withCVRQualifiers(unsigned CVR) const { |
848 | return withFastQualifiers(CVR); |
849 | } |
850 | |
851 | void addFastQualifiers(unsigned TQs) { |
852 | assert(!(TQs & ~Qualifiers::FastMask)((!(TQs & ~Qualifiers::FastMask) && "non-fast qualifier bits set in mask!" ) ? static_cast<void> (0) : __assert_fail ("!(TQs & ~Qualifiers::FastMask) && \"non-fast qualifier bits set in mask!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 853, __PRETTY_FUNCTION__)) |
853 | && "non-fast qualifier bits set in mask!")((!(TQs & ~Qualifiers::FastMask) && "non-fast qualifier bits set in mask!" ) ? static_cast<void> (0) : __assert_fail ("!(TQs & ~Qualifiers::FastMask) && \"non-fast qualifier bits set in mask!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 853, __PRETTY_FUNCTION__)); |
854 | Value.setInt(Value.getInt() | TQs); |
855 | } |
856 | |
857 | void removeLocalConst(); |
858 | void removeLocalVolatile(); |
859 | void removeLocalRestrict(); |
860 | void removeLocalCVRQualifiers(unsigned Mask); |
861 | |
862 | void removeLocalFastQualifiers() { Value.setInt(0); } |
863 | void removeLocalFastQualifiers(unsigned Mask) { |
864 | assert(!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers")((!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers" ) ? static_cast<void> (0) : __assert_fail ("!(Mask & ~Qualifiers::FastMask) && \"mask has non-fast qualifiers\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 864, __PRETTY_FUNCTION__)); |
865 | Value.setInt(Value.getInt() & ~Mask); |
866 | } |
867 | |
868 | // Creates a type with the given qualifiers in addition to any |
869 | // qualifiers already on this type. |
870 | QualType withFastQualifiers(unsigned TQs) const { |
871 | QualType T = *this; |
872 | T.addFastQualifiers(TQs); |
873 | return T; |
874 | } |
875 | |
876 | // Creates a type with exactly the given fast qualifiers, removing |
877 | // any existing fast qualifiers. |
878 | QualType withExactLocalFastQualifiers(unsigned TQs) const { |
879 | return withoutLocalFastQualifiers().withFastQualifiers(TQs); |
880 | } |
881 | |
882 | // Removes fast qualifiers, but leaves any extended qualifiers in place. |
883 | QualType withoutLocalFastQualifiers() const { |
884 | QualType T = *this; |
885 | T.removeLocalFastQualifiers(); |
886 | return T; |
887 | } |
888 | |
889 | QualType getCanonicalType() const; |
890 | |
891 | /// Return this type with all of the instance-specific qualifiers |
892 | /// removed, but without removing any qualifiers that may have been applied |
893 | /// through typedefs. |
894 | QualType getLocalUnqualifiedType() const { return QualType(getTypePtr(), 0); } |
895 | |
896 | /// Retrieve the unqualified variant of the given type, |
897 | /// removing as little sugar as possible. |
898 | /// |
899 | /// This routine looks through various kinds of sugar to find the |
900 | /// least-desugared type that is unqualified. For example, given: |
901 | /// |
902 | /// \code |
903 | /// typedef int Integer; |
904 | /// typedef const Integer CInteger; |
905 | /// typedef CInteger DifferenceType; |
906 | /// \endcode |
907 | /// |
908 | /// Executing \c getUnqualifiedType() on the type \c DifferenceType will |
909 | /// desugar until we hit the type \c Integer, which has no qualifiers on it. |
910 | /// |
911 | /// The resulting type might still be qualified if it's sugar for an array |
912 | /// type. To strip qualifiers even from within a sugared array type, use |
913 | /// ASTContext::getUnqualifiedArrayType. |
914 | inline QualType getUnqualifiedType() const; |
915 | |
916 | /// Retrieve the unqualified variant of the given type, removing as little |
917 | /// sugar as possible. |
918 | /// |
919 | /// Like getUnqualifiedType(), but also returns the set of |
920 | /// qualifiers that were built up. |
921 | /// |
922 | /// The resulting type might still be qualified if it's sugar for an array |
923 | /// type. To strip qualifiers even from within a sugared array type, use |
924 | /// ASTContext::getUnqualifiedArrayType. |
925 | inline SplitQualType getSplitUnqualifiedType() const; |
926 | |
927 | /// Determine whether this type is more qualified than the other |
928 | /// given type, requiring exact equality for non-CVR qualifiers. |
929 | bool isMoreQualifiedThan(QualType Other) const; |
930 | |
931 | /// Determine whether this type is at least as qualified as the other |
932 | /// given type, requiring exact equality for non-CVR qualifiers. |
933 | bool isAtLeastAsQualifiedAs(QualType Other) const; |
934 | |
935 | QualType getNonReferenceType() const; |
936 | |
937 | /// Determine the type of a (typically non-lvalue) expression with the |
938 | /// specified result type. |
939 | /// |
940 | /// This routine should be used for expressions for which the return type is |
941 | /// explicitly specified (e.g., in a cast or call) and isn't necessarily |
942 | /// an lvalue. It removes a top-level reference (since there are no |
943 | /// expressions of reference type) and deletes top-level cvr-qualifiers |
944 | /// from non-class types (in C++) or all types (in C). |
945 | QualType getNonLValueExprType(const ASTContext &Context) const; |
946 | |
947 | /// Return the specified type with any "sugar" removed from |
948 | /// the type. This takes off typedefs, typeof's etc. If the outer level of |
949 | /// the type is already concrete, it returns it unmodified. This is similar |
950 | /// to getting the canonical type, but it doesn't remove *all* typedefs. For |
951 | /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is |
952 | /// concrete. |
953 | /// |
954 | /// Qualifiers are left in place. |
955 | QualType getDesugaredType(const ASTContext &Context) const { |
956 | return getDesugaredType(*this, Context); |
957 | } |
958 | |
959 | SplitQualType getSplitDesugaredType() const { |
960 | return getSplitDesugaredType(*this); |
961 | } |
962 | |
963 | /// Return the specified type with one level of "sugar" removed from |
964 | /// the type. |
965 | /// |
966 | /// This routine takes off the first typedef, typeof, etc. If the outer level |
967 | /// of the type is already concrete, it returns it unmodified. |
968 | QualType getSingleStepDesugaredType(const ASTContext &Context) const { |
969 | return getSingleStepDesugaredTypeImpl(*this, Context); |
970 | } |
971 | |
972 | /// Returns the specified type after dropping any |
973 | /// outer-level parentheses. |
974 | QualType IgnoreParens() const { |
975 | if (isa<ParenType>(*this)) |
976 | return QualType::IgnoreParens(*this); |
977 | return *this; |
978 | } |
979 | |
980 | /// Indicate whether the specified types and qualifiers are identical. |
981 | friend bool operator==(const QualType &LHS, const QualType &RHS) { |
982 | return LHS.Value == RHS.Value; |
983 | } |
984 | friend bool operator!=(const QualType &LHS, const QualType &RHS) { |
985 | return LHS.Value != RHS.Value; |
986 | } |
987 | friend bool operator<(const QualType &LHS, const QualType &RHS) { |
988 | return LHS.Value < RHS.Value; |
989 | } |
990 | |
991 | static std::string getAsString(SplitQualType split, |
992 | const PrintingPolicy &Policy) { |
993 | return getAsString(split.Ty, split.Quals, Policy); |
994 | } |
995 | static std::string getAsString(const Type *ty, Qualifiers qs, |
996 | const PrintingPolicy &Policy); |
997 | |
998 | std::string getAsString() const; |
999 | std::string getAsString(const PrintingPolicy &Policy) const; |
1000 | |
1001 | void print(raw_ostream &OS, const PrintingPolicy &Policy, |
1002 | const Twine &PlaceHolder = Twine(), |
1003 | unsigned Indentation = 0) const; |
1004 | |
1005 | static void print(SplitQualType split, raw_ostream &OS, |
1006 | const PrintingPolicy &policy, const Twine &PlaceHolder, |
1007 | unsigned Indentation = 0) { |
1008 | return print(split.Ty, split.Quals, OS, policy, PlaceHolder, Indentation); |
1009 | } |
1010 | |
1011 | static void print(const Type *ty, Qualifiers qs, |
1012 | raw_ostream &OS, const PrintingPolicy &policy, |
1013 | const Twine &PlaceHolder, |
1014 | unsigned Indentation = 0); |
1015 | |
1016 | void getAsStringInternal(std::string &Str, |
1017 | const PrintingPolicy &Policy) const; |
1018 | |
1019 | static void getAsStringInternal(SplitQualType split, std::string &out, |
1020 | const PrintingPolicy &policy) { |
1021 | return getAsStringInternal(split.Ty, split.Quals, out, policy); |
1022 | } |
1023 | |
1024 | static void getAsStringInternal(const Type *ty, Qualifiers qs, |
1025 | std::string &out, |
1026 | const PrintingPolicy &policy); |
1027 | |
1028 | class StreamedQualTypeHelper { |
1029 | const QualType &T; |
1030 | const PrintingPolicy &Policy; |
1031 | const Twine &PlaceHolder; |
1032 | unsigned Indentation; |
1033 | |
1034 | public: |
1035 | StreamedQualTypeHelper(const QualType &T, const PrintingPolicy &Policy, |
1036 | const Twine &PlaceHolder, unsigned Indentation) |
1037 | : T(T), Policy(Policy), PlaceHolder(PlaceHolder), |
1038 | Indentation(Indentation) {} |
1039 | |
1040 | friend raw_ostream &operator<<(raw_ostream &OS, |
1041 | const StreamedQualTypeHelper &SQT) { |
1042 | SQT.T.print(OS, SQT.Policy, SQT.PlaceHolder, SQT.Indentation); |
1043 | return OS; |
1044 | } |
1045 | }; |
1046 | |
1047 | StreamedQualTypeHelper stream(const PrintingPolicy &Policy, |
1048 | const Twine &PlaceHolder = Twine(), |
1049 | unsigned Indentation = 0) const { |
1050 | return StreamedQualTypeHelper(*this, Policy, PlaceHolder, Indentation); |
1051 | } |
1052 | |
1053 | void dump(const char *s) const; |
1054 | void dump() const; |
1055 | void dump(llvm::raw_ostream &OS) const; |
1056 | |
1057 | void Profile(llvm::FoldingSetNodeID &ID) const { |
1058 | ID.AddPointer(getAsOpaquePtr()); |
1059 | } |
1060 | |
1061 | /// Check if this type has any address space qualifier. |
1062 | inline bool hasAddressSpace() const; |
1063 | |
1064 | /// Return the address space of this type. |
1065 | inline LangAS getAddressSpace() const; |
1066 | |
1067 | /// Returns gc attribute of this type. |
1068 | inline Qualifiers::GC getObjCGCAttr() const; |
1069 | |
1070 | /// true when Type is objc's weak. |
1071 | bool isObjCGCWeak() const { |
1072 | return getObjCGCAttr() == Qualifiers::Weak; |
1073 | } |
1074 | |
1075 | /// true when Type is objc's strong. |
1076 | bool isObjCGCStrong() const { |
1077 | return getObjCGCAttr() == Qualifiers::Strong; |
1078 | } |
1079 | |
1080 | /// Returns lifetime attribute of this type. |
1081 | Qualifiers::ObjCLifetime getObjCLifetime() const { |
1082 | return getQualifiers().getObjCLifetime(); |
1083 | } |
1084 | |
1085 | bool hasNonTrivialObjCLifetime() const { |
1086 | return getQualifiers().hasNonTrivialObjCLifetime(); |
1087 | } |
1088 | |
1089 | bool hasStrongOrWeakObjCLifetime() const { |
1090 | return getQualifiers().hasStrongOrWeakObjCLifetime(); |
1091 | } |
1092 | |
1093 | // true when Type is objc's weak and weak is enabled but ARC isn't. |
1094 | bool isNonWeakInMRRWithObjCWeak(const ASTContext &Context) const; |
1095 | |
1096 | enum PrimitiveDefaultInitializeKind { |
1097 | /// The type does not fall into any of the following categories. Note that |
1098 | /// this case is zero-valued so that values of this enum can be used as a |
1099 | /// boolean condition for non-triviality. |
1100 | PDIK_Trivial, |
1101 | |
1102 | /// The type is an Objective-C retainable pointer type that is qualified |
1103 | /// with the ARC __strong qualifier. |
1104 | PDIK_ARCStrong, |
1105 | |
1106 | /// The type is an Objective-C retainable pointer type that is qualified |
1107 | /// with the ARC __weak qualifier. |
1108 | PDIK_ARCWeak, |
1109 | |
1110 | /// The type is a struct containing a field whose type is not PCK_Trivial. |
1111 | PDIK_Struct |
1112 | }; |
1113 | |
1114 | /// Functions to query basic properties of non-trivial C struct types. |
1115 | |
1116 | /// Check if this is a non-trivial type that would cause a C struct |
1117 | /// transitively containing this type to be non-trivial to default initialize |
1118 | /// and return the kind. |
1119 | PrimitiveDefaultInitializeKind |
1120 | isNonTrivialToPrimitiveDefaultInitialize() const; |
1121 | |
1122 | enum PrimitiveCopyKind { |
1123 | /// The type does not fall into any of the following categories. Note that |
1124 | /// this case is zero-valued so that values of this enum can be used as a |
1125 | /// boolean condition for non-triviality. |
1126 | PCK_Trivial, |
1127 | |
1128 | /// The type would be trivial except that it is volatile-qualified. Types |
1129 | /// that fall into one of the other non-trivial cases may additionally be |
1130 | /// volatile-qualified. |
1131 | PCK_VolatileTrivial, |
1132 | |
1133 | /// The type is an Objective-C retainable pointer type that is qualified |
1134 | /// with the ARC __strong qualifier. |
1135 | PCK_ARCStrong, |
1136 | |
1137 | /// The type is an Objective-C retainable pointer type that is qualified |
1138 | /// with the ARC __weak qualifier. |
1139 | PCK_ARCWeak, |
1140 | |
1141 | /// The type is a struct containing a field whose type is neither |
1142 | /// PCK_Trivial nor PCK_VolatileTrivial. |
1143 | /// Note that a C++ struct type does not necessarily match this; C++ copying |
1144 | /// semantics are too complex to express here, in part because they depend |
1145 | /// on the exact constructor or assignment operator that is chosen by |
1146 | /// overload resolution to do the copy. |
1147 | PCK_Struct |
1148 | }; |
1149 | |
1150 | /// Check if this is a non-trivial type that would cause a C struct |
1151 | /// transitively containing this type to be non-trivial to copy and return the |
1152 | /// kind. |
1153 | PrimitiveCopyKind isNonTrivialToPrimitiveCopy() const; |
1154 | |
1155 | /// Check if this is a non-trivial type that would cause a C struct |
1156 | /// transitively containing this type to be non-trivial to destructively |
1157 | /// move and return the kind. Destructive move in this context is a C++-style |
1158 | /// move in which the source object is placed in a valid but unspecified state |
1159 | /// after it is moved, as opposed to a truly destructive move in which the |
1160 | /// source object is placed in an uninitialized state. |
1161 | PrimitiveCopyKind isNonTrivialToPrimitiveDestructiveMove() const; |
1162 | |
1163 | enum DestructionKind { |
1164 | DK_none, |
1165 | DK_cxx_destructor, |
1166 | DK_objc_strong_lifetime, |
1167 | DK_objc_weak_lifetime, |
1168 | DK_nontrivial_c_struct |
1169 | }; |
1170 | |
1171 | /// Returns a nonzero value if objects of this type require |
1172 | /// non-trivial work to clean up after. Non-zero because it's |
1173 | /// conceivable that qualifiers (objc_gc(weak)?) could make |
1174 | /// something require destruction. |
1175 | DestructionKind isDestructedType() const { |
1176 | return isDestructedTypeImpl(*this); |
1177 | } |
1178 | |
1179 | /// Check if this is or contains a C union that is non-trivial to |
1180 | /// default-initialize, which is a union that has a member that is non-trivial |
1181 | /// to default-initialize. If this returns true, |
1182 | /// isNonTrivialToPrimitiveDefaultInitialize returns PDIK_Struct. |
1183 | bool hasNonTrivialToPrimitiveDefaultInitializeCUnion() const; |
1184 | |
1185 | /// Check if this is or contains a C union that is non-trivial to destruct, |
1186 | /// which is a union that has a member that is non-trivial to destruct. If |
1187 | /// this returns true, isDestructedType returns DK_nontrivial_c_struct. |
1188 | bool hasNonTrivialToPrimitiveDestructCUnion() const; |
1189 | |
1190 | /// Check if this is or contains a C union that is non-trivial to copy, which |
1191 | /// is a union that has a member that is non-trivial to copy. If this returns |
1192 | /// true, isNonTrivialToPrimitiveCopy returns PCK_Struct. |
1193 | bool hasNonTrivialToPrimitiveCopyCUnion() const; |
1194 | |
1195 | /// Determine whether expressions of the given type are forbidden |
1196 | /// from being lvalues in C. |
1197 | /// |
1198 | /// The expression types that are forbidden to be lvalues are: |
1199 | /// - 'void', but not qualified void |
1200 | /// - function types |
1201 | /// |
1202 | /// The exact rule here is C99 6.3.2.1: |
1203 | /// An lvalue is an expression with an object type or an incomplete |
1204 | /// type other than void. |
1205 | bool isCForbiddenLValueType() const; |
1206 | |
1207 | /// Substitute type arguments for the Objective-C type parameters used in the |
1208 | /// subject type. |
1209 | /// |
1210 | /// \param ctx ASTContext in which the type exists. |
1211 | /// |
1212 | /// \param typeArgs The type arguments that will be substituted for the |
1213 | /// Objective-C type parameters in the subject type, which are generally |
1214 | /// computed via \c Type::getObjCSubstitutions. If empty, the type |
1215 | /// parameters will be replaced with their bounds or id/Class, as appropriate |
1216 | /// for the context. |
1217 | /// |
1218 | /// \param context The context in which the subject type was written. |
1219 | /// |
1220 | /// \returns the resulting type. |
1221 | QualType substObjCTypeArgs(ASTContext &ctx, |
1222 | ArrayRef<QualType> typeArgs, |
1223 | ObjCSubstitutionContext context) const; |
1224 | |
1225 | /// Substitute type arguments from an object type for the Objective-C type |
1226 | /// parameters used in the subject type. |
1227 | /// |
1228 | /// This operation combines the computation of type arguments for |
1229 | /// substitution (\c Type::getObjCSubstitutions) with the actual process of |
1230 | /// substitution (\c QualType::substObjCTypeArgs) for the convenience of |
1231 | /// callers that need to perform a single substitution in isolation. |
1232 | /// |
1233 | /// \param objectType The type of the object whose member type we're |
1234 | /// substituting into. For example, this might be the receiver of a message |
1235 | /// or the base of a property access. |
1236 | /// |
1237 | /// \param dc The declaration context from which the subject type was |
1238 | /// retrieved, which indicates (for example) which type parameters should |
1239 | /// be substituted. |
1240 | /// |
1241 | /// \param context The context in which the subject type was written. |
1242 | /// |
1243 | /// \returns the subject type after replacing all of the Objective-C type |
1244 | /// parameters with their corresponding arguments. |
1245 | QualType substObjCMemberType(QualType objectType, |
1246 | const DeclContext *dc, |
1247 | ObjCSubstitutionContext context) const; |
1248 | |
1249 | /// Strip Objective-C "__kindof" types from the given type. |
1250 | QualType stripObjCKindOfType(const ASTContext &ctx) const; |
1251 | |
1252 | /// Remove all qualifiers including _Atomic. |
1253 | QualType getAtomicUnqualifiedType() const; |
1254 | |
1255 | private: |
1256 | // These methods are implemented in a separate translation unit; |
1257 | // "static"-ize them to avoid creating temporary QualTypes in the |
1258 | // caller. |
1259 | static bool isConstant(QualType T, const ASTContext& Ctx); |
1260 | static QualType getDesugaredType(QualType T, const ASTContext &Context); |
1261 | static SplitQualType getSplitDesugaredType(QualType T); |
1262 | static SplitQualType getSplitUnqualifiedTypeImpl(QualType type); |
1263 | static QualType getSingleStepDesugaredTypeImpl(QualType type, |
1264 | const ASTContext &C); |
1265 | static QualType IgnoreParens(QualType T); |
1266 | static DestructionKind isDestructedTypeImpl(QualType type); |
1267 | |
1268 | /// Check if \param RD is or contains a non-trivial C union. |
1269 | static bool hasNonTrivialToPrimitiveDefaultInitializeCUnion(const RecordDecl *RD); |
1270 | static bool hasNonTrivialToPrimitiveDestructCUnion(const RecordDecl *RD); |
1271 | static bool hasNonTrivialToPrimitiveCopyCUnion(const RecordDecl *RD); |
1272 | }; |
1273 | |
1274 | } // namespace clang |
1275 | |
1276 | namespace llvm { |
1277 | |
1278 | /// Implement simplify_type for QualType, so that we can dyn_cast from QualType |
1279 | /// to a specific Type class. |
1280 | template<> struct simplify_type< ::clang::QualType> { |
1281 | using SimpleType = const ::clang::Type *; |
1282 | |
1283 | static SimpleType getSimplifiedValue(::clang::QualType Val) { |
1284 | return Val.getTypePtr(); |
1285 | } |
1286 | }; |
1287 | |
1288 | // Teach SmallPtrSet that QualType is "basically a pointer". |
1289 | template<> |
1290 | struct PointerLikeTypeTraits<clang::QualType> { |
1291 | static inline void *getAsVoidPointer(clang::QualType P) { |
1292 | return P.getAsOpaquePtr(); |
1293 | } |
1294 | |
1295 | static inline clang::QualType getFromVoidPointer(void *P) { |
1296 | return clang::QualType::getFromOpaquePtr(P); |
1297 | } |
1298 | |
1299 | // Various qualifiers go in low bits. |
1300 | static constexpr int NumLowBitsAvailable = 0; |
1301 | }; |
1302 | |
1303 | } // namespace llvm |
1304 | |
1305 | namespace clang { |
1306 | |
1307 | /// Base class that is common to both the \c ExtQuals and \c Type |
1308 | /// classes, which allows \c QualType to access the common fields between the |
1309 | /// two. |
1310 | class ExtQualsTypeCommonBase { |
1311 | friend class ExtQuals; |
1312 | friend class QualType; |
1313 | friend class Type; |
1314 | |
1315 | /// The "base" type of an extended qualifiers type (\c ExtQuals) or |
1316 | /// a self-referential pointer (for \c Type). |
1317 | /// |
1318 | /// This pointer allows an efficient mapping from a QualType to its |
1319 | /// underlying type pointer. |
1320 | const Type *const BaseType; |
1321 | |
1322 | /// The canonical type of this type. A QualType. |
1323 | QualType CanonicalType; |
1324 | |
1325 | ExtQualsTypeCommonBase(const Type *baseType, QualType canon) |
1326 | : BaseType(baseType), CanonicalType(canon) {} |
1327 | }; |
1328 | |
1329 | /// We can encode up to four bits in the low bits of a |
1330 | /// type pointer, but there are many more type qualifiers that we want |
1331 | /// to be able to apply to an arbitrary type. Therefore we have this |
1332 | /// struct, intended to be heap-allocated and used by QualType to |
1333 | /// store qualifiers. |
1334 | /// |
1335 | /// The current design tags the 'const', 'restrict', and 'volatile' qualifiers |
1336 | /// in three low bits on the QualType pointer; a fourth bit records whether |
1337 | /// the pointer is an ExtQuals node. The extended qualifiers (address spaces, |
1338 | /// Objective-C GC attributes) are much more rare. |
1339 | class ExtQuals : public ExtQualsTypeCommonBase, public llvm::FoldingSetNode { |
1340 | // NOTE: changing the fast qualifiers should be straightforward as |
1341 | // long as you don't make 'const' non-fast. |
1342 | // 1. Qualifiers: |
1343 | // a) Modify the bitmasks (Qualifiers::TQ and DeclSpec::TQ). |
1344 | // Fast qualifiers must occupy the low-order bits. |
1345 | // b) Update Qualifiers::FastWidth and FastMask. |
1346 | // 2. QualType: |
1347 | // a) Update is{Volatile,Restrict}Qualified(), defined inline. |
1348 | // b) Update remove{Volatile,Restrict}, defined near the end of |
1349 | // this header. |
1350 | // 3. ASTContext: |
1351 | // a) Update get{Volatile,Restrict}Type. |
1352 | |
1353 | /// The immutable set of qualifiers applied by this node. Always contains |
1354 | /// extended qualifiers. |
1355 | Qualifiers Quals; |
1356 | |
1357 | ExtQuals *this_() { return this; } |
1358 | |
1359 | public: |
1360 | ExtQuals(const Type *baseType, QualType canon, Qualifiers quals) |
1361 | : ExtQualsTypeCommonBase(baseType, |
1362 | canon.isNull() ? QualType(this_(), 0) : canon), |
1363 | Quals(quals) { |
1364 | assert(Quals.hasNonFastQualifiers()((Quals.hasNonFastQualifiers() && "ExtQuals created with no fast qualifiers" ) ? static_cast<void> (0) : __assert_fail ("Quals.hasNonFastQualifiers() && \"ExtQuals created with no fast qualifiers\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 1365, __PRETTY_FUNCTION__)) |
1365 | && "ExtQuals created with no fast qualifiers")((Quals.hasNonFastQualifiers() && "ExtQuals created with no fast qualifiers" ) ? static_cast<void> (0) : __assert_fail ("Quals.hasNonFastQualifiers() && \"ExtQuals created with no fast qualifiers\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 1365, __PRETTY_FUNCTION__)); |
1366 | assert(!Quals.hasFastQualifiers()((!Quals.hasFastQualifiers() && "ExtQuals created with fast qualifiers" ) ? static_cast<void> (0) : __assert_fail ("!Quals.hasFastQualifiers() && \"ExtQuals created with fast qualifiers\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 1367, __PRETTY_FUNCTION__)) |
1367 | && "ExtQuals created with fast qualifiers")((!Quals.hasFastQualifiers() && "ExtQuals created with fast qualifiers" ) ? static_cast<void> (0) : __assert_fail ("!Quals.hasFastQualifiers() && \"ExtQuals created with fast qualifiers\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 1367, __PRETTY_FUNCTION__)); |
1368 | } |
1369 | |
1370 | Qualifiers getQualifiers() const { return Quals; } |
1371 | |
1372 | bool hasObjCGCAttr() const { return Quals.hasObjCGCAttr(); } |
1373 | Qualifiers::GC getObjCGCAttr() const { return Quals.getObjCGCAttr(); } |
1374 | |
1375 | bool hasObjCLifetime() const { return Quals.hasObjCLifetime(); } |
1376 | Qualifiers::ObjCLifetime getObjCLifetime() const { |
1377 | return Quals.getObjCLifetime(); |
1378 | } |
1379 | |
1380 | bool hasAddressSpace() const { return Quals.hasAddressSpace(); } |
1381 | LangAS getAddressSpace() const { return Quals.getAddressSpace(); } |
1382 | |
1383 | const Type *getBaseType() const { return BaseType; } |
1384 | |
1385 | public: |
1386 | void Profile(llvm::FoldingSetNodeID &ID) const { |
1387 | Profile(ID, getBaseType(), Quals); |
1388 | } |
1389 | |
1390 | static void Profile(llvm::FoldingSetNodeID &ID, |
1391 | const Type *BaseType, |
1392 | Qualifiers Quals) { |
1393 | assert(!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!")((!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!" ) ? static_cast<void> (0) : __assert_fail ("!Quals.hasFastQualifiers() && \"fast qualifiers in ExtQuals hash!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 1393, __PRETTY_FUNCTION__)); |
1394 | ID.AddPointer(BaseType); |
1395 | Quals.Profile(ID); |
1396 | } |
1397 | }; |
1398 | |
1399 | /// The kind of C++11 ref-qualifier associated with a function type. |
1400 | /// This determines whether a member function's "this" object can be an |
1401 | /// lvalue, rvalue, or neither. |
1402 | enum RefQualifierKind { |
1403 | /// No ref-qualifier was provided. |
1404 | RQ_None = 0, |
1405 | |
1406 | /// An lvalue ref-qualifier was provided (\c &). |
1407 | RQ_LValue, |
1408 | |
1409 | /// An rvalue ref-qualifier was provided (\c &&). |
1410 | RQ_RValue |
1411 | }; |
1412 | |
1413 | /// Which keyword(s) were used to create an AutoType. |
1414 | enum class AutoTypeKeyword { |
1415 | /// auto |
1416 | Auto, |
1417 | |
1418 | /// decltype(auto) |
1419 | DecltypeAuto, |
1420 | |
1421 | /// __auto_type (GNU extension) |
1422 | GNUAutoType |
1423 | }; |
1424 | |
1425 | /// The base class of the type hierarchy. |
1426 | /// |
1427 | /// A central concept with types is that each type always has a canonical |
1428 | /// type. A canonical type is the type with any typedef names stripped out |
1429 | /// of it or the types it references. For example, consider: |
1430 | /// |
1431 | /// typedef int foo; |
1432 | /// typedef foo* bar; |
1433 | /// 'int *' 'foo *' 'bar' |
1434 | /// |
1435 | /// There will be a Type object created for 'int'. Since int is canonical, its |
1436 | /// CanonicalType pointer points to itself. There is also a Type for 'foo' (a |
1437 | /// TypedefType). Its CanonicalType pointer points to the 'int' Type. Next |
1438 | /// there is a PointerType that represents 'int*', which, like 'int', is |
1439 | /// canonical. Finally, there is a PointerType type for 'foo*' whose canonical |
1440 | /// type is 'int*', and there is a TypedefType for 'bar', whose canonical type |
1441 | /// is also 'int*'. |
1442 | /// |
1443 | /// Non-canonical types are useful for emitting diagnostics, without losing |
1444 | /// information about typedefs being used. Canonical types are useful for type |
1445 | /// comparisons (they allow by-pointer equality tests) and useful for reasoning |
1446 | /// about whether something has a particular form (e.g. is a function type), |
1447 | /// because they implicitly, recursively, strip all typedefs out of a type. |
1448 | /// |
1449 | /// Types, once created, are immutable. |
1450 | /// |
1451 | class alignas(8) Type : public ExtQualsTypeCommonBase { |
1452 | public: |
1453 | enum TypeClass { |
1454 | #define TYPE(Class, Base) Class, |
1455 | #define LAST_TYPE(Class) TypeLast = Class |
1456 | #define ABSTRACT_TYPE(Class, Base) |
1457 | #include "clang/AST/TypeNodes.inc" |
1458 | }; |
1459 | |
1460 | private: |
1461 | /// Bitfields required by the Type class. |
1462 | class TypeBitfields { |
1463 | friend class Type; |
1464 | template <class T> friend class TypePropertyCache; |
1465 | |
1466 | /// TypeClass bitfield - Enum that specifies what subclass this belongs to. |
1467 | unsigned TC : 8; |
1468 | |
1469 | /// Store information on the type dependency. |
1470 | /*TypeDependence*/ unsigned Dependence : TypeDependenceBits; |
1471 | |
1472 | /// True if the cache (i.e. the bitfields here starting with |
1473 | /// 'Cache') is valid. |
1474 | mutable unsigned CacheValid : 1; |
1475 | |
1476 | /// Linkage of this type. |
1477 | mutable unsigned CachedLinkage : 3; |
1478 | |
1479 | /// Whether this type involves and local or unnamed types. |
1480 | mutable unsigned CachedLocalOrUnnamed : 1; |
1481 | |
1482 | /// Whether this type comes from an AST file. |
1483 | mutable unsigned FromAST : 1; |
1484 | |
1485 | bool isCacheValid() const { |
1486 | return CacheValid; |
1487 | } |
1488 | |
1489 | Linkage getLinkage() const { |
1490 | assert(isCacheValid() && "getting linkage from invalid cache")((isCacheValid() && "getting linkage from invalid cache" ) ? static_cast<void> (0) : __assert_fail ("isCacheValid() && \"getting linkage from invalid cache\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 1490, __PRETTY_FUNCTION__)); |
1491 | return static_cast<Linkage>(CachedLinkage); |
1492 | } |
1493 | |
1494 | bool hasLocalOrUnnamedType() const { |
1495 | assert(isCacheValid() && "getting linkage from invalid cache")((isCacheValid() && "getting linkage from invalid cache" ) ? static_cast<void> (0) : __assert_fail ("isCacheValid() && \"getting linkage from invalid cache\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 1495, __PRETTY_FUNCTION__)); |
1496 | return CachedLocalOrUnnamed; |
1497 | } |
1498 | }; |
1499 | enum { NumTypeBits = 18 }; |
1500 | |
1501 | protected: |
1502 | // These classes allow subclasses to somewhat cleanly pack bitfields |
1503 | // into Type. |
1504 | |
1505 | class ArrayTypeBitfields { |
1506 | friend class ArrayType; |
1507 | |
1508 | unsigned : NumTypeBits; |
1509 | |
1510 | /// CVR qualifiers from declarations like |
1511 | /// 'int X[static restrict 4]'. For function parameters only. |
1512 | unsigned IndexTypeQuals : 3; |
1513 | |
1514 | /// Storage class qualifiers from declarations like |
1515 | /// 'int X[static restrict 4]'. For function parameters only. |
1516 | /// Actually an ArrayType::ArraySizeModifier. |
1517 | unsigned SizeModifier : 3; |
1518 | }; |
1519 | |
1520 | class ConstantArrayTypeBitfields { |
1521 | friend class ConstantArrayType; |
1522 | |
1523 | unsigned : NumTypeBits + 3 + 3; |
1524 | |
1525 | /// Whether we have a stored size expression. |
1526 | unsigned HasStoredSizeExpr : 1; |
1527 | }; |
1528 | |
1529 | class BuiltinTypeBitfields { |
1530 | friend class BuiltinType; |
1531 | |
1532 | unsigned : NumTypeBits; |
1533 | |
1534 | /// The kind (BuiltinType::Kind) of builtin type this is. |
1535 | unsigned Kind : 8; |
1536 | }; |
1537 | |
1538 | /// FunctionTypeBitfields store various bits belonging to FunctionProtoType. |
1539 | /// Only common bits are stored here. Additional uncommon bits are stored |
1540 | /// in a trailing object after FunctionProtoType. |
1541 | class FunctionTypeBitfields { |
1542 | friend class FunctionProtoType; |
1543 | friend class FunctionType; |
1544 | |
1545 | unsigned : NumTypeBits; |
1546 | |
1547 | /// Extra information which affects how the function is called, like |
1548 | /// regparm and the calling convention. |
1549 | unsigned ExtInfo : 12; |
1550 | |
1551 | /// The ref-qualifier associated with a \c FunctionProtoType. |
1552 | /// |
1553 | /// This is a value of type \c RefQualifierKind. |
1554 | unsigned RefQualifier : 2; |
1555 | |
1556 | /// Used only by FunctionProtoType, put here to pack with the |
1557 | /// other bitfields. |
1558 | /// The qualifiers are part of FunctionProtoType because... |
1559 | /// |
1560 | /// C++ 8.3.5p4: The return type, the parameter type list and the |
1561 | /// cv-qualifier-seq, [...], are part of the function type. |
1562 | unsigned FastTypeQuals : Qualifiers::FastWidth; |
1563 | /// Whether this function has extended Qualifiers. |
1564 | unsigned HasExtQuals : 1; |
1565 | |
1566 | /// The number of parameters this function has, not counting '...'. |
1567 | /// According to [implimits] 8 bits should be enough here but this is |
1568 | /// somewhat easy to exceed with metaprogramming and so we would like to |
1569 | /// keep NumParams as wide as reasonably possible. |
1570 | unsigned NumParams : 16; |
1571 | |
1572 | /// The type of exception specification this function has. |
1573 | unsigned ExceptionSpecType : 4; |
1574 | |
1575 | /// Whether this function has extended parameter information. |
1576 | unsigned HasExtParameterInfos : 1; |
1577 | |
1578 | /// Whether the function is variadic. |
1579 | unsigned Variadic : 1; |
1580 | |
1581 | /// Whether this function has a trailing return type. |
1582 | unsigned HasTrailingReturn : 1; |
1583 | }; |
1584 | |
1585 | class ObjCObjectTypeBitfields { |
1586 | friend class ObjCObjectType; |
1587 | |
1588 | unsigned : NumTypeBits; |
1589 | |
1590 | /// The number of type arguments stored directly on this object type. |
1591 | unsigned NumTypeArgs : 7; |
1592 | |
1593 | /// The number of protocols stored directly on this object type. |
1594 | unsigned NumProtocols : 6; |
1595 | |
1596 | /// Whether this is a "kindof" type. |
1597 | unsigned IsKindOf : 1; |
1598 | }; |
1599 | |
1600 | class ReferenceTypeBitfields { |
1601 | friend class ReferenceType; |
1602 | |
1603 | unsigned : NumTypeBits; |
1604 | |
1605 | /// True if the type was originally spelled with an lvalue sigil. |
1606 | /// This is never true of rvalue references but can also be false |
1607 | /// on lvalue references because of C++0x [dcl.typedef]p9, |
1608 | /// as follows: |
1609 | /// |
1610 | /// typedef int &ref; // lvalue, spelled lvalue |
1611 | /// typedef int &&rvref; // rvalue |
1612 | /// ref &a; // lvalue, inner ref, spelled lvalue |
1613 | /// ref &&a; // lvalue, inner ref |
1614 | /// rvref &a; // lvalue, inner ref, spelled lvalue |
1615 | /// rvref &&a; // rvalue, inner ref |
1616 | unsigned SpelledAsLValue : 1; |
1617 | |
1618 | /// True if the inner type is a reference type. This only happens |
1619 | /// in non-canonical forms. |
1620 | unsigned InnerRef : 1; |
1621 | }; |
1622 | |
1623 | class TypeWithKeywordBitfields { |
1624 | friend class TypeWithKeyword; |
1625 | |
1626 | unsigned : NumTypeBits; |
1627 | |
1628 | /// An ElaboratedTypeKeyword. 8 bits for efficient access. |
1629 | unsigned Keyword : 8; |
1630 | }; |
1631 | |
1632 | enum { NumTypeWithKeywordBits = 8 }; |
1633 | |
1634 | class ElaboratedTypeBitfields { |
1635 | friend class ElaboratedType; |
1636 | |
1637 | unsigned : NumTypeBits; |
1638 | unsigned : NumTypeWithKeywordBits; |
1639 | |
1640 | /// Whether the ElaboratedType has a trailing OwnedTagDecl. |
1641 | unsigned HasOwnedTagDecl : 1; |
1642 | }; |
1643 | |
1644 | class VectorTypeBitfields { |
1645 | friend class VectorType; |
1646 | friend class DependentVectorType; |
1647 | |
1648 | unsigned : NumTypeBits; |
1649 | |
1650 | /// The kind of vector, either a generic vector type or some |
1651 | /// target-specific vector type such as for AltiVec or Neon. |
1652 | unsigned VecKind : 3; |
1653 | |
1654 | /// The number of elements in the vector. |
1655 | unsigned NumElements : 29 - NumTypeBits; |
1656 | |
1657 | enum { MaxNumElements = (1 << (29 - NumTypeBits)) - 1 }; |
1658 | }; |
1659 | |
1660 | class AttributedTypeBitfields { |
1661 | friend class AttributedType; |
1662 | |
1663 | unsigned : NumTypeBits; |
1664 | |
1665 | /// An AttributedType::Kind |
1666 | unsigned AttrKind : 32 - NumTypeBits; |
1667 | }; |
1668 | |
1669 | class AutoTypeBitfields { |
1670 | friend class AutoType; |
1671 | |
1672 | unsigned : NumTypeBits; |
1673 | |
1674 | /// Was this placeholder type spelled as 'auto', 'decltype(auto)', |
1675 | /// or '__auto_type'? AutoTypeKeyword value. |
1676 | unsigned Keyword : 2; |
1677 | |
1678 | /// The number of template arguments in the type-constraints, which is |
1679 | /// expected to be able to hold at least 1024 according to [implimits]. |
1680 | /// However as this limit is somewhat easy to hit with template |
1681 | /// metaprogramming we'd prefer to keep it as large as possible. |
1682 | /// At the moment it has been left as a non-bitfield since this type |
1683 | /// safely fits in 64 bits as an unsigned, so there is no reason to |
1684 | /// introduce the performance impact of a bitfield. |
1685 | unsigned NumArgs; |
1686 | }; |
1687 | |
1688 | class SubstTemplateTypeParmPackTypeBitfields { |
1689 | friend class SubstTemplateTypeParmPackType; |
1690 | |
1691 | unsigned : NumTypeBits; |
1692 | |
1693 | /// The number of template arguments in \c Arguments, which is |
1694 | /// expected to be able to hold at least 1024 according to [implimits]. |
1695 | /// However as this limit is somewhat easy to hit with template |
1696 | /// metaprogramming we'd prefer to keep it as large as possible. |
1697 | /// At the moment it has been left as a non-bitfield since this type |
1698 | /// safely fits in 64 bits as an unsigned, so there is no reason to |
1699 | /// introduce the performance impact of a bitfield. |
1700 | unsigned NumArgs; |
1701 | }; |
1702 | |
1703 | class TemplateSpecializationTypeBitfields { |
1704 | friend class TemplateSpecializationType; |
1705 | |
1706 | unsigned : NumTypeBits; |
1707 | |
1708 | /// Whether this template specialization type is a substituted type alias. |
1709 | unsigned TypeAlias : 1; |
1710 | |
1711 | /// The number of template arguments named in this class template |
1712 | /// specialization, which is expected to be able to hold at least 1024 |
1713 | /// according to [implimits]. However, as this limit is somewhat easy to |
1714 | /// hit with template metaprogramming we'd prefer to keep it as large |
1715 | /// as possible. At the moment it has been left as a non-bitfield since |
1716 | /// this type safely fits in 64 bits as an unsigned, so there is no reason |
1717 | /// to introduce the performance impact of a bitfield. |
1718 | unsigned NumArgs; |
1719 | }; |
1720 | |
1721 | class DependentTemplateSpecializationTypeBitfields { |
1722 | friend class DependentTemplateSpecializationType; |
1723 | |
1724 | unsigned : NumTypeBits; |
1725 | unsigned : NumTypeWithKeywordBits; |
1726 | |
1727 | /// The number of template arguments named in this class template |
1728 | /// specialization, which is expected to be able to hold at least 1024 |
1729 | /// according to [implimits]. However, as this limit is somewhat easy to |
1730 | /// hit with template metaprogramming we'd prefer to keep it as large |
1731 | /// as possible. At the moment it has been left as a non-bitfield since |
1732 | /// this type safely fits in 64 bits as an unsigned, so there is no reason |
1733 | /// to introduce the performance impact of a bitfield. |
1734 | unsigned NumArgs; |
1735 | }; |
1736 | |
1737 | class PackExpansionTypeBitfields { |
1738 | friend class PackExpansionType; |
1739 | |
1740 | unsigned : NumTypeBits; |
1741 | |
1742 | /// The number of expansions that this pack expansion will |
1743 | /// generate when substituted (+1), which is expected to be able to |
1744 | /// hold at least 1024 according to [implimits]. However, as this limit |
1745 | /// is somewhat easy to hit with template metaprogramming we'd prefer to |
1746 | /// keep it as large as possible. At the moment it has been left as a |
1747 | /// non-bitfield since this type safely fits in 64 bits as an unsigned, so |
1748 | /// there is no reason to introduce the performance impact of a bitfield. |
1749 | /// |
1750 | /// This field will only have a non-zero value when some of the parameter |
1751 | /// packs that occur within the pattern have been substituted but others |
1752 | /// have not. |
1753 | unsigned NumExpansions; |
1754 | }; |
1755 | |
1756 | union { |
1757 | TypeBitfields TypeBits; |
1758 | ArrayTypeBitfields ArrayTypeBits; |
1759 | ConstantArrayTypeBitfields ConstantArrayTypeBits; |
1760 | AttributedTypeBitfields AttributedTypeBits; |
1761 | AutoTypeBitfields AutoTypeBits; |
1762 | BuiltinTypeBitfields BuiltinTypeBits; |
1763 | FunctionTypeBitfields FunctionTypeBits; |
1764 | ObjCObjectTypeBitfields ObjCObjectTypeBits; |
1765 | ReferenceTypeBitfields ReferenceTypeBits; |
1766 | TypeWithKeywordBitfields TypeWithKeywordBits; |
1767 | ElaboratedTypeBitfields ElaboratedTypeBits; |
1768 | VectorTypeBitfields VectorTypeBits; |
1769 | SubstTemplateTypeParmPackTypeBitfields SubstTemplateTypeParmPackTypeBits; |
1770 | TemplateSpecializationTypeBitfields TemplateSpecializationTypeBits; |
1771 | DependentTemplateSpecializationTypeBitfields |
1772 | DependentTemplateSpecializationTypeBits; |
1773 | PackExpansionTypeBitfields PackExpansionTypeBits; |
1774 | |
1775 | static_assert(sizeof(TypeBitfields) <= 8, |
1776 | "TypeBitfields is larger than 8 bytes!"); |
1777 | static_assert(sizeof(ArrayTypeBitfields) <= 8, |
1778 | "ArrayTypeBitfields is larger than 8 bytes!"); |
1779 | static_assert(sizeof(AttributedTypeBitfields) <= 8, |
1780 | "AttributedTypeBitfields is larger than 8 bytes!"); |
1781 | static_assert(sizeof(AutoTypeBitfields) <= 8, |
1782 | "AutoTypeBitfields is larger than 8 bytes!"); |
1783 | static_assert(sizeof(BuiltinTypeBitfields) <= 8, |
1784 | "BuiltinTypeBitfields is larger than 8 bytes!"); |
1785 | static_assert(sizeof(FunctionTypeBitfields) <= 8, |
1786 | "FunctionTypeBitfields is larger than 8 bytes!"); |
1787 | static_assert(sizeof(ObjCObjectTypeBitfields) <= 8, |
1788 | "ObjCObjectTypeBitfields is larger than 8 bytes!"); |
1789 | static_assert(sizeof(ReferenceTypeBitfields) <= 8, |
1790 | "ReferenceTypeBitfields is larger than 8 bytes!"); |
1791 | static_assert(sizeof(TypeWithKeywordBitfields) <= 8, |
1792 | "TypeWithKeywordBitfields is larger than 8 bytes!"); |
1793 | static_assert(sizeof(ElaboratedTypeBitfields) <= 8, |
1794 | "ElaboratedTypeBitfields is larger than 8 bytes!"); |
1795 | static_assert(sizeof(VectorTypeBitfields) <= 8, |
1796 | "VectorTypeBitfields is larger than 8 bytes!"); |
1797 | static_assert(sizeof(SubstTemplateTypeParmPackTypeBitfields) <= 8, |
1798 | "SubstTemplateTypeParmPackTypeBitfields is larger" |
1799 | " than 8 bytes!"); |
1800 | static_assert(sizeof(TemplateSpecializationTypeBitfields) <= 8, |
1801 | "TemplateSpecializationTypeBitfields is larger" |
1802 | " than 8 bytes!"); |
1803 | static_assert(sizeof(DependentTemplateSpecializationTypeBitfields) <= 8, |
1804 | "DependentTemplateSpecializationTypeBitfields is larger" |
1805 | " than 8 bytes!"); |
1806 | static_assert(sizeof(PackExpansionTypeBitfields) <= 8, |
1807 | "PackExpansionTypeBitfields is larger than 8 bytes"); |
1808 | }; |
1809 | |
1810 | private: |
1811 | template <class T> friend class TypePropertyCache; |
1812 | |
1813 | /// Set whether this type comes from an AST file. |
1814 | void setFromAST(bool V = true) const { |
1815 | TypeBits.FromAST = V; |
1816 | } |
1817 | |
1818 | protected: |
1819 | friend class ASTContext; |
1820 | |
1821 | Type(TypeClass tc, QualType canon, bool Dependent, |
1822 | bool InstantiationDependent, bool VariablyModified, |
1823 | bool ContainsUnexpandedParameterPack) |
1824 | : ExtQualsTypeCommonBase(this, |
1825 | canon.isNull() ? QualType(this_(), 0) : canon) { |
1826 | auto Deps = TypeDependence::None; |
1827 | if (Dependent) |
1828 | Deps |= TypeDependence::Dependent | TypeDependence::Instantiation; |
1829 | if (InstantiationDependent) |
1830 | Deps |= TypeDependence::Instantiation; |
1831 | if (ContainsUnexpandedParameterPack) |
1832 | Deps |= TypeDependence::UnexpandedPack; |
1833 | if (VariablyModified) |
1834 | Deps |= TypeDependence::VariablyModified; |
1835 | |
1836 | TypeBits.TC = tc; |
1837 | TypeBits.Dependence = static_cast<unsigned>(Deps); |
1838 | TypeBits.CacheValid = false; |
1839 | TypeBits.CachedLocalOrUnnamed = false; |
1840 | TypeBits.CachedLinkage = NoLinkage; |
1841 | TypeBits.FromAST = false; |
1842 | } |
1843 | |
1844 | // silence VC++ warning C4355: 'this' : used in base member initializer list |
1845 | Type *this_() { return this; } |
1846 | |
1847 | void setDependent(bool D = true) { |
1848 | if (!D) { |
1849 | TypeBits.Dependence &= ~static_cast<unsigned>(TypeDependence::Dependent); |
1850 | return; |
1851 | } |
1852 | TypeBits.Dependence |= static_cast<unsigned>(TypeDependence::Dependent | |
1853 | TypeDependence::Instantiation); |
1854 | } |
1855 | |
1856 | void setInstantiationDependent(bool D = true) { |
1857 | if (D) |
1858 | TypeBits.Dependence |= |
1859 | static_cast<unsigned>(TypeDependence::Instantiation); |
1860 | else |
1861 | TypeBits.Dependence &= |
1862 | ~static_cast<unsigned>(TypeDependence::Instantiation); |
1863 | } |
1864 | |
1865 | void setVariablyModified(bool VM = true) { |
1866 | if (VM) |
1867 | TypeBits.Dependence |= |
1868 | static_cast<unsigned>(TypeDependence::VariablyModified); |
1869 | else |
1870 | TypeBits.Dependence &= |
1871 | ~static_cast<unsigned>(TypeDependence::VariablyModified); |
1872 | } |
1873 | |
1874 | void setContainsUnexpandedParameterPack(bool PP = true) { |
1875 | if (PP) |
1876 | TypeBits.Dependence |= |
1877 | static_cast<unsigned>(TypeDependence::UnexpandedPack); |
1878 | else |
1879 | TypeBits.Dependence &= |
1880 | ~static_cast<unsigned>(TypeDependence::UnexpandedPack); |
1881 | } |
1882 | |
1883 | public: |
1884 | friend class ASTReader; |
1885 | friend class ASTWriter; |
1886 | template <class T> friend class serialization::AbstractTypeReader; |
1887 | template <class T> friend class serialization::AbstractTypeWriter; |
1888 | |
1889 | Type(const Type &) = delete; |
1890 | Type(Type &&) = delete; |
1891 | Type &operator=(const Type &) = delete; |
1892 | Type &operator=(Type &&) = delete; |
1893 | |
1894 | TypeClass getTypeClass() const { return static_cast<TypeClass>(TypeBits.TC); } |
1895 | |
1896 | /// Whether this type comes from an AST file. |
1897 | bool isFromAST() const { return TypeBits.FromAST; } |
1898 | |
1899 | /// Whether this type is or contains an unexpanded parameter |
1900 | /// pack, used to support C++0x variadic templates. |
1901 | /// |
1902 | /// A type that contains a parameter pack shall be expanded by the |
1903 | /// ellipsis operator at some point. For example, the typedef in the |
1904 | /// following example contains an unexpanded parameter pack 'T': |
1905 | /// |
1906 | /// \code |
1907 | /// template<typename ...T> |
1908 | /// struct X { |
1909 | /// typedef T* pointer_types; // ill-formed; T is a parameter pack. |
1910 | /// }; |
1911 | /// \endcode |
1912 | /// |
1913 | /// Note that this routine does not specify which |
1914 | bool containsUnexpandedParameterPack() const { |
1915 | return getDependence() & TypeDependence::UnexpandedPack; |
1916 | } |
1917 | |
1918 | /// Determines if this type would be canonical if it had no further |
1919 | /// qualification. |
1920 | bool isCanonicalUnqualified() const { |
1921 | return CanonicalType == QualType(this, 0); |
1922 | } |
1923 | |
1924 | /// Pull a single level of sugar off of this locally-unqualified type. |
1925 | /// Users should generally prefer SplitQualType::getSingleStepDesugaredType() |
1926 | /// or QualType::getSingleStepDesugaredType(const ASTContext&). |
1927 | QualType getLocallyUnqualifiedSingleStepDesugaredType() const; |
1928 | |
1929 | /// Types are partitioned into 3 broad categories (C99 6.2.5p1): |
1930 | /// object types, function types, and incomplete types. |
1931 | |
1932 | /// Return true if this is an incomplete type. |
1933 | /// A type that can describe objects, but which lacks information needed to |
1934 | /// determine its size (e.g. void, or a fwd declared struct). Clients of this |
1935 | /// routine will need to determine if the size is actually required. |
1936 | /// |
1937 | /// Def If non-null, and the type refers to some kind of declaration |
1938 | /// that can be completed (such as a C struct, C++ class, or Objective-C |
1939 | /// class), will be set to the declaration. |
1940 | bool isIncompleteType(NamedDecl **Def = nullptr) const; |
1941 | |
1942 | /// Return true if this is an incomplete or object |
1943 | /// type, in other words, not a function type. |
1944 | bool isIncompleteOrObjectType() const { |
1945 | return !isFunctionType(); |
1946 | } |
1947 | |
1948 | /// Determine whether this type is an object type. |
1949 | bool isObjectType() const { |
1950 | // C++ [basic.types]p8: |
1951 | // An object type is a (possibly cv-qualified) type that is not a |
1952 | // function type, not a reference type, and not a void type. |
1953 | return !isReferenceType() && !isFunctionType() && !isVoidType(); |
1954 | } |
1955 | |
1956 | /// Return true if this is a literal type |
1957 | /// (C++11 [basic.types]p10) |
1958 | bool isLiteralType(const ASTContext &Ctx) const; |
1959 | |
1960 | /// Test if this type is a standard-layout type. |
1961 | /// (C++0x [basic.type]p9) |
1962 | bool isStandardLayoutType() const; |
1963 | |
1964 | /// Helper methods to distinguish type categories. All type predicates |
1965 | /// operate on the canonical type, ignoring typedefs and qualifiers. |
1966 | |
1967 | /// Returns true if the type is a builtin type. |
1968 | bool isBuiltinType() const; |
1969 | |
1970 | /// Test for a particular builtin type. |
1971 | bool isSpecificBuiltinType(unsigned K) const; |
1972 | |
1973 | /// Test for a type which does not represent an actual type-system type but |
1974 | /// is instead used as a placeholder for various convenient purposes within |
1975 | /// Clang. All such types are BuiltinTypes. |
1976 | bool isPlaceholderType() const; |
1977 | const BuiltinType *getAsPlaceholderType() const; |
1978 | |
1979 | /// Test for a specific placeholder type. |
1980 | bool isSpecificPlaceholderType(unsigned K) const; |
1981 | |
1982 | /// Test for a placeholder type other than Overload; see |
1983 | /// BuiltinType::isNonOverloadPlaceholderType. |
1984 | bool isNonOverloadPlaceholderType() const; |
1985 | |
1986 | /// isIntegerType() does *not* include complex integers (a GCC extension). |
1987 | /// isComplexIntegerType() can be used to test for complex integers. |
1988 | bool isIntegerType() const; // C99 6.2.5p17 (int, char, bool, enum) |
1989 | bool isEnumeralType() const; |
1990 | |
1991 | /// Determine whether this type is a scoped enumeration type. |
1992 | bool isScopedEnumeralType() const; |
1993 | bool isBooleanType() const; |
1994 | bool isCharType() const; |
1995 | bool isWideCharType() const; |
1996 | bool isChar8Type() const; |
1997 | bool isChar16Type() const; |
1998 | bool isChar32Type() const; |
1999 | bool isAnyCharacterType() const; |
2000 | bool isIntegralType(const ASTContext &Ctx) const; |
2001 | |
2002 | /// Determine whether this type is an integral or enumeration type. |
2003 | bool isIntegralOrEnumerationType() const; |
2004 | |
2005 | /// Determine whether this type is an integral or unscoped enumeration type. |
2006 | bool isIntegralOrUnscopedEnumerationType() const; |
2007 | bool isUnscopedEnumerationType() const; |
2008 | |
2009 | /// Floating point categories. |
2010 | bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double) |
2011 | /// isComplexType() does *not* include complex integers (a GCC extension). |
2012 | /// isComplexIntegerType() can be used to test for complex integers. |
2013 | bool isComplexType() const; // C99 6.2.5p11 (complex) |
2014 | bool isAnyComplexType() const; // C99 6.2.5p11 (complex) + Complex Int. |
2015 | bool isFloatingType() const; // C99 6.2.5p11 (real floating + complex) |
2016 | bool isHalfType() const; // OpenCL 6.1.1.1, NEON (IEEE 754-2008 half) |
2017 | bool isFloat16Type() const; // C11 extension ISO/IEC TS 18661 |
2018 | bool isFloat128Type() const; |
2019 | bool isRealType() const; // C99 6.2.5p17 (real floating + integer) |
2020 | bool isArithmeticType() const; // C99 6.2.5p18 (integer + floating) |
2021 | bool isVoidType() const; // C99 6.2.5p19 |
2022 | bool isScalarType() const; // C99 6.2.5p21 (arithmetic + pointers) |
2023 | bool isAggregateType() const; |
2024 | bool isFundamentalType() const; |
2025 | bool isCompoundType() const; |
2026 | |
2027 | // Type Predicates: Check to see if this type is structurally the specified |
2028 | // type, ignoring typedefs and qualifiers. |
2029 | bool isFunctionType() const; |
2030 | bool isFunctionNoProtoType() const { return getAs<FunctionNoProtoType>(); } |
2031 | bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); } |
2032 | bool isPointerType() const; |
2033 | bool isAnyPointerType() const; // Any C pointer or ObjC object pointer |
2034 | bool isBlockPointerType() const; |
2035 | bool isVoidPointerType() const; |
2036 | bool isReferenceType() const; |
2037 | bool isLValueReferenceType() const; |
2038 | bool isRValueReferenceType() const; |
2039 | bool isObjectPointerType() const; |
2040 | bool isFunctionPointerType() const; |
2041 | bool isFunctionReferenceType() const; |
2042 | bool isMemberPointerType() const; |
2043 | bool isMemberFunctionPointerType() const; |
2044 | bool isMemberDataPointerType() const; |
2045 | bool isArrayType() const; |
2046 | bool isConstantArrayType() const; |
2047 | bool isIncompleteArrayType() const; |
2048 | bool isVariableArrayType() const; |
2049 | bool isDependentSizedArrayType() const; |
2050 | bool isRecordType() const; |
2051 | bool isClassType() const; |
2052 | bool isStructureType() const; |
2053 | bool isObjCBoxableRecordType() const; |
2054 | bool isInterfaceType() const; |
2055 | bool isStructureOrClassType() const; |
2056 | bool isUnionType() const; |
2057 | bool isComplexIntegerType() const; // GCC _Complex integer type. |
2058 | bool isVectorType() const; // GCC vector type. |
2059 | bool isExtVectorType() const; // Extended vector type. |
2060 | bool isDependentAddressSpaceType() const; // value-dependent address space qualifier |
2061 | bool isObjCObjectPointerType() const; // pointer to ObjC object |
2062 | bool isObjCRetainableType() const; // ObjC object or block pointer |
2063 | bool isObjCLifetimeType() const; // (array of)* retainable type |
2064 | bool isObjCIndirectLifetimeType() const; // (pointer to)* lifetime type |
2065 | bool isObjCNSObjectType() const; // __attribute__((NSObject)) |
2066 | bool isObjCIndependentClassType() const; // __attribute__((objc_independent_class)) |
2067 | // FIXME: change this to 'raw' interface type, so we can used 'interface' type |
2068 | // for the common case. |
2069 | bool isObjCObjectType() const; // NSString or typeof(*(id)0) |
2070 | bool isObjCQualifiedInterfaceType() const; // NSString<foo> |
2071 | bool isObjCQualifiedIdType() const; // id<foo> |
2072 | bool isObjCQualifiedClassType() const; // Class<foo> |
2073 | bool isObjCObjectOrInterfaceType() const; |
2074 | bool isObjCIdType() const; // id |
2075 | bool isDecltypeType() const; |
2076 | /// Was this type written with the special inert-in-ARC __unsafe_unretained |
2077 | /// qualifier? |
2078 | /// |
2079 | /// This approximates the answer to the following question: if this |
2080 | /// translation unit were compiled in ARC, would this type be qualified |
2081 | /// with __unsafe_unretained? |
2082 | bool isObjCInertUnsafeUnretainedType() const { |
2083 | return hasAttr(attr::ObjCInertUnsafeUnretained); |
2084 | } |
2085 | |
2086 | /// Whether the type is Objective-C 'id' or a __kindof type of an |
2087 | /// object type, e.g., __kindof NSView * or __kindof id |
2088 | /// <NSCopying>. |
2089 | /// |
2090 | /// \param bound Will be set to the bound on non-id subtype types, |
2091 | /// which will be (possibly specialized) Objective-C class type, or |
2092 | /// null for 'id. |
2093 | bool isObjCIdOrObjectKindOfType(const ASTContext &ctx, |
2094 | const ObjCObjectType *&bound) const; |
2095 | |
2096 | bool isObjCClassType() const; // Class |
2097 | |
2098 | /// Whether the type is Objective-C 'Class' or a __kindof type of an |
2099 | /// Class type, e.g., __kindof Class <NSCopying>. |
2100 | /// |
2101 | /// Unlike \c isObjCIdOrObjectKindOfType, there is no relevant bound |
2102 | /// here because Objective-C's type system cannot express "a class |
2103 | /// object for a subclass of NSFoo". |
2104 | bool isObjCClassOrClassKindOfType() const; |
2105 | |
2106 | bool isBlockCompatibleObjCPointerType(ASTContext &ctx) const; |
2107 | bool isObjCSelType() const; // Class |
2108 | bool isObjCBuiltinType() const; // 'id' or 'Class' |
2109 | bool isObjCARCBridgableType() const; |
2110 | bool isCARCBridgableType() const; |
2111 | bool isTemplateTypeParmType() const; // C++ template type parameter |
2112 | bool isNullPtrType() const; // C++11 std::nullptr_t |
2113 | bool isNothrowT() const; // C++ std::nothrow_t |
2114 | bool isAlignValT() const; // C++17 std::align_val_t |
2115 | bool isStdByteType() const; // C++17 std::byte |
2116 | bool isAtomicType() const; // C11 _Atomic() |
2117 | bool isUndeducedAutoType() const; // C++11 auto or |
2118 | // C++14 decltype(auto) |
2119 | |
2120 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
2121 | bool is##Id##Type() const; |
2122 | #include "clang/Basic/OpenCLImageTypes.def" |
2123 | |
2124 | bool isImageType() const; // Any OpenCL image type |
2125 | |
2126 | bool isSamplerT() const; // OpenCL sampler_t |
2127 | bool isEventT() const; // OpenCL event_t |
2128 | bool isClkEventT() const; // OpenCL clk_event_t |
2129 | bool isQueueT() const; // OpenCL queue_t |
2130 | bool isReserveIDT() const; // OpenCL reserve_id_t |
2131 | |
2132 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
2133 | bool is##Id##Type() const; |
2134 | #include "clang/Basic/OpenCLExtensionTypes.def" |
2135 | // Type defined in cl_intel_device_side_avc_motion_estimation OpenCL extension |
2136 | bool isOCLIntelSubgroupAVCType() const; |
2137 | bool isOCLExtOpaqueType() const; // Any OpenCL extension type |
2138 | |
2139 | bool isPipeType() const; // OpenCL pipe type |
2140 | bool isOpenCLSpecificType() const; // Any OpenCL specific type |
2141 | |
2142 | /// Determines if this type, which must satisfy |
2143 | /// isObjCLifetimeType(), is implicitly __unsafe_unretained rather |
2144 | /// than implicitly __strong. |
2145 | bool isObjCARCImplicitlyUnretainedType() const; |
2146 | |
2147 | /// Return the implicit lifetime for this type, which must not be dependent. |
2148 | Qualifiers::ObjCLifetime getObjCARCImplicitLifetime() const; |
2149 | |
2150 | enum ScalarTypeKind { |
2151 | STK_CPointer, |
2152 | STK_BlockPointer, |
2153 | STK_ObjCObjectPointer, |
2154 | STK_MemberPointer, |
2155 | STK_Bool, |
2156 | STK_Integral, |
2157 | STK_Floating, |
2158 | STK_IntegralComplex, |
2159 | STK_FloatingComplex, |
2160 | STK_FixedPoint |
2161 | }; |
2162 | |
2163 | /// Given that this is a scalar type, classify it. |
2164 | ScalarTypeKind getScalarTypeKind() const; |
2165 | |
2166 | TypeDependence getDependence() const { |
2167 | return static_cast<TypeDependence>(TypeBits.Dependence); |
2168 | } |
2169 | |
2170 | /// Whether this type is a dependent type, meaning that its definition |
2171 | /// somehow depends on a template parameter (C++ [temp.dep.type]). |
2172 | bool isDependentType() const { |
2173 | return getDependence() & TypeDependence::Dependent; |
2174 | } |
2175 | |
2176 | /// Determine whether this type is an instantiation-dependent type, |
2177 | /// meaning that the type involves a template parameter (even if the |
2178 | /// definition does not actually depend on the type substituted for that |
2179 | /// template parameter). |
2180 | bool isInstantiationDependentType() const { |
2181 | return getDependence() & TypeDependence::Instantiation; |
2182 | } |
2183 | |
2184 | /// Determine whether this type is an undeduced type, meaning that |
2185 | /// it somehow involves a C++11 'auto' type or similar which has not yet been |
2186 | /// deduced. |
2187 | bool isUndeducedType() const; |
2188 | |
2189 | /// Whether this type is a variably-modified type (C99 6.7.5). |
2190 | bool isVariablyModifiedType() const { |
2191 | return getDependence() & TypeDependence::VariablyModified; |
2192 | } |
2193 | |
2194 | /// Whether this type involves a variable-length array type |
2195 | /// with a definite size. |
2196 | bool hasSizedVLAType() const; |
2197 | |
2198 | /// Whether this type is or contains a local or unnamed type. |
2199 | bool hasUnnamedOrLocalType() const; |
2200 | |
2201 | bool isOverloadableType() const; |
2202 | |
2203 | /// Determine wither this type is a C++ elaborated-type-specifier. |
2204 | bool isElaboratedTypeSpecifier() const; |
2205 | |
2206 | bool canDecayToPointerType() const; |
2207 | |
2208 | /// Whether this type is represented natively as a pointer. This includes |
2209 | /// pointers, references, block pointers, and Objective-C interface, |
2210 | /// qualified id, and qualified interface types, as well as nullptr_t. |
2211 | bool hasPointerRepresentation() const; |
2212 | |
2213 | /// Whether this type can represent an objective pointer type for the |
2214 | /// purpose of GC'ability |
2215 | bool hasObjCPointerRepresentation() const; |
2216 | |
2217 | /// Determine whether this type has an integer representation |
2218 | /// of some sort, e.g., it is an integer type or a vector. |
2219 | bool hasIntegerRepresentation() const; |
2220 | |
2221 | /// Determine whether this type has an signed integer representation |
2222 | /// of some sort, e.g., it is an signed integer type or a vector. |
2223 | bool hasSignedIntegerRepresentation() const; |
2224 | |
2225 | /// Determine whether this type has an unsigned integer representation |
2226 | /// of some sort, e.g., it is an unsigned integer type or a vector. |
2227 | bool hasUnsignedIntegerRepresentation() const; |
2228 | |
2229 | /// Determine whether this type has a floating-point representation |
2230 | /// of some sort, e.g., it is a floating-point type or a vector thereof. |
2231 | bool hasFloatingRepresentation() const; |
2232 | |
2233 | // Type Checking Functions: Check to see if this type is structurally the |
2234 | // specified type, ignoring typedefs and qualifiers, and return a pointer to |
2235 | // the best type we can. |
2236 | const RecordType *getAsStructureType() const; |
2237 | /// NOTE: getAs*ArrayType are methods on ASTContext. |
2238 | const RecordType *getAsUnionType() const; |
2239 | const ComplexType *getAsComplexIntegerType() const; // GCC complex int type. |
2240 | const ObjCObjectType *getAsObjCInterfaceType() const; |
2241 | |
2242 | // The following is a convenience method that returns an ObjCObjectPointerType |
2243 | // for object declared using an interface. |
2244 | const ObjCObjectPointerType *getAsObjCInterfacePointerType() const; |
2245 | const ObjCObjectPointerType *getAsObjCQualifiedIdType() const; |
2246 | const ObjCObjectPointerType *getAsObjCQualifiedClassType() const; |
2247 | const ObjCObjectType *getAsObjCQualifiedInterfaceType() const; |
2248 | |
2249 | /// Retrieves the CXXRecordDecl that this type refers to, either |
2250 | /// because the type is a RecordType or because it is the injected-class-name |
2251 | /// type of a class template or class template partial specialization. |
2252 | CXXRecordDecl *getAsCXXRecordDecl() const; |
2253 | |
2254 | /// Retrieves the RecordDecl this type refers to. |
2255 | RecordDecl *getAsRecordDecl() const; |
2256 | |
2257 | /// Retrieves the TagDecl that this type refers to, either |
2258 | /// because the type is a TagType or because it is the injected-class-name |
2259 | /// type of a class template or class template partial specialization. |
2260 | TagDecl *getAsTagDecl() const; |
2261 | |
2262 | /// If this is a pointer or reference to a RecordType, return the |
2263 | /// CXXRecordDecl that the type refers to. |
2264 | /// |
2265 | /// If this is not a pointer or reference, or the type being pointed to does |
2266 | /// not refer to a CXXRecordDecl, returns NULL. |
2267 | const CXXRecordDecl *getPointeeCXXRecordDecl() const; |
2268 | |
2269 | /// Get the DeducedType whose type will be deduced for a variable with |
2270 | /// an initializer of this type. This looks through declarators like pointer |
2271 | /// types, but not through decltype or typedefs. |
2272 | DeducedType *getContainedDeducedType() const; |
2273 | |
2274 | /// Get the AutoType whose type will be deduced for a variable with |
2275 | /// an initializer of this type. This looks through declarators like pointer |
2276 | /// types, but not through decltype or typedefs. |
2277 | AutoType *getContainedAutoType() const { |
2278 | return dyn_cast_or_null<AutoType>(getContainedDeducedType()); |
2279 | } |
2280 | |
2281 | /// Determine whether this type was written with a leading 'auto' |
2282 | /// corresponding to a trailing return type (possibly for a nested |
2283 | /// function type within a pointer to function type or similar). |
2284 | bool hasAutoForTrailingReturnType() const; |
2285 | |
2286 | /// Member-template getAs<specific type>'. Look through sugar for |
2287 | /// an instance of \<specific type>. This scheme will eventually |
2288 | /// replace the specific getAsXXXX methods above. |
2289 | /// |
2290 | /// There are some specializations of this member template listed |
2291 | /// immediately following this class. |
2292 | template <typename T> const T *getAs() const; |
2293 | |
2294 | /// Member-template getAsAdjusted<specific type>. Look through specific kinds |
2295 | /// of sugar (parens, attributes, etc) for an instance of \<specific type>. |
2296 | /// This is used when you need to walk over sugar nodes that represent some |
2297 | /// kind of type adjustment from a type that was written as a \<specific type> |
2298 | /// to another type that is still canonically a \<specific type>. |
2299 | template <typename T> const T *getAsAdjusted() const; |
2300 | |
2301 | /// A variant of getAs<> for array types which silently discards |
2302 | /// qualifiers from the outermost type. |
2303 | const ArrayType *getAsArrayTypeUnsafe() const; |
2304 | |
2305 | /// Member-template castAs<specific type>. Look through sugar for |
2306 | /// the underlying instance of \<specific type>. |
2307 | /// |
2308 | /// This method has the same relationship to getAs<T> as cast<T> has |
2309 | /// to dyn_cast<T>; which is to say, the underlying type *must* |
2310 | /// have the intended type, and this method will never return null. |
2311 | template <typename T> const T *castAs() const; |
2312 | |
2313 | /// A variant of castAs<> for array type which silently discards |
2314 | /// qualifiers from the outermost type. |
2315 | const ArrayType *castAsArrayTypeUnsafe() const; |
2316 | |
2317 | /// Determine whether this type had the specified attribute applied to it |
2318 | /// (looking through top-level type sugar). |
2319 | bool hasAttr(attr::Kind AK) const; |
2320 | |
2321 | /// Get the base element type of this type, potentially discarding type |
2322 | /// qualifiers. This should never be used when type qualifiers |
2323 | /// are meaningful. |
2324 | const Type *getBaseElementTypeUnsafe() const; |
2325 | |
2326 | /// If this is an array type, return the element type of the array, |
2327 | /// potentially with type qualifiers missing. |
2328 | /// This should never be used when type qualifiers are meaningful. |
2329 | const Type *getArrayElementTypeNoTypeQual() const; |
2330 | |
2331 | /// If this is a pointer type, return the pointee type. |
2332 | /// If this is an array type, return the array element type. |
2333 | /// This should never be used when type qualifiers are meaningful. |
2334 | const Type *getPointeeOrArrayElementType() const; |
2335 | |
2336 | /// If this is a pointer, ObjC object pointer, or block |
2337 | /// pointer, this returns the respective pointee. |
2338 | QualType getPointeeType() const; |
2339 | |
2340 | /// Return the specified type with any "sugar" removed from the type, |
2341 | /// removing any typedefs, typeofs, etc., as well as any qualifiers. |
2342 | const Type *getUnqualifiedDesugaredType() const; |
2343 | |
2344 | /// More type predicates useful for type checking/promotion |
2345 | bool isPromotableIntegerType() const; // C99 6.3.1.1p2 |
2346 | |
2347 | /// Return true if this is an integer type that is |
2348 | /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], |
2349 | /// or an enum decl which has a signed representation. |
2350 | bool isSignedIntegerType() const; |
2351 | |
2352 | /// Return true if this is an integer type that is |
2353 | /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], |
2354 | /// or an enum decl which has an unsigned representation. |
2355 | bool isUnsignedIntegerType() const; |
2356 | |
2357 | /// Determines whether this is an integer type that is signed or an |
2358 | /// enumeration types whose underlying type is a signed integer type. |
2359 | bool isSignedIntegerOrEnumerationType() const; |
2360 | |
2361 | /// Determines whether this is an integer type that is unsigned or an |
2362 | /// enumeration types whose underlying type is a unsigned integer type. |
2363 | bool isUnsignedIntegerOrEnumerationType() const; |
2364 | |
2365 | /// Return true if this is a fixed point type according to |
2366 | /// ISO/IEC JTC1 SC22 WG14 N1169. |
2367 | bool isFixedPointType() const; |
2368 | |
2369 | /// Return true if this is a fixed point or integer type. |
2370 | bool isFixedPointOrIntegerType() const; |
2371 | |
2372 | /// Return true if this is a saturated fixed point type according to |
2373 | /// ISO/IEC JTC1 SC22 WG14 N1169. This type can be signed or unsigned. |
2374 | bool isSaturatedFixedPointType() const; |
2375 | |
2376 | /// Return true if this is a saturated fixed point type according to |
2377 | /// ISO/IEC JTC1 SC22 WG14 N1169. This type can be signed or unsigned. |
2378 | bool isUnsaturatedFixedPointType() const; |
2379 | |
2380 | /// Return true if this is a fixed point type that is signed according |
2381 | /// to ISO/IEC JTC1 SC22 WG14 N1169. This type can also be saturated. |
2382 | bool isSignedFixedPointType() const; |
2383 | |
2384 | /// Return true if this is a fixed point type that is unsigned according |
2385 | /// to ISO/IEC JTC1 SC22 WG14 N1169. This type can also be saturated. |
2386 | bool isUnsignedFixedPointType() const; |
2387 | |
2388 | /// Return true if this is not a variable sized type, |
2389 | /// according to the rules of C99 6.7.5p3. It is not legal to call this on |
2390 | /// incomplete types. |
2391 | bool isConstantSizeType() const; |
2392 | |
2393 | /// Returns true if this type can be represented by some |
2394 | /// set of type specifiers. |
2395 | bool isSpecifierType() const; |
2396 | |
2397 | /// Determine the linkage of this type. |
2398 | Linkage getLinkage() const; |
2399 | |
2400 | /// Determine the visibility of this type. |
2401 | Visibility getVisibility() const { |
2402 | return getLinkageAndVisibility().getVisibility(); |
2403 | } |
2404 | |
2405 | /// Return true if the visibility was explicitly set is the code. |
2406 | bool isVisibilityExplicit() const { |
2407 | return getLinkageAndVisibility().isVisibilityExplicit(); |
2408 | } |
2409 | |
2410 | /// Determine the linkage and visibility of this type. |
2411 | LinkageInfo getLinkageAndVisibility() const; |
2412 | |
2413 | /// True if the computed linkage is valid. Used for consistency |
2414 | /// checking. Should always return true. |
2415 | bool isLinkageValid() const; |
2416 | |
2417 | /// Determine the nullability of the given type. |
2418 | /// |
2419 | /// Note that nullability is only captured as sugar within the type |
2420 | /// system, not as part of the canonical type, so nullability will |
2421 | /// be lost by canonicalization and desugaring. |
2422 | Optional<NullabilityKind> getNullability(const ASTContext &context) const; |
2423 | |
2424 | /// Determine whether the given type can have a nullability |
2425 | /// specifier applied to it, i.e., if it is any kind of pointer type. |
2426 | /// |
2427 | /// \param ResultIfUnknown The value to return if we don't yet know whether |
2428 | /// this type can have nullability because it is dependent. |
2429 | bool canHaveNullability(bool ResultIfUnknown = true) const; |
2430 | |
2431 | /// Retrieve the set of substitutions required when accessing a member |
2432 | /// of the Objective-C receiver type that is declared in the given context. |
2433 | /// |
2434 | /// \c *this is the type of the object we're operating on, e.g., the |
2435 | /// receiver for a message send or the base of a property access, and is |
2436 | /// expected to be of some object or object pointer type. |
2437 | /// |
2438 | /// \param dc The declaration context for which we are building up a |
2439 | /// substitution mapping, which should be an Objective-C class, extension, |
2440 | /// category, or method within. |
2441 | /// |
2442 | /// \returns an array of type arguments that can be substituted for |
2443 | /// the type parameters of the given declaration context in any type described |
2444 | /// within that context, or an empty optional to indicate that no |
2445 | /// substitution is required. |
2446 | Optional<ArrayRef<QualType>> |
2447 | getObjCSubstitutions(const DeclContext *dc) const; |
2448 | |
2449 | /// Determines if this is an ObjC interface type that may accept type |
2450 | /// parameters. |
2451 | bool acceptsObjCTypeParams() const; |
2452 | |
2453 | const char *getTypeClassName() const; |
2454 | |
2455 | QualType getCanonicalTypeInternal() const { |
2456 | return CanonicalType; |
2457 | } |
2458 | |
2459 | CanQualType getCanonicalTypeUnqualified() const; // in CanonicalType.h |
2460 | void dump() const; |
2461 | void dump(llvm::raw_ostream &OS) const; |
2462 | }; |
2463 | |
2464 | /// This will check for a TypedefType by removing any existing sugar |
2465 | /// until it reaches a TypedefType or a non-sugared type. |
2466 | template <> const TypedefType *Type::getAs() const; |
2467 | |
2468 | /// This will check for a TemplateSpecializationType by removing any |
2469 | /// existing sugar until it reaches a TemplateSpecializationType or a |
2470 | /// non-sugared type. |
2471 | template <> const TemplateSpecializationType *Type::getAs() const; |
2472 | |
2473 | /// This will check for an AttributedType by removing any existing sugar |
2474 | /// until it reaches an AttributedType or a non-sugared type. |
2475 | template <> const AttributedType *Type::getAs() const; |
2476 | |
2477 | // We can do canonical leaf types faster, because we don't have to |
2478 | // worry about preserving child type decoration. |
2479 | #define TYPE(Class, Base) |
2480 | #define LEAF_TYPE(Class) \ |
2481 | template <> inline const Class##Type *Type::getAs() const { \ |
2482 | return dyn_cast<Class##Type>(CanonicalType); \ |
2483 | } \ |
2484 | template <> inline const Class##Type *Type::castAs() const { \ |
2485 | return cast<Class##Type>(CanonicalType); \ |
2486 | } |
2487 | #include "clang/AST/TypeNodes.inc" |
2488 | |
2489 | /// This class is used for builtin types like 'int'. Builtin |
2490 | /// types are always canonical and have a literal name field. |
2491 | class BuiltinType : public Type { |
2492 | public: |
2493 | enum Kind { |
2494 | // OpenCL image types |
2495 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) Id, |
2496 | #include "clang/Basic/OpenCLImageTypes.def" |
2497 | // OpenCL extension types |
2498 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) Id, |
2499 | #include "clang/Basic/OpenCLExtensionTypes.def" |
2500 | // SVE Types |
2501 | #define SVE_TYPE(Name, Id, SingletonId) Id, |
2502 | #include "clang/Basic/AArch64SVEACLETypes.def" |
2503 | // All other builtin types |
2504 | #define BUILTIN_TYPE(Id, SingletonId) Id, |
2505 | #define LAST_BUILTIN_TYPE(Id) LastKind = Id |
2506 | #include "clang/AST/BuiltinTypes.def" |
2507 | }; |
2508 | |
2509 | private: |
2510 | friend class ASTContext; // ASTContext creates these. |
2511 | |
2512 | BuiltinType(Kind K) |
2513 | : Type(Builtin, QualType(), /*Dependent=*/(K == Dependent), |
2514 | /*InstantiationDependent=*/(K == Dependent), |
2515 | /*VariablyModified=*/false, |
2516 | /*Unexpanded parameter pack=*/false) { |
2517 | BuiltinTypeBits.Kind = K; |
2518 | } |
2519 | |
2520 | public: |
2521 | Kind getKind() const { return static_cast<Kind>(BuiltinTypeBits.Kind); } |
2522 | StringRef getName(const PrintingPolicy &Policy) const; |
2523 | |
2524 | const char *getNameAsCString(const PrintingPolicy &Policy) const { |
2525 | // The StringRef is null-terminated. |
2526 | StringRef str = getName(Policy); |
2527 | assert(!str.empty() && str.data()[str.size()] == '\0')((!str.empty() && str.data()[str.size()] == '\0') ? static_cast <void> (0) : __assert_fail ("!str.empty() && str.data()[str.size()] == '\\0'" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 2527, __PRETTY_FUNCTION__)); |
2528 | return str.data(); |
2529 | } |
2530 | |
2531 | bool isSugared() const { return false; } |
2532 | QualType desugar() const { return QualType(this, 0); } |
2533 | |
2534 | bool isInteger() const { |
2535 | return getKind() >= Bool && getKind() <= Int128; |
2536 | } |
2537 | |
2538 | bool isSignedInteger() const { |
2539 | return getKind() >= Char_S && getKind() <= Int128; |
2540 | } |
2541 | |
2542 | bool isUnsignedInteger() const { |
2543 | return getKind() >= Bool && getKind() <= UInt128; |
2544 | } |
2545 | |
2546 | bool isFloatingPoint() const { |
2547 | return getKind() >= Half && getKind() <= Float128; |
2548 | } |
2549 | |
2550 | /// Determines whether the given kind corresponds to a placeholder type. |
2551 | static bool isPlaceholderTypeKind(Kind K) { |
2552 | return K >= Overload; |
2553 | } |
2554 | |
2555 | /// Determines whether this type is a placeholder type, i.e. a type |
2556 | /// which cannot appear in arbitrary positions in a fully-formed |
2557 | /// expression. |
2558 | bool isPlaceholderType() const { |
2559 | return isPlaceholderTypeKind(getKind()); |
2560 | } |
2561 | |
2562 | /// Determines whether this type is a placeholder type other than |
2563 | /// Overload. Most placeholder types require only syntactic |
2564 | /// information about their context in order to be resolved (e.g. |
2565 | /// whether it is a call expression), which means they can (and |
2566 | /// should) be resolved in an earlier "phase" of analysis. |
2567 | /// Overload expressions sometimes pick up further information |
2568 | /// from their context, like whether the context expects a |
2569 | /// specific function-pointer type, and so frequently need |
2570 | /// special treatment. |
2571 | bool isNonOverloadPlaceholderType() const { |
2572 | return getKind() > Overload; |
2573 | } |
2574 | |
2575 | static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } |
2576 | }; |
2577 | |
2578 | /// Complex values, per C99 6.2.5p11. This supports the C99 complex |
2579 | /// types (_Complex float etc) as well as the GCC integer complex extensions. |
2580 | class ComplexType : public Type, public llvm::FoldingSetNode { |
2581 | friend class ASTContext; // ASTContext creates these. |
2582 | |
2583 | QualType ElementType; |
2584 | |
2585 | ComplexType(QualType Element, QualType CanonicalPtr) |
2586 | : Type(Complex, CanonicalPtr, Element->isDependentType(), |
2587 | Element->isInstantiationDependentType(), |
2588 | Element->isVariablyModifiedType(), |
2589 | Element->containsUnexpandedParameterPack()), |
2590 | ElementType(Element) {} |
2591 | |
2592 | public: |
2593 | QualType getElementType() const { return ElementType; } |
2594 | |
2595 | bool isSugared() const { return false; } |
2596 | QualType desugar() const { return QualType(this, 0); } |
2597 | |
2598 | void Profile(llvm::FoldingSetNodeID &ID) { |
2599 | Profile(ID, getElementType()); |
2600 | } |
2601 | |
2602 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) { |
2603 | ID.AddPointer(Element.getAsOpaquePtr()); |
2604 | } |
2605 | |
2606 | static bool classof(const Type *T) { return T->getTypeClass() == Complex; } |
2607 | }; |
2608 | |
2609 | /// Sugar for parentheses used when specifying types. |
2610 | class ParenType : public Type, public llvm::FoldingSetNode { |
2611 | friend class ASTContext; // ASTContext creates these. |
2612 | |
2613 | QualType Inner; |
2614 | |
2615 | ParenType(QualType InnerType, QualType CanonType) |
2616 | : Type(Paren, CanonType, InnerType->isDependentType(), |
2617 | InnerType->isInstantiationDependentType(), |
2618 | InnerType->isVariablyModifiedType(), |
2619 | InnerType->containsUnexpandedParameterPack()), |
2620 | Inner(InnerType) {} |
2621 | |
2622 | public: |
2623 | QualType getInnerType() const { return Inner; } |
2624 | |
2625 | bool isSugared() const { return true; } |
2626 | QualType desugar() const { return getInnerType(); } |
2627 | |
2628 | void Profile(llvm::FoldingSetNodeID &ID) { |
2629 | Profile(ID, getInnerType()); |
2630 | } |
2631 | |
2632 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Inner) { |
2633 | Inner.Profile(ID); |
2634 | } |
2635 | |
2636 | static bool classof(const Type *T) { return T->getTypeClass() == Paren; } |
2637 | }; |
2638 | |
2639 | /// PointerType - C99 6.7.5.1 - Pointer Declarators. |
2640 | class PointerType : public Type, public llvm::FoldingSetNode { |
2641 | friend class ASTContext; // ASTContext creates these. |
2642 | |
2643 | QualType PointeeType; |
2644 | |
2645 | PointerType(QualType Pointee, QualType CanonicalPtr) |
2646 | : Type(Pointer, CanonicalPtr, Pointee->isDependentType(), |
2647 | Pointee->isInstantiationDependentType(), |
2648 | Pointee->isVariablyModifiedType(), |
2649 | Pointee->containsUnexpandedParameterPack()), |
2650 | PointeeType(Pointee) {} |
2651 | |
2652 | public: |
2653 | QualType getPointeeType() const { return PointeeType; } |
2654 | |
2655 | /// Returns true if address spaces of pointers overlap. |
2656 | /// OpenCL v2.0 defines conversion rules for pointers to different |
2657 | /// address spaces (OpenCLC v2.0 s6.5.5) and notion of overlapping |
2658 | /// address spaces. |
2659 | /// CL1.1 or CL1.2: |
2660 | /// address spaces overlap iff they are they same. |
2661 | /// CL2.0 adds: |
2662 | /// __generic overlaps with any address space except for __constant. |
2663 | bool isAddressSpaceOverlapping(const PointerType &other) const { |
2664 | Qualifiers thisQuals = PointeeType.getQualifiers(); |
2665 | Qualifiers otherQuals = other.getPointeeType().getQualifiers(); |
2666 | // Address spaces overlap if at least one of them is a superset of another |
2667 | return thisQuals.isAddressSpaceSupersetOf(otherQuals) || |
2668 | otherQuals.isAddressSpaceSupersetOf(thisQuals); |
2669 | } |
2670 | |
2671 | bool isSugared() const { return false; } |
2672 | QualType desugar() const { return QualType(this, 0); } |
2673 | |
2674 | void Profile(llvm::FoldingSetNodeID &ID) { |
2675 | Profile(ID, getPointeeType()); |
2676 | } |
2677 | |
2678 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { |
2679 | ID.AddPointer(Pointee.getAsOpaquePtr()); |
2680 | } |
2681 | |
2682 | static bool classof(const Type *T) { return T->getTypeClass() == Pointer; } |
2683 | }; |
2684 | |
2685 | /// Represents a type which was implicitly adjusted by the semantic |
2686 | /// engine for arbitrary reasons. For example, array and function types can |
2687 | /// decay, and function types can have their calling conventions adjusted. |
2688 | class AdjustedType : public Type, public llvm::FoldingSetNode { |
2689 | QualType OriginalTy; |
2690 | QualType AdjustedTy; |
2691 | |
2692 | protected: |
2693 | friend class ASTContext; // ASTContext creates these. |
2694 | |
2695 | AdjustedType(TypeClass TC, QualType OriginalTy, QualType AdjustedTy, |
2696 | QualType CanonicalPtr) |
2697 | : Type(TC, CanonicalPtr, OriginalTy->isDependentType(), |
2698 | OriginalTy->isInstantiationDependentType(), |
2699 | OriginalTy->isVariablyModifiedType(), |
2700 | OriginalTy->containsUnexpandedParameterPack()), |
2701 | OriginalTy(OriginalTy), AdjustedTy(AdjustedTy) {} |
2702 | |
2703 | public: |
2704 | QualType getOriginalType() const { return OriginalTy; } |
2705 | QualType getAdjustedType() const { return AdjustedTy; } |
2706 | |
2707 | bool isSugared() const { return true; } |
2708 | QualType desugar() const { return AdjustedTy; } |
2709 | |
2710 | void Profile(llvm::FoldingSetNodeID &ID) { |
2711 | Profile(ID, OriginalTy, AdjustedTy); |
2712 | } |
2713 | |
2714 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Orig, QualType New) { |
2715 | ID.AddPointer(Orig.getAsOpaquePtr()); |
2716 | ID.AddPointer(New.getAsOpaquePtr()); |
2717 | } |
2718 | |
2719 | static bool classof(const Type *T) { |
2720 | return T->getTypeClass() == Adjusted || T->getTypeClass() == Decayed; |
2721 | } |
2722 | }; |
2723 | |
2724 | /// Represents a pointer type decayed from an array or function type. |
2725 | class DecayedType : public AdjustedType { |
2726 | friend class ASTContext; // ASTContext creates these. |
2727 | |
2728 | inline |
2729 | DecayedType(QualType OriginalType, QualType Decayed, QualType Canonical); |
2730 | |
2731 | public: |
2732 | QualType getDecayedType() const { return getAdjustedType(); } |
2733 | |
2734 | inline QualType getPointeeType() const; |
2735 | |
2736 | static bool classof(const Type *T) { return T->getTypeClass() == Decayed; } |
2737 | }; |
2738 | |
2739 | /// Pointer to a block type. |
2740 | /// This type is to represent types syntactically represented as |
2741 | /// "void (^)(int)", etc. Pointee is required to always be a function type. |
2742 | class BlockPointerType : public Type, public llvm::FoldingSetNode { |
2743 | friend class ASTContext; // ASTContext creates these. |
2744 | |
2745 | // Block is some kind of pointer type |
2746 | QualType PointeeType; |
2747 | |
2748 | BlockPointerType(QualType Pointee, QualType CanonicalCls) |
2749 | : Type(BlockPointer, CanonicalCls, Pointee->isDependentType(), |
2750 | Pointee->isInstantiationDependentType(), |
2751 | Pointee->isVariablyModifiedType(), |
2752 | Pointee->containsUnexpandedParameterPack()), |
2753 | PointeeType(Pointee) {} |
2754 | |
2755 | public: |
2756 | // Get the pointee type. Pointee is required to always be a function type. |
2757 | QualType getPointeeType() const { return PointeeType; } |
2758 | |
2759 | bool isSugared() const { return false; } |
2760 | QualType desugar() const { return QualType(this, 0); } |
2761 | |
2762 | void Profile(llvm::FoldingSetNodeID &ID) { |
2763 | Profile(ID, getPointeeType()); |
2764 | } |
2765 | |
2766 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { |
2767 | ID.AddPointer(Pointee.getAsOpaquePtr()); |
2768 | } |
2769 | |
2770 | static bool classof(const Type *T) { |
2771 | return T->getTypeClass() == BlockPointer; |
2772 | } |
2773 | }; |
2774 | |
2775 | /// Base for LValueReferenceType and RValueReferenceType |
2776 | class ReferenceType : public Type, public llvm::FoldingSetNode { |
2777 | QualType PointeeType; |
2778 | |
2779 | protected: |
2780 | ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef, |
2781 | bool SpelledAsLValue) |
2782 | : Type(tc, CanonicalRef, Referencee->isDependentType(), |
2783 | Referencee->isInstantiationDependentType(), |
2784 | Referencee->isVariablyModifiedType(), |
2785 | Referencee->containsUnexpandedParameterPack()), |
2786 | PointeeType(Referencee) { |
2787 | ReferenceTypeBits.SpelledAsLValue = SpelledAsLValue; |
2788 | ReferenceTypeBits.InnerRef = Referencee->isReferenceType(); |
2789 | } |
2790 | |
2791 | public: |
2792 | bool isSpelledAsLValue() const { return ReferenceTypeBits.SpelledAsLValue; } |
2793 | bool isInnerRef() const { return ReferenceTypeBits.InnerRef; } |
2794 | |
2795 | QualType getPointeeTypeAsWritten() const { return PointeeType; } |
2796 | |
2797 | QualType getPointeeType() const { |
2798 | // FIXME: this might strip inner qualifiers; okay? |
2799 | const ReferenceType *T = this; |
2800 | while (T->isInnerRef()) |
2801 | T = T->PointeeType->castAs<ReferenceType>(); |
2802 | return T->PointeeType; |
2803 | } |
2804 | |
2805 | void Profile(llvm::FoldingSetNodeID &ID) { |
2806 | Profile(ID, PointeeType, isSpelledAsLValue()); |
2807 | } |
2808 | |
2809 | static void Profile(llvm::FoldingSetNodeID &ID, |
2810 | QualType Referencee, |
2811 | bool SpelledAsLValue) { |
2812 | ID.AddPointer(Referencee.getAsOpaquePtr()); |
2813 | ID.AddBoolean(SpelledAsLValue); |
2814 | } |
2815 | |
2816 | static bool classof(const Type *T) { |
2817 | return T->getTypeClass() == LValueReference || |
2818 | T->getTypeClass() == RValueReference; |
2819 | } |
2820 | }; |
2821 | |
2822 | /// An lvalue reference type, per C++11 [dcl.ref]. |
2823 | class LValueReferenceType : public ReferenceType { |
2824 | friend class ASTContext; // ASTContext creates these |
2825 | |
2826 | LValueReferenceType(QualType Referencee, QualType CanonicalRef, |
2827 | bool SpelledAsLValue) |
2828 | : ReferenceType(LValueReference, Referencee, CanonicalRef, |
2829 | SpelledAsLValue) {} |
2830 | |
2831 | public: |
2832 | bool isSugared() const { return false; } |
2833 | QualType desugar() const { return QualType(this, 0); } |
2834 | |
2835 | static bool classof(const Type *T) { |
2836 | return T->getTypeClass() == LValueReference; |
2837 | } |
2838 | }; |
2839 | |
2840 | /// An rvalue reference type, per C++11 [dcl.ref]. |
2841 | class RValueReferenceType : public ReferenceType { |
2842 | friend class ASTContext; // ASTContext creates these |
2843 | |
2844 | RValueReferenceType(QualType Referencee, QualType CanonicalRef) |
2845 | : ReferenceType(RValueReference, Referencee, CanonicalRef, false) {} |
2846 | |
2847 | public: |
2848 | bool isSugared() const { return false; } |
2849 | QualType desugar() const { return QualType(this, 0); } |
2850 | |
2851 | static bool classof(const Type *T) { |
2852 | return T->getTypeClass() == RValueReference; |
2853 | } |
2854 | }; |
2855 | |
2856 | /// A pointer to member type per C++ 8.3.3 - Pointers to members. |
2857 | /// |
2858 | /// This includes both pointers to data members and pointer to member functions. |
2859 | class MemberPointerType : public Type, public llvm::FoldingSetNode { |
2860 | friend class ASTContext; // ASTContext creates these. |
2861 | |
2862 | QualType PointeeType; |
2863 | |
2864 | /// The class of which the pointee is a member. Must ultimately be a |
2865 | /// RecordType, but could be a typedef or a template parameter too. |
2866 | const Type *Class; |
2867 | |
2868 | MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr) |
2869 | : Type(MemberPointer, CanonicalPtr, |
2870 | Cls->isDependentType() || Pointee->isDependentType(), |
2871 | (Cls->isInstantiationDependentType() || |
2872 | Pointee->isInstantiationDependentType()), |
2873 | Pointee->isVariablyModifiedType(), |
2874 | (Cls->containsUnexpandedParameterPack() || |
2875 | Pointee->containsUnexpandedParameterPack())), |
2876 | PointeeType(Pointee), Class(Cls) {} |
2877 | |
2878 | public: |
2879 | QualType getPointeeType() const { return PointeeType; } |
2880 | |
2881 | /// Returns true if the member type (i.e. the pointee type) is a |
2882 | /// function type rather than a data-member type. |
2883 | bool isMemberFunctionPointer() const { |
2884 | return PointeeType->isFunctionProtoType(); |
2885 | } |
2886 | |
2887 | /// Returns true if the member type (i.e. the pointee type) is a |
2888 | /// data type rather than a function type. |
2889 | bool isMemberDataPointer() const { |
2890 | return !PointeeType->isFunctionProtoType(); |
2891 | } |
2892 | |
2893 | const Type *getClass() const { return Class; } |
2894 | CXXRecordDecl *getMostRecentCXXRecordDecl() const; |
2895 | |
2896 | bool isSugared() const { return false; } |
2897 | QualType desugar() const { return QualType(this, 0); } |
2898 | |
2899 | void Profile(llvm::FoldingSetNodeID &ID) { |
2900 | Profile(ID, getPointeeType(), getClass()); |
2901 | } |
2902 | |
2903 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee, |
2904 | const Type *Class) { |
2905 | ID.AddPointer(Pointee.getAsOpaquePtr()); |
2906 | ID.AddPointer(Class); |
2907 | } |
2908 | |
2909 | static bool classof(const Type *T) { |
2910 | return T->getTypeClass() == MemberPointer; |
2911 | } |
2912 | }; |
2913 | |
2914 | /// Represents an array type, per C99 6.7.5.2 - Array Declarators. |
2915 | class ArrayType : public Type, public llvm::FoldingSetNode { |
2916 | public: |
2917 | /// Capture whether this is a normal array (e.g. int X[4]) |
2918 | /// an array with a static size (e.g. int X[static 4]), or an array |
2919 | /// with a star size (e.g. int X[*]). |
2920 | /// 'static' is only allowed on function parameters. |
2921 | enum ArraySizeModifier { |
2922 | Normal, Static, Star |
2923 | }; |
2924 | |
2925 | private: |
2926 | /// The element type of the array. |
2927 | QualType ElementType; |
2928 | |
2929 | protected: |
2930 | friend class ASTContext; // ASTContext creates these. |
2931 | |
2932 | ArrayType(TypeClass tc, QualType et, QualType can, ArraySizeModifier sm, |
2933 | unsigned tq, const Expr *sz = nullptr); |
2934 | |
2935 | public: |
2936 | QualType getElementType() const { return ElementType; } |
2937 | |
2938 | ArraySizeModifier getSizeModifier() const { |
2939 | return ArraySizeModifier(ArrayTypeBits.SizeModifier); |
2940 | } |
2941 | |
2942 | Qualifiers getIndexTypeQualifiers() const { |
2943 | return Qualifiers::fromCVRMask(getIndexTypeCVRQualifiers()); |
2944 | } |
2945 | |
2946 | unsigned getIndexTypeCVRQualifiers() const { |
2947 | return ArrayTypeBits.IndexTypeQuals; |
2948 | } |
2949 | |
2950 | static bool classof(const Type *T) { |
2951 | return T->getTypeClass() == ConstantArray || |
2952 | T->getTypeClass() == VariableArray || |
2953 | T->getTypeClass() == IncompleteArray || |
2954 | T->getTypeClass() == DependentSizedArray; |
2955 | } |
2956 | }; |
2957 | |
2958 | /// Represents the canonical version of C arrays with a specified constant size. |
2959 | /// For example, the canonical type for 'int A[4 + 4*100]' is a |
2960 | /// ConstantArrayType where the element type is 'int' and the size is 404. |
2961 | class ConstantArrayType final |
2962 | : public ArrayType, |
2963 | private llvm::TrailingObjects<ConstantArrayType, const Expr *> { |
2964 | friend class ASTContext; // ASTContext creates these. |
2965 | friend TrailingObjects; |
2966 | |
2967 | llvm::APInt Size; // Allows us to unique the type. |
2968 | |
2969 | ConstantArrayType(QualType et, QualType can, const llvm::APInt &size, |
2970 | const Expr *sz, ArraySizeModifier sm, unsigned tq) |
2971 | : ArrayType(ConstantArray, et, can, sm, tq, sz), Size(size) { |
2972 | ConstantArrayTypeBits.HasStoredSizeExpr = sz != nullptr; |
2973 | if (ConstantArrayTypeBits.HasStoredSizeExpr) { |
2974 | assert(!can.isNull() && "canonical constant array should not have size")((!can.isNull() && "canonical constant array should not have size" ) ? static_cast<void> (0) : __assert_fail ("!can.isNull() && \"canonical constant array should not have size\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 2974, __PRETTY_FUNCTION__)); |
2975 | *getTrailingObjects<const Expr*>() = sz; |
2976 | } |
2977 | } |
2978 | |
2979 | unsigned numTrailingObjects(OverloadToken<const Expr*>) const { |
2980 | return ConstantArrayTypeBits.HasStoredSizeExpr; |
2981 | } |
2982 | |
2983 | public: |
2984 | const llvm::APInt &getSize() const { return Size; } |
2985 | const Expr *getSizeExpr() const { |
2986 | return ConstantArrayTypeBits.HasStoredSizeExpr |
2987 | ? *getTrailingObjects<const Expr *>() |
2988 | : nullptr; |
2989 | } |
2990 | bool isSugared() const { return false; } |
2991 | QualType desugar() const { return QualType(this, 0); } |
2992 | |
2993 | /// Determine the number of bits required to address a member of |
2994 | // an array with the given element type and number of elements. |
2995 | static unsigned getNumAddressingBits(const ASTContext &Context, |
2996 | QualType ElementType, |
2997 | const llvm::APInt &NumElements); |
2998 | |
2999 | /// Determine the maximum number of active bits that an array's size |
3000 | /// can require, which limits the maximum size of the array. |
3001 | static unsigned getMaxSizeBits(const ASTContext &Context); |
3002 | |
3003 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) { |
3004 | Profile(ID, Ctx, getElementType(), getSize(), getSizeExpr(), |
3005 | getSizeModifier(), getIndexTypeCVRQualifiers()); |
3006 | } |
3007 | |
3008 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx, |
3009 | QualType ET, const llvm::APInt &ArraySize, |
3010 | const Expr *SizeExpr, ArraySizeModifier SizeMod, |
3011 | unsigned TypeQuals); |
3012 | |
3013 | static bool classof(const Type *T) { |
3014 | return T->getTypeClass() == ConstantArray; |
3015 | } |
3016 | }; |
3017 | |
3018 | /// Represents a C array with an unspecified size. For example 'int A[]' has |
3019 | /// an IncompleteArrayType where the element type is 'int' and the size is |
3020 | /// unspecified. |
3021 | class IncompleteArrayType : public ArrayType { |
3022 | friend class ASTContext; // ASTContext creates these. |
3023 | |
3024 | IncompleteArrayType(QualType et, QualType can, |
3025 | ArraySizeModifier sm, unsigned tq) |
3026 | : ArrayType(IncompleteArray, et, can, sm, tq) {} |
3027 | |
3028 | public: |
3029 | friend class StmtIteratorBase; |
3030 | |
3031 | bool isSugared() const { return false; } |
3032 | QualType desugar() const { return QualType(this, 0); } |
3033 | |
3034 | static bool classof(const Type *T) { |
3035 | return T->getTypeClass() == IncompleteArray; |
3036 | } |
3037 | |
3038 | void Profile(llvm::FoldingSetNodeID &ID) { |
3039 | Profile(ID, getElementType(), getSizeModifier(), |
3040 | getIndexTypeCVRQualifiers()); |
3041 | } |
3042 | |
3043 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, |
3044 | ArraySizeModifier SizeMod, unsigned TypeQuals) { |
3045 | ID.AddPointer(ET.getAsOpaquePtr()); |
3046 | ID.AddInteger(SizeMod); |
3047 | ID.AddInteger(TypeQuals); |
3048 | } |
3049 | }; |
3050 | |
3051 | /// Represents a C array with a specified size that is not an |
3052 | /// integer-constant-expression. For example, 'int s[x+foo()]'. |
3053 | /// Since the size expression is an arbitrary expression, we store it as such. |
3054 | /// |
3055 | /// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and |
3056 | /// should not be: two lexically equivalent variable array types could mean |
3057 | /// different things, for example, these variables do not have the same type |
3058 | /// dynamically: |
3059 | /// |
3060 | /// void foo(int x) { |
3061 | /// int Y[x]; |
3062 | /// ++x; |
3063 | /// int Z[x]; |
3064 | /// } |
3065 | class VariableArrayType : public ArrayType { |
3066 | friend class ASTContext; // ASTContext creates these. |
3067 | |
3068 | /// An assignment-expression. VLA's are only permitted within |
3069 | /// a function block. |
3070 | Stmt *SizeExpr; |
3071 | |
3072 | /// The range spanned by the left and right array brackets. |
3073 | SourceRange Brackets; |
3074 | |
3075 | VariableArrayType(QualType et, QualType can, Expr *e, |
3076 | ArraySizeModifier sm, unsigned tq, |
3077 | SourceRange brackets) |
3078 | : ArrayType(VariableArray, et, can, sm, tq, e), |
3079 | SizeExpr((Stmt*) e), Brackets(brackets) {} |
3080 | |
3081 | public: |
3082 | friend class StmtIteratorBase; |
3083 | |
3084 | Expr *getSizeExpr() const { |
3085 | // We use C-style casts instead of cast<> here because we do not wish |
3086 | // to have a dependency of Type.h on Stmt.h/Expr.h. |
3087 | return (Expr*) SizeExpr; |
3088 | } |
3089 | |
3090 | SourceRange getBracketsRange() const { return Brackets; } |
3091 | SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } |
3092 | SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } |
3093 | |
3094 | bool isSugared() const { return false; } |
3095 | QualType desugar() const { return QualType(this, 0); } |
3096 | |
3097 | static bool classof(const Type *T) { |
3098 | return T->getTypeClass() == VariableArray; |
3099 | } |
3100 | |
3101 | void Profile(llvm::FoldingSetNodeID &ID) { |
3102 | llvm_unreachable("Cannot unique VariableArrayTypes.")::llvm::llvm_unreachable_internal("Cannot unique VariableArrayTypes." , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 3102); |
3103 | } |
3104 | }; |
3105 | |
3106 | /// Represents an array type in C++ whose size is a value-dependent expression. |
3107 | /// |
3108 | /// For example: |
3109 | /// \code |
3110 | /// template<typename T, int Size> |
3111 | /// class array { |
3112 | /// T data[Size]; |
3113 | /// }; |
3114 | /// \endcode |
3115 | /// |
3116 | /// For these types, we won't actually know what the array bound is |
3117 | /// until template instantiation occurs, at which point this will |
3118 | /// become either a ConstantArrayType or a VariableArrayType. |
3119 | class DependentSizedArrayType : public ArrayType { |
3120 | friend class ASTContext; // ASTContext creates these. |
3121 | |
3122 | const ASTContext &Context; |
3123 | |
3124 | /// An assignment expression that will instantiate to the |
3125 | /// size of the array. |
3126 | /// |
3127 | /// The expression itself might be null, in which case the array |
3128 | /// type will have its size deduced from an initializer. |
3129 | Stmt *SizeExpr; |
3130 | |
3131 | /// The range spanned by the left and right array brackets. |
3132 | SourceRange Brackets; |
3133 | |
3134 | DependentSizedArrayType(const ASTContext &Context, QualType et, QualType can, |
3135 | Expr *e, ArraySizeModifier sm, unsigned tq, |
3136 | SourceRange brackets); |
3137 | |
3138 | public: |
3139 | friend class StmtIteratorBase; |
3140 | |
3141 | Expr *getSizeExpr() const { |
3142 | // We use C-style casts instead of cast<> here because we do not wish |
3143 | // to have a dependency of Type.h on Stmt.h/Expr.h. |
3144 | return (Expr*) SizeExpr; |
3145 | } |
3146 | |
3147 | SourceRange getBracketsRange() const { return Brackets; } |
3148 | SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } |
3149 | SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } |
3150 | |
3151 | bool isSugared() const { return false; } |
3152 | QualType desugar() const { return QualType(this, 0); } |
3153 | |
3154 | static bool classof(const Type *T) { |
3155 | return T->getTypeClass() == DependentSizedArray; |
3156 | } |
3157 | |
3158 | void Profile(llvm::FoldingSetNodeID &ID) { |
3159 | Profile(ID, Context, getElementType(), |
3160 | getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr()); |
3161 | } |
3162 | |
3163 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3164 | QualType ET, ArraySizeModifier SizeMod, |
3165 | unsigned TypeQuals, Expr *E); |
3166 | }; |
3167 | |
3168 | /// Represents an extended address space qualifier where the input address space |
3169 | /// value is dependent. Non-dependent address spaces are not represented with a |
3170 | /// special Type subclass; they are stored on an ExtQuals node as part of a QualType. |
3171 | /// |
3172 | /// For example: |
3173 | /// \code |
3174 | /// template<typename T, int AddrSpace> |
3175 | /// class AddressSpace { |
3176 | /// typedef T __attribute__((address_space(AddrSpace))) type; |
3177 | /// } |
3178 | /// \endcode |
3179 | class DependentAddressSpaceType : public Type, public llvm::FoldingSetNode { |
3180 | friend class ASTContext; |
3181 | |
3182 | const ASTContext &Context; |
3183 | Expr *AddrSpaceExpr; |
3184 | QualType PointeeType; |
3185 | SourceLocation loc; |
3186 | |
3187 | DependentAddressSpaceType(const ASTContext &Context, QualType PointeeType, |
3188 | QualType can, Expr *AddrSpaceExpr, |
3189 | SourceLocation loc); |
3190 | |
3191 | public: |
3192 | Expr *getAddrSpaceExpr() const { return AddrSpaceExpr; } |
3193 | QualType getPointeeType() const { return PointeeType; } |
3194 | SourceLocation getAttributeLoc() const { return loc; } |
3195 | |
3196 | bool isSugared() const { return false; } |
3197 | QualType desugar() const { return QualType(this, 0); } |
3198 | |
3199 | static bool classof(const Type *T) { |
3200 | return T->getTypeClass() == DependentAddressSpace; |
3201 | } |
3202 | |
3203 | void Profile(llvm::FoldingSetNodeID &ID) { |
3204 | Profile(ID, Context, getPointeeType(), getAddrSpaceExpr()); |
3205 | } |
3206 | |
3207 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3208 | QualType PointeeType, Expr *AddrSpaceExpr); |
3209 | }; |
3210 | |
3211 | /// Represents an extended vector type where either the type or size is |
3212 | /// dependent. |
3213 | /// |
3214 | /// For example: |
3215 | /// \code |
3216 | /// template<typename T, int Size> |
3217 | /// class vector { |
3218 | /// typedef T __attribute__((ext_vector_type(Size))) type; |
3219 | /// } |
3220 | /// \endcode |
3221 | class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode { |
3222 | friend class ASTContext; |
3223 | |
3224 | const ASTContext &Context; |
3225 | Expr *SizeExpr; |
3226 | |
3227 | /// The element type of the array. |
3228 | QualType ElementType; |
3229 | |
3230 | SourceLocation loc; |
3231 | |
3232 | DependentSizedExtVectorType(const ASTContext &Context, QualType ElementType, |
3233 | QualType can, Expr *SizeExpr, SourceLocation loc); |
3234 | |
3235 | public: |
3236 | Expr *getSizeExpr() const { return SizeExpr; } |
3237 | QualType getElementType() const { return ElementType; } |
3238 | SourceLocation getAttributeLoc() const { return loc; } |
3239 | |
3240 | bool isSugared() const { return false; } |
3241 | QualType desugar() const { return QualType(this, 0); } |
3242 | |
3243 | static bool classof(const Type *T) { |
3244 | return T->getTypeClass() == DependentSizedExtVector; |
3245 | } |
3246 | |
3247 | void Profile(llvm::FoldingSetNodeID &ID) { |
3248 | Profile(ID, Context, getElementType(), getSizeExpr()); |
3249 | } |
3250 | |
3251 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3252 | QualType ElementType, Expr *SizeExpr); |
3253 | }; |
3254 | |
3255 | |
3256 | /// Represents a GCC generic vector type. This type is created using |
3257 | /// __attribute__((vector_size(n)), where "n" specifies the vector size in |
3258 | /// bytes; or from an Altivec __vector or vector declaration. |
3259 | /// Since the constructor takes the number of vector elements, the |
3260 | /// client is responsible for converting the size into the number of elements. |
3261 | class VectorType : public Type, public llvm::FoldingSetNode { |
3262 | public: |
3263 | enum VectorKind { |
3264 | /// not a target-specific vector type |
3265 | GenericVector, |
3266 | |
3267 | /// is AltiVec vector |
3268 | AltiVecVector, |
3269 | |
3270 | /// is AltiVec 'vector Pixel' |
3271 | AltiVecPixel, |
3272 | |
3273 | /// is AltiVec 'vector bool ...' |
3274 | AltiVecBool, |
3275 | |
3276 | /// is ARM Neon vector |
3277 | NeonVector, |
3278 | |
3279 | /// is ARM Neon polynomial vector |
3280 | NeonPolyVector |
3281 | }; |
3282 | |
3283 | protected: |
3284 | friend class ASTContext; // ASTContext creates these. |
3285 | |
3286 | /// The element type of the vector. |
3287 | QualType ElementType; |
3288 | |
3289 | VectorType(QualType vecType, unsigned nElements, QualType canonType, |
3290 | VectorKind vecKind); |
3291 | |
3292 | VectorType(TypeClass tc, QualType vecType, unsigned nElements, |
3293 | QualType canonType, VectorKind vecKind); |
3294 | |
3295 | public: |
3296 | QualType getElementType() const { return ElementType; } |
3297 | unsigned getNumElements() const { return VectorTypeBits.NumElements; } |
3298 | |
3299 | static bool isVectorSizeTooLarge(unsigned NumElements) { |
3300 | return NumElements > VectorTypeBitfields::MaxNumElements; |
3301 | } |
3302 | |
3303 | bool isSugared() const { return false; } |
3304 | QualType desugar() const { return QualType(this, 0); } |
3305 | |
3306 | VectorKind getVectorKind() const { |
3307 | return VectorKind(VectorTypeBits.VecKind); |
3308 | } |
3309 | |
3310 | void Profile(llvm::FoldingSetNodeID &ID) { |
3311 | Profile(ID, getElementType(), getNumElements(), |
3312 | getTypeClass(), getVectorKind()); |
3313 | } |
3314 | |
3315 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, |
3316 | unsigned NumElements, TypeClass TypeClass, |
3317 | VectorKind VecKind) { |
3318 | ID.AddPointer(ElementType.getAsOpaquePtr()); |
3319 | ID.AddInteger(NumElements); |
3320 | ID.AddInteger(TypeClass); |
3321 | ID.AddInteger(VecKind); |
3322 | } |
3323 | |
3324 | static bool classof(const Type *T) { |
3325 | return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector; |
3326 | } |
3327 | }; |
3328 | |
3329 | /// Represents a vector type where either the type or size is dependent. |
3330 | //// |
3331 | /// For example: |
3332 | /// \code |
3333 | /// template<typename T, int Size> |
3334 | /// class vector { |
3335 | /// typedef T __attribute__((vector_size(Size))) type; |
3336 | /// } |
3337 | /// \endcode |
3338 | class DependentVectorType : public Type, public llvm::FoldingSetNode { |
3339 | friend class ASTContext; |
3340 | |
3341 | const ASTContext &Context; |
3342 | QualType ElementType; |
3343 | Expr *SizeExpr; |
3344 | SourceLocation Loc; |
3345 | |
3346 | DependentVectorType(const ASTContext &Context, QualType ElementType, |
3347 | QualType CanonType, Expr *SizeExpr, |
3348 | SourceLocation Loc, VectorType::VectorKind vecKind); |
3349 | |
3350 | public: |
3351 | Expr *getSizeExpr() const { return SizeExpr; } |
3352 | QualType getElementType() const { return ElementType; } |
3353 | SourceLocation getAttributeLoc() const { return Loc; } |
3354 | VectorType::VectorKind getVectorKind() const { |
3355 | return VectorType::VectorKind(VectorTypeBits.VecKind); |
3356 | } |
3357 | |
3358 | bool isSugared() const { return false; } |
3359 | QualType desugar() const { return QualType(this, 0); } |
3360 | |
3361 | static bool classof(const Type *T) { |
3362 | return T->getTypeClass() == DependentVector; |
3363 | } |
3364 | |
3365 | void Profile(llvm::FoldingSetNodeID &ID) { |
3366 | Profile(ID, Context, getElementType(), getSizeExpr(), getVectorKind()); |
3367 | } |
3368 | |
3369 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3370 | QualType ElementType, const Expr *SizeExpr, |
3371 | VectorType::VectorKind VecKind); |
3372 | }; |
3373 | |
3374 | /// ExtVectorType - Extended vector type. This type is created using |
3375 | /// __attribute__((ext_vector_type(n)), where "n" is the number of elements. |
3376 | /// Unlike vector_size, ext_vector_type is only allowed on typedef's. This |
3377 | /// class enables syntactic extensions, like Vector Components for accessing |
3378 | /// points (as .xyzw), colors (as .rgba), and textures (modeled after OpenGL |
3379 | /// Shading Language). |
3380 | class ExtVectorType : public VectorType { |
3381 | friend class ASTContext; // ASTContext creates these. |
3382 | |
3383 | ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) |
3384 | : VectorType(ExtVector, vecType, nElements, canonType, GenericVector) {} |
3385 | |
3386 | public: |
3387 | static int getPointAccessorIdx(char c) { |
3388 | switch (c) { |
3389 | default: return -1; |
3390 | case 'x': case 'r': return 0; |
3391 | case 'y': case 'g': return 1; |
3392 | case 'z': case 'b': return 2; |
3393 | case 'w': case 'a': return 3; |
3394 | } |
3395 | } |
3396 | |
3397 | static int getNumericAccessorIdx(char c) { |
3398 | switch (c) { |
3399 | default: return -1; |
3400 | case '0': return 0; |
3401 | case '1': return 1; |
3402 | case '2': return 2; |
3403 | case '3': return 3; |
3404 | case '4': return 4; |
3405 | case '5': return 5; |
3406 | case '6': return 6; |
3407 | case '7': return 7; |
3408 | case '8': return 8; |
3409 | case '9': return 9; |
3410 | case 'A': |
3411 | case 'a': return 10; |
3412 | case 'B': |
3413 | case 'b': return 11; |
3414 | case 'C': |
3415 | case 'c': return 12; |
3416 | case 'D': |
3417 | case 'd': return 13; |
3418 | case 'E': |
3419 | case 'e': return 14; |
3420 | case 'F': |
3421 | case 'f': return 15; |
3422 | } |
3423 | } |
3424 | |
3425 | static int getAccessorIdx(char c, bool isNumericAccessor) { |
3426 | if (isNumericAccessor) |
3427 | return getNumericAccessorIdx(c); |
3428 | else |
3429 | return getPointAccessorIdx(c); |
3430 | } |
3431 | |
3432 | bool isAccessorWithinNumElements(char c, bool isNumericAccessor) const { |
3433 | if (int idx = getAccessorIdx(c, isNumericAccessor)+1) |
3434 | return unsigned(idx-1) < getNumElements(); |
3435 | return false; |
3436 | } |
3437 | |
3438 | bool isSugared() const { return false; } |
3439 | QualType desugar() const { return QualType(this, 0); } |
3440 | |
3441 | static bool classof(const Type *T) { |
3442 | return T->getTypeClass() == ExtVector; |
3443 | } |
3444 | }; |
3445 | |
3446 | /// FunctionType - C99 6.7.5.3 - Function Declarators. This is the common base |
3447 | /// class of FunctionNoProtoType and FunctionProtoType. |
3448 | class FunctionType : public Type { |
3449 | // The type returned by the function. |
3450 | QualType ResultType; |
3451 | |
3452 | public: |
3453 | /// Interesting information about a specific parameter that can't simply |
3454 | /// be reflected in parameter's type. This is only used by FunctionProtoType |
3455 | /// but is in FunctionType to make this class available during the |
3456 | /// specification of the bases of FunctionProtoType. |
3457 | /// |
3458 | /// It makes sense to model language features this way when there's some |
3459 | /// sort of parameter-specific override (such as an attribute) that |
3460 | /// affects how the function is called. For example, the ARC ns_consumed |
3461 | /// attribute changes whether a parameter is passed at +0 (the default) |
3462 | /// or +1 (ns_consumed). This must be reflected in the function type, |
3463 | /// but isn't really a change to the parameter type. |
3464 | /// |
3465 | /// One serious disadvantage of modelling language features this way is |
3466 | /// that they generally do not work with language features that attempt |
3467 | /// to destructure types. For example, template argument deduction will |
3468 | /// not be able to match a parameter declared as |
3469 | /// T (*)(U) |
3470 | /// against an argument of type |
3471 | /// void (*)(__attribute__((ns_consumed)) id) |
3472 | /// because the substitution of T=void, U=id into the former will |
3473 | /// not produce the latter. |
3474 | class ExtParameterInfo { |
3475 | enum { |
3476 | ABIMask = 0x0F, |
3477 | IsConsumed = 0x10, |
3478 | HasPassObjSize = 0x20, |
3479 | IsNoEscape = 0x40, |
3480 | }; |
3481 | unsigned char Data = 0; |
3482 | |
3483 | public: |
3484 | ExtParameterInfo() = default; |
3485 | |
3486 | /// Return the ABI treatment of this parameter. |
3487 | ParameterABI getABI() const { return ParameterABI(Data & ABIMask); } |
3488 | ExtParameterInfo withABI(ParameterABI kind) const { |
3489 | ExtParameterInfo copy = *this; |
3490 | copy.Data = (copy.Data & ~ABIMask) | unsigned(kind); |
3491 | return copy; |
3492 | } |
3493 | |
3494 | /// Is this parameter considered "consumed" by Objective-C ARC? |
3495 | /// Consumed parameters must have retainable object type. |
3496 | bool isConsumed() const { return (Data & IsConsumed); } |
3497 | ExtParameterInfo withIsConsumed(bool consumed) const { |
3498 | ExtParameterInfo copy = *this; |
3499 | if (consumed) |
3500 | copy.Data |= IsConsumed; |
3501 | else |
3502 | copy.Data &= ~IsConsumed; |
3503 | return copy; |
3504 | } |
3505 | |
3506 | bool hasPassObjectSize() const { return Data & HasPassObjSize; } |
3507 | ExtParameterInfo withHasPassObjectSize() const { |
3508 | ExtParameterInfo Copy = *this; |
3509 | Copy.Data |= HasPassObjSize; |
3510 | return Copy; |
3511 | } |
3512 | |
3513 | bool isNoEscape() const { return Data & IsNoEscape; } |
3514 | ExtParameterInfo withIsNoEscape(bool NoEscape) const { |
3515 | ExtParameterInfo Copy = *this; |
3516 | if (NoEscape) |
3517 | Copy.Data |= IsNoEscape; |
3518 | else |
3519 | Copy.Data &= ~IsNoEscape; |
3520 | return Copy; |
3521 | } |
3522 | |
3523 | unsigned char getOpaqueValue() const { return Data; } |
3524 | static ExtParameterInfo getFromOpaqueValue(unsigned char data) { |
3525 | ExtParameterInfo result; |
3526 | result.Data = data; |
3527 | return result; |
3528 | } |
3529 | |
3530 | friend bool operator==(ExtParameterInfo lhs, ExtParameterInfo rhs) { |
3531 | return lhs.Data == rhs.Data; |
3532 | } |
3533 | |
3534 | friend bool operator!=(ExtParameterInfo lhs, ExtParameterInfo rhs) { |
3535 | return lhs.Data != rhs.Data; |
3536 | } |
3537 | }; |
3538 | |
3539 | /// A class which abstracts out some details necessary for |
3540 | /// making a call. |
3541 | /// |
3542 | /// It is not actually used directly for storing this information in |
3543 | /// a FunctionType, although FunctionType does currently use the |
3544 | /// same bit-pattern. |
3545 | /// |
3546 | // If you add a field (say Foo), other than the obvious places (both, |
3547 | // constructors, compile failures), what you need to update is |
3548 | // * Operator== |
3549 | // * getFoo |
3550 | // * withFoo |
3551 | // * functionType. Add Foo, getFoo. |
3552 | // * ASTContext::getFooType |
3553 | // * ASTContext::mergeFunctionTypes |
3554 | // * FunctionNoProtoType::Profile |
3555 | // * FunctionProtoType::Profile |
3556 | // * TypePrinter::PrintFunctionProto |
3557 | // * AST read and write |
3558 | // * Codegen |
3559 | class ExtInfo { |
3560 | friend class FunctionType; |
3561 | |
3562 | // Feel free to rearrange or add bits, but if you go over 12, |
3563 | // you'll need to adjust both the Bits field below and |
3564 | // Type::FunctionTypeBitfields. |
3565 | |
3566 | // | CC |noreturn|produces|nocallersavedregs|regparm|nocfcheck| |
3567 | // |0 .. 4| 5 | 6 | 7 |8 .. 10| 11 | |
3568 | // |
3569 | // regparm is either 0 (no regparm attribute) or the regparm value+1. |
3570 | enum { CallConvMask = 0x1F }; |
3571 | enum { NoReturnMask = 0x20 }; |
3572 | enum { ProducesResultMask = 0x40 }; |
3573 | enum { NoCallerSavedRegsMask = 0x80 }; |
3574 | enum { NoCfCheckMask = 0x800 }; |
3575 | enum { |
3576 | RegParmMask = ~(CallConvMask | NoReturnMask | ProducesResultMask | |
3577 | NoCallerSavedRegsMask | NoCfCheckMask), |
3578 | RegParmOffset = 8 |
3579 | }; // Assumed to be the last field |
3580 | uint16_t Bits = CC_C; |
3581 | |
3582 | ExtInfo(unsigned Bits) : Bits(static_cast<uint16_t>(Bits)) {} |
3583 | |
3584 | public: |
3585 | // Constructor with no defaults. Use this when you know that you |
3586 | // have all the elements (when reading an AST file for example). |
3587 | ExtInfo(bool noReturn, bool hasRegParm, unsigned regParm, CallingConv cc, |
3588 | bool producesResult, bool noCallerSavedRegs, bool NoCfCheck) { |
3589 | assert((!hasRegParm || regParm < 7) && "Invalid regparm value")(((!hasRegParm || regParm < 7) && "Invalid regparm value" ) ? static_cast<void> (0) : __assert_fail ("(!hasRegParm || regParm < 7) && \"Invalid regparm value\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 3589, __PRETTY_FUNCTION__)); |
3590 | Bits = ((unsigned)cc) | (noReturn ? NoReturnMask : 0) | |
3591 | (producesResult ? ProducesResultMask : 0) | |
3592 | (noCallerSavedRegs ? NoCallerSavedRegsMask : 0) | |
3593 | (hasRegParm ? ((regParm + 1) << RegParmOffset) : 0) | |
3594 | (NoCfCheck ? NoCfCheckMask : 0); |
3595 | } |
3596 | |
3597 | // Constructor with all defaults. Use when for example creating a |
3598 | // function known to use defaults. |
3599 | ExtInfo() = default; |
3600 | |
3601 | // Constructor with just the calling convention, which is an important part |
3602 | // of the canonical type. |
3603 | ExtInfo(CallingConv CC) : Bits(CC) {} |
3604 | |
3605 | bool getNoReturn() const { return Bits & NoReturnMask; } |
3606 | bool getProducesResult() const { return Bits & ProducesResultMask; } |
3607 | bool getNoCallerSavedRegs() const { return Bits & NoCallerSavedRegsMask; } |
3608 | bool getNoCfCheck() const { return Bits & NoCfCheckMask; } |
3609 | bool getHasRegParm() const { return (Bits >> RegParmOffset) != 0; } |
3610 | |
3611 | unsigned getRegParm() const { |
3612 | unsigned RegParm = (Bits & RegParmMask) >> RegParmOffset; |
3613 | if (RegParm > 0) |
3614 | --RegParm; |
3615 | return RegParm; |
3616 | } |
3617 | |
3618 | CallingConv getCC() const { return CallingConv(Bits & CallConvMask); } |
3619 | |
3620 | bool operator==(ExtInfo Other) const { |
3621 | return Bits == Other.Bits; |
3622 | } |
3623 | bool operator!=(ExtInfo Other) const { |
3624 | return Bits != Other.Bits; |
3625 | } |
3626 | |
3627 | // Note that we don't have setters. That is by design, use |
3628 | // the following with methods instead of mutating these objects. |
3629 | |
3630 | ExtInfo withNoReturn(bool noReturn) const { |
3631 | if (noReturn) |
3632 | return ExtInfo(Bits | NoReturnMask); |
3633 | else |
3634 | return ExtInfo(Bits & ~NoReturnMask); |
3635 | } |
3636 | |
3637 | ExtInfo withProducesResult(bool producesResult) const { |
3638 | if (producesResult) |
3639 | return ExtInfo(Bits | ProducesResultMask); |
3640 | else |
3641 | return ExtInfo(Bits & ~ProducesResultMask); |
3642 | } |
3643 | |
3644 | ExtInfo withNoCallerSavedRegs(bool noCallerSavedRegs) const { |
3645 | if (noCallerSavedRegs) |
3646 | return ExtInfo(Bits | NoCallerSavedRegsMask); |
3647 | else |
3648 | return ExtInfo(Bits & ~NoCallerSavedRegsMask); |
3649 | } |
3650 | |
3651 | ExtInfo withNoCfCheck(bool noCfCheck) const { |
3652 | if (noCfCheck) |
3653 | return ExtInfo(Bits | NoCfCheckMask); |
3654 | else |
3655 | return ExtInfo(Bits & ~NoCfCheckMask); |
3656 | } |
3657 | |
3658 | ExtInfo withRegParm(unsigned RegParm) const { |
3659 | assert(RegParm < 7 && "Invalid regparm value")((RegParm < 7 && "Invalid regparm value") ? static_cast <void> (0) : __assert_fail ("RegParm < 7 && \"Invalid regparm value\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 3659, __PRETTY_FUNCTION__)); |
3660 | return ExtInfo((Bits & ~RegParmMask) | |
3661 | ((RegParm + 1) << RegParmOffset)); |
3662 | } |
3663 | |
3664 | ExtInfo withCallingConv(CallingConv cc) const { |
3665 | return ExtInfo((Bits & ~CallConvMask) | (unsigned) cc); |
3666 | } |
3667 | |
3668 | void Profile(llvm::FoldingSetNodeID &ID) const { |
3669 | ID.AddInteger(Bits); |
3670 | } |
3671 | }; |
3672 | |
3673 | /// A simple holder for a QualType representing a type in an |
3674 | /// exception specification. Unfortunately needed by FunctionProtoType |
3675 | /// because TrailingObjects cannot handle repeated types. |
3676 | struct ExceptionType { QualType Type; }; |
3677 | |
3678 | /// A simple holder for various uncommon bits which do not fit in |
3679 | /// FunctionTypeBitfields. Aligned to alignof(void *) to maintain the |
3680 | /// alignment of subsequent objects in TrailingObjects. You must update |
3681 | /// hasExtraBitfields in FunctionProtoType after adding extra data here. |
3682 | struct alignas(void *) FunctionTypeExtraBitfields { |
3683 | /// The number of types in the exception specification. |
3684 | /// A whole unsigned is not needed here and according to |
3685 | /// [implimits] 8 bits would be enough here. |
3686 | unsigned NumExceptionType; |
3687 | }; |
3688 | |
3689 | protected: |
3690 | FunctionType(TypeClass tc, QualType res, |
3691 | QualType Canonical, bool Dependent, |
3692 | bool InstantiationDependent, |
3693 | bool VariablyModified, bool ContainsUnexpandedParameterPack, |
3694 | ExtInfo Info) |
3695 | : Type(tc, Canonical, Dependent, InstantiationDependent, VariablyModified, |
3696 | ContainsUnexpandedParameterPack), |
3697 | ResultType(res) { |
3698 | FunctionTypeBits.ExtInfo = Info.Bits; |
3699 | } |
3700 | |
3701 | Qualifiers getFastTypeQuals() const { |
3702 | return Qualifiers::fromFastMask(FunctionTypeBits.FastTypeQuals); |
3703 | } |
3704 | |
3705 | public: |
3706 | QualType getReturnType() const { return ResultType; } |
3707 | |
3708 | bool getHasRegParm() const { return getExtInfo().getHasRegParm(); } |
3709 | unsigned getRegParmType() const { return getExtInfo().getRegParm(); } |
3710 | |
3711 | /// Determine whether this function type includes the GNU noreturn |
3712 | /// attribute. The C++11 [[noreturn]] attribute does not affect the function |
3713 | /// type. |
3714 | bool getNoReturnAttr() const { return getExtInfo().getNoReturn(); } |
3715 | |
3716 | CallingConv getCallConv() const { return getExtInfo().getCC(); } |
3717 | ExtInfo getExtInfo() const { return ExtInfo(FunctionTypeBits.ExtInfo); } |
3718 | |
3719 | static_assert((~Qualifiers::FastMask & Qualifiers::CVRMask) == 0, |
3720 | "Const, volatile and restrict are assumed to be a subset of " |
3721 | "the fast qualifiers."); |
3722 | |
3723 | bool isConst() const { return getFastTypeQuals().hasConst(); } |
3724 | bool isVolatile() const { return getFastTypeQuals().hasVolatile(); } |
3725 | bool isRestrict() const { return getFastTypeQuals().hasRestrict(); } |
3726 | |
3727 | /// Determine the type of an expression that calls a function of |
3728 | /// this type. |
3729 | QualType getCallResultType(const ASTContext &Context) const { |
3730 | return getReturnType().getNonLValueExprType(Context); |
3731 | } |
3732 | |
3733 | static StringRef getNameForCallConv(CallingConv CC); |
3734 | |
3735 | static bool classof(const Type *T) { |
3736 | return T->getTypeClass() == FunctionNoProto || |
3737 | T->getTypeClass() == FunctionProto; |
3738 | } |
3739 | }; |
3740 | |
3741 | /// Represents a K&R-style 'int foo()' function, which has |
3742 | /// no information available about its arguments. |
3743 | class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode { |
3744 | friend class ASTContext; // ASTContext creates these. |
3745 | |
3746 | FunctionNoProtoType(QualType Result, QualType Canonical, ExtInfo Info) |
3747 | : FunctionType(FunctionNoProto, Result, Canonical, |
3748 | /*Dependent=*/false, /*InstantiationDependent=*/false, |
3749 | Result->isVariablyModifiedType(), |
3750 | /*ContainsUnexpandedParameterPack=*/false, Info) {} |
3751 | |
3752 | public: |
3753 | // No additional state past what FunctionType provides. |
3754 | |
3755 | bool isSugared() const { return false; } |
3756 | QualType desugar() const { return QualType(this, 0); } |
3757 | |
3758 | void Profile(llvm::FoldingSetNodeID &ID) { |
3759 | Profile(ID, getReturnType(), getExtInfo()); |
3760 | } |
3761 | |
3762 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType, |
3763 | ExtInfo Info) { |
3764 | Info.Profile(ID); |
3765 | ID.AddPointer(ResultType.getAsOpaquePtr()); |
3766 | } |
3767 | |
3768 | static bool classof(const Type *T) { |
3769 | return T->getTypeClass() == FunctionNoProto; |
3770 | } |
3771 | }; |
3772 | |
3773 | /// Represents a prototype with parameter type info, e.g. |
3774 | /// 'int foo(int)' or 'int foo(void)'. 'void' is represented as having no |
3775 | /// parameters, not as having a single void parameter. Such a type can have |
3776 | /// an exception specification, but this specification is not part of the |
3777 | /// canonical type. FunctionProtoType has several trailing objects, some of |
3778 | /// which optional. For more information about the trailing objects see |
3779 | /// the first comment inside FunctionProtoType. |
3780 | class FunctionProtoType final |
3781 | : public FunctionType, |
3782 | public llvm::FoldingSetNode, |
3783 | private llvm::TrailingObjects< |
3784 | FunctionProtoType, QualType, SourceLocation, |
3785 | FunctionType::FunctionTypeExtraBitfields, FunctionType::ExceptionType, |
3786 | Expr *, FunctionDecl *, FunctionType::ExtParameterInfo, Qualifiers> { |
3787 | friend class ASTContext; // ASTContext creates these. |
3788 | friend TrailingObjects; |
3789 | |
3790 | // FunctionProtoType is followed by several trailing objects, some of |
3791 | // which optional. They are in order: |
3792 | // |
3793 | // * An array of getNumParams() QualType holding the parameter types. |
3794 | // Always present. Note that for the vast majority of FunctionProtoType, |
3795 | // these will be the only trailing objects. |
3796 | // |
3797 | // * Optionally if the function is variadic, the SourceLocation of the |
3798 | // ellipsis. |
3799 | // |
3800 | // * Optionally if some extra data is stored in FunctionTypeExtraBitfields |
3801 | // (see FunctionTypeExtraBitfields and FunctionTypeBitfields): |
3802 | // a single FunctionTypeExtraBitfields. Present if and only if |
3803 | // hasExtraBitfields() is true. |
3804 | // |
3805 | // * Optionally exactly one of: |
3806 | // * an array of getNumExceptions() ExceptionType, |
3807 | // * a single Expr *, |
3808 | // * a pair of FunctionDecl *, |
3809 | // * a single FunctionDecl * |
3810 | // used to store information about the various types of exception |
3811 | // specification. See getExceptionSpecSize for the details. |
3812 | // |
3813 | // * Optionally an array of getNumParams() ExtParameterInfo holding |
3814 | // an ExtParameterInfo for each of the parameters. Present if and |
3815 | // only if hasExtParameterInfos() is true. |
3816 | // |
3817 | // * Optionally a Qualifiers object to represent extra qualifiers that can't |
3818 | // be represented by FunctionTypeBitfields.FastTypeQuals. Present if and only |
3819 | // if hasExtQualifiers() is true. |
3820 | // |
3821 | // The optional FunctionTypeExtraBitfields has to be before the data |
3822 | // related to the exception specification since it contains the number |
3823 | // of exception types. |
3824 | // |
3825 | // We put the ExtParameterInfos last. If all were equal, it would make |
3826 | // more sense to put these before the exception specification, because |
3827 | // it's much easier to skip past them compared to the elaborate switch |
3828 | // required to skip the exception specification. However, all is not |
3829 | // equal; ExtParameterInfos are used to model very uncommon features, |
3830 | // and it's better not to burden the more common paths. |
3831 | |
3832 | public: |
3833 | /// Holds information about the various types of exception specification. |
3834 | /// ExceptionSpecInfo is not stored as such in FunctionProtoType but is |
3835 | /// used to group together the various bits of information about the |
3836 | /// exception specification. |
3837 | struct ExceptionSpecInfo { |
3838 | /// The kind of exception specification this is. |
3839 | ExceptionSpecificationType Type = EST_None; |
3840 | |
3841 | /// Explicitly-specified list of exception types. |
3842 | ArrayRef<QualType> Exceptions; |
3843 | |
3844 | /// Noexcept expression, if this is a computed noexcept specification. |
3845 | Expr *NoexceptExpr = nullptr; |
3846 | |
3847 | /// The function whose exception specification this is, for |
3848 | /// EST_Unevaluated and EST_Uninstantiated. |
3849 | FunctionDecl *SourceDecl = nullptr; |
3850 | |
3851 | /// The function template whose exception specification this is instantiated |
3852 | /// from, for EST_Uninstantiated. |
3853 | FunctionDecl *SourceTemplate = nullptr; |
3854 | |
3855 | ExceptionSpecInfo() = default; |
3856 | |
3857 | ExceptionSpecInfo(ExceptionSpecificationType EST) : Type(EST) {} |
3858 | }; |
3859 | |
3860 | /// Extra information about a function prototype. ExtProtoInfo is not |
3861 | /// stored as such in FunctionProtoType but is used to group together |
3862 | /// the various bits of extra information about a function prototype. |
3863 | struct ExtProtoInfo { |
3864 | FunctionType::ExtInfo ExtInfo; |
3865 | bool Variadic : 1; |
3866 | bool HasTrailingReturn : 1; |
3867 | Qualifiers TypeQuals; |
3868 | RefQualifierKind RefQualifier = RQ_None; |
3869 | ExceptionSpecInfo ExceptionSpec; |
3870 | const ExtParameterInfo *ExtParameterInfos = nullptr; |
3871 | SourceLocation EllipsisLoc; |
3872 | |
3873 | ExtProtoInfo() : Variadic(false), HasTrailingReturn(false) {} |
3874 | |
3875 | ExtProtoInfo(CallingConv CC) |
3876 | : ExtInfo(CC), Variadic(false), HasTrailingReturn(false) {} |
3877 | |
3878 | ExtProtoInfo withExceptionSpec(const ExceptionSpecInfo &ESI) { |
3879 | ExtProtoInfo Result(*this); |
3880 | Result.ExceptionSpec = ESI; |
3881 | return Result; |
3882 | } |
3883 | }; |
3884 | |
3885 | private: |
3886 | unsigned numTrailingObjects(OverloadToken<QualType>) const { |
3887 | return getNumParams(); |
3888 | } |
3889 | |
3890 | unsigned numTrailingObjects(OverloadToken<SourceLocation>) const { |
3891 | return isVariadic(); |
3892 | } |
3893 | |
3894 | unsigned numTrailingObjects(OverloadToken<FunctionTypeExtraBitfields>) const { |
3895 | return hasExtraBitfields(); |
3896 | } |
3897 | |
3898 | unsigned numTrailingObjects(OverloadToken<ExceptionType>) const { |
3899 | return getExceptionSpecSize().NumExceptionType; |
3900 | } |
3901 | |
3902 | unsigned numTrailingObjects(OverloadToken<Expr *>) const { |
3903 | return getExceptionSpecSize().NumExprPtr; |
3904 | } |
3905 | |
3906 | unsigned numTrailingObjects(OverloadToken<FunctionDecl *>) const { |
3907 | return getExceptionSpecSize().NumFunctionDeclPtr; |
3908 | } |
3909 | |
3910 | unsigned numTrailingObjects(OverloadToken<ExtParameterInfo>) const { |
3911 | return hasExtParameterInfos() ? getNumParams() : 0; |
3912 | } |
3913 | |
3914 | /// Determine whether there are any argument types that |
3915 | /// contain an unexpanded parameter pack. |
3916 | static bool containsAnyUnexpandedParameterPack(const QualType *ArgArray, |
3917 | unsigned numArgs) { |
3918 | for (unsigned Idx = 0; Idx < numArgs; ++Idx) |
3919 | if (ArgArray[Idx]->containsUnexpandedParameterPack()) |
3920 | return true; |
3921 | |
3922 | return false; |
3923 | } |
3924 | |
3925 | FunctionProtoType(QualType result, ArrayRef<QualType> params, |
3926 | QualType canonical, const ExtProtoInfo &epi); |
3927 | |
3928 | /// This struct is returned by getExceptionSpecSize and is used to |
3929 | /// translate an ExceptionSpecificationType to the number and kind |
3930 | /// of trailing objects related to the exception specification. |
3931 | struct ExceptionSpecSizeHolder { |
3932 | unsigned NumExceptionType; |
3933 | unsigned NumExprPtr; |
3934 | unsigned NumFunctionDeclPtr; |
3935 | }; |
3936 | |
3937 | /// Return the number and kind of trailing objects |
3938 | /// related to the exception specification. |
3939 | static ExceptionSpecSizeHolder |
3940 | getExceptionSpecSize(ExceptionSpecificationType EST, unsigned NumExceptions) { |
3941 | switch (EST) { |
3942 | case EST_None: |
3943 | case EST_DynamicNone: |
3944 | case EST_MSAny: |
3945 | case EST_BasicNoexcept: |
3946 | case EST_Unparsed: |
3947 | case EST_NoThrow: |
3948 | return {0, 0, 0}; |
3949 | |
3950 | case EST_Dynamic: |
3951 | return {NumExceptions, 0, 0}; |
3952 | |
3953 | case EST_DependentNoexcept: |
3954 | case EST_NoexceptFalse: |
3955 | case EST_NoexceptTrue: |
3956 | return {0, 1, 0}; |
3957 | |
3958 | case EST_Uninstantiated: |
3959 | return {0, 0, 2}; |
3960 | |
3961 | case EST_Unevaluated: |
3962 | return {0, 0, 1}; |
3963 | } |
3964 | llvm_unreachable("bad exception specification kind")::llvm::llvm_unreachable_internal("bad exception specification kind" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 3964); |
3965 | } |
3966 | |
3967 | /// Return the number and kind of trailing objects |
3968 | /// related to the exception specification. |
3969 | ExceptionSpecSizeHolder getExceptionSpecSize() const { |
3970 | return getExceptionSpecSize(getExceptionSpecType(), getNumExceptions()); |
3971 | } |
3972 | |
3973 | /// Whether the trailing FunctionTypeExtraBitfields is present. |
3974 | static bool hasExtraBitfields(ExceptionSpecificationType EST) { |
3975 | // If the exception spec type is EST_Dynamic then we have > 0 exception |
3976 | // types and the exact number is stored in FunctionTypeExtraBitfields. |
3977 | return EST == EST_Dynamic; |
3978 | } |
3979 | |
3980 | /// Whether the trailing FunctionTypeExtraBitfields is present. |
3981 | bool hasExtraBitfields() const { |
3982 | return hasExtraBitfields(getExceptionSpecType()); |
3983 | } |
3984 | |
3985 | bool hasExtQualifiers() const { |
3986 | return FunctionTypeBits.HasExtQuals; |
3987 | } |
3988 | |
3989 | public: |
3990 | unsigned getNumParams() const { return FunctionTypeBits.NumParams; } |
3991 | |
3992 | QualType getParamType(unsigned i) const { |
3993 | assert(i < getNumParams() && "invalid parameter index")((i < getNumParams() && "invalid parameter index") ? static_cast<void> (0) : __assert_fail ("i < getNumParams() && \"invalid parameter index\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 3993, __PRETTY_FUNCTION__)); |
3994 | return param_type_begin()[i]; |
3995 | } |
3996 | |
3997 | ArrayRef<QualType> getParamTypes() const { |
3998 | return llvm::makeArrayRef(param_type_begin(), param_type_end()); |
3999 | } |
4000 | |
4001 | ExtProtoInfo getExtProtoInfo() const { |
4002 | ExtProtoInfo EPI; |
4003 | EPI.ExtInfo = getExtInfo(); |
4004 | EPI.Variadic = isVariadic(); |
4005 | EPI.EllipsisLoc = getEllipsisLoc(); |
4006 | EPI.HasTrailingReturn = hasTrailingReturn(); |
4007 | EPI.ExceptionSpec = getExceptionSpecInfo(); |
4008 | EPI.TypeQuals = getMethodQuals(); |
4009 | EPI.RefQualifier = getRefQualifier(); |
4010 | EPI.ExtParameterInfos = getExtParameterInfosOrNull(); |
4011 | return EPI; |
4012 | } |
4013 | |
4014 | /// Get the kind of exception specification on this function. |
4015 | ExceptionSpecificationType getExceptionSpecType() const { |
4016 | return static_cast<ExceptionSpecificationType>( |
4017 | FunctionTypeBits.ExceptionSpecType); |
4018 | } |
4019 | |
4020 | /// Return whether this function has any kind of exception spec. |
4021 | bool hasExceptionSpec() const { return getExceptionSpecType() != EST_None; } |
4022 | |
4023 | /// Return whether this function has a dynamic (throw) exception spec. |
4024 | bool hasDynamicExceptionSpec() const { |
4025 | return isDynamicExceptionSpec(getExceptionSpecType()); |
4026 | } |
4027 | |
4028 | /// Return whether this function has a noexcept exception spec. |
4029 | bool hasNoexceptExceptionSpec() const { |
4030 | return isNoexceptExceptionSpec(getExceptionSpecType()); |
4031 | } |
4032 | |
4033 | /// Return whether this function has a dependent exception spec. |
4034 | bool hasDependentExceptionSpec() const; |
4035 | |
4036 | /// Return whether this function has an instantiation-dependent exception |
4037 | /// spec. |
4038 | bool hasInstantiationDependentExceptionSpec() const; |
4039 | |
4040 | /// Return all the available information about this type's exception spec. |
4041 | ExceptionSpecInfo getExceptionSpecInfo() const { |
4042 | ExceptionSpecInfo Result; |
4043 | Result.Type = getExceptionSpecType(); |
4044 | if (Result.Type == EST_Dynamic) { |
4045 | Result.Exceptions = exceptions(); |
4046 | } else if (isComputedNoexcept(Result.Type)) { |
4047 | Result.NoexceptExpr = getNoexceptExpr(); |
4048 | } else if (Result.Type == EST_Uninstantiated) { |
4049 | Result.SourceDecl = getExceptionSpecDecl(); |
4050 | Result.SourceTemplate = getExceptionSpecTemplate(); |
4051 | } else if (Result.Type == EST_Unevaluated) { |
4052 | Result.SourceDecl = getExceptionSpecDecl(); |
4053 | } |
4054 | return Result; |
4055 | } |
4056 | |
4057 | /// Return the number of types in the exception specification. |
4058 | unsigned getNumExceptions() const { |
4059 | return getExceptionSpecType() == EST_Dynamic |
4060 | ? getTrailingObjects<FunctionTypeExtraBitfields>() |
4061 | ->NumExceptionType |
4062 | : 0; |
4063 | } |
4064 | |
4065 | /// Return the ith exception type, where 0 <= i < getNumExceptions(). |
4066 | QualType getExceptionType(unsigned i) const { |
4067 | assert(i < getNumExceptions() && "Invalid exception number!")((i < getNumExceptions() && "Invalid exception number!" ) ? static_cast<void> (0) : __assert_fail ("i < getNumExceptions() && \"Invalid exception number!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 4067, __PRETTY_FUNCTION__)); |
4068 | return exception_begin()[i]; |
4069 | } |
4070 | |
4071 | /// Return the expression inside noexcept(expression), or a null pointer |
4072 | /// if there is none (because the exception spec is not of this form). |
4073 | Expr *getNoexceptExpr() const { |
4074 | if (!isComputedNoexcept(getExceptionSpecType())) |
4075 | return nullptr; |
4076 | return *getTrailingObjects<Expr *>(); |
4077 | } |
4078 | |
4079 | /// If this function type has an exception specification which hasn't |
4080 | /// been determined yet (either because it has not been evaluated or because |
4081 | /// it has not been instantiated), this is the function whose exception |
4082 | /// specification is represented by this type. |
4083 | FunctionDecl *getExceptionSpecDecl() const { |
4084 | if (getExceptionSpecType() != EST_Uninstantiated && |
4085 | getExceptionSpecType() != EST_Unevaluated) |
4086 | return nullptr; |
4087 | return getTrailingObjects<FunctionDecl *>()[0]; |
4088 | } |
4089 | |
4090 | /// If this function type has an uninstantiated exception |
4091 | /// specification, this is the function whose exception specification |
4092 | /// should be instantiated to find the exception specification for |
4093 | /// this type. |
4094 | FunctionDecl *getExceptionSpecTemplate() const { |
4095 | if (getExceptionSpecType() != EST_Uninstantiated) |
4096 | return nullptr; |
4097 | return getTrailingObjects<FunctionDecl *>()[1]; |
4098 | } |
4099 | |
4100 | /// Determine whether this function type has a non-throwing exception |
4101 | /// specification. |
4102 | CanThrowResult canThrow() const; |
4103 | |
4104 | /// Determine whether this function type has a non-throwing exception |
4105 | /// specification. If this depends on template arguments, returns |
4106 | /// \c ResultIfDependent. |
4107 | bool isNothrow(bool ResultIfDependent = false) const { |
4108 | return ResultIfDependent ? canThrow() != CT_Can : canThrow() == CT_Cannot; |
4109 | } |
4110 | |
4111 | /// Whether this function prototype is variadic. |
4112 | bool isVariadic() const { return FunctionTypeBits.Variadic; } |
4113 | |
4114 | SourceLocation getEllipsisLoc() const { |
4115 | return isVariadic() ? *getTrailingObjects<SourceLocation>() |
4116 | : SourceLocation(); |
4117 | } |
4118 | |
4119 | /// Determines whether this function prototype contains a |
4120 | /// parameter pack at the end. |
4121 | /// |
4122 | /// A function template whose last parameter is a parameter pack can be |
4123 | /// called with an arbitrary number of arguments, much like a variadic |
4124 | /// function. |
4125 | bool isTemplateVariadic() const; |
4126 | |
4127 | /// Whether this function prototype has a trailing return type. |
4128 | bool hasTrailingReturn() const { return FunctionTypeBits.HasTrailingReturn; } |
4129 | |
4130 | Qualifiers getMethodQuals() const { |
4131 | if (hasExtQualifiers()) |
4132 | return *getTrailingObjects<Qualifiers>(); |
4133 | else |
4134 | return getFastTypeQuals(); |
4135 | } |
4136 | |
4137 | /// Retrieve the ref-qualifier associated with this function type. |
4138 | RefQualifierKind getRefQualifier() const { |
4139 | return static_cast<RefQualifierKind>(FunctionTypeBits.RefQualifier); |
4140 | } |
4141 | |
4142 | using param_type_iterator = const QualType *; |
4143 | using param_type_range = llvm::iterator_range<param_type_iterator>; |
4144 | |
4145 | param_type_range param_types() const { |
4146 | return param_type_range(param_type_begin(), param_type_end()); |
4147 | } |
4148 | |
4149 | param_type_iterator param_type_begin() const { |
4150 | return getTrailingObjects<QualType>(); |
4151 | } |
4152 | |
4153 | param_type_iterator param_type_end() const { |
4154 | return param_type_begin() + getNumParams(); |
4155 | } |
4156 | |
4157 | using exception_iterator = const QualType *; |
4158 | |
4159 | ArrayRef<QualType> exceptions() const { |
4160 | return llvm::makeArrayRef(exception_begin(), exception_end()); |
4161 | } |
4162 | |
4163 | exception_iterator exception_begin() const { |
4164 | return reinterpret_cast<exception_iterator>( |
4165 | getTrailingObjects<ExceptionType>()); |
4166 | } |
4167 | |
4168 | exception_iterator exception_end() const { |
4169 | return exception_begin() + getNumExceptions(); |
4170 | } |
4171 | |
4172 | /// Is there any interesting extra information for any of the parameters |
4173 | /// of this function type? |
4174 | bool hasExtParameterInfos() const { |
4175 | return FunctionTypeBits.HasExtParameterInfos; |
4176 | } |
4177 | |
4178 | ArrayRef<ExtParameterInfo> getExtParameterInfos() const { |
4179 | assert(hasExtParameterInfos())((hasExtParameterInfos()) ? static_cast<void> (0) : __assert_fail ("hasExtParameterInfos()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 4179, __PRETTY_FUNCTION__)); |
4180 | return ArrayRef<ExtParameterInfo>(getTrailingObjects<ExtParameterInfo>(), |
4181 | getNumParams()); |
4182 | } |
4183 | |
4184 | /// Return a pointer to the beginning of the array of extra parameter |
4185 | /// information, if present, or else null if none of the parameters |
4186 | /// carry it. This is equivalent to getExtProtoInfo().ExtParameterInfos. |
4187 | const ExtParameterInfo *getExtParameterInfosOrNull() const { |
4188 | if (!hasExtParameterInfos()) |
4189 | return nullptr; |
4190 | return getTrailingObjects<ExtParameterInfo>(); |
4191 | } |
4192 | |
4193 | ExtParameterInfo getExtParameterInfo(unsigned I) const { |
4194 | assert(I < getNumParams() && "parameter index out of range")((I < getNumParams() && "parameter index out of range" ) ? static_cast<void> (0) : __assert_fail ("I < getNumParams() && \"parameter index out of range\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 4194, __PRETTY_FUNCTION__)); |
4195 | if (hasExtParameterInfos()) |
4196 | return getTrailingObjects<ExtParameterInfo>()[I]; |
4197 | return ExtParameterInfo(); |
4198 | } |
4199 | |
4200 | ParameterABI getParameterABI(unsigned I) const { |
4201 | assert(I < getNumParams() && "parameter index out of range")((I < getNumParams() && "parameter index out of range" ) ? static_cast<void> (0) : __assert_fail ("I < getNumParams() && \"parameter index out of range\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 4201, __PRETTY_FUNCTION__)); |
4202 | if (hasExtParameterInfos()) |
4203 | return getTrailingObjects<ExtParameterInfo>()[I].getABI(); |
4204 | return ParameterABI::Ordinary; |
4205 | } |
4206 | |
4207 | bool isParamConsumed(unsigned I) const { |
4208 | assert(I < getNumParams() && "parameter index out of range")((I < getNumParams() && "parameter index out of range" ) ? static_cast<void> (0) : __assert_fail ("I < getNumParams() && \"parameter index out of range\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 4208, __PRETTY_FUNCTION__)); |
4209 | if (hasExtParameterInfos()) |
4210 | return getTrailingObjects<ExtParameterInfo>()[I].isConsumed(); |
4211 | return false; |
4212 | } |
4213 | |
4214 | bool isSugared() const { return false; } |
4215 | QualType desugar() const { return QualType(this, 0); } |
4216 | |
4217 | void printExceptionSpecification(raw_ostream &OS, |
4218 | const PrintingPolicy &Policy) const; |
4219 | |
4220 | static bool classof(const Type *T) { |
4221 | return T->getTypeClass() == FunctionProto; |
4222 | } |
4223 | |
4224 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx); |
4225 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Result, |
4226 | param_type_iterator ArgTys, unsigned NumArgs, |
4227 | const ExtProtoInfo &EPI, const ASTContext &Context, |
4228 | bool Canonical); |
4229 | }; |
4230 | |
4231 | /// Represents the dependent type named by a dependently-scoped |
4232 | /// typename using declaration, e.g. |
4233 | /// using typename Base<T>::foo; |
4234 | /// |
4235 | /// Template instantiation turns these into the underlying type. |
4236 | class UnresolvedUsingType : public Type { |
4237 | friend class ASTContext; // ASTContext creates these. |
4238 | |
4239 | UnresolvedUsingTypenameDecl *Decl; |
4240 | |
4241 | UnresolvedUsingType(const UnresolvedUsingTypenameDecl *D) |
4242 | : Type(UnresolvedUsing, QualType(), true, true, false, |
4243 | /*ContainsUnexpandedParameterPack=*/false), |
4244 | Decl(const_cast<UnresolvedUsingTypenameDecl*>(D)) {} |
4245 | |
4246 | public: |
4247 | UnresolvedUsingTypenameDecl *getDecl() const { return Decl; } |
4248 | |
4249 | bool isSugared() const { return false; } |
4250 | QualType desugar() const { return QualType(this, 0); } |
4251 | |
4252 | static bool classof(const Type *T) { |
4253 | return T->getTypeClass() == UnresolvedUsing; |
4254 | } |
4255 | |
4256 | void Profile(llvm::FoldingSetNodeID &ID) { |
4257 | return Profile(ID, Decl); |
4258 | } |
4259 | |
4260 | static void Profile(llvm::FoldingSetNodeID &ID, |
4261 | UnresolvedUsingTypenameDecl *D) { |
4262 | ID.AddPointer(D); |
4263 | } |
4264 | }; |
4265 | |
4266 | class TypedefType : public Type { |
4267 | TypedefNameDecl *Decl; |
4268 | |
4269 | protected: |
4270 | friend class ASTContext; // ASTContext creates these. |
4271 | |
4272 | TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType can) |
4273 | : Type(tc, can, can->isDependentType(), |
4274 | can->isInstantiationDependentType(), |
4275 | can->isVariablyModifiedType(), |
4276 | /*ContainsUnexpandedParameterPack=*/false), |
4277 | Decl(const_cast<TypedefNameDecl*>(D)) { |
4278 | assert(!isa<TypedefType>(can) && "Invalid canonical type")((!isa<TypedefType>(can) && "Invalid canonical type" ) ? static_cast<void> (0) : __assert_fail ("!isa<TypedefType>(can) && \"Invalid canonical type\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 4278, __PRETTY_FUNCTION__)); |
4279 | } |
4280 | |
4281 | public: |
4282 | TypedefNameDecl *getDecl() const { return Decl; } |
4283 | |
4284 | bool isSugared() const { return true; } |
4285 | QualType desugar() const; |
4286 | |
4287 | static bool classof(const Type *T) { return T->getTypeClass() == Typedef; } |
4288 | }; |
4289 | |
4290 | /// Sugar type that represents a type that was qualified by a qualifier written |
4291 | /// as a macro invocation. |
4292 | class MacroQualifiedType : public Type { |
4293 | friend class ASTContext; // ASTContext creates these. |
4294 | |
4295 | QualType UnderlyingTy; |
4296 | const IdentifierInfo *MacroII; |
4297 | |
4298 | MacroQualifiedType(QualType UnderlyingTy, QualType CanonTy, |
4299 | const IdentifierInfo *MacroII) |
4300 | : Type(MacroQualified, CanonTy, UnderlyingTy->isDependentType(), |
4301 | UnderlyingTy->isInstantiationDependentType(), |
4302 | UnderlyingTy->isVariablyModifiedType(), |
4303 | UnderlyingTy->containsUnexpandedParameterPack()), |
4304 | UnderlyingTy(UnderlyingTy), MacroII(MacroII) { |
4305 | assert(isa<AttributedType>(UnderlyingTy) &&((isa<AttributedType>(UnderlyingTy) && "Expected a macro qualified type to only wrap attributed types." ) ? static_cast<void> (0) : __assert_fail ("isa<AttributedType>(UnderlyingTy) && \"Expected a macro qualified type to only wrap attributed types.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 4306, __PRETTY_FUNCTION__)) |
4306 | "Expected a macro qualified type to only wrap attributed types.")((isa<AttributedType>(UnderlyingTy) && "Expected a macro qualified type to only wrap attributed types." ) ? static_cast<void> (0) : __assert_fail ("isa<AttributedType>(UnderlyingTy) && \"Expected a macro qualified type to only wrap attributed types.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 4306, __PRETTY_FUNCTION__)); |
4307 | } |
4308 | |
4309 | public: |
4310 | const IdentifierInfo *getMacroIdentifier() const { return MacroII; } |
4311 | QualType getUnderlyingType() const { return UnderlyingTy; } |
4312 | |
4313 | /// Return this attributed type's modified type with no qualifiers attached to |
4314 | /// it. |
4315 | QualType getModifiedType() const; |
4316 | |
4317 | bool isSugared() const { return true; } |
4318 | QualType desugar() const; |
4319 | |
4320 | static bool classof(const Type *T) { |
4321 | return T->getTypeClass() == MacroQualified; |
4322 | } |
4323 | }; |
4324 | |
4325 | /// Represents a `typeof` (or __typeof__) expression (a GCC extension). |
4326 | class TypeOfExprType : public Type { |
4327 | Expr *TOExpr; |
4328 | |
4329 | protected: |
4330 | friend class ASTContext; // ASTContext creates these. |
4331 | |
4332 | TypeOfExprType(Expr *E, QualType can = QualType()); |
4333 | |
4334 | public: |
4335 | Expr *getUnderlyingExpr() const { return TOExpr; } |
4336 | |
4337 | /// Remove a single level of sugar. |
4338 | QualType desugar() const; |
4339 | |
4340 | /// Returns whether this type directly provides sugar. |
4341 | bool isSugared() const; |
4342 | |
4343 | static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; } |
4344 | }; |
4345 | |
4346 | /// Internal representation of canonical, dependent |
4347 | /// `typeof(expr)` types. |
4348 | /// |
4349 | /// This class is used internally by the ASTContext to manage |
4350 | /// canonical, dependent types, only. Clients will only see instances |
4351 | /// of this class via TypeOfExprType nodes. |
4352 | class DependentTypeOfExprType |
4353 | : public TypeOfExprType, public llvm::FoldingSetNode { |
4354 | const ASTContext &Context; |
4355 | |
4356 | public: |
4357 | DependentTypeOfExprType(const ASTContext &Context, Expr *E) |
4358 | : TypeOfExprType(E), Context(Context) {} |
4359 | |
4360 | void Profile(llvm::FoldingSetNodeID &ID) { |
4361 | Profile(ID, Context, getUnderlyingExpr()); |
4362 | } |
4363 | |
4364 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
4365 | Expr *E); |
4366 | }; |
4367 | |
4368 | /// Represents `typeof(type)`, a GCC extension. |
4369 | class TypeOfType : public Type { |
4370 | friend class ASTContext; // ASTContext creates these. |
4371 | |
4372 | QualType TOType; |
4373 | |
4374 | TypeOfType(QualType T, QualType can) |
4375 | : Type(TypeOf, can, T->isDependentType(), |
4376 | T->isInstantiationDependentType(), |
4377 | T->isVariablyModifiedType(), |
4378 | T->containsUnexpandedParameterPack()), |
4379 | TOType(T) { |
4380 | assert(!isa<TypedefType>(can) && "Invalid canonical type")((!isa<TypedefType>(can) && "Invalid canonical type" ) ? static_cast<void> (0) : __assert_fail ("!isa<TypedefType>(can) && \"Invalid canonical type\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 4380, __PRETTY_FUNCTION__)); |
4381 | } |
4382 | |
4383 | public: |
4384 | QualType getUnderlyingType() const { return TOType; } |
4385 | |
4386 | /// Remove a single level of sugar. |
4387 | QualType desugar() const { return getUnderlyingType(); } |
4388 | |
4389 | /// Returns whether this type directly provides sugar. |
4390 | bool isSugared() const { return true; } |
4391 | |
4392 | static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; } |
4393 | }; |
4394 | |
4395 | /// Represents the type `decltype(expr)` (C++11). |
4396 | class DecltypeType : public Type { |
4397 | Expr *E; |
4398 | QualType UnderlyingType; |
4399 | |
4400 | protected: |
4401 | friend class ASTContext; // ASTContext creates these. |
4402 | |
4403 | DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType()); |
4404 | |
4405 | public: |
4406 | Expr *getUnderlyingExpr() const { return E; } |
4407 | QualType getUnderlyingType() const { return UnderlyingType; } |
4408 | |
4409 | /// Remove a single level of sugar. |
4410 | QualType desugar() const; |
4411 | |
4412 | /// Returns whether this type directly provides sugar. |
4413 | bool isSugared() const; |
4414 | |
4415 | static bool classof(const Type *T) { return T->getTypeClass() == Decltype; } |
4416 | }; |
4417 | |
4418 | /// Internal representation of canonical, dependent |
4419 | /// decltype(expr) types. |
4420 | /// |
4421 | /// This class is used internally by the ASTContext to manage |
4422 | /// canonical, dependent types, only. Clients will only see instances |
4423 | /// of this class via DecltypeType nodes. |
4424 | class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode { |
4425 | const ASTContext &Context; |
4426 | |
4427 | public: |
4428 | DependentDecltypeType(const ASTContext &Context, Expr *E); |
4429 | |
4430 | void Profile(llvm::FoldingSetNodeID &ID) { |
4431 | Profile(ID, Context, getUnderlyingExpr()); |
4432 | } |
4433 | |
4434 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
4435 | Expr *E); |
4436 | }; |
4437 | |
4438 | /// A unary type transform, which is a type constructed from another. |
4439 | class UnaryTransformType : public Type { |
4440 | public: |
4441 | enum UTTKind { |
4442 | EnumUnderlyingType |
4443 | }; |
4444 | |
4445 | private: |
4446 | /// The untransformed type. |
4447 | QualType BaseType; |
4448 | |
4449 | /// The transformed type if not dependent, otherwise the same as BaseType. |
4450 | QualType UnderlyingType; |
4451 | |
4452 | UTTKind UKind; |
4453 | |
4454 | protected: |
4455 | friend class ASTContext; |
4456 | |
4457 | UnaryTransformType(QualType BaseTy, QualType UnderlyingTy, UTTKind UKind, |
4458 | QualType CanonicalTy); |
4459 | |
4460 | public: |
4461 | bool isSugared() const { return !isDependentType(); } |
4462 | QualType desugar() const { return UnderlyingType; } |
4463 | |
4464 | QualType getUnderlyingType() const { return UnderlyingType; } |
4465 | QualType getBaseType() const { return BaseType; } |
4466 | |
4467 | UTTKind getUTTKind() const { return UKind; } |
4468 | |
4469 | static bool classof(const Type *T) { |
4470 | return T->getTypeClass() == UnaryTransform; |
4471 | } |
4472 | }; |
4473 | |
4474 | /// Internal representation of canonical, dependent |
4475 | /// __underlying_type(type) types. |
4476 | /// |
4477 | /// This class is used internally by the ASTContext to manage |
4478 | /// canonical, dependent types, only. Clients will only see instances |
4479 | /// of this class via UnaryTransformType nodes. |
4480 | class DependentUnaryTransformType : public UnaryTransformType, |
4481 | public llvm::FoldingSetNode { |
4482 | public: |
4483 | DependentUnaryTransformType(const ASTContext &C, QualType BaseType, |
4484 | UTTKind UKind); |
4485 | |
4486 | void Profile(llvm::FoldingSetNodeID &ID) { |
4487 | Profile(ID, getBaseType(), getUTTKind()); |
4488 | } |
4489 | |
4490 | static void Profile(llvm::FoldingSetNodeID &ID, QualType BaseType, |
4491 | UTTKind UKind) { |
4492 | ID.AddPointer(BaseType.getAsOpaquePtr()); |
4493 | ID.AddInteger((unsigned)UKind); |
4494 | } |
4495 | }; |
4496 | |
4497 | class TagType : public Type { |
4498 | friend class ASTReader; |
4499 | template <class T> friend class serialization::AbstractTypeReader; |
4500 | |
4501 | /// Stores the TagDecl associated with this type. The decl may point to any |
4502 | /// TagDecl that declares the entity. |
4503 | TagDecl *decl; |
4504 | |
4505 | protected: |
4506 | TagType(TypeClass TC, const TagDecl *D, QualType can); |
4507 | |
4508 | public: |
4509 | TagDecl *getDecl() const; |
4510 | |
4511 | /// Determines whether this type is in the process of being defined. |
4512 | bool isBeingDefined() const; |
4513 | |
4514 | static bool classof(const Type *T) { |
4515 | return T->getTypeClass() == Enum || T->getTypeClass() == Record; |
4516 | } |
4517 | }; |
4518 | |
4519 | /// A helper class that allows the use of isa/cast/dyncast |
4520 | /// to detect TagType objects of structs/unions/classes. |
4521 | class RecordType : public TagType { |
4522 | protected: |
4523 | friend class ASTContext; // ASTContext creates these. |
4524 | |
4525 | explicit RecordType(const RecordDecl *D) |
4526 | : TagType(Record, reinterpret_cast<const TagDecl*>(D), QualType()) {} |
4527 | explicit RecordType(TypeClass TC, RecordDecl *D) |
4528 | : TagType(TC, reinterpret_cast<const TagDecl*>(D), QualType()) {} |
4529 | |
4530 | public: |
4531 | RecordDecl *getDecl() const { |
4532 | return reinterpret_cast<RecordDecl*>(TagType::getDecl()); |
4533 | } |
4534 | |
4535 | /// Recursively check all fields in the record for const-ness. If any field |
4536 | /// is declared const, return true. Otherwise, return false. |
4537 | bool hasConstFields() const; |
4538 | |
4539 | bool isSugared() const { return false; } |
4540 | QualType desugar() const { return QualType(this, 0); } |
4541 | |
4542 | static bool classof(const Type *T) { return T->getTypeClass() == Record; } |
4543 | }; |
4544 | |
4545 | /// A helper class that allows the use of isa/cast/dyncast |
4546 | /// to detect TagType objects of enums. |
4547 | class EnumType : public TagType { |
4548 | friend class ASTContext; // ASTContext creates these. |
4549 | |
4550 | explicit EnumType(const EnumDecl *D) |
4551 | : TagType(Enum, reinterpret_cast<const TagDecl*>(D), QualType()) {} |
4552 | |
4553 | public: |
4554 | EnumDecl *getDecl() const { |
4555 | return reinterpret_cast<EnumDecl*>(TagType::getDecl()); |
4556 | } |
4557 | |
4558 | bool isSugared() const { return false; } |
4559 | QualType desugar() const { return QualType(this, 0); } |
4560 | |
4561 | static bool classof(const Type *T) { return T->getTypeClass() == Enum; } |
4562 | }; |
4563 | |
4564 | /// An attributed type is a type to which a type attribute has been applied. |
4565 | /// |
4566 | /// The "modified type" is the fully-sugared type to which the attributed |
4567 | /// type was applied; generally it is not canonically equivalent to the |
4568 | /// attributed type. The "equivalent type" is the minimally-desugared type |
4569 | /// which the type is canonically equivalent to. |
4570 | /// |
4571 | /// For example, in the following attributed type: |
4572 | /// int32_t __attribute__((vector_size(16))) |
4573 | /// - the modified type is the TypedefType for int32_t |
4574 | /// - the equivalent type is VectorType(16, int32_t) |
4575 | /// - the canonical type is VectorType(16, int) |
4576 | class AttributedType : public Type, public llvm::FoldingSetNode { |
4577 | public: |
4578 | using Kind = attr::Kind; |
4579 | |
4580 | private: |
4581 | friend class ASTContext; // ASTContext creates these |
4582 | |
4583 | QualType ModifiedType; |
4584 | QualType EquivalentType; |
4585 | |
4586 | AttributedType(QualType canon, attr::Kind attrKind, QualType modified, |
4587 | QualType equivalent) |
4588 | : Type(Attributed, canon, equivalent->isDependentType(), |
4589 | equivalent->isInstantiationDependentType(), |
4590 | equivalent->isVariablyModifiedType(), |
4591 | equivalent->containsUnexpandedParameterPack()), |
4592 | ModifiedType(modified), EquivalentType(equivalent) { |
4593 | AttributedTypeBits.AttrKind = attrKind; |
4594 | } |
4595 | |
4596 | public: |
4597 | Kind getAttrKind() const { |
4598 | return static_cast<Kind>(AttributedTypeBits.AttrKind); |
4599 | } |
4600 | |
4601 | QualType getModifiedType() const { return ModifiedType; } |
4602 | QualType getEquivalentType() const { return EquivalentType; } |
4603 | |
4604 | bool isSugared() const { return true; } |
4605 | QualType desugar() const { return getEquivalentType(); } |
4606 | |
4607 | /// Does this attribute behave like a type qualifier? |
4608 | /// |
4609 | /// A type qualifier adjusts a type to provide specialized rules for |
4610 | /// a specific object, like the standard const and volatile qualifiers. |
4611 | /// This includes attributes controlling things like nullability, |
4612 | /// address spaces, and ARC ownership. The value of the object is still |
4613 | /// largely described by the modified type. |
4614 | /// |
4615 | /// In contrast, many type attributes "rewrite" their modified type to |
4616 | /// produce a fundamentally different type, not necessarily related in any |
4617 | /// formalizable way to the original type. For example, calling convention |
4618 | /// and vector attributes are not simple type qualifiers. |
4619 | /// |
4620 | /// Type qualifiers are often, but not always, reflected in the canonical |
4621 | /// type. |
4622 | bool isQualifier() const; |
4623 | |
4624 | bool isMSTypeSpec() const; |
4625 | |
4626 | bool isCallingConv() const; |
4627 | |
4628 | llvm::Optional<NullabilityKind> getImmediateNullability() const; |
4629 | |
4630 | /// Retrieve the attribute kind corresponding to the given |
4631 | /// nullability kind. |
4632 | static Kind getNullabilityAttrKind(NullabilityKind kind) { |
4633 | switch (kind) { |
4634 | case NullabilityKind::NonNull: |
4635 | return attr::TypeNonNull; |
4636 | |
4637 | case NullabilityKind::Nullable: |
4638 | return attr::TypeNullable; |
4639 | |
4640 | case NullabilityKind::Unspecified: |
4641 | return attr::TypeNullUnspecified; |
4642 | } |
4643 | llvm_unreachable("Unknown nullability kind.")::llvm::llvm_unreachable_internal("Unknown nullability kind." , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 4643); |
4644 | } |
4645 | |
4646 | /// Strip off the top-level nullability annotation on the given |
4647 | /// type, if it's there. |
4648 | /// |
4649 | /// \param T The type to strip. If the type is exactly an |
4650 | /// AttributedType specifying nullability (without looking through |
4651 | /// type sugar), the nullability is returned and this type changed |
4652 | /// to the underlying modified type. |
4653 | /// |
4654 | /// \returns the top-level nullability, if present. |
4655 | static Optional<NullabilityKind> stripOuterNullability(QualType &T); |
4656 | |
4657 | void Profile(llvm::FoldingSetNodeID &ID) { |
4658 | Profile(ID, getAttrKind(), ModifiedType, EquivalentType); |
4659 | } |
4660 | |
4661 | static void Profile(llvm::FoldingSetNodeID &ID, Kind attrKind, |
4662 | QualType modified, QualType equivalent) { |
4663 | ID.AddInteger(attrKind); |
4664 | ID.AddPointer(modified.getAsOpaquePtr()); |
4665 | ID.AddPointer(equivalent.getAsOpaquePtr()); |
4666 | } |
4667 | |
4668 | static bool classof(const Type *T) { |
4669 | return T->getTypeClass() == Attributed; |
4670 | } |
4671 | }; |
4672 | |
4673 | class TemplateTypeParmType : public Type, public llvm::FoldingSetNode { |
4674 | friend class ASTContext; // ASTContext creates these |
4675 | |
4676 | // Helper data collector for canonical types. |
4677 | struct CanonicalTTPTInfo { |
4678 | unsigned Depth : 15; |
4679 | unsigned ParameterPack : 1; |
4680 | unsigned Index : 16; |
4681 | }; |
4682 | |
4683 | union { |
4684 | // Info for the canonical type. |
4685 | CanonicalTTPTInfo CanTTPTInfo; |
4686 | |
4687 | // Info for the non-canonical type. |
4688 | TemplateTypeParmDecl *TTPDecl; |
4689 | }; |
4690 | |
4691 | /// Build a non-canonical type. |
4692 | TemplateTypeParmType(TemplateTypeParmDecl *TTPDecl, QualType Canon) |
4693 | : Type(TemplateTypeParm, Canon, /*Dependent=*/true, |
4694 | /*InstantiationDependent=*/true, |
4695 | /*VariablyModified=*/false, |
4696 | Canon->containsUnexpandedParameterPack()), |
4697 | TTPDecl(TTPDecl) {} |
4698 | |
4699 | /// Build the canonical type. |
4700 | TemplateTypeParmType(unsigned D, unsigned I, bool PP) |
4701 | : Type(TemplateTypeParm, QualType(this, 0), |
4702 | /*Dependent=*/true, |
4703 | /*InstantiationDependent=*/true, |
4704 | /*VariablyModified=*/false, PP) { |
4705 | CanTTPTInfo.Depth = D; |
4706 | CanTTPTInfo.Index = I; |
4707 | CanTTPTInfo.ParameterPack = PP; |
4708 | } |
4709 | |
4710 | const CanonicalTTPTInfo& getCanTTPTInfo() const { |
4711 | QualType Can = getCanonicalTypeInternal(); |
4712 | return Can->castAs<TemplateTypeParmType>()->CanTTPTInfo; |
4713 | } |
4714 | |
4715 | public: |
4716 | unsigned getDepth() const { return getCanTTPTInfo().Depth; } |
4717 | unsigned getIndex() const { return getCanTTPTInfo().Index; } |
4718 | bool isParameterPack() const { return getCanTTPTInfo().ParameterPack; } |
4719 | |
4720 | TemplateTypeParmDecl *getDecl() const { |
4721 | return isCanonicalUnqualified() ? nullptr : TTPDecl; |
4722 | } |
4723 | |
4724 | IdentifierInfo *getIdentifier() const; |
4725 | |
4726 | bool isSugared() const { return false; } |
4727 | QualType desugar() const { return QualType(this, 0); } |
4728 | |
4729 | void Profile(llvm::FoldingSetNodeID &ID) { |
4730 | Profile(ID, getDepth(), getIndex(), isParameterPack(), getDecl()); |
4731 | } |
4732 | |
4733 | static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth, |
4734 | unsigned Index, bool ParameterPack, |
4735 | TemplateTypeParmDecl *TTPDecl) { |
4736 | ID.AddInteger(Depth); |
4737 | ID.AddInteger(Index); |
4738 | ID.AddBoolean(ParameterPack); |
4739 | ID.AddPointer(TTPDecl); |
4740 | } |
4741 | |
4742 | static bool classof(const Type *T) { |
4743 | return T->getTypeClass() == TemplateTypeParm; |
4744 | } |
4745 | }; |
4746 | |
4747 | /// Represents the result of substituting a type for a template |
4748 | /// type parameter. |
4749 | /// |
4750 | /// Within an instantiated template, all template type parameters have |
4751 | /// been replaced with these. They are used solely to record that a |
4752 | /// type was originally written as a template type parameter; |
4753 | /// therefore they are never canonical. |
4754 | class SubstTemplateTypeParmType : public Type, public llvm::FoldingSetNode { |
4755 | friend class ASTContext; |
4756 | |
4757 | // The original type parameter. |
4758 | const TemplateTypeParmType *Replaced; |
4759 | |
4760 | SubstTemplateTypeParmType(const TemplateTypeParmType *Param, QualType Canon) |
4761 | : Type(SubstTemplateTypeParm, Canon, Canon->isDependentType(), |
4762 | Canon->isInstantiationDependentType(), |
4763 | Canon->isVariablyModifiedType(), |
4764 | Canon->containsUnexpandedParameterPack()), |
4765 | Replaced(Param) {} |
4766 | |
4767 | public: |
4768 | /// Gets the template parameter that was substituted for. |
4769 | const TemplateTypeParmType *getReplacedParameter() const { |
4770 | return Replaced; |
4771 | } |
4772 | |
4773 | /// Gets the type that was substituted for the template |
4774 | /// parameter. |
4775 | QualType getReplacementType() const { |
4776 | return getCanonicalTypeInternal(); |
4777 | } |
4778 | |
4779 | bool isSugared() const { return true; } |
4780 | QualType desugar() const { return getReplacementType(); } |
4781 | |
4782 | void Profile(llvm::FoldingSetNodeID &ID) { |
4783 | Profile(ID, getReplacedParameter(), getReplacementType()); |
4784 | } |
4785 | |
4786 | static void Profile(llvm::FoldingSetNodeID &ID, |
4787 | const TemplateTypeParmType *Replaced, |
4788 | QualType Replacement) { |
4789 | ID.AddPointer(Replaced); |
4790 | ID.AddPointer(Replacement.getAsOpaquePtr()); |
4791 | } |
4792 | |
4793 | static bool classof(const Type *T) { |
4794 | return T->getTypeClass() == SubstTemplateTypeParm; |
4795 | } |
4796 | }; |
4797 | |
4798 | /// Represents the result of substituting a set of types for a template |
4799 | /// type parameter pack. |
4800 | /// |
4801 | /// When a pack expansion in the source code contains multiple parameter packs |
4802 | /// and those parameter packs correspond to different levels of template |
4803 | /// parameter lists, this type node is used to represent a template type |
4804 | /// parameter pack from an outer level, which has already had its argument pack |
4805 | /// substituted but that still lives within a pack expansion that itself |
4806 | /// could not be instantiated. When actually performing a substitution into |
4807 | /// that pack expansion (e.g., when all template parameters have corresponding |
4808 | /// arguments), this type will be replaced with the \c SubstTemplateTypeParmType |
4809 | /// at the current pack substitution index. |
4810 | class SubstTemplateTypeParmPackType : public Type, public llvm::FoldingSetNode { |
4811 | friend class ASTContext; |
4812 | |
4813 | /// The original type parameter. |
4814 | const TemplateTypeParmType *Replaced; |
4815 | |
4816 | /// A pointer to the set of template arguments that this |
4817 | /// parameter pack is instantiated with. |
4818 | const TemplateArgument *Arguments; |
4819 | |
4820 | SubstTemplateTypeParmPackType(const TemplateTypeParmType *Param, |
4821 | QualType Canon, |
4822 | const TemplateArgument &ArgPack); |
4823 | |
4824 | public: |
4825 | IdentifierInfo *getIdentifier() const { return Replaced->getIdentifier(); } |
4826 | |
4827 | /// Gets the template parameter that was substituted for. |
4828 | const TemplateTypeParmType *getReplacedParameter() const { |
4829 | return Replaced; |
4830 | } |
4831 | |
4832 | unsigned getNumArgs() const { |
4833 | return SubstTemplateTypeParmPackTypeBits.NumArgs; |
4834 | } |
4835 | |
4836 | bool isSugared() const { return false; } |
4837 | QualType desugar() const { return QualType(this, 0); } |
4838 | |
4839 | TemplateArgument getArgumentPack() const; |
4840 | |
4841 | void Profile(llvm::FoldingSetNodeID &ID); |
4842 | static void Profile(llvm::FoldingSetNodeID &ID, |
4843 | const TemplateTypeParmType *Replaced, |
4844 | const TemplateArgument &ArgPack); |
4845 | |
4846 | static bool classof(const Type *T) { |
4847 | return T->getTypeClass() == SubstTemplateTypeParmPack; |
4848 | } |
4849 | }; |
4850 | |
4851 | /// Common base class for placeholders for types that get replaced by |
4852 | /// placeholder type deduction: C++11 auto, C++14 decltype(auto), C++17 deduced |
4853 | /// class template types, and constrained type names. |
4854 | /// |
4855 | /// These types are usually a placeholder for a deduced type. However, before |
4856 | /// the initializer is attached, or (usually) if the initializer is |
4857 | /// type-dependent, there is no deduced type and the type is canonical. In |
4858 | /// the latter case, it is also a dependent type. |
4859 | class DeducedType : public Type { |
4860 | protected: |
4861 | DeducedType(TypeClass TC, QualType DeducedAsType, bool IsDependent, |
4862 | bool IsInstantiationDependent, bool ContainsParameterPack) |
4863 | : Type(TC, |
4864 | // FIXME: Retain the sugared deduced type? |
4865 | DeducedAsType.isNull() ? QualType(this, 0) |
4866 | : DeducedAsType.getCanonicalType(), |
4867 | IsDependent, IsInstantiationDependent, |
4868 | /*VariablyModified=*/false, ContainsParameterPack) { |
4869 | if (!DeducedAsType.isNull()) { |
4870 | if (DeducedAsType->isDependentType()) |
4871 | setDependent(); |
4872 | if (DeducedAsType->isInstantiationDependentType()) |
4873 | setInstantiationDependent(); |
4874 | if (DeducedAsType->containsUnexpandedParameterPack()) |
4875 | setContainsUnexpandedParameterPack(); |
4876 | } |
4877 | } |
4878 | |
4879 | public: |
4880 | bool isSugared() const { return !isCanonicalUnqualified(); } |
4881 | QualType desugar() const { return getCanonicalTypeInternal(); } |
4882 | |
4883 | /// Get the type deduced for this placeholder type, or null if it's |
4884 | /// either not been deduced or was deduced to a dependent type. |
4885 | QualType getDeducedType() const { |
4886 | return !isCanonicalUnqualified() ? getCanonicalTypeInternal() : QualType(); |
4887 | } |
4888 | bool isDeduced() const { |
4889 | return !isCanonicalUnqualified() || isDependentType(); |
4890 | } |
4891 | |
4892 | static bool classof(const Type *T) { |
4893 | return T->getTypeClass() == Auto || |
4894 | T->getTypeClass() == DeducedTemplateSpecialization; |
4895 | } |
4896 | }; |
4897 | |
4898 | /// Represents a C++11 auto or C++14 decltype(auto) type, possibly constrained |
4899 | /// by a type-constraint. |
4900 | class alignas(8) AutoType : public DeducedType, public llvm::FoldingSetNode { |
4901 | friend class ASTContext; // ASTContext creates these |
4902 | |
4903 | ConceptDecl *TypeConstraintConcept; |
4904 | |
4905 | AutoType(QualType DeducedAsType, AutoTypeKeyword Keyword, |
4906 | bool IsDeducedAsDependent, bool IsDeducedAsPack, ConceptDecl *CD, |
4907 | ArrayRef<TemplateArgument> TypeConstraintArgs); |
4908 | |
4909 | const TemplateArgument *getArgBuffer() const { |
4910 | return reinterpret_cast<const TemplateArgument*>(this+1); |
4911 | } |
4912 | |
4913 | TemplateArgument *getArgBuffer() { |
4914 | return reinterpret_cast<TemplateArgument*>(this+1); |
4915 | } |
4916 | |
4917 | public: |
4918 | /// Retrieve the template arguments. |
4919 | const TemplateArgument *getArgs() const { |
4920 | return getArgBuffer(); |
4921 | } |
4922 | |
4923 | /// Retrieve the number of template arguments. |
4924 | unsigned getNumArgs() const { |
4925 | return AutoTypeBits.NumArgs; |
4926 | } |
4927 | |
4928 | const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h |
4929 | |
4930 | ArrayRef<TemplateArgument> getTypeConstraintArguments() const { |
4931 | return {getArgs(), getNumArgs()}; |
4932 | } |
4933 | |
4934 | ConceptDecl *getTypeConstraintConcept() const { |
4935 | return TypeConstraintConcept; |
4936 | } |
4937 | |
4938 | bool isConstrained() const { |
4939 | return TypeConstraintConcept != nullptr; |
4940 | } |
4941 | |
4942 | bool isDecltypeAuto() const { |
4943 | return getKeyword() == AutoTypeKeyword::DecltypeAuto; |
4944 | } |
4945 | |
4946 | AutoTypeKeyword getKeyword() const { |
4947 | return (AutoTypeKeyword)AutoTypeBits.Keyword; |
4948 | } |
4949 | |
4950 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { |
4951 | Profile(ID, Context, getDeducedType(), getKeyword(), isDependentType(), |
4952 | getTypeConstraintConcept(), getTypeConstraintArguments()); |
4953 | } |
4954 | |
4955 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
4956 | QualType Deduced, AutoTypeKeyword Keyword, |
4957 | bool IsDependent, ConceptDecl *CD, |
4958 | ArrayRef<TemplateArgument> Arguments); |
4959 | |
4960 | static bool classof(const Type *T) { |
4961 | return T->getTypeClass() == Auto; |
4962 | } |
4963 | }; |
4964 | |
4965 | /// Represents a C++17 deduced template specialization type. |
4966 | class DeducedTemplateSpecializationType : public DeducedType, |
4967 | public llvm::FoldingSetNode { |
4968 | friend class ASTContext; // ASTContext creates these |
4969 | |
4970 | /// The name of the template whose arguments will be deduced. |
4971 | TemplateName Template; |
4972 | |
4973 | DeducedTemplateSpecializationType(TemplateName Template, |
4974 | QualType DeducedAsType, |
4975 | bool IsDeducedAsDependent) |
4976 | : DeducedType(DeducedTemplateSpecialization, DeducedAsType, |
4977 | IsDeducedAsDependent || Template.isDependent(), |
4978 | IsDeducedAsDependent || Template.isInstantiationDependent(), |
4979 | Template.containsUnexpandedParameterPack()), |
4980 | Template(Template) {} |
4981 | |
4982 | public: |
4983 | /// Retrieve the name of the template that we are deducing. |
4984 | TemplateName getTemplateName() const { return Template;} |
4985 | |
4986 | void Profile(llvm::FoldingSetNodeID &ID) { |
4987 | Profile(ID, getTemplateName(), getDeducedType(), isDependentType()); |
4988 | } |
4989 | |
4990 | static void Profile(llvm::FoldingSetNodeID &ID, TemplateName Template, |
4991 | QualType Deduced, bool IsDependent) { |
4992 | Template.Profile(ID); |
4993 | ID.AddPointer(Deduced.getAsOpaquePtr()); |
4994 | ID.AddBoolean(IsDependent); |
4995 | } |
4996 | |
4997 | static bool classof(const Type *T) { |
4998 | return T->getTypeClass() == DeducedTemplateSpecialization; |
4999 | } |
5000 | }; |
5001 | |
5002 | /// Represents a type template specialization; the template |
5003 | /// must be a class template, a type alias template, or a template |
5004 | /// template parameter. A template which cannot be resolved to one of |
5005 | /// these, e.g. because it is written with a dependent scope |
5006 | /// specifier, is instead represented as a |
5007 | /// @c DependentTemplateSpecializationType. |
5008 | /// |
5009 | /// A non-dependent template specialization type is always "sugar", |
5010 | /// typically for a \c RecordType. For example, a class template |
5011 | /// specialization type of \c vector<int> will refer to a tag type for |
5012 | /// the instantiation \c std::vector<int, std::allocator<int>> |
5013 | /// |
5014 | /// Template specializations are dependent if either the template or |
5015 | /// any of the template arguments are dependent, in which case the |
5016 | /// type may also be canonical. |
5017 | /// |
5018 | /// Instances of this type are allocated with a trailing array of |
5019 | /// TemplateArguments, followed by a QualType representing the |
5020 | /// non-canonical aliased type when the template is a type alias |
5021 | /// template. |
5022 | class alignas(8) TemplateSpecializationType |
5023 | : public Type, |
5024 | public llvm::FoldingSetNode { |
5025 | friend class ASTContext; // ASTContext creates these |
5026 | |
5027 | /// The name of the template being specialized. This is |
5028 | /// either a TemplateName::Template (in which case it is a |
5029 | /// ClassTemplateDecl*, a TemplateTemplateParmDecl*, or a |
5030 | /// TypeAliasTemplateDecl*), a |
5031 | /// TemplateName::SubstTemplateTemplateParmPack, or a |
5032 | /// TemplateName::SubstTemplateTemplateParm (in which case the |
5033 | /// replacement must, recursively, be one of these). |
5034 | TemplateName Template; |
5035 | |
5036 | TemplateSpecializationType(TemplateName T, |
5037 | ArrayRef<TemplateArgument> Args, |
5038 | QualType Canon, |
5039 | QualType Aliased); |
5040 | |
5041 | public: |
5042 | /// Determine whether any of the given template arguments are dependent. |
5043 | static bool anyDependentTemplateArguments(ArrayRef<TemplateArgumentLoc> Args, |
5044 | bool &InstantiationDependent); |
5045 | |
5046 | static bool anyDependentTemplateArguments(const TemplateArgumentListInfo &, |
5047 | bool &InstantiationDependent); |
5048 | |
5049 | /// True if this template specialization type matches a current |
5050 | /// instantiation in the context in which it is found. |
5051 | bool isCurrentInstantiation() const { |
5052 | return isa<InjectedClassNameType>(getCanonicalTypeInternal()); |
5053 | } |
5054 | |
5055 | /// Determine if this template specialization type is for a type alias |
5056 | /// template that has been substituted. |
5057 | /// |
5058 | /// Nearly every template specialization type whose template is an alias |
5059 | /// template will be substituted. However, this is not the case when |
5060 | /// the specialization contains a pack expansion but the template alias |
5061 | /// does not have a corresponding parameter pack, e.g., |
5062 | /// |
5063 | /// \code |
5064 | /// template<typename T, typename U, typename V> struct S; |
5065 | /// template<typename T, typename U> using A = S<T, int, U>; |
5066 | /// template<typename... Ts> struct X { |
5067 | /// typedef A<Ts...> type; // not a type alias |
5068 | /// }; |
5069 | /// \endcode |
5070 | bool isTypeAlias() const { return TemplateSpecializationTypeBits.TypeAlias; } |
5071 | |
5072 | /// Get the aliased type, if this is a specialization of a type alias |
5073 | /// template. |
5074 | QualType getAliasedType() const { |
5075 | assert(isTypeAlias() && "not a type alias template specialization")((isTypeAlias() && "not a type alias template specialization" ) ? static_cast<void> (0) : __assert_fail ("isTypeAlias() && \"not a type alias template specialization\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 5075, __PRETTY_FUNCTION__)); |
5076 | return *reinterpret_cast<const QualType*>(end()); |
5077 | } |
5078 | |
5079 | using iterator = const TemplateArgument *; |
5080 | |
5081 | iterator begin() const { return getArgs(); } |
5082 | iterator end() const; // defined inline in TemplateBase.h |
5083 | |
5084 | /// Retrieve the name of the template that we are specializing. |
5085 | TemplateName getTemplateName() const { return Template; } |
5086 | |
5087 | /// Retrieve the template arguments. |
5088 | const TemplateArgument *getArgs() const { |
5089 | return reinterpret_cast<const TemplateArgument *>(this + 1); |
5090 | } |
5091 | |
5092 | /// Retrieve the number of template arguments. |
5093 | unsigned getNumArgs() const { |
5094 | return TemplateSpecializationTypeBits.NumArgs; |
5095 | } |
5096 | |
5097 | /// Retrieve a specific template argument as a type. |
5098 | /// \pre \c isArgType(Arg) |
5099 | const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h |
5100 | |
5101 | ArrayRef<TemplateArgument> template_arguments() const { |
5102 | return {getArgs(), getNumArgs()}; |
5103 | } |
5104 | |
5105 | bool isSugared() const { |
5106 | return !isDependentType() || isCurrentInstantiation() || isTypeAlias(); |
5107 | } |
5108 | |
5109 | QualType desugar() const { |
5110 | return isTypeAlias() ? getAliasedType() : getCanonicalTypeInternal(); |
5111 | } |
5112 | |
5113 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) { |
5114 | Profile(ID, Template, template_arguments(), Ctx); |
5115 | if (isTypeAlias()) |
5116 | getAliasedType().Profile(ID); |
5117 | } |
5118 | |
5119 | static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T, |
5120 | ArrayRef<TemplateArgument> Args, |
5121 | const ASTContext &Context); |
5122 | |
5123 | static bool classof(const Type *T) { |
5124 | return T->getTypeClass() == TemplateSpecialization; |
5125 | } |
5126 | }; |
5127 | |
5128 | /// Print a template argument list, including the '<' and '>' |
5129 | /// enclosing the template arguments. |
5130 | void printTemplateArgumentList(raw_ostream &OS, |
5131 | ArrayRef<TemplateArgument> Args, |
5132 | const PrintingPolicy &Policy); |
5133 | |
5134 | void printTemplateArgumentList(raw_ostream &OS, |
5135 | ArrayRef<TemplateArgumentLoc> Args, |
5136 | const PrintingPolicy &Policy); |
5137 | |
5138 | void printTemplateArgumentList(raw_ostream &OS, |
5139 | const TemplateArgumentListInfo &Args, |
5140 | const PrintingPolicy &Policy); |
5141 | |
5142 | /// The injected class name of a C++ class template or class |
5143 | /// template partial specialization. Used to record that a type was |
5144 | /// spelled with a bare identifier rather than as a template-id; the |
5145 | /// equivalent for non-templated classes is just RecordType. |
5146 | /// |
5147 | /// Injected class name types are always dependent. Template |
5148 | /// instantiation turns these into RecordTypes. |
5149 | /// |
5150 | /// Injected class name types are always canonical. This works |
5151 | /// because it is impossible to compare an injected class name type |
5152 | /// with the corresponding non-injected template type, for the same |
5153 | /// reason that it is impossible to directly compare template |
5154 | /// parameters from different dependent contexts: injected class name |
5155 | /// types can only occur within the scope of a particular templated |
5156 | /// declaration, and within that scope every template specialization |
5157 | /// will canonicalize to the injected class name (when appropriate |
5158 | /// according to the rules of the language). |
5159 | class InjectedClassNameType : public Type { |
5160 | friend class ASTContext; // ASTContext creates these. |
5161 | friend class ASTNodeImporter; |
5162 | friend class ASTReader; // FIXME: ASTContext::getInjectedClassNameType is not |
5163 | // currently suitable for AST reading, too much |
5164 | // interdependencies. |
5165 | template <class T> friend class serialization::AbstractTypeReader; |
5166 | |
5167 | CXXRecordDecl *Decl; |
5168 | |
5169 | /// The template specialization which this type represents. |
5170 | /// For example, in |
5171 | /// template <class T> class A { ... }; |
5172 | /// this is A<T>, whereas in |
5173 | /// template <class X, class Y> class A<B<X,Y> > { ... }; |
5174 | /// this is A<B<X,Y> >. |
5175 | /// |
5176 | /// It is always unqualified, always a template specialization type, |
5177 | /// and always dependent. |
5178 | QualType InjectedType; |
5179 | |
5180 | InjectedClassNameType(CXXRecordDecl *D, QualType TST) |
5181 | : Type(InjectedClassName, QualType(), /*Dependent=*/true, |
5182 | /*InstantiationDependent=*/true, |
5183 | /*VariablyModified=*/false, |
5184 | /*ContainsUnexpandedParameterPack=*/false), |
5185 | Decl(D), InjectedType(TST) { |
5186 | assert(isa<TemplateSpecializationType>(TST))((isa<TemplateSpecializationType>(TST)) ? static_cast< void> (0) : __assert_fail ("isa<TemplateSpecializationType>(TST)" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 5186, __PRETTY_FUNCTION__)); |
5187 | assert(!TST.hasQualifiers())((!TST.hasQualifiers()) ? static_cast<void> (0) : __assert_fail ("!TST.hasQualifiers()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 5187, __PRETTY_FUNCTION__)); |
5188 | assert(TST->isDependentType())((TST->isDependentType()) ? static_cast<void> (0) : __assert_fail ("TST->isDependentType()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 5188, __PRETTY_FUNCTION__)); |
5189 | } |
5190 | |
5191 | public: |
5192 | QualType getInjectedSpecializationType() const { return InjectedType; } |
5193 | |
5194 | const TemplateSpecializationType *getInjectedTST() const { |
5195 | return cast<TemplateSpecializationType>(InjectedType.getTypePtr()); |
5196 | } |
5197 | |
5198 | TemplateName getTemplateName() const { |
5199 | return getInjectedTST()->getTemplateName(); |
5200 | } |
5201 | |
5202 | CXXRecordDecl *getDecl() const; |
5203 | |
5204 | bool isSugared() const { return false; } |
5205 | QualType desugar() const { return QualType(this, 0); } |
5206 | |
5207 | static bool classof(const Type *T) { |
5208 | return T->getTypeClass() == InjectedClassName; |
5209 | } |
5210 | }; |
5211 | |
5212 | /// The kind of a tag type. |
5213 | enum TagTypeKind { |
5214 | /// The "struct" keyword. |
5215 | TTK_Struct, |
5216 | |
5217 | /// The "__interface" keyword. |
5218 | TTK_Interface, |
5219 | |
5220 | /// The "union" keyword. |
5221 | TTK_Union, |
5222 | |
5223 | /// The "class" keyword. |
5224 | TTK_Class, |
5225 | |
5226 | /// The "enum" keyword. |
5227 | TTK_Enum |
5228 | }; |
5229 | |
5230 | /// The elaboration keyword that precedes a qualified type name or |
5231 | /// introduces an elaborated-type-specifier. |
5232 | enum ElaboratedTypeKeyword { |
5233 | /// The "struct" keyword introduces the elaborated-type-specifier. |
5234 | ETK_Struct, |
5235 | |
5236 | /// The "__interface" keyword introduces the elaborated-type-specifier. |
5237 | ETK_Interface, |
5238 | |
5239 | /// The "union" keyword introduces the elaborated-type-specifier. |
5240 | ETK_Union, |
5241 | |
5242 | /// The "class" keyword introduces the elaborated-type-specifier. |
5243 | ETK_Class, |
5244 | |
5245 | /// The "enum" keyword introduces the elaborated-type-specifier. |
5246 | ETK_Enum, |
5247 | |
5248 | /// The "typename" keyword precedes the qualified type name, e.g., |
5249 | /// \c typename T::type. |
5250 | ETK_Typename, |
5251 | |
5252 | /// No keyword precedes the qualified type name. |
5253 | ETK_None |
5254 | }; |
5255 | |
5256 | /// A helper class for Type nodes having an ElaboratedTypeKeyword. |
5257 | /// The keyword in stored in the free bits of the base class. |
5258 | /// Also provides a few static helpers for converting and printing |
5259 | /// elaborated type keyword and tag type kind enumerations. |
5260 | class TypeWithKeyword : public Type { |
5261 | protected: |
5262 | TypeWithKeyword(ElaboratedTypeKeyword Keyword, TypeClass tc, |
5263 | QualType Canonical, bool Dependent, |
5264 | bool InstantiationDependent, bool VariablyModified, |
5265 | bool ContainsUnexpandedParameterPack) |
5266 | : Type(tc, Canonical, Dependent, InstantiationDependent, VariablyModified, |
5267 | ContainsUnexpandedParameterPack) { |
5268 | TypeWithKeywordBits.Keyword = Keyword; |
5269 | } |
5270 | |
5271 | public: |
5272 | ElaboratedTypeKeyword getKeyword() const { |
5273 | return static_cast<ElaboratedTypeKeyword>(TypeWithKeywordBits.Keyword); |
5274 | } |
5275 | |
5276 | /// Converts a type specifier (DeclSpec::TST) into an elaborated type keyword. |
5277 | static ElaboratedTypeKeyword getKeywordForTypeSpec(unsigned TypeSpec); |
5278 | |
5279 | /// Converts a type specifier (DeclSpec::TST) into a tag type kind. |
5280 | /// It is an error to provide a type specifier which *isn't* a tag kind here. |
5281 | static TagTypeKind getTagTypeKindForTypeSpec(unsigned TypeSpec); |
5282 | |
5283 | /// Converts a TagTypeKind into an elaborated type keyword. |
5284 | static ElaboratedTypeKeyword getKeywordForTagTypeKind(TagTypeKind Tag); |
5285 | |
5286 | /// Converts an elaborated type keyword into a TagTypeKind. |
5287 | /// It is an error to provide an elaborated type keyword |
5288 | /// which *isn't* a tag kind here. |
5289 | static TagTypeKind getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword); |
5290 | |
5291 | static bool KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword); |
5292 | |
5293 | static StringRef getKeywordName(ElaboratedTypeKeyword Keyword); |
5294 | |
5295 | static StringRef getTagTypeKindName(TagTypeKind Kind) { |
5296 | return getKeywordName(getKeywordForTagTypeKind(Kind)); |
5297 | } |
5298 | |
5299 | class CannotCastToThisType {}; |
5300 | static CannotCastToThisType classof(const Type *); |
5301 | }; |
5302 | |
5303 | /// Represents a type that was referred to using an elaborated type |
5304 | /// keyword, e.g., struct S, or via a qualified name, e.g., N::M::type, |
5305 | /// or both. |
5306 | /// |
5307 | /// This type is used to keep track of a type name as written in the |
5308 | /// source code, including tag keywords and any nested-name-specifiers. |
5309 | /// The type itself is always "sugar", used to express what was written |
5310 | /// in the source code but containing no additional semantic information. |
5311 | class ElaboratedType final |
5312 | : public TypeWithKeyword, |
5313 | public llvm::FoldingSetNode, |
5314 | private llvm::TrailingObjects<ElaboratedType, TagDecl *> { |
5315 | friend class ASTContext; // ASTContext creates these |
5316 | friend TrailingObjects; |
5317 | |
5318 | /// The nested name specifier containing the qualifier. |
5319 | NestedNameSpecifier *NNS; |
5320 | |
5321 | /// The type that this qualified name refers to. |
5322 | QualType NamedType; |
5323 | |
5324 | /// The (re)declaration of this tag type owned by this occurrence is stored |
5325 | /// as a trailing object if there is one. Use getOwnedTagDecl to obtain |
5326 | /// it, or obtain a null pointer if there is none. |
5327 | |
5328 | ElaboratedType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, |
5329 | QualType NamedType, QualType CanonType, TagDecl *OwnedTagDecl) |
5330 | : TypeWithKeyword(Keyword, Elaborated, CanonType, |
5331 | NamedType->isDependentType(), |
5332 | NamedType->isInstantiationDependentType(), |
5333 | NamedType->isVariablyModifiedType(), |
5334 | NamedType->containsUnexpandedParameterPack()), |
5335 | NNS(NNS), NamedType(NamedType) { |
5336 | ElaboratedTypeBits.HasOwnedTagDecl = false; |
5337 | if (OwnedTagDecl) { |
5338 | ElaboratedTypeBits.HasOwnedTagDecl = true; |
5339 | *getTrailingObjects<TagDecl *>() = OwnedTagDecl; |
5340 | } |
5341 | assert(!(Keyword == ETK_None && NNS == nullptr) &&((!(Keyword == ETK_None && NNS == nullptr) && "ElaboratedType cannot have elaborated type keyword " "and name qualifier both null." ) ? static_cast<void> (0) : __assert_fail ("!(Keyword == ETK_None && NNS == nullptr) && \"ElaboratedType cannot have elaborated type keyword \" \"and name qualifier both null.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 5343, __PRETTY_FUNCTION__)) |
5342 | "ElaboratedType cannot have elaborated type keyword "((!(Keyword == ETK_None && NNS == nullptr) && "ElaboratedType cannot have elaborated type keyword " "and name qualifier both null." ) ? static_cast<void> (0) : __assert_fail ("!(Keyword == ETK_None && NNS == nullptr) && \"ElaboratedType cannot have elaborated type keyword \" \"and name qualifier both null.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 5343, __PRETTY_FUNCTION__)) |
5343 | "and name qualifier both null.")((!(Keyword == ETK_None && NNS == nullptr) && "ElaboratedType cannot have elaborated type keyword " "and name qualifier both null." ) ? static_cast<void> (0) : __assert_fail ("!(Keyword == ETK_None && NNS == nullptr) && \"ElaboratedType cannot have elaborated type keyword \" \"and name qualifier both null.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 5343, __PRETTY_FUNCTION__)); |
5344 | } |
5345 | |
5346 | public: |
5347 | /// Retrieve the qualification on this type. |
5348 | NestedNameSpecifier *getQualifier() const { return NNS; } |
5349 | |
5350 | /// Retrieve the type named by the qualified-id. |
5351 | QualType getNamedType() const { return NamedType; } |
5352 | |
5353 | /// Remove a single level of sugar. |
5354 | QualType desugar() const { return getNamedType(); } |
5355 | |
5356 | /// Returns whether this type directly provides sugar. |
5357 | bool isSugared() const { return true; } |
5358 | |
5359 | /// Return the (re)declaration of this type owned by this occurrence of this |
5360 | /// type, or nullptr if there is none. |
5361 | TagDecl *getOwnedTagDecl() const { |
5362 | return ElaboratedTypeBits.HasOwnedTagDecl ? *getTrailingObjects<TagDecl *>() |
5363 | : nullptr; |
5364 | } |
5365 | |
5366 | void Profile(llvm::FoldingSetNodeID &ID) { |
5367 | Profile(ID, getKeyword(), NNS, NamedType, getOwnedTagDecl()); |
5368 | } |
5369 | |
5370 | static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, |
5371 | NestedNameSpecifier *NNS, QualType NamedType, |
5372 | TagDecl *OwnedTagDecl) { |
5373 | ID.AddInteger(Keyword); |
5374 | ID.AddPointer(NNS); |
5375 | NamedType.Profile(ID); |
5376 | ID.AddPointer(OwnedTagDecl); |
5377 | } |
5378 | |
5379 | static bool classof(const Type *T) { return T->getTypeClass() == Elaborated; } |
5380 | }; |
5381 | |
5382 | /// Represents a qualified type name for which the type name is |
5383 | /// dependent. |
5384 | /// |
5385 | /// DependentNameType represents a class of dependent types that involve a |
5386 | /// possibly dependent nested-name-specifier (e.g., "T::") followed by a |
5387 | /// name of a type. The DependentNameType may start with a "typename" (for a |
5388 | /// typename-specifier), "class", "struct", "union", or "enum" (for a |
5389 | /// dependent elaborated-type-specifier), or nothing (in contexts where we |
5390 | /// know that we must be referring to a type, e.g., in a base class specifier). |
5391 | /// Typically the nested-name-specifier is dependent, but in MSVC compatibility |
5392 | /// mode, this type is used with non-dependent names to delay name lookup until |
5393 | /// instantiation. |
5394 | class DependentNameType : public TypeWithKeyword, public llvm::FoldingSetNode { |
5395 | friend class ASTContext; // ASTContext creates these |
5396 | |
5397 | /// The nested name specifier containing the qualifier. |
5398 | NestedNameSpecifier *NNS; |
5399 | |
5400 | /// The type that this typename specifier refers to. |
5401 | const IdentifierInfo *Name; |
5402 | |
5403 | DependentNameType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, |
5404 | const IdentifierInfo *Name, QualType CanonType) |
5405 | : TypeWithKeyword(Keyword, DependentName, CanonType, /*Dependent=*/true, |
5406 | /*InstantiationDependent=*/true, |
5407 | /*VariablyModified=*/false, |
5408 | NNS->containsUnexpandedParameterPack()), |
5409 | NNS(NNS), Name(Name) {} |
5410 | |
5411 | public: |
5412 | /// Retrieve the qualification on this type. |
5413 | NestedNameSpecifier *getQualifier() const { return NNS; } |
5414 | |
5415 | /// Retrieve the type named by the typename specifier as an identifier. |
5416 | /// |
5417 | /// This routine will return a non-NULL identifier pointer when the |
5418 | /// form of the original typename was terminated by an identifier, |
5419 | /// e.g., "typename T::type". |
5420 | const IdentifierInfo *getIdentifier() const { |
5421 | return Name; |
5422 | } |
5423 | |
5424 | bool isSugared() const { return false; } |
5425 | QualType desugar() const { return QualType(this, 0); } |
5426 | |
5427 | void Profile(llvm::FoldingSetNodeID &ID) { |
5428 | Profile(ID, getKeyword(), NNS, Name); |
5429 | } |
5430 | |
5431 | static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, |
5432 | NestedNameSpecifier *NNS, const IdentifierInfo *Name) { |
5433 | ID.AddInteger(Keyword); |
5434 | ID.AddPointer(NNS); |
5435 | ID.AddPointer(Name); |
5436 | } |
5437 | |
5438 | static bool classof(const Type *T) { |
5439 | return T->getTypeClass() == DependentName; |
5440 | } |
5441 | }; |
5442 | |
5443 | /// Represents a template specialization type whose template cannot be |
5444 | /// resolved, e.g. |
5445 | /// A<T>::template B<T> |
5446 | class alignas(8) DependentTemplateSpecializationType |
5447 | : public TypeWithKeyword, |
5448 | public llvm::FoldingSetNode { |
5449 | friend class ASTContext; // ASTContext creates these |
5450 | |
5451 | /// The nested name specifier containing the qualifier. |
5452 | NestedNameSpecifier *NNS; |
5453 | |
5454 | /// The identifier of the template. |
5455 | const IdentifierInfo *Name; |
5456 | |
5457 | DependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, |
5458 | NestedNameSpecifier *NNS, |
5459 | const IdentifierInfo *Name, |
5460 | ArrayRef<TemplateArgument> Args, |
5461 | QualType Canon); |
5462 | |
5463 | const TemplateArgument *getArgBuffer() const { |
5464 | return reinterpret_cast<const TemplateArgument*>(this+1); |
5465 | } |
5466 | |
5467 | TemplateArgument *getArgBuffer() { |
5468 | return reinterpret_cast<TemplateArgument*>(this+1); |
5469 | } |
5470 | |
5471 | public: |
5472 | NestedNameSpecifier *getQualifier() const { return NNS; } |
5473 | const IdentifierInfo *getIdentifier() const { return Name; } |
5474 | |
5475 | /// Retrieve the template arguments. |
5476 | const TemplateArgument *getArgs() const { |
5477 | return getArgBuffer(); |
5478 | } |
5479 | |
5480 | /// Retrieve the number of template arguments. |
5481 | unsigned getNumArgs() const { |
5482 | return DependentTemplateSpecializationTypeBits.NumArgs; |
5483 | } |
5484 | |
5485 | const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h |
5486 | |
5487 | ArrayRef<TemplateArgument> template_arguments() const { |
5488 | return {getArgs(), getNumArgs()}; |
5489 | } |
5490 | |
5491 | using iterator = const TemplateArgument *; |
5492 | |
5493 | iterator begin() const { return getArgs(); } |
5494 | iterator end() const; // inline in TemplateBase.h |
5495 | |
5496 | bool isSugared() const { return false; } |
5497 | QualType desugar() const { return QualType(this, 0); } |
5498 | |
5499 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { |
5500 | Profile(ID, Context, getKeyword(), NNS, Name, {getArgs(), getNumArgs()}); |
5501 | } |
5502 | |
5503 | static void Profile(llvm::FoldingSetNodeID &ID, |
5504 | const ASTContext &Context, |
5505 | ElaboratedTypeKeyword Keyword, |
5506 | NestedNameSpecifier *Qualifier, |
5507 | const IdentifierInfo *Name, |
5508 | ArrayRef<TemplateArgument> Args); |
5509 | |
5510 | static bool classof(const Type *T) { |
5511 | return T->getTypeClass() == DependentTemplateSpecialization; |
5512 | } |
5513 | }; |
5514 | |
5515 | /// Represents a pack expansion of types. |
5516 | /// |
5517 | /// Pack expansions are part of C++11 variadic templates. A pack |
5518 | /// expansion contains a pattern, which itself contains one or more |
5519 | /// "unexpanded" parameter packs. When instantiated, a pack expansion |
5520 | /// produces a series of types, each instantiated from the pattern of |
5521 | /// the expansion, where the Ith instantiation of the pattern uses the |
5522 | /// Ith arguments bound to each of the unexpanded parameter packs. The |
5523 | /// pack expansion is considered to "expand" these unexpanded |
5524 | /// parameter packs. |
5525 | /// |
5526 | /// \code |
5527 | /// template<typename ...Types> struct tuple; |
5528 | /// |
5529 | /// template<typename ...Types> |
5530 | /// struct tuple_of_references { |
5531 | /// typedef tuple<Types&...> type; |
5532 | /// }; |
5533 | /// \endcode |
5534 | /// |
5535 | /// Here, the pack expansion \c Types&... is represented via a |
5536 | /// PackExpansionType whose pattern is Types&. |
5537 | class PackExpansionType : public Type, public llvm::FoldingSetNode { |
5538 | friend class ASTContext; // ASTContext creates these |
5539 | |
5540 | /// The pattern of the pack expansion. |
5541 | QualType Pattern; |
5542 | |
5543 | PackExpansionType(QualType Pattern, QualType Canon, |
5544 | Optional<unsigned> NumExpansions) |
5545 | : Type(PackExpansion, Canon, /*Dependent=*/Pattern->isDependentType(), |
5546 | /*InstantiationDependent=*/true, |
5547 | /*VariablyModified=*/Pattern->isVariablyModifiedType(), |
5548 | /*ContainsUnexpandedParameterPack=*/false), |
5549 | Pattern(Pattern) { |
5550 | PackExpansionTypeBits.NumExpansions = |
5551 | NumExpansions ? *NumExpansions + 1 : 0; |
5552 | } |
5553 | |
5554 | public: |
5555 | /// Retrieve the pattern of this pack expansion, which is the |
5556 | /// type that will be repeatedly instantiated when instantiating the |
5557 | /// pack expansion itself. |
5558 | QualType getPattern() const { return Pattern; } |
5559 | |
5560 | /// Retrieve the number of expansions that this pack expansion will |
5561 | /// generate, if known. |
5562 | Optional<unsigned> getNumExpansions() const { |
5563 | if (PackExpansionTypeBits.NumExpansions) |
5564 | return PackExpansionTypeBits.NumExpansions - 1; |
5565 | return None; |
5566 | } |
5567 | |
5568 | bool isSugared() const { return !Pattern->isDependentType(); } |
5569 | QualType desugar() const { return isSugared() ? Pattern : QualType(this, 0); } |
5570 | |
5571 | void Profile(llvm::FoldingSetNodeID &ID) { |
5572 | Profile(ID, getPattern(), getNumExpansions()); |
5573 | } |
5574 | |
5575 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pattern, |
5576 | Optional<unsigned> NumExpansions) { |
5577 | ID.AddPointer(Pattern.getAsOpaquePtr()); |
5578 | ID.AddBoolean(NumExpansions.hasValue()); |
5579 | if (NumExpansions) |
5580 | ID.AddInteger(*NumExpansions); |
5581 | } |
5582 | |
5583 | static bool classof(const Type *T) { |
5584 | return T->getTypeClass() == PackExpansion; |
5585 | } |
5586 | }; |
5587 | |
5588 | /// This class wraps the list of protocol qualifiers. For types that can |
5589 | /// take ObjC protocol qualifers, they can subclass this class. |
5590 | template <class T> |
5591 | class ObjCProtocolQualifiers { |
5592 | protected: |
5593 | ObjCProtocolQualifiers() = default; |
5594 | |
5595 | ObjCProtocolDecl * const *getProtocolStorage() const { |
5596 | return const_cast<ObjCProtocolQualifiers*>(this)->getProtocolStorage(); |
5597 | } |
5598 | |
5599 | ObjCProtocolDecl **getProtocolStorage() { |
5600 | return static_cast<T*>(this)->getProtocolStorageImpl(); |
5601 | } |
5602 | |
5603 | void setNumProtocols(unsigned N) { |
5604 | static_cast<T*>(this)->setNumProtocolsImpl(N); |
5605 | } |
5606 | |
5607 | void initialize(ArrayRef<ObjCProtocolDecl *> protocols) { |
5608 | setNumProtocols(protocols.size()); |
5609 | assert(getNumProtocols() == protocols.size() &&((getNumProtocols() == protocols.size() && "bitfield overflow in protocol count" ) ? static_cast<void> (0) : __assert_fail ("getNumProtocols() == protocols.size() && \"bitfield overflow in protocol count\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 5610, __PRETTY_FUNCTION__)) |
5610 | "bitfield overflow in protocol count")((getNumProtocols() == protocols.size() && "bitfield overflow in protocol count" ) ? static_cast<void> (0) : __assert_fail ("getNumProtocols() == protocols.size() && \"bitfield overflow in protocol count\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 5610, __PRETTY_FUNCTION__)); |
5611 | if (!protocols.empty()) |
5612 | memcpy(getProtocolStorage(), protocols.data(), |
5613 | protocols.size() * sizeof(ObjCProtocolDecl*)); |
5614 | } |
5615 | |
5616 | public: |
5617 | using qual_iterator = ObjCProtocolDecl * const *; |
5618 | using qual_range = llvm::iterator_range<qual_iterator>; |
5619 | |
5620 | qual_range quals() const { return qual_range(qual_begin(), qual_end()); } |
5621 | qual_iterator qual_begin() const { return getProtocolStorage(); } |
5622 | qual_iterator qual_end() const { return qual_begin() + getNumProtocols(); } |
5623 | |
5624 | bool qual_empty() const { return getNumProtocols() == 0; } |
5625 | |
5626 | /// Return the number of qualifying protocols in this type, or 0 if |
5627 | /// there are none. |
5628 | unsigned getNumProtocols() const { |
5629 | return static_cast<const T*>(this)->getNumProtocolsImpl(); |
5630 | } |
5631 | |
5632 | /// Fetch a protocol by index. |
5633 | ObjCProtocolDecl *getProtocol(unsigned I) const { |
5634 | assert(I < getNumProtocols() && "Out-of-range protocol access")((I < getNumProtocols() && "Out-of-range protocol access" ) ? static_cast<void> (0) : __assert_fail ("I < getNumProtocols() && \"Out-of-range protocol access\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 5634, __PRETTY_FUNCTION__)); |
5635 | return qual_begin()[I]; |
5636 | } |
5637 | |
5638 | /// Retrieve all of the protocol qualifiers. |
5639 | ArrayRef<ObjCProtocolDecl *> getProtocols() const { |
5640 | return ArrayRef<ObjCProtocolDecl *>(qual_begin(), getNumProtocols()); |
5641 | } |
5642 | }; |
5643 | |
5644 | /// Represents a type parameter type in Objective C. It can take |
5645 | /// a list of protocols. |
5646 | class ObjCTypeParamType : public Type, |
5647 | public ObjCProtocolQualifiers<ObjCTypeParamType>, |
5648 | public llvm::FoldingSetNode { |
5649 | friend class ASTContext; |
5650 | friend class ObjCProtocolQualifiers<ObjCTypeParamType>; |
5651 | |
5652 | /// The number of protocols stored on this type. |
5653 | unsigned NumProtocols : 6; |
5654 | |
5655 | ObjCTypeParamDecl *OTPDecl; |
5656 | |
5657 | /// The protocols are stored after the ObjCTypeParamType node. In the |
5658 | /// canonical type, the list of protocols are sorted alphabetically |
5659 | /// and uniqued. |
5660 | ObjCProtocolDecl **getProtocolStorageImpl(); |
5661 | |
5662 | /// Return the number of qualifying protocols in this interface type, |
5663 | /// or 0 if there are none. |
5664 | unsigned getNumProtocolsImpl() const { |
5665 | return NumProtocols; |
5666 | } |
5667 | |
5668 | void setNumProtocolsImpl(unsigned N) { |
5669 | NumProtocols = N; |
5670 | } |
5671 | |
5672 | ObjCTypeParamType(const ObjCTypeParamDecl *D, |
5673 | QualType can, |
5674 | ArrayRef<ObjCProtocolDecl *> protocols); |
5675 | |
5676 | public: |
5677 | bool isSugared() const { return true; } |
5678 | QualType desugar() const { return getCanonicalTypeInternal(); } |
5679 | |
5680 | static bool classof(const Type *T) { |
5681 | return T->getTypeClass() == ObjCTypeParam; |
5682 | } |
5683 | |
5684 | void Profile(llvm::FoldingSetNodeID &ID); |
5685 | static void Profile(llvm::FoldingSetNodeID &ID, |
5686 | const ObjCTypeParamDecl *OTPDecl, |
5687 | ArrayRef<ObjCProtocolDecl *> protocols); |
5688 | |
5689 | ObjCTypeParamDecl *getDecl() const { return OTPDecl; } |
5690 | }; |
5691 | |
5692 | /// Represents a class type in Objective C. |
5693 | /// |
5694 | /// Every Objective C type is a combination of a base type, a set of |
5695 | /// type arguments (optional, for parameterized classes) and a list of |
5696 | /// protocols. |
5697 | /// |
5698 | /// Given the following declarations: |
5699 | /// \code |
5700 | /// \@class C<T>; |
5701 | /// \@protocol P; |
5702 | /// \endcode |
5703 | /// |
5704 | /// 'C' is an ObjCInterfaceType C. It is sugar for an ObjCObjectType |
5705 | /// with base C and no protocols. |
5706 | /// |
5707 | /// 'C<P>' is an unspecialized ObjCObjectType with base C and protocol list [P]. |
5708 | /// 'C<C*>' is a specialized ObjCObjectType with type arguments 'C*' and no |
5709 | /// protocol list. |
5710 | /// 'C<C*><P>' is a specialized ObjCObjectType with base C, type arguments 'C*', |
5711 | /// and protocol list [P]. |
5712 | /// |
5713 | /// 'id' is a TypedefType which is sugar for an ObjCObjectPointerType whose |
5714 | /// pointee is an ObjCObjectType with base BuiltinType::ObjCIdType |
5715 | /// and no protocols. |
5716 | /// |
5717 | /// 'id<P>' is an ObjCObjectPointerType whose pointee is an ObjCObjectType |
5718 | /// with base BuiltinType::ObjCIdType and protocol list [P]. Eventually |
5719 | /// this should get its own sugar class to better represent the source. |
5720 | class ObjCObjectType : public Type, |
5721 | public ObjCProtocolQualifiers<ObjCObjectType> { |
5722 | friend class ObjCProtocolQualifiers<ObjCObjectType>; |
5723 | |
5724 | // ObjCObjectType.NumTypeArgs - the number of type arguments stored |
5725 | // after the ObjCObjectPointerType node. |
5726 | // ObjCObjectType.NumProtocols - the number of protocols stored |
5727 | // after the type arguments of ObjCObjectPointerType node. |
5728 | // |
5729 | // These protocols are those written directly on the type. If |
5730 | // protocol qualifiers ever become additive, the iterators will need |
5731 | // to get kindof complicated. |
5732 | // |
5733 | // In the canonical object type, these are sorted alphabetically |
5734 | // and uniqued. |
5735 | |
5736 | /// Either a BuiltinType or an InterfaceType or sugar for either. |
5737 | QualType BaseType; |
5738 | |
5739 | /// Cached superclass type. |
5740 | mutable llvm::PointerIntPair<const ObjCObjectType *, 1, bool> |
5741 | CachedSuperClassType; |
5742 | |
5743 | QualType *getTypeArgStorage(); |
5744 | const QualType *getTypeArgStorage() const { |
5745 | return const_cast<ObjCObjectType *>(this)->getTypeArgStorage(); |
5746 | } |
5747 | |
5748 | ObjCProtocolDecl **getProtocolStorageImpl(); |
5749 | /// Return the number of qualifying protocols in this interface type, |
5750 | /// or 0 if there are none. |
5751 | unsigned getNumProtocolsImpl() const { |
5752 | return ObjCObjectTypeBits.NumProtocols; |
5753 | } |
5754 | void setNumProtocolsImpl(unsigned N) { |
5755 | ObjCObjectTypeBits.NumProtocols = N; |
5756 | } |
5757 | |
5758 | protected: |
5759 | enum Nonce_ObjCInterface { Nonce_ObjCInterface }; |
5760 | |
5761 | ObjCObjectType(QualType Canonical, QualType Base, |
5762 | ArrayRef<QualType> typeArgs, |
5763 | ArrayRef<ObjCProtocolDecl *> protocols, |
5764 | bool isKindOf); |
5765 | |
5766 | ObjCObjectType(enum Nonce_ObjCInterface) |
5767 | : Type(ObjCInterface, QualType(), false, false, false, false), |
5768 | BaseType(QualType(this_(), 0)) { |
5769 | ObjCObjectTypeBits.NumProtocols = 0; |
5770 | ObjCObjectTypeBits.NumTypeArgs = 0; |
5771 | ObjCObjectTypeBits.IsKindOf = 0; |
5772 | } |
5773 | |
5774 | void computeSuperClassTypeSlow() const; |
5775 | |
5776 | public: |
5777 | /// Gets the base type of this object type. This is always (possibly |
5778 | /// sugar for) one of: |
5779 | /// - the 'id' builtin type (as opposed to the 'id' type visible to the |
5780 | /// user, which is a typedef for an ObjCObjectPointerType) |
5781 | /// - the 'Class' builtin type (same caveat) |
5782 | /// - an ObjCObjectType (currently always an ObjCInterfaceType) |
5783 | QualType getBaseType() const { return BaseType; } |
5784 | |
5785 | bool isObjCId() const { |
5786 | return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCId); |
5787 | } |
5788 | |
5789 | bool isObjCClass() const { |
5790 | return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCClass); |
5791 | } |
5792 | |
5793 | bool isObjCUnqualifiedId() const { return qual_empty() && isObjCId(); } |
5794 | bool isObjCUnqualifiedClass() const { return qual_empty() && isObjCClass(); } |
5795 | bool isObjCUnqualifiedIdOrClass() const { |
5796 | if (!qual_empty()) return false; |
5797 | if (const BuiltinType *T = getBaseType()->getAs<BuiltinType>()) |
5798 | return T->getKind() == BuiltinType::ObjCId || |
5799 | T->getKind() == BuiltinType::ObjCClass; |
5800 | return false; |
5801 | } |
5802 | bool isObjCQualifiedId() const { return !qual_empty() && isObjCId(); } |
5803 | bool isObjCQualifiedClass() const { return !qual_empty() && isObjCClass(); } |
5804 | |
5805 | /// Gets the interface declaration for this object type, if the base type |
5806 | /// really is an interface. |
5807 | ObjCInterfaceDecl *getInterface() const; |
5808 | |
5809 | /// Determine whether this object type is "specialized", meaning |
5810 | /// that it has type arguments. |
5811 | bool isSpecialized() const; |
5812 | |
5813 | /// Determine whether this object type was written with type arguments. |
5814 | bool isSpecializedAsWritten() const { |
5815 | return ObjCObjectTypeBits.NumTypeArgs > 0; |
5816 | } |
5817 | |
5818 | /// Determine whether this object type is "unspecialized", meaning |
5819 | /// that it has no type arguments. |
5820 | bool isUnspecialized() const { return !isSpecialized(); } |
5821 | |
5822 | /// Determine whether this object type is "unspecialized" as |
5823 | /// written, meaning that it has no type arguments. |
5824 | bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); } |
5825 | |
5826 | /// Retrieve the type arguments of this object type (semantically). |
5827 | ArrayRef<QualType> getTypeArgs() const; |
5828 | |
5829 | /// Retrieve the type arguments of this object type as they were |
5830 | /// written. |
5831 | ArrayRef<QualType> getTypeArgsAsWritten() const { |
5832 | return llvm::makeArrayRef(getTypeArgStorage(), |
5833 | ObjCObjectTypeBits.NumTypeArgs); |
5834 | } |
5835 | |
5836 | /// Whether this is a "__kindof" type as written. |
5837 | bool isKindOfTypeAsWritten() const { return ObjCObjectTypeBits.IsKindOf; } |
5838 | |
5839 | /// Whether this ia a "__kindof" type (semantically). |
5840 | bool isKindOfType() const; |
5841 | |
5842 | /// Retrieve the type of the superclass of this object type. |
5843 | /// |
5844 | /// This operation substitutes any type arguments into the |
5845 | /// superclass of the current class type, potentially producing a |
5846 | /// specialization of the superclass type. Produces a null type if |
5847 | /// there is no superclass. |
5848 | QualType getSuperClassType() const { |
5849 | if (!CachedSuperClassType.getInt()) |
5850 | computeSuperClassTypeSlow(); |
5851 | |
5852 | assert(CachedSuperClassType.getInt() && "Superclass not set?")((CachedSuperClassType.getInt() && "Superclass not set?" ) ? static_cast<void> (0) : __assert_fail ("CachedSuperClassType.getInt() && \"Superclass not set?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 5852, __PRETTY_FUNCTION__)); |
5853 | return QualType(CachedSuperClassType.getPointer(), 0); |
5854 | } |
5855 | |
5856 | /// Strip off the Objective-C "kindof" type and (with it) any |
5857 | /// protocol qualifiers. |
5858 | QualType stripObjCKindOfTypeAndQuals(const ASTContext &ctx) const; |
5859 | |
5860 | bool isSugared() const { return false; } |
5861 | QualType desugar() const { return QualType(this, 0); } |
5862 | |
5863 | static bool classof(const Type *T) { |
5864 | return T->getTypeClass() == ObjCObject || |
5865 | T->getTypeClass() == ObjCInterface; |
5866 | } |
5867 | }; |
5868 | |
5869 | /// A class providing a concrete implementation |
5870 | /// of ObjCObjectType, so as to not increase the footprint of |
5871 | /// ObjCInterfaceType. Code outside of ASTContext and the core type |
5872 | /// system should not reference this type. |
5873 | class ObjCObjectTypeImpl : public ObjCObjectType, public llvm::FoldingSetNode { |
5874 | friend class ASTContext; |
5875 | |
5876 | // If anyone adds fields here, ObjCObjectType::getProtocolStorage() |
5877 | // will need to be modified. |
5878 | |
5879 | ObjCObjectTypeImpl(QualType Canonical, QualType Base, |
5880 | ArrayRef<QualType> typeArgs, |
5881 | ArrayRef<ObjCProtocolDecl *> protocols, |
5882 | bool isKindOf) |
5883 | : ObjCObjectType(Canonical, Base, typeArgs, protocols, isKindOf) {} |
5884 | |
5885 | public: |
5886 | void Profile(llvm::FoldingSetNodeID &ID); |
5887 | static void Profile(llvm::FoldingSetNodeID &ID, |
5888 | QualType Base, |
5889 | ArrayRef<QualType> typeArgs, |
5890 | ArrayRef<ObjCProtocolDecl *> protocols, |
5891 | bool isKindOf); |
5892 | }; |
5893 | |
5894 | inline QualType *ObjCObjectType::getTypeArgStorage() { |
5895 | return reinterpret_cast<QualType *>(static_cast<ObjCObjectTypeImpl*>(this)+1); |
5896 | } |
5897 | |
5898 | inline ObjCProtocolDecl **ObjCObjectType::getProtocolStorageImpl() { |
5899 | return reinterpret_cast<ObjCProtocolDecl**>( |
5900 | getTypeArgStorage() + ObjCObjectTypeBits.NumTypeArgs); |
5901 | } |
5902 | |
5903 | inline ObjCProtocolDecl **ObjCTypeParamType::getProtocolStorageImpl() { |
5904 | return reinterpret_cast<ObjCProtocolDecl**>( |
5905 | static_cast<ObjCTypeParamType*>(this)+1); |
5906 | } |
5907 | |
5908 | /// Interfaces are the core concept in Objective-C for object oriented design. |
5909 | /// They basically correspond to C++ classes. There are two kinds of interface |
5910 | /// types: normal interfaces like `NSString`, and qualified interfaces, which |
5911 | /// are qualified with a protocol list like `NSString<NSCopyable, NSAmazing>`. |
5912 | /// |
5913 | /// ObjCInterfaceType guarantees the following properties when considered |
5914 | /// as a subtype of its superclass, ObjCObjectType: |
5915 | /// - There are no protocol qualifiers. To reinforce this, code which |
5916 | /// tries to invoke the protocol methods via an ObjCInterfaceType will |
5917 | /// fail to compile. |
5918 | /// - It is its own base type. That is, if T is an ObjCInterfaceType*, |
5919 | /// T->getBaseType() == QualType(T, 0). |
5920 | class ObjCInterfaceType : public ObjCObjectType { |
5921 | friend class ASTContext; // ASTContext creates these. |
5922 | friend class ASTReader; |
5923 | friend class ObjCInterfaceDecl; |
5924 | template <class T> friend class serialization::AbstractTypeReader; |
5925 | |
5926 | mutable ObjCInterfaceDecl *Decl; |
5927 | |
5928 | ObjCInterfaceType(const ObjCInterfaceDecl *D) |
5929 | : ObjCObjectType(Nonce_ObjCInterface), |
5930 | Decl(const_cast<ObjCInterfaceDecl*>(D)) {} |
5931 | |
5932 | public: |
5933 | /// Get the declaration of this interface. |
5934 | ObjCInterfaceDecl *getDecl() const { return Decl; } |
5935 | |
5936 | bool isSugared() const { return false; } |
5937 | QualType desugar() const { return QualType(this, 0); } |
5938 | |
5939 | static bool classof(const Type *T) { |
5940 | return T->getTypeClass() == ObjCInterface; |
5941 | } |
5942 | |
5943 | // Nonsense to "hide" certain members of ObjCObjectType within this |
5944 | // class. People asking for protocols on an ObjCInterfaceType are |
5945 | // not going to get what they want: ObjCInterfaceTypes are |
5946 | // guaranteed to have no protocols. |
5947 | enum { |
5948 | qual_iterator, |
5949 | qual_begin, |
5950 | qual_end, |
5951 | getNumProtocols, |
5952 | getProtocol |
5953 | }; |
5954 | }; |
5955 | |
5956 | inline ObjCInterfaceDecl *ObjCObjectType::getInterface() const { |
5957 | QualType baseType = getBaseType(); |
5958 | while (const auto *ObjT = baseType->getAs<ObjCObjectType>()) { |
5959 | if (const auto *T = dyn_cast<ObjCInterfaceType>(ObjT)) |
5960 | return T->getDecl(); |
5961 | |
5962 | baseType = ObjT->getBaseType(); |
5963 | } |
5964 | |
5965 | return nullptr; |
5966 | } |
5967 | |
5968 | /// Represents a pointer to an Objective C object. |
5969 | /// |
5970 | /// These are constructed from pointer declarators when the pointee type is |
5971 | /// an ObjCObjectType (or sugar for one). In addition, the 'id' and 'Class' |
5972 | /// types are typedefs for these, and the protocol-qualified types 'id<P>' |
5973 | /// and 'Class<P>' are translated into these. |
5974 | /// |
5975 | /// Pointers to pointers to Objective C objects are still PointerTypes; |
5976 | /// only the first level of pointer gets it own type implementation. |
5977 | class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode { |
5978 | friend class ASTContext; // ASTContext creates these. |
5979 | |
5980 | QualType PointeeType; |
5981 | |
5982 | ObjCObjectPointerType(QualType Canonical, QualType Pointee) |
5983 | : Type(ObjCObjectPointer, Canonical, |
5984 | Pointee->isDependentType(), |
5985 | Pointee->isInstantiationDependentType(), |
5986 | Pointee->isVariablyModifiedType(), |
5987 | Pointee->containsUnexpandedParameterPack()), |
5988 | PointeeType(Pointee) {} |
5989 | |
5990 | public: |
5991 | /// Gets the type pointed to by this ObjC pointer. |
5992 | /// The result will always be an ObjCObjectType or sugar thereof. |
5993 | QualType getPointeeType() const { return PointeeType; } |
5994 | |
5995 | /// Gets the type pointed to by this ObjC pointer. Always returns non-null. |
5996 | /// |
5997 | /// This method is equivalent to getPointeeType() except that |
5998 | /// it discards any typedefs (or other sugar) between this |
5999 | /// type and the "outermost" object type. So for: |
6000 | /// \code |
6001 | /// \@class A; \@protocol P; \@protocol Q; |
6002 | /// typedef A<P> AP; |
6003 | /// typedef A A1; |
6004 | /// typedef A1<P> A1P; |
6005 | /// typedef A1P<Q> A1PQ; |
6006 | /// \endcode |
6007 | /// For 'A*', getObjectType() will return 'A'. |
6008 | /// For 'A<P>*', getObjectType() will return 'A<P>'. |
6009 | /// For 'AP*', getObjectType() will return 'A<P>'. |
6010 | /// For 'A1*', getObjectType() will return 'A'. |
6011 | /// For 'A1<P>*', getObjectType() will return 'A1<P>'. |
6012 | /// For 'A1P*', getObjectType() will return 'A1<P>'. |
6013 | /// For 'A1PQ*', getObjectType() will return 'A1<Q>', because |
6014 | /// adding protocols to a protocol-qualified base discards the |
6015 | /// old qualifiers (for now). But if it didn't, getObjectType() |
6016 | /// would return 'A1P<Q>' (and we'd have to make iterating over |
6017 | /// qualifiers more complicated). |
6018 | const ObjCObjectType *getObjectType() const { |
6019 | return PointeeType->castAs<ObjCObjectType>(); |
6020 | } |
6021 | |
6022 | /// If this pointer points to an Objective C |
6023 | /// \@interface type, gets the type for that interface. Any protocol |
6024 | /// qualifiers on the interface are ignored. |
6025 | /// |
6026 | /// \return null if the base type for this pointer is 'id' or 'Class' |
6027 | const ObjCInterfaceType *getInterfaceType() const; |
6028 | |
6029 | /// If this pointer points to an Objective \@interface |
6030 | /// type, gets the declaration for that interface. |
6031 | /// |
6032 | /// \return null if the base type for this pointer is 'id' or 'Class' |
6033 | ObjCInterfaceDecl *getInterfaceDecl() const { |
6034 | return getObjectType()->getInterface(); |
6035 | } |
6036 | |
6037 | /// True if this is equivalent to the 'id' type, i.e. if |
6038 | /// its object type is the primitive 'id' type with no protocols. |
6039 | bool isObjCIdType() const { |
6040 | return getObjectType()->isObjCUnqualifiedId(); |
6041 | } |
6042 | |
6043 | /// True if this is equivalent to the 'Class' type, |
6044 | /// i.e. if its object tive is the primitive 'Class' type with no protocols. |
6045 | bool isObjCClassType() const { |
6046 | return getObjectType()->isObjCUnqualifiedClass(); |
6047 | } |
6048 | |
6049 | /// True if this is equivalent to the 'id' or 'Class' type, |
6050 | bool isObjCIdOrClassType() const { |
6051 | return getObjectType()->isObjCUnqualifiedIdOrClass(); |
6052 | } |
6053 | |
6054 | /// True if this is equivalent to 'id<P>' for some non-empty set of |
6055 | /// protocols. |
6056 | bool isObjCQualifiedIdType() const { |
6057 | return getObjectType()->isObjCQualifiedId(); |
6058 | } |
6059 | |
6060 | /// True if this is equivalent to 'Class<P>' for some non-empty set of |
6061 | /// protocols. |
6062 | bool isObjCQualifiedClassType() const { |
6063 | return getObjectType()->isObjCQualifiedClass(); |
6064 | } |
6065 | |
6066 | /// Whether this is a "__kindof" type. |
6067 | bool isKindOfType() const { return getObjectType()->isKindOfType(); } |
6068 | |
6069 | /// Whether this type is specialized, meaning that it has type arguments. |
6070 | bool isSpecialized() const { return getObjectType()->isSpecialized(); } |
6071 | |
6072 | /// Whether this type is specialized, meaning that it has type arguments. |
6073 | bool isSpecializedAsWritten() const { |
6074 | return getObjectType()->isSpecializedAsWritten(); |
6075 | } |
6076 | |
6077 | /// Whether this type is unspecialized, meaning that is has no type arguments. |
6078 | bool isUnspecialized() const { return getObjectType()->isUnspecialized(); } |
6079 | |
6080 | /// Determine whether this object type is "unspecialized" as |
6081 | /// written, meaning that it has no type arguments. |
6082 | bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); } |
6083 | |
6084 | /// Retrieve the type arguments for this type. |
6085 | ArrayRef<QualType> getTypeArgs() const { |
6086 | return getObjectType()->getTypeArgs(); |
6087 | } |
6088 | |
6089 | /// Retrieve the type arguments for this type. |
6090 | ArrayRef<QualType> getTypeArgsAsWritten() const { |
6091 | return getObjectType()->getTypeArgsAsWritten(); |
6092 | } |
6093 | |
6094 | /// An iterator over the qualifiers on the object type. Provided |
6095 | /// for convenience. This will always iterate over the full set of |
6096 | /// protocols on a type, not just those provided directly. |
6097 | using qual_iterator = ObjCObjectType::qual_iterator; |
6098 | using qual_range = llvm::iterator_range<qual_iterator>; |
6099 | |
6100 | qual_range quals() const { return qual_range(qual_begin(), qual_end()); } |
6101 | |
6102 | qual_iterator qual_begin() const { |
6103 | return getObjectType()->qual_begin(); |
6104 | } |
6105 | |
6106 | qual_iterator qual_end() const { |
6107 | return getObjectType()->qual_end(); |
6108 | } |
6109 | |
6110 | bool qual_empty() const { return getObjectType()->qual_empty(); } |
6111 | |
6112 | /// Return the number of qualifying protocols on the object type. |
6113 | unsigned getNumProtocols() const { |
6114 | return getObjectType()->getNumProtocols(); |
6115 | } |
6116 | |
6117 | /// Retrieve a qualifying protocol by index on the object type. |
6118 | ObjCProtocolDecl *getProtocol(unsigned I) const { |
6119 | return getObjectType()->getProtocol(I); |
6120 | } |
6121 | |
6122 | bool isSugared() const { return false; } |
6123 | QualType desugar() const { return QualType(this, 0); } |
6124 | |
6125 | /// Retrieve the type of the superclass of this object pointer type. |
6126 | /// |
6127 | /// This operation substitutes any type arguments into the |
6128 | /// superclass of the current class type, potentially producing a |
6129 | /// pointer to a specialization of the superclass type. Produces a |
6130 | /// null type if there is no superclass. |
6131 | QualType getSuperClassType() const; |
6132 | |
6133 | /// Strip off the Objective-C "kindof" type and (with it) any |
6134 | /// protocol qualifiers. |
6135 | const ObjCObjectPointerType *stripObjCKindOfTypeAndQuals( |
6136 | const ASTContext &ctx) const; |
6137 | |
6138 | void Profile(llvm::FoldingSetNodeID &ID) { |
6139 | Profile(ID, getPointeeType()); |
6140 | } |
6141 | |
6142 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { |
6143 | ID.AddPointer(T.getAsOpaquePtr()); |
6144 | } |
6145 | |
6146 | static bool classof(const Type *T) { |
6147 | return T->getTypeClass() == ObjCObjectPointer; |
6148 | } |
6149 | }; |
6150 | |
6151 | class AtomicType : public Type, public llvm::FoldingSetNode { |
6152 | friend class ASTContext; // ASTContext creates these. |
6153 | |
6154 | QualType ValueType; |
6155 | |
6156 | AtomicType(QualType ValTy, QualType Canonical) |
6157 | : Type(Atomic, Canonical, ValTy->isDependentType(), |
6158 | ValTy->isInstantiationDependentType(), |
6159 | ValTy->isVariablyModifiedType(), |
6160 | ValTy->containsUnexpandedParameterPack()), |
6161 | ValueType(ValTy) {} |
6162 | |
6163 | public: |
6164 | /// Gets the type contained by this atomic type, i.e. |
6165 | /// the type returned by performing an atomic load of this atomic type. |
6166 | QualType getValueType() const { return ValueType; } |
6167 | |
6168 | bool isSugared() const { return false; } |
6169 | QualType desugar() const { return QualType(this, 0); } |
6170 | |
6171 | void Profile(llvm::FoldingSetNodeID &ID) { |
6172 | Profile(ID, getValueType()); |
6173 | } |
6174 | |
6175 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { |
6176 | ID.AddPointer(T.getAsOpaquePtr()); |
6177 | } |
6178 | |
6179 | static bool classof(const Type *T) { |
6180 | return T->getTypeClass() == Atomic; |
6181 | } |
6182 | }; |
6183 | |
6184 | /// PipeType - OpenCL20. |
6185 | class PipeType : public Type, public llvm::FoldingSetNode { |
6186 | friend class ASTContext; // ASTContext creates these. |
6187 | |
6188 | QualType ElementType; |
6189 | bool isRead; |
6190 | |
6191 | PipeType(QualType elemType, QualType CanonicalPtr, bool isRead) |
6192 | : Type(Pipe, CanonicalPtr, elemType->isDependentType(), |
6193 | elemType->isInstantiationDependentType(), |
6194 | elemType->isVariablyModifiedType(), |
6195 | elemType->containsUnexpandedParameterPack()), |
6196 | ElementType(elemType), isRead(isRead) {} |
6197 | |
6198 | public: |
6199 | QualType getElementType() const { return ElementType; } |
6200 | |
6201 | bool isSugared() const { return false; } |
6202 | |
6203 | QualType desugar() const { return QualType(this, 0); } |
6204 | |
6205 | void Profile(llvm::FoldingSetNodeID &ID) { |
6206 | Profile(ID, getElementType(), isReadOnly()); |
6207 | } |
6208 | |
6209 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T, bool isRead) { |
6210 | ID.AddPointer(T.getAsOpaquePtr()); |
6211 | ID.AddBoolean(isRead); |
6212 | } |
6213 | |
6214 | static bool classof(const Type *T) { |
6215 | return T->getTypeClass() == Pipe; |
6216 | } |
6217 | |
6218 | bool isReadOnly() const { return isRead; } |
6219 | }; |
6220 | |
6221 | /// A qualifier set is used to build a set of qualifiers. |
6222 | class QualifierCollector : public Qualifiers { |
6223 | public: |
6224 | QualifierCollector(Qualifiers Qs = Qualifiers()) : Qualifiers(Qs) {} |
6225 | |
6226 | /// Collect any qualifiers on the given type and return an |
6227 | /// unqualified type. The qualifiers are assumed to be consistent |
6228 | /// with those already in the type. |
6229 | const Type *strip(QualType type) { |
6230 | addFastQualifiers(type.getLocalFastQualifiers()); |
6231 | if (!type.hasLocalNonFastQualifiers()) |
6232 | return type.getTypePtrUnsafe(); |
6233 | |
6234 | const ExtQuals *extQuals = type.getExtQualsUnsafe(); |
6235 | addConsistentQualifiers(extQuals->getQualifiers()); |
6236 | return extQuals->getBaseType(); |
6237 | } |
6238 | |
6239 | /// Apply the collected qualifiers to the given type. |
6240 | QualType apply(const ASTContext &Context, QualType QT) const; |
6241 | |
6242 | /// Apply the collected qualifiers to the given type. |
6243 | QualType apply(const ASTContext &Context, const Type* T) const; |
6244 | }; |
6245 | |
6246 | /// A container of type source information. |
6247 | /// |
6248 | /// A client can read the relevant info using TypeLoc wrappers, e.g: |
6249 | /// @code |
6250 | /// TypeLoc TL = TypeSourceInfo->getTypeLoc(); |
6251 | /// TL.getBeginLoc().print(OS, SrcMgr); |
6252 | /// @endcode |
6253 | class alignas(8) TypeSourceInfo { |
6254 | // Contains a memory block after the class, used for type source information, |
6255 | // allocated by ASTContext. |
6256 | friend class ASTContext; |
6257 | |
6258 | QualType Ty; |
6259 | |
6260 | TypeSourceInfo(QualType ty) : Ty(ty) {} |
6261 | |
6262 | public: |
6263 | /// Return the type wrapped by this type source info. |
6264 | QualType getType() const { return Ty; } |
6265 | |
6266 | /// Return the TypeLoc wrapper for the type source info. |
6267 | TypeLoc getTypeLoc() const; // implemented in TypeLoc.h |
6268 | |
6269 | /// Override the type stored in this TypeSourceInfo. Use with caution! |
6270 | void overrideType(QualType T) { Ty = T; } |
6271 | }; |
6272 | |
6273 | // Inline function definitions. |
6274 | |
6275 | inline SplitQualType SplitQualType::getSingleStepDesugaredType() const { |
6276 | SplitQualType desugar = |
6277 | Ty->getLocallyUnqualifiedSingleStepDesugaredType().split(); |
6278 | desugar.Quals.addConsistentQualifiers(Quals); |
6279 | return desugar; |
6280 | } |
6281 | |
6282 | inline const Type *QualType::getTypePtr() const { |
6283 | return getCommonPtr()->BaseType; |
6284 | } |
6285 | |
6286 | inline const Type *QualType::getTypePtrOrNull() const { |
6287 | return (isNull() ? nullptr : getCommonPtr()->BaseType); |
6288 | } |
6289 | |
6290 | inline SplitQualType QualType::split() const { |
6291 | if (!hasLocalNonFastQualifiers()) |
6292 | return SplitQualType(getTypePtrUnsafe(), |
6293 | Qualifiers::fromFastMask(getLocalFastQualifiers())); |
6294 | |
6295 | const ExtQuals *eq = getExtQualsUnsafe(); |
6296 | Qualifiers qs = eq->getQualifiers(); |
6297 | qs.addFastQualifiers(getLocalFastQualifiers()); |
6298 | return SplitQualType(eq->getBaseType(), qs); |
6299 | } |
6300 | |
6301 | inline Qualifiers QualType::getLocalQualifiers() const { |
6302 | Qualifiers Quals; |
6303 | if (hasLocalNonFastQualifiers()) |
6304 | Quals = getExtQualsUnsafe()->getQualifiers(); |
6305 | Quals.addFastQualifiers(getLocalFastQualifiers()); |
6306 | return Quals; |
6307 | } |
6308 | |
6309 | inline Qualifiers QualType::getQualifiers() const { |
6310 | Qualifiers quals = getCommonPtr()->CanonicalType.getLocalQualifiers(); |
6311 | quals.addFastQualifiers(getLocalFastQualifiers()); |
6312 | return quals; |
6313 | } |
6314 | |
6315 | inline unsigned QualType::getCVRQualifiers() const { |
6316 | unsigned cvr = getCommonPtr()->CanonicalType.getLocalCVRQualifiers(); |
6317 | cvr |= getLocalCVRQualifiers(); |
6318 | return cvr; |
6319 | } |
6320 | |
6321 | inline QualType QualType::getCanonicalType() const { |
6322 | QualType canon = getCommonPtr()->CanonicalType; |
6323 | return canon.withFastQualifiers(getLocalFastQualifiers()); |
6324 | } |
6325 | |
6326 | inline bool QualType::isCanonical() const { |
6327 | return getTypePtr()->isCanonicalUnqualified(); |
6328 | } |
6329 | |
6330 | inline bool QualType::isCanonicalAsParam() const { |
6331 | if (!isCanonical()) return false; |
6332 | if (hasLocalQualifiers()) return false; |
6333 | |
6334 | const Type *T = getTypePtr(); |
6335 | if (T->isVariablyModifiedType() && T->hasSizedVLAType()) |
6336 | return false; |
6337 | |
6338 | return !isa<FunctionType>(T) && !isa<ArrayType>(T); |
6339 | } |
6340 | |
6341 | inline bool QualType::isConstQualified() const { |
6342 | return isLocalConstQualified() || |
6343 | getCommonPtr()->CanonicalType.isLocalConstQualified(); |
6344 | } |
6345 | |
6346 | inline bool QualType::isRestrictQualified() const { |
6347 | return isLocalRestrictQualified() || |
6348 | getCommonPtr()->CanonicalType.isLocalRestrictQualified(); |
6349 | } |
6350 | |
6351 | |
6352 | inline bool QualType::isVolatileQualified() const { |
6353 | return isLocalVolatileQualified() || |
6354 | getCommonPtr()->CanonicalType.isLocalVolatileQualified(); |
6355 | } |
6356 | |
6357 | inline bool QualType::hasQualifiers() const { |
6358 | return hasLocalQualifiers() || |
6359 | getCommonPtr()->CanonicalType.hasLocalQualifiers(); |
6360 | } |
6361 | |
6362 | inline QualType QualType::getUnqualifiedType() const { |
6363 | if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) |
6364 | return QualType(getTypePtr(), 0); |
6365 | |
6366 | return QualType(getSplitUnqualifiedTypeImpl(*this).Ty, 0); |
6367 | } |
6368 | |
6369 | inline SplitQualType QualType::getSplitUnqualifiedType() const { |
6370 | if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) |
6371 | return split(); |
6372 | |
6373 | return getSplitUnqualifiedTypeImpl(*this); |
6374 | } |
6375 | |
6376 | inline void QualType::removeLocalConst() { |
6377 | removeLocalFastQualifiers(Qualifiers::Const); |
6378 | } |
6379 | |
6380 | inline void QualType::removeLocalRestrict() { |
6381 | removeLocalFastQualifiers(Qualifiers::Restrict); |
6382 | } |
6383 | |
6384 | inline void QualType::removeLocalVolatile() { |
6385 | removeLocalFastQualifiers(Qualifiers::Volatile); |
6386 | } |
6387 | |
6388 | inline void QualType::removeLocalCVRQualifiers(unsigned Mask) { |
6389 | assert(!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits")((!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits" ) ? static_cast<void> (0) : __assert_fail ("!(Mask & ~Qualifiers::CVRMask) && \"mask has non-CVR bits\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 6389, __PRETTY_FUNCTION__)); |
6390 | static_assert((int)Qualifiers::CVRMask == (int)Qualifiers::FastMask, |
6391 | "Fast bits differ from CVR bits!"); |
6392 | |
6393 | // Fast path: we don't need to touch the slow qualifiers. |
6394 | removeLocalFastQualifiers(Mask); |
6395 | } |
6396 | |
6397 | /// Check if this type has any address space qualifier. |
6398 | inline bool QualType::hasAddressSpace() const { |
6399 | return getQualifiers().hasAddressSpace(); |
6400 | } |
6401 | |
6402 | /// Return the address space of this type. |
6403 | inline LangAS QualType::getAddressSpace() const { |
6404 | return getQualifiers().getAddressSpace(); |
6405 | } |
6406 | |
6407 | /// Return the gc attribute of this type. |
6408 | inline Qualifiers::GC QualType::getObjCGCAttr() const { |
6409 | return getQualifiers().getObjCGCAttr(); |
6410 | } |
6411 | |
6412 | inline bool QualType::hasNonTrivialToPrimitiveDefaultInitializeCUnion() const { |
6413 | if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) |
6414 | return hasNonTrivialToPrimitiveDefaultInitializeCUnion(RD); |
6415 | return false; |
6416 | } |
6417 | |
6418 | inline bool QualType::hasNonTrivialToPrimitiveDestructCUnion() const { |
6419 | if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) |
6420 | return hasNonTrivialToPrimitiveDestructCUnion(RD); |
6421 | return false; |
6422 | } |
6423 | |
6424 | inline bool QualType::hasNonTrivialToPrimitiveCopyCUnion() const { |
6425 | if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) |
6426 | return hasNonTrivialToPrimitiveCopyCUnion(RD); |
6427 | return false; |
6428 | } |
6429 | |
6430 | inline FunctionType::ExtInfo getFunctionExtInfo(const Type &t) { |
6431 | if (const auto *PT = t.getAs<PointerType>()) { |
6432 | if (const auto *FT = PT->getPointeeType()->getAs<FunctionType>()) |
6433 | return FT->getExtInfo(); |
6434 | } else if (const auto *FT = t.getAs<FunctionType>()) |
6435 | return FT->getExtInfo(); |
6436 | |
6437 | return FunctionType::ExtInfo(); |
6438 | } |
6439 | |
6440 | inline FunctionType::ExtInfo getFunctionExtInfo(QualType t) { |
6441 | return getFunctionExtInfo(*t); |
6442 | } |
6443 | |
6444 | /// Determine whether this type is more |
6445 | /// qualified than the Other type. For example, "const volatile int" |
6446 | /// is more qualified than "const int", "volatile int", and |
6447 | /// "int". However, it is not more qualified than "const volatile |
6448 | /// int". |
6449 | inline bool QualType::isMoreQualifiedThan(QualType other) const { |
6450 | Qualifiers MyQuals = getQualifiers(); |
6451 | Qualifiers OtherQuals = other.getQualifiers(); |
6452 | return (MyQuals != OtherQuals && MyQuals.compatiblyIncludes(OtherQuals)); |
6453 | } |
6454 | |
6455 | /// Determine whether this type is at last |
6456 | /// as qualified as the Other type. For example, "const volatile |
6457 | /// int" is at least as qualified as "const int", "volatile int", |
6458 | /// "int", and "const volatile int". |
6459 | inline bool QualType::isAtLeastAsQualifiedAs(QualType other) const { |
6460 | Qualifiers OtherQuals = other.getQualifiers(); |
6461 | |
6462 | // Ignore __unaligned qualifier if this type is a void. |
6463 | if (getUnqualifiedType()->isVoidType()) |
6464 | OtherQuals.removeUnaligned(); |
6465 | |
6466 | return getQualifiers().compatiblyIncludes(OtherQuals); |
6467 | } |
6468 | |
6469 | /// If Type is a reference type (e.g., const |
6470 | /// int&), returns the type that the reference refers to ("const |
6471 | /// int"). Otherwise, returns the type itself. This routine is used |
6472 | /// throughout Sema to implement C++ 5p6: |
6473 | /// |
6474 | /// If an expression initially has the type "reference to T" (8.3.2, |
6475 | /// 8.5.3), the type is adjusted to "T" prior to any further |
6476 | /// analysis, the expression designates the object or function |
6477 | /// denoted by the reference, and the expression is an lvalue. |
6478 | inline QualType QualType::getNonReferenceType() const { |
6479 | if (const auto *RefType = (*this)->getAs<ReferenceType>()) |
6480 | return RefType->getPointeeType(); |
6481 | else |
6482 | return *this; |
6483 | } |
6484 | |
6485 | inline bool QualType::isCForbiddenLValueType() const { |
6486 | return ((getTypePtr()->isVoidType() && !hasQualifiers()) || |
6487 | getTypePtr()->isFunctionType()); |
6488 | } |
6489 | |
6490 | /// Tests whether the type is categorized as a fundamental type. |
6491 | /// |
6492 | /// \returns True for types specified in C++0x [basic.fundamental]. |
6493 | inline bool Type::isFundamentalType() const { |
6494 | return isVoidType() || |
6495 | isNullPtrType() || |
6496 | // FIXME: It's really annoying that we don't have an |
6497 | // 'isArithmeticType()' which agrees with the standard definition. |
6498 | (isArithmeticType() && !isEnumeralType()); |
6499 | } |
6500 | |
6501 | /// Tests whether the type is categorized as a compound type. |
6502 | /// |
6503 | /// \returns True for types specified in C++0x [basic.compound]. |
6504 | inline bool Type::isCompoundType() const { |
6505 | // C++0x [basic.compound]p1: |
6506 | // Compound types can be constructed in the following ways: |
6507 | // -- arrays of objects of a given type [...]; |
6508 | return isArrayType() || |
6509 | // -- functions, which have parameters of given types [...]; |
6510 | isFunctionType() || |
6511 | // -- pointers to void or objects or functions [...]; |
6512 | isPointerType() || |
6513 | // -- references to objects or functions of a given type. [...] |
6514 | isReferenceType() || |
6515 | // -- classes containing a sequence of objects of various types, [...]; |
6516 | isRecordType() || |
6517 | // -- unions, which are classes capable of containing objects of different |
6518 | // types at different times; |
6519 | isUnionType() || |
6520 | // -- enumerations, which comprise a set of named constant values. [...]; |
6521 | isEnumeralType() || |
6522 | // -- pointers to non-static class members, [...]. |
6523 | isMemberPointerType(); |
6524 | } |
6525 | |
6526 | inline bool Type::isFunctionType() const { |
6527 | return isa<FunctionType>(CanonicalType); |
6528 | } |
6529 | |
6530 | inline bool Type::isPointerType() const { |
6531 | return isa<PointerType>(CanonicalType); |
6532 | } |
6533 | |
6534 | inline bool Type::isAnyPointerType() const { |
6535 | return isPointerType() || isObjCObjectPointerType(); |
6536 | } |
6537 | |
6538 | inline bool Type::isBlockPointerType() const { |
6539 | return isa<BlockPointerType>(CanonicalType); |
6540 | } |
6541 | |
6542 | inline bool Type::isReferenceType() const { |
6543 | return isa<ReferenceType>(CanonicalType); |
6544 | } |
6545 | |
6546 | inline bool Type::isLValueReferenceType() const { |
6547 | return isa<LValueReferenceType>(CanonicalType); |
6548 | } |
6549 | |
6550 | inline bool Type::isRValueReferenceType() const { |
6551 | return isa<RValueReferenceType>(CanonicalType); |
6552 | } |
6553 | |
6554 | inline bool Type::isObjectPointerType() const { |
6555 | // Note: an "object pointer type" is not the same thing as a pointer to an |
6556 | // object type; rather, it is a pointer to an object type or a pointer to cv |
6557 | // void. |
6558 | if (const auto *T = getAs<PointerType>()) |
6559 | return !T->getPointeeType()->isFunctionType(); |
6560 | else |
6561 | return false; |
6562 | } |
6563 | |
6564 | inline bool Type::isFunctionPointerType() const { |
6565 | if (const auto *T = getAs<PointerType>()) |
6566 | return T->getPointeeType()->isFunctionType(); |
6567 | else |
6568 | return false; |
6569 | } |
6570 | |
6571 | inline bool Type::isFunctionReferenceType() const { |
6572 | if (const auto *T = getAs<ReferenceType>()) |
6573 | return T->getPointeeType()->isFunctionType(); |
6574 | else |
6575 | return false; |
6576 | } |
6577 | |
6578 | inline bool Type::isMemberPointerType() const { |
6579 | return isa<MemberPointerType>(CanonicalType); |
6580 | } |
6581 | |
6582 | inline bool Type::isMemberFunctionPointerType() const { |
6583 | if (const auto *T = getAs<MemberPointerType>()) |
6584 | return T->isMemberFunctionPointer(); |
6585 | else |
6586 | return false; |
6587 | } |
6588 | |
6589 | inline bool Type::isMemberDataPointerType() const { |
6590 | if (const auto *T = getAs<MemberPointerType>()) |
6591 | return T->isMemberDataPointer(); |
6592 | else |
6593 | return false; |
6594 | } |
6595 | |
6596 | inline bool Type::isArrayType() const { |
6597 | return isa<ArrayType>(CanonicalType); |
6598 | } |
6599 | |
6600 | inline bool Type::isConstantArrayType() const { |
6601 | return isa<ConstantArrayType>(CanonicalType); |
6602 | } |
6603 | |
6604 | inline bool Type::isIncompleteArrayType() const { |
6605 | return isa<IncompleteArrayType>(CanonicalType); |
6606 | } |
6607 | |
6608 | inline bool Type::isVariableArrayType() const { |
6609 | return isa<VariableArrayType>(CanonicalType); |
6610 | } |
6611 | |
6612 | inline bool Type::isDependentSizedArrayType() const { |
6613 | return isa<DependentSizedArrayType>(CanonicalType); |
6614 | } |
6615 | |
6616 | inline bool Type::isBuiltinType() const { |
6617 | return isa<BuiltinType>(CanonicalType); |
6618 | } |
6619 | |
6620 | inline bool Type::isRecordType() const { |
6621 | return isa<RecordType>(CanonicalType); |
6622 | } |
6623 | |
6624 | inline bool Type::isEnumeralType() const { |
6625 | return isa<EnumType>(CanonicalType); |
6626 | } |
6627 | |
6628 | inline bool Type::isAnyComplexType() const { |
6629 | return isa<ComplexType>(CanonicalType); |
6630 | } |
6631 | |
6632 | inline bool Type::isVectorType() const { |
6633 | return isa<VectorType>(CanonicalType); |
6634 | } |
6635 | |
6636 | inline bool Type::isExtVectorType() const { |
6637 | return isa<ExtVectorType>(CanonicalType); |
6638 | } |
6639 | |
6640 | inline bool Type::isDependentAddressSpaceType() const { |
6641 | return isa<DependentAddressSpaceType>(CanonicalType); |
6642 | } |
6643 | |
6644 | inline bool Type::isObjCObjectPointerType() const { |
6645 | return isa<ObjCObjectPointerType>(CanonicalType); |
6646 | } |
6647 | |
6648 | inline bool Type::isObjCObjectType() const { |
6649 | return isa<ObjCObjectType>(CanonicalType); |
6650 | } |
6651 | |
6652 | inline bool Type::isObjCObjectOrInterfaceType() const { |
6653 | return isa<ObjCInterfaceType>(CanonicalType) || |
6654 | isa<ObjCObjectType>(CanonicalType); |
6655 | } |
6656 | |
6657 | inline bool Type::isAtomicType() const { |
6658 | return isa<AtomicType>(CanonicalType); |
6659 | } |
6660 | |
6661 | inline bool Type::isUndeducedAutoType() const { |
6662 | return isa<AutoType>(CanonicalType); |
6663 | } |
6664 | |
6665 | inline bool Type::isObjCQualifiedIdType() const { |
6666 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6667 | return OPT->isObjCQualifiedIdType(); |
6668 | return false; |
6669 | } |
6670 | |
6671 | inline bool Type::isObjCQualifiedClassType() const { |
6672 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6673 | return OPT->isObjCQualifiedClassType(); |
6674 | return false; |
6675 | } |
6676 | |
6677 | inline bool Type::isObjCIdType() const { |
6678 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6679 | return OPT->isObjCIdType(); |
6680 | return false; |
6681 | } |
6682 | |
6683 | inline bool Type::isObjCClassType() const { |
6684 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6685 | return OPT->isObjCClassType(); |
6686 | return false; |
6687 | } |
6688 | |
6689 | inline bool Type::isObjCSelType() const { |
6690 | if (const auto *OPT = getAs<PointerType>()) |
6691 | return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel); |
6692 | return false; |
6693 | } |
6694 | |
6695 | inline bool Type::isObjCBuiltinType() const { |
6696 | return isObjCIdType() || isObjCClassType() || isObjCSelType(); |
6697 | } |
6698 | |
6699 | inline bool Type::isDecltypeType() const { |
6700 | return isa<DecltypeType>(this); |
6701 | } |
6702 | |
6703 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
6704 | inline bool Type::is##Id##Type() const { \ |
6705 | return isSpecificBuiltinType(BuiltinType::Id); \ |
6706 | } |
6707 | #include "clang/Basic/OpenCLImageTypes.def" |
6708 | |
6709 | inline bool Type::isSamplerT() const { |
6710 | return isSpecificBuiltinType(BuiltinType::OCLSampler); |
6711 | } |
6712 | |
6713 | inline bool Type::isEventT() const { |
6714 | return isSpecificBuiltinType(BuiltinType::OCLEvent); |
6715 | } |
6716 | |
6717 | inline bool Type::isClkEventT() const { |
6718 | return isSpecificBuiltinType(BuiltinType::OCLClkEvent); |
6719 | } |
6720 | |
6721 | inline bool Type::isQueueT() const { |
6722 | return isSpecificBuiltinType(BuiltinType::OCLQueue); |
6723 | } |
6724 | |
6725 | inline bool Type::isReserveIDT() const { |
6726 | return isSpecificBuiltinType(BuiltinType::OCLReserveID); |
6727 | } |
6728 | |
6729 | inline bool Type::isImageType() const { |
6730 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) is##Id##Type() || |
6731 | return |
6732 | #include "clang/Basic/OpenCLImageTypes.def" |
6733 | false; // end boolean or operation |
6734 | } |
6735 | |
6736 | inline bool Type::isPipeType() const { |
6737 | return isa<PipeType>(CanonicalType); |
6738 | } |
6739 | |
6740 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
6741 | inline bool Type::is##Id##Type() const { \ |
6742 | return isSpecificBuiltinType(BuiltinType::Id); \ |
6743 | } |
6744 | #include "clang/Basic/OpenCLExtensionTypes.def" |
6745 | |
6746 | inline bool Type::isOCLIntelSubgroupAVCType() const { |
6747 | #define INTEL_SUBGROUP_AVC_TYPE(ExtType, Id) \ |
6748 | isOCLIntelSubgroupAVC##Id##Type() || |
6749 | return |
6750 | #include "clang/Basic/OpenCLExtensionTypes.def" |
6751 | false; // end of boolean or operation |
6752 | } |
6753 | |
6754 | inline bool Type::isOCLExtOpaqueType() const { |
6755 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) is##Id##Type() || |
6756 | return |
6757 | #include "clang/Basic/OpenCLExtensionTypes.def" |
6758 | false; // end of boolean or operation |
6759 | } |
6760 | |
6761 | inline bool Type::isOpenCLSpecificType() const { |
6762 | return isSamplerT() || isEventT() || isImageType() || isClkEventT() || |
6763 | isQueueT() || isReserveIDT() || isPipeType() || isOCLExtOpaqueType(); |
6764 | } |
6765 | |
6766 | inline bool Type::isTemplateTypeParmType() const { |
6767 | return isa<TemplateTypeParmType>(CanonicalType); |
6768 | } |
6769 | |
6770 | inline bool Type::isSpecificBuiltinType(unsigned K) const { |
6771 | if (const BuiltinType *BT = getAs<BuiltinType>()) |
6772 | if (BT->getKind() == (BuiltinType::Kind) K) |
6773 | return true; |
6774 | return false; |
6775 | } |
6776 | |
6777 | inline bool Type::isPlaceholderType() const { |
6778 | if (const auto *BT = dyn_cast<BuiltinType>(this)) |
6779 | return BT->isPlaceholderType(); |
6780 | return false; |
6781 | } |
6782 | |
6783 | inline const BuiltinType *Type::getAsPlaceholderType() const { |
6784 | if (const auto *BT = dyn_cast<BuiltinType>(this)) |
6785 | if (BT->isPlaceholderType()) |
6786 | return BT; |
6787 | return nullptr; |
6788 | } |
6789 | |
6790 | inline bool Type::isSpecificPlaceholderType(unsigned K) const { |
6791 | assert(BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K))((BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K)) ? static_cast<void> (0) : __assert_fail ("BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K)" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 6791, __PRETTY_FUNCTION__)); |
6792 | if (const auto *BT = dyn_cast<BuiltinType>(this)) |
6793 | return (BT->getKind() == (BuiltinType::Kind) K); |
6794 | return false; |
6795 | } |
6796 | |
6797 | inline bool Type::isNonOverloadPlaceholderType() const { |
6798 | if (const auto *BT = dyn_cast<BuiltinType>(this)) |
6799 | return BT->isNonOverloadPlaceholderType(); |
6800 | return false; |
6801 | } |
6802 | |
6803 | inline bool Type::isVoidType() const { |
6804 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
6805 | return BT->getKind() == BuiltinType::Void; |
6806 | return false; |
6807 | } |
6808 | |
6809 | inline bool Type::isHalfType() const { |
6810 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
6811 | return BT->getKind() == BuiltinType::Half; |
6812 | // FIXME: Should we allow complex __fp16? Probably not. |
6813 | return false; |
6814 | } |
6815 | |
6816 | inline bool Type::isFloat16Type() const { |
6817 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
6818 | return BT->getKind() == BuiltinType::Float16; |
6819 | return false; |
6820 | } |
6821 | |
6822 | inline bool Type::isFloat128Type() const { |
6823 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
6824 | return BT->getKind() == BuiltinType::Float128; |
6825 | return false; |
6826 | } |
6827 | |
6828 | inline bool Type::isNullPtrType() const { |
6829 | if (const auto *BT = getAs<BuiltinType>()) |
6830 | return BT->getKind() == BuiltinType::NullPtr; |
6831 | return false; |
6832 | } |
6833 | |
6834 | bool IsEnumDeclComplete(EnumDecl *); |
6835 | bool IsEnumDeclScoped(EnumDecl *); |
6836 | |
6837 | inline bool Type::isIntegerType() const { |
6838 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
6839 | return BT->getKind() >= BuiltinType::Bool && |
6840 | BT->getKind() <= BuiltinType::Int128; |
6841 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) { |
6842 | // Incomplete enum types are not treated as integer types. |
6843 | // FIXME: In C++, enum types are never integer types. |
6844 | return IsEnumDeclComplete(ET->getDecl()) && |
6845 | !IsEnumDeclScoped(ET->getDecl()); |
6846 | } |
6847 | return false; |
6848 | } |
6849 | |
6850 | inline bool Type::isFixedPointType() const { |
6851 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
6852 | return BT->getKind() >= BuiltinType::ShortAccum && |
6853 | BT->getKind() <= BuiltinType::SatULongFract; |
6854 | } |
6855 | return false; |
6856 | } |
6857 | |
6858 | inline bool Type::isFixedPointOrIntegerType() const { |
6859 | return isFixedPointType() || isIntegerType(); |
6860 | } |
6861 | |
6862 | inline bool Type::isSaturatedFixedPointType() const { |
6863 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
6864 | return BT->getKind() >= BuiltinType::SatShortAccum && |
6865 | BT->getKind() <= BuiltinType::SatULongFract; |
6866 | } |
6867 | return false; |
6868 | } |
6869 | |
6870 | inline bool Type::isUnsaturatedFixedPointType() const { |
6871 | return isFixedPointType() && !isSaturatedFixedPointType(); |
6872 | } |
6873 | |
6874 | inline bool Type::isSignedFixedPointType() const { |
6875 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
6876 | return ((BT->getKind() >= BuiltinType::ShortAccum && |
6877 | BT->getKind() <= BuiltinType::LongAccum) || |
6878 | (BT->getKind() >= BuiltinType::ShortFract && |
6879 | BT->getKind() <= BuiltinType::LongFract) || |
6880 | (BT->getKind() >= BuiltinType::SatShortAccum && |
6881 | BT->getKind() <= BuiltinType::SatLongAccum) || |
6882 | (BT->getKind() >= BuiltinType::SatShortFract && |
6883 | BT->getKind() <= BuiltinType::SatLongFract)); |
6884 | } |
6885 | return false; |
6886 | } |
6887 | |
6888 | inline bool Type::isUnsignedFixedPointType() const { |
6889 | return isFixedPointType() && !isSignedFixedPointType(); |
6890 | } |
6891 | |
6892 | inline bool Type::isScalarType() const { |
6893 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
6894 | return BT->getKind() > BuiltinType::Void && |
6895 | BT->getKind() <= BuiltinType::NullPtr; |
6896 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
6897 | // Enums are scalar types, but only if they are defined. Incomplete enums |
6898 | // are not treated as scalar types. |
6899 | return IsEnumDeclComplete(ET->getDecl()); |
6900 | return isa<PointerType>(CanonicalType) || |
6901 | isa<BlockPointerType>(CanonicalType) || |
6902 | isa<MemberPointerType>(CanonicalType) || |
6903 | isa<ComplexType>(CanonicalType) || |
6904 | isa<ObjCObjectPointerType>(CanonicalType); |
6905 | } |
6906 | |
6907 | inline bool Type::isIntegralOrEnumerationType() const { |
6908 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
6909 | return BT->getKind() >= BuiltinType::Bool && |
6910 | BT->getKind() <= BuiltinType::Int128; |
6911 | |
6912 | // Check for a complete enum type; incomplete enum types are not properly an |
6913 | // enumeration type in the sense required here. |
6914 | if (const auto *ET = dyn_cast<EnumType>(CanonicalType)) |
6915 | return IsEnumDeclComplete(ET->getDecl()); |
6916 | |
6917 | return false; |
6918 | } |
6919 | |
6920 | inline bool Type::isBooleanType() const { |
6921 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
6922 | return BT->getKind() == BuiltinType::Bool; |
6923 | return false; |
6924 | } |
6925 | |
6926 | inline bool Type::isUndeducedType() const { |
6927 | auto *DT = getContainedDeducedType(); |
6928 | return DT && !DT->isDeduced(); |
6929 | } |
6930 | |
6931 | /// Determines whether this is a type for which one can define |
6932 | /// an overloaded operator. |
6933 | inline bool Type::isOverloadableType() const { |
6934 | return isDependentType() || isRecordType() || isEnumeralType(); |
6935 | } |
6936 | |
6937 | /// Determines whether this type can decay to a pointer type. |
6938 | inline bool Type::canDecayToPointerType() const { |
6939 | return isFunctionType() || isArrayType(); |
6940 | } |
6941 | |
6942 | inline bool Type::hasPointerRepresentation() const { |
6943 | return (isPointerType() || isReferenceType() || isBlockPointerType() || |
6944 | isObjCObjectPointerType() || isNullPtrType()); |
6945 | } |
6946 | |
6947 | inline bool Type::hasObjCPointerRepresentation() const { |
6948 | return isObjCObjectPointerType(); |
6949 | } |
6950 | |
6951 | inline const Type *Type::getBaseElementTypeUnsafe() const { |
6952 | const Type *type = this; |
6953 | while (const ArrayType *arrayType = type->getAsArrayTypeUnsafe()) |
6954 | type = arrayType->getElementType().getTypePtr(); |
6955 | return type; |
6956 | } |
6957 | |
6958 | inline const Type *Type::getPointeeOrArrayElementType() const { |
6959 | const Type *type = this; |
6960 | if (type->isAnyPointerType()) |
6961 | return type->getPointeeType().getTypePtr(); |
6962 | else if (type->isArrayType()) |
6963 | return type->getBaseElementTypeUnsafe(); |
6964 | return type; |
6965 | } |
6966 | /// Insertion operator for diagnostics. This allows sending address spaces into |
6967 | /// a diagnostic with <<. |
6968 | inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, |
6969 | LangAS AS) { |
6970 | DB.AddTaggedVal(static_cast<std::underlying_type_t<LangAS>>(AS), |
6971 | DiagnosticsEngine::ArgumentKind::ak_addrspace); |
6972 | return DB; |
6973 | } |
6974 | |
6975 | /// Insertion operator for partial diagnostics. This allows sending adress |
6976 | /// spaces into a diagnostic with <<. |
6977 | inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD, |
6978 | LangAS AS) { |
6979 | PD.AddTaggedVal(static_cast<std::underlying_type_t<LangAS>>(AS), |
6980 | DiagnosticsEngine::ArgumentKind::ak_addrspace); |
6981 | return PD; |
6982 | } |
6983 | |
6984 | /// Insertion operator for diagnostics. This allows sending Qualifiers into a |
6985 | /// diagnostic with <<. |
6986 | inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, |
6987 | Qualifiers Q) { |
6988 | DB.AddTaggedVal(Q.getAsOpaqueValue(), |
6989 | DiagnosticsEngine::ArgumentKind::ak_qual); |
6990 | return DB; |
6991 | } |
6992 | |
6993 | /// Insertion operator for partial diagnostics. This allows sending Qualifiers |
6994 | /// into a diagnostic with <<. |
6995 | inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD, |
6996 | Qualifiers Q) { |
6997 | PD.AddTaggedVal(Q.getAsOpaqueValue(), |
6998 | DiagnosticsEngine::ArgumentKind::ak_qual); |
6999 | return PD; |
7000 | } |
7001 | |
7002 | /// Insertion operator for diagnostics. This allows sending QualType's into a |
7003 | /// diagnostic with <<. |
7004 | inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, |
7005 | QualType T) { |
7006 | DB.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), |
7007 | DiagnosticsEngine::ak_qualtype); |
7008 | return DB; |
7009 | } |
7010 | |
7011 | /// Insertion operator for partial diagnostics. This allows sending QualType's |
7012 | /// into a diagnostic with <<. |
7013 | inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD, |
7014 | QualType T) { |
7015 | PD.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), |
7016 | DiagnosticsEngine::ak_qualtype); |
7017 | return PD; |
7018 | } |
7019 | |
7020 | // Helper class template that is used by Type::getAs to ensure that one does |
7021 | // not try to look through a qualified type to get to an array type. |
7022 | template <typename T> |
7023 | using TypeIsArrayType = |
7024 | std::integral_constant<bool, std::is_same<T, ArrayType>::value || |
7025 | std::is_base_of<ArrayType, T>::value>; |
7026 | |
7027 | // Member-template getAs<specific type>'. |
7028 | template <typename T> const T *Type::getAs() const { |
7029 | static_assert(!TypeIsArrayType<T>::value, |
7030 | "ArrayType cannot be used with getAs!"); |
7031 | |
7032 | // If this is directly a T type, return it. |
7033 | if (const auto *Ty = dyn_cast<T>(this)) |
7034 | return Ty; |
7035 | |
7036 | // If the canonical form of this type isn't the right kind, reject it. |
7037 | if (!isa<T>(CanonicalType)) |
7038 | return nullptr; |
7039 | |
7040 | // If this is a typedef for the type, strip the typedef off without |
7041 | // losing all typedef information. |
7042 | return cast<T>(getUnqualifiedDesugaredType()); |
7043 | } |
7044 | |
7045 | template <typename T> const T *Type::getAsAdjusted() const { |
7046 | static_assert(!TypeIsArrayType<T>::value, "ArrayType cannot be used with getAsAdjusted!"); |
7047 | |
7048 | // If this is directly a T type, return it. |
7049 | if (const auto *Ty = dyn_cast<T>(this)) |
7050 | return Ty; |
7051 | |
7052 | // If the canonical form of this type isn't the right kind, reject it. |
7053 | if (!isa<T>(CanonicalType)) |
7054 | return nullptr; |
7055 | |
7056 | // Strip off type adjustments that do not modify the underlying nature of the |
7057 | // type. |
7058 | const Type *Ty = this; |
7059 | while (Ty) { |
7060 | if (const auto *A = dyn_cast<AttributedType>(Ty)) |
7061 | Ty = A->getModifiedType().getTypePtr(); |
7062 | else if (const auto *E = dyn_cast<ElaboratedType>(Ty)) |
7063 | Ty = E->desugar().getTypePtr(); |
7064 | else if (const auto *P = dyn_cast<ParenType>(Ty)) |
7065 | Ty = P->desugar().getTypePtr(); |
7066 | else if (const auto *A = dyn_cast<AdjustedType>(Ty)) |
7067 | Ty = A->desugar().getTypePtr(); |
7068 | else if (const auto *M = dyn_cast<MacroQualifiedType>(Ty)) |
7069 | Ty = M->desugar().getTypePtr(); |
7070 | else |
7071 | break; |
7072 | } |
7073 | |
7074 | // Just because the canonical type is correct does not mean we can use cast<>, |
7075 | // since we may not have stripped off all the sugar down to the base type. |
7076 | return dyn_cast<T>(Ty); |
7077 | } |
7078 | |
7079 | inline const ArrayType *Type::getAsArrayTypeUnsafe() const { |
7080 | // If this is directly an array type, return it. |
7081 | if (const auto *arr = dyn_cast<ArrayType>(this)) |
7082 | return arr; |
7083 | |
7084 | // If the canonical form of this type isn't the right kind, reject it. |
7085 | if (!isa<ArrayType>(CanonicalType)) |
7086 | return nullptr; |
7087 | |
7088 | // If this is a typedef for the type, strip the typedef off without |
7089 | // losing all typedef information. |
7090 | return cast<ArrayType>(getUnqualifiedDesugaredType()); |
7091 | } |
7092 | |
7093 | template <typename T> const T *Type::castAs() const { |
7094 | static_assert(!TypeIsArrayType<T>::value, |
7095 | "ArrayType cannot be used with castAs!"); |
7096 | |
7097 | if (const auto *ty = dyn_cast<T>(this)) return ty; |
7098 | assert(isa<T>(CanonicalType))((isa<T>(CanonicalType)) ? static_cast<void> (0) : __assert_fail ("isa<T>(CanonicalType)", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 7098, __PRETTY_FUNCTION__)); |
7099 | return cast<T>(getUnqualifiedDesugaredType()); |
7100 | } |
7101 | |
7102 | inline const ArrayType *Type::castAsArrayTypeUnsafe() const { |
7103 | assert(isa<ArrayType>(CanonicalType))((isa<ArrayType>(CanonicalType)) ? static_cast<void> (0) : __assert_fail ("isa<ArrayType>(CanonicalType)", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 7103, __PRETTY_FUNCTION__)); |
7104 | if (const auto *arr = dyn_cast<ArrayType>(this)) return arr; |
7105 | return cast<ArrayType>(getUnqualifiedDesugaredType()); |
7106 | } |
7107 | |
7108 | DecayedType::DecayedType(QualType OriginalType, QualType DecayedPtr, |
7109 | QualType CanonicalPtr) |
7110 | : AdjustedType(Decayed, OriginalType, DecayedPtr, CanonicalPtr) { |
7111 | #ifndef NDEBUG |
7112 | QualType Adjusted = getAdjustedType(); |
7113 | (void)AttributedType::stripOuterNullability(Adjusted); |
7114 | assert(isa<PointerType>(Adjusted))((isa<PointerType>(Adjusted)) ? static_cast<void> (0) : __assert_fail ("isa<PointerType>(Adjusted)", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include/clang/AST/Type.h" , 7114, __PRETTY_FUNCTION__)); |
7115 | #endif |
7116 | } |
7117 | |
7118 | QualType DecayedType::getPointeeType() const { |
7119 | QualType Decayed = getDecayedType(); |
7120 | (void)AttributedType::stripOuterNullability(Decayed); |
7121 | return cast<PointerType>(Decayed)->getPointeeType(); |
7122 | } |
7123 | |
7124 | // Get the decimal string representation of a fixed point type, represented |
7125 | // as a scaled integer. |
7126 | // TODO: At some point, we should change the arguments to instead just accept an |
7127 | // APFixedPoint instead of APSInt and scale. |
7128 | void FixedPointValueToString(SmallVectorImpl<char> &Str, llvm::APSInt Val, |
7129 | unsigned Scale); |
7130 | |
7131 | } // namespace clang |
7132 | |
7133 | #endif // LLVM_CLANG_AST_TYPE_H |