File: | tools/clang/lib/Analysis/BodyFarm.cpp |
Warning: | line 141, column 19 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //== BodyFarm.cpp - Factory for conjuring up fake bodies ----------*- 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 | // BodyFarm is a factory for creating faux implementations for functions/methods | |||
10 | // for analysis purposes. | |||
11 | // | |||
12 | //===----------------------------------------------------------------------===// | |||
13 | ||||
14 | #include "clang/Analysis/BodyFarm.h" | |||
15 | #include "clang/AST/ASTContext.h" | |||
16 | #include "clang/AST/CXXInheritance.h" | |||
17 | #include "clang/AST/Decl.h" | |||
18 | #include "clang/AST/Expr.h" | |||
19 | #include "clang/AST/ExprCXX.h" | |||
20 | #include "clang/AST/ExprObjC.h" | |||
21 | #include "clang/AST/NestedNameSpecifier.h" | |||
22 | #include "clang/Analysis/CodeInjector.h" | |||
23 | #include "clang/Basic/OperatorKinds.h" | |||
24 | #include "llvm/ADT/StringSwitch.h" | |||
25 | #include "llvm/Support/Debug.h" | |||
26 | ||||
27 | #define DEBUG_TYPE"body-farm" "body-farm" | |||
28 | ||||
29 | using namespace clang; | |||
30 | ||||
31 | //===----------------------------------------------------------------------===// | |||
32 | // Helper creation functions for constructing faux ASTs. | |||
33 | //===----------------------------------------------------------------------===// | |||
34 | ||||
35 | static bool isDispatchBlock(QualType Ty) { | |||
36 | // Is it a block pointer? | |||
37 | const BlockPointerType *BPT = Ty->getAs<BlockPointerType>(); | |||
38 | if (!BPT) | |||
39 | return false; | |||
40 | ||||
41 | // Check if the block pointer type takes no arguments and | |||
42 | // returns void. | |||
43 | const FunctionProtoType *FT = | |||
44 | BPT->getPointeeType()->getAs<FunctionProtoType>(); | |||
45 | return FT && FT->getReturnType()->isVoidType() && FT->getNumParams() == 0; | |||
46 | } | |||
47 | ||||
48 | namespace { | |||
49 | class ASTMaker { | |||
50 | public: | |||
51 | ASTMaker(ASTContext &C) : C(C) {} | |||
52 | ||||
53 | /// Create a new BinaryOperator representing a simple assignment. | |||
54 | BinaryOperator *makeAssignment(const Expr *LHS, const Expr *RHS, QualType Ty); | |||
55 | ||||
56 | /// Create a new BinaryOperator representing a comparison. | |||
57 | BinaryOperator *makeComparison(const Expr *LHS, const Expr *RHS, | |||
58 | BinaryOperator::Opcode Op); | |||
59 | ||||
60 | /// Create a new compound stmt using the provided statements. | |||
61 | CompoundStmt *makeCompound(ArrayRef<Stmt*>); | |||
62 | ||||
63 | /// Create a new DeclRefExpr for the referenced variable. | |||
64 | DeclRefExpr *makeDeclRefExpr(const VarDecl *D, | |||
65 | bool RefersToEnclosingVariableOrCapture = false); | |||
66 | ||||
67 | /// Create a new UnaryOperator representing a dereference. | |||
68 | UnaryOperator *makeDereference(const Expr *Arg, QualType Ty); | |||
69 | ||||
70 | /// Create an implicit cast for an integer conversion. | |||
71 | Expr *makeIntegralCast(const Expr *Arg, QualType Ty); | |||
72 | ||||
73 | /// Create an implicit cast to a builtin boolean type. | |||
74 | ImplicitCastExpr *makeIntegralCastToBoolean(const Expr *Arg); | |||
75 | ||||
76 | /// Create an implicit cast for lvalue-to-rvaluate conversions. | |||
77 | ImplicitCastExpr *makeLvalueToRvalue(const Expr *Arg, QualType Ty); | |||
78 | ||||
79 | /// Make RValue out of variable declaration, creating a temporary | |||
80 | /// DeclRefExpr in the process. | |||
81 | ImplicitCastExpr * | |||
82 | makeLvalueToRvalue(const VarDecl *Decl, | |||
83 | bool RefersToEnclosingVariableOrCapture = false); | |||
84 | ||||
85 | /// Create an implicit cast of the given type. | |||
86 | ImplicitCastExpr *makeImplicitCast(const Expr *Arg, QualType Ty, | |||
87 | CastKind CK = CK_LValueToRValue); | |||
88 | ||||
89 | /// Create an Objective-C bool literal. | |||
90 | ObjCBoolLiteralExpr *makeObjCBool(bool Val); | |||
91 | ||||
92 | /// Create an Objective-C ivar reference. | |||
93 | ObjCIvarRefExpr *makeObjCIvarRef(const Expr *Base, const ObjCIvarDecl *IVar); | |||
94 | ||||
95 | /// Create a Return statement. | |||
96 | ReturnStmt *makeReturn(const Expr *RetVal); | |||
97 | ||||
98 | /// Create an integer literal expression of the given type. | |||
99 | IntegerLiteral *makeIntegerLiteral(uint64_t Value, QualType Ty); | |||
100 | ||||
101 | /// Create a member expression. | |||
102 | MemberExpr *makeMemberExpression(Expr *base, ValueDecl *MemberDecl, | |||
103 | bool IsArrow = false, | |||
104 | ExprValueKind ValueKind = VK_LValue); | |||
105 | ||||
106 | /// Returns a *first* member field of a record declaration with a given name. | |||
107 | /// \return an nullptr if no member with such a name exists. | |||
108 | ValueDecl *findMemberField(const RecordDecl *RD, StringRef Name); | |||
109 | ||||
110 | private: | |||
111 | ASTContext &C; | |||
112 | }; | |||
113 | } | |||
114 | ||||
115 | BinaryOperator *ASTMaker::makeAssignment(const Expr *LHS, const Expr *RHS, | |||
116 | QualType Ty) { | |||
117 | return new (C) BinaryOperator(const_cast<Expr*>(LHS), const_cast<Expr*>(RHS), | |||
118 | BO_Assign, Ty, VK_RValue, | |||
119 | OK_Ordinary, SourceLocation(), FPOptions()); | |||
120 | } | |||
121 | ||||
122 | BinaryOperator *ASTMaker::makeComparison(const Expr *LHS, const Expr *RHS, | |||
123 | BinaryOperator::Opcode Op) { | |||
124 | assert(BinaryOperator::isLogicalOp(Op) ||((BinaryOperator::isLogicalOp(Op) || BinaryOperator::isComparisonOp (Op)) ? static_cast<void> (0) : __assert_fail ("BinaryOperator::isLogicalOp(Op) || BinaryOperator::isComparisonOp(Op)" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/Analysis/BodyFarm.cpp" , 125, __PRETTY_FUNCTION__)) | |||
125 | BinaryOperator::isComparisonOp(Op))((BinaryOperator::isLogicalOp(Op) || BinaryOperator::isComparisonOp (Op)) ? static_cast<void> (0) : __assert_fail ("BinaryOperator::isLogicalOp(Op) || BinaryOperator::isComparisonOp(Op)" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/Analysis/BodyFarm.cpp" , 125, __PRETTY_FUNCTION__)); | |||
126 | return new (C) BinaryOperator(const_cast<Expr*>(LHS), | |||
127 | const_cast<Expr*>(RHS), | |||
128 | Op, | |||
129 | C.getLogicalOperationType(), | |||
130 | VK_RValue, | |||
131 | OK_Ordinary, SourceLocation(), FPOptions()); | |||
132 | } | |||
133 | ||||
134 | CompoundStmt *ASTMaker::makeCompound(ArrayRef<Stmt *> Stmts) { | |||
135 | return CompoundStmt::Create(C, Stmts, SourceLocation(), SourceLocation()); | |||
136 | } | |||
137 | ||||
138 | DeclRefExpr *ASTMaker::makeDeclRefExpr( | |||
139 | const VarDecl *D, | |||
140 | bool RefersToEnclosingVariableOrCapture) { | |||
141 | QualType Type = D->getType().getNonReferenceType(); | |||
| ||||
142 | ||||
143 | DeclRefExpr *DR = DeclRefExpr::Create( | |||
144 | C, NestedNameSpecifierLoc(), SourceLocation(), const_cast<VarDecl *>(D), | |||
145 | RefersToEnclosingVariableOrCapture, SourceLocation(), Type, VK_LValue); | |||
146 | return DR; | |||
147 | } | |||
148 | ||||
149 | UnaryOperator *ASTMaker::makeDereference(const Expr *Arg, QualType Ty) { | |||
150 | return new (C) UnaryOperator(const_cast<Expr*>(Arg), UO_Deref, Ty, | |||
151 | VK_LValue, OK_Ordinary, SourceLocation(), | |||
152 | /*CanOverflow*/ false); | |||
153 | } | |||
154 | ||||
155 | ImplicitCastExpr *ASTMaker::makeLvalueToRvalue(const Expr *Arg, QualType Ty) { | |||
156 | return makeImplicitCast(Arg, Ty, CK_LValueToRValue); | |||
157 | } | |||
158 | ||||
159 | ImplicitCastExpr * | |||
160 | ASTMaker::makeLvalueToRvalue(const VarDecl *Arg, | |||
161 | bool RefersToEnclosingVariableOrCapture) { | |||
162 | QualType Type = Arg->getType().getNonReferenceType(); | |||
163 | return makeLvalueToRvalue(makeDeclRefExpr(Arg, | |||
164 | RefersToEnclosingVariableOrCapture), | |||
165 | Type); | |||
166 | } | |||
167 | ||||
168 | ImplicitCastExpr *ASTMaker::makeImplicitCast(const Expr *Arg, QualType Ty, | |||
169 | CastKind CK) { | |||
170 | return ImplicitCastExpr::Create(C, Ty, | |||
171 | /* CastKind=*/ CK, | |||
172 | /* Expr=*/ const_cast<Expr *>(Arg), | |||
173 | /* CXXCastPath=*/ nullptr, | |||
174 | /* ExprValueKind=*/ VK_RValue); | |||
175 | } | |||
176 | ||||
177 | Expr *ASTMaker::makeIntegralCast(const Expr *Arg, QualType Ty) { | |||
178 | if (Arg->getType() == Ty) | |||
179 | return const_cast<Expr*>(Arg); | |||
180 | ||||
181 | return ImplicitCastExpr::Create(C, Ty, CK_IntegralCast, | |||
182 | const_cast<Expr*>(Arg), nullptr, VK_RValue); | |||
183 | } | |||
184 | ||||
185 | ImplicitCastExpr *ASTMaker::makeIntegralCastToBoolean(const Expr *Arg) { | |||
186 | return ImplicitCastExpr::Create(C, C.BoolTy, CK_IntegralToBoolean, | |||
187 | const_cast<Expr*>(Arg), nullptr, VK_RValue); | |||
188 | } | |||
189 | ||||
190 | ObjCBoolLiteralExpr *ASTMaker::makeObjCBool(bool Val) { | |||
191 | QualType Ty = C.getBOOLDecl() ? C.getBOOLType() : C.ObjCBuiltinBoolTy; | |||
192 | return new (C) ObjCBoolLiteralExpr(Val, Ty, SourceLocation()); | |||
193 | } | |||
194 | ||||
195 | ObjCIvarRefExpr *ASTMaker::makeObjCIvarRef(const Expr *Base, | |||
196 | const ObjCIvarDecl *IVar) { | |||
197 | return new (C) ObjCIvarRefExpr(const_cast<ObjCIvarDecl*>(IVar), | |||
198 | IVar->getType(), SourceLocation(), | |||
199 | SourceLocation(), const_cast<Expr*>(Base), | |||
200 | /*arrow=*/true, /*free=*/false); | |||
201 | } | |||
202 | ||||
203 | ReturnStmt *ASTMaker::makeReturn(const Expr *RetVal) { | |||
204 | return ReturnStmt::Create(C, SourceLocation(), const_cast<Expr *>(RetVal), | |||
205 | /* NRVOCandidate=*/nullptr); | |||
206 | } | |||
207 | ||||
208 | IntegerLiteral *ASTMaker::makeIntegerLiteral(uint64_t Value, QualType Ty) { | |||
209 | llvm::APInt APValue = llvm::APInt(C.getTypeSize(Ty), Value); | |||
210 | return IntegerLiteral::Create(C, APValue, Ty, SourceLocation()); | |||
211 | } | |||
212 | ||||
213 | MemberExpr *ASTMaker::makeMemberExpression(Expr *base, ValueDecl *MemberDecl, | |||
214 | bool IsArrow, | |||
215 | ExprValueKind ValueKind) { | |||
216 | ||||
217 | DeclAccessPair FoundDecl = DeclAccessPair::make(MemberDecl, AS_public); | |||
218 | return MemberExpr::Create( | |||
219 | C, base, IsArrow, SourceLocation(), NestedNameSpecifierLoc(), | |||
220 | SourceLocation(), MemberDecl, FoundDecl, | |||
221 | DeclarationNameInfo(MemberDecl->getDeclName(), SourceLocation()), | |||
222 | /* TemplateArgumentListInfo=*/ nullptr, MemberDecl->getType(), ValueKind, | |||
223 | OK_Ordinary); | |||
224 | } | |||
225 | ||||
226 | ValueDecl *ASTMaker::findMemberField(const RecordDecl *RD, StringRef Name) { | |||
227 | ||||
228 | CXXBasePaths Paths( | |||
229 | /* FindAmbiguities=*/false, | |||
230 | /* RecordPaths=*/false, | |||
231 | /* DetectVirtual=*/ false); | |||
232 | const IdentifierInfo &II = C.Idents.get(Name); | |||
233 | DeclarationName DeclName = C.DeclarationNames.getIdentifier(&II); | |||
234 | ||||
235 | DeclContextLookupResult Decls = RD->lookup(DeclName); | |||
236 | for (NamedDecl *FoundDecl : Decls) | |||
237 | if (!FoundDecl->getDeclContext()->isFunctionOrMethod()) | |||
238 | return cast<ValueDecl>(FoundDecl); | |||
239 | ||||
240 | return nullptr; | |||
241 | } | |||
242 | ||||
243 | //===----------------------------------------------------------------------===// | |||
244 | // Creation functions for faux ASTs. | |||
245 | //===----------------------------------------------------------------------===// | |||
246 | ||||
247 | typedef Stmt *(*FunctionFarmer)(ASTContext &C, const FunctionDecl *D); | |||
248 | ||||
249 | static CallExpr *create_call_once_funcptr_call(ASTContext &C, ASTMaker M, | |||
250 | const ParmVarDecl *Callback, | |||
251 | ArrayRef<Expr *> CallArgs) { | |||
252 | ||||
253 | QualType Ty = Callback->getType(); | |||
254 | DeclRefExpr *Call = M.makeDeclRefExpr(Callback); | |||
255 | Expr *SubExpr; | |||
256 | if (Ty->isRValueReferenceType()) { | |||
257 | SubExpr = M.makeImplicitCast( | |||
258 | Call, Ty.getNonReferenceType(), CK_LValueToRValue); | |||
259 | } else if (Ty->isLValueReferenceType() && | |||
260 | Call->getType()->isFunctionType()) { | |||
261 | Ty = C.getPointerType(Ty.getNonReferenceType()); | |||
262 | SubExpr = M.makeImplicitCast(Call, Ty, CK_FunctionToPointerDecay); | |||
263 | } else if (Ty->isLValueReferenceType() | |||
264 | && Call->getType()->isPointerType() | |||
265 | && Call->getType()->getPointeeType()->isFunctionType()){ | |||
266 | SubExpr = Call; | |||
267 | } else { | |||
268 | llvm_unreachable("Unexpected state")::llvm::llvm_unreachable_internal("Unexpected state", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/Analysis/BodyFarm.cpp" , 268); | |||
269 | } | |||
270 | ||||
271 | return CallExpr::Create(C, SubExpr, CallArgs, C.VoidTy, VK_RValue, | |||
272 | SourceLocation()); | |||
273 | } | |||
274 | ||||
275 | static CallExpr *create_call_once_lambda_call(ASTContext &C, ASTMaker M, | |||
276 | const ParmVarDecl *Callback, | |||
277 | CXXRecordDecl *CallbackDecl, | |||
278 | ArrayRef<Expr *> CallArgs) { | |||
279 | assert(CallbackDecl != nullptr)((CallbackDecl != nullptr) ? static_cast<void> (0) : __assert_fail ("CallbackDecl != nullptr", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/Analysis/BodyFarm.cpp" , 279, __PRETTY_FUNCTION__)); | |||
280 | assert(CallbackDecl->isLambda())((CallbackDecl->isLambda()) ? static_cast<void> (0) : __assert_fail ("CallbackDecl->isLambda()", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/Analysis/BodyFarm.cpp" , 280, __PRETTY_FUNCTION__)); | |||
281 | FunctionDecl *callOperatorDecl = CallbackDecl->getLambdaCallOperator(); | |||
282 | assert(callOperatorDecl != nullptr)((callOperatorDecl != nullptr) ? static_cast<void> (0) : __assert_fail ("callOperatorDecl != nullptr", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/Analysis/BodyFarm.cpp" , 282, __PRETTY_FUNCTION__)); | |||
283 | ||||
284 | DeclRefExpr *callOperatorDeclRef = | |||
285 | DeclRefExpr::Create(/* Ctx =*/ C, | |||
286 | /* QualifierLoc =*/ NestedNameSpecifierLoc(), | |||
287 | /* TemplateKWLoc =*/ SourceLocation(), | |||
288 | const_cast<FunctionDecl *>(callOperatorDecl), | |||
289 | /* RefersToEnclosingVariableOrCapture=*/ false, | |||
290 | /* NameLoc =*/ SourceLocation(), | |||
291 | /* T =*/ callOperatorDecl->getType(), | |||
292 | /* VK =*/ VK_LValue); | |||
293 | ||||
294 | return CXXOperatorCallExpr::Create( | |||
295 | /*AstContext=*/C, OO_Call, callOperatorDeclRef, | |||
296 | /*args=*/CallArgs, | |||
297 | /*QualType=*/C.VoidTy, | |||
298 | /*ExprValueType=*/VK_RValue, | |||
299 | /*SourceLocation=*/SourceLocation(), FPOptions()); | |||
300 | } | |||
301 | ||||
302 | /// Create a fake body for std::call_once. | |||
303 | /// Emulates the following function body: | |||
304 | /// | |||
305 | /// \code | |||
306 | /// typedef struct once_flag_s { | |||
307 | /// unsigned long __state = 0; | |||
308 | /// } once_flag; | |||
309 | /// template<class Callable> | |||
310 | /// void call_once(once_flag& o, Callable func) { | |||
311 | /// if (!o.__state) { | |||
312 | /// func(); | |||
313 | /// } | |||
314 | /// o.__state = 1; | |||
315 | /// } | |||
316 | /// \endcode | |||
317 | static Stmt *create_call_once(ASTContext &C, const FunctionDecl *D) { | |||
318 | LLVM_DEBUG(llvm::dbgs() << "Generating body for call_once\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("body-farm")) { llvm::dbgs() << "Generating body for call_once\n" ; } } while (false); | |||
319 | ||||
320 | // We need at least two parameters. | |||
321 | if (D->param_size() < 2) | |||
322 | return nullptr; | |||
323 | ||||
324 | ASTMaker M(C); | |||
325 | ||||
326 | const ParmVarDecl *Flag = D->getParamDecl(0); | |||
327 | const ParmVarDecl *Callback = D->getParamDecl(1); | |||
328 | ||||
329 | if (!Callback->getType()->isReferenceType()) { | |||
330 | llvm::dbgs() << "libcxx03 std::call_once implementation, skipping.\n"; | |||
331 | return nullptr; | |||
332 | } | |||
333 | if (!Flag->getType()->isReferenceType()) { | |||
334 | llvm::dbgs() << "unknown std::call_once implementation, skipping.\n"; | |||
335 | return nullptr; | |||
336 | } | |||
337 | ||||
338 | QualType CallbackType = Callback->getType().getNonReferenceType(); | |||
339 | ||||
340 | // Nullable pointer, non-null iff function is a CXXRecordDecl. | |||
341 | CXXRecordDecl *CallbackRecordDecl = CallbackType->getAsCXXRecordDecl(); | |||
342 | QualType FlagType = Flag->getType().getNonReferenceType(); | |||
343 | auto *FlagRecordDecl = FlagType->getAsRecordDecl(); | |||
344 | ||||
345 | if (!FlagRecordDecl) { | |||
346 | LLVM_DEBUG(llvm::dbgs() << "Flag field is not a record: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("body-farm")) { llvm::dbgs() << "Flag field is not a record: " << "unknown std::call_once implementation, " << "ignoring the call.\n" ; } } while (false) | |||
347 | << "unknown std::call_once implementation, "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("body-farm")) { llvm::dbgs() << "Flag field is not a record: " << "unknown std::call_once implementation, " << "ignoring the call.\n" ; } } while (false) | |||
348 | << "ignoring the call.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("body-farm")) { llvm::dbgs() << "Flag field is not a record: " << "unknown std::call_once implementation, " << "ignoring the call.\n" ; } } while (false); | |||
349 | return nullptr; | |||
350 | } | |||
351 | ||||
352 | // We initially assume libc++ implementation of call_once, | |||
353 | // where the once_flag struct has a field `__state_`. | |||
354 | ValueDecl *FlagFieldDecl = M.findMemberField(FlagRecordDecl, "__state_"); | |||
355 | ||||
356 | // Otherwise, try libstdc++ implementation, with a field | |||
357 | // `_M_once` | |||
358 | if (!FlagFieldDecl) { | |||
359 | FlagFieldDecl = M.findMemberField(FlagRecordDecl, "_M_once"); | |||
360 | } | |||
361 | ||||
362 | if (!FlagFieldDecl) { | |||
363 | LLVM_DEBUG(llvm::dbgs() << "No field _M_once or __state_ found on "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("body-farm")) { llvm::dbgs() << "No field _M_once or __state_ found on " << "std::once_flag struct: unknown std::call_once " << "implementation, ignoring the call."; } } while (false) | |||
364 | << "std::once_flag struct: unknown std::call_once "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("body-farm")) { llvm::dbgs() << "No field _M_once or __state_ found on " << "std::once_flag struct: unknown std::call_once " << "implementation, ignoring the call."; } } while (false) | |||
365 | << "implementation, ignoring the call.")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("body-farm")) { llvm::dbgs() << "No field _M_once or __state_ found on " << "std::once_flag struct: unknown std::call_once " << "implementation, ignoring the call."; } } while (false); | |||
366 | return nullptr; | |||
367 | } | |||
368 | ||||
369 | bool isLambdaCall = CallbackRecordDecl && CallbackRecordDecl->isLambda(); | |||
370 | if (CallbackRecordDecl && !isLambdaCall) { | |||
371 | LLVM_DEBUG(llvm::dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("body-farm")) { llvm::dbgs() << "Not supported: synthesizing body for functors when " << "body farming std::call_once, ignoring the call."; } } while (false) | |||
372 | << "Not supported: synthesizing body for functors when "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("body-farm")) { llvm::dbgs() << "Not supported: synthesizing body for functors when " << "body farming std::call_once, ignoring the call."; } } while (false) | |||
373 | << "body farming std::call_once, ignoring the call.")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("body-farm")) { llvm::dbgs() << "Not supported: synthesizing body for functors when " << "body farming std::call_once, ignoring the call."; } } while (false); | |||
374 | return nullptr; | |||
375 | } | |||
376 | ||||
377 | SmallVector<Expr *, 5> CallArgs; | |||
378 | const FunctionProtoType *CallbackFunctionType; | |||
379 | if (isLambdaCall) { | |||
380 | ||||
381 | // Lambda requires callback itself inserted as a first parameter. | |||
382 | CallArgs.push_back( | |||
383 | M.makeDeclRefExpr(Callback, | |||
384 | /* RefersToEnclosingVariableOrCapture=*/ true)); | |||
385 | CallbackFunctionType = CallbackRecordDecl->getLambdaCallOperator() | |||
386 | ->getType() | |||
387 | ->getAs<FunctionProtoType>(); | |||
388 | } else if (!CallbackType->getPointeeType().isNull()) { | |||
389 | CallbackFunctionType = | |||
390 | CallbackType->getPointeeType()->getAs<FunctionProtoType>(); | |||
391 | } else { | |||
392 | CallbackFunctionType = CallbackType->getAs<FunctionProtoType>(); | |||
393 | } | |||
394 | ||||
395 | if (!CallbackFunctionType) | |||
396 | return nullptr; | |||
397 | ||||
398 | // First two arguments are used for the flag and for the callback. | |||
399 | if (D->getNumParams() != CallbackFunctionType->getNumParams() + 2) { | |||
400 | LLVM_DEBUG(llvm::dbgs() << "Types of params of the callback do not match "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("body-farm")) { llvm::dbgs() << "Types of params of the callback do not match " << "params passed to std::call_once, " << "ignoring the call\n" ; } } while (false) | |||
401 | << "params passed to std::call_once, "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("body-farm")) { llvm::dbgs() << "Types of params of the callback do not match " << "params passed to std::call_once, " << "ignoring the call\n" ; } } while (false) | |||
402 | << "ignoring the call\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("body-farm")) { llvm::dbgs() << "Types of params of the callback do not match " << "params passed to std::call_once, " << "ignoring the call\n" ; } } while (false); | |||
403 | return nullptr; | |||
404 | } | |||
405 | ||||
406 | // All arguments past first two ones are passed to the callback, | |||
407 | // and we turn lvalues into rvalues if the argument is not passed by | |||
408 | // reference. | |||
409 | for (unsigned int ParamIdx = 2; ParamIdx < D->getNumParams(); ParamIdx++) { | |||
410 | const ParmVarDecl *PDecl = D->getParamDecl(ParamIdx); | |||
411 | if (PDecl && | |||
412 | CallbackFunctionType->getParamType(ParamIdx - 2) | |||
413 | .getNonReferenceType() | |||
414 | .getCanonicalType() != | |||
415 | PDecl->getType().getNonReferenceType().getCanonicalType()) { | |||
416 | LLVM_DEBUG(llvm::dbgs() << "Types of params of the callback do not match "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("body-farm")) { llvm::dbgs() << "Types of params of the callback do not match " << "params passed to std::call_once, " << "ignoring the call\n" ; } } while (false) | |||
417 | << "params passed to std::call_once, "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("body-farm")) { llvm::dbgs() << "Types of params of the callback do not match " << "params passed to std::call_once, " << "ignoring the call\n" ; } } while (false) | |||
418 | << "ignoring the call\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("body-farm")) { llvm::dbgs() << "Types of params of the callback do not match " << "params passed to std::call_once, " << "ignoring the call\n" ; } } while (false); | |||
419 | return nullptr; | |||
420 | } | |||
421 | Expr *ParamExpr = M.makeDeclRefExpr(PDecl); | |||
422 | if (!CallbackFunctionType->getParamType(ParamIdx - 2)->isReferenceType()) { | |||
423 | QualType PTy = PDecl->getType().getNonReferenceType(); | |||
424 | ParamExpr = M.makeLvalueToRvalue(ParamExpr, PTy); | |||
425 | } | |||
426 | CallArgs.push_back(ParamExpr); | |||
427 | } | |||
428 | ||||
429 | CallExpr *CallbackCall; | |||
430 | if (isLambdaCall) { | |||
431 | ||||
432 | CallbackCall = create_call_once_lambda_call(C, M, Callback, | |||
433 | CallbackRecordDecl, CallArgs); | |||
434 | } else { | |||
435 | ||||
436 | // Function pointer case. | |||
437 | CallbackCall = create_call_once_funcptr_call(C, M, Callback, CallArgs); | |||
438 | } | |||
439 | ||||
440 | DeclRefExpr *FlagDecl = | |||
441 | M.makeDeclRefExpr(Flag, | |||
442 | /* RefersToEnclosingVariableOrCapture=*/true); | |||
443 | ||||
444 | ||||
445 | MemberExpr *Deref = M.makeMemberExpression(FlagDecl, FlagFieldDecl); | |||
446 | assert(Deref->isLValue())((Deref->isLValue()) ? static_cast<void> (0) : __assert_fail ("Deref->isLValue()", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/Analysis/BodyFarm.cpp" , 446, __PRETTY_FUNCTION__)); | |||
447 | QualType DerefType = Deref->getType(); | |||
448 | ||||
449 | // Negation predicate. | |||
450 | UnaryOperator *FlagCheck = new (C) UnaryOperator( | |||
451 | /* input=*/ | |||
452 | M.makeImplicitCast(M.makeLvalueToRvalue(Deref, DerefType), DerefType, | |||
453 | CK_IntegralToBoolean), | |||
454 | /* opc=*/ UO_LNot, | |||
455 | /* QualType=*/ C.IntTy, | |||
456 | /* ExprValueKind=*/ VK_RValue, | |||
457 | /* ExprObjectKind=*/ OK_Ordinary, SourceLocation(), | |||
458 | /* CanOverflow*/ false); | |||
459 | ||||
460 | // Create assignment. | |||
461 | BinaryOperator *FlagAssignment = M.makeAssignment( | |||
462 | Deref, M.makeIntegralCast(M.makeIntegerLiteral(1, C.IntTy), DerefType), | |||
463 | DerefType); | |||
464 | ||||
465 | auto *Out = | |||
466 | IfStmt::Create(C, SourceLocation(), | |||
467 | /* IsConstexpr=*/false, | |||
468 | /* init=*/nullptr, | |||
469 | /* var=*/nullptr, | |||
470 | /* cond=*/FlagCheck, | |||
471 | /* then=*/M.makeCompound({CallbackCall, FlagAssignment})); | |||
472 | ||||
473 | return Out; | |||
474 | } | |||
475 | ||||
476 | /// Create a fake body for dispatch_once. | |||
477 | static Stmt *create_dispatch_once(ASTContext &C, const FunctionDecl *D) { | |||
478 | // Check if we have at least two parameters. | |||
479 | if (D->param_size() != 2) | |||
480 | return nullptr; | |||
481 | ||||
482 | // Check if the first parameter is a pointer to integer type. | |||
483 | const ParmVarDecl *Predicate = D->getParamDecl(0); | |||
484 | QualType PredicateQPtrTy = Predicate->getType(); | |||
485 | const PointerType *PredicatePtrTy = PredicateQPtrTy->getAs<PointerType>(); | |||
486 | if (!PredicatePtrTy) | |||
487 | return nullptr; | |||
488 | QualType PredicateTy = PredicatePtrTy->getPointeeType(); | |||
489 | if (!PredicateTy->isIntegerType()) | |||
490 | return nullptr; | |||
491 | ||||
492 | // Check if the second parameter is the proper block type. | |||
493 | const ParmVarDecl *Block = D->getParamDecl(1); | |||
494 | QualType Ty = Block->getType(); | |||
495 | if (!isDispatchBlock(Ty)) | |||
496 | return nullptr; | |||
497 | ||||
498 | // Everything checks out. Create a fakse body that checks the predicate, | |||
499 | // sets it, and calls the block. Basically, an AST dump of: | |||
500 | // | |||
501 | // void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block) { | |||
502 | // if (*predicate != ~0l) { | |||
503 | // *predicate = ~0l; | |||
504 | // block(); | |||
505 | // } | |||
506 | // } | |||
507 | ||||
508 | ASTMaker M(C); | |||
509 | ||||
510 | // (1) Create the call. | |||
511 | CallExpr *CE = CallExpr::Create( | |||
512 | /*ASTContext=*/C, | |||
513 | /*StmtClass=*/M.makeLvalueToRvalue(/*Expr=*/Block), | |||
514 | /*args=*/None, | |||
515 | /*QualType=*/C.VoidTy, | |||
516 | /*ExprValueType=*/VK_RValue, | |||
517 | /*SourceLocation=*/SourceLocation()); | |||
518 | ||||
519 | // (2) Create the assignment to the predicate. | |||
520 | Expr *DoneValue = | |||
521 | new (C) UnaryOperator(M.makeIntegerLiteral(0, C.LongTy), UO_Not, C.LongTy, | |||
522 | VK_RValue, OK_Ordinary, SourceLocation(), | |||
523 | /*CanOverflow*/false); | |||
524 | ||||
525 | BinaryOperator *B = | |||
526 | M.makeAssignment( | |||
527 | M.makeDereference( | |||
528 | M.makeLvalueToRvalue( | |||
529 | M.makeDeclRefExpr(Predicate), PredicateQPtrTy), | |||
530 | PredicateTy), | |||
531 | M.makeIntegralCast(DoneValue, PredicateTy), | |||
532 | PredicateTy); | |||
533 | ||||
534 | // (3) Create the compound statement. | |||
535 | Stmt *Stmts[] = { B, CE }; | |||
536 | CompoundStmt *CS = M.makeCompound(Stmts); | |||
537 | ||||
538 | // (4) Create the 'if' condition. | |||
539 | ImplicitCastExpr *LValToRval = | |||
540 | M.makeLvalueToRvalue( | |||
541 | M.makeDereference( | |||
542 | M.makeLvalueToRvalue( | |||
543 | M.makeDeclRefExpr(Predicate), | |||
544 | PredicateQPtrTy), | |||
545 | PredicateTy), | |||
546 | PredicateTy); | |||
547 | ||||
548 | Expr *GuardCondition = M.makeComparison(LValToRval, DoneValue, BO_NE); | |||
549 | // (5) Create the 'if' statement. | |||
550 | auto *If = IfStmt::Create(C, SourceLocation(), | |||
551 | /* IsConstexpr=*/false, | |||
552 | /* init=*/nullptr, | |||
553 | /* var=*/nullptr, | |||
554 | /* cond=*/GuardCondition, | |||
555 | /* then=*/CS); | |||
556 | return If; | |||
557 | } | |||
558 | ||||
559 | /// Create a fake body for dispatch_sync. | |||
560 | static Stmt *create_dispatch_sync(ASTContext &C, const FunctionDecl *D) { | |||
561 | // Check if we have at least two parameters. | |||
562 | if (D->param_size() != 2) | |||
563 | return nullptr; | |||
564 | ||||
565 | // Check if the second parameter is a block. | |||
566 | const ParmVarDecl *PV = D->getParamDecl(1); | |||
567 | QualType Ty = PV->getType(); | |||
568 | if (!isDispatchBlock(Ty)) | |||
569 | return nullptr; | |||
570 | ||||
571 | // Everything checks out. Create a fake body that just calls the block. | |||
572 | // This is basically just an AST dump of: | |||
573 | // | |||
574 | // void dispatch_sync(dispatch_queue_t queue, void (^block)(void)) { | |||
575 | // block(); | |||
576 | // } | |||
577 | // | |||
578 | ASTMaker M(C); | |||
579 | DeclRefExpr *DR = M.makeDeclRefExpr(PV); | |||
580 | ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty); | |||
581 | CallExpr *CE = | |||
582 | CallExpr::Create(C, ICE, None, C.VoidTy, VK_RValue, SourceLocation()); | |||
583 | return CE; | |||
584 | } | |||
585 | ||||
586 | static Stmt *create_OSAtomicCompareAndSwap(ASTContext &C, const FunctionDecl *D) | |||
587 | { | |||
588 | // There are exactly 3 arguments. | |||
589 | if (D->param_size() != 3) | |||
590 | return nullptr; | |||
591 | ||||
592 | // Signature: | |||
593 | // _Bool OSAtomicCompareAndSwapPtr(void *__oldValue, | |||
594 | // void *__newValue, | |||
595 | // void * volatile *__theValue) | |||
596 | // Generate body: | |||
597 | // if (oldValue == *theValue) { | |||
598 | // *theValue = newValue; | |||
599 | // return YES; | |||
600 | // } | |||
601 | // else return NO; | |||
602 | ||||
603 | QualType ResultTy = D->getReturnType(); | |||
604 | bool isBoolean = ResultTy->isBooleanType(); | |||
605 | if (!isBoolean && !ResultTy->isIntegralType(C)) | |||
606 | return nullptr; | |||
607 | ||||
608 | const ParmVarDecl *OldValue = D->getParamDecl(0); | |||
609 | QualType OldValueTy = OldValue->getType(); | |||
610 | ||||
611 | const ParmVarDecl *NewValue = D->getParamDecl(1); | |||
612 | QualType NewValueTy = NewValue->getType(); | |||
613 | ||||
614 | assert(OldValueTy == NewValueTy)((OldValueTy == NewValueTy) ? static_cast<void> (0) : __assert_fail ("OldValueTy == NewValueTy", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/Analysis/BodyFarm.cpp" , 614, __PRETTY_FUNCTION__)); | |||
615 | ||||
616 | const ParmVarDecl *TheValue = D->getParamDecl(2); | |||
617 | QualType TheValueTy = TheValue->getType(); | |||
618 | const PointerType *PT = TheValueTy->getAs<PointerType>(); | |||
619 | if (!PT) | |||
620 | return nullptr; | |||
621 | QualType PointeeTy = PT->getPointeeType(); | |||
622 | ||||
623 | ASTMaker M(C); | |||
624 | // Construct the comparison. | |||
625 | Expr *Comparison = | |||
626 | M.makeComparison( | |||
627 | M.makeLvalueToRvalue(M.makeDeclRefExpr(OldValue), OldValueTy), | |||
628 | M.makeLvalueToRvalue( | |||
629 | M.makeDereference( | |||
630 | M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy), | |||
631 | PointeeTy), | |||
632 | PointeeTy), | |||
633 | BO_EQ); | |||
634 | ||||
635 | // Construct the body of the IfStmt. | |||
636 | Stmt *Stmts[2]; | |||
637 | Stmts[0] = | |||
638 | M.makeAssignment( | |||
639 | M.makeDereference( | |||
640 | M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy), | |||
641 | PointeeTy), | |||
642 | M.makeLvalueToRvalue(M.makeDeclRefExpr(NewValue), NewValueTy), | |||
643 | NewValueTy); | |||
644 | ||||
645 | Expr *BoolVal = M.makeObjCBool(true); | |||
646 | Expr *RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal) | |||
647 | : M.makeIntegralCast(BoolVal, ResultTy); | |||
648 | Stmts[1] = M.makeReturn(RetVal); | |||
649 | CompoundStmt *Body = M.makeCompound(Stmts); | |||
650 | ||||
651 | // Construct the else clause. | |||
652 | BoolVal = M.makeObjCBool(false); | |||
653 | RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal) | |||
654 | : M.makeIntegralCast(BoolVal, ResultTy); | |||
655 | Stmt *Else = M.makeReturn(RetVal); | |||
656 | ||||
657 | /// Construct the If. | |||
658 | auto *If = IfStmt::Create(C, SourceLocation(), | |||
659 | /* IsConstexpr=*/false, | |||
660 | /* init=*/nullptr, | |||
661 | /* var=*/nullptr, Comparison, Body, | |||
662 | SourceLocation(), Else); | |||
663 | ||||
664 | return If; | |||
665 | } | |||
666 | ||||
667 | Stmt *BodyFarm::getBody(const FunctionDecl *D) { | |||
668 | Optional<Stmt *> &Val = Bodies[D]; | |||
669 | if (Val.hasValue()) | |||
| ||||
670 | return Val.getValue(); | |||
671 | ||||
672 | Val = nullptr; | |||
673 | ||||
674 | if (D->getIdentifier() == nullptr) | |||
675 | return nullptr; | |||
676 | ||||
677 | StringRef Name = D->getName(); | |||
678 | if (Name.empty()) | |||
679 | return nullptr; | |||
680 | ||||
681 | FunctionFarmer FF; | |||
682 | ||||
683 | if (Name.startswith("OSAtomicCompareAndSwap") || | |||
684 | Name.startswith("objc_atomicCompareAndSwap")) { | |||
685 | FF = create_OSAtomicCompareAndSwap; | |||
686 | } else if (Name == "call_once" && D->getDeclContext()->isStdNamespace()) { | |||
687 | FF = create_call_once; | |||
688 | } else { | |||
689 | FF = llvm::StringSwitch<FunctionFarmer>(Name) | |||
690 | .Case("dispatch_sync", create_dispatch_sync) | |||
691 | .Case("dispatch_once", create_dispatch_once) | |||
692 | .Default(nullptr); | |||
693 | } | |||
694 | ||||
695 | if (FF) { Val = FF(C, D); } | |||
696 | else if (Injector) { Val = Injector->getBody(D); } | |||
697 | return Val.getValue(); | |||
698 | } | |||
699 | ||||
700 | static const ObjCIvarDecl *findBackingIvar(const ObjCPropertyDecl *Prop) { | |||
701 | const ObjCIvarDecl *IVar = Prop->getPropertyIvarDecl(); | |||
702 | ||||
703 | if (IVar) | |||
704 | return IVar; | |||
705 | ||||
706 | // When a readonly property is shadowed in a class extensions with a | |||
707 | // a readwrite property, the instance variable belongs to the shadowing | |||
708 | // property rather than the shadowed property. If there is no instance | |||
709 | // variable on a readonly property, check to see whether the property is | |||
710 | // shadowed and if so try to get the instance variable from shadowing | |||
711 | // property. | |||
712 | if (!Prop->isReadOnly()) | |||
713 | return nullptr; | |||
714 | ||||
715 | auto *Container = cast<ObjCContainerDecl>(Prop->getDeclContext()); | |||
716 | const ObjCInterfaceDecl *PrimaryInterface = nullptr; | |||
717 | if (auto *InterfaceDecl = dyn_cast<ObjCInterfaceDecl>(Container)) { | |||
718 | PrimaryInterface = InterfaceDecl; | |||
719 | } else if (auto *CategoryDecl = dyn_cast<ObjCCategoryDecl>(Container)) { | |||
720 | PrimaryInterface = CategoryDecl->getClassInterface(); | |||
721 | } else if (auto *ImplDecl = dyn_cast<ObjCImplDecl>(Container)) { | |||
722 | PrimaryInterface = ImplDecl->getClassInterface(); | |||
723 | } else { | |||
724 | return nullptr; | |||
725 | } | |||
726 | ||||
727 | // FindPropertyVisibleInPrimaryClass() looks first in class extensions, so it | |||
728 | // is guaranteed to find the shadowing property, if it exists, rather than | |||
729 | // the shadowed property. | |||
730 | auto *ShadowingProp = PrimaryInterface->FindPropertyVisibleInPrimaryClass( | |||
731 | Prop->getIdentifier(), Prop->getQueryKind()); | |||
732 | if (ShadowingProp && ShadowingProp != Prop) { | |||
733 | IVar = ShadowingProp->getPropertyIvarDecl(); | |||
734 | } | |||
735 | ||||
736 | return IVar; | |||
737 | } | |||
738 | ||||
739 | static Stmt *createObjCPropertyGetter(ASTContext &Ctx, | |||
740 | const ObjCPropertyDecl *Prop) { | |||
741 | // First, find the backing ivar. | |||
742 | const ObjCIvarDecl *IVar = findBackingIvar(Prop); | |||
743 | if (!IVar) | |||
744 | return nullptr; | |||
745 | ||||
746 | // Ignore weak variables, which have special behavior. | |||
747 | if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) | |||
748 | return nullptr; | |||
749 | ||||
750 | // Look to see if Sema has synthesized a body for us. This happens in | |||
751 | // Objective-C++ because the return value may be a C++ class type with a | |||
752 | // non-trivial copy constructor. We can only do this if we can find the | |||
753 | // @synthesize for this property, though (or if we know it's been auto- | |||
754 | // synthesized). | |||
755 | const ObjCImplementationDecl *ImplDecl = | |||
756 | IVar->getContainingInterface()->getImplementation(); | |||
757 | if (ImplDecl) { | |||
758 | for (const auto *I : ImplDecl->property_impls()) { | |||
759 | if (I->getPropertyDecl() != Prop) | |||
760 | continue; | |||
761 | ||||
762 | if (I->getGetterCXXConstructor()) { | |||
763 | ASTMaker M(Ctx); | |||
764 | return M.makeReturn(I->getGetterCXXConstructor()); | |||
765 | } | |||
766 | } | |||
767 | } | |||
768 | ||||
769 | // Sanity check that the property is the same type as the ivar, or a | |||
770 | // reference to it, and that it is either an object pointer or trivially | |||
771 | // copyable. | |||
772 | if (!Ctx.hasSameUnqualifiedType(IVar->getType(), | |||
773 | Prop->getType().getNonReferenceType())) | |||
774 | return nullptr; | |||
775 | if (!IVar->getType()->isObjCLifetimeType() && | |||
776 | !IVar->getType().isTriviallyCopyableType(Ctx)) | |||
777 | return nullptr; | |||
778 | ||||
779 | // Generate our body: | |||
780 | // return self->_ivar; | |||
781 | ASTMaker M(Ctx); | |||
782 | ||||
783 | const VarDecl *selfVar = Prop->getGetterMethodDecl()->getSelfDecl(); | |||
784 | if (!selfVar) | |||
785 | return nullptr; | |||
786 | ||||
787 | Expr *loadedIVar = | |||
788 | M.makeObjCIvarRef( | |||
789 | M.makeLvalueToRvalue( | |||
790 | M.makeDeclRefExpr(selfVar), | |||
791 | selfVar->getType()), | |||
792 | IVar); | |||
793 | ||||
794 | if (!Prop->getType()->isReferenceType()) | |||
795 | loadedIVar = M.makeLvalueToRvalue(loadedIVar, IVar->getType()); | |||
796 | ||||
797 | return M.makeReturn(loadedIVar); | |||
798 | } | |||
799 | ||||
800 | Stmt *BodyFarm::getBody(const ObjCMethodDecl *D) { | |||
801 | // We currently only know how to synthesize property accessors. | |||
802 | if (!D->isPropertyAccessor()) | |||
803 | return nullptr; | |||
804 | ||||
805 | D = D->getCanonicalDecl(); | |||
806 | ||||
807 | // We should not try to synthesize explicitly redefined accessors. | |||
808 | // We do not know for sure how they behave. | |||
809 | if (!D->isImplicit()) | |||
810 | return nullptr; | |||
811 | ||||
812 | Optional<Stmt *> &Val = Bodies[D]; | |||
813 | if (Val.hasValue()) | |||
814 | return Val.getValue(); | |||
815 | Val = nullptr; | |||
816 | ||||
817 | const ObjCPropertyDecl *Prop = D->findPropertyDecl(); | |||
818 | if (!Prop) | |||
819 | return nullptr; | |||
820 | ||||
821 | // For now, we only synthesize getters. | |||
822 | // Synthesizing setters would cause false negatives in the | |||
823 | // RetainCountChecker because the method body would bind the parameter | |||
824 | // to an instance variable, causing it to escape. This would prevent | |||
825 | // warning in the following common scenario: | |||
826 | // | |||
827 | // id foo = [[NSObject alloc] init]; | |||
828 | // self.foo = foo; // We should warn that foo leaks here. | |||
829 | // | |||
830 | if (D->param_size() != 0) | |||
831 | return nullptr; | |||
832 | ||||
833 | Val = createObjCPropertyGetter(C, Prop); | |||
834 | ||||
835 | return Val.getValue(); | |||
836 | } |
1 | //===- Decl.h - Classes for representing declarations -----------*- 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 Decl subclasses. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_CLANG_AST_DECL_H |
14 | #define LLVM_CLANG_AST_DECL_H |
15 | |
16 | #include "clang/AST/APValue.h" |
17 | #include "clang/AST/ASTContextAllocate.h" |
18 | #include "clang/AST/DeclBase.h" |
19 | #include "clang/AST/DeclarationName.h" |
20 | #include "clang/AST/ExternalASTSource.h" |
21 | #include "clang/AST/NestedNameSpecifier.h" |
22 | #include "clang/AST/Redeclarable.h" |
23 | #include "clang/AST/Type.h" |
24 | #include "clang/Basic/AddressSpaces.h" |
25 | #include "clang/Basic/Diagnostic.h" |
26 | #include "clang/Basic/IdentifierTable.h" |
27 | #include "clang/Basic/LLVM.h" |
28 | #include "clang/Basic/Linkage.h" |
29 | #include "clang/Basic/OperatorKinds.h" |
30 | #include "clang/Basic/PartialDiagnostic.h" |
31 | #include "clang/Basic/PragmaKinds.h" |
32 | #include "clang/Basic/SourceLocation.h" |
33 | #include "clang/Basic/Specifiers.h" |
34 | #include "clang/Basic/Visibility.h" |
35 | #include "llvm/ADT/APSInt.h" |
36 | #include "llvm/ADT/ArrayRef.h" |
37 | #include "llvm/ADT/Optional.h" |
38 | #include "llvm/ADT/PointerIntPair.h" |
39 | #include "llvm/ADT/PointerUnion.h" |
40 | #include "llvm/ADT/StringRef.h" |
41 | #include "llvm/ADT/iterator_range.h" |
42 | #include "llvm/Support/Casting.h" |
43 | #include "llvm/Support/Compiler.h" |
44 | #include "llvm/Support/TrailingObjects.h" |
45 | #include <cassert> |
46 | #include <cstddef> |
47 | #include <cstdint> |
48 | #include <string> |
49 | #include <utility> |
50 | |
51 | namespace clang { |
52 | |
53 | class ASTContext; |
54 | struct ASTTemplateArgumentListInfo; |
55 | class Attr; |
56 | class CompoundStmt; |
57 | class DependentFunctionTemplateSpecializationInfo; |
58 | class EnumDecl; |
59 | class Expr; |
60 | class FunctionTemplateDecl; |
61 | class FunctionTemplateSpecializationInfo; |
62 | class LabelStmt; |
63 | class MemberSpecializationInfo; |
64 | class Module; |
65 | class NamespaceDecl; |
66 | class ParmVarDecl; |
67 | class RecordDecl; |
68 | class Stmt; |
69 | class StringLiteral; |
70 | class TagDecl; |
71 | class TemplateArgumentList; |
72 | class TemplateArgumentListInfo; |
73 | class TemplateParameterList; |
74 | class TypeAliasTemplateDecl; |
75 | class TypeLoc; |
76 | class UnresolvedSetImpl; |
77 | class VarTemplateDecl; |
78 | |
79 | /// A container of type source information. |
80 | /// |
81 | /// A client can read the relevant info using TypeLoc wrappers, e.g: |
82 | /// @code |
83 | /// TypeLoc TL = TypeSourceInfo->getTypeLoc(); |
84 | /// TL.getBeginLoc().print(OS, SrcMgr); |
85 | /// @endcode |
86 | class alignas(8) TypeSourceInfo { |
87 | // Contains a memory block after the class, used for type source information, |
88 | // allocated by ASTContext. |
89 | friend class ASTContext; |
90 | |
91 | QualType Ty; |
92 | |
93 | TypeSourceInfo(QualType ty) : Ty(ty) {} |
94 | |
95 | public: |
96 | /// Return the type wrapped by this type source info. |
97 | QualType getType() const { return Ty; } |
98 | |
99 | /// Return the TypeLoc wrapper for the type source info. |
100 | TypeLoc getTypeLoc() const; // implemented in TypeLoc.h |
101 | |
102 | /// Override the type stored in this TypeSourceInfo. Use with caution! |
103 | void overrideType(QualType T) { Ty = T; } |
104 | }; |
105 | |
106 | /// The top declaration context. |
107 | class TranslationUnitDecl : public Decl, public DeclContext { |
108 | ASTContext &Ctx; |
109 | |
110 | /// The (most recently entered) anonymous namespace for this |
111 | /// translation unit, if one has been created. |
112 | NamespaceDecl *AnonymousNamespace = nullptr; |
113 | |
114 | explicit TranslationUnitDecl(ASTContext &ctx); |
115 | |
116 | virtual void anchor(); |
117 | |
118 | public: |
119 | ASTContext &getASTContext() const { return Ctx; } |
120 | |
121 | NamespaceDecl *getAnonymousNamespace() const { return AnonymousNamespace; } |
122 | void setAnonymousNamespace(NamespaceDecl *D) { AnonymousNamespace = D; } |
123 | |
124 | static TranslationUnitDecl *Create(ASTContext &C); |
125 | |
126 | // Implement isa/cast/dyncast/etc. |
127 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
128 | static bool classofKind(Kind K) { return K == TranslationUnit; } |
129 | static DeclContext *castToDeclContext(const TranslationUnitDecl *D) { |
130 | return static_cast<DeclContext *>(const_cast<TranslationUnitDecl*>(D)); |
131 | } |
132 | static TranslationUnitDecl *castFromDeclContext(const DeclContext *DC) { |
133 | return static_cast<TranslationUnitDecl *>(const_cast<DeclContext*>(DC)); |
134 | } |
135 | }; |
136 | |
137 | /// Represents a `#pragma comment` line. Always a child of |
138 | /// TranslationUnitDecl. |
139 | class PragmaCommentDecl final |
140 | : public Decl, |
141 | private llvm::TrailingObjects<PragmaCommentDecl, char> { |
142 | friend class ASTDeclReader; |
143 | friend class ASTDeclWriter; |
144 | friend TrailingObjects; |
145 | |
146 | PragmaMSCommentKind CommentKind; |
147 | |
148 | PragmaCommentDecl(TranslationUnitDecl *TU, SourceLocation CommentLoc, |
149 | PragmaMSCommentKind CommentKind) |
150 | : Decl(PragmaComment, TU, CommentLoc), CommentKind(CommentKind) {} |
151 | |
152 | virtual void anchor(); |
153 | |
154 | public: |
155 | static PragmaCommentDecl *Create(const ASTContext &C, TranslationUnitDecl *DC, |
156 | SourceLocation CommentLoc, |
157 | PragmaMSCommentKind CommentKind, |
158 | StringRef Arg); |
159 | static PragmaCommentDecl *CreateDeserialized(ASTContext &C, unsigned ID, |
160 | unsigned ArgSize); |
161 | |
162 | PragmaMSCommentKind getCommentKind() const { return CommentKind; } |
163 | |
164 | StringRef getArg() const { return getTrailingObjects<char>(); } |
165 | |
166 | // Implement isa/cast/dyncast/etc. |
167 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
168 | static bool classofKind(Kind K) { return K == PragmaComment; } |
169 | }; |
170 | |
171 | /// Represents a `#pragma detect_mismatch` line. Always a child of |
172 | /// TranslationUnitDecl. |
173 | class PragmaDetectMismatchDecl final |
174 | : public Decl, |
175 | private llvm::TrailingObjects<PragmaDetectMismatchDecl, char> { |
176 | friend class ASTDeclReader; |
177 | friend class ASTDeclWriter; |
178 | friend TrailingObjects; |
179 | |
180 | size_t ValueStart; |
181 | |
182 | PragmaDetectMismatchDecl(TranslationUnitDecl *TU, SourceLocation Loc, |
183 | size_t ValueStart) |
184 | : Decl(PragmaDetectMismatch, TU, Loc), ValueStart(ValueStart) {} |
185 | |
186 | virtual void anchor(); |
187 | |
188 | public: |
189 | static PragmaDetectMismatchDecl *Create(const ASTContext &C, |
190 | TranslationUnitDecl *DC, |
191 | SourceLocation Loc, StringRef Name, |
192 | StringRef Value); |
193 | static PragmaDetectMismatchDecl * |
194 | CreateDeserialized(ASTContext &C, unsigned ID, unsigned NameValueSize); |
195 | |
196 | StringRef getName() const { return getTrailingObjects<char>(); } |
197 | StringRef getValue() const { return getTrailingObjects<char>() + ValueStart; } |
198 | |
199 | // Implement isa/cast/dyncast/etc. |
200 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
201 | static bool classofKind(Kind K) { return K == PragmaDetectMismatch; } |
202 | }; |
203 | |
204 | /// Declaration context for names declared as extern "C" in C++. This |
205 | /// is neither the semantic nor lexical context for such declarations, but is |
206 | /// used to check for conflicts with other extern "C" declarations. Example: |
207 | /// |
208 | /// \code |
209 | /// namespace N { extern "C" void f(); } // #1 |
210 | /// void N::f() {} // #2 |
211 | /// namespace M { extern "C" void f(); } // #3 |
212 | /// \endcode |
213 | /// |
214 | /// The semantic context of #1 is namespace N and its lexical context is the |
215 | /// LinkageSpecDecl; the semantic context of #2 is namespace N and its lexical |
216 | /// context is the TU. However, both declarations are also visible in the |
217 | /// extern "C" context. |
218 | /// |
219 | /// The declaration at #3 finds it is a redeclaration of \c N::f through |
220 | /// lookup in the extern "C" context. |
221 | class ExternCContextDecl : public Decl, public DeclContext { |
222 | explicit ExternCContextDecl(TranslationUnitDecl *TU) |
223 | : Decl(ExternCContext, TU, SourceLocation()), |
224 | DeclContext(ExternCContext) {} |
225 | |
226 | virtual void anchor(); |
227 | |
228 | public: |
229 | static ExternCContextDecl *Create(const ASTContext &C, |
230 | TranslationUnitDecl *TU); |
231 | |
232 | // Implement isa/cast/dyncast/etc. |
233 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
234 | static bool classofKind(Kind K) { return K == ExternCContext; } |
235 | static DeclContext *castToDeclContext(const ExternCContextDecl *D) { |
236 | return static_cast<DeclContext *>(const_cast<ExternCContextDecl*>(D)); |
237 | } |
238 | static ExternCContextDecl *castFromDeclContext(const DeclContext *DC) { |
239 | return static_cast<ExternCContextDecl *>(const_cast<DeclContext*>(DC)); |
240 | } |
241 | }; |
242 | |
243 | /// This represents a decl that may have a name. Many decls have names such |
244 | /// as ObjCMethodDecl, but not \@class, etc. |
245 | /// |
246 | /// Note that not every NamedDecl is actually named (e.g., a struct might |
247 | /// be anonymous), and not every name is an identifier. |
248 | class NamedDecl : public Decl { |
249 | /// The name of this declaration, which is typically a normal |
250 | /// identifier but may also be a special kind of name (C++ |
251 | /// constructor, Objective-C selector, etc.) |
252 | DeclarationName Name; |
253 | |
254 | virtual void anchor(); |
255 | |
256 | private: |
257 | NamedDecl *getUnderlyingDeclImpl() LLVM_READONLY__attribute__((__pure__)); |
258 | |
259 | protected: |
260 | NamedDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName N) |
261 | : Decl(DK, DC, L), Name(N) {} |
262 | |
263 | public: |
264 | /// Get the identifier that names this declaration, if there is one. |
265 | /// |
266 | /// This will return NULL if this declaration has no name (e.g., for |
267 | /// an unnamed class) or if the name is a special name (C++ constructor, |
268 | /// Objective-C selector, etc.). |
269 | IdentifierInfo *getIdentifier() const { return Name.getAsIdentifierInfo(); } |
270 | |
271 | /// Get the name of identifier for this declaration as a StringRef. |
272 | /// |
273 | /// This requires that the declaration have a name and that it be a simple |
274 | /// identifier. |
275 | StringRef getName() const { |
276 | assert(Name.isIdentifier() && "Name is not a simple identifier")((Name.isIdentifier() && "Name is not a simple identifier" ) ? static_cast<void> (0) : __assert_fail ("Name.isIdentifier() && \"Name is not a simple identifier\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 276, __PRETTY_FUNCTION__)); |
277 | return getIdentifier() ? getIdentifier()->getName() : ""; |
278 | } |
279 | |
280 | /// Get a human-readable name for the declaration, even if it is one of the |
281 | /// special kinds of names (C++ constructor, Objective-C selector, etc). |
282 | /// |
283 | /// Creating this name requires expensive string manipulation, so it should |
284 | /// be called only when performance doesn't matter. For simple declarations, |
285 | /// getNameAsCString() should suffice. |
286 | // |
287 | // FIXME: This function should be renamed to indicate that it is not just an |
288 | // alternate form of getName(), and clients should move as appropriate. |
289 | // |
290 | // FIXME: Deprecated, move clients to getName(). |
291 | std::string getNameAsString() const { return Name.getAsString(); } |
292 | |
293 | virtual void printName(raw_ostream &os) const; |
294 | |
295 | /// Get the actual, stored name of the declaration, which may be a special |
296 | /// name. |
297 | DeclarationName getDeclName() const { return Name; } |
298 | |
299 | /// Set the name of this declaration. |
300 | void setDeclName(DeclarationName N) { Name = N; } |
301 | |
302 | /// Returns a human-readable qualified name for this declaration, like |
303 | /// A::B::i, for i being member of namespace A::B. |
304 | /// |
305 | /// If the declaration is not a member of context which can be named (record, |
306 | /// namespace), it will return the same result as printName(). |
307 | /// |
308 | /// Creating this name is expensive, so it should be called only when |
309 | /// performance doesn't matter. |
310 | void printQualifiedName(raw_ostream &OS) const; |
311 | void printQualifiedName(raw_ostream &OS, const PrintingPolicy &Policy) const; |
312 | |
313 | // FIXME: Remove string version. |
314 | std::string getQualifiedNameAsString() const; |
315 | |
316 | /// Appends a human-readable name for this declaration into the given stream. |
317 | /// |
318 | /// This is the method invoked by Sema when displaying a NamedDecl |
319 | /// in a diagnostic. It does not necessarily produce the same |
320 | /// result as printName(); for example, class template |
321 | /// specializations are printed with their template arguments. |
322 | virtual void getNameForDiagnostic(raw_ostream &OS, |
323 | const PrintingPolicy &Policy, |
324 | bool Qualified) const; |
325 | |
326 | /// Determine whether this declaration, if known to be well-formed within |
327 | /// its context, will replace the declaration OldD if introduced into scope. |
328 | /// |
329 | /// A declaration will replace another declaration if, for example, it is |
330 | /// a redeclaration of the same variable or function, but not if it is a |
331 | /// declaration of a different kind (function vs. class) or an overloaded |
332 | /// function. |
333 | /// |
334 | /// \param IsKnownNewer \c true if this declaration is known to be newer |
335 | /// than \p OldD (for instance, if this declaration is newly-created). |
336 | bool declarationReplaces(NamedDecl *OldD, bool IsKnownNewer = true) const; |
337 | |
338 | /// Determine whether this declaration has linkage. |
339 | bool hasLinkage() const; |
340 | |
341 | using Decl::isModulePrivate; |
342 | using Decl::setModulePrivate; |
343 | |
344 | /// Determine whether this declaration is a C++ class member. |
345 | bool isCXXClassMember() const { |
346 | const DeclContext *DC = getDeclContext(); |
347 | |
348 | // C++0x [class.mem]p1: |
349 | // The enumerators of an unscoped enumeration defined in |
350 | // the class are members of the class. |
351 | if (isa<EnumDecl>(DC)) |
352 | DC = DC->getRedeclContext(); |
353 | |
354 | return DC->isRecord(); |
355 | } |
356 | |
357 | /// Determine whether the given declaration is an instance member of |
358 | /// a C++ class. |
359 | bool isCXXInstanceMember() const; |
360 | |
361 | /// Determine what kind of linkage this entity has. |
362 | /// |
363 | /// This is not the linkage as defined by the standard or the codegen notion |
364 | /// of linkage. It is just an implementation detail that is used to compute |
365 | /// those. |
366 | Linkage getLinkageInternal() const; |
367 | |
368 | /// Get the linkage from a semantic point of view. Entities in |
369 | /// anonymous namespaces are external (in c++98). |
370 | Linkage getFormalLinkage() const { |
371 | return clang::getFormalLinkage(getLinkageInternal()); |
372 | } |
373 | |
374 | /// True if this decl has external linkage. |
375 | bool hasExternalFormalLinkage() const { |
376 | return isExternalFormalLinkage(getLinkageInternal()); |
377 | } |
378 | |
379 | bool isExternallyVisible() const { |
380 | return clang::isExternallyVisible(getLinkageInternal()); |
381 | } |
382 | |
383 | /// Determine whether this declaration can be redeclared in a |
384 | /// different translation unit. |
385 | bool isExternallyDeclarable() const { |
386 | return isExternallyVisible() && !getOwningModuleForLinkage(); |
387 | } |
388 | |
389 | /// Determines the visibility of this entity. |
390 | Visibility getVisibility() const { |
391 | return getLinkageAndVisibility().getVisibility(); |
392 | } |
393 | |
394 | /// Determines the linkage and visibility of this entity. |
395 | LinkageInfo getLinkageAndVisibility() const; |
396 | |
397 | /// Kinds of explicit visibility. |
398 | enum ExplicitVisibilityKind { |
399 | /// Do an LV computation for, ultimately, a type. |
400 | /// Visibility may be restricted by type visibility settings and |
401 | /// the visibility of template arguments. |
402 | VisibilityForType, |
403 | |
404 | /// Do an LV computation for, ultimately, a non-type declaration. |
405 | /// Visibility may be restricted by value visibility settings and |
406 | /// the visibility of template arguments. |
407 | VisibilityForValue |
408 | }; |
409 | |
410 | /// If visibility was explicitly specified for this |
411 | /// declaration, return that visibility. |
412 | Optional<Visibility> |
413 | getExplicitVisibility(ExplicitVisibilityKind kind) const; |
414 | |
415 | /// True if the computed linkage is valid. Used for consistency |
416 | /// checking. Should always return true. |
417 | bool isLinkageValid() const; |
418 | |
419 | /// True if something has required us to compute the linkage |
420 | /// of this declaration. |
421 | /// |
422 | /// Language features which can retroactively change linkage (like a |
423 | /// typedef name for linkage purposes) may need to consider this, |
424 | /// but hopefully only in transitory ways during parsing. |
425 | bool hasLinkageBeenComputed() const { |
426 | return hasCachedLinkage(); |
427 | } |
428 | |
429 | /// Looks through UsingDecls and ObjCCompatibleAliasDecls for |
430 | /// the underlying named decl. |
431 | NamedDecl *getUnderlyingDecl() { |
432 | // Fast-path the common case. |
433 | if (this->getKind() != UsingShadow && |
434 | this->getKind() != ConstructorUsingShadow && |
435 | this->getKind() != ObjCCompatibleAlias && |
436 | this->getKind() != NamespaceAlias) |
437 | return this; |
438 | |
439 | return getUnderlyingDeclImpl(); |
440 | } |
441 | const NamedDecl *getUnderlyingDecl() const { |
442 | return const_cast<NamedDecl*>(this)->getUnderlyingDecl(); |
443 | } |
444 | |
445 | NamedDecl *getMostRecentDecl() { |
446 | return cast<NamedDecl>(static_cast<Decl *>(this)->getMostRecentDecl()); |
447 | } |
448 | const NamedDecl *getMostRecentDecl() const { |
449 | return const_cast<NamedDecl*>(this)->getMostRecentDecl(); |
450 | } |
451 | |
452 | ObjCStringFormatFamily getObjCFStringFormattingFamily() const; |
453 | |
454 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
455 | static bool classofKind(Kind K) { return K >= firstNamed && K <= lastNamed; } |
456 | }; |
457 | |
458 | inline raw_ostream &operator<<(raw_ostream &OS, const NamedDecl &ND) { |
459 | ND.printName(OS); |
460 | return OS; |
461 | } |
462 | |
463 | /// Represents the declaration of a label. Labels also have a |
464 | /// corresponding LabelStmt, which indicates the position that the label was |
465 | /// defined at. For normal labels, the location of the decl is the same as the |
466 | /// location of the statement. For GNU local labels (__label__), the decl |
467 | /// location is where the __label__ is. |
468 | class LabelDecl : public NamedDecl { |
469 | LabelStmt *TheStmt; |
470 | StringRef MSAsmName; |
471 | bool MSAsmNameResolved = false; |
472 | |
473 | /// For normal labels, this is the same as the main declaration |
474 | /// label, i.e., the location of the identifier; for GNU local labels, |
475 | /// this is the location of the __label__ keyword. |
476 | SourceLocation LocStart; |
477 | |
478 | LabelDecl(DeclContext *DC, SourceLocation IdentL, IdentifierInfo *II, |
479 | LabelStmt *S, SourceLocation StartL) |
480 | : NamedDecl(Label, DC, IdentL, II), TheStmt(S), LocStart(StartL) {} |
481 | |
482 | void anchor() override; |
483 | |
484 | public: |
485 | static LabelDecl *Create(ASTContext &C, DeclContext *DC, |
486 | SourceLocation IdentL, IdentifierInfo *II); |
487 | static LabelDecl *Create(ASTContext &C, DeclContext *DC, |
488 | SourceLocation IdentL, IdentifierInfo *II, |
489 | SourceLocation GnuLabelL); |
490 | static LabelDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
491 | |
492 | LabelStmt *getStmt() const { return TheStmt; } |
493 | void setStmt(LabelStmt *T) { TheStmt = T; } |
494 | |
495 | bool isGnuLocal() const { return LocStart != getLocation(); } |
496 | void setLocStart(SourceLocation L) { LocStart = L; } |
497 | |
498 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) { |
499 | return SourceRange(LocStart, getLocation()); |
500 | } |
501 | |
502 | bool isMSAsmLabel() const { return !MSAsmName.empty(); } |
503 | bool isResolvedMSAsmLabel() const { return isMSAsmLabel() && MSAsmNameResolved; } |
504 | void setMSAsmLabel(StringRef Name); |
505 | StringRef getMSAsmLabel() const { return MSAsmName; } |
506 | void setMSAsmLabelResolved() { MSAsmNameResolved = true; } |
507 | |
508 | // Implement isa/cast/dyncast/etc. |
509 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
510 | static bool classofKind(Kind K) { return K == Label; } |
511 | }; |
512 | |
513 | /// Represent a C++ namespace. |
514 | class NamespaceDecl : public NamedDecl, public DeclContext, |
515 | public Redeclarable<NamespaceDecl> |
516 | { |
517 | /// The starting location of the source range, pointing |
518 | /// to either the namespace or the inline keyword. |
519 | SourceLocation LocStart; |
520 | |
521 | /// The ending location of the source range. |
522 | SourceLocation RBraceLoc; |
523 | |
524 | /// A pointer to either the anonymous namespace that lives just inside |
525 | /// this namespace or to the first namespace in the chain (the latter case |
526 | /// only when this is not the first in the chain), along with a |
527 | /// boolean value indicating whether this is an inline namespace. |
528 | llvm::PointerIntPair<NamespaceDecl *, 1, bool> AnonOrFirstNamespaceAndInline; |
529 | |
530 | NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline, |
531 | SourceLocation StartLoc, SourceLocation IdLoc, |
532 | IdentifierInfo *Id, NamespaceDecl *PrevDecl); |
533 | |
534 | using redeclarable_base = Redeclarable<NamespaceDecl>; |
535 | |
536 | NamespaceDecl *getNextRedeclarationImpl() override; |
537 | NamespaceDecl *getPreviousDeclImpl() override; |
538 | NamespaceDecl *getMostRecentDeclImpl() override; |
539 | |
540 | public: |
541 | friend class ASTDeclReader; |
542 | friend class ASTDeclWriter; |
543 | |
544 | static NamespaceDecl *Create(ASTContext &C, DeclContext *DC, |
545 | bool Inline, SourceLocation StartLoc, |
546 | SourceLocation IdLoc, IdentifierInfo *Id, |
547 | NamespaceDecl *PrevDecl); |
548 | |
549 | static NamespaceDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
550 | |
551 | using redecl_range = redeclarable_base::redecl_range; |
552 | using redecl_iterator = redeclarable_base::redecl_iterator; |
553 | |
554 | using redeclarable_base::redecls_begin; |
555 | using redeclarable_base::redecls_end; |
556 | using redeclarable_base::redecls; |
557 | using redeclarable_base::getPreviousDecl; |
558 | using redeclarable_base::getMostRecentDecl; |
559 | using redeclarable_base::isFirstDecl; |
560 | |
561 | /// Returns true if this is an anonymous namespace declaration. |
562 | /// |
563 | /// For example: |
564 | /// \code |
565 | /// namespace { |
566 | /// ... |
567 | /// }; |
568 | /// \endcode |
569 | /// q.v. C++ [namespace.unnamed] |
570 | bool isAnonymousNamespace() const { |
571 | return !getIdentifier(); |
572 | } |
573 | |
574 | /// Returns true if this is an inline namespace declaration. |
575 | bool isInline() const { |
576 | return AnonOrFirstNamespaceAndInline.getInt(); |
577 | } |
578 | |
579 | /// Set whether this is an inline namespace declaration. |
580 | void setInline(bool Inline) { |
581 | AnonOrFirstNamespaceAndInline.setInt(Inline); |
582 | } |
583 | |
584 | /// Get the original (first) namespace declaration. |
585 | NamespaceDecl *getOriginalNamespace(); |
586 | |
587 | /// Get the original (first) namespace declaration. |
588 | const NamespaceDecl *getOriginalNamespace() const; |
589 | |
590 | /// Return true if this declaration is an original (first) declaration |
591 | /// of the namespace. This is false for non-original (subsequent) namespace |
592 | /// declarations and anonymous namespaces. |
593 | bool isOriginalNamespace() const; |
594 | |
595 | /// Retrieve the anonymous namespace nested inside this namespace, |
596 | /// if any. |
597 | NamespaceDecl *getAnonymousNamespace() const { |
598 | return getOriginalNamespace()->AnonOrFirstNamespaceAndInline.getPointer(); |
599 | } |
600 | |
601 | void setAnonymousNamespace(NamespaceDecl *D) { |
602 | getOriginalNamespace()->AnonOrFirstNamespaceAndInline.setPointer(D); |
603 | } |
604 | |
605 | /// Retrieves the canonical declaration of this namespace. |
606 | NamespaceDecl *getCanonicalDecl() override { |
607 | return getOriginalNamespace(); |
608 | } |
609 | const NamespaceDecl *getCanonicalDecl() const { |
610 | return getOriginalNamespace(); |
611 | } |
612 | |
613 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) { |
614 | return SourceRange(LocStart, RBraceLoc); |
615 | } |
616 | |
617 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return LocStart; } |
618 | SourceLocation getRBraceLoc() const { return RBraceLoc; } |
619 | void setLocStart(SourceLocation L) { LocStart = L; } |
620 | void setRBraceLoc(SourceLocation L) { RBraceLoc = L; } |
621 | |
622 | // Implement isa/cast/dyncast/etc. |
623 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
624 | static bool classofKind(Kind K) { return K == Namespace; } |
625 | static DeclContext *castToDeclContext(const NamespaceDecl *D) { |
626 | return static_cast<DeclContext *>(const_cast<NamespaceDecl*>(D)); |
627 | } |
628 | static NamespaceDecl *castFromDeclContext(const DeclContext *DC) { |
629 | return static_cast<NamespaceDecl *>(const_cast<DeclContext*>(DC)); |
630 | } |
631 | }; |
632 | |
633 | /// Represent the declaration of a variable (in which case it is |
634 | /// an lvalue) a function (in which case it is a function designator) or |
635 | /// an enum constant. |
636 | class ValueDecl : public NamedDecl { |
637 | QualType DeclType; |
638 | |
639 | void anchor() override; |
640 | |
641 | protected: |
642 | ValueDecl(Kind DK, DeclContext *DC, SourceLocation L, |
643 | DeclarationName N, QualType T) |
644 | : NamedDecl(DK, DC, L, N), DeclType(T) {} |
645 | |
646 | public: |
647 | QualType getType() const { return DeclType; } |
648 | void setType(QualType newType) { DeclType = newType; } |
649 | |
650 | /// Determine whether this symbol is weakly-imported, |
651 | /// or declared with the weak or weak-ref attr. |
652 | bool isWeak() const; |
653 | |
654 | // Implement isa/cast/dyncast/etc. |
655 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
656 | static bool classofKind(Kind K) { return K >= firstValue && K <= lastValue; } |
657 | }; |
658 | |
659 | /// A struct with extended info about a syntactic |
660 | /// name qualifier, to be used for the case of out-of-line declarations. |
661 | struct QualifierInfo { |
662 | NestedNameSpecifierLoc QualifierLoc; |
663 | |
664 | /// The number of "outer" template parameter lists. |
665 | /// The count includes all of the template parameter lists that were matched |
666 | /// against the template-ids occurring into the NNS and possibly (in the |
667 | /// case of an explicit specialization) a final "template <>". |
668 | unsigned NumTemplParamLists = 0; |
669 | |
670 | /// A new-allocated array of size NumTemplParamLists, |
671 | /// containing pointers to the "outer" template parameter lists. |
672 | /// It includes all of the template parameter lists that were matched |
673 | /// against the template-ids occurring into the NNS and possibly (in the |
674 | /// case of an explicit specialization) a final "template <>". |
675 | TemplateParameterList** TemplParamLists = nullptr; |
676 | |
677 | QualifierInfo() = default; |
678 | QualifierInfo(const QualifierInfo &) = delete; |
679 | QualifierInfo& operator=(const QualifierInfo &) = delete; |
680 | |
681 | /// Sets info about "outer" template parameter lists. |
682 | void setTemplateParameterListsInfo(ASTContext &Context, |
683 | ArrayRef<TemplateParameterList *> TPLists); |
684 | }; |
685 | |
686 | /// Represents a ValueDecl that came out of a declarator. |
687 | /// Contains type source information through TypeSourceInfo. |
688 | class DeclaratorDecl : public ValueDecl { |
689 | // A struct representing both a TInfo and a syntactic qualifier, |
690 | // to be used for the (uncommon) case of out-of-line declarations. |
691 | struct ExtInfo : public QualifierInfo { |
692 | TypeSourceInfo *TInfo; |
693 | }; |
694 | |
695 | llvm::PointerUnion<TypeSourceInfo *, ExtInfo *> DeclInfo; |
696 | |
697 | /// The start of the source range for this declaration, |
698 | /// ignoring outer template declarations. |
699 | SourceLocation InnerLocStart; |
700 | |
701 | bool hasExtInfo() const { return DeclInfo.is<ExtInfo*>(); } |
702 | ExtInfo *getExtInfo() { return DeclInfo.get<ExtInfo*>(); } |
703 | const ExtInfo *getExtInfo() const { return DeclInfo.get<ExtInfo*>(); } |
704 | |
705 | protected: |
706 | DeclaratorDecl(Kind DK, DeclContext *DC, SourceLocation L, |
707 | DeclarationName N, QualType T, TypeSourceInfo *TInfo, |
708 | SourceLocation StartL) |
709 | : ValueDecl(DK, DC, L, N, T), DeclInfo(TInfo), InnerLocStart(StartL) {} |
710 | |
711 | public: |
712 | friend class ASTDeclReader; |
713 | friend class ASTDeclWriter; |
714 | |
715 | TypeSourceInfo *getTypeSourceInfo() const { |
716 | return hasExtInfo() |
717 | ? getExtInfo()->TInfo |
718 | : DeclInfo.get<TypeSourceInfo*>(); |
719 | } |
720 | |
721 | void setTypeSourceInfo(TypeSourceInfo *TI) { |
722 | if (hasExtInfo()) |
723 | getExtInfo()->TInfo = TI; |
724 | else |
725 | DeclInfo = TI; |
726 | } |
727 | |
728 | /// Return start of source range ignoring outer template declarations. |
729 | SourceLocation getInnerLocStart() const { return InnerLocStart; } |
730 | void setInnerLocStart(SourceLocation L) { InnerLocStart = L; } |
731 | |
732 | /// Return start of source range taking into account any outer template |
733 | /// declarations. |
734 | SourceLocation getOuterLocStart() const; |
735 | |
736 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
737 | |
738 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { |
739 | return getOuterLocStart(); |
740 | } |
741 | |
742 | /// Retrieve the nested-name-specifier that qualifies the name of this |
743 | /// declaration, if it was present in the source. |
744 | NestedNameSpecifier *getQualifier() const { |
745 | return hasExtInfo() ? getExtInfo()->QualifierLoc.getNestedNameSpecifier() |
746 | : nullptr; |
747 | } |
748 | |
749 | /// Retrieve the nested-name-specifier (with source-location |
750 | /// information) that qualifies the name of this declaration, if it was |
751 | /// present in the source. |
752 | NestedNameSpecifierLoc getQualifierLoc() const { |
753 | return hasExtInfo() ? getExtInfo()->QualifierLoc |
754 | : NestedNameSpecifierLoc(); |
755 | } |
756 | |
757 | void setQualifierInfo(NestedNameSpecifierLoc QualifierLoc); |
758 | |
759 | unsigned getNumTemplateParameterLists() const { |
760 | return hasExtInfo() ? getExtInfo()->NumTemplParamLists : 0; |
761 | } |
762 | |
763 | TemplateParameterList *getTemplateParameterList(unsigned index) const { |
764 | assert(index < getNumTemplateParameterLists())((index < getNumTemplateParameterLists()) ? static_cast< void> (0) : __assert_fail ("index < getNumTemplateParameterLists()" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 764, __PRETTY_FUNCTION__)); |
765 | return getExtInfo()->TemplParamLists[index]; |
766 | } |
767 | |
768 | void setTemplateParameterListsInfo(ASTContext &Context, |
769 | ArrayRef<TemplateParameterList *> TPLists); |
770 | |
771 | SourceLocation getTypeSpecStartLoc() const; |
772 | |
773 | // Implement isa/cast/dyncast/etc. |
774 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
775 | static bool classofKind(Kind K) { |
776 | return K >= firstDeclarator && K <= lastDeclarator; |
777 | } |
778 | }; |
779 | |
780 | /// Structure used to store a statement, the constant value to |
781 | /// which it was evaluated (if any), and whether or not the statement |
782 | /// is an integral constant expression (if known). |
783 | struct EvaluatedStmt { |
784 | /// Whether this statement was already evaluated. |
785 | bool WasEvaluated : 1; |
786 | |
787 | /// Whether this statement is being evaluated. |
788 | bool IsEvaluating : 1; |
789 | |
790 | /// Whether we already checked whether this statement was an |
791 | /// integral constant expression. |
792 | bool CheckedICE : 1; |
793 | |
794 | /// Whether we are checking whether this statement is an |
795 | /// integral constant expression. |
796 | bool CheckingICE : 1; |
797 | |
798 | /// Whether this statement is an integral constant expression, |
799 | /// or in C++11, whether the statement is a constant expression. Only |
800 | /// valid if CheckedICE is true. |
801 | bool IsICE : 1; |
802 | |
803 | Stmt *Value; |
804 | APValue Evaluated; |
805 | |
806 | EvaluatedStmt() : WasEvaluated(false), IsEvaluating(false), CheckedICE(false), |
807 | CheckingICE(false), IsICE(false) {} |
808 | |
809 | }; |
810 | |
811 | /// Represents a variable declaration or definition. |
812 | class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> { |
813 | public: |
814 | /// Initialization styles. |
815 | enum InitializationStyle { |
816 | /// C-style initialization with assignment |
817 | CInit, |
818 | |
819 | /// Call-style initialization (C++98) |
820 | CallInit, |
821 | |
822 | /// Direct list-initialization (C++11) |
823 | ListInit |
824 | }; |
825 | |
826 | /// Kinds of thread-local storage. |
827 | enum TLSKind { |
828 | /// Not a TLS variable. |
829 | TLS_None, |
830 | |
831 | /// TLS with a known-constant initializer. |
832 | TLS_Static, |
833 | |
834 | /// TLS with a dynamic initializer. |
835 | TLS_Dynamic |
836 | }; |
837 | |
838 | /// Return the string used to specify the storage class \p SC. |
839 | /// |
840 | /// It is illegal to call this function with SC == None. |
841 | static const char *getStorageClassSpecifierString(StorageClass SC); |
842 | |
843 | protected: |
844 | // A pointer union of Stmt * and EvaluatedStmt *. When an EvaluatedStmt, we |
845 | // have allocated the auxiliary struct of information there. |
846 | // |
847 | // TODO: It is a bit unfortunate to use a PointerUnion inside the VarDecl for |
848 | // this as *many* VarDecls are ParmVarDecls that don't have default |
849 | // arguments. We could save some space by moving this pointer union to be |
850 | // allocated in trailing space when necessary. |
851 | using InitType = llvm::PointerUnion<Stmt *, EvaluatedStmt *>; |
852 | |
853 | /// The initializer for this variable or, for a ParmVarDecl, the |
854 | /// C++ default argument. |
855 | mutable InitType Init; |
856 | |
857 | private: |
858 | friend class ASTDeclReader; |
859 | friend class ASTNodeImporter; |
860 | friend class StmtIteratorBase; |
861 | |
862 | class VarDeclBitfields { |
863 | friend class ASTDeclReader; |
864 | friend class VarDecl; |
865 | |
866 | unsigned SClass : 3; |
867 | unsigned TSCSpec : 2; |
868 | unsigned InitStyle : 2; |
869 | |
870 | /// Whether this variable is an ARC pseudo-__strong variable; see |
871 | /// isARCPseudoStrong() for details. |
872 | unsigned ARCPseudoStrong : 1; |
873 | }; |
874 | enum { NumVarDeclBits = 8 }; |
875 | |
876 | protected: |
877 | enum { NumParameterIndexBits = 8 }; |
878 | |
879 | enum DefaultArgKind { |
880 | DAK_None, |
881 | DAK_Unparsed, |
882 | DAK_Uninstantiated, |
883 | DAK_Normal |
884 | }; |
885 | |
886 | class ParmVarDeclBitfields { |
887 | friend class ASTDeclReader; |
888 | friend class ParmVarDecl; |
889 | |
890 | unsigned : NumVarDeclBits; |
891 | |
892 | /// Whether this parameter inherits a default argument from a |
893 | /// prior declaration. |
894 | unsigned HasInheritedDefaultArg : 1; |
895 | |
896 | /// Describes the kind of default argument for this parameter. By default |
897 | /// this is none. If this is normal, then the default argument is stored in |
898 | /// the \c VarDecl initializer expression unless we were unable to parse |
899 | /// (even an invalid) expression for the default argument. |
900 | unsigned DefaultArgKind : 2; |
901 | |
902 | /// Whether this parameter undergoes K&R argument promotion. |
903 | unsigned IsKNRPromoted : 1; |
904 | |
905 | /// Whether this parameter is an ObjC method parameter or not. |
906 | unsigned IsObjCMethodParam : 1; |
907 | |
908 | /// If IsObjCMethodParam, a Decl::ObjCDeclQualifier. |
909 | /// Otherwise, the number of function parameter scopes enclosing |
910 | /// the function parameter scope in which this parameter was |
911 | /// declared. |
912 | unsigned ScopeDepthOrObjCQuals : 7; |
913 | |
914 | /// The number of parameters preceding this parameter in the |
915 | /// function parameter scope in which it was declared. |
916 | unsigned ParameterIndex : NumParameterIndexBits; |
917 | }; |
918 | |
919 | class NonParmVarDeclBitfields { |
920 | friend class ASTDeclReader; |
921 | friend class ImplicitParamDecl; |
922 | friend class VarDecl; |
923 | |
924 | unsigned : NumVarDeclBits; |
925 | |
926 | // FIXME: We need something similar to CXXRecordDecl::DefinitionData. |
927 | /// Whether this variable is a definition which was demoted due to |
928 | /// module merge. |
929 | unsigned IsThisDeclarationADemotedDefinition : 1; |
930 | |
931 | /// Whether this variable is the exception variable in a C++ catch |
932 | /// or an Objective-C @catch statement. |
933 | unsigned ExceptionVar : 1; |
934 | |
935 | /// Whether this local variable could be allocated in the return |
936 | /// slot of its function, enabling the named return value optimization |
937 | /// (NRVO). |
938 | unsigned NRVOVariable : 1; |
939 | |
940 | /// Whether this variable is the for-range-declaration in a C++0x |
941 | /// for-range statement. |
942 | unsigned CXXForRangeDecl : 1; |
943 | |
944 | /// Whether this variable is the for-in loop declaration in Objective-C. |
945 | unsigned ObjCForDecl : 1; |
946 | |
947 | /// Whether this variable is (C++1z) inline. |
948 | unsigned IsInline : 1; |
949 | |
950 | /// Whether this variable has (C++1z) inline explicitly specified. |
951 | unsigned IsInlineSpecified : 1; |
952 | |
953 | /// Whether this variable is (C++0x) constexpr. |
954 | unsigned IsConstexpr : 1; |
955 | |
956 | /// Whether this variable is the implicit variable for a lambda |
957 | /// init-capture. |
958 | unsigned IsInitCapture : 1; |
959 | |
960 | /// Whether this local extern variable's previous declaration was |
961 | /// declared in the same block scope. This controls whether we should merge |
962 | /// the type of this declaration with its previous declaration. |
963 | unsigned PreviousDeclInSameBlockScope : 1; |
964 | |
965 | /// Defines kind of the ImplicitParamDecl: 'this', 'self', 'vtt', '_cmd' or |
966 | /// something else. |
967 | unsigned ImplicitParamKind : 3; |
968 | |
969 | unsigned EscapingByref : 1; |
970 | }; |
971 | |
972 | union { |
973 | unsigned AllBits; |
974 | VarDeclBitfields VarDeclBits; |
975 | ParmVarDeclBitfields ParmVarDeclBits; |
976 | NonParmVarDeclBitfields NonParmVarDeclBits; |
977 | }; |
978 | |
979 | VarDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
980 | SourceLocation IdLoc, IdentifierInfo *Id, QualType T, |
981 | TypeSourceInfo *TInfo, StorageClass SC); |
982 | |
983 | using redeclarable_base = Redeclarable<VarDecl>; |
984 | |
985 | VarDecl *getNextRedeclarationImpl() override { |
986 | return getNextRedeclaration(); |
987 | } |
988 | |
989 | VarDecl *getPreviousDeclImpl() override { |
990 | return getPreviousDecl(); |
991 | } |
992 | |
993 | VarDecl *getMostRecentDeclImpl() override { |
994 | return getMostRecentDecl(); |
995 | } |
996 | |
997 | public: |
998 | using redecl_range = redeclarable_base::redecl_range; |
999 | using redecl_iterator = redeclarable_base::redecl_iterator; |
1000 | |
1001 | using redeclarable_base::redecls_begin; |
1002 | using redeclarable_base::redecls_end; |
1003 | using redeclarable_base::redecls; |
1004 | using redeclarable_base::getPreviousDecl; |
1005 | using redeclarable_base::getMostRecentDecl; |
1006 | using redeclarable_base::isFirstDecl; |
1007 | |
1008 | static VarDecl *Create(ASTContext &C, DeclContext *DC, |
1009 | SourceLocation StartLoc, SourceLocation IdLoc, |
1010 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
1011 | StorageClass S); |
1012 | |
1013 | static VarDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
1014 | |
1015 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
1016 | |
1017 | /// Returns the storage class as written in the source. For the |
1018 | /// computed linkage of symbol, see getLinkage. |
1019 | StorageClass getStorageClass() const { |
1020 | return (StorageClass) VarDeclBits.SClass; |
1021 | } |
1022 | void setStorageClass(StorageClass SC); |
1023 | |
1024 | void setTSCSpec(ThreadStorageClassSpecifier TSC) { |
1025 | VarDeclBits.TSCSpec = TSC; |
1026 | assert(VarDeclBits.TSCSpec == TSC && "truncation")((VarDeclBits.TSCSpec == TSC && "truncation") ? static_cast <void> (0) : __assert_fail ("VarDeclBits.TSCSpec == TSC && \"truncation\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1026, __PRETTY_FUNCTION__)); |
1027 | } |
1028 | ThreadStorageClassSpecifier getTSCSpec() const { |
1029 | return static_cast<ThreadStorageClassSpecifier>(VarDeclBits.TSCSpec); |
1030 | } |
1031 | TLSKind getTLSKind() const; |
1032 | |
1033 | /// Returns true if a variable with function scope is a non-static local |
1034 | /// variable. |
1035 | bool hasLocalStorage() const { |
1036 | if (getStorageClass() == SC_None) { |
1037 | // OpenCL v1.2 s6.5.3: The __constant or constant address space name is |
1038 | // used to describe variables allocated in global memory and which are |
1039 | // accessed inside a kernel(s) as read-only variables. As such, variables |
1040 | // in constant address space cannot have local storage. |
1041 | if (getType().getAddressSpace() == LangAS::opencl_constant) |
1042 | return false; |
1043 | // Second check is for C++11 [dcl.stc]p4. |
1044 | return !isFileVarDecl() && getTSCSpec() == TSCS_unspecified; |
1045 | } |
1046 | |
1047 | // Global Named Register (GNU extension) |
1048 | if (getStorageClass() == SC_Register && !isLocalVarDeclOrParm()) |
1049 | return false; |
1050 | |
1051 | // Return true for: Auto, Register. |
1052 | // Return false for: Extern, Static, PrivateExtern, OpenCLWorkGroupLocal. |
1053 | |
1054 | return getStorageClass() >= SC_Auto; |
1055 | } |
1056 | |
1057 | /// Returns true if a variable with function scope is a static local |
1058 | /// variable. |
1059 | bool isStaticLocal() const { |
1060 | return (getStorageClass() == SC_Static || |
1061 | // C++11 [dcl.stc]p4 |
1062 | (getStorageClass() == SC_None && getTSCSpec() == TSCS_thread_local)) |
1063 | && !isFileVarDecl(); |
1064 | } |
1065 | |
1066 | /// Returns true if a variable has extern or __private_extern__ |
1067 | /// storage. |
1068 | bool hasExternalStorage() const { |
1069 | return getStorageClass() == SC_Extern || |
1070 | getStorageClass() == SC_PrivateExtern; |
1071 | } |
1072 | |
1073 | /// Returns true for all variables that do not have local storage. |
1074 | /// |
1075 | /// This includes all global variables as well as static variables declared |
1076 | /// within a function. |
1077 | bool hasGlobalStorage() const { return !hasLocalStorage(); } |
1078 | |
1079 | /// Get the storage duration of this variable, per C++ [basic.stc]. |
1080 | StorageDuration getStorageDuration() const { |
1081 | return hasLocalStorage() ? SD_Automatic : |
1082 | getTSCSpec() ? SD_Thread : SD_Static; |
1083 | } |
1084 | |
1085 | /// Compute the language linkage. |
1086 | LanguageLinkage getLanguageLinkage() const; |
1087 | |
1088 | /// Determines whether this variable is a variable with external, C linkage. |
1089 | bool isExternC() const; |
1090 | |
1091 | /// Determines whether this variable's context is, or is nested within, |
1092 | /// a C++ extern "C" linkage spec. |
1093 | bool isInExternCContext() const; |
1094 | |
1095 | /// Determines whether this variable's context is, or is nested within, |
1096 | /// a C++ extern "C++" linkage spec. |
1097 | bool isInExternCXXContext() const; |
1098 | |
1099 | /// Returns true for local variable declarations other than parameters. |
1100 | /// Note that this includes static variables inside of functions. It also |
1101 | /// includes variables inside blocks. |
1102 | /// |
1103 | /// void foo() { int x; static int y; extern int z; } |
1104 | bool isLocalVarDecl() const { |
1105 | if (getKind() != Decl::Var && getKind() != Decl::Decomposition) |
1106 | return false; |
1107 | if (const DeclContext *DC = getLexicalDeclContext()) |
1108 | return DC->getRedeclContext()->isFunctionOrMethod(); |
1109 | return false; |
1110 | } |
1111 | |
1112 | /// Similar to isLocalVarDecl but also includes parameters. |
1113 | bool isLocalVarDeclOrParm() const { |
1114 | return isLocalVarDecl() || getKind() == Decl::ParmVar; |
1115 | } |
1116 | |
1117 | /// Similar to isLocalVarDecl, but excludes variables declared in blocks. |
1118 | bool isFunctionOrMethodVarDecl() const { |
1119 | if (getKind() != Decl::Var && getKind() != Decl::Decomposition) |
1120 | return false; |
1121 | const DeclContext *DC = getLexicalDeclContext()->getRedeclContext(); |
1122 | return DC->isFunctionOrMethod() && DC->getDeclKind() != Decl::Block; |
1123 | } |
1124 | |
1125 | /// Determines whether this is a static data member. |
1126 | /// |
1127 | /// This will only be true in C++, and applies to, e.g., the |
1128 | /// variable 'x' in: |
1129 | /// \code |
1130 | /// struct S { |
1131 | /// static int x; |
1132 | /// }; |
1133 | /// \endcode |
1134 | bool isStaticDataMember() const { |
1135 | // If it wasn't static, it would be a FieldDecl. |
1136 | return getKind() != Decl::ParmVar && getDeclContext()->isRecord(); |
1137 | } |
1138 | |
1139 | VarDecl *getCanonicalDecl() override; |
1140 | const VarDecl *getCanonicalDecl() const { |
1141 | return const_cast<VarDecl*>(this)->getCanonicalDecl(); |
1142 | } |
1143 | |
1144 | enum DefinitionKind { |
1145 | /// This declaration is only a declaration. |
1146 | DeclarationOnly, |
1147 | |
1148 | /// This declaration is a tentative definition. |
1149 | TentativeDefinition, |
1150 | |
1151 | /// This declaration is definitely a definition. |
1152 | Definition |
1153 | }; |
1154 | |
1155 | /// Check whether this declaration is a definition. If this could be |
1156 | /// a tentative definition (in C), don't check whether there's an overriding |
1157 | /// definition. |
1158 | DefinitionKind isThisDeclarationADefinition(ASTContext &) const; |
1159 | DefinitionKind isThisDeclarationADefinition() const { |
1160 | return isThisDeclarationADefinition(getASTContext()); |
1161 | } |
1162 | |
1163 | /// Check whether this variable is defined in this translation unit. |
1164 | DefinitionKind hasDefinition(ASTContext &) const; |
1165 | DefinitionKind hasDefinition() const { |
1166 | return hasDefinition(getASTContext()); |
1167 | } |
1168 | |
1169 | /// Get the tentative definition that acts as the real definition in a TU. |
1170 | /// Returns null if there is a proper definition available. |
1171 | VarDecl *getActingDefinition(); |
1172 | const VarDecl *getActingDefinition() const { |
1173 | return const_cast<VarDecl*>(this)->getActingDefinition(); |
1174 | } |
1175 | |
1176 | /// Get the real (not just tentative) definition for this declaration. |
1177 | VarDecl *getDefinition(ASTContext &); |
1178 | const VarDecl *getDefinition(ASTContext &C) const { |
1179 | return const_cast<VarDecl*>(this)->getDefinition(C); |
1180 | } |
1181 | VarDecl *getDefinition() { |
1182 | return getDefinition(getASTContext()); |
1183 | } |
1184 | const VarDecl *getDefinition() const { |
1185 | return const_cast<VarDecl*>(this)->getDefinition(); |
1186 | } |
1187 | |
1188 | /// Determine whether this is or was instantiated from an out-of-line |
1189 | /// definition of a static data member. |
1190 | bool isOutOfLine() const override; |
1191 | |
1192 | /// Returns true for file scoped variable declaration. |
1193 | bool isFileVarDecl() const { |
1194 | Kind K = getKind(); |
1195 | if (K == ParmVar || K == ImplicitParam) |
1196 | return false; |
1197 | |
1198 | if (getLexicalDeclContext()->getRedeclContext()->isFileContext()) |
1199 | return true; |
1200 | |
1201 | if (isStaticDataMember()) |
1202 | return true; |
1203 | |
1204 | return false; |
1205 | } |
1206 | |
1207 | /// Get the initializer for this variable, no matter which |
1208 | /// declaration it is attached to. |
1209 | const Expr *getAnyInitializer() const { |
1210 | const VarDecl *D; |
1211 | return getAnyInitializer(D); |
1212 | } |
1213 | |
1214 | /// Get the initializer for this variable, no matter which |
1215 | /// declaration it is attached to. Also get that declaration. |
1216 | const Expr *getAnyInitializer(const VarDecl *&D) const; |
1217 | |
1218 | bool hasInit() const; |
1219 | const Expr *getInit() const { |
1220 | return const_cast<VarDecl *>(this)->getInit(); |
1221 | } |
1222 | Expr *getInit(); |
1223 | |
1224 | /// Retrieve the address of the initializer expression. |
1225 | Stmt **getInitAddress(); |
1226 | |
1227 | void setInit(Expr *I); |
1228 | |
1229 | /// Determine whether this variable's value can be used in a |
1230 | /// constant expression, according to the relevant language standard. |
1231 | /// This only checks properties of the declaration, and does not check |
1232 | /// whether the initializer is in fact a constant expression. |
1233 | bool isUsableInConstantExpressions(ASTContext &C) const; |
1234 | |
1235 | EvaluatedStmt *ensureEvaluatedStmt() const; |
1236 | |
1237 | /// Attempt to evaluate the value of the initializer attached to this |
1238 | /// declaration, and produce notes explaining why it cannot be evaluated or is |
1239 | /// not a constant expression. Returns a pointer to the value if evaluation |
1240 | /// succeeded, 0 otherwise. |
1241 | APValue *evaluateValue() const; |
1242 | APValue *evaluateValue(SmallVectorImpl<PartialDiagnosticAt> &Notes) const; |
1243 | |
1244 | /// Return the already-evaluated value of this variable's |
1245 | /// initializer, or NULL if the value is not yet known. Returns pointer |
1246 | /// to untyped APValue if the value could not be evaluated. |
1247 | APValue *getEvaluatedValue() const; |
1248 | |
1249 | /// Determines whether it is already known whether the |
1250 | /// initializer is an integral constant expression or not. |
1251 | bool isInitKnownICE() const; |
1252 | |
1253 | /// Determines whether the initializer is an integral constant |
1254 | /// expression, or in C++11, whether the initializer is a constant |
1255 | /// expression. |
1256 | /// |
1257 | /// \pre isInitKnownICE() |
1258 | bool isInitICE() const; |
1259 | |
1260 | /// Determine whether the value of the initializer attached to this |
1261 | /// declaration is an integral constant expression. |
1262 | bool checkInitIsICE() const; |
1263 | |
1264 | void setInitStyle(InitializationStyle Style) { |
1265 | VarDeclBits.InitStyle = Style; |
1266 | } |
1267 | |
1268 | /// The style of initialization for this declaration. |
1269 | /// |
1270 | /// C-style initialization is "int x = 1;". Call-style initialization is |
1271 | /// a C++98 direct-initializer, e.g. "int x(1);". The Init expression will be |
1272 | /// the expression inside the parens or a "ClassType(a,b,c)" class constructor |
1273 | /// expression for class types. List-style initialization is C++11 syntax, |
1274 | /// e.g. "int x{1};". Clients can distinguish between different forms of |
1275 | /// initialization by checking this value. In particular, "int x = {1};" is |
1276 | /// C-style, "int x({1})" is call-style, and "int x{1};" is list-style; the |
1277 | /// Init expression in all three cases is an InitListExpr. |
1278 | InitializationStyle getInitStyle() const { |
1279 | return static_cast<InitializationStyle>(VarDeclBits.InitStyle); |
1280 | } |
1281 | |
1282 | /// Whether the initializer is a direct-initializer (list or call). |
1283 | bool isDirectInit() const { |
1284 | return getInitStyle() != CInit; |
1285 | } |
1286 | |
1287 | /// If this definition should pretend to be a declaration. |
1288 | bool isThisDeclarationADemotedDefinition() const { |
1289 | return isa<ParmVarDecl>(this) ? false : |
1290 | NonParmVarDeclBits.IsThisDeclarationADemotedDefinition; |
1291 | } |
1292 | |
1293 | /// This is a definition which should be demoted to a declaration. |
1294 | /// |
1295 | /// In some cases (mostly module merging) we can end up with two visible |
1296 | /// definitions one of which needs to be demoted to a declaration to keep |
1297 | /// the AST invariants. |
1298 | void demoteThisDefinitionToDeclaration() { |
1299 | assert(isThisDeclarationADefinition() && "Not a definition!")((isThisDeclarationADefinition() && "Not a definition!" ) ? static_cast<void> (0) : __assert_fail ("isThisDeclarationADefinition() && \"Not a definition!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1299, __PRETTY_FUNCTION__)); |
1300 | assert(!isa<ParmVarDecl>(this) && "Cannot demote ParmVarDecls!")((!isa<ParmVarDecl>(this) && "Cannot demote ParmVarDecls!" ) ? static_cast<void> (0) : __assert_fail ("!isa<ParmVarDecl>(this) && \"Cannot demote ParmVarDecls!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1300, __PRETTY_FUNCTION__)); |
1301 | NonParmVarDeclBits.IsThisDeclarationADemotedDefinition = 1; |
1302 | } |
1303 | |
1304 | /// Determine whether this variable is the exception variable in a |
1305 | /// C++ catch statememt or an Objective-C \@catch statement. |
1306 | bool isExceptionVariable() const { |
1307 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.ExceptionVar; |
1308 | } |
1309 | void setExceptionVariable(bool EV) { |
1310 | assert(!isa<ParmVarDecl>(this))((!isa<ParmVarDecl>(this)) ? static_cast<void> (0 ) : __assert_fail ("!isa<ParmVarDecl>(this)", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1310, __PRETTY_FUNCTION__)); |
1311 | NonParmVarDeclBits.ExceptionVar = EV; |
1312 | } |
1313 | |
1314 | /// Determine whether this local variable can be used with the named |
1315 | /// return value optimization (NRVO). |
1316 | /// |
1317 | /// The named return value optimization (NRVO) works by marking certain |
1318 | /// non-volatile local variables of class type as NRVO objects. These |
1319 | /// locals can be allocated within the return slot of their containing |
1320 | /// function, in which case there is no need to copy the object to the |
1321 | /// return slot when returning from the function. Within the function body, |
1322 | /// each return that returns the NRVO object will have this variable as its |
1323 | /// NRVO candidate. |
1324 | bool isNRVOVariable() const { |
1325 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.NRVOVariable; |
1326 | } |
1327 | void setNRVOVariable(bool NRVO) { |
1328 | assert(!isa<ParmVarDecl>(this))((!isa<ParmVarDecl>(this)) ? static_cast<void> (0 ) : __assert_fail ("!isa<ParmVarDecl>(this)", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1328, __PRETTY_FUNCTION__)); |
1329 | NonParmVarDeclBits.NRVOVariable = NRVO; |
1330 | } |
1331 | |
1332 | /// Determine whether this variable is the for-range-declaration in |
1333 | /// a C++0x for-range statement. |
1334 | bool isCXXForRangeDecl() const { |
1335 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.CXXForRangeDecl; |
1336 | } |
1337 | void setCXXForRangeDecl(bool FRD) { |
1338 | assert(!isa<ParmVarDecl>(this))((!isa<ParmVarDecl>(this)) ? static_cast<void> (0 ) : __assert_fail ("!isa<ParmVarDecl>(this)", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1338, __PRETTY_FUNCTION__)); |
1339 | NonParmVarDeclBits.CXXForRangeDecl = FRD; |
1340 | } |
1341 | |
1342 | /// Determine whether this variable is a for-loop declaration for a |
1343 | /// for-in statement in Objective-C. |
1344 | bool isObjCForDecl() const { |
1345 | return NonParmVarDeclBits.ObjCForDecl; |
1346 | } |
1347 | |
1348 | void setObjCForDecl(bool FRD) { |
1349 | NonParmVarDeclBits.ObjCForDecl = FRD; |
1350 | } |
1351 | |
1352 | /// Determine whether this variable is an ARC pseudo-__strong variable. A |
1353 | /// pseudo-__strong variable has a __strong-qualified type but does not |
1354 | /// actually retain the object written into it. Generally such variables are |
1355 | /// also 'const' for safety. There are 3 cases where this will be set, 1) if |
1356 | /// the variable is annotated with the objc_externally_retained attribute, 2) |
1357 | /// if its 'self' in a non-init method, or 3) if its the variable in an for-in |
1358 | /// loop. |
1359 | bool isARCPseudoStrong() const { return VarDeclBits.ARCPseudoStrong; } |
1360 | void setARCPseudoStrong(bool PS) { VarDeclBits.ARCPseudoStrong = PS; } |
1361 | |
1362 | /// Whether this variable is (C++1z) inline. |
1363 | bool isInline() const { |
1364 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsInline; |
1365 | } |
1366 | bool isInlineSpecified() const { |
1367 | return isa<ParmVarDecl>(this) ? false |
1368 | : NonParmVarDeclBits.IsInlineSpecified; |
1369 | } |
1370 | void setInlineSpecified() { |
1371 | assert(!isa<ParmVarDecl>(this))((!isa<ParmVarDecl>(this)) ? static_cast<void> (0 ) : __assert_fail ("!isa<ParmVarDecl>(this)", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1371, __PRETTY_FUNCTION__)); |
1372 | NonParmVarDeclBits.IsInline = true; |
1373 | NonParmVarDeclBits.IsInlineSpecified = true; |
1374 | } |
1375 | void setImplicitlyInline() { |
1376 | assert(!isa<ParmVarDecl>(this))((!isa<ParmVarDecl>(this)) ? static_cast<void> (0 ) : __assert_fail ("!isa<ParmVarDecl>(this)", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1376, __PRETTY_FUNCTION__)); |
1377 | NonParmVarDeclBits.IsInline = true; |
1378 | } |
1379 | |
1380 | /// Whether this variable is (C++11) constexpr. |
1381 | bool isConstexpr() const { |
1382 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsConstexpr; |
1383 | } |
1384 | void setConstexpr(bool IC) { |
1385 | assert(!isa<ParmVarDecl>(this))((!isa<ParmVarDecl>(this)) ? static_cast<void> (0 ) : __assert_fail ("!isa<ParmVarDecl>(this)", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1385, __PRETTY_FUNCTION__)); |
1386 | NonParmVarDeclBits.IsConstexpr = IC; |
1387 | } |
1388 | |
1389 | /// Whether this variable is the implicit variable for a lambda init-capture. |
1390 | bool isInitCapture() const { |
1391 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsInitCapture; |
1392 | } |
1393 | void setInitCapture(bool IC) { |
1394 | assert(!isa<ParmVarDecl>(this))((!isa<ParmVarDecl>(this)) ? static_cast<void> (0 ) : __assert_fail ("!isa<ParmVarDecl>(this)", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1394, __PRETTY_FUNCTION__)); |
1395 | NonParmVarDeclBits.IsInitCapture = IC; |
1396 | } |
1397 | |
1398 | /// Determine whether this variable is actually a function parameter pack or |
1399 | /// init-capture pack. |
1400 | bool isParameterPack() const; |
1401 | |
1402 | /// Whether this local extern variable declaration's previous declaration |
1403 | /// was declared in the same block scope. Only correct in C++. |
1404 | bool isPreviousDeclInSameBlockScope() const { |
1405 | return isa<ParmVarDecl>(this) |
1406 | ? false |
1407 | : NonParmVarDeclBits.PreviousDeclInSameBlockScope; |
1408 | } |
1409 | void setPreviousDeclInSameBlockScope(bool Same) { |
1410 | assert(!isa<ParmVarDecl>(this))((!isa<ParmVarDecl>(this)) ? static_cast<void> (0 ) : __assert_fail ("!isa<ParmVarDecl>(this)", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1410, __PRETTY_FUNCTION__)); |
1411 | NonParmVarDeclBits.PreviousDeclInSameBlockScope = Same; |
1412 | } |
1413 | |
1414 | /// Indicates the capture is a __block variable that is captured by a block |
1415 | /// that can potentially escape (a block for which BlockDecl::doesNotEscape |
1416 | /// returns false). |
1417 | bool isEscapingByref() const; |
1418 | |
1419 | /// Indicates the capture is a __block variable that is never captured by an |
1420 | /// escaping block. |
1421 | bool isNonEscapingByref() const; |
1422 | |
1423 | void setEscapingByref() { |
1424 | NonParmVarDeclBits.EscapingByref = true; |
1425 | } |
1426 | |
1427 | /// Retrieve the variable declaration from which this variable could |
1428 | /// be instantiated, if it is an instantiation (rather than a non-template). |
1429 | VarDecl *getTemplateInstantiationPattern() const; |
1430 | |
1431 | /// If this variable is an instantiated static data member of a |
1432 | /// class template specialization, returns the templated static data member |
1433 | /// from which it was instantiated. |
1434 | VarDecl *getInstantiatedFromStaticDataMember() const; |
1435 | |
1436 | /// If this variable is an instantiation of a variable template or a |
1437 | /// static data member of a class template, determine what kind of |
1438 | /// template specialization or instantiation this is. |
1439 | TemplateSpecializationKind getTemplateSpecializationKind() const; |
1440 | |
1441 | /// Get the template specialization kind of this variable for the purposes of |
1442 | /// template instantiation. This differs from getTemplateSpecializationKind() |
1443 | /// for an instantiation of a class-scope explicit specialization. |
1444 | TemplateSpecializationKind |
1445 | getTemplateSpecializationKindForInstantiation() const; |
1446 | |
1447 | /// If this variable is an instantiation of a variable template or a |
1448 | /// static data member of a class template, determine its point of |
1449 | /// instantiation. |
1450 | SourceLocation getPointOfInstantiation() const; |
1451 | |
1452 | /// If this variable is an instantiation of a static data member of a |
1453 | /// class template specialization, retrieves the member specialization |
1454 | /// information. |
1455 | MemberSpecializationInfo *getMemberSpecializationInfo() const; |
1456 | |
1457 | /// For a static data member that was instantiated from a static |
1458 | /// data member of a class template, set the template specialiation kind. |
1459 | void setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
1460 | SourceLocation PointOfInstantiation = SourceLocation()); |
1461 | |
1462 | /// Specify that this variable is an instantiation of the |
1463 | /// static data member VD. |
1464 | void setInstantiationOfStaticDataMember(VarDecl *VD, |
1465 | TemplateSpecializationKind TSK); |
1466 | |
1467 | /// Retrieves the variable template that is described by this |
1468 | /// variable declaration. |
1469 | /// |
1470 | /// Every variable template is represented as a VarTemplateDecl and a |
1471 | /// VarDecl. The former contains template properties (such as |
1472 | /// the template parameter lists) while the latter contains the |
1473 | /// actual description of the template's |
1474 | /// contents. VarTemplateDecl::getTemplatedDecl() retrieves the |
1475 | /// VarDecl that from a VarTemplateDecl, while |
1476 | /// getDescribedVarTemplate() retrieves the VarTemplateDecl from |
1477 | /// a VarDecl. |
1478 | VarTemplateDecl *getDescribedVarTemplate() const; |
1479 | |
1480 | void setDescribedVarTemplate(VarTemplateDecl *Template); |
1481 | |
1482 | // Is this variable known to have a definition somewhere in the complete |
1483 | // program? This may be true even if the declaration has internal linkage and |
1484 | // has no definition within this source file. |
1485 | bool isKnownToBeDefined() const; |
1486 | |
1487 | /// Do we need to emit an exit-time destructor for this variable? |
1488 | bool isNoDestroy(const ASTContext &) const; |
1489 | |
1490 | // Implement isa/cast/dyncast/etc. |
1491 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
1492 | static bool classofKind(Kind K) { return K >= firstVar && K <= lastVar; } |
1493 | }; |
1494 | |
1495 | class ImplicitParamDecl : public VarDecl { |
1496 | void anchor() override; |
1497 | |
1498 | public: |
1499 | /// Defines the kind of the implicit parameter: is this an implicit parameter |
1500 | /// with pointer to 'this', 'self', '_cmd', virtual table pointers, captured |
1501 | /// context or something else. |
1502 | enum ImplicitParamKind : unsigned { |
1503 | /// Parameter for Objective-C 'self' argument |
1504 | ObjCSelf, |
1505 | |
1506 | /// Parameter for Objective-C '_cmd' argument |
1507 | ObjCCmd, |
1508 | |
1509 | /// Parameter for C++ 'this' argument |
1510 | CXXThis, |
1511 | |
1512 | /// Parameter for C++ virtual table pointers |
1513 | CXXVTT, |
1514 | |
1515 | /// Parameter for captured context |
1516 | CapturedContext, |
1517 | |
1518 | /// Other implicit parameter |
1519 | Other, |
1520 | }; |
1521 | |
1522 | /// Create implicit parameter. |
1523 | static ImplicitParamDecl *Create(ASTContext &C, DeclContext *DC, |
1524 | SourceLocation IdLoc, IdentifierInfo *Id, |
1525 | QualType T, ImplicitParamKind ParamKind); |
1526 | static ImplicitParamDecl *Create(ASTContext &C, QualType T, |
1527 | ImplicitParamKind ParamKind); |
1528 | |
1529 | static ImplicitParamDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
1530 | |
1531 | ImplicitParamDecl(ASTContext &C, DeclContext *DC, SourceLocation IdLoc, |
1532 | IdentifierInfo *Id, QualType Type, |
1533 | ImplicitParamKind ParamKind) |
1534 | : VarDecl(ImplicitParam, C, DC, IdLoc, IdLoc, Id, Type, |
1535 | /*TInfo=*/nullptr, SC_None) { |
1536 | NonParmVarDeclBits.ImplicitParamKind = ParamKind; |
1537 | setImplicit(); |
1538 | } |
1539 | |
1540 | ImplicitParamDecl(ASTContext &C, QualType Type, ImplicitParamKind ParamKind) |
1541 | : VarDecl(ImplicitParam, C, /*DC=*/nullptr, SourceLocation(), |
1542 | SourceLocation(), /*Id=*/nullptr, Type, |
1543 | /*TInfo=*/nullptr, SC_None) { |
1544 | NonParmVarDeclBits.ImplicitParamKind = ParamKind; |
1545 | setImplicit(); |
1546 | } |
1547 | |
1548 | /// Returns the implicit parameter kind. |
1549 | ImplicitParamKind getParameterKind() const { |
1550 | return static_cast<ImplicitParamKind>(NonParmVarDeclBits.ImplicitParamKind); |
1551 | } |
1552 | |
1553 | // Implement isa/cast/dyncast/etc. |
1554 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
1555 | static bool classofKind(Kind K) { return K == ImplicitParam; } |
1556 | }; |
1557 | |
1558 | /// Represents a parameter to a function. |
1559 | class ParmVarDecl : public VarDecl { |
1560 | public: |
1561 | enum { MaxFunctionScopeDepth = 255 }; |
1562 | enum { MaxFunctionScopeIndex = 255 }; |
1563 | |
1564 | protected: |
1565 | ParmVarDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
1566 | SourceLocation IdLoc, IdentifierInfo *Id, QualType T, |
1567 | TypeSourceInfo *TInfo, StorageClass S, Expr *DefArg) |
1568 | : VarDecl(DK, C, DC, StartLoc, IdLoc, Id, T, TInfo, S) { |
1569 | assert(ParmVarDeclBits.HasInheritedDefaultArg == false)((ParmVarDeclBits.HasInheritedDefaultArg == false) ? static_cast <void> (0) : __assert_fail ("ParmVarDeclBits.HasInheritedDefaultArg == false" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1569, __PRETTY_FUNCTION__)); |
1570 | assert(ParmVarDeclBits.DefaultArgKind == DAK_None)((ParmVarDeclBits.DefaultArgKind == DAK_None) ? static_cast< void> (0) : __assert_fail ("ParmVarDeclBits.DefaultArgKind == DAK_None" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1570, __PRETTY_FUNCTION__)); |
1571 | assert(ParmVarDeclBits.IsKNRPromoted == false)((ParmVarDeclBits.IsKNRPromoted == false) ? static_cast<void > (0) : __assert_fail ("ParmVarDeclBits.IsKNRPromoted == false" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1571, __PRETTY_FUNCTION__)); |
1572 | assert(ParmVarDeclBits.IsObjCMethodParam == false)((ParmVarDeclBits.IsObjCMethodParam == false) ? static_cast< void> (0) : __assert_fail ("ParmVarDeclBits.IsObjCMethodParam == false" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1572, __PRETTY_FUNCTION__)); |
1573 | setDefaultArg(DefArg); |
1574 | } |
1575 | |
1576 | public: |
1577 | static ParmVarDecl *Create(ASTContext &C, DeclContext *DC, |
1578 | SourceLocation StartLoc, |
1579 | SourceLocation IdLoc, IdentifierInfo *Id, |
1580 | QualType T, TypeSourceInfo *TInfo, |
1581 | StorageClass S, Expr *DefArg); |
1582 | |
1583 | static ParmVarDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
1584 | |
1585 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
1586 | |
1587 | void setObjCMethodScopeInfo(unsigned parameterIndex) { |
1588 | ParmVarDeclBits.IsObjCMethodParam = true; |
1589 | setParameterIndex(parameterIndex); |
1590 | } |
1591 | |
1592 | void setScopeInfo(unsigned scopeDepth, unsigned parameterIndex) { |
1593 | assert(!ParmVarDeclBits.IsObjCMethodParam)((!ParmVarDeclBits.IsObjCMethodParam) ? static_cast<void> (0) : __assert_fail ("!ParmVarDeclBits.IsObjCMethodParam", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1593, __PRETTY_FUNCTION__)); |
1594 | |
1595 | ParmVarDeclBits.ScopeDepthOrObjCQuals = scopeDepth; |
1596 | assert(ParmVarDeclBits.ScopeDepthOrObjCQuals == scopeDepth((ParmVarDeclBits.ScopeDepthOrObjCQuals == scopeDepth && "truncation!") ? static_cast<void> (0) : __assert_fail ("ParmVarDeclBits.ScopeDepthOrObjCQuals == scopeDepth && \"truncation!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1597, __PRETTY_FUNCTION__)) |
1597 | && "truncation!")((ParmVarDeclBits.ScopeDepthOrObjCQuals == scopeDepth && "truncation!") ? static_cast<void> (0) : __assert_fail ("ParmVarDeclBits.ScopeDepthOrObjCQuals == scopeDepth && \"truncation!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1597, __PRETTY_FUNCTION__)); |
1598 | |
1599 | setParameterIndex(parameterIndex); |
1600 | } |
1601 | |
1602 | bool isObjCMethodParameter() const { |
1603 | return ParmVarDeclBits.IsObjCMethodParam; |
1604 | } |
1605 | |
1606 | unsigned getFunctionScopeDepth() const { |
1607 | if (ParmVarDeclBits.IsObjCMethodParam) return 0; |
1608 | return ParmVarDeclBits.ScopeDepthOrObjCQuals; |
1609 | } |
1610 | |
1611 | /// Returns the index of this parameter in its prototype or method scope. |
1612 | unsigned getFunctionScopeIndex() const { |
1613 | return getParameterIndex(); |
1614 | } |
1615 | |
1616 | ObjCDeclQualifier getObjCDeclQualifier() const { |
1617 | if (!ParmVarDeclBits.IsObjCMethodParam) return OBJC_TQ_None; |
1618 | return ObjCDeclQualifier(ParmVarDeclBits.ScopeDepthOrObjCQuals); |
1619 | } |
1620 | void setObjCDeclQualifier(ObjCDeclQualifier QTVal) { |
1621 | assert(ParmVarDeclBits.IsObjCMethodParam)((ParmVarDeclBits.IsObjCMethodParam) ? static_cast<void> (0) : __assert_fail ("ParmVarDeclBits.IsObjCMethodParam", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1621, __PRETTY_FUNCTION__)); |
1622 | ParmVarDeclBits.ScopeDepthOrObjCQuals = QTVal; |
1623 | } |
1624 | |
1625 | /// True if the value passed to this parameter must undergo |
1626 | /// K&R-style default argument promotion: |
1627 | /// |
1628 | /// C99 6.5.2.2. |
1629 | /// If the expression that denotes the called function has a type |
1630 | /// that does not include a prototype, the integer promotions are |
1631 | /// performed on each argument, and arguments that have type float |
1632 | /// are promoted to double. |
1633 | bool isKNRPromoted() const { |
1634 | return ParmVarDeclBits.IsKNRPromoted; |
1635 | } |
1636 | void setKNRPromoted(bool promoted) { |
1637 | ParmVarDeclBits.IsKNRPromoted = promoted; |
1638 | } |
1639 | |
1640 | Expr *getDefaultArg(); |
1641 | const Expr *getDefaultArg() const { |
1642 | return const_cast<ParmVarDecl *>(this)->getDefaultArg(); |
1643 | } |
1644 | |
1645 | void setDefaultArg(Expr *defarg); |
1646 | |
1647 | /// Retrieve the source range that covers the entire default |
1648 | /// argument. |
1649 | SourceRange getDefaultArgRange() const; |
1650 | void setUninstantiatedDefaultArg(Expr *arg); |
1651 | Expr *getUninstantiatedDefaultArg(); |
1652 | const Expr *getUninstantiatedDefaultArg() const { |
1653 | return const_cast<ParmVarDecl *>(this)->getUninstantiatedDefaultArg(); |
1654 | } |
1655 | |
1656 | /// Determines whether this parameter has a default argument, |
1657 | /// either parsed or not. |
1658 | bool hasDefaultArg() const; |
1659 | |
1660 | /// Determines whether this parameter has a default argument that has not |
1661 | /// yet been parsed. This will occur during the processing of a C++ class |
1662 | /// whose member functions have default arguments, e.g., |
1663 | /// @code |
1664 | /// class X { |
1665 | /// public: |
1666 | /// void f(int x = 17); // x has an unparsed default argument now |
1667 | /// }; // x has a regular default argument now |
1668 | /// @endcode |
1669 | bool hasUnparsedDefaultArg() const { |
1670 | return ParmVarDeclBits.DefaultArgKind == DAK_Unparsed; |
1671 | } |
1672 | |
1673 | bool hasUninstantiatedDefaultArg() const { |
1674 | return ParmVarDeclBits.DefaultArgKind == DAK_Uninstantiated; |
1675 | } |
1676 | |
1677 | /// Specify that this parameter has an unparsed default argument. |
1678 | /// The argument will be replaced with a real default argument via |
1679 | /// setDefaultArg when the class definition enclosing the function |
1680 | /// declaration that owns this default argument is completed. |
1681 | void setUnparsedDefaultArg() { |
1682 | ParmVarDeclBits.DefaultArgKind = DAK_Unparsed; |
1683 | } |
1684 | |
1685 | bool hasInheritedDefaultArg() const { |
1686 | return ParmVarDeclBits.HasInheritedDefaultArg; |
1687 | } |
1688 | |
1689 | void setHasInheritedDefaultArg(bool I = true) { |
1690 | ParmVarDeclBits.HasInheritedDefaultArg = I; |
1691 | } |
1692 | |
1693 | QualType getOriginalType() const; |
1694 | |
1695 | /// Sets the function declaration that owns this |
1696 | /// ParmVarDecl. Since ParmVarDecls are often created before the |
1697 | /// FunctionDecls that own them, this routine is required to update |
1698 | /// the DeclContext appropriately. |
1699 | void setOwningFunction(DeclContext *FD) { setDeclContext(FD); } |
1700 | |
1701 | // Implement isa/cast/dyncast/etc. |
1702 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
1703 | static bool classofKind(Kind K) { return K == ParmVar; } |
1704 | |
1705 | private: |
1706 | enum { ParameterIndexSentinel = (1 << NumParameterIndexBits) - 1 }; |
1707 | |
1708 | void setParameterIndex(unsigned parameterIndex) { |
1709 | if (parameterIndex >= ParameterIndexSentinel) { |
1710 | setParameterIndexLarge(parameterIndex); |
1711 | return; |
1712 | } |
1713 | |
1714 | ParmVarDeclBits.ParameterIndex = parameterIndex; |
1715 | assert(ParmVarDeclBits.ParameterIndex == parameterIndex && "truncation!")((ParmVarDeclBits.ParameterIndex == parameterIndex && "truncation!") ? static_cast<void> (0) : __assert_fail ("ParmVarDeclBits.ParameterIndex == parameterIndex && \"truncation!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 1715, __PRETTY_FUNCTION__)); |
1716 | } |
1717 | unsigned getParameterIndex() const { |
1718 | unsigned d = ParmVarDeclBits.ParameterIndex; |
1719 | return d == ParameterIndexSentinel ? getParameterIndexLarge() : d; |
1720 | } |
1721 | |
1722 | void setParameterIndexLarge(unsigned parameterIndex); |
1723 | unsigned getParameterIndexLarge() const; |
1724 | }; |
1725 | |
1726 | enum class MultiVersionKind { |
1727 | None, |
1728 | Target, |
1729 | CPUSpecific, |
1730 | CPUDispatch |
1731 | }; |
1732 | |
1733 | /// Represents a function declaration or definition. |
1734 | /// |
1735 | /// Since a given function can be declared several times in a program, |
1736 | /// there may be several FunctionDecls that correspond to that |
1737 | /// function. Only one of those FunctionDecls will be found when |
1738 | /// traversing the list of declarations in the context of the |
1739 | /// FunctionDecl (e.g., the translation unit); this FunctionDecl |
1740 | /// contains all of the information known about the function. Other, |
1741 | /// previous declarations of the function are available via the |
1742 | /// getPreviousDecl() chain. |
1743 | class FunctionDecl : public DeclaratorDecl, |
1744 | public DeclContext, |
1745 | public Redeclarable<FunctionDecl> { |
1746 | // This class stores some data in DeclContext::FunctionDeclBits |
1747 | // to save some space. Use the provided accessors to access it. |
1748 | public: |
1749 | /// The kind of templated function a FunctionDecl can be. |
1750 | enum TemplatedKind { |
1751 | // Not templated. |
1752 | TK_NonTemplate, |
1753 | // The pattern in a function template declaration. |
1754 | TK_FunctionTemplate, |
1755 | // A non-template function that is an instantiation or explicit |
1756 | // specialization of a member of a templated class. |
1757 | TK_MemberSpecialization, |
1758 | // An instantiation or explicit specialization of a function template. |
1759 | // Note: this might have been instantiated from a templated class if it |
1760 | // is a class-scope explicit specialization. |
1761 | TK_FunctionTemplateSpecialization, |
1762 | // A function template specialization that hasn't yet been resolved to a |
1763 | // particular specialized function template. |
1764 | TK_DependentFunctionTemplateSpecialization |
1765 | }; |
1766 | |
1767 | private: |
1768 | /// A new[]'d array of pointers to VarDecls for the formal |
1769 | /// parameters of this function. This is null if a prototype or if there are |
1770 | /// no formals. |
1771 | ParmVarDecl **ParamInfo = nullptr; |
1772 | |
1773 | LazyDeclStmtPtr Body; |
1774 | |
1775 | unsigned ODRHash; |
1776 | |
1777 | /// End part of this FunctionDecl's source range. |
1778 | /// |
1779 | /// We could compute the full range in getSourceRange(). However, when we're |
1780 | /// dealing with a function definition deserialized from a PCH/AST file, |
1781 | /// we can only compute the full range once the function body has been |
1782 | /// de-serialized, so it's far better to have the (sometimes-redundant) |
1783 | /// EndRangeLoc. |
1784 | SourceLocation EndRangeLoc; |
1785 | |
1786 | /// The template or declaration that this declaration |
1787 | /// describes or was instantiated from, respectively. |
1788 | /// |
1789 | /// For non-templates, this value will be NULL. For function |
1790 | /// declarations that describe a function template, this will be a |
1791 | /// pointer to a FunctionTemplateDecl. For member functions |
1792 | /// of class template specializations, this will be a MemberSpecializationInfo |
1793 | /// pointer containing information about the specialization. |
1794 | /// For function template specializations, this will be a |
1795 | /// FunctionTemplateSpecializationInfo, which contains information about |
1796 | /// the template being specialized and the template arguments involved in |
1797 | /// that specialization. |
1798 | llvm::PointerUnion4<FunctionTemplateDecl *, |
1799 | MemberSpecializationInfo *, |
1800 | FunctionTemplateSpecializationInfo *, |
1801 | DependentFunctionTemplateSpecializationInfo *> |
1802 | TemplateOrSpecialization; |
1803 | |
1804 | /// Provides source/type location info for the declaration name embedded in |
1805 | /// the DeclaratorDecl base class. |
1806 | DeclarationNameLoc DNLoc; |
1807 | |
1808 | /// Specify that this function declaration is actually a function |
1809 | /// template specialization. |
1810 | /// |
1811 | /// \param C the ASTContext. |
1812 | /// |
1813 | /// \param Template the function template that this function template |
1814 | /// specialization specializes. |
1815 | /// |
1816 | /// \param TemplateArgs the template arguments that produced this |
1817 | /// function template specialization from the template. |
1818 | /// |
1819 | /// \param InsertPos If non-NULL, the position in the function template |
1820 | /// specialization set where the function template specialization data will |
1821 | /// be inserted. |
1822 | /// |
1823 | /// \param TSK the kind of template specialization this is. |
1824 | /// |
1825 | /// \param TemplateArgsAsWritten location info of template arguments. |
1826 | /// |
1827 | /// \param PointOfInstantiation point at which the function template |
1828 | /// specialization was first instantiated. |
1829 | void setFunctionTemplateSpecialization(ASTContext &C, |
1830 | FunctionTemplateDecl *Template, |
1831 | const TemplateArgumentList *TemplateArgs, |
1832 | void *InsertPos, |
1833 | TemplateSpecializationKind TSK, |
1834 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
1835 | SourceLocation PointOfInstantiation); |
1836 | |
1837 | /// Specify that this record is an instantiation of the |
1838 | /// member function FD. |
1839 | void setInstantiationOfMemberFunction(ASTContext &C, FunctionDecl *FD, |
1840 | TemplateSpecializationKind TSK); |
1841 | |
1842 | void setParams(ASTContext &C, ArrayRef<ParmVarDecl *> NewParamInfo); |
1843 | |
1844 | // This is unfortunately needed because ASTDeclWriter::VisitFunctionDecl |
1845 | // need to access this bit but we want to avoid making ASTDeclWriter |
1846 | // a friend of FunctionDeclBitfields just for this. |
1847 | bool isDeletedBit() const { return FunctionDeclBits.IsDeleted; } |
1848 | |
1849 | /// Whether an ODRHash has been stored. |
1850 | bool hasODRHash() const { return FunctionDeclBits.HasODRHash; } |
1851 | |
1852 | /// State that an ODRHash has been stored. |
1853 | void setHasODRHash(bool B = true) { FunctionDeclBits.HasODRHash = B; } |
1854 | |
1855 | protected: |
1856 | FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
1857 | const DeclarationNameInfo &NameInfo, QualType T, |
1858 | TypeSourceInfo *TInfo, StorageClass S, bool isInlineSpecified, |
1859 | bool isConstexprSpecified); |
1860 | |
1861 | using redeclarable_base = Redeclarable<FunctionDecl>; |
1862 | |
1863 | FunctionDecl *getNextRedeclarationImpl() override { |
1864 | return getNextRedeclaration(); |
1865 | } |
1866 | |
1867 | FunctionDecl *getPreviousDeclImpl() override { |
1868 | return getPreviousDecl(); |
1869 | } |
1870 | |
1871 | FunctionDecl *getMostRecentDeclImpl() override { |
1872 | return getMostRecentDecl(); |
1873 | } |
1874 | |
1875 | public: |
1876 | friend class ASTDeclReader; |
1877 | friend class ASTDeclWriter; |
1878 | |
1879 | using redecl_range = redeclarable_base::redecl_range; |
1880 | using redecl_iterator = redeclarable_base::redecl_iterator; |
1881 | |
1882 | using redeclarable_base::redecls_begin; |
1883 | using redeclarable_base::redecls_end; |
1884 | using redeclarable_base::redecls; |
1885 | using redeclarable_base::getPreviousDecl; |
1886 | using redeclarable_base::getMostRecentDecl; |
1887 | using redeclarable_base::isFirstDecl; |
1888 | |
1889 | static FunctionDecl *Create(ASTContext &C, DeclContext *DC, |
1890 | SourceLocation StartLoc, SourceLocation NLoc, |
1891 | DeclarationName N, QualType T, |
1892 | TypeSourceInfo *TInfo, |
1893 | StorageClass SC, |
1894 | bool isInlineSpecified = false, |
1895 | bool hasWrittenPrototype = true, |
1896 | bool isConstexprSpecified = false) { |
1897 | DeclarationNameInfo NameInfo(N, NLoc); |
1898 | return FunctionDecl::Create(C, DC, StartLoc, NameInfo, T, TInfo, |
1899 | SC, |
1900 | isInlineSpecified, hasWrittenPrototype, |
1901 | isConstexprSpecified); |
1902 | } |
1903 | |
1904 | static FunctionDecl *Create(ASTContext &C, DeclContext *DC, |
1905 | SourceLocation StartLoc, |
1906 | const DeclarationNameInfo &NameInfo, |
1907 | QualType T, TypeSourceInfo *TInfo, |
1908 | StorageClass SC, |
1909 | bool isInlineSpecified, |
1910 | bool hasWrittenPrototype, |
1911 | bool isConstexprSpecified = false); |
1912 | |
1913 | static FunctionDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
1914 | |
1915 | DeclarationNameInfo getNameInfo() const { |
1916 | return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc); |
1917 | } |
1918 | |
1919 | void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy, |
1920 | bool Qualified) const override; |
1921 | |
1922 | void setRangeEnd(SourceLocation E) { EndRangeLoc = E; } |
1923 | |
1924 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
1925 | |
1926 | // Function definitions. |
1927 | // |
1928 | // A function declaration may be: |
1929 | // - a non defining declaration, |
1930 | // - a definition. A function may be defined because: |
1931 | // - it has a body, or will have it in the case of late parsing. |
1932 | // - it has an uninstantiated body. The body does not exist because the |
1933 | // function is not used yet, but the declaration is considered a |
1934 | // definition and does not allow other definition of this function. |
1935 | // - it does not have a user specified body, but it does not allow |
1936 | // redefinition, because it is deleted/defaulted or is defined through |
1937 | // some other mechanism (alias, ifunc). |
1938 | |
1939 | /// Returns true if the function has a body. |
1940 | /// |
1941 | /// The function body might be in any of the (re-)declarations of this |
1942 | /// function. The variant that accepts a FunctionDecl pointer will set that |
1943 | /// function declaration to the actual declaration containing the body (if |
1944 | /// there is one). |
1945 | bool hasBody(const FunctionDecl *&Definition) const; |
1946 | |
1947 | bool hasBody() const override { |
1948 | const FunctionDecl* Definition; |
1949 | return hasBody(Definition); |
1950 | } |
1951 | |
1952 | /// Returns whether the function has a trivial body that does not require any |
1953 | /// specific codegen. |
1954 | bool hasTrivialBody() const; |
1955 | |
1956 | /// Returns true if the function has a definition that does not need to be |
1957 | /// instantiated. |
1958 | /// |
1959 | /// The variant that accepts a FunctionDecl pointer will set that function |
1960 | /// declaration to the declaration that is a definition (if there is one). |
1961 | bool isDefined(const FunctionDecl *&Definition) const; |
1962 | |
1963 | virtual bool isDefined() const { |
1964 | const FunctionDecl* Definition; |
1965 | return isDefined(Definition); |
1966 | } |
1967 | |
1968 | /// Get the definition for this declaration. |
1969 | FunctionDecl *getDefinition() { |
1970 | const FunctionDecl *Definition; |
1971 | if (isDefined(Definition)) |
1972 | return const_cast<FunctionDecl *>(Definition); |
1973 | return nullptr; |
1974 | } |
1975 | const FunctionDecl *getDefinition() const { |
1976 | return const_cast<FunctionDecl *>(this)->getDefinition(); |
1977 | } |
1978 | |
1979 | /// Retrieve the body (definition) of the function. The function body might be |
1980 | /// in any of the (re-)declarations of this function. The variant that accepts |
1981 | /// a FunctionDecl pointer will set that function declaration to the actual |
1982 | /// declaration containing the body (if there is one). |
1983 | /// NOTE: For checking if there is a body, use hasBody() instead, to avoid |
1984 | /// unnecessary AST de-serialization of the body. |
1985 | Stmt *getBody(const FunctionDecl *&Definition) const; |
1986 | |
1987 | Stmt *getBody() const override { |
1988 | const FunctionDecl* Definition; |
1989 | return getBody(Definition); |
1990 | } |
1991 | |
1992 | /// Returns whether this specific declaration of the function is also a |
1993 | /// definition that does not contain uninstantiated body. |
1994 | /// |
1995 | /// This does not determine whether the function has been defined (e.g., in a |
1996 | /// previous definition); for that information, use isDefined. |
1997 | bool isThisDeclarationADefinition() const { |
1998 | return isDeletedAsWritten() || isDefaulted() || Body || hasSkippedBody() || |
1999 | isLateTemplateParsed() || willHaveBody() || hasDefiningAttr(); |
2000 | } |
2001 | |
2002 | /// Returns whether this specific declaration of the function has a body. |
2003 | bool doesThisDeclarationHaveABody() const { |
2004 | return Body || isLateTemplateParsed(); |
2005 | } |
2006 | |
2007 | void setBody(Stmt *B); |
2008 | void setLazyBody(uint64_t Offset) { Body = Offset; } |
2009 | |
2010 | /// Whether this function is variadic. |
2011 | bool isVariadic() const; |
2012 | |
2013 | /// Whether this function is marked as virtual explicitly. |
2014 | bool isVirtualAsWritten() const { |
2015 | return FunctionDeclBits.IsVirtualAsWritten; |
2016 | } |
2017 | |
2018 | /// State that this function is marked as virtual explicitly. |
2019 | void setVirtualAsWritten(bool V) { FunctionDeclBits.IsVirtualAsWritten = V; } |
2020 | |
2021 | /// Whether this virtual function is pure, i.e. makes the containing class |
2022 | /// abstract. |
2023 | bool isPure() const { return FunctionDeclBits.IsPure; } |
2024 | void setPure(bool P = true); |
2025 | |
2026 | /// Whether this templated function will be late parsed. |
2027 | bool isLateTemplateParsed() const { |
2028 | return FunctionDeclBits.IsLateTemplateParsed; |
2029 | } |
2030 | |
2031 | /// State that this templated function will be late parsed. |
2032 | void setLateTemplateParsed(bool ILT = true) { |
2033 | FunctionDeclBits.IsLateTemplateParsed = ILT; |
2034 | } |
2035 | |
2036 | /// Whether this function is "trivial" in some specialized C++ senses. |
2037 | /// Can only be true for default constructors, copy constructors, |
2038 | /// copy assignment operators, and destructors. Not meaningful until |
2039 | /// the class has been fully built by Sema. |
2040 | bool isTrivial() const { return FunctionDeclBits.IsTrivial; } |
2041 | void setTrivial(bool IT) { FunctionDeclBits.IsTrivial = IT; } |
2042 | |
2043 | bool isTrivialForCall() const { return FunctionDeclBits.IsTrivialForCall; } |
2044 | void setTrivialForCall(bool IT) { FunctionDeclBits.IsTrivialForCall = IT; } |
2045 | |
2046 | /// Whether this function is defaulted per C++0x. Only valid for |
2047 | /// special member functions. |
2048 | bool isDefaulted() const { return FunctionDeclBits.IsDefaulted; } |
2049 | void setDefaulted(bool D = true) { FunctionDeclBits.IsDefaulted = D; } |
2050 | |
2051 | /// Whether this function is explicitly defaulted per C++0x. Only valid |
2052 | /// for special member functions. |
2053 | bool isExplicitlyDefaulted() const { |
2054 | return FunctionDeclBits.IsExplicitlyDefaulted; |
2055 | } |
2056 | |
2057 | /// State that this function is explicitly defaulted per C++0x. Only valid |
2058 | /// for special member functions. |
2059 | void setExplicitlyDefaulted(bool ED = true) { |
2060 | FunctionDeclBits.IsExplicitlyDefaulted = ED; |
2061 | } |
2062 | |
2063 | /// Whether falling off this function implicitly returns null/zero. |
2064 | /// If a more specific implicit return value is required, front-ends |
2065 | /// should synthesize the appropriate return statements. |
2066 | bool hasImplicitReturnZero() const { |
2067 | return FunctionDeclBits.HasImplicitReturnZero; |
2068 | } |
2069 | |
2070 | /// State that falling off this function implicitly returns null/zero. |
2071 | /// If a more specific implicit return value is required, front-ends |
2072 | /// should synthesize the appropriate return statements. |
2073 | void setHasImplicitReturnZero(bool IRZ) { |
2074 | FunctionDeclBits.HasImplicitReturnZero = IRZ; |
2075 | } |
2076 | |
2077 | /// Whether this function has a prototype, either because one |
2078 | /// was explicitly written or because it was "inherited" by merging |
2079 | /// a declaration without a prototype with a declaration that has a |
2080 | /// prototype. |
2081 | bool hasPrototype() const { |
2082 | return hasWrittenPrototype() || hasInheritedPrototype(); |
2083 | } |
2084 | |
2085 | /// Whether this function has a written prototype. |
2086 | bool hasWrittenPrototype() const { |
2087 | return FunctionDeclBits.HasWrittenPrototype; |
2088 | } |
2089 | |
2090 | /// State that this function has a written prototype. |
2091 | void setHasWrittenPrototype(bool P = true) { |
2092 | FunctionDeclBits.HasWrittenPrototype = P; |
2093 | } |
2094 | |
2095 | /// Whether this function inherited its prototype from a |
2096 | /// previous declaration. |
2097 | bool hasInheritedPrototype() const { |
2098 | return FunctionDeclBits.HasInheritedPrototype; |
2099 | } |
2100 | |
2101 | /// State that this function inherited its prototype from a |
2102 | /// previous declaration. |
2103 | void setHasInheritedPrototype(bool P = true) { |
2104 | FunctionDeclBits.HasInheritedPrototype = P; |
2105 | } |
2106 | |
2107 | /// Whether this is a (C++11) constexpr function or constexpr constructor. |
2108 | bool isConstexpr() const { return FunctionDeclBits.IsConstexpr; } |
2109 | void setConstexpr(bool IC) { FunctionDeclBits.IsConstexpr = IC; } |
2110 | |
2111 | /// Whether the instantiation of this function is pending. |
2112 | /// This bit is set when the decision to instantiate this function is made |
2113 | /// and unset if and when the function body is created. That leaves out |
2114 | /// cases where instantiation did not happen because the template definition |
2115 | /// was not seen in this TU. This bit remains set in those cases, under the |
2116 | /// assumption that the instantiation will happen in some other TU. |
2117 | bool instantiationIsPending() const { |
2118 | return FunctionDeclBits.InstantiationIsPending; |
2119 | } |
2120 | |
2121 | /// State that the instantiation of this function is pending. |
2122 | /// (see instantiationIsPending) |
2123 | void setInstantiationIsPending(bool IC) { |
2124 | FunctionDeclBits.InstantiationIsPending = IC; |
2125 | } |
2126 | |
2127 | /// Indicates the function uses __try. |
2128 | bool usesSEHTry() const { return FunctionDeclBits.UsesSEHTry; } |
2129 | void setUsesSEHTry(bool UST) { FunctionDeclBits.UsesSEHTry = UST; } |
2130 | |
2131 | /// Whether this function has been deleted. |
2132 | /// |
2133 | /// A function that is "deleted" (via the C++0x "= delete" syntax) |
2134 | /// acts like a normal function, except that it cannot actually be |
2135 | /// called or have its address taken. Deleted functions are |
2136 | /// typically used in C++ overload resolution to attract arguments |
2137 | /// whose type or lvalue/rvalue-ness would permit the use of a |
2138 | /// different overload that would behave incorrectly. For example, |
2139 | /// one might use deleted functions to ban implicit conversion from |
2140 | /// a floating-point number to an Integer type: |
2141 | /// |
2142 | /// @code |
2143 | /// struct Integer { |
2144 | /// Integer(long); // construct from a long |
2145 | /// Integer(double) = delete; // no construction from float or double |
2146 | /// Integer(long double) = delete; // no construction from long double |
2147 | /// }; |
2148 | /// @endcode |
2149 | // If a function is deleted, its first declaration must be. |
2150 | bool isDeleted() const { |
2151 | return getCanonicalDecl()->FunctionDeclBits.IsDeleted; |
2152 | } |
2153 | |
2154 | bool isDeletedAsWritten() const { |
2155 | return FunctionDeclBits.IsDeleted && !isDefaulted(); |
2156 | } |
2157 | |
2158 | void setDeletedAsWritten(bool D = true) { FunctionDeclBits.IsDeleted = D; } |
2159 | |
2160 | /// Determines whether this function is "main", which is the |
2161 | /// entry point into an executable program. |
2162 | bool isMain() const; |
2163 | |
2164 | /// Determines whether this function is a MSVCRT user defined entry |
2165 | /// point. |
2166 | bool isMSVCRTEntryPoint() const; |
2167 | |
2168 | /// Determines whether this operator new or delete is one |
2169 | /// of the reserved global placement operators: |
2170 | /// void *operator new(size_t, void *); |
2171 | /// void *operator new[](size_t, void *); |
2172 | /// void operator delete(void *, void *); |
2173 | /// void operator delete[](void *, void *); |
2174 | /// These functions have special behavior under [new.delete.placement]: |
2175 | /// These functions are reserved, a C++ program may not define |
2176 | /// functions that displace the versions in the Standard C++ library. |
2177 | /// The provisions of [basic.stc.dynamic] do not apply to these |
2178 | /// reserved placement forms of operator new and operator delete. |
2179 | /// |
2180 | /// This function must be an allocation or deallocation function. |
2181 | bool isReservedGlobalPlacementOperator() const; |
2182 | |
2183 | /// Determines whether this function is one of the replaceable |
2184 | /// global allocation functions: |
2185 | /// void *operator new(size_t); |
2186 | /// void *operator new(size_t, const std::nothrow_t &) noexcept; |
2187 | /// void *operator new[](size_t); |
2188 | /// void *operator new[](size_t, const std::nothrow_t &) noexcept; |
2189 | /// void operator delete(void *) noexcept; |
2190 | /// void operator delete(void *, std::size_t) noexcept; [C++1y] |
2191 | /// void operator delete(void *, const std::nothrow_t &) noexcept; |
2192 | /// void operator delete[](void *) noexcept; |
2193 | /// void operator delete[](void *, std::size_t) noexcept; [C++1y] |
2194 | /// void operator delete[](void *, const std::nothrow_t &) noexcept; |
2195 | /// These functions have special behavior under C++1y [expr.new]: |
2196 | /// An implementation is allowed to omit a call to a replaceable global |
2197 | /// allocation function. [...] |
2198 | /// |
2199 | /// If this function is an aligned allocation/deallocation function, return |
2200 | /// true through IsAligned. |
2201 | bool isReplaceableGlobalAllocationFunction(bool *IsAligned = nullptr) const; |
2202 | |
2203 | /// Determine whether this is a destroying operator delete. |
2204 | bool isDestroyingOperatorDelete() const; |
2205 | |
2206 | /// Compute the language linkage. |
2207 | LanguageLinkage getLanguageLinkage() const; |
2208 | |
2209 | /// Determines whether this function is a function with |
2210 | /// external, C linkage. |
2211 | bool isExternC() const; |
2212 | |
2213 | /// Determines whether this function's context is, or is nested within, |
2214 | /// a C++ extern "C" linkage spec. |
2215 | bool isInExternCContext() const; |
2216 | |
2217 | /// Determines whether this function's context is, or is nested within, |
2218 | /// a C++ extern "C++" linkage spec. |
2219 | bool isInExternCXXContext() const; |
2220 | |
2221 | /// Determines whether this is a global function. |
2222 | bool isGlobal() const; |
2223 | |
2224 | /// Determines whether this function is known to be 'noreturn', through |
2225 | /// an attribute on its declaration or its type. |
2226 | bool isNoReturn() const; |
2227 | |
2228 | /// True if the function was a definition but its body was skipped. |
2229 | bool hasSkippedBody() const { return FunctionDeclBits.HasSkippedBody; } |
2230 | void setHasSkippedBody(bool Skipped = true) { |
2231 | FunctionDeclBits.HasSkippedBody = Skipped; |
2232 | } |
2233 | |
2234 | /// True if this function will eventually have a body, once it's fully parsed. |
2235 | bool willHaveBody() const { return FunctionDeclBits.WillHaveBody; } |
2236 | void setWillHaveBody(bool V = true) { FunctionDeclBits.WillHaveBody = V; } |
2237 | |
2238 | /// True if this function is considered a multiversioned function. |
2239 | bool isMultiVersion() const { |
2240 | return getCanonicalDecl()->FunctionDeclBits.IsMultiVersion; |
2241 | } |
2242 | |
2243 | /// Sets the multiversion state for this declaration and all of its |
2244 | /// redeclarations. |
2245 | void setIsMultiVersion(bool V = true) { |
2246 | getCanonicalDecl()->FunctionDeclBits.IsMultiVersion = V; |
2247 | } |
2248 | |
2249 | /// Gets the kind of multiversioning attribute this declaration has. Note that |
2250 | /// this can return a value even if the function is not multiversion, such as |
2251 | /// the case of 'target'. |
2252 | MultiVersionKind getMultiVersionKind() const; |
2253 | |
2254 | |
2255 | /// True if this function is a multiversioned dispatch function as a part of |
2256 | /// the cpu_specific/cpu_dispatch functionality. |
2257 | bool isCPUDispatchMultiVersion() const; |
2258 | /// True if this function is a multiversioned processor specific function as a |
2259 | /// part of the cpu_specific/cpu_dispatch functionality. |
2260 | bool isCPUSpecificMultiVersion() const; |
2261 | |
2262 | /// True if this function is a multiversioned dispatch function as a part of |
2263 | /// the target functionality. |
2264 | bool isTargetMultiVersion() const; |
2265 | |
2266 | void setPreviousDeclaration(FunctionDecl * PrevDecl); |
2267 | |
2268 | FunctionDecl *getCanonicalDecl() override; |
2269 | const FunctionDecl *getCanonicalDecl() const { |
2270 | return const_cast<FunctionDecl*>(this)->getCanonicalDecl(); |
2271 | } |
2272 | |
2273 | unsigned getBuiltinID(bool ConsiderWrapperFunctions = false) const; |
2274 | |
2275 | // ArrayRef interface to parameters. |
2276 | ArrayRef<ParmVarDecl *> parameters() const { |
2277 | return {ParamInfo, getNumParams()}; |
2278 | } |
2279 | MutableArrayRef<ParmVarDecl *> parameters() { |
2280 | return {ParamInfo, getNumParams()}; |
2281 | } |
2282 | |
2283 | // Iterator access to formal parameters. |
2284 | using param_iterator = MutableArrayRef<ParmVarDecl *>::iterator; |
2285 | using param_const_iterator = ArrayRef<ParmVarDecl *>::const_iterator; |
2286 | |
2287 | bool param_empty() const { return parameters().empty(); } |
2288 | param_iterator param_begin() { return parameters().begin(); } |
2289 | param_iterator param_end() { return parameters().end(); } |
2290 | param_const_iterator param_begin() const { return parameters().begin(); } |
2291 | param_const_iterator param_end() const { return parameters().end(); } |
2292 | size_t param_size() const { return parameters().size(); } |
2293 | |
2294 | /// Return the number of parameters this function must have based on its |
2295 | /// FunctionType. This is the length of the ParamInfo array after it has been |
2296 | /// created. |
2297 | unsigned getNumParams() const; |
2298 | |
2299 | const ParmVarDecl *getParamDecl(unsigned i) const { |
2300 | assert(i < getNumParams() && "Illegal param #")((i < getNumParams() && "Illegal param #") ? static_cast <void> (0) : __assert_fail ("i < getNumParams() && \"Illegal param #\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 2300, __PRETTY_FUNCTION__)); |
2301 | return ParamInfo[i]; |
2302 | } |
2303 | ParmVarDecl *getParamDecl(unsigned i) { |
2304 | assert(i < getNumParams() && "Illegal param #")((i < getNumParams() && "Illegal param #") ? static_cast <void> (0) : __assert_fail ("i < getNumParams() && \"Illegal param #\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 2304, __PRETTY_FUNCTION__)); |
2305 | return ParamInfo[i]; |
2306 | } |
2307 | void setParams(ArrayRef<ParmVarDecl *> NewParamInfo) { |
2308 | setParams(getASTContext(), NewParamInfo); |
2309 | } |
2310 | |
2311 | /// Returns the minimum number of arguments needed to call this function. This |
2312 | /// may be fewer than the number of function parameters, if some of the |
2313 | /// parameters have default arguments (in C++). |
2314 | unsigned getMinRequiredArguments() const; |
2315 | |
2316 | QualType getReturnType() const { |
2317 | return getType()->castAs<FunctionType>()->getReturnType(); |
2318 | } |
2319 | |
2320 | /// Attempt to compute an informative source range covering the |
2321 | /// function return type. This may omit qualifiers and other information with |
2322 | /// limited representation in the AST. |
2323 | SourceRange getReturnTypeSourceRange() const; |
2324 | |
2325 | /// Get the declared return type, which may differ from the actual return |
2326 | /// type if the return type is deduced. |
2327 | QualType getDeclaredReturnType() const { |
2328 | auto *TSI = getTypeSourceInfo(); |
2329 | QualType T = TSI ? TSI->getType() : getType(); |
2330 | return T->castAs<FunctionType>()->getReturnType(); |
2331 | } |
2332 | |
2333 | /// Gets the ExceptionSpecificationType as declared. |
2334 | ExceptionSpecificationType getExceptionSpecType() const { |
2335 | auto *TSI = getTypeSourceInfo(); |
2336 | QualType T = TSI ? TSI->getType() : getType(); |
2337 | const auto *FPT = T->getAs<FunctionProtoType>(); |
2338 | return FPT ? FPT->getExceptionSpecType() : EST_None; |
2339 | } |
2340 | |
2341 | /// Attempt to compute an informative source range covering the |
2342 | /// function exception specification, if any. |
2343 | SourceRange getExceptionSpecSourceRange() const; |
2344 | |
2345 | /// Determine the type of an expression that calls this function. |
2346 | QualType getCallResultType() const { |
2347 | return getType()->castAs<FunctionType>()->getCallResultType( |
2348 | getASTContext()); |
2349 | } |
2350 | |
2351 | /// Returns the storage class as written in the source. For the |
2352 | /// computed linkage of symbol, see getLinkage. |
2353 | StorageClass getStorageClass() const { |
2354 | return static_cast<StorageClass>(FunctionDeclBits.SClass); |
2355 | } |
2356 | |
2357 | /// Sets the storage class as written in the source. |
2358 | void setStorageClass(StorageClass SClass) { |
2359 | FunctionDeclBits.SClass = SClass; |
2360 | } |
2361 | |
2362 | /// Determine whether the "inline" keyword was specified for this |
2363 | /// function. |
2364 | bool isInlineSpecified() const { return FunctionDeclBits.IsInlineSpecified; } |
2365 | |
2366 | /// Set whether the "inline" keyword was specified for this function. |
2367 | void setInlineSpecified(bool I) { |
2368 | FunctionDeclBits.IsInlineSpecified = I; |
2369 | FunctionDeclBits.IsInline = I; |
2370 | } |
2371 | |
2372 | /// Flag that this function is implicitly inline. |
2373 | void setImplicitlyInline(bool I = true) { FunctionDeclBits.IsInline = I; } |
2374 | |
2375 | /// Determine whether this function should be inlined, because it is |
2376 | /// either marked "inline" or "constexpr" or is a member function of a class |
2377 | /// that was defined in the class body. |
2378 | bool isInlined() const { return FunctionDeclBits.IsInline; } |
2379 | |
2380 | bool isInlineDefinitionExternallyVisible() const; |
2381 | |
2382 | bool isMSExternInline() const; |
2383 | |
2384 | bool doesDeclarationForceExternallyVisibleDefinition() const; |
2385 | |
2386 | /// Whether this function declaration represents an C++ overloaded |
2387 | /// operator, e.g., "operator+". |
2388 | bool isOverloadedOperator() const { |
2389 | return getOverloadedOperator() != OO_None; |
2390 | } |
2391 | |
2392 | OverloadedOperatorKind getOverloadedOperator() const; |
2393 | |
2394 | const IdentifierInfo *getLiteralIdentifier() const; |
2395 | |
2396 | /// If this function is an instantiation of a member function |
2397 | /// of a class template specialization, retrieves the function from |
2398 | /// which it was instantiated. |
2399 | /// |
2400 | /// This routine will return non-NULL for (non-templated) member |
2401 | /// functions of class templates and for instantiations of function |
2402 | /// templates. For example, given: |
2403 | /// |
2404 | /// \code |
2405 | /// template<typename T> |
2406 | /// struct X { |
2407 | /// void f(T); |
2408 | /// }; |
2409 | /// \endcode |
2410 | /// |
2411 | /// The declaration for X<int>::f is a (non-templated) FunctionDecl |
2412 | /// whose parent is the class template specialization X<int>. For |
2413 | /// this declaration, getInstantiatedFromFunction() will return |
2414 | /// the FunctionDecl X<T>::A. When a complete definition of |
2415 | /// X<int>::A is required, it will be instantiated from the |
2416 | /// declaration returned by getInstantiatedFromMemberFunction(). |
2417 | FunctionDecl *getInstantiatedFromMemberFunction() const; |
2418 | |
2419 | /// What kind of templated function this is. |
2420 | TemplatedKind getTemplatedKind() const; |
2421 | |
2422 | /// If this function is an instantiation of a member function of a |
2423 | /// class template specialization, retrieves the member specialization |
2424 | /// information. |
2425 | MemberSpecializationInfo *getMemberSpecializationInfo() const; |
2426 | |
2427 | /// Specify that this record is an instantiation of the |
2428 | /// member function FD. |
2429 | void setInstantiationOfMemberFunction(FunctionDecl *FD, |
2430 | TemplateSpecializationKind TSK) { |
2431 | setInstantiationOfMemberFunction(getASTContext(), FD, TSK); |
2432 | } |
2433 | |
2434 | /// Retrieves the function template that is described by this |
2435 | /// function declaration. |
2436 | /// |
2437 | /// Every function template is represented as a FunctionTemplateDecl |
2438 | /// and a FunctionDecl (or something derived from FunctionDecl). The |
2439 | /// former contains template properties (such as the template |
2440 | /// parameter lists) while the latter contains the actual |
2441 | /// description of the template's |
2442 | /// contents. FunctionTemplateDecl::getTemplatedDecl() retrieves the |
2443 | /// FunctionDecl that describes the function template, |
2444 | /// getDescribedFunctionTemplate() retrieves the |
2445 | /// FunctionTemplateDecl from a FunctionDecl. |
2446 | FunctionTemplateDecl *getDescribedFunctionTemplate() const; |
2447 | |
2448 | void setDescribedFunctionTemplate(FunctionTemplateDecl *Template); |
2449 | |
2450 | /// Determine whether this function is a function template |
2451 | /// specialization. |
2452 | bool isFunctionTemplateSpecialization() const { |
2453 | return getPrimaryTemplate() != nullptr; |
2454 | } |
2455 | |
2456 | /// If this function is actually a function template specialization, |
2457 | /// retrieve information about this function template specialization. |
2458 | /// Otherwise, returns NULL. |
2459 | FunctionTemplateSpecializationInfo *getTemplateSpecializationInfo() const; |
2460 | |
2461 | /// Determines whether this function is a function template |
2462 | /// specialization or a member of a class template specialization that can |
2463 | /// be implicitly instantiated. |
2464 | bool isImplicitlyInstantiable() const; |
2465 | |
2466 | /// Determines if the given function was instantiated from a |
2467 | /// function template. |
2468 | bool isTemplateInstantiation() const; |
2469 | |
2470 | /// Retrieve the function declaration from which this function could |
2471 | /// be instantiated, if it is an instantiation (rather than a non-template |
2472 | /// or a specialization, for example). |
2473 | FunctionDecl *getTemplateInstantiationPattern() const; |
2474 | |
2475 | /// Retrieve the primary template that this function template |
2476 | /// specialization either specializes or was instantiated from. |
2477 | /// |
2478 | /// If this function declaration is not a function template specialization, |
2479 | /// returns NULL. |
2480 | FunctionTemplateDecl *getPrimaryTemplate() const; |
2481 | |
2482 | /// Retrieve the template arguments used to produce this function |
2483 | /// template specialization from the primary template. |
2484 | /// |
2485 | /// If this function declaration is not a function template specialization, |
2486 | /// returns NULL. |
2487 | const TemplateArgumentList *getTemplateSpecializationArgs() const; |
2488 | |
2489 | /// Retrieve the template argument list as written in the sources, |
2490 | /// if any. |
2491 | /// |
2492 | /// If this function declaration is not a function template specialization |
2493 | /// or if it had no explicit template argument list, returns NULL. |
2494 | /// Note that it an explicit template argument list may be written empty, |
2495 | /// e.g., template<> void foo<>(char* s); |
2496 | const ASTTemplateArgumentListInfo* |
2497 | getTemplateSpecializationArgsAsWritten() const; |
2498 | |
2499 | /// Specify that this function declaration is actually a function |
2500 | /// template specialization. |
2501 | /// |
2502 | /// \param Template the function template that this function template |
2503 | /// specialization specializes. |
2504 | /// |
2505 | /// \param TemplateArgs the template arguments that produced this |
2506 | /// function template specialization from the template. |
2507 | /// |
2508 | /// \param InsertPos If non-NULL, the position in the function template |
2509 | /// specialization set where the function template specialization data will |
2510 | /// be inserted. |
2511 | /// |
2512 | /// \param TSK the kind of template specialization this is. |
2513 | /// |
2514 | /// \param TemplateArgsAsWritten location info of template arguments. |
2515 | /// |
2516 | /// \param PointOfInstantiation point at which the function template |
2517 | /// specialization was first instantiated. |
2518 | void setFunctionTemplateSpecialization(FunctionTemplateDecl *Template, |
2519 | const TemplateArgumentList *TemplateArgs, |
2520 | void *InsertPos, |
2521 | TemplateSpecializationKind TSK = TSK_ImplicitInstantiation, |
2522 | const TemplateArgumentListInfo *TemplateArgsAsWritten = nullptr, |
2523 | SourceLocation PointOfInstantiation = SourceLocation()) { |
2524 | setFunctionTemplateSpecialization(getASTContext(), Template, TemplateArgs, |
2525 | InsertPos, TSK, TemplateArgsAsWritten, |
2526 | PointOfInstantiation); |
2527 | } |
2528 | |
2529 | /// Specifies that this function declaration is actually a |
2530 | /// dependent function template specialization. |
2531 | void setDependentTemplateSpecialization(ASTContext &Context, |
2532 | const UnresolvedSetImpl &Templates, |
2533 | const TemplateArgumentListInfo &TemplateArgs); |
2534 | |
2535 | DependentFunctionTemplateSpecializationInfo * |
2536 | getDependentSpecializationInfo() const; |
2537 | |
2538 | /// Determine what kind of template instantiation this function |
2539 | /// represents. |
2540 | TemplateSpecializationKind getTemplateSpecializationKind() const; |
2541 | |
2542 | /// Determine the kind of template specialization this function represents |
2543 | /// for the purpose of template instantiation. |
2544 | TemplateSpecializationKind |
2545 | getTemplateSpecializationKindForInstantiation() const; |
2546 | |
2547 | /// Determine what kind of template instantiation this function |
2548 | /// represents. |
2549 | void setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
2550 | SourceLocation PointOfInstantiation = SourceLocation()); |
2551 | |
2552 | /// Retrieve the (first) point of instantiation of a function template |
2553 | /// specialization or a member of a class template specialization. |
2554 | /// |
2555 | /// \returns the first point of instantiation, if this function was |
2556 | /// instantiated from a template; otherwise, returns an invalid source |
2557 | /// location. |
2558 | SourceLocation getPointOfInstantiation() const; |
2559 | |
2560 | /// Determine whether this is or was instantiated from an out-of-line |
2561 | /// definition of a member function. |
2562 | bool isOutOfLine() const override; |
2563 | |
2564 | /// Identify a memory copying or setting function. |
2565 | /// If the given function is a memory copy or setting function, returns |
2566 | /// the corresponding Builtin ID. If the function is not a memory function, |
2567 | /// returns 0. |
2568 | unsigned getMemoryFunctionKind() const; |
2569 | |
2570 | /// Returns ODRHash of the function. This value is calculated and |
2571 | /// stored on first call, then the stored value returned on the other calls. |
2572 | unsigned getODRHash(); |
2573 | |
2574 | /// Returns cached ODRHash of the function. This must have been previously |
2575 | /// computed and stored. |
2576 | unsigned getODRHash() const; |
2577 | |
2578 | // Implement isa/cast/dyncast/etc. |
2579 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
2580 | static bool classofKind(Kind K) { |
2581 | return K >= firstFunction && K <= lastFunction; |
2582 | } |
2583 | static DeclContext *castToDeclContext(const FunctionDecl *D) { |
2584 | return static_cast<DeclContext *>(const_cast<FunctionDecl*>(D)); |
2585 | } |
2586 | static FunctionDecl *castFromDeclContext(const DeclContext *DC) { |
2587 | return static_cast<FunctionDecl *>(const_cast<DeclContext*>(DC)); |
2588 | } |
2589 | }; |
2590 | |
2591 | /// Represents a member of a struct/union/class. |
2592 | class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> { |
2593 | unsigned BitField : 1; |
2594 | unsigned Mutable : 1; |
2595 | mutable unsigned CachedFieldIndex : 30; |
2596 | |
2597 | /// The kinds of value we can store in InitializerOrBitWidth. |
2598 | /// |
2599 | /// Note that this is compatible with InClassInitStyle except for |
2600 | /// ISK_CapturedVLAType. |
2601 | enum InitStorageKind { |
2602 | /// If the pointer is null, there's nothing special. Otherwise, |
2603 | /// this is a bitfield and the pointer is the Expr* storing the |
2604 | /// bit-width. |
2605 | ISK_NoInit = (unsigned) ICIS_NoInit, |
2606 | |
2607 | /// The pointer is an (optional due to delayed parsing) Expr* |
2608 | /// holding the copy-initializer. |
2609 | ISK_InClassCopyInit = (unsigned) ICIS_CopyInit, |
2610 | |
2611 | /// The pointer is an (optional due to delayed parsing) Expr* |
2612 | /// holding the list-initializer. |
2613 | ISK_InClassListInit = (unsigned) ICIS_ListInit, |
2614 | |
2615 | /// The pointer is a VariableArrayType* that's been captured; |
2616 | /// the enclosing context is a lambda or captured statement. |
2617 | ISK_CapturedVLAType, |
2618 | }; |
2619 | |
2620 | /// If this is a bitfield with a default member initializer, this |
2621 | /// structure is used to represent the two expressions. |
2622 | struct InitAndBitWidth { |
2623 | Expr *Init; |
2624 | Expr *BitWidth; |
2625 | }; |
2626 | |
2627 | /// Storage for either the bit-width, the in-class initializer, or |
2628 | /// both (via InitAndBitWidth), or the captured variable length array bound. |
2629 | /// |
2630 | /// If the storage kind is ISK_InClassCopyInit or |
2631 | /// ISK_InClassListInit, but the initializer is null, then this |
2632 | /// field has an in-class initializer that has not yet been parsed |
2633 | /// and attached. |
2634 | // FIXME: Tail-allocate this to reduce the size of FieldDecl in the |
2635 | // overwhelmingly common case that we have none of these things. |
2636 | llvm::PointerIntPair<void *, 2, InitStorageKind> InitStorage; |
2637 | |
2638 | protected: |
2639 | FieldDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc, |
2640 | SourceLocation IdLoc, IdentifierInfo *Id, |
2641 | QualType T, TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
2642 | InClassInitStyle InitStyle) |
2643 | : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), |
2644 | BitField(false), Mutable(Mutable), CachedFieldIndex(0), |
2645 | InitStorage(nullptr, (InitStorageKind) InitStyle) { |
2646 | if (BW) |
2647 | setBitWidth(BW); |
2648 | } |
2649 | |
2650 | public: |
2651 | friend class ASTDeclReader; |
2652 | friend class ASTDeclWriter; |
2653 | |
2654 | static FieldDecl *Create(const ASTContext &C, DeclContext *DC, |
2655 | SourceLocation StartLoc, SourceLocation IdLoc, |
2656 | IdentifierInfo *Id, QualType T, |
2657 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
2658 | InClassInitStyle InitStyle); |
2659 | |
2660 | static FieldDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
2661 | |
2662 | /// Returns the index of this field within its record, |
2663 | /// as appropriate for passing to ASTRecordLayout::getFieldOffset. |
2664 | unsigned getFieldIndex() const; |
2665 | |
2666 | /// Determines whether this field is mutable (C++ only). |
2667 | bool isMutable() const { return Mutable; } |
2668 | |
2669 | /// Determines whether this field is a bitfield. |
2670 | bool isBitField() const { return BitField; } |
2671 | |
2672 | /// Determines whether this is an unnamed bitfield. |
2673 | bool isUnnamedBitfield() const { return isBitField() && !getDeclName(); } |
2674 | |
2675 | /// Determines whether this field is a |
2676 | /// representative for an anonymous struct or union. Such fields are |
2677 | /// unnamed and are implicitly generated by the implementation to |
2678 | /// store the data for the anonymous union or struct. |
2679 | bool isAnonymousStructOrUnion() const; |
2680 | |
2681 | Expr *getBitWidth() const { |
2682 | if (!BitField) |
2683 | return nullptr; |
2684 | void *Ptr = InitStorage.getPointer(); |
2685 | if (getInClassInitStyle()) |
2686 | return static_cast<InitAndBitWidth*>(Ptr)->BitWidth; |
2687 | return static_cast<Expr*>(Ptr); |
2688 | } |
2689 | |
2690 | unsigned getBitWidthValue(const ASTContext &Ctx) const; |
2691 | |
2692 | /// Set the bit-field width for this member. |
2693 | // Note: used by some clients (i.e., do not remove it). |
2694 | void setBitWidth(Expr *Width) { |
2695 | assert(!hasCapturedVLAType() && !BitField &&((!hasCapturedVLAType() && !BitField && "bit width or captured type already set" ) ? static_cast<void> (0) : __assert_fail ("!hasCapturedVLAType() && !BitField && \"bit width or captured type already set\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 2696, __PRETTY_FUNCTION__)) |
2696 | "bit width or captured type already set")((!hasCapturedVLAType() && !BitField && "bit width or captured type already set" ) ? static_cast<void> (0) : __assert_fail ("!hasCapturedVLAType() && !BitField && \"bit width or captured type already set\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 2696, __PRETTY_FUNCTION__)); |
2697 | assert(Width && "no bit width specified")((Width && "no bit width specified") ? static_cast< void> (0) : __assert_fail ("Width && \"no bit width specified\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 2697, __PRETTY_FUNCTION__)); |
2698 | InitStorage.setPointer( |
2699 | InitStorage.getInt() |
2700 | ? new (getASTContext()) |
2701 | InitAndBitWidth{getInClassInitializer(), Width} |
2702 | : static_cast<void*>(Width)); |
2703 | BitField = true; |
2704 | } |
2705 | |
2706 | /// Remove the bit-field width from this member. |
2707 | // Note: used by some clients (i.e., do not remove it). |
2708 | void removeBitWidth() { |
2709 | assert(isBitField() && "no bitfield width to remove")((isBitField() && "no bitfield width to remove") ? static_cast <void> (0) : __assert_fail ("isBitField() && \"no bitfield width to remove\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 2709, __PRETTY_FUNCTION__)); |
2710 | InitStorage.setPointer(getInClassInitializer()); |
2711 | BitField = false; |
2712 | } |
2713 | |
2714 | /// Is this a zero-length bit-field? Such bit-fields aren't really bit-fields |
2715 | /// at all and instead act as a separator between contiguous runs of other |
2716 | /// bit-fields. |
2717 | bool isZeroLengthBitField(const ASTContext &Ctx) const; |
2718 | |
2719 | /// Get the kind of (C++11) default member initializer that this field has. |
2720 | InClassInitStyle getInClassInitStyle() const { |
2721 | InitStorageKind storageKind = InitStorage.getInt(); |
2722 | return (storageKind == ISK_CapturedVLAType |
2723 | ? ICIS_NoInit : (InClassInitStyle) storageKind); |
2724 | } |
2725 | |
2726 | /// Determine whether this member has a C++11 default member initializer. |
2727 | bool hasInClassInitializer() const { |
2728 | return getInClassInitStyle() != ICIS_NoInit; |
2729 | } |
2730 | |
2731 | /// Get the C++11 default member initializer for this member, or null if one |
2732 | /// has not been set. If a valid declaration has a default member initializer, |
2733 | /// but this returns null, then we have not parsed and attached it yet. |
2734 | Expr *getInClassInitializer() const { |
2735 | if (!hasInClassInitializer()) |
2736 | return nullptr; |
2737 | void *Ptr = InitStorage.getPointer(); |
2738 | if (BitField) |
2739 | return static_cast<InitAndBitWidth*>(Ptr)->Init; |
2740 | return static_cast<Expr*>(Ptr); |
2741 | } |
2742 | |
2743 | /// Set the C++11 in-class initializer for this member. |
2744 | void setInClassInitializer(Expr *Init) { |
2745 | assert(hasInClassInitializer() && !getInClassInitializer())((hasInClassInitializer() && !getInClassInitializer() ) ? static_cast<void> (0) : __assert_fail ("hasInClassInitializer() && !getInClassInitializer()" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 2745, __PRETTY_FUNCTION__)); |
2746 | if (BitField) |
2747 | static_cast<InitAndBitWidth*>(InitStorage.getPointer())->Init = Init; |
2748 | else |
2749 | InitStorage.setPointer(Init); |
2750 | } |
2751 | |
2752 | /// Remove the C++11 in-class initializer from this member. |
2753 | void removeInClassInitializer() { |
2754 | assert(hasInClassInitializer() && "no initializer to remove")((hasInClassInitializer() && "no initializer to remove" ) ? static_cast<void> (0) : __assert_fail ("hasInClassInitializer() && \"no initializer to remove\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 2754, __PRETTY_FUNCTION__)); |
2755 | InitStorage.setPointerAndInt(getBitWidth(), ISK_NoInit); |
2756 | } |
2757 | |
2758 | /// Determine whether this member captures the variable length array |
2759 | /// type. |
2760 | bool hasCapturedVLAType() const { |
2761 | return InitStorage.getInt() == ISK_CapturedVLAType; |
2762 | } |
2763 | |
2764 | /// Get the captured variable length array type. |
2765 | const VariableArrayType *getCapturedVLAType() const { |
2766 | return hasCapturedVLAType() ? static_cast<const VariableArrayType *>( |
2767 | InitStorage.getPointer()) |
2768 | : nullptr; |
2769 | } |
2770 | |
2771 | /// Set the captured variable length array type for this field. |
2772 | void setCapturedVLAType(const VariableArrayType *VLAType); |
2773 | |
2774 | /// Returns the parent of this field declaration, which |
2775 | /// is the struct in which this field is defined. |
2776 | const RecordDecl *getParent() const { |
2777 | return cast<RecordDecl>(getDeclContext()); |
2778 | } |
2779 | |
2780 | RecordDecl *getParent() { |
2781 | return cast<RecordDecl>(getDeclContext()); |
2782 | } |
2783 | |
2784 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
2785 | |
2786 | /// Retrieves the canonical declaration of this field. |
2787 | FieldDecl *getCanonicalDecl() override { return getFirstDecl(); } |
2788 | const FieldDecl *getCanonicalDecl() const { return getFirstDecl(); } |
2789 | |
2790 | // Implement isa/cast/dyncast/etc. |
2791 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
2792 | static bool classofKind(Kind K) { return K >= firstField && K <= lastField; } |
2793 | }; |
2794 | |
2795 | /// An instance of this object exists for each enum constant |
2796 | /// that is defined. For example, in "enum X {a,b}", each of a/b are |
2797 | /// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a |
2798 | /// TagType for the X EnumDecl. |
2799 | class EnumConstantDecl : public ValueDecl, public Mergeable<EnumConstantDecl> { |
2800 | Stmt *Init; // an integer constant expression |
2801 | llvm::APSInt Val; // The value. |
2802 | |
2803 | protected: |
2804 | EnumConstantDecl(DeclContext *DC, SourceLocation L, |
2805 | IdentifierInfo *Id, QualType T, Expr *E, |
2806 | const llvm::APSInt &V) |
2807 | : ValueDecl(EnumConstant, DC, L, Id, T), Init((Stmt*)E), Val(V) {} |
2808 | |
2809 | public: |
2810 | friend class StmtIteratorBase; |
2811 | |
2812 | static EnumConstantDecl *Create(ASTContext &C, EnumDecl *DC, |
2813 | SourceLocation L, IdentifierInfo *Id, |
2814 | QualType T, Expr *E, |
2815 | const llvm::APSInt &V); |
2816 | static EnumConstantDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
2817 | |
2818 | const Expr *getInitExpr() const { return (const Expr*) Init; } |
2819 | Expr *getInitExpr() { return (Expr*) Init; } |
2820 | const llvm::APSInt &getInitVal() const { return Val; } |
2821 | |
2822 | void setInitExpr(Expr *E) { Init = (Stmt*) E; } |
2823 | void setInitVal(const llvm::APSInt &V) { Val = V; } |
2824 | |
2825 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
2826 | |
2827 | /// Retrieves the canonical declaration of this enumerator. |
2828 | EnumConstantDecl *getCanonicalDecl() override { return getFirstDecl(); } |
2829 | const EnumConstantDecl *getCanonicalDecl() const { return getFirstDecl(); } |
2830 | |
2831 | // Implement isa/cast/dyncast/etc. |
2832 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
2833 | static bool classofKind(Kind K) { return K == EnumConstant; } |
2834 | }; |
2835 | |
2836 | /// Represents a field injected from an anonymous union/struct into the parent |
2837 | /// scope. These are always implicit. |
2838 | class IndirectFieldDecl : public ValueDecl, |
2839 | public Mergeable<IndirectFieldDecl> { |
2840 | NamedDecl **Chaining; |
2841 | unsigned ChainingSize; |
2842 | |
2843 | IndirectFieldDecl(ASTContext &C, DeclContext *DC, SourceLocation L, |
2844 | DeclarationName N, QualType T, |
2845 | MutableArrayRef<NamedDecl *> CH); |
2846 | |
2847 | void anchor() override; |
2848 | |
2849 | public: |
2850 | friend class ASTDeclReader; |
2851 | |
2852 | static IndirectFieldDecl *Create(ASTContext &C, DeclContext *DC, |
2853 | SourceLocation L, IdentifierInfo *Id, |
2854 | QualType T, llvm::MutableArrayRef<NamedDecl *> CH); |
2855 | |
2856 | static IndirectFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
2857 | |
2858 | using chain_iterator = ArrayRef<NamedDecl *>::const_iterator; |
2859 | |
2860 | ArrayRef<NamedDecl *> chain() const { |
2861 | return llvm::makeArrayRef(Chaining, ChainingSize); |
2862 | } |
2863 | chain_iterator chain_begin() const { return chain().begin(); } |
2864 | chain_iterator chain_end() const { return chain().end(); } |
2865 | |
2866 | unsigned getChainingSize() const { return ChainingSize; } |
2867 | |
2868 | FieldDecl *getAnonField() const { |
2869 | assert(chain().size() >= 2)((chain().size() >= 2) ? static_cast<void> (0) : __assert_fail ("chain().size() >= 2", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 2869, __PRETTY_FUNCTION__)); |
2870 | return cast<FieldDecl>(chain().back()); |
2871 | } |
2872 | |
2873 | VarDecl *getVarDecl() const { |
2874 | assert(chain().size() >= 2)((chain().size() >= 2) ? static_cast<void> (0) : __assert_fail ("chain().size() >= 2", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 2874, __PRETTY_FUNCTION__)); |
2875 | return dyn_cast<VarDecl>(chain().front()); |
2876 | } |
2877 | |
2878 | IndirectFieldDecl *getCanonicalDecl() override { return getFirstDecl(); } |
2879 | const IndirectFieldDecl *getCanonicalDecl() const { return getFirstDecl(); } |
2880 | |
2881 | // Implement isa/cast/dyncast/etc. |
2882 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
2883 | static bool classofKind(Kind K) { return K == IndirectField; } |
2884 | }; |
2885 | |
2886 | /// Represents a declaration of a type. |
2887 | class TypeDecl : public NamedDecl { |
2888 | friend class ASTContext; |
2889 | |
2890 | /// This indicates the Type object that represents |
2891 | /// this TypeDecl. It is a cache maintained by |
2892 | /// ASTContext::getTypedefType, ASTContext::getTagDeclType, and |
2893 | /// ASTContext::getTemplateTypeParmType, and TemplateTypeParmDecl. |
2894 | mutable const Type *TypeForDecl = nullptr; |
2895 | |
2896 | /// The start of the source range for this declaration. |
2897 | SourceLocation LocStart; |
2898 | |
2899 | void anchor() override; |
2900 | |
2901 | protected: |
2902 | TypeDecl(Kind DK, DeclContext *DC, SourceLocation L, IdentifierInfo *Id, |
2903 | SourceLocation StartL = SourceLocation()) |
2904 | : NamedDecl(DK, DC, L, Id), LocStart(StartL) {} |
2905 | |
2906 | public: |
2907 | // Low-level accessor. If you just want the type defined by this node, |
2908 | // check out ASTContext::getTypeDeclType or one of |
2909 | // ASTContext::getTypedefType, ASTContext::getRecordType, etc. if you |
2910 | // already know the specific kind of node this is. |
2911 | const Type *getTypeForDecl() const { return TypeForDecl; } |
2912 | void setTypeForDecl(const Type *TD) { TypeForDecl = TD; } |
2913 | |
2914 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return LocStart; } |
2915 | void setLocStart(SourceLocation L) { LocStart = L; } |
2916 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) { |
2917 | if (LocStart.isValid()) |
2918 | return SourceRange(LocStart, getLocation()); |
2919 | else |
2920 | return SourceRange(getLocation()); |
2921 | } |
2922 | |
2923 | // Implement isa/cast/dyncast/etc. |
2924 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
2925 | static bool classofKind(Kind K) { return K >= firstType && K <= lastType; } |
2926 | }; |
2927 | |
2928 | /// Base class for declarations which introduce a typedef-name. |
2929 | class TypedefNameDecl : public TypeDecl, public Redeclarable<TypedefNameDecl> { |
2930 | struct alignas(8) ModedTInfo { |
2931 | TypeSourceInfo *first; |
2932 | QualType second; |
2933 | }; |
2934 | |
2935 | /// If int part is 0, we have not computed IsTransparentTag. |
2936 | /// Otherwise, IsTransparentTag is (getInt() >> 1). |
2937 | mutable llvm::PointerIntPair< |
2938 | llvm::PointerUnion<TypeSourceInfo *, ModedTInfo *>, 2> |
2939 | MaybeModedTInfo; |
2940 | |
2941 | void anchor() override; |
2942 | |
2943 | protected: |
2944 | TypedefNameDecl(Kind DK, ASTContext &C, DeclContext *DC, |
2945 | SourceLocation StartLoc, SourceLocation IdLoc, |
2946 | IdentifierInfo *Id, TypeSourceInfo *TInfo) |
2947 | : TypeDecl(DK, DC, IdLoc, Id, StartLoc), redeclarable_base(C), |
2948 | MaybeModedTInfo(TInfo, 0) {} |
2949 | |
2950 | using redeclarable_base = Redeclarable<TypedefNameDecl>; |
2951 | |
2952 | TypedefNameDecl *getNextRedeclarationImpl() override { |
2953 | return getNextRedeclaration(); |
2954 | } |
2955 | |
2956 | TypedefNameDecl *getPreviousDeclImpl() override { |
2957 | return getPreviousDecl(); |
2958 | } |
2959 | |
2960 | TypedefNameDecl *getMostRecentDeclImpl() override { |
2961 | return getMostRecentDecl(); |
2962 | } |
2963 | |
2964 | public: |
2965 | using redecl_range = redeclarable_base::redecl_range; |
2966 | using redecl_iterator = redeclarable_base::redecl_iterator; |
2967 | |
2968 | using redeclarable_base::redecls_begin; |
2969 | using redeclarable_base::redecls_end; |
2970 | using redeclarable_base::redecls; |
2971 | using redeclarable_base::getPreviousDecl; |
2972 | using redeclarable_base::getMostRecentDecl; |
2973 | using redeclarable_base::isFirstDecl; |
2974 | |
2975 | bool isModed() const { |
2976 | return MaybeModedTInfo.getPointer().is<ModedTInfo *>(); |
2977 | } |
2978 | |
2979 | TypeSourceInfo *getTypeSourceInfo() const { |
2980 | return isModed() ? MaybeModedTInfo.getPointer().get<ModedTInfo *>()->first |
2981 | : MaybeModedTInfo.getPointer().get<TypeSourceInfo *>(); |
2982 | } |
2983 | |
2984 | QualType getUnderlyingType() const { |
2985 | return isModed() ? MaybeModedTInfo.getPointer().get<ModedTInfo *>()->second |
2986 | : MaybeModedTInfo.getPointer() |
2987 | .get<TypeSourceInfo *>() |
2988 | ->getType(); |
2989 | } |
2990 | |
2991 | void setTypeSourceInfo(TypeSourceInfo *newType) { |
2992 | MaybeModedTInfo.setPointer(newType); |
2993 | } |
2994 | |
2995 | void setModedTypeSourceInfo(TypeSourceInfo *unmodedTSI, QualType modedTy) { |
2996 | MaybeModedTInfo.setPointer(new (getASTContext(), 8) |
2997 | ModedTInfo({unmodedTSI, modedTy})); |
2998 | } |
2999 | |
3000 | /// Retrieves the canonical declaration of this typedef-name. |
3001 | TypedefNameDecl *getCanonicalDecl() override { return getFirstDecl(); } |
3002 | const TypedefNameDecl *getCanonicalDecl() const { return getFirstDecl(); } |
3003 | |
3004 | /// Retrieves the tag declaration for which this is the typedef name for |
3005 | /// linkage purposes, if any. |
3006 | /// |
3007 | /// \param AnyRedecl Look for the tag declaration in any redeclaration of |
3008 | /// this typedef declaration. |
3009 | TagDecl *getAnonDeclWithTypedefName(bool AnyRedecl = false) const; |
3010 | |
3011 | /// Determines if this typedef shares a name and spelling location with its |
3012 | /// underlying tag type, as is the case with the NS_ENUM macro. |
3013 | bool isTransparentTag() const { |
3014 | if (MaybeModedTInfo.getInt()) |
3015 | return MaybeModedTInfo.getInt() & 0x2; |
3016 | return isTransparentTagSlow(); |
3017 | } |
3018 | |
3019 | // Implement isa/cast/dyncast/etc. |
3020 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3021 | static bool classofKind(Kind K) { |
3022 | return K >= firstTypedefName && K <= lastTypedefName; |
3023 | } |
3024 | |
3025 | private: |
3026 | bool isTransparentTagSlow() const; |
3027 | }; |
3028 | |
3029 | /// Represents the declaration of a typedef-name via the 'typedef' |
3030 | /// type specifier. |
3031 | class TypedefDecl : public TypedefNameDecl { |
3032 | TypedefDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
3033 | SourceLocation IdLoc, IdentifierInfo *Id, TypeSourceInfo *TInfo) |
3034 | : TypedefNameDecl(Typedef, C, DC, StartLoc, IdLoc, Id, TInfo) {} |
3035 | |
3036 | public: |
3037 | static TypedefDecl *Create(ASTContext &C, DeclContext *DC, |
3038 | SourceLocation StartLoc, SourceLocation IdLoc, |
3039 | IdentifierInfo *Id, TypeSourceInfo *TInfo); |
3040 | static TypedefDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3041 | |
3042 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
3043 | |
3044 | // Implement isa/cast/dyncast/etc. |
3045 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3046 | static bool classofKind(Kind K) { return K == Typedef; } |
3047 | }; |
3048 | |
3049 | /// Represents the declaration of a typedef-name via a C++11 |
3050 | /// alias-declaration. |
3051 | class TypeAliasDecl : public TypedefNameDecl { |
3052 | /// The template for which this is the pattern, if any. |
3053 | TypeAliasTemplateDecl *Template; |
3054 | |
3055 | TypeAliasDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
3056 | SourceLocation IdLoc, IdentifierInfo *Id, TypeSourceInfo *TInfo) |
3057 | : TypedefNameDecl(TypeAlias, C, DC, StartLoc, IdLoc, Id, TInfo), |
3058 | Template(nullptr) {} |
3059 | |
3060 | public: |
3061 | static TypeAliasDecl *Create(ASTContext &C, DeclContext *DC, |
3062 | SourceLocation StartLoc, SourceLocation IdLoc, |
3063 | IdentifierInfo *Id, TypeSourceInfo *TInfo); |
3064 | static TypeAliasDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3065 | |
3066 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
3067 | |
3068 | TypeAliasTemplateDecl *getDescribedAliasTemplate() const { return Template; } |
3069 | void setDescribedAliasTemplate(TypeAliasTemplateDecl *TAT) { Template = TAT; } |
3070 | |
3071 | // Implement isa/cast/dyncast/etc. |
3072 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3073 | static bool classofKind(Kind K) { return K == TypeAlias; } |
3074 | }; |
3075 | |
3076 | /// Represents the declaration of a struct/union/class/enum. |
3077 | class TagDecl : public TypeDecl, |
3078 | public DeclContext, |
3079 | public Redeclarable<TagDecl> { |
3080 | // This class stores some data in DeclContext::TagDeclBits |
3081 | // to save some space. Use the provided accessors to access it. |
3082 | public: |
3083 | // This is really ugly. |
3084 | using TagKind = TagTypeKind; |
3085 | |
3086 | private: |
3087 | SourceRange BraceRange; |
3088 | |
3089 | // A struct representing syntactic qualifier info, |
3090 | // to be used for the (uncommon) case of out-of-line declarations. |
3091 | using ExtInfo = QualifierInfo; |
3092 | |
3093 | /// If the (out-of-line) tag declaration name |
3094 | /// is qualified, it points to the qualifier info (nns and range); |
3095 | /// otherwise, if the tag declaration is anonymous and it is part of |
3096 | /// a typedef or alias, it points to the TypedefNameDecl (used for mangling); |
3097 | /// otherwise, if the tag declaration is anonymous and it is used as a |
3098 | /// declaration specifier for variables, it points to the first VarDecl (used |
3099 | /// for mangling); |
3100 | /// otherwise, it is a null (TypedefNameDecl) pointer. |
3101 | llvm::PointerUnion<TypedefNameDecl *, ExtInfo *> TypedefNameDeclOrQualifier; |
3102 | |
3103 | bool hasExtInfo() const { return TypedefNameDeclOrQualifier.is<ExtInfo *>(); } |
3104 | ExtInfo *getExtInfo() { return TypedefNameDeclOrQualifier.get<ExtInfo *>(); } |
3105 | const ExtInfo *getExtInfo() const { |
3106 | return TypedefNameDeclOrQualifier.get<ExtInfo *>(); |
3107 | } |
3108 | |
3109 | protected: |
3110 | TagDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC, |
3111 | SourceLocation L, IdentifierInfo *Id, TagDecl *PrevDecl, |
3112 | SourceLocation StartL); |
3113 | |
3114 | using redeclarable_base = Redeclarable<TagDecl>; |
3115 | |
3116 | TagDecl *getNextRedeclarationImpl() override { |
3117 | return getNextRedeclaration(); |
3118 | } |
3119 | |
3120 | TagDecl *getPreviousDeclImpl() override { |
3121 | return getPreviousDecl(); |
3122 | } |
3123 | |
3124 | TagDecl *getMostRecentDeclImpl() override { |
3125 | return getMostRecentDecl(); |
3126 | } |
3127 | |
3128 | /// Completes the definition of this tag declaration. |
3129 | /// |
3130 | /// This is a helper function for derived classes. |
3131 | void completeDefinition(); |
3132 | |
3133 | /// True if this decl is currently being defined. |
3134 | void setBeingDefined(bool V = true) { TagDeclBits.IsBeingDefined = V; } |
3135 | |
3136 | /// Indicates whether it is possible for declarations of this kind |
3137 | /// to have an out-of-date definition. |
3138 | /// |
3139 | /// This option is only enabled when modules are enabled. |
3140 | void setMayHaveOutOfDateDef(bool V = true) { |
3141 | TagDeclBits.MayHaveOutOfDateDef = V; |
3142 | } |
3143 | |
3144 | public: |
3145 | friend class ASTDeclReader; |
3146 | friend class ASTDeclWriter; |
3147 | |
3148 | using redecl_range = redeclarable_base::redecl_range; |
3149 | using redecl_iterator = redeclarable_base::redecl_iterator; |
3150 | |
3151 | using redeclarable_base::redecls_begin; |
3152 | using redeclarable_base::redecls_end; |
3153 | using redeclarable_base::redecls; |
3154 | using redeclarable_base::getPreviousDecl; |
3155 | using redeclarable_base::getMostRecentDecl; |
3156 | using redeclarable_base::isFirstDecl; |
3157 | |
3158 | SourceRange getBraceRange() const { return BraceRange; } |
3159 | void setBraceRange(SourceRange R) { BraceRange = R; } |
3160 | |
3161 | /// Return SourceLocation representing start of source |
3162 | /// range ignoring outer template declarations. |
3163 | SourceLocation getInnerLocStart() const { return getBeginLoc(); } |
3164 | |
3165 | /// Return SourceLocation representing start of source |
3166 | /// range taking into account any outer template declarations. |
3167 | SourceLocation getOuterLocStart() const; |
3168 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
3169 | |
3170 | TagDecl *getCanonicalDecl() override; |
3171 | const TagDecl *getCanonicalDecl() const { |
3172 | return const_cast<TagDecl*>(this)->getCanonicalDecl(); |
3173 | } |
3174 | |
3175 | /// Return true if this declaration is a completion definition of the type. |
3176 | /// Provided for consistency. |
3177 | bool isThisDeclarationADefinition() const { |
3178 | return isCompleteDefinition(); |
3179 | } |
3180 | |
3181 | /// Return true if this decl has its body fully specified. |
3182 | bool isCompleteDefinition() const { return TagDeclBits.IsCompleteDefinition; } |
3183 | |
3184 | /// True if this decl has its body fully specified. |
3185 | void setCompleteDefinition(bool V = true) { |
3186 | TagDeclBits.IsCompleteDefinition = V; |
3187 | } |
3188 | |
3189 | /// Return true if this complete decl is |
3190 | /// required to be complete for some existing use. |
3191 | bool isCompleteDefinitionRequired() const { |
3192 | return TagDeclBits.IsCompleteDefinitionRequired; |
3193 | } |
3194 | |
3195 | /// True if this complete decl is |
3196 | /// required to be complete for some existing use. |
3197 | void setCompleteDefinitionRequired(bool V = true) { |
3198 | TagDeclBits.IsCompleteDefinitionRequired = V; |
3199 | } |
3200 | |
3201 | /// Return true if this decl is currently being defined. |
3202 | bool isBeingDefined() const { return TagDeclBits.IsBeingDefined; } |
3203 | |
3204 | /// True if this tag declaration is "embedded" (i.e., defined or declared |
3205 | /// for the very first time) in the syntax of a declarator. |
3206 | bool isEmbeddedInDeclarator() const { |
3207 | return TagDeclBits.IsEmbeddedInDeclarator; |
3208 | } |
3209 | |
3210 | /// True if this tag declaration is "embedded" (i.e., defined or declared |
3211 | /// for the very first time) in the syntax of a declarator. |
3212 | void setEmbeddedInDeclarator(bool isInDeclarator) { |
3213 | TagDeclBits.IsEmbeddedInDeclarator = isInDeclarator; |
3214 | } |
3215 | |
3216 | /// True if this tag is free standing, e.g. "struct foo;". |
3217 | bool isFreeStanding() const { return TagDeclBits.IsFreeStanding; } |
3218 | |
3219 | /// True if this tag is free standing, e.g. "struct foo;". |
3220 | void setFreeStanding(bool isFreeStanding = true) { |
3221 | TagDeclBits.IsFreeStanding = isFreeStanding; |
3222 | } |
3223 | |
3224 | /// Indicates whether it is possible for declarations of this kind |
3225 | /// to have an out-of-date definition. |
3226 | /// |
3227 | /// This option is only enabled when modules are enabled. |
3228 | bool mayHaveOutOfDateDef() const { return TagDeclBits.MayHaveOutOfDateDef; } |
3229 | |
3230 | /// Whether this declaration declares a type that is |
3231 | /// dependent, i.e., a type that somehow depends on template |
3232 | /// parameters. |
3233 | bool isDependentType() const { return isDependentContext(); } |
3234 | |
3235 | /// Starts the definition of this tag declaration. |
3236 | /// |
3237 | /// This method should be invoked at the beginning of the definition |
3238 | /// of this tag declaration. It will set the tag type into a state |
3239 | /// where it is in the process of being defined. |
3240 | void startDefinition(); |
3241 | |
3242 | /// Returns the TagDecl that actually defines this |
3243 | /// struct/union/class/enum. When determining whether or not a |
3244 | /// struct/union/class/enum has a definition, one should use this |
3245 | /// method as opposed to 'isDefinition'. 'isDefinition' indicates |
3246 | /// whether or not a specific TagDecl is defining declaration, not |
3247 | /// whether or not the struct/union/class/enum type is defined. |
3248 | /// This method returns NULL if there is no TagDecl that defines |
3249 | /// the struct/union/class/enum. |
3250 | TagDecl *getDefinition() const; |
3251 | |
3252 | StringRef getKindName() const { |
3253 | return TypeWithKeyword::getTagTypeKindName(getTagKind()); |
3254 | } |
3255 | |
3256 | TagKind getTagKind() const { |
3257 | return static_cast<TagKind>(TagDeclBits.TagDeclKind); |
3258 | } |
3259 | |
3260 | void setTagKind(TagKind TK) { TagDeclBits.TagDeclKind = TK; } |
3261 | |
3262 | bool isStruct() const { return getTagKind() == TTK_Struct; } |
3263 | bool isInterface() const { return getTagKind() == TTK_Interface; } |
3264 | bool isClass() const { return getTagKind() == TTK_Class; } |
3265 | bool isUnion() const { return getTagKind() == TTK_Union; } |
3266 | bool isEnum() const { return getTagKind() == TTK_Enum; } |
3267 | |
3268 | /// Is this tag type named, either directly or via being defined in |
3269 | /// a typedef of this type? |
3270 | /// |
3271 | /// C++11 [basic.link]p8: |
3272 | /// A type is said to have linkage if and only if: |
3273 | /// - it is a class or enumeration type that is named (or has a |
3274 | /// name for linkage purposes) and the name has linkage; ... |
3275 | /// C++11 [dcl.typedef]p9: |
3276 | /// If the typedef declaration defines an unnamed class (or enum), |
3277 | /// the first typedef-name declared by the declaration to be that |
3278 | /// class type (or enum type) is used to denote the class type (or |
3279 | /// enum type) for linkage purposes only. |
3280 | /// |
3281 | /// C does not have an analogous rule, but the same concept is |
3282 | /// nonetheless useful in some places. |
3283 | bool hasNameForLinkage() const { |
3284 | return (getDeclName() || getTypedefNameForAnonDecl()); |
3285 | } |
3286 | |
3287 | TypedefNameDecl *getTypedefNameForAnonDecl() const { |
3288 | return hasExtInfo() ? nullptr |
3289 | : TypedefNameDeclOrQualifier.get<TypedefNameDecl *>(); |
3290 | } |
3291 | |
3292 | void setTypedefNameForAnonDecl(TypedefNameDecl *TDD); |
3293 | |
3294 | /// Retrieve the nested-name-specifier that qualifies the name of this |
3295 | /// declaration, if it was present in the source. |
3296 | NestedNameSpecifier *getQualifier() const { |
3297 | return hasExtInfo() ? getExtInfo()->QualifierLoc.getNestedNameSpecifier() |
3298 | : nullptr; |
3299 | } |
3300 | |
3301 | /// Retrieve the nested-name-specifier (with source-location |
3302 | /// information) that qualifies the name of this declaration, if it was |
3303 | /// present in the source. |
3304 | NestedNameSpecifierLoc getQualifierLoc() const { |
3305 | return hasExtInfo() ? getExtInfo()->QualifierLoc |
3306 | : NestedNameSpecifierLoc(); |
3307 | } |
3308 | |
3309 | void setQualifierInfo(NestedNameSpecifierLoc QualifierLoc); |
3310 | |
3311 | unsigned getNumTemplateParameterLists() const { |
3312 | return hasExtInfo() ? getExtInfo()->NumTemplParamLists : 0; |
3313 | } |
3314 | |
3315 | TemplateParameterList *getTemplateParameterList(unsigned i) const { |
3316 | assert(i < getNumTemplateParameterLists())((i < getNumTemplateParameterLists()) ? static_cast<void > (0) : __assert_fail ("i < getNumTemplateParameterLists()" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 3316, __PRETTY_FUNCTION__)); |
3317 | return getExtInfo()->TemplParamLists[i]; |
3318 | } |
3319 | |
3320 | void setTemplateParameterListsInfo(ASTContext &Context, |
3321 | ArrayRef<TemplateParameterList *> TPLists); |
3322 | |
3323 | // Implement isa/cast/dyncast/etc. |
3324 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3325 | static bool classofKind(Kind K) { return K >= firstTag && K <= lastTag; } |
3326 | |
3327 | static DeclContext *castToDeclContext(const TagDecl *D) { |
3328 | return static_cast<DeclContext *>(const_cast<TagDecl*>(D)); |
3329 | } |
3330 | |
3331 | static TagDecl *castFromDeclContext(const DeclContext *DC) { |
3332 | return static_cast<TagDecl *>(const_cast<DeclContext*>(DC)); |
3333 | } |
3334 | }; |
3335 | |
3336 | /// Represents an enum. In C++11, enums can be forward-declared |
3337 | /// with a fixed underlying type, and in C we allow them to be forward-declared |
3338 | /// with no underlying type as an extension. |
3339 | class EnumDecl : public TagDecl { |
3340 | // This class stores some data in DeclContext::EnumDeclBits |
3341 | // to save some space. Use the provided accessors to access it. |
3342 | |
3343 | /// This represent the integer type that the enum corresponds |
3344 | /// to for code generation purposes. Note that the enumerator constants may |
3345 | /// have a different type than this does. |
3346 | /// |
3347 | /// If the underlying integer type was explicitly stated in the source |
3348 | /// code, this is a TypeSourceInfo* for that type. Otherwise this type |
3349 | /// was automatically deduced somehow, and this is a Type*. |
3350 | /// |
3351 | /// Normally if IsFixed(), this would contain a TypeSourceInfo*, but in |
3352 | /// some cases it won't. |
3353 | /// |
3354 | /// The underlying type of an enumeration never has any qualifiers, so |
3355 | /// we can get away with just storing a raw Type*, and thus save an |
3356 | /// extra pointer when TypeSourceInfo is needed. |
3357 | llvm::PointerUnion<const Type *, TypeSourceInfo *> IntegerType; |
3358 | |
3359 | /// The integer type that values of this type should |
3360 | /// promote to. In C, enumerators are generally of an integer type |
3361 | /// directly, but gcc-style large enumerators (and all enumerators |
3362 | /// in C++) are of the enum type instead. |
3363 | QualType PromotionType; |
3364 | |
3365 | /// If this enumeration is an instantiation of a member enumeration |
3366 | /// of a class template specialization, this is the member specialization |
3367 | /// information. |
3368 | MemberSpecializationInfo *SpecializationInfo = nullptr; |
3369 | |
3370 | /// Store the ODRHash after first calculation. |
3371 | /// The corresponding flag HasODRHash is in EnumDeclBits |
3372 | /// and can be accessed with the provided accessors. |
3373 | unsigned ODRHash; |
3374 | |
3375 | EnumDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
3376 | SourceLocation IdLoc, IdentifierInfo *Id, EnumDecl *PrevDecl, |
3377 | bool Scoped, bool ScopedUsingClassTag, bool Fixed); |
3378 | |
3379 | void anchor() override; |
3380 | |
3381 | void setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, |
3382 | TemplateSpecializationKind TSK); |
3383 | |
3384 | /// Sets the width in bits required to store all the |
3385 | /// non-negative enumerators of this enum. |
3386 | void setNumPositiveBits(unsigned Num) { |
3387 | EnumDeclBits.NumPositiveBits = Num; |
3388 | assert(EnumDeclBits.NumPositiveBits == Num && "can't store this bitcount")((EnumDeclBits.NumPositiveBits == Num && "can't store this bitcount" ) ? static_cast<void> (0) : __assert_fail ("EnumDeclBits.NumPositiveBits == Num && \"can't store this bitcount\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 3388, __PRETTY_FUNCTION__)); |
3389 | } |
3390 | |
3391 | /// Returns the width in bits required to store all the |
3392 | /// negative enumerators of this enum. (see getNumNegativeBits) |
3393 | void setNumNegativeBits(unsigned Num) { EnumDeclBits.NumNegativeBits = Num; } |
3394 | |
3395 | /// True if this tag declaration is a scoped enumeration. Only |
3396 | /// possible in C++11 mode. |
3397 | void setScoped(bool Scoped = true) { EnumDeclBits.IsScoped = Scoped; } |
3398 | |
3399 | /// If this tag declaration is a scoped enum, |
3400 | /// then this is true if the scoped enum was declared using the class |
3401 | /// tag, false if it was declared with the struct tag. No meaning is |
3402 | /// associated if this tag declaration is not a scoped enum. |
3403 | void setScopedUsingClassTag(bool ScopedUCT = true) { |
3404 | EnumDeclBits.IsScopedUsingClassTag = ScopedUCT; |
3405 | } |
3406 | |
3407 | /// True if this is an Objective-C, C++11, or |
3408 | /// Microsoft-style enumeration with a fixed underlying type. |
3409 | void setFixed(bool Fixed = true) { EnumDeclBits.IsFixed = Fixed; } |
3410 | |
3411 | /// True if a valid hash is stored in ODRHash. |
3412 | bool hasODRHash() const { return EnumDeclBits.HasODRHash; } |
3413 | void setHasODRHash(bool Hash = true) { EnumDeclBits.HasODRHash = Hash; } |
3414 | |
3415 | public: |
3416 | friend class ASTDeclReader; |
3417 | |
3418 | EnumDecl *getCanonicalDecl() override { |
3419 | return cast<EnumDecl>(TagDecl::getCanonicalDecl()); |
3420 | } |
3421 | const EnumDecl *getCanonicalDecl() const { |
3422 | return const_cast<EnumDecl*>(this)->getCanonicalDecl(); |
3423 | } |
3424 | |
3425 | EnumDecl *getPreviousDecl() { |
3426 | return cast_or_null<EnumDecl>( |
3427 | static_cast<TagDecl *>(this)->getPreviousDecl()); |
3428 | } |
3429 | const EnumDecl *getPreviousDecl() const { |
3430 | return const_cast<EnumDecl*>(this)->getPreviousDecl(); |
3431 | } |
3432 | |
3433 | EnumDecl *getMostRecentDecl() { |
3434 | return cast<EnumDecl>(static_cast<TagDecl *>(this)->getMostRecentDecl()); |
3435 | } |
3436 | const EnumDecl *getMostRecentDecl() const { |
3437 | return const_cast<EnumDecl*>(this)->getMostRecentDecl(); |
3438 | } |
3439 | |
3440 | EnumDecl *getDefinition() const { |
3441 | return cast_or_null<EnumDecl>(TagDecl::getDefinition()); |
3442 | } |
3443 | |
3444 | static EnumDecl *Create(ASTContext &C, DeclContext *DC, |
3445 | SourceLocation StartLoc, SourceLocation IdLoc, |
3446 | IdentifierInfo *Id, EnumDecl *PrevDecl, |
3447 | bool IsScoped, bool IsScopedUsingClassTag, |
3448 | bool IsFixed); |
3449 | static EnumDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3450 | |
3451 | /// When created, the EnumDecl corresponds to a |
3452 | /// forward-declared enum. This method is used to mark the |
3453 | /// declaration as being defined; its enumerators have already been |
3454 | /// added (via DeclContext::addDecl). NewType is the new underlying |
3455 | /// type of the enumeration type. |
3456 | void completeDefinition(QualType NewType, |
3457 | QualType PromotionType, |
3458 | unsigned NumPositiveBits, |
3459 | unsigned NumNegativeBits); |
3460 | |
3461 | // Iterates through the enumerators of this enumeration. |
3462 | using enumerator_iterator = specific_decl_iterator<EnumConstantDecl>; |
3463 | using enumerator_range = |
3464 | llvm::iterator_range<specific_decl_iterator<EnumConstantDecl>>; |
3465 | |
3466 | enumerator_range enumerators() const { |
3467 | return enumerator_range(enumerator_begin(), enumerator_end()); |
3468 | } |
3469 | |
3470 | enumerator_iterator enumerator_begin() const { |
3471 | const EnumDecl *E = getDefinition(); |
3472 | if (!E) |
3473 | E = this; |
3474 | return enumerator_iterator(E->decls_begin()); |
3475 | } |
3476 | |
3477 | enumerator_iterator enumerator_end() const { |
3478 | const EnumDecl *E = getDefinition(); |
3479 | if (!E) |
3480 | E = this; |
3481 | return enumerator_iterator(E->decls_end()); |
3482 | } |
3483 | |
3484 | /// Return the integer type that enumerators should promote to. |
3485 | QualType getPromotionType() const { return PromotionType; } |
3486 | |
3487 | /// Set the promotion type. |
3488 | void setPromotionType(QualType T) { PromotionType = T; } |
3489 | |
3490 | /// Return the integer type this enum decl corresponds to. |
3491 | /// This returns a null QualType for an enum forward definition with no fixed |
3492 | /// underlying type. |
3493 | QualType getIntegerType() const { |
3494 | if (!IntegerType) |
3495 | return QualType(); |
3496 | if (const Type *T = IntegerType.dyn_cast<const Type*>()) |
3497 | return QualType(T, 0); |
3498 | return IntegerType.get<TypeSourceInfo*>()->getType().getUnqualifiedType(); |
3499 | } |
3500 | |
3501 | /// Set the underlying integer type. |
3502 | void setIntegerType(QualType T) { IntegerType = T.getTypePtrOrNull(); } |
3503 | |
3504 | /// Set the underlying integer type source info. |
3505 | void setIntegerTypeSourceInfo(TypeSourceInfo *TInfo) { IntegerType = TInfo; } |
3506 | |
3507 | /// Return the type source info for the underlying integer type, |
3508 | /// if no type source info exists, return 0. |
3509 | TypeSourceInfo *getIntegerTypeSourceInfo() const { |
3510 | return IntegerType.dyn_cast<TypeSourceInfo*>(); |
3511 | } |
3512 | |
3513 | /// Retrieve the source range that covers the underlying type if |
3514 | /// specified. |
3515 | SourceRange getIntegerTypeRange() const LLVM_READONLY__attribute__((__pure__)); |
3516 | |
3517 | /// Returns the width in bits required to store all the |
3518 | /// non-negative enumerators of this enum. |
3519 | unsigned getNumPositiveBits() const { return EnumDeclBits.NumPositiveBits; } |
3520 | |
3521 | /// Returns the width in bits required to store all the |
3522 | /// negative enumerators of this enum. These widths include |
3523 | /// the rightmost leading 1; that is: |
3524 | /// |
3525 | /// MOST NEGATIVE ENUMERATOR PATTERN NUM NEGATIVE BITS |
3526 | /// ------------------------ ------- ----------------- |
3527 | /// -1 1111111 1 |
3528 | /// -10 1110110 5 |
3529 | /// -101 1001011 8 |
3530 | unsigned getNumNegativeBits() const { return EnumDeclBits.NumNegativeBits; } |
3531 | |
3532 | /// Returns true if this is a C++11 scoped enumeration. |
3533 | bool isScoped() const { return EnumDeclBits.IsScoped; } |
3534 | |
3535 | /// Returns true if this is a C++11 scoped enumeration. |
3536 | bool isScopedUsingClassTag() const { |
3537 | return EnumDeclBits.IsScopedUsingClassTag; |
3538 | } |
3539 | |
3540 | /// Returns true if this is an Objective-C, C++11, or |
3541 | /// Microsoft-style enumeration with a fixed underlying type. |
3542 | bool isFixed() const { return EnumDeclBits.IsFixed; } |
3543 | |
3544 | unsigned getODRHash(); |
3545 | |
3546 | /// Returns true if this can be considered a complete type. |
3547 | bool isComplete() const { |
3548 | // IntegerType is set for fixed type enums and non-fixed but implicitly |
3549 | // int-sized Microsoft enums. |
3550 | return isCompleteDefinition() || IntegerType; |
3551 | } |
3552 | |
3553 | /// Returns true if this enum is either annotated with |
3554 | /// enum_extensibility(closed) or isn't annotated with enum_extensibility. |
3555 | bool isClosed() const; |
3556 | |
3557 | /// Returns true if this enum is annotated with flag_enum and isn't annotated |
3558 | /// with enum_extensibility(open). |
3559 | bool isClosedFlag() const; |
3560 | |
3561 | /// Returns true if this enum is annotated with neither flag_enum nor |
3562 | /// enum_extensibility(open). |
3563 | bool isClosedNonFlag() const; |
3564 | |
3565 | /// Retrieve the enum definition from which this enumeration could |
3566 | /// be instantiated, if it is an instantiation (rather than a non-template). |
3567 | EnumDecl *getTemplateInstantiationPattern() const; |
3568 | |
3569 | /// Returns the enumeration (declared within the template) |
3570 | /// from which this enumeration type was instantiated, or NULL if |
3571 | /// this enumeration was not instantiated from any template. |
3572 | EnumDecl *getInstantiatedFromMemberEnum() const; |
3573 | |
3574 | /// If this enumeration is a member of a specialization of a |
3575 | /// templated class, determine what kind of template specialization |
3576 | /// or instantiation this is. |
3577 | TemplateSpecializationKind getTemplateSpecializationKind() const; |
3578 | |
3579 | /// For an enumeration member that was instantiated from a member |
3580 | /// enumeration of a templated class, set the template specialiation kind. |
3581 | void setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
3582 | SourceLocation PointOfInstantiation = SourceLocation()); |
3583 | |
3584 | /// If this enumeration is an instantiation of a member enumeration of |
3585 | /// a class template specialization, retrieves the member specialization |
3586 | /// information. |
3587 | MemberSpecializationInfo *getMemberSpecializationInfo() const { |
3588 | return SpecializationInfo; |
3589 | } |
3590 | |
3591 | /// Specify that this enumeration is an instantiation of the |
3592 | /// member enumeration ED. |
3593 | void setInstantiationOfMemberEnum(EnumDecl *ED, |
3594 | TemplateSpecializationKind TSK) { |
3595 | setInstantiationOfMemberEnum(getASTContext(), ED, TSK); |
3596 | } |
3597 | |
3598 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3599 | static bool classofKind(Kind K) { return K == Enum; } |
3600 | }; |
3601 | |
3602 | /// Represents a struct/union/class. For example: |
3603 | /// struct X; // Forward declaration, no "body". |
3604 | /// union Y { int A, B; }; // Has body with members A and B (FieldDecls). |
3605 | /// This decl will be marked invalid if *any* members are invalid. |
3606 | class RecordDecl : public TagDecl { |
3607 | // This class stores some data in DeclContext::RecordDeclBits |
3608 | // to save some space. Use the provided accessors to access it. |
3609 | public: |
3610 | friend class DeclContext; |
3611 | /// Enum that represents the different ways arguments are passed to and |
3612 | /// returned from function calls. This takes into account the target-specific |
3613 | /// and version-specific rules along with the rules determined by the |
3614 | /// language. |
3615 | enum ArgPassingKind : unsigned { |
3616 | /// The argument of this type can be passed directly in registers. |
3617 | APK_CanPassInRegs, |
3618 | |
3619 | /// The argument of this type cannot be passed directly in registers. |
3620 | /// Records containing this type as a subobject are not forced to be passed |
3621 | /// indirectly. This value is used only in C++. This value is required by |
3622 | /// C++ because, in uncommon situations, it is possible for a class to have |
3623 | /// only trivial copy/move constructors even when one of its subobjects has |
3624 | /// a non-trivial copy/move constructor (if e.g. the corresponding copy/move |
3625 | /// constructor in the derived class is deleted). |
3626 | APK_CannotPassInRegs, |
3627 | |
3628 | /// The argument of this type cannot be passed directly in registers. |
3629 | /// Records containing this type as a subobject are forced to be passed |
3630 | /// indirectly. |
3631 | APK_CanNeverPassInRegs |
3632 | }; |
3633 | |
3634 | protected: |
3635 | RecordDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC, |
3636 | SourceLocation StartLoc, SourceLocation IdLoc, |
3637 | IdentifierInfo *Id, RecordDecl *PrevDecl); |
3638 | |
3639 | public: |
3640 | static RecordDecl *Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
3641 | SourceLocation StartLoc, SourceLocation IdLoc, |
3642 | IdentifierInfo *Id, RecordDecl* PrevDecl = nullptr); |
3643 | static RecordDecl *CreateDeserialized(const ASTContext &C, unsigned ID); |
3644 | |
3645 | RecordDecl *getPreviousDecl() { |
3646 | return cast_or_null<RecordDecl>( |
3647 | static_cast<TagDecl *>(this)->getPreviousDecl()); |
3648 | } |
3649 | const RecordDecl *getPreviousDecl() const { |
3650 | return const_cast<RecordDecl*>(this)->getPreviousDecl(); |
3651 | } |
3652 | |
3653 | RecordDecl *getMostRecentDecl() { |
3654 | return cast<RecordDecl>(static_cast<TagDecl *>(this)->getMostRecentDecl()); |
3655 | } |
3656 | const RecordDecl *getMostRecentDecl() const { |
3657 | return const_cast<RecordDecl*>(this)->getMostRecentDecl(); |
3658 | } |
3659 | |
3660 | bool hasFlexibleArrayMember() const { |
3661 | return RecordDeclBits.HasFlexibleArrayMember; |
3662 | } |
3663 | |
3664 | void setHasFlexibleArrayMember(bool V) { |
3665 | RecordDeclBits.HasFlexibleArrayMember = V; |
3666 | } |
3667 | |
3668 | /// Whether this is an anonymous struct or union. To be an anonymous |
3669 | /// struct or union, it must have been declared without a name and |
3670 | /// there must be no objects of this type declared, e.g., |
3671 | /// @code |
3672 | /// union { int i; float f; }; |
3673 | /// @endcode |
3674 | /// is an anonymous union but neither of the following are: |
3675 | /// @code |
3676 | /// union X { int i; float f; }; |
3677 | /// union { int i; float f; } obj; |
3678 | /// @endcode |
3679 | bool isAnonymousStructOrUnion() const { |
3680 | return RecordDeclBits.AnonymousStructOrUnion; |
3681 | } |
3682 | |
3683 | void setAnonymousStructOrUnion(bool Anon) { |
3684 | RecordDeclBits.AnonymousStructOrUnion = Anon; |
3685 | } |
3686 | |
3687 | bool hasObjectMember() const { return RecordDeclBits.HasObjectMember; } |
3688 | void setHasObjectMember(bool val) { RecordDeclBits.HasObjectMember = val; } |
3689 | |
3690 | bool hasVolatileMember() const { return RecordDeclBits.HasVolatileMember; } |
3691 | |
3692 | void setHasVolatileMember(bool val) { |
3693 | RecordDeclBits.HasVolatileMember = val; |
3694 | } |
3695 | |
3696 | bool hasLoadedFieldsFromExternalStorage() const { |
3697 | return RecordDeclBits.LoadedFieldsFromExternalStorage; |
3698 | } |
3699 | |
3700 | void setHasLoadedFieldsFromExternalStorage(bool val) const { |
3701 | RecordDeclBits.LoadedFieldsFromExternalStorage = val; |
3702 | } |
3703 | |
3704 | /// Functions to query basic properties of non-trivial C structs. |
3705 | bool isNonTrivialToPrimitiveDefaultInitialize() const { |
3706 | return RecordDeclBits.NonTrivialToPrimitiveDefaultInitialize; |
3707 | } |
3708 | |
3709 | void setNonTrivialToPrimitiveDefaultInitialize(bool V) { |
3710 | RecordDeclBits.NonTrivialToPrimitiveDefaultInitialize = V; |
3711 | } |
3712 | |
3713 | bool isNonTrivialToPrimitiveCopy() const { |
3714 | return RecordDeclBits.NonTrivialToPrimitiveCopy; |
3715 | } |
3716 | |
3717 | void setNonTrivialToPrimitiveCopy(bool V) { |
3718 | RecordDeclBits.NonTrivialToPrimitiveCopy = V; |
3719 | } |
3720 | |
3721 | bool isNonTrivialToPrimitiveDestroy() const { |
3722 | return RecordDeclBits.NonTrivialToPrimitiveDestroy; |
3723 | } |
3724 | |
3725 | void setNonTrivialToPrimitiveDestroy(bool V) { |
3726 | RecordDeclBits.NonTrivialToPrimitiveDestroy = V; |
3727 | } |
3728 | |
3729 | /// Determine whether this class can be passed in registers. In C++ mode, |
3730 | /// it must have at least one trivial, non-deleted copy or move constructor. |
3731 | /// FIXME: This should be set as part of completeDefinition. |
3732 | bool canPassInRegisters() const { |
3733 | return getArgPassingRestrictions() == APK_CanPassInRegs; |
3734 | } |
3735 | |
3736 | ArgPassingKind getArgPassingRestrictions() const { |
3737 | return static_cast<ArgPassingKind>(RecordDeclBits.ArgPassingRestrictions); |
3738 | } |
3739 | |
3740 | void setArgPassingRestrictions(ArgPassingKind Kind) { |
3741 | RecordDeclBits.ArgPassingRestrictions = Kind; |
3742 | } |
3743 | |
3744 | bool isParamDestroyedInCallee() const { |
3745 | return RecordDeclBits.ParamDestroyedInCallee; |
3746 | } |
3747 | |
3748 | void setParamDestroyedInCallee(bool V) { |
3749 | RecordDeclBits.ParamDestroyedInCallee = V; |
3750 | } |
3751 | |
3752 | /// Determines whether this declaration represents the |
3753 | /// injected class name. |
3754 | /// |
3755 | /// The injected class name in C++ is the name of the class that |
3756 | /// appears inside the class itself. For example: |
3757 | /// |
3758 | /// \code |
3759 | /// struct C { |
3760 | /// // C is implicitly declared here as a synonym for the class name. |
3761 | /// }; |
3762 | /// |
3763 | /// C::C c; // same as "C c;" |
3764 | /// \endcode |
3765 | bool isInjectedClassName() const; |
3766 | |
3767 | /// Determine whether this record is a class describing a lambda |
3768 | /// function object. |
3769 | bool isLambda() const; |
3770 | |
3771 | /// Determine whether this record is a record for captured variables in |
3772 | /// CapturedStmt construct. |
3773 | bool isCapturedRecord() const; |
3774 | |
3775 | /// Mark the record as a record for captured variables in CapturedStmt |
3776 | /// construct. |
3777 | void setCapturedRecord(); |
3778 | |
3779 | /// Returns the RecordDecl that actually defines |
3780 | /// this struct/union/class. When determining whether or not a |
3781 | /// struct/union/class is completely defined, one should use this |
3782 | /// method as opposed to 'isCompleteDefinition'. |
3783 | /// 'isCompleteDefinition' indicates whether or not a specific |
3784 | /// RecordDecl is a completed definition, not whether or not the |
3785 | /// record type is defined. This method returns NULL if there is |
3786 | /// no RecordDecl that defines the struct/union/tag. |
3787 | RecordDecl *getDefinition() const { |
3788 | return cast_or_null<RecordDecl>(TagDecl::getDefinition()); |
3789 | } |
3790 | |
3791 | // Iterator access to field members. The field iterator only visits |
3792 | // the non-static data members of this class, ignoring any static |
3793 | // data members, functions, constructors, destructors, etc. |
3794 | using field_iterator = specific_decl_iterator<FieldDecl>; |
3795 | using field_range = llvm::iterator_range<specific_decl_iterator<FieldDecl>>; |
3796 | |
3797 | field_range fields() const { return field_range(field_begin(), field_end()); } |
3798 | field_iterator field_begin() const; |
3799 | |
3800 | field_iterator field_end() const { |
3801 | return field_iterator(decl_iterator()); |
3802 | } |
3803 | |
3804 | // Whether there are any fields (non-static data members) in this record. |
3805 | bool field_empty() const { |
3806 | return field_begin() == field_end(); |
3807 | } |
3808 | |
3809 | /// Note that the definition of this type is now complete. |
3810 | virtual void completeDefinition(); |
3811 | |
3812 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3813 | static bool classofKind(Kind K) { |
3814 | return K >= firstRecord && K <= lastRecord; |
3815 | } |
3816 | |
3817 | /// Get whether or not this is an ms_struct which can |
3818 | /// be turned on with an attribute, pragma, or -mms-bitfields |
3819 | /// commandline option. |
3820 | bool isMsStruct(const ASTContext &C) const; |
3821 | |
3822 | /// Whether we are allowed to insert extra padding between fields. |
3823 | /// These padding are added to help AddressSanitizer detect |
3824 | /// intra-object-overflow bugs. |
3825 | bool mayInsertExtraPadding(bool EmitRemark = false) const; |
3826 | |
3827 | /// Finds the first data member which has a name. |
3828 | /// nullptr is returned if no named data member exists. |
3829 | const FieldDecl *findFirstNamedDataMember() const; |
3830 | |
3831 | private: |
3832 | /// Deserialize just the fields. |
3833 | void LoadFieldsFromExternalStorage() const; |
3834 | }; |
3835 | |
3836 | class FileScopeAsmDecl : public Decl { |
3837 | StringLiteral *AsmString; |
3838 | SourceLocation RParenLoc; |
3839 | |
3840 | FileScopeAsmDecl(DeclContext *DC, StringLiteral *asmstring, |
3841 | SourceLocation StartL, SourceLocation EndL) |
3842 | : Decl(FileScopeAsm, DC, StartL), AsmString(asmstring), RParenLoc(EndL) {} |
3843 | |
3844 | virtual void anchor(); |
3845 | |
3846 | public: |
3847 | static FileScopeAsmDecl *Create(ASTContext &C, DeclContext *DC, |
3848 | StringLiteral *Str, SourceLocation AsmLoc, |
3849 | SourceLocation RParenLoc); |
3850 | |
3851 | static FileScopeAsmDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3852 | |
3853 | SourceLocation getAsmLoc() const { return getLocation(); } |
3854 | SourceLocation getRParenLoc() const { return RParenLoc; } |
3855 | void setRParenLoc(SourceLocation L) { RParenLoc = L; } |
3856 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) { |
3857 | return SourceRange(getAsmLoc(), getRParenLoc()); |
3858 | } |
3859 | |
3860 | const StringLiteral *getAsmString() const { return AsmString; } |
3861 | StringLiteral *getAsmString() { return AsmString; } |
3862 | void setAsmString(StringLiteral *Asm) { AsmString = Asm; } |
3863 | |
3864 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3865 | static bool classofKind(Kind K) { return K == FileScopeAsm; } |
3866 | }; |
3867 | |
3868 | /// Represents a block literal declaration, which is like an |
3869 | /// unnamed FunctionDecl. For example: |
3870 | /// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } |
3871 | class BlockDecl : public Decl, public DeclContext { |
3872 | // This class stores some data in DeclContext::BlockDeclBits |
3873 | // to save some space. Use the provided accessors to access it. |
3874 | public: |
3875 | /// A class which contains all the information about a particular |
3876 | /// captured value. |
3877 | class Capture { |
3878 | enum { |
3879 | flag_isByRef = 0x1, |
3880 | flag_isNested = 0x2 |
3881 | }; |
3882 | |
3883 | /// The variable being captured. |
3884 | llvm::PointerIntPair<VarDecl*, 2> VariableAndFlags; |
3885 | |
3886 | /// The copy expression, expressed in terms of a DeclRef (or |
3887 | /// BlockDeclRef) to the captured variable. Only required if the |
3888 | /// variable has a C++ class type. |
3889 | Expr *CopyExpr; |
3890 | |
3891 | public: |
3892 | Capture(VarDecl *variable, bool byRef, bool nested, Expr *copy) |
3893 | : VariableAndFlags(variable, |
3894 | (byRef ? flag_isByRef : 0) | (nested ? flag_isNested : 0)), |
3895 | CopyExpr(copy) {} |
3896 | |
3897 | /// The variable being captured. |
3898 | VarDecl *getVariable() const { return VariableAndFlags.getPointer(); } |
3899 | |
3900 | /// Whether this is a "by ref" capture, i.e. a capture of a __block |
3901 | /// variable. |
3902 | bool isByRef() const { return VariableAndFlags.getInt() & flag_isByRef; } |
3903 | |
3904 | bool isEscapingByref() const { |
3905 | return getVariable()->isEscapingByref(); |
3906 | } |
3907 | |
3908 | bool isNonEscapingByref() const { |
3909 | return getVariable()->isNonEscapingByref(); |
3910 | } |
3911 | |
3912 | /// Whether this is a nested capture, i.e. the variable captured |
3913 | /// is not from outside the immediately enclosing function/block. |
3914 | bool isNested() const { return VariableAndFlags.getInt() & flag_isNested; } |
3915 | |
3916 | bool hasCopyExpr() const { return CopyExpr != nullptr; } |
3917 | Expr *getCopyExpr() const { return CopyExpr; } |
3918 | void setCopyExpr(Expr *e) { CopyExpr = e; } |
3919 | }; |
3920 | |
3921 | private: |
3922 | /// A new[]'d array of pointers to ParmVarDecls for the formal |
3923 | /// parameters of this function. This is null if a prototype or if there are |
3924 | /// no formals. |
3925 | ParmVarDecl **ParamInfo = nullptr; |
3926 | unsigned NumParams = 0; |
3927 | |
3928 | Stmt *Body = nullptr; |
3929 | TypeSourceInfo *SignatureAsWritten = nullptr; |
3930 | |
3931 | const Capture *Captures = nullptr; |
3932 | unsigned NumCaptures = 0; |
3933 | |
3934 | unsigned ManglingNumber = 0; |
3935 | Decl *ManglingContextDecl = nullptr; |
3936 | |
3937 | protected: |
3938 | BlockDecl(DeclContext *DC, SourceLocation CaretLoc); |
3939 | |
3940 | public: |
3941 | static BlockDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L); |
3942 | static BlockDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3943 | |
3944 | SourceLocation getCaretLocation() const { return getLocation(); } |
3945 | |
3946 | bool isVariadic() const { return BlockDeclBits.IsVariadic; } |
3947 | void setIsVariadic(bool value) { BlockDeclBits.IsVariadic = value; } |
3948 | |
3949 | CompoundStmt *getCompoundBody() const { return (CompoundStmt*) Body; } |
3950 | Stmt *getBody() const override { return (Stmt*) Body; } |
3951 | void setBody(CompoundStmt *B) { Body = (Stmt*) B; } |
3952 | |
3953 | void setSignatureAsWritten(TypeSourceInfo *Sig) { SignatureAsWritten = Sig; } |
3954 | TypeSourceInfo *getSignatureAsWritten() const { return SignatureAsWritten; } |
3955 | |
3956 | // ArrayRef access to formal parameters. |
3957 | ArrayRef<ParmVarDecl *> parameters() const { |
3958 | return {ParamInfo, getNumParams()}; |
3959 | } |
3960 | MutableArrayRef<ParmVarDecl *> parameters() { |
3961 | return {ParamInfo, getNumParams()}; |
3962 | } |
3963 | |
3964 | // Iterator access to formal parameters. |
3965 | using param_iterator = MutableArrayRef<ParmVarDecl *>::iterator; |
3966 | using param_const_iterator = ArrayRef<ParmVarDecl *>::const_iterator; |
3967 | |
3968 | bool param_empty() const { return parameters().empty(); } |
3969 | param_iterator param_begin() { return parameters().begin(); } |
3970 | param_iterator param_end() { return parameters().end(); } |
3971 | param_const_iterator param_begin() const { return parameters().begin(); } |
3972 | param_const_iterator param_end() const { return parameters().end(); } |
3973 | size_t param_size() const { return parameters().size(); } |
3974 | |
3975 | unsigned getNumParams() const { return NumParams; } |
3976 | |
3977 | const ParmVarDecl *getParamDecl(unsigned i) const { |
3978 | assert(i < getNumParams() && "Illegal param #")((i < getNumParams() && "Illegal param #") ? static_cast <void> (0) : __assert_fail ("i < getNumParams() && \"Illegal param #\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 3978, __PRETTY_FUNCTION__)); |
3979 | return ParamInfo[i]; |
3980 | } |
3981 | ParmVarDecl *getParamDecl(unsigned i) { |
3982 | assert(i < getNumParams() && "Illegal param #")((i < getNumParams() && "Illegal param #") ? static_cast <void> (0) : __assert_fail ("i < getNumParams() && \"Illegal param #\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 3982, __PRETTY_FUNCTION__)); |
3983 | return ParamInfo[i]; |
3984 | } |
3985 | |
3986 | void setParams(ArrayRef<ParmVarDecl *> NewParamInfo); |
3987 | |
3988 | /// True if this block (or its nested blocks) captures |
3989 | /// anything of local storage from its enclosing scopes. |
3990 | bool hasCaptures() const { return NumCaptures || capturesCXXThis(); } |
3991 | |
3992 | /// Returns the number of captured variables. |
3993 | /// Does not include an entry for 'this'. |
3994 | unsigned getNumCaptures() const { return NumCaptures; } |
3995 | |
3996 | using capture_const_iterator = ArrayRef<Capture>::const_iterator; |
3997 | |
3998 | ArrayRef<Capture> captures() const { return {Captures, NumCaptures}; } |
3999 | |
4000 | capture_const_iterator capture_begin() const { return captures().begin(); } |
4001 | capture_const_iterator capture_end() const { return captures().end(); } |
4002 | |
4003 | bool capturesCXXThis() const { return BlockDeclBits.CapturesCXXThis; } |
4004 | void setCapturesCXXThis(bool B = true) { BlockDeclBits.CapturesCXXThis = B; } |
4005 | |
4006 | bool blockMissingReturnType() const { |
4007 | return BlockDeclBits.BlockMissingReturnType; |
4008 | } |
4009 | |
4010 | void setBlockMissingReturnType(bool val = true) { |
4011 | BlockDeclBits.BlockMissingReturnType = val; |
4012 | } |
4013 | |
4014 | bool isConversionFromLambda() const { |
4015 | return BlockDeclBits.IsConversionFromLambda; |
4016 | } |
4017 | |
4018 | void setIsConversionFromLambda(bool val = true) { |
4019 | BlockDeclBits.IsConversionFromLambda = val; |
4020 | } |
4021 | |
4022 | bool doesNotEscape() const { return BlockDeclBits.DoesNotEscape; } |
4023 | void setDoesNotEscape(bool B = true) { BlockDeclBits.DoesNotEscape = B; } |
4024 | |
4025 | bool canAvoidCopyToHeap() const { |
4026 | return BlockDeclBits.CanAvoidCopyToHeap; |
4027 | } |
4028 | void setCanAvoidCopyToHeap(bool B = true) { |
4029 | BlockDeclBits.CanAvoidCopyToHeap = B; |
4030 | } |
4031 | |
4032 | bool capturesVariable(const VarDecl *var) const; |
4033 | |
4034 | void setCaptures(ASTContext &Context, ArrayRef<Capture> Captures, |
4035 | bool CapturesCXXThis); |
4036 | |
4037 | unsigned getBlockManglingNumber() const { |
4038 | return ManglingNumber; |
4039 | } |
4040 | |
4041 | Decl *getBlockManglingContextDecl() const { |
4042 | return ManglingContextDecl; |
4043 | } |
4044 | |
4045 | void setBlockMangling(unsigned Number, Decl *Ctx) { |
4046 | ManglingNumber = Number; |
4047 | ManglingContextDecl = Ctx; |
4048 | } |
4049 | |
4050 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
4051 | |
4052 | // Implement isa/cast/dyncast/etc. |
4053 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4054 | static bool classofKind(Kind K) { return K == Block; } |
4055 | static DeclContext *castToDeclContext(const BlockDecl *D) { |
4056 | return static_cast<DeclContext *>(const_cast<BlockDecl*>(D)); |
4057 | } |
4058 | static BlockDecl *castFromDeclContext(const DeclContext *DC) { |
4059 | return static_cast<BlockDecl *>(const_cast<DeclContext*>(DC)); |
4060 | } |
4061 | }; |
4062 | |
4063 | /// Represents the body of a CapturedStmt, and serves as its DeclContext. |
4064 | class CapturedDecl final |
4065 | : public Decl, |
4066 | public DeclContext, |
4067 | private llvm::TrailingObjects<CapturedDecl, ImplicitParamDecl *> { |
4068 | protected: |
4069 | size_t numTrailingObjects(OverloadToken<ImplicitParamDecl>) { |
4070 | return NumParams; |
4071 | } |
4072 | |
4073 | private: |
4074 | /// The number of parameters to the outlined function. |
4075 | unsigned NumParams; |
4076 | |
4077 | /// The position of context parameter in list of parameters. |
4078 | unsigned ContextParam; |
4079 | |
4080 | /// The body of the outlined function. |
4081 | llvm::PointerIntPair<Stmt *, 1, bool> BodyAndNothrow; |
4082 | |
4083 | explicit CapturedDecl(DeclContext *DC, unsigned NumParams); |
4084 | |
4085 | ImplicitParamDecl *const *getParams() const { |
4086 | return getTrailingObjects<ImplicitParamDecl *>(); |
4087 | } |
4088 | |
4089 | ImplicitParamDecl **getParams() { |
4090 | return getTrailingObjects<ImplicitParamDecl *>(); |
4091 | } |
4092 | |
4093 | public: |
4094 | friend class ASTDeclReader; |
4095 | friend class ASTDeclWriter; |
4096 | friend TrailingObjects; |
4097 | |
4098 | static CapturedDecl *Create(ASTContext &C, DeclContext *DC, |
4099 | unsigned NumParams); |
4100 | static CapturedDecl *CreateDeserialized(ASTContext &C, unsigned ID, |
4101 | unsigned NumParams); |
4102 | |
4103 | Stmt *getBody() const override; |
4104 | void setBody(Stmt *B); |
4105 | |
4106 | bool isNothrow() const; |
4107 | void setNothrow(bool Nothrow = true); |
4108 | |
4109 | unsigned getNumParams() const { return NumParams; } |
4110 | |
4111 | ImplicitParamDecl *getParam(unsigned i) const { |
4112 | assert(i < NumParams)((i < NumParams) ? static_cast<void> (0) : __assert_fail ("i < NumParams", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 4112, __PRETTY_FUNCTION__)); |
4113 | return getParams()[i]; |
4114 | } |
4115 | void setParam(unsigned i, ImplicitParamDecl *P) { |
4116 | assert(i < NumParams)((i < NumParams) ? static_cast<void> (0) : __assert_fail ("i < NumParams", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 4116, __PRETTY_FUNCTION__)); |
4117 | getParams()[i] = P; |
4118 | } |
4119 | |
4120 | // ArrayRef interface to parameters. |
4121 | ArrayRef<ImplicitParamDecl *> parameters() const { |
4122 | return {getParams(), getNumParams()}; |
4123 | } |
4124 | MutableArrayRef<ImplicitParamDecl *> parameters() { |
4125 | return {getParams(), getNumParams()}; |
4126 | } |
4127 | |
4128 | /// Retrieve the parameter containing captured variables. |
4129 | ImplicitParamDecl *getContextParam() const { |
4130 | assert(ContextParam < NumParams)((ContextParam < NumParams) ? static_cast<void> (0) : __assert_fail ("ContextParam < NumParams", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 4130, __PRETTY_FUNCTION__)); |
4131 | return getParam(ContextParam); |
4132 | } |
4133 | void setContextParam(unsigned i, ImplicitParamDecl *P) { |
4134 | assert(i < NumParams)((i < NumParams) ? static_cast<void> (0) : __assert_fail ("i < NumParams", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 4134, __PRETTY_FUNCTION__)); |
4135 | ContextParam = i; |
4136 | setParam(i, P); |
4137 | } |
4138 | unsigned getContextParamPosition() const { return ContextParam; } |
4139 | |
4140 | using param_iterator = ImplicitParamDecl *const *; |
4141 | using param_range = llvm::iterator_range<param_iterator>; |
4142 | |
4143 | /// Retrieve an iterator pointing to the first parameter decl. |
4144 | param_iterator param_begin() const { return getParams(); } |
4145 | /// Retrieve an iterator one past the last parameter decl. |
4146 | param_iterator param_end() const { return getParams() + NumParams; } |
4147 | |
4148 | // Implement isa/cast/dyncast/etc. |
4149 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4150 | static bool classofKind(Kind K) { return K == Captured; } |
4151 | static DeclContext *castToDeclContext(const CapturedDecl *D) { |
4152 | return static_cast<DeclContext *>(const_cast<CapturedDecl *>(D)); |
4153 | } |
4154 | static CapturedDecl *castFromDeclContext(const DeclContext *DC) { |
4155 | return static_cast<CapturedDecl *>(const_cast<DeclContext *>(DC)); |
4156 | } |
4157 | }; |
4158 | |
4159 | /// Describes a module import declaration, which makes the contents |
4160 | /// of the named module visible in the current translation unit. |
4161 | /// |
4162 | /// An import declaration imports the named module (or submodule). For example: |
4163 | /// \code |
4164 | /// @import std.vector; |
4165 | /// \endcode |
4166 | /// |
4167 | /// Import declarations can also be implicitly generated from |
4168 | /// \#include/\#import directives. |
4169 | class ImportDecl final : public Decl, |
4170 | llvm::TrailingObjects<ImportDecl, SourceLocation> { |
4171 | friend class ASTContext; |
4172 | friend class ASTDeclReader; |
4173 | friend class ASTReader; |
4174 | friend TrailingObjects; |
4175 | |
4176 | /// The imported module, along with a bit that indicates whether |
4177 | /// we have source-location information for each identifier in the module |
4178 | /// name. |
4179 | /// |
4180 | /// When the bit is false, we only have a single source location for the |
4181 | /// end of the import declaration. |
4182 | llvm::PointerIntPair<Module *, 1, bool> ImportedAndComplete; |
4183 | |
4184 | /// The next import in the list of imports local to the translation |
4185 | /// unit being parsed (not loaded from an AST file). |
4186 | ImportDecl *NextLocalImport = nullptr; |
4187 | |
4188 | ImportDecl(DeclContext *DC, SourceLocation StartLoc, Module *Imported, |
4189 | ArrayRef<SourceLocation> IdentifierLocs); |
4190 | |
4191 | ImportDecl(DeclContext *DC, SourceLocation StartLoc, Module *Imported, |
4192 | SourceLocation EndLoc); |
4193 | |
4194 | ImportDecl(EmptyShell Empty) : Decl(Import, Empty) {} |
4195 | |
4196 | public: |
4197 | /// Create a new module import declaration. |
4198 | static ImportDecl *Create(ASTContext &C, DeclContext *DC, |
4199 | SourceLocation StartLoc, Module *Imported, |
4200 | ArrayRef<SourceLocation> IdentifierLocs); |
4201 | |
4202 | /// Create a new module import declaration for an implicitly-generated |
4203 | /// import. |
4204 | static ImportDecl *CreateImplicit(ASTContext &C, DeclContext *DC, |
4205 | SourceLocation StartLoc, Module *Imported, |
4206 | SourceLocation EndLoc); |
4207 | |
4208 | /// Create a new, deserialized module import declaration. |
4209 | static ImportDecl *CreateDeserialized(ASTContext &C, unsigned ID, |
4210 | unsigned NumLocations); |
4211 | |
4212 | /// Retrieve the module that was imported by the import declaration. |
4213 | Module *getImportedModule() const { return ImportedAndComplete.getPointer(); } |
4214 | |
4215 | /// Retrieves the locations of each of the identifiers that make up |
4216 | /// the complete module name in the import declaration. |
4217 | /// |
4218 | /// This will return an empty array if the locations of the individual |
4219 | /// identifiers aren't available. |
4220 | ArrayRef<SourceLocation> getIdentifierLocs() const; |
4221 | |
4222 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
4223 | |
4224 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4225 | static bool classofKind(Kind K) { return K == Import; } |
4226 | }; |
4227 | |
4228 | /// Represents a C++ Modules TS module export declaration. |
4229 | /// |
4230 | /// For example: |
4231 | /// \code |
4232 | /// export void foo(); |
4233 | /// \endcode |
4234 | class ExportDecl final : public Decl, public DeclContext { |
4235 | virtual void anchor(); |
4236 | |
4237 | private: |
4238 | friend class ASTDeclReader; |
4239 | |
4240 | /// The source location for the right brace (if valid). |
4241 | SourceLocation RBraceLoc; |
4242 | |
4243 | ExportDecl(DeclContext *DC, SourceLocation ExportLoc) |
4244 | : Decl(Export, DC, ExportLoc), DeclContext(Export), |
4245 | RBraceLoc(SourceLocation()) {} |
4246 | |
4247 | public: |
4248 | static ExportDecl *Create(ASTContext &C, DeclContext *DC, |
4249 | SourceLocation ExportLoc); |
4250 | static ExportDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
4251 | |
4252 | SourceLocation getExportLoc() const { return getLocation(); } |
4253 | SourceLocation getRBraceLoc() const { return RBraceLoc; } |
4254 | void setRBraceLoc(SourceLocation L) { RBraceLoc = L; } |
4255 | |
4256 | bool hasBraces() const { return RBraceLoc.isValid(); } |
4257 | |
4258 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { |
4259 | if (hasBraces()) |
4260 | return RBraceLoc; |
4261 | // No braces: get the end location of the (only) declaration in context |
4262 | // (if present). |
4263 | return decls_empty() ? getLocation() : decls_begin()->getEndLoc(); |
4264 | } |
4265 | |
4266 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) { |
4267 | return SourceRange(getLocation(), getEndLoc()); |
4268 | } |
4269 | |
4270 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4271 | static bool classofKind(Kind K) { return K == Export; } |
4272 | static DeclContext *castToDeclContext(const ExportDecl *D) { |
4273 | return static_cast<DeclContext *>(const_cast<ExportDecl*>(D)); |
4274 | } |
4275 | static ExportDecl *castFromDeclContext(const DeclContext *DC) { |
4276 | return static_cast<ExportDecl *>(const_cast<DeclContext*>(DC)); |
4277 | } |
4278 | }; |
4279 | |
4280 | /// Represents an empty-declaration. |
4281 | class EmptyDecl : public Decl { |
4282 | EmptyDecl(DeclContext *DC, SourceLocation L) : Decl(Empty, DC, L) {} |
4283 | |
4284 | virtual void anchor(); |
4285 | |
4286 | public: |
4287 | static EmptyDecl *Create(ASTContext &C, DeclContext *DC, |
4288 | SourceLocation L); |
4289 | static EmptyDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
4290 | |
4291 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4292 | static bool classofKind(Kind K) { return K == Empty; } |
4293 | }; |
4294 | |
4295 | /// Insertion operator for diagnostics. This allows sending NamedDecl's |
4296 | /// into a diagnostic with <<. |
4297 | inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, |
4298 | const NamedDecl* ND) { |
4299 | DB.AddTaggedVal(reinterpret_cast<intptr_t>(ND), |
4300 | DiagnosticsEngine::ak_nameddecl); |
4301 | return DB; |
4302 | } |
4303 | inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD, |
4304 | const NamedDecl* ND) { |
4305 | PD.AddTaggedVal(reinterpret_cast<intptr_t>(ND), |
4306 | DiagnosticsEngine::ak_nameddecl); |
4307 | return PD; |
4308 | } |
4309 | |
4310 | template<typename decl_type> |
4311 | void Redeclarable<decl_type>::setPreviousDecl(decl_type *PrevDecl) { |
4312 | // Note: This routine is implemented here because we need both NamedDecl |
4313 | // and Redeclarable to be defined. |
4314 | assert(RedeclLink.isFirst() &&((RedeclLink.isFirst() && "setPreviousDecl on a decl already in a redeclaration chain" ) ? static_cast<void> (0) : __assert_fail ("RedeclLink.isFirst() && \"setPreviousDecl on a decl already in a redeclaration chain\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 4315, __PRETTY_FUNCTION__)) |
4315 | "setPreviousDecl on a decl already in a redeclaration chain")((RedeclLink.isFirst() && "setPreviousDecl on a decl already in a redeclaration chain" ) ? static_cast<void> (0) : __assert_fail ("RedeclLink.isFirst() && \"setPreviousDecl on a decl already in a redeclaration chain\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 4315, __PRETTY_FUNCTION__)); |
4316 | |
4317 | if (PrevDecl) { |
4318 | // Point to previous. Make sure that this is actually the most recent |
4319 | // redeclaration, or we can build invalid chains. If the most recent |
4320 | // redeclaration is invalid, it won't be PrevDecl, but we want it anyway. |
4321 | First = PrevDecl->getFirstDecl(); |
4322 | assert(First->RedeclLink.isFirst() && "Expected first")((First->RedeclLink.isFirst() && "Expected first") ? static_cast<void> (0) : __assert_fail ("First->RedeclLink.isFirst() && \"Expected first\"" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 4322, __PRETTY_FUNCTION__)); |
4323 | decl_type *MostRecent = First->getNextRedeclaration(); |
4324 | RedeclLink = PreviousDeclLink(cast<decl_type>(MostRecent)); |
4325 | |
4326 | // If the declaration was previously visible, a redeclaration of it remains |
4327 | // visible even if it wouldn't be visible by itself. |
4328 | static_cast<decl_type*>(this)->IdentifierNamespace |= |
4329 | MostRecent->getIdentifierNamespace() & |
4330 | (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type); |
4331 | } else { |
4332 | // Make this first. |
4333 | First = static_cast<decl_type*>(this); |
4334 | } |
4335 | |
4336 | // First one will point to this one as latest. |
4337 | First->RedeclLink.setLatest(static_cast<decl_type*>(this)); |
4338 | |
4339 | assert(!isa<NamedDecl>(static_cast<decl_type*>(this)) ||((!isa<NamedDecl>(static_cast<decl_type*>(this)) || cast<NamedDecl>(static_cast<decl_type*>(this))-> isLinkageValid()) ? static_cast<void> (0) : __assert_fail ("!isa<NamedDecl>(static_cast<decl_type*>(this)) || cast<NamedDecl>(static_cast<decl_type*>(this))->isLinkageValid()" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 4340, __PRETTY_FUNCTION__)) |
4340 | cast<NamedDecl>(static_cast<decl_type*>(this))->isLinkageValid())((!isa<NamedDecl>(static_cast<decl_type*>(this)) || cast<NamedDecl>(static_cast<decl_type*>(this))-> isLinkageValid()) ? static_cast<void> (0) : __assert_fail ("!isa<NamedDecl>(static_cast<decl_type*>(this)) || cast<NamedDecl>(static_cast<decl_type*>(this))->isLinkageValid()" , "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/Decl.h" , 4340, __PRETTY_FUNCTION__)); |
4341 | } |
4342 | |
4343 | // Inline function definitions. |
4344 | |
4345 | /// Check if the given decl is complete. |
4346 | /// |
4347 | /// We use this function to break a cycle between the inline definitions in |
4348 | /// Type.h and Decl.h. |
4349 | inline bool IsEnumDeclComplete(EnumDecl *ED) { |
4350 | return ED->isComplete(); |
4351 | } |
4352 | |
4353 | /// Check if the given decl is scoped. |
4354 | /// |
4355 | /// We use this function to break a cycle between the inline definitions in |
4356 | /// Type.h and Decl.h. |
4357 | inline bool IsEnumDeclScoped(EnumDecl *ED) { |
4358 | return ED->isScoped(); |
4359 | } |
4360 | |
4361 | } // namespace clang |
4362 | |
4363 | #endif // LLVM_CLANG_AST_DECL_H |