File: | clang/lib/Sema/SemaCast.cpp |
Warning: | line 1360, column 14 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===--- SemaCast.cpp - Semantic Analysis for Casts -----------------------===// | ||||||
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 cast expressions, including | ||||||
10 | // 1) C-style casts like '(int) x' | ||||||
11 | // 2) C++ functional casts like 'int(x)' | ||||||
12 | // 3) C++ named casts like 'static_cast<int>(x)' | ||||||
13 | // | ||||||
14 | //===----------------------------------------------------------------------===// | ||||||
15 | |||||||
16 | #include "clang/AST/ASTContext.h" | ||||||
17 | #include "clang/AST/ASTStructuralEquivalence.h" | ||||||
18 | #include "clang/AST/CXXInheritance.h" | ||||||
19 | #include "clang/AST/ExprCXX.h" | ||||||
20 | #include "clang/AST/ExprObjC.h" | ||||||
21 | #include "clang/AST/RecordLayout.h" | ||||||
22 | #include "clang/Basic/PartialDiagnostic.h" | ||||||
23 | #include "clang/Basic/TargetInfo.h" | ||||||
24 | #include "clang/Lex/Preprocessor.h" | ||||||
25 | #include "clang/Sema/Initialization.h" | ||||||
26 | #include "clang/Sema/SemaInternal.h" | ||||||
27 | #include "llvm/ADT/SmallVector.h" | ||||||
28 | #include <set> | ||||||
29 | using namespace clang; | ||||||
30 | |||||||
31 | |||||||
32 | |||||||
33 | enum TryCastResult { | ||||||
34 | TC_NotApplicable, ///< The cast method is not applicable. | ||||||
35 | TC_Success, ///< The cast method is appropriate and successful. | ||||||
36 | TC_Extension, ///< The cast method is appropriate and accepted as a | ||||||
37 | ///< language extension. | ||||||
38 | TC_Failed ///< The cast method is appropriate, but failed. A | ||||||
39 | ///< diagnostic has been emitted. | ||||||
40 | }; | ||||||
41 | |||||||
42 | static bool isValidCast(TryCastResult TCR) { | ||||||
43 | return TCR == TC_Success || TCR == TC_Extension; | ||||||
44 | } | ||||||
45 | |||||||
46 | enum CastType { | ||||||
47 | CT_Const, ///< const_cast | ||||||
48 | CT_Static, ///< static_cast | ||||||
49 | CT_Reinterpret, ///< reinterpret_cast | ||||||
50 | CT_Dynamic, ///< dynamic_cast | ||||||
51 | CT_CStyle, ///< (Type)expr | ||||||
52 | CT_Functional, ///< Type(expr) | ||||||
53 | CT_Addrspace ///< addrspace_cast | ||||||
54 | }; | ||||||
55 | |||||||
56 | namespace { | ||||||
57 | struct CastOperation { | ||||||
58 | CastOperation(Sema &S, QualType destType, ExprResult src) | ||||||
59 | : Self(S), SrcExpr(src), DestType(destType), | ||||||
60 | ResultType(destType.getNonLValueExprType(S.Context)), | ||||||
61 | ValueKind(Expr::getValueKindForType(destType)), | ||||||
62 | Kind(CK_Dependent), IsARCUnbridgedCast(false) { | ||||||
63 | |||||||
64 | // C++ [expr.type]/8.2.2: | ||||||
65 | // If a pr-value initially has the type cv-T, where T is a | ||||||
66 | // cv-unqualified non-class, non-array type, the type of the | ||||||
67 | // expression is adjusted to T prior to any further analysis. | ||||||
68 | if (!S.Context.getLangOpts().ObjC && !DestType->isRecordType() && | ||||||
69 | !DestType->isArrayType()) { | ||||||
70 | DestType = DestType.getUnqualifiedType(); | ||||||
71 | } | ||||||
72 | |||||||
73 | if (const BuiltinType *placeholder = | ||||||
74 | src.get()->getType()->getAsPlaceholderType()) { | ||||||
75 | PlaceholderKind = placeholder->getKind(); | ||||||
76 | } else { | ||||||
77 | PlaceholderKind = (BuiltinType::Kind) 0; | ||||||
78 | } | ||||||
79 | } | ||||||
80 | |||||||
81 | Sema &Self; | ||||||
82 | ExprResult SrcExpr; | ||||||
83 | QualType DestType; | ||||||
84 | QualType ResultType; | ||||||
85 | ExprValueKind ValueKind; | ||||||
86 | CastKind Kind; | ||||||
87 | BuiltinType::Kind PlaceholderKind; | ||||||
88 | CXXCastPath BasePath; | ||||||
89 | bool IsARCUnbridgedCast; | ||||||
90 | |||||||
91 | SourceRange OpRange; | ||||||
92 | SourceRange DestRange; | ||||||
93 | |||||||
94 | // Top-level semantics-checking routines. | ||||||
95 | void CheckConstCast(); | ||||||
96 | void CheckReinterpretCast(); | ||||||
97 | void CheckStaticCast(); | ||||||
98 | void CheckDynamicCast(); | ||||||
99 | void CheckCXXCStyleCast(bool FunctionalCast, bool ListInitialization); | ||||||
100 | void CheckCStyleCast(); | ||||||
101 | void CheckBuiltinBitCast(); | ||||||
102 | void CheckAddrspaceCast(); | ||||||
103 | |||||||
104 | void updatePartOfExplicitCastFlags(CastExpr *CE) { | ||||||
105 | // Walk down from the CE to the OrigSrcExpr, and mark all immediate | ||||||
106 | // ImplicitCastExpr's as being part of ExplicitCastExpr. The original CE | ||||||
107 | // (which is a ExplicitCastExpr), and the OrigSrcExpr are not touched. | ||||||
108 | for (; auto *ICE = dyn_cast<ImplicitCastExpr>(CE->getSubExpr()); CE = ICE) | ||||||
109 | ICE->setIsPartOfExplicitCast(true); | ||||||
110 | } | ||||||
111 | |||||||
112 | /// Complete an apparently-successful cast operation that yields | ||||||
113 | /// the given expression. | ||||||
114 | ExprResult complete(CastExpr *castExpr) { | ||||||
115 | // If this is an unbridged cast, wrap the result in an implicit | ||||||
116 | // cast that yields the unbridged-cast placeholder type. | ||||||
117 | if (IsARCUnbridgedCast) { | ||||||
118 | castExpr = ImplicitCastExpr::Create( | ||||||
119 | Self.Context, Self.Context.ARCUnbridgedCastTy, CK_Dependent, | ||||||
120 | castExpr, nullptr, castExpr->getValueKind(), | ||||||
121 | Self.CurFPFeatureOverrides()); | ||||||
122 | } | ||||||
123 | updatePartOfExplicitCastFlags(castExpr); | ||||||
124 | return castExpr; | ||||||
125 | } | ||||||
126 | |||||||
127 | // Internal convenience methods. | ||||||
128 | |||||||
129 | /// Try to handle the given placeholder expression kind. Return | ||||||
130 | /// true if the source expression has the appropriate placeholder | ||||||
131 | /// kind. A placeholder can only be claimed once. | ||||||
132 | bool claimPlaceholder(BuiltinType::Kind K) { | ||||||
133 | if (PlaceholderKind != K) return false; | ||||||
134 | |||||||
135 | PlaceholderKind = (BuiltinType::Kind) 0; | ||||||
136 | return true; | ||||||
137 | } | ||||||
138 | |||||||
139 | bool isPlaceholder() const { | ||||||
140 | return PlaceholderKind != 0; | ||||||
141 | } | ||||||
142 | bool isPlaceholder(BuiltinType::Kind K) const { | ||||||
143 | return PlaceholderKind == K; | ||||||
144 | } | ||||||
145 | |||||||
146 | // Language specific cast restrictions for address spaces. | ||||||
147 | void checkAddressSpaceCast(QualType SrcType, QualType DestType); | ||||||
148 | |||||||
149 | void checkCastAlign() { | ||||||
150 | Self.CheckCastAlign(SrcExpr.get(), DestType, OpRange); | ||||||
151 | } | ||||||
152 | |||||||
153 | void checkObjCConversion(Sema::CheckedConversionKind CCK) { | ||||||
154 | assert(Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers())(static_cast <bool> (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers ()) ? void (0) : __assert_fail ("Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers()" , "clang/lib/Sema/SemaCast.cpp", 154, __extension__ __PRETTY_FUNCTION__ )); | ||||||
155 | |||||||
156 | Expr *src = SrcExpr.get(); | ||||||
157 | if (Self.CheckObjCConversion(OpRange, DestType, src, CCK) == | ||||||
158 | Sema::ACR_unbridged) | ||||||
159 | IsARCUnbridgedCast = true; | ||||||
160 | SrcExpr = src; | ||||||
161 | } | ||||||
162 | |||||||
163 | /// Check for and handle non-overload placeholder expressions. | ||||||
164 | void checkNonOverloadPlaceholders() { | ||||||
165 | if (!isPlaceholder() || isPlaceholder(BuiltinType::Overload)) | ||||||
166 | return; | ||||||
167 | |||||||
168 | SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.get()); | ||||||
169 | if (SrcExpr.isInvalid()) | ||||||
170 | return; | ||||||
171 | PlaceholderKind = (BuiltinType::Kind) 0; | ||||||
172 | } | ||||||
173 | }; | ||||||
174 | |||||||
175 | void CheckNoDeref(Sema &S, const QualType FromType, const QualType ToType, | ||||||
176 | SourceLocation OpLoc) { | ||||||
177 | if (const auto *PtrType = dyn_cast<PointerType>(FromType)) { | ||||||
178 | if (PtrType->getPointeeType()->hasAttr(attr::NoDeref)) { | ||||||
179 | if (const auto *DestType = dyn_cast<PointerType>(ToType)) { | ||||||
180 | if (!DestType->getPointeeType()->hasAttr(attr::NoDeref)) { | ||||||
181 | S.Diag(OpLoc, diag::warn_noderef_to_dereferenceable_pointer); | ||||||
182 | } | ||||||
183 | } | ||||||
184 | } | ||||||
185 | } | ||||||
186 | } | ||||||
187 | |||||||
188 | struct CheckNoDerefRAII { | ||||||
189 | CheckNoDerefRAII(CastOperation &Op) : Op(Op) {} | ||||||
190 | ~CheckNoDerefRAII() { | ||||||
191 | if (!Op.SrcExpr.isInvalid()) | ||||||
192 | CheckNoDeref(Op.Self, Op.SrcExpr.get()->getType(), Op.ResultType, | ||||||
193 | Op.OpRange.getBegin()); | ||||||
194 | } | ||||||
195 | |||||||
196 | CastOperation &Op; | ||||||
197 | }; | ||||||
198 | } | ||||||
199 | |||||||
200 | static void DiagnoseCastQual(Sema &Self, const ExprResult &SrcExpr, | ||||||
201 | QualType DestType); | ||||||
202 | |||||||
203 | // The Try functions attempt a specific way of casting. If they succeed, they | ||||||
204 | // return TC_Success. If their way of casting is not appropriate for the given | ||||||
205 | // arguments, they return TC_NotApplicable and *may* set diag to a diagnostic | ||||||
206 | // to emit if no other way succeeds. If their way of casting is appropriate but | ||||||
207 | // fails, they return TC_Failed and *must* set diag; they can set it to 0 if | ||||||
208 | // they emit a specialized diagnostic. | ||||||
209 | // All diagnostics returned by these functions must expect the same three | ||||||
210 | // arguments: | ||||||
211 | // %0: Cast Type (a value from the CastType enumeration) | ||||||
212 | // %1: Source Type | ||||||
213 | // %2: Destination Type | ||||||
214 | static TryCastResult TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, | ||||||
215 | QualType DestType, bool CStyle, | ||||||
216 | CastKind &Kind, | ||||||
217 | CXXCastPath &BasePath, | ||||||
218 | unsigned &msg); | ||||||
219 | static TryCastResult TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, | ||||||
220 | QualType DestType, bool CStyle, | ||||||
221 | SourceRange OpRange, | ||||||
222 | unsigned &msg, | ||||||
223 | CastKind &Kind, | ||||||
224 | CXXCastPath &BasePath); | ||||||
225 | static TryCastResult TryStaticPointerDowncast(Sema &Self, QualType SrcType, | ||||||
226 | QualType DestType, bool CStyle, | ||||||
227 | SourceRange OpRange, | ||||||
228 | unsigned &msg, | ||||||
229 | CastKind &Kind, | ||||||
230 | CXXCastPath &BasePath); | ||||||
231 | static TryCastResult TryStaticDowncast(Sema &Self, CanQualType SrcType, | ||||||
232 | CanQualType DestType, bool CStyle, | ||||||
233 | SourceRange OpRange, | ||||||
234 | QualType OrigSrcType, | ||||||
235 | QualType OrigDestType, unsigned &msg, | ||||||
236 | CastKind &Kind, | ||||||
237 | CXXCastPath &BasePath); | ||||||
238 | static TryCastResult TryStaticMemberPointerUpcast(Sema &Self, ExprResult &SrcExpr, | ||||||
239 | QualType SrcType, | ||||||
240 | QualType DestType,bool CStyle, | ||||||
241 | SourceRange OpRange, | ||||||
242 | unsigned &msg, | ||||||
243 | CastKind &Kind, | ||||||
244 | CXXCastPath &BasePath); | ||||||
245 | |||||||
246 | static TryCastResult TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr, | ||||||
247 | QualType DestType, | ||||||
248 | Sema::CheckedConversionKind CCK, | ||||||
249 | SourceRange OpRange, | ||||||
250 | unsigned &msg, CastKind &Kind, | ||||||
251 | bool ListInitialization); | ||||||
252 | static TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr, | ||||||
253 | QualType DestType, | ||||||
254 | Sema::CheckedConversionKind CCK, | ||||||
255 | SourceRange OpRange, | ||||||
256 | unsigned &msg, CastKind &Kind, | ||||||
257 | CXXCastPath &BasePath, | ||||||
258 | bool ListInitialization); | ||||||
259 | static TryCastResult TryConstCast(Sema &Self, ExprResult &SrcExpr, | ||||||
260 | QualType DestType, bool CStyle, | ||||||
261 | unsigned &msg); | ||||||
262 | static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr, | ||||||
263 | QualType DestType, bool CStyle, | ||||||
264 | SourceRange OpRange, unsigned &msg, | ||||||
265 | CastKind &Kind); | ||||||
266 | static TryCastResult TryAddressSpaceCast(Sema &Self, ExprResult &SrcExpr, | ||||||
267 | QualType DestType, bool CStyle, | ||||||
268 | unsigned &msg, CastKind &Kind); | ||||||
269 | |||||||
270 | /// ActOnCXXNamedCast - Parse | ||||||
271 | /// {dynamic,static,reinterpret,const,addrspace}_cast's. | ||||||
272 | ExprResult | ||||||
273 | Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind, | ||||||
274 | SourceLocation LAngleBracketLoc, Declarator &D, | ||||||
275 | SourceLocation RAngleBracketLoc, | ||||||
276 | SourceLocation LParenLoc, Expr *E, | ||||||
277 | SourceLocation RParenLoc) { | ||||||
278 | |||||||
279 | assert(!D.isInvalidType())(static_cast <bool> (!D.isInvalidType()) ? void (0) : __assert_fail ("!D.isInvalidType()", "clang/lib/Sema/SemaCast.cpp", 279, __extension__ __PRETTY_FUNCTION__)); | ||||||
280 | |||||||
281 | TypeSourceInfo *TInfo = GetTypeForDeclaratorCast(D, E->getType()); | ||||||
282 | if (D.isInvalidType()) | ||||||
283 | return ExprError(); | ||||||
284 | |||||||
285 | if (getLangOpts().CPlusPlus) { | ||||||
286 | // Check that there are no default arguments (C++ only). | ||||||
287 | CheckExtraCXXDefaultArguments(D); | ||||||
288 | } | ||||||
289 | |||||||
290 | return BuildCXXNamedCast(OpLoc, Kind, TInfo, E, | ||||||
291 | SourceRange(LAngleBracketLoc, RAngleBracketLoc), | ||||||
292 | SourceRange(LParenLoc, RParenLoc)); | ||||||
293 | } | ||||||
294 | |||||||
295 | ExprResult | ||||||
296 | Sema::BuildCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind, | ||||||
297 | TypeSourceInfo *DestTInfo, Expr *E, | ||||||
298 | SourceRange AngleBrackets, SourceRange Parens) { | ||||||
299 | ExprResult Ex = E; | ||||||
300 | QualType DestType = DestTInfo->getType(); | ||||||
301 | |||||||
302 | // If the type is dependent, we won't do the semantic analysis now. | ||||||
303 | bool TypeDependent = | ||||||
304 | DestType->isDependentType() || Ex.get()->isTypeDependent(); | ||||||
305 | |||||||
306 | CastOperation Op(*this, DestType, E); | ||||||
307 | Op.OpRange = SourceRange(OpLoc, Parens.getEnd()); | ||||||
308 | Op.DestRange = AngleBrackets; | ||||||
309 | |||||||
310 | switch (Kind) { | ||||||
311 | default: llvm_unreachable("Unknown C++ cast!")::llvm::llvm_unreachable_internal("Unknown C++ cast!", "clang/lib/Sema/SemaCast.cpp" , 311); | ||||||
312 | |||||||
313 | case tok::kw_addrspace_cast: | ||||||
314 | if (!TypeDependent) { | ||||||
315 | Op.CheckAddrspaceCast(); | ||||||
316 | if (Op.SrcExpr.isInvalid()) | ||||||
317 | return ExprError(); | ||||||
318 | } | ||||||
319 | return Op.complete(CXXAddrspaceCastExpr::Create( | ||||||
320 | Context, Op.ResultType, Op.ValueKind, Op.Kind, Op.SrcExpr.get(), | ||||||
321 | DestTInfo, OpLoc, Parens.getEnd(), AngleBrackets)); | ||||||
322 | |||||||
323 | case tok::kw_const_cast: | ||||||
324 | if (!TypeDependent) { | ||||||
325 | Op.CheckConstCast(); | ||||||
326 | if (Op.SrcExpr.isInvalid()) | ||||||
327 | return ExprError(); | ||||||
328 | DiscardMisalignedMemberAddress(DestType.getTypePtr(), E); | ||||||
329 | } | ||||||
330 | return Op.complete(CXXConstCastExpr::Create(Context, Op.ResultType, | ||||||
331 | Op.ValueKind, Op.SrcExpr.get(), DestTInfo, | ||||||
332 | OpLoc, Parens.getEnd(), | ||||||
333 | AngleBrackets)); | ||||||
334 | |||||||
335 | case tok::kw_dynamic_cast: { | ||||||
336 | // dynamic_cast is not supported in C++ for OpenCL. | ||||||
337 | if (getLangOpts().OpenCLCPlusPlus) { | ||||||
338 | return ExprError(Diag(OpLoc, diag::err_openclcxx_not_supported) | ||||||
339 | << "dynamic_cast"); | ||||||
340 | } | ||||||
341 | |||||||
342 | if (!TypeDependent) { | ||||||
343 | Op.CheckDynamicCast(); | ||||||
344 | if (Op.SrcExpr.isInvalid()) | ||||||
345 | return ExprError(); | ||||||
346 | } | ||||||
347 | return Op.complete(CXXDynamicCastExpr::Create(Context, Op.ResultType, | ||||||
348 | Op.ValueKind, Op.Kind, Op.SrcExpr.get(), | ||||||
349 | &Op.BasePath, DestTInfo, | ||||||
350 | OpLoc, Parens.getEnd(), | ||||||
351 | AngleBrackets)); | ||||||
352 | } | ||||||
353 | case tok::kw_reinterpret_cast: { | ||||||
354 | if (!TypeDependent) { | ||||||
355 | Op.CheckReinterpretCast(); | ||||||
356 | if (Op.SrcExpr.isInvalid()) | ||||||
357 | return ExprError(); | ||||||
358 | DiscardMisalignedMemberAddress(DestType.getTypePtr(), E); | ||||||
359 | } | ||||||
360 | return Op.complete(CXXReinterpretCastExpr::Create(Context, Op.ResultType, | ||||||
361 | Op.ValueKind, Op.Kind, Op.SrcExpr.get(), | ||||||
362 | nullptr, DestTInfo, OpLoc, | ||||||
363 | Parens.getEnd(), | ||||||
364 | AngleBrackets)); | ||||||
365 | } | ||||||
366 | case tok::kw_static_cast: { | ||||||
367 | if (!TypeDependent) { | ||||||
368 | Op.CheckStaticCast(); | ||||||
369 | if (Op.SrcExpr.isInvalid()) | ||||||
370 | return ExprError(); | ||||||
371 | DiscardMisalignedMemberAddress(DestType.getTypePtr(), E); | ||||||
372 | } | ||||||
373 | |||||||
374 | return Op.complete(CXXStaticCastExpr::Create( | ||||||
375 | Context, Op.ResultType, Op.ValueKind, Op.Kind, Op.SrcExpr.get(), | ||||||
376 | &Op.BasePath, DestTInfo, CurFPFeatureOverrides(), OpLoc, | ||||||
377 | Parens.getEnd(), AngleBrackets)); | ||||||
378 | } | ||||||
379 | } | ||||||
380 | } | ||||||
381 | |||||||
382 | ExprResult Sema::ActOnBuiltinBitCastExpr(SourceLocation KWLoc, Declarator &D, | ||||||
383 | ExprResult Operand, | ||||||
384 | SourceLocation RParenLoc) { | ||||||
385 | assert(!D.isInvalidType())(static_cast <bool> (!D.isInvalidType()) ? void (0) : __assert_fail ("!D.isInvalidType()", "clang/lib/Sema/SemaCast.cpp", 385, __extension__ __PRETTY_FUNCTION__)); | ||||||
386 | |||||||
387 | TypeSourceInfo *TInfo = GetTypeForDeclaratorCast(D, Operand.get()->getType()); | ||||||
388 | if (D.isInvalidType()) | ||||||
389 | return ExprError(); | ||||||
390 | |||||||
391 | return BuildBuiltinBitCastExpr(KWLoc, TInfo, Operand.get(), RParenLoc); | ||||||
392 | } | ||||||
393 | |||||||
394 | ExprResult Sema::BuildBuiltinBitCastExpr(SourceLocation KWLoc, | ||||||
395 | TypeSourceInfo *TSI, Expr *Operand, | ||||||
396 | SourceLocation RParenLoc) { | ||||||
397 | CastOperation Op(*this, TSI->getType(), Operand); | ||||||
398 | Op.OpRange = SourceRange(KWLoc, RParenLoc); | ||||||
399 | TypeLoc TL = TSI->getTypeLoc(); | ||||||
400 | Op.DestRange = SourceRange(TL.getBeginLoc(), TL.getEndLoc()); | ||||||
401 | |||||||
402 | if (!Operand->isTypeDependent() && !TSI->getType()->isDependentType()) { | ||||||
403 | Op.CheckBuiltinBitCast(); | ||||||
404 | if (Op.SrcExpr.isInvalid()) | ||||||
405 | return ExprError(); | ||||||
406 | } | ||||||
407 | |||||||
408 | BuiltinBitCastExpr *BCE = | ||||||
409 | new (Context) BuiltinBitCastExpr(Op.ResultType, Op.ValueKind, Op.Kind, | ||||||
410 | Op.SrcExpr.get(), TSI, KWLoc, RParenLoc); | ||||||
411 | return Op.complete(BCE); | ||||||
412 | } | ||||||
413 | |||||||
414 | /// Try to diagnose a failed overloaded cast. Returns true if | ||||||
415 | /// diagnostics were emitted. | ||||||
416 | static bool tryDiagnoseOverloadedCast(Sema &S, CastType CT, | ||||||
417 | SourceRange range, Expr *src, | ||||||
418 | QualType destType, | ||||||
419 | bool listInitialization) { | ||||||
420 | switch (CT) { | ||||||
421 | // These cast kinds don't consider user-defined conversions. | ||||||
422 | case CT_Const: | ||||||
423 | case CT_Reinterpret: | ||||||
424 | case CT_Dynamic: | ||||||
425 | case CT_Addrspace: | ||||||
426 | return false; | ||||||
427 | |||||||
428 | // These do. | ||||||
429 | case CT_Static: | ||||||
430 | case CT_CStyle: | ||||||
431 | case CT_Functional: | ||||||
432 | break; | ||||||
433 | } | ||||||
434 | |||||||
435 | QualType srcType = src->getType(); | ||||||
436 | if (!destType->isRecordType() && !srcType->isRecordType()) | ||||||
437 | return false; | ||||||
438 | |||||||
439 | InitializedEntity entity = InitializedEntity::InitializeTemporary(destType); | ||||||
440 | InitializationKind initKind | ||||||
441 | = (CT == CT_CStyle)? InitializationKind::CreateCStyleCast(range.getBegin(), | ||||||
442 | range, listInitialization) | ||||||
443 | : (CT == CT_Functional)? InitializationKind::CreateFunctionalCast(range, | ||||||
444 | listInitialization) | ||||||
445 | : InitializationKind::CreateCast(/*type range?*/ range); | ||||||
446 | InitializationSequence sequence(S, entity, initKind, src); | ||||||
447 | |||||||
448 | assert(sequence.Failed() && "initialization succeeded on second try?")(static_cast <bool> (sequence.Failed() && "initialization succeeded on second try?" ) ? void (0) : __assert_fail ("sequence.Failed() && \"initialization succeeded on second try?\"" , "clang/lib/Sema/SemaCast.cpp", 448, __extension__ __PRETTY_FUNCTION__ )); | ||||||
449 | switch (sequence.getFailureKind()) { | ||||||
450 | default: return false; | ||||||
451 | |||||||
452 | case InitializationSequence::FK_ConstructorOverloadFailed: | ||||||
453 | case InitializationSequence::FK_UserConversionOverloadFailed: | ||||||
454 | break; | ||||||
455 | } | ||||||
456 | |||||||
457 | OverloadCandidateSet &candidates = sequence.getFailedCandidateSet(); | ||||||
458 | |||||||
459 | unsigned msg = 0; | ||||||
460 | OverloadCandidateDisplayKind howManyCandidates = OCD_AllCandidates; | ||||||
461 | |||||||
462 | switch (sequence.getFailedOverloadResult()) { | ||||||
463 | case OR_Success: llvm_unreachable("successful failed overload")::llvm::llvm_unreachable_internal("successful failed overload" , "clang/lib/Sema/SemaCast.cpp", 463); | ||||||
464 | case OR_No_Viable_Function: | ||||||
465 | if (candidates.empty()) | ||||||
466 | msg = diag::err_ovl_no_conversion_in_cast; | ||||||
467 | else | ||||||
468 | msg = diag::err_ovl_no_viable_conversion_in_cast; | ||||||
469 | howManyCandidates = OCD_AllCandidates; | ||||||
470 | break; | ||||||
471 | |||||||
472 | case OR_Ambiguous: | ||||||
473 | msg = diag::err_ovl_ambiguous_conversion_in_cast; | ||||||
474 | howManyCandidates = OCD_AmbiguousCandidates; | ||||||
475 | break; | ||||||
476 | |||||||
477 | case OR_Deleted: | ||||||
478 | msg = diag::err_ovl_deleted_conversion_in_cast; | ||||||
479 | howManyCandidates = OCD_ViableCandidates; | ||||||
480 | break; | ||||||
481 | } | ||||||
482 | |||||||
483 | candidates.NoteCandidates( | ||||||
484 | PartialDiagnosticAt(range.getBegin(), | ||||||
485 | S.PDiag(msg) << CT << srcType << destType << range | ||||||
486 | << src->getSourceRange()), | ||||||
487 | S, howManyCandidates, src); | ||||||
488 | |||||||
489 | return true; | ||||||
490 | } | ||||||
491 | |||||||
492 | /// Diagnose a failed cast. | ||||||
493 | static void diagnoseBadCast(Sema &S, unsigned msg, CastType castType, | ||||||
494 | SourceRange opRange, Expr *src, QualType destType, | ||||||
495 | bool listInitialization) { | ||||||
496 | if (msg == diag::err_bad_cxx_cast_generic && | ||||||
497 | tryDiagnoseOverloadedCast(S, castType, opRange, src, destType, | ||||||
498 | listInitialization)) | ||||||
499 | return; | ||||||
500 | |||||||
501 | S.Diag(opRange.getBegin(), msg) << castType | ||||||
502 | << src->getType() << destType << opRange << src->getSourceRange(); | ||||||
503 | |||||||
504 | // Detect if both types are (ptr to) class, and note any incompleteness. | ||||||
505 | int DifferentPtrness = 0; | ||||||
506 | QualType From = destType; | ||||||
507 | if (auto Ptr = From->getAs<PointerType>()) { | ||||||
508 | From = Ptr->getPointeeType(); | ||||||
509 | DifferentPtrness++; | ||||||
510 | } | ||||||
511 | QualType To = src->getType(); | ||||||
512 | if (auto Ptr = To->getAs<PointerType>()) { | ||||||
513 | To = Ptr->getPointeeType(); | ||||||
514 | DifferentPtrness--; | ||||||
515 | } | ||||||
516 | if (!DifferentPtrness) { | ||||||
517 | auto RecFrom = From->getAs<RecordType>(); | ||||||
518 | auto RecTo = To->getAs<RecordType>(); | ||||||
519 | if (RecFrom && RecTo) { | ||||||
520 | auto DeclFrom = RecFrom->getAsCXXRecordDecl(); | ||||||
521 | if (!DeclFrom->isCompleteDefinition()) | ||||||
522 | S.Diag(DeclFrom->getLocation(), diag::note_type_incomplete) << DeclFrom; | ||||||
523 | auto DeclTo = RecTo->getAsCXXRecordDecl(); | ||||||
524 | if (!DeclTo->isCompleteDefinition()) | ||||||
525 | S.Diag(DeclTo->getLocation(), diag::note_type_incomplete) << DeclTo; | ||||||
526 | } | ||||||
527 | } | ||||||
528 | } | ||||||
529 | |||||||
530 | namespace { | ||||||
531 | /// The kind of unwrapping we did when determining whether a conversion casts | ||||||
532 | /// away constness. | ||||||
533 | enum CastAwayConstnessKind { | ||||||
534 | /// The conversion does not cast away constness. | ||||||
535 | CACK_None = 0, | ||||||
536 | /// We unwrapped similar types. | ||||||
537 | CACK_Similar = 1, | ||||||
538 | /// We unwrapped dissimilar types with similar representations (eg, a pointer | ||||||
539 | /// versus an Objective-C object pointer). | ||||||
540 | CACK_SimilarKind = 2, | ||||||
541 | /// We unwrapped representationally-unrelated types, such as a pointer versus | ||||||
542 | /// a pointer-to-member. | ||||||
543 | CACK_Incoherent = 3, | ||||||
544 | }; | ||||||
545 | } | ||||||
546 | |||||||
547 | /// Unwrap one level of types for CastsAwayConstness. | ||||||
548 | /// | ||||||
549 | /// Like Sema::UnwrapSimilarTypes, this removes one level of indirection from | ||||||
550 | /// both types, provided that they're both pointer-like or array-like. Unlike | ||||||
551 | /// the Sema function, doesn't care if the unwrapped pieces are related. | ||||||
552 | /// | ||||||
553 | /// This function may remove additional levels as necessary for correctness: | ||||||
554 | /// the resulting T1 is unwrapped sufficiently that it is never an array type, | ||||||
555 | /// so that its qualifiers can be directly compared to those of T2 (which will | ||||||
556 | /// have the combined set of qualifiers from all indermediate levels of T2), | ||||||
557 | /// as (effectively) required by [expr.const.cast]p7 replacing T1's qualifiers | ||||||
558 | /// with those from T2. | ||||||
559 | static CastAwayConstnessKind | ||||||
560 | unwrapCastAwayConstnessLevel(ASTContext &Context, QualType &T1, QualType &T2) { | ||||||
561 | enum { None, Ptr, MemPtr, BlockPtr, Array }; | ||||||
562 | auto Classify = [](QualType T) { | ||||||
563 | if (T->isAnyPointerType()) return Ptr; | ||||||
564 | if (T->isMemberPointerType()) return MemPtr; | ||||||
565 | if (T->isBlockPointerType()) return BlockPtr; | ||||||
566 | // We somewhat-arbitrarily don't look through VLA types here. This is at | ||||||
567 | // least consistent with the behavior of UnwrapSimilarTypes. | ||||||
568 | if (T->isConstantArrayType() || T->isIncompleteArrayType()) return Array; | ||||||
569 | return None; | ||||||
570 | }; | ||||||
571 | |||||||
572 | auto Unwrap = [&](QualType T) { | ||||||
573 | if (auto *AT = Context.getAsArrayType(T)) | ||||||
574 | return AT->getElementType(); | ||||||
575 | return T->getPointeeType(); | ||||||
576 | }; | ||||||
577 | |||||||
578 | CastAwayConstnessKind Kind; | ||||||
579 | |||||||
580 | if (T2->isReferenceType()) { | ||||||
581 | // Special case: if the destination type is a reference type, unwrap it as | ||||||
582 | // the first level. (The source will have been an lvalue expression in this | ||||||
583 | // case, so there is no corresponding "reference to" in T1 to remove.) This | ||||||
584 | // simulates removing a "pointer to" from both sides. | ||||||
585 | T2 = T2->getPointeeType(); | ||||||
586 | Kind = CastAwayConstnessKind::CACK_Similar; | ||||||
587 | } else if (Context.UnwrapSimilarTypes(T1, T2)) { | ||||||
588 | Kind = CastAwayConstnessKind::CACK_Similar; | ||||||
589 | } else { | ||||||
590 | // Try unwrapping mismatching levels. | ||||||
591 | int T1Class = Classify(T1); | ||||||
592 | if (T1Class == None) | ||||||
593 | return CastAwayConstnessKind::CACK_None; | ||||||
594 | |||||||
595 | int T2Class = Classify(T2); | ||||||
596 | if (T2Class == None) | ||||||
597 | return CastAwayConstnessKind::CACK_None; | ||||||
598 | |||||||
599 | T1 = Unwrap(T1); | ||||||
600 | T2 = Unwrap(T2); | ||||||
601 | Kind = T1Class == T2Class ? CastAwayConstnessKind::CACK_SimilarKind | ||||||
602 | : CastAwayConstnessKind::CACK_Incoherent; | ||||||
603 | } | ||||||
604 | |||||||
605 | // We've unwrapped at least one level. If the resulting T1 is a (possibly | ||||||
606 | // multidimensional) array type, any qualifier on any matching layer of | ||||||
607 | // T2 is considered to correspond to T1. Decompose down to the element | ||||||
608 | // type of T1 so that we can compare properly. | ||||||
609 | while (true) { | ||||||
610 | Context.UnwrapSimilarArrayTypes(T1, T2); | ||||||
611 | |||||||
612 | if (Classify(T1) != Array) | ||||||
613 | break; | ||||||
614 | |||||||
615 | auto T2Class = Classify(T2); | ||||||
616 | if (T2Class == None) | ||||||
617 | break; | ||||||
618 | |||||||
619 | if (T2Class != Array) | ||||||
620 | Kind = CastAwayConstnessKind::CACK_Incoherent; | ||||||
621 | else if (Kind != CastAwayConstnessKind::CACK_Incoherent) | ||||||
622 | Kind = CastAwayConstnessKind::CACK_SimilarKind; | ||||||
623 | |||||||
624 | T1 = Unwrap(T1); | ||||||
625 | T2 = Unwrap(T2).withCVRQualifiers(T2.getCVRQualifiers()); | ||||||
626 | } | ||||||
627 | |||||||
628 | return Kind; | ||||||
629 | } | ||||||
630 | |||||||
631 | /// Check if the pointer conversion from SrcType to DestType casts away | ||||||
632 | /// constness as defined in C++ [expr.const.cast]. This is used by the cast | ||||||
633 | /// checkers. Both arguments must denote pointer (possibly to member) types. | ||||||
634 | /// | ||||||
635 | /// \param CheckCVR Whether to check for const/volatile/restrict qualifiers. | ||||||
636 | /// \param CheckObjCLifetime Whether to check Objective-C lifetime qualifiers. | ||||||
637 | static CastAwayConstnessKind | ||||||
638 | CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType, | ||||||
639 | bool CheckCVR, bool CheckObjCLifetime, | ||||||
640 | QualType *TheOffendingSrcType = nullptr, | ||||||
641 | QualType *TheOffendingDestType = nullptr, | ||||||
642 | Qualifiers *CastAwayQualifiers = nullptr) { | ||||||
643 | // If the only checking we care about is for Objective-C lifetime qualifiers, | ||||||
644 | // and we're not in ObjC mode, there's nothing to check. | ||||||
645 | if (!CheckCVR && CheckObjCLifetime && !Self.Context.getLangOpts().ObjC) | ||||||
646 | return CastAwayConstnessKind::CACK_None; | ||||||
647 | |||||||
648 | if (!DestType->isReferenceType()) { | ||||||
649 | assert((SrcType->isAnyPointerType() || SrcType->isMemberPointerType() ||(static_cast <bool> ((SrcType->isAnyPointerType() || SrcType->isMemberPointerType() || SrcType->isBlockPointerType ()) && "Source type is not pointer or pointer to member." ) ? void (0) : __assert_fail ("(SrcType->isAnyPointerType() || SrcType->isMemberPointerType() || SrcType->isBlockPointerType()) && \"Source type is not pointer or pointer to member.\"" , "clang/lib/Sema/SemaCast.cpp", 651, __extension__ __PRETTY_FUNCTION__ )) | ||||||
650 | SrcType->isBlockPointerType()) &&(static_cast <bool> ((SrcType->isAnyPointerType() || SrcType->isMemberPointerType() || SrcType->isBlockPointerType ()) && "Source type is not pointer or pointer to member." ) ? void (0) : __assert_fail ("(SrcType->isAnyPointerType() || SrcType->isMemberPointerType() || SrcType->isBlockPointerType()) && \"Source type is not pointer or pointer to member.\"" , "clang/lib/Sema/SemaCast.cpp", 651, __extension__ __PRETTY_FUNCTION__ )) | ||||||
651 | "Source type is not pointer or pointer to member.")(static_cast <bool> ((SrcType->isAnyPointerType() || SrcType->isMemberPointerType() || SrcType->isBlockPointerType ()) && "Source type is not pointer or pointer to member." ) ? void (0) : __assert_fail ("(SrcType->isAnyPointerType() || SrcType->isMemberPointerType() || SrcType->isBlockPointerType()) && \"Source type is not pointer or pointer to member.\"" , "clang/lib/Sema/SemaCast.cpp", 651, __extension__ __PRETTY_FUNCTION__ )); | ||||||
652 | assert((DestType->isAnyPointerType() || DestType->isMemberPointerType() ||(static_cast <bool> ((DestType->isAnyPointerType() || DestType->isMemberPointerType() || DestType->isBlockPointerType ()) && "Destination type is not pointer or pointer to member." ) ? void (0) : __assert_fail ("(DestType->isAnyPointerType() || DestType->isMemberPointerType() || DestType->isBlockPointerType()) && \"Destination type is not pointer or pointer to member.\"" , "clang/lib/Sema/SemaCast.cpp", 654, __extension__ __PRETTY_FUNCTION__ )) | ||||||
653 | DestType->isBlockPointerType()) &&(static_cast <bool> ((DestType->isAnyPointerType() || DestType->isMemberPointerType() || DestType->isBlockPointerType ()) && "Destination type is not pointer or pointer to member." ) ? void (0) : __assert_fail ("(DestType->isAnyPointerType() || DestType->isMemberPointerType() || DestType->isBlockPointerType()) && \"Destination type is not pointer or pointer to member.\"" , "clang/lib/Sema/SemaCast.cpp", 654, __extension__ __PRETTY_FUNCTION__ )) | ||||||
654 | "Destination type is not pointer or pointer to member.")(static_cast <bool> ((DestType->isAnyPointerType() || DestType->isMemberPointerType() || DestType->isBlockPointerType ()) && "Destination type is not pointer or pointer to member." ) ? void (0) : __assert_fail ("(DestType->isAnyPointerType() || DestType->isMemberPointerType() || DestType->isBlockPointerType()) && \"Destination type is not pointer or pointer to member.\"" , "clang/lib/Sema/SemaCast.cpp", 654, __extension__ __PRETTY_FUNCTION__ )); | ||||||
655 | } | ||||||
656 | |||||||
657 | QualType UnwrappedSrcType = Self.Context.getCanonicalType(SrcType), | ||||||
658 | UnwrappedDestType = Self.Context.getCanonicalType(DestType); | ||||||
659 | |||||||
660 | // Find the qualifiers. We only care about cvr-qualifiers for the | ||||||
661 | // purpose of this check, because other qualifiers (address spaces, | ||||||
662 | // Objective-C GC, etc.) are part of the type's identity. | ||||||
663 | QualType PrevUnwrappedSrcType = UnwrappedSrcType; | ||||||
664 | QualType PrevUnwrappedDestType = UnwrappedDestType; | ||||||
665 | auto WorstKind = CastAwayConstnessKind::CACK_Similar; | ||||||
666 | bool AllConstSoFar = true; | ||||||
667 | while (auto Kind = unwrapCastAwayConstnessLevel( | ||||||
668 | Self.Context, UnwrappedSrcType, UnwrappedDestType)) { | ||||||
669 | // Track the worst kind of unwrap we needed to do before we found a | ||||||
670 | // problem. | ||||||
671 | if (Kind > WorstKind) | ||||||
672 | WorstKind = Kind; | ||||||
673 | |||||||
674 | // Determine the relevant qualifiers at this level. | ||||||
675 | Qualifiers SrcQuals, DestQuals; | ||||||
676 | Self.Context.getUnqualifiedArrayType(UnwrappedSrcType, SrcQuals); | ||||||
677 | Self.Context.getUnqualifiedArrayType(UnwrappedDestType, DestQuals); | ||||||
678 | |||||||
679 | // We do not meaningfully track object const-ness of Objective-C object | ||||||
680 | // types. Remove const from the source type if either the source or | ||||||
681 | // the destination is an Objective-C object type. | ||||||
682 | if (UnwrappedSrcType->isObjCObjectType() || | ||||||
683 | UnwrappedDestType->isObjCObjectType()) | ||||||
684 | SrcQuals.removeConst(); | ||||||
685 | |||||||
686 | if (CheckCVR) { | ||||||
687 | Qualifiers SrcCvrQuals = | ||||||
688 | Qualifiers::fromCVRMask(SrcQuals.getCVRQualifiers()); | ||||||
689 | Qualifiers DestCvrQuals = | ||||||
690 | Qualifiers::fromCVRMask(DestQuals.getCVRQualifiers()); | ||||||
691 | |||||||
692 | if (SrcCvrQuals != DestCvrQuals) { | ||||||
693 | if (CastAwayQualifiers) | ||||||
694 | *CastAwayQualifiers = SrcCvrQuals - DestCvrQuals; | ||||||
695 | |||||||
696 | // If we removed a cvr-qualifier, this is casting away 'constness'. | ||||||
697 | if (!DestCvrQuals.compatiblyIncludes(SrcCvrQuals)) { | ||||||
698 | if (TheOffendingSrcType) | ||||||
699 | *TheOffendingSrcType = PrevUnwrappedSrcType; | ||||||
700 | if (TheOffendingDestType) | ||||||
701 | *TheOffendingDestType = PrevUnwrappedDestType; | ||||||
702 | return WorstKind; | ||||||
703 | } | ||||||
704 | |||||||
705 | // If any prior level was not 'const', this is also casting away | ||||||
706 | // 'constness'. We noted the outermost type missing a 'const' already. | ||||||
707 | if (!AllConstSoFar) | ||||||
708 | return WorstKind; | ||||||
709 | } | ||||||
710 | } | ||||||
711 | |||||||
712 | if (CheckObjCLifetime && | ||||||
713 | !DestQuals.compatiblyIncludesObjCLifetime(SrcQuals)) | ||||||
714 | return WorstKind; | ||||||
715 | |||||||
716 | // If we found our first non-const-qualified type, this may be the place | ||||||
717 | // where things start to go wrong. | ||||||
718 | if (AllConstSoFar && !DestQuals.hasConst()) { | ||||||
719 | AllConstSoFar = false; | ||||||
720 | if (TheOffendingSrcType) | ||||||
721 | *TheOffendingSrcType = PrevUnwrappedSrcType; | ||||||
722 | if (TheOffendingDestType) | ||||||
723 | *TheOffendingDestType = PrevUnwrappedDestType; | ||||||
724 | } | ||||||
725 | |||||||
726 | PrevUnwrappedSrcType = UnwrappedSrcType; | ||||||
727 | PrevUnwrappedDestType = UnwrappedDestType; | ||||||
728 | } | ||||||
729 | |||||||
730 | return CastAwayConstnessKind::CACK_None; | ||||||
731 | } | ||||||
732 | |||||||
733 | static TryCastResult getCastAwayConstnessCastKind(CastAwayConstnessKind CACK, | ||||||
734 | unsigned &DiagID) { | ||||||
735 | switch (CACK) { | ||||||
736 | case CastAwayConstnessKind::CACK_None: | ||||||
737 | llvm_unreachable("did not cast away constness")::llvm::llvm_unreachable_internal("did not cast away constness" , "clang/lib/Sema/SemaCast.cpp", 737); | ||||||
738 | |||||||
739 | case CastAwayConstnessKind::CACK_Similar: | ||||||
740 | // FIXME: Accept these as an extension too? | ||||||
741 | case CastAwayConstnessKind::CACK_SimilarKind: | ||||||
742 | DiagID = diag::err_bad_cxx_cast_qualifiers_away; | ||||||
743 | return TC_Failed; | ||||||
744 | |||||||
745 | case CastAwayConstnessKind::CACK_Incoherent: | ||||||
746 | DiagID = diag::ext_bad_cxx_cast_qualifiers_away_incoherent; | ||||||
747 | return TC_Extension; | ||||||
748 | } | ||||||
749 | |||||||
750 | llvm_unreachable("unexpected cast away constness kind")::llvm::llvm_unreachable_internal("unexpected cast away constness kind" , "clang/lib/Sema/SemaCast.cpp", 750); | ||||||
751 | } | ||||||
752 | |||||||
753 | /// CheckDynamicCast - Check that a dynamic_cast\<DestType\>(SrcExpr) is valid. | ||||||
754 | /// Refer to C++ 5.2.7 for details. Dynamic casts are used mostly for runtime- | ||||||
755 | /// checked downcasts in class hierarchies. | ||||||
756 | void CastOperation::CheckDynamicCast() { | ||||||
757 | CheckNoDerefRAII NoderefCheck(*this); | ||||||
758 | |||||||
759 | if (ValueKind == VK_PRValue) | ||||||
760 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get()); | ||||||
761 | else if (isPlaceholder()) | ||||||
762 | SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.get()); | ||||||
763 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error | ||||||
764 | return; | ||||||
765 | |||||||
766 | QualType OrigSrcType = SrcExpr.get()->getType(); | ||||||
767 | QualType DestType = Self.Context.getCanonicalType(this->DestType); | ||||||
768 | |||||||
769 | // C++ 5.2.7p1: T shall be a pointer or reference to a complete class type, | ||||||
770 | // or "pointer to cv void". | ||||||
771 | |||||||
772 | QualType DestPointee; | ||||||
773 | const PointerType *DestPointer = DestType->getAs<PointerType>(); | ||||||
774 | const ReferenceType *DestReference = nullptr; | ||||||
775 | if (DestPointer) { | ||||||
776 | DestPointee = DestPointer->getPointeeType(); | ||||||
777 | } else if ((DestReference = DestType->getAs<ReferenceType>())) { | ||||||
778 | DestPointee = DestReference->getPointeeType(); | ||||||
779 | } else { | ||||||
780 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr) | ||||||
781 | << this->DestType << DestRange; | ||||||
782 | SrcExpr = ExprError(); | ||||||
783 | return; | ||||||
784 | } | ||||||
785 | |||||||
786 | const RecordType *DestRecord = DestPointee->getAs<RecordType>(); | ||||||
787 | if (DestPointee->isVoidType()) { | ||||||
788 | assert(DestPointer && "Reference to void is not possible")(static_cast <bool> (DestPointer && "Reference to void is not possible" ) ? void (0) : __assert_fail ("DestPointer && \"Reference to void is not possible\"" , "clang/lib/Sema/SemaCast.cpp", 788, __extension__ __PRETTY_FUNCTION__ )); | ||||||
789 | } else if (DestRecord) { | ||||||
790 | if (Self.RequireCompleteType(OpRange.getBegin(), DestPointee, | ||||||
791 | diag::err_bad_cast_incomplete, | ||||||
792 | DestRange)) { | ||||||
793 | SrcExpr = ExprError(); | ||||||
794 | return; | ||||||
795 | } | ||||||
796 | } else { | ||||||
797 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class) | ||||||
798 | << DestPointee.getUnqualifiedType() << DestRange; | ||||||
799 | SrcExpr = ExprError(); | ||||||
800 | return; | ||||||
801 | } | ||||||
802 | |||||||
803 | // C++0x 5.2.7p2: If T is a pointer type, v shall be an rvalue of a pointer to | ||||||
804 | // complete class type, [...]. If T is an lvalue reference type, v shall be | ||||||
805 | // an lvalue of a complete class type, [...]. If T is an rvalue reference | ||||||
806 | // type, v shall be an expression having a complete class type, [...] | ||||||
807 | QualType SrcType = Self.Context.getCanonicalType(OrigSrcType); | ||||||
808 | QualType SrcPointee; | ||||||
809 | if (DestPointer) { | ||||||
810 | if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) { | ||||||
811 | SrcPointee = SrcPointer->getPointeeType(); | ||||||
812 | } else { | ||||||
813 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr) | ||||||
814 | << OrigSrcType << this->DestType << SrcExpr.get()->getSourceRange(); | ||||||
815 | SrcExpr = ExprError(); | ||||||
816 | return; | ||||||
817 | } | ||||||
818 | } else if (DestReference->isLValueReferenceType()) { | ||||||
819 | if (!SrcExpr.get()->isLValue()) { | ||||||
820 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue) | ||||||
821 | << CT_Dynamic << OrigSrcType << this->DestType << OpRange; | ||||||
822 | } | ||||||
823 | SrcPointee = SrcType; | ||||||
824 | } else { | ||||||
825 | // If we're dynamic_casting from a prvalue to an rvalue reference, we need | ||||||
826 | // to materialize the prvalue before we bind the reference to it. | ||||||
827 | if (SrcExpr.get()->isPRValue()) | ||||||
828 | SrcExpr = Self.CreateMaterializeTemporaryExpr( | ||||||
829 | SrcType, SrcExpr.get(), /*IsLValueReference*/ false); | ||||||
830 | SrcPointee = SrcType; | ||||||
831 | } | ||||||
832 | |||||||
833 | const RecordType *SrcRecord = SrcPointee->getAs<RecordType>(); | ||||||
834 | if (SrcRecord) { | ||||||
835 | if (Self.RequireCompleteType(OpRange.getBegin(), SrcPointee, | ||||||
836 | diag::err_bad_cast_incomplete, | ||||||
837 | SrcExpr.get())) { | ||||||
838 | SrcExpr = ExprError(); | ||||||
839 | return; | ||||||
840 | } | ||||||
841 | } else { | ||||||
842 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class) | ||||||
843 | << SrcPointee.getUnqualifiedType() << SrcExpr.get()->getSourceRange(); | ||||||
844 | SrcExpr = ExprError(); | ||||||
845 | return; | ||||||
846 | } | ||||||
847 | |||||||
848 | assert((DestPointer || DestReference) &&(static_cast <bool> ((DestPointer || DestReference) && "Bad destination non-ptr/ref slipped through.") ? void (0) : __assert_fail ("(DestPointer || DestReference) && \"Bad destination non-ptr/ref slipped through.\"" , "clang/lib/Sema/SemaCast.cpp", 849, __extension__ __PRETTY_FUNCTION__ )) | ||||||
849 | "Bad destination non-ptr/ref slipped through.")(static_cast <bool> ((DestPointer || DestReference) && "Bad destination non-ptr/ref slipped through.") ? void (0) : __assert_fail ("(DestPointer || DestReference) && \"Bad destination non-ptr/ref slipped through.\"" , "clang/lib/Sema/SemaCast.cpp", 849, __extension__ __PRETTY_FUNCTION__ )); | ||||||
850 | assert((DestRecord || DestPointee->isVoidType()) &&(static_cast <bool> ((DestRecord || DestPointee->isVoidType ()) && "Bad destination pointee slipped through.") ? void (0) : __assert_fail ("(DestRecord || DestPointee->isVoidType()) && \"Bad destination pointee slipped through.\"" , "clang/lib/Sema/SemaCast.cpp", 851, __extension__ __PRETTY_FUNCTION__ )) | ||||||
851 | "Bad destination pointee slipped through.")(static_cast <bool> ((DestRecord || DestPointee->isVoidType ()) && "Bad destination pointee slipped through.") ? void (0) : __assert_fail ("(DestRecord || DestPointee->isVoidType()) && \"Bad destination pointee slipped through.\"" , "clang/lib/Sema/SemaCast.cpp", 851, __extension__ __PRETTY_FUNCTION__ )); | ||||||
852 | assert(SrcRecord && "Bad source pointee slipped through.")(static_cast <bool> (SrcRecord && "Bad source pointee slipped through." ) ? void (0) : __assert_fail ("SrcRecord && \"Bad source pointee slipped through.\"" , "clang/lib/Sema/SemaCast.cpp", 852, __extension__ __PRETTY_FUNCTION__ )); | ||||||
853 | |||||||
854 | // C++ 5.2.7p1: The dynamic_cast operator shall not cast away constness. | ||||||
855 | if (!DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) { | ||||||
856 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_qualifiers_away) | ||||||
857 | << CT_Dynamic << OrigSrcType << this->DestType << OpRange; | ||||||
858 | SrcExpr = ExprError(); | ||||||
859 | return; | ||||||
860 | } | ||||||
861 | |||||||
862 | // C++ 5.2.7p3: If the type of v is the same as the required result type, | ||||||
863 | // [except for cv]. | ||||||
864 | if (DestRecord == SrcRecord) { | ||||||
865 | Kind = CK_NoOp; | ||||||
866 | return; | ||||||
867 | } | ||||||
868 | |||||||
869 | // C++ 5.2.7p5 | ||||||
870 | // Upcasts are resolved statically. | ||||||
871 | if (DestRecord && | ||||||
872 | Self.IsDerivedFrom(OpRange.getBegin(), SrcPointee, DestPointee)) { | ||||||
873 | if (Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee, | ||||||
874 | OpRange.getBegin(), OpRange, | ||||||
875 | &BasePath)) { | ||||||
876 | SrcExpr = ExprError(); | ||||||
877 | return; | ||||||
878 | } | ||||||
879 | |||||||
880 | Kind = CK_DerivedToBase; | ||||||
881 | return; | ||||||
882 | } | ||||||
883 | |||||||
884 | // C++ 5.2.7p6: Otherwise, v shall be [polymorphic]. | ||||||
885 | const RecordDecl *SrcDecl = SrcRecord->getDecl()->getDefinition(); | ||||||
886 | assert(SrcDecl && "Definition missing")(static_cast <bool> (SrcDecl && "Definition missing" ) ? void (0) : __assert_fail ("SrcDecl && \"Definition missing\"" , "clang/lib/Sema/SemaCast.cpp", 886, __extension__ __PRETTY_FUNCTION__ )); | ||||||
887 | if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) { | ||||||
888 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic) | ||||||
889 | << SrcPointee.getUnqualifiedType() << SrcExpr.get()->getSourceRange(); | ||||||
890 | SrcExpr = ExprError(); | ||||||
891 | } | ||||||
892 | |||||||
893 | // dynamic_cast is not available with -fno-rtti. | ||||||
894 | // As an exception, dynamic_cast to void* is available because it doesn't | ||||||
895 | // use RTTI. | ||||||
896 | if (!Self.getLangOpts().RTTI && !DestPointee->isVoidType()) { | ||||||
897 | Self.Diag(OpRange.getBegin(), diag::err_no_dynamic_cast_with_fno_rtti); | ||||||
898 | SrcExpr = ExprError(); | ||||||
899 | return; | ||||||
900 | } | ||||||
901 | |||||||
902 | // Warns when dynamic_cast is used with RTTI data disabled. | ||||||
903 | if (!Self.getLangOpts().RTTIData) { | ||||||
904 | bool MicrosoftABI = | ||||||
905 | Self.getASTContext().getTargetInfo().getCXXABI().isMicrosoft(); | ||||||
906 | bool isClangCL = Self.getDiagnostics().getDiagnosticOptions().getFormat() == | ||||||
907 | DiagnosticOptions::MSVC; | ||||||
908 | if (MicrosoftABI || !DestPointee->isVoidType()) | ||||||
909 | Self.Diag(OpRange.getBegin(), | ||||||
910 | diag::warn_no_dynamic_cast_with_rtti_disabled) | ||||||
911 | << isClangCL; | ||||||
912 | } | ||||||
913 | |||||||
914 | // Done. Everything else is run-time checks. | ||||||
915 | Kind = CK_Dynamic; | ||||||
916 | } | ||||||
917 | |||||||
918 | /// CheckConstCast - Check that a const_cast\<DestType\>(SrcExpr) is valid. | ||||||
919 | /// Refer to C++ 5.2.11 for details. const_cast is typically used in code | ||||||
920 | /// like this: | ||||||
921 | /// const char *str = "literal"; | ||||||
922 | /// legacy_function(const_cast\<char*\>(str)); | ||||||
923 | void CastOperation::CheckConstCast() { | ||||||
924 | CheckNoDerefRAII NoderefCheck(*this); | ||||||
925 | |||||||
926 | if (ValueKind == VK_PRValue) | ||||||
927 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get()); | ||||||
928 | else if (isPlaceholder()) | ||||||
929 | SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.get()); | ||||||
930 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error | ||||||
931 | return; | ||||||
932 | |||||||
933 | unsigned msg = diag::err_bad_cxx_cast_generic; | ||||||
934 | auto TCR = TryConstCast(Self, SrcExpr, DestType, /*CStyle*/ false, msg); | ||||||
935 | if (TCR != TC_Success && msg != 0) { | ||||||
936 | Self.Diag(OpRange.getBegin(), msg) << CT_Const | ||||||
937 | << SrcExpr.get()->getType() << DestType << OpRange; | ||||||
938 | } | ||||||
939 | if (!isValidCast(TCR)) | ||||||
940 | SrcExpr = ExprError(); | ||||||
941 | } | ||||||
942 | |||||||
943 | void CastOperation::CheckAddrspaceCast() { | ||||||
944 | unsigned msg = diag::err_bad_cxx_cast_generic; | ||||||
945 | auto TCR = | ||||||
946 | TryAddressSpaceCast(Self, SrcExpr, DestType, /*CStyle*/ false, msg, Kind); | ||||||
947 | if (TCR != TC_Success && msg != 0) { | ||||||
948 | Self.Diag(OpRange.getBegin(), msg) | ||||||
949 | << CT_Addrspace << SrcExpr.get()->getType() << DestType << OpRange; | ||||||
950 | } | ||||||
951 | if (!isValidCast(TCR)) | ||||||
952 | SrcExpr = ExprError(); | ||||||
953 | } | ||||||
954 | |||||||
955 | /// Check that a reinterpret_cast\<DestType\>(SrcExpr) is not used as upcast | ||||||
956 | /// or downcast between respective pointers or references. | ||||||
957 | static void DiagnoseReinterpretUpDownCast(Sema &Self, const Expr *SrcExpr, | ||||||
958 | QualType DestType, | ||||||
959 | SourceRange OpRange) { | ||||||
960 | QualType SrcType = SrcExpr->getType(); | ||||||
961 | // When casting from pointer or reference, get pointee type; use original | ||||||
962 | // type otherwise. | ||||||
963 | const CXXRecordDecl *SrcPointeeRD = SrcType->getPointeeCXXRecordDecl(); | ||||||
964 | const CXXRecordDecl *SrcRD = | ||||||
965 | SrcPointeeRD ? SrcPointeeRD : SrcType->getAsCXXRecordDecl(); | ||||||
966 | |||||||
967 | // Examining subobjects for records is only possible if the complete and | ||||||
968 | // valid definition is available. Also, template instantiation is not | ||||||
969 | // allowed here. | ||||||
970 | if (!SrcRD || !SrcRD->isCompleteDefinition() || SrcRD->isInvalidDecl()) | ||||||
971 | return; | ||||||
972 | |||||||
973 | const CXXRecordDecl *DestRD = DestType->getPointeeCXXRecordDecl(); | ||||||
974 | |||||||
975 | if (!DestRD || !DestRD->isCompleteDefinition() || DestRD->isInvalidDecl()) | ||||||
976 | return; | ||||||
977 | |||||||
978 | enum { | ||||||
979 | ReinterpretUpcast, | ||||||
980 | ReinterpretDowncast | ||||||
981 | } ReinterpretKind; | ||||||
982 | |||||||
983 | CXXBasePaths BasePaths; | ||||||
984 | |||||||
985 | if (SrcRD->isDerivedFrom(DestRD, BasePaths)) | ||||||
986 | ReinterpretKind = ReinterpretUpcast; | ||||||
987 | else if (DestRD->isDerivedFrom(SrcRD, BasePaths)) | ||||||
988 | ReinterpretKind = ReinterpretDowncast; | ||||||
989 | else | ||||||
990 | return; | ||||||
991 | |||||||
992 | bool VirtualBase = true; | ||||||
993 | bool NonZeroOffset = false; | ||||||
994 | for (CXXBasePaths::const_paths_iterator I = BasePaths.begin(), | ||||||
995 | E = BasePaths.end(); | ||||||
996 | I != E; ++I) { | ||||||
997 | const CXXBasePath &Path = *I; | ||||||
998 | CharUnits Offset = CharUnits::Zero(); | ||||||
999 | bool IsVirtual = false; | ||||||
1000 | for (CXXBasePath::const_iterator IElem = Path.begin(), EElem = Path.end(); | ||||||
1001 | IElem != EElem; ++IElem) { | ||||||
1002 | IsVirtual = IElem->Base->isVirtual(); | ||||||
1003 | if (IsVirtual) | ||||||
1004 | break; | ||||||
1005 | const CXXRecordDecl *BaseRD = IElem->Base->getType()->getAsCXXRecordDecl(); | ||||||
1006 | assert(BaseRD && "Base type should be a valid unqualified class type")(static_cast <bool> (BaseRD && "Base type should be a valid unqualified class type" ) ? void (0) : __assert_fail ("BaseRD && \"Base type should be a valid unqualified class type\"" , "clang/lib/Sema/SemaCast.cpp", 1006, __extension__ __PRETTY_FUNCTION__ )); | ||||||
1007 | // Don't check if any base has invalid declaration or has no definition | ||||||
1008 | // since it has no layout info. | ||||||
1009 | const CXXRecordDecl *Class = IElem->Class, | ||||||
1010 | *ClassDefinition = Class->getDefinition(); | ||||||
1011 | if (Class->isInvalidDecl() || !ClassDefinition || | ||||||
1012 | !ClassDefinition->isCompleteDefinition()) | ||||||
1013 | return; | ||||||
1014 | |||||||
1015 | const ASTRecordLayout &DerivedLayout = | ||||||
1016 | Self.Context.getASTRecordLayout(Class); | ||||||
1017 | Offset += DerivedLayout.getBaseClassOffset(BaseRD); | ||||||
1018 | } | ||||||
1019 | if (!IsVirtual) { | ||||||
1020 | // Don't warn if any path is a non-virtually derived base at offset zero. | ||||||
1021 | if (Offset.isZero()) | ||||||
1022 | return; | ||||||
1023 | // Offset makes sense only for non-virtual bases. | ||||||
1024 | else | ||||||
1025 | NonZeroOffset = true; | ||||||
1026 | } | ||||||
1027 | VirtualBase = VirtualBase && IsVirtual; | ||||||
1028 | } | ||||||
1029 | |||||||
1030 | (void) NonZeroOffset; // Silence set but not used warning. | ||||||
1031 | assert((VirtualBase || NonZeroOffset) &&(static_cast <bool> ((VirtualBase || NonZeroOffset) && "Should have returned if has non-virtual base with zero offset" ) ? void (0) : __assert_fail ("(VirtualBase || NonZeroOffset) && \"Should have returned if has non-virtual base with zero offset\"" , "clang/lib/Sema/SemaCast.cpp", 1032, __extension__ __PRETTY_FUNCTION__ )) | ||||||
1032 | "Should have returned if has non-virtual base with zero offset")(static_cast <bool> ((VirtualBase || NonZeroOffset) && "Should have returned if has non-virtual base with zero offset" ) ? void (0) : __assert_fail ("(VirtualBase || NonZeroOffset) && \"Should have returned if has non-virtual base with zero offset\"" , "clang/lib/Sema/SemaCast.cpp", 1032, __extension__ __PRETTY_FUNCTION__ )); | ||||||
1033 | |||||||
1034 | QualType BaseType = | ||||||
1035 | ReinterpretKind == ReinterpretUpcast? DestType : SrcType; | ||||||
1036 | QualType DerivedType = | ||||||
1037 | ReinterpretKind == ReinterpretUpcast? SrcType : DestType; | ||||||
1038 | |||||||
1039 | SourceLocation BeginLoc = OpRange.getBegin(); | ||||||
1040 | Self.Diag(BeginLoc, diag::warn_reinterpret_different_from_static) | ||||||
1041 | << DerivedType << BaseType << !VirtualBase << int(ReinterpretKind) | ||||||
1042 | << OpRange; | ||||||
1043 | Self.Diag(BeginLoc, diag::note_reinterpret_updowncast_use_static) | ||||||
1044 | << int(ReinterpretKind) | ||||||
1045 | << FixItHint::CreateReplacement(BeginLoc, "static_cast"); | ||||||
1046 | } | ||||||
1047 | |||||||
1048 | static bool argTypeIsABIEquivalent(QualType SrcType, QualType DestType, | ||||||
1049 | ASTContext &Context) { | ||||||
1050 | if (SrcType->isPointerType() && DestType->isPointerType()) | ||||||
1051 | return true; | ||||||
1052 | |||||||
1053 | // Allow integral type mismatch if their size are equal. | ||||||
1054 | if (SrcType->isIntegralType(Context) && DestType->isIntegralType(Context)) | ||||||
1055 | if (Context.getTypeInfoInChars(SrcType).Width == | ||||||
1056 | Context.getTypeInfoInChars(DestType).Width) | ||||||
1057 | return true; | ||||||
1058 | |||||||
1059 | return Context.hasSameUnqualifiedType(SrcType, DestType); | ||||||
1060 | } | ||||||
1061 | |||||||
1062 | static bool checkCastFunctionType(Sema &Self, const ExprResult &SrcExpr, | ||||||
1063 | QualType DestType) { | ||||||
1064 | if (Self.Diags.isIgnored(diag::warn_cast_function_type, | ||||||
1065 | SrcExpr.get()->getExprLoc())) | ||||||
1066 | return true; | ||||||
1067 | |||||||
1068 | QualType SrcType = SrcExpr.get()->getType(); | ||||||
1069 | const FunctionType *SrcFTy = nullptr; | ||||||
1070 | const FunctionType *DstFTy = nullptr; | ||||||
1071 | if (((SrcType->isBlockPointerType() || SrcType->isFunctionPointerType()) && | ||||||
1072 | DestType->isFunctionPointerType()) || | ||||||
1073 | (SrcType->isMemberFunctionPointerType() && | ||||||
1074 | DestType->isMemberFunctionPointerType())) { | ||||||
1075 | SrcFTy = SrcType->getPointeeType()->castAs<FunctionType>(); | ||||||
1076 | DstFTy = DestType->getPointeeType()->castAs<FunctionType>(); | ||||||
1077 | } else if (SrcType->isFunctionType() && DestType->isFunctionReferenceType()) { | ||||||
1078 | SrcFTy = SrcType->castAs<FunctionType>(); | ||||||
1079 | DstFTy = DestType.getNonReferenceType()->castAs<FunctionType>(); | ||||||
1080 | } else { | ||||||
1081 | return true; | ||||||
1082 | } | ||||||
1083 | assert(SrcFTy && DstFTy)(static_cast <bool> (SrcFTy && DstFTy) ? void ( 0) : __assert_fail ("SrcFTy && DstFTy", "clang/lib/Sema/SemaCast.cpp" , 1083, __extension__ __PRETTY_FUNCTION__)); | ||||||
1084 | |||||||
1085 | auto IsVoidVoid = [](const FunctionType *T) { | ||||||
1086 | if (!T->getReturnType()->isVoidType()) | ||||||
1087 | return false; | ||||||
1088 | if (const auto *PT = T->getAs<FunctionProtoType>()) | ||||||
1089 | return !PT->isVariadic() && PT->getNumParams() == 0; | ||||||
1090 | return false; | ||||||
1091 | }; | ||||||
1092 | |||||||
1093 | // Skip if either function type is void(*)(void) | ||||||
1094 | if (IsVoidVoid(SrcFTy) || IsVoidVoid(DstFTy)) | ||||||
1095 | return true; | ||||||
1096 | |||||||
1097 | // Check return type. | ||||||
1098 | if (!argTypeIsABIEquivalent(SrcFTy->getReturnType(), DstFTy->getReturnType(), | ||||||
1099 | Self.Context)) | ||||||
1100 | return false; | ||||||
1101 | |||||||
1102 | // Check if either has unspecified number of parameters | ||||||
1103 | if (SrcFTy->isFunctionNoProtoType() || DstFTy->isFunctionNoProtoType()) | ||||||
1104 | return true; | ||||||
1105 | |||||||
1106 | // Check parameter types. | ||||||
1107 | |||||||
1108 | const auto *SrcFPTy = cast<FunctionProtoType>(SrcFTy); | ||||||
1109 | const auto *DstFPTy = cast<FunctionProtoType>(DstFTy); | ||||||
1110 | |||||||
1111 | // In a cast involving function types with a variable argument list only the | ||||||
1112 | // types of initial arguments that are provided are considered. | ||||||
1113 | unsigned NumParams = SrcFPTy->getNumParams(); | ||||||
1114 | unsigned DstNumParams = DstFPTy->getNumParams(); | ||||||
1115 | if (NumParams > DstNumParams) { | ||||||
1116 | if (!DstFPTy->isVariadic()) | ||||||
1117 | return false; | ||||||
1118 | NumParams = DstNumParams; | ||||||
1119 | } else if (NumParams < DstNumParams) { | ||||||
1120 | if (!SrcFPTy->isVariadic()) | ||||||
1121 | return false; | ||||||
1122 | } | ||||||
1123 | |||||||
1124 | for (unsigned i = 0; i < NumParams; ++i) | ||||||
1125 | if (!argTypeIsABIEquivalent(SrcFPTy->getParamType(i), | ||||||
1126 | DstFPTy->getParamType(i), Self.Context)) | ||||||
1127 | return false; | ||||||
1128 | |||||||
1129 | return true; | ||||||
1130 | } | ||||||
1131 | |||||||
1132 | /// CheckReinterpretCast - Check that a reinterpret_cast\<DestType\>(SrcExpr) is | ||||||
1133 | /// valid. | ||||||
1134 | /// Refer to C++ 5.2.10 for details. reinterpret_cast is typically used in code | ||||||
1135 | /// like this: | ||||||
1136 | /// char *bytes = reinterpret_cast\<char*\>(int_ptr); | ||||||
1137 | void CastOperation::CheckReinterpretCast() { | ||||||
1138 | if (ValueKind == VK_PRValue && !isPlaceholder(BuiltinType::Overload)) | ||||||
1139 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get()); | ||||||
1140 | else | ||||||
1141 | checkNonOverloadPlaceholders(); | ||||||
1142 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error | ||||||
1143 | return; | ||||||
1144 | |||||||
1145 | unsigned msg = diag::err_bad_cxx_cast_generic; | ||||||
1146 | TryCastResult tcr = | ||||||
1147 | TryReinterpretCast(Self, SrcExpr, DestType, | ||||||
1148 | /*CStyle*/false, OpRange, msg, Kind); | ||||||
1149 | if (tcr != TC_Success && msg != 0) { | ||||||
1150 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error | ||||||
1151 | return; | ||||||
1152 | if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { | ||||||
1153 | //FIXME: &f<int>; is overloaded and resolvable | ||||||
1154 | Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_overload) | ||||||
1155 | << OverloadExpr::find(SrcExpr.get()).Expression->getName() | ||||||
1156 | << DestType << OpRange; | ||||||
1157 | Self.NoteAllOverloadCandidates(SrcExpr.get()); | ||||||
1158 | |||||||
1159 | } else { | ||||||
1160 | diagnoseBadCast(Self, msg, CT_Reinterpret, OpRange, SrcExpr.get(), | ||||||
1161 | DestType, /*listInitialization=*/false); | ||||||
1162 | } | ||||||
1163 | } | ||||||
1164 | |||||||
1165 | if (isValidCast(tcr)) { | ||||||
1166 | if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers()) | ||||||
1167 | checkObjCConversion(Sema::CCK_OtherCast); | ||||||
1168 | DiagnoseReinterpretUpDownCast(Self, SrcExpr.get(), DestType, OpRange); | ||||||
1169 | |||||||
1170 | if (!checkCastFunctionType(Self, SrcExpr, DestType)) | ||||||
1171 | Self.Diag(OpRange.getBegin(), diag::warn_cast_function_type) | ||||||
1172 | << SrcExpr.get()->getType() << DestType << OpRange; | ||||||
1173 | } else { | ||||||
1174 | SrcExpr = ExprError(); | ||||||
1175 | } | ||||||
1176 | } | ||||||
1177 | |||||||
1178 | |||||||
1179 | /// CheckStaticCast - Check that a static_cast\<DestType\>(SrcExpr) is valid. | ||||||
1180 | /// Refer to C++ 5.2.9 for details. Static casts are mostly used for making | ||||||
1181 | /// implicit conversions explicit and getting rid of data loss warnings. | ||||||
1182 | void CastOperation::CheckStaticCast() { | ||||||
1183 | CheckNoDerefRAII NoderefCheck(*this); | ||||||
1184 | |||||||
1185 | if (isPlaceholder()) { | ||||||
| |||||||
1186 | checkNonOverloadPlaceholders(); | ||||||
1187 | if (SrcExpr.isInvalid()) | ||||||
1188 | return; | ||||||
1189 | } | ||||||
1190 | |||||||
1191 | // This test is outside everything else because it's the only case where | ||||||
1192 | // a non-lvalue-reference target type does not lead to decay. | ||||||
1193 | // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void". | ||||||
1194 | if (DestType->isVoidType()) { | ||||||
1195 | Kind = CK_ToVoid; | ||||||
1196 | |||||||
1197 | if (claimPlaceholder(BuiltinType::Overload)) { | ||||||
1198 | Self.ResolveAndFixSingleFunctionTemplateSpecialization(SrcExpr, | ||||||
1199 | false, // Decay Function to ptr | ||||||
1200 | true, // Complain | ||||||
1201 | OpRange, DestType, diag::err_bad_static_cast_overload); | ||||||
1202 | if (SrcExpr.isInvalid()) | ||||||
1203 | return; | ||||||
1204 | } | ||||||
1205 | |||||||
1206 | SrcExpr = Self.IgnoredValueConversions(SrcExpr.get()); | ||||||
1207 | return; | ||||||
1208 | } | ||||||
1209 | |||||||
1210 | if (ValueKind == VK_PRValue && !DestType->isRecordType() && | ||||||
1211 | !isPlaceholder(BuiltinType::Overload)) { | ||||||
1212 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get()); | ||||||
1213 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error | ||||||
1214 | return; | ||||||
1215 | } | ||||||
1216 | |||||||
1217 | unsigned msg = diag::err_bad_cxx_cast_generic; | ||||||
1218 | TryCastResult tcr | ||||||
1219 | = TryStaticCast(Self, SrcExpr, DestType, Sema::CCK_OtherCast, OpRange, msg, | ||||||
1220 | Kind, BasePath, /*ListInitialization=*/false); | ||||||
1221 | if (tcr != TC_Success && msg != 0) { | ||||||
1222 | if (SrcExpr.isInvalid()) | ||||||
1223 | return; | ||||||
1224 | if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { | ||||||
1225 | OverloadExpr* oe = OverloadExpr::find(SrcExpr.get()).Expression; | ||||||
1226 | Self.Diag(OpRange.getBegin(), diag::err_bad_static_cast_overload) | ||||||
1227 | << oe->getName() << DestType << OpRange | ||||||
1228 | << oe->getQualifierLoc().getSourceRange(); | ||||||
1229 | Self.NoteAllOverloadCandidates(SrcExpr.get()); | ||||||
1230 | } else { | ||||||
1231 | diagnoseBadCast(Self, msg, CT_Static, OpRange, SrcExpr.get(), DestType, | ||||||
1232 | /*listInitialization=*/false); | ||||||
1233 | } | ||||||
1234 | } | ||||||
1235 | |||||||
1236 | if (isValidCast(tcr)) { | ||||||
1237 | if (Kind == CK_BitCast) | ||||||
1238 | checkCastAlign(); | ||||||
1239 | if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers()) | ||||||
1240 | checkObjCConversion(Sema::CCK_OtherCast); | ||||||
1241 | } else { | ||||||
1242 | SrcExpr = ExprError(); | ||||||
1243 | } | ||||||
1244 | } | ||||||
1245 | |||||||
1246 | static bool IsAddressSpaceConversion(QualType SrcType, QualType DestType) { | ||||||
1247 | auto *SrcPtrType = SrcType->getAs<PointerType>(); | ||||||
1248 | if (!SrcPtrType) | ||||||
1249 | return false; | ||||||
1250 | auto *DestPtrType = DestType->getAs<PointerType>(); | ||||||
1251 | if (!DestPtrType) | ||||||
1252 | return false; | ||||||
1253 | return SrcPtrType->getPointeeType().getAddressSpace() != | ||||||
1254 | DestPtrType->getPointeeType().getAddressSpace(); | ||||||
1255 | } | ||||||
1256 | |||||||
1257 | /// TryStaticCast - Check if a static cast can be performed, and do so if | ||||||
1258 | /// possible. If @p CStyle, ignore access restrictions on hierarchy casting | ||||||
1259 | /// and casting away constness. | ||||||
1260 | static TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr, | ||||||
1261 | QualType DestType, | ||||||
1262 | Sema::CheckedConversionKind CCK, | ||||||
1263 | SourceRange OpRange, unsigned &msg, | ||||||
1264 | CastKind &Kind, CXXCastPath &BasePath, | ||||||
1265 | bool ListInitialization) { | ||||||
1266 | // Determine whether we have the semantics of a C-style cast. | ||||||
1267 | bool CStyle | ||||||
1268 | = (CCK
| ||||||
1269 | |||||||
1270 | // The order the tests is not entirely arbitrary. There is one conversion | ||||||
1271 | // that can be handled in two different ways. Given: | ||||||
1272 | // struct A {}; | ||||||
1273 | // struct B : public A { | ||||||
1274 | // B(); B(const A&); | ||||||
1275 | // }; | ||||||
1276 | // const A &a = B(); | ||||||
1277 | // the cast static_cast<const B&>(a) could be seen as either a static | ||||||
1278 | // reference downcast, or an explicit invocation of the user-defined | ||||||
1279 | // conversion using B's conversion constructor. | ||||||
1280 | // DR 427 specifies that the downcast is to be applied here. | ||||||
1281 | |||||||
1282 | // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void". | ||||||
1283 | // Done outside this function. | ||||||
1284 | |||||||
1285 | TryCastResult tcr; | ||||||
1286 | |||||||
1287 | // C++ 5.2.9p5, reference downcast. | ||||||
1288 | // See the function for details. | ||||||
1289 | // DR 427 specifies that this is to be applied before paragraph 2. | ||||||
1290 | tcr = TryStaticReferenceDowncast(Self, SrcExpr.get(), DestType, CStyle, | ||||||
1291 | OpRange, msg, Kind, BasePath); | ||||||
1292 | if (tcr
| ||||||
1293 | return tcr; | ||||||
1294 | |||||||
1295 | // C++11 [expr.static.cast]p3: | ||||||
1296 | // A glvalue of type "cv1 T1" can be cast to type "rvalue reference to cv2 | ||||||
1297 | // T2" if "cv2 T2" is reference-compatible with "cv1 T1". | ||||||
1298 | tcr = TryLValueToRValueCast(Self, SrcExpr.get(), DestType, CStyle, Kind, | ||||||
1299 | BasePath, msg); | ||||||
1300 | if (tcr
| ||||||
1301 | return tcr; | ||||||
1302 | |||||||
1303 | // C++ 5.2.9p2: An expression e can be explicitly converted to a type T | ||||||
1304 | // [...] if the declaration "T t(e);" is well-formed, [...]. | ||||||
1305 | tcr = TryStaticImplicitCast(Self, SrcExpr, DestType, CCK, OpRange, msg, | ||||||
1306 | Kind, ListInitialization); | ||||||
1307 | if (SrcExpr.isInvalid()) | ||||||
1308 | return TC_Failed; | ||||||
1309 | if (tcr
| ||||||
1310 | return tcr; | ||||||
1311 | |||||||
1312 | // C++ 5.2.9p6: May apply the reverse of any standard conversion, except | ||||||
1313 | // lvalue-to-rvalue, array-to-pointer, function-to-pointer, and boolean | ||||||
1314 | // conversions, subject to further restrictions. | ||||||
1315 | // Also, C++ 5.2.9p1 forbids casting away constness, which makes reversal | ||||||
1316 | // of qualification conversions impossible. (In C++20, adding an array bound | ||||||
1317 | // would be the reverse of a qualification conversion, but adding permission | ||||||
1318 | // to add an array bound in a static_cast is a wording oversight.) | ||||||
1319 | // In the CStyle case, the earlier attempt to const_cast should have taken | ||||||
1320 | // care of reverse qualification conversions. | ||||||
1321 | |||||||
1322 | QualType SrcType = Self.Context.getCanonicalType(SrcExpr.get()->getType()); | ||||||
1323 | |||||||
1324 | // C++0x 5.2.9p9: A value of a scoped enumeration type can be explicitly | ||||||
1325 | // converted to an integral type. [...] A value of a scoped enumeration type | ||||||
1326 | // can also be explicitly converted to a floating-point type [...]. | ||||||
1327 | if (const EnumType *Enum
| ||||||
1328 | if (Enum->getDecl()->isScoped()) { | ||||||
1329 | if (DestType->isBooleanType()) { | ||||||
1330 | Kind = CK_IntegralToBoolean; | ||||||
1331 | return TC_Success; | ||||||
1332 | } else if (DestType->isIntegralType(Self.Context)) { | ||||||
1333 | Kind = CK_IntegralCast; | ||||||
1334 | return TC_Success; | ||||||
1335 | } else if (DestType->isRealFloatingType()) { | ||||||
1336 | Kind = CK_IntegralToFloating; | ||||||
1337 | return TC_Success; | ||||||
1338 | } | ||||||
1339 | } | ||||||
1340 | } | ||||||
1341 | |||||||
1342 | // Reverse integral promotion/conversion. All such conversions are themselves | ||||||
1343 | // again integral promotions or conversions and are thus already handled by | ||||||
1344 | // p2 (TryDirectInitialization above). | ||||||
1345 | // (Note: any data loss warnings should be suppressed.) | ||||||
1346 | // The exception is the reverse of enum->integer, i.e. integer->enum (and | ||||||
1347 | // enum->enum). See also C++ 5.2.9p7. | ||||||
1348 | // The same goes for reverse floating point promotion/conversion and | ||||||
1349 | // floating-integral conversions. Again, only floating->enum is relevant. | ||||||
1350 | if (DestType->isEnumeralType()) { | ||||||
1351 | if (Self.RequireCompleteType(OpRange.getBegin(), DestType, | ||||||
1352 | diag::err_bad_cast_incomplete)) { | ||||||
1353 | SrcExpr = ExprError(); | ||||||
1354 | return TC_Failed; | ||||||
1355 | } | ||||||
1356 | if (SrcType->isIntegralOrEnumerationType()) { | ||||||
1357 | // [expr.static.cast]p10 If the enumeration type has a fixed underlying | ||||||
1358 | // type, the value is first converted to that type by integral conversion | ||||||
1359 | const EnumType *Enum = DestType->getAs<EnumType>(); | ||||||
1360 | Kind = Enum->getDecl()->isFixed() && | ||||||
| |||||||
1361 | Enum->getDecl()->getIntegerType()->isBooleanType() | ||||||
1362 | ? CK_IntegralToBoolean | ||||||
1363 | : CK_IntegralCast; | ||||||
1364 | return TC_Success; | ||||||
1365 | } else if (SrcType->isRealFloatingType()) { | ||||||
1366 | Kind = CK_FloatingToIntegral; | ||||||
1367 | return TC_Success; | ||||||
1368 | } | ||||||
1369 | } | ||||||
1370 | |||||||
1371 | // Reverse pointer upcast. C++ 4.10p3 specifies pointer upcast. | ||||||
1372 | // C++ 5.2.9p8 additionally disallows a cast path through virtual inheritance. | ||||||
1373 | tcr = TryStaticPointerDowncast(Self, SrcType, DestType, CStyle, OpRange, msg, | ||||||
1374 | Kind, BasePath); | ||||||
1375 | if (tcr != TC_NotApplicable) | ||||||
1376 | return tcr; | ||||||
1377 | |||||||
1378 | // Reverse member pointer conversion. C++ 4.11 specifies member pointer | ||||||
1379 | // conversion. C++ 5.2.9p9 has additional information. | ||||||
1380 | // DR54's access restrictions apply here also. | ||||||
1381 | tcr = TryStaticMemberPointerUpcast(Self, SrcExpr, SrcType, DestType, CStyle, | ||||||
1382 | OpRange, msg, Kind, BasePath); | ||||||
1383 | if (tcr != TC_NotApplicable) | ||||||
1384 | return tcr; | ||||||
1385 | |||||||
1386 | // Reverse pointer conversion to void*. C++ 4.10.p2 specifies conversion to | ||||||
1387 | // void*. C++ 5.2.9p10 specifies additional restrictions, which really is | ||||||
1388 | // just the usual constness stuff. | ||||||
1389 | if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) { | ||||||
1390 | QualType SrcPointee = SrcPointer->getPointeeType(); | ||||||
1391 | if (SrcPointee->isVoidType()) { | ||||||
1392 | if (const PointerType *DestPointer = DestType->getAs<PointerType>()) { | ||||||
1393 | QualType DestPointee = DestPointer->getPointeeType(); | ||||||
1394 | if (DestPointee->isIncompleteOrObjectType()) { | ||||||
1395 | // This is definitely the intended conversion, but it might fail due | ||||||
1396 | // to a qualifier violation. Note that we permit Objective-C lifetime | ||||||
1397 | // and GC qualifier mismatches here. | ||||||
1398 | if (!CStyle) { | ||||||
1399 | Qualifiers DestPointeeQuals = DestPointee.getQualifiers(); | ||||||
1400 | Qualifiers SrcPointeeQuals = SrcPointee.getQualifiers(); | ||||||
1401 | DestPointeeQuals.removeObjCGCAttr(); | ||||||
1402 | DestPointeeQuals.removeObjCLifetime(); | ||||||
1403 | SrcPointeeQuals.removeObjCGCAttr(); | ||||||
1404 | SrcPointeeQuals.removeObjCLifetime(); | ||||||
1405 | if (DestPointeeQuals != SrcPointeeQuals && | ||||||
1406 | !DestPointeeQuals.compatiblyIncludes(SrcPointeeQuals)) { | ||||||
1407 | msg = diag::err_bad_cxx_cast_qualifiers_away; | ||||||
1408 | return TC_Failed; | ||||||
1409 | } | ||||||
1410 | } | ||||||
1411 | Kind = IsAddressSpaceConversion(SrcType, DestType) | ||||||
1412 | ? CK_AddressSpaceConversion | ||||||
1413 | : CK_BitCast; | ||||||
1414 | return TC_Success; | ||||||
1415 | } | ||||||
1416 | |||||||
1417 | // Microsoft permits static_cast from 'pointer-to-void' to | ||||||
1418 | // 'pointer-to-function'. | ||||||
1419 | if (!CStyle && Self.getLangOpts().MSVCCompat && | ||||||
1420 | DestPointee->isFunctionType()) { | ||||||
1421 | Self.Diag(OpRange.getBegin(), diag::ext_ms_cast_fn_obj) << OpRange; | ||||||
1422 | Kind = CK_BitCast; | ||||||
1423 | return TC_Success; | ||||||
1424 | } | ||||||
1425 | } | ||||||
1426 | else if (DestType->isObjCObjectPointerType()) { | ||||||
1427 | // allow both c-style cast and static_cast of objective-c pointers as | ||||||
1428 | // they are pervasive. | ||||||
1429 | Kind = CK_CPointerToObjCPointerCast; | ||||||
1430 | return TC_Success; | ||||||
1431 | } | ||||||
1432 | else if (CStyle && DestType->isBlockPointerType()) { | ||||||
1433 | // allow c-style cast of void * to block pointers. | ||||||
1434 | Kind = CK_AnyPointerToBlockPointerCast; | ||||||
1435 | return TC_Success; | ||||||
1436 | } | ||||||
1437 | } | ||||||
1438 | } | ||||||
1439 | // Allow arbitrary objective-c pointer conversion with static casts. | ||||||
1440 | if (SrcType->isObjCObjectPointerType() && | ||||||
1441 | DestType->isObjCObjectPointerType()) { | ||||||
1442 | Kind = CK_BitCast; | ||||||
1443 | return TC_Success; | ||||||
1444 | } | ||||||
1445 | // Allow ns-pointer to cf-pointer conversion in either direction | ||||||
1446 | // with static casts. | ||||||
1447 | if (!CStyle && | ||||||
1448 | Self.CheckTollFreeBridgeStaticCast(DestType, SrcExpr.get(), Kind)) | ||||||
1449 | return TC_Success; | ||||||
1450 | |||||||
1451 | // See if it looks like the user is trying to convert between | ||||||
1452 | // related record types, and select a better diagnostic if so. | ||||||
1453 | if (auto SrcPointer = SrcType->getAs<PointerType>()) | ||||||
1454 | if (auto DestPointer = DestType->getAs<PointerType>()) | ||||||
1455 | if (SrcPointer->getPointeeType()->getAs<RecordType>() && | ||||||
1456 | DestPointer->getPointeeType()->getAs<RecordType>()) | ||||||
1457 | msg = diag::err_bad_cxx_cast_unrelated_class; | ||||||
1458 | |||||||
1459 | if (SrcType->isMatrixType() && DestType->isMatrixType()) { | ||||||
1460 | if (Self.CheckMatrixCast(OpRange, DestType, SrcType, Kind)) { | ||||||
1461 | SrcExpr = ExprError(); | ||||||
1462 | return TC_Failed; | ||||||
1463 | } | ||||||
1464 | return TC_Success; | ||||||
1465 | } | ||||||
1466 | |||||||
1467 | // We tried everything. Everything! Nothing works! :-( | ||||||
1468 | return TC_NotApplicable; | ||||||
1469 | } | ||||||
1470 | |||||||
1471 | /// Tests whether a conversion according to N2844 is valid. | ||||||
1472 | TryCastResult TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, | ||||||
1473 | QualType DestType, bool CStyle, | ||||||
1474 | CastKind &Kind, CXXCastPath &BasePath, | ||||||
1475 | unsigned &msg) { | ||||||
1476 | // C++11 [expr.static.cast]p3: | ||||||
1477 | // A glvalue of type "cv1 T1" can be cast to type "rvalue reference to | ||||||
1478 | // cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1". | ||||||
1479 | const RValueReferenceType *R = DestType->getAs<RValueReferenceType>(); | ||||||
1480 | if (!R) | ||||||
1481 | return TC_NotApplicable; | ||||||
1482 | |||||||
1483 | if (!SrcExpr->isGLValue()) | ||||||
1484 | return TC_NotApplicable; | ||||||
1485 | |||||||
1486 | // Because we try the reference downcast before this function, from now on | ||||||
1487 | // this is the only cast possibility, so we issue an error if we fail now. | ||||||
1488 | // FIXME: Should allow casting away constness if CStyle. | ||||||
1489 | QualType FromType = SrcExpr->getType(); | ||||||
1490 | QualType ToType = R->getPointeeType(); | ||||||
1491 | if (CStyle) { | ||||||
1492 | FromType = FromType.getUnqualifiedType(); | ||||||
1493 | ToType = ToType.getUnqualifiedType(); | ||||||
1494 | } | ||||||
1495 | |||||||
1496 | Sema::ReferenceConversions RefConv; | ||||||
1497 | Sema::ReferenceCompareResult RefResult = Self.CompareReferenceRelationship( | ||||||
1498 | SrcExpr->getBeginLoc(), ToType, FromType, &RefConv); | ||||||
1499 | if (RefResult != Sema::Ref_Compatible) { | ||||||
1500 | if (CStyle || RefResult == Sema::Ref_Incompatible) | ||||||
1501 | return TC_NotApplicable; | ||||||
1502 | // Diagnose types which are reference-related but not compatible here since | ||||||
1503 | // we can provide better diagnostics. In these cases forwarding to | ||||||
1504 | // [expr.static.cast]p4 should never result in a well-formed cast. | ||||||
1505 | msg = SrcExpr->isLValue() ? diag::err_bad_lvalue_to_rvalue_cast | ||||||
1506 | : diag::err_bad_rvalue_to_rvalue_cast; | ||||||
1507 | return TC_Failed; | ||||||
1508 | } | ||||||
1509 | |||||||
1510 | if (RefConv & Sema::ReferenceConversions::DerivedToBase) { | ||||||
1511 | Kind = CK_DerivedToBase; | ||||||
1512 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, | ||||||
1513 | /*DetectVirtual=*/true); | ||||||
1514 | if (!Self.IsDerivedFrom(SrcExpr->getBeginLoc(), SrcExpr->getType(), | ||||||
1515 | R->getPointeeType(), Paths)) | ||||||
1516 | return TC_NotApplicable; | ||||||
1517 | |||||||
1518 | Self.BuildBasePathArray(Paths, BasePath); | ||||||
1519 | } else | ||||||
1520 | Kind = CK_NoOp; | ||||||
1521 | |||||||
1522 | return TC_Success; | ||||||
1523 | } | ||||||
1524 | |||||||
1525 | /// Tests whether a conversion according to C++ 5.2.9p5 is valid. | ||||||
1526 | TryCastResult | ||||||
1527 | TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType, | ||||||
1528 | bool CStyle, SourceRange OpRange, | ||||||
1529 | unsigned &msg, CastKind &Kind, | ||||||
1530 | CXXCastPath &BasePath) { | ||||||
1531 | // C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be | ||||||
1532 | // cast to type "reference to cv2 D", where D is a class derived from B, | ||||||
1533 | // if a valid standard conversion from "pointer to D" to "pointer to B" | ||||||
1534 | // exists, cv2 >= cv1, and B is not a virtual base class of D. | ||||||
1535 | // In addition, DR54 clarifies that the base must be accessible in the | ||||||
1536 | // current context. Although the wording of DR54 only applies to the pointer | ||||||
1537 | // variant of this rule, the intent is clearly for it to apply to the this | ||||||
1538 | // conversion as well. | ||||||
1539 | |||||||
1540 | const ReferenceType *DestReference = DestType->getAs<ReferenceType>(); | ||||||
1541 | if (!DestReference) { | ||||||
1542 | return TC_NotApplicable; | ||||||
1543 | } | ||||||
1544 | bool RValueRef = DestReference->isRValueReferenceType(); | ||||||
1545 | if (!RValueRef && !SrcExpr->isLValue()) { | ||||||
1546 | // We know the left side is an lvalue reference, so we can suggest a reason. | ||||||
1547 | msg = diag::err_bad_cxx_cast_rvalue; | ||||||
1548 | return TC_NotApplicable; | ||||||
1549 | } | ||||||
1550 | |||||||
1551 | QualType DestPointee = DestReference->getPointeeType(); | ||||||
1552 | |||||||
1553 | // FIXME: If the source is a prvalue, we should issue a warning (because the | ||||||
1554 | // cast always has undefined behavior), and for AST consistency, we should | ||||||
1555 | // materialize a temporary. | ||||||
1556 | return TryStaticDowncast(Self, | ||||||
1557 | Self.Context.getCanonicalType(SrcExpr->getType()), | ||||||
1558 | Self.Context.getCanonicalType(DestPointee), CStyle, | ||||||
1559 | OpRange, SrcExpr->getType(), DestType, msg, Kind, | ||||||
1560 | BasePath); | ||||||
1561 | } | ||||||
1562 | |||||||
1563 | /// Tests whether a conversion according to C++ 5.2.9p8 is valid. | ||||||
1564 | TryCastResult | ||||||
1565 | TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType, | ||||||
1566 | bool CStyle, SourceRange OpRange, | ||||||
1567 | unsigned &msg, CastKind &Kind, | ||||||
1568 | CXXCastPath &BasePath) { | ||||||
1569 | // C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class | ||||||
1570 | // type, can be converted to an rvalue of type "pointer to cv2 D", where D | ||||||
1571 | // is a class derived from B, if a valid standard conversion from "pointer | ||||||
1572 | // to D" to "pointer to B" exists, cv2 >= cv1, and B is not a virtual base | ||||||
1573 | // class of D. | ||||||
1574 | // In addition, DR54 clarifies that the base must be accessible in the | ||||||
1575 | // current context. | ||||||
1576 | |||||||
1577 | const PointerType *DestPointer = DestType->getAs<PointerType>(); | ||||||
1578 | if (!DestPointer) { | ||||||
1579 | return TC_NotApplicable; | ||||||
1580 | } | ||||||
1581 | |||||||
1582 | const PointerType *SrcPointer = SrcType->getAs<PointerType>(); | ||||||
1583 | if (!SrcPointer) { | ||||||
1584 | msg = diag::err_bad_static_cast_pointer_nonpointer; | ||||||
1585 | return TC_NotApplicable; | ||||||
1586 | } | ||||||
1587 | |||||||
1588 | return TryStaticDowncast(Self, | ||||||
1589 | Self.Context.getCanonicalType(SrcPointer->getPointeeType()), | ||||||
1590 | Self.Context.getCanonicalType(DestPointer->getPointeeType()), | ||||||
1591 | CStyle, OpRange, SrcType, DestType, msg, Kind, | ||||||
1592 | BasePath); | ||||||
1593 | } | ||||||
1594 | |||||||
1595 | /// TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and | ||||||
1596 | /// TryStaticPointerDowncast. Tests whether a static downcast from SrcType to | ||||||
1597 | /// DestType is possible and allowed. | ||||||
1598 | TryCastResult | ||||||
1599 | TryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType, | ||||||
1600 | bool CStyle, SourceRange OpRange, QualType OrigSrcType, | ||||||
1601 | QualType OrigDestType, unsigned &msg, | ||||||
1602 | CastKind &Kind, CXXCastPath &BasePath) { | ||||||
1603 | // We can only work with complete types. But don't complain if it doesn't work | ||||||
1604 | if (!Self.isCompleteType(OpRange.getBegin(), SrcType) || | ||||||
1605 | !Self.isCompleteType(OpRange.getBegin(), DestType)) | ||||||
1606 | return TC_NotApplicable; | ||||||
1607 | |||||||
1608 | // Downcast can only happen in class hierarchies, so we need classes. | ||||||
1609 | if (!DestType->getAs<RecordType>() || !SrcType->getAs<RecordType>()) { | ||||||
1610 | return TC_NotApplicable; | ||||||
1611 | } | ||||||
1612 | |||||||
1613 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, | ||||||
1614 | /*DetectVirtual=*/true); | ||||||
1615 | if (!Self.IsDerivedFrom(OpRange.getBegin(), DestType, SrcType, Paths)) { | ||||||
1616 | return TC_NotApplicable; | ||||||
1617 | } | ||||||
1618 | |||||||
1619 | // Target type does derive from source type. Now we're serious. If an error | ||||||
1620 | // appears now, it's not ignored. | ||||||
1621 | // This may not be entirely in line with the standard. Take for example: | ||||||
1622 | // struct A {}; | ||||||
1623 | // struct B : virtual A { | ||||||
1624 | // B(A&); | ||||||
1625 | // }; | ||||||
1626 | // | ||||||
1627 | // void f() | ||||||
1628 | // { | ||||||
1629 | // (void)static_cast<const B&>(*((A*)0)); | ||||||
1630 | // } | ||||||
1631 | // As far as the standard is concerned, p5 does not apply (A is virtual), so | ||||||
1632 | // p2 should be used instead - "const B& t(*((A*)0));" is perfectly valid. | ||||||
1633 | // However, both GCC and Comeau reject this example, and accepting it would | ||||||
1634 | // mean more complex code if we're to preserve the nice error message. | ||||||
1635 | // FIXME: Being 100% compliant here would be nice to have. | ||||||
1636 | |||||||
1637 | // Must preserve cv, as always, unless we're in C-style mode. | ||||||
1638 | if (!CStyle && !DestType.isAtLeastAsQualifiedAs(SrcType)) { | ||||||
1639 | msg = diag::err_bad_cxx_cast_qualifiers_away; | ||||||
1640 | return TC_Failed; | ||||||
1641 | } | ||||||
1642 | |||||||
1643 | if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) { | ||||||
1644 | // This code is analoguous to that in CheckDerivedToBaseConversion, except | ||||||
1645 | // that it builds the paths in reverse order. | ||||||
1646 | // To sum up: record all paths to the base and build a nice string from | ||||||
1647 | // them. Use it to spice up the error message. | ||||||
1648 | if (!Paths.isRecordingPaths()) { | ||||||
1649 | Paths.clear(); | ||||||
1650 | Paths.setRecordingPaths(true); | ||||||
1651 | Self.IsDerivedFrom(OpRange.getBegin(), DestType, SrcType, Paths); | ||||||
1652 | } | ||||||
1653 | std::string PathDisplayStr; | ||||||
1654 | std::set<unsigned> DisplayedPaths; | ||||||
1655 | for (clang::CXXBasePath &Path : Paths) { | ||||||
1656 | if (DisplayedPaths.insert(Path.back().SubobjectNumber).second) { | ||||||
1657 | // We haven't displayed a path to this particular base | ||||||
1658 | // class subobject yet. | ||||||
1659 | PathDisplayStr += "\n "; | ||||||
1660 | for (CXXBasePathElement &PE : llvm::reverse(Path)) | ||||||
1661 | PathDisplayStr += PE.Base->getType().getAsString() + " -> "; | ||||||
1662 | PathDisplayStr += QualType(DestType).getAsString(); | ||||||
1663 | } | ||||||
1664 | } | ||||||
1665 | |||||||
1666 | Self.Diag(OpRange.getBegin(), diag::err_ambiguous_base_to_derived_cast) | ||||||
1667 | << QualType(SrcType).getUnqualifiedType() | ||||||
1668 | << QualType(DestType).getUnqualifiedType() | ||||||
1669 | << PathDisplayStr << OpRange; | ||||||
1670 | msg = 0; | ||||||
1671 | return TC_Failed; | ||||||
1672 | } | ||||||
1673 | |||||||
1674 | if (Paths.getDetectedVirtual() != nullptr) { | ||||||
1675 | QualType VirtualBase(Paths.getDetectedVirtual(), 0); | ||||||
1676 | Self.Diag(OpRange.getBegin(), diag::err_static_downcast_via_virtual) | ||||||
1677 | << OrigSrcType << OrigDestType << VirtualBase << OpRange; | ||||||
1678 | msg = 0; | ||||||
1679 | return TC_Failed; | ||||||
1680 | } | ||||||
1681 | |||||||
1682 | if (!CStyle) { | ||||||
1683 | switch (Self.CheckBaseClassAccess(OpRange.getBegin(), | ||||||
1684 | SrcType, DestType, | ||||||
1685 | Paths.front(), | ||||||
1686 | diag::err_downcast_from_inaccessible_base)) { | ||||||
1687 | case Sema::AR_accessible: | ||||||
1688 | case Sema::AR_delayed: // be optimistic | ||||||
1689 | case Sema::AR_dependent: // be optimistic | ||||||
1690 | break; | ||||||
1691 | |||||||
1692 | case Sema::AR_inaccessible: | ||||||
1693 | msg = 0; | ||||||
1694 | return TC_Failed; | ||||||
1695 | } | ||||||
1696 | } | ||||||
1697 | |||||||
1698 | Self.BuildBasePathArray(Paths, BasePath); | ||||||
1699 | Kind = CK_BaseToDerived; | ||||||
1700 | return TC_Success; | ||||||
1701 | } | ||||||
1702 | |||||||
1703 | /// TryStaticMemberPointerUpcast - Tests whether a conversion according to | ||||||
1704 | /// C++ 5.2.9p9 is valid: | ||||||
1705 | /// | ||||||
1706 | /// An rvalue of type "pointer to member of D of type cv1 T" can be | ||||||
1707 | /// converted to an rvalue of type "pointer to member of B of type cv2 T", | ||||||
1708 | /// where B is a base class of D [...]. | ||||||
1709 | /// | ||||||
1710 | TryCastResult | ||||||
1711 | TryStaticMemberPointerUpcast(Sema &Self, ExprResult &SrcExpr, QualType SrcType, | ||||||
1712 | QualType DestType, bool CStyle, | ||||||
1713 | SourceRange OpRange, | ||||||
1714 | unsigned &msg, CastKind &Kind, | ||||||
1715 | CXXCastPath &BasePath) { | ||||||
1716 | const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(); | ||||||
1717 | if (!DestMemPtr) | ||||||
1718 | return TC_NotApplicable; | ||||||
1719 | |||||||
1720 | bool WasOverloadedFunction = false; | ||||||
1721 | DeclAccessPair FoundOverload; | ||||||
1722 | if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { | ||||||
1723 | if (FunctionDecl *Fn | ||||||
1724 | = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(), DestType, false, | ||||||
1725 | FoundOverload)) { | ||||||
1726 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); | ||||||
1727 | SrcType = Self.Context.getMemberPointerType(Fn->getType(), | ||||||
1728 | Self.Context.getTypeDeclType(M->getParent()).getTypePtr()); | ||||||
1729 | WasOverloadedFunction = true; | ||||||
1730 | } | ||||||
1731 | } | ||||||
1732 | |||||||
1733 | const MemberPointerType *SrcMemPtr = SrcType->getAs<MemberPointerType>(); | ||||||
1734 | if (!SrcMemPtr) { | ||||||
1735 | msg = diag::err_bad_static_cast_member_pointer_nonmp; | ||||||
1736 | return TC_NotApplicable; | ||||||
1737 | } | ||||||
1738 | |||||||
1739 | // Lock down the inheritance model right now in MS ABI, whether or not the | ||||||
1740 | // pointee types are the same. | ||||||
1741 | if (Self.Context.getTargetInfo().getCXXABI().isMicrosoft()) { | ||||||
1742 | (void)Self.isCompleteType(OpRange.getBegin(), SrcType); | ||||||
1743 | (void)Self.isCompleteType(OpRange.getBegin(), DestType); | ||||||
1744 | } | ||||||
1745 | |||||||
1746 | // T == T, modulo cv | ||||||
1747 | if (!Self.Context.hasSameUnqualifiedType(SrcMemPtr->getPointeeType(), | ||||||
1748 | DestMemPtr->getPointeeType())) | ||||||
1749 | return TC_NotApplicable; | ||||||
1750 | |||||||
1751 | // B base of D | ||||||
1752 | QualType SrcClass(SrcMemPtr->getClass(), 0); | ||||||
1753 | QualType DestClass(DestMemPtr->getClass(), 0); | ||||||
1754 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, | ||||||
1755 | /*DetectVirtual=*/true); | ||||||
1756 | if (!Self.IsDerivedFrom(OpRange.getBegin(), SrcClass, DestClass, Paths)) | ||||||
1757 | return TC_NotApplicable; | ||||||
1758 | |||||||
1759 | // B is a base of D. But is it an allowed base? If not, it's a hard error. | ||||||
1760 | if (Paths.isAmbiguous(Self.Context.getCanonicalType(DestClass))) { | ||||||
1761 | Paths.clear(); | ||||||
1762 | Paths.setRecordingPaths(true); | ||||||
1763 | bool StillOkay = | ||||||
1764 | Self.IsDerivedFrom(OpRange.getBegin(), SrcClass, DestClass, Paths); | ||||||
1765 | assert(StillOkay)(static_cast <bool> (StillOkay) ? void (0) : __assert_fail ("StillOkay", "clang/lib/Sema/SemaCast.cpp", 1765, __extension__ __PRETTY_FUNCTION__)); | ||||||
1766 | (void)StillOkay; | ||||||
1767 | std::string PathDisplayStr = Self.getAmbiguousPathsDisplayString(Paths); | ||||||
1768 | Self.Diag(OpRange.getBegin(), diag::err_ambiguous_memptr_conv) | ||||||
1769 | << 1 << SrcClass << DestClass << PathDisplayStr << OpRange; | ||||||
1770 | msg = 0; | ||||||
1771 | return TC_Failed; | ||||||
1772 | } | ||||||
1773 | |||||||
1774 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { | ||||||
1775 | Self.Diag(OpRange.getBegin(), diag::err_memptr_conv_via_virtual) | ||||||
1776 | << SrcClass << DestClass << QualType(VBase, 0) << OpRange; | ||||||
1777 | msg = 0; | ||||||
1778 | return TC_Failed; | ||||||
1779 | } | ||||||
1780 | |||||||
1781 | if (!CStyle) { | ||||||
1782 | switch (Self.CheckBaseClassAccess(OpRange.getBegin(), | ||||||
1783 | DestClass, SrcClass, | ||||||
1784 | Paths.front(), | ||||||
1785 | diag::err_upcast_to_inaccessible_base)) { | ||||||
1786 | case Sema::AR_accessible: | ||||||
1787 | case Sema::AR_delayed: | ||||||
1788 | case Sema::AR_dependent: | ||||||
1789 | // Optimistically assume that the delayed and dependent cases | ||||||
1790 | // will work out. | ||||||
1791 | break; | ||||||
1792 | |||||||
1793 | case Sema::AR_inaccessible: | ||||||
1794 | msg = 0; | ||||||
1795 | return TC_Failed; | ||||||
1796 | } | ||||||
1797 | } | ||||||
1798 | |||||||
1799 | if (WasOverloadedFunction) { | ||||||
1800 | // Resolve the address of the overloaded function again, this time | ||||||
1801 | // allowing complaints if something goes wrong. | ||||||
1802 | FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(), | ||||||
1803 | DestType, | ||||||
1804 | true, | ||||||
1805 | FoundOverload); | ||||||
1806 | if (!Fn) { | ||||||
1807 | msg = 0; | ||||||
1808 | return TC_Failed; | ||||||
1809 | } | ||||||
1810 | |||||||
1811 | SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr, FoundOverload, Fn); | ||||||
1812 | if (!SrcExpr.isUsable()) { | ||||||
1813 | msg = 0; | ||||||
1814 | return TC_Failed; | ||||||
1815 | } | ||||||
1816 | } | ||||||
1817 | |||||||
1818 | Self.BuildBasePathArray(Paths, BasePath); | ||||||
1819 | Kind = CK_DerivedToBaseMemberPointer; | ||||||
1820 | return TC_Success; | ||||||
1821 | } | ||||||
1822 | |||||||
1823 | /// TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2 | ||||||
1824 | /// is valid: | ||||||
1825 | /// | ||||||
1826 | /// An expression e can be explicitly converted to a type T using a | ||||||
1827 | /// @c static_cast if the declaration "T t(e);" is well-formed [...]. | ||||||
1828 | TryCastResult | ||||||
1829 | TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr, QualType DestType, | ||||||
1830 | Sema::CheckedConversionKind CCK, | ||||||
1831 | SourceRange OpRange, unsigned &msg, | ||||||
1832 | CastKind &Kind, bool ListInitialization) { | ||||||
1833 | if (DestType->isRecordType()) { | ||||||
1834 | if (Self.RequireCompleteType(OpRange.getBegin(), DestType, | ||||||
1835 | diag::err_bad_cast_incomplete) || | ||||||
1836 | Self.RequireNonAbstractType(OpRange.getBegin(), DestType, | ||||||
1837 | diag::err_allocation_of_abstract_type)) { | ||||||
1838 | msg = 0; | ||||||
1839 | return TC_Failed; | ||||||
1840 | } | ||||||
1841 | } | ||||||
1842 | |||||||
1843 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(DestType); | ||||||
1844 | InitializationKind InitKind | ||||||
1845 | = (CCK == Sema::CCK_CStyleCast) | ||||||
1846 | ? InitializationKind::CreateCStyleCast(OpRange.getBegin(), OpRange, | ||||||
1847 | ListInitialization) | ||||||
1848 | : (CCK == Sema::CCK_FunctionalCast) | ||||||
1849 | ? InitializationKind::CreateFunctionalCast(OpRange, ListInitialization) | ||||||
1850 | : InitializationKind::CreateCast(OpRange); | ||||||
1851 | Expr *SrcExprRaw = SrcExpr.get(); | ||||||
1852 | // FIXME: Per DR242, we should check for an implicit conversion sequence | ||||||
1853 | // or for a constructor that could be invoked by direct-initialization | ||||||
1854 | // here, not for an initialization sequence. | ||||||
1855 | InitializationSequence InitSeq(Self, Entity, InitKind, SrcExprRaw); | ||||||
1856 | |||||||
1857 | // At this point of CheckStaticCast, if the destination is a reference, | ||||||
1858 | // or the expression is an overload expression this has to work. | ||||||
1859 | // There is no other way that works. | ||||||
1860 | // On the other hand, if we're checking a C-style cast, we've still got | ||||||
1861 | // the reinterpret_cast way. | ||||||
1862 | bool CStyle | ||||||
1863 | = (CCK == Sema::CCK_CStyleCast || CCK == Sema::CCK_FunctionalCast); | ||||||
1864 | if (InitSeq.Failed() && (CStyle || !DestType->isReferenceType())) | ||||||
1865 | return TC_NotApplicable; | ||||||
1866 | |||||||
1867 | ExprResult Result = InitSeq.Perform(Self, Entity, InitKind, SrcExprRaw); | ||||||
1868 | if (Result.isInvalid()) { | ||||||
1869 | msg = 0; | ||||||
1870 | return TC_Failed; | ||||||
1871 | } | ||||||
1872 | |||||||
1873 | if (InitSeq.isConstructorInitialization()) | ||||||
1874 | Kind = CK_ConstructorConversion; | ||||||
1875 | else | ||||||
1876 | Kind = CK_NoOp; | ||||||
1877 | |||||||
1878 | SrcExpr = Result; | ||||||
1879 | return TC_Success; | ||||||
1880 | } | ||||||
1881 | |||||||
1882 | /// TryConstCast - See if a const_cast from source to destination is allowed, | ||||||
1883 | /// and perform it if it is. | ||||||
1884 | static TryCastResult TryConstCast(Sema &Self, ExprResult &SrcExpr, | ||||||
1885 | QualType DestType, bool CStyle, | ||||||
1886 | unsigned &msg) { | ||||||
1887 | DestType = Self.Context.getCanonicalType(DestType); | ||||||
1888 | QualType SrcType = SrcExpr.get()->getType(); | ||||||
1889 | bool NeedToMaterializeTemporary = false; | ||||||
1890 | |||||||
1891 | if (const ReferenceType *DestTypeTmp =DestType->getAs<ReferenceType>()) { | ||||||
1892 | // C++11 5.2.11p4: | ||||||
1893 | // if a pointer to T1 can be explicitly converted to the type "pointer to | ||||||
1894 | // T2" using a const_cast, then the following conversions can also be | ||||||
1895 | // made: | ||||||
1896 | // -- an lvalue of type T1 can be explicitly converted to an lvalue of | ||||||
1897 | // type T2 using the cast const_cast<T2&>; | ||||||
1898 | // -- a glvalue of type T1 can be explicitly converted to an xvalue of | ||||||
1899 | // type T2 using the cast const_cast<T2&&>; and | ||||||
1900 | // -- if T1 is a class type, a prvalue of type T1 can be explicitly | ||||||
1901 | // converted to an xvalue of type T2 using the cast const_cast<T2&&>. | ||||||
1902 | |||||||
1903 | if (isa<LValueReferenceType>(DestTypeTmp) && !SrcExpr.get()->isLValue()) { | ||||||
1904 | // Cannot const_cast non-lvalue to lvalue reference type. But if this | ||||||
1905 | // is C-style, static_cast might find a way, so we simply suggest a | ||||||
1906 | // message and tell the parent to keep searching. | ||||||
1907 | msg = diag::err_bad_cxx_cast_rvalue; | ||||||
1908 | return TC_NotApplicable; | ||||||
1909 | } | ||||||
1910 | |||||||
1911 | if (isa<RValueReferenceType>(DestTypeTmp) && SrcExpr.get()->isPRValue()) { | ||||||
1912 | if (!SrcType->isRecordType()) { | ||||||
1913 | // Cannot const_cast non-class prvalue to rvalue reference type. But if | ||||||
1914 | // this is C-style, static_cast can do this. | ||||||
1915 | msg = diag::err_bad_cxx_cast_rvalue; | ||||||
1916 | return TC_NotApplicable; | ||||||
1917 | } | ||||||
1918 | |||||||
1919 | // Materialize the class prvalue so that the const_cast can bind a | ||||||
1920 | // reference to it. | ||||||
1921 | NeedToMaterializeTemporary = true; | ||||||
1922 | } | ||||||
1923 | |||||||
1924 | // It's not completely clear under the standard whether we can | ||||||
1925 | // const_cast bit-field gl-values. Doing so would not be | ||||||
1926 | // intrinsically complicated, but for now, we say no for | ||||||
1927 | // consistency with other compilers and await the word of the | ||||||
1928 | // committee. | ||||||
1929 | if (SrcExpr.get()->refersToBitField()) { | ||||||
1930 | msg = diag::err_bad_cxx_cast_bitfield; | ||||||
1931 | return TC_NotApplicable; | ||||||
1932 | } | ||||||
1933 | |||||||
1934 | DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType()); | ||||||
1935 | SrcType = Self.Context.getPointerType(SrcType); | ||||||
1936 | } | ||||||
1937 | |||||||
1938 | // C++ 5.2.11p5: For a const_cast involving pointers to data members [...] | ||||||
1939 | // the rules for const_cast are the same as those used for pointers. | ||||||
1940 | |||||||
1941 | if (!DestType->isPointerType() && | ||||||
1942 | !DestType->isMemberPointerType() && | ||||||
1943 | !DestType->isObjCObjectPointerType()) { | ||||||
1944 | // Cannot cast to non-pointer, non-reference type. Note that, if DestType | ||||||
1945 | // was a reference type, we converted it to a pointer above. | ||||||
1946 | // The status of rvalue references isn't entirely clear, but it looks like | ||||||
1947 | // conversion to them is simply invalid. | ||||||
1948 | // C++ 5.2.11p3: For two pointer types [...] | ||||||
1949 | if (!CStyle) | ||||||
1950 | msg = diag::err_bad_const_cast_dest; | ||||||
1951 | return TC_NotApplicable; | ||||||
1952 | } | ||||||
1953 | if (DestType->isFunctionPointerType() || | ||||||
1954 | DestType->isMemberFunctionPointerType()) { | ||||||
1955 | // Cannot cast direct function pointers. | ||||||
1956 | // C++ 5.2.11p2: [...] where T is any object type or the void type [...] | ||||||
1957 | // T is the ultimate pointee of source and target type. | ||||||
1958 | if (!CStyle) | ||||||
1959 | msg = diag::err_bad_const_cast_dest; | ||||||
1960 | return TC_NotApplicable; | ||||||
1961 | } | ||||||
1962 | |||||||
1963 | // C++ [expr.const.cast]p3: | ||||||
1964 | // "For two similar types T1 and T2, [...]" | ||||||
1965 | // | ||||||
1966 | // We only allow a const_cast to change cvr-qualifiers, not other kinds of | ||||||
1967 | // type qualifiers. (Likewise, we ignore other changes when determining | ||||||
1968 | // whether a cast casts away constness.) | ||||||
1969 | if (!Self.Context.hasCvrSimilarType(SrcType, DestType)) | ||||||
1970 | return TC_NotApplicable; | ||||||
1971 | |||||||
1972 | if (NeedToMaterializeTemporary) | ||||||
1973 | // This is a const_cast from a class prvalue to an rvalue reference type. | ||||||
1974 | // Materialize a temporary to store the result of the conversion. | ||||||
1975 | SrcExpr = Self.CreateMaterializeTemporaryExpr(SrcExpr.get()->getType(), | ||||||
1976 | SrcExpr.get(), | ||||||
1977 | /*IsLValueReference*/ false); | ||||||
1978 | |||||||
1979 | return TC_Success; | ||||||
1980 | } | ||||||
1981 | |||||||
1982 | // Checks for undefined behavior in reinterpret_cast. | ||||||
1983 | // The cases that is checked for is: | ||||||
1984 | // *reinterpret_cast<T*>(&a) | ||||||
1985 | // reinterpret_cast<T&>(a) | ||||||
1986 | // where accessing 'a' as type 'T' will result in undefined behavior. | ||||||
1987 | void Sema::CheckCompatibleReinterpretCast(QualType SrcType, QualType DestType, | ||||||
1988 | bool IsDereference, | ||||||
1989 | SourceRange Range) { | ||||||
1990 | unsigned DiagID = IsDereference ? | ||||||
1991 | diag::warn_pointer_indirection_from_incompatible_type : | ||||||
1992 | diag::warn_undefined_reinterpret_cast; | ||||||
1993 | |||||||
1994 | if (Diags.isIgnored(DiagID, Range.getBegin())) | ||||||
1995 | return; | ||||||
1996 | |||||||
1997 | QualType SrcTy, DestTy; | ||||||
1998 | if (IsDereference) { | ||||||
1999 | if (!SrcType->getAs<PointerType>() || !DestType->getAs<PointerType>()) { | ||||||
2000 | return; | ||||||
2001 | } | ||||||
2002 | SrcTy = SrcType->getPointeeType(); | ||||||
2003 | DestTy = DestType->getPointeeType(); | ||||||
2004 | } else { | ||||||
2005 | if (!DestType->getAs<ReferenceType>()) { | ||||||
2006 | return; | ||||||
2007 | } | ||||||
2008 | SrcTy = SrcType; | ||||||
2009 | DestTy = DestType->getPointeeType(); | ||||||
2010 | } | ||||||
2011 | |||||||
2012 | // Cast is compatible if the types are the same. | ||||||
2013 | if (Context.hasSameUnqualifiedType(DestTy, SrcTy)) { | ||||||
2014 | return; | ||||||
2015 | } | ||||||
2016 | // or one of the types is a char or void type | ||||||
2017 | if (DestTy->isAnyCharacterType() || DestTy->isVoidType() || | ||||||
2018 | SrcTy->isAnyCharacterType() || SrcTy->isVoidType()) { | ||||||
2019 | return; | ||||||
2020 | } | ||||||
2021 | // or one of the types is a tag type. | ||||||
2022 | if (SrcTy->getAs<TagType>() || DestTy->getAs<TagType>()) { | ||||||
2023 | return; | ||||||
2024 | } | ||||||
2025 | |||||||
2026 | // FIXME: Scoped enums? | ||||||
2027 | if ((SrcTy->isUnsignedIntegerType() && DestTy->isSignedIntegerType()) || | ||||||
2028 | (SrcTy->isSignedIntegerType() && DestTy->isUnsignedIntegerType())) { | ||||||
2029 | if (Context.getTypeSize(DestTy) == Context.getTypeSize(SrcTy)) { | ||||||
2030 | return; | ||||||
2031 | } | ||||||
2032 | } | ||||||
2033 | |||||||
2034 | Diag(Range.getBegin(), DiagID) << SrcType << DestType << Range; | ||||||
2035 | } | ||||||
2036 | |||||||
2037 | static void DiagnoseCastOfObjCSEL(Sema &Self, const ExprResult &SrcExpr, | ||||||
2038 | QualType DestType) { | ||||||
2039 | QualType SrcType = SrcExpr.get()->getType(); | ||||||
2040 | if (Self.Context.hasSameType(SrcType, DestType)) | ||||||
2041 | return; | ||||||
2042 | if (const PointerType *SrcPtrTy = SrcType->getAs<PointerType>()) | ||||||
2043 | if (SrcPtrTy->isObjCSelType()) { | ||||||
2044 | QualType DT = DestType; | ||||||
2045 | if (isa<PointerType>(DestType)) | ||||||
2046 | DT = DestType->getPointeeType(); | ||||||
2047 | if (!DT.getUnqualifiedType()->isVoidType()) | ||||||
2048 | Self.Diag(SrcExpr.get()->getExprLoc(), | ||||||
2049 | diag::warn_cast_pointer_from_sel) | ||||||
2050 | << SrcType << DestType << SrcExpr.get()->getSourceRange(); | ||||||
2051 | } | ||||||
2052 | } | ||||||
2053 | |||||||
2054 | /// Diagnose casts that change the calling convention of a pointer to a function | ||||||
2055 | /// defined in the current TU. | ||||||
2056 | static void DiagnoseCallingConvCast(Sema &Self, const ExprResult &SrcExpr, | ||||||
2057 | QualType DstType, SourceRange OpRange) { | ||||||
2058 | // Check if this cast would change the calling convention of a function | ||||||
2059 | // pointer type. | ||||||
2060 | QualType SrcType = SrcExpr.get()->getType(); | ||||||
2061 | if (Self.Context.hasSameType(SrcType, DstType) || | ||||||
2062 | !SrcType->isFunctionPointerType() || !DstType->isFunctionPointerType()) | ||||||
2063 | return; | ||||||
2064 | const auto *SrcFTy = | ||||||
2065 | SrcType->castAs<PointerType>()->getPointeeType()->castAs<FunctionType>(); | ||||||
2066 | const auto *DstFTy = | ||||||
2067 | DstType->castAs<PointerType>()->getPointeeType()->castAs<FunctionType>(); | ||||||
2068 | CallingConv SrcCC = SrcFTy->getCallConv(); | ||||||
2069 | CallingConv DstCC = DstFTy->getCallConv(); | ||||||
2070 | if (SrcCC == DstCC) | ||||||
2071 | return; | ||||||
2072 | |||||||
2073 | // We have a calling convention cast. Check if the source is a pointer to a | ||||||
2074 | // known, specific function that has already been defined. | ||||||
2075 | Expr *Src = SrcExpr.get()->IgnoreParenImpCasts(); | ||||||
2076 | if (auto *UO = dyn_cast<UnaryOperator>(Src)) | ||||||
2077 | if (UO->getOpcode() == UO_AddrOf) | ||||||
2078 | Src = UO->getSubExpr()->IgnoreParenImpCasts(); | ||||||
2079 | auto *DRE = dyn_cast<DeclRefExpr>(Src); | ||||||
2080 | if (!DRE) | ||||||
2081 | return; | ||||||
2082 | auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl()); | ||||||
2083 | if (!FD) | ||||||
2084 | return; | ||||||
2085 | |||||||
2086 | // Only warn if we are casting from the default convention to a non-default | ||||||
2087 | // convention. This can happen when the programmer forgot to apply the calling | ||||||
2088 | // convention to the function declaration and then inserted this cast to | ||||||
2089 | // satisfy the type system. | ||||||
2090 | CallingConv DefaultCC = Self.getASTContext().getDefaultCallingConvention( | ||||||
2091 | FD->isVariadic(), FD->isCXXInstanceMember()); | ||||||
2092 | if (DstCC == DefaultCC || SrcCC != DefaultCC) | ||||||
2093 | return; | ||||||
2094 | |||||||
2095 | // Diagnose this cast, as it is probably bad. | ||||||
2096 | StringRef SrcCCName = FunctionType::getNameForCallConv(SrcCC); | ||||||
2097 | StringRef DstCCName = FunctionType::getNameForCallConv(DstCC); | ||||||
2098 | Self.Diag(OpRange.getBegin(), diag::warn_cast_calling_conv) | ||||||
2099 | << SrcCCName << DstCCName << OpRange; | ||||||
2100 | |||||||
2101 | // The checks above are cheaper than checking if the diagnostic is enabled. | ||||||
2102 | // However, it's worth checking if the warning is enabled before we construct | ||||||
2103 | // a fixit. | ||||||
2104 | if (Self.Diags.isIgnored(diag::warn_cast_calling_conv, OpRange.getBegin())) | ||||||
2105 | return; | ||||||
2106 | |||||||
2107 | // Try to suggest a fixit to change the calling convention of the function | ||||||
2108 | // whose address was taken. Try to use the latest macro for the convention. | ||||||
2109 | // For example, users probably want to write "WINAPI" instead of "__stdcall" | ||||||
2110 | // to match the Windows header declarations. | ||||||
2111 | SourceLocation NameLoc = FD->getFirstDecl()->getNameInfo().getLoc(); | ||||||
2112 | Preprocessor &PP = Self.getPreprocessor(); | ||||||
2113 | SmallVector<TokenValue, 6> AttrTokens; | ||||||
2114 | SmallString<64> CCAttrText; | ||||||
2115 | llvm::raw_svector_ostream OS(CCAttrText); | ||||||
2116 | if (Self.getLangOpts().MicrosoftExt) { | ||||||
2117 | // __stdcall or __vectorcall | ||||||
2118 | OS << "__" << DstCCName; | ||||||
2119 | IdentifierInfo *II = PP.getIdentifierInfo(OS.str()); | ||||||
2120 | AttrTokens.push_back(II->isKeyword(Self.getLangOpts()) | ||||||
2121 | ? TokenValue(II->getTokenID()) | ||||||
2122 | : TokenValue(II)); | ||||||
2123 | } else { | ||||||
2124 | // __attribute__((stdcall)) or __attribute__((vectorcall)) | ||||||
2125 | OS << "__attribute__((" << DstCCName << "))"; | ||||||
2126 | AttrTokens.push_back(tok::kw___attribute); | ||||||
2127 | AttrTokens.push_back(tok::l_paren); | ||||||
2128 | AttrTokens.push_back(tok::l_paren); | ||||||
2129 | IdentifierInfo *II = PP.getIdentifierInfo(DstCCName); | ||||||
2130 | AttrTokens.push_back(II->isKeyword(Self.getLangOpts()) | ||||||
2131 | ? TokenValue(II->getTokenID()) | ||||||
2132 | : TokenValue(II)); | ||||||
2133 | AttrTokens.push_back(tok::r_paren); | ||||||
2134 | AttrTokens.push_back(tok::r_paren); | ||||||
2135 | } | ||||||
2136 | StringRef AttrSpelling = PP.getLastMacroWithSpelling(NameLoc, AttrTokens); | ||||||
2137 | if (!AttrSpelling.empty()) | ||||||
2138 | CCAttrText = AttrSpelling; | ||||||
2139 | OS << ' '; | ||||||
2140 | Self.Diag(NameLoc, diag::note_change_calling_conv_fixit) | ||||||
2141 | << FD << DstCCName << FixItHint::CreateInsertion(NameLoc, CCAttrText); | ||||||
2142 | } | ||||||
2143 | |||||||
2144 | static void checkIntToPointerCast(bool CStyle, const SourceRange &OpRange, | ||||||
2145 | const Expr *SrcExpr, QualType DestType, | ||||||
2146 | Sema &Self) { | ||||||
2147 | QualType SrcType = SrcExpr->getType(); | ||||||
2148 | |||||||
2149 | // Not warning on reinterpret_cast, boolean, constant expressions, etc | ||||||
2150 | // are not explicit design choices, but consistent with GCC's behavior. | ||||||
2151 | // Feel free to modify them if you've reason/evidence for an alternative. | ||||||
2152 | if (CStyle && SrcType->isIntegralType(Self.Context) | ||||||
2153 | && !SrcType->isBooleanType() | ||||||
2154 | && !SrcType->isEnumeralType() | ||||||
2155 | && !SrcExpr->isIntegerConstantExpr(Self.Context) | ||||||
2156 | && Self.Context.getTypeSize(DestType) > | ||||||
2157 | Self.Context.getTypeSize(SrcType)) { | ||||||
2158 | // Separate between casts to void* and non-void* pointers. | ||||||
2159 | // Some APIs use (abuse) void* for something like a user context, | ||||||
2160 | // and often that value is an integer even if it isn't a pointer itself. | ||||||
2161 | // Having a separate warning flag allows users to control the warning | ||||||
2162 | // for their workflow. | ||||||
2163 | unsigned Diag = DestType->isVoidPointerType() ? | ||||||
2164 | diag::warn_int_to_void_pointer_cast | ||||||
2165 | : diag::warn_int_to_pointer_cast; | ||||||
2166 | Self.Diag(OpRange.getBegin(), Diag) << SrcType << DestType << OpRange; | ||||||
2167 | } | ||||||
2168 | } | ||||||
2169 | |||||||
2170 | static bool fixOverloadedReinterpretCastExpr(Sema &Self, QualType DestType, | ||||||
2171 | ExprResult &Result) { | ||||||
2172 | // We can only fix an overloaded reinterpret_cast if | ||||||
2173 | // - it is a template with explicit arguments that resolves to an lvalue | ||||||
2174 | // unambiguously, or | ||||||
2175 | // - it is the only function in an overload set that may have its address | ||||||
2176 | // taken. | ||||||
2177 | |||||||
2178 | Expr *E = Result.get(); | ||||||
2179 | // TODO: what if this fails because of DiagnoseUseOfDecl or something | ||||||
2180 | // like it? | ||||||
2181 | if (Self.ResolveAndFixSingleFunctionTemplateSpecialization( | ||||||
2182 | Result, | ||||||
2183 | Expr::getValueKindForType(DestType) == | ||||||
2184 | VK_PRValue // Convert Fun to Ptr | ||||||
2185 | ) && | ||||||
2186 | Result.isUsable()) | ||||||
2187 | return true; | ||||||
2188 | |||||||
2189 | // No guarantees that ResolveAndFixSingleFunctionTemplateSpecialization | ||||||
2190 | // preserves Result. | ||||||
2191 | Result = E; | ||||||
2192 | if (!Self.resolveAndFixAddressOfSingleOverloadCandidate( | ||||||
2193 | Result, /*DoFunctionPointerConversion=*/true)) | ||||||
2194 | return false; | ||||||
2195 | return Result.isUsable(); | ||||||
2196 | } | ||||||
2197 | |||||||
2198 | static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr, | ||||||
2199 | QualType DestType, bool CStyle, | ||||||
2200 | SourceRange OpRange, | ||||||
2201 | unsigned &msg, | ||||||
2202 | CastKind &Kind) { | ||||||
2203 | bool IsLValueCast = false; | ||||||
2204 | |||||||
2205 | DestType = Self.Context.getCanonicalType(DestType); | ||||||
2206 | QualType SrcType = SrcExpr.get()->getType(); | ||||||
2207 | |||||||
2208 | // Is the source an overloaded name? (i.e. &foo) | ||||||
2209 | // If so, reinterpret_cast generally can not help us here (13.4, p1, bullet 5) | ||||||
2210 | if (SrcType == Self.Context.OverloadTy) { | ||||||
2211 | ExprResult FixedExpr = SrcExpr; | ||||||
2212 | if (!fixOverloadedReinterpretCastExpr(Self, DestType, FixedExpr)) | ||||||
2213 | return TC_NotApplicable; | ||||||
2214 | |||||||
2215 | assert(FixedExpr.isUsable() && "Invalid result fixing overloaded expr")(static_cast <bool> (FixedExpr.isUsable() && "Invalid result fixing overloaded expr" ) ? void (0) : __assert_fail ("FixedExpr.isUsable() && \"Invalid result fixing overloaded expr\"" , "clang/lib/Sema/SemaCast.cpp", 2215, __extension__ __PRETTY_FUNCTION__ )); | ||||||
2216 | SrcExpr = FixedExpr; | ||||||
2217 | SrcType = SrcExpr.get()->getType(); | ||||||
2218 | } | ||||||
2219 | |||||||
2220 | if (const ReferenceType *DestTypeTmp = DestType->getAs<ReferenceType>()) { | ||||||
2221 | if (!SrcExpr.get()->isGLValue()) { | ||||||
2222 | // Cannot cast non-glvalue to (lvalue or rvalue) reference type. See the | ||||||
2223 | // similar comment in const_cast. | ||||||
2224 | msg = diag::err_bad_cxx_cast_rvalue; | ||||||
2225 | return TC_NotApplicable; | ||||||
2226 | } | ||||||
2227 | |||||||
2228 | if (!CStyle) { | ||||||
2229 | Self.CheckCompatibleReinterpretCast(SrcType, DestType, | ||||||
2230 | /*IsDereference=*/false, OpRange); | ||||||
2231 | } | ||||||
2232 | |||||||
2233 | // C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the | ||||||
2234 | // same effect as the conversion *reinterpret_cast<T*>(&x) with the | ||||||
2235 | // built-in & and * operators. | ||||||
2236 | |||||||
2237 | const char *inappropriate = nullptr; | ||||||
2238 | switch (SrcExpr.get()->getObjectKind()) { | ||||||
2239 | case OK_Ordinary: | ||||||
2240 | break; | ||||||
2241 | case OK_BitField: | ||||||
2242 | msg = diag::err_bad_cxx_cast_bitfield; | ||||||
2243 | return TC_NotApplicable; | ||||||
2244 | // FIXME: Use a specific diagnostic for the rest of these cases. | ||||||
2245 | case OK_VectorComponent: inappropriate = "vector element"; break; | ||||||
2246 | case OK_MatrixComponent: | ||||||
2247 | inappropriate = "matrix element"; | ||||||
2248 | break; | ||||||
2249 | case OK_ObjCProperty: inappropriate = "property expression"; break; | ||||||
2250 | case OK_ObjCSubscript: inappropriate = "container subscripting expression"; | ||||||
2251 | break; | ||||||
2252 | } | ||||||
2253 | if (inappropriate) { | ||||||
2254 | Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_reference) | ||||||
2255 | << inappropriate << DestType | ||||||
2256 | << OpRange << SrcExpr.get()->getSourceRange(); | ||||||
2257 | msg = 0; SrcExpr = ExprError(); | ||||||
2258 | return TC_NotApplicable; | ||||||
2259 | } | ||||||
2260 | |||||||
2261 | // This code does this transformation for the checked types. | ||||||
2262 | DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType()); | ||||||
2263 | SrcType = Self.Context.getPointerType(SrcType); | ||||||
2264 | |||||||
2265 | IsLValueCast = true; | ||||||
2266 | } | ||||||
2267 | |||||||
2268 | // Canonicalize source for comparison. | ||||||
2269 | SrcType = Self.Context.getCanonicalType(SrcType); | ||||||
2270 | |||||||
2271 | const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(), | ||||||
2272 | *SrcMemPtr = SrcType->getAs<MemberPointerType>(); | ||||||
2273 | if (DestMemPtr && SrcMemPtr) { | ||||||
2274 | // C++ 5.2.10p9: An rvalue of type "pointer to member of X of type T1" | ||||||
2275 | // can be explicitly converted to an rvalue of type "pointer to member | ||||||
2276 | // of Y of type T2" if T1 and T2 are both function types or both object | ||||||
2277 | // types. | ||||||
2278 | if (DestMemPtr->isMemberFunctionPointer() != | ||||||
2279 | SrcMemPtr->isMemberFunctionPointer()) | ||||||
2280 | return TC_NotApplicable; | ||||||
2281 | |||||||
2282 | if (Self.Context.getTargetInfo().getCXXABI().isMicrosoft()) { | ||||||
2283 | // We need to determine the inheritance model that the class will use if | ||||||
2284 | // haven't yet. | ||||||
2285 | (void)Self.isCompleteType(OpRange.getBegin(), SrcType); | ||||||
2286 | (void)Self.isCompleteType(OpRange.getBegin(), DestType); | ||||||
2287 | } | ||||||
2288 | |||||||
2289 | // Don't allow casting between member pointers of different sizes. | ||||||
2290 | if (Self.Context.getTypeSize(DestMemPtr) != | ||||||
2291 | Self.Context.getTypeSize(SrcMemPtr)) { | ||||||
2292 | msg = diag::err_bad_cxx_cast_member_pointer_size; | ||||||
2293 | return TC_Failed; | ||||||
2294 | } | ||||||
2295 | |||||||
2296 | // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away | ||||||
2297 | // constness. | ||||||
2298 | // A reinterpret_cast followed by a const_cast can, though, so in C-style, | ||||||
2299 | // we accept it. | ||||||
2300 | if (auto CACK = | ||||||
2301 | CastsAwayConstness(Self, SrcType, DestType, /*CheckCVR=*/!CStyle, | ||||||
2302 | /*CheckObjCLifetime=*/CStyle)) | ||||||
2303 | return getCastAwayConstnessCastKind(CACK, msg); | ||||||
2304 | |||||||
2305 | // A valid member pointer cast. | ||||||
2306 | assert(!IsLValueCast)(static_cast <bool> (!IsLValueCast) ? void (0) : __assert_fail ("!IsLValueCast", "clang/lib/Sema/SemaCast.cpp", 2306, __extension__ __PRETTY_FUNCTION__)); | ||||||
2307 | Kind = CK_ReinterpretMemberPointer; | ||||||
2308 | return TC_Success; | ||||||
2309 | } | ||||||
2310 | |||||||
2311 | // See below for the enumeral issue. | ||||||
2312 | if (SrcType->isNullPtrType() && DestType->isIntegralType(Self.Context)) { | ||||||
2313 | // C++0x 5.2.10p4: A pointer can be explicitly converted to any integral | ||||||
2314 | // type large enough to hold it. A value of std::nullptr_t can be | ||||||
2315 | // converted to an integral type; the conversion has the same meaning | ||||||
2316 | // and validity as a conversion of (void*)0 to the integral type. | ||||||
2317 | if (Self.Context.getTypeSize(SrcType) > | ||||||
2318 | Self.Context.getTypeSize(DestType)) { | ||||||
2319 | msg = diag::err_bad_reinterpret_cast_small_int; | ||||||
2320 | return TC_Failed; | ||||||
2321 | } | ||||||
2322 | Kind = CK_PointerToIntegral; | ||||||
2323 | return TC_Success; | ||||||
2324 | } | ||||||
2325 | |||||||
2326 | // Allow reinterpret_casts between vectors of the same size and | ||||||
2327 | // between vectors and integers of the same size. | ||||||
2328 | bool destIsVector = DestType->isVectorType(); | ||||||
2329 | bool srcIsVector = SrcType->isVectorType(); | ||||||
2330 | if (srcIsVector || destIsVector) { | ||||||
2331 | // Allow bitcasting between SVE VLATs and VLSTs, and vice-versa. | ||||||
2332 | if (Self.isValidSveBitcast(SrcType, DestType)) { | ||||||
2333 | Kind = CK_BitCast; | ||||||
2334 | return TC_Success; | ||||||
2335 | } | ||||||
2336 | |||||||
2337 | // The non-vector type, if any, must have integral type. This is | ||||||
2338 | // the same rule that C vector casts use; note, however, that enum | ||||||
2339 | // types are not integral in C++. | ||||||
2340 | if ((!destIsVector && !DestType->isIntegralType(Self.Context)) || | ||||||
2341 | (!srcIsVector && !SrcType->isIntegralType(Self.Context))) | ||||||
2342 | return TC_NotApplicable; | ||||||
2343 | |||||||
2344 | // The size we want to consider is eltCount * eltSize. | ||||||
2345 | // That's exactly what the lax-conversion rules will check. | ||||||
2346 | if (Self.areLaxCompatibleVectorTypes(SrcType, DestType)) { | ||||||
2347 | Kind = CK_BitCast; | ||||||
2348 | return TC_Success; | ||||||
2349 | } | ||||||
2350 | |||||||
2351 | if (Self.LangOpts.OpenCL && !CStyle) { | ||||||
2352 | if (DestType->isExtVectorType() || SrcType->isExtVectorType()) { | ||||||
2353 | // FIXME: Allow for reinterpret cast between 3 and 4 element vectors | ||||||
2354 | if (Self.areVectorTypesSameSize(SrcType, DestType)) { | ||||||
2355 | Kind = CK_BitCast; | ||||||
2356 | return TC_Success; | ||||||
2357 | } | ||||||
2358 | } | ||||||
2359 | } | ||||||
2360 | |||||||
2361 | // Otherwise, pick a reasonable diagnostic. | ||||||
2362 | if (!destIsVector) | ||||||
2363 | msg = diag::err_bad_cxx_cast_vector_to_scalar_different_size; | ||||||
2364 | else if (!srcIsVector) | ||||||
2365 | msg = diag::err_bad_cxx_cast_scalar_to_vector_different_size; | ||||||
2366 | else | ||||||
2367 | msg = diag::err_bad_cxx_cast_vector_to_vector_different_size; | ||||||
2368 | |||||||
2369 | return TC_Failed; | ||||||
2370 | } | ||||||
2371 | |||||||
2372 | if (SrcType == DestType) { | ||||||
2373 | // C++ 5.2.10p2 has a note that mentions that, subject to all other | ||||||
2374 | // restrictions, a cast to the same type is allowed so long as it does not | ||||||
2375 | // cast away constness. In C++98, the intent was not entirely clear here, | ||||||
2376 | // since all other paragraphs explicitly forbid casts to the same type. | ||||||
2377 | // C++11 clarifies this case with p2. | ||||||
2378 | // | ||||||
2379 | // The only allowed types are: integral, enumeration, pointer, or | ||||||
2380 | // pointer-to-member types. We also won't restrict Obj-C pointers either. | ||||||
2381 | Kind = CK_NoOp; | ||||||
2382 | TryCastResult Result = TC_NotApplicable; | ||||||
2383 | if (SrcType->isIntegralOrEnumerationType() || | ||||||
2384 | SrcType->isAnyPointerType() || | ||||||
2385 | SrcType->isMemberPointerType() || | ||||||
2386 | SrcType->isBlockPointerType()) { | ||||||
2387 | Result = TC_Success; | ||||||
2388 | } | ||||||
2389 | return Result; | ||||||
2390 | } | ||||||
2391 | |||||||
2392 | bool destIsPtr = DestType->isAnyPointerType() || | ||||||
2393 | DestType->isBlockPointerType(); | ||||||
2394 | bool srcIsPtr = SrcType->isAnyPointerType() || | ||||||
2395 | SrcType->isBlockPointerType(); | ||||||
2396 | if (!destIsPtr && !srcIsPtr) { | ||||||
2397 | // Except for std::nullptr_t->integer and lvalue->reference, which are | ||||||
2398 | // handled above, at least one of the two arguments must be a pointer. | ||||||
2399 | return TC_NotApplicable; | ||||||
2400 | } | ||||||
2401 | |||||||
2402 | if (DestType->isIntegralType(Self.Context)) { | ||||||
2403 | assert(srcIsPtr && "One type must be a pointer")(static_cast <bool> (srcIsPtr && "One type must be a pointer" ) ? void (0) : __assert_fail ("srcIsPtr && \"One type must be a pointer\"" , "clang/lib/Sema/SemaCast.cpp", 2403, __extension__ __PRETTY_FUNCTION__ )); | ||||||
2404 | // C++ 5.2.10p4: A pointer can be explicitly converted to any integral | ||||||
2405 | // type large enough to hold it; except in Microsoft mode, where the | ||||||
2406 | // integral type size doesn't matter (except we don't allow bool). | ||||||
2407 | if ((Self.Context.getTypeSize(SrcType) > | ||||||
2408 | Self.Context.getTypeSize(DestType))) { | ||||||
2409 | bool MicrosoftException = | ||||||
2410 | Self.getLangOpts().MicrosoftExt && !DestType->isBooleanType(); | ||||||
2411 | if (MicrosoftException) { | ||||||
2412 | unsigned Diag = SrcType->isVoidPointerType() | ||||||
2413 | ? diag::warn_void_pointer_to_int_cast | ||||||
2414 | : diag::warn_pointer_to_int_cast; | ||||||
2415 | Self.Diag(OpRange.getBegin(), Diag) << SrcType << DestType << OpRange; | ||||||
2416 | } else { | ||||||
2417 | msg = diag::err_bad_reinterpret_cast_small_int; | ||||||
2418 | return TC_Failed; | ||||||
2419 | } | ||||||
2420 | } | ||||||
2421 | Kind = CK_PointerToIntegral; | ||||||
2422 | return TC_Success; | ||||||
2423 | } | ||||||
2424 | |||||||
2425 | if (SrcType->isIntegralOrEnumerationType()) { | ||||||
2426 | assert(destIsPtr && "One type must be a pointer")(static_cast <bool> (destIsPtr && "One type must be a pointer" ) ? void (0) : __assert_fail ("destIsPtr && \"One type must be a pointer\"" , "clang/lib/Sema/SemaCast.cpp", 2426, __extension__ __PRETTY_FUNCTION__ )); | ||||||
2427 | checkIntToPointerCast(CStyle, OpRange, SrcExpr.get(), DestType, Self); | ||||||
2428 | // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly | ||||||
2429 | // converted to a pointer. | ||||||
2430 | // C++ 5.2.10p9: [Note: ...a null pointer constant of integral type is not | ||||||
2431 | // necessarily converted to a null pointer value.] | ||||||
2432 | Kind = CK_IntegralToPointer; | ||||||
2433 | return TC_Success; | ||||||
2434 | } | ||||||
2435 | |||||||
2436 | if (!destIsPtr || !srcIsPtr) { | ||||||
2437 | // With the valid non-pointer conversions out of the way, we can be even | ||||||
2438 | // more stringent. | ||||||
2439 | return TC_NotApplicable; | ||||||
2440 | } | ||||||
2441 | |||||||
2442 | // Cannot convert between block pointers and Objective-C object pointers. | ||||||
2443 | if ((SrcType->isBlockPointerType() && DestType->isObjCObjectPointerType()) || | ||||||
2444 | (DestType->isBlockPointerType() && SrcType->isObjCObjectPointerType())) | ||||||
2445 | return TC_NotApplicable; | ||||||
2446 | |||||||
2447 | // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness. | ||||||
2448 | // The C-style cast operator can. | ||||||
2449 | TryCastResult SuccessResult = TC_Success; | ||||||
2450 | if (auto CACK = | ||||||
2451 | CastsAwayConstness(Self, SrcType, DestType, /*CheckCVR=*/!CStyle, | ||||||
2452 | /*CheckObjCLifetime=*/CStyle)) | ||||||
2453 | SuccessResult = getCastAwayConstnessCastKind(CACK, msg); | ||||||
2454 | |||||||
2455 | if (IsAddressSpaceConversion(SrcType, DestType)) { | ||||||
2456 | Kind = CK_AddressSpaceConversion; | ||||||
2457 | assert(SrcType->isPointerType() && DestType->isPointerType())(static_cast <bool> (SrcType->isPointerType() && DestType->isPointerType()) ? void (0) : __assert_fail ("SrcType->isPointerType() && DestType->isPointerType()" , "clang/lib/Sema/SemaCast.cpp", 2457, __extension__ __PRETTY_FUNCTION__ )); | ||||||
2458 | if (!CStyle && | ||||||
2459 | !DestType->getPointeeType().getQualifiers().isAddressSpaceSupersetOf( | ||||||
2460 | SrcType->getPointeeType().getQualifiers())) { | ||||||
2461 | SuccessResult = TC_Failed; | ||||||
2462 | } | ||||||
2463 | } else if (IsLValueCast) { | ||||||
2464 | Kind = CK_LValueBitCast; | ||||||
2465 | } else if (DestType->isObjCObjectPointerType()) { | ||||||
2466 | Kind = Self.PrepareCastToObjCObjectPointer(SrcExpr); | ||||||
2467 | } else if (DestType->isBlockPointerType()) { | ||||||
2468 | if (!SrcType->isBlockPointerType()) { | ||||||
2469 | Kind = CK_AnyPointerToBlockPointerCast; | ||||||
2470 | } else { | ||||||
2471 | Kind = CK_BitCast; | ||||||
2472 | } | ||||||
2473 | } else { | ||||||
2474 | Kind = CK_BitCast; | ||||||
2475 | } | ||||||
2476 | |||||||
2477 | // Any pointer can be cast to an Objective-C pointer type with a C-style | ||||||
2478 | // cast. | ||||||
2479 | if (CStyle && DestType->isObjCObjectPointerType()) { | ||||||
2480 | return SuccessResult; | ||||||
2481 | } | ||||||
2482 | if (CStyle) | ||||||
2483 | DiagnoseCastOfObjCSEL(Self, SrcExpr, DestType); | ||||||
2484 | |||||||
2485 | DiagnoseCallingConvCast(Self, SrcExpr, DestType, OpRange); | ||||||
2486 | |||||||
2487 | // Not casting away constness, so the only remaining check is for compatible | ||||||
2488 | // pointer categories. | ||||||
2489 | |||||||
2490 | if (SrcType->isFunctionPointerType()) { | ||||||
2491 | if (DestType->isFunctionPointerType()) { | ||||||
2492 | // C++ 5.2.10p6: A pointer to a function can be explicitly converted to | ||||||
2493 | // a pointer to a function of a different type. | ||||||
2494 | return SuccessResult; | ||||||
2495 | } | ||||||
2496 | |||||||
2497 | // C++0x 5.2.10p8: Converting a pointer to a function into a pointer to | ||||||
2498 | // an object type or vice versa is conditionally-supported. | ||||||
2499 | // Compilers support it in C++03 too, though, because it's necessary for | ||||||
2500 | // casting the return value of dlsym() and GetProcAddress(). | ||||||
2501 | // FIXME: Conditionally-supported behavior should be configurable in the | ||||||
2502 | // TargetInfo or similar. | ||||||
2503 | Self.Diag(OpRange.getBegin(), | ||||||
2504 | Self.getLangOpts().CPlusPlus11 ? | ||||||
2505 | diag::warn_cxx98_compat_cast_fn_obj : diag::ext_cast_fn_obj) | ||||||
2506 | << OpRange; | ||||||
2507 | return SuccessResult; | ||||||
2508 | } | ||||||
2509 | |||||||
2510 | if (DestType->isFunctionPointerType()) { | ||||||
2511 | // See above. | ||||||
2512 | Self.Diag(OpRange.getBegin(), | ||||||
2513 | Self.getLangOpts().CPlusPlus11 ? | ||||||
2514 | diag::warn_cxx98_compat_cast_fn_obj : diag::ext_cast_fn_obj) | ||||||
2515 | << OpRange; | ||||||
2516 | return SuccessResult; | ||||||
2517 | } | ||||||
2518 | |||||||
2519 | // Diagnose address space conversion in nested pointers. | ||||||
2520 | QualType DestPtee = DestType->getPointeeType().isNull() | ||||||
2521 | ? DestType->getPointeeType() | ||||||
2522 | : DestType->getPointeeType()->getPointeeType(); | ||||||
2523 | QualType SrcPtee = SrcType->getPointeeType().isNull() | ||||||
2524 | ? SrcType->getPointeeType() | ||||||
2525 | : SrcType->getPointeeType()->getPointeeType(); | ||||||
2526 | while (!DestPtee.isNull() && !SrcPtee.isNull()) { | ||||||
2527 | if (DestPtee.getAddressSpace() != SrcPtee.getAddressSpace()) { | ||||||
2528 | Self.Diag(OpRange.getBegin(), | ||||||
2529 | diag::warn_bad_cxx_cast_nested_pointer_addr_space) | ||||||
2530 | << CStyle << SrcType << DestType << SrcExpr.get()->getSourceRange(); | ||||||
2531 | break; | ||||||
2532 | } | ||||||
2533 | DestPtee = DestPtee->getPointeeType(); | ||||||
2534 | SrcPtee = SrcPtee->getPointeeType(); | ||||||
2535 | } | ||||||
2536 | |||||||
2537 | // C++ 5.2.10p7: A pointer to an object can be explicitly converted to | ||||||
2538 | // a pointer to an object of different type. | ||||||
2539 | // Void pointers are not specified, but supported by every compiler out there. | ||||||
2540 | // So we finish by allowing everything that remains - it's got to be two | ||||||
2541 | // object pointers. | ||||||
2542 | return SuccessResult; | ||||||
2543 | } | ||||||
2544 | |||||||
2545 | static TryCastResult TryAddressSpaceCast(Sema &Self, ExprResult &SrcExpr, | ||||||
2546 | QualType DestType, bool CStyle, | ||||||
2547 | unsigned &msg, CastKind &Kind) { | ||||||
2548 | if (!Self.getLangOpts().OpenCL && !Self.getLangOpts().SYCLIsDevice) | ||||||
2549 | // FIXME: As compiler doesn't have any information about overlapping addr | ||||||
2550 | // spaces at the moment we have to be permissive here. | ||||||
2551 | return TC_NotApplicable; | ||||||
2552 | // Even though the logic below is general enough and can be applied to | ||||||
2553 | // non-OpenCL mode too, we fast-path above because no other languages | ||||||
2554 | // define overlapping address spaces currently. | ||||||
2555 | auto SrcType = SrcExpr.get()->getType(); | ||||||
2556 | // FIXME: Should this be generalized to references? The reference parameter | ||||||
2557 | // however becomes a reference pointee type here and therefore rejected. | ||||||
2558 | // Perhaps this is the right behavior though according to C++. | ||||||
2559 | auto SrcPtrType = SrcType->getAs<PointerType>(); | ||||||
2560 | if (!SrcPtrType) | ||||||
2561 | return TC_NotApplicable; | ||||||
2562 | auto DestPtrType = DestType->getAs<PointerType>(); | ||||||
2563 | if (!DestPtrType) | ||||||
2564 | return TC_NotApplicable; | ||||||
2565 | auto SrcPointeeType = SrcPtrType->getPointeeType(); | ||||||
2566 | auto DestPointeeType = DestPtrType->getPointeeType(); | ||||||
2567 | if (!DestPointeeType.isAddressSpaceOverlapping(SrcPointeeType)) { | ||||||
2568 | msg = diag::err_bad_cxx_cast_addr_space_mismatch; | ||||||
2569 | return TC_Failed; | ||||||
2570 | } | ||||||
2571 | auto SrcPointeeTypeWithoutAS = | ||||||
2572 | Self.Context.removeAddrSpaceQualType(SrcPointeeType.getCanonicalType()); | ||||||
2573 | auto DestPointeeTypeWithoutAS = | ||||||
2574 | Self.Context.removeAddrSpaceQualType(DestPointeeType.getCanonicalType()); | ||||||
2575 | if (Self.Context.hasSameType(SrcPointeeTypeWithoutAS, | ||||||
2576 | DestPointeeTypeWithoutAS)) { | ||||||
2577 | Kind = SrcPointeeType.getAddressSpace() == DestPointeeType.getAddressSpace() | ||||||
2578 | ? CK_NoOp | ||||||
2579 | : CK_AddressSpaceConversion; | ||||||
2580 | return TC_Success; | ||||||
2581 | } else { | ||||||
2582 | return TC_NotApplicable; | ||||||
2583 | } | ||||||
2584 | } | ||||||
2585 | |||||||
2586 | void CastOperation::checkAddressSpaceCast(QualType SrcType, QualType DestType) { | ||||||
2587 | // In OpenCL only conversions between pointers to objects in overlapping | ||||||
2588 | // addr spaces are allowed. v2.0 s6.5.5 - Generic addr space overlaps | ||||||
2589 | // with any named one, except for constant. | ||||||
2590 | |||||||
2591 | // Converting the top level pointee addrspace is permitted for compatible | ||||||
2592 | // addrspaces (such as 'generic int *' to 'local int *' or vice versa), but | ||||||
2593 | // if any of the nested pointee addrspaces differ, we emit a warning | ||||||
2594 | // regardless of addrspace compatibility. This makes | ||||||
2595 | // local int ** p; | ||||||
2596 | // return (generic int **) p; | ||||||
2597 | // warn even though local -> generic is permitted. | ||||||
2598 | if (Self.getLangOpts().OpenCL) { | ||||||
2599 | const Type *DestPtr, *SrcPtr; | ||||||
2600 | bool Nested = false; | ||||||
2601 | unsigned DiagID = diag::err_typecheck_incompatible_address_space; | ||||||
2602 | DestPtr = Self.getASTContext().getCanonicalType(DestType.getTypePtr()), | ||||||
2603 | SrcPtr = Self.getASTContext().getCanonicalType(SrcType.getTypePtr()); | ||||||
2604 | |||||||
2605 | while (isa<PointerType>(DestPtr) && isa<PointerType>(SrcPtr)) { | ||||||
2606 | const PointerType *DestPPtr = cast<PointerType>(DestPtr); | ||||||
2607 | const PointerType *SrcPPtr = cast<PointerType>(SrcPtr); | ||||||
2608 | QualType DestPPointee = DestPPtr->getPointeeType(); | ||||||
2609 | QualType SrcPPointee = SrcPPtr->getPointeeType(); | ||||||
2610 | if (Nested | ||||||
2611 | ? DestPPointee.getAddressSpace() != SrcPPointee.getAddressSpace() | ||||||
2612 | : !DestPPointee.isAddressSpaceOverlapping(SrcPPointee)) { | ||||||
2613 | Self.Diag(OpRange.getBegin(), DiagID) | ||||||
2614 | << SrcType << DestType << Sema::AA_Casting | ||||||
2615 | << SrcExpr.get()->getSourceRange(); | ||||||
2616 | if (!Nested) | ||||||
2617 | SrcExpr = ExprError(); | ||||||
2618 | return; | ||||||
2619 | } | ||||||
2620 | |||||||
2621 | DestPtr = DestPPtr->getPointeeType().getTypePtr(); | ||||||
2622 | SrcPtr = SrcPPtr->getPointeeType().getTypePtr(); | ||||||
2623 | Nested = true; | ||||||
2624 | DiagID = diag::ext_nested_pointer_qualifier_mismatch; | ||||||
2625 | } | ||||||
2626 | } | ||||||
2627 | } | ||||||
2628 | |||||||
2629 | bool Sema::ShouldSplatAltivecScalarInCast(const VectorType *VecTy) { | ||||||
2630 | bool SrcCompatXL = this->getLangOpts().getAltivecSrcCompat() == | ||||||
2631 | LangOptions::AltivecSrcCompatKind::XL; | ||||||
2632 | VectorType::VectorKind VKind = VecTy->getVectorKind(); | ||||||
2633 | |||||||
2634 | if ((VKind == VectorType::AltiVecVector) || | ||||||
2635 | (SrcCompatXL && ((VKind == VectorType::AltiVecBool) || | ||||||
2636 | (VKind == VectorType::AltiVecPixel)))) { | ||||||
2637 | return true; | ||||||
2638 | } | ||||||
2639 | return false; | ||||||
2640 | } | ||||||
2641 | |||||||
2642 | bool Sema::CheckAltivecInitFromScalar(SourceRange R, QualType VecTy, | ||||||
2643 | QualType SrcTy) { | ||||||
2644 | bool SrcCompatGCC = this->getLangOpts().getAltivecSrcCompat() == | ||||||
2645 | LangOptions::AltivecSrcCompatKind::GCC; | ||||||
2646 | if (this->getLangOpts().AltiVec && SrcCompatGCC) { | ||||||
2647 | this->Diag(R.getBegin(), | ||||||
2648 | diag::err_invalid_conversion_between_vector_and_integer) | ||||||
2649 | << VecTy << SrcTy << R; | ||||||
2650 | return true; | ||||||
2651 | } | ||||||
2652 | return false; | ||||||
2653 | } | ||||||
2654 | |||||||
2655 | void CastOperation::CheckCXXCStyleCast(bool FunctionalStyle, | ||||||
2656 | bool ListInitialization) { | ||||||
2657 | assert(Self.getLangOpts().CPlusPlus)(static_cast <bool> (Self.getLangOpts().CPlusPlus) ? void (0) : __assert_fail ("Self.getLangOpts().CPlusPlus", "clang/lib/Sema/SemaCast.cpp" , 2657, __extension__ __PRETTY_FUNCTION__)); | ||||||
2658 | |||||||
2659 | // Handle placeholders. | ||||||
2660 | if (isPlaceholder()) { | ||||||
2661 | // C-style casts can resolve __unknown_any types. | ||||||
2662 | if (claimPlaceholder(BuiltinType::UnknownAny)) { | ||||||
2663 | SrcExpr = Self.checkUnknownAnyCast(DestRange, DestType, | ||||||
2664 | SrcExpr.get(), Kind, | ||||||
2665 | ValueKind, BasePath); | ||||||
2666 | return; | ||||||
2667 | } | ||||||
2668 | |||||||
2669 | checkNonOverloadPlaceholders(); | ||||||
2670 | if (SrcExpr.isInvalid()) | ||||||
2671 | return; | ||||||
2672 | } | ||||||
2673 | |||||||
2674 | // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void". | ||||||
2675 | // This test is outside everything else because it's the only case where | ||||||
2676 | // a non-lvalue-reference target type does not lead to decay. | ||||||
2677 | if (DestType->isVoidType()) { | ||||||
2678 | Kind = CK_ToVoid; | ||||||
2679 | |||||||
2680 | if (claimPlaceholder(BuiltinType::Overload)) { | ||||||
2681 | Self.ResolveAndFixSingleFunctionTemplateSpecialization( | ||||||
2682 | SrcExpr, /* Decay Function to ptr */ false, | ||||||
2683 | /* Complain */ true, DestRange, DestType, | ||||||
2684 | diag::err_bad_cstyle_cast_overload); | ||||||
2685 | if (SrcExpr.isInvalid()) | ||||||
2686 | return; | ||||||
2687 | } | ||||||
2688 | |||||||
2689 | SrcExpr = Self.IgnoredValueConversions(SrcExpr.get()); | ||||||
2690 | return; | ||||||
2691 | } | ||||||
2692 | |||||||
2693 | // If the type is dependent, we won't do any other semantic analysis now. | ||||||
2694 | if (DestType->isDependentType() || SrcExpr.get()->isTypeDependent() || | ||||||
2695 | SrcExpr.get()->isValueDependent()) { | ||||||
2696 | assert(Kind == CK_Dependent)(static_cast <bool> (Kind == CK_Dependent) ? void (0) : __assert_fail ("Kind == CK_Dependent", "clang/lib/Sema/SemaCast.cpp" , 2696, __extension__ __PRETTY_FUNCTION__)); | ||||||
2697 | return; | ||||||
2698 | } | ||||||
2699 | |||||||
2700 | if (ValueKind == VK_PRValue && !DestType->isRecordType() && | ||||||
2701 | !isPlaceholder(BuiltinType::Overload)) { | ||||||
2702 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get()); | ||||||
2703 | if (SrcExpr.isInvalid()) | ||||||
2704 | return; | ||||||
2705 | } | ||||||
2706 | |||||||
2707 | // AltiVec vector initialization with a single literal. | ||||||
2708 | if (const VectorType *vecTy = DestType->getAs<VectorType>()) { | ||||||
2709 | if (Self.CheckAltivecInitFromScalar(OpRange, DestType, | ||||||
2710 | SrcExpr.get()->getType())) { | ||||||
2711 | SrcExpr = ExprError(); | ||||||
2712 | return; | ||||||
2713 | } | ||||||
2714 | if (Self.ShouldSplatAltivecScalarInCast(vecTy) && | ||||||
2715 | (SrcExpr.get()->getType()->isIntegerType() || | ||||||
2716 | SrcExpr.get()->getType()->isFloatingType())) { | ||||||
2717 | Kind = CK_VectorSplat; | ||||||
2718 | SrcExpr = Self.prepareVectorSplat(DestType, SrcExpr.get()); | ||||||
2719 | return; | ||||||
2720 | } | ||||||
2721 | } | ||||||
2722 | |||||||
2723 | // C++ [expr.cast]p5: The conversions performed by | ||||||
2724 | // - a const_cast, | ||||||
2725 | // - a static_cast, | ||||||
2726 | // - a static_cast followed by a const_cast, | ||||||
2727 | // - a reinterpret_cast, or | ||||||
2728 | // - a reinterpret_cast followed by a const_cast, | ||||||
2729 | // can be performed using the cast notation of explicit type conversion. | ||||||
2730 | // [...] If a conversion can be interpreted in more than one of the ways | ||||||
2731 | // listed above, the interpretation that appears first in the list is used, | ||||||
2732 | // even if a cast resulting from that interpretation is ill-formed. | ||||||
2733 | // In plain language, this means trying a const_cast ... | ||||||
2734 | // Note that for address space we check compatibility after const_cast. | ||||||
2735 | unsigned msg = diag::err_bad_cxx_cast_generic; | ||||||
2736 | TryCastResult tcr = TryConstCast(Self, SrcExpr, DestType, | ||||||
2737 | /*CStyle*/ true, msg); | ||||||
2738 | if (SrcExpr.isInvalid()) | ||||||
2739 | return; | ||||||
2740 | if (isValidCast(tcr)) | ||||||
2741 | Kind = CK_NoOp; | ||||||
2742 | |||||||
2743 | Sema::CheckedConversionKind CCK = | ||||||
2744 | FunctionalStyle ? Sema::CCK_FunctionalCast : Sema::CCK_CStyleCast; | ||||||
2745 | if (tcr == TC_NotApplicable) { | ||||||
2746 | tcr = TryAddressSpaceCast(Self, SrcExpr, DestType, /*CStyle*/ true, msg, | ||||||
2747 | Kind); | ||||||
2748 | if (SrcExpr.isInvalid()) | ||||||
2749 | return; | ||||||
2750 | |||||||
2751 | if (tcr == TC_NotApplicable) { | ||||||
2752 | // ... or if that is not possible, a static_cast, ignoring const and | ||||||
2753 | // addr space, ... | ||||||
2754 | tcr = TryStaticCast(Self, SrcExpr, DestType, CCK, OpRange, msg, Kind, | ||||||
2755 | BasePath, ListInitialization); | ||||||
2756 | if (SrcExpr.isInvalid()) | ||||||
2757 | return; | ||||||
2758 | |||||||
2759 | if (tcr == TC_NotApplicable) { | ||||||
2760 | // ... and finally a reinterpret_cast, ignoring const and addr space. | ||||||
2761 | tcr = TryReinterpretCast(Self, SrcExpr, DestType, /*CStyle*/ true, | ||||||
2762 | OpRange, msg, Kind); | ||||||
2763 | if (SrcExpr.isInvalid()) | ||||||
2764 | return; | ||||||
2765 | } | ||||||
2766 | } | ||||||
2767 | } | ||||||
2768 | |||||||
2769 | if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() && | ||||||
2770 | isValidCast(tcr)) | ||||||
2771 | checkObjCConversion(CCK); | ||||||
2772 | |||||||
2773 | if (tcr != TC_Success && msg != 0) { | ||||||
2774 | if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { | ||||||
2775 | DeclAccessPair Found; | ||||||
2776 | FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(), | ||||||
2777 | DestType, | ||||||
2778 | /*Complain*/ true, | ||||||
2779 | Found); | ||||||
2780 | if (Fn) { | ||||||
2781 | // If DestType is a function type (not to be confused with the function | ||||||
2782 | // pointer type), it will be possible to resolve the function address, | ||||||
2783 | // but the type cast should be considered as failure. | ||||||
2784 | OverloadExpr *OE = OverloadExpr::find(SrcExpr.get()).Expression; | ||||||
2785 | Self.Diag(OpRange.getBegin(), diag::err_bad_cstyle_cast_overload) | ||||||
2786 | << OE->getName() << DestType << OpRange | ||||||
2787 | << OE->getQualifierLoc().getSourceRange(); | ||||||
2788 | Self.NoteAllOverloadCandidates(SrcExpr.get()); | ||||||
2789 | } | ||||||
2790 | } else { | ||||||
2791 | diagnoseBadCast(Self, msg, (FunctionalStyle ? CT_Functional : CT_CStyle), | ||||||
2792 | OpRange, SrcExpr.get(), DestType, ListInitialization); | ||||||
2793 | } | ||||||
2794 | } | ||||||
2795 | |||||||
2796 | if (isValidCast(tcr)) { | ||||||
2797 | if (Kind == CK_BitCast) | ||||||
2798 | checkCastAlign(); | ||||||
2799 | |||||||
2800 | if (!checkCastFunctionType(Self, SrcExpr, DestType)) | ||||||
2801 | Self.Diag(OpRange.getBegin(), diag::warn_cast_function_type) | ||||||
2802 | << SrcExpr.get()->getType() << DestType << OpRange; | ||||||
2803 | |||||||
2804 | } else { | ||||||
2805 | SrcExpr = ExprError(); | ||||||
2806 | } | ||||||
2807 | } | ||||||
2808 | |||||||
2809 | /// DiagnoseBadFunctionCast - Warn whenever a function call is cast to a | ||||||
2810 | /// non-matching type. Such as enum function call to int, int call to | ||||||
2811 | /// pointer; etc. Cast to 'void' is an exception. | ||||||
2812 | static void DiagnoseBadFunctionCast(Sema &Self, const ExprResult &SrcExpr, | ||||||
2813 | QualType DestType) { | ||||||
2814 | if (Self.Diags.isIgnored(diag::warn_bad_function_cast, | ||||||
2815 | SrcExpr.get()->getExprLoc())) | ||||||
2816 | return; | ||||||
2817 | |||||||
2818 | if (!isa<CallExpr>(SrcExpr.get())) | ||||||
2819 | return; | ||||||
2820 | |||||||
2821 | QualType SrcType = SrcExpr.get()->getType(); | ||||||
2822 | if (DestType.getUnqualifiedType()->isVoidType()) | ||||||
2823 | return; | ||||||
2824 | if ((SrcType->isAnyPointerType() || SrcType->isBlockPointerType()) | ||||||
2825 | && (DestType->isAnyPointerType() || DestType->isBlockPointerType())) | ||||||
2826 | return; | ||||||
2827 | if (SrcType->isIntegerType() && DestType->isIntegerType() && | ||||||
2828 | (SrcType->isBooleanType() == DestType->isBooleanType()) && | ||||||
2829 | (SrcType->isEnumeralType() == DestType->isEnumeralType())) | ||||||
2830 | return; | ||||||
2831 | if (SrcType->isRealFloatingType() && DestType->isRealFloatingType()) | ||||||
2832 | return; | ||||||
2833 | if (SrcType->isEnumeralType() && DestType->isEnumeralType()) | ||||||
2834 | return; | ||||||
2835 | if (SrcType->isComplexType() && DestType->isComplexType()) | ||||||
2836 | return; | ||||||
2837 | if (SrcType->isComplexIntegerType() && DestType->isComplexIntegerType()) | ||||||
2838 | return; | ||||||
2839 | if (SrcType->isFixedPointType() && DestType->isFixedPointType()) | ||||||
2840 | return; | ||||||
2841 | |||||||
2842 | Self.Diag(SrcExpr.get()->getExprLoc(), | ||||||
2843 | diag::warn_bad_function_cast) | ||||||
2844 | << SrcType << DestType << SrcExpr.get()->getSourceRange(); | ||||||
2845 | } | ||||||
2846 | |||||||
2847 | /// Check the semantics of a C-style cast operation, in C. | ||||||
2848 | void CastOperation::CheckCStyleCast() { | ||||||
2849 | assert(!Self.getLangOpts().CPlusPlus)(static_cast <bool> (!Self.getLangOpts().CPlusPlus) ? void (0) : __assert_fail ("!Self.getLangOpts().CPlusPlus", "clang/lib/Sema/SemaCast.cpp" , 2849, __extension__ __PRETTY_FUNCTION__)); | ||||||
2850 | |||||||
2851 | // C-style casts can resolve __unknown_any types. | ||||||
2852 | if (claimPlaceholder(BuiltinType::UnknownAny)) { | ||||||
2853 | SrcExpr = Self.checkUnknownAnyCast(DestRange, DestType, | ||||||
2854 | SrcExpr.get(), Kind, | ||||||
2855 | ValueKind, BasePath); | ||||||
2856 | return; | ||||||
2857 | } | ||||||
2858 | |||||||
2859 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression | ||||||
2860 | // type needs to be scalar. | ||||||
2861 | if (DestType->isVoidType()) { | ||||||
2862 | // We don't necessarily do lvalue-to-rvalue conversions on this. | ||||||
2863 | SrcExpr = Self.IgnoredValueConversions(SrcExpr.get()); | ||||||
2864 | if (SrcExpr.isInvalid()) | ||||||
2865 | return; | ||||||
2866 | |||||||
2867 | // Cast to void allows any expr type. | ||||||
2868 | Kind = CK_ToVoid; | ||||||
2869 | return; | ||||||
2870 | } | ||||||
2871 | |||||||
2872 | // If the type is dependent, we won't do any other semantic analysis now. | ||||||
2873 | if (Self.getASTContext().isDependenceAllowed() && | ||||||
2874 | (DestType->isDependentType() || SrcExpr.get()->isTypeDependent() || | ||||||
2875 | SrcExpr.get()->isValueDependent())) { | ||||||
2876 | assert((DestType->containsErrors() || SrcExpr.get()->containsErrors() ||(static_cast <bool> ((DestType->containsErrors() || SrcExpr .get()->containsErrors() || SrcExpr.get()->containsErrors ()) && "should only occur in error-recovery path.") ? void (0) : __assert_fail ("(DestType->containsErrors() || SrcExpr.get()->containsErrors() || SrcExpr.get()->containsErrors()) && \"should only occur in error-recovery path.\"" , "clang/lib/Sema/SemaCast.cpp", 2878, __extension__ __PRETTY_FUNCTION__ )) | ||||||
2877 | SrcExpr.get()->containsErrors()) &&(static_cast <bool> ((DestType->containsErrors() || SrcExpr .get()->containsErrors() || SrcExpr.get()->containsErrors ()) && "should only occur in error-recovery path.") ? void (0) : __assert_fail ("(DestType->containsErrors() || SrcExpr.get()->containsErrors() || SrcExpr.get()->containsErrors()) && \"should only occur in error-recovery path.\"" , "clang/lib/Sema/SemaCast.cpp", 2878, __extension__ __PRETTY_FUNCTION__ )) | ||||||
2878 | "should only occur in error-recovery path.")(static_cast <bool> ((DestType->containsErrors() || SrcExpr .get()->containsErrors() || SrcExpr.get()->containsErrors ()) && "should only occur in error-recovery path.") ? void (0) : __assert_fail ("(DestType->containsErrors() || SrcExpr.get()->containsErrors() || SrcExpr.get()->containsErrors()) && \"should only occur in error-recovery path.\"" , "clang/lib/Sema/SemaCast.cpp", 2878, __extension__ __PRETTY_FUNCTION__ )); | ||||||
2879 | assert(Kind == CK_Dependent)(static_cast <bool> (Kind == CK_Dependent) ? void (0) : __assert_fail ("Kind == CK_Dependent", "clang/lib/Sema/SemaCast.cpp" , 2879, __extension__ __PRETTY_FUNCTION__)); | ||||||
2880 | return; | ||||||
2881 | } | ||||||
2882 | |||||||
2883 | // Overloads are allowed with C extensions, so we need to support them. | ||||||
2884 | if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { | ||||||
2885 | DeclAccessPair DAP; | ||||||
2886 | if (FunctionDecl *FD = Self.ResolveAddressOfOverloadedFunction( | ||||||
2887 | SrcExpr.get(), DestType, /*Complain=*/true, DAP)) | ||||||
2888 | SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr.get(), DAP, FD); | ||||||
2889 | else | ||||||
2890 | return; | ||||||
2891 | assert(SrcExpr.isUsable())(static_cast <bool> (SrcExpr.isUsable()) ? void (0) : __assert_fail ("SrcExpr.isUsable()", "clang/lib/Sema/SemaCast.cpp", 2891, __extension__ __PRETTY_FUNCTION__)); | ||||||
2892 | } | ||||||
2893 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get()); | ||||||
2894 | if (SrcExpr.isInvalid()) | ||||||
2895 | return; | ||||||
2896 | QualType SrcType = SrcExpr.get()->getType(); | ||||||
2897 | |||||||
2898 | assert(!SrcType->isPlaceholderType())(static_cast <bool> (!SrcType->isPlaceholderType()) ? void (0) : __assert_fail ("!SrcType->isPlaceholderType()" , "clang/lib/Sema/SemaCast.cpp", 2898, __extension__ __PRETTY_FUNCTION__ )); | ||||||
2899 | |||||||
2900 | checkAddressSpaceCast(SrcType, DestType); | ||||||
2901 | if (SrcExpr.isInvalid()) | ||||||
2902 | return; | ||||||
2903 | |||||||
2904 | if (Self.RequireCompleteType(OpRange.getBegin(), DestType, | ||||||
2905 | diag::err_typecheck_cast_to_incomplete)) { | ||||||
2906 | SrcExpr = ExprError(); | ||||||
2907 | return; | ||||||
2908 | } | ||||||
2909 | |||||||
2910 | // Allow casting a sizeless built-in type to itself. | ||||||
2911 | if (DestType->isSizelessBuiltinType() && | ||||||
2912 | Self.Context.hasSameUnqualifiedType(DestType, SrcType)) { | ||||||
2913 | Kind = CK_NoOp; | ||||||
2914 | return; | ||||||
2915 | } | ||||||
2916 | |||||||
2917 | // Allow bitcasting between compatible SVE vector types. | ||||||
2918 | if ((SrcType->isVectorType() || DestType->isVectorType()) && | ||||||
2919 | Self.isValidSveBitcast(SrcType, DestType)) { | ||||||
2920 | Kind = CK_BitCast; | ||||||
2921 | return; | ||||||
2922 | } | ||||||
2923 | |||||||
2924 | if (!DestType->isScalarType() && !DestType->isVectorType() && | ||||||
2925 | !DestType->isMatrixType()) { | ||||||
2926 | const RecordType *DestRecordTy = DestType->getAs<RecordType>(); | ||||||
2927 | |||||||
2928 | if (DestRecordTy && Self.Context.hasSameUnqualifiedType(DestType, SrcType)){ | ||||||
2929 | // GCC struct/union extension: allow cast to self. | ||||||
2930 | Self.Diag(OpRange.getBegin(), diag::ext_typecheck_cast_nonscalar) | ||||||
2931 | << DestType << SrcExpr.get()->getSourceRange(); | ||||||
2932 | Kind = CK_NoOp; | ||||||
2933 | return; | ||||||
2934 | } | ||||||
2935 | |||||||
2936 | // GCC's cast to union extension. | ||||||
2937 | if (DestRecordTy && DestRecordTy->getDecl()->isUnion()) { | ||||||
2938 | RecordDecl *RD = DestRecordTy->getDecl(); | ||||||
2939 | if (CastExpr::getTargetFieldForToUnionCast(RD, SrcType)) { | ||||||
2940 | Self.Diag(OpRange.getBegin(), diag::ext_typecheck_cast_to_union) | ||||||
2941 | << SrcExpr.get()->getSourceRange(); | ||||||
2942 | Kind = CK_ToUnion; | ||||||
2943 | return; | ||||||
2944 | } else { | ||||||
2945 | Self.Diag(OpRange.getBegin(), diag::err_typecheck_cast_to_union_no_type) | ||||||
2946 | << SrcType << SrcExpr.get()->getSourceRange(); | ||||||
2947 | SrcExpr = ExprError(); | ||||||
2948 | return; | ||||||
2949 | } | ||||||
2950 | } | ||||||
2951 | |||||||
2952 | // OpenCL v2.0 s6.13.10 - Allow casts from '0' to event_t type. | ||||||
2953 | if (Self.getLangOpts().OpenCL && DestType->isEventT()) { | ||||||
2954 | Expr::EvalResult Result; | ||||||
2955 | if (SrcExpr.get()->EvaluateAsInt(Result, Self.Context)) { | ||||||
2956 | llvm::APSInt CastInt = Result.Val.getInt(); | ||||||
2957 | if (0 == CastInt) { | ||||||
2958 | Kind = CK_ZeroToOCLOpaqueType; | ||||||
2959 | return; | ||||||
2960 | } | ||||||
2961 | Self.Diag(OpRange.getBegin(), | ||||||
2962 | diag::err_opencl_cast_non_zero_to_event_t) | ||||||
2963 | << toString(CastInt, 10) << SrcExpr.get()->getSourceRange(); | ||||||
2964 | SrcExpr = ExprError(); | ||||||
2965 | return; | ||||||
2966 | } | ||||||
2967 | } | ||||||
2968 | |||||||
2969 | // Reject any other conversions to non-scalar types. | ||||||
2970 | Self.Diag(OpRange.getBegin(), diag::err_typecheck_cond_expect_scalar) | ||||||
2971 | << DestType << SrcExpr.get()->getSourceRange(); | ||||||
2972 | SrcExpr = ExprError(); | ||||||
2973 | return; | ||||||
2974 | } | ||||||
2975 | |||||||
2976 | // The type we're casting to is known to be a scalar, a vector, or a matrix. | ||||||
2977 | |||||||
2978 | // Require the operand to be a scalar, a vector, or a matrix. | ||||||
2979 | if (!SrcType->isScalarType() && !SrcType->isVectorType() && | ||||||
2980 | !SrcType->isMatrixType()) { | ||||||
2981 | Self.Diag(SrcExpr.get()->getExprLoc(), | ||||||
2982 | diag::err_typecheck_expect_scalar_operand) | ||||||
2983 | << SrcType << SrcExpr.get()->getSourceRange(); | ||||||
2984 | SrcExpr = ExprError(); | ||||||
2985 | return; | ||||||
2986 | } | ||||||
2987 | |||||||
2988 | if (DestType->isExtVectorType()) { | ||||||
2989 | SrcExpr = Self.CheckExtVectorCast(OpRange, DestType, SrcExpr.get(), Kind); | ||||||
2990 | return; | ||||||
2991 | } | ||||||
2992 | |||||||
2993 | if (DestType->getAs<MatrixType>() || SrcType->getAs<MatrixType>()) { | ||||||
2994 | if (Self.CheckMatrixCast(OpRange, DestType, SrcType, Kind)) | ||||||
2995 | SrcExpr = ExprError(); | ||||||
2996 | return; | ||||||
2997 | } | ||||||
2998 | |||||||
2999 | if (const VectorType *DestVecTy = DestType->getAs<VectorType>()) { | ||||||
3000 | if (Self.CheckAltivecInitFromScalar(OpRange, DestType, SrcType)) { | ||||||
3001 | SrcExpr = ExprError(); | ||||||
3002 | return; | ||||||
3003 | } | ||||||
3004 | if (Self.ShouldSplatAltivecScalarInCast(DestVecTy) && | ||||||
3005 | (SrcType->isIntegerType() || SrcType->isFloatingType())) { | ||||||
3006 | Kind = CK_VectorSplat; | ||||||
3007 | SrcExpr = Self.prepareVectorSplat(DestType, SrcExpr.get()); | ||||||
3008 | } else if (Self.CheckVectorCast(OpRange, DestType, SrcType, Kind)) { | ||||||
3009 | SrcExpr = ExprError(); | ||||||
3010 | } | ||||||
3011 | return; | ||||||
3012 | } | ||||||
3013 | |||||||
3014 | if (SrcType->isVectorType()) { | ||||||
3015 | if (Self.CheckVectorCast(OpRange, SrcType, DestType, Kind)) | ||||||
3016 | SrcExpr = ExprError(); | ||||||
3017 | return; | ||||||
3018 | } | ||||||
3019 | |||||||
3020 | // The source and target types are both scalars, i.e. | ||||||
3021 | // - arithmetic types (fundamental, enum, and complex) | ||||||
3022 | // - all kinds of pointers | ||||||
3023 | // Note that member pointers were filtered out with C++, above. | ||||||
3024 | |||||||
3025 | if (isa<ObjCSelectorExpr>(SrcExpr.get())) { | ||||||
3026 | Self.Diag(SrcExpr.get()->getExprLoc(), diag::err_cast_selector_expr); | ||||||
3027 | SrcExpr = ExprError(); | ||||||
3028 | return; | ||||||
3029 | } | ||||||
3030 | |||||||
3031 | // Can't cast to or from bfloat | ||||||
3032 | if (DestType->isBFloat16Type() && !SrcType->isBFloat16Type()) { | ||||||
3033 | Self.Diag(SrcExpr.get()->getExprLoc(), diag::err_cast_to_bfloat16) | ||||||
3034 | << SrcExpr.get()->getSourceRange(); | ||||||
3035 | SrcExpr = ExprError(); | ||||||
3036 | return; | ||||||
3037 | } | ||||||
3038 | if (SrcType->isBFloat16Type() && !DestType->isBFloat16Type()) { | ||||||
3039 | Self.Diag(SrcExpr.get()->getExprLoc(), diag::err_cast_from_bfloat16) | ||||||
3040 | << SrcExpr.get()->getSourceRange(); | ||||||
3041 | SrcExpr = ExprError(); | ||||||
3042 | return; | ||||||
3043 | } | ||||||
3044 | |||||||
3045 | // If either type is a pointer, the other type has to be either an | ||||||
3046 | // integer or a pointer. | ||||||
3047 | if (!DestType->isArithmeticType()) { | ||||||
3048 | if (!SrcType->isIntegralType(Self.Context) && SrcType->isArithmeticType()) { | ||||||
3049 | Self.Diag(SrcExpr.get()->getExprLoc(), | ||||||
3050 | diag::err_cast_pointer_from_non_pointer_int) | ||||||
3051 | << SrcType << SrcExpr.get()->getSourceRange(); | ||||||
3052 | SrcExpr = ExprError(); | ||||||
3053 | return; | ||||||
3054 | } | ||||||
3055 | checkIntToPointerCast(/* CStyle */ true, OpRange, SrcExpr.get(), DestType, | ||||||
3056 | Self); | ||||||
3057 | } else if (!SrcType->isArithmeticType()) { | ||||||
3058 | if (!DestType->isIntegralType(Self.Context) && | ||||||
3059 | DestType->isArithmeticType()) { | ||||||
3060 | Self.Diag(SrcExpr.get()->getBeginLoc(), | ||||||
3061 | diag::err_cast_pointer_to_non_pointer_int) | ||||||
3062 | << DestType << SrcExpr.get()->getSourceRange(); | ||||||
3063 | SrcExpr = ExprError(); | ||||||
3064 | return; | ||||||
3065 | } | ||||||
3066 | |||||||
3067 | if ((Self.Context.getTypeSize(SrcType) > | ||||||
3068 | Self.Context.getTypeSize(DestType)) && | ||||||
3069 | !DestType->isBooleanType()) { | ||||||
3070 | // C 6.3.2.3p6: Any pointer type may be converted to an integer type. | ||||||
3071 | // Except as previously specified, the result is implementation-defined. | ||||||
3072 | // If the result cannot be represented in the integer type, the behavior | ||||||
3073 | // is undefined. The result need not be in the range of values of any | ||||||
3074 | // integer type. | ||||||
3075 | unsigned Diag; | ||||||
3076 | if (SrcType->isVoidPointerType()) | ||||||
3077 | Diag = DestType->isEnumeralType() ? diag::warn_void_pointer_to_enum_cast | ||||||
3078 | : diag::warn_void_pointer_to_int_cast; | ||||||
3079 | else if (DestType->isEnumeralType()) | ||||||
3080 | Diag = diag::warn_pointer_to_enum_cast; | ||||||
3081 | else | ||||||
3082 | Diag = diag::warn_pointer_to_int_cast; | ||||||
3083 | Self.Diag(OpRange.getBegin(), Diag) << SrcType << DestType << OpRange; | ||||||
3084 | } | ||||||
3085 | } | ||||||
3086 | |||||||
3087 | if (Self.getLangOpts().OpenCL && !Self.getOpenCLOptions().isAvailableOption( | ||||||
3088 | "cl_khr_fp16", Self.getLangOpts())) { | ||||||
3089 | if (DestType->isHalfType()) { | ||||||
3090 | Self.Diag(SrcExpr.get()->getBeginLoc(), diag::err_opencl_cast_to_half) | ||||||
3091 | << DestType << SrcExpr.get()->getSourceRange(); | ||||||
3092 | SrcExpr = ExprError(); | ||||||
3093 | return; | ||||||
3094 | } | ||||||
3095 | } | ||||||
3096 | |||||||
3097 | // ARC imposes extra restrictions on casts. | ||||||
3098 | if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers()) { | ||||||
3099 | checkObjCConversion(Sema::CCK_CStyleCast); | ||||||
3100 | if (SrcExpr.isInvalid()) | ||||||
3101 | return; | ||||||
3102 | |||||||
3103 | const PointerType *CastPtr = DestType->getAs<PointerType>(); | ||||||
3104 | if (Self.getLangOpts().ObjCAutoRefCount && CastPtr) { | ||||||
3105 | if (const PointerType *ExprPtr = SrcType->getAs<PointerType>()) { | ||||||
3106 | Qualifiers CastQuals = CastPtr->getPointeeType().getQualifiers(); | ||||||
3107 | Qualifiers ExprQuals = ExprPtr->getPointeeType().getQualifiers(); | ||||||
3108 | if (CastPtr->getPointeeType()->isObjCLifetimeType() && | ||||||
3109 | ExprPtr->getPointeeType()->isObjCLifetimeType() && | ||||||
3110 | !CastQuals.compatiblyIncludesObjCLifetime(ExprQuals)) { | ||||||
3111 | Self.Diag(SrcExpr.get()->getBeginLoc(), | ||||||
3112 | diag::err_typecheck_incompatible_ownership) | ||||||
3113 | << SrcType << DestType << Sema::AA_Casting | ||||||
3114 | << SrcExpr.get()->getSourceRange(); | ||||||
3115 | return; | ||||||
3116 | } | ||||||
3117 | } | ||||||
3118 | } | ||||||
3119 | else if (!Self.CheckObjCARCUnavailableWeakConversion(DestType, SrcType)) { | ||||||
3120 | Self.Diag(SrcExpr.get()->getBeginLoc(), | ||||||
3121 | diag::err_arc_convesion_of_weak_unavailable) | ||||||
3122 | << 1 << SrcType << DestType << SrcExpr.get()->getSourceRange(); | ||||||
3123 | SrcExpr = ExprError(); | ||||||
3124 | return; | ||||||
3125 | } | ||||||
3126 | } | ||||||
3127 | |||||||
3128 | if (!checkCastFunctionType(Self, SrcExpr, DestType)) | ||||||
3129 | Self.Diag(OpRange.getBegin(), diag::warn_cast_function_type) | ||||||
3130 | << SrcType << DestType << OpRange; | ||||||
3131 | |||||||
3132 | DiagnoseCastOfObjCSEL(Self, SrcExpr, DestType); | ||||||
3133 | DiagnoseCallingConvCast(Self, SrcExpr, DestType, OpRange); | ||||||
3134 | DiagnoseBadFunctionCast(Self, SrcExpr, DestType); | ||||||
3135 | Kind = Self.PrepareScalarCast(SrcExpr, DestType); | ||||||
3136 | if (SrcExpr.isInvalid()) | ||||||
3137 | return; | ||||||
3138 | |||||||
3139 | if (Kind == CK_BitCast) | ||||||
3140 | checkCastAlign(); | ||||||
3141 | } | ||||||
3142 | |||||||
3143 | void CastOperation::CheckBuiltinBitCast() { | ||||||
3144 | QualType SrcType = SrcExpr.get()->getType(); | ||||||
3145 | |||||||
3146 | if (Self.RequireCompleteType(OpRange.getBegin(), DestType, | ||||||
3147 | diag::err_typecheck_cast_to_incomplete) || | ||||||
3148 | Self.RequireCompleteType(OpRange.getBegin(), SrcType, | ||||||
3149 | diag::err_incomplete_type)) { | ||||||
3150 | SrcExpr = ExprError(); | ||||||
3151 | return; | ||||||
3152 | } | ||||||
3153 | |||||||
3154 | if (SrcExpr.get()->isPRValue()) | ||||||
3155 | SrcExpr = Self.CreateMaterializeTemporaryExpr(SrcType, SrcExpr.get(), | ||||||
3156 | /*IsLValueReference=*/false); | ||||||
3157 | |||||||
3158 | CharUnits DestSize = Self.Context.getTypeSizeInChars(DestType); | ||||||
3159 | CharUnits SourceSize = Self.Context.getTypeSizeInChars(SrcType); | ||||||
3160 | if (DestSize != SourceSize) { | ||||||
3161 | Self.Diag(OpRange.getBegin(), diag::err_bit_cast_type_size_mismatch) | ||||||
3162 | << (int)SourceSize.getQuantity() << (int)DestSize.getQuantity(); | ||||||
3163 | SrcExpr = ExprError(); | ||||||
3164 | return; | ||||||
3165 | } | ||||||
3166 | |||||||
3167 | if (!DestType.isTriviallyCopyableType(Self.Context)) { | ||||||
3168 | Self.Diag(OpRange.getBegin(), diag::err_bit_cast_non_trivially_copyable) | ||||||
3169 | << 1; | ||||||
3170 | SrcExpr = ExprError(); | ||||||
3171 | return; | ||||||
3172 | } | ||||||
3173 | |||||||
3174 | if (!SrcType.isTriviallyCopyableType(Self.Context)) { | ||||||
3175 | Self.Diag(OpRange.getBegin(), diag::err_bit_cast_non_trivially_copyable) | ||||||
3176 | << 0; | ||||||
3177 | SrcExpr = ExprError(); | ||||||
3178 | return; | ||||||
3179 | } | ||||||
3180 | |||||||
3181 | Kind = CK_LValueToRValueBitCast; | ||||||
3182 | } | ||||||
3183 | |||||||
3184 | /// DiagnoseCastQual - Warn whenever casts discards a qualifiers, be it either | ||||||
3185 | /// const, volatile or both. | ||||||
3186 | static void DiagnoseCastQual(Sema &Self, const ExprResult &SrcExpr, | ||||||
3187 | QualType DestType) { | ||||||
3188 | if (SrcExpr.isInvalid()) | ||||||
3189 | return; | ||||||
3190 | |||||||
3191 | QualType SrcType = SrcExpr.get()->getType(); | ||||||
3192 | if (!((SrcType->isAnyPointerType() && DestType->isAnyPointerType()) || | ||||||
3193 | DestType->isLValueReferenceType())) | ||||||
3194 | return; | ||||||
3195 | |||||||
3196 | QualType TheOffendingSrcType, TheOffendingDestType; | ||||||
3197 | Qualifiers CastAwayQualifiers; | ||||||
3198 | if (CastsAwayConstness(Self, SrcType, DestType, true, false, | ||||||
3199 | &TheOffendingSrcType, &TheOffendingDestType, | ||||||
3200 | &CastAwayQualifiers) != | ||||||
3201 | CastAwayConstnessKind::CACK_Similar) | ||||||
3202 | return; | ||||||
3203 | |||||||
3204 | // FIXME: 'restrict' is not properly handled here. | ||||||
3205 | int qualifiers = -1; | ||||||
3206 | if (CastAwayQualifiers.hasConst() && CastAwayQualifiers.hasVolatile()) { | ||||||
3207 | qualifiers = 0; | ||||||
3208 | } else if (CastAwayQualifiers.hasConst()) { | ||||||
3209 | qualifiers = 1; | ||||||
3210 | } else if (CastAwayQualifiers.hasVolatile()) { | ||||||
3211 | qualifiers = 2; | ||||||
3212 | } | ||||||
3213 | // This is a variant of int **x; const int **y = (const int **)x; | ||||||
3214 | if (qualifiers == -1) | ||||||
3215 | Self.Diag(SrcExpr.get()->getBeginLoc(), diag::warn_cast_qual2) | ||||||
3216 | << SrcType << DestType; | ||||||
3217 | else | ||||||
3218 | Self.Diag(SrcExpr.get()->getBeginLoc(), diag::warn_cast_qual) | ||||||
3219 | << TheOffendingSrcType << TheOffendingDestType << qualifiers; | ||||||
3220 | } | ||||||
3221 | |||||||
3222 | ExprResult Sema::BuildCStyleCastExpr(SourceLocation LPLoc, | ||||||
3223 | TypeSourceInfo *CastTypeInfo, | ||||||
3224 | SourceLocation RPLoc, | ||||||
3225 | Expr *CastExpr) { | ||||||
3226 | CastOperation Op(*this, CastTypeInfo->getType(), CastExpr); | ||||||
3227 | Op.DestRange = CastTypeInfo->getTypeLoc().getSourceRange(); | ||||||
3228 | Op.OpRange = SourceRange(LPLoc, CastExpr->getEndLoc()); | ||||||
3229 | |||||||
3230 | if (getLangOpts().CPlusPlus) { | ||||||
3231 | Op.CheckCXXCStyleCast(/*FunctionalCast=*/ false, | ||||||
3232 | isa<InitListExpr>(CastExpr)); | ||||||
3233 | } else { | ||||||
3234 | Op.CheckCStyleCast(); | ||||||
3235 | } | ||||||
3236 | |||||||
3237 | if (Op.SrcExpr.isInvalid()) | ||||||
3238 | return ExprError(); | ||||||
3239 | |||||||
3240 | // -Wcast-qual | ||||||
3241 | DiagnoseCastQual(Op.Self, Op.SrcExpr, Op.DestType); | ||||||
3242 | |||||||
3243 | return Op.complete(CStyleCastExpr::Create( | ||||||
3244 | Context, Op.ResultType, Op.ValueKind, Op.Kind, Op.SrcExpr.get(), | ||||||
3245 | &Op.BasePath, CurFPFeatureOverrides(), CastTypeInfo, LPLoc, RPLoc)); | ||||||
3246 | } | ||||||
3247 | |||||||
3248 | ExprResult Sema::BuildCXXFunctionalCastExpr(TypeSourceInfo *CastTypeInfo, | ||||||
3249 | QualType Type, | ||||||
3250 | SourceLocation LPLoc, | ||||||
3251 | Expr *CastExpr, | ||||||
3252 | SourceLocation RPLoc) { | ||||||
3253 | assert(LPLoc.isValid() && "List-initialization shouldn't get here.")(static_cast <bool> (LPLoc.isValid() && "List-initialization shouldn't get here." ) ? void (0) : __assert_fail ("LPLoc.isValid() && \"List-initialization shouldn't get here.\"" , "clang/lib/Sema/SemaCast.cpp", 3253, __extension__ __PRETTY_FUNCTION__ )); | ||||||
3254 | CastOperation Op(*this, Type, CastExpr); | ||||||
3255 | Op.DestRange = CastTypeInfo->getTypeLoc().getSourceRange(); | ||||||
3256 | Op.OpRange = SourceRange(Op.DestRange.getBegin(), CastExpr->getEndLoc()); | ||||||
3257 | |||||||
3258 | Op.CheckCXXCStyleCast(/*FunctionalCast=*/true, /*ListInit=*/false); | ||||||
3259 | if (Op.SrcExpr.isInvalid()) | ||||||
3260 | return ExprError(); | ||||||
3261 | |||||||
3262 | auto *SubExpr = Op.SrcExpr.get(); | ||||||
3263 | if (auto *BindExpr = dyn_cast<CXXBindTemporaryExpr>(SubExpr)) | ||||||
3264 | SubExpr = BindExpr->getSubExpr(); | ||||||
3265 | if (auto *ConstructExpr = dyn_cast<CXXConstructExpr>(SubExpr)) | ||||||
3266 | ConstructExpr->setParenOrBraceRange(SourceRange(LPLoc, RPLoc)); | ||||||
3267 | |||||||
3268 | return Op.complete(CXXFunctionalCastExpr::Create( | ||||||
3269 | Context, Op.ResultType, Op.ValueKind, CastTypeInfo, Op.Kind, | ||||||
3270 | Op.SrcExpr.get(), &Op.BasePath, CurFPFeatureOverrides(), LPLoc, RPLoc)); | ||||||
3271 | } |
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/DependenceFlags.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 TemplateParameterList; | ||||||
65 | class Type; | ||||||
66 | |||||||
67 | enum { | ||||||
68 | TypeAlignmentInBits = 4, | ||||||
69 | TypeAlignment = 1 << TypeAlignmentInBits | ||||||
70 | }; | ||||||
71 | |||||||
72 | namespace serialization { | ||||||
73 | template <class T> class AbstractTypeReader; | ||||||
74 | template <class T> class AbstractTypeWriter; | ||||||
75 | } | ||||||
76 | |||||||
77 | } // namespace clang | ||||||
78 | |||||||
79 | namespace llvm { | ||||||
80 | |||||||
81 | template <typename T> | ||||||
82 | struct PointerLikeTypeTraits; | ||||||
83 | template<> | ||||||
84 | struct PointerLikeTypeTraits< ::clang::Type*> { | ||||||
85 | static inline void *getAsVoidPointer(::clang::Type *P) { return P; } | ||||||
86 | |||||||
87 | static inline ::clang::Type *getFromVoidPointer(void *P) { | ||||||
88 | return static_cast< ::clang::Type*>(P); | ||||||
89 | } | ||||||
90 | |||||||
91 | static constexpr int NumLowBitsAvailable = clang::TypeAlignmentInBits; | ||||||
92 | }; | ||||||
93 | |||||||
94 | template<> | ||||||
95 | struct PointerLikeTypeTraits< ::clang::ExtQuals*> { | ||||||
96 | static inline void *getAsVoidPointer(::clang::ExtQuals *P) { return P; } | ||||||
97 | |||||||
98 | static inline ::clang::ExtQuals *getFromVoidPointer(void *P) { | ||||||
99 | return static_cast< ::clang::ExtQuals*>(P); | ||||||
100 | } | ||||||
101 | |||||||
102 | static constexpr int NumLowBitsAvailable = clang::TypeAlignmentInBits; | ||||||
103 | }; | ||||||
104 | |||||||
105 | } // namespace llvm | ||||||
106 | |||||||
107 | namespace clang { | ||||||
108 | |||||||
109 | class ASTContext; | ||||||
110 | template <typename> class CanQual; | ||||||
111 | class CXXRecordDecl; | ||||||
112 | class DeclContext; | ||||||
113 | class EnumDecl; | ||||||
114 | class Expr; | ||||||
115 | class ExtQualsTypeCommonBase; | ||||||
116 | class FunctionDecl; | ||||||
117 | class IdentifierInfo; | ||||||
118 | class NamedDecl; | ||||||
119 | class ObjCInterfaceDecl; | ||||||
120 | class ObjCProtocolDecl; | ||||||
121 | class ObjCTypeParamDecl; | ||||||
122 | struct PrintingPolicy; | ||||||
123 | class RecordDecl; | ||||||
124 | class Stmt; | ||||||
125 | class TagDecl; | ||||||
126 | class TemplateArgument; | ||||||
127 | class TemplateArgumentListInfo; | ||||||
128 | class TemplateArgumentLoc; | ||||||
129 | class TemplateTypeParmDecl; | ||||||
130 | class TypedefNameDecl; | ||||||
131 | class UnresolvedUsingTypenameDecl; | ||||||
132 | class UsingShadowDecl; | ||||||
133 | |||||||
134 | using CanQualType = CanQual<Type>; | ||||||
135 | |||||||
136 | // Provide forward declarations for all of the *Type classes. | ||||||
137 | #define TYPE(Class, Base) class Class##Type; | ||||||
138 | #include "clang/AST/TypeNodes.inc" | ||||||
139 | |||||||
140 | /// The collection of all-type qualifiers we support. | ||||||
141 | /// Clang supports five independent qualifiers: | ||||||
142 | /// * C99: const, volatile, and restrict | ||||||
143 | /// * MS: __unaligned | ||||||
144 | /// * Embedded C (TR18037): address spaces | ||||||
145 | /// * Objective C: the GC attributes (none, weak, or strong) | ||||||
146 | class Qualifiers { | ||||||
147 | public: | ||||||
148 | enum TQ { // NOTE: These flags must be kept in sync with DeclSpec::TQ. | ||||||
149 | Const = 0x1, | ||||||
150 | Restrict = 0x2, | ||||||
151 | Volatile = 0x4, | ||||||
152 | CVRMask = Const | Volatile | Restrict | ||||||
153 | }; | ||||||
154 | |||||||
155 | enum GC { | ||||||
156 | GCNone = 0, | ||||||
157 | Weak, | ||||||
158 | Strong | ||||||
159 | }; | ||||||
160 | |||||||
161 | enum ObjCLifetime { | ||||||
162 | /// There is no lifetime qualification on this type. | ||||||
163 | OCL_None, | ||||||
164 | |||||||
165 | /// This object can be modified without requiring retains or | ||||||
166 | /// releases. | ||||||
167 | OCL_ExplicitNone, | ||||||
168 | |||||||
169 | /// Assigning into this object requires the old value to be | ||||||
170 | /// released and the new value to be retained. The timing of the | ||||||
171 | /// release of the old value is inexact: it may be moved to | ||||||
172 | /// immediately after the last known point where the value is | ||||||
173 | /// live. | ||||||
174 | OCL_Strong, | ||||||
175 | |||||||
176 | /// Reading or writing from this object requires a barrier call. | ||||||
177 | OCL_Weak, | ||||||
178 | |||||||
179 | /// Assigning into this object requires a lifetime extension. | ||||||
180 | OCL_Autoreleasing | ||||||
181 | }; | ||||||
182 | |||||||
183 | enum { | ||||||
184 | /// The maximum supported address space number. | ||||||
185 | /// 23 bits should be enough for anyone. | ||||||
186 | MaxAddressSpace = 0x7fffffu, | ||||||
187 | |||||||
188 | /// The width of the "fast" qualifier mask. | ||||||
189 | FastWidth = 3, | ||||||
190 | |||||||
191 | /// The fast qualifier mask. | ||||||
192 | FastMask = (1 << FastWidth) - 1 | ||||||
193 | }; | ||||||
194 | |||||||
195 | /// Returns the common set of qualifiers while removing them from | ||||||
196 | /// the given sets. | ||||||
197 | static Qualifiers removeCommonQualifiers(Qualifiers &L, Qualifiers &R) { | ||||||
198 | // If both are only CVR-qualified, bit operations are sufficient. | ||||||
199 | if (!(L.Mask & ~CVRMask) && !(R.Mask & ~CVRMask)) { | ||||||
200 | Qualifiers Q; | ||||||
201 | Q.Mask = L.Mask & R.Mask; | ||||||
202 | L.Mask &= ~Q.Mask; | ||||||
203 | R.Mask &= ~Q.Mask; | ||||||
204 | return Q; | ||||||
205 | } | ||||||
206 | |||||||
207 | Qualifiers Q; | ||||||
208 | unsigned CommonCRV = L.getCVRQualifiers() & R.getCVRQualifiers(); | ||||||
209 | Q.addCVRQualifiers(CommonCRV); | ||||||
210 | L.removeCVRQualifiers(CommonCRV); | ||||||
211 | R.removeCVRQualifiers(CommonCRV); | ||||||
212 | |||||||
213 | if (L.getObjCGCAttr() == R.getObjCGCAttr()) { | ||||||
214 | Q.setObjCGCAttr(L.getObjCGCAttr()); | ||||||
215 | L.removeObjCGCAttr(); | ||||||
216 | R.removeObjCGCAttr(); | ||||||
217 | } | ||||||
218 | |||||||
219 | if (L.getObjCLifetime() == R.getObjCLifetime()) { | ||||||
220 | Q.setObjCLifetime(L.getObjCLifetime()); | ||||||
221 | L.removeObjCLifetime(); | ||||||
222 | R.removeObjCLifetime(); | ||||||
223 | } | ||||||
224 | |||||||
225 | if (L.getAddressSpace() == R.getAddressSpace()) { | ||||||
226 | Q.setAddressSpace(L.getAddressSpace()); | ||||||
227 | L.removeAddressSpace(); | ||||||
228 | R.removeAddressSpace(); | ||||||
229 | } | ||||||
230 | return Q; | ||||||
231 | } | ||||||
232 | |||||||
233 | static Qualifiers fromFastMask(unsigned Mask) { | ||||||
234 | Qualifiers Qs; | ||||||
235 | Qs.addFastQualifiers(Mask); | ||||||
236 | return Qs; | ||||||
237 | } | ||||||
238 | |||||||
239 | static Qualifiers fromCVRMask(unsigned CVR) { | ||||||
240 | Qualifiers Qs; | ||||||
241 | Qs.addCVRQualifiers(CVR); | ||||||
242 | return Qs; | ||||||
243 | } | ||||||
244 | |||||||
245 | static Qualifiers fromCVRUMask(unsigned CVRU) { | ||||||
246 | Qualifiers Qs; | ||||||
247 | Qs.addCVRUQualifiers(CVRU); | ||||||
248 | return Qs; | ||||||
249 | } | ||||||
250 | |||||||
251 | // Deserialize qualifiers from an opaque representation. | ||||||
252 | static Qualifiers fromOpaqueValue(unsigned opaque) { | ||||||
253 | Qualifiers Qs; | ||||||
254 | Qs.Mask = opaque; | ||||||
255 | return Qs; | ||||||
256 | } | ||||||
257 | |||||||
258 | // Serialize these qualifiers into an opaque representation. | ||||||
259 | unsigned getAsOpaqueValue() const { | ||||||
260 | return Mask; | ||||||
261 | } | ||||||
262 | |||||||
263 | bool hasConst() const { return Mask & Const; } | ||||||
264 | bool hasOnlyConst() const { return Mask == Const; } | ||||||
265 | void removeConst() { Mask &= ~Const; } | ||||||
266 | void addConst() { Mask |= Const; } | ||||||
267 | |||||||
268 | bool hasVolatile() const { return Mask & Volatile; } | ||||||
269 | bool hasOnlyVolatile() const { return Mask == Volatile; } | ||||||
270 | void removeVolatile() { Mask &= ~Volatile; } | ||||||
271 | void addVolatile() { Mask |= Volatile; } | ||||||
272 | |||||||
273 | bool hasRestrict() const { return Mask & Restrict; } | ||||||
274 | bool hasOnlyRestrict() const { return Mask == Restrict; } | ||||||
275 | void removeRestrict() { Mask &= ~Restrict; } | ||||||
276 | void addRestrict() { Mask |= Restrict; } | ||||||
277 | |||||||
278 | bool hasCVRQualifiers() const { return getCVRQualifiers(); } | ||||||
279 | unsigned getCVRQualifiers() const { return Mask & CVRMask; } | ||||||
280 | unsigned getCVRUQualifiers() const { return Mask & (CVRMask | UMask); } | ||||||
281 | |||||||
282 | void setCVRQualifiers(unsigned mask) { | ||||||
283 | assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits")(static_cast <bool> (!(mask & ~CVRMask) && "bitmask contains non-CVR bits" ) ? void (0) : __assert_fail ("!(mask & ~CVRMask) && \"bitmask contains non-CVR bits\"" , "clang/include/clang/AST/Type.h", 283, __extension__ __PRETTY_FUNCTION__ )); | ||||||
284 | Mask = (Mask & ~CVRMask) | mask; | ||||||
285 | } | ||||||
286 | void removeCVRQualifiers(unsigned mask) { | ||||||
287 | assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits")(static_cast <bool> (!(mask & ~CVRMask) && "bitmask contains non-CVR bits" ) ? void (0) : __assert_fail ("!(mask & ~CVRMask) && \"bitmask contains non-CVR bits\"" , "clang/include/clang/AST/Type.h", 287, __extension__ __PRETTY_FUNCTION__ )); | ||||||
288 | Mask &= ~mask; | ||||||
289 | } | ||||||
290 | void removeCVRQualifiers() { | ||||||
291 | removeCVRQualifiers(CVRMask); | ||||||
292 | } | ||||||
293 | void addCVRQualifiers(unsigned mask) { | ||||||
294 | assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits")(static_cast <bool> (!(mask & ~CVRMask) && "bitmask contains non-CVR bits" ) ? void (0) : __assert_fail ("!(mask & ~CVRMask) && \"bitmask contains non-CVR bits\"" , "clang/include/clang/AST/Type.h", 294, __extension__ __PRETTY_FUNCTION__ )); | ||||||
295 | Mask |= mask; | ||||||
296 | } | ||||||
297 | void addCVRUQualifiers(unsigned mask) { | ||||||
298 | assert(!(mask & ~CVRMask & ~UMask) && "bitmask contains non-CVRU bits")(static_cast <bool> (!(mask & ~CVRMask & ~UMask ) && "bitmask contains non-CVRU bits") ? void (0) : __assert_fail ("!(mask & ~CVRMask & ~UMask) && \"bitmask contains non-CVRU bits\"" , "clang/include/clang/AST/Type.h", 298, __extension__ __PRETTY_FUNCTION__ )); | ||||||
299 | Mask |= mask; | ||||||
300 | } | ||||||
301 | |||||||
302 | bool hasUnaligned() const { return Mask & UMask; } | ||||||
303 | void setUnaligned(bool flag) { | ||||||
304 | Mask = (Mask & ~UMask) | (flag ? UMask : 0); | ||||||
305 | } | ||||||
306 | void removeUnaligned() { Mask &= ~UMask; } | ||||||
307 | void addUnaligned() { Mask |= UMask; } | ||||||
308 | |||||||
309 | bool hasObjCGCAttr() const { return Mask & GCAttrMask; } | ||||||
310 | GC getObjCGCAttr() const { return GC((Mask & GCAttrMask) >> GCAttrShift); } | ||||||
311 | void setObjCGCAttr(GC type) { | ||||||
312 | Mask = (Mask & ~GCAttrMask) | (type << GCAttrShift); | ||||||
313 | } | ||||||
314 | void removeObjCGCAttr() { setObjCGCAttr(GCNone); } | ||||||
315 | void addObjCGCAttr(GC type) { | ||||||
316 | assert(type)(static_cast <bool> (type) ? void (0) : __assert_fail ( "type", "clang/include/clang/AST/Type.h", 316, __extension__ __PRETTY_FUNCTION__ )); | ||||||
317 | setObjCGCAttr(type); | ||||||
318 | } | ||||||
319 | Qualifiers withoutObjCGCAttr() const { | ||||||
320 | Qualifiers qs = *this; | ||||||
321 | qs.removeObjCGCAttr(); | ||||||
322 | return qs; | ||||||
323 | } | ||||||
324 | Qualifiers withoutObjCLifetime() const { | ||||||
325 | Qualifiers qs = *this; | ||||||
326 | qs.removeObjCLifetime(); | ||||||
327 | return qs; | ||||||
328 | } | ||||||
329 | Qualifiers withoutAddressSpace() const { | ||||||
330 | Qualifiers qs = *this; | ||||||
331 | qs.removeAddressSpace(); | ||||||
332 | return qs; | ||||||
333 | } | ||||||
334 | |||||||
335 | bool hasObjCLifetime() const { return Mask & LifetimeMask; } | ||||||
336 | ObjCLifetime getObjCLifetime() const { | ||||||
337 | return ObjCLifetime((Mask & LifetimeMask) >> LifetimeShift); | ||||||
338 | } | ||||||
339 | void setObjCLifetime(ObjCLifetime type) { | ||||||
340 | Mask = (Mask & ~LifetimeMask) | (type << LifetimeShift); | ||||||
341 | } | ||||||
342 | void removeObjCLifetime() { setObjCLifetime(OCL_None); } | ||||||
343 | void addObjCLifetime(ObjCLifetime type) { | ||||||
344 | assert(type)(static_cast <bool> (type) ? void (0) : __assert_fail ( "type", "clang/include/clang/AST/Type.h", 344, __extension__ __PRETTY_FUNCTION__ )); | ||||||
345 | assert(!hasObjCLifetime())(static_cast <bool> (!hasObjCLifetime()) ? void (0) : __assert_fail ("!hasObjCLifetime()", "clang/include/clang/AST/Type.h", 345 , __extension__ __PRETTY_FUNCTION__)); | ||||||
346 | Mask |= (type << LifetimeShift); | ||||||
347 | } | ||||||
348 | |||||||
349 | /// True if the lifetime is neither None or ExplicitNone. | ||||||
350 | bool hasNonTrivialObjCLifetime() const { | ||||||
351 | ObjCLifetime lifetime = getObjCLifetime(); | ||||||
352 | return (lifetime > OCL_ExplicitNone); | ||||||
353 | } | ||||||
354 | |||||||
355 | /// True if the lifetime is either strong or weak. | ||||||
356 | bool hasStrongOrWeakObjCLifetime() const { | ||||||
357 | ObjCLifetime lifetime = getObjCLifetime(); | ||||||
358 | return (lifetime == OCL_Strong || lifetime == OCL_Weak); | ||||||
359 | } | ||||||
360 | |||||||
361 | bool hasAddressSpace() const { return Mask & AddressSpaceMask; } | ||||||
362 | LangAS getAddressSpace() const { | ||||||
363 | return static_cast<LangAS>(Mask >> AddressSpaceShift); | ||||||
364 | } | ||||||
365 | bool hasTargetSpecificAddressSpace() const { | ||||||
366 | return isTargetAddressSpace(getAddressSpace()); | ||||||
367 | } | ||||||
368 | /// Get the address space attribute value to be printed by diagnostics. | ||||||
369 | unsigned getAddressSpaceAttributePrintValue() const { | ||||||
370 | auto Addr = getAddressSpace(); | ||||||
371 | // This function is not supposed to be used with language specific | ||||||
372 | // address spaces. If that happens, the diagnostic message should consider | ||||||
373 | // printing the QualType instead of the address space value. | ||||||
374 | assert(Addr == LangAS::Default || hasTargetSpecificAddressSpace())(static_cast <bool> (Addr == LangAS::Default || hasTargetSpecificAddressSpace ()) ? void (0) : __assert_fail ("Addr == LangAS::Default || hasTargetSpecificAddressSpace()" , "clang/include/clang/AST/Type.h", 374, __extension__ __PRETTY_FUNCTION__ )); | ||||||
375 | if (Addr != LangAS::Default) | ||||||
376 | return toTargetAddressSpace(Addr); | ||||||
377 | // TODO: The diagnostic messages where Addr may be 0 should be fixed | ||||||
378 | // since it cannot differentiate the situation where 0 denotes the default | ||||||
379 | // address space or user specified __attribute__((address_space(0))). | ||||||
380 | return 0; | ||||||
381 | } | ||||||
382 | void setAddressSpace(LangAS space) { | ||||||
383 | assert((unsigned)space <= MaxAddressSpace)(static_cast <bool> ((unsigned)space <= MaxAddressSpace ) ? void (0) : __assert_fail ("(unsigned)space <= MaxAddressSpace" , "clang/include/clang/AST/Type.h", 383, __extension__ __PRETTY_FUNCTION__ )); | ||||||
384 | Mask = (Mask & ~AddressSpaceMask) | ||||||
385 | | (((uint32_t) space) << AddressSpaceShift); | ||||||
386 | } | ||||||
387 | void removeAddressSpace() { setAddressSpace(LangAS::Default); } | ||||||
388 | void addAddressSpace(LangAS space) { | ||||||
389 | assert(space != LangAS::Default)(static_cast <bool> (space != LangAS::Default) ? void ( 0) : __assert_fail ("space != LangAS::Default", "clang/include/clang/AST/Type.h" , 389, __extension__ __PRETTY_FUNCTION__)); | ||||||
390 | setAddressSpace(space); | ||||||
391 | } | ||||||
392 | |||||||
393 | // Fast qualifiers are those that can be allocated directly | ||||||
394 | // on a QualType object. | ||||||
395 | bool hasFastQualifiers() const { return getFastQualifiers(); } | ||||||
396 | unsigned getFastQualifiers() const { return Mask & FastMask; } | ||||||
397 | void setFastQualifiers(unsigned mask) { | ||||||
398 | assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits")(static_cast <bool> (!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits") ? void (0) : __assert_fail ("!(mask & ~FastMask) && \"bitmask contains non-fast qualifier bits\"" , "clang/include/clang/AST/Type.h", 398, __extension__ __PRETTY_FUNCTION__ )); | ||||||
399 | Mask = (Mask & ~FastMask) | mask; | ||||||
400 | } | ||||||
401 | void removeFastQualifiers(unsigned mask) { | ||||||
402 | assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits")(static_cast <bool> (!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits") ? void (0) : __assert_fail ("!(mask & ~FastMask) && \"bitmask contains non-fast qualifier bits\"" , "clang/include/clang/AST/Type.h", 402, __extension__ __PRETTY_FUNCTION__ )); | ||||||
403 | Mask &= ~mask; | ||||||
404 | } | ||||||
405 | void removeFastQualifiers() { | ||||||
406 | removeFastQualifiers(FastMask); | ||||||
407 | } | ||||||
408 | void addFastQualifiers(unsigned mask) { | ||||||
409 | assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits")(static_cast <bool> (!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits") ? void (0) : __assert_fail ("!(mask & ~FastMask) && \"bitmask contains non-fast qualifier bits\"" , "clang/include/clang/AST/Type.h", 409, __extension__ __PRETTY_FUNCTION__ )); | ||||||
410 | Mask |= mask; | ||||||
411 | } | ||||||
412 | |||||||
413 | /// Return true if the set contains any qualifiers which require an ExtQuals | ||||||
414 | /// node to be allocated. | ||||||
415 | bool hasNonFastQualifiers() const { return Mask & ~FastMask; } | ||||||
416 | Qualifiers getNonFastQualifiers() const { | ||||||
417 | Qualifiers Quals = *this; | ||||||
418 | Quals.setFastQualifiers(0); | ||||||
419 | return Quals; | ||||||
420 | } | ||||||
421 | |||||||
422 | /// Return true if the set contains any qualifiers. | ||||||
423 | bool hasQualifiers() const { return Mask; } | ||||||
424 | bool empty() const { return !Mask; } | ||||||
425 | |||||||
426 | /// Add the qualifiers from the given set to this set. | ||||||
427 | void addQualifiers(Qualifiers Q) { | ||||||
428 | // If the other set doesn't have any non-boolean qualifiers, just | ||||||
429 | // bit-or it in. | ||||||
430 | if (!(Q.Mask & ~CVRMask)) | ||||||
431 | Mask |= Q.Mask; | ||||||
432 | else { | ||||||
433 | Mask |= (Q.Mask & CVRMask); | ||||||
434 | if (Q.hasAddressSpace()) | ||||||
435 | addAddressSpace(Q.getAddressSpace()); | ||||||
436 | if (Q.hasObjCGCAttr()) | ||||||
437 | addObjCGCAttr(Q.getObjCGCAttr()); | ||||||
438 | if (Q.hasObjCLifetime()) | ||||||
439 | addObjCLifetime(Q.getObjCLifetime()); | ||||||
440 | } | ||||||
441 | } | ||||||
442 | |||||||
443 | /// Remove the qualifiers from the given set from this set. | ||||||
444 | void removeQualifiers(Qualifiers Q) { | ||||||
445 | // If the other set doesn't have any non-boolean qualifiers, just | ||||||
446 | // bit-and the inverse in. | ||||||
447 | if (!(Q.Mask & ~CVRMask)) | ||||||
448 | Mask &= ~Q.Mask; | ||||||
449 | else { | ||||||
450 | Mask &= ~(Q.Mask & CVRMask); | ||||||
451 | if (getObjCGCAttr() == Q.getObjCGCAttr()) | ||||||
452 | removeObjCGCAttr(); | ||||||
453 | if (getObjCLifetime() == Q.getObjCLifetime()) | ||||||
454 | removeObjCLifetime(); | ||||||
455 | if (getAddressSpace() == Q.getAddressSpace()) | ||||||
456 | removeAddressSpace(); | ||||||
457 | } | ||||||
458 | } | ||||||
459 | |||||||
460 | /// Add the qualifiers from the given set to this set, given that | ||||||
461 | /// they don't conflict. | ||||||
462 | void addConsistentQualifiers(Qualifiers qs) { | ||||||
463 | assert(getAddressSpace() == qs.getAddressSpace() ||(static_cast <bool> (getAddressSpace() == qs.getAddressSpace () || !hasAddressSpace() || !qs.hasAddressSpace()) ? void (0) : __assert_fail ("getAddressSpace() == qs.getAddressSpace() || !hasAddressSpace() || !qs.hasAddressSpace()" , "clang/include/clang/AST/Type.h", 464, __extension__ __PRETTY_FUNCTION__ )) | ||||||
464 | !hasAddressSpace() || !qs.hasAddressSpace())(static_cast <bool> (getAddressSpace() == qs.getAddressSpace () || !hasAddressSpace() || !qs.hasAddressSpace()) ? void (0) : __assert_fail ("getAddressSpace() == qs.getAddressSpace() || !hasAddressSpace() || !qs.hasAddressSpace()" , "clang/include/clang/AST/Type.h", 464, __extension__ __PRETTY_FUNCTION__ )); | ||||||
465 | assert(getObjCGCAttr() == qs.getObjCGCAttr() ||(static_cast <bool> (getObjCGCAttr() == qs.getObjCGCAttr () || !hasObjCGCAttr() || !qs.hasObjCGCAttr()) ? void (0) : __assert_fail ("getObjCGCAttr() == qs.getObjCGCAttr() || !hasObjCGCAttr() || !qs.hasObjCGCAttr()" , "clang/include/clang/AST/Type.h", 466, __extension__ __PRETTY_FUNCTION__ )) | ||||||
466 | !hasObjCGCAttr() || !qs.hasObjCGCAttr())(static_cast <bool> (getObjCGCAttr() == qs.getObjCGCAttr () || !hasObjCGCAttr() || !qs.hasObjCGCAttr()) ? void (0) : __assert_fail ("getObjCGCAttr() == qs.getObjCGCAttr() || !hasObjCGCAttr() || !qs.hasObjCGCAttr()" , "clang/include/clang/AST/Type.h", 466, __extension__ __PRETTY_FUNCTION__ )); | ||||||
467 | assert(getObjCLifetime() == qs.getObjCLifetime() ||(static_cast <bool> (getObjCLifetime() == qs.getObjCLifetime () || !hasObjCLifetime() || !qs.hasObjCLifetime()) ? void (0) : __assert_fail ("getObjCLifetime() == qs.getObjCLifetime() || !hasObjCLifetime() || !qs.hasObjCLifetime()" , "clang/include/clang/AST/Type.h", 468, __extension__ __PRETTY_FUNCTION__ )) | ||||||
468 | !hasObjCLifetime() || !qs.hasObjCLifetime())(static_cast <bool> (getObjCLifetime() == qs.getObjCLifetime () || !hasObjCLifetime() || !qs.hasObjCLifetime()) ? void (0) : __assert_fail ("getObjCLifetime() == qs.getObjCLifetime() || !hasObjCLifetime() || !qs.hasObjCLifetime()" , "clang/include/clang/AST/Type.h", 468, __extension__ __PRETTY_FUNCTION__ )); | ||||||
469 | Mask |= qs.Mask; | ||||||
470 | } | ||||||
471 | |||||||
472 | /// Returns true if address space A is equal to or a superset of B. | ||||||
473 | /// OpenCL v2.0 defines conversion rules (OpenCLC v2.0 s6.5.5) and notion of | ||||||
474 | /// overlapping address spaces. | ||||||
475 | /// CL1.1 or CL1.2: | ||||||
476 | /// every address space is a superset of itself. | ||||||
477 | /// CL2.0 adds: | ||||||
478 | /// __generic is a superset of any address space except for __constant. | ||||||
479 | static bool isAddressSpaceSupersetOf(LangAS A, LangAS B) { | ||||||
480 | // Address spaces must match exactly. | ||||||
481 | return A == B || | ||||||
482 | // Otherwise in OpenCLC v2.0 s6.5.5: every address space except | ||||||
483 | // for __constant can be used as __generic. | ||||||
484 | (A == LangAS::opencl_generic && B != LangAS::opencl_constant) || | ||||||
485 | // We also define global_device and global_host address spaces, | ||||||
486 | // to distinguish global pointers allocated on host from pointers | ||||||
487 | // allocated on device, which are a subset of __global. | ||||||
488 | (A == LangAS::opencl_global && (B == LangAS::opencl_global_device || | ||||||
489 | B == LangAS::opencl_global_host)) || | ||||||
490 | (A == LangAS::sycl_global && (B == LangAS::sycl_global_device || | ||||||
491 | B == LangAS::sycl_global_host)) || | ||||||
492 | // Consider pointer size address spaces to be equivalent to default. | ||||||
493 | ((isPtrSizeAddressSpace(A) || A == LangAS::Default) && | ||||||
494 | (isPtrSizeAddressSpace(B) || B == LangAS::Default)) || | ||||||
495 | // Default is a superset of SYCL address spaces. | ||||||
496 | (A == LangAS::Default && | ||||||
497 | (B == LangAS::sycl_private || B == LangAS::sycl_local || | ||||||
498 | B == LangAS::sycl_global || B == LangAS::sycl_global_device || | ||||||
499 | B == LangAS::sycl_global_host)) || | ||||||
500 | // In HIP device compilation, any cuda address space is allowed | ||||||
501 | // to implicitly cast into the default address space. | ||||||
502 | (A == LangAS::Default && | ||||||
503 | (B == LangAS::cuda_constant || B == LangAS::cuda_device || | ||||||
504 | B == LangAS::cuda_shared)); | ||||||
505 | } | ||||||
506 | |||||||
507 | /// Returns true if the address space in these qualifiers is equal to or | ||||||
508 | /// a superset of the address space in the argument qualifiers. | ||||||
509 | bool isAddressSpaceSupersetOf(Qualifiers other) const { | ||||||
510 | return isAddressSpaceSupersetOf(getAddressSpace(), other.getAddressSpace()); | ||||||
511 | } | ||||||
512 | |||||||
513 | /// Determines if these qualifiers compatibly include another set. | ||||||
514 | /// Generally this answers the question of whether an object with the other | ||||||
515 | /// qualifiers can be safely used as an object with these qualifiers. | ||||||
516 | bool compatiblyIncludes(Qualifiers other) const { | ||||||
517 | return isAddressSpaceSupersetOf(other) && | ||||||
518 | // ObjC GC qualifiers can match, be added, or be removed, but can't | ||||||
519 | // be changed. | ||||||
520 | (getObjCGCAttr() == other.getObjCGCAttr() || !hasObjCGCAttr() || | ||||||
521 | !other.hasObjCGCAttr()) && | ||||||
522 | // ObjC lifetime qualifiers must match exactly. | ||||||
523 | getObjCLifetime() == other.getObjCLifetime() && | ||||||
524 | // CVR qualifiers may subset. | ||||||
525 | (((Mask & CVRMask) | (other.Mask & CVRMask)) == (Mask & CVRMask)) && | ||||||
526 | // U qualifier may superset. | ||||||
527 | (!other.hasUnaligned() || hasUnaligned()); | ||||||
528 | } | ||||||
529 | |||||||
530 | /// Determines if these qualifiers compatibly include another set of | ||||||
531 | /// qualifiers from the narrow perspective of Objective-C ARC lifetime. | ||||||
532 | /// | ||||||
533 | /// One set of Objective-C lifetime qualifiers compatibly includes the other | ||||||
534 | /// if the lifetime qualifiers match, or if both are non-__weak and the | ||||||
535 | /// including set also contains the 'const' qualifier, or both are non-__weak | ||||||
536 | /// and one is None (which can only happen in non-ARC modes). | ||||||
537 | bool compatiblyIncludesObjCLifetime(Qualifiers other) const { | ||||||
538 | if (getObjCLifetime() == other.getObjCLifetime()) | ||||||
539 | return true; | ||||||
540 | |||||||
541 | if (getObjCLifetime() == OCL_Weak || other.getObjCLifetime() == OCL_Weak) | ||||||
542 | return false; | ||||||
543 | |||||||
544 | if (getObjCLifetime() == OCL_None || other.getObjCLifetime() == OCL_None) | ||||||
545 | return true; | ||||||
546 | |||||||
547 | return hasConst(); | ||||||
548 | } | ||||||
549 | |||||||
550 | /// Determine whether this set of qualifiers is a strict superset of | ||||||
551 | /// another set of qualifiers, not considering qualifier compatibility. | ||||||
552 | bool isStrictSupersetOf(Qualifiers Other) const; | ||||||
553 | |||||||
554 | bool operator==(Qualifiers Other) const { return Mask == Other.Mask; } | ||||||
555 | bool operator!=(Qualifiers Other) const { return Mask != Other.Mask; } | ||||||
556 | |||||||
557 | explicit operator bool() const { return hasQualifiers(); } | ||||||
558 | |||||||
559 | Qualifiers &operator+=(Qualifiers R) { | ||||||
560 | addQualifiers(R); | ||||||
561 | return *this; | ||||||
562 | } | ||||||
563 | |||||||
564 | // Union two qualifier sets. If an enumerated qualifier appears | ||||||
565 | // in both sets, use the one from the right. | ||||||
566 | friend Qualifiers operator+(Qualifiers L, Qualifiers R) { | ||||||
567 | L += R; | ||||||
568 | return L; | ||||||
569 | } | ||||||
570 | |||||||
571 | Qualifiers &operator-=(Qualifiers R) { | ||||||
572 | removeQualifiers(R); | ||||||
573 | return *this; | ||||||
574 | } | ||||||
575 | |||||||
576 | /// Compute the difference between two qualifier sets. | ||||||
577 | friend Qualifiers operator-(Qualifiers L, Qualifiers R) { | ||||||
578 | L -= R; | ||||||
579 | return L; | ||||||
580 | } | ||||||
581 | |||||||
582 | std::string getAsString() const; | ||||||
583 | std::string getAsString(const PrintingPolicy &Policy) const; | ||||||
584 | |||||||
585 | static std::string getAddrSpaceAsString(LangAS AS); | ||||||
586 | |||||||
587 | bool isEmptyWhenPrinted(const PrintingPolicy &Policy) const; | ||||||
588 | void print(raw_ostream &OS, const PrintingPolicy &Policy, | ||||||
589 | bool appendSpaceIfNonEmpty = false) const; | ||||||
590 | |||||||
591 | void Profile(llvm::FoldingSetNodeID &ID) const { | ||||||
592 | ID.AddInteger(Mask); | ||||||
593 | } | ||||||
594 | |||||||
595 | private: | ||||||
596 | // bits: |0 1 2|3|4 .. 5|6 .. 8|9 ... 31| | ||||||
597 | // |C R V|U|GCAttr|Lifetime|AddressSpace| | ||||||
598 | uint32_t Mask = 0; | ||||||
599 | |||||||
600 | static const uint32_t UMask = 0x8; | ||||||
601 | static const uint32_t UShift = 3; | ||||||
602 | static const uint32_t GCAttrMask = 0x30; | ||||||
603 | static const uint32_t GCAttrShift = 4; | ||||||
604 | static const uint32_t LifetimeMask = 0x1C0; | ||||||
605 | static const uint32_t LifetimeShift = 6; | ||||||
606 | static const uint32_t AddressSpaceMask = | ||||||
607 | ~(CVRMask | UMask | GCAttrMask | LifetimeMask); | ||||||
608 | static const uint32_t AddressSpaceShift = 9; | ||||||
609 | }; | ||||||
610 | |||||||
611 | /// A std::pair-like structure for storing a qualified type split | ||||||
612 | /// into its local qualifiers and its locally-unqualified type. | ||||||
613 | struct SplitQualType { | ||||||
614 | /// The locally-unqualified type. | ||||||
615 | const Type *Ty = nullptr; | ||||||
616 | |||||||
617 | /// The local qualifiers. | ||||||
618 | Qualifiers Quals; | ||||||
619 | |||||||
620 | SplitQualType() = default; | ||||||
621 | SplitQualType(const Type *ty, Qualifiers qs) : Ty(ty), Quals(qs) {} | ||||||
622 | |||||||
623 | SplitQualType getSingleStepDesugaredType() const; // end of this file | ||||||
624 | |||||||
625 | // Make std::tie work. | ||||||
626 | std::pair<const Type *,Qualifiers> asPair() const { | ||||||
627 | return std::pair<const Type *, Qualifiers>(Ty, Quals); | ||||||
628 | } | ||||||
629 | |||||||
630 | friend bool operator==(SplitQualType a, SplitQualType b) { | ||||||
631 | return a.Ty == b.Ty && a.Quals == b.Quals; | ||||||
632 | } | ||||||
633 | friend bool operator!=(SplitQualType a, SplitQualType b) { | ||||||
634 | return a.Ty != b.Ty || a.Quals != b.Quals; | ||||||
635 | } | ||||||
636 | }; | ||||||
637 | |||||||
638 | /// The kind of type we are substituting Objective-C type arguments into. | ||||||
639 | /// | ||||||
640 | /// The kind of substitution affects the replacement of type parameters when | ||||||
641 | /// no concrete type information is provided, e.g., when dealing with an | ||||||
642 | /// unspecialized type. | ||||||
643 | enum class ObjCSubstitutionContext { | ||||||
644 | /// An ordinary type. | ||||||
645 | Ordinary, | ||||||
646 | |||||||
647 | /// The result type of a method or function. | ||||||
648 | Result, | ||||||
649 | |||||||
650 | /// The parameter type of a method or function. | ||||||
651 | Parameter, | ||||||
652 | |||||||
653 | /// The type of a property. | ||||||
654 | Property, | ||||||
655 | |||||||
656 | /// The superclass of a type. | ||||||
657 | Superclass, | ||||||
658 | }; | ||||||
659 | |||||||
660 | /// A (possibly-)qualified type. | ||||||
661 | /// | ||||||
662 | /// For efficiency, we don't store CV-qualified types as nodes on their | ||||||
663 | /// own: instead each reference to a type stores the qualifiers. This | ||||||
664 | /// greatly reduces the number of nodes we need to allocate for types (for | ||||||
665 | /// example we only need one for 'int', 'const int', 'volatile int', | ||||||
666 | /// 'const volatile int', etc). | ||||||
667 | /// | ||||||
668 | /// As an added efficiency bonus, instead of making this a pair, we | ||||||
669 | /// just store the two bits we care about in the low bits of the | ||||||
670 | /// pointer. To handle the packing/unpacking, we make QualType be a | ||||||
671 | /// simple wrapper class that acts like a smart pointer. A third bit | ||||||
672 | /// indicates whether there are extended qualifiers present, in which | ||||||
673 | /// case the pointer points to a special structure. | ||||||
674 | class QualType { | ||||||
675 | friend class QualifierCollector; | ||||||
676 | |||||||
677 | // Thankfully, these are efficiently composable. | ||||||
678 | llvm::PointerIntPair<llvm::PointerUnion<const Type *, const ExtQuals *>, | ||||||
679 | Qualifiers::FastWidth> Value; | ||||||
680 | |||||||
681 | const ExtQuals *getExtQualsUnsafe() const { | ||||||
682 | return Value.getPointer().get<const ExtQuals*>(); | ||||||
683 | } | ||||||
684 | |||||||
685 | const Type *getTypePtrUnsafe() const { | ||||||
686 | return Value.getPointer().get<const Type*>(); | ||||||
687 | } | ||||||
688 | |||||||
689 | const ExtQualsTypeCommonBase *getCommonPtr() const { | ||||||
690 | assert(!isNull() && "Cannot retrieve a NULL type pointer")(static_cast <bool> (!isNull() && "Cannot retrieve a NULL type pointer" ) ? void (0) : __assert_fail ("!isNull() && \"Cannot retrieve a NULL type pointer\"" , "clang/include/clang/AST/Type.h", 690, __extension__ __PRETTY_FUNCTION__ )); | ||||||
691 | auto CommonPtrVal = reinterpret_cast<uintptr_t>(Value.getOpaqueValue()); | ||||||
692 | CommonPtrVal &= ~(uintptr_t)((1 << TypeAlignmentInBits) - 1); | ||||||
693 | return reinterpret_cast<ExtQualsTypeCommonBase*>(CommonPtrVal); | ||||||
694 | } | ||||||
695 | |||||||
696 | public: | ||||||
697 | QualType() = default; | ||||||
698 | QualType(const Type *Ptr, unsigned Quals) : Value(Ptr, Quals) {} | ||||||
699 | QualType(const ExtQuals *Ptr, unsigned Quals) : Value(Ptr, Quals) {} | ||||||
700 | |||||||
701 | unsigned getLocalFastQualifiers() const { return Value.getInt(); } | ||||||
702 | void setLocalFastQualifiers(unsigned Quals) { Value.setInt(Quals); } | ||||||
703 | |||||||
704 | /// Retrieves a pointer to the underlying (unqualified) type. | ||||||
705 | /// | ||||||
706 | /// This function requires that the type not be NULL. If the type might be | ||||||
707 | /// NULL, use the (slightly less efficient) \c getTypePtrOrNull(). | ||||||
708 | const Type *getTypePtr() const; | ||||||
709 | |||||||
710 | const Type *getTypePtrOrNull() const; | ||||||
711 | |||||||
712 | /// Retrieves a pointer to the name of the base type. | ||||||
713 | const IdentifierInfo *getBaseTypeIdentifier() const; | ||||||
714 | |||||||
715 | /// Divides a QualType into its unqualified type and a set of local | ||||||
716 | /// qualifiers. | ||||||
717 | SplitQualType split() const; | ||||||
718 | |||||||
719 | void *getAsOpaquePtr() const { return Value.getOpaqueValue(); } | ||||||
720 | |||||||
721 | static QualType getFromOpaquePtr(const void *Ptr) { | ||||||
722 | QualType T; | ||||||
723 | T.Value.setFromOpaqueValue(const_cast<void*>(Ptr)); | ||||||
724 | return T; | ||||||
725 | } | ||||||
726 | |||||||
727 | const Type &operator*() const { | ||||||
728 | return *getTypePtr(); | ||||||
729 | } | ||||||
730 | |||||||
731 | const Type *operator->() const { | ||||||
732 | return getTypePtr(); | ||||||
733 | } | ||||||
734 | |||||||
735 | bool isCanonical() const; | ||||||
736 | bool isCanonicalAsParam() const; | ||||||
737 | |||||||
738 | /// Return true if this QualType doesn't point to a type yet. | ||||||
739 | bool isNull() const { | ||||||
740 | return Value.getPointer().isNull(); | ||||||
741 | } | ||||||
742 | |||||||
743 | /// Determine whether this particular QualType instance has the | ||||||
744 | /// "const" qualifier set, without looking through typedefs that may have | ||||||
745 | /// added "const" at a different level. | ||||||
746 | bool isLocalConstQualified() const { | ||||||
747 | return (getLocalFastQualifiers() & Qualifiers::Const); | ||||||
748 | } | ||||||
749 | |||||||
750 | /// Determine whether this type is const-qualified. | ||||||
751 | bool isConstQualified() const; | ||||||
752 | |||||||
753 | /// Determine whether this particular QualType instance has the | ||||||
754 | /// "restrict" qualifier set, without looking through typedefs that may have | ||||||
755 | /// added "restrict" at a different level. | ||||||
756 | bool isLocalRestrictQualified() const { | ||||||
757 | return (getLocalFastQualifiers() & Qualifiers::Restrict); | ||||||
758 | } | ||||||
759 | |||||||
760 | /// Determine whether this type is restrict-qualified. | ||||||
761 | bool isRestrictQualified() const; | ||||||
762 | |||||||
763 | /// Determine whether this particular QualType instance has the | ||||||
764 | /// "volatile" qualifier set, without looking through typedefs that may have | ||||||
765 | /// added "volatile" at a different level. | ||||||
766 | bool isLocalVolatileQualified() const { | ||||||
767 | return (getLocalFastQualifiers() & Qualifiers::Volatile); | ||||||
768 | } | ||||||
769 | |||||||
770 | /// Determine whether this type is volatile-qualified. | ||||||
771 | bool isVolatileQualified() const; | ||||||
772 | |||||||
773 | /// Determine whether this particular QualType instance has any | ||||||
774 | /// qualifiers, without looking through any typedefs that might add | ||||||
775 | /// qualifiers at a different level. | ||||||
776 | bool hasLocalQualifiers() const { | ||||||
777 | return getLocalFastQualifiers() || hasLocalNonFastQualifiers(); | ||||||
778 | } | ||||||
779 | |||||||
780 | /// Determine whether this type has any qualifiers. | ||||||
781 | bool hasQualifiers() const; | ||||||
782 | |||||||
783 | /// Determine whether this particular QualType instance has any | ||||||
784 | /// "non-fast" qualifiers, e.g., those that are stored in an ExtQualType | ||||||
785 | /// instance. | ||||||
786 | bool hasLocalNonFastQualifiers() const { | ||||||
787 | return Value.getPointer().is<const ExtQuals*>(); | ||||||
788 | } | ||||||
789 | |||||||
790 | /// Retrieve the set of qualifiers local to this particular QualType | ||||||
791 | /// instance, not including any qualifiers acquired through typedefs or | ||||||
792 | /// other sugar. | ||||||
793 | Qualifiers getLocalQualifiers() const; | ||||||
794 | |||||||
795 | /// Retrieve the set of qualifiers applied to this type. | ||||||
796 | Qualifiers getQualifiers() const; | ||||||
797 | |||||||
798 | /// Retrieve the set of CVR (const-volatile-restrict) qualifiers | ||||||
799 | /// local to this particular QualType instance, not including any qualifiers | ||||||
800 | /// acquired through typedefs or other sugar. | ||||||
801 | unsigned getLocalCVRQualifiers() const { | ||||||
802 | return getLocalFastQualifiers(); | ||||||
803 | } | ||||||
804 | |||||||
805 | /// Retrieve the set of CVR (const-volatile-restrict) qualifiers | ||||||
806 | /// applied to this type. | ||||||
807 | unsigned getCVRQualifiers() const; | ||||||
808 | |||||||
809 | bool isConstant(const ASTContext& Ctx) const { | ||||||
810 | return QualType::isConstant(*this, Ctx); | ||||||
811 | } | ||||||
812 | |||||||
813 | /// Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10). | ||||||
814 | bool isPODType(const ASTContext &Context) const; | ||||||
815 | |||||||
816 | /// Return true if this is a POD type according to the rules of the C++98 | ||||||
817 | /// standard, regardless of the current compilation's language. | ||||||
818 | bool isCXX98PODType(const ASTContext &Context) const; | ||||||
819 | |||||||
820 | /// Return true if this is a POD type according to the more relaxed rules | ||||||
821 | /// of the C++11 standard, regardless of the current compilation's language. | ||||||
822 | /// (C++0x [basic.types]p9). Note that, unlike | ||||||
823 | /// CXXRecordDecl::isCXX11StandardLayout, this takes DRs into account. | ||||||
824 | bool isCXX11PODType(const ASTContext &Context) const; | ||||||
825 | |||||||
826 | /// Return true if this is a trivial type per (C++0x [basic.types]p9) | ||||||
827 | bool isTrivialType(const ASTContext &Context) const; | ||||||
828 | |||||||
829 | /// Return true if this is a trivially copyable type (C++0x [basic.types]p9) | ||||||
830 | bool isTriviallyCopyableType(const ASTContext &Context) const; | ||||||
831 | |||||||
832 | /// Return true if this is a trivially relocatable type. | ||||||
833 | bool isTriviallyRelocatableType(const ASTContext &Context) const; | ||||||
834 | |||||||
835 | /// Returns true if it is a class and it might be dynamic. | ||||||
836 | bool mayBeDynamicClass() const; | ||||||
837 | |||||||
838 | /// Returns true if it is not a class or if the class might not be dynamic. | ||||||
839 | bool mayBeNotDynamicClass() const; | ||||||
840 | |||||||
841 | // Don't promise in the API that anything besides 'const' can be | ||||||
842 | // easily added. | ||||||
843 | |||||||
844 | /// Add the `const` type qualifier to this QualType. | ||||||
845 | void addConst() { | ||||||
846 | addFastQualifiers(Qualifiers::Const); | ||||||
847 | } | ||||||
848 | QualType withConst() const { | ||||||
849 | return withFastQualifiers(Qualifiers::Const); | ||||||
850 | } | ||||||
851 | |||||||
852 | /// Add the `volatile` type qualifier to this QualType. | ||||||
853 | void addVolatile() { | ||||||
854 | addFastQualifiers(Qualifiers::Volatile); | ||||||
855 | } | ||||||
856 | QualType withVolatile() const { | ||||||
857 | return withFastQualifiers(Qualifiers::Volatile); | ||||||
858 | } | ||||||
859 | |||||||
860 | /// Add the `restrict` qualifier to this QualType. | ||||||
861 | void addRestrict() { | ||||||
862 | addFastQualifiers(Qualifiers::Restrict); | ||||||
863 | } | ||||||
864 | QualType withRestrict() const { | ||||||
865 | return withFastQualifiers(Qualifiers::Restrict); | ||||||
866 | } | ||||||
867 | |||||||
868 | QualType withCVRQualifiers(unsigned CVR) const { | ||||||
869 | return withFastQualifiers(CVR); | ||||||
870 | } | ||||||
871 | |||||||
872 | void addFastQualifiers(unsigned TQs) { | ||||||
873 | assert(!(TQs & ~Qualifiers::FastMask)(static_cast <bool> (!(TQs & ~Qualifiers::FastMask) && "non-fast qualifier bits set in mask!") ? void (0 ) : __assert_fail ("!(TQs & ~Qualifiers::FastMask) && \"non-fast qualifier bits set in mask!\"" , "clang/include/clang/AST/Type.h", 874, __extension__ __PRETTY_FUNCTION__ )) | ||||||
874 | && "non-fast qualifier bits set in mask!")(static_cast <bool> (!(TQs & ~Qualifiers::FastMask) && "non-fast qualifier bits set in mask!") ? void (0 ) : __assert_fail ("!(TQs & ~Qualifiers::FastMask) && \"non-fast qualifier bits set in mask!\"" , "clang/include/clang/AST/Type.h", 874, __extension__ __PRETTY_FUNCTION__ )); | ||||||
875 | Value.setInt(Value.getInt() | TQs); | ||||||
876 | } | ||||||
877 | |||||||
878 | void removeLocalConst(); | ||||||
879 | void removeLocalVolatile(); | ||||||
880 | void removeLocalRestrict(); | ||||||
881 | void removeLocalCVRQualifiers(unsigned Mask); | ||||||
882 | |||||||
883 | void removeLocalFastQualifiers() { Value.setInt(0); } | ||||||
884 | void removeLocalFastQualifiers(unsigned Mask) { | ||||||
885 | assert(!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers")(static_cast <bool> (!(Mask & ~Qualifiers::FastMask ) && "mask has non-fast qualifiers") ? void (0) : __assert_fail ("!(Mask & ~Qualifiers::FastMask) && \"mask has non-fast qualifiers\"" , "clang/include/clang/AST/Type.h", 885, __extension__ __PRETTY_FUNCTION__ )); | ||||||
886 | Value.setInt(Value.getInt() & ~Mask); | ||||||
887 | } | ||||||
888 | |||||||
889 | // Creates a type with the given qualifiers in addition to any | ||||||
890 | // qualifiers already on this type. | ||||||
891 | QualType withFastQualifiers(unsigned TQs) const { | ||||||
892 | QualType T = *this; | ||||||
893 | T.addFastQualifiers(TQs); | ||||||
894 | return T; | ||||||
895 | } | ||||||
896 | |||||||
897 | // Creates a type with exactly the given fast qualifiers, removing | ||||||
898 | // any existing fast qualifiers. | ||||||
899 | QualType withExactLocalFastQualifiers(unsigned TQs) const { | ||||||
900 | return withoutLocalFastQualifiers().withFastQualifiers(TQs); | ||||||
901 | } | ||||||
902 | |||||||
903 | // Removes fast qualifiers, but leaves any extended qualifiers in place. | ||||||
904 | QualType withoutLocalFastQualifiers() const { | ||||||
905 | QualType T = *this; | ||||||
906 | T.removeLocalFastQualifiers(); | ||||||
907 | return T; | ||||||
908 | } | ||||||
909 | |||||||
910 | QualType getCanonicalType() const; | ||||||
911 | |||||||
912 | /// Return this type with all of the instance-specific qualifiers | ||||||
913 | /// removed, but without removing any qualifiers that may have been applied | ||||||
914 | /// through typedefs. | ||||||
915 | QualType getLocalUnqualifiedType() const { return QualType(getTypePtr(), 0); } | ||||||
916 | |||||||
917 | /// Retrieve the unqualified variant of the given type, | ||||||
918 | /// removing as little sugar as possible. | ||||||
919 | /// | ||||||
920 | /// This routine looks through various kinds of sugar to find the | ||||||
921 | /// least-desugared type that is unqualified. For example, given: | ||||||
922 | /// | ||||||
923 | /// \code | ||||||
924 | /// typedef int Integer; | ||||||
925 | /// typedef const Integer CInteger; | ||||||
926 | /// typedef CInteger DifferenceType; | ||||||
927 | /// \endcode | ||||||
928 | /// | ||||||
929 | /// Executing \c getUnqualifiedType() on the type \c DifferenceType will | ||||||
930 | /// desugar until we hit the type \c Integer, which has no qualifiers on it. | ||||||
931 | /// | ||||||
932 | /// The resulting type might still be qualified if it's sugar for an array | ||||||
933 | /// type. To strip qualifiers even from within a sugared array type, use | ||||||
934 | /// ASTContext::getUnqualifiedArrayType. | ||||||
935 | inline QualType getUnqualifiedType() const; | ||||||
936 | |||||||
937 | /// Retrieve the unqualified variant of the given type, removing as little | ||||||
938 | /// sugar as possible. | ||||||
939 | /// | ||||||
940 | /// Like getUnqualifiedType(), but also returns the set of | ||||||
941 | /// qualifiers that were built up. | ||||||
942 | /// | ||||||
943 | /// The resulting type might still be qualified if it's sugar for an array | ||||||
944 | /// type. To strip qualifiers even from within a sugared array type, use | ||||||
945 | /// ASTContext::getUnqualifiedArrayType. | ||||||
946 | inline SplitQualType getSplitUnqualifiedType() const; | ||||||
947 | |||||||
948 | /// Determine whether this type is more qualified than the other | ||||||
949 | /// given type, requiring exact equality for non-CVR qualifiers. | ||||||
950 | bool isMoreQualifiedThan(QualType Other) const; | ||||||
951 | |||||||
952 | /// Determine whether this type is at least as qualified as the other | ||||||
953 | /// given type, requiring exact equality for non-CVR qualifiers. | ||||||
954 | bool isAtLeastAsQualifiedAs(QualType Other) const; | ||||||
955 | |||||||
956 | QualType getNonReferenceType() const; | ||||||
957 | |||||||
958 | /// Determine the type of a (typically non-lvalue) expression with the | ||||||
959 | /// specified result type. | ||||||
960 | /// | ||||||
961 | /// This routine should be used for expressions for which the return type is | ||||||
962 | /// explicitly specified (e.g., in a cast or call) and isn't necessarily | ||||||
963 | /// an lvalue. It removes a top-level reference (since there are no | ||||||
964 | /// expressions of reference type) and deletes top-level cvr-qualifiers | ||||||
965 | /// from non-class types (in C++) or all types (in C). | ||||||
966 | QualType getNonLValueExprType(const ASTContext &Context) const; | ||||||
967 | |||||||
968 | /// Remove an outer pack expansion type (if any) from this type. Used as part | ||||||
969 | /// of converting the type of a declaration to the type of an expression that | ||||||
970 | /// references that expression. It's meaningless for an expression to have a | ||||||
971 | /// pack expansion type. | ||||||
972 | QualType getNonPackExpansionType() const; | ||||||
973 | |||||||
974 | /// Return the specified type with any "sugar" removed from | ||||||
975 | /// the type. This takes off typedefs, typeof's etc. If the outer level of | ||||||
976 | /// the type is already concrete, it returns it unmodified. This is similar | ||||||
977 | /// to getting the canonical type, but it doesn't remove *all* typedefs. For | ||||||
978 | /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is | ||||||
979 | /// concrete. | ||||||
980 | /// | ||||||
981 | /// Qualifiers are left in place. | ||||||
982 | QualType getDesugaredType(const ASTContext &Context) const { | ||||||
983 | return getDesugaredType(*this, Context); | ||||||
984 | } | ||||||
985 | |||||||
986 | SplitQualType getSplitDesugaredType() const { | ||||||
987 | return getSplitDesugaredType(*this); | ||||||
988 | } | ||||||
989 | |||||||
990 | /// Return the specified type with one level of "sugar" removed from | ||||||
991 | /// the type. | ||||||
992 | /// | ||||||
993 | /// This routine takes off the first typedef, typeof, etc. If the outer level | ||||||
994 | /// of the type is already concrete, it returns it unmodified. | ||||||
995 | QualType getSingleStepDesugaredType(const ASTContext &Context) const { | ||||||
996 | return getSingleStepDesugaredTypeImpl(*this, Context); | ||||||
997 | } | ||||||
998 | |||||||
999 | /// Returns the specified type after dropping any | ||||||
1000 | /// outer-level parentheses. | ||||||
1001 | QualType IgnoreParens() const { | ||||||
1002 | if (isa<ParenType>(*this)) | ||||||
1003 | return QualType::IgnoreParens(*this); | ||||||
1004 | return *this; | ||||||
1005 | } | ||||||
1006 | |||||||
1007 | /// Indicate whether the specified types and qualifiers are identical. | ||||||
1008 | friend bool operator==(const QualType &LHS, const QualType &RHS) { | ||||||
1009 | return LHS.Value == RHS.Value; | ||||||
1010 | } | ||||||
1011 | friend bool operator!=(const QualType &LHS, const QualType &RHS) { | ||||||
1012 | return LHS.Value != RHS.Value; | ||||||
1013 | } | ||||||
1014 | friend bool operator<(const QualType &LHS, const QualType &RHS) { | ||||||
1015 | return LHS.Value < RHS.Value; | ||||||
1016 | } | ||||||
1017 | |||||||
1018 | static std::string getAsString(SplitQualType split, | ||||||
1019 | const PrintingPolicy &Policy) { | ||||||
1020 | return getAsString(split.Ty, split.Quals, Policy); | ||||||
1021 | } | ||||||
1022 | static std::string getAsString(const Type *ty, Qualifiers qs, | ||||||
1023 | const PrintingPolicy &Policy); | ||||||
1024 | |||||||
1025 | std::string getAsString() const; | ||||||
1026 | std::string getAsString(const PrintingPolicy &Policy) const; | ||||||
1027 | |||||||
1028 | void print(raw_ostream &OS, const PrintingPolicy &Policy, | ||||||
1029 | const Twine &PlaceHolder = Twine(), | ||||||
1030 | unsigned Indentation = 0) const; | ||||||
1031 | |||||||
1032 | static void print(SplitQualType split, raw_ostream &OS, | ||||||
1033 | const PrintingPolicy &policy, const Twine &PlaceHolder, | ||||||
1034 | unsigned Indentation = 0) { | ||||||
1035 | return print(split.Ty, split.Quals, OS, policy, PlaceHolder, Indentation); | ||||||
1036 | } | ||||||
1037 | |||||||
1038 | static void print(const Type *ty, Qualifiers qs, | ||||||
1039 | raw_ostream &OS, const PrintingPolicy &policy, | ||||||
1040 | const Twine &PlaceHolder, | ||||||
1041 | unsigned Indentation = 0); | ||||||
1042 | |||||||
1043 | void getAsStringInternal(std::string &Str, | ||||||
1044 | const PrintingPolicy &Policy) const; | ||||||
1045 | |||||||
1046 | static void getAsStringInternal(SplitQualType split, std::string &out, | ||||||
1047 | const PrintingPolicy &policy) { | ||||||
1048 | return getAsStringInternal(split.Ty, split.Quals, out, policy); | ||||||
1049 | } | ||||||
1050 | |||||||
1051 | static void getAsStringInternal(const Type *ty, Qualifiers qs, | ||||||
1052 | std::string &out, | ||||||
1053 | const PrintingPolicy &policy); | ||||||
1054 | |||||||
1055 | class StreamedQualTypeHelper { | ||||||
1056 | const QualType &T; | ||||||
1057 | const PrintingPolicy &Policy; | ||||||
1058 | const Twine &PlaceHolder; | ||||||
1059 | unsigned Indentation; | ||||||
1060 | |||||||
1061 | public: | ||||||
1062 | StreamedQualTypeHelper(const QualType &T, const PrintingPolicy &Policy, | ||||||
1063 | const Twine &PlaceHolder, unsigned Indentation) | ||||||
1064 | : T(T), Policy(Policy), PlaceHolder(PlaceHolder), | ||||||
1065 | Indentation(Indentation) {} | ||||||
1066 | |||||||
1067 | friend raw_ostream &operator<<(raw_ostream &OS, | ||||||
1068 | const StreamedQualTypeHelper &SQT) { | ||||||
1069 | SQT.T.print(OS, SQT.Policy, SQT.PlaceHolder, SQT.Indentation); | ||||||
1070 | return OS; | ||||||
1071 | } | ||||||
1072 | }; | ||||||
1073 | |||||||
1074 | StreamedQualTypeHelper stream(const PrintingPolicy &Policy, | ||||||
1075 | const Twine &PlaceHolder = Twine(), | ||||||
1076 | unsigned Indentation = 0) const { | ||||||
1077 | return StreamedQualTypeHelper(*this, Policy, PlaceHolder, Indentation); | ||||||
1078 | } | ||||||
1079 | |||||||
1080 | void dump(const char *s) const; | ||||||
1081 | void dump() const; | ||||||
1082 | void dump(llvm::raw_ostream &OS, const ASTContext &Context) const; | ||||||
1083 | |||||||
1084 | void Profile(llvm::FoldingSetNodeID &ID) const { | ||||||
1085 | ID.AddPointer(getAsOpaquePtr()); | ||||||
1086 | } | ||||||
1087 | |||||||
1088 | /// Check if this type has any address space qualifier. | ||||||
1089 | inline bool hasAddressSpace() const; | ||||||
1090 | |||||||
1091 | /// Return the address space of this type. | ||||||
1092 | inline LangAS getAddressSpace() const; | ||||||
1093 | |||||||
1094 | /// Returns true if address space qualifiers overlap with T address space | ||||||
1095 | /// qualifiers. | ||||||
1096 | /// OpenCL C defines conversion rules for pointers to different address spaces | ||||||
1097 | /// and notion of overlapping address spaces. | ||||||
1098 | /// CL1.1 or CL1.2: | ||||||
1099 | /// address spaces overlap iff they are they same. | ||||||
1100 | /// OpenCL C v2.0 s6.5.5 adds: | ||||||
1101 | /// __generic overlaps with any address space except for __constant. | ||||||
1102 | bool isAddressSpaceOverlapping(QualType T) const { | ||||||
1103 | Qualifiers Q = getQualifiers(); | ||||||
1104 | Qualifiers TQ = T.getQualifiers(); | ||||||
1105 | // Address spaces overlap if at least one of them is a superset of another | ||||||
1106 | return Q.isAddressSpaceSupersetOf(TQ) || TQ.isAddressSpaceSupersetOf(Q); | ||||||
1107 | } | ||||||
1108 | |||||||
1109 | /// Returns gc attribute of this type. | ||||||
1110 | inline Qualifiers::GC getObjCGCAttr() const; | ||||||
1111 | |||||||
1112 | /// true when Type is objc's weak. | ||||||
1113 | bool isObjCGCWeak() const { | ||||||
1114 | return getObjCGCAttr() == Qualifiers::Weak; | ||||||
1115 | } | ||||||
1116 | |||||||
1117 | /// true when Type is objc's strong. | ||||||
1118 | bool isObjCGCStrong() const { | ||||||
1119 | return getObjCGCAttr() == Qualifiers::Strong; | ||||||
1120 | } | ||||||
1121 | |||||||
1122 | /// Returns lifetime attribute of this type. | ||||||
1123 | Qualifiers::ObjCLifetime getObjCLifetime() const { | ||||||
1124 | return getQualifiers().getObjCLifetime(); | ||||||
1125 | } | ||||||
1126 | |||||||
1127 | bool hasNonTrivialObjCLifetime() const { | ||||||
1128 | return getQualifiers().hasNonTrivialObjCLifetime(); | ||||||
1129 | } | ||||||
1130 | |||||||
1131 | bool hasStrongOrWeakObjCLifetime() const { | ||||||
1132 | return getQualifiers().hasStrongOrWeakObjCLifetime(); | ||||||
1133 | } | ||||||
1134 | |||||||
1135 | // true when Type is objc's weak and weak is enabled but ARC isn't. | ||||||
1136 | bool isNonWeakInMRRWithObjCWeak(const ASTContext &Context) const; | ||||||
1137 | |||||||
1138 | enum PrimitiveDefaultInitializeKind { | ||||||
1139 | /// The type does not fall into any of the following categories. Note that | ||||||
1140 | /// this case is zero-valued so that values of this enum can be used as a | ||||||
1141 | /// boolean condition for non-triviality. | ||||||
1142 | PDIK_Trivial, | ||||||
1143 | |||||||
1144 | /// The type is an Objective-C retainable pointer type that is qualified | ||||||
1145 | /// with the ARC __strong qualifier. | ||||||
1146 | PDIK_ARCStrong, | ||||||
1147 | |||||||
1148 | /// The type is an Objective-C retainable pointer type that is qualified | ||||||
1149 | /// with the ARC __weak qualifier. | ||||||
1150 | PDIK_ARCWeak, | ||||||
1151 | |||||||
1152 | /// The type is a struct containing a field whose type is not PCK_Trivial. | ||||||
1153 | PDIK_Struct | ||||||
1154 | }; | ||||||
1155 | |||||||
1156 | /// Functions to query basic properties of non-trivial C struct types. | ||||||
1157 | |||||||
1158 | /// Check if this is a non-trivial type that would cause a C struct | ||||||
1159 | /// transitively containing this type to be non-trivial to default initialize | ||||||
1160 | /// and return the kind. | ||||||
1161 | PrimitiveDefaultInitializeKind | ||||||
1162 | isNonTrivialToPrimitiveDefaultInitialize() const; | ||||||
1163 | |||||||
1164 | enum PrimitiveCopyKind { | ||||||
1165 | /// The type does not fall into any of the following categories. Note that | ||||||
1166 | /// this case is zero-valued so that values of this enum can be used as a | ||||||
1167 | /// boolean condition for non-triviality. | ||||||
1168 | PCK_Trivial, | ||||||
1169 | |||||||
1170 | /// The type would be trivial except that it is volatile-qualified. Types | ||||||
1171 | /// that fall into one of the other non-trivial cases may additionally be | ||||||
1172 | /// volatile-qualified. | ||||||
1173 | PCK_VolatileTrivial, | ||||||
1174 | |||||||
1175 | /// The type is an Objective-C retainable pointer type that is qualified | ||||||
1176 | /// with the ARC __strong qualifier. | ||||||
1177 | PCK_ARCStrong, | ||||||
1178 | |||||||
1179 | /// The type is an Objective-C retainable pointer type that is qualified | ||||||
1180 | /// with the ARC __weak qualifier. | ||||||
1181 | PCK_ARCWeak, | ||||||
1182 | |||||||
1183 | /// The type is a struct containing a field whose type is neither | ||||||
1184 | /// PCK_Trivial nor PCK_VolatileTrivial. | ||||||
1185 | /// Note that a C++ struct type does not necessarily match this; C++ copying | ||||||
1186 | /// semantics are too complex to express here, in part because they depend | ||||||
1187 | /// on the exact constructor or assignment operator that is chosen by | ||||||
1188 | /// overload resolution to do the copy. | ||||||
1189 | PCK_Struct | ||||||
1190 | }; | ||||||
1191 | |||||||
1192 | /// Check if this is a non-trivial type that would cause a C struct | ||||||
1193 | /// transitively containing this type to be non-trivial to copy and return the | ||||||
1194 | /// kind. | ||||||
1195 | PrimitiveCopyKind isNonTrivialToPrimitiveCopy() const; | ||||||
1196 | |||||||
1197 | /// Check if this is a non-trivial type that would cause a C struct | ||||||
1198 | /// transitively containing this type to be non-trivial to destructively | ||||||
1199 | /// move and return the kind. Destructive move in this context is a C++-style | ||||||
1200 | /// move in which the source object is placed in a valid but unspecified state | ||||||
1201 | /// after it is moved, as opposed to a truly destructive move in which the | ||||||
1202 | /// source object is placed in an uninitialized state. | ||||||
1203 | PrimitiveCopyKind isNonTrivialToPrimitiveDestructiveMove() const; | ||||||
1204 | |||||||
1205 | enum DestructionKind { | ||||||
1206 | DK_none, | ||||||
1207 | DK_cxx_destructor, | ||||||
1208 | DK_objc_strong_lifetime, | ||||||
1209 | DK_objc_weak_lifetime, | ||||||
1210 | DK_nontrivial_c_struct | ||||||
1211 | }; | ||||||
1212 | |||||||
1213 | /// Returns a nonzero value if objects of this type require | ||||||
1214 | /// non-trivial work to clean up after. Non-zero because it's | ||||||
1215 | /// conceivable that qualifiers (objc_gc(weak)?) could make | ||||||
1216 | /// something require destruction. | ||||||
1217 | DestructionKind isDestructedType() const { | ||||||
1218 | return isDestructedTypeImpl(*this); | ||||||
1219 | } | ||||||
1220 | |||||||
1221 | /// Check if this is or contains a C union that is non-trivial to | ||||||
1222 | /// default-initialize, which is a union that has a member that is non-trivial | ||||||
1223 | /// to default-initialize. If this returns true, | ||||||
1224 | /// isNonTrivialToPrimitiveDefaultInitialize returns PDIK_Struct. | ||||||
1225 | bool hasNonTrivialToPrimitiveDefaultInitializeCUnion() const; | ||||||
1226 | |||||||
1227 | /// Check if this is or contains a C union that is non-trivial to destruct, | ||||||
1228 | /// which is a union that has a member that is non-trivial to destruct. If | ||||||
1229 | /// this returns true, isDestructedType returns DK_nontrivial_c_struct. | ||||||
1230 | bool hasNonTrivialToPrimitiveDestructCUnion() const; | ||||||
1231 | |||||||
1232 | /// Check if this is or contains a C union that is non-trivial to copy, which | ||||||
1233 | /// is a union that has a member that is non-trivial to copy. If this returns | ||||||
1234 | /// true, isNonTrivialToPrimitiveCopy returns PCK_Struct. | ||||||
1235 | bool hasNonTrivialToPrimitiveCopyCUnion() const; | ||||||
1236 | |||||||
1237 | /// Determine whether expressions of the given type are forbidden | ||||||
1238 | /// from being lvalues in C. | ||||||
1239 | /// | ||||||
1240 | /// The expression types that are forbidden to be lvalues are: | ||||||
1241 | /// - 'void', but not qualified void | ||||||
1242 | /// - function types | ||||||
1243 | /// | ||||||
1244 | /// The exact rule here is C99 6.3.2.1: | ||||||
1245 | /// An lvalue is an expression with an object type or an incomplete | ||||||
1246 | /// type other than void. | ||||||
1247 | bool isCForbiddenLValueType() const; | ||||||
1248 | |||||||
1249 | /// Substitute type arguments for the Objective-C type parameters used in the | ||||||
1250 | /// subject type. | ||||||
1251 | /// | ||||||
1252 | /// \param ctx ASTContext in which the type exists. | ||||||
1253 | /// | ||||||
1254 | /// \param typeArgs The type arguments that will be substituted for the | ||||||
1255 | /// Objective-C type parameters in the subject type, which are generally | ||||||
1256 | /// computed via \c Type::getObjCSubstitutions. If empty, the type | ||||||
1257 | /// parameters will be replaced with their bounds or id/Class, as appropriate | ||||||
1258 | /// for the context. | ||||||
1259 | /// | ||||||
1260 | /// \param context The context in which the subject type was written. | ||||||
1261 | /// | ||||||
1262 | /// \returns the resulting type. | ||||||
1263 | QualType substObjCTypeArgs(ASTContext &ctx, | ||||||
1264 | ArrayRef<QualType> typeArgs, | ||||||
1265 | ObjCSubstitutionContext context) const; | ||||||
1266 | |||||||
1267 | /// Substitute type arguments from an object type for the Objective-C type | ||||||
1268 | /// parameters used in the subject type. | ||||||
1269 | /// | ||||||
1270 | /// This operation combines the computation of type arguments for | ||||||
1271 | /// substitution (\c Type::getObjCSubstitutions) with the actual process of | ||||||
1272 | /// substitution (\c QualType::substObjCTypeArgs) for the convenience of | ||||||
1273 | /// callers that need to perform a single substitution in isolation. | ||||||
1274 | /// | ||||||
1275 | /// \param objectType The type of the object whose member type we're | ||||||
1276 | /// substituting into. For example, this might be the receiver of a message | ||||||
1277 | /// or the base of a property access. | ||||||
1278 | /// | ||||||
1279 | /// \param dc The declaration context from which the subject type was | ||||||
1280 | /// retrieved, which indicates (for example) which type parameters should | ||||||
1281 | /// be substituted. | ||||||
1282 | /// | ||||||
1283 | /// \param context The context in which the subject type was written. | ||||||
1284 | /// | ||||||
1285 | /// \returns the subject type after replacing all of the Objective-C type | ||||||
1286 | /// parameters with their corresponding arguments. | ||||||
1287 | QualType substObjCMemberType(QualType objectType, | ||||||
1288 | const DeclContext *dc, | ||||||
1289 | ObjCSubstitutionContext context) const; | ||||||
1290 | |||||||
1291 | /// Strip Objective-C "__kindof" types from the given type. | ||||||
1292 | QualType stripObjCKindOfType(const ASTContext &ctx) const; | ||||||
1293 | |||||||
1294 | /// Remove all qualifiers including _Atomic. | ||||||
1295 | QualType getAtomicUnqualifiedType() const; | ||||||
1296 | |||||||
1297 | private: | ||||||
1298 | // These methods are implemented in a separate translation unit; | ||||||
1299 | // "static"-ize them to avoid creating temporary QualTypes in the | ||||||
1300 | // caller. | ||||||
1301 | static bool isConstant(QualType T, const ASTContext& Ctx); | ||||||
1302 | static QualType getDesugaredType(QualType T, const ASTContext &Context); | ||||||
1303 | static SplitQualType getSplitDesugaredType(QualType T); | ||||||
1304 | static SplitQualType getSplitUnqualifiedTypeImpl(QualType type); | ||||||
1305 | static QualType getSingleStepDesugaredTypeImpl(QualType type, | ||||||
1306 | const ASTContext &C); | ||||||
1307 | static QualType IgnoreParens(QualType T); | ||||||
1308 | static DestructionKind isDestructedTypeImpl(QualType type); | ||||||
1309 | |||||||
1310 | /// Check if \param RD is or contains a non-trivial C union. | ||||||
1311 | static bool hasNonTrivialToPrimitiveDefaultInitializeCUnion(const RecordDecl *RD); | ||||||
1312 | static bool hasNonTrivialToPrimitiveDestructCUnion(const RecordDecl *RD); | ||||||
1313 | static bool hasNonTrivialToPrimitiveCopyCUnion(const RecordDecl *RD); | ||||||
1314 | }; | ||||||
1315 | |||||||
1316 | } // namespace clang | ||||||
1317 | |||||||
1318 | namespace llvm { | ||||||
1319 | |||||||
1320 | /// Implement simplify_type for QualType, so that we can dyn_cast from QualType | ||||||
1321 | /// to a specific Type class. | ||||||
1322 | template<> struct simplify_type< ::clang::QualType> { | ||||||
1323 | using SimpleType = const ::clang::Type *; | ||||||
1324 | |||||||
1325 | static SimpleType getSimplifiedValue(::clang::QualType Val) { | ||||||
1326 | return Val.getTypePtr(); | ||||||
1327 | } | ||||||
1328 | }; | ||||||
1329 | |||||||
1330 | // Teach SmallPtrSet that QualType is "basically a pointer". | ||||||
1331 | template<> | ||||||
1332 | struct PointerLikeTypeTraits<clang::QualType> { | ||||||
1333 | static inline void *getAsVoidPointer(clang::QualType P) { | ||||||
1334 | return P.getAsOpaquePtr(); | ||||||
1335 | } | ||||||
1336 | |||||||
1337 | static inline clang::QualType getFromVoidPointer(void *P) { | ||||||
1338 | return clang::QualType::getFromOpaquePtr(P); | ||||||
1339 | } | ||||||
1340 | |||||||
1341 | // Various qualifiers go in low bits. | ||||||
1342 | static constexpr int NumLowBitsAvailable = 0; | ||||||
1343 | }; | ||||||
1344 | |||||||
1345 | } // namespace llvm | ||||||
1346 | |||||||
1347 | namespace clang { | ||||||
1348 | |||||||
1349 | /// Base class that is common to both the \c ExtQuals and \c Type | ||||||
1350 | /// classes, which allows \c QualType to access the common fields between the | ||||||
1351 | /// two. | ||||||
1352 | class ExtQualsTypeCommonBase { | ||||||
1353 | friend class ExtQuals; | ||||||
1354 | friend class QualType; | ||||||
1355 | friend class Type; | ||||||
1356 | |||||||
1357 | /// The "base" type of an extended qualifiers type (\c ExtQuals) or | ||||||
1358 | /// a self-referential pointer (for \c Type). | ||||||
1359 | /// | ||||||
1360 | /// This pointer allows an efficient mapping from a QualType to its | ||||||
1361 | /// underlying type pointer. | ||||||
1362 | const Type *const BaseType; | ||||||
1363 | |||||||
1364 | /// The canonical type of this type. A QualType. | ||||||
1365 | QualType CanonicalType; | ||||||
1366 | |||||||
1367 | ExtQualsTypeCommonBase(const Type *baseType, QualType canon) | ||||||
1368 | : BaseType(baseType), CanonicalType(canon) {} | ||||||
1369 | }; | ||||||
1370 | |||||||
1371 | /// We can encode up to four bits in the low bits of a | ||||||
1372 | /// type pointer, but there are many more type qualifiers that we want | ||||||
1373 | /// to be able to apply to an arbitrary type. Therefore we have this | ||||||
1374 | /// struct, intended to be heap-allocated and used by QualType to | ||||||
1375 | /// store qualifiers. | ||||||
1376 | /// | ||||||
1377 | /// The current design tags the 'const', 'restrict', and 'volatile' qualifiers | ||||||
1378 | /// in three low bits on the QualType pointer; a fourth bit records whether | ||||||
1379 | /// the pointer is an ExtQuals node. The extended qualifiers (address spaces, | ||||||
1380 | /// Objective-C GC attributes) are much more rare. | ||||||
1381 | class ExtQuals : public ExtQualsTypeCommonBase, public llvm::FoldingSetNode { | ||||||
1382 | // NOTE: changing the fast qualifiers should be straightforward as | ||||||
1383 | // long as you don't make 'const' non-fast. | ||||||
1384 | // 1. Qualifiers: | ||||||
1385 | // a) Modify the bitmasks (Qualifiers::TQ and DeclSpec::TQ). | ||||||
1386 | // Fast qualifiers must occupy the low-order bits. | ||||||
1387 | // b) Update Qualifiers::FastWidth and FastMask. | ||||||
1388 | // 2. QualType: | ||||||
1389 | // a) Update is{Volatile,Restrict}Qualified(), defined inline. | ||||||
1390 | // b) Update remove{Volatile,Restrict}, defined near the end of | ||||||
1391 | // this header. | ||||||
1392 | // 3. ASTContext: | ||||||
1393 | // a) Update get{Volatile,Restrict}Type. | ||||||
1394 | |||||||
1395 | /// The immutable set of qualifiers applied by this node. Always contains | ||||||
1396 | /// extended qualifiers. | ||||||
1397 | Qualifiers Quals; | ||||||
1398 | |||||||
1399 | ExtQuals *this_() { return this; } | ||||||
1400 | |||||||
1401 | public: | ||||||
1402 | ExtQuals(const Type *baseType, QualType canon, Qualifiers quals) | ||||||
1403 | : ExtQualsTypeCommonBase(baseType, | ||||||
1404 | canon.isNull() ? QualType(this_(), 0) : canon), | ||||||
1405 | Quals(quals) { | ||||||
1406 | assert(Quals.hasNonFastQualifiers()(static_cast <bool> (Quals.hasNonFastQualifiers() && "ExtQuals created with no fast qualifiers") ? void (0) : __assert_fail ("Quals.hasNonFastQualifiers() && \"ExtQuals created with no fast qualifiers\"" , "clang/include/clang/AST/Type.h", 1407, __extension__ __PRETTY_FUNCTION__ )) | ||||||
1407 | && "ExtQuals created with no fast qualifiers")(static_cast <bool> (Quals.hasNonFastQualifiers() && "ExtQuals created with no fast qualifiers") ? void (0) : __assert_fail ("Quals.hasNonFastQualifiers() && \"ExtQuals created with no fast qualifiers\"" , "clang/include/clang/AST/Type.h", 1407, __extension__ __PRETTY_FUNCTION__ )); | ||||||
1408 | assert(!Quals.hasFastQualifiers()(static_cast <bool> (!Quals.hasFastQualifiers() && "ExtQuals created with fast qualifiers") ? void (0) : __assert_fail ("!Quals.hasFastQualifiers() && \"ExtQuals created with fast qualifiers\"" , "clang/include/clang/AST/Type.h", 1409, __extension__ __PRETTY_FUNCTION__ )) | ||||||
1409 | && "ExtQuals created with fast qualifiers")(static_cast <bool> (!Quals.hasFastQualifiers() && "ExtQuals created with fast qualifiers") ? void (0) : __assert_fail ("!Quals.hasFastQualifiers() && \"ExtQuals created with fast qualifiers\"" , "clang/include/clang/AST/Type.h", 1409, __extension__ __PRETTY_FUNCTION__ )); | ||||||
1410 | } | ||||||
1411 | |||||||
1412 | Qualifiers getQualifiers() const { return Quals; } | ||||||
1413 | |||||||
1414 | bool hasObjCGCAttr() const { return Quals.hasObjCGCAttr(); } | ||||||
1415 | Qualifiers::GC getObjCGCAttr() const { return Quals.getObjCGCAttr(); } | ||||||
1416 | |||||||
1417 | bool hasObjCLifetime() const { return Quals.hasObjCLifetime(); } | ||||||
1418 | Qualifiers::ObjCLifetime getObjCLifetime() const { | ||||||
1419 | return Quals.getObjCLifetime(); | ||||||
1420 | } | ||||||
1421 | |||||||
1422 | bool hasAddressSpace() const { return Quals.hasAddressSpace(); } | ||||||
1423 | LangAS getAddressSpace() const { return Quals.getAddressSpace(); } | ||||||
1424 | |||||||
1425 | const Type *getBaseType() const { return BaseType; } | ||||||
1426 | |||||||
1427 | public: | ||||||
1428 | void Profile(llvm::FoldingSetNodeID &ID) const { | ||||||
1429 | Profile(ID, getBaseType(), Quals); | ||||||
1430 | } | ||||||
1431 | |||||||
1432 | static void Profile(llvm::FoldingSetNodeID &ID, | ||||||
1433 | const Type *BaseType, | ||||||
1434 | Qualifiers Quals) { | ||||||
1435 | assert(!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!")(static_cast <bool> (!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!") ? void (0) : __assert_fail ("!Quals.hasFastQualifiers() && \"fast qualifiers in ExtQuals hash!\"" , "clang/include/clang/AST/Type.h", 1435, __extension__ __PRETTY_FUNCTION__ )); | ||||||
1436 | ID.AddPointer(BaseType); | ||||||
1437 | Quals.Profile(ID); | ||||||
1438 | } | ||||||
1439 | }; | ||||||
1440 | |||||||
1441 | /// The kind of C++11 ref-qualifier associated with a function type. | ||||||
1442 | /// This determines whether a member function's "this" object can be an | ||||||
1443 | /// lvalue, rvalue, or neither. | ||||||
1444 | enum RefQualifierKind { | ||||||
1445 | /// No ref-qualifier was provided. | ||||||
1446 | RQ_None = 0, | ||||||
1447 | |||||||
1448 | /// An lvalue ref-qualifier was provided (\c &). | ||||||
1449 | RQ_LValue, | ||||||
1450 | |||||||
1451 | /// An rvalue ref-qualifier was provided (\c &&). | ||||||
1452 | RQ_RValue | ||||||
1453 | }; | ||||||
1454 | |||||||
1455 | /// Which keyword(s) were used to create an AutoType. | ||||||
1456 | enum class AutoTypeKeyword { | ||||||
1457 | /// auto | ||||||
1458 | Auto, | ||||||
1459 | |||||||
1460 | /// decltype(auto) | ||||||
1461 | DecltypeAuto, | ||||||
1462 | |||||||
1463 | /// __auto_type (GNU extension) | ||||||
1464 | GNUAutoType | ||||||
1465 | }; | ||||||
1466 | |||||||
1467 | /// The base class of the type hierarchy. | ||||||
1468 | /// | ||||||
1469 | /// A central concept with types is that each type always has a canonical | ||||||
1470 | /// type. A canonical type is the type with any typedef names stripped out | ||||||
1471 | /// of it or the types it references. For example, consider: | ||||||
1472 | /// | ||||||
1473 | /// typedef int foo; | ||||||
1474 | /// typedef foo* bar; | ||||||
1475 | /// 'int *' 'foo *' 'bar' | ||||||
1476 | /// | ||||||
1477 | /// There will be a Type object created for 'int'. Since int is canonical, its | ||||||
1478 | /// CanonicalType pointer points to itself. There is also a Type for 'foo' (a | ||||||
1479 | /// TypedefType). Its CanonicalType pointer points to the 'int' Type. Next | ||||||
1480 | /// there is a PointerType that represents 'int*', which, like 'int', is | ||||||
1481 | /// canonical. Finally, there is a PointerType type for 'foo*' whose canonical | ||||||
1482 | /// type is 'int*', and there is a TypedefType for 'bar', whose canonical type | ||||||
1483 | /// is also 'int*'. | ||||||
1484 | /// | ||||||
1485 | /// Non-canonical types are useful for emitting diagnostics, without losing | ||||||
1486 | /// information about typedefs being used. Canonical types are useful for type | ||||||
1487 | /// comparisons (they allow by-pointer equality tests) and useful for reasoning | ||||||
1488 | /// about whether something has a particular form (e.g. is a function type), | ||||||
1489 | /// because they implicitly, recursively, strip all typedefs out of a type. | ||||||
1490 | /// | ||||||
1491 | /// Types, once created, are immutable. | ||||||
1492 | /// | ||||||
1493 | class alignas(8) Type : public ExtQualsTypeCommonBase { | ||||||
1494 | public: | ||||||
1495 | enum TypeClass { | ||||||
1496 | #define TYPE(Class, Base) Class, | ||||||
1497 | #define LAST_TYPE(Class) TypeLast = Class | ||||||
1498 | #define ABSTRACT_TYPE(Class, Base) | ||||||
1499 | #include "clang/AST/TypeNodes.inc" | ||||||
1500 | }; | ||||||
1501 | |||||||
1502 | private: | ||||||
1503 | /// Bitfields required by the Type class. | ||||||
1504 | class TypeBitfields { | ||||||
1505 | friend class Type; | ||||||
1506 | template <class T> friend class TypePropertyCache; | ||||||
1507 | |||||||
1508 | /// TypeClass bitfield - Enum that specifies what subclass this belongs to. | ||||||
1509 | unsigned TC : 8; | ||||||
1510 | |||||||
1511 | /// Store information on the type dependency. | ||||||
1512 | unsigned Dependence : llvm::BitWidth<TypeDependence>; | ||||||
1513 | |||||||
1514 | /// True if the cache (i.e. the bitfields here starting with | ||||||
1515 | /// 'Cache') is valid. | ||||||
1516 | mutable unsigned CacheValid : 1; | ||||||
1517 | |||||||
1518 | /// Linkage of this type. | ||||||
1519 | mutable unsigned CachedLinkage : 3; | ||||||
1520 | |||||||
1521 | /// Whether this type involves and local or unnamed types. | ||||||
1522 | mutable unsigned CachedLocalOrUnnamed : 1; | ||||||
1523 | |||||||
1524 | /// Whether this type comes from an AST file. | ||||||
1525 | mutable unsigned FromAST : 1; | ||||||
1526 | |||||||
1527 | bool isCacheValid() const { | ||||||
1528 | return CacheValid; | ||||||
1529 | } | ||||||
1530 | |||||||
1531 | Linkage getLinkage() const { | ||||||
1532 | assert(isCacheValid() && "getting linkage from invalid cache")(static_cast <bool> (isCacheValid() && "getting linkage from invalid cache" ) ? void (0) : __assert_fail ("isCacheValid() && \"getting linkage from invalid cache\"" , "clang/include/clang/AST/Type.h", 1532, __extension__ __PRETTY_FUNCTION__ )); | ||||||
1533 | return static_cast<Linkage>(CachedLinkage); | ||||||
1534 | } | ||||||
1535 | |||||||
1536 | bool hasLocalOrUnnamedType() const { | ||||||
1537 | assert(isCacheValid() && "getting linkage from invalid cache")(static_cast <bool> (isCacheValid() && "getting linkage from invalid cache" ) ? void (0) : __assert_fail ("isCacheValid() && \"getting linkage from invalid cache\"" , "clang/include/clang/AST/Type.h", 1537, __extension__ __PRETTY_FUNCTION__ )); | ||||||
1538 | return CachedLocalOrUnnamed; | ||||||
1539 | } | ||||||
1540 | }; | ||||||
1541 | enum { NumTypeBits = 8 + llvm::BitWidth<TypeDependence> + 6 }; | ||||||
1542 | |||||||
1543 | protected: | ||||||
1544 | // These classes allow subclasses to somewhat cleanly pack bitfields | ||||||
1545 | // into Type. | ||||||
1546 | |||||||
1547 | class ArrayTypeBitfields { | ||||||
1548 | friend class ArrayType; | ||||||
1549 | |||||||
1550 | unsigned : NumTypeBits; | ||||||
1551 | |||||||
1552 | /// CVR qualifiers from declarations like | ||||||
1553 | /// 'int X[static restrict 4]'. For function parameters only. | ||||||
1554 | unsigned IndexTypeQuals : 3; | ||||||
1555 | |||||||
1556 | /// Storage class qualifiers from declarations like | ||||||
1557 | /// 'int X[static restrict 4]'. For function parameters only. | ||||||
1558 | /// Actually an ArrayType::ArraySizeModifier. | ||||||
1559 | unsigned SizeModifier : 3; | ||||||
1560 | }; | ||||||
1561 | |||||||
1562 | class ConstantArrayTypeBitfields { | ||||||
1563 | friend class ConstantArrayType; | ||||||
1564 | |||||||
1565 | unsigned : NumTypeBits + 3 + 3; | ||||||
1566 | |||||||
1567 | /// Whether we have a stored size expression. | ||||||
1568 | unsigned HasStoredSizeExpr : 1; | ||||||
1569 | }; | ||||||
1570 | |||||||
1571 | class BuiltinTypeBitfields { | ||||||
1572 | friend class BuiltinType; | ||||||
1573 | |||||||
1574 | unsigned : NumTypeBits; | ||||||
1575 | |||||||
1576 | /// The kind (BuiltinType::Kind) of builtin type this is. | ||||||
1577 | unsigned Kind : 8; | ||||||
1578 | }; | ||||||
1579 | |||||||
1580 | /// FunctionTypeBitfields store various bits belonging to FunctionProtoType. | ||||||
1581 | /// Only common bits are stored here. Additional uncommon bits are stored | ||||||
1582 | /// in a trailing object after FunctionProtoType. | ||||||
1583 | class FunctionTypeBitfields { | ||||||
1584 | friend class FunctionProtoType; | ||||||
1585 | friend class FunctionType; | ||||||
1586 | |||||||
1587 | unsigned : NumTypeBits; | ||||||
1588 | |||||||
1589 | /// Extra information which affects how the function is called, like | ||||||
1590 | /// regparm and the calling convention. | ||||||
1591 | unsigned ExtInfo : 13; | ||||||
1592 | |||||||
1593 | /// The ref-qualifier associated with a \c FunctionProtoType. | ||||||
1594 | /// | ||||||
1595 | /// This is a value of type \c RefQualifierKind. | ||||||
1596 | unsigned RefQualifier : 2; | ||||||
1597 | |||||||
1598 | /// Used only by FunctionProtoType, put here to pack with the | ||||||
1599 | /// other bitfields. | ||||||
1600 | /// The qualifiers are part of FunctionProtoType because... | ||||||
1601 | /// | ||||||
1602 | /// C++ 8.3.5p4: The return type, the parameter type list and the | ||||||
1603 | /// cv-qualifier-seq, [...], are part of the function type. | ||||||
1604 | unsigned FastTypeQuals : Qualifiers::FastWidth; | ||||||
1605 | /// Whether this function has extended Qualifiers. | ||||||
1606 | unsigned HasExtQuals : 1; | ||||||
1607 | |||||||
1608 | /// The number of parameters this function has, not counting '...'. | ||||||
1609 | /// According to [implimits] 8 bits should be enough here but this is | ||||||
1610 | /// somewhat easy to exceed with metaprogramming and so we would like to | ||||||
1611 | /// keep NumParams as wide as reasonably possible. | ||||||
1612 | unsigned NumParams : 16; | ||||||
1613 | |||||||
1614 | /// The type of exception specification this function has. | ||||||
1615 | unsigned ExceptionSpecType : 4; | ||||||
1616 | |||||||
1617 | /// Whether this function has extended parameter information. | ||||||
1618 | unsigned HasExtParameterInfos : 1; | ||||||
1619 | |||||||
1620 | /// Whether the function is variadic. | ||||||
1621 | unsigned Variadic : 1; | ||||||
1622 | |||||||
1623 | /// Whether this function has a trailing return type. | ||||||
1624 | unsigned HasTrailingReturn : 1; | ||||||
1625 | }; | ||||||
1626 | |||||||
1627 | class ObjCObjectTypeBitfields { | ||||||
1628 | friend class ObjCObjectType; | ||||||
1629 | |||||||
1630 | unsigned : NumTypeBits; | ||||||
1631 | |||||||
1632 | /// The number of type arguments stored directly on this object type. | ||||||
1633 | unsigned NumTypeArgs : 7; | ||||||
1634 | |||||||
1635 | /// The number of protocols stored directly on this object type. | ||||||
1636 | unsigned NumProtocols : 6; | ||||||
1637 | |||||||
1638 | /// Whether this is a "kindof" type. | ||||||
1639 | unsigned IsKindOf : 1; | ||||||
1640 | }; | ||||||
1641 | |||||||
1642 | class ReferenceTypeBitfields { | ||||||
1643 | friend class ReferenceType; | ||||||
1644 | |||||||
1645 | unsigned : NumTypeBits; | ||||||
1646 | |||||||
1647 | /// True if the type was originally spelled with an lvalue sigil. | ||||||
1648 | /// This is never true of rvalue references but can also be false | ||||||
1649 | /// on lvalue references because of C++0x [dcl.typedef]p9, | ||||||
1650 | /// as follows: | ||||||
1651 | /// | ||||||
1652 | /// typedef int &ref; // lvalue, spelled lvalue | ||||||
1653 | /// typedef int &&rvref; // rvalue | ||||||
1654 | /// ref &a; // lvalue, inner ref, spelled lvalue | ||||||
1655 | /// ref &&a; // lvalue, inner ref | ||||||
1656 | /// rvref &a; // lvalue, inner ref, spelled lvalue | ||||||
1657 | /// rvref &&a; // rvalue, inner ref | ||||||
1658 | unsigned SpelledAsLValue : 1; | ||||||
1659 | |||||||
1660 | /// True if the inner type is a reference type. This only happens | ||||||
1661 | /// in non-canonical forms. | ||||||
1662 | unsigned InnerRef : 1; | ||||||
1663 | }; | ||||||
1664 | |||||||
1665 | class TypeWithKeywordBitfields { | ||||||
1666 | friend class TypeWithKeyword; | ||||||
1667 | |||||||
1668 | unsigned : NumTypeBits; | ||||||
1669 | |||||||
1670 | /// An ElaboratedTypeKeyword. 8 bits for efficient access. | ||||||
1671 | unsigned Keyword : 8; | ||||||
1672 | }; | ||||||
1673 | |||||||
1674 | enum { NumTypeWithKeywordBits = 8 }; | ||||||
1675 | |||||||
1676 | class ElaboratedTypeBitfields { | ||||||
1677 | friend class ElaboratedType; | ||||||
1678 | |||||||
1679 | unsigned : NumTypeBits; | ||||||
1680 | unsigned : NumTypeWithKeywordBits; | ||||||
1681 | |||||||
1682 | /// Whether the ElaboratedType has a trailing OwnedTagDecl. | ||||||
1683 | unsigned HasOwnedTagDecl : 1; | ||||||
1684 | }; | ||||||
1685 | |||||||
1686 | class VectorTypeBitfields { | ||||||
1687 | friend class VectorType; | ||||||
1688 | friend class DependentVectorType; | ||||||
1689 | |||||||
1690 | unsigned : NumTypeBits; | ||||||
1691 | |||||||
1692 | /// The kind of vector, either a generic vector type or some | ||||||
1693 | /// target-specific vector type such as for AltiVec or Neon. | ||||||
1694 | unsigned VecKind : 3; | ||||||
1695 | /// The number of elements in the vector. | ||||||
1696 | uint32_t NumElements; | ||||||
1697 | }; | ||||||
1698 | |||||||
1699 | class AttributedTypeBitfields { | ||||||
1700 | friend class AttributedType; | ||||||
1701 | |||||||
1702 | unsigned : NumTypeBits; | ||||||
1703 | |||||||
1704 | /// An AttributedType::Kind | ||||||
1705 | unsigned AttrKind : 32 - NumTypeBits; | ||||||
1706 | }; | ||||||
1707 | |||||||
1708 | class AutoTypeBitfields { | ||||||
1709 | friend class AutoType; | ||||||
1710 | |||||||
1711 | unsigned : NumTypeBits; | ||||||
1712 | |||||||
1713 | /// Was this placeholder type spelled as 'auto', 'decltype(auto)', | ||||||
1714 | /// or '__auto_type'? AutoTypeKeyword value. | ||||||
1715 | unsigned Keyword : 2; | ||||||
1716 | |||||||
1717 | /// The number of template arguments in the type-constraints, which is | ||||||
1718 | /// expected to be able to hold at least 1024 according to [implimits]. | ||||||
1719 | /// However as this limit is somewhat easy to hit with template | ||||||
1720 | /// metaprogramming we'd prefer to keep it as large as possible. | ||||||
1721 | /// At the moment it has been left as a non-bitfield since this type | ||||||
1722 | /// safely fits in 64 bits as an unsigned, so there is no reason to | ||||||
1723 | /// introduce the performance impact of a bitfield. | ||||||
1724 | unsigned NumArgs; | ||||||
1725 | }; | ||||||
1726 | |||||||
1727 | class SubstTemplateTypeParmPackTypeBitfields { | ||||||
1728 | friend class SubstTemplateTypeParmPackType; | ||||||
1729 | |||||||
1730 | unsigned : NumTypeBits; | ||||||
1731 | |||||||
1732 | /// The number of template arguments in \c Arguments, which is | ||||||
1733 | /// expected to be able to hold at least 1024 according to [implimits]. | ||||||
1734 | /// However as this limit is somewhat easy to hit with template | ||||||
1735 | /// metaprogramming we'd prefer to keep it as large as possible. | ||||||
1736 | /// At the moment it has been left as a non-bitfield since this type | ||||||
1737 | /// safely fits in 64 bits as an unsigned, so there is no reason to | ||||||
1738 | /// introduce the performance impact of a bitfield. | ||||||
1739 | unsigned NumArgs; | ||||||
1740 | }; | ||||||
1741 | |||||||
1742 | class TemplateSpecializationTypeBitfields { | ||||||
1743 | friend class TemplateSpecializationType; | ||||||
1744 | |||||||
1745 | unsigned : NumTypeBits; | ||||||
1746 | |||||||
1747 | /// Whether this template specialization type is a substituted type alias. | ||||||
1748 | unsigned TypeAlias : 1; | ||||||
1749 | |||||||
1750 | /// The number of template arguments named in this class template | ||||||
1751 | /// specialization, which is expected to be able to hold at least 1024 | ||||||
1752 | /// according to [implimits]. However, as this limit is somewhat easy to | ||||||
1753 | /// hit with template metaprogramming we'd prefer to keep it as large | ||||||
1754 | /// as possible. At the moment it has been left as a non-bitfield since | ||||||
1755 | /// this type safely fits in 64 bits as an unsigned, so there is no reason | ||||||
1756 | /// to introduce the performance impact of a bitfield. | ||||||
1757 | unsigned NumArgs; | ||||||
1758 | }; | ||||||
1759 | |||||||
1760 | class DependentTemplateSpecializationTypeBitfields { | ||||||
1761 | friend class DependentTemplateSpecializationType; | ||||||
1762 | |||||||
1763 | unsigned : NumTypeBits; | ||||||
1764 | unsigned : NumTypeWithKeywordBits; | ||||||
1765 | |||||||
1766 | /// The number of template arguments named in this class template | ||||||
1767 | /// specialization, which is expected to be able to hold at least 1024 | ||||||
1768 | /// according to [implimits]. However, as this limit is somewhat easy to | ||||||
1769 | /// hit with template metaprogramming we'd prefer to keep it as large | ||||||
1770 | /// as possible. At the moment it has been left as a non-bitfield since | ||||||
1771 | /// this type safely fits in 64 bits as an unsigned, so there is no reason | ||||||
1772 | /// to introduce the performance impact of a bitfield. | ||||||
1773 | unsigned NumArgs; | ||||||
1774 | }; | ||||||
1775 | |||||||
1776 | class PackExpansionTypeBitfields { | ||||||
1777 | friend class PackExpansionType; | ||||||
1778 | |||||||
1779 | unsigned : NumTypeBits; | ||||||
1780 | |||||||
1781 | /// The number of expansions that this pack expansion will | ||||||
1782 | /// generate when substituted (+1), which is expected to be able to | ||||||
1783 | /// hold at least 1024 according to [implimits]. However, as this limit | ||||||
1784 | /// is somewhat easy to hit with template metaprogramming we'd prefer to | ||||||
1785 | /// keep it as large as possible. At the moment it has been left as a | ||||||
1786 | /// non-bitfield since this type safely fits in 64 bits as an unsigned, so | ||||||
1787 | /// there is no reason to introduce the performance impact of a bitfield. | ||||||
1788 | /// | ||||||
1789 | /// This field will only have a non-zero value when some of the parameter | ||||||
1790 | /// packs that occur within the pattern have been substituted but others | ||||||
1791 | /// have not. | ||||||
1792 | unsigned NumExpansions; | ||||||
1793 | }; | ||||||
1794 | |||||||
1795 | union { | ||||||
1796 | TypeBitfields TypeBits; | ||||||
1797 | ArrayTypeBitfields ArrayTypeBits; | ||||||
1798 | ConstantArrayTypeBitfields ConstantArrayTypeBits; | ||||||
1799 | AttributedTypeBitfields AttributedTypeBits; | ||||||
1800 | AutoTypeBitfields AutoTypeBits; | ||||||
1801 | BuiltinTypeBitfields BuiltinTypeBits; | ||||||
1802 | FunctionTypeBitfields FunctionTypeBits; | ||||||
1803 | ObjCObjectTypeBitfields ObjCObjectTypeBits; | ||||||
1804 | ReferenceTypeBitfields ReferenceTypeBits; | ||||||
1805 | TypeWithKeywordBitfields TypeWithKeywordBits; | ||||||
1806 | ElaboratedTypeBitfields ElaboratedTypeBits; | ||||||
1807 | VectorTypeBitfields VectorTypeBits; | ||||||
1808 | SubstTemplateTypeParmPackTypeBitfields SubstTemplateTypeParmPackTypeBits; | ||||||
1809 | TemplateSpecializationTypeBitfields TemplateSpecializationTypeBits; | ||||||
1810 | DependentTemplateSpecializationTypeBitfields | ||||||
1811 | DependentTemplateSpecializationTypeBits; | ||||||
1812 | PackExpansionTypeBitfields PackExpansionTypeBits; | ||||||
1813 | }; | ||||||
1814 | |||||||
1815 | private: | ||||||
1816 | template <class T> friend class TypePropertyCache; | ||||||
1817 | |||||||
1818 | /// Set whether this type comes from an AST file. | ||||||
1819 | void setFromAST(bool V = true) const { | ||||||
1820 | TypeBits.FromAST = V; | ||||||
1821 | } | ||||||
1822 | |||||||
1823 | protected: | ||||||
1824 | friend class ASTContext; | ||||||
1825 | |||||||
1826 | Type(TypeClass tc, QualType canon, TypeDependence Dependence) | ||||||
1827 | : ExtQualsTypeCommonBase(this, | ||||||
1828 | canon.isNull() ? QualType(this_(), 0) : canon) { | ||||||
1829 | static_assert(sizeof(*this) <= 8 + sizeof(ExtQualsTypeCommonBase), | ||||||
1830 | "changing bitfields changed sizeof(Type)!"); | ||||||
1831 | static_assert(alignof(decltype(*this)) % sizeof(void *) == 0, | ||||||
1832 | "Insufficient alignment!"); | ||||||
1833 | TypeBits.TC = tc; | ||||||
1834 | TypeBits.Dependence = static_cast<unsigned>(Dependence); | ||||||
1835 | TypeBits.CacheValid = false; | ||||||
1836 | TypeBits.CachedLocalOrUnnamed = false; | ||||||
1837 | TypeBits.CachedLinkage = NoLinkage; | ||||||
1838 | TypeBits.FromAST = false; | ||||||
1839 | } | ||||||
1840 | |||||||
1841 | // silence VC++ warning C4355: 'this' : used in base member initializer list | ||||||
1842 | Type *this_() { return this; } | ||||||
1843 | |||||||
1844 | void setDependence(TypeDependence D) { | ||||||
1845 | TypeBits.Dependence = static_cast<unsigned>(D); | ||||||
1846 | } | ||||||
1847 | |||||||
1848 | void addDependence(TypeDependence D) { setDependence(getDependence() | D); } | ||||||
1849 | |||||||
1850 | public: | ||||||
1851 | friend class ASTReader; | ||||||
1852 | friend class ASTWriter; | ||||||
1853 | template <class T> friend class serialization::AbstractTypeReader; | ||||||
1854 | template <class T> friend class serialization::AbstractTypeWriter; | ||||||
1855 | |||||||
1856 | Type(const Type &) = delete; | ||||||
1857 | Type(Type &&) = delete; | ||||||
1858 | Type &operator=(const Type &) = delete; | ||||||
1859 | Type &operator=(Type &&) = delete; | ||||||
1860 | |||||||
1861 | TypeClass getTypeClass() const { return static_cast<TypeClass>(TypeBits.TC); } | ||||||
1862 | |||||||
1863 | /// Whether this type comes from an AST file. | ||||||
1864 | bool isFromAST() const { return TypeBits.FromAST; } | ||||||
1865 | |||||||
1866 | /// Whether this type is or contains an unexpanded parameter | ||||||
1867 | /// pack, used to support C++0x variadic templates. | ||||||
1868 | /// | ||||||
1869 | /// A type that contains a parameter pack shall be expanded by the | ||||||
1870 | /// ellipsis operator at some point. For example, the typedef in the | ||||||
1871 | /// following example contains an unexpanded parameter pack 'T': | ||||||
1872 | /// | ||||||
1873 | /// \code | ||||||
1874 | /// template<typename ...T> | ||||||
1875 | /// struct X { | ||||||
1876 | /// typedef T* pointer_types; // ill-formed; T is a parameter pack. | ||||||
1877 | /// }; | ||||||
1878 | /// \endcode | ||||||
1879 | /// | ||||||
1880 | /// Note that this routine does not specify which | ||||||
1881 | bool containsUnexpandedParameterPack() const { | ||||||
1882 | return getDependence() & TypeDependence::UnexpandedPack; | ||||||
1883 | } | ||||||
1884 | |||||||
1885 | /// Determines if this type would be canonical if it had no further | ||||||
1886 | /// qualification. | ||||||
1887 | bool isCanonicalUnqualified() const { | ||||||
1888 | return CanonicalType == QualType(this, 0); | ||||||
1889 | } | ||||||
1890 | |||||||
1891 | /// Pull a single level of sugar off of this locally-unqualified type. | ||||||
1892 | /// Users should generally prefer SplitQualType::getSingleStepDesugaredType() | ||||||
1893 | /// or QualType::getSingleStepDesugaredType(const ASTContext&). | ||||||
1894 | QualType getLocallyUnqualifiedSingleStepDesugaredType() const; | ||||||
1895 | |||||||
1896 | /// As an extension, we classify types as one of "sized" or "sizeless"; | ||||||
1897 | /// every type is one or the other. Standard types are all sized; | ||||||
1898 | /// sizeless types are purely an extension. | ||||||
1899 | /// | ||||||
1900 | /// Sizeless types contain data with no specified size, alignment, | ||||||
1901 | /// or layout. | ||||||
1902 | bool isSizelessType() const; | ||||||
1903 | bool isSizelessBuiltinType() const; | ||||||
1904 | |||||||
1905 | /// Determines if this is a sizeless type supported by the | ||||||
1906 | /// 'arm_sve_vector_bits' type attribute, which can be applied to a single | ||||||
1907 | /// SVE vector or predicate, excluding tuple types such as svint32x4_t. | ||||||
1908 | bool isVLSTBuiltinType() const; | ||||||
1909 | |||||||
1910 | /// Returns the representative type for the element of an SVE builtin type. | ||||||
1911 | /// This is used to represent fixed-length SVE vectors created with the | ||||||
1912 | /// 'arm_sve_vector_bits' type attribute as VectorType. | ||||||
1913 | QualType getSveEltType(const ASTContext &Ctx) const; | ||||||
1914 | |||||||
1915 | /// Types are partitioned into 3 broad categories (C99 6.2.5p1): | ||||||
1916 | /// object types, function types, and incomplete types. | ||||||
1917 | |||||||
1918 | /// Return true if this is an incomplete type. | ||||||
1919 | /// A type that can describe objects, but which lacks information needed to | ||||||
1920 | /// determine its size (e.g. void, or a fwd declared struct). Clients of this | ||||||
1921 | /// routine will need to determine if the size is actually required. | ||||||
1922 | /// | ||||||
1923 | /// Def If non-null, and the type refers to some kind of declaration | ||||||
1924 | /// that can be completed (such as a C struct, C++ class, or Objective-C | ||||||
1925 | /// class), will be set to the declaration. | ||||||
1926 | bool isIncompleteType(NamedDecl **Def = nullptr) const; | ||||||
1927 | |||||||
1928 | /// Return true if this is an incomplete or object | ||||||
1929 | /// type, in other words, not a function type. | ||||||
1930 | bool isIncompleteOrObjectType() const { | ||||||
1931 | return !isFunctionType(); | ||||||
1932 | } | ||||||
1933 | |||||||
1934 | /// Determine whether this type is an object type. | ||||||
1935 | bool isObjectType() const { | ||||||
1936 | // C++ [basic.types]p8: | ||||||
1937 | // An object type is a (possibly cv-qualified) type that is not a | ||||||
1938 | // function type, not a reference type, and not a void type. | ||||||
1939 | return !isReferenceType() && !isFunctionType() && !isVoidType(); | ||||||
1940 | } | ||||||
1941 | |||||||
1942 | /// Return true if this is a literal type | ||||||
1943 | /// (C++11 [basic.types]p10) | ||||||
1944 | bool isLiteralType(const ASTContext &Ctx) const; | ||||||
1945 | |||||||
1946 | /// Determine if this type is a structural type, per C++20 [temp.param]p7. | ||||||
1947 | bool isStructuralType() const; | ||||||
1948 | |||||||
1949 | /// Test if this type is a standard-layout type. | ||||||
1950 | /// (C++0x [basic.type]p9) | ||||||
1951 | bool isStandardLayoutType() const; | ||||||
1952 | |||||||
1953 | /// Helper methods to distinguish type categories. All type predicates | ||||||
1954 | /// operate on the canonical type, ignoring typedefs and qualifiers. | ||||||
1955 | |||||||
1956 | /// Returns true if the type is a builtin type. | ||||||
1957 | bool isBuiltinType() const; | ||||||
1958 | |||||||
1959 | /// Test for a particular builtin type. | ||||||
1960 | bool isSpecificBuiltinType(unsigned K) const; | ||||||
1961 | |||||||
1962 | /// Test for a type which does not represent an actual type-system type but | ||||||
1963 | /// is instead used as a placeholder for various convenient purposes within | ||||||
1964 | /// Clang. All such types are BuiltinTypes. | ||||||
1965 | bool isPlaceholderType() const; | ||||||
1966 | const BuiltinType *getAsPlaceholderType() const; | ||||||
1967 | |||||||
1968 | /// Test for a specific placeholder type. | ||||||
1969 | bool isSpecificPlaceholderType(unsigned K) const; | ||||||
1970 | |||||||
1971 | /// Test for a placeholder type other than Overload; see | ||||||
1972 | /// BuiltinType::isNonOverloadPlaceholderType. | ||||||
1973 | bool isNonOverloadPlaceholderType() const; | ||||||
1974 | |||||||
1975 | /// isIntegerType() does *not* include complex integers (a GCC extension). | ||||||
1976 | /// isComplexIntegerType() can be used to test for complex integers. | ||||||
1977 | bool isIntegerType() const; // C99 6.2.5p17 (int, char, bool, enum) | ||||||
1978 | bool isEnumeralType() const; | ||||||
1979 | |||||||
1980 | /// Determine whether this type is a scoped enumeration type. | ||||||
1981 | bool isScopedEnumeralType() const; | ||||||
1982 | bool isBooleanType() const; | ||||||
1983 | bool isCharType() const; | ||||||
1984 | bool isWideCharType() const; | ||||||
1985 | bool isChar8Type() const; | ||||||
1986 | bool isChar16Type() const; | ||||||
1987 | bool isChar32Type() const; | ||||||
1988 | bool isAnyCharacterType() const; | ||||||
1989 | bool isIntegralType(const ASTContext &Ctx) const; | ||||||
1990 | |||||||
1991 | /// Determine whether this type is an integral or enumeration type. | ||||||
1992 | bool isIntegralOrEnumerationType() const; | ||||||
1993 | |||||||
1994 | /// Determine whether this type is an integral or unscoped enumeration type. | ||||||
1995 | bool isIntegralOrUnscopedEnumerationType() const; | ||||||
1996 | bool isUnscopedEnumerationType() const; | ||||||
1997 | |||||||
1998 | /// Floating point categories. | ||||||
1999 | bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double) | ||||||
2000 | /// isComplexType() does *not* include complex integers (a GCC extension). | ||||||
2001 | /// isComplexIntegerType() can be used to test for complex integers. | ||||||
2002 | bool isComplexType() const; // C99 6.2.5p11 (complex) | ||||||
2003 | bool isAnyComplexType() const; // C99 6.2.5p11 (complex) + Complex Int. | ||||||
2004 | bool isFloatingType() const; // C99 6.2.5p11 (real floating + complex) | ||||||
2005 | bool isHalfType() const; // OpenCL 6.1.1.1, NEON (IEEE 754-2008 half) | ||||||
2006 | bool isFloat16Type() const; // C11 extension ISO/IEC TS 18661 | ||||||
2007 | bool isBFloat16Type() const; | ||||||
2008 | bool isFloat128Type() const; | ||||||
2009 | bool isIbm128Type() const; | ||||||
2010 | bool isRealType() const; // C99 6.2.5p17 (real floating + integer) | ||||||
2011 | bool isArithmeticType() const; // C99 6.2.5p18 (integer + floating) | ||||||
2012 | bool isVoidType() const; // C99 6.2.5p19 | ||||||
2013 | bool isScalarType() const; // C99 6.2.5p21 (arithmetic + pointers) | ||||||
2014 | bool isAggregateType() const; | ||||||
2015 | bool isFundamentalType() const; | ||||||
2016 | bool isCompoundType() const; | ||||||
2017 | |||||||
2018 | // Type Predicates: Check to see if this type is structurally the specified | ||||||
2019 | // type, ignoring typedefs and qualifiers. | ||||||
2020 | bool isFunctionType() const; | ||||||
2021 | bool isFunctionNoProtoType() const { return getAs<FunctionNoProtoType>(); } | ||||||
2022 | bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); } | ||||||
2023 | bool isPointerType() const; | ||||||
2024 | bool isAnyPointerType() const; // Any C pointer or ObjC object pointer | ||||||
2025 | bool isBlockPointerType() const; | ||||||
2026 | bool isVoidPointerType() const; | ||||||
2027 | bool isReferenceType() const; | ||||||
2028 | bool isLValueReferenceType() const; | ||||||
2029 | bool isRValueReferenceType() const; | ||||||
2030 | bool isObjectPointerType() const; | ||||||
2031 | bool isFunctionPointerType() const; | ||||||
2032 | bool isFunctionReferenceType() const; | ||||||
2033 | bool isMemberPointerType() const; | ||||||
2034 | bool isMemberFunctionPointerType() const; | ||||||
2035 | bool isMemberDataPointerType() const; | ||||||
2036 | bool isArrayType() const; | ||||||
2037 | bool isConstantArrayType() const; | ||||||
2038 | bool isIncompleteArrayType() const; | ||||||
2039 | bool isVariableArrayType() const; | ||||||
2040 | bool isDependentSizedArrayType() const; | ||||||
2041 | bool isRecordType() const; | ||||||
2042 | bool isClassType() const; | ||||||
2043 | bool isStructureType() const; | ||||||
2044 | bool isObjCBoxableRecordType() const; | ||||||
2045 | bool isInterfaceType() const; | ||||||
2046 | bool isStructureOrClassType() const; | ||||||
2047 | bool isUnionType() const; | ||||||
2048 | bool isComplexIntegerType() const; // GCC _Complex integer type. | ||||||
2049 | bool isVectorType() const; // GCC vector type. | ||||||
2050 | bool isExtVectorType() const; // Extended vector type. | ||||||
2051 | bool isMatrixType() const; // Matrix type. | ||||||
2052 | bool isConstantMatrixType() const; // Constant matrix type. | ||||||
2053 | bool isDependentAddressSpaceType() const; // value-dependent address space qualifier | ||||||
2054 | bool isObjCObjectPointerType() const; // pointer to ObjC object | ||||||
2055 | bool isObjCRetainableType() const; // ObjC object or block pointer | ||||||
2056 | bool isObjCLifetimeType() const; // (array of)* retainable type | ||||||
2057 | bool isObjCIndirectLifetimeType() const; // (pointer to)* lifetime type | ||||||
2058 | bool isObjCNSObjectType() const; // __attribute__((NSObject)) | ||||||
2059 | bool isObjCIndependentClassType() const; // __attribute__((objc_independent_class)) | ||||||
2060 | // FIXME: change this to 'raw' interface type, so we can used 'interface' type | ||||||
2061 | // for the common case. | ||||||
2062 | bool isObjCObjectType() const; // NSString or typeof(*(id)0) | ||||||
2063 | bool isObjCQualifiedInterfaceType() const; // NSString<foo> | ||||||
2064 | bool isObjCQualifiedIdType() const; // id<foo> | ||||||
2065 | bool isObjCQualifiedClassType() const; // Class<foo> | ||||||
2066 | bool isObjCObjectOrInterfaceType() const; | ||||||
2067 | bool isObjCIdType() const; // id | ||||||
2068 | bool isDecltypeType() const; | ||||||
2069 | /// Was this type written with the special inert-in-ARC __unsafe_unretained | ||||||
2070 | /// qualifier? | ||||||
2071 | /// | ||||||
2072 | /// This approximates the answer to the following question: if this | ||||||
2073 | /// translation unit were compiled in ARC, would this type be qualified | ||||||
2074 | /// with __unsafe_unretained? | ||||||
2075 | bool isObjCInertUnsafeUnretainedType() const { | ||||||
2076 | return hasAttr(attr::ObjCInertUnsafeUnretained); | ||||||
2077 | } | ||||||
2078 | |||||||
2079 | /// Whether the type is Objective-C 'id' or a __kindof type of an | ||||||
2080 | /// object type, e.g., __kindof NSView * or __kindof id | ||||||
2081 | /// <NSCopying>. | ||||||
2082 | /// | ||||||
2083 | /// \param bound Will be set to the bound on non-id subtype types, | ||||||
2084 | /// which will be (possibly specialized) Objective-C class type, or | ||||||
2085 | /// null for 'id. | ||||||
2086 | bool isObjCIdOrObjectKindOfType(const ASTContext &ctx, | ||||||
2087 | const ObjCObjectType *&bound) const; | ||||||
2088 | |||||||
2089 | bool isObjCClassType() const; // Class | ||||||
2090 | |||||||
2091 | /// Whether the type is Objective-C 'Class' or a __kindof type of an | ||||||
2092 | /// Class type, e.g., __kindof Class <NSCopying>. | ||||||
2093 | /// | ||||||
2094 | /// Unlike \c isObjCIdOrObjectKindOfType, there is no relevant bound | ||||||
2095 | /// here because Objective-C's type system cannot express "a class | ||||||
2096 | /// object for a subclass of NSFoo". | ||||||
2097 | bool isObjCClassOrClassKindOfType() const; | ||||||
2098 | |||||||
2099 | bool isBlockCompatibleObjCPointerType(ASTContext &ctx) const; | ||||||
2100 | bool isObjCSelType() const; // Class | ||||||
2101 | bool isObjCBuiltinType() const; // 'id' or 'Class' | ||||||
2102 | bool isObjCARCBridgableType() const; | ||||||
2103 | bool isCARCBridgableType() const; | ||||||
2104 | bool isTemplateTypeParmType() const; // C++ template type parameter | ||||||
2105 | bool isNullPtrType() const; // C++11 std::nullptr_t | ||||||
2106 | bool isNothrowT() const; // C++ std::nothrow_t | ||||||
2107 | bool isAlignValT() const; // C++17 std::align_val_t | ||||||
2108 | bool isStdByteType() const; // C++17 std::byte | ||||||
2109 | bool isAtomicType() const; // C11 _Atomic() | ||||||
2110 | bool isUndeducedAutoType() const; // C++11 auto or | ||||||
2111 | // C++14 decltype(auto) | ||||||
2112 | bool isTypedefNameType() const; // typedef or alias template | ||||||
2113 | |||||||
2114 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ | ||||||
2115 | bool is##Id##Type() const; | ||||||
2116 | #include "clang/Basic/OpenCLImageTypes.def" | ||||||
2117 | |||||||
2118 | bool isImageType() const; // Any OpenCL image type | ||||||
2119 | |||||||
2120 | bool isSamplerT() const; // OpenCL sampler_t | ||||||
2121 | bool isEventT() const; // OpenCL event_t | ||||||
2122 | bool isClkEventT() const; // OpenCL clk_event_t | ||||||
2123 | bool isQueueT() const; // OpenCL queue_t | ||||||
2124 | bool isReserveIDT() const; // OpenCL reserve_id_t | ||||||
2125 | |||||||
2126 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ | ||||||
2127 | bool is##Id##Type() const; | ||||||
2128 | #include "clang/Basic/OpenCLExtensionTypes.def" | ||||||
2129 | // Type defined in cl_intel_device_side_avc_motion_estimation OpenCL extension | ||||||
2130 | bool isOCLIntelSubgroupAVCType() const; | ||||||
2131 | bool isOCLExtOpaqueType() const; // Any OpenCL extension type | ||||||
2132 | |||||||
2133 | bool isPipeType() const; // OpenCL pipe type | ||||||
2134 | bool isBitIntType() const; // Bit-precise integer type | ||||||
2135 | bool isOpenCLSpecificType() const; // Any OpenCL specific type | ||||||
2136 | |||||||
2137 | /// Determines if this type, which must satisfy | ||||||
2138 | /// isObjCLifetimeType(), is implicitly __unsafe_unretained rather | ||||||
2139 | /// than implicitly __strong. | ||||||
2140 | bool isObjCARCImplicitlyUnretainedType() const; | ||||||
2141 | |||||||
2142 | /// Check if the type is the CUDA device builtin surface type. | ||||||
2143 | bool isCUDADeviceBuiltinSurfaceType() const; | ||||||
2144 | /// Check if the type is the CUDA device builtin texture type. | ||||||
2145 | bool isCUDADeviceBuiltinTextureType() 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 an error type. | ||||||
2171 | bool containsErrors() const { | ||||||
2172 | return getDependence() & TypeDependence::Error; | ||||||
2173 | } | ||||||
2174 | |||||||
2175 | /// Whether this type is a dependent type, meaning that its definition | ||||||
2176 | /// somehow depends on a template parameter (C++ [temp.dep.type]). | ||||||
2177 | bool isDependentType() const { | ||||||
2178 | return getDependence() & TypeDependence::Dependent; | ||||||
2179 | } | ||||||
2180 | |||||||
2181 | /// Determine whether this type is an instantiation-dependent type, | ||||||
2182 | /// meaning that the type involves a template parameter (even if the | ||||||
2183 | /// definition does not actually depend on the type substituted for that | ||||||
2184 | /// template parameter). | ||||||
2185 | bool isInstantiationDependentType() const { | ||||||
2186 | return getDependence() & TypeDependence::Instantiation; | ||||||
2187 | } | ||||||
2188 | |||||||
2189 | /// Determine whether this type is an undeduced type, meaning that | ||||||
2190 | /// it somehow involves a C++11 'auto' type or similar which has not yet been | ||||||
2191 | /// deduced. | ||||||
2192 | bool isUndeducedType() const; | ||||||
2193 | |||||||
2194 | /// Whether this type is a variably-modified type (C99 6.7.5). | ||||||
2195 | bool isVariablyModifiedType() const { | ||||||
2196 | return getDependence() & TypeDependence::VariablyModified; | ||||||
2197 | } | ||||||
2198 | |||||||
2199 | /// Whether this type involves a variable-length array type | ||||||
2200 | /// with a definite size. | ||||||
2201 | bool hasSizedVLAType() const; | ||||||
2202 | |||||||
2203 | /// Whether this type is or contains a local or unnamed type. | ||||||
2204 | bool hasUnnamedOrLocalType() const; | ||||||
2205 | |||||||
2206 | bool isOverloadableType() const; | ||||||
2207 | |||||||
2208 | /// Determine wither this type is a C++ elaborated-type-specifier. | ||||||
2209 | bool isElaboratedTypeSpecifier() const; | ||||||
2210 | |||||||
2211 | bool canDecayToPointerType() const; | ||||||
2212 | |||||||
2213 | /// Whether this type is represented natively as a pointer. This includes | ||||||
2214 | /// pointers, references, block pointers, and Objective-C interface, | ||||||
2215 | /// qualified id, and qualified interface types, as well as nullptr_t. | ||||||
2216 | bool hasPointerRepresentation() const; | ||||||
2217 | |||||||
2218 | /// Whether this type can represent an objective pointer type for the | ||||||
2219 | /// purpose of GC'ability | ||||||
2220 | bool hasObjCPointerRepresentation() const; | ||||||
2221 | |||||||
2222 | /// Determine whether this type has an integer representation | ||||||
2223 | /// of some sort, e.g., it is an integer type or a vector. | ||||||
2224 | bool hasIntegerRepresentation() const; | ||||||
2225 | |||||||
2226 | /// Determine whether this type has an signed integer representation | ||||||
2227 | /// of some sort, e.g., it is an signed integer type or a vector. | ||||||
2228 | bool hasSignedIntegerRepresentation() const; | ||||||
2229 | |||||||
2230 | /// Determine whether this type has an unsigned integer representation | ||||||
2231 | /// of some sort, e.g., it is an unsigned integer type or a vector. | ||||||
2232 | bool hasUnsignedIntegerRepresentation() const; | ||||||
2233 | |||||||
2234 | /// Determine whether this type has a floating-point representation | ||||||
2235 | /// of some sort, e.g., it is a floating-point type or a vector thereof. | ||||||
2236 | bool hasFloatingRepresentation() const; | ||||||
2237 | |||||||
2238 | // Type Checking Functions: Check to see if this type is structurally the | ||||||
2239 | // specified type, ignoring typedefs and qualifiers, and return a pointer to | ||||||
2240 | // the best type we can. | ||||||
2241 | const RecordType *getAsStructureType() const; | ||||||
2242 | /// NOTE: getAs*ArrayType are methods on ASTContext. | ||||||
2243 | const RecordType *getAsUnionType() const; | ||||||
2244 | const ComplexType *getAsComplexIntegerType() const; // GCC complex int type. | ||||||
2245 | const ObjCObjectType *getAsObjCInterfaceType() const; | ||||||
2246 | |||||||
2247 | // The following is a convenience method that returns an ObjCObjectPointerType | ||||||
2248 | // for object declared using an interface. | ||||||
2249 | const ObjCObjectPointerType *getAsObjCInterfacePointerType() const; | ||||||
2250 | const ObjCObjectPointerType *getAsObjCQualifiedIdType() const; | ||||||
2251 | const ObjCObjectPointerType *getAsObjCQualifiedClassType() const; | ||||||
2252 | const ObjCObjectType *getAsObjCQualifiedInterfaceType() const; | ||||||
2253 | |||||||
2254 | /// Retrieves the CXXRecordDecl that this type refers to, either | ||||||
2255 | /// because the type is a RecordType or because it is the injected-class-name | ||||||
2256 | /// type of a class template or class template partial specialization. | ||||||
2257 | CXXRecordDecl *getAsCXXRecordDecl() const; | ||||||
2258 | |||||||
2259 | /// Retrieves the RecordDecl this type refers to. | ||||||
2260 | RecordDecl *getAsRecordDecl() const; | ||||||
2261 | |||||||
2262 | /// Retrieves the TagDecl that this type refers to, either | ||||||
2263 | /// because the type is a TagType or because it is the injected-class-name | ||||||
2264 | /// type of a class template or class template partial specialization. | ||||||
2265 | TagDecl *getAsTagDecl() const; | ||||||
2266 | |||||||
2267 | /// If this is a pointer or reference to a RecordType, return the | ||||||
2268 | /// CXXRecordDecl that the type refers to. | ||||||
2269 | /// | ||||||
2270 | /// If this is not a pointer or reference, or the type being pointed to does | ||||||
2271 | /// not refer to a CXXRecordDecl, returns NULL. | ||||||
2272 | const CXXRecordDecl *getPointeeCXXRecordDecl() const; | ||||||
2273 | |||||||
2274 | /// Get the DeducedType 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 | DeducedType *getContainedDeducedType() const; | ||||||
2278 | |||||||
2279 | /// Get the AutoType whose type will be deduced for a variable with | ||||||
2280 | /// an initializer of this type. This looks through declarators like pointer | ||||||
2281 | /// types, but not through decltype or typedefs. | ||||||
2282 | AutoType *getContainedAutoType() const { | ||||||
2283 | return dyn_cast_or_null<AutoType>(getContainedDeducedType()); | ||||||
2284 | } | ||||||
2285 | |||||||
2286 | /// Determine whether this type was written with a leading 'auto' | ||||||
2287 | /// corresponding to a trailing return type (possibly for a nested | ||||||
2288 | /// function type within a pointer to function type or similar). | ||||||
2289 | bool hasAutoForTrailingReturnType() const; | ||||||
2290 | |||||||
2291 | /// Member-template getAs<specific type>'. Look through sugar for | ||||||
2292 | /// an instance of \<specific type>. This scheme will eventually | ||||||
2293 | /// replace the specific getAsXXXX methods above. | ||||||
2294 | /// | ||||||
2295 | /// There are some specializations of this member template listed | ||||||
2296 | /// immediately following this class. | ||||||
2297 | template <typename T> const T *getAs() const; | ||||||
2298 | |||||||
2299 | /// Member-template getAsAdjusted<specific type>. Look through specific kinds | ||||||
2300 | /// of sugar (parens, attributes, etc) for an instance of \<specific type>. | ||||||
2301 | /// This is used when you need to walk over sugar nodes that represent some | ||||||
2302 | /// kind of type adjustment from a type that was written as a \<specific type> | ||||||
2303 | /// to another type that is still canonically a \<specific type>. | ||||||
2304 | template <typename T> const T *getAsAdjusted() const; | ||||||
2305 | |||||||
2306 | /// A variant of getAs<> for array types which silently discards | ||||||
2307 | /// qualifiers from the outermost type. | ||||||
2308 | const ArrayType *getAsArrayTypeUnsafe() const; | ||||||
2309 | |||||||
2310 | /// Member-template castAs<specific type>. Look through sugar for | ||||||
2311 | /// the underlying instance of \<specific type>. | ||||||
2312 | /// | ||||||
2313 | /// This method has the same relationship to getAs<T> as cast<T> has | ||||||
2314 | /// to dyn_cast<T>; which is to say, the underlying type *must* | ||||||
2315 | /// have the intended type, and this method will never return null. | ||||||
2316 | template <typename T> const T *castAs() const; | ||||||
2317 | |||||||
2318 | /// A variant of castAs<> for array type which silently discards | ||||||
2319 | /// qualifiers from the outermost type. | ||||||
2320 | const ArrayType *castAsArrayTypeUnsafe() const; | ||||||
2321 | |||||||
2322 | /// Determine whether this type had the specified attribute applied to it | ||||||
2323 | /// (looking through top-level type sugar). | ||||||
2324 | bool hasAttr(attr::Kind AK) const; | ||||||
2325 | |||||||
2326 | /// Get the base element type of this type, potentially discarding type | ||||||
2327 | /// qualifiers. This should never be used when type qualifiers | ||||||
2328 | /// are meaningful. | ||||||
2329 | const Type *getBaseElementTypeUnsafe() const; | ||||||
2330 | |||||||
2331 | /// If this is an array type, return the element type of the array, | ||||||
2332 | /// potentially with type qualifiers missing. | ||||||
2333 | /// This should never be used when type qualifiers are meaningful. | ||||||
2334 | const Type *getArrayElementTypeNoTypeQual() const; | ||||||
2335 | |||||||
2336 | /// If this is a pointer type, return the pointee type. | ||||||
2337 | /// If this is an array type, return the array element type. | ||||||
2338 | /// This should never be used when type qualifiers are meaningful. | ||||||
2339 | const Type *getPointeeOrArrayElementType() const; | ||||||
2340 | |||||||
2341 | /// If this is a pointer, ObjC object pointer, or block | ||||||
2342 | /// pointer, this returns the respective pointee. | ||||||
2343 | QualType getPointeeType() const; | ||||||
2344 | |||||||
2345 | /// Return the specified type with any "sugar" removed from the type, | ||||||
2346 | /// removing any typedefs, typeofs, etc., as well as any qualifiers. | ||||||
2347 | const Type *getUnqualifiedDesugaredType() const; | ||||||
2348 | |||||||
2349 | /// More type predicates useful for type checking/promotion | ||||||
2350 | bool isPromotableIntegerType() const; // C99 6.3.1.1p2 | ||||||
2351 | |||||||
2352 | /// Return true if this is an integer type that is | ||||||
2353 | /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], | ||||||
2354 | /// or an enum decl which has a signed representation. | ||||||
2355 | bool isSignedIntegerType() const; | ||||||
2356 | |||||||
2357 | /// Return true if this is an integer type that is | ||||||
2358 | /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], | ||||||
2359 | /// or an enum decl which has an unsigned representation. | ||||||
2360 | bool isUnsignedIntegerType() const; | ||||||
2361 | |||||||
2362 | /// Determines whether this is an integer type that is signed or an | ||||||
2363 | /// enumeration types whose underlying type is a signed integer type. | ||||||
2364 | bool isSignedIntegerOrEnumerationType() const; | ||||||
2365 | |||||||
2366 | /// Determines whether this is an integer type that is unsigned or an | ||||||
2367 | /// enumeration types whose underlying type is a unsigned integer type. | ||||||
2368 | bool isUnsignedIntegerOrEnumerationType() const; | ||||||
2369 | |||||||
2370 | /// Return true if this is a fixed point type according to | ||||||
2371 | /// ISO/IEC JTC1 SC22 WG14 N1169. | ||||||
2372 | bool isFixedPointType() const; | ||||||
2373 | |||||||
2374 | /// Return true if this is a fixed point or integer type. | ||||||
2375 | bool isFixedPointOrIntegerType() const; | ||||||
2376 | |||||||
2377 | /// Return true if this is a saturated fixed point type according to | ||||||
2378 | /// ISO/IEC JTC1 SC22 WG14 N1169. This type can be signed or unsigned. | ||||||
2379 | bool isSaturatedFixedPointType() const; | ||||||
2380 | |||||||
2381 | /// Return true if this is a saturated fixed point type according to | ||||||
2382 | /// ISO/IEC JTC1 SC22 WG14 N1169. This type can be signed or unsigned. | ||||||
2383 | bool isUnsaturatedFixedPointType() const; | ||||||
2384 | |||||||
2385 | /// Return true if this is a fixed point type that is signed according | ||||||
2386 | /// to ISO/IEC JTC1 SC22 WG14 N1169. This type can also be saturated. | ||||||
2387 | bool isSignedFixedPointType() const; | ||||||
2388 | |||||||
2389 | /// Return true if this is a fixed point type that is unsigned according | ||||||
2390 | /// to ISO/IEC JTC1 SC22 WG14 N1169. This type can also be saturated. | ||||||
2391 | bool isUnsignedFixedPointType() const; | ||||||
2392 | |||||||
2393 | /// Return true if this is not a variable sized type, | ||||||
2394 | /// according to the rules of C99 6.7.5p3. It is not legal to call this on | ||||||
2395 | /// incomplete types. | ||||||
2396 | bool isConstantSizeType() const; | ||||||
2397 | |||||||
2398 | /// Returns true if this type can be represented by some | ||||||
2399 | /// set of type specifiers. | ||||||
2400 | bool isSpecifierType() const; | ||||||
2401 | |||||||
2402 | /// Determine the linkage of this type. | ||||||
2403 | Linkage getLinkage() const; | ||||||
2404 | |||||||
2405 | /// Determine the visibility of this type. | ||||||
2406 | Visibility getVisibility() const { | ||||||
2407 | return getLinkageAndVisibility().getVisibility(); | ||||||
2408 | } | ||||||
2409 | |||||||
2410 | /// Return true if the visibility was explicitly set is the code. | ||||||
2411 | bool isVisibilityExplicit() const { | ||||||
2412 | return getLinkageAndVisibility().isVisibilityExplicit(); | ||||||
2413 | } | ||||||
2414 | |||||||
2415 | /// Determine the linkage and visibility of this type. | ||||||
2416 | LinkageInfo getLinkageAndVisibility() const; | ||||||
2417 | |||||||
2418 | /// True if the computed linkage is valid. Used for consistency | ||||||
2419 | /// checking. Should always return true. | ||||||
2420 | bool isLinkageValid() const; | ||||||
2421 | |||||||
2422 | /// Determine the nullability of the given type. | ||||||
2423 | /// | ||||||
2424 | /// Note that nullability is only captured as sugar within the type | ||||||
2425 | /// system, not as part of the canonical type, so nullability will | ||||||
2426 | /// be lost by canonicalization and desugaring. | ||||||
2427 | Optional<NullabilityKind> getNullability(const ASTContext &context) const; | ||||||
2428 | |||||||
2429 | /// Determine whether the given type can have a nullability | ||||||
2430 | /// specifier applied to it, i.e., if it is any kind of pointer type. | ||||||
2431 | /// | ||||||
2432 | /// \param ResultIfUnknown The value to return if we don't yet know whether | ||||||
2433 | /// this type can have nullability because it is dependent. | ||||||
2434 | bool canHaveNullability(bool ResultIfUnknown = true) const; | ||||||
2435 | |||||||
2436 | /// Retrieve the set of substitutions required when accessing a member | ||||||
2437 | /// of the Objective-C receiver type that is declared in the given context. | ||||||
2438 | /// | ||||||
2439 | /// \c *this is the type of the object we're operating on, e.g., the | ||||||
2440 | /// receiver for a message send or the base of a property access, and is | ||||||
2441 | /// expected to be of some object or object pointer type. | ||||||
2442 | /// | ||||||
2443 | /// \param dc The declaration context for which we are building up a | ||||||
2444 | /// substitution mapping, which should be an Objective-C class, extension, | ||||||
2445 | /// category, or method within. | ||||||
2446 | /// | ||||||
2447 | /// \returns an array of type arguments that can be substituted for | ||||||
2448 | /// the type parameters of the given declaration context in any type described | ||||||
2449 | /// within that context, or an empty optional to indicate that no | ||||||
2450 | /// substitution is required. | ||||||
2451 | Optional<ArrayRef<QualType>> | ||||||
2452 | getObjCSubstitutions(const DeclContext *dc) const; | ||||||
2453 | |||||||
2454 | /// Determines if this is an ObjC interface type that may accept type | ||||||
2455 | /// parameters. | ||||||
2456 | bool acceptsObjCTypeParams() const; | ||||||
2457 | |||||||
2458 | const char *getTypeClassName() const; | ||||||
2459 | |||||||
2460 | QualType getCanonicalTypeInternal() const { | ||||||
2461 | return CanonicalType; | ||||||
2462 | } | ||||||
2463 | |||||||
2464 | CanQualType getCanonicalTypeUnqualified() const; // in CanonicalType.h | ||||||
2465 | void dump() const; | ||||||
2466 | void dump(llvm::raw_ostream &OS, const ASTContext &Context) const; | ||||||
2467 | }; | ||||||
2468 | |||||||
2469 | /// This will check for a TypedefType by removing any existing sugar | ||||||
2470 | /// until it reaches a TypedefType or a non-sugared type. | ||||||
2471 | template <> const TypedefType *Type::getAs() const; | ||||||
2472 | |||||||
2473 | /// This will check for a TemplateSpecializationType by removing any | ||||||
2474 | /// existing sugar until it reaches a TemplateSpecializationType or a | ||||||
2475 | /// non-sugared type. | ||||||
2476 | template <> const TemplateSpecializationType *Type::getAs() const; | ||||||
2477 | |||||||
2478 | /// This will check for an AttributedType by removing any existing sugar | ||||||
2479 | /// until it reaches an AttributedType or a non-sugared type. | ||||||
2480 | template <> const AttributedType *Type::getAs() const; | ||||||
2481 | |||||||
2482 | // We can do canonical leaf types faster, because we don't have to | ||||||
2483 | // worry about preserving child type decoration. | ||||||
2484 | #define TYPE(Class, Base) | ||||||
2485 | #define LEAF_TYPE(Class) \ | ||||||
2486 | template <> inline const Class##Type *Type::getAs() const { \ | ||||||
2487 | return dyn_cast<Class##Type>(CanonicalType); \ | ||||||
2488 | } \ | ||||||
2489 | template <> inline const Class##Type *Type::castAs() const { \ | ||||||
2490 | return cast<Class##Type>(CanonicalType); \ | ||||||
2491 | } | ||||||
2492 | #include "clang/AST/TypeNodes.inc" | ||||||
2493 | |||||||
2494 | /// This class is used for builtin types like 'int'. Builtin | ||||||
2495 | /// types are always canonical and have a literal name field. | ||||||
2496 | class BuiltinType : public Type { | ||||||
2497 | public: | ||||||
2498 | enum Kind { | ||||||
2499 | // OpenCL image types | ||||||
2500 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) Id, | ||||||
2501 | #include "clang/Basic/OpenCLImageTypes.def" | ||||||
2502 | // OpenCL extension types | ||||||
2503 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) Id, | ||||||
2504 | #include "clang/Basic/OpenCLExtensionTypes.def" | ||||||
2505 | // SVE Types | ||||||
2506 | #define SVE_TYPE(Name, Id, SingletonId) Id, | ||||||
2507 | #include "clang/Basic/AArch64SVEACLETypes.def" | ||||||
2508 | // PPC MMA Types | ||||||
2509 | #define PPC_VECTOR_TYPE(Name, Id, Size) Id, | ||||||
2510 | #include "clang/Basic/PPCTypes.def" | ||||||
2511 | // RVV Types | ||||||
2512 | #define RVV_TYPE(Name, Id, SingletonId) Id, | ||||||
2513 | #include "clang/Basic/RISCVVTypes.def" | ||||||
2514 | // All other builtin types | ||||||
2515 | #define BUILTIN_TYPE(Id, SingletonId) Id, | ||||||
2516 | #define LAST_BUILTIN_TYPE(Id) LastKind = Id | ||||||
2517 | #include "clang/AST/BuiltinTypes.def" | ||||||
2518 | }; | ||||||
2519 | |||||||
2520 | private: | ||||||
2521 | friend class ASTContext; // ASTContext creates these. | ||||||
2522 | |||||||
2523 | BuiltinType(Kind K) | ||||||
2524 | : Type(Builtin, QualType(), | ||||||
2525 | K == Dependent ? TypeDependence::DependentInstantiation | ||||||
2526 | : TypeDependence::None) { | ||||||
2527 | BuiltinTypeBits.Kind = K; | ||||||
2528 | } | ||||||
2529 | |||||||
2530 | public: | ||||||
2531 | Kind getKind() const { return static_cast<Kind>(BuiltinTypeBits.Kind); } | ||||||
2532 | StringRef getName(const PrintingPolicy &Policy) const; | ||||||
2533 | |||||||
2534 | const char *getNameAsCString(const PrintingPolicy &Policy) const { | ||||||
2535 | // The StringRef is null-terminated. | ||||||
2536 | StringRef str = getName(Policy); | ||||||
2537 | assert(!str.empty() && str.data()[str.size()] == '\0')(static_cast <bool> (!str.empty() && str.data() [str.size()] == '\0') ? void (0) : __assert_fail ("!str.empty() && str.data()[str.size()] == '\\0'" , "clang/include/clang/AST/Type.h", 2537, __extension__ __PRETTY_FUNCTION__ )); | ||||||
2538 | return str.data(); | ||||||
2539 | } | ||||||
2540 | |||||||
2541 | bool isSugared() const { return false; } | ||||||
2542 | QualType desugar() const { return QualType(this, 0); } | ||||||
2543 | |||||||
2544 | bool isInteger() const { | ||||||
2545 | return getKind() >= Bool && getKind() <= Int128; | ||||||
2546 | } | ||||||
2547 | |||||||
2548 | bool isSignedInteger() const { | ||||||
2549 | return getKind() >= Char_S && getKind() <= Int128; | ||||||
2550 | } | ||||||
2551 | |||||||
2552 | bool isUnsignedInteger() const { | ||||||
2553 | return getKind() >= Bool && getKind() <= UInt128; | ||||||
2554 | } | ||||||
2555 | |||||||
2556 | bool isFloatingPoint() const { | ||||||
2557 | return getKind() >= Half && getKind() <= Ibm128; | ||||||
2558 | } | ||||||
2559 | |||||||
2560 | /// Determines whether the given kind corresponds to a placeholder type. | ||||||
2561 | static bool isPlaceholderTypeKind(Kind K) { | ||||||
2562 | return K >= Overload; | ||||||
2563 | } | ||||||
2564 | |||||||
2565 | /// Determines whether this type is a placeholder type, i.e. a type | ||||||
2566 | /// which cannot appear in arbitrary positions in a fully-formed | ||||||
2567 | /// expression. | ||||||
2568 | bool isPlaceholderType() const { | ||||||
2569 | return isPlaceholderTypeKind(getKind()); | ||||||
2570 | } | ||||||
2571 | |||||||
2572 | /// Determines whether this type is a placeholder type other than | ||||||
2573 | /// Overload. Most placeholder types require only syntactic | ||||||
2574 | /// information about their context in order to be resolved (e.g. | ||||||
2575 | /// whether it is a call expression), which means they can (and | ||||||
2576 | /// should) be resolved in an earlier "phase" of analysis. | ||||||
2577 | /// Overload expressions sometimes pick up further information | ||||||
2578 | /// from their context, like whether the context expects a | ||||||
2579 | /// specific function-pointer type, and so frequently need | ||||||
2580 | /// special treatment. | ||||||
2581 | bool isNonOverloadPlaceholderType() const { | ||||||
2582 | return getKind() > Overload; | ||||||
2583 | } | ||||||
2584 | |||||||
2585 | static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } | ||||||
2586 | }; | ||||||
2587 | |||||||
2588 | /// Complex values, per C99 6.2.5p11. This supports the C99 complex | ||||||
2589 | /// types (_Complex float etc) as well as the GCC integer complex extensions. | ||||||
2590 | class ComplexType : public Type, public llvm::FoldingSetNode { | ||||||
2591 | friend class ASTContext; // ASTContext creates these. | ||||||
2592 | |||||||
2593 | QualType ElementType; | ||||||
2594 | |||||||
2595 | ComplexType(QualType Element, QualType CanonicalPtr) | ||||||
2596 | : Type(Complex, CanonicalPtr, Element->getDependence()), | ||||||
2597 | ElementType(Element) {} | ||||||
2598 | |||||||
2599 | public: | ||||||
2600 | QualType getElementType() const { return ElementType; } | ||||||
2601 | |||||||
2602 | bool isSugared() const { return false; } | ||||||
2603 | QualType desugar() const { return QualType(this, 0); } | ||||||
2604 | |||||||
2605 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
2606 | Profile(ID, getElementType()); | ||||||
2607 | } | ||||||
2608 | |||||||
2609 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) { | ||||||
2610 | ID.AddPointer(Element.getAsOpaquePtr()); | ||||||
2611 | } | ||||||
2612 | |||||||
2613 | static bool classof(const Type *T) { return T->getTypeClass() == Complex; } | ||||||
2614 | }; | ||||||
2615 | |||||||
2616 | /// Sugar for parentheses used when specifying types. | ||||||
2617 | class ParenType : public Type, public llvm::FoldingSetNode { | ||||||
2618 | friend class ASTContext; // ASTContext creates these. | ||||||
2619 | |||||||
2620 | QualType Inner; | ||||||
2621 | |||||||
2622 | ParenType(QualType InnerType, QualType CanonType) | ||||||
2623 | : Type(Paren, CanonType, InnerType->getDependence()), Inner(InnerType) {} | ||||||
2624 | |||||||
2625 | public: | ||||||
2626 | QualType getInnerType() const { return Inner; } | ||||||
2627 | |||||||
2628 | bool isSugared() const { return true; } | ||||||
2629 | QualType desugar() const { return getInnerType(); } | ||||||
2630 | |||||||
2631 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
2632 | Profile(ID, getInnerType()); | ||||||
2633 | } | ||||||
2634 | |||||||
2635 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Inner) { | ||||||
2636 | Inner.Profile(ID); | ||||||
2637 | } | ||||||
2638 | |||||||
2639 | static bool classof(const Type *T) { return T->getTypeClass() == Paren; } | ||||||
2640 | }; | ||||||
2641 | |||||||
2642 | /// PointerType - C99 6.7.5.1 - Pointer Declarators. | ||||||
2643 | class PointerType : public Type, public llvm::FoldingSetNode { | ||||||
2644 | friend class ASTContext; // ASTContext creates these. | ||||||
2645 | |||||||
2646 | QualType PointeeType; | ||||||
2647 | |||||||
2648 | PointerType(QualType Pointee, QualType CanonicalPtr) | ||||||
2649 | : Type(Pointer, CanonicalPtr, Pointee->getDependence()), | ||||||
2650 | PointeeType(Pointee) {} | ||||||
2651 | |||||||
2652 | public: | ||||||
2653 | QualType getPointeeType() const { return PointeeType; } | ||||||
2654 | |||||||
2655 | bool isSugared() const { return false; } | ||||||
2656 | QualType desugar() const { return QualType(this, 0); } | ||||||
2657 | |||||||
2658 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
2659 | Profile(ID, getPointeeType()); | ||||||
2660 | } | ||||||
2661 | |||||||
2662 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { | ||||||
2663 | ID.AddPointer(Pointee.getAsOpaquePtr()); | ||||||
2664 | } | ||||||
2665 | |||||||
2666 | static bool classof(const Type *T) { return T->getTypeClass() == Pointer; } | ||||||
2667 | }; | ||||||
2668 | |||||||
2669 | /// Represents a type which was implicitly adjusted by the semantic | ||||||
2670 | /// engine for arbitrary reasons. For example, array and function types can | ||||||
2671 | /// decay, and function types can have their calling conventions adjusted. | ||||||
2672 | class AdjustedType : public Type, public llvm::FoldingSetNode { | ||||||
2673 | QualType OriginalTy; | ||||||
2674 | QualType AdjustedTy; | ||||||
2675 | |||||||
2676 | protected: | ||||||
2677 | friend class ASTContext; // ASTContext creates these. | ||||||
2678 | |||||||
2679 | AdjustedType(TypeClass TC, QualType OriginalTy, QualType AdjustedTy, | ||||||
2680 | QualType CanonicalPtr) | ||||||
2681 | : Type(TC, CanonicalPtr, OriginalTy->getDependence()), | ||||||
2682 | OriginalTy(OriginalTy), AdjustedTy(AdjustedTy) {} | ||||||
2683 | |||||||
2684 | public: | ||||||
2685 | QualType getOriginalType() const { return OriginalTy; } | ||||||
2686 | QualType getAdjustedType() const { return AdjustedTy; } | ||||||
2687 | |||||||
2688 | bool isSugared() const { return true; } | ||||||
2689 | QualType desugar() const { return AdjustedTy; } | ||||||
2690 | |||||||
2691 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
2692 | Profile(ID, OriginalTy, AdjustedTy); | ||||||
2693 | } | ||||||
2694 | |||||||
2695 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Orig, QualType New) { | ||||||
2696 | ID.AddPointer(Orig.getAsOpaquePtr()); | ||||||
2697 | ID.AddPointer(New.getAsOpaquePtr()); | ||||||
2698 | } | ||||||
2699 | |||||||
2700 | static bool classof(const Type *T) { | ||||||
2701 | return T->getTypeClass() == Adjusted || T->getTypeClass() == Decayed; | ||||||
2702 | } | ||||||
2703 | }; | ||||||
2704 | |||||||
2705 | /// Represents a pointer type decayed from an array or function type. | ||||||
2706 | class DecayedType : public AdjustedType { | ||||||
2707 | friend class ASTContext; // ASTContext creates these. | ||||||
2708 | |||||||
2709 | inline | ||||||
2710 | DecayedType(QualType OriginalType, QualType Decayed, QualType Canonical); | ||||||
2711 | |||||||
2712 | public: | ||||||
2713 | QualType getDecayedType() const { return getAdjustedType(); } | ||||||
2714 | |||||||
2715 | inline QualType getPointeeType() const; | ||||||
2716 | |||||||
2717 | static bool classof(const Type *T) { return T->getTypeClass() == Decayed; } | ||||||
2718 | }; | ||||||
2719 | |||||||
2720 | /// Pointer to a block type. | ||||||
2721 | /// This type is to represent types syntactically represented as | ||||||
2722 | /// "void (^)(int)", etc. Pointee is required to always be a function type. | ||||||
2723 | class BlockPointerType : public Type, public llvm::FoldingSetNode { | ||||||
2724 | friend class ASTContext; // ASTContext creates these. | ||||||
2725 | |||||||
2726 | // Block is some kind of pointer type | ||||||
2727 | QualType PointeeType; | ||||||
2728 | |||||||
2729 | BlockPointerType(QualType Pointee, QualType CanonicalCls) | ||||||
2730 | : Type(BlockPointer, CanonicalCls, Pointee->getDependence()), | ||||||
2731 | PointeeType(Pointee) {} | ||||||
2732 | |||||||
2733 | public: | ||||||
2734 | // Get the pointee type. Pointee is required to always be a function type. | ||||||
2735 | QualType getPointeeType() const { return PointeeType; } | ||||||
2736 | |||||||
2737 | bool isSugared() const { return false; } | ||||||
2738 | QualType desugar() const { return QualType(this, 0); } | ||||||
2739 | |||||||
2740 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
2741 | Profile(ID, getPointeeType()); | ||||||
2742 | } | ||||||
2743 | |||||||
2744 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { | ||||||
2745 | ID.AddPointer(Pointee.getAsOpaquePtr()); | ||||||
2746 | } | ||||||
2747 | |||||||
2748 | static bool classof(const Type *T) { | ||||||
2749 | return T->getTypeClass() == BlockPointer; | ||||||
2750 | } | ||||||
2751 | }; | ||||||
2752 | |||||||
2753 | /// Base for LValueReferenceType and RValueReferenceType | ||||||
2754 | class ReferenceType : public Type, public llvm::FoldingSetNode { | ||||||
2755 | QualType PointeeType; | ||||||
2756 | |||||||
2757 | protected: | ||||||
2758 | ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef, | ||||||
2759 | bool SpelledAsLValue) | ||||||
2760 | : Type(tc, CanonicalRef, Referencee->getDependence()), | ||||||
2761 | PointeeType(Referencee) { | ||||||
2762 | ReferenceTypeBits.SpelledAsLValue = SpelledAsLValue; | ||||||
2763 | ReferenceTypeBits.InnerRef = Referencee->isReferenceType(); | ||||||
2764 | } | ||||||
2765 | |||||||
2766 | public: | ||||||
2767 | bool isSpelledAsLValue() const { return ReferenceTypeBits.SpelledAsLValue; } | ||||||
2768 | bool isInnerRef() const { return ReferenceTypeBits.InnerRef; } | ||||||
2769 | |||||||
2770 | QualType getPointeeTypeAsWritten() const { return PointeeType; } | ||||||
2771 | |||||||
2772 | QualType getPointeeType() const { | ||||||
2773 | // FIXME: this might strip inner qualifiers; okay? | ||||||
2774 | const ReferenceType *T = this; | ||||||
2775 | while (T->isInnerRef()) | ||||||
2776 | T = T->PointeeType->castAs<ReferenceType>(); | ||||||
2777 | return T->PointeeType; | ||||||
2778 | } | ||||||
2779 | |||||||
2780 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
2781 | Profile(ID, PointeeType, isSpelledAsLValue()); | ||||||
2782 | } | ||||||
2783 | |||||||
2784 | static void Profile(llvm::FoldingSetNodeID &ID, | ||||||
2785 | QualType Referencee, | ||||||
2786 | bool SpelledAsLValue) { | ||||||
2787 | ID.AddPointer(Referencee.getAsOpaquePtr()); | ||||||
2788 | ID.AddBoolean(SpelledAsLValue); | ||||||
2789 | } | ||||||
2790 | |||||||
2791 | static bool classof(const Type *T) { | ||||||
2792 | return T->getTypeClass() == LValueReference || | ||||||
2793 | T->getTypeClass() == RValueReference; | ||||||
2794 | } | ||||||
2795 | }; | ||||||
2796 | |||||||
2797 | /// An lvalue reference type, per C++11 [dcl.ref]. | ||||||
2798 | class LValueReferenceType : public ReferenceType { | ||||||
2799 | friend class ASTContext; // ASTContext creates these | ||||||
2800 | |||||||
2801 | LValueReferenceType(QualType Referencee, QualType CanonicalRef, | ||||||
2802 | bool SpelledAsLValue) | ||||||
2803 | : ReferenceType(LValueReference, Referencee, CanonicalRef, | ||||||
2804 | SpelledAsLValue) {} | ||||||
2805 | |||||||
2806 | public: | ||||||
2807 | bool isSugared() const { return false; } | ||||||
2808 | QualType desugar() const { return QualType(this, 0); } | ||||||
2809 | |||||||
2810 | static bool classof(const Type *T) { | ||||||
2811 | return T->getTypeClass() == LValueReference; | ||||||
2812 | } | ||||||
2813 | }; | ||||||
2814 | |||||||
2815 | /// An rvalue reference type, per C++11 [dcl.ref]. | ||||||
2816 | class RValueReferenceType : public ReferenceType { | ||||||
2817 | friend class ASTContext; // ASTContext creates these | ||||||
2818 | |||||||
2819 | RValueReferenceType(QualType Referencee, QualType CanonicalRef) | ||||||
2820 | : ReferenceType(RValueReference, Referencee, CanonicalRef, false) {} | ||||||
2821 | |||||||
2822 | public: | ||||||
2823 | bool isSugared() const { return false; } | ||||||
2824 | QualType desugar() const { return QualType(this, 0); } | ||||||
2825 | |||||||
2826 | static bool classof(const Type *T) { | ||||||
2827 | return T->getTypeClass() == RValueReference; | ||||||
2828 | } | ||||||
2829 | }; | ||||||
2830 | |||||||
2831 | /// A pointer to member type per C++ 8.3.3 - Pointers to members. | ||||||
2832 | /// | ||||||
2833 | /// This includes both pointers to data members and pointer to member functions. | ||||||
2834 | class MemberPointerType : public Type, public llvm::FoldingSetNode { | ||||||
2835 | friend class ASTContext; // ASTContext creates these. | ||||||
2836 | |||||||
2837 | QualType PointeeType; | ||||||
2838 | |||||||
2839 | /// The class of which the pointee is a member. Must ultimately be a | ||||||
2840 | /// RecordType, but could be a typedef or a template parameter too. | ||||||
2841 | const Type *Class; | ||||||
2842 | |||||||
2843 | MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr) | ||||||
2844 | : Type(MemberPointer, CanonicalPtr, | ||||||
2845 | (Cls->getDependence() & ~TypeDependence::VariablyModified) | | ||||||
2846 | Pointee->getDependence()), | ||||||
2847 | PointeeType(Pointee), Class(Cls) {} | ||||||
2848 | |||||||
2849 | public: | ||||||
2850 | QualType getPointeeType() const { return PointeeType; } | ||||||
2851 | |||||||
2852 | /// Returns true if the member type (i.e. the pointee type) is a | ||||||
2853 | /// function type rather than a data-member type. | ||||||
2854 | bool isMemberFunctionPointer() const { | ||||||
2855 | return PointeeType->isFunctionProtoType(); | ||||||
2856 | } | ||||||
2857 | |||||||
2858 | /// Returns true if the member type (i.e. the pointee type) is a | ||||||
2859 | /// data type rather than a function type. | ||||||
2860 | bool isMemberDataPointer() const { | ||||||
2861 | return !PointeeType->isFunctionProtoType(); | ||||||
2862 | } | ||||||
2863 | |||||||
2864 | const Type *getClass() const { return Class; } | ||||||
2865 | CXXRecordDecl *getMostRecentCXXRecordDecl() const; | ||||||
2866 | |||||||
2867 | bool isSugared() const { return false; } | ||||||
2868 | QualType desugar() const { return QualType(this, 0); } | ||||||
2869 | |||||||
2870 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
2871 | Profile(ID, getPointeeType(), getClass()); | ||||||
2872 | } | ||||||
2873 | |||||||
2874 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee, | ||||||
2875 | const Type *Class) { | ||||||
2876 | ID.AddPointer(Pointee.getAsOpaquePtr()); | ||||||
2877 | ID.AddPointer(Class); | ||||||
2878 | } | ||||||
2879 | |||||||
2880 | static bool classof(const Type *T) { | ||||||
2881 | return T->getTypeClass() == MemberPointer; | ||||||
2882 | } | ||||||
2883 | }; | ||||||
2884 | |||||||
2885 | /// Represents an array type, per C99 6.7.5.2 - Array Declarators. | ||||||
2886 | class ArrayType : public Type, public llvm::FoldingSetNode { | ||||||
2887 | public: | ||||||
2888 | /// Capture whether this is a normal array (e.g. int X[4]) | ||||||
2889 | /// an array with a static size (e.g. int X[static 4]), or an array | ||||||
2890 | /// with a star size (e.g. int X[*]). | ||||||
2891 | /// 'static' is only allowed on function parameters. | ||||||
2892 | enum ArraySizeModifier { | ||||||
2893 | Normal, Static, Star | ||||||
2894 | }; | ||||||
2895 | |||||||
2896 | private: | ||||||
2897 | /// The element type of the array. | ||||||
2898 | QualType ElementType; | ||||||
2899 | |||||||
2900 | protected: | ||||||
2901 | friend class ASTContext; // ASTContext creates these. | ||||||
2902 | |||||||
2903 | ArrayType(TypeClass tc, QualType et, QualType can, ArraySizeModifier sm, | ||||||
2904 | unsigned tq, const Expr *sz = nullptr); | ||||||
2905 | |||||||
2906 | public: | ||||||
2907 | QualType getElementType() const { return ElementType; } | ||||||
2908 | |||||||
2909 | ArraySizeModifier getSizeModifier() const { | ||||||
2910 | return ArraySizeModifier(ArrayTypeBits.SizeModifier); | ||||||
2911 | } | ||||||
2912 | |||||||
2913 | Qualifiers getIndexTypeQualifiers() const { | ||||||
2914 | return Qualifiers::fromCVRMask(getIndexTypeCVRQualifiers()); | ||||||
2915 | } | ||||||
2916 | |||||||
2917 | unsigned getIndexTypeCVRQualifiers() const { | ||||||
2918 | return ArrayTypeBits.IndexTypeQuals; | ||||||
2919 | } | ||||||
2920 | |||||||
2921 | static bool classof(const Type *T) { | ||||||
2922 | return T->getTypeClass() == ConstantArray || | ||||||
2923 | T->getTypeClass() == VariableArray || | ||||||
2924 | T->getTypeClass() == IncompleteArray || | ||||||
2925 | T->getTypeClass() == DependentSizedArray; | ||||||
2926 | } | ||||||
2927 | }; | ||||||
2928 | |||||||
2929 | /// Represents the canonical version of C arrays with a specified constant size. | ||||||
2930 | /// For example, the canonical type for 'int A[4 + 4*100]' is a | ||||||
2931 | /// ConstantArrayType where the element type is 'int' and the size is 404. | ||||||
2932 | class ConstantArrayType final | ||||||
2933 | : public ArrayType, | ||||||
2934 | private llvm::TrailingObjects<ConstantArrayType, const Expr *> { | ||||||
2935 | friend class ASTContext; // ASTContext creates these. | ||||||
2936 | friend TrailingObjects; | ||||||
2937 | |||||||
2938 | llvm::APInt Size; // Allows us to unique the type. | ||||||
2939 | |||||||
2940 | ConstantArrayType(QualType et, QualType can, const llvm::APInt &size, | ||||||
2941 | const Expr *sz, ArraySizeModifier sm, unsigned tq) | ||||||
2942 | : ArrayType(ConstantArray, et, can, sm, tq, sz), Size(size) { | ||||||
2943 | ConstantArrayTypeBits.HasStoredSizeExpr = sz != nullptr; | ||||||
2944 | if (ConstantArrayTypeBits.HasStoredSizeExpr) { | ||||||
2945 | assert(!can.isNull() && "canonical constant array should not have size")(static_cast <bool> (!can.isNull() && "canonical constant array should not have size" ) ? void (0) : __assert_fail ("!can.isNull() && \"canonical constant array should not have size\"" , "clang/include/clang/AST/Type.h", 2945, __extension__ __PRETTY_FUNCTION__ )); | ||||||
2946 | *getTrailingObjects<const Expr*>() = sz; | ||||||
2947 | } | ||||||
2948 | } | ||||||
2949 | |||||||
2950 | unsigned numTrailingObjects(OverloadToken<const Expr*>) const { | ||||||
2951 | return ConstantArrayTypeBits.HasStoredSizeExpr; | ||||||
2952 | } | ||||||
2953 | |||||||
2954 | public: | ||||||
2955 | const llvm::APInt &getSize() const { return Size; } | ||||||
2956 | const Expr *getSizeExpr() const { | ||||||
2957 | return ConstantArrayTypeBits.HasStoredSizeExpr | ||||||
2958 | ? *getTrailingObjects<const Expr *>() | ||||||
2959 | : nullptr; | ||||||
2960 | } | ||||||
2961 | bool isSugared() const { return false; } | ||||||
2962 | QualType desugar() const { return QualType(this, 0); } | ||||||
2963 | |||||||
2964 | /// Determine the number of bits required to address a member of | ||||||
2965 | // an array with the given element type and number of elements. | ||||||
2966 | static unsigned getNumAddressingBits(const ASTContext &Context, | ||||||
2967 | QualType ElementType, | ||||||
2968 | const llvm::APInt &NumElements); | ||||||
2969 | |||||||
2970 | /// Determine the maximum number of active bits that an array's size | ||||||
2971 | /// can require, which limits the maximum size of the array. | ||||||
2972 | static unsigned getMaxSizeBits(const ASTContext &Context); | ||||||
2973 | |||||||
2974 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) { | ||||||
2975 | Profile(ID, Ctx, getElementType(), getSize(), getSizeExpr(), | ||||||
2976 | getSizeModifier(), getIndexTypeCVRQualifiers()); | ||||||
2977 | } | ||||||
2978 | |||||||
2979 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx, | ||||||
2980 | QualType ET, const llvm::APInt &ArraySize, | ||||||
2981 | const Expr *SizeExpr, ArraySizeModifier SizeMod, | ||||||
2982 | unsigned TypeQuals); | ||||||
2983 | |||||||
2984 | static bool classof(const Type *T) { | ||||||
2985 | return T->getTypeClass() == ConstantArray; | ||||||
2986 | } | ||||||
2987 | }; | ||||||
2988 | |||||||
2989 | /// Represents a C array with an unspecified size. For example 'int A[]' has | ||||||
2990 | /// an IncompleteArrayType where the element type is 'int' and the size is | ||||||
2991 | /// unspecified. | ||||||
2992 | class IncompleteArrayType : public ArrayType { | ||||||
2993 | friend class ASTContext; // ASTContext creates these. | ||||||
2994 | |||||||
2995 | IncompleteArrayType(QualType et, QualType can, | ||||||
2996 | ArraySizeModifier sm, unsigned tq) | ||||||
2997 | : ArrayType(IncompleteArray, et, can, sm, tq) {} | ||||||
2998 | |||||||
2999 | public: | ||||||
3000 | friend class StmtIteratorBase; | ||||||
3001 | |||||||
3002 | bool isSugared() const { return false; } | ||||||
3003 | QualType desugar() const { return QualType(this, 0); } | ||||||
3004 | |||||||
3005 | static bool classof(const Type *T) { | ||||||
3006 | return T->getTypeClass() == IncompleteArray; | ||||||
3007 | } | ||||||
3008 | |||||||
3009 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
3010 | Profile(ID, getElementType(), getSizeModifier(), | ||||||
3011 | getIndexTypeCVRQualifiers()); | ||||||
3012 | } | ||||||
3013 | |||||||
3014 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, | ||||||
3015 | ArraySizeModifier SizeMod, unsigned TypeQuals) { | ||||||
3016 | ID.AddPointer(ET.getAsOpaquePtr()); | ||||||
3017 | ID.AddInteger(SizeMod); | ||||||
3018 | ID.AddInteger(TypeQuals); | ||||||
3019 | } | ||||||
3020 | }; | ||||||
3021 | |||||||
3022 | /// Represents a C array with a specified size that is not an | ||||||
3023 | /// integer-constant-expression. For example, 'int s[x+foo()]'. | ||||||
3024 | /// Since the size expression is an arbitrary expression, we store it as such. | ||||||
3025 | /// | ||||||
3026 | /// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and | ||||||
3027 | /// should not be: two lexically equivalent variable array types could mean | ||||||
3028 | /// different things, for example, these variables do not have the same type | ||||||
3029 | /// dynamically: | ||||||
3030 | /// | ||||||
3031 | /// void foo(int x) { | ||||||
3032 | /// int Y[x]; | ||||||
3033 | /// ++x; | ||||||
3034 | /// int Z[x]; | ||||||
3035 | /// } | ||||||
3036 | class VariableArrayType : public ArrayType { | ||||||
3037 | friend class ASTContext; // ASTContext creates these. | ||||||
3038 | |||||||
3039 | /// An assignment-expression. VLA's are only permitted within | ||||||
3040 | /// a function block. | ||||||
3041 | Stmt *SizeExpr; | ||||||
3042 | |||||||
3043 | /// The range spanned by the left and right array brackets. | ||||||
3044 | SourceRange Brackets; | ||||||
3045 | |||||||
3046 | VariableArrayType(QualType et, QualType can, Expr *e, | ||||||
3047 | ArraySizeModifier sm, unsigned tq, | ||||||
3048 | SourceRange brackets) | ||||||
3049 | : ArrayType(VariableArray, et, can, sm, tq, e), | ||||||
3050 | SizeExpr((Stmt*) e), Brackets(brackets) {} | ||||||
3051 | |||||||
3052 | public: | ||||||
3053 | friend class StmtIteratorBase; | ||||||
3054 | |||||||
3055 | Expr *getSizeExpr() const { | ||||||
3056 | // We use C-style casts instead of cast<> here because we do not wish | ||||||
3057 | // to have a dependency of Type.h on Stmt.h/Expr.h. | ||||||
3058 | return (Expr*) SizeExpr; | ||||||
3059 | } | ||||||
3060 | |||||||
3061 | SourceRange getBracketsRange() const { return Brackets; } | ||||||
3062 | SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } | ||||||
3063 | SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } | ||||||
3064 | |||||||
3065 | bool isSugared() const { return false; } | ||||||
3066 | QualType desugar() const { return QualType(this, 0); } | ||||||
3067 | |||||||
3068 | static bool classof(const Type *T) { | ||||||
3069 | return T->getTypeClass() == VariableArray; | ||||||
3070 | } | ||||||
3071 | |||||||
3072 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
3073 | llvm_unreachable("Cannot unique VariableArrayTypes.")::llvm::llvm_unreachable_internal("Cannot unique VariableArrayTypes." , "clang/include/clang/AST/Type.h", 3073); | ||||||
3074 | } | ||||||
3075 | }; | ||||||
3076 | |||||||
3077 | /// Represents an array type in C++ whose size is a value-dependent expression. | ||||||
3078 | /// | ||||||
3079 | /// For example: | ||||||
3080 | /// \code | ||||||
3081 | /// template<typename T, int Size> | ||||||
3082 | /// class array { | ||||||
3083 | /// T data[Size]; | ||||||
3084 | /// }; | ||||||
3085 | /// \endcode | ||||||
3086 | /// | ||||||
3087 | /// For these types, we won't actually know what the array bound is | ||||||
3088 | /// until template instantiation occurs, at which point this will | ||||||
3089 | /// become either a ConstantArrayType or a VariableArrayType. | ||||||
3090 | class DependentSizedArrayType : public ArrayType { | ||||||
3091 | friend class ASTContext; // ASTContext creates these. | ||||||
3092 | |||||||
3093 | const ASTContext &Context; | ||||||
3094 | |||||||
3095 | /// An assignment expression that will instantiate to the | ||||||
3096 | /// size of the array. | ||||||
3097 | /// | ||||||
3098 | /// The expression itself might be null, in which case the array | ||||||
3099 | /// type will have its size deduced from an initializer. | ||||||
3100 | Stmt *SizeExpr; | ||||||
3101 | |||||||
3102 | /// The range spanned by the left and right array brackets. | ||||||
3103 | SourceRange Brackets; | ||||||
3104 | |||||||
3105 | DependentSizedArrayType(const ASTContext &Context, QualType et, QualType can, | ||||||
3106 | Expr *e, ArraySizeModifier sm, unsigned tq, | ||||||
3107 | SourceRange brackets); | ||||||
3108 | |||||||
3109 | public: | ||||||
3110 | friend class StmtIteratorBase; | ||||||
3111 | |||||||
3112 | Expr *getSizeExpr() const { | ||||||
3113 | // We use C-style casts instead of cast<> here because we do not wish | ||||||
3114 | // to have a dependency of Type.h on Stmt.h/Expr.h. | ||||||
3115 | return (Expr*) SizeExpr; | ||||||
3116 | } | ||||||
3117 | |||||||
3118 | SourceRange getBracketsRange() const { return Brackets; } | ||||||
3119 | SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } | ||||||
3120 | SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } | ||||||
3121 | |||||||
3122 | bool isSugared() const { return false; } | ||||||
3123 | QualType desugar() const { return QualType(this, 0); } | ||||||
3124 | |||||||
3125 | static bool classof(const Type *T) { | ||||||
3126 | return T->getTypeClass() == DependentSizedArray; | ||||||
3127 | } | ||||||
3128 | |||||||
3129 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
3130 | Profile(ID, Context, getElementType(), | ||||||
3131 | getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr()); | ||||||
3132 | } | ||||||
3133 | |||||||
3134 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, | ||||||
3135 | QualType ET, ArraySizeModifier SizeMod, | ||||||
3136 | unsigned TypeQuals, Expr *E); | ||||||
3137 | }; | ||||||
3138 | |||||||
3139 | /// Represents an extended address space qualifier where the input address space | ||||||
3140 | /// value is dependent. Non-dependent address spaces are not represented with a | ||||||
3141 | /// special Type subclass; they are stored on an ExtQuals node as part of a QualType. | ||||||
3142 | /// | ||||||
3143 | /// For example: | ||||||
3144 | /// \code | ||||||
3145 | /// template<typename T, int AddrSpace> | ||||||
3146 | /// class AddressSpace { | ||||||
3147 | /// typedef T __attribute__((address_space(AddrSpace))) type; | ||||||
3148 | /// } | ||||||
3149 | /// \endcode | ||||||
3150 | class DependentAddressSpaceType : public Type, public llvm::FoldingSetNode { | ||||||
3151 | friend class ASTContext; | ||||||
3152 | |||||||
3153 | const ASTContext &Context; | ||||||
3154 | Expr *AddrSpaceExpr; | ||||||
3155 | QualType PointeeType; | ||||||
3156 | SourceLocation loc; | ||||||
3157 | |||||||
3158 | DependentAddressSpaceType(const ASTContext &Context, QualType PointeeType, | ||||||
3159 | QualType can, Expr *AddrSpaceExpr, | ||||||
3160 | SourceLocation loc); | ||||||
3161 | |||||||
3162 | public: | ||||||
3163 | Expr *getAddrSpaceExpr() const { return AddrSpaceExpr; } | ||||||
3164 | QualType getPointeeType() const { return PointeeType; } | ||||||
3165 | SourceLocation getAttributeLoc() const { return loc; } | ||||||
3166 | |||||||
3167 | bool isSugared() const { return false; } | ||||||
3168 | QualType desugar() const { return QualType(this, 0); } | ||||||
3169 | |||||||
3170 | static bool classof(const Type *T) { | ||||||
3171 | return T->getTypeClass() == DependentAddressSpace; | ||||||
3172 | } | ||||||
3173 | |||||||
3174 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
3175 | Profile(ID, Context, getPointeeType(), getAddrSpaceExpr()); | ||||||
3176 | } | ||||||
3177 | |||||||
3178 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, | ||||||
3179 | QualType PointeeType, Expr *AddrSpaceExpr); | ||||||
3180 | }; | ||||||
3181 | |||||||
3182 | /// Represents an extended vector type where either the type or size is | ||||||
3183 | /// dependent. | ||||||
3184 | /// | ||||||
3185 | /// For example: | ||||||
3186 | /// \code | ||||||
3187 | /// template<typename T, int Size> | ||||||
3188 | /// class vector { | ||||||
3189 | /// typedef T __attribute__((ext_vector_type(Size))) type; | ||||||
3190 | /// } | ||||||
3191 | /// \endcode | ||||||
3192 | class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode { | ||||||
3193 | friend class ASTContext; | ||||||
3194 | |||||||
3195 | const ASTContext &Context; | ||||||
3196 | Expr *SizeExpr; | ||||||
3197 | |||||||
3198 | /// The element type of the array. | ||||||
3199 | QualType ElementType; | ||||||
3200 | |||||||
3201 | SourceLocation loc; | ||||||
3202 | |||||||
3203 | DependentSizedExtVectorType(const ASTContext &Context, QualType ElementType, | ||||||
3204 | QualType can, Expr *SizeExpr, SourceLocation loc); | ||||||
3205 | |||||||
3206 | public: | ||||||
3207 | Expr *getSizeExpr() const { return SizeExpr; } | ||||||
3208 | QualType getElementType() const { return ElementType; } | ||||||
3209 | SourceLocation getAttributeLoc() const { return loc; } | ||||||
3210 | |||||||
3211 | bool isSugared() const { return false; } | ||||||
3212 | QualType desugar() const { return QualType(this, 0); } | ||||||
3213 | |||||||
3214 | static bool classof(const Type *T) { | ||||||
3215 | return T->getTypeClass() == DependentSizedExtVector; | ||||||
3216 | } | ||||||
3217 | |||||||
3218 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
3219 | Profile(ID, Context, getElementType(), getSizeExpr()); | ||||||
3220 | } | ||||||
3221 | |||||||
3222 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, | ||||||
3223 | QualType ElementType, Expr *SizeExpr); | ||||||
3224 | }; | ||||||
3225 | |||||||
3226 | |||||||
3227 | /// Represents a GCC generic vector type. This type is created using | ||||||
3228 | /// __attribute__((vector_size(n)), where "n" specifies the vector size in | ||||||
3229 | /// bytes; or from an Altivec __vector or vector declaration. | ||||||
3230 | /// Since the constructor takes the number of vector elements, the | ||||||
3231 | /// client is responsible for converting the size into the number of elements. | ||||||
3232 | class VectorType : public Type, public llvm::FoldingSetNode { | ||||||
3233 | public: | ||||||
3234 | enum VectorKind { | ||||||
3235 | /// not a target-specific vector type | ||||||
3236 | GenericVector, | ||||||
3237 | |||||||
3238 | /// is AltiVec vector | ||||||
3239 | AltiVecVector, | ||||||
3240 | |||||||
3241 | /// is AltiVec 'vector Pixel' | ||||||
3242 | AltiVecPixel, | ||||||
3243 | |||||||
3244 | /// is AltiVec 'vector bool ...' | ||||||
3245 | AltiVecBool, | ||||||
3246 | |||||||
3247 | /// is ARM Neon vector | ||||||
3248 | NeonVector, | ||||||
3249 | |||||||
3250 | /// is ARM Neon polynomial vector | ||||||
3251 | NeonPolyVector, | ||||||
3252 | |||||||
3253 | /// is AArch64 SVE fixed-length data vector | ||||||
3254 | SveFixedLengthDataVector, | ||||||
3255 | |||||||
3256 | /// is AArch64 SVE fixed-length predicate vector | ||||||
3257 | SveFixedLengthPredicateVector | ||||||
3258 | }; | ||||||
3259 | |||||||
3260 | protected: | ||||||
3261 | friend class ASTContext; // ASTContext creates these. | ||||||
3262 | |||||||
3263 | /// The element type of the vector. | ||||||
3264 | QualType ElementType; | ||||||
3265 | |||||||
3266 | VectorType(QualType vecType, unsigned nElements, QualType canonType, | ||||||
3267 | VectorKind vecKind); | ||||||
3268 | |||||||
3269 | VectorType(TypeClass tc, QualType vecType, unsigned nElements, | ||||||
3270 | QualType canonType, VectorKind vecKind); | ||||||
3271 | |||||||
3272 | public: | ||||||
3273 | QualType getElementType() const { return ElementType; } | ||||||
3274 | unsigned getNumElements() const { return VectorTypeBits.NumElements; } | ||||||
3275 | |||||||
3276 | bool isSugared() const { return false; } | ||||||
3277 | QualType desugar() const { return QualType(this, 0); } | ||||||
3278 | |||||||
3279 | VectorKind getVectorKind() const { | ||||||
3280 | return VectorKind(VectorTypeBits.VecKind); | ||||||
3281 | } | ||||||
3282 | |||||||
3283 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
3284 | Profile(ID, getElementType(), getNumElements(), | ||||||
3285 | getTypeClass(), getVectorKind()); | ||||||
3286 | } | ||||||
3287 | |||||||
3288 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, | ||||||
3289 | unsigned NumElements, TypeClass TypeClass, | ||||||
3290 | VectorKind VecKind) { | ||||||
3291 | ID.AddPointer(ElementType.getAsOpaquePtr()); | ||||||
3292 | ID.AddInteger(NumElements); | ||||||
3293 | ID.AddInteger(TypeClass); | ||||||
3294 | ID.AddInteger(VecKind); | ||||||
3295 | } | ||||||
3296 | |||||||
3297 | static bool classof(const Type *T) { | ||||||
3298 | return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector; | ||||||
3299 | } | ||||||
3300 | }; | ||||||
3301 | |||||||
3302 | /// Represents a vector type where either the type or size is dependent. | ||||||
3303 | //// | ||||||
3304 | /// For example: | ||||||
3305 | /// \code | ||||||
3306 | /// template<typename T, int Size> | ||||||
3307 | /// class vector { | ||||||
3308 | /// typedef T __attribute__((vector_size(Size))) type; | ||||||
3309 | /// } | ||||||
3310 | /// \endcode | ||||||
3311 | class DependentVectorType : public Type, public llvm::FoldingSetNode { | ||||||
3312 | friend class ASTContext; | ||||||
3313 | |||||||
3314 | const ASTContext &Context; | ||||||
3315 | QualType ElementType; | ||||||
3316 | Expr *SizeExpr; | ||||||
3317 | SourceLocation Loc; | ||||||
3318 | |||||||
3319 | DependentVectorType(const ASTContext &Context, QualType ElementType, | ||||||
3320 | QualType CanonType, Expr *SizeExpr, | ||||||
3321 | SourceLocation Loc, VectorType::VectorKind vecKind); | ||||||
3322 | |||||||
3323 | public: | ||||||
3324 | Expr *getSizeExpr() const { return SizeExpr; } | ||||||
3325 | QualType getElementType() const { return ElementType; } | ||||||
3326 | SourceLocation getAttributeLoc() const { return Loc; } | ||||||
3327 | VectorType::VectorKind getVectorKind() const { | ||||||
3328 | return VectorType::VectorKind(VectorTypeBits.VecKind); | ||||||
3329 | } | ||||||
3330 | |||||||
3331 | bool isSugared() const { return false; } | ||||||
3332 | QualType desugar() const { return QualType(this, 0); } | ||||||
3333 | |||||||
3334 | static bool classof(const Type *T) { | ||||||
3335 | return T->getTypeClass() == DependentVector; | ||||||
3336 | } | ||||||
3337 | |||||||
3338 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
3339 | Profile(ID, Context, getElementType(), getSizeExpr(), getVectorKind()); | ||||||
3340 | } | ||||||
3341 | |||||||
3342 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, | ||||||
3343 | QualType ElementType, const Expr *SizeExpr, | ||||||
3344 | VectorType::VectorKind VecKind); | ||||||
3345 | }; | ||||||
3346 | |||||||
3347 | /// ExtVectorType - Extended vector type. This type is created using | ||||||
3348 | /// __attribute__((ext_vector_type(n)), where "n" is the number of elements. | ||||||
3349 | /// Unlike vector_size, ext_vector_type is only allowed on typedef's. This | ||||||
3350 | /// class enables syntactic extensions, like Vector Components for accessing | ||||||
3351 | /// points (as .xyzw), colors (as .rgba), and textures (modeled after OpenGL | ||||||
3352 | /// Shading Language). | ||||||
3353 | class ExtVectorType : public VectorType { | ||||||
3354 | friend class ASTContext; // ASTContext creates these. | ||||||
3355 | |||||||
3356 | ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) | ||||||
3357 | : VectorType(ExtVector, vecType, nElements, canonType, GenericVector) {} | ||||||
3358 | |||||||
3359 | public: | ||||||
3360 | static int getPointAccessorIdx(char c) { | ||||||
3361 | switch (c) { | ||||||
3362 | default: return -1; | ||||||
3363 | case 'x': case 'r': return 0; | ||||||
3364 | case 'y': case 'g': return 1; | ||||||
3365 | case 'z': case 'b': return 2; | ||||||
3366 | case 'w': case 'a': return 3; | ||||||
3367 | } | ||||||
3368 | } | ||||||
3369 | |||||||
3370 | static int getNumericAccessorIdx(char c) { | ||||||
3371 | switch (c) { | ||||||
3372 | default: return -1; | ||||||
3373 | case '0': return 0; | ||||||
3374 | case '1': return 1; | ||||||
3375 | case '2': return 2; | ||||||
3376 | case '3': return 3; | ||||||
3377 | case '4': return 4; | ||||||
3378 | case '5': return 5; | ||||||
3379 | case '6': return 6; | ||||||
3380 | case '7': return 7; | ||||||
3381 | case '8': return 8; | ||||||
3382 | case '9': return 9; | ||||||
3383 | case 'A': | ||||||
3384 | case 'a': return 10; | ||||||
3385 | case 'B': | ||||||
3386 | case 'b': return 11; | ||||||
3387 | case 'C': | ||||||
3388 | case 'c': return 12; | ||||||
3389 | case 'D': | ||||||
3390 | case 'd': return 13; | ||||||
3391 | case 'E': | ||||||
3392 | case 'e': return 14; | ||||||
3393 | case 'F': | ||||||
3394 | case 'f': return 15; | ||||||
3395 | } | ||||||
3396 | } | ||||||
3397 | |||||||
3398 | static int getAccessorIdx(char c, bool isNumericAccessor) { | ||||||
3399 | if (isNumericAccessor) | ||||||
3400 | return getNumericAccessorIdx(c); | ||||||
3401 | else | ||||||
3402 | return getPointAccessorIdx(c); | ||||||
3403 | } | ||||||
3404 | |||||||
3405 | bool isAccessorWithinNumElements(char c, bool isNumericAccessor) const { | ||||||
3406 | if (int idx = getAccessorIdx(c, isNumericAccessor)+1) | ||||||
3407 | return unsigned(idx-1) < getNumElements(); | ||||||
3408 | return false; | ||||||
3409 | } | ||||||
3410 | |||||||
3411 | bool isSugared() const { return false; } | ||||||
3412 | QualType desugar() const { return QualType(this, 0); } | ||||||
3413 | |||||||
3414 | static bool classof(const Type *T) { | ||||||
3415 | return T->getTypeClass() == ExtVector; | ||||||
3416 | } | ||||||
3417 | }; | ||||||
3418 | |||||||
3419 | /// Represents a matrix type, as defined in the Matrix Types clang extensions. | ||||||
3420 | /// __attribute__((matrix_type(rows, columns))), where "rows" specifies | ||||||
3421 | /// number of rows and "columns" specifies the number of columns. | ||||||
3422 | class MatrixType : public Type, public llvm::FoldingSetNode { | ||||||
3423 | protected: | ||||||
3424 | friend class ASTContext; | ||||||
3425 | |||||||
3426 | /// The element type of the matrix. | ||||||
3427 | QualType ElementType; | ||||||
3428 | |||||||
3429 | MatrixType(QualType ElementTy, QualType CanonElementTy); | ||||||
3430 | |||||||
3431 | MatrixType(TypeClass TypeClass, QualType ElementTy, QualType CanonElementTy, | ||||||
3432 | const Expr *RowExpr = nullptr, const Expr *ColumnExpr = nullptr); | ||||||
3433 | |||||||
3434 | public: | ||||||
3435 | /// Returns type of the elements being stored in the matrix | ||||||
3436 | QualType getElementType() const { return ElementType; } | ||||||
3437 | |||||||
3438 | /// Valid elements types are the following: | ||||||
3439 | /// * an integer type (as in C2x 6.2.5p19), but excluding enumerated types | ||||||
3440 | /// and _Bool | ||||||
3441 | /// * the standard floating types float or double | ||||||
3442 | /// * a half-precision floating point type, if one is supported on the target | ||||||
3443 | static bool isValidElementType(QualType T) { | ||||||
3444 | return T->isDependentType() || | ||||||
3445 | (T->isRealType() && !T->isBooleanType() && !T->isEnumeralType()); | ||||||
3446 | } | ||||||
3447 | |||||||
3448 | bool isSugared() const { return false; } | ||||||
3449 | QualType desugar() const { return QualType(this, 0); } | ||||||
3450 | |||||||
3451 | static bool classof(const Type *T) { | ||||||
3452 | return T->getTypeClass() == ConstantMatrix || | ||||||
3453 | T->getTypeClass() == DependentSizedMatrix; | ||||||
3454 | } | ||||||
3455 | }; | ||||||
3456 | |||||||
3457 | /// Represents a concrete matrix type with constant number of rows and columns | ||||||
3458 | class ConstantMatrixType final : public MatrixType { | ||||||
3459 | protected: | ||||||
3460 | friend class ASTContext; | ||||||
3461 | |||||||
3462 | /// Number of rows and columns. | ||||||
3463 | unsigned NumRows; | ||||||
3464 | unsigned NumColumns; | ||||||
3465 | |||||||
3466 | static constexpr unsigned MaxElementsPerDimension = (1 << 20) - 1; | ||||||
3467 | |||||||
3468 | ConstantMatrixType(QualType MatrixElementType, unsigned NRows, | ||||||
3469 | unsigned NColumns, QualType CanonElementType); | ||||||
3470 | |||||||
3471 | ConstantMatrixType(TypeClass typeClass, QualType MatrixType, unsigned NRows, | ||||||
3472 | unsigned NColumns, QualType CanonElementType); | ||||||
3473 | |||||||
3474 | public: | ||||||
3475 | /// Returns the number of rows in the matrix. | ||||||
3476 | unsigned getNumRows() const { return NumRows; } | ||||||
3477 | |||||||
3478 | /// Returns the number of columns in the matrix. | ||||||
3479 | unsigned getNumColumns() const { return NumColumns; } | ||||||
3480 | |||||||
3481 | /// Returns the number of elements required to embed the matrix into a vector. | ||||||
3482 | unsigned getNumElementsFlattened() const { | ||||||
3483 | return getNumRows() * getNumColumns(); | ||||||
3484 | } | ||||||
3485 | |||||||
3486 | /// Returns true if \p NumElements is a valid matrix dimension. | ||||||
3487 | static constexpr bool isDimensionValid(size_t NumElements) { | ||||||
3488 | return NumElements > 0 && NumElements <= MaxElementsPerDimension; | ||||||
3489 | } | ||||||
3490 | |||||||
3491 | /// Returns the maximum number of elements per dimension. | ||||||
3492 | static constexpr unsigned getMaxElementsPerDimension() { | ||||||
3493 | return MaxElementsPerDimension; | ||||||
3494 | } | ||||||
3495 | |||||||
3496 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
3497 | Profile(ID, getElementType(), getNumRows(), getNumColumns(), | ||||||
3498 | getTypeClass()); | ||||||
3499 | } | ||||||
3500 | |||||||
3501 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, | ||||||
3502 | unsigned NumRows, unsigned NumColumns, | ||||||
3503 | TypeClass TypeClass) { | ||||||
3504 | ID.AddPointer(ElementType.getAsOpaquePtr()); | ||||||
3505 | ID.AddInteger(NumRows); | ||||||
3506 | ID.AddInteger(NumColumns); | ||||||
3507 | ID.AddInteger(TypeClass); | ||||||
3508 | } | ||||||
3509 | |||||||
3510 | static bool classof(const Type *T) { | ||||||
3511 | return T->getTypeClass() == ConstantMatrix; | ||||||
3512 | } | ||||||
3513 | }; | ||||||
3514 | |||||||
3515 | /// Represents a matrix type where the type and the number of rows and columns | ||||||
3516 | /// is dependent on a template. | ||||||
3517 | class DependentSizedMatrixType final : public MatrixType { | ||||||
3518 | friend class ASTContext; | ||||||
3519 | |||||||
3520 | const ASTContext &Context; | ||||||
3521 | Expr *RowExpr; | ||||||
3522 | Expr *ColumnExpr; | ||||||
3523 | |||||||
3524 | SourceLocation loc; | ||||||
3525 | |||||||
3526 | DependentSizedMatrixType(const ASTContext &Context, QualType ElementType, | ||||||
3527 | QualType CanonicalType, Expr *RowExpr, | ||||||
3528 | Expr *ColumnExpr, SourceLocation loc); | ||||||
3529 | |||||||
3530 | public: | ||||||
3531 | Expr *getRowExpr() const { return RowExpr; } | ||||||
3532 | Expr *getColumnExpr() const { return ColumnExpr; } | ||||||
3533 | SourceLocation getAttributeLoc() const { return loc; } | ||||||
3534 | |||||||
3535 | static bool classof(const Type *T) { | ||||||
3536 | return T->getTypeClass() == DependentSizedMatrix; | ||||||
3537 | } | ||||||
3538 | |||||||
3539 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
3540 | Profile(ID, Context, getElementType(), getRowExpr(), getColumnExpr()); | ||||||
3541 | } | ||||||
3542 | |||||||
3543 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, | ||||||
3544 | QualType ElementType, Expr *RowExpr, Expr *ColumnExpr); | ||||||
3545 | }; | ||||||
3546 | |||||||
3547 | /// FunctionType - C99 6.7.5.3 - Function Declarators. This is the common base | ||||||
3548 | /// class of FunctionNoProtoType and FunctionProtoType. | ||||||
3549 | class FunctionType : public Type { | ||||||
3550 | // The type returned by the function. | ||||||
3551 | QualType ResultType; | ||||||
3552 | |||||||
3553 | public: | ||||||
3554 | /// Interesting information about a specific parameter that can't simply | ||||||
3555 | /// be reflected in parameter's type. This is only used by FunctionProtoType | ||||||
3556 | /// but is in FunctionType to make this class available during the | ||||||
3557 | /// specification of the bases of FunctionProtoType. | ||||||
3558 | /// | ||||||
3559 | /// It makes sense to model language features this way when there's some | ||||||
3560 | /// sort of parameter-specific override (such as an attribute) that | ||||||
3561 | /// affects how the function is called. For example, the ARC ns_consumed | ||||||
3562 | /// attribute changes whether a parameter is passed at +0 (the default) | ||||||
3563 | /// or +1 (ns_consumed). This must be reflected in the function type, | ||||||
3564 | /// but isn't really a change to the parameter type. | ||||||
3565 | /// | ||||||
3566 | /// One serious disadvantage of modelling language features this way is | ||||||
3567 | /// that they generally do not work with language features that attempt | ||||||
3568 | /// to destructure types. For example, template argument deduction will | ||||||
3569 | /// not be able to match a parameter declared as | ||||||
3570 | /// T (*)(U) | ||||||
3571 | /// against an argument of type | ||||||
3572 | /// void (*)(__attribute__((ns_consumed)) id) | ||||||
3573 | /// because the substitution of T=void, U=id into the former will | ||||||
3574 | /// not produce the latter. | ||||||
3575 | class ExtParameterInfo { | ||||||
3576 | enum { | ||||||
3577 | ABIMask = 0x0F, | ||||||
3578 | IsConsumed = 0x10, | ||||||
3579 | HasPassObjSize = 0x20, | ||||||
3580 | IsNoEscape = 0x40, | ||||||
3581 | }; | ||||||
3582 | unsigned char Data = 0; | ||||||
3583 | |||||||
3584 | public: | ||||||
3585 | ExtParameterInfo() = default; | ||||||
3586 | |||||||
3587 | /// Return the ABI treatment of this parameter. | ||||||
3588 | ParameterABI getABI() const { return ParameterABI(Data & ABIMask); } | ||||||
3589 | ExtParameterInfo withABI(ParameterABI kind) const { | ||||||
3590 | ExtParameterInfo copy = *this; | ||||||
3591 | copy.Data = (copy.Data & ~ABIMask) | unsigned(kind); | ||||||
3592 | return copy; | ||||||
3593 | } | ||||||
3594 | |||||||
3595 | /// Is this parameter considered "consumed" by Objective-C ARC? | ||||||
3596 | /// Consumed parameters must have retainable object type. | ||||||
3597 | bool isConsumed() const { return (Data & IsConsumed); } | ||||||
3598 | ExtParameterInfo withIsConsumed(bool consumed) const { | ||||||
3599 | ExtParameterInfo copy = *this; | ||||||
3600 | if (consumed) | ||||||
3601 | copy.Data |= IsConsumed; | ||||||
3602 | else | ||||||
3603 | copy.Data &= ~IsConsumed; | ||||||
3604 | return copy; | ||||||
3605 | } | ||||||
3606 | |||||||
3607 | bool hasPassObjectSize() const { return Data & HasPassObjSize; } | ||||||
3608 | ExtParameterInfo withHasPassObjectSize() const { | ||||||
3609 | ExtParameterInfo Copy = *this; | ||||||
3610 | Copy.Data |= HasPassObjSize; | ||||||
3611 | return Copy; | ||||||
3612 | } | ||||||
3613 | |||||||
3614 | bool isNoEscape() const { return Data & IsNoEscape; } | ||||||
3615 | ExtParameterInfo withIsNoEscape(bool NoEscape) const { | ||||||
3616 | ExtParameterInfo Copy = *this; | ||||||
3617 | if (NoEscape) | ||||||
3618 | Copy.Data |= IsNoEscape; | ||||||
3619 | else | ||||||
3620 | Copy.Data &= ~IsNoEscape; | ||||||
3621 | return Copy; | ||||||
3622 | } | ||||||
3623 | |||||||
3624 | unsigned char getOpaqueValue() const { return Data; } | ||||||
3625 | static ExtParameterInfo getFromOpaqueValue(unsigned char data) { | ||||||
3626 | ExtParameterInfo result; | ||||||
3627 | result.Data = data; | ||||||
3628 | return result; | ||||||
3629 | } | ||||||
3630 | |||||||
3631 | friend bool operator==(ExtParameterInfo lhs, ExtParameterInfo rhs) { | ||||||
3632 | return lhs.Data == rhs.Data; | ||||||
3633 | } | ||||||
3634 | |||||||
3635 | friend bool operator!=(ExtParameterInfo lhs, ExtParameterInfo rhs) { | ||||||
3636 | return lhs.Data != rhs.Data; | ||||||
3637 | } | ||||||
3638 | }; | ||||||
3639 | |||||||
3640 | /// A class which abstracts out some details necessary for | ||||||
3641 | /// making a call. | ||||||
3642 | /// | ||||||
3643 | /// It is not actually used directly for storing this information in | ||||||
3644 | /// a FunctionType, although FunctionType does currently use the | ||||||
3645 | /// same bit-pattern. | ||||||
3646 | /// | ||||||
3647 | // If you add a field (say Foo), other than the obvious places (both, | ||||||
3648 | // constructors, compile failures), what you need to update is | ||||||
3649 | // * Operator== | ||||||
3650 | // * getFoo | ||||||
3651 | // * withFoo | ||||||
3652 | // * functionType. Add Foo, getFoo. | ||||||
3653 | // * ASTContext::getFooType | ||||||
3654 | // * ASTContext::mergeFunctionTypes | ||||||
3655 | // * FunctionNoProtoType::Profile | ||||||
3656 | // * FunctionProtoType::Profile | ||||||
3657 | // * TypePrinter::PrintFunctionProto | ||||||
3658 | // * AST read and write | ||||||
3659 | // * Codegen | ||||||
3660 | class ExtInfo { | ||||||
3661 | friend class FunctionType; | ||||||
3662 | |||||||
3663 | // Feel free to rearrange or add bits, but if you go over 16, you'll need to | ||||||
3664 | // adjust the Bits field below, and if you add bits, you'll need to adjust | ||||||
3665 | // Type::FunctionTypeBitfields::ExtInfo as well. | ||||||
3666 | |||||||
3667 | // | CC |noreturn|produces|nocallersavedregs|regparm|nocfcheck|cmsenscall| | ||||||
3668 | // |0 .. 4| 5 | 6 | 7 |8 .. 10| 11 | 12 | | ||||||
3669 | // | ||||||
3670 | // regparm is either 0 (no regparm attribute) or the regparm value+1. | ||||||
3671 | enum { CallConvMask = 0x1F }; | ||||||
3672 | enum { NoReturnMask = 0x20 }; | ||||||
3673 | enum { ProducesResultMask = 0x40 }; | ||||||
3674 | enum { NoCallerSavedRegsMask = 0x80 }; | ||||||
3675 | enum { | ||||||
3676 | RegParmMask = 0x700, | ||||||
3677 | RegParmOffset = 8 | ||||||
3678 | }; | ||||||
3679 | enum { NoCfCheckMask = 0x800 }; | ||||||
3680 | enum { CmseNSCallMask = 0x1000 }; | ||||||
3681 | uint16_t Bits = CC_C; | ||||||
3682 | |||||||
3683 | ExtInfo(unsigned Bits) : Bits(static_cast<uint16_t>(Bits)) {} | ||||||
3684 | |||||||
3685 | public: | ||||||
3686 | // Constructor with no defaults. Use this when you know that you | ||||||
3687 | // have all the elements (when reading an AST file for example). | ||||||
3688 | ExtInfo(bool noReturn, bool hasRegParm, unsigned regParm, CallingConv cc, | ||||||
3689 | bool producesResult, bool noCallerSavedRegs, bool NoCfCheck, | ||||||
3690 | bool cmseNSCall) { | ||||||
3691 | assert((!hasRegParm || regParm < 7) && "Invalid regparm value")(static_cast <bool> ((!hasRegParm || regParm < 7) && "Invalid regparm value") ? void (0) : __assert_fail ("(!hasRegParm || regParm < 7) && \"Invalid regparm value\"" , "clang/include/clang/AST/Type.h", 3691, __extension__ __PRETTY_FUNCTION__ )); | ||||||
3692 | Bits = ((unsigned)cc) | (noReturn ? NoReturnMask : 0) | | ||||||
3693 | (producesResult ? ProducesResultMask : 0) | | ||||||
3694 | (noCallerSavedRegs ? NoCallerSavedRegsMask : 0) | | ||||||
3695 | (hasRegParm ? ((regParm + 1) << RegParmOffset) : 0) | | ||||||
3696 | (NoCfCheck ? NoCfCheckMask : 0) | | ||||||
3697 | (cmseNSCall ? CmseNSCallMask : 0); | ||||||
3698 | } | ||||||
3699 | |||||||
3700 | // Constructor with all defaults. Use when for example creating a | ||||||
3701 | // function known to use defaults. | ||||||
3702 | ExtInfo() = default; | ||||||
3703 | |||||||
3704 | // Constructor with just the calling convention, which is an important part | ||||||
3705 | // of the canonical type. | ||||||
3706 | ExtInfo(CallingConv CC) : Bits(CC) {} | ||||||
3707 | |||||||
3708 | bool getNoReturn() const { return Bits & NoReturnMask; } | ||||||
3709 | bool getProducesResult() const { return Bits & ProducesResultMask; } | ||||||
3710 | bool getCmseNSCall() const { return Bits & CmseNSCallMask; } | ||||||
3711 | bool getNoCallerSavedRegs() const { return Bits & NoCallerSavedRegsMask; } | ||||||
3712 | bool getNoCfCheck() const { return Bits & NoCfCheckMask; } | ||||||
3713 | bool getHasRegParm() const { return ((Bits & RegParmMask) >> RegParmOffset) != 0; } | ||||||
3714 | |||||||
3715 | unsigned getRegParm() const { | ||||||
3716 | unsigned RegParm = (Bits & RegParmMask) >> RegParmOffset; | ||||||
3717 | if (RegParm > 0) | ||||||
3718 | --RegParm; | ||||||
3719 | return RegParm; | ||||||
3720 | } | ||||||
3721 | |||||||
3722 | CallingConv getCC() const { return CallingConv(Bits & CallConvMask); } | ||||||
3723 | |||||||
3724 | bool operator==(ExtInfo Other) const { | ||||||
3725 | return Bits == Other.Bits; | ||||||
3726 | } | ||||||
3727 | bool operator!=(ExtInfo Other) const { | ||||||
3728 | return Bits != Other.Bits; | ||||||
3729 | } | ||||||
3730 | |||||||
3731 | // Note that we don't have setters. That is by design, use | ||||||
3732 | // the following with methods instead of mutating these objects. | ||||||
3733 | |||||||
3734 | ExtInfo withNoReturn(bool noReturn) const { | ||||||
3735 | if (noReturn) | ||||||
3736 | return ExtInfo(Bits | NoReturnMask); | ||||||
3737 | else | ||||||
3738 | return ExtInfo(Bits & ~NoReturnMask); | ||||||
3739 | } | ||||||
3740 | |||||||
3741 | ExtInfo withProducesResult(bool producesResult) const { | ||||||
3742 | if (producesResult) | ||||||
3743 | return ExtInfo(Bits | ProducesResultMask); | ||||||
3744 | else | ||||||
3745 | return ExtInfo(Bits & ~ProducesResultMask); | ||||||
3746 | } | ||||||
3747 | |||||||
3748 | ExtInfo withCmseNSCall(bool cmseNSCall) const { | ||||||
3749 | if (cmseNSCall) | ||||||
3750 | return ExtInfo(Bits | CmseNSCallMask); | ||||||
3751 | else | ||||||
3752 | return ExtInfo(Bits & ~CmseNSCallMask); | ||||||
3753 | } | ||||||
3754 | |||||||
3755 | ExtInfo withNoCallerSavedRegs(bool noCallerSavedRegs) const { | ||||||
3756 | if (noCallerSavedRegs) | ||||||
3757 | return ExtInfo(Bits | NoCallerSavedRegsMask); | ||||||
3758 | else | ||||||
3759 | return ExtInfo(Bits & ~NoCallerSavedRegsMask); | ||||||
3760 | } | ||||||
3761 | |||||||
3762 | ExtInfo withNoCfCheck(bool noCfCheck) const { | ||||||
3763 | if (noCfCheck) | ||||||
3764 | return ExtInfo(Bits | NoCfCheckMask); | ||||||
3765 | else | ||||||
3766 | return ExtInfo(Bits & ~NoCfCheckMask); | ||||||
3767 | } | ||||||
3768 | |||||||
3769 | ExtInfo withRegParm(unsigned RegParm) const { | ||||||
3770 | assert(RegParm < 7 && "Invalid regparm value")(static_cast <bool> (RegParm < 7 && "Invalid regparm value" ) ? void (0) : __assert_fail ("RegParm < 7 && \"Invalid regparm value\"" , "clang/include/clang/AST/Type.h", 3770, __extension__ __PRETTY_FUNCTION__ )); | ||||||
3771 | return ExtInfo((Bits & ~RegParmMask) | | ||||||
3772 | ((RegParm + 1) << RegParmOffset)); | ||||||
3773 | } | ||||||
3774 | |||||||
3775 | ExtInfo withCallingConv(CallingConv cc) const { | ||||||
3776 | return ExtInfo((Bits & ~CallConvMask) | (unsigned) cc); | ||||||
3777 | } | ||||||
3778 | |||||||
3779 | void Profile(llvm::FoldingSetNodeID &ID) const { | ||||||
3780 | ID.AddInteger(Bits); | ||||||
3781 | } | ||||||
3782 | }; | ||||||
3783 | |||||||
3784 | /// A simple holder for a QualType representing a type in an | ||||||
3785 | /// exception specification. Unfortunately needed by FunctionProtoType | ||||||
3786 | /// because TrailingObjects cannot handle repeated types. | ||||||
3787 | struct ExceptionType { QualType Type; }; | ||||||
3788 | |||||||
3789 | /// A simple holder for various uncommon bits which do not fit in | ||||||
3790 | /// FunctionTypeBitfields. Aligned to alignof(void *) to maintain the | ||||||
3791 | /// alignment of subsequent objects in TrailingObjects. You must update | ||||||
3792 | /// hasExtraBitfields in FunctionProtoType after adding extra data here. | ||||||
3793 | struct alignas(void *) FunctionTypeExtraBitfields { | ||||||
3794 | /// The number of types in the exception specification. | ||||||
3795 | /// A whole unsigned is not needed here and according to | ||||||
3796 | /// [implimits] 8 bits would be enough here. | ||||||
3797 | unsigned NumExceptionType; | ||||||
3798 | }; | ||||||
3799 | |||||||
3800 | protected: | ||||||
3801 | FunctionType(TypeClass tc, QualType res, QualType Canonical, | ||||||
3802 | TypeDependence Dependence, ExtInfo Info) | ||||||
3803 | : Type(tc, Canonical, Dependence), ResultType(res) { | ||||||
3804 | FunctionTypeBits.ExtInfo = Info.Bits; | ||||||
3805 | } | ||||||
3806 | |||||||
3807 | Qualifiers getFastTypeQuals() const { | ||||||
3808 | return Qualifiers::fromFastMask(FunctionTypeBits.FastTypeQuals); | ||||||
3809 | } | ||||||
3810 | |||||||
3811 | public: | ||||||
3812 | QualType getReturnType() const { return ResultType; } | ||||||
3813 | |||||||
3814 | bool getHasRegParm() const { return getExtInfo().getHasRegParm(); } | ||||||
3815 | unsigned getRegParmType() const { return getExtInfo().getRegParm(); } | ||||||
3816 | |||||||
3817 | /// Determine whether this function type includes the GNU noreturn | ||||||
3818 | /// attribute. The C++11 [[noreturn]] attribute does not affect the function | ||||||
3819 | /// type. | ||||||
3820 | bool getNoReturnAttr() const { return getExtInfo().getNoReturn(); } | ||||||
3821 | |||||||
3822 | bool getCmseNSCallAttr() const { return getExtInfo().getCmseNSCall(); } | ||||||
3823 | CallingConv getCallConv() const { return getExtInfo().getCC(); } | ||||||
3824 | ExtInfo getExtInfo() const { return ExtInfo(FunctionTypeBits.ExtInfo); } | ||||||
3825 | |||||||
3826 | static_assert((~Qualifiers::FastMask & Qualifiers::CVRMask) == 0, | ||||||
3827 | "Const, volatile and restrict are assumed to be a subset of " | ||||||
3828 | "the fast qualifiers."); | ||||||
3829 | |||||||
3830 | bool isConst() const { return getFastTypeQuals().hasConst(); } | ||||||
3831 | bool isVolatile() const { return getFastTypeQuals().hasVolatile(); } | ||||||
3832 | bool isRestrict() const { return getFastTypeQuals().hasRestrict(); } | ||||||
3833 | |||||||
3834 | /// Determine the type of an expression that calls a function of | ||||||
3835 | /// this type. | ||||||
3836 | QualType getCallResultType(const ASTContext &Context) const { | ||||||
3837 | return getReturnType().getNonLValueExprType(Context); | ||||||
3838 | } | ||||||
3839 | |||||||
3840 | static StringRef getNameForCallConv(CallingConv CC); | ||||||
3841 | |||||||
3842 | static bool classof(const Type *T) { | ||||||
3843 | return T->getTypeClass() == FunctionNoProto || | ||||||
3844 | T->getTypeClass() == FunctionProto; | ||||||
3845 | } | ||||||
3846 | }; | ||||||
3847 | |||||||
3848 | /// Represents a K&R-style 'int foo()' function, which has | ||||||
3849 | /// no information available about its arguments. | ||||||
3850 | class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode { | ||||||
3851 | friend class ASTContext; // ASTContext creates these. | ||||||
3852 | |||||||
3853 | FunctionNoProtoType(QualType Result, QualType Canonical, ExtInfo Info) | ||||||
3854 | : FunctionType(FunctionNoProto, Result, Canonical, | ||||||
3855 | Result->getDependence() & | ||||||
3856 | ~(TypeDependence::DependentInstantiation | | ||||||
3857 | TypeDependence::UnexpandedPack), | ||||||
3858 | Info) {} | ||||||
3859 | |||||||
3860 | public: | ||||||
3861 | // No additional state past what FunctionType provides. | ||||||
3862 | |||||||
3863 | bool isSugared() const { return false; } | ||||||
3864 | QualType desugar() const { return QualType(this, 0); } | ||||||
3865 | |||||||
3866 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
3867 | Profile(ID, getReturnType(), getExtInfo()); | ||||||
3868 | } | ||||||
3869 | |||||||
3870 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType, | ||||||
3871 | ExtInfo Info) { | ||||||
3872 | Info.Profile(ID); | ||||||
3873 | ID.AddPointer(ResultType.getAsOpaquePtr()); | ||||||
3874 | } | ||||||
3875 | |||||||
3876 | static bool classof(const Type *T) { | ||||||
3877 | return T->getTypeClass() == FunctionNoProto; | ||||||
3878 | } | ||||||
3879 | }; | ||||||
3880 | |||||||
3881 | /// Represents a prototype with parameter type info, e.g. | ||||||
3882 | /// 'int foo(int)' or 'int foo(void)'. 'void' is represented as having no | ||||||
3883 | /// parameters, not as having a single void parameter. Such a type can have | ||||||
3884 | /// an exception specification, but this specification is not part of the | ||||||
3885 | /// canonical type. FunctionProtoType has several trailing objects, some of | ||||||
3886 | /// which optional. For more information about the trailing objects see | ||||||
3887 | /// the first comment inside FunctionProtoType. | ||||||
3888 | class FunctionProtoType final | ||||||
3889 | : public FunctionType, | ||||||
3890 | public llvm::FoldingSetNode, | ||||||
3891 | private llvm::TrailingObjects< | ||||||
3892 | FunctionProtoType, QualType, SourceLocation, | ||||||
3893 | FunctionType::FunctionTypeExtraBitfields, FunctionType::ExceptionType, | ||||||
3894 | Expr *, FunctionDecl *, FunctionType::ExtParameterInfo, Qualifiers> { | ||||||
3895 | friend class ASTContext; // ASTContext creates these. | ||||||
3896 | friend TrailingObjects; | ||||||
3897 | |||||||
3898 | // FunctionProtoType is followed by several trailing objects, some of | ||||||
3899 | // which optional. They are in order: | ||||||
3900 | // | ||||||
3901 | // * An array of getNumParams() QualType holding the parameter types. | ||||||
3902 | // Always present. Note that for the vast majority of FunctionProtoType, | ||||||
3903 | // these will be the only trailing objects. | ||||||
3904 | // | ||||||
3905 | // * Optionally if the function is variadic, the SourceLocation of the | ||||||
3906 | // ellipsis. | ||||||
3907 | // | ||||||
3908 | // * Optionally if some extra data is stored in FunctionTypeExtraBitfields | ||||||
3909 | // (see FunctionTypeExtraBitfields and FunctionTypeBitfields): | ||||||
3910 | // a single FunctionTypeExtraBitfields. Present if and only if | ||||||
3911 | // hasExtraBitfields() is true. | ||||||
3912 | // | ||||||
3913 | // * Optionally exactly one of: | ||||||
3914 | // * an array of getNumExceptions() ExceptionType, | ||||||
3915 | // * a single Expr *, | ||||||
3916 | // * a pair of FunctionDecl *, | ||||||
3917 | // * a single FunctionDecl * | ||||||
3918 | // used to store information about the various types of exception | ||||||
3919 | // specification. See getExceptionSpecSize for the details. | ||||||
3920 | // | ||||||
3921 | // * Optionally an array of getNumParams() ExtParameterInfo holding | ||||||
3922 | // an ExtParameterInfo for each of the parameters. Present if and | ||||||
3923 | // only if hasExtParameterInfos() is true. | ||||||
3924 | // | ||||||
3925 | // * Optionally a Qualifiers object to represent extra qualifiers that can't | ||||||
3926 | // be represented by FunctionTypeBitfields.FastTypeQuals. Present if and only | ||||||
3927 | // if hasExtQualifiers() is true. | ||||||
3928 | // | ||||||
3929 | // The optional FunctionTypeExtraBitfields has to be before the data | ||||||
3930 | // related to the exception specification since it contains the number | ||||||
3931 | // of exception types. | ||||||
3932 | // | ||||||
3933 | // We put the ExtParameterInfos last. If all were equal, it would make | ||||||
3934 | // more sense to put these before the exception specification, because | ||||||
3935 | // it's much easier to skip past them compared to the elaborate switch | ||||||
3936 | // required to skip the exception specification. However, all is not | ||||||
3937 | // equal; ExtParameterInfos are used to model very uncommon features, | ||||||
3938 | // and it's better not to burden the more common paths. | ||||||
3939 | |||||||
3940 | public: | ||||||
3941 | /// Holds information about the various types of exception specification. | ||||||
3942 | /// ExceptionSpecInfo is not stored as such in FunctionProtoType but is | ||||||
3943 | /// used to group together the various bits of information about the | ||||||
3944 | /// exception specification. | ||||||
3945 | struct ExceptionSpecInfo { | ||||||
3946 | /// The kind of exception specification this is. | ||||||
3947 | ExceptionSpecificationType Type = EST_None; | ||||||
3948 | |||||||
3949 | /// Explicitly-specified list of exception types. | ||||||
3950 | ArrayRef<QualType> Exceptions; | ||||||
3951 | |||||||
3952 | /// Noexcept expression, if this is a computed noexcept specification. | ||||||
3953 | Expr *NoexceptExpr = nullptr; | ||||||
3954 | |||||||
3955 | /// The function whose exception specification this is, for | ||||||
3956 | /// EST_Unevaluated and EST_Uninstantiated. | ||||||
3957 | FunctionDecl *SourceDecl = nullptr; | ||||||
3958 | |||||||
3959 | /// The function template whose exception specification this is instantiated | ||||||
3960 | /// from, for EST_Uninstantiated. | ||||||
3961 | FunctionDecl *SourceTemplate = nullptr; | ||||||
3962 | |||||||
3963 | ExceptionSpecInfo() = default; | ||||||
3964 | |||||||
3965 | ExceptionSpecInfo(ExceptionSpecificationType EST) : Type(EST) {} | ||||||
3966 | }; | ||||||
3967 | |||||||
3968 | /// Extra information about a function prototype. ExtProtoInfo is not | ||||||
3969 | /// stored as such in FunctionProtoType but is used to group together | ||||||
3970 | /// the various bits of extra information about a function prototype. | ||||||
3971 | struct ExtProtoInfo { | ||||||
3972 | FunctionType::ExtInfo ExtInfo; | ||||||
3973 | bool Variadic : 1; | ||||||
3974 | bool HasTrailingReturn : 1; | ||||||
3975 | Qualifiers TypeQuals; | ||||||
3976 | RefQualifierKind RefQualifier = RQ_None; | ||||||
3977 | ExceptionSpecInfo ExceptionSpec; | ||||||
3978 | const ExtParameterInfo *ExtParameterInfos = nullptr; | ||||||
3979 | SourceLocation EllipsisLoc; | ||||||
3980 | |||||||
3981 | ExtProtoInfo() : Variadic(false), HasTrailingReturn(false) {} | ||||||
3982 | |||||||
3983 | ExtProtoInfo(CallingConv CC) | ||||||
3984 | : ExtInfo(CC), Variadic(false), HasTrailingReturn(false) {} | ||||||
3985 | |||||||
3986 | ExtProtoInfo withExceptionSpec(const ExceptionSpecInfo &ESI) { | ||||||
3987 | ExtProtoInfo Result(*this); | ||||||
3988 | Result.ExceptionSpec = ESI; | ||||||
3989 | return Result; | ||||||
3990 | } | ||||||
3991 | }; | ||||||
3992 | |||||||
3993 | private: | ||||||
3994 | unsigned numTrailingObjects(OverloadToken<QualType>) const { | ||||||
3995 | return getNumParams(); | ||||||
3996 | } | ||||||
3997 | |||||||
3998 | unsigned numTrailingObjects(OverloadToken<SourceLocation>) const { | ||||||
3999 | return isVariadic(); | ||||||
4000 | } | ||||||
4001 | |||||||
4002 | unsigned numTrailingObjects(OverloadToken<FunctionTypeExtraBitfields>) const { | ||||||
4003 | return hasExtraBitfields(); | ||||||
4004 | } | ||||||
4005 | |||||||
4006 | unsigned numTrailingObjects(OverloadToken<ExceptionType>) const { | ||||||
4007 | return getExceptionSpecSize().NumExceptionType; | ||||||
4008 | } | ||||||
4009 | |||||||
4010 | unsigned numTrailingObjects(OverloadToken<Expr *>) const { | ||||||
4011 | return getExceptionSpecSize().NumExprPtr; | ||||||
4012 | } | ||||||
4013 | |||||||
4014 | unsigned numTrailingObjects(OverloadToken<FunctionDecl *>) const { | ||||||
4015 | return getExceptionSpecSize().NumFunctionDeclPtr; | ||||||
4016 | } | ||||||
4017 | |||||||
4018 | unsigned numTrailingObjects(OverloadToken<ExtParameterInfo>) const { | ||||||
4019 | return hasExtParameterInfos() ? getNumParams() : 0; | ||||||
4020 | } | ||||||
4021 | |||||||
4022 | /// Determine whether there are any argument types that | ||||||
4023 | /// contain an unexpanded parameter pack. | ||||||
4024 | static bool containsAnyUnexpandedParameterPack(const QualType *ArgArray, | ||||||
4025 | unsigned numArgs) { | ||||||
4026 | for (unsigned Idx = 0; Idx < numArgs; ++Idx) | ||||||
4027 | if (ArgArray[Idx]->containsUnexpandedParameterPack()) | ||||||
4028 | return true; | ||||||
4029 | |||||||
4030 | return false; | ||||||
4031 | } | ||||||
4032 | |||||||
4033 | FunctionProtoType(QualType result, ArrayRef<QualType> params, | ||||||
4034 | QualType canonical, const ExtProtoInfo &epi); | ||||||
4035 | |||||||
4036 | /// This struct is returned by getExceptionSpecSize and is used to | ||||||
4037 | /// translate an ExceptionSpecificationType to the number and kind | ||||||
4038 | /// of trailing objects related to the exception specification. | ||||||
4039 | struct ExceptionSpecSizeHolder { | ||||||
4040 | unsigned NumExceptionType; | ||||||
4041 | unsigned NumExprPtr; | ||||||
4042 | unsigned NumFunctionDeclPtr; | ||||||
4043 | }; | ||||||
4044 | |||||||
4045 | /// Return the number and kind of trailing objects | ||||||
4046 | /// related to the exception specification. | ||||||
4047 | static ExceptionSpecSizeHolder | ||||||
4048 | getExceptionSpecSize(ExceptionSpecificationType EST, unsigned NumExceptions) { | ||||||
4049 | switch (EST) { | ||||||
4050 | case EST_None: | ||||||
4051 | case EST_DynamicNone: | ||||||
4052 | case EST_MSAny: | ||||||
4053 | case EST_BasicNoexcept: | ||||||
4054 | case EST_Unparsed: | ||||||
4055 | case EST_NoThrow: | ||||||
4056 | return {0, 0, 0}; | ||||||
4057 | |||||||
4058 | case EST_Dynamic: | ||||||
4059 | return {NumExceptions, 0, 0}; | ||||||
4060 | |||||||
4061 | case EST_DependentNoexcept: | ||||||
4062 | case EST_NoexceptFalse: | ||||||
4063 | case EST_NoexceptTrue: | ||||||
4064 | return {0, 1, 0}; | ||||||
4065 | |||||||
4066 | case EST_Uninstantiated: | ||||||
4067 | return {0, 0, 2}; | ||||||
4068 | |||||||
4069 | case EST_Unevaluated: | ||||||
4070 | return {0, 0, 1}; | ||||||
4071 | } | ||||||
4072 | llvm_unreachable("bad exception specification kind")::llvm::llvm_unreachable_internal("bad exception specification kind" , "clang/include/clang/AST/Type.h", 4072); | ||||||
4073 | } | ||||||
4074 | |||||||
4075 | /// Return the number and kind of trailing objects | ||||||
4076 | /// related to the exception specification. | ||||||
4077 | ExceptionSpecSizeHolder getExceptionSpecSize() const { | ||||||
4078 | return getExceptionSpecSize(getExceptionSpecType(), getNumExceptions()); | ||||||
4079 | } | ||||||
4080 | |||||||
4081 | /// Whether the trailing FunctionTypeExtraBitfields is present. | ||||||
4082 | static bool hasExtraBitfields(ExceptionSpecificationType EST) { | ||||||
4083 | // If the exception spec type is EST_Dynamic then we have > 0 exception | ||||||
4084 | // types and the exact number is stored in FunctionTypeExtraBitfields. | ||||||
4085 | return EST == EST_Dynamic; | ||||||
4086 | } | ||||||
4087 | |||||||
4088 | /// Whether the trailing FunctionTypeExtraBitfields is present. | ||||||
4089 | bool hasExtraBitfields() const { | ||||||
4090 | return hasExtraBitfields(getExceptionSpecType()); | ||||||
4091 | } | ||||||
4092 | |||||||
4093 | bool hasExtQualifiers() const { | ||||||
4094 | return FunctionTypeBits.HasExtQuals; | ||||||
4095 | } | ||||||
4096 | |||||||
4097 | public: | ||||||
4098 | unsigned getNumParams() const { return FunctionTypeBits.NumParams; } | ||||||
4099 | |||||||
4100 | QualType getParamType(unsigned i) const { | ||||||
4101 | assert(i < getNumParams() && "invalid parameter index")(static_cast <bool> (i < getNumParams() && "invalid parameter index" ) ? void (0) : __assert_fail ("i < getNumParams() && \"invalid parameter index\"" , "clang/include/clang/AST/Type.h", 4101, __extension__ __PRETTY_FUNCTION__ )); | ||||||
4102 | return param_type_begin()[i]; | ||||||
4103 | } | ||||||
4104 | |||||||
4105 | ArrayRef<QualType> getParamTypes() const { | ||||||
4106 | return llvm::makeArrayRef(param_type_begin(), param_type_end()); | ||||||
4107 | } | ||||||
4108 | |||||||
4109 | ExtProtoInfo getExtProtoInfo() const { | ||||||
4110 | ExtProtoInfo EPI; | ||||||
4111 | EPI.ExtInfo = getExtInfo(); | ||||||
4112 | EPI.Variadic = isVariadic(); | ||||||
4113 | EPI.EllipsisLoc = getEllipsisLoc(); | ||||||
4114 | EPI.HasTrailingReturn = hasTrailingReturn(); | ||||||
4115 | EPI.ExceptionSpec = getExceptionSpecInfo(); | ||||||
4116 | EPI.TypeQuals = getMethodQuals(); | ||||||
4117 | EPI.RefQualifier = getRefQualifier(); | ||||||
4118 | EPI.ExtParameterInfos = getExtParameterInfosOrNull(); | ||||||
4119 | return EPI; | ||||||
4120 | } | ||||||
4121 | |||||||
4122 | /// Get the kind of exception specification on this function. | ||||||
4123 | ExceptionSpecificationType getExceptionSpecType() const { | ||||||
4124 | return static_cast<ExceptionSpecificationType>( | ||||||
4125 | FunctionTypeBits.ExceptionSpecType); | ||||||
4126 | } | ||||||
4127 | |||||||
4128 | /// Return whether this function has any kind of exception spec. | ||||||
4129 | bool hasExceptionSpec() const { return getExceptionSpecType() != EST_None; } | ||||||
4130 | |||||||
4131 | /// Return whether this function has a dynamic (throw) exception spec. | ||||||
4132 | bool hasDynamicExceptionSpec() const { | ||||||
4133 | return isDynamicExceptionSpec(getExceptionSpecType()); | ||||||
4134 | } | ||||||
4135 | |||||||
4136 | /// Return whether this function has a noexcept exception spec. | ||||||
4137 | bool hasNoexceptExceptionSpec() const { | ||||||
4138 | return isNoexceptExceptionSpec(getExceptionSpecType()); | ||||||
4139 | } | ||||||
4140 | |||||||
4141 | /// Return whether this function has a dependent exception spec. | ||||||
4142 | bool hasDependentExceptionSpec() const; | ||||||
4143 | |||||||
4144 | /// Return whether this function has an instantiation-dependent exception | ||||||
4145 | /// spec. | ||||||
4146 | bool hasInstantiationDependentExceptionSpec() const; | ||||||
4147 | |||||||
4148 | /// Return all the available information about this type's exception spec. | ||||||
4149 | ExceptionSpecInfo getExceptionSpecInfo() const { | ||||||
4150 | ExceptionSpecInfo Result; | ||||||
4151 | Result.Type = getExceptionSpecType(); | ||||||
4152 | if (Result.Type == EST_Dynamic) { | ||||||
4153 | Result.Exceptions = exceptions(); | ||||||
4154 | } else if (isComputedNoexcept(Result.Type)) { | ||||||
4155 | Result.NoexceptExpr = getNoexceptExpr(); | ||||||
4156 | } else if (Result.Type == EST_Uninstantiated) { | ||||||
4157 | Result.SourceDecl = getExceptionSpecDecl(); | ||||||
4158 | Result.SourceTemplate = getExceptionSpecTemplate(); | ||||||
4159 | } else if (Result.Type == EST_Unevaluated) { | ||||||
4160 | Result.SourceDecl = getExceptionSpecDecl(); | ||||||
4161 | } | ||||||
4162 | return Result; | ||||||
4163 | } | ||||||
4164 | |||||||
4165 | /// Return the number of types in the exception specification. | ||||||
4166 | unsigned getNumExceptions() const { | ||||||
4167 | return getExceptionSpecType() == EST_Dynamic | ||||||
4168 | ? getTrailingObjects<FunctionTypeExtraBitfields>() | ||||||
4169 | ->NumExceptionType | ||||||
4170 | : 0; | ||||||
4171 | } | ||||||
4172 | |||||||
4173 | /// Return the ith exception type, where 0 <= i < getNumExceptions(). | ||||||
4174 | QualType getExceptionType(unsigned i) const { | ||||||
4175 | assert(i < getNumExceptions() && "Invalid exception number!")(static_cast <bool> (i < getNumExceptions() && "Invalid exception number!") ? void (0) : __assert_fail ("i < getNumExceptions() && \"Invalid exception number!\"" , "clang/include/clang/AST/Type.h", 4175, __extension__ __PRETTY_FUNCTION__ )); | ||||||
4176 | return exception_begin()[i]; | ||||||
4177 | } | ||||||
4178 | |||||||
4179 | /// Return the expression inside noexcept(expression), or a null pointer | ||||||
4180 | /// if there is none (because the exception spec is not of this form). | ||||||
4181 | Expr *getNoexceptExpr() const { | ||||||
4182 | if (!isComputedNoexcept(getExceptionSpecType())) | ||||||
4183 | return nullptr; | ||||||
4184 | return *getTrailingObjects<Expr *>(); | ||||||
4185 | } | ||||||
4186 | |||||||
4187 | /// If this function type has an exception specification which hasn't | ||||||
4188 | /// been determined yet (either because it has not been evaluated or because | ||||||
4189 | /// it has not been instantiated), this is the function whose exception | ||||||
4190 | /// specification is represented by this type. | ||||||
4191 | FunctionDecl *getExceptionSpecDecl() const { | ||||||
4192 | if (getExceptionSpecType() != EST_Uninstantiated && | ||||||
4193 | getExceptionSpecType() != EST_Unevaluated) | ||||||
4194 | return nullptr; | ||||||
4195 | return getTrailingObjects<FunctionDecl *>()[0]; | ||||||
4196 | } | ||||||
4197 | |||||||
4198 | /// If this function type has an uninstantiated exception | ||||||
4199 | /// specification, this is the function whose exception specification | ||||||
4200 | /// should be instantiated to find the exception specification for | ||||||
4201 | /// this type. | ||||||
4202 | FunctionDecl *getExceptionSpecTemplate() const { | ||||||
4203 | if (getExceptionSpecType() != EST_Uninstantiated) | ||||||
4204 | return nullptr; | ||||||
4205 | return getTrailingObjects<FunctionDecl *>()[1]; | ||||||
4206 | } | ||||||
4207 | |||||||
4208 | /// Determine whether this function type has a non-throwing exception | ||||||
4209 | /// specification. | ||||||
4210 | CanThrowResult canThrow() const; | ||||||
4211 | |||||||
4212 | /// Determine whether this function type has a non-throwing exception | ||||||
4213 | /// specification. If this depends on template arguments, returns | ||||||
4214 | /// \c ResultIfDependent. | ||||||
4215 | bool isNothrow(bool ResultIfDependent = false) const { | ||||||
4216 | return ResultIfDependent ? canThrow() != CT_Can : canThrow() == CT_Cannot; | ||||||
4217 | } | ||||||
4218 | |||||||
4219 | /// Whether this function prototype is variadic. | ||||||
4220 | bool isVariadic() const { return FunctionTypeBits.Variadic; } | ||||||
4221 | |||||||
4222 | SourceLocation getEllipsisLoc() const { | ||||||
4223 | return isVariadic() ? *getTrailingObjects<SourceLocation>() | ||||||
4224 | : SourceLocation(); | ||||||
4225 | } | ||||||
4226 | |||||||
4227 | /// Determines whether this function prototype contains a | ||||||
4228 | /// parameter pack at the end. | ||||||
4229 | /// | ||||||
4230 | /// A function template whose last parameter is a parameter pack can be | ||||||
4231 | /// called with an arbitrary number of arguments, much like a variadic | ||||||
4232 | /// function. | ||||||
4233 | bool isTemplateVariadic() const; | ||||||
4234 | |||||||
4235 | /// Whether this function prototype has a trailing return type. | ||||||
4236 | bool hasTrailingReturn() const { return FunctionTypeBits.HasTrailingReturn; } | ||||||
4237 | |||||||
4238 | Qualifiers getMethodQuals() const { | ||||||
4239 | if (hasExtQualifiers()) | ||||||
4240 | return *getTrailingObjects<Qualifiers>(); | ||||||
4241 | else | ||||||
4242 | return getFastTypeQuals(); | ||||||
4243 | } | ||||||
4244 | |||||||
4245 | /// Retrieve the ref-qualifier associated with this function type. | ||||||
4246 | RefQualifierKind getRefQualifier() const { | ||||||
4247 | return static_cast<RefQualifierKind>(FunctionTypeBits.RefQualifier); | ||||||
4248 | } | ||||||
4249 | |||||||
4250 | using param_type_iterator = const QualType *; | ||||||
4251 | using param_type_range = llvm::iterator_range<param_type_iterator>; | ||||||
4252 | |||||||
4253 | param_type_range param_types() const { | ||||||
4254 | return param_type_range(param_type_begin(), param_type_end()); | ||||||
4255 | } | ||||||
4256 | |||||||
4257 | param_type_iterator param_type_begin() const { | ||||||
4258 | return getTrailingObjects<QualType>(); | ||||||
4259 | } | ||||||
4260 | |||||||
4261 | param_type_iterator param_type_end() const { | ||||||
4262 | return param_type_begin() + getNumParams(); | ||||||
4263 | } | ||||||
4264 | |||||||
4265 | using exception_iterator = const QualType *; | ||||||
4266 | |||||||
4267 | ArrayRef<QualType> exceptions() const { | ||||||
4268 | return llvm::makeArrayRef(exception_begin(), exception_end()); | ||||||
4269 | } | ||||||
4270 | |||||||
4271 | exception_iterator exception_begin() const { | ||||||
4272 | return reinterpret_cast<exception_iterator>( | ||||||
4273 | getTrailingObjects<ExceptionType>()); | ||||||
4274 | } | ||||||
4275 | |||||||
4276 | exception_iterator exception_end() const { | ||||||
4277 | return exception_begin() + getNumExceptions(); | ||||||
4278 | } | ||||||
4279 | |||||||
4280 | /// Is there any interesting extra information for any of the parameters | ||||||
4281 | /// of this function type? | ||||||
4282 | bool hasExtParameterInfos() const { | ||||||
4283 | return FunctionTypeBits.HasExtParameterInfos; | ||||||
4284 | } | ||||||
4285 | |||||||
4286 | ArrayRef<ExtParameterInfo> getExtParameterInfos() const { | ||||||
4287 | assert(hasExtParameterInfos())(static_cast <bool> (hasExtParameterInfos()) ? void (0) : __assert_fail ("hasExtParameterInfos()", "clang/include/clang/AST/Type.h" , 4287, __extension__ __PRETTY_FUNCTION__)); | ||||||
4288 | return ArrayRef<ExtParameterInfo>(getTrailingObjects<ExtParameterInfo>(), | ||||||
4289 | getNumParams()); | ||||||
4290 | } | ||||||
4291 | |||||||
4292 | /// Return a pointer to the beginning of the array of extra parameter | ||||||
4293 | /// information, if present, or else null if none of the parameters | ||||||
4294 | /// carry it. This is equivalent to getExtProtoInfo().ExtParameterInfos. | ||||||
4295 | const ExtParameterInfo *getExtParameterInfosOrNull() const { | ||||||
4296 | if (!hasExtParameterInfos()) | ||||||
4297 | return nullptr; | ||||||
4298 | return getTrailingObjects<ExtParameterInfo>(); | ||||||
4299 | } | ||||||
4300 | |||||||
4301 | ExtParameterInfo getExtParameterInfo(unsigned I) const { | ||||||
4302 | assert(I < getNumParams() && "parameter index out of range")(static_cast <bool> (I < getNumParams() && "parameter index out of range" ) ? void (0) : __assert_fail ("I < getNumParams() && \"parameter index out of range\"" , "clang/include/clang/AST/Type.h", 4302, __extension__ __PRETTY_FUNCTION__ )); | ||||||
4303 | if (hasExtParameterInfos()) | ||||||
4304 | return getTrailingObjects<ExtParameterInfo>()[I]; | ||||||
4305 | return ExtParameterInfo(); | ||||||
4306 | } | ||||||
4307 | |||||||
4308 | ParameterABI getParameterABI(unsigned I) const { | ||||||
4309 | assert(I < getNumParams() && "parameter index out of range")(static_cast <bool> (I < getNumParams() && "parameter index out of range" ) ? void (0) : __assert_fail ("I < getNumParams() && \"parameter index out of range\"" , "clang/include/clang/AST/Type.h", 4309, __extension__ __PRETTY_FUNCTION__ )); | ||||||
4310 | if (hasExtParameterInfos()) | ||||||
4311 | return getTrailingObjects<ExtParameterInfo>()[I].getABI(); | ||||||
4312 | return ParameterABI::Ordinary; | ||||||
4313 | } | ||||||
4314 | |||||||
4315 | bool isParamConsumed(unsigned I) const { | ||||||
4316 | assert(I < getNumParams() && "parameter index out of range")(static_cast <bool> (I < getNumParams() && "parameter index out of range" ) ? void (0) : __assert_fail ("I < getNumParams() && \"parameter index out of range\"" , "clang/include/clang/AST/Type.h", 4316, __extension__ __PRETTY_FUNCTION__ )); | ||||||
4317 | if (hasExtParameterInfos()) | ||||||
4318 | return getTrailingObjects<ExtParameterInfo>()[I].isConsumed(); | ||||||
4319 | return false; | ||||||
4320 | } | ||||||
4321 | |||||||
4322 | bool isSugared() const { return false; } | ||||||
4323 | QualType desugar() const { return QualType(this, 0); } | ||||||
4324 | |||||||
4325 | void printExceptionSpecification(raw_ostream &OS, | ||||||
4326 | const PrintingPolicy &Policy) const; | ||||||
4327 | |||||||
4328 | static bool classof(const Type *T) { | ||||||
4329 | return T->getTypeClass() == FunctionProto; | ||||||
4330 | } | ||||||
4331 | |||||||
4332 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx); | ||||||
4333 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Result, | ||||||
4334 | param_type_iterator ArgTys, unsigned NumArgs, | ||||||
4335 | const ExtProtoInfo &EPI, const ASTContext &Context, | ||||||
4336 | bool Canonical); | ||||||
4337 | }; | ||||||
4338 | |||||||
4339 | /// Represents the dependent type named by a dependently-scoped | ||||||
4340 | /// typename using declaration, e.g. | ||||||
4341 | /// using typename Base<T>::foo; | ||||||
4342 | /// | ||||||
4343 | /// Template instantiation turns these into the underlying type. | ||||||
4344 | class UnresolvedUsingType : public Type { | ||||||
4345 | friend class ASTContext; // ASTContext creates these. | ||||||
4346 | |||||||
4347 | UnresolvedUsingTypenameDecl *Decl; | ||||||
4348 | |||||||
4349 | UnresolvedUsingType(const UnresolvedUsingTypenameDecl *D) | ||||||
4350 | : Type(UnresolvedUsing, QualType(), | ||||||
4351 | TypeDependence::DependentInstantiation), | ||||||
4352 | Decl(const_cast<UnresolvedUsingTypenameDecl *>(D)) {} | ||||||
4353 | |||||||
4354 | public: | ||||||
4355 | UnresolvedUsingTypenameDecl *getDecl() const { return Decl; } | ||||||
4356 | |||||||
4357 | bool isSugared() const { return false; } | ||||||
4358 | QualType desugar() const { return QualType(this, 0); } | ||||||
4359 | |||||||
4360 | static bool classof(const Type *T) { | ||||||
4361 | return T->getTypeClass() == UnresolvedUsing; | ||||||
4362 | } | ||||||
4363 | |||||||
4364 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
4365 | return Profile(ID, Decl); | ||||||
4366 | } | ||||||
4367 | |||||||
4368 | static void Profile(llvm::FoldingSetNodeID &ID, | ||||||
4369 | UnresolvedUsingTypenameDecl *D) { | ||||||
4370 | ID.AddPointer(D); | ||||||
4371 | } | ||||||
4372 | }; | ||||||
4373 | |||||||
4374 | class UsingType : public Type, public llvm::FoldingSetNode { | ||||||
4375 | UsingShadowDecl *Found; | ||||||
4376 | friend class ASTContext; // ASTContext creates these. | ||||||
4377 | |||||||
4378 | UsingType(const UsingShadowDecl *Found, QualType Underlying, QualType Canon); | ||||||
4379 | |||||||
4380 | public: | ||||||
4381 | UsingShadowDecl *getFoundDecl() const { return Found; } | ||||||
4382 | QualType getUnderlyingType() const; | ||||||
4383 | |||||||
4384 | bool isSugared() const { return true; } | ||||||
4385 | QualType desugar() const { return getUnderlyingType(); } | ||||||
4386 | |||||||
4387 | void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, Found); } | ||||||
4388 | static void Profile(llvm::FoldingSetNodeID &ID, | ||||||
4389 | const UsingShadowDecl *Found) { | ||||||
4390 | ID.AddPointer(Found); | ||||||
4391 | } | ||||||
4392 | static bool classof(const Type *T) { return T->getTypeClass() == Using; } | ||||||
4393 | }; | ||||||
4394 | |||||||
4395 | class TypedefType : public Type { | ||||||
4396 | TypedefNameDecl *Decl; | ||||||
4397 | |||||||
4398 | private: | ||||||
4399 | friend class ASTContext; // ASTContext creates these. | ||||||
4400 | |||||||
4401 | TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType underlying, | ||||||
4402 | QualType can); | ||||||
4403 | |||||||
4404 | public: | ||||||
4405 | TypedefNameDecl *getDecl() const { return Decl; } | ||||||
4406 | |||||||
4407 | bool isSugared() const { return true; } | ||||||
4408 | QualType desugar() const; | ||||||
4409 | |||||||
4410 | static bool classof(const Type *T) { return T->getTypeClass() == Typedef; } | ||||||
4411 | }; | ||||||
4412 | |||||||
4413 | /// Sugar type that represents a type that was qualified by a qualifier written | ||||||
4414 | /// as a macro invocation. | ||||||
4415 | class MacroQualifiedType : public Type { | ||||||
4416 | friend class ASTContext; // ASTContext creates these. | ||||||
4417 | |||||||
4418 | QualType UnderlyingTy; | ||||||
4419 | const IdentifierInfo *MacroII; | ||||||
4420 | |||||||
4421 | MacroQualifiedType(QualType UnderlyingTy, QualType CanonTy, | ||||||
4422 | const IdentifierInfo *MacroII) | ||||||
4423 | : Type(MacroQualified, CanonTy, UnderlyingTy->getDependence()), | ||||||
4424 | UnderlyingTy(UnderlyingTy), MacroII(MacroII) { | ||||||
4425 | assert(isa<AttributedType>(UnderlyingTy) &&(static_cast <bool> (isa<AttributedType>(UnderlyingTy ) && "Expected a macro qualified type to only wrap attributed types." ) ? void (0) : __assert_fail ("isa<AttributedType>(UnderlyingTy) && \"Expected a macro qualified type to only wrap attributed types.\"" , "clang/include/clang/AST/Type.h", 4426, __extension__ __PRETTY_FUNCTION__ )) | ||||||
4426 | "Expected a macro qualified type to only wrap attributed types.")(static_cast <bool> (isa<AttributedType>(UnderlyingTy ) && "Expected a macro qualified type to only wrap attributed types." ) ? void (0) : __assert_fail ("isa<AttributedType>(UnderlyingTy) && \"Expected a macro qualified type to only wrap attributed types.\"" , "clang/include/clang/AST/Type.h", 4426, __extension__ __PRETTY_FUNCTION__ )); | ||||||
4427 | } | ||||||
4428 | |||||||
4429 | public: | ||||||
4430 | const IdentifierInfo *getMacroIdentifier() const { return MacroII; } | ||||||
4431 | QualType getUnderlyingType() const { return UnderlyingTy; } | ||||||
4432 | |||||||
4433 | /// Return this attributed type's modified type with no qualifiers attached to | ||||||
4434 | /// it. | ||||||
4435 | QualType getModifiedType() const; | ||||||
4436 | |||||||
4437 | bool isSugared() const { return true; } | ||||||
4438 | QualType desugar() const; | ||||||
4439 | |||||||
4440 | static bool classof(const Type *T) { | ||||||
4441 | return T->getTypeClass() == MacroQualified; | ||||||
4442 | } | ||||||
4443 | }; | ||||||
4444 | |||||||
4445 | /// Represents a `typeof` (or __typeof__) expression (a GCC extension). | ||||||
4446 | class TypeOfExprType : public Type { | ||||||
4447 | Expr *TOExpr; | ||||||
4448 | |||||||
4449 | protected: | ||||||
4450 | friend class ASTContext; // ASTContext creates these. | ||||||
4451 | |||||||
4452 | TypeOfExprType(Expr *E, QualType can = QualType()); | ||||||
4453 | |||||||
4454 | public: | ||||||
4455 | Expr *getUnderlyingExpr() const { return TOExpr; } | ||||||
4456 | |||||||
4457 | /// Remove a single level of sugar. | ||||||
4458 | QualType desugar() const; | ||||||
4459 | |||||||
4460 | /// Returns whether this type directly provides sugar. | ||||||
4461 | bool isSugared() const; | ||||||
4462 | |||||||
4463 | static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; } | ||||||
4464 | }; | ||||||
4465 | |||||||
4466 | /// Internal representation of canonical, dependent | ||||||
4467 | /// `typeof(expr)` types. | ||||||
4468 | /// | ||||||
4469 | /// This class is used internally by the ASTContext to manage | ||||||
4470 | /// canonical, dependent types, only. Clients will only see instances | ||||||
4471 | /// of this class via TypeOfExprType nodes. | ||||||
4472 | class DependentTypeOfExprType | ||||||
4473 | : public TypeOfExprType, public llvm::FoldingSetNode { | ||||||
4474 | const ASTContext &Context; | ||||||
4475 | |||||||
4476 | public: | ||||||
4477 | DependentTypeOfExprType(const ASTContext &Context, Expr *E) | ||||||
4478 | : TypeOfExprType(E), Context(Context) {} | ||||||
4479 | |||||||
4480 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
4481 | Profile(ID, Context, getUnderlyingExpr()); | ||||||
4482 | } | ||||||
4483 | |||||||
4484 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, | ||||||
4485 | Expr *E); | ||||||
4486 | }; | ||||||
4487 | |||||||
4488 | /// Represents `typeof(type)`, a GCC extension. | ||||||
4489 | class TypeOfType : public Type { | ||||||
4490 | friend class ASTContext; // ASTContext creates these. | ||||||
4491 | |||||||
4492 | QualType TOType; | ||||||
4493 | |||||||
4494 | TypeOfType(QualType T, QualType can) | ||||||
4495 | : Type(TypeOf, can, T->getDependence()), TOType(T) { | ||||||
4496 | assert(!isa<TypedefType>(can) && "Invalid canonical type")(static_cast <bool> (!isa<TypedefType>(can) && "Invalid canonical type") ? void (0) : __assert_fail ("!isa<TypedefType>(can) && \"Invalid canonical type\"" , "clang/include/clang/AST/Type.h", 4496, __extension__ __PRETTY_FUNCTION__ )); | ||||||
4497 | } | ||||||
4498 | |||||||
4499 | public: | ||||||
4500 | QualType getUnderlyingType() const { return TOType; } | ||||||
4501 | |||||||
4502 | /// Remove a single level of sugar. | ||||||
4503 | QualType desugar() const { return getUnderlyingType(); } | ||||||
4504 | |||||||
4505 | /// Returns whether this type directly provides sugar. | ||||||
4506 | bool isSugared() const { return true; } | ||||||
4507 | |||||||
4508 | static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; } | ||||||
4509 | }; | ||||||
4510 | |||||||
4511 | /// Represents the type `decltype(expr)` (C++11). | ||||||
4512 | class DecltypeType : public Type { | ||||||
4513 | Expr *E; | ||||||
4514 | QualType UnderlyingType; | ||||||
4515 | |||||||
4516 | protected: | ||||||
4517 | friend class ASTContext; // ASTContext creates these. | ||||||
4518 | |||||||
4519 | DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType()); | ||||||
4520 | |||||||
4521 | public: | ||||||
4522 | Expr *getUnderlyingExpr() const { return E; } | ||||||
4523 | QualType getUnderlyingType() const { return UnderlyingType; } | ||||||
4524 | |||||||
4525 | /// Remove a single level of sugar. | ||||||
4526 | QualType desugar() const; | ||||||
4527 | |||||||
4528 | /// Returns whether this type directly provides sugar. | ||||||
4529 | bool isSugared() const; | ||||||
4530 | |||||||
4531 | static bool classof(const Type *T) { return T->getTypeClass() == Decltype; } | ||||||
4532 | }; | ||||||
4533 | |||||||
4534 | /// Internal representation of canonical, dependent | ||||||
4535 | /// decltype(expr) types. | ||||||
4536 | /// | ||||||
4537 | /// This class is used internally by the ASTContext to manage | ||||||
4538 | /// canonical, dependent types, only. Clients will only see instances | ||||||
4539 | /// of this class via DecltypeType nodes. | ||||||
4540 | class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode { | ||||||
4541 | const ASTContext &Context; | ||||||
4542 | |||||||
4543 | public: | ||||||
4544 | DependentDecltypeType(const ASTContext &Context, Expr *E); | ||||||
4545 | |||||||
4546 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
4547 | Profile(ID, Context, getUnderlyingExpr()); | ||||||
4548 | } | ||||||
4549 | |||||||
4550 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, | ||||||
4551 | Expr *E); | ||||||
4552 | }; | ||||||
4553 | |||||||
4554 | /// A unary type transform, which is a type constructed from another. | ||||||
4555 | class UnaryTransformType : public Type { | ||||||
4556 | public: | ||||||
4557 | enum UTTKind { | ||||||
4558 | EnumUnderlyingType | ||||||
4559 | }; | ||||||
4560 | |||||||
4561 | private: | ||||||
4562 | /// The untransformed type. | ||||||
4563 | QualType BaseType; | ||||||
4564 | |||||||
4565 | /// The transformed type if not dependent, otherwise the same as BaseType. | ||||||
4566 | QualType UnderlyingType; | ||||||
4567 | |||||||
4568 | UTTKind UKind; | ||||||
4569 | |||||||
4570 | protected: | ||||||
4571 | friend class ASTContext; | ||||||
4572 | |||||||
4573 | UnaryTransformType(QualType BaseTy, QualType UnderlyingTy, UTTKind UKind, | ||||||
4574 | QualType CanonicalTy); | ||||||
4575 | |||||||
4576 | public: | ||||||
4577 | bool isSugared() const { return !isDependentType(); } | ||||||
4578 | QualType desugar() const { return UnderlyingType; } | ||||||
4579 | |||||||
4580 | QualType getUnderlyingType() const { return UnderlyingType; } | ||||||
4581 | QualType getBaseType() const { return BaseType; } | ||||||
4582 | |||||||
4583 | UTTKind getUTTKind() const { return UKind; } | ||||||
4584 | |||||||
4585 | static bool classof(const Type *T) { | ||||||
4586 | return T->getTypeClass() == UnaryTransform; | ||||||
4587 | } | ||||||
4588 | }; | ||||||
4589 | |||||||
4590 | /// Internal representation of canonical, dependent | ||||||
4591 | /// __underlying_type(type) types. | ||||||
4592 | /// | ||||||
4593 | /// This class is used internally by the ASTContext to manage | ||||||
4594 | /// canonical, dependent types, only. Clients will only see instances | ||||||
4595 | /// of this class via UnaryTransformType nodes. | ||||||
4596 | class DependentUnaryTransformType : public UnaryTransformType, | ||||||
4597 | public llvm::FoldingSetNode { | ||||||
4598 | public: | ||||||
4599 | DependentUnaryTransformType(const ASTContext &C, QualType BaseType, | ||||||
4600 | UTTKind UKind); | ||||||
4601 | |||||||
4602 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
4603 | Profile(ID, getBaseType(), getUTTKind()); | ||||||
4604 | } | ||||||
4605 | |||||||
4606 | static void Profile(llvm::FoldingSetNodeID &ID, QualType BaseType, | ||||||
4607 | UTTKind UKind) { | ||||||
4608 | ID.AddPointer(BaseType.getAsOpaquePtr()); | ||||||
4609 | ID.AddInteger((unsigned)UKind); | ||||||
4610 | } | ||||||
4611 | }; | ||||||
4612 | |||||||
4613 | class TagType : public Type { | ||||||
4614 | friend class ASTReader; | ||||||
4615 | template <class T> friend class serialization::AbstractTypeReader; | ||||||
4616 | |||||||
4617 | /// Stores the TagDecl associated with this type. The decl may point to any | ||||||
4618 | /// TagDecl that declares the entity. | ||||||
4619 | TagDecl *decl; | ||||||
4620 | |||||||
4621 | protected: | ||||||
4622 | TagType(TypeClass TC, const TagDecl *D, QualType can); | ||||||
4623 | |||||||
4624 | public: | ||||||
4625 | TagDecl *getDecl() const; | ||||||
4626 | |||||||
4627 | /// Determines whether this type is in the process of being defined. | ||||||
4628 | bool isBeingDefined() const; | ||||||
4629 | |||||||
4630 | static bool classof(const Type *T) { | ||||||
4631 | return T->getTypeClass() == Enum || T->getTypeClass() == Record; | ||||||
4632 | } | ||||||
4633 | }; | ||||||
4634 | |||||||
4635 | /// A helper class that allows the use of isa/cast/dyncast | ||||||
4636 | /// to detect TagType objects of structs/unions/classes. | ||||||
4637 | class RecordType : public TagType { | ||||||
4638 | protected: | ||||||
4639 | friend class ASTContext; // ASTContext creates these. | ||||||
4640 | |||||||
4641 | explicit RecordType(const RecordDecl *D) | ||||||
4642 | : TagType(Record, reinterpret_cast<const TagDecl*>(D), QualType()) {} | ||||||
4643 | explicit RecordType(TypeClass TC, RecordDecl *D) | ||||||
4644 | : TagType(TC, reinterpret_cast<const TagDecl*>(D), QualType()) {} | ||||||
4645 | |||||||
4646 | public: | ||||||
4647 | RecordDecl *getDecl() const { | ||||||
4648 | return reinterpret_cast<RecordDecl*>(TagType::getDecl()); | ||||||
4649 | } | ||||||
4650 | |||||||
4651 | /// Recursively check all fields in the record for const-ness. If any field | ||||||
4652 | /// is declared const, return true. Otherwise, return false. | ||||||
4653 | bool hasConstFields() const; | ||||||
4654 | |||||||
4655 | bool isSugared() const { return false; } | ||||||
4656 | QualType desugar() const { return QualType(this, 0); } | ||||||
4657 | |||||||
4658 | static bool classof(const Type *T) { return T->getTypeClass() == Record; } | ||||||
4659 | }; | ||||||
4660 | |||||||
4661 | /// A helper class that allows the use of isa/cast/dyncast | ||||||
4662 | /// to detect TagType objects of enums. | ||||||
4663 | class EnumType : public TagType { | ||||||
4664 | friend class ASTContext; // ASTContext creates these. | ||||||
4665 | |||||||
4666 | explicit EnumType(const EnumDecl *D) | ||||||
4667 | : TagType(Enum, reinterpret_cast<const TagDecl*>(D), QualType()) {} | ||||||
4668 | |||||||
4669 | public: | ||||||
4670 | EnumDecl *getDecl() const { | ||||||
4671 | return reinterpret_cast<EnumDecl*>(TagType::getDecl()); | ||||||
4672 | } | ||||||
4673 | |||||||
4674 | bool isSugared() const { return false; } | ||||||
4675 | QualType desugar() const { return QualType(this, 0); } | ||||||
4676 | |||||||
4677 | static bool classof(const Type *T) { return T->getTypeClass() == Enum; } | ||||||
4678 | }; | ||||||
4679 | |||||||
4680 | /// An attributed type is a type to which a type attribute has been applied. | ||||||
4681 | /// | ||||||
4682 | /// The "modified type" is the fully-sugared type to which the attributed | ||||||
4683 | /// type was applied; generally it is not canonically equivalent to the | ||||||
4684 | /// attributed type. The "equivalent type" is the minimally-desugared type | ||||||
4685 | /// which the type is canonically equivalent to. | ||||||
4686 | /// | ||||||
4687 | /// For example, in the following attributed type: | ||||||
4688 | /// int32_t __attribute__((vector_size(16))) | ||||||
4689 | /// - the modified type is the TypedefType for int32_t | ||||||
4690 | /// - the equivalent type is VectorType(16, int32_t) | ||||||
4691 | /// - the canonical type is VectorType(16, int) | ||||||
4692 | class AttributedType : public Type, public llvm::FoldingSetNode { | ||||||
4693 | public: | ||||||
4694 | using Kind = attr::Kind; | ||||||
4695 | |||||||
4696 | private: | ||||||
4697 | friend class ASTContext; // ASTContext creates these | ||||||
4698 | |||||||
4699 | QualType ModifiedType; | ||||||
4700 | QualType EquivalentType; | ||||||
4701 | |||||||
4702 | AttributedType(QualType canon, attr::Kind attrKind, QualType modified, | ||||||
4703 | QualType equivalent) | ||||||
4704 | : Type(Attributed, canon, equivalent->getDependence()), | ||||||
4705 | ModifiedType(modified), EquivalentType(equivalent) { | ||||||
4706 | AttributedTypeBits.AttrKind = attrKind; | ||||||
4707 | } | ||||||
4708 | |||||||
4709 | public: | ||||||
4710 | Kind getAttrKind() const { | ||||||
4711 | return static_cast<Kind>(AttributedTypeBits.AttrKind); | ||||||
4712 | } | ||||||
4713 | |||||||
4714 | QualType getModifiedType() const { return ModifiedType; } | ||||||
4715 | QualType getEquivalentType() const { return EquivalentType; } | ||||||
4716 | |||||||
4717 | bool isSugared() const { return true; } | ||||||
4718 | QualType desugar() const { return getEquivalentType(); } | ||||||
4719 | |||||||
4720 | /// Does this attribute behave like a type qualifier? | ||||||
4721 | /// | ||||||
4722 | /// A type qualifier adjusts a type to provide specialized rules for | ||||||
4723 | /// a specific object, like the standard const and volatile qualifiers. | ||||||
4724 | /// This includes attributes controlling things like nullability, | ||||||
4725 | /// address spaces, and ARC ownership. The value of the object is still | ||||||
4726 | /// largely described by the modified type. | ||||||
4727 | /// | ||||||
4728 | /// In contrast, many type attributes "rewrite" their modified type to | ||||||
4729 | /// produce a fundamentally different type, not necessarily related in any | ||||||
4730 | /// formalizable way to the original type. For example, calling convention | ||||||
4731 | /// and vector attributes are not simple type qualifiers. | ||||||
4732 | /// | ||||||
4733 | /// Type qualifiers are often, but not always, reflected in the canonical | ||||||
4734 | /// type. | ||||||
4735 | bool isQualifier() const; | ||||||
4736 | |||||||
4737 | bool isMSTypeSpec() const; | ||||||
4738 | |||||||
4739 | bool isCallingConv() const; | ||||||
4740 | |||||||
4741 | llvm::Optional<NullabilityKind> getImmediateNullability() const; | ||||||
4742 | |||||||
4743 | /// Retrieve the attribute kind corresponding to the given | ||||||
4744 | /// nullability kind. | ||||||
4745 | static Kind getNullabilityAttrKind(NullabilityKind kind) { | ||||||
4746 | switch (kind) { | ||||||
4747 | case NullabilityKind::NonNull: | ||||||
4748 | return attr::TypeNonNull; | ||||||
4749 | |||||||
4750 | case NullabilityKind::Nullable: | ||||||
4751 | return attr::TypeNullable; | ||||||
4752 | |||||||
4753 | case NullabilityKind::NullableResult: | ||||||
4754 | return attr::TypeNullableResult; | ||||||
4755 | |||||||
4756 | case NullabilityKind::Unspecified: | ||||||
4757 | return attr::TypeNullUnspecified; | ||||||
4758 | } | ||||||
4759 | llvm_unreachable("Unknown nullability kind.")::llvm::llvm_unreachable_internal("Unknown nullability kind." , "clang/include/clang/AST/Type.h", 4759); | ||||||
4760 | } | ||||||
4761 | |||||||
4762 | /// Strip off the top-level nullability annotation on the given | ||||||
4763 | /// type, if it's there. | ||||||
4764 | /// | ||||||
4765 | /// \param T The type to strip. If the type is exactly an | ||||||
4766 | /// AttributedType specifying nullability (without looking through | ||||||
4767 | /// type sugar), the nullability is returned and this type changed | ||||||
4768 | /// to the underlying modified type. | ||||||
4769 | /// | ||||||
4770 | /// \returns the top-level nullability, if present. | ||||||
4771 | static Optional<NullabilityKind> stripOuterNullability(QualType &T); | ||||||
4772 | |||||||
4773 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
4774 | Profile(ID, getAttrKind(), ModifiedType, EquivalentType); | ||||||
4775 | } | ||||||
4776 | |||||||
4777 | static void Profile(llvm::FoldingSetNodeID &ID, Kind attrKind, | ||||||
4778 | QualType modified, QualType equivalent) { | ||||||
4779 | ID.AddInteger(attrKind); | ||||||
4780 | ID.AddPointer(modified.getAsOpaquePtr()); | ||||||
4781 | ID.AddPointer(equivalent.getAsOpaquePtr()); | ||||||
4782 | } | ||||||
4783 | |||||||
4784 | static bool classof(const Type *T) { | ||||||
4785 | return T->getTypeClass() == Attributed; | ||||||
4786 | } | ||||||
4787 | }; | ||||||
4788 | |||||||
4789 | class TemplateTypeParmType : public Type, public llvm::FoldingSetNode { | ||||||
4790 | friend class ASTContext; // ASTContext creates these | ||||||
4791 | |||||||
4792 | // Helper data collector for canonical types. | ||||||
4793 | struct CanonicalTTPTInfo { | ||||||
4794 | unsigned Depth : 15; | ||||||
4795 | unsigned ParameterPack : 1; | ||||||
4796 | unsigned Index : 16; | ||||||
4797 | }; | ||||||
4798 | |||||||
4799 | union { | ||||||
4800 | // Info for the canonical type. | ||||||
4801 | CanonicalTTPTInfo CanTTPTInfo; | ||||||
4802 | |||||||
4803 | // Info for the non-canonical type. | ||||||
4804 | TemplateTypeParmDecl *TTPDecl; | ||||||
4805 | }; | ||||||
4806 | |||||||
4807 | /// Build a non-canonical type. | ||||||
4808 | TemplateTypeParmType(TemplateTypeParmDecl *TTPDecl, QualType Canon) | ||||||
4809 | : Type(TemplateTypeParm, Canon, | ||||||
4810 | TypeDependence::DependentInstantiation | | ||||||
4811 | (Canon->getDependence() & TypeDependence::UnexpandedPack)), | ||||||
4812 | TTPDecl(TTPDecl) {} | ||||||
4813 | |||||||
4814 | /// Build the canonical type. | ||||||
4815 | TemplateTypeParmType(unsigned D, unsigned I, bool PP) | ||||||
4816 | : Type(TemplateTypeParm, QualType(this, 0), | ||||||
4817 | TypeDependence::DependentInstantiation | | ||||||
4818 | (PP ? TypeDependence::UnexpandedPack : TypeDependence::None)) { | ||||||
4819 | CanTTPTInfo.Depth = D; | ||||||
4820 | CanTTPTInfo.Index = I; | ||||||
4821 | CanTTPTInfo.ParameterPack = PP; | ||||||
4822 | } | ||||||
4823 | |||||||
4824 | const CanonicalTTPTInfo& getCanTTPTInfo() const { | ||||||
4825 | QualType Can = getCanonicalTypeInternal(); | ||||||
4826 | return Can->castAs<TemplateTypeParmType>()->CanTTPTInfo; | ||||||
4827 | } | ||||||
4828 | |||||||
4829 | public: | ||||||
4830 | unsigned getDepth() const { return getCanTTPTInfo().Depth; } | ||||||
4831 | unsigned getIndex() const { return getCanTTPTInfo().Index; } | ||||||
4832 | bool isParameterPack() const { return getCanTTPTInfo().ParameterPack; } | ||||||
4833 | |||||||
4834 | TemplateTypeParmDecl *getDecl() const { | ||||||
4835 | return isCanonicalUnqualified() ? nullptr : TTPDecl; | ||||||
4836 | } | ||||||
4837 | |||||||
4838 | IdentifierInfo *getIdentifier() const; | ||||||
4839 | |||||||
4840 | bool isSugared() const { return false; } | ||||||
4841 | QualType desugar() const { return QualType(this, 0); } | ||||||
4842 | |||||||
4843 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
4844 | Profile(ID, getDepth(), getIndex(), isParameterPack(), getDecl()); | ||||||
4845 | } | ||||||
4846 | |||||||
4847 | static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth, | ||||||
4848 | unsigned Index, bool ParameterPack, | ||||||
4849 | TemplateTypeParmDecl *TTPDecl) { | ||||||
4850 | ID.AddInteger(Depth); | ||||||
4851 | ID.AddInteger(Index); | ||||||
4852 | ID.AddBoolean(ParameterPack); | ||||||
4853 | ID.AddPointer(TTPDecl); | ||||||
4854 | } | ||||||
4855 | |||||||
4856 | static bool classof(const Type *T) { | ||||||
4857 | return T->getTypeClass() == TemplateTypeParm; | ||||||
4858 | } | ||||||
4859 | }; | ||||||
4860 | |||||||
4861 | /// Represents the result of substituting a type for a template | ||||||
4862 | /// type parameter. | ||||||
4863 | /// | ||||||
4864 | /// Within an instantiated template, all template type parameters have | ||||||
4865 | /// been replaced with these. They are used solely to record that a | ||||||
4866 | /// type was originally written as a template type parameter; | ||||||
4867 | /// therefore they are never canonical. | ||||||
4868 | class SubstTemplateTypeParmType : public Type, public llvm::FoldingSetNode { | ||||||
4869 | friend class ASTContext; | ||||||
4870 | |||||||
4871 | // The original type parameter. | ||||||
4872 | const TemplateTypeParmType *Replaced; | ||||||
4873 | |||||||
4874 | SubstTemplateTypeParmType(const TemplateTypeParmType *Param, QualType Canon) | ||||||
4875 | : Type(SubstTemplateTypeParm, Canon, Canon->getDependence()), | ||||||
4876 | Replaced(Param) {} | ||||||
4877 | |||||||
4878 | public: | ||||||
4879 | /// Gets the template parameter that was substituted for. | ||||||
4880 | const TemplateTypeParmType *getReplacedParameter() const { | ||||||
4881 | return Replaced; | ||||||
4882 | } | ||||||
4883 | |||||||
4884 | /// Gets the type that was substituted for the template | ||||||
4885 | /// parameter. | ||||||
4886 | QualType getReplacementType() const { | ||||||
4887 | return getCanonicalTypeInternal(); | ||||||
4888 | } | ||||||
4889 | |||||||
4890 | bool isSugared() const { return true; } | ||||||
4891 | QualType desugar() const { return getReplacementType(); } | ||||||
4892 | |||||||
4893 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
4894 | Profile(ID, getReplacedParameter(), getReplacementType()); | ||||||
4895 | } | ||||||
4896 | |||||||
4897 | static void Profile(llvm::FoldingSetNodeID &ID, | ||||||
4898 | const TemplateTypeParmType *Replaced, | ||||||
4899 | QualType Replacement) { | ||||||
4900 | ID.AddPointer(Replaced); | ||||||
4901 | ID.AddPointer(Replacement.getAsOpaquePtr()); | ||||||
4902 | } | ||||||
4903 | |||||||
4904 | static bool classof(const Type *T) { | ||||||
4905 | return T->getTypeClass() == SubstTemplateTypeParm; | ||||||
4906 | } | ||||||
4907 | }; | ||||||
4908 | |||||||
4909 | /// Represents the result of substituting a set of types for a template | ||||||
4910 | /// type parameter pack. | ||||||
4911 | /// | ||||||
4912 | /// When a pack expansion in the source code contains multiple parameter packs | ||||||
4913 | /// and those parameter packs correspond to different levels of template | ||||||
4914 | /// parameter lists, this type node is used to represent a template type | ||||||
4915 | /// parameter pack from an outer level, which has already had its argument pack | ||||||
4916 | /// substituted but that still lives within a pack expansion that itself | ||||||
4917 | /// could not be instantiated. When actually performing a substitution into | ||||||
4918 | /// that pack expansion (e.g., when all template parameters have corresponding | ||||||
4919 | /// arguments), this type will be replaced with the \c SubstTemplateTypeParmType | ||||||
4920 | /// at the current pack substitution index. | ||||||
4921 | class SubstTemplateTypeParmPackType : public Type, public llvm::FoldingSetNode { | ||||||
4922 | friend class ASTContext; | ||||||
4923 | |||||||
4924 | /// The original type parameter. | ||||||
4925 | const TemplateTypeParmType *Replaced; | ||||||
4926 | |||||||
4927 | /// A pointer to the set of template arguments that this | ||||||
4928 | /// parameter pack is instantiated with. | ||||||
4929 | const TemplateArgument *Arguments; | ||||||
4930 | |||||||
4931 | SubstTemplateTypeParmPackType(const TemplateTypeParmType *Param, | ||||||
4932 | QualType Canon, | ||||||
4933 | const TemplateArgument &ArgPack); | ||||||
4934 | |||||||
4935 | public: | ||||||
4936 | IdentifierInfo *getIdentifier() const { return Replaced->getIdentifier(); } | ||||||
4937 | |||||||
4938 | /// Gets the template parameter that was substituted for. | ||||||
4939 | const TemplateTypeParmType *getReplacedParameter() const { | ||||||
4940 | return Replaced; | ||||||
4941 | } | ||||||
4942 | |||||||
4943 | unsigned getNumArgs() const { | ||||||
4944 | return SubstTemplateTypeParmPackTypeBits.NumArgs; | ||||||
4945 | } | ||||||
4946 | |||||||
4947 | bool isSugared() const { return false; } | ||||||
4948 | QualType desugar() const { return QualType(this, 0); } | ||||||
4949 | |||||||
4950 | TemplateArgument getArgumentPack() const; | ||||||
4951 | |||||||
4952 | void Profile(llvm::FoldingSetNodeID &ID); | ||||||
4953 | static void Profile(llvm::FoldingSetNodeID &ID, | ||||||
4954 | const TemplateTypeParmType *Replaced, | ||||||
4955 | const TemplateArgument &ArgPack); | ||||||
4956 | |||||||
4957 | static bool classof(const Type *T) { | ||||||
4958 | return T->getTypeClass() == SubstTemplateTypeParmPack; | ||||||
4959 | } | ||||||
4960 | }; | ||||||
4961 | |||||||
4962 | /// Common base class for placeholders for types that get replaced by | ||||||
4963 | /// placeholder type deduction: C++11 auto, C++14 decltype(auto), C++17 deduced | ||||||
4964 | /// class template types, and constrained type names. | ||||||
4965 | /// | ||||||
4966 | /// These types are usually a placeholder for a deduced type. However, before | ||||||
4967 | /// the initializer is attached, or (usually) if the initializer is | ||||||
4968 | /// type-dependent, there is no deduced type and the type is canonical. In | ||||||
4969 | /// the latter case, it is also a dependent type. | ||||||
4970 | class DeducedType : public Type { | ||||||
4971 | QualType DeducedAsType; | ||||||
4972 | |||||||
4973 | protected: | ||||||
4974 | DeducedType(TypeClass TC, QualType DeducedAsType, | ||||||
4975 | TypeDependence ExtraDependence, QualType Canon) | ||||||
4976 | : Type(TC, Canon, | ||||||
4977 | ExtraDependence | (DeducedAsType.isNull() | ||||||
4978 | ? TypeDependence::None | ||||||
4979 | : DeducedAsType->getDependence() & | ||||||
4980 | ~TypeDependence::VariablyModified)), | ||||||
4981 | DeducedAsType(DeducedAsType) {} | ||||||
4982 | |||||||
4983 | public: | ||||||
4984 | bool isSugared() const { return !DeducedAsType.isNull(); } | ||||||
4985 | QualType desugar() const { | ||||||
4986 | return isSugared() ? DeducedAsType : QualType(this, 0); | ||||||
4987 | } | ||||||
4988 | |||||||
4989 | /// Get the type deduced for this placeholder type, or null if it | ||||||
4990 | /// has not been deduced. | ||||||
4991 | QualType getDeducedType() const { return DeducedAsType; } | ||||||
4992 | bool isDeduced() const { | ||||||
4993 | return !DeducedAsType.isNull() || isDependentType(); | ||||||
4994 | } | ||||||
4995 | |||||||
4996 | static bool classof(const Type *T) { | ||||||
4997 | return T->getTypeClass() == Auto || | ||||||
4998 | T->getTypeClass() == DeducedTemplateSpecialization; | ||||||
4999 | } | ||||||
5000 | }; | ||||||
5001 | |||||||
5002 | /// Represents a C++11 auto or C++14 decltype(auto) type, possibly constrained | ||||||
5003 | /// by a type-constraint. | ||||||
5004 | class alignas(8) AutoType : public DeducedType, public llvm::FoldingSetNode { | ||||||
5005 | friend class ASTContext; // ASTContext creates these | ||||||
5006 | |||||||
5007 | ConceptDecl *TypeConstraintConcept; | ||||||
5008 | |||||||
5009 | AutoType(QualType DeducedAsType, AutoTypeKeyword Keyword, | ||||||
5010 | TypeDependence ExtraDependence, QualType Canon, ConceptDecl *CD, | ||||||
5011 | ArrayRef<TemplateArgument> TypeConstraintArgs); | ||||||
5012 | |||||||
5013 | const TemplateArgument *getArgBuffer() const { | ||||||
5014 | return reinterpret_cast<const TemplateArgument*>(this+1); | ||||||
5015 | } | ||||||
5016 | |||||||
5017 | TemplateArgument *getArgBuffer() { | ||||||
5018 | return reinterpret_cast<TemplateArgument*>(this+1); | ||||||
5019 | } | ||||||
5020 | |||||||
5021 | public: | ||||||
5022 | /// Retrieve the template arguments. | ||||||
5023 | const TemplateArgument *getArgs() const { | ||||||
5024 | return getArgBuffer(); | ||||||
5025 | } | ||||||
5026 | |||||||
5027 | /// Retrieve the number of template arguments. | ||||||
5028 | unsigned getNumArgs() const { | ||||||
5029 | return AutoTypeBits.NumArgs; | ||||||
5030 | } | ||||||
5031 | |||||||
5032 | const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h | ||||||
5033 | |||||||
5034 | ArrayRef<TemplateArgument> getTypeConstraintArguments() const { | ||||||
5035 | return {getArgs(), getNumArgs()}; | ||||||
5036 | } | ||||||
5037 | |||||||
5038 | ConceptDecl *getTypeConstraintConcept() const { | ||||||
5039 | return TypeConstraintConcept; | ||||||
5040 | } | ||||||
5041 | |||||||
5042 | bool isConstrained() const { | ||||||
5043 | return TypeConstraintConcept != nullptr; | ||||||
5044 | } | ||||||
5045 | |||||||
5046 | bool isDecltypeAuto() const { | ||||||
5047 | return getKeyword() == AutoTypeKeyword::DecltypeAuto; | ||||||
5048 | } | ||||||
5049 | |||||||
5050 | AutoTypeKeyword getKeyword() const { | ||||||
5051 | return (AutoTypeKeyword)AutoTypeBits.Keyword; | ||||||
5052 | } | ||||||
5053 | |||||||
5054 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { | ||||||
5055 | Profile(ID, Context, getDeducedType(), getKeyword(), isDependentType(), | ||||||
5056 | getTypeConstraintConcept(), getTypeConstraintArguments()); | ||||||
5057 | } | ||||||
5058 | |||||||
5059 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, | ||||||
5060 | QualType Deduced, AutoTypeKeyword Keyword, | ||||||
5061 | bool IsDependent, ConceptDecl *CD, | ||||||
5062 | ArrayRef<TemplateArgument> Arguments); | ||||||
5063 | |||||||
5064 | static bool classof(const Type *T) { | ||||||
5065 | return T->getTypeClass() == Auto; | ||||||
5066 | } | ||||||
5067 | }; | ||||||
5068 | |||||||
5069 | /// Represents a C++17 deduced template specialization type. | ||||||
5070 | class DeducedTemplateSpecializationType : public DeducedType, | ||||||
5071 | public llvm::FoldingSetNode { | ||||||
5072 | friend class ASTContext; // ASTContext creates these | ||||||
5073 | |||||||
5074 | /// The name of the template whose arguments will be deduced. | ||||||
5075 | TemplateName Template; | ||||||
5076 | |||||||
5077 | DeducedTemplateSpecializationType(TemplateName Template, | ||||||
5078 | QualType DeducedAsType, | ||||||
5079 | bool IsDeducedAsDependent) | ||||||
5080 | : DeducedType(DeducedTemplateSpecialization, DeducedAsType, | ||||||
5081 | toTypeDependence(Template.getDependence()) | | ||||||
5082 | (IsDeducedAsDependent | ||||||
5083 | ? TypeDependence::DependentInstantiation | ||||||
5084 | : TypeDependence::None), | ||||||
5085 | DeducedAsType.isNull() ? QualType(this, 0) | ||||||
5086 | : DeducedAsType.getCanonicalType()), | ||||||
5087 | Template(Template) {} | ||||||
5088 | |||||||
5089 | public: | ||||||
5090 | /// Retrieve the name of the template that we are deducing. | ||||||
5091 | TemplateName getTemplateName() const { return Template;} | ||||||
5092 | |||||||
5093 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
5094 | Profile(ID, getTemplateName(), getDeducedType(), isDependentType()); | ||||||
5095 | } | ||||||
5096 | |||||||
5097 | static void Profile(llvm::FoldingSetNodeID &ID, TemplateName Template, | ||||||
5098 | QualType Deduced, bool IsDependent) { | ||||||
5099 | Template.Profile(ID); | ||||||
5100 | QualType CanonicalType = | ||||||
5101 | Deduced.isNull() ? Deduced : Deduced.getCanonicalType(); | ||||||
5102 | ID.AddPointer(CanonicalType.getAsOpaquePtr()); | ||||||
5103 | ID.AddBoolean(IsDependent || Template.isDependent()); | ||||||
5104 | } | ||||||
5105 | |||||||
5106 | static bool classof(const Type *T) { | ||||||
5107 | return T->getTypeClass() == DeducedTemplateSpecialization; | ||||||
5108 | } | ||||||
5109 | }; | ||||||
5110 | |||||||
5111 | /// Represents a type template specialization; the template | ||||||
5112 | /// must be a class template, a type alias template, or a template | ||||||
5113 | /// template parameter. A template which cannot be resolved to one of | ||||||
5114 | /// these, e.g. because it is written with a dependent scope | ||||||
5115 | /// specifier, is instead represented as a | ||||||
5116 | /// @c DependentTemplateSpecializationType. | ||||||
5117 | /// | ||||||
5118 | /// A non-dependent template specialization type is always "sugar", | ||||||
5119 | /// typically for a \c RecordType. For example, a class template | ||||||
5120 | /// specialization type of \c vector<int> will refer to a tag type for | ||||||
5121 | /// the instantiation \c std::vector<int, std::allocator<int>> | ||||||
5122 | /// | ||||||
5123 | /// Template specializations are dependent if either the template or | ||||||
5124 | /// any of the template arguments are dependent, in which case the | ||||||
5125 | /// type may also be canonical. | ||||||
5126 | /// | ||||||
5127 | /// Instances of this type are allocated with a trailing array of | ||||||
5128 | /// TemplateArguments, followed by a QualType representing the | ||||||
5129 | /// non-canonical aliased type when the template is a type alias | ||||||
5130 | /// template. | ||||||
5131 | class alignas(8) TemplateSpecializationType | ||||||
5132 | : public Type, | ||||||
5133 | public llvm::FoldingSetNode { | ||||||
5134 | friend class ASTContext; // ASTContext creates these | ||||||
5135 | |||||||
5136 | /// The name of the template being specialized. This is | ||||||
5137 | /// either a TemplateName::Template (in which case it is a | ||||||
5138 | /// ClassTemplateDecl*, a TemplateTemplateParmDecl*, or a | ||||||
5139 | /// TypeAliasTemplateDecl*), a | ||||||
5140 | /// TemplateName::SubstTemplateTemplateParmPack, or a | ||||||
5141 | /// TemplateName::SubstTemplateTemplateParm (in which case the | ||||||
5142 | /// replacement must, recursively, be one of these). | ||||||
5143 | TemplateName Template; | ||||||
5144 | |||||||
5145 | TemplateSpecializationType(TemplateName T, | ||||||
5146 | ArrayRef<TemplateArgument> Args, | ||||||
5147 | QualType Canon, | ||||||
5148 | QualType Aliased); | ||||||
5149 | |||||||
5150 | public: | ||||||
5151 | /// Determine whether any of the given template arguments are dependent. | ||||||
5152 | /// | ||||||
5153 | /// The converted arguments should be supplied when known; whether an | ||||||
5154 | /// argument is dependent can depend on the conversions performed on it | ||||||
5155 | /// (for example, a 'const int' passed as a template argument might be | ||||||
5156 | /// dependent if the parameter is a reference but non-dependent if the | ||||||
5157 | /// parameter is an int). | ||||||
5158 | /// | ||||||
5159 | /// Note that the \p Args parameter is unused: this is intentional, to remind | ||||||
5160 | /// the caller that they need to pass in the converted arguments, not the | ||||||
5161 | /// specified arguments. | ||||||
5162 | static bool | ||||||
5163 | anyDependentTemplateArguments(ArrayRef<TemplateArgumentLoc> Args, | ||||||
5164 | ArrayRef<TemplateArgument> Converted); | ||||||
5165 | static bool | ||||||
5166 | anyDependentTemplateArguments(const TemplateArgumentListInfo &, | ||||||
5167 | ArrayRef<TemplateArgument> Converted); | ||||||
5168 | static bool anyInstantiationDependentTemplateArguments( | ||||||
5169 | ArrayRef<TemplateArgumentLoc> Args); | ||||||
5170 | |||||||
5171 | /// True if this template specialization type matches a current | ||||||
5172 | /// instantiation in the context in which it is found. | ||||||
5173 | bool isCurrentInstantiation() const { | ||||||
5174 | return isa<InjectedClassNameType>(getCanonicalTypeInternal()); | ||||||
5175 | } | ||||||
5176 | |||||||
5177 | /// Determine if this template specialization type is for a type alias | ||||||
5178 | /// template that has been substituted. | ||||||
5179 | /// | ||||||
5180 | /// Nearly every template specialization type whose template is an alias | ||||||
5181 | /// template will be substituted. However, this is not the case when | ||||||
5182 | /// the specialization contains a pack expansion but the template alias | ||||||
5183 | /// does not have a corresponding parameter pack, e.g., | ||||||
5184 | /// | ||||||
5185 | /// \code | ||||||
5186 | /// template<typename T, typename U, typename V> struct S; | ||||||
5187 | /// template<typename T, typename U> using A = S<T, int, U>; | ||||||
5188 | /// template<typename... Ts> struct X { | ||||||
5189 | /// typedef A<Ts...> type; // not a type alias | ||||||
5190 | /// }; | ||||||
5191 | /// \endcode | ||||||
5192 | bool isTypeAlias() const { return TemplateSpecializationTypeBits.TypeAlias; } | ||||||
5193 | |||||||
5194 | /// Get the aliased type, if this is a specialization of a type alias | ||||||
5195 | /// template. | ||||||
5196 | QualType getAliasedType() const { | ||||||
5197 | assert(isTypeAlias() && "not a type alias template specialization")(static_cast <bool> (isTypeAlias() && "not a type alias template specialization" ) ? void (0) : __assert_fail ("isTypeAlias() && \"not a type alias template specialization\"" , "clang/include/clang/AST/Type.h", 5197, __extension__ __PRETTY_FUNCTION__ )); | ||||||
5198 | return *reinterpret_cast<const QualType*>(end()); | ||||||
5199 | } | ||||||
5200 | |||||||
5201 | using iterator = const TemplateArgument *; | ||||||
5202 | |||||||
5203 | iterator begin() const { return getArgs(); } | ||||||
5204 | iterator end() const; // defined inline in TemplateBase.h | ||||||
5205 | |||||||
5206 | /// Retrieve the name of the template that we are specializing. | ||||||
5207 | TemplateName getTemplateName() const { return Template; } | ||||||
5208 | |||||||
5209 | /// Retrieve the template arguments. | ||||||
5210 | const TemplateArgument *getArgs() const { | ||||||
5211 | return reinterpret_cast<const TemplateArgument *>(this + 1); | ||||||
5212 | } | ||||||
5213 | |||||||
5214 | /// Retrieve the number of template arguments. | ||||||
5215 | unsigned getNumArgs() const { | ||||||
5216 | return TemplateSpecializationTypeBits.NumArgs; | ||||||
5217 | } | ||||||
5218 | |||||||
5219 | /// Retrieve a specific template argument as a type. | ||||||
5220 | /// \pre \c isArgType(Arg) | ||||||
5221 | const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h | ||||||
5222 | |||||||
5223 | ArrayRef<TemplateArgument> template_arguments() const { | ||||||
5224 | return {getArgs(), getNumArgs()}; | ||||||
5225 | } | ||||||
5226 | |||||||
5227 | bool isSugared() const { | ||||||
5228 | return !isDependentType() || isCurrentInstantiation() || isTypeAlias(); | ||||||
5229 | } | ||||||
5230 | |||||||
5231 | QualType desugar() const { | ||||||
5232 | return isTypeAlias() ? getAliasedType() : getCanonicalTypeInternal(); | ||||||
5233 | } | ||||||
5234 | |||||||
5235 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) { | ||||||
5236 | Profile(ID, Template, template_arguments(), Ctx); | ||||||
5237 | if (isTypeAlias()) | ||||||
5238 | getAliasedType().Profile(ID); | ||||||
5239 | } | ||||||
5240 | |||||||
5241 | static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T, | ||||||
5242 | ArrayRef<TemplateArgument> Args, | ||||||
5243 | const ASTContext &Context); | ||||||
5244 | |||||||
5245 | static bool classof(const Type *T) { | ||||||
5246 | return T->getTypeClass() == TemplateSpecialization; | ||||||
5247 | } | ||||||
5248 | }; | ||||||
5249 | |||||||
5250 | /// Print a template argument list, including the '<' and '>' | ||||||
5251 | /// enclosing the template arguments. | ||||||
5252 | void printTemplateArgumentList(raw_ostream &OS, | ||||||
5253 | ArrayRef<TemplateArgument> Args, | ||||||
5254 | const PrintingPolicy &Policy, | ||||||
5255 | const TemplateParameterList *TPL = nullptr); | ||||||
5256 | |||||||
5257 | void printTemplateArgumentList(raw_ostream &OS, | ||||||
5258 | ArrayRef<TemplateArgumentLoc> Args, | ||||||
5259 | const PrintingPolicy &Policy, | ||||||
5260 | const TemplateParameterList *TPL = nullptr); | ||||||
5261 | |||||||
5262 | void printTemplateArgumentList(raw_ostream &OS, | ||||||
5263 | const TemplateArgumentListInfo &Args, | ||||||
5264 | const PrintingPolicy &Policy, | ||||||
5265 | const TemplateParameterList *TPL = nullptr); | ||||||
5266 | |||||||
5267 | /// The injected class name of a C++ class template or class | ||||||
5268 | /// template partial specialization. Used to record that a type was | ||||||
5269 | /// spelled with a bare identifier rather than as a template-id; the | ||||||
5270 | /// equivalent for non-templated classes is just RecordType. | ||||||
5271 | /// | ||||||
5272 | /// Injected class name types are always dependent. Template | ||||||
5273 | /// instantiation turns these into RecordTypes. | ||||||
5274 | /// | ||||||
5275 | /// Injected class name types are always canonical. This works | ||||||
5276 | /// because it is impossible to compare an injected class name type | ||||||
5277 | /// with the corresponding non-injected template type, for the same | ||||||
5278 | /// reason that it is impossible to directly compare template | ||||||
5279 | /// parameters from different dependent contexts: injected class name | ||||||
5280 | /// types can only occur within the scope of a particular templated | ||||||
5281 | /// declaration, and within that scope every template specialization | ||||||
5282 | /// will canonicalize to the injected class name (when appropriate | ||||||
5283 | /// according to the rules of the language). | ||||||
5284 | class InjectedClassNameType : public Type { | ||||||
5285 | friend class ASTContext; // ASTContext creates these. | ||||||
5286 | friend class ASTNodeImporter; | ||||||
5287 | friend class ASTReader; // FIXME: ASTContext::getInjectedClassNameType is not | ||||||
5288 | // currently suitable for AST reading, too much | ||||||
5289 | // interdependencies. | ||||||
5290 | template <class T> friend class serialization::AbstractTypeReader; | ||||||
5291 | |||||||
5292 | CXXRecordDecl *Decl; | ||||||
5293 | |||||||
5294 | /// The template specialization which this type represents. | ||||||
5295 | /// For example, in | ||||||
5296 | /// template <class T> class A { ... }; | ||||||
5297 | /// this is A<T>, whereas in | ||||||
5298 | /// template <class X, class Y> class A<B<X,Y> > { ... }; | ||||||
5299 | /// this is A<B<X,Y> >. | ||||||
5300 | /// | ||||||
5301 | /// It is always unqualified, always a template specialization type, | ||||||
5302 | /// and always dependent. | ||||||
5303 | QualType InjectedType; | ||||||
5304 | |||||||
5305 | InjectedClassNameType(CXXRecordDecl *D, QualType TST) | ||||||
5306 | : Type(InjectedClassName, QualType(), | ||||||
5307 | TypeDependence::DependentInstantiation), | ||||||
5308 | Decl(D), InjectedType(TST) { | ||||||
5309 | assert(isa<TemplateSpecializationType>(TST))(static_cast <bool> (isa<TemplateSpecializationType> (TST)) ? void (0) : __assert_fail ("isa<TemplateSpecializationType>(TST)" , "clang/include/clang/AST/Type.h", 5309, __extension__ __PRETTY_FUNCTION__ )); | ||||||
5310 | assert(!TST.hasQualifiers())(static_cast <bool> (!TST.hasQualifiers()) ? void (0) : __assert_fail ("!TST.hasQualifiers()", "clang/include/clang/AST/Type.h" , 5310, __extension__ __PRETTY_FUNCTION__)); | ||||||
5311 | assert(TST->isDependentType())(static_cast <bool> (TST->isDependentType()) ? void ( 0) : __assert_fail ("TST->isDependentType()", "clang/include/clang/AST/Type.h" , 5311, __extension__ __PRETTY_FUNCTION__)); | ||||||
5312 | } | ||||||
5313 | |||||||
5314 | public: | ||||||
5315 | QualType getInjectedSpecializationType() const { return InjectedType; } | ||||||
5316 | |||||||
5317 | const TemplateSpecializationType *getInjectedTST() const { | ||||||
5318 | return cast<TemplateSpecializationType>(InjectedType.getTypePtr()); | ||||||
5319 | } | ||||||
5320 | |||||||
5321 | TemplateName getTemplateName() const { | ||||||
5322 | return getInjectedTST()->getTemplateName(); | ||||||
5323 | } | ||||||
5324 | |||||||
5325 | CXXRecordDecl *getDecl() const; | ||||||
5326 | |||||||
5327 | bool isSugared() const { return false; } | ||||||
5328 | QualType desugar() const { return QualType(this, 0); } | ||||||
5329 | |||||||
5330 | static bool classof(const Type *T) { | ||||||
5331 | return T->getTypeClass() == InjectedClassName; | ||||||
5332 | } | ||||||
5333 | }; | ||||||
5334 | |||||||
5335 | /// The kind of a tag type. | ||||||
5336 | enum TagTypeKind { | ||||||
5337 | /// The "struct" keyword. | ||||||
5338 | TTK_Struct, | ||||||
5339 | |||||||
5340 | /// The "__interface" keyword. | ||||||
5341 | TTK_Interface, | ||||||
5342 | |||||||
5343 | /// The "union" keyword. | ||||||
5344 | TTK_Union, | ||||||
5345 | |||||||
5346 | /// The "class" keyword. | ||||||
5347 | TTK_Class, | ||||||
5348 | |||||||
5349 | /// The "enum" keyword. | ||||||
5350 | TTK_Enum | ||||||
5351 | }; | ||||||
5352 | |||||||
5353 | /// The elaboration keyword that precedes a qualified type name or | ||||||
5354 | /// introduces an elaborated-type-specifier. | ||||||
5355 | enum ElaboratedTypeKeyword { | ||||||
5356 | /// The "struct" keyword introduces the elaborated-type-specifier. | ||||||
5357 | ETK_Struct, | ||||||
5358 | |||||||
5359 | /// The "__interface" keyword introduces the elaborated-type-specifier. | ||||||
5360 | ETK_Interface, | ||||||
5361 | |||||||
5362 | /// The "union" keyword introduces the elaborated-type-specifier. | ||||||
5363 | ETK_Union, | ||||||
5364 | |||||||
5365 | /// The "class" keyword introduces the elaborated-type-specifier. | ||||||
5366 | ETK_Class, | ||||||
5367 | |||||||
5368 | /// The "enum" keyword introduces the elaborated-type-specifier. | ||||||
5369 | ETK_Enum, | ||||||
5370 | |||||||
5371 | /// The "typename" keyword precedes the qualified type name, e.g., | ||||||
5372 | /// \c typename T::type. | ||||||
5373 | ETK_Typename, | ||||||
5374 | |||||||
5375 | /// No keyword precedes the qualified type name. | ||||||
5376 | ETK_None | ||||||
5377 | }; | ||||||
5378 | |||||||
5379 | /// A helper class for Type nodes having an ElaboratedTypeKeyword. | ||||||
5380 | /// The keyword in stored in the free bits of the base class. | ||||||
5381 | /// Also provides a few static helpers for converting and printing | ||||||
5382 | /// elaborated type keyword and tag type kind enumerations. | ||||||
5383 | class TypeWithKeyword : public Type { | ||||||
5384 | protected: | ||||||
5385 | TypeWithKeyword(ElaboratedTypeKeyword Keyword, TypeClass tc, | ||||||
5386 | QualType Canonical, TypeDependence Dependence) | ||||||
5387 | : Type(tc, Canonical, Dependence) { | ||||||
5388 | TypeWithKeywordBits.Keyword = Keyword; | ||||||
5389 | } | ||||||
5390 | |||||||
5391 | public: | ||||||
5392 | ElaboratedTypeKeyword getKeyword() const { | ||||||
5393 | return static_cast<ElaboratedTypeKeyword>(TypeWithKeywordBits.Keyword); | ||||||
5394 | } | ||||||
5395 | |||||||
5396 | /// Converts a type specifier (DeclSpec::TST) into an elaborated type keyword. | ||||||
5397 | static ElaboratedTypeKeyword getKeywordForTypeSpec(unsigned TypeSpec); | ||||||
5398 | |||||||
5399 | /// Converts a type specifier (DeclSpec::TST) into a tag type kind. | ||||||
5400 | /// It is an error to provide a type specifier which *isn't* a tag kind here. | ||||||
5401 | static TagTypeKind getTagTypeKindForTypeSpec(unsigned TypeSpec); | ||||||
5402 | |||||||
5403 | /// Converts a TagTypeKind into an elaborated type keyword. | ||||||
5404 | static ElaboratedTypeKeyword getKeywordForTagTypeKind(TagTypeKind Tag); | ||||||
5405 | |||||||
5406 | /// Converts an elaborated type keyword into a TagTypeKind. | ||||||
5407 | /// It is an error to provide an elaborated type keyword | ||||||
5408 | /// which *isn't* a tag kind here. | ||||||
5409 | static TagTypeKind getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword); | ||||||
5410 | |||||||
5411 | static bool KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword); | ||||||
5412 | |||||||
5413 | static StringRef getKeywordName(ElaboratedTypeKeyword Keyword); | ||||||
5414 | |||||||
5415 | static StringRef getTagTypeKindName(TagTypeKind Kind) { | ||||||
5416 | return getKeywordName(getKeywordForTagTypeKind(Kind)); | ||||||
5417 | } | ||||||
5418 | |||||||
5419 | class CannotCastToThisType {}; | ||||||
5420 | static CannotCastToThisType classof(const Type *); | ||||||
5421 | }; | ||||||
5422 | |||||||
5423 | /// Represents a type that was referred to using an elaborated type | ||||||
5424 | /// keyword, e.g., struct S, or via a qualified name, e.g., N::M::type, | ||||||
5425 | /// or both. | ||||||
5426 | /// | ||||||
5427 | /// This type is used to keep track of a type name as written in the | ||||||
5428 | /// source code, including tag keywords and any nested-name-specifiers. | ||||||
5429 | /// The type itself is always "sugar", used to express what was written | ||||||
5430 | /// in the source code but containing no additional semantic information. | ||||||
5431 | class ElaboratedType final | ||||||
5432 | : public TypeWithKeyword, | ||||||
5433 | public llvm::FoldingSetNode, | ||||||
5434 | private llvm::TrailingObjects<ElaboratedType, TagDecl *> { | ||||||
5435 | friend class ASTContext; // ASTContext creates these | ||||||
5436 | friend TrailingObjects; | ||||||
5437 | |||||||
5438 | /// The nested name specifier containing the qualifier. | ||||||
5439 | NestedNameSpecifier *NNS; | ||||||
5440 | |||||||
5441 | /// The type that this qualified name refers to. | ||||||
5442 | QualType NamedType; | ||||||
5443 | |||||||
5444 | /// The (re)declaration of this tag type owned by this occurrence is stored | ||||||
5445 | /// as a trailing object if there is one. Use getOwnedTagDecl to obtain | ||||||
5446 | /// it, or obtain a null pointer if there is none. | ||||||
5447 | |||||||
5448 | ElaboratedType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, | ||||||
5449 | QualType NamedType, QualType CanonType, TagDecl *OwnedTagDecl) | ||||||
5450 | : TypeWithKeyword(Keyword, Elaborated, CanonType, | ||||||
5451 | // Any semantic dependence on the qualifier will have | ||||||
5452 | // been incorporated into NamedType. We still need to | ||||||
5453 | // track syntactic (instantiation / error / pack) | ||||||
5454 | // dependence on the qualifier. | ||||||
5455 | NamedType->getDependence() | | ||||||
5456 | (NNS ? toSyntacticDependence( | ||||||
5457 | toTypeDependence(NNS->getDependence())) | ||||||
5458 | : TypeDependence::None)), | ||||||
5459 | NNS(NNS), NamedType(NamedType) { | ||||||
5460 | ElaboratedTypeBits.HasOwnedTagDecl = false; | ||||||
5461 | if (OwnedTagDecl) { | ||||||
5462 | ElaboratedTypeBits.HasOwnedTagDecl = true; | ||||||
5463 | *getTrailingObjects<TagDecl *>() = OwnedTagDecl; | ||||||
5464 | } | ||||||
5465 | assert(!(Keyword == ETK_None && NNS == nullptr) &&(static_cast <bool> (!(Keyword == ETK_None && NNS == nullptr) && "ElaboratedType cannot have elaborated type keyword " "and name qualifier both null.") ? void (0) : __assert_fail ( "!(Keyword == ETK_None && NNS == nullptr) && \"ElaboratedType cannot have elaborated type keyword \" \"and name qualifier both null.\"" , "clang/include/clang/AST/Type.h", 5467, __extension__ __PRETTY_FUNCTION__ )) | ||||||
5466 | "ElaboratedType cannot have elaborated type keyword "(static_cast <bool> (!(Keyword == ETK_None && NNS == nullptr) && "ElaboratedType cannot have elaborated type keyword " "and name qualifier both null.") ? void (0) : __assert_fail ( "!(Keyword == ETK_None && NNS == nullptr) && \"ElaboratedType cannot have elaborated type keyword \" \"and name qualifier both null.\"" , "clang/include/clang/AST/Type.h", 5467, __extension__ __PRETTY_FUNCTION__ )) | ||||||
5467 | "and name qualifier both null.")(static_cast <bool> (!(Keyword == ETK_None && NNS == nullptr) && "ElaboratedType cannot have elaborated type keyword " "and name qualifier both null.") ? void (0) : __assert_fail ( "!(Keyword == ETK_None && NNS == nullptr) && \"ElaboratedType cannot have elaborated type keyword \" \"and name qualifier both null.\"" , "clang/include/clang/AST/Type.h", 5467, __extension__ __PRETTY_FUNCTION__ )); | ||||||
5468 | } | ||||||
5469 | |||||||
5470 | public: | ||||||
5471 | /// Retrieve the qualification on this type. | ||||||
5472 | NestedNameSpecifier *getQualifier() const { return NNS; } | ||||||
5473 | |||||||
5474 | /// Retrieve the type named by the qualified-id. | ||||||
5475 | QualType getNamedType() const { return NamedType; } | ||||||
5476 | |||||||
5477 | /// Remove a single level of sugar. | ||||||
5478 | QualType desugar() const { return getNamedType(); } | ||||||
5479 | |||||||
5480 | /// Returns whether this type directly provides sugar. | ||||||
5481 | bool isSugared() const { return true; } | ||||||
5482 | |||||||
5483 | /// Return the (re)declaration of this type owned by this occurrence of this | ||||||
5484 | /// type, or nullptr if there is none. | ||||||
5485 | TagDecl *getOwnedTagDecl() const { | ||||||
5486 | return ElaboratedTypeBits.HasOwnedTagDecl ? *getTrailingObjects<TagDecl *>() | ||||||
5487 | : nullptr; | ||||||
5488 | } | ||||||
5489 | |||||||
5490 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
5491 | Profile(ID, getKeyword(), NNS, NamedType, getOwnedTagDecl()); | ||||||
5492 | } | ||||||
5493 | |||||||
5494 | static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, | ||||||
5495 | NestedNameSpecifier *NNS, QualType NamedType, | ||||||
5496 | TagDecl *OwnedTagDecl) { | ||||||
5497 | ID.AddInteger(Keyword); | ||||||
5498 | ID.AddPointer(NNS); | ||||||
5499 | NamedType.Profile(ID); | ||||||
5500 | ID.AddPointer(OwnedTagDecl); | ||||||
5501 | } | ||||||
5502 | |||||||
5503 | static bool classof(const Type *T) { return T->getTypeClass() == Elaborated; } | ||||||
5504 | }; | ||||||
5505 | |||||||
5506 | /// Represents a qualified type name for which the type name is | ||||||
5507 | /// dependent. | ||||||
5508 | /// | ||||||
5509 | /// DependentNameType represents a class of dependent types that involve a | ||||||
5510 | /// possibly dependent nested-name-specifier (e.g., "T::") followed by a | ||||||
5511 | /// name of a type. The DependentNameType may start with a "typename" (for a | ||||||
5512 | /// typename-specifier), "class", "struct", "union", or "enum" (for a | ||||||
5513 | /// dependent elaborated-type-specifier), or nothing (in contexts where we | ||||||
5514 | /// know that we must be referring to a type, e.g., in a base class specifier). | ||||||
5515 | /// Typically the nested-name-specifier is dependent, but in MSVC compatibility | ||||||
5516 | /// mode, this type is used with non-dependent names to delay name lookup until | ||||||
5517 | /// instantiation. | ||||||
5518 | class DependentNameType : public TypeWithKeyword, public llvm::FoldingSetNode { | ||||||
5519 | friend class ASTContext; // ASTContext creates these | ||||||
5520 | |||||||
5521 | /// The nested name specifier containing the qualifier. | ||||||
5522 | NestedNameSpecifier *NNS; | ||||||
5523 | |||||||
5524 | /// The type that this typename specifier refers to. | ||||||
5525 | const IdentifierInfo *Name; | ||||||
5526 | |||||||
5527 | DependentNameType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, | ||||||
5528 | const IdentifierInfo *Name, QualType CanonType) | ||||||
5529 | : TypeWithKeyword(Keyword, DependentName, CanonType, | ||||||
5530 | TypeDependence::DependentInstantiation | | ||||||
5531 | toTypeDependence(NNS->getDependence())), | ||||||
5532 | NNS(NNS), Name(Name) {} | ||||||
5533 | |||||||
5534 | public: | ||||||
5535 | /// Retrieve the qualification on this type. | ||||||
5536 | NestedNameSpecifier *getQualifier() const { return NNS; } | ||||||
5537 | |||||||
5538 | /// Retrieve the type named by the typename specifier as an identifier. | ||||||
5539 | /// | ||||||
5540 | /// This routine will return a non-NULL identifier pointer when the | ||||||
5541 | /// form of the original typename was terminated by an identifier, | ||||||
5542 | /// e.g., "typename T::type". | ||||||
5543 | const IdentifierInfo *getIdentifier() const { | ||||||
5544 | return Name; | ||||||
5545 | } | ||||||
5546 | |||||||
5547 | bool isSugared() const { return false; } | ||||||
5548 | QualType desugar() const { return QualType(this, 0); } | ||||||
5549 | |||||||
5550 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
5551 | Profile(ID, getKeyword(), NNS, Name); | ||||||
5552 | } | ||||||
5553 | |||||||
5554 | static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, | ||||||
5555 | NestedNameSpecifier *NNS, const IdentifierInfo *Name) { | ||||||
5556 | ID.AddInteger(Keyword); | ||||||
5557 | ID.AddPointer(NNS); | ||||||
5558 | ID.AddPointer(Name); | ||||||
5559 | } | ||||||
5560 | |||||||
5561 | static bool classof(const Type *T) { | ||||||
5562 | return T->getTypeClass() == DependentName; | ||||||
5563 | } | ||||||
5564 | }; | ||||||
5565 | |||||||
5566 | /// Represents a template specialization type whose template cannot be | ||||||
5567 | /// resolved, e.g. | ||||||
5568 | /// A<T>::template B<T> | ||||||
5569 | class alignas(8) DependentTemplateSpecializationType | ||||||
5570 | : public TypeWithKeyword, | ||||||
5571 | public llvm::FoldingSetNode { | ||||||
5572 | friend class ASTContext; // ASTContext creates these | ||||||
5573 | |||||||
5574 | /// The nested name specifier containing the qualifier. | ||||||
5575 | NestedNameSpecifier *NNS; | ||||||
5576 | |||||||
5577 | /// The identifier of the template. | ||||||
5578 | const IdentifierInfo *Name; | ||||||
5579 | |||||||
5580 | DependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, | ||||||
5581 | NestedNameSpecifier *NNS, | ||||||
5582 | const IdentifierInfo *Name, | ||||||
5583 | ArrayRef<TemplateArgument> Args, | ||||||
5584 | QualType Canon); | ||||||
5585 | |||||||
5586 | const TemplateArgument *getArgBuffer() const { | ||||||
5587 | return reinterpret_cast<const TemplateArgument*>(this+1); | ||||||
5588 | } | ||||||
5589 | |||||||
5590 | TemplateArgument *getArgBuffer() { | ||||||
5591 | return reinterpret_cast<TemplateArgument*>(this+1); | ||||||
5592 | } | ||||||
5593 | |||||||
5594 | public: | ||||||
5595 | NestedNameSpecifier *getQualifier() const { return NNS; } | ||||||
5596 | const IdentifierInfo *getIdentifier() const { return Name; } | ||||||
5597 | |||||||
5598 | /// Retrieve the template arguments. | ||||||
5599 | const TemplateArgument *getArgs() const { | ||||||
5600 | return getArgBuffer(); | ||||||
5601 | } | ||||||
5602 | |||||||
5603 | /// Retrieve the number of template arguments. | ||||||
5604 | unsigned getNumArgs() const { | ||||||
5605 | return DependentTemplateSpecializationTypeBits.NumArgs; | ||||||
5606 | } | ||||||
5607 | |||||||
5608 | const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h | ||||||
5609 | |||||||
5610 | ArrayRef<TemplateArgument> template_arguments() const { | ||||||
5611 | return {getArgs(), getNumArgs()}; | ||||||
5612 | } | ||||||
5613 | |||||||
5614 | using iterator = const TemplateArgument *; | ||||||
5615 | |||||||
5616 | iterator begin() const { return getArgs(); } | ||||||
5617 | iterator end() const; // inline in TemplateBase.h | ||||||
5618 | |||||||
5619 | bool isSugared() const { return false; } | ||||||
5620 | QualType desugar() const { return QualType(this, 0); } | ||||||
5621 | |||||||
5622 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { | ||||||
5623 | Profile(ID, Context, getKeyword(), NNS, Name, {getArgs(), getNumArgs()}); | ||||||
5624 | } | ||||||
5625 | |||||||
5626 | static void Profile(llvm::FoldingSetNodeID &ID, | ||||||
5627 | const ASTContext &Context, | ||||||
5628 | ElaboratedTypeKeyword Keyword, | ||||||
5629 | NestedNameSpecifier *Qualifier, | ||||||
5630 | const IdentifierInfo *Name, | ||||||
5631 | ArrayRef<TemplateArgument> Args); | ||||||
5632 | |||||||
5633 | static bool classof(const Type *T) { | ||||||
5634 | return T->getTypeClass() == DependentTemplateSpecialization; | ||||||
5635 | } | ||||||
5636 | }; | ||||||
5637 | |||||||
5638 | /// Represents a pack expansion of types. | ||||||
5639 | /// | ||||||
5640 | /// Pack expansions are part of C++11 variadic templates. A pack | ||||||
5641 | /// expansion contains a pattern, which itself contains one or more | ||||||
5642 | /// "unexpanded" parameter packs. When instantiated, a pack expansion | ||||||
5643 | /// produces a series of types, each instantiated from the pattern of | ||||||
5644 | /// the expansion, where the Ith instantiation of the pattern uses the | ||||||
5645 | /// Ith arguments bound to each of the unexpanded parameter packs. The | ||||||
5646 | /// pack expansion is considered to "expand" these unexpanded | ||||||
5647 | /// parameter packs. | ||||||
5648 | /// | ||||||
5649 | /// \code | ||||||
5650 | /// template<typename ...Types> struct tuple; | ||||||
5651 | /// | ||||||
5652 | /// template<typename ...Types> | ||||||
5653 | /// struct tuple_of_references { | ||||||
5654 | /// typedef tuple<Types&...> type; | ||||||
5655 | /// }; | ||||||
5656 | /// \endcode | ||||||
5657 | /// | ||||||
5658 | /// Here, the pack expansion \c Types&... is represented via a | ||||||
5659 | /// PackExpansionType whose pattern is Types&. | ||||||
5660 | class PackExpansionType : public Type, public llvm::FoldingSetNode { | ||||||
5661 | friend class ASTContext; // ASTContext creates these | ||||||
5662 | |||||||
5663 | /// The pattern of the pack expansion. | ||||||
5664 | QualType Pattern; | ||||||
5665 | |||||||
5666 | PackExpansionType(QualType Pattern, QualType Canon, | ||||||
5667 | Optional<unsigned> NumExpansions) | ||||||
5668 | : Type(PackExpansion, Canon, | ||||||
5669 | (Pattern->getDependence() | TypeDependence::Dependent | | ||||||
5670 | TypeDependence::Instantiation) & | ||||||
5671 | ~TypeDependence::UnexpandedPack), | ||||||
5672 | Pattern(Pattern) { | ||||||
5673 | PackExpansionTypeBits.NumExpansions = | ||||||
5674 | NumExpansions ? *NumExpansions + 1 : 0; | ||||||
5675 | } | ||||||
5676 | |||||||
5677 | public: | ||||||
5678 | /// Retrieve the pattern of this pack expansion, which is the | ||||||
5679 | /// type that will be repeatedly instantiated when instantiating the | ||||||
5680 | /// pack expansion itself. | ||||||
5681 | QualType getPattern() const { return Pattern; } | ||||||
5682 | |||||||
5683 | /// Retrieve the number of expansions that this pack expansion will | ||||||
5684 | /// generate, if known. | ||||||
5685 | Optional<unsigned> getNumExpansions() const { | ||||||
5686 | if (PackExpansionTypeBits.NumExpansions) | ||||||
5687 | return PackExpansionTypeBits.NumExpansions - 1; | ||||||
5688 | return None; | ||||||
5689 | } | ||||||
5690 | |||||||
5691 | bool isSugared() const { return false; } | ||||||
5692 | QualType desugar() const { return QualType(this, 0); } | ||||||
5693 | |||||||
5694 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
5695 | Profile(ID, getPattern(), getNumExpansions()); | ||||||
5696 | } | ||||||
5697 | |||||||
5698 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pattern, | ||||||
5699 | Optional<unsigned> NumExpansions) { | ||||||
5700 | ID.AddPointer(Pattern.getAsOpaquePtr()); | ||||||
5701 | ID.AddBoolean(NumExpansions.hasValue()); | ||||||
5702 | if (NumExpansions) | ||||||
5703 | ID.AddInteger(*NumExpansions); | ||||||
5704 | } | ||||||
5705 | |||||||
5706 | static bool classof(const Type *T) { | ||||||
5707 | return T->getTypeClass() == PackExpansion; | ||||||
5708 | } | ||||||
5709 | }; | ||||||
5710 | |||||||
5711 | /// This class wraps the list of protocol qualifiers. For types that can | ||||||
5712 | /// take ObjC protocol qualifers, they can subclass this class. | ||||||
5713 | template <class T> | ||||||
5714 | class ObjCProtocolQualifiers { | ||||||
5715 | protected: | ||||||
5716 | ObjCProtocolQualifiers() = default; | ||||||
5717 | |||||||
5718 | ObjCProtocolDecl * const *getProtocolStorage() const { | ||||||
5719 | return const_cast<ObjCProtocolQualifiers*>(this)->getProtocolStorage(); | ||||||
5720 | } | ||||||
5721 | |||||||
5722 | ObjCProtocolDecl **getProtocolStorage() { | ||||||
5723 | return static_cast<T*>(this)->getProtocolStorageImpl(); | ||||||
5724 | } | ||||||
5725 | |||||||
5726 | void setNumProtocols(unsigned N) { | ||||||
5727 | static_cast<T*>(this)->setNumProtocolsImpl(N); | ||||||
5728 | } | ||||||
5729 | |||||||
5730 | void initialize(ArrayRef<ObjCProtocolDecl *> protocols) { | ||||||
5731 | setNumProtocols(protocols.size()); | ||||||
5732 | assert(getNumProtocols() == protocols.size() &&(static_cast <bool> (getNumProtocols() == protocols.size () && "bitfield overflow in protocol count") ? void ( 0) : __assert_fail ("getNumProtocols() == protocols.size() && \"bitfield overflow in protocol count\"" , "clang/include/clang/AST/Type.h", 5733, __extension__ __PRETTY_FUNCTION__ )) | ||||||
5733 | "bitfield overflow in protocol count")(static_cast <bool> (getNumProtocols() == protocols.size () && "bitfield overflow in protocol count") ? void ( 0) : __assert_fail ("getNumProtocols() == protocols.size() && \"bitfield overflow in protocol count\"" , "clang/include/clang/AST/Type.h", 5733, __extension__ __PRETTY_FUNCTION__ )); | ||||||
5734 | if (!protocols.empty()) | ||||||
5735 | memcpy(getProtocolStorage(), protocols.data(), | ||||||
5736 | protocols.size() * sizeof(ObjCProtocolDecl*)); | ||||||
5737 | } | ||||||
5738 | |||||||
5739 | public: | ||||||
5740 | using qual_iterator = ObjCProtocolDecl * const *; | ||||||
5741 | using qual_range = llvm::iterator_range<qual_iterator>; | ||||||
5742 | |||||||
5743 | qual_range quals() const { return qual_range(qual_begin(), qual_end()); } | ||||||
5744 | qual_iterator qual_begin() const { return getProtocolStorage(); } | ||||||
5745 | qual_iterator qual_end() const { return qual_begin() + getNumProtocols(); } | ||||||
5746 | |||||||
5747 | bool qual_empty() const { return getNumProtocols() == 0; } | ||||||
5748 | |||||||
5749 | /// Return the number of qualifying protocols in this type, or 0 if | ||||||
5750 | /// there are none. | ||||||
5751 | unsigned getNumProtocols() const { | ||||||
5752 | return static_cast<const T*>(this)->getNumProtocolsImpl(); | ||||||
5753 | } | ||||||
5754 | |||||||
5755 | /// Fetch a protocol by index. | ||||||
5756 | ObjCProtocolDecl *getProtocol(unsigned I) const { | ||||||
5757 | assert(I < getNumProtocols() && "Out-of-range protocol access")(static_cast <bool> (I < getNumProtocols() && "Out-of-range protocol access") ? void (0) : __assert_fail ( "I < getNumProtocols() && \"Out-of-range protocol access\"" , "clang/include/clang/AST/Type.h", 5757, __extension__ __PRETTY_FUNCTION__ )); | ||||||
5758 | return qual_begin()[I]; | ||||||
5759 | } | ||||||
5760 | |||||||
5761 | /// Retrieve all of the protocol qualifiers. | ||||||
5762 | ArrayRef<ObjCProtocolDecl *> getProtocols() const { | ||||||
5763 | return ArrayRef<ObjCProtocolDecl *>(qual_begin(), getNumProtocols()); | ||||||
5764 | } | ||||||
5765 | }; | ||||||
5766 | |||||||
5767 | /// Represents a type parameter type in Objective C. It can take | ||||||
5768 | /// a list of protocols. | ||||||
5769 | class ObjCTypeParamType : public Type, | ||||||
5770 | public ObjCProtocolQualifiers<ObjCTypeParamType>, | ||||||
5771 | public llvm::FoldingSetNode { | ||||||
5772 | friend class ASTContext; | ||||||
5773 | friend class ObjCProtocolQualifiers<ObjCTypeParamType>; | ||||||
5774 | |||||||
5775 | /// The number of protocols stored on this type. | ||||||
5776 | unsigned NumProtocols : 6; | ||||||
5777 | |||||||
5778 | ObjCTypeParamDecl *OTPDecl; | ||||||
5779 | |||||||
5780 | /// The protocols are stored after the ObjCTypeParamType node. In the | ||||||
5781 | /// canonical type, the list of protocols are sorted alphabetically | ||||||
5782 | /// and uniqued. | ||||||
5783 | ObjCProtocolDecl **getProtocolStorageImpl(); | ||||||
5784 | |||||||
5785 | /// Return the number of qualifying protocols in this interface type, | ||||||
5786 | /// or 0 if there are none. | ||||||
5787 | unsigned getNumProtocolsImpl() const { | ||||||
5788 | return NumProtocols; | ||||||
5789 | } | ||||||
5790 | |||||||
5791 | void setNumProtocolsImpl(unsigned N) { | ||||||
5792 | NumProtocols = N; | ||||||
5793 | } | ||||||
5794 | |||||||
5795 | ObjCTypeParamType(const ObjCTypeParamDecl *D, | ||||||
5796 | QualType can, | ||||||
5797 | ArrayRef<ObjCProtocolDecl *> protocols); | ||||||
5798 | |||||||
5799 | public: | ||||||
5800 | bool isSugared() const { return true; } | ||||||
5801 | QualType desugar() const { return getCanonicalTypeInternal(); } | ||||||
5802 | |||||||
5803 | static bool classof(const Type *T) { | ||||||
5804 | return T->getTypeClass() == ObjCTypeParam; | ||||||
5805 | } | ||||||
5806 | |||||||
5807 | void Profile(llvm::FoldingSetNodeID &ID); | ||||||
5808 | static void Profile(llvm::FoldingSetNodeID &ID, | ||||||
5809 | const ObjCTypeParamDecl *OTPDecl, | ||||||
5810 | QualType CanonicalType, | ||||||
5811 | ArrayRef<ObjCProtocolDecl *> protocols); | ||||||
5812 | |||||||
5813 | ObjCTypeParamDecl *getDecl() const { return OTPDecl; } | ||||||
5814 | }; | ||||||
5815 | |||||||
5816 | /// Represents a class type in Objective C. | ||||||
5817 | /// | ||||||
5818 | /// Every Objective C type is a combination of a base type, a set of | ||||||
5819 | /// type arguments (optional, for parameterized classes) and a list of | ||||||
5820 | /// protocols. | ||||||
5821 | /// | ||||||
5822 | /// Given the following declarations: | ||||||
5823 | /// \code | ||||||
5824 | /// \@class C<T>; | ||||||
5825 | /// \@protocol P; | ||||||
5826 | /// \endcode | ||||||
5827 | /// | ||||||
5828 | /// 'C' is an ObjCInterfaceType C. It is sugar for an ObjCObjectType | ||||||
5829 | /// with base C and no protocols. | ||||||
5830 | /// | ||||||
5831 | /// 'C<P>' is an unspecialized ObjCObjectType with base C and protocol list [P]. | ||||||
5832 | /// 'C<C*>' is a specialized ObjCObjectType with type arguments 'C*' and no | ||||||
5833 | /// protocol list. | ||||||
5834 | /// 'C<C*><P>' is a specialized ObjCObjectType with base C, type arguments 'C*', | ||||||
5835 | /// and protocol list [P]. | ||||||
5836 | /// | ||||||
5837 | /// 'id' is a TypedefType which is sugar for an ObjCObjectPointerType whose | ||||||
5838 | /// pointee is an ObjCObjectType with base BuiltinType::ObjCIdType | ||||||
5839 | /// and no protocols. | ||||||
5840 | /// | ||||||
5841 | /// 'id<P>' is an ObjCObjectPointerType whose pointee is an ObjCObjectType | ||||||
5842 | /// with base BuiltinType::ObjCIdType and protocol list [P]. Eventually | ||||||
5843 | /// this should get its own sugar class to better represent the source. | ||||||
5844 | class ObjCObjectType : public Type, | ||||||
5845 | public ObjCProtocolQualifiers<ObjCObjectType> { | ||||||
5846 | friend class ObjCProtocolQualifiers<ObjCObjectType>; | ||||||
5847 | |||||||
5848 | // ObjCObjectType.NumTypeArgs - the number of type arguments stored | ||||||
5849 | // after the ObjCObjectPointerType node. | ||||||
5850 | // ObjCObjectType.NumProtocols - the number of protocols stored | ||||||
5851 | // after the type arguments of ObjCObjectPointerType node. | ||||||
5852 | // | ||||||
5853 | // These protocols are those written directly on the type. If | ||||||
5854 | // protocol qualifiers ever become additive, the iterators will need | ||||||
5855 | // to get kindof complicated. | ||||||
5856 | // | ||||||
5857 | // In the canonical object type, these are sorted alphabetically | ||||||
5858 | // and uniqued. | ||||||
5859 | |||||||
5860 | /// Either a BuiltinType or an InterfaceType or sugar for either. | ||||||
5861 | QualType BaseType; | ||||||
5862 | |||||||
5863 | /// Cached superclass type. | ||||||
5864 | mutable llvm::PointerIntPair<const ObjCObjectType *, 1, bool> | ||||||
5865 | CachedSuperClassType; | ||||||
5866 | |||||||
5867 | QualType *getTypeArgStorage(); | ||||||
5868 | const QualType *getTypeArgStorage() const { | ||||||
5869 | return const_cast<ObjCObjectType *>(this)->getTypeArgStorage(); | ||||||
5870 | } | ||||||
5871 | |||||||
5872 | ObjCProtocolDecl **getProtocolStorageImpl(); | ||||||
5873 | /// Return the number of qualifying protocols in this interface type, | ||||||
5874 | /// or 0 if there are none. | ||||||
5875 | unsigned getNumProtocolsImpl() const { | ||||||
5876 | return ObjCObjectTypeBits.NumProtocols; | ||||||
5877 | } | ||||||
5878 | void setNumProtocolsImpl(unsigned N) { | ||||||
5879 | ObjCObjectTypeBits.NumProtocols = N; | ||||||
5880 | } | ||||||
5881 | |||||||
5882 | protected: | ||||||
5883 | enum Nonce_ObjCInterface { Nonce_ObjCInterface }; | ||||||
5884 | |||||||
5885 | ObjCObjectType(QualType Canonical, QualType Base, | ||||||
5886 | ArrayRef<QualType> typeArgs, | ||||||
5887 | ArrayRef<ObjCProtocolDecl *> protocols, | ||||||
5888 | bool isKindOf); | ||||||
5889 | |||||||
5890 | ObjCObjectType(enum Nonce_ObjCInterface) | ||||||
5891 | : Type(ObjCInterface, QualType(), TypeDependence::None), | ||||||
5892 | BaseType(QualType(this_(), 0)) { | ||||||
5893 | ObjCObjectTypeBits.NumProtocols = 0; | ||||||
5894 | ObjCObjectTypeBits.NumTypeArgs = 0; | ||||||
5895 | ObjCObjectTypeBits.IsKindOf = 0; | ||||||
5896 | } | ||||||
5897 | |||||||
5898 | void computeSuperClassTypeSlow() const; | ||||||
5899 | |||||||
5900 | public: | ||||||
5901 | /// Gets the base type of this object type. This is always (possibly | ||||||
5902 | /// sugar for) one of: | ||||||
5903 | /// - the 'id' builtin type (as opposed to the 'id' type visible to the | ||||||
5904 | /// user, which is a typedef for an ObjCObjectPointerType) | ||||||
5905 | /// - the 'Class' builtin type (same caveat) | ||||||
5906 | /// - an ObjCObjectType (currently always an ObjCInterfaceType) | ||||||
5907 | QualType getBaseType() const { return BaseType; } | ||||||
5908 | |||||||
5909 | bool isObjCId() const { | ||||||
5910 | return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCId); | ||||||
5911 | } | ||||||
5912 | |||||||
5913 | bool isObjCClass() const { | ||||||
5914 | return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCClass); | ||||||
5915 | } | ||||||
5916 | |||||||
5917 | bool isObjCUnqualifiedId() const { return qual_empty() && isObjCId(); } | ||||||
5918 | bool isObjCUnqualifiedClass() const { return qual_empty() && isObjCClass(); } | ||||||
5919 | bool isObjCUnqualifiedIdOrClass() const { | ||||||
5920 | if (!qual_empty()) return false; | ||||||
5921 | if (const BuiltinType *T = getBaseType()->getAs<BuiltinType>()) | ||||||
5922 | return T->getKind() == BuiltinType::ObjCId || | ||||||
5923 | T->getKind() == BuiltinType::ObjCClass; | ||||||
5924 | return false; | ||||||
5925 | } | ||||||
5926 | bool isObjCQualifiedId() const { return !qual_empty() && isObjCId(); } | ||||||
5927 | bool isObjCQualifiedClass() const { return !qual_empty() && isObjCClass(); } | ||||||
5928 | |||||||
5929 | /// Gets the interface declaration for this object type, if the base type | ||||||
5930 | /// really is an interface. | ||||||
5931 | ObjCInterfaceDecl *getInterface() const; | ||||||
5932 | |||||||
5933 | /// Determine whether this object type is "specialized", meaning | ||||||
5934 | /// that it has type arguments. | ||||||
5935 | bool isSpecialized() const; | ||||||
5936 | |||||||
5937 | /// Determine whether this object type was written with type arguments. | ||||||
5938 | bool isSpecializedAsWritten() const { | ||||||
5939 | return ObjCObjectTypeBits.NumTypeArgs > 0; | ||||||
5940 | } | ||||||
5941 | |||||||
5942 | /// Determine whether this object type is "unspecialized", meaning | ||||||
5943 | /// that it has no type arguments. | ||||||
5944 | bool isUnspecialized() const { return !isSpecialized(); } | ||||||
5945 | |||||||
5946 | /// Determine whether this object type is "unspecialized" as | ||||||
5947 | /// written, meaning that it has no type arguments. | ||||||
5948 | bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); } | ||||||
5949 | |||||||
5950 | /// Retrieve the type arguments of this object type (semantically). | ||||||
5951 | ArrayRef<QualType> getTypeArgs() const; | ||||||
5952 | |||||||
5953 | /// Retrieve the type arguments of this object type as they were | ||||||
5954 | /// written. | ||||||
5955 | ArrayRef<QualType> getTypeArgsAsWritten() const { | ||||||
5956 | return llvm::makeArrayRef(getTypeArgStorage(), | ||||||
5957 | ObjCObjectTypeBits.NumTypeArgs); | ||||||
5958 | } | ||||||
5959 | |||||||
5960 | /// Whether this is a "__kindof" type as written. | ||||||
5961 | bool isKindOfTypeAsWritten() const { return ObjCObjectTypeBits.IsKindOf; } | ||||||
5962 | |||||||
5963 | /// Whether this ia a "__kindof" type (semantically). | ||||||
5964 | bool isKindOfType() const; | ||||||
5965 | |||||||
5966 | /// Retrieve the type of the superclass of this object type. | ||||||
5967 | /// | ||||||
5968 | /// This operation substitutes any type arguments into the | ||||||
5969 | /// superclass of the current class type, potentially producing a | ||||||
5970 | /// specialization of the superclass type. Produces a null type if | ||||||
5971 | /// there is no superclass. | ||||||
5972 | QualType getSuperClassType() const { | ||||||
5973 | if (!CachedSuperClassType.getInt()) | ||||||
5974 | computeSuperClassTypeSlow(); | ||||||
5975 | |||||||
5976 | assert(CachedSuperClassType.getInt() && "Superclass not set?")(static_cast <bool> (CachedSuperClassType.getInt() && "Superclass not set?") ? void (0) : __assert_fail ("CachedSuperClassType.getInt() && \"Superclass not set?\"" , "clang/include/clang/AST/Type.h", 5976, __extension__ __PRETTY_FUNCTION__ )); | ||||||
5977 | return QualType(CachedSuperClassType.getPointer(), 0); | ||||||
5978 | } | ||||||
5979 | |||||||
5980 | /// Strip off the Objective-C "kindof" type and (with it) any | ||||||
5981 | /// protocol qualifiers. | ||||||
5982 | QualType stripObjCKindOfTypeAndQuals(const ASTContext &ctx) const; | ||||||
5983 | |||||||
5984 | bool isSugared() const { return false; } | ||||||
5985 | QualType desugar() const { return QualType(this, 0); } | ||||||
5986 | |||||||
5987 | static bool classof(const Type *T) { | ||||||
5988 | return T->getTypeClass() == ObjCObject || | ||||||
5989 | T->getTypeClass() == ObjCInterface; | ||||||
5990 | } | ||||||
5991 | }; | ||||||
5992 | |||||||
5993 | /// A class providing a concrete implementation | ||||||
5994 | /// of ObjCObjectType, so as to not increase the footprint of | ||||||
5995 | /// ObjCInterfaceType. Code outside of ASTContext and the core type | ||||||
5996 | /// system should not reference this type. | ||||||
5997 | class ObjCObjectTypeImpl : public ObjCObjectType, public llvm::FoldingSetNode { | ||||||
5998 | friend class ASTContext; | ||||||
5999 | |||||||
6000 | // If anyone adds fields here, ObjCObjectType::getProtocolStorage() | ||||||
6001 | // will need to be modified. | ||||||
6002 | |||||||
6003 | ObjCObjectTypeImpl(QualType Canonical, QualType Base, | ||||||
6004 | ArrayRef<QualType> typeArgs, | ||||||
6005 | ArrayRef<ObjCProtocolDecl *> protocols, | ||||||
6006 | bool isKindOf) | ||||||
6007 | : ObjCObjectType(Canonical, Base, typeArgs, protocols, isKindOf) {} | ||||||
6008 | |||||||
6009 | public: | ||||||
6010 | void Profile(llvm::FoldingSetNodeID &ID); | ||||||
6011 | static void Profile(llvm::FoldingSetNodeID &ID, | ||||||
6012 | QualType Base, | ||||||
6013 | ArrayRef<QualType> typeArgs, | ||||||
6014 | ArrayRef<ObjCProtocolDecl *> protocols, | ||||||
6015 | bool isKindOf); | ||||||
6016 | }; | ||||||
6017 | |||||||
6018 | inline QualType *ObjCObjectType::getTypeArgStorage() { | ||||||
6019 | return reinterpret_cast<QualType *>(static_cast<ObjCObjectTypeImpl*>(this)+1); | ||||||
6020 | } | ||||||
6021 | |||||||
6022 | inline ObjCProtocolDecl **ObjCObjectType::getProtocolStorageImpl() { | ||||||
6023 | return reinterpret_cast<ObjCProtocolDecl**>( | ||||||
6024 | getTypeArgStorage() + ObjCObjectTypeBits.NumTypeArgs); | ||||||
6025 | } | ||||||
6026 | |||||||
6027 | inline ObjCProtocolDecl **ObjCTypeParamType::getProtocolStorageImpl() { | ||||||
6028 | return reinterpret_cast<ObjCProtocolDecl**>( | ||||||
6029 | static_cast<ObjCTypeParamType*>(this)+1); | ||||||
6030 | } | ||||||
6031 | |||||||
6032 | /// Interfaces are the core concept in Objective-C for object oriented design. | ||||||
6033 | /// They basically correspond to C++ classes. There are two kinds of interface | ||||||
6034 | /// types: normal interfaces like `NSString`, and qualified interfaces, which | ||||||
6035 | /// are qualified with a protocol list like `NSString<NSCopyable, NSAmazing>`. | ||||||
6036 | /// | ||||||
6037 | /// ObjCInterfaceType guarantees the following properties when considered | ||||||
6038 | /// as a subtype of its superclass, ObjCObjectType: | ||||||
6039 | /// - There are no protocol qualifiers. To reinforce this, code which | ||||||
6040 | /// tries to invoke the protocol methods via an ObjCInterfaceType will | ||||||
6041 | /// fail to compile. | ||||||
6042 | /// - It is its own base type. That is, if T is an ObjCInterfaceType*, | ||||||
6043 | /// T->getBaseType() == QualType(T, 0). | ||||||
6044 | class ObjCInterfaceType : public ObjCObjectType { | ||||||
6045 | friend class ASTContext; // ASTContext creates these. | ||||||
6046 | friend class ASTReader; | ||||||
6047 | template <class T> friend class serialization::AbstractTypeReader; | ||||||
6048 | |||||||
6049 | ObjCInterfaceDecl *Decl; | ||||||
6050 | |||||||
6051 | ObjCInterfaceType(const ObjCInterfaceDecl *D) | ||||||
6052 | : ObjCObjectType(Nonce_ObjCInterface), | ||||||
6053 | Decl(const_cast<ObjCInterfaceDecl*>(D)) {} | ||||||
6054 | |||||||
6055 | public: | ||||||
6056 | /// Get the declaration of this interface. | ||||||
6057 | ObjCInterfaceDecl *getDecl() const; | ||||||
6058 | |||||||
6059 | bool isSugared() const { return false; } | ||||||
6060 | QualType desugar() const { return QualType(this, 0); } | ||||||
6061 | |||||||
6062 | static bool classof(const Type *T) { | ||||||
6063 | return T->getTypeClass() == ObjCInterface; | ||||||
6064 | } | ||||||
6065 | |||||||
6066 | // Nonsense to "hide" certain members of ObjCObjectType within this | ||||||
6067 | // class. People asking for protocols on an ObjCInterfaceType are | ||||||
6068 | // not going to get what they want: ObjCInterfaceTypes are | ||||||
6069 | // guaranteed to have no protocols. | ||||||
6070 | enum { | ||||||
6071 | qual_iterator, | ||||||
6072 | qual_begin, | ||||||
6073 | qual_end, | ||||||
6074 | getNumProtocols, | ||||||
6075 | getProtocol | ||||||
6076 | }; | ||||||
6077 | }; | ||||||
6078 | |||||||
6079 | inline ObjCInterfaceDecl *ObjCObjectType::getInterface() const { | ||||||
6080 | QualType baseType = getBaseType(); | ||||||
6081 | while (const auto *ObjT = baseType->getAs<ObjCObjectType>()) { | ||||||
6082 | if (const auto *T = dyn_cast<ObjCInterfaceType>(ObjT)) | ||||||
6083 | return T->getDecl(); | ||||||
6084 | |||||||
6085 | baseType = ObjT->getBaseType(); | ||||||
6086 | } | ||||||
6087 | |||||||
6088 | return nullptr; | ||||||
6089 | } | ||||||
6090 | |||||||
6091 | /// Represents a pointer to an Objective C object. | ||||||
6092 | /// | ||||||
6093 | /// These are constructed from pointer declarators when the pointee type is | ||||||
6094 | /// an ObjCObjectType (or sugar for one). In addition, the 'id' and 'Class' | ||||||
6095 | /// types are typedefs for these, and the protocol-qualified types 'id<P>' | ||||||
6096 | /// and 'Class<P>' are translated into these. | ||||||
6097 | /// | ||||||
6098 | /// Pointers to pointers to Objective C objects are still PointerTypes; | ||||||
6099 | /// only the first level of pointer gets it own type implementation. | ||||||
6100 | class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode { | ||||||
6101 | friend class ASTContext; // ASTContext creates these. | ||||||
6102 | |||||||
6103 | QualType PointeeType; | ||||||
6104 | |||||||
6105 | ObjCObjectPointerType(QualType Canonical, QualType Pointee) | ||||||
6106 | : Type(ObjCObjectPointer, Canonical, Pointee->getDependence()), | ||||||
6107 | PointeeType(Pointee) {} | ||||||
6108 | |||||||
6109 | public: | ||||||
6110 | /// Gets the type pointed to by this ObjC pointer. | ||||||
6111 | /// The result will always be an ObjCObjectType or sugar thereof. | ||||||
6112 | QualType getPointeeType() const { return PointeeType; } | ||||||
6113 | |||||||
6114 | /// Gets the type pointed to by this ObjC pointer. Always returns non-null. | ||||||
6115 | /// | ||||||
6116 | /// This method is equivalent to getPointeeType() except that | ||||||
6117 | /// it discards any typedefs (or other sugar) between this | ||||||
6118 | /// type and the "outermost" object type. So for: | ||||||
6119 | /// \code | ||||||
6120 | /// \@class A; \@protocol P; \@protocol Q; | ||||||
6121 | /// typedef A<P> AP; | ||||||
6122 | /// typedef A A1; | ||||||
6123 | /// typedef A1<P> A1P; | ||||||
6124 | /// typedef A1P<Q> A1PQ; | ||||||
6125 | /// \endcode | ||||||
6126 | /// For 'A*', getObjectType() will return 'A'. | ||||||
6127 | /// For 'A<P>*', getObjectType() will return 'A<P>'. | ||||||
6128 | /// For 'AP*', getObjectType() will return 'A<P>'. | ||||||
6129 | /// For 'A1*', getObjectType() will return 'A'. | ||||||
6130 | /// For 'A1<P>*', getObjectType() will return 'A1<P>'. | ||||||
6131 | /// For 'A1P*', getObjectType() will return 'A1<P>'. | ||||||
6132 | /// For 'A1PQ*', getObjectType() will return 'A1<Q>', because | ||||||
6133 | /// adding protocols to a protocol-qualified base discards the | ||||||
6134 | /// old qualifiers (for now). But if it didn't, getObjectType() | ||||||
6135 | /// would return 'A1P<Q>' (and we'd have to make iterating over | ||||||
6136 | /// qualifiers more complicated). | ||||||
6137 | const ObjCObjectType *getObjectType() const { | ||||||
6138 | return PointeeType->castAs<ObjCObjectType>(); | ||||||
6139 | } | ||||||
6140 | |||||||
6141 | /// If this pointer points to an Objective C | ||||||
6142 | /// \@interface type, gets the type for that interface. Any protocol | ||||||
6143 | /// qualifiers on the interface are ignored. | ||||||
6144 | /// | ||||||
6145 | /// \return null if the base type for this pointer is 'id' or 'Class' | ||||||
6146 | const ObjCInterfaceType *getInterfaceType() const; | ||||||
6147 | |||||||
6148 | /// If this pointer points to an Objective \@interface | ||||||
6149 | /// type, gets the declaration for that interface. | ||||||
6150 | /// | ||||||
6151 | /// \return null if the base type for this pointer is 'id' or 'Class' | ||||||
6152 | ObjCInterfaceDecl *getInterfaceDecl() const { | ||||||
6153 | return getObjectType()->getInterface(); | ||||||
6154 | } | ||||||
6155 | |||||||
6156 | /// True if this is equivalent to the 'id' type, i.e. if | ||||||
6157 | /// its object type is the primitive 'id' type with no protocols. | ||||||
6158 | bool isObjCIdType() const { | ||||||
6159 | return getObjectType()->isObjCUnqualifiedId(); | ||||||
6160 | } | ||||||
6161 | |||||||
6162 | /// True if this is equivalent to the 'Class' type, | ||||||
6163 | /// i.e. if its object tive is the primitive 'Class' type with no protocols. | ||||||
6164 | bool isObjCClassType() const { | ||||||
6165 | return getObjectType()->isObjCUnqualifiedClass(); | ||||||
6166 | } | ||||||
6167 | |||||||
6168 | /// True if this is equivalent to the 'id' or 'Class' type, | ||||||
6169 | bool isObjCIdOrClassType() const { | ||||||
6170 | return getObjectType()->isObjCUnqualifiedIdOrClass(); | ||||||
6171 | } | ||||||
6172 | |||||||
6173 | /// True if this is equivalent to 'id<P>' for some non-empty set of | ||||||
6174 | /// protocols. | ||||||
6175 | bool isObjCQualifiedIdType() const { | ||||||
6176 | return getObjectType()->isObjCQualifiedId(); | ||||||
6177 | } | ||||||
6178 | |||||||
6179 | /// True if this is equivalent to 'Class<P>' for some non-empty set of | ||||||
6180 | /// protocols. | ||||||
6181 | bool isObjCQualifiedClassType() const { | ||||||
6182 | return getObjectType()->isObjCQualifiedClass(); | ||||||
6183 | } | ||||||
6184 | |||||||
6185 | /// Whether this is a "__kindof" type. | ||||||
6186 | bool isKindOfType() const { return getObjectType()->isKindOfType(); } | ||||||
6187 | |||||||
6188 | /// Whether this type is specialized, meaning that it has type arguments. | ||||||
6189 | bool isSpecialized() const { return getObjectType()->isSpecialized(); } | ||||||
6190 | |||||||
6191 | /// Whether this type is specialized, meaning that it has type arguments. | ||||||
6192 | bool isSpecializedAsWritten() const { | ||||||
6193 | return getObjectType()->isSpecializedAsWritten(); | ||||||
6194 | } | ||||||
6195 | |||||||
6196 | /// Whether this type is unspecialized, meaning that is has no type arguments. | ||||||
6197 | bool isUnspecialized() const { return getObjectType()->isUnspecialized(); } | ||||||
6198 | |||||||
6199 | /// Determine whether this object type is "unspecialized" as | ||||||
6200 | /// written, meaning that it has no type arguments. | ||||||
6201 | bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); } | ||||||
6202 | |||||||
6203 | /// Retrieve the type arguments for this type. | ||||||
6204 | ArrayRef<QualType> getTypeArgs() const { | ||||||
6205 | return getObjectType()->getTypeArgs(); | ||||||
6206 | } | ||||||
6207 | |||||||
6208 | /// Retrieve the type arguments for this type. | ||||||
6209 | ArrayRef<QualType> getTypeArgsAsWritten() const { | ||||||
6210 | return getObjectType()->getTypeArgsAsWritten(); | ||||||
6211 | } | ||||||
6212 | |||||||
6213 | /// An iterator over the qualifiers on the object type. Provided | ||||||
6214 | /// for convenience. This will always iterate over the full set of | ||||||
6215 | /// protocols on a type, not just those provided directly. | ||||||
6216 | using qual_iterator = ObjCObjectType::qual_iterator; | ||||||
6217 | using qual_range = llvm::iterator_range<qual_iterator>; | ||||||
6218 | |||||||
6219 | qual_range quals() const { return qual_range(qual_begin(), qual_end()); } | ||||||
6220 | |||||||
6221 | qual_iterator qual_begin() const { | ||||||
6222 | return getObjectType()->qual_begin(); | ||||||
6223 | } | ||||||
6224 | |||||||
6225 | qual_iterator qual_end() const { | ||||||
6226 | return getObjectType()->qual_end(); | ||||||
6227 | } | ||||||
6228 | |||||||
6229 | bool qual_empty() const { return getObjectType()->qual_empty(); } | ||||||
6230 | |||||||
6231 | /// Return the number of qualifying protocols on the object type. | ||||||
6232 | unsigned getNumProtocols() const { | ||||||
6233 | return getObjectType()->getNumProtocols(); | ||||||
6234 | } | ||||||
6235 | |||||||
6236 | /// Retrieve a qualifying protocol by index on the object type. | ||||||
6237 | ObjCProtocolDecl *getProtocol(unsigned I) const { | ||||||
6238 | return getObjectType()->getProtocol(I); | ||||||
6239 | } | ||||||
6240 | |||||||
6241 | bool isSugared() const { return false; } | ||||||
6242 | QualType desugar() const { return QualType(this, 0); } | ||||||
6243 | |||||||
6244 | /// Retrieve the type of the superclass of this object pointer type. | ||||||
6245 | /// | ||||||
6246 | /// This operation substitutes any type arguments into the | ||||||
6247 | /// superclass of the current class type, potentially producing a | ||||||
6248 | /// pointer to a specialization of the superclass type. Produces a | ||||||
6249 | /// null type if there is no superclass. | ||||||
6250 | QualType getSuperClassType() const; | ||||||
6251 | |||||||
6252 | /// Strip off the Objective-C "kindof" type and (with it) any | ||||||
6253 | /// protocol qualifiers. | ||||||
6254 | const ObjCObjectPointerType *stripObjCKindOfTypeAndQuals( | ||||||
6255 | const ASTContext &ctx) const; | ||||||
6256 | |||||||
6257 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
6258 | Profile(ID, getPointeeType()); | ||||||
6259 | } | ||||||
6260 | |||||||
6261 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { | ||||||
6262 | ID.AddPointer(T.getAsOpaquePtr()); | ||||||
6263 | } | ||||||
6264 | |||||||
6265 | static bool classof(const Type *T) { | ||||||
6266 | return T->getTypeClass() == ObjCObjectPointer; | ||||||
6267 | } | ||||||
6268 | }; | ||||||
6269 | |||||||
6270 | class AtomicType : public Type, public llvm::FoldingSetNode { | ||||||
6271 | friend class ASTContext; // ASTContext creates these. | ||||||
6272 | |||||||
6273 | QualType ValueType; | ||||||
6274 | |||||||
6275 | AtomicType(QualType ValTy, QualType Canonical) | ||||||
6276 | : Type(Atomic, Canonical, ValTy->getDependence()), ValueType(ValTy) {} | ||||||
6277 | |||||||
6278 | public: | ||||||
6279 | /// Gets the type contained by this atomic type, i.e. | ||||||
6280 | /// the type returned by performing an atomic load of this atomic type. | ||||||
6281 | QualType getValueType() const { return ValueType; } | ||||||
6282 | |||||||
6283 | bool isSugared() const { return false; } | ||||||
6284 | QualType desugar() const { return QualType(this, 0); } | ||||||
6285 | |||||||
6286 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
6287 | Profile(ID, getValueType()); | ||||||
6288 | } | ||||||
6289 | |||||||
6290 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { | ||||||
6291 | ID.AddPointer(T.getAsOpaquePtr()); | ||||||
6292 | } | ||||||
6293 | |||||||
6294 | static bool classof(const Type *T) { | ||||||
6295 | return T->getTypeClass() == Atomic; | ||||||
6296 | } | ||||||
6297 | }; | ||||||
6298 | |||||||
6299 | /// PipeType - OpenCL20. | ||||||
6300 | class PipeType : public Type, public llvm::FoldingSetNode { | ||||||
6301 | friend class ASTContext; // ASTContext creates these. | ||||||
6302 | |||||||
6303 | QualType ElementType; | ||||||
6304 | bool isRead; | ||||||
6305 | |||||||
6306 | PipeType(QualType elemType, QualType CanonicalPtr, bool isRead) | ||||||
6307 | : Type(Pipe, CanonicalPtr, elemType->getDependence()), | ||||||
6308 | ElementType(elemType), isRead(isRead) {} | ||||||
6309 | |||||||
6310 | public: | ||||||
6311 | QualType getElementType() const { return ElementType; } | ||||||
6312 | |||||||
6313 | bool isSugared() const { return false; } | ||||||
6314 | |||||||
6315 | QualType desugar() const { return QualType(this, 0); } | ||||||
6316 | |||||||
6317 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
6318 | Profile(ID, getElementType(), isReadOnly()); | ||||||
6319 | } | ||||||
6320 | |||||||
6321 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T, bool isRead) { | ||||||
6322 | ID.AddPointer(T.getAsOpaquePtr()); | ||||||
6323 | ID.AddBoolean(isRead); | ||||||
6324 | } | ||||||
6325 | |||||||
6326 | static bool classof(const Type *T) { | ||||||
6327 | return T->getTypeClass() == Pipe; | ||||||
6328 | } | ||||||
6329 | |||||||
6330 | bool isReadOnly() const { return isRead; } | ||||||
6331 | }; | ||||||
6332 | |||||||
6333 | /// A fixed int type of a specified bitwidth. | ||||||
6334 | class BitIntType final : public Type, public llvm::FoldingSetNode { | ||||||
6335 | friend class ASTContext; | ||||||
6336 | unsigned IsUnsigned : 1; | ||||||
6337 | unsigned NumBits : 24; | ||||||
6338 | |||||||
6339 | protected: | ||||||
6340 | BitIntType(bool isUnsigned, unsigned NumBits); | ||||||
6341 | |||||||
6342 | public: | ||||||
6343 | bool isUnsigned() const { return IsUnsigned; } | ||||||
6344 | bool isSigned() const { return !IsUnsigned; } | ||||||
6345 | unsigned getNumBits() const { return NumBits; } | ||||||
6346 | |||||||
6347 | bool isSugared() const { return false; } | ||||||
6348 | QualType desugar() const { return QualType(this, 0); } | ||||||
6349 | |||||||
6350 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
6351 | Profile(ID, isUnsigned(), getNumBits()); | ||||||
6352 | } | ||||||
6353 | |||||||
6354 | static void Profile(llvm::FoldingSetNodeID &ID, bool IsUnsigned, | ||||||
6355 | unsigned NumBits) { | ||||||
6356 | ID.AddBoolean(IsUnsigned); | ||||||
6357 | ID.AddInteger(NumBits); | ||||||
6358 | } | ||||||
6359 | |||||||
6360 | static bool classof(const Type *T) { return T->getTypeClass() == BitInt; } | ||||||
6361 | }; | ||||||
6362 | |||||||
6363 | class DependentBitIntType final : public Type, public llvm::FoldingSetNode { | ||||||
6364 | friend class ASTContext; | ||||||
6365 | const ASTContext &Context; | ||||||
6366 | llvm::PointerIntPair<Expr*, 1, bool> ExprAndUnsigned; | ||||||
6367 | |||||||
6368 | protected: | ||||||
6369 | DependentBitIntType(const ASTContext &Context, bool IsUnsigned, | ||||||
6370 | Expr *NumBits); | ||||||
6371 | |||||||
6372 | public: | ||||||
6373 | bool isUnsigned() const; | ||||||
6374 | bool isSigned() const { return !isUnsigned(); } | ||||||
6375 | Expr *getNumBitsExpr() const; | ||||||
6376 | |||||||
6377 | bool isSugared() const { return false; } | ||||||
6378 | QualType desugar() const { return QualType(this, 0); } | ||||||
6379 | |||||||
6380 | void Profile(llvm::FoldingSetNodeID &ID) { | ||||||
6381 | Profile(ID, Context, isUnsigned(), getNumBitsExpr()); | ||||||
6382 | } | ||||||
6383 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, | ||||||
6384 | bool IsUnsigned, Expr *NumBitsExpr); | ||||||
6385 | |||||||
6386 | static bool classof(const Type *T) { | ||||||
6387 | return T->getTypeClass() == DependentBitInt; | ||||||
6388 | } | ||||||
6389 | }; | ||||||
6390 | |||||||
6391 | /// A qualifier set is used to build a set of qualifiers. | ||||||
6392 | class QualifierCollector : public Qualifiers { | ||||||
6393 | public: | ||||||
6394 | QualifierCollector(Qualifiers Qs = Qualifiers()) : Qualifiers(Qs) {} | ||||||
6395 | |||||||
6396 | /// Collect any qualifiers on the given type and return an | ||||||
6397 | /// unqualified type. The qualifiers are assumed to be consistent | ||||||
6398 | /// with those already in the type. | ||||||
6399 | const Type *strip(QualType type) { | ||||||
6400 | addFastQualifiers(type.getLocalFastQualifiers()); | ||||||
6401 | if (!type.hasLocalNonFastQualifiers()) | ||||||
6402 | return type.getTypePtrUnsafe(); | ||||||
6403 | |||||||
6404 | const ExtQuals *extQuals = type.getExtQualsUnsafe(); | ||||||
6405 | addConsistentQualifiers(extQuals->getQualifiers()); | ||||||
6406 | return extQuals->getBaseType(); | ||||||
6407 | } | ||||||
6408 | |||||||
6409 | /// Apply the collected qualifiers to the given type. | ||||||
6410 | QualType apply(const ASTContext &Context, QualType QT) const; | ||||||
6411 | |||||||
6412 | /// Apply the collected qualifiers to the given type. | ||||||
6413 | QualType apply(const ASTContext &Context, const Type* T) const; | ||||||
6414 | }; | ||||||
6415 | |||||||
6416 | /// A container of type source information. | ||||||
6417 | /// | ||||||
6418 | /// A client can read the relevant info using TypeLoc wrappers, e.g: | ||||||
6419 | /// @code | ||||||
6420 | /// TypeLoc TL = TypeSourceInfo->getTypeLoc(); | ||||||
6421 | /// TL.getBeginLoc().print(OS, SrcMgr); | ||||||
6422 | /// @endcode | ||||||
6423 | class alignas(8) TypeSourceInfo { | ||||||
6424 | // Contains a memory block after the class, used for type source information, | ||||||
6425 | // allocated by ASTContext. | ||||||
6426 | friend class ASTContext; | ||||||
6427 | |||||||
6428 | QualType Ty; | ||||||
6429 | |||||||
6430 | TypeSourceInfo(QualType ty) : Ty(ty) {} | ||||||
6431 | |||||||
6432 | public: | ||||||
6433 | /// Return the type wrapped by this type source info. | ||||||
6434 | QualType getType() const { return Ty; } | ||||||
6435 | |||||||
6436 | /// Return the TypeLoc wrapper for the type source info. | ||||||
6437 | TypeLoc getTypeLoc() const; // implemented in TypeLoc.h | ||||||
6438 | |||||||
6439 | /// Override the type stored in this TypeSourceInfo. Use with caution! | ||||||
6440 | void overrideType(QualType T) { Ty = T; } | ||||||
6441 | }; | ||||||
6442 | |||||||
6443 | // Inline function definitions. | ||||||
6444 | |||||||
6445 | inline SplitQualType SplitQualType::getSingleStepDesugaredType() const { | ||||||
6446 | SplitQualType desugar = | ||||||
6447 | Ty->getLocallyUnqualifiedSingleStepDesugaredType().split(); | ||||||
6448 | desugar.Quals.addConsistentQualifiers(Quals); | ||||||
6449 | return desugar; | ||||||
6450 | } | ||||||
6451 | |||||||
6452 | inline const Type *QualType::getTypePtr() const { | ||||||
6453 | return getCommonPtr()->BaseType; | ||||||
6454 | } | ||||||
6455 | |||||||
6456 | inline const Type *QualType::getTypePtrOrNull() const { | ||||||
6457 | return (isNull() ? nullptr : getCommonPtr()->BaseType); | ||||||
6458 | } | ||||||
6459 | |||||||
6460 | inline SplitQualType QualType::split() const { | ||||||
6461 | if (!hasLocalNonFastQualifiers()) | ||||||
6462 | return SplitQualType(getTypePtrUnsafe(), | ||||||
6463 | Qualifiers::fromFastMask(getLocalFastQualifiers())); | ||||||
6464 | |||||||
6465 | const ExtQuals *eq = getExtQualsUnsafe(); | ||||||
6466 | Qualifiers qs = eq->getQualifiers(); | ||||||
6467 | qs.addFastQualifiers(getLocalFastQualifiers()); | ||||||
6468 | return SplitQualType(eq->getBaseType(), qs); | ||||||
6469 | } | ||||||
6470 | |||||||
6471 | inline Qualifiers QualType::getLocalQualifiers() const { | ||||||
6472 | Qualifiers Quals; | ||||||
6473 | if (hasLocalNonFastQualifiers()) | ||||||
6474 | Quals = getExtQualsUnsafe()->getQualifiers(); | ||||||
6475 | Quals.addFastQualifiers(getLocalFastQualifiers()); | ||||||
6476 | return Quals; | ||||||
6477 | } | ||||||
6478 | |||||||
6479 | inline Qualifiers QualType::getQualifiers() const { | ||||||
6480 | Qualifiers quals = getCommonPtr()->CanonicalType.getLocalQualifiers(); | ||||||
6481 | quals.addFastQualifiers(getLocalFastQualifiers()); | ||||||
6482 | return quals; | ||||||
6483 | } | ||||||
6484 | |||||||
6485 | inline unsigned QualType::getCVRQualifiers() const { | ||||||
6486 | unsigned cvr = getCommonPtr()->CanonicalType.getLocalCVRQualifiers(); | ||||||
6487 | cvr |= getLocalCVRQualifiers(); | ||||||
6488 | return cvr; | ||||||
6489 | } | ||||||
6490 | |||||||
6491 | inline QualType QualType::getCanonicalType() const { | ||||||
6492 | QualType canon = getCommonPtr()->CanonicalType; | ||||||
6493 | return canon.withFastQualifiers(getLocalFastQualifiers()); | ||||||
6494 | } | ||||||
6495 | |||||||
6496 | inline bool QualType::isCanonical() const { | ||||||
6497 | return getTypePtr()->isCanonicalUnqualified(); | ||||||
6498 | } | ||||||
6499 | |||||||
6500 | inline bool QualType::isCanonicalAsParam() const { | ||||||
6501 | if (!isCanonical()) return false; | ||||||
6502 | if (hasLocalQualifiers()) return false; | ||||||
6503 | |||||||
6504 | const Type *T = getTypePtr(); | ||||||
6505 | if (T->isVariablyModifiedType() && T->hasSizedVLAType()) | ||||||
6506 | return false; | ||||||
6507 | |||||||
6508 | return !isa<FunctionType>(T) && !isa<ArrayType>(T); | ||||||
6509 | } | ||||||
6510 | |||||||
6511 | inline bool QualType::isConstQualified() const { | ||||||
6512 | return isLocalConstQualified() || | ||||||
6513 | getCommonPtr()->CanonicalType.isLocalConstQualified(); | ||||||
6514 | } | ||||||
6515 | |||||||
6516 | inline bool QualType::isRestrictQualified() const { | ||||||
6517 | return isLocalRestrictQualified() || | ||||||
6518 | getCommonPtr()->CanonicalType.isLocalRestrictQualified(); | ||||||
6519 | } | ||||||
6520 | |||||||
6521 | |||||||
6522 | inline bool QualType::isVolatileQualified() const { | ||||||
6523 | return isLocalVolatileQualified() || | ||||||
6524 | getCommonPtr()->CanonicalType.isLocalVolatileQualified(); | ||||||
6525 | } | ||||||
6526 | |||||||
6527 | inline bool QualType::hasQualifiers() const { | ||||||
6528 | return hasLocalQualifiers() || | ||||||
6529 | getCommonPtr()->CanonicalType.hasLocalQualifiers(); | ||||||
6530 | } | ||||||
6531 | |||||||
6532 | inline QualType QualType::getUnqualifiedType() const { | ||||||
6533 | if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) | ||||||
6534 | return QualType(getTypePtr(), 0); | ||||||
6535 | |||||||
6536 | return QualType(getSplitUnqualifiedTypeImpl(*this).Ty, 0); | ||||||
6537 | } | ||||||
6538 | |||||||
6539 | inline SplitQualType QualType::getSplitUnqualifiedType() const { | ||||||
6540 | if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) | ||||||
6541 | return split(); | ||||||
6542 | |||||||
6543 | return getSplitUnqualifiedTypeImpl(*this); | ||||||
6544 | } | ||||||
6545 | |||||||
6546 | inline void QualType::removeLocalConst() { | ||||||
6547 | removeLocalFastQualifiers(Qualifiers::Const); | ||||||
6548 | } | ||||||
6549 | |||||||
6550 | inline void QualType::removeLocalRestrict() { | ||||||
6551 | removeLocalFastQualifiers(Qualifiers::Restrict); | ||||||
6552 | } | ||||||
6553 | |||||||
6554 | inline void QualType::removeLocalVolatile() { | ||||||
6555 | removeLocalFastQualifiers(Qualifiers::Volatile); | ||||||
6556 | } | ||||||
6557 | |||||||
6558 | inline void QualType::removeLocalCVRQualifiers(unsigned Mask) { | ||||||
6559 | assert(!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits")(static_cast <bool> (!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits") ? void (0) : __assert_fail ("!(Mask & ~Qualifiers::CVRMask) && \"mask has non-CVR bits\"" , "clang/include/clang/AST/Type.h", 6559, __extension__ __PRETTY_FUNCTION__ )); | ||||||
6560 | static_assert((int)Qualifiers::CVRMask == (int)Qualifiers::FastMask, | ||||||
6561 | "Fast bits differ from CVR bits!"); | ||||||
6562 | |||||||
6563 | // Fast path: we don't need to touch the slow qualifiers. | ||||||
6564 | removeLocalFastQualifiers(Mask); | ||||||
6565 | } | ||||||
6566 | |||||||
6567 | /// Check if this type has any address space qualifier. | ||||||
6568 | inline bool QualType::hasAddressSpace() const { | ||||||
6569 | return getQualifiers().hasAddressSpace(); | ||||||
6570 | } | ||||||
6571 | |||||||
6572 | /// Return the address space of this type. | ||||||
6573 | inline LangAS QualType::getAddressSpace() const { | ||||||
6574 | return getQualifiers().getAddressSpace(); | ||||||
6575 | } | ||||||
6576 | |||||||
6577 | /// Return the gc attribute of this type. | ||||||
6578 | inline Qualifiers::GC QualType::getObjCGCAttr() const { | ||||||
6579 | return getQualifiers().getObjCGCAttr(); | ||||||
6580 | } | ||||||
6581 | |||||||
6582 | inline bool QualType::hasNonTrivialToPrimitiveDefaultInitializeCUnion() const { | ||||||
6583 | if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) | ||||||
6584 | return hasNonTrivialToPrimitiveDefaultInitializeCUnion(RD); | ||||||
6585 | return false; | ||||||
6586 | } | ||||||
6587 | |||||||
6588 | inline bool QualType::hasNonTrivialToPrimitiveDestructCUnion() const { | ||||||
6589 | if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) | ||||||
6590 | return hasNonTrivialToPrimitiveDestructCUnion(RD); | ||||||
6591 | return false; | ||||||
6592 | } | ||||||
6593 | |||||||
6594 | inline bool QualType::hasNonTrivialToPrimitiveCopyCUnion() const { | ||||||
6595 | if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) | ||||||
6596 | return hasNonTrivialToPrimitiveCopyCUnion(RD); | ||||||
6597 | return false; | ||||||
6598 | } | ||||||
6599 | |||||||
6600 | inline FunctionType::ExtInfo getFunctionExtInfo(const Type &t) { | ||||||
6601 | if (const auto *PT = t.getAs<PointerType>()) { | ||||||
6602 | if (const auto *FT = PT->getPointeeType()->getAs<FunctionType>()) | ||||||
6603 | return FT->getExtInfo(); | ||||||
6604 | } else if (const auto *FT = t.getAs<FunctionType>()) | ||||||
6605 | return FT->getExtInfo(); | ||||||
6606 | |||||||
6607 | return FunctionType::ExtInfo(); | ||||||
6608 | } | ||||||
6609 | |||||||
6610 | inline FunctionType::ExtInfo getFunctionExtInfo(QualType t) { | ||||||
6611 | return getFunctionExtInfo(*t); | ||||||
6612 | } | ||||||
6613 | |||||||
6614 | /// Determine whether this type is more | ||||||
6615 | /// qualified than the Other type. For example, "const volatile int" | ||||||
6616 | /// is more qualified than "const int", "volatile int", and | ||||||
6617 | /// "int". However, it is not more qualified than "const volatile | ||||||
6618 | /// int". | ||||||
6619 | inline bool QualType::isMoreQualifiedThan(QualType other) const { | ||||||
6620 | Qualifiers MyQuals = getQualifiers(); | ||||||
6621 | Qualifiers OtherQuals = other.getQualifiers(); | ||||||
6622 | return (MyQuals != OtherQuals && MyQuals.compatiblyIncludes(OtherQuals)); | ||||||
6623 | } | ||||||
6624 | |||||||
6625 | /// Determine whether this type is at last | ||||||
6626 | /// as qualified as the Other type. For example, "const volatile | ||||||
6627 | /// int" is at least as qualified as "const int", "volatile int", | ||||||
6628 | /// "int", and "const volatile int". | ||||||
6629 | inline bool QualType::isAtLeastAsQualifiedAs(QualType other) const { | ||||||
6630 | Qualifiers OtherQuals = other.getQualifiers(); | ||||||
6631 | |||||||
6632 | // Ignore __unaligned qualifier if this type is a void. | ||||||
6633 | if (getUnqualifiedType()->isVoidType()) | ||||||
6634 | OtherQuals.removeUnaligned(); | ||||||
6635 | |||||||
6636 | return getQualifiers().compatiblyIncludes(OtherQuals); | ||||||
6637 | } | ||||||
6638 | |||||||
6639 | /// If Type is a reference type (e.g., const | ||||||
6640 | /// int&), returns the type that the reference refers to ("const | ||||||
6641 | /// int"). Otherwise, returns the type itself. This routine is used | ||||||
6642 | /// throughout Sema to implement C++ 5p6: | ||||||
6643 | /// | ||||||
6644 | /// If an expression initially has the type "reference to T" (8.3.2, | ||||||
6645 | /// 8.5.3), the type is adjusted to "T" prior to any further | ||||||
6646 | /// analysis, the expression designates the object or function | ||||||
6647 | /// denoted by the reference, and the expression is an lvalue. | ||||||
6648 | inline QualType QualType::getNonReferenceType() const { | ||||||
6649 | if (const auto *RefType = (*this)->getAs<ReferenceType>()) | ||||||
6650 | return RefType->getPointeeType(); | ||||||
6651 | else | ||||||
6652 | return *this; | ||||||
6653 | } | ||||||
6654 | |||||||
6655 | inline bool QualType::isCForbiddenLValueType() const { | ||||||
6656 | return ((getTypePtr()->isVoidType() && !hasQualifiers()) || | ||||||
6657 | getTypePtr()->isFunctionType()); | ||||||
6658 | } | ||||||
6659 | |||||||
6660 | /// Tests whether the type is categorized as a fundamental type. | ||||||
6661 | /// | ||||||
6662 | /// \returns True for types specified in C++0x [basic.fundamental]. | ||||||
6663 | inline bool Type::isFundamentalType() const { | ||||||
6664 | return isVoidType() || | ||||||
6665 | isNullPtrType() || | ||||||
6666 | // FIXME: It's really annoying that we don't have an | ||||||
6667 | // 'isArithmeticType()' which agrees with the standard definition. | ||||||
6668 | (isArithmeticType() && !isEnumeralType()); | ||||||
6669 | } | ||||||
6670 | |||||||
6671 | /// Tests whether the type is categorized as a compound type. | ||||||
6672 | /// | ||||||
6673 | /// \returns True for types specified in C++0x [basic.compound]. | ||||||
6674 | inline bool Type::isCompoundType() const { | ||||||
6675 | // C++0x [basic.compound]p1: | ||||||
6676 | // Compound types can be constructed in the following ways: | ||||||
6677 | // -- arrays of objects of a given type [...]; | ||||||
6678 | return isArrayType() || | ||||||
6679 | // -- functions, which have parameters of given types [...]; | ||||||
6680 | isFunctionType() || | ||||||
6681 | // -- pointers to void or objects or functions [...]; | ||||||
6682 | isPointerType() || | ||||||
6683 | // -- references to objects or functions of a given type. [...] | ||||||
6684 | isReferenceType() || | ||||||
6685 | // -- classes containing a sequence of objects of various types, [...]; | ||||||
6686 | isRecordType() || | ||||||
6687 | // -- unions, which are classes capable of containing objects of different | ||||||
6688 | // types at different times; | ||||||
6689 | isUnionType() || | ||||||
6690 | // -- enumerations, which comprise a set of named constant values. [...]; | ||||||
6691 | isEnumeralType() || | ||||||
6692 | // -- pointers to non-static class members, [...]. | ||||||
6693 | isMemberPointerType(); | ||||||
6694 | } | ||||||
6695 | |||||||
6696 | inline bool Type::isFunctionType() const { | ||||||
6697 | return isa<FunctionType>(CanonicalType); | ||||||
6698 | } | ||||||
6699 | |||||||
6700 | inline bool Type::isPointerType() const { | ||||||
6701 | return isa<PointerType>(CanonicalType); | ||||||
6702 | } | ||||||
6703 | |||||||
6704 | inline bool Type::isAnyPointerType() const { | ||||||
6705 | return isPointerType() || isObjCObjectPointerType(); | ||||||
6706 | } | ||||||
6707 | |||||||
6708 | inline bool Type::isBlockPointerType() const { | ||||||
6709 | return isa<BlockPointerType>(CanonicalType); | ||||||
6710 | } | ||||||
6711 | |||||||
6712 | inline bool Type::isReferenceType() const { | ||||||
6713 | return isa<ReferenceType>(CanonicalType); | ||||||
6714 | } | ||||||
6715 | |||||||
6716 | inline bool Type::isLValueReferenceType() const { | ||||||
6717 | return isa<LValueReferenceType>(CanonicalType); | ||||||
6718 | } | ||||||
6719 | |||||||
6720 | inline bool Type::isRValueReferenceType() const { | ||||||
6721 | return isa<RValueReferenceType>(CanonicalType); | ||||||
6722 | } | ||||||
6723 | |||||||
6724 | inline bool Type::isObjectPointerType() const { | ||||||
6725 | // Note: an "object pointer type" is not the same thing as a pointer to an | ||||||
6726 | // object type; rather, it is a pointer to an object type or a pointer to cv | ||||||
6727 | // void. | ||||||
6728 | if (const auto *T = getAs<PointerType>()) | ||||||
6729 | return !T->getPointeeType()->isFunctionType(); | ||||||
6730 | else | ||||||
6731 | return false; | ||||||
6732 | } | ||||||
6733 | |||||||
6734 | inline bool Type::isFunctionPointerType() const { | ||||||
6735 | if (const auto *T = getAs<PointerType>()) | ||||||
6736 | return T->getPointeeType()->isFunctionType(); | ||||||
6737 | else | ||||||
6738 | return false; | ||||||
6739 | } | ||||||
6740 | |||||||
6741 | inline bool Type::isFunctionReferenceType() const { | ||||||
6742 | if (const auto *T = getAs<ReferenceType>()) | ||||||
6743 | return T->getPointeeType()->isFunctionType(); | ||||||
6744 | else | ||||||
6745 | return false; | ||||||
6746 | } | ||||||
6747 | |||||||
6748 | inline bool Type::isMemberPointerType() const { | ||||||
6749 | return isa<MemberPointerType>(CanonicalType); | ||||||
6750 | } | ||||||
6751 | |||||||
6752 | inline bool Type::isMemberFunctionPointerType() const { | ||||||
6753 | if (const auto *T = getAs<MemberPointerType>()) | ||||||
6754 | return T->isMemberFunctionPointer(); | ||||||
6755 | else | ||||||
6756 | return false; | ||||||
6757 | } | ||||||
6758 | |||||||
6759 | inline bool Type::isMemberDataPointerType() const { | ||||||
6760 | if (const auto *T = getAs<MemberPointerType>()) | ||||||
6761 | return T->isMemberDataPointer(); | ||||||
6762 | else | ||||||
6763 | return false; | ||||||
6764 | } | ||||||
6765 | |||||||
6766 | inline bool Type::isArrayType() const { | ||||||
6767 | return isa<ArrayType>(CanonicalType); | ||||||
6768 | } | ||||||
6769 | |||||||
6770 | inline bool Type::isConstantArrayType() const { | ||||||
6771 | return isa<ConstantArrayType>(CanonicalType); | ||||||
6772 | } | ||||||
6773 | |||||||
6774 | inline bool Type::isIncompleteArrayType() const { | ||||||
6775 | return isa<IncompleteArrayType>(CanonicalType); | ||||||
6776 | } | ||||||
6777 | |||||||
6778 | inline bool Type::isVariableArrayType() const { | ||||||
6779 | return isa<VariableArrayType>(CanonicalType); | ||||||
6780 | } | ||||||
6781 | |||||||
6782 | inline bool Type::isDependentSizedArrayType() const { | ||||||
6783 | return isa<DependentSizedArrayType>(CanonicalType); | ||||||
6784 | } | ||||||
6785 | |||||||
6786 | inline bool Type::isBuiltinType() const { | ||||||
6787 | return isa<BuiltinType>(CanonicalType); | ||||||
6788 | } | ||||||
6789 | |||||||
6790 | inline bool Type::isRecordType() const { | ||||||
6791 | return isa<RecordType>(CanonicalType); | ||||||
6792 | } | ||||||
6793 | |||||||
6794 | inline bool Type::isEnumeralType() const { | ||||||
6795 | return isa<EnumType>(CanonicalType); | ||||||
6796 | } | ||||||
6797 | |||||||
6798 | inline bool Type::isAnyComplexType() const { | ||||||
6799 | return isa<ComplexType>(CanonicalType); | ||||||
6800 | } | ||||||
6801 | |||||||
6802 | inline bool Type::isVectorType() const { | ||||||
6803 | return isa<VectorType>(CanonicalType); | ||||||
6804 | } | ||||||
6805 | |||||||
6806 | inline bool Type::isExtVectorType() const { | ||||||
6807 | return isa<ExtVectorType>(CanonicalType); | ||||||
6808 | } | ||||||
6809 | |||||||
6810 | inline bool Type::isMatrixType() const { | ||||||
6811 | return isa<MatrixType>(CanonicalType); | ||||||
6812 | } | ||||||
6813 | |||||||
6814 | inline bool Type::isConstantMatrixType() const { | ||||||
6815 | return isa<ConstantMatrixType>(CanonicalType); | ||||||
6816 | } | ||||||
6817 | |||||||
6818 | inline bool Type::isDependentAddressSpaceType() const { | ||||||
6819 | return isa<DependentAddressSpaceType>(CanonicalType); | ||||||
6820 | } | ||||||
6821 | |||||||
6822 | inline bool Type::isObjCObjectPointerType() const { | ||||||
6823 | return isa<ObjCObjectPointerType>(CanonicalType); | ||||||
6824 | } | ||||||
6825 | |||||||
6826 | inline bool Type::isObjCObjectType() const { | ||||||
6827 | return isa<ObjCObjectType>(CanonicalType); | ||||||
6828 | } | ||||||
6829 | |||||||
6830 | inline bool Type::isObjCObjectOrInterfaceType() const { | ||||||
6831 | return isa<ObjCInterfaceType>(CanonicalType) || | ||||||
6832 | isa<ObjCObjectType>(CanonicalType); | ||||||
6833 | } | ||||||
6834 | |||||||
6835 | inline bool Type::isAtomicType() const { | ||||||
6836 | return isa<AtomicType>(CanonicalType); | ||||||
6837 | } | ||||||
6838 | |||||||
6839 | inline bool Type::isUndeducedAutoType() const { | ||||||
6840 | return isa<AutoType>(CanonicalType); | ||||||
6841 | } | ||||||
6842 | |||||||
6843 | inline bool Type::isObjCQualifiedIdType() const { | ||||||
6844 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) | ||||||
6845 | return OPT->isObjCQualifiedIdType(); | ||||||
6846 | return false; | ||||||
6847 | } | ||||||
6848 | |||||||
6849 | inline bool Type::isObjCQualifiedClassType() const { | ||||||
6850 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) | ||||||
6851 | return OPT->isObjCQualifiedClassType(); | ||||||
6852 | return false; | ||||||
6853 | } | ||||||
6854 | |||||||
6855 | inline bool Type::isObjCIdType() const { | ||||||
6856 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) | ||||||
6857 | return OPT->isObjCIdType(); | ||||||
6858 | return false; | ||||||
6859 | } | ||||||
6860 | |||||||
6861 | inline bool Type::isObjCClassType() const { | ||||||
6862 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) | ||||||
6863 | return OPT->isObjCClassType(); | ||||||
6864 | return false; | ||||||
6865 | } | ||||||
6866 | |||||||
6867 | inline bool Type::isObjCSelType() const { | ||||||
6868 | if (const auto *OPT = getAs<PointerType>()) | ||||||
6869 | return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel); | ||||||
6870 | return false; | ||||||
6871 | } | ||||||
6872 | |||||||
6873 | inline bool Type::isObjCBuiltinType() const { | ||||||
6874 | return isObjCIdType() || isObjCClassType() || isObjCSelType(); | ||||||
6875 | } | ||||||
6876 | |||||||
6877 | inline bool Type::isDecltypeType() const { | ||||||
6878 | return isa<DecltypeType>(this); | ||||||
6879 | } | ||||||
6880 | |||||||
6881 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ | ||||||
6882 | inline bool Type::is##Id##Type() const { \ | ||||||
6883 | return isSpecificBuiltinType(BuiltinType::Id); \ | ||||||
6884 | } | ||||||
6885 | #include "clang/Basic/OpenCLImageTypes.def" | ||||||
6886 | |||||||
6887 | inline bool Type::isSamplerT() const { | ||||||
6888 | return isSpecificBuiltinType(BuiltinType::OCLSampler); | ||||||
6889 | } | ||||||
6890 | |||||||
6891 | inline bool Type::isEventT() const { | ||||||
6892 | return isSpecificBuiltinType(BuiltinType::OCLEvent); | ||||||
6893 | } | ||||||
6894 | |||||||
6895 | inline bool Type::isClkEventT() const { | ||||||
6896 | return isSpecificBuiltinType(BuiltinType::OCLClkEvent); | ||||||
6897 | } | ||||||
6898 | |||||||
6899 | inline bool Type::isQueueT() const { | ||||||
6900 | return isSpecificBuiltinType(BuiltinType::OCLQueue); | ||||||
6901 | } | ||||||
6902 | |||||||
6903 | inline bool Type::isReserveIDT() const { | ||||||
6904 | return isSpecificBuiltinType(BuiltinType::OCLReserveID); | ||||||
6905 | } | ||||||
6906 | |||||||
6907 | inline bool Type::isImageType() const { | ||||||
6908 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) is##Id##Type() || | ||||||
6909 | return | ||||||
6910 | #include "clang/Basic/OpenCLImageTypes.def" | ||||||
6911 | false; // end boolean or operation | ||||||
6912 | } | ||||||
6913 | |||||||
6914 | inline bool Type::isPipeType() const { | ||||||
6915 | return isa<PipeType>(CanonicalType); | ||||||
6916 | } | ||||||
6917 | |||||||
6918 | inline bool Type::isBitIntType() const { | ||||||
6919 | return isa<BitIntType>(CanonicalType); | ||||||
6920 | } | ||||||
6921 | |||||||
6922 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ | ||||||
6923 | inline bool Type::is##Id##Type() const { \ | ||||||
6924 | return isSpecificBuiltinType(BuiltinType::Id); \ | ||||||
6925 | } | ||||||
6926 | #include "clang/Basic/OpenCLExtensionTypes.def" | ||||||
6927 | |||||||
6928 | inline bool Type::isOCLIntelSubgroupAVCType() const { | ||||||
6929 | #define INTEL_SUBGROUP_AVC_TYPE(ExtType, Id) \ | ||||||
6930 | isOCLIntelSubgroupAVC##Id##Type() || | ||||||
6931 | return | ||||||
6932 | #include "clang/Basic/OpenCLExtensionTypes.def" | ||||||
6933 | false; // end of boolean or operation | ||||||
6934 | } | ||||||
6935 | |||||||
6936 | inline bool Type::isOCLExtOpaqueType() const { | ||||||
6937 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) is##Id##Type() || | ||||||
6938 | return | ||||||
6939 | #include "clang/Basic/OpenCLExtensionTypes.def" | ||||||
6940 | false; // end of boolean or operation | ||||||
6941 | } | ||||||
6942 | |||||||
6943 | inline bool Type::isOpenCLSpecificType() const { | ||||||
6944 | return isSamplerT() || isEventT() || isImageType() || isClkEventT() || | ||||||
6945 | isQueueT() || isReserveIDT() || isPipeType() || isOCLExtOpaqueType(); | ||||||
6946 | } | ||||||
6947 | |||||||
6948 | inline bool Type::isTemplateTypeParmType() const { | ||||||
6949 | return isa<TemplateTypeParmType>(CanonicalType); | ||||||
6950 | } | ||||||
6951 | |||||||
6952 | inline bool Type::isSpecificBuiltinType(unsigned K) const { | ||||||
6953 | if (const BuiltinType *BT = getAs<BuiltinType>()) { | ||||||
6954 | return BT->getKind() == static_cast<BuiltinType::Kind>(K); | ||||||
6955 | } | ||||||
6956 | return false; | ||||||
6957 | } | ||||||
6958 | |||||||
6959 | inline bool Type::isPlaceholderType() const { | ||||||
6960 | if (const auto *BT = dyn_cast<BuiltinType>(this)) | ||||||
6961 | return BT->isPlaceholderType(); | ||||||
6962 | return false; | ||||||
6963 | } | ||||||
6964 | |||||||
6965 | inline const BuiltinType *Type::getAsPlaceholderType() const { | ||||||
6966 | if (const auto *BT = dyn_cast<BuiltinType>(this)) | ||||||
6967 | if (BT->isPlaceholderType()) | ||||||
6968 | return BT; | ||||||
6969 | return nullptr; | ||||||
6970 | } | ||||||
6971 | |||||||
6972 | inline bool Type::isSpecificPlaceholderType(unsigned K) const { | ||||||
6973 | assert(BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K))(static_cast <bool> (BuiltinType::isPlaceholderTypeKind ((BuiltinType::Kind) K)) ? void (0) : __assert_fail ("BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K)" , "clang/include/clang/AST/Type.h", 6973, __extension__ __PRETTY_FUNCTION__ )); | ||||||
6974 | return isSpecificBuiltinType(K); | ||||||
6975 | } | ||||||
6976 | |||||||
6977 | inline bool Type::isNonOverloadPlaceholderType() const { | ||||||
6978 | if (const auto *BT = dyn_cast<BuiltinType>(this)) | ||||||
6979 | return BT->isNonOverloadPlaceholderType(); | ||||||
6980 | return false; | ||||||
6981 | } | ||||||
6982 | |||||||
6983 | inline bool Type::isVoidType() const { | ||||||
6984 | return isSpecificBuiltinType(BuiltinType::Void); | ||||||
6985 | } | ||||||
6986 | |||||||
6987 | inline bool Type::isHalfType() const { | ||||||
6988 | // FIXME: Should we allow complex __fp16? Probably not. | ||||||
6989 | return isSpecificBuiltinType(BuiltinType::Half); | ||||||
6990 | } | ||||||
6991 | |||||||
6992 | inline bool Type::isFloat16Type() const { | ||||||
6993 | return isSpecificBuiltinType(BuiltinType::Float16); | ||||||
6994 | } | ||||||
6995 | |||||||
6996 | inline bool Type::isBFloat16Type() const { | ||||||
6997 | return isSpecificBuiltinType(BuiltinType::BFloat16); | ||||||
6998 | } | ||||||
6999 | |||||||
7000 | inline bool Type::isFloat128Type() const { | ||||||
7001 | return isSpecificBuiltinType(BuiltinType::Float128); | ||||||
7002 | } | ||||||
7003 | |||||||
7004 | inline bool Type::isIbm128Type() const { | ||||||
7005 | return isSpecificBuiltinType(BuiltinType::Ibm128); | ||||||
7006 | } | ||||||
7007 | |||||||
7008 | inline bool Type::isNullPtrType() const { | ||||||
7009 | return isSpecificBuiltinType(BuiltinType::NullPtr); | ||||||
7010 | } | ||||||
7011 | |||||||
7012 | bool IsEnumDeclComplete(EnumDecl *); | ||||||
7013 | bool IsEnumDeclScoped(EnumDecl *); | ||||||
7014 | |||||||
7015 | inline bool Type::isIntegerType() const { | ||||||
7016 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) | ||||||
7017 | return BT->getKind() >= BuiltinType::Bool && | ||||||
7018 | BT->getKind() <= BuiltinType::Int128; | ||||||
7019 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) { | ||||||
7020 | // Incomplete enum types are not treated as integer types. | ||||||
7021 | // FIXME: In C++, enum types are never integer types. | ||||||
7022 | return IsEnumDeclComplete(ET->getDecl()) && | ||||||
7023 | !IsEnumDeclScoped(ET->getDecl()); | ||||||
7024 | } | ||||||
7025 | return isBitIntType(); | ||||||
7026 | } | ||||||
7027 | |||||||
7028 | inline bool Type::isFixedPointType() const { | ||||||
7029 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { | ||||||
7030 | return BT->getKind() >= BuiltinType::ShortAccum && | ||||||
7031 | BT->getKind() <= BuiltinType::SatULongFract; | ||||||
7032 | } | ||||||
7033 | return false; | ||||||
7034 | } | ||||||
7035 | |||||||
7036 | inline bool Type::isFixedPointOrIntegerType() const { | ||||||
7037 | return isFixedPointType() || isIntegerType(); | ||||||
7038 | } | ||||||
7039 | |||||||
7040 | inline bool Type::isSaturatedFixedPointType() const { | ||||||
7041 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { | ||||||
7042 | return BT->getKind() >= BuiltinType::SatShortAccum && | ||||||
7043 | BT->getKind() <= BuiltinType::SatULongFract; | ||||||
7044 | } | ||||||
7045 | return false; | ||||||
7046 | } | ||||||
7047 | |||||||
7048 | inline bool Type::isUnsaturatedFixedPointType() const { | ||||||
7049 | return isFixedPointType() && !isSaturatedFixedPointType(); | ||||||
7050 | } | ||||||
7051 | |||||||
7052 | inline bool Type::isSignedFixedPointType() const { | ||||||
7053 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { | ||||||
7054 | return ((BT->getKind() >= BuiltinType::ShortAccum && | ||||||
7055 | BT->getKind() <= BuiltinType::LongAccum) || | ||||||
7056 | (BT->getKind() >= BuiltinType::ShortFract && | ||||||
7057 | BT->getKind() <= BuiltinType::LongFract) || | ||||||
7058 | (BT->getKind() >= BuiltinType::SatShortAccum && | ||||||
7059 | BT->getKind() <= BuiltinType::SatLongAccum) || | ||||||
7060 | (BT->getKind() >= BuiltinType::SatShortFract && | ||||||
7061 | BT->getKind() <= BuiltinType::SatLongFract)); | ||||||
7062 | } | ||||||
7063 | return false; | ||||||
7064 | } | ||||||
7065 | |||||||
7066 | inline bool Type::isUnsignedFixedPointType() const { | ||||||
7067 | return isFixedPointType() && !isSignedFixedPointType(); | ||||||
7068 | } | ||||||
7069 | |||||||
7070 | inline bool Type::isScalarType() const { | ||||||
7071 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) | ||||||
7072 | return BT->getKind() > BuiltinType::Void && | ||||||
7073 | BT->getKind() <= BuiltinType::NullPtr; | ||||||
7074 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) | ||||||
7075 | // Enums are scalar types, but only if they are defined. Incomplete enums | ||||||
7076 | // are not treated as scalar types. | ||||||
7077 | return IsEnumDeclComplete(ET->getDecl()); | ||||||
7078 | return isa<PointerType>(CanonicalType) || | ||||||
7079 | isa<BlockPointerType>(CanonicalType) || | ||||||
7080 | isa<MemberPointerType>(CanonicalType) || | ||||||
7081 | isa<ComplexType>(CanonicalType) || | ||||||
7082 | isa<ObjCObjectPointerType>(CanonicalType) || | ||||||
7083 | isBitIntType(); | ||||||
7084 | } | ||||||
7085 | |||||||
7086 | inline bool Type::isIntegralOrEnumerationType() const { | ||||||
7087 | if (const auto *BT
| ||||||
7088 | return BT->getKind() >= BuiltinType::Bool && | ||||||
7089 | BT->getKind() <= BuiltinType::Int128; | ||||||
7090 | |||||||
7091 | // Check for a complete enum type; incomplete enum types are not properly an | ||||||
7092 | // enumeration type in the sense required here. | ||||||
7093 | if (const auto *ET
| ||||||
7094 | return IsEnumDeclComplete(ET->getDecl()); | ||||||
7095 | |||||||
7096 | return isBitIntType(); | ||||||
7097 | } | ||||||
7098 | |||||||
7099 | inline bool Type::isBooleanType() const { | ||||||
7100 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) | ||||||
7101 | return BT->getKind() == BuiltinType::Bool; | ||||||
7102 | return false; | ||||||
7103 | } | ||||||
7104 | |||||||
7105 | inline bool Type::isUndeducedType() const { | ||||||
7106 | auto *DT = getContainedDeducedType(); | ||||||
7107 | return DT && !DT->isDeduced(); | ||||||
7108 | } | ||||||
7109 | |||||||
7110 | /// Determines whether this is a type for which one can define | ||||||
7111 | /// an overloaded operator. | ||||||
7112 | inline bool Type::isOverloadableType() const { | ||||||
7113 | return isDependentType() || isRecordType() || isEnumeralType(); | ||||||
7114 | } | ||||||
7115 | |||||||
7116 | /// Determines whether this type is written as a typedef-name. | ||||||
7117 | inline bool Type::isTypedefNameType() const { | ||||||
7118 | if (getAs<TypedefType>()) | ||||||
7119 | return true; | ||||||
7120 | if (auto *TST = getAs<TemplateSpecializationType>()) | ||||||
7121 | return TST->isTypeAlias(); | ||||||
7122 | return false; | ||||||
7123 | } | ||||||
7124 | |||||||
7125 | /// Determines whether this type can decay to a pointer type. | ||||||
7126 | inline bool Type::canDecayToPointerType() const { | ||||||
7127 | return isFunctionType() || isArrayType(); | ||||||
7128 | } | ||||||
7129 | |||||||
7130 | inline bool Type::hasPointerRepresentation() const { | ||||||
7131 | return (isPointerType() || isReferenceType() || isBlockPointerType() || | ||||||
7132 | isObjCObjectPointerType() || isNullPtrType()); | ||||||
7133 | } | ||||||
7134 | |||||||
7135 | inline bool Type::hasObjCPointerRepresentation() const { | ||||||
7136 | return isObjCObjectPointerType(); | ||||||
7137 | } | ||||||
7138 | |||||||
7139 | inline const Type *Type::getBaseElementTypeUnsafe() const { | ||||||
7140 | const Type *type = this; | ||||||
7141 | while (const ArrayType *arrayType = type->getAsArrayTypeUnsafe()) | ||||||
7142 | type = arrayType->getElementType().getTypePtr(); | ||||||
7143 | return type; | ||||||
7144 | } | ||||||
7145 | |||||||
7146 | inline const Type *Type::getPointeeOrArrayElementType() const { | ||||||
7147 | const Type *type = this; | ||||||
7148 | if (type->isAnyPointerType()) | ||||||
7149 | return type->getPointeeType().getTypePtr(); | ||||||
7150 | else if (type->isArrayType()) | ||||||
7151 | return type->getBaseElementTypeUnsafe(); | ||||||
7152 | return type; | ||||||
7153 | } | ||||||
7154 | /// Insertion operator for partial diagnostics. This allows sending adress | ||||||
7155 | /// spaces into a diagnostic with <<. | ||||||
7156 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, | ||||||
7157 | LangAS AS) { | ||||||
7158 | PD.AddTaggedVal(static_cast<std::underlying_type_t<LangAS>>(AS), | ||||||
7159 | DiagnosticsEngine::ArgumentKind::ak_addrspace); | ||||||
7160 | return PD; | ||||||
7161 | } | ||||||
7162 | |||||||
7163 | /// Insertion operator for partial diagnostics. This allows sending Qualifiers | ||||||
7164 | /// into a diagnostic with <<. | ||||||
7165 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, | ||||||
7166 | Qualifiers Q) { | ||||||
7167 | PD.AddTaggedVal(Q.getAsOpaqueValue(), | ||||||
7168 | DiagnosticsEngine::ArgumentKind::ak_qual); | ||||||
7169 | return PD; | ||||||
7170 | } | ||||||
7171 | |||||||
7172 | /// Insertion operator for partial diagnostics. This allows sending QualType's | ||||||
7173 | /// into a diagnostic with <<. | ||||||
7174 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, | ||||||
7175 | QualType T) { | ||||||
7176 | PD.AddTaggedVal(reinterpret_cast<uint64_t>(T.getAsOpaquePtr()), | ||||||
7177 | DiagnosticsEngine::ak_qualtype); | ||||||
7178 | return PD; | ||||||
7179 | } | ||||||
7180 | |||||||
7181 | // Helper class template that is used by Type::getAs to ensure that one does | ||||||
7182 | // not try to look through a qualified type to get to an array type. | ||||||
7183 | template <typename T> | ||||||
7184 | using TypeIsArrayType = | ||||||
7185 | std::integral_constant<bool, std::is_same<T, ArrayType>::value || | ||||||
7186 | std::is_base_of<ArrayType, T>::value>; | ||||||
7187 | |||||||
7188 | // Member-template getAs<specific type>'. | ||||||
7189 | template <typename T> const T *Type::getAs() const { | ||||||
7190 | static_assert(!TypeIsArrayType<T>::value, | ||||||
7191 | "ArrayType cannot be used with getAs!"); | ||||||
7192 | |||||||
7193 | // If this is directly a T type, return it. | ||||||
7194 | if (const auto *Ty = dyn_cast<T>(this)) | ||||||
7195 | return Ty; | ||||||
7196 | |||||||
7197 | // If the canonical form of this type isn't the right kind, reject it. | ||||||
7198 | if (!isa<T>(CanonicalType)) | ||||||
7199 | return nullptr; | ||||||
7200 | |||||||
7201 | // If this is a typedef for the type, strip the typedef off without | ||||||
7202 | // losing all typedef information. | ||||||
7203 | return cast<T>(getUnqualifiedDesugaredType()); | ||||||
7204 | } | ||||||
7205 | |||||||
7206 | template <typename T> const T *Type::getAsAdjusted() const { | ||||||
7207 | static_assert(!TypeIsArrayType<T>::value, "ArrayType cannot be used with getAsAdjusted!"); | ||||||
7208 | |||||||
7209 | // If this is directly a T type, return it. | ||||||
7210 | if (const auto *Ty = dyn_cast<T>(this)) | ||||||
7211 | return Ty; | ||||||
7212 | |||||||
7213 | // If the canonical form of this type isn't the right kind, reject it. | ||||||
7214 | if (!isa<T>(CanonicalType)) | ||||||
7215 | return nullptr; | ||||||
7216 | |||||||
7217 | // Strip off type adjustments that do not modify the underlying nature of the | ||||||
7218 | // type. | ||||||
7219 | const Type *Ty = this; | ||||||
7220 | while (Ty) { | ||||||
7221 | if (const auto *A = dyn_cast<AttributedType>(Ty)) | ||||||
7222 | Ty = A->getModifiedType().getTypePtr(); | ||||||
7223 | else if (const auto *E = dyn_cast<ElaboratedType>(Ty)) | ||||||
7224 | Ty = E->desugar().getTypePtr(); | ||||||
7225 | else if (const auto *P = dyn_cast<ParenType>(Ty)) | ||||||
7226 | Ty = P->desugar().getTypePtr(); | ||||||
7227 | else if (const auto *A = dyn_cast<AdjustedType>(Ty)) | ||||||
7228 | Ty = A->desugar().getTypePtr(); | ||||||
7229 | else if (const auto *M = dyn_cast<MacroQualifiedType>(Ty)) | ||||||
7230 | Ty = M->desugar().getTypePtr(); | ||||||
7231 | else | ||||||
7232 | break; | ||||||
7233 | } | ||||||
7234 | |||||||
7235 | // Just because the canonical type is correct does not mean we can use cast<>, | ||||||
7236 | // since we may not have stripped off all the sugar down to the base type. | ||||||
7237 | return dyn_cast<T>(Ty); | ||||||
7238 | } | ||||||
7239 | |||||||
7240 | inline const ArrayType *Type::getAsArrayTypeUnsafe() const { | ||||||
7241 | // If this is directly an array type, return it. | ||||||
7242 | if (const auto *arr = dyn_cast<ArrayType>(this)) | ||||||
7243 | return arr; | ||||||
7244 | |||||||
7245 | // If the canonical form of this type isn't the right kind, reject it. | ||||||
7246 | if (!isa<ArrayType>(CanonicalType)) | ||||||
7247 | return nullptr; | ||||||
7248 | |||||||
7249 | // If this is a typedef for the type, strip the typedef off without | ||||||
7250 | // losing all typedef information. | ||||||
7251 | return cast<ArrayType>(getUnqualifiedDesugaredType()); | ||||||
7252 | } | ||||||
7253 | |||||||
7254 | template <typename T> const T *Type::castAs() const { | ||||||
7255 | static_assert(!TypeIsArrayType<T>::value, | ||||||
7256 | "ArrayType cannot be used with castAs!"); | ||||||
7257 | |||||||
7258 | if (const auto *ty = dyn_cast<T>(this)) return ty; | ||||||
7259 | assert(isa<T>(CanonicalType))(static_cast <bool> (isa<T>(CanonicalType)) ? void (0) : __assert_fail ("isa<T>(CanonicalType)", "clang/include/clang/AST/Type.h" , 7259, __extension__ __PRETTY_FUNCTION__)); | ||||||
7260 | return cast<T>(getUnqualifiedDesugaredType()); | ||||||
7261 | } | ||||||
7262 | |||||||
7263 | inline const ArrayType *Type::castAsArrayTypeUnsafe() const { | ||||||
7264 | assert(isa<ArrayType>(CanonicalType))(static_cast <bool> (isa<ArrayType>(CanonicalType )) ? void (0) : __assert_fail ("isa<ArrayType>(CanonicalType)" , "clang/include/clang/AST/Type.h", 7264, __extension__ __PRETTY_FUNCTION__ )); | ||||||
7265 | if (const auto *arr = dyn_cast<ArrayType>(this)) return arr; | ||||||
7266 | return cast<ArrayType>(getUnqualifiedDesugaredType()); | ||||||
7267 | } | ||||||
7268 | |||||||
7269 | DecayedType::DecayedType(QualType OriginalType, QualType DecayedPtr, | ||||||
7270 | QualType CanonicalPtr) | ||||||
7271 | : AdjustedType(Decayed, OriginalType, DecayedPtr, CanonicalPtr) { | ||||||
7272 | #ifndef NDEBUG | ||||||
7273 | QualType Adjusted = getAdjustedType(); | ||||||
7274 | (void)AttributedType::stripOuterNullability(Adjusted); | ||||||
7275 | assert(isa<PointerType>(Adjusted))(static_cast <bool> (isa<PointerType>(Adjusted)) ? void (0) : __assert_fail ("isa<PointerType>(Adjusted)" , "clang/include/clang/AST/Type.h", 7275, __extension__ __PRETTY_FUNCTION__ )); | ||||||
7276 | #endif | ||||||
7277 | } | ||||||
7278 | |||||||
7279 | QualType DecayedType::getPointeeType() const { | ||||||
7280 | QualType Decayed = getDecayedType(); | ||||||
7281 | (void)AttributedType::stripOuterNullability(Decayed); | ||||||
7282 | return cast<PointerType>(Decayed)->getPointeeType(); | ||||||
7283 | } | ||||||
7284 | |||||||
7285 | // Get the decimal string representation of a fixed point type, represented | ||||||
7286 | // as a scaled integer. | ||||||
7287 | // TODO: At some point, we should change the arguments to instead just accept an | ||||||
7288 | // APFixedPoint instead of APSInt and scale. | ||||||
7289 | void FixedPointValueToString(SmallVectorImpl<char> &Str, llvm::APSInt Val, | ||||||
7290 | unsigned Scale); | ||||||
7291 | |||||||
7292 | } // namespace clang | ||||||
7293 | |||||||
7294 | #endif // LLVM_CLANG_AST_TYPE_H |
1 | //===- llvm/Support/Casting.h - Allow flexible, checked, casts --*- 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 defines the isa<X>(), cast<X>(), dyn_cast<X>(), cast_or_null<X>(), |
10 | // and dyn_cast_or_null<X>() templates. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #ifndef LLVM_SUPPORT_CASTING_H |
15 | #define LLVM_SUPPORT_CASTING_H |
16 | |
17 | #include "llvm/Support/Compiler.h" |
18 | #include "llvm/Support/type_traits.h" |
19 | #include <cassert> |
20 | #include <memory> |
21 | #include <type_traits> |
22 | |
23 | namespace llvm { |
24 | |
25 | //===----------------------------------------------------------------------===// |
26 | // isa<x> Support Templates |
27 | //===----------------------------------------------------------------------===// |
28 | |
29 | // Define a template that can be specialized by smart pointers to reflect the |
30 | // fact that they are automatically dereferenced, and are not involved with the |
31 | // template selection process... the default implementation is a noop. |
32 | // |
33 | template<typename From> struct simplify_type { |
34 | using SimpleType = From; // The real type this represents... |
35 | |
36 | // An accessor to get the real value... |
37 | static SimpleType &getSimplifiedValue(From &Val) { return Val; } |
38 | }; |
39 | |
40 | template<typename From> struct simplify_type<const From> { |
41 | using NonConstSimpleType = typename simplify_type<From>::SimpleType; |
42 | using SimpleType = |
43 | typename add_const_past_pointer<NonConstSimpleType>::type; |
44 | using RetType = |
45 | typename add_lvalue_reference_if_not_pointer<SimpleType>::type; |
46 | |
47 | static RetType getSimplifiedValue(const From& Val) { |
48 | return simplify_type<From>::getSimplifiedValue(const_cast<From&>(Val)); |
49 | } |
50 | }; |
51 | |
52 | // The core of the implementation of isa<X> is here; To and From should be |
53 | // the names of classes. This template can be specialized to customize the |
54 | // implementation of isa<> without rewriting it from scratch. |
55 | template <typename To, typename From, typename Enabler = void> |
56 | struct isa_impl { |
57 | static inline bool doit(const From &Val) { |
58 | return To::classof(&Val); |
59 | } |
60 | }; |
61 | |
62 | /// Always allow upcasts, and perform no dynamic check for them. |
63 | template <typename To, typename From> |
64 | struct isa_impl<To, From, std::enable_if_t<std::is_base_of<To, From>::value>> { |
65 | static inline bool doit(const From &) { return true; } |
66 | }; |
67 | |
68 | template <typename To, typename From> struct isa_impl_cl { |
69 | static inline bool doit(const From &Val) { |
70 | return isa_impl<To, From>::doit(Val); |
71 | } |
72 | }; |
73 | |
74 | template <typename To, typename From> struct isa_impl_cl<To, const From> { |
75 | static inline bool doit(const From &Val) { |
76 | return isa_impl<To, From>::doit(Val); |
77 | } |
78 | }; |
79 | |
80 | template <typename To, typename From> |
81 | struct isa_impl_cl<To, const std::unique_ptr<From>> { |
82 | static inline bool doit(const std::unique_ptr<From> &Val) { |
83 | assert(Val && "isa<> used on a null pointer")(static_cast <bool> (Val && "isa<> used on a null pointer" ) ? void (0) : __assert_fail ("Val && \"isa<> used on a null pointer\"" , "llvm/include/llvm/Support/Casting.h", 83, __extension__ __PRETTY_FUNCTION__ )); |
84 | return isa_impl_cl<To, From>::doit(*Val); |
85 | } |
86 | }; |
87 | |
88 | template <typename To, typename From> struct isa_impl_cl<To, From*> { |
89 | static inline bool doit(const From *Val) { |
90 | assert(Val && "isa<> used on a null pointer")(static_cast <bool> (Val && "isa<> used on a null pointer" ) ? void (0) : __assert_fail ("Val && \"isa<> used on a null pointer\"" , "llvm/include/llvm/Support/Casting.h", 90, __extension__ __PRETTY_FUNCTION__ )); |
91 | return isa_impl<To, From>::doit(*Val); |
92 | } |
93 | }; |
94 | |
95 | template <typename To, typename From> struct isa_impl_cl<To, From*const> { |
96 | static inline bool doit(const From *Val) { |
97 | assert(Val && "isa<> used on a null pointer")(static_cast <bool> (Val && "isa<> used on a null pointer" ) ? void (0) : __assert_fail ("Val && \"isa<> used on a null pointer\"" , "llvm/include/llvm/Support/Casting.h", 97, __extension__ __PRETTY_FUNCTION__ )); |
98 | return isa_impl<To, From>::doit(*Val); |
99 | } |
100 | }; |
101 | |
102 | template <typename To, typename From> struct isa_impl_cl<To, const From*> { |
103 | static inline bool doit(const From *Val) { |
104 | assert(Val && "isa<> used on a null pointer")(static_cast <bool> (Val && "isa<> used on a null pointer" ) ? void (0) : __assert_fail ("Val && \"isa<> used on a null pointer\"" , "llvm/include/llvm/Support/Casting.h", 104, __extension__ __PRETTY_FUNCTION__ )); |
105 | return isa_impl<To, From>::doit(*Val); |
106 | } |
107 | }; |
108 | |
109 | template <typename To, typename From> struct isa_impl_cl<To, const From*const> { |
110 | static inline bool doit(const From *Val) { |
111 | assert(Val && "isa<> used on a null pointer")(static_cast <bool> (Val && "isa<> used on a null pointer" ) ? void (0) : __assert_fail ("Val && \"isa<> used on a null pointer\"" , "llvm/include/llvm/Support/Casting.h", 111, __extension__ __PRETTY_FUNCTION__ )); |
112 | return isa_impl<To, From>::doit(*Val); |
113 | } |
114 | }; |
115 | |
116 | template<typename To, typename From, typename SimpleFrom> |
117 | struct isa_impl_wrap { |
118 | // When From != SimplifiedType, we can simplify the type some more by using |
119 | // the simplify_type template. |
120 | static bool doit(const From &Val) { |
121 | return isa_impl_wrap<To, SimpleFrom, |
122 | typename simplify_type<SimpleFrom>::SimpleType>::doit( |
123 | simplify_type<const From>::getSimplifiedValue(Val)); |
124 | } |
125 | }; |
126 | |
127 | template<typename To, typename FromTy> |
128 | struct isa_impl_wrap<To, FromTy, FromTy> { |
129 | // When From == SimpleType, we are as simple as we are going to get. |
130 | static bool doit(const FromTy &Val) { |
131 | return isa_impl_cl<To,FromTy>::doit(Val); |
132 | } |
133 | }; |
134 | |
135 | // isa<X> - Return true if the parameter to the template is an instance of one |
136 | // of the template type arguments. Used like this: |
137 | // |
138 | // if (isa<Type>(myVal)) { ... } |
139 | // if (isa<Type0, Type1, Type2>(myVal)) { ... } |
140 | // |
141 | template <class X, class Y> LLVM_NODISCARD[[clang::warn_unused_result]] inline bool isa(const Y &Val) { |
142 | return isa_impl_wrap<X, const Y, |
143 | typename simplify_type<const Y>::SimpleType>::doit(Val); |
144 | } |
145 | |
146 | template <typename First, typename Second, typename... Rest, typename Y> |
147 | LLVM_NODISCARD[[clang::warn_unused_result]] inline bool isa(const Y &Val) { |
148 | return isa<First>(Val) || isa<Second, Rest...>(Val); |
149 | } |
150 | |
151 | // isa_and_nonnull<X> - Functionally identical to isa, except that a null value |
152 | // is accepted. |
153 | // |
154 | template <typename... X, class Y> |
155 | LLVM_NODISCARD[[clang::warn_unused_result]] inline bool isa_and_nonnull(const Y &Val) { |
156 | if (!Val) |
157 | return false; |
158 | return isa<X...>(Val); |
159 | } |
160 | |
161 | //===----------------------------------------------------------------------===// |
162 | // cast<x> Support Templates |
163 | //===----------------------------------------------------------------------===// |
164 | |
165 | template<class To, class From> struct cast_retty; |
166 | |
167 | // Calculate what type the 'cast' function should return, based on a requested |
168 | // type of To and a source type of From. |
169 | template<class To, class From> struct cast_retty_impl { |
170 | using ret_type = To &; // Normal case, return Ty& |
171 | }; |
172 | template<class To, class From> struct cast_retty_impl<To, const From> { |
173 | using ret_type = const To &; // Normal case, return Ty& |
174 | }; |
175 | |
176 | template<class To, class From> struct cast_retty_impl<To, From*> { |
177 | using ret_type = To *; // Pointer arg case, return Ty* |
178 | }; |
179 | |
180 | template<class To, class From> struct cast_retty_impl<To, const From*> { |
181 | using ret_type = const To *; // Constant pointer arg case, return const Ty* |
182 | }; |
183 | |
184 | template<class To, class From> struct cast_retty_impl<To, const From*const> { |
185 | using ret_type = const To *; // Constant pointer arg case, return const Ty* |
186 | }; |
187 | |
188 | template <class To, class From> |
189 | struct cast_retty_impl<To, std::unique_ptr<From>> { |
190 | private: |
191 | using PointerType = typename cast_retty_impl<To, From *>::ret_type; |
192 | using ResultType = std::remove_pointer_t<PointerType>; |
193 | |
194 | public: |
195 | using ret_type = std::unique_ptr<ResultType>; |
196 | }; |
197 | |
198 | template<class To, class From, class SimpleFrom> |
199 | struct cast_retty_wrap { |
200 | // When the simplified type and the from type are not the same, use the type |
201 | // simplifier to reduce the type, then reuse cast_retty_impl to get the |
202 | // resultant type. |
203 | using ret_type = typename cast_retty<To, SimpleFrom>::ret_type; |
204 | }; |
205 | |
206 | template<class To, class FromTy> |
207 | struct cast_retty_wrap<To, FromTy, FromTy> { |
208 | // When the simplified type is equal to the from type, use it directly. |
209 | using ret_type = typename cast_retty_impl<To,FromTy>::ret_type; |
210 | }; |
211 | |
212 | template<class To, class From> |
213 | struct cast_retty { |
214 | using ret_type = typename cast_retty_wrap< |
215 | To, From, typename simplify_type<From>::SimpleType>::ret_type; |
216 | }; |
217 | |
218 | // Ensure the non-simple values are converted using the simplify_type template |
219 | // that may be specialized by smart pointers... |
220 | // |
221 | template<class To, class From, class SimpleFrom> struct cast_convert_val { |
222 | // This is not a simple type, use the template to simplify it... |
223 | static typename cast_retty<To, From>::ret_type doit(From &Val) { |
224 | return cast_convert_val<To, SimpleFrom, |
225 | typename simplify_type<SimpleFrom>::SimpleType>::doit( |
226 | simplify_type<From>::getSimplifiedValue(Val)); |
227 | } |
228 | }; |
229 | |
230 | template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> { |
231 | // This _is_ a simple type, just cast it. |
232 | static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) { |
233 | typename cast_retty<To, FromTy>::ret_type Res2 |
234 | = (typename cast_retty<To, FromTy>::ret_type)const_cast<FromTy&>(Val); |
235 | return Res2; |
236 | } |
237 | }; |
238 | |
239 | template <class X> struct is_simple_type { |
240 | static const bool value = |
241 | std::is_same<X, typename simplify_type<X>::SimpleType>::value; |
242 | }; |
243 | |
244 | // cast<X> - Return the argument parameter cast to the specified type. This |
245 | // casting operator asserts that the type is correct, so it does not return null |
246 | // on failure. It does not allow a null argument (use cast_or_null for that). |
247 | // It is typically used like this: |
248 | // |
249 | // cast<Instruction>(myVal)->getParent() |
250 | // |
251 | template <class X, class Y> |
252 | inline std::enable_if_t<!is_simple_type<Y>::value, |
253 | typename cast_retty<X, const Y>::ret_type> |
254 | cast(const Y &Val) { |
255 | assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!")(static_cast <bool> (isa<X>(Val) && "cast<Ty>() argument of incompatible type!" ) ? void (0) : __assert_fail ("isa<X>(Val) && \"cast<Ty>() argument of incompatible type!\"" , "llvm/include/llvm/Support/Casting.h", 255, __extension__ __PRETTY_FUNCTION__ )); |
256 | return cast_convert_val< |
257 | X, const Y, typename simplify_type<const Y>::SimpleType>::doit(Val); |
258 | } |
259 | |
260 | template <class X, class Y> |
261 | inline typename cast_retty<X, Y>::ret_type cast(Y &Val) { |
262 | assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!")(static_cast <bool> (isa<X>(Val) && "cast<Ty>() argument of incompatible type!" ) ? void (0) : __assert_fail ("isa<X>(Val) && \"cast<Ty>() argument of incompatible type!\"" , "llvm/include/llvm/Support/Casting.h", 262, __extension__ __PRETTY_FUNCTION__ )); |
263 | return cast_convert_val<X, Y, |
264 | typename simplify_type<Y>::SimpleType>::doit(Val); |
265 | } |
266 | |
267 | template <class X, class Y> |
268 | inline typename cast_retty<X, Y *>::ret_type cast(Y *Val) { |
269 | assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!")(static_cast <bool> (isa<X>(Val) && "cast<Ty>() argument of incompatible type!" ) ? void (0) : __assert_fail ("isa<X>(Val) && \"cast<Ty>() argument of incompatible type!\"" , "llvm/include/llvm/Support/Casting.h", 269, __extension__ __PRETTY_FUNCTION__ )); |
270 | return cast_convert_val<X, Y*, |
271 | typename simplify_type<Y*>::SimpleType>::doit(Val); |
272 | } |
273 | |
274 | template <class X, class Y> |
275 | inline typename cast_retty<X, std::unique_ptr<Y>>::ret_type |
276 | cast(std::unique_ptr<Y> &&Val) { |
277 | assert(isa<X>(Val.get()) && "cast<Ty>() argument of incompatible type!")(static_cast <bool> (isa<X>(Val.get()) && "cast<Ty>() argument of incompatible type!") ? void (0 ) : __assert_fail ("isa<X>(Val.get()) && \"cast<Ty>() argument of incompatible type!\"" , "llvm/include/llvm/Support/Casting.h", 277, __extension__ __PRETTY_FUNCTION__ )); |
278 | using ret_type = typename cast_retty<X, std::unique_ptr<Y>>::ret_type; |
279 | return ret_type( |
280 | cast_convert_val<X, Y *, typename simplify_type<Y *>::SimpleType>::doit( |
281 | Val.release())); |
282 | } |
283 | |
284 | // cast_or_null<X> - Functionally identical to cast, except that a null value is |
285 | // accepted. |
286 | // |
287 | template <class X, class Y> |
288 | LLVM_NODISCARD[[clang::warn_unused_result]] inline std::enable_if_t< |
289 | !is_simple_type<Y>::value, typename cast_retty<X, const Y>::ret_type> |
290 | cast_or_null(const Y &Val) { |
291 | if (!Val) |
292 | return nullptr; |
293 | assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!")(static_cast <bool> (isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!" ) ? void (0) : __assert_fail ("isa<X>(Val) && \"cast_or_null<Ty>() argument of incompatible type!\"" , "llvm/include/llvm/Support/Casting.h", 293, __extension__ __PRETTY_FUNCTION__ )); |
294 | return cast<X>(Val); |
295 | } |
296 | |
297 | template <class X, class Y> |
298 | LLVM_NODISCARD[[clang::warn_unused_result]] inline std::enable_if_t<!is_simple_type<Y>::value, |
299 | typename cast_retty<X, Y>::ret_type> |
300 | cast_or_null(Y &Val) { |
301 | if (!Val) |
302 | return nullptr; |
303 | assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!")(static_cast <bool> (isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!" ) ? void (0) : __assert_fail ("isa<X>(Val) && \"cast_or_null<Ty>() argument of incompatible type!\"" , "llvm/include/llvm/Support/Casting.h", 303, __extension__ __PRETTY_FUNCTION__ )); |
304 | return cast<X>(Val); |
305 | } |
306 | |
307 | template <class X, class Y> |
308 | LLVM_NODISCARD[[clang::warn_unused_result]] inline typename cast_retty<X, Y *>::ret_type |
309 | cast_or_null(Y *Val) { |
310 | if (!Val) return nullptr; |
311 | assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!")(static_cast <bool> (isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!" ) ? void (0) : __assert_fail ("isa<X>(Val) && \"cast_or_null<Ty>() argument of incompatible type!\"" , "llvm/include/llvm/Support/Casting.h", 311, __extension__ __PRETTY_FUNCTION__ )); |
312 | return cast<X>(Val); |
313 | } |
314 | |
315 | template <class X, class Y> |
316 | inline typename cast_retty<X, std::unique_ptr<Y>>::ret_type |
317 | cast_or_null(std::unique_ptr<Y> &&Val) { |
318 | if (!Val) |
319 | return nullptr; |
320 | return cast<X>(std::move(Val)); |
321 | } |
322 | |
323 | // dyn_cast<X> - Return the argument parameter cast to the specified type. This |
324 | // casting operator returns null if the argument is of the wrong type, so it can |
325 | // be used to test for a type as well as cast if successful. This should be |
326 | // used in the context of an if statement like this: |
327 | // |
328 | // if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... } |
329 | // |
330 | |
331 | template <class X, class Y> |
332 | LLVM_NODISCARD[[clang::warn_unused_result]] inline std::enable_if_t< |
333 | !is_simple_type<Y>::value, typename cast_retty<X, const Y>::ret_type> |
334 | dyn_cast(const Y &Val) { |
335 | return isa<X>(Val) ? cast<X>(Val) : nullptr; |
336 | } |
337 | |
338 | template <class X, class Y> |
339 | LLVM_NODISCARD[[clang::warn_unused_result]] inline typename cast_retty<X, Y>::ret_type dyn_cast(Y &Val) { |
340 | return isa<X>(Val) ? cast<X>(Val) : nullptr; |
341 | } |
342 | |
343 | template <class X, class Y> |
344 | LLVM_NODISCARD[[clang::warn_unused_result]] inline typename cast_retty<X, Y *>::ret_type dyn_cast(Y *Val) { |
345 | return isa<X>(Val) ? cast<X>(Val) : nullptr; |
346 | } |
347 | |
348 | // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null |
349 | // value is accepted. |
350 | // |
351 | template <class X, class Y> |
352 | LLVM_NODISCARD[[clang::warn_unused_result]] inline std::enable_if_t< |
353 | !is_simple_type<Y>::value, typename cast_retty<X, const Y>::ret_type> |
354 | dyn_cast_or_null(const Y &Val) { |
355 | return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr; |
356 | } |
357 | |
358 | template <class X, class Y> |
359 | LLVM_NODISCARD[[clang::warn_unused_result]] inline std::enable_if_t<!is_simple_type<Y>::value, |
360 | typename cast_retty<X, Y>::ret_type> |
361 | dyn_cast_or_null(Y &Val) { |
362 | return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr; |
363 | } |
364 | |
365 | template <class X, class Y> |
366 | LLVM_NODISCARD[[clang::warn_unused_result]] inline typename cast_retty<X, Y *>::ret_type |
367 | dyn_cast_or_null(Y *Val) { |
368 | return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr; |
369 | } |
370 | |
371 | // unique_dyn_cast<X> - Given a unique_ptr<Y>, try to return a unique_ptr<X>, |
372 | // taking ownership of the input pointer iff isa<X>(Val) is true. If the |
373 | // cast is successful, From refers to nullptr on exit and the casted value |
374 | // is returned. If the cast is unsuccessful, the function returns nullptr |
375 | // and From is unchanged. |
376 | template <class X, class Y> |
377 | LLVM_NODISCARD[[clang::warn_unused_result]] inline auto unique_dyn_cast(std::unique_ptr<Y> &Val) |
378 | -> decltype(cast<X>(Val)) { |
379 | if (!isa<X>(Val)) |
380 | return nullptr; |
381 | return cast<X>(std::move(Val)); |
382 | } |
383 | |
384 | template <class X, class Y> |
385 | LLVM_NODISCARD[[clang::warn_unused_result]] inline auto unique_dyn_cast(std::unique_ptr<Y> &&Val) { |
386 | return unique_dyn_cast<X, Y>(Val); |
387 | } |
388 | |
389 | // dyn_cast_or_null<X> - Functionally identical to unique_dyn_cast, except that |
390 | // a null value is accepted. |
391 | template <class X, class Y> |
392 | LLVM_NODISCARD[[clang::warn_unused_result]] inline auto unique_dyn_cast_or_null(std::unique_ptr<Y> &Val) |
393 | -> decltype(cast<X>(Val)) { |
394 | if (!Val) |
395 | return nullptr; |
396 | return unique_dyn_cast<X, Y>(Val); |
397 | } |
398 | |
399 | template <class X, class Y> |
400 | LLVM_NODISCARD[[clang::warn_unused_result]] inline auto unique_dyn_cast_or_null(std::unique_ptr<Y> &&Val) { |
401 | return unique_dyn_cast_or_null<X, Y>(Val); |
402 | } |
403 | |
404 | } // end namespace llvm |
405 | |
406 | #endif // LLVM_SUPPORT_CASTING_H |