blob: d4051648f86cb061bc6b90418acd18a6a63e2e92 [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"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/ExprObjC.h"
20#include "llvm/ADT/StringSwitch.h"
Ted Kremenek14f779c2012-09-21 00:09:11 +000021
22using namespace clang;
23
Ted Kremenek2b5c83c2012-09-21 17:54:32 +000024//===----------------------------------------------------------------------===//
25// Helper creation functions for constructing faux ASTs.
26//===----------------------------------------------------------------------===//
Ted Kremenek14f779c2012-09-21 00:09:11 +000027
Ted Kremenekd81a4a12012-09-21 00:52:24 +000028static bool isDispatchBlock(QualType Ty) {
29 // Is it a block pointer?
30 const BlockPointerType *BPT = Ty->getAs<BlockPointerType>();
31 if (!BPT)
32 return false;
33
34 // Check if the block pointer type takes no arguments and
35 // returns void.
36 const FunctionProtoType *FT =
37 BPT->getPointeeType()->getAs<FunctionProtoType>();
Alp Toker314cc812014-01-25 16:55:45 +000038 if (!FT || !FT->getReturnType()->isVoidType() || FT->getNumParams() != 0)
Ted Kremenekd81a4a12012-09-21 00:52:24 +000039 return false;
40
41 return true;
42}
43
Ted Kremenek72418132012-09-21 17:54:35 +000044namespace {
45class ASTMaker {
46public:
47 ASTMaker(ASTContext &C) : C(C) {}
48
Ted Kremenekf465dc12012-09-21 18:33:54 +000049 /// Create a new BinaryOperator representing a simple assignment.
50 BinaryOperator *makeAssignment(const Expr *LHS, const Expr *RHS, QualType Ty);
51
Ted Kremenek089ffd02012-10-11 20:58:18 +000052 /// Create a new BinaryOperator representing a comparison.
53 BinaryOperator *makeComparison(const Expr *LHS, const Expr *RHS,
54 BinaryOperator::Opcode Op);
55
56 /// Create a new compound stmt using the provided statements.
57 CompoundStmt *makeCompound(ArrayRef<Stmt*>);
58
Ted Kremenek69bcb822012-09-21 18:13:23 +000059 /// Create a new DeclRefExpr for the referenced variable.
Ted Kremenek72418132012-09-21 17:54:35 +000060 DeclRefExpr *makeDeclRefExpr(const VarDecl *D);
61
Ted Kremenekdff35532012-09-21 18:33:52 +000062 /// Create a new UnaryOperator representing a dereference.
63 UnaryOperator *makeDereference(const Expr *Arg, QualType Ty);
64
Ted Kremenek69bcb822012-09-21 18:13:23 +000065 /// Create an implicit cast for an integer conversion.
Ted Kremenek6fdefb52012-10-12 00:18:19 +000066 Expr *makeIntegralCast(const Expr *Arg, QualType Ty);
Ted Kremenek69bcb822012-09-21 18:13:23 +000067
Ted Kremenek089ffd02012-10-11 20:58:18 +000068 /// Create an implicit cast to a builtin boolean type.
69 ImplicitCastExpr *makeIntegralCastToBoolean(const Expr *Arg);
70
Ted Kremenekca90ea52012-09-21 18:13:27 +000071 // Create an implicit cast for lvalue-to-rvaluate conversions.
72 ImplicitCastExpr *makeLvalueToRvalue(const Expr *Arg, QualType Ty);
73
Ted Kremenek089ffd02012-10-11 20:58:18 +000074 /// Create an Objective-C bool literal.
75 ObjCBoolLiteralExpr *makeObjCBool(bool Val);
Jordan Rose1a866cd2014-01-10 20:06:06 +000076
77 /// Create an Objective-C ivar reference.
78 ObjCIvarRefExpr *makeObjCIvarRef(const Expr *Base, const ObjCIvarDecl *IVar);
Ted Kremenek089ffd02012-10-11 20:58:18 +000079
80 /// Create a Return statement.
81 ReturnStmt *makeReturn(const Expr *RetVal);
82
Ted Kremenek72418132012-09-21 17:54:35 +000083private:
84 ASTContext &C;
85};
86}
87
Ted Kremenekf465dc12012-09-21 18:33:54 +000088BinaryOperator *ASTMaker::makeAssignment(const Expr *LHS, const Expr *RHS,
89 QualType Ty) {
90 return new (C) BinaryOperator(const_cast<Expr*>(LHS), const_cast<Expr*>(RHS),
91 BO_Assign, Ty, VK_RValue,
Lang Hames5de91cc2012-10-02 04:45:10 +000092 OK_Ordinary, SourceLocation(), false);
Ted Kremenekf465dc12012-09-21 18:33:54 +000093}
94
Ted Kremenek089ffd02012-10-11 20:58:18 +000095BinaryOperator *ASTMaker::makeComparison(const Expr *LHS, const Expr *RHS,
96 BinaryOperator::Opcode Op) {
97 assert(BinaryOperator::isLogicalOp(Op) ||
98 BinaryOperator::isComparisonOp(Op));
99 return new (C) BinaryOperator(const_cast<Expr*>(LHS),
100 const_cast<Expr*>(RHS),
101 Op,
102 C.getLogicalOperationType(),
103 VK_RValue,
104 OK_Ordinary, SourceLocation(), false);
105}
106
107CompoundStmt *ASTMaker::makeCompound(ArrayRef<Stmt *> Stmts) {
Nico Webera2a0eb92012-12-29 20:03:39 +0000108 return new (C) CompoundStmt(C, Stmts, SourceLocation(), SourceLocation());
Ted Kremenek089ffd02012-10-11 20:58:18 +0000109}
110
Ted Kremenek72418132012-09-21 17:54:35 +0000111DeclRefExpr *ASTMaker::makeDeclRefExpr(const VarDecl *D) {
112 DeclRefExpr *DR =
113 DeclRefExpr::Create(/* Ctx = */ C,
114 /* QualifierLoc = */ NestedNameSpecifierLoc(),
115 /* TemplateKWLoc = */ SourceLocation(),
116 /* D = */ const_cast<VarDecl*>(D),
117 /* isEnclosingLocal = */ false,
118 /* NameLoc = */ SourceLocation(),
119 /* T = */ D->getType(),
120 /* VK = */ VK_LValue);
121 return DR;
122}
123
Ted Kremenekdff35532012-09-21 18:33:52 +0000124UnaryOperator *ASTMaker::makeDereference(const Expr *Arg, QualType Ty) {
125 return new (C) UnaryOperator(const_cast<Expr*>(Arg), UO_Deref, Ty,
126 VK_LValue, OK_Ordinary, SourceLocation());
127}
128
Ted Kremenekca90ea52012-09-21 18:13:27 +0000129ImplicitCastExpr *ASTMaker::makeLvalueToRvalue(const Expr *Arg, QualType Ty) {
130 return ImplicitCastExpr::Create(C, Ty, CK_LValueToRValue,
Craig Topper25542942014-05-20 04:30:07 +0000131 const_cast<Expr*>(Arg), nullptr, VK_RValue);
Ted Kremenekca90ea52012-09-21 18:13:27 +0000132}
133
Ted Kremenek6fdefb52012-10-12 00:18:19 +0000134Expr *ASTMaker::makeIntegralCast(const Expr *Arg, QualType Ty) {
135 if (Arg->getType() == Ty)
136 return const_cast<Expr*>(Arg);
Craig Topper25542942014-05-20 04:30:07 +0000137
Ted Kremenek69bcb822012-09-21 18:13:23 +0000138 return ImplicitCastExpr::Create(C, Ty, CK_IntegralCast,
Craig Topper25542942014-05-20 04:30:07 +0000139 const_cast<Expr*>(Arg), nullptr, VK_RValue);
Ted Kremenek69bcb822012-09-21 18:13:23 +0000140}
141
Ted Kremenek089ffd02012-10-11 20:58:18 +0000142ImplicitCastExpr *ASTMaker::makeIntegralCastToBoolean(const Expr *Arg) {
143 return ImplicitCastExpr::Create(C, C.BoolTy, CK_IntegralToBoolean,
Craig Topper25542942014-05-20 04:30:07 +0000144 const_cast<Expr*>(Arg), nullptr, VK_RValue);
Ted Kremenek089ffd02012-10-11 20:58:18 +0000145}
146
147ObjCBoolLiteralExpr *ASTMaker::makeObjCBool(bool Val) {
148 QualType Ty = C.getBOOLDecl() ? C.getBOOLType() : C.ObjCBuiltinBoolTy;
149 return new (C) ObjCBoolLiteralExpr(Val, Ty, SourceLocation());
150}
151
Jordan Rose1a866cd2014-01-10 20:06:06 +0000152ObjCIvarRefExpr *ASTMaker::makeObjCIvarRef(const Expr *Base,
153 const ObjCIvarDecl *IVar) {
154 return new (C) ObjCIvarRefExpr(const_cast<ObjCIvarDecl*>(IVar),
155 IVar->getType(), SourceLocation(),
156 SourceLocation(), const_cast<Expr*>(Base),
157 /*arrow=*/true, /*free=*/false);
158}
159
160
Ted Kremenek089ffd02012-10-11 20:58:18 +0000161ReturnStmt *ASTMaker::makeReturn(const Expr *RetVal) {
Craig Topper25542942014-05-20 04:30:07 +0000162 return new (C) ReturnStmt(SourceLocation(), const_cast<Expr*>(RetVal),
163 nullptr);
Ted Kremenek089ffd02012-10-11 20:58:18 +0000164}
165
Ted Kremenek2b5c83c2012-09-21 17:54:32 +0000166//===----------------------------------------------------------------------===//
167// Creation functions for faux ASTs.
168//===----------------------------------------------------------------------===//
169
170typedef Stmt *(*FunctionFarmer)(ASTContext &C, const FunctionDecl *D);
171
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000172/// Create a fake body for dispatch_once.
173static Stmt *create_dispatch_once(ASTContext &C, const FunctionDecl *D) {
174 // Check if we have at least two parameters.
175 if (D->param_size() != 2)
Craig Topper25542942014-05-20 04:30:07 +0000176 return nullptr;
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000177
178 // Check if the first parameter is a pointer to integer type.
179 const ParmVarDecl *Predicate = D->getParamDecl(0);
180 QualType PredicateQPtrTy = Predicate->getType();
181 const PointerType *PredicatePtrTy = PredicateQPtrTy->getAs<PointerType>();
182 if (!PredicatePtrTy)
Craig Topper25542942014-05-20 04:30:07 +0000183 return nullptr;
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000184 QualType PredicateTy = PredicatePtrTy->getPointeeType();
185 if (!PredicateTy->isIntegerType())
Craig Topper25542942014-05-20 04:30:07 +0000186 return nullptr;
187
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000188 // Check if the second parameter is the proper block type.
189 const ParmVarDecl *Block = D->getParamDecl(1);
190 QualType Ty = Block->getType();
191 if (!isDispatchBlock(Ty))
Craig Topper25542942014-05-20 04:30:07 +0000192 return nullptr;
193
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000194 // Everything checks out. Create a fakse body that checks the predicate,
195 // sets it, and calls the block. Basically, an AST dump of:
196 //
197 // void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block) {
198 // if (!*predicate) {
199 // *predicate = 1;
200 // block();
201 // }
202 // }
203
Ted Kremenek72418132012-09-21 17:54:35 +0000204 ASTMaker M(C);
205
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000206 // (1) Create the call.
Ted Kremenek72418132012-09-21 17:54:35 +0000207 DeclRefExpr *DR = M.makeDeclRefExpr(Block);
Ted Kremenekca90ea52012-09-21 18:13:27 +0000208 ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000209 CallExpr *CE = new (C) CallExpr(C, ICE, None, C.VoidTy, VK_RValue,
210 SourceLocation());
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000211
212 // (2) Create the assignment to the predicate.
213 IntegerLiteral *IL =
214 IntegerLiteral::Create(C, llvm::APInt(C.getTypeSize(C.IntTy), (uint64_t) 1),
215 C.IntTy, SourceLocation());
Ted Kremeneke7ad5352012-09-21 18:33:56 +0000216 BinaryOperator *B =
217 M.makeAssignment(
218 M.makeDereference(
219 M.makeLvalueToRvalue(
220 M.makeDeclRefExpr(Predicate), PredicateQPtrTy),
221 PredicateTy),
222 M.makeIntegralCast(IL, PredicateTy),
223 PredicateTy);
224
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000225 // (3) Create the compound statement.
Craig Topper5fc8fc22014-08-27 06:28:36 +0000226 Stmt *Stmts[] = { B, CE };
227 CompoundStmt *CS = M.makeCompound(Stmts);
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000228
229 // (4) Create the 'if' condition.
Ted Kremeneke7ad5352012-09-21 18:33:56 +0000230 ImplicitCastExpr *LValToRval =
231 M.makeLvalueToRvalue(
232 M.makeDereference(
233 M.makeLvalueToRvalue(
234 M.makeDeclRefExpr(Predicate),
235 PredicateQPtrTy),
236 PredicateTy),
237 PredicateTy);
238
239 UnaryOperator *UO = new (C) UnaryOperator(LValToRval, UO_LNot, C.IntTy,
240 VK_RValue, OK_Ordinary,
241 SourceLocation());
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000242
243 // (5) Create the 'if' statement.
Craig Topper25542942014-05-20 04:30:07 +0000244 IfStmt *If = new (C) IfStmt(C, SourceLocation(), nullptr, UO, CS);
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000245 return If;
246}
247
Ted Kremenek14f779c2012-09-21 00:09:11 +0000248/// Create a fake body for dispatch_sync.
249static Stmt *create_dispatch_sync(ASTContext &C, const FunctionDecl *D) {
250 // Check if we have at least two parameters.
251 if (D->param_size() != 2)
Craig Topper25542942014-05-20 04:30:07 +0000252 return nullptr;
253
Ted Kremenek14f779c2012-09-21 00:09:11 +0000254 // Check if the second parameter is a block.
255 const ParmVarDecl *PV = D->getParamDecl(1);
256 QualType Ty = PV->getType();
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000257 if (!isDispatchBlock(Ty))
Craig Topper25542942014-05-20 04:30:07 +0000258 return nullptr;
259
Ted Kremenek14f779c2012-09-21 00:09:11 +0000260 // Everything checks out. Create a fake body that just calls the block.
261 // This is basically just an AST dump of:
262 //
263 // void dispatch_sync(dispatch_queue_t queue, void (^block)(void)) {
264 // block();
265 // }
Ted Kremenek72418132012-09-21 17:54:35 +0000266 //
267 ASTMaker M(C);
268 DeclRefExpr *DR = M.makeDeclRefExpr(PV);
Ted Kremenekdff35532012-09-21 18:33:52 +0000269 ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000270 CallExpr *CE = new (C) CallExpr(C, ICE, None, C.VoidTy, VK_RValue,
271 SourceLocation());
Ted Kremenek14f779c2012-09-21 00:09:11 +0000272 return CE;
273}
274
Ted Kremenek089ffd02012-10-11 20:58:18 +0000275static Stmt *create_OSAtomicCompareAndSwap(ASTContext &C, const FunctionDecl *D)
276{
277 // There are exactly 3 arguments.
278 if (D->param_size() != 3)
Craig Topper25542942014-05-20 04:30:07 +0000279 return nullptr;
280
Anna Zaks064185a2013-02-05 19:52:26 +0000281 // Signature:
282 // _Bool OSAtomicCompareAndSwapPtr(void *__oldValue,
283 // void *__newValue,
284 // void * volatile *__theValue)
285 // Generate body:
Ted Kremenek089ffd02012-10-11 20:58:18 +0000286 // if (oldValue == *theValue) {
287 // *theValue = newValue;
288 // return YES;
289 // }
290 // else return NO;
Alp Toker314cc812014-01-25 16:55:45 +0000291
292 QualType ResultTy = D->getReturnType();
Ted Kremenek6fdefb52012-10-12 00:18:19 +0000293 bool isBoolean = ResultTy->isBooleanType();
294 if (!isBoolean && !ResultTy->isIntegralType(C))
Craig Topper25542942014-05-20 04:30:07 +0000295 return nullptr;
296
Ted Kremenek089ffd02012-10-11 20:58:18 +0000297 const ParmVarDecl *OldValue = D->getParamDecl(0);
298 QualType OldValueTy = OldValue->getType();
299
300 const ParmVarDecl *NewValue = D->getParamDecl(1);
301 QualType NewValueTy = NewValue->getType();
302
303 assert(OldValueTy == NewValueTy);
304
305 const ParmVarDecl *TheValue = D->getParamDecl(2);
306 QualType TheValueTy = TheValue->getType();
307 const PointerType *PT = TheValueTy->getAs<PointerType>();
308 if (!PT)
Craig Topper25542942014-05-20 04:30:07 +0000309 return nullptr;
Ted Kremenek089ffd02012-10-11 20:58:18 +0000310 QualType PointeeTy = PT->getPointeeType();
311
312 ASTMaker M(C);
313 // Construct the comparison.
314 Expr *Comparison =
315 M.makeComparison(
316 M.makeLvalueToRvalue(M.makeDeclRefExpr(OldValue), OldValueTy),
317 M.makeLvalueToRvalue(
318 M.makeDereference(
319 M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
320 PointeeTy),
321 PointeeTy),
322 BO_EQ);
323
324 // Construct the body of the IfStmt.
325 Stmt *Stmts[2];
326 Stmts[0] =
327 M.makeAssignment(
328 M.makeDereference(
329 M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
330 PointeeTy),
331 M.makeLvalueToRvalue(M.makeDeclRefExpr(NewValue), NewValueTy),
332 NewValueTy);
Ted Kremenek6fdefb52012-10-12 00:18:19 +0000333
334 Expr *BoolVal = M.makeObjCBool(true);
335 Expr *RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
336 : M.makeIntegralCast(BoolVal, ResultTy);
337 Stmts[1] = M.makeReturn(RetVal);
Craig Topper5fc8fc22014-08-27 06:28:36 +0000338 CompoundStmt *Body = M.makeCompound(Stmts);
Ted Kremenek089ffd02012-10-11 20:58:18 +0000339
340 // Construct the else clause.
Ted Kremenek6fdefb52012-10-12 00:18:19 +0000341 BoolVal = M.makeObjCBool(false);
342 RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
343 : M.makeIntegralCast(BoolVal, ResultTy);
344 Stmt *Else = M.makeReturn(RetVal);
Ted Kremenek089ffd02012-10-11 20:58:18 +0000345
346 /// Construct the If.
347 Stmt *If =
Craig Topper25542942014-05-20 04:30:07 +0000348 new (C) IfStmt(C, SourceLocation(), nullptr, Comparison, Body,
Ted Kremenek089ffd02012-10-11 20:58:18 +0000349 SourceLocation(), Else);
Craig Topper25542942014-05-20 04:30:07 +0000350
Ted Kremenek089ffd02012-10-11 20:58:18 +0000351 return If;
352}
353
Ted Kremenek14f779c2012-09-21 00:09:11 +0000354Stmt *BodyFarm::getBody(const FunctionDecl *D) {
355 D = D->getCanonicalDecl();
356
David Blaikie05785d12013-02-20 22:23:23 +0000357 Optional<Stmt *> &Val = Bodies[D];
Ted Kremenek14f779c2012-09-21 00:09:11 +0000358 if (Val.hasValue())
359 return Val.getValue();
Craig Topper25542942014-05-20 04:30:07 +0000360
361 Val = nullptr;
362
363 if (D->getIdentifier() == nullptr)
364 return nullptr;
Ted Kremenek14f779c2012-09-21 00:09:11 +0000365
366 StringRef Name = D->getName();
367 if (Name.empty())
Craig Topper25542942014-05-20 04:30:07 +0000368 return nullptr;
Ted Kremenek089ffd02012-10-11 20:58:18 +0000369
370 FunctionFarmer FF;
371
372 if (Name.startswith("OSAtomicCompareAndSwap") ||
373 Name.startswith("objc_atomicCompareAndSwap")) {
374 FF = create_OSAtomicCompareAndSwap;
375 }
376 else {
377 FF = llvm::StringSwitch<FunctionFarmer>(Name)
378 .Case("dispatch_sync", create_dispatch_sync)
379 .Case("dispatch_once", create_dispatch_once)
Craig Topper25542942014-05-20 04:30:07 +0000380 .Default(nullptr);
Ted Kremenek14f779c2012-09-21 00:09:11 +0000381 }
382
Ted Kremenek089ffd02012-10-11 20:58:18 +0000383 if (FF) { Val = FF(C, D); }
Ted Kremenek14f779c2012-09-21 00:09:11 +0000384 return Val.getValue();
385}
386
Jordan Rose1a866cd2014-01-10 20:06:06 +0000387static Stmt *createObjCPropertyGetter(ASTContext &Ctx,
388 const ObjCPropertyDecl *Prop) {
Jordan Roseddf19662014-01-23 03:59:10 +0000389 // First, find the backing ivar.
Jordan Rose1a866cd2014-01-10 20:06:06 +0000390 const ObjCIvarDecl *IVar = Prop->getPropertyIvarDecl();
391 if (!IVar)
Craig Topper25542942014-05-20 04:30:07 +0000392 return nullptr;
Jordan Roseddf19662014-01-23 03:59:10 +0000393
394 // Ignore weak variables, which have special behavior.
Jordan Rose1a866cd2014-01-10 20:06:06 +0000395 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak)
Craig Topper25542942014-05-20 04:30:07 +0000396 return nullptr;
Jordan Rosea3f27812014-01-14 17:29:06 +0000397
Jordan Roseddf19662014-01-23 03:59:10 +0000398 // Look to see if Sema has synthesized a body for us. This happens in
399 // Objective-C++ because the return value may be a C++ class type with a
400 // non-trivial copy constructor. We can only do this if we can find the
401 // @synthesize for this property, though (or if we know it's been auto-
402 // synthesized).
Jordan Rosea3f27812014-01-14 17:29:06 +0000403 const ObjCImplementationDecl *ImplDecl =
404 IVar->getContainingInterface()->getImplementation();
405 if (ImplDecl) {
Aaron Ballmand85eff42014-03-14 15:02:45 +0000406 for (const auto *I : ImplDecl->property_impls()) {
Jordan Rosea3f27812014-01-14 17:29:06 +0000407 if (I->getPropertyDecl() != Prop)
408 continue;
409
410 if (I->getGetterCXXConstructor()) {
411 ASTMaker M(Ctx);
412 return M.makeReturn(I->getGetterCXXConstructor());
413 }
414 }
415 }
416
Jordan Roseddf19662014-01-23 03:59:10 +0000417 // Sanity check that the property is the same type as the ivar, or a
418 // reference to it, and that it is either an object pointer or trivially
419 // copyable.
420 if (!Ctx.hasSameUnqualifiedType(IVar->getType(),
421 Prop->getType().getNonReferenceType()))
Craig Topper25542942014-05-20 04:30:07 +0000422 return nullptr;
Jordan Roseddf19662014-01-23 03:59:10 +0000423 if (!IVar->getType()->isObjCLifetimeType() &&
424 !IVar->getType().isTriviallyCopyableType(Ctx))
Craig Topper25542942014-05-20 04:30:07 +0000425 return nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000426
Jordan Roseddf19662014-01-23 03:59:10 +0000427 // Generate our body:
428 // return self->_ivar;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000429 ASTMaker M(Ctx);
430
431 const VarDecl *selfVar = Prop->getGetterMethodDecl()->getSelfDecl();
432
433 Expr *loadedIVar =
434 M.makeObjCIvarRef(
435 M.makeLvalueToRvalue(
436 M.makeDeclRefExpr(selfVar),
437 selfVar->getType()),
438 IVar);
439
440 if (!Prop->getType()->isReferenceType())
441 loadedIVar = M.makeLvalueToRvalue(loadedIVar, IVar->getType());
442
443 return M.makeReturn(loadedIVar);
444}
445
Jordan Roseddf19662014-01-23 03:59:10 +0000446Stmt *BodyFarm::getBody(const ObjCMethodDecl *D) {
447 // We currently only know how to synthesize property accessors.
Jordan Rose1a866cd2014-01-10 20:06:06 +0000448 if (!D->isPropertyAccessor())
Craig Topper25542942014-05-20 04:30:07 +0000449 return nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000450
451 D = D->getCanonicalDecl();
452
453 Optional<Stmt *> &Val = Bodies[D];
454 if (Val.hasValue())
455 return Val.getValue();
Craig Topper25542942014-05-20 04:30:07 +0000456 Val = nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000457
Jordan Roseddf19662014-01-23 03:59:10 +0000458 const ObjCPropertyDecl *Prop = D->findPropertyDecl();
Jordan Rose1a866cd2014-01-10 20:06:06 +0000459 if (!Prop)
Craig Topper25542942014-05-20 04:30:07 +0000460 return nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000461
Jordan Roseddf19662014-01-23 03:59:10 +0000462 // For now, we only synthesize getters.
Jordan Rose1a866cd2014-01-10 20:06:06 +0000463 if (D->param_size() != 0)
Craig Topper25542942014-05-20 04:30:07 +0000464 return nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000465
466 Val = createObjCPropertyGetter(C, Prop);
467
468 return Val.getValue();
469}
470