blob: 23286b2320a8102bc76da01e3b9d3e0598cc247d [file] [log] [blame]
Ted Kremenek14f779c2012-09-21 00:09:11 +00001//== BodyFarm.cpp - Factory for conjuring up fake bodies ----------*- C++ -*-//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// BodyFarm is a factory for creating faux implementations for functions/methods
11// for analysis purposes.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek14f779c2012-09-21 00:09:11 +000015#include "BodyFarm.h"
Ted Kremenekeeccb302014-08-27 15:14:15 +000016#include "clang/Analysis/CodeInjector.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/ExprObjC.h"
21#include "llvm/ADT/StringSwitch.h"
Ted Kremenek14f779c2012-09-21 00:09:11 +000022
23using namespace clang;
24
Ted Kremenek2b5c83c2012-09-21 17:54:32 +000025//===----------------------------------------------------------------------===//
26// Helper creation functions for constructing faux ASTs.
27//===----------------------------------------------------------------------===//
Ted Kremenek14f779c2012-09-21 00:09:11 +000028
Ted Kremenekd81a4a12012-09-21 00:52:24 +000029static bool isDispatchBlock(QualType Ty) {
30 // Is it a block pointer?
31 const BlockPointerType *BPT = Ty->getAs<BlockPointerType>();
32 if (!BPT)
33 return false;
34
35 // Check if the block pointer type takes no arguments and
36 // returns void.
37 const FunctionProtoType *FT =
38 BPT->getPointeeType()->getAs<FunctionProtoType>();
Alp Toker314cc812014-01-25 16:55:45 +000039 if (!FT || !FT->getReturnType()->isVoidType() || FT->getNumParams() != 0)
Ted Kremenekd81a4a12012-09-21 00:52:24 +000040 return false;
41
42 return true;
43}
44
Ted Kremenek72418132012-09-21 17:54:35 +000045namespace {
46class ASTMaker {
47public:
48 ASTMaker(ASTContext &C) : C(C) {}
49
Ted Kremenekf465dc12012-09-21 18:33:54 +000050 /// Create a new BinaryOperator representing a simple assignment.
51 BinaryOperator *makeAssignment(const Expr *LHS, const Expr *RHS, QualType Ty);
52
Ted Kremenek089ffd02012-10-11 20:58:18 +000053 /// Create a new BinaryOperator representing a comparison.
54 BinaryOperator *makeComparison(const Expr *LHS, const Expr *RHS,
55 BinaryOperator::Opcode Op);
56
57 /// Create a new compound stmt using the provided statements.
58 CompoundStmt *makeCompound(ArrayRef<Stmt*>);
59
Ted Kremenek69bcb822012-09-21 18:13:23 +000060 /// Create a new DeclRefExpr for the referenced variable.
Ted Kremenek72418132012-09-21 17:54:35 +000061 DeclRefExpr *makeDeclRefExpr(const VarDecl *D);
62
Ted Kremenekdff35532012-09-21 18:33:52 +000063 /// Create a new UnaryOperator representing a dereference.
64 UnaryOperator *makeDereference(const Expr *Arg, QualType Ty);
65
Ted Kremenek69bcb822012-09-21 18:13:23 +000066 /// Create an implicit cast for an integer conversion.
Ted Kremenek6fdefb52012-10-12 00:18:19 +000067 Expr *makeIntegralCast(const Expr *Arg, QualType Ty);
Ted Kremenek69bcb822012-09-21 18:13:23 +000068
Ted Kremenek089ffd02012-10-11 20:58:18 +000069 /// Create an implicit cast to a builtin boolean type.
70 ImplicitCastExpr *makeIntegralCastToBoolean(const Expr *Arg);
71
Ted Kremenekca90ea52012-09-21 18:13:27 +000072 // Create an implicit cast for lvalue-to-rvaluate conversions.
73 ImplicitCastExpr *makeLvalueToRvalue(const Expr *Arg, QualType Ty);
74
Ted Kremenek089ffd02012-10-11 20:58:18 +000075 /// Create an Objective-C bool literal.
76 ObjCBoolLiteralExpr *makeObjCBool(bool Val);
Jordan Rose1a866cd2014-01-10 20:06:06 +000077
78 /// Create an Objective-C ivar reference.
79 ObjCIvarRefExpr *makeObjCIvarRef(const Expr *Base, const ObjCIvarDecl *IVar);
Ted Kremenek089ffd02012-10-11 20:58:18 +000080
81 /// Create a Return statement.
82 ReturnStmt *makeReturn(const Expr *RetVal);
83
Ted Kremenek72418132012-09-21 17:54:35 +000084private:
85 ASTContext &C;
86};
87}
88
Ted Kremenekf465dc12012-09-21 18:33:54 +000089BinaryOperator *ASTMaker::makeAssignment(const Expr *LHS, const Expr *RHS,
90 QualType Ty) {
91 return new (C) BinaryOperator(const_cast<Expr*>(LHS), const_cast<Expr*>(RHS),
92 BO_Assign, Ty, VK_RValue,
Lang Hames5de91cc2012-10-02 04:45:10 +000093 OK_Ordinary, SourceLocation(), false);
Ted Kremenekf465dc12012-09-21 18:33:54 +000094}
95
Ted Kremenek089ffd02012-10-11 20:58:18 +000096BinaryOperator *ASTMaker::makeComparison(const Expr *LHS, const Expr *RHS,
97 BinaryOperator::Opcode Op) {
98 assert(BinaryOperator::isLogicalOp(Op) ||
99 BinaryOperator::isComparisonOp(Op));
100 return new (C) BinaryOperator(const_cast<Expr*>(LHS),
101 const_cast<Expr*>(RHS),
102 Op,
103 C.getLogicalOperationType(),
104 VK_RValue,
105 OK_Ordinary, SourceLocation(), false);
106}
107
108CompoundStmt *ASTMaker::makeCompound(ArrayRef<Stmt *> Stmts) {
Nico Webera2a0eb92012-12-29 20:03:39 +0000109 return new (C) CompoundStmt(C, Stmts, SourceLocation(), SourceLocation());
Ted Kremenek089ffd02012-10-11 20:58:18 +0000110}
111
Ted Kremenek72418132012-09-21 17:54:35 +0000112DeclRefExpr *ASTMaker::makeDeclRefExpr(const VarDecl *D) {
113 DeclRefExpr *DR =
114 DeclRefExpr::Create(/* Ctx = */ C,
115 /* QualifierLoc = */ NestedNameSpecifierLoc(),
116 /* TemplateKWLoc = */ SourceLocation(),
117 /* D = */ const_cast<VarDecl*>(D),
118 /* isEnclosingLocal = */ false,
119 /* NameLoc = */ SourceLocation(),
120 /* T = */ D->getType(),
121 /* VK = */ VK_LValue);
122 return DR;
123}
124
Ted Kremenekdff35532012-09-21 18:33:52 +0000125UnaryOperator *ASTMaker::makeDereference(const Expr *Arg, QualType Ty) {
126 return new (C) UnaryOperator(const_cast<Expr*>(Arg), UO_Deref, Ty,
127 VK_LValue, OK_Ordinary, SourceLocation());
128}
129
Ted Kremenekca90ea52012-09-21 18:13:27 +0000130ImplicitCastExpr *ASTMaker::makeLvalueToRvalue(const Expr *Arg, QualType Ty) {
131 return ImplicitCastExpr::Create(C, Ty, CK_LValueToRValue,
Craig Topper25542942014-05-20 04:30:07 +0000132 const_cast<Expr*>(Arg), nullptr, VK_RValue);
Ted Kremenekca90ea52012-09-21 18:13:27 +0000133}
134
Ted Kremenek6fdefb52012-10-12 00:18:19 +0000135Expr *ASTMaker::makeIntegralCast(const Expr *Arg, QualType Ty) {
136 if (Arg->getType() == Ty)
137 return const_cast<Expr*>(Arg);
Craig Topper25542942014-05-20 04:30:07 +0000138
Ted Kremenek69bcb822012-09-21 18:13:23 +0000139 return ImplicitCastExpr::Create(C, Ty, CK_IntegralCast,
Craig Topper25542942014-05-20 04:30:07 +0000140 const_cast<Expr*>(Arg), nullptr, VK_RValue);
Ted Kremenek69bcb822012-09-21 18:13:23 +0000141}
142
Ted Kremenek089ffd02012-10-11 20:58:18 +0000143ImplicitCastExpr *ASTMaker::makeIntegralCastToBoolean(const Expr *Arg) {
144 return ImplicitCastExpr::Create(C, C.BoolTy, CK_IntegralToBoolean,
Craig Topper25542942014-05-20 04:30:07 +0000145 const_cast<Expr*>(Arg), nullptr, VK_RValue);
Ted Kremenek089ffd02012-10-11 20:58:18 +0000146}
147
148ObjCBoolLiteralExpr *ASTMaker::makeObjCBool(bool Val) {
149 QualType Ty = C.getBOOLDecl() ? C.getBOOLType() : C.ObjCBuiltinBoolTy;
150 return new (C) ObjCBoolLiteralExpr(Val, Ty, SourceLocation());
151}
152
Jordan Rose1a866cd2014-01-10 20:06:06 +0000153ObjCIvarRefExpr *ASTMaker::makeObjCIvarRef(const Expr *Base,
154 const ObjCIvarDecl *IVar) {
155 return new (C) ObjCIvarRefExpr(const_cast<ObjCIvarDecl*>(IVar),
156 IVar->getType(), SourceLocation(),
157 SourceLocation(), const_cast<Expr*>(Base),
158 /*arrow=*/true, /*free=*/false);
159}
160
161
Ted Kremenek089ffd02012-10-11 20:58:18 +0000162ReturnStmt *ASTMaker::makeReturn(const Expr *RetVal) {
Craig Topper25542942014-05-20 04:30:07 +0000163 return new (C) ReturnStmt(SourceLocation(), const_cast<Expr*>(RetVal),
164 nullptr);
Ted Kremenek089ffd02012-10-11 20:58:18 +0000165}
166
Ted Kremenek2b5c83c2012-09-21 17:54:32 +0000167//===----------------------------------------------------------------------===//
168// Creation functions for faux ASTs.
169//===----------------------------------------------------------------------===//
170
171typedef Stmt *(*FunctionFarmer)(ASTContext &C, const FunctionDecl *D);
172
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000173/// Create a fake body for dispatch_once.
174static Stmt *create_dispatch_once(ASTContext &C, const FunctionDecl *D) {
175 // Check if we have at least two parameters.
176 if (D->param_size() != 2)
Craig Topper25542942014-05-20 04:30:07 +0000177 return nullptr;
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000178
179 // Check if the first parameter is a pointer to integer type.
180 const ParmVarDecl *Predicate = D->getParamDecl(0);
181 QualType PredicateQPtrTy = Predicate->getType();
182 const PointerType *PredicatePtrTy = PredicateQPtrTy->getAs<PointerType>();
183 if (!PredicatePtrTy)
Craig Topper25542942014-05-20 04:30:07 +0000184 return nullptr;
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000185 QualType PredicateTy = PredicatePtrTy->getPointeeType();
186 if (!PredicateTy->isIntegerType())
Craig Topper25542942014-05-20 04:30:07 +0000187 return nullptr;
188
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000189 // Check if the second parameter is the proper block type.
190 const ParmVarDecl *Block = D->getParamDecl(1);
191 QualType Ty = Block->getType();
192 if (!isDispatchBlock(Ty))
Craig Topper25542942014-05-20 04:30:07 +0000193 return nullptr;
194
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000195 // Everything checks out. Create a fakse body that checks the predicate,
196 // sets it, and calls the block. Basically, an AST dump of:
197 //
198 // void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block) {
199 // if (!*predicate) {
200 // *predicate = 1;
201 // block();
202 // }
203 // }
204
Ted Kremenek72418132012-09-21 17:54:35 +0000205 ASTMaker M(C);
206
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000207 // (1) Create the call.
Ted Kremenek72418132012-09-21 17:54:35 +0000208 DeclRefExpr *DR = M.makeDeclRefExpr(Block);
Ted Kremenekca90ea52012-09-21 18:13:27 +0000209 ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000210 CallExpr *CE = new (C) CallExpr(C, ICE, None, C.VoidTy, VK_RValue,
211 SourceLocation());
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000212
213 // (2) Create the assignment to the predicate.
214 IntegerLiteral *IL =
215 IntegerLiteral::Create(C, llvm::APInt(C.getTypeSize(C.IntTy), (uint64_t) 1),
216 C.IntTy, SourceLocation());
Ted Kremeneke7ad5352012-09-21 18:33:56 +0000217 BinaryOperator *B =
218 M.makeAssignment(
219 M.makeDereference(
220 M.makeLvalueToRvalue(
221 M.makeDeclRefExpr(Predicate), PredicateQPtrTy),
222 PredicateTy),
223 M.makeIntegralCast(IL, PredicateTy),
224 PredicateTy);
225
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000226 // (3) Create the compound statement.
Craig Topper5fc8fc22014-08-27 06:28:36 +0000227 Stmt *Stmts[] = { B, CE };
228 CompoundStmt *CS = M.makeCompound(Stmts);
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000229
230 // (4) Create the 'if' condition.
Ted Kremeneke7ad5352012-09-21 18:33:56 +0000231 ImplicitCastExpr *LValToRval =
232 M.makeLvalueToRvalue(
233 M.makeDereference(
234 M.makeLvalueToRvalue(
235 M.makeDeclRefExpr(Predicate),
236 PredicateQPtrTy),
237 PredicateTy),
238 PredicateTy);
239
240 UnaryOperator *UO = new (C) UnaryOperator(LValToRval, UO_LNot, C.IntTy,
241 VK_RValue, OK_Ordinary,
242 SourceLocation());
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000243
244 // (5) Create the 'if' statement.
Craig Topper25542942014-05-20 04:30:07 +0000245 IfStmt *If = new (C) IfStmt(C, SourceLocation(), nullptr, UO, CS);
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000246 return If;
247}
248
Ted Kremenek14f779c2012-09-21 00:09:11 +0000249/// Create a fake body for dispatch_sync.
250static Stmt *create_dispatch_sync(ASTContext &C, const FunctionDecl *D) {
251 // Check if we have at least two parameters.
252 if (D->param_size() != 2)
Craig Topper25542942014-05-20 04:30:07 +0000253 return nullptr;
254
Ted Kremenek14f779c2012-09-21 00:09:11 +0000255 // Check if the second parameter is a block.
256 const ParmVarDecl *PV = D->getParamDecl(1);
257 QualType Ty = PV->getType();
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000258 if (!isDispatchBlock(Ty))
Craig Topper25542942014-05-20 04:30:07 +0000259 return nullptr;
260
Ted Kremenek14f779c2012-09-21 00:09:11 +0000261 // Everything checks out. Create a fake body that just calls the block.
262 // This is basically just an AST dump of:
263 //
264 // void dispatch_sync(dispatch_queue_t queue, void (^block)(void)) {
265 // block();
266 // }
Ted Kremenek72418132012-09-21 17:54:35 +0000267 //
268 ASTMaker M(C);
269 DeclRefExpr *DR = M.makeDeclRefExpr(PV);
Ted Kremenekdff35532012-09-21 18:33:52 +0000270 ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000271 CallExpr *CE = new (C) CallExpr(C, ICE, None, C.VoidTy, VK_RValue,
272 SourceLocation());
Ted Kremenek14f779c2012-09-21 00:09:11 +0000273 return CE;
274}
275
Ted Kremenek089ffd02012-10-11 20:58:18 +0000276static Stmt *create_OSAtomicCompareAndSwap(ASTContext &C, const FunctionDecl *D)
277{
278 // There are exactly 3 arguments.
279 if (D->param_size() != 3)
Craig Topper25542942014-05-20 04:30:07 +0000280 return nullptr;
281
Anna Zaks064185a2013-02-05 19:52:26 +0000282 // Signature:
283 // _Bool OSAtomicCompareAndSwapPtr(void *__oldValue,
284 // void *__newValue,
285 // void * volatile *__theValue)
286 // Generate body:
Ted Kremenek089ffd02012-10-11 20:58:18 +0000287 // if (oldValue == *theValue) {
288 // *theValue = newValue;
289 // return YES;
290 // }
291 // else return NO;
Alp Toker314cc812014-01-25 16:55:45 +0000292
293 QualType ResultTy = D->getReturnType();
Ted Kremenek6fdefb52012-10-12 00:18:19 +0000294 bool isBoolean = ResultTy->isBooleanType();
295 if (!isBoolean && !ResultTy->isIntegralType(C))
Craig Topper25542942014-05-20 04:30:07 +0000296 return nullptr;
297
Ted Kremenek089ffd02012-10-11 20:58:18 +0000298 const ParmVarDecl *OldValue = D->getParamDecl(0);
299 QualType OldValueTy = OldValue->getType();
300
301 const ParmVarDecl *NewValue = D->getParamDecl(1);
302 QualType NewValueTy = NewValue->getType();
303
304 assert(OldValueTy == NewValueTy);
305
306 const ParmVarDecl *TheValue = D->getParamDecl(2);
307 QualType TheValueTy = TheValue->getType();
308 const PointerType *PT = TheValueTy->getAs<PointerType>();
309 if (!PT)
Craig Topper25542942014-05-20 04:30:07 +0000310 return nullptr;
Ted Kremenek089ffd02012-10-11 20:58:18 +0000311 QualType PointeeTy = PT->getPointeeType();
312
313 ASTMaker M(C);
314 // Construct the comparison.
315 Expr *Comparison =
316 M.makeComparison(
317 M.makeLvalueToRvalue(M.makeDeclRefExpr(OldValue), OldValueTy),
318 M.makeLvalueToRvalue(
319 M.makeDereference(
320 M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
321 PointeeTy),
322 PointeeTy),
323 BO_EQ);
324
325 // Construct the body of the IfStmt.
326 Stmt *Stmts[2];
327 Stmts[0] =
328 M.makeAssignment(
329 M.makeDereference(
330 M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
331 PointeeTy),
332 M.makeLvalueToRvalue(M.makeDeclRefExpr(NewValue), NewValueTy),
333 NewValueTy);
Ted Kremenek6fdefb52012-10-12 00:18:19 +0000334
335 Expr *BoolVal = M.makeObjCBool(true);
336 Expr *RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
337 : M.makeIntegralCast(BoolVal, ResultTy);
338 Stmts[1] = M.makeReturn(RetVal);
Craig Topper5fc8fc22014-08-27 06:28:36 +0000339 CompoundStmt *Body = M.makeCompound(Stmts);
Ted Kremenek089ffd02012-10-11 20:58:18 +0000340
341 // Construct the else clause.
Ted Kremenek6fdefb52012-10-12 00:18:19 +0000342 BoolVal = M.makeObjCBool(false);
343 RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
344 : M.makeIntegralCast(BoolVal, ResultTy);
345 Stmt *Else = M.makeReturn(RetVal);
Ted Kremenek089ffd02012-10-11 20:58:18 +0000346
347 /// Construct the If.
348 Stmt *If =
Craig Topper25542942014-05-20 04:30:07 +0000349 new (C) IfStmt(C, SourceLocation(), nullptr, Comparison, Body,
Ted Kremenek089ffd02012-10-11 20:58:18 +0000350 SourceLocation(), Else);
Craig Topper25542942014-05-20 04:30:07 +0000351
Ted Kremenek089ffd02012-10-11 20:58:18 +0000352 return If;
353}
354
Ted Kremenek14f779c2012-09-21 00:09:11 +0000355Stmt *BodyFarm::getBody(const FunctionDecl *D) {
356 D = D->getCanonicalDecl();
357
David Blaikie05785d12013-02-20 22:23:23 +0000358 Optional<Stmt *> &Val = Bodies[D];
Ted Kremenek14f779c2012-09-21 00:09:11 +0000359 if (Val.hasValue())
360 return Val.getValue();
Craig Topper25542942014-05-20 04:30:07 +0000361
362 Val = nullptr;
363
364 if (D->getIdentifier() == nullptr)
365 return nullptr;
Ted Kremenek14f779c2012-09-21 00:09:11 +0000366
367 StringRef Name = D->getName();
368 if (Name.empty())
Craig Topper25542942014-05-20 04:30:07 +0000369 return nullptr;
Ted Kremenek089ffd02012-10-11 20:58:18 +0000370
371 FunctionFarmer FF;
372
373 if (Name.startswith("OSAtomicCompareAndSwap") ||
374 Name.startswith("objc_atomicCompareAndSwap")) {
375 FF = create_OSAtomicCompareAndSwap;
376 }
377 else {
378 FF = llvm::StringSwitch<FunctionFarmer>(Name)
379 .Case("dispatch_sync", create_dispatch_sync)
380 .Case("dispatch_once", create_dispatch_once)
Craig Topper25542942014-05-20 04:30:07 +0000381 .Default(nullptr);
Ted Kremenek14f779c2012-09-21 00:09:11 +0000382 }
383
Ted Kremenek089ffd02012-10-11 20:58:18 +0000384 if (FF) { Val = FF(C, D); }
Ted Kremenekeeccb302014-08-27 15:14:15 +0000385 else if (Injector) { Val = Injector->getBody(D); }
Ted Kremenek14f779c2012-09-21 00:09:11 +0000386 return Val.getValue();
387}
388
Jordan Rose1a866cd2014-01-10 20:06:06 +0000389static Stmt *createObjCPropertyGetter(ASTContext &Ctx,
390 const ObjCPropertyDecl *Prop) {
Jordan Roseddf19662014-01-23 03:59:10 +0000391 // First, find the backing ivar.
Jordan Rose1a866cd2014-01-10 20:06:06 +0000392 const ObjCIvarDecl *IVar = Prop->getPropertyIvarDecl();
393 if (!IVar)
Craig Topper25542942014-05-20 04:30:07 +0000394 return nullptr;
Jordan Roseddf19662014-01-23 03:59:10 +0000395
396 // Ignore weak variables, which have special behavior.
Jordan Rose1a866cd2014-01-10 20:06:06 +0000397 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak)
Craig Topper25542942014-05-20 04:30:07 +0000398 return nullptr;
Jordan Rosea3f27812014-01-14 17:29:06 +0000399
Jordan Roseddf19662014-01-23 03:59:10 +0000400 // Look to see if Sema has synthesized a body for us. This happens in
401 // Objective-C++ because the return value may be a C++ class type with a
402 // non-trivial copy constructor. We can only do this if we can find the
403 // @synthesize for this property, though (or if we know it's been auto-
404 // synthesized).
Jordan Rosea3f27812014-01-14 17:29:06 +0000405 const ObjCImplementationDecl *ImplDecl =
406 IVar->getContainingInterface()->getImplementation();
407 if (ImplDecl) {
Aaron Ballmand85eff42014-03-14 15:02:45 +0000408 for (const auto *I : ImplDecl->property_impls()) {
Jordan Rosea3f27812014-01-14 17:29:06 +0000409 if (I->getPropertyDecl() != Prop)
410 continue;
411
412 if (I->getGetterCXXConstructor()) {
413 ASTMaker M(Ctx);
414 return M.makeReturn(I->getGetterCXXConstructor());
415 }
416 }
417 }
418
Jordan Roseddf19662014-01-23 03:59:10 +0000419 // Sanity check that the property is the same type as the ivar, or a
420 // reference to it, and that it is either an object pointer or trivially
421 // copyable.
422 if (!Ctx.hasSameUnqualifiedType(IVar->getType(),
423 Prop->getType().getNonReferenceType()))
Craig Topper25542942014-05-20 04:30:07 +0000424 return nullptr;
Jordan Roseddf19662014-01-23 03:59:10 +0000425 if (!IVar->getType()->isObjCLifetimeType() &&
426 !IVar->getType().isTriviallyCopyableType(Ctx))
Craig Topper25542942014-05-20 04:30:07 +0000427 return nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000428
Jordan Roseddf19662014-01-23 03:59:10 +0000429 // Generate our body:
430 // return self->_ivar;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000431 ASTMaker M(Ctx);
432
433 const VarDecl *selfVar = Prop->getGetterMethodDecl()->getSelfDecl();
434
435 Expr *loadedIVar =
436 M.makeObjCIvarRef(
437 M.makeLvalueToRvalue(
438 M.makeDeclRefExpr(selfVar),
439 selfVar->getType()),
440 IVar);
441
442 if (!Prop->getType()->isReferenceType())
443 loadedIVar = M.makeLvalueToRvalue(loadedIVar, IVar->getType());
444
445 return M.makeReturn(loadedIVar);
446}
447
Jordan Roseddf19662014-01-23 03:59:10 +0000448Stmt *BodyFarm::getBody(const ObjCMethodDecl *D) {
449 // We currently only know how to synthesize property accessors.
Jordan Rose1a866cd2014-01-10 20:06:06 +0000450 if (!D->isPropertyAccessor())
Craig Topper25542942014-05-20 04:30:07 +0000451 return nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000452
453 D = D->getCanonicalDecl();
454
455 Optional<Stmt *> &Val = Bodies[D];
456 if (Val.hasValue())
457 return Val.getValue();
Craig Topper25542942014-05-20 04:30:07 +0000458 Val = nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000459
Jordan Roseddf19662014-01-23 03:59:10 +0000460 const ObjCPropertyDecl *Prop = D->findPropertyDecl();
Jordan Rose1a866cd2014-01-10 20:06:06 +0000461 if (!Prop)
Craig Topper25542942014-05-20 04:30:07 +0000462 return nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000463
Jordan Roseddf19662014-01-23 03:59:10 +0000464 // For now, we only synthesize getters.
Jordan Rose1a866cd2014-01-10 20:06:06 +0000465 if (D->param_size() != 0)
Craig Topper25542942014-05-20 04:30:07 +0000466 return nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000467
468 Val = createObjCPropertyGetter(C, Prop);
469
470 return Val.getValue();
471}
472