Anders Carlsson | 76f4a90 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1 | //===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Anders Carlsson | 76f4a90 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit Objective-C code as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Devang Patel | d2d6665 | 2011-01-19 01:36:36 +0000 | [diff] [blame] | 14 | #include "CGDebugInfo.h" |
Ted Kremenek | 43e0633 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 15 | #include "CGObjCRuntime.h" |
Anders Carlsson | 76f4a90 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 16 | #include "CodeGenFunction.h" |
| 17 | #include "CodeGenModule.h" |
Daniel Dunbar | 9e22c0d | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | f0b64d7 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtObjC.h" |
Daniel Dunbar | c5d3304 | 2008-09-03 00:27:26 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
Anders Carlsson | 2e744e8 | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | 76f4a90 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | |
Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 27 | /// Emits an instance of NSConstantString representing the object. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 28 | llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) |
Daniel Dunbar | 44b58a2 | 2008-11-25 21:53:21 +0000 | [diff] [blame] | 29 | { |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 30 | llvm::Constant *C = |
| 31 | CGM.getObjCRuntime().GenerateConstantString(E->getString()); |
Daniel Dunbar | 66912a1 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 32 | // FIXME: This bitcast should just be made an invariant on the Runtime. |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 33 | return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); |
Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /// Emit a selector. |
| 37 | llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) { |
| 38 | // Untyped selector. |
| 39 | // Note that this implementation allows for non-constant strings to be passed |
| 40 | // as arguments to @selector(). Currently, the only thing preventing this |
| 41 | // behaviour is the type checking in the front end. |
Daniel Dunbar | 45858d2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 42 | return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector()); |
Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Daniel Dunbar | 66912a1 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 45 | llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) { |
| 46 | // FIXME: This should pass the Decl not the name. |
| 47 | return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol()); |
| 48 | } |
Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 49 | |
| 50 | |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 51 | RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E, |
| 52 | ReturnValueSlot Return) { |
Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 53 | // Only the lookup mechanism and first two arguments of the method |
| 54 | // implementation vary between runtimes. We can get the receiver and |
| 55 | // arguments in generic code. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | |
Daniel Dunbar | 8d48059 | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 57 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); |
Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 58 | bool isSuperMessage = false; |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 59 | bool isClassMessage = false; |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 60 | ObjCInterfaceDecl *OID = 0; |
Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 61 | // Find the receiver |
Daniel Dunbar | b219780 | 2010-04-22 03:17:06 +0000 | [diff] [blame] | 62 | llvm::Value *Receiver = 0; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 63 | switch (E->getReceiverKind()) { |
| 64 | case ObjCMessageExpr::Instance: |
| 65 | Receiver = EmitScalarExpr(E->getInstanceReceiver()); |
| 66 | break; |
Daniel Dunbar | 7c6d3a7 | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 67 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 68 | case ObjCMessageExpr::Class: { |
John McCall | 3e29492 | 2010-05-17 20:12:43 +0000 | [diff] [blame] | 69 | const ObjCObjectType *ObjTy |
| 70 | = E->getClassReceiver()->getAs<ObjCObjectType>(); |
| 71 | assert(ObjTy && "Invalid Objective-C class message send"); |
| 72 | OID = ObjTy->getInterface(); |
| 73 | assert(OID && "Invalid Objective-C class message send"); |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 74 | Receiver = Runtime.GetClass(Builder, OID); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 75 | isClassMessage = true; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 76 | break; |
| 77 | } |
| 78 | |
| 79 | case ObjCMessageExpr::SuperInstance: |
Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 80 | Receiver = LoadObjCSelf(); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 81 | isSuperMessage = true; |
| 82 | break; |
| 83 | |
| 84 | case ObjCMessageExpr::SuperClass: |
| 85 | Receiver = LoadObjCSelf(); |
| 86 | isSuperMessage = true; |
| 87 | isClassMessage = true; |
| 88 | break; |
Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 91 | CallArgList Args; |
Anders Carlsson | 623dcae | 2009-04-18 20:29:27 +0000 | [diff] [blame] | 92 | EmitCallArgs(Args, E->getMethodDecl(), E->arg_begin(), E->arg_end()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | |
Anders Carlsson | 280e61f1 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 94 | QualType ResultType = |
| 95 | E->getMethodDecl() ? E->getMethodDecl()->getResultType() : E->getType(); |
| 96 | |
Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 97 | if (isSuperMessage) { |
Chris Lattner | 6cfec78 | 2008-06-26 04:42:20 +0000 | [diff] [blame] | 98 | // super is only valid in an Objective-C method |
| 99 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 100 | bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
Anders Carlsson | 280e61f1 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 101 | return Runtime.GenerateMessageSendSuper(*this, Return, ResultType, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 102 | E->getSelector(), |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 103 | OMD->getClassInterface(), |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 104 | isCategoryImpl, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 105 | Receiver, |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 106 | isClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 107 | Args, |
| 108 | E->getMethodDecl()); |
Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 109 | } |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 110 | |
Anders Carlsson | 280e61f1 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 111 | return Runtime.GenerateMessageSend(*this, Return, ResultType, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 112 | E->getSelector(), |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 113 | Receiver, Args, OID, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 114 | E->getMethodDecl()); |
Anders Carlsson | 76f4a90 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 117 | /// StartObjCMethod - Begin emission of an ObjCMethod. This generates |
| 118 | /// the LLVM function and sets the other context used by |
| 119 | /// CodeGenFunction. |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 120 | void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD, |
| 121 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | bc915f4 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 122 | FunctionArgList Args; |
Devang Patel | a2c048e | 2010-04-05 21:09:15 +0000 | [diff] [blame] | 123 | // Check if we should generate debug info for this method. |
| 124 | if (CGM.getDebugInfo() && !OMD->hasAttr<NoDebugAttr>()) |
| 125 | DebugInfo = CGM.getDebugInfo(); |
| 126 | |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 127 | llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD); |
Daniel Dunbar | 449a339 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 128 | |
Daniel Dunbar | c3e7cff | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 129 | const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD); |
| 130 | CGM.SetInternalFunctionAttributes(OMD, Fn, FI); |
Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 131 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | Args.push_back(std::make_pair(OMD->getSelfDecl(), |
Daniel Dunbar | bc915f4 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 133 | OMD->getSelfDecl()->getType())); |
| 134 | Args.push_back(std::make_pair(OMD->getCmdDecl(), |
| 135 | OMD->getCmdDecl()->getType())); |
Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 136 | |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 137 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 138 | E = OMD->param_end(); PI != E; ++PI) |
| 139 | Args.push_back(std::make_pair(*PI, (*PI)->getType())); |
Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 140 | |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 141 | CurGD = OMD; |
| 142 | |
Tilmann Scheller | 454464b | 2011-03-02 19:36:23 +0000 | [diff] [blame] | 143 | StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocStart(), |
| 144 | CC_Default); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 145 | } |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 146 | |
Fariborz Jahanian | 302a3d4 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 147 | void CodeGenFunction::GenerateObjCGetterBody(ObjCIvarDecl *Ivar, |
| 148 | bool IsAtomic, bool IsStrong) { |
| 149 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), |
| 150 | Ivar, 0); |
| 151 | llvm::Value *GetCopyStructFn = |
| 152 | CGM.getObjCRuntime().GetGetStructFunction(); |
| 153 | CodeGenTypes &Types = CGM.getTypes(); |
| 154 | // objc_copyStruct (ReturnValue, &structIvar, |
| 155 | // sizeof (Type of Ivar), isAtomic, false); |
| 156 | CallArgList Args; |
| 157 | RValue RV = RValue::get(Builder.CreateBitCast(ReturnValue, |
| 158 | Types.ConvertType(getContext().VoidPtrTy))); |
| 159 | Args.push_back(std::make_pair(RV, getContext().VoidPtrTy)); |
| 160 | RV = RValue::get(Builder.CreateBitCast(LV.getAddress(), |
| 161 | Types.ConvertType(getContext().VoidPtrTy))); |
| 162 | Args.push_back(std::make_pair(RV, getContext().VoidPtrTy)); |
| 163 | // sizeof (Type of Ivar) |
| 164 | CharUnits Size = getContext().getTypeSizeInChars(Ivar->getType()); |
| 165 | llvm::Value *SizeVal = |
| 166 | llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), |
| 167 | Size.getQuantity()); |
| 168 | Args.push_back(std::make_pair(RValue::get(SizeVal), |
| 169 | getContext().LongTy)); |
| 170 | llvm::Value *isAtomic = |
| 171 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), |
| 172 | IsAtomic ? 1 : 0); |
| 173 | Args.push_back(std::make_pair(RValue::get(isAtomic), |
| 174 | getContext().BoolTy)); |
| 175 | llvm::Value *hasStrong = |
| 176 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), |
| 177 | IsStrong ? 1 : 0); |
| 178 | Args.push_back(std::make_pair(RValue::get(hasStrong), |
| 179 | getContext().BoolTy)); |
| 180 | EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args, |
| 181 | FunctionType::ExtInfo()), |
| 182 | GetCopyStructFn, ReturnValueSlot(), Args); |
| 183 | } |
| 184 | |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 185 | /// Generate an Objective-C method. An Objective-C method is a C function with |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | /// its pointer, name, and types registered in the class struture. |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 187 | void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 188 | StartObjCMethod(OMD, OMD->getClassInterface()); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 189 | EmitStmt(OMD->getBody()); |
| 190 | FinishFunction(OMD->getBodyRBrace()); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 193 | // FIXME: I wasn't sure about the synthesis approach. If we end up generating an |
| 194 | // AST for the whole body we can just fall back to having a GenerateFunction |
| 195 | // which takes the body Stmt. |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 196 | |
| 197 | /// GenerateObjCGetter - Generate an Objective-C property getter |
Steve Naroff | 5a7dd78 | 2009-01-10 22:55:25 +0000 | [diff] [blame] | 198 | /// function. The given Decl must be an ObjCImplementationDecl. @synthesize |
| 199 | /// is illegal within a category. |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 200 | void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP, |
| 201 | const ObjCPropertyImplDecl *PID) { |
Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 202 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 203 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Fariborz Jahanian | 7e9d52a | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 204 | bool IsAtomic = |
| 205 | !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 206 | ObjCMethodDecl *OMD = PD->getGetterMethodDecl(); |
| 207 | assert(OMD && "Invalid call to generate getter (empty method)"); |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 208 | StartObjCMethod(OMD, IMP->getClassInterface()); |
Fariborz Jahanian | 7e9d52a | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 209 | |
Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 210 | // Determine if we should use an objc_getProperty call for |
Fariborz Jahanian | 8e0079c | 2008-12-08 23:56:17 +0000 | [diff] [blame] | 211 | // this. Non-atomic properties are directly evaluated. |
| 212 | // atomic 'copy' and 'retain' properties are also directly |
| 213 | // evaluated in gc-only mode. |
Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 214 | if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly && |
Fariborz Jahanian | 7e9d52a | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 215 | IsAtomic && |
Fariborz Jahanian | 8e0079c | 2008-12-08 23:56:17 +0000 | [diff] [blame] | 216 | (PD->getSetterKind() == ObjCPropertyDecl::Copy || |
| 217 | PD->getSetterKind() == ObjCPropertyDecl::Retain)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | llvm::Value *GetPropertyFn = |
Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 219 | CGM.getObjCRuntime().GetPropertyGetFunction(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 221 | if (!GetPropertyFn) { |
| 222 | CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy"); |
| 223 | FinishFunction(); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true). |
| 228 | // FIXME: Can't this be simpler? This might even be worse than the |
| 229 | // corresponding gcc code. |
| 230 | CodeGenTypes &Types = CGM.getTypes(); |
| 231 | ValueDecl *Cmd = OMD->getCmdDecl(); |
| 232 | llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd"); |
| 233 | QualType IdTy = getContext().getObjCIdType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | llvm::Value *SelfAsId = |
Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 235 | Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 236 | llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar); |
Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 237 | llvm::Value *True = |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 238 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1); |
Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 239 | CallArgList Args; |
| 240 | Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy)); |
| 241 | Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType())); |
| 242 | Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy)); |
| 243 | Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy)); |
Daniel Dunbar | 1ef7373 | 2009-02-03 23:43:59 +0000 | [diff] [blame] | 244 | // FIXME: We shouldn't need to get the function info here, the |
| 245 | // runtime already should have computed it to build the function. |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 246 | RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 247 | FunctionType::ExtInfo()), |
Anders Carlsson | 61a401c | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 248 | GetPropertyFn, ReturnValueSlot(), Args); |
Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 249 | // We need to fix the type here. Ivars with copy & retain are |
| 250 | // always objects so we don't need to worry about complex or |
| 251 | // aggregates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(), |
Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 253 | Types.ConvertType(PD->getType()))); |
| 254 | EmitReturnOfRValue(RV, PD->getType()); |
| 255 | } else { |
Fariborz Jahanian | 302a3d4 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 256 | const llvm::Triple &Triple = getContext().Target.getTriple(); |
| 257 | QualType IVART = Ivar->getType(); |
| 258 | if (IsAtomic && |
| 259 | IVART->isScalarType() && |
| 260 | (Triple.getArch() == llvm::Triple::arm || |
| 261 | Triple.getArch() == llvm::Triple::thumb) && |
| 262 | (getContext().getTypeSizeInChars(IVART) |
| 263 | > CharUnits::fromQuantity(4)) && |
| 264 | CGM.getObjCRuntime().GetGetStructFunction()) { |
| 265 | GenerateObjCGetterBody(Ivar, true, false); |
| 266 | } |
| 267 | else if (IVART->isAnyComplexType()) { |
Fariborz Jahanian | b899338 | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 268 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), |
| 269 | Ivar, 0); |
Fariborz Jahanian | f9c4585 | 2010-03-25 21:56:43 +0000 | [diff] [blame] | 270 | ComplexPairTy Pair = LoadComplexFromAddr(LV.getAddress(), |
| 271 | LV.isVolatileQualified()); |
| 272 | StoreComplexToAddr(Pair, ReturnValue, LV.isVolatileQualified()); |
| 273 | } |
Fariborz Jahanian | 302a3d4 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 274 | else if (hasAggregateLLVMType(IVART)) { |
Fariborz Jahanian | 7e9d52a | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 275 | bool IsStrong = false; |
Fariborz Jahanian | 302a3d4 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 276 | if ((IsAtomic || (IsStrong = IvarTypeWithAggrGCObjects(IVART))) |
Fariborz Jahanian | 08b0f66 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 277 | && CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 278 | && CGM.getObjCRuntime().GetGetStructFunction()) { |
Fariborz Jahanian | 302a3d4 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 279 | GenerateObjCGetterBody(Ivar, IsAtomic, IsStrong); |
Fariborz Jahanian | 08b0f66 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 280 | } |
Fariborz Jahanian | b899338 | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 281 | else { |
| 282 | if (PID->getGetterCXXConstructor()) { |
| 283 | ReturnStmt *Stmt = |
| 284 | new (getContext()) ReturnStmt(SourceLocation(), |
Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 285 | PID->getGetterCXXConstructor(), |
| 286 | 0); |
Fariborz Jahanian | b899338 | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 287 | EmitReturnStmt(*Stmt); |
| 288 | } |
| 289 | else { |
| 290 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), |
| 291 | Ivar, 0); |
Fariborz Jahanian | 302a3d4 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 292 | EmitAggregateCopy(ReturnValue, LV.getAddress(), IVART); |
Fariborz Jahanian | b899338 | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
Fariborz Jahanian | 302a3d4 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 295 | } |
| 296 | else { |
| 297 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), |
Fariborz Jahanian | b899338 | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 298 | Ivar, 0); |
Fariborz Jahanian | 302a3d4 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 299 | CodeGenTypes &Types = CGM.getTypes(); |
| 300 | RValue RV = EmitLoadOfLValue(LV, IVART); |
| 301 | RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(), |
| 302 | Types.ConvertType(PD->getType()))); |
| 303 | EmitReturnOfRValue(RV, PD->getType()); |
Fariborz Jahanian | eab5ecd | 2009-03-03 18:49:40 +0000 | [diff] [blame] | 304 | } |
Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 305 | } |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 306 | |
| 307 | FinishFunction(); |
| 308 | } |
| 309 | |
Fariborz Jahanian | 302a3d4 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 310 | void CodeGenFunction::GenerateObjCAtomicSetterBody(ObjCMethodDecl *OMD, |
| 311 | ObjCIvarDecl *Ivar) { |
| 312 | // objc_copyStruct (&structIvar, &Arg, |
| 313 | // sizeof (struct something), true, false); |
| 314 | llvm::Value *GetCopyStructFn = |
| 315 | CGM.getObjCRuntime().GetSetStructFunction(); |
| 316 | CodeGenTypes &Types = CGM.getTypes(); |
| 317 | CallArgList Args; |
| 318 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0); |
| 319 | RValue RV = |
| 320 | RValue::get(Builder.CreateBitCast(LV.getAddress(), |
| 321 | Types.ConvertType(getContext().VoidPtrTy))); |
| 322 | Args.push_back(std::make_pair(RV, getContext().VoidPtrTy)); |
| 323 | llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()]; |
| 324 | llvm::Value *ArgAsPtrTy = |
| 325 | Builder.CreateBitCast(Arg, |
| 326 | Types.ConvertType(getContext().VoidPtrTy)); |
| 327 | RV = RValue::get(ArgAsPtrTy); |
| 328 | Args.push_back(std::make_pair(RV, getContext().VoidPtrTy)); |
| 329 | // sizeof (Type of Ivar) |
| 330 | CharUnits Size = getContext().getTypeSizeInChars(Ivar->getType()); |
| 331 | llvm::Value *SizeVal = |
| 332 | llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), |
| 333 | Size.getQuantity()); |
| 334 | Args.push_back(std::make_pair(RValue::get(SizeVal), |
| 335 | getContext().LongTy)); |
| 336 | llvm::Value *True = |
| 337 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1); |
| 338 | Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy)); |
| 339 | llvm::Value *False = |
| 340 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0); |
| 341 | Args.push_back(std::make_pair(RValue::get(False), getContext().BoolTy)); |
| 342 | EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args, |
| 343 | FunctionType::ExtInfo()), |
| 344 | GetCopyStructFn, ReturnValueSlot(), Args); |
| 345 | } |
| 346 | |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 347 | /// GenerateObjCSetter - Generate an Objective-C property setter |
Steve Naroff | 5a7dd78 | 2009-01-10 22:55:25 +0000 | [diff] [blame] | 348 | /// function. The given Decl must be an ObjCImplementationDecl. @synthesize |
| 349 | /// is illegal within a category. |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 350 | void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP, |
| 351 | const ObjCPropertyImplDecl *PID) { |
Daniel Dunbar | 5449ce5 | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 352 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 353 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 354 | ObjCMethodDecl *OMD = PD->getSetterMethodDecl(); |
| 355 | assert(OMD && "Invalid call to generate setter (empty method)"); |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 356 | StartObjCMethod(OMD, IMP->getClassInterface()); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 357 | |
Daniel Dunbar | 5449ce5 | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 358 | bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | bool IsAtomic = |
Daniel Dunbar | 5449ce5 | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 360 | !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic); |
| 361 | |
| 362 | // Determine if we should use an objc_setProperty call for |
| 363 | // this. Properties with 'copy' semantics always use it, as do |
| 364 | // non-atomic properties with 'release' semantics as long as we are |
| 365 | // not in gc-only mode. |
| 366 | if (IsCopy || |
| 367 | (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly && |
| 368 | PD->getSetterKind() == ObjCPropertyDecl::Retain)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | llvm::Value *SetPropertyFn = |
Daniel Dunbar | 5449ce5 | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 370 | CGM.getObjCRuntime().GetPropertySetFunction(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 371 | |
Daniel Dunbar | 5449ce5 | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 372 | if (!SetPropertyFn) { |
| 373 | CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy"); |
| 374 | FinishFunction(); |
| 375 | return; |
| 376 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | |
| 378 | // Emit objc_setProperty((id) self, _cmd, offset, arg, |
Daniel Dunbar | 5449ce5 | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 379 | // <is-atomic>, <is-copy>). |
| 380 | // FIXME: Can't this be simpler? This might even be worse than the |
| 381 | // corresponding gcc code. |
| 382 | CodeGenTypes &Types = CGM.getTypes(); |
| 383 | ValueDecl *Cmd = OMD->getCmdDecl(); |
| 384 | llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd"); |
| 385 | QualType IdTy = getContext().getObjCIdType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | llvm::Value *SelfAsId = |
Daniel Dunbar | 5449ce5 | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 387 | Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 388 | llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar); |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 389 | llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | llvm::Value *ArgAsId = |
Daniel Dunbar | 5449ce5 | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 391 | Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"), |
| 392 | Types.ConvertType(IdTy)); |
| 393 | llvm::Value *True = |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 394 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1); |
Daniel Dunbar | 5449ce5 | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 395 | llvm::Value *False = |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 396 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0); |
Daniel Dunbar | 5449ce5 | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 397 | CallArgList Args; |
| 398 | Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy)); |
| 399 | Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType())); |
| 400 | Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy)); |
| 401 | Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False), |
Daniel Dunbar | 5449ce5 | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 403 | getContext().BoolTy)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 404 | Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False), |
Daniel Dunbar | 5449ce5 | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 405 | getContext().BoolTy)); |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 406 | // FIXME: We shouldn't need to get the function info here, the runtime |
| 407 | // already should have computed it to build the function. |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 408 | EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 409 | FunctionType::ExtInfo()), |
| 410 | SetPropertyFn, |
Anders Carlsson | 61a401c | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 411 | ReturnValueSlot(), Args); |
Fariborz Jahanian | 08b0f66 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 412 | } else if (IsAtomic && hasAggregateLLVMType(Ivar->getType()) && |
| 413 | !Ivar->getType()->isAnyComplexType() && |
| 414 | IndirectObjCSetterArg(*CurFnInfo) |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 415 | && CGM.getObjCRuntime().GetSetStructFunction()) { |
Fariborz Jahanian | 08b0f66 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 416 | // objc_copyStruct (&structIvar, &Arg, |
| 417 | // sizeof (struct something), true, false); |
Fariborz Jahanian | 302a3d4 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 418 | GenerateObjCAtomicSetterBody(OMD, Ivar); |
Fariborz Jahanian | b899338 | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 419 | } else if (PID->getSetterCXXAssignment()) { |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 420 | EmitIgnoredExpr(PID->getSetterCXXAssignment()); |
Daniel Dunbar | 5449ce5 | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 421 | } else { |
Fariborz Jahanian | 302a3d4 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 422 | const llvm::Triple &Triple = getContext().Target.getTriple(); |
| 423 | QualType IVART = Ivar->getType(); |
| 424 | if (IsAtomic && |
| 425 | IVART->isScalarType() && |
| 426 | (Triple.getArch() == llvm::Triple::arm || |
| 427 | Triple.getArch() == llvm::Triple::thumb) && |
| 428 | (getContext().getTypeSizeInChars(IVART) |
| 429 | > CharUnits::fromQuantity(4)) && |
| 430 | CGM.getObjCRuntime().GetGetStructFunction()) { |
| 431 | GenerateObjCAtomicSetterBody(OMD, Ivar); |
| 432 | } |
| 433 | else { |
| 434 | // FIXME: Find a clean way to avoid AST node creation. |
| 435 | SourceLocation Loc = PD->getLocation(); |
| 436 | ValueDecl *Self = OMD->getSelfDecl(); |
| 437 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
| 438 | DeclRefExpr Base(Self, Self->getType(), VK_RValue, Loc); |
| 439 | ParmVarDecl *ArgDecl = *OMD->param_begin(); |
| 440 | DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), VK_LValue, Loc); |
| 441 | ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true); |
Daniel Dunbar | c14753b | 2009-10-27 19:21:30 +0000 | [diff] [blame] | 442 | |
Fariborz Jahanian | 302a3d4 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 443 | // The property type can differ from the ivar type in some situations with |
| 444 | // Objective-C pointer types, we can always bit cast the RHS in these cases. |
| 445 | if (getContext().getCanonicalType(Ivar->getType()) != |
| 446 | getContext().getCanonicalType(ArgDecl->getType())) { |
| 447 | ImplicitCastExpr ArgCasted(ImplicitCastExpr::OnStack, |
| 448 | Ivar->getType(), CK_BitCast, &Arg, |
| 449 | VK_RValue); |
| 450 | BinaryOperator Assign(&IvarRef, &ArgCasted, BO_Assign, |
| 451 | Ivar->getType(), VK_RValue, OK_Ordinary, Loc); |
| 452 | EmitStmt(&Assign); |
| 453 | } else { |
| 454 | BinaryOperator Assign(&IvarRef, &Arg, BO_Assign, |
| 455 | Ivar->getType(), VK_RValue, OK_Ordinary, Loc); |
| 456 | EmitStmt(&Assign); |
| 457 | } |
Daniel Dunbar | c14753b | 2009-10-27 19:21:30 +0000 | [diff] [blame] | 458 | } |
Daniel Dunbar | 5449ce5 | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 459 | } |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 460 | |
| 461 | FinishFunction(); |
Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 464 | void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, |
| 465 | ObjCMethodDecl *MD, |
| 466 | bool ctor) { |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 467 | llvm::SmallVector<CXXCtorInitializer *, 8> IvarInitializers; |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 468 | MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface()); |
| 469 | StartObjCMethod(MD, IMP->getClassInterface()); |
| 470 | for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(), |
| 471 | E = IMP->init_end(); B != E; ++B) { |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 472 | CXXCtorInitializer *Member = (*B); |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 473 | IvarInitializers.push_back(Member); |
| 474 | } |
| 475 | if (ctor) { |
| 476 | for (unsigned I = 0, E = IvarInitializers.size(); I != E; ++I) { |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 477 | CXXCtorInitializer *IvarInit = IvarInitializers[I]; |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 478 | FieldDecl *Field = IvarInit->getAnyMember(); |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 479 | ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); |
Fariborz Jahanian | 499b902 | 2010-04-28 22:30:33 +0000 | [diff] [blame] | 480 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), |
| 481 | LoadObjCSelf(), Ivar, 0); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 482 | EmitAggExpr(IvarInit->getInit(), AggValueSlot::forLValue(LV, true)); |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 483 | } |
| 484 | // constructor returns 'self'. |
| 485 | CodeGenTypes &Types = CGM.getTypes(); |
| 486 | QualType IdTy(CGM.getContext().getObjCIdType()); |
| 487 | llvm::Value *SelfAsId = |
| 488 | Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); |
| 489 | EmitReturnOfRValue(RValue::get(SelfAsId), IdTy); |
Chandler Carruth | f398365 | 2010-05-06 00:20:39 +0000 | [diff] [blame] | 490 | } else { |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 491 | // dtor |
| 492 | for (size_t i = IvarInitializers.size(); i > 0; --i) { |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 493 | FieldDecl *Field = IvarInitializers[i - 1]->getAnyMember(); |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 494 | QualType FieldType = Field->getType(); |
Fariborz Jahanian | 499b902 | 2010-04-28 22:30:33 +0000 | [diff] [blame] | 495 | const ConstantArrayType *Array = |
| 496 | getContext().getAsConstantArrayType(FieldType); |
| 497 | if (Array) |
| 498 | FieldType = getContext().getBaseElementType(FieldType); |
| 499 | |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 500 | ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); |
| 501 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), |
| 502 | LoadObjCSelf(), Ivar, 0); |
| 503 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 504 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Douglas Gregor | bac7490 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 505 | CXXDestructorDecl *Dtor = FieldClassDecl->getDestructor(); |
Chandler Carruth | f398365 | 2010-05-06 00:20:39 +0000 | [diff] [blame] | 506 | if (!Dtor->isTrivial()) { |
Fariborz Jahanian | 60c7e16 | 2010-05-04 19:29:32 +0000 | [diff] [blame] | 507 | if (Array) { |
| 508 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 509 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 510 | llvm::Value *BaseAddrPtr = |
| 511 | Builder.CreateBitCast(LV.getAddress(), BasePtr); |
| 512 | EmitCXXAggrDestructorCall(Dtor, |
| 513 | Array, BaseAddrPtr); |
Chandler Carruth | f398365 | 2010-05-06 00:20:39 +0000 | [diff] [blame] | 514 | } else { |
Fariborz Jahanian | 60c7e16 | 2010-05-04 19:29:32 +0000 | [diff] [blame] | 515 | EmitCXXDestructorCall(Dtor, |
| 516 | Dtor_Complete, /*ForVirtualBase=*/false, |
| 517 | LV.getAddress()); |
Chandler Carruth | f398365 | 2010-05-06 00:20:39 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | } |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 521 | } |
| 522 | FinishFunction(); |
| 523 | } |
| 524 | |
Fariborz Jahanian | 08b0f66 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 525 | bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) { |
| 526 | CGFunctionInfo::const_arg_iterator it = FI.arg_begin(); |
| 527 | it++; it++; |
| 528 | const ABIArgInfo &AI = it->info; |
| 529 | // FIXME. Is this sufficient check? |
| 530 | return (AI.getKind() == ABIArgInfo::Indirect); |
| 531 | } |
| 532 | |
Fariborz Jahanian | 7e9d52a | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 533 | bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) { |
| 534 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 535 | return false; |
| 536 | if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>()) |
| 537 | return FDTTy->getDecl()->hasObjectMember(); |
| 538 | return false; |
| 539 | } |
| 540 | |
Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 541 | llvm::Value *CodeGenFunction::LoadObjCSelf() { |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 542 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 543 | return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self"); |
Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 546 | QualType CodeGenFunction::TypeOfSelfObject() { |
| 547 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 548 | ImplicitParamDecl *selfDecl = OMD->getSelfDecl(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 549 | const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>( |
| 550 | getContext().getCanonicalType(selfDecl->getType())); |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 551 | return PTy->getPointeeType(); |
| 552 | } |
| 553 | |
John McCall | 0692a32 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 554 | LValue |
| 555 | CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) { |
| 556 | // This is a special l-value that just issues sends when we load or |
| 557 | // store through it. |
| 558 | |
| 559 | // For certain base kinds, we need to emit the base immediately. |
| 560 | llvm::Value *Base; |
| 561 | if (E->isSuperReceiver()) |
| 562 | Base = LoadObjCSelf(); |
| 563 | else if (E->isClassReceiver()) |
| 564 | Base = CGM.getObjCRuntime().GetClass(Builder, E->getClassReceiver()); |
| 565 | else |
| 566 | Base = EmitScalarExpr(E->getBase()); |
| 567 | return LValue::MakePropertyRef(E, Base); |
| 568 | } |
| 569 | |
| 570 | static RValue GenerateMessageSendSuper(CodeGenFunction &CGF, |
| 571 | ReturnValueSlot Return, |
| 572 | QualType ResultType, |
| 573 | Selector S, |
| 574 | llvm::Value *Receiver, |
| 575 | const CallArgList &CallArgs) { |
| 576 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CGF.CurFuncDecl); |
Fariborz Jahanian | 391d4fc | 2009-03-20 19:18:21 +0000 | [diff] [blame] | 577 | bool isClassMessage = OMD->isClassMethod(); |
| 578 | bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
John McCall | 0692a32 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 579 | return CGF.CGM.getObjCRuntime() |
| 580 | .GenerateMessageSendSuper(CGF, Return, ResultType, |
| 581 | S, OMD->getClassInterface(), |
| 582 | isCategoryImpl, Receiver, |
| 583 | isClassMessage, CallArgs); |
Fariborz Jahanian | 391d4fc | 2009-03-20 19:18:21 +0000 | [diff] [blame] | 584 | } |
| 585 | |
John McCall | f3eb96f | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 586 | RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV, |
| 587 | ReturnValueSlot Return) { |
| 588 | const ObjCPropertyRefExpr *E = LV.getPropertyRefExpr(); |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 589 | QualType ResultType; |
| 590 | Selector S; |
| 591 | if (E->isExplicitProperty()) { |
| 592 | const ObjCPropertyDecl *Property = E->getExplicitProperty(); |
| 593 | S = Property->getGetterName(); |
| 594 | ResultType = E->getType(); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 595 | } else { |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 596 | const ObjCMethodDecl *Getter = E->getImplicitPropertyGetter(); |
| 597 | S = Getter->getSelector(); |
| 598 | ResultType = Getter->getResultType(); // with reference! |
Fariborz Jahanian | 9ac5351 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 599 | } |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 600 | |
John McCall | f3eb96f | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 601 | llvm::Value *Receiver = LV.getPropertyRefBaseAddr(); |
John McCall | 0692a32 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 602 | |
| 603 | // Accesses to 'super' follow a different code path. |
| 604 | if (E->isSuperReceiver()) |
| 605 | return GenerateMessageSendSuper(*this, Return, ResultType, |
| 606 | S, Receiver, CallArgList()); |
| 607 | |
John McCall | f3eb96f | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 608 | const ObjCInterfaceDecl *ReceiverClass |
| 609 | = (E->isClassReceiver() ? E->getClassReceiver() : 0); |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 610 | return CGM.getObjCRuntime(). |
| 611 | GenerateMessageSend(*this, Return, ResultType, S, |
| 612 | Receiver, CallArgList(), ReceiverClass); |
Daniel Dunbar | 55310df | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 613 | } |
| 614 | |
John McCall | f3eb96f | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 615 | void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src, |
| 616 | LValue Dst) { |
| 617 | const ObjCPropertyRefExpr *E = Dst.getPropertyRefExpr(); |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 618 | Selector S = E->getSetterSelector(); |
| 619 | QualType ArgType; |
| 620 | if (E->isImplicitProperty()) { |
| 621 | const ObjCMethodDecl *Setter = E->getImplicitPropertySetter(); |
| 622 | ObjCMethodDecl::param_iterator P = Setter->param_begin(); |
| 623 | ArgType = (*P)->getType(); |
| 624 | } else { |
| 625 | ArgType = E->getType(); |
| 626 | } |
Fariborz Jahanian | 701f094 | 2011-02-08 22:33:23 +0000 | [diff] [blame] | 627 | // FIXME. Other than scalars, AST is not adequate for setter and |
| 628 | // getter type mismatches which require conversion. |
| 629 | if (Src.isScalar()) { |
| 630 | llvm::Value *SrcVal = Src.getScalarVal(); |
| 631 | QualType DstType = getContext().getCanonicalType(ArgType); |
| 632 | const llvm::Type *DstTy = ConvertType(DstType); |
| 633 | if (SrcVal->getType() != DstTy) |
| 634 | Src = |
| 635 | RValue::get(EmitScalarConversion(SrcVal, E->getType(), DstType)); |
| 636 | } |
| 637 | |
John McCall | 0692a32 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 638 | CallArgList Args; |
| 639 | Args.push_back(std::make_pair(Src, ArgType)); |
| 640 | |
| 641 | llvm::Value *Receiver = Dst.getPropertyRefBaseAddr(); |
| 642 | QualType ResultType = getContext().VoidTy; |
| 643 | |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 644 | if (E->isSuperReceiver()) { |
John McCall | 0692a32 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 645 | GenerateMessageSendSuper(*this, ReturnValueSlot(), |
| 646 | ResultType, S, Receiver, Args); |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 647 | return; |
| 648 | } |
| 649 | |
John McCall | f3eb96f | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 650 | const ObjCInterfaceDecl *ReceiverClass |
| 651 | = (E->isClassReceiver() ? E->getClassReceiver() : 0); |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 652 | |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 653 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
John McCall | 0692a32 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 654 | ResultType, S, Receiver, Args, |
| 655 | ReceiverClass); |
Daniel Dunbar | 9e22c0d | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 658 | void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 659 | llvm::Constant *EnumerationMutationFn = |
Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 660 | CGM.getObjCRuntime().EnumerationMutationFunction(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 661 | |
Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 662 | if (!EnumerationMutationFn) { |
| 663 | CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime"); |
| 664 | return; |
| 665 | } |
| 666 | |
John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 667 | // The local variable comes into scope immediately. |
| 668 | AutoVarEmission variable = AutoVarEmission::invalid(); |
| 669 | if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) |
| 670 | variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl())); |
| 671 | |
Devang Patel | d2d6665 | 2011-01-19 01:36:36 +0000 | [diff] [blame] | 672 | CGDebugInfo *DI = getDebugInfo(); |
| 673 | if (DI) { |
| 674 | DI->setLocation(S.getSourceRange().getBegin()); |
| 675 | DI->EmitRegionStart(Builder); |
| 676 | } |
| 677 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 678 | JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end"); |
| 679 | JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 680 | |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 681 | // Fast enumeration state. |
| 682 | QualType StateTy = getContext().getObjCFastEnumerationStateType(); |
Daniel Dunbar | a7566f1 | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 683 | llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr"); |
Anders Carlsson | c0964b6 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 684 | EmitNullInitialization(StatePtr, StateTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 685 | |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 686 | // Number of elements in the items array. |
Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 687 | static const unsigned NumItems = 16; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 688 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 689 | // Fetch the countByEnumeratingWithState:objects:count: selector. |
Benjamin Kramer | 9e649c3 | 2010-03-30 11:36:44 +0000 | [diff] [blame] | 690 | IdentifierInfo *II[] = { |
| 691 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 692 | &CGM.getContext().Idents.get("objects"), |
| 693 | &CGM.getContext().Idents.get("count") |
| 694 | }; |
| 695 | Selector FastEnumSel = |
| 696 | CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]); |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 697 | |
| 698 | QualType ItemsTy = |
| 699 | getContext().getConstantArrayType(getContext().getObjCIdType(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 700 | llvm::APInt(32, NumItems), |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 701 | ArrayType::Normal, 0); |
Daniel Dunbar | a7566f1 | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 702 | llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 703 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 704 | // Emit the collection pointer. |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 705 | llvm::Value *Collection = EmitScalarExpr(S.getCollection()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 706 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 707 | // Send it our message: |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 708 | CallArgList Args; |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 709 | |
| 710 | // The first argument is a temporary of the enumeration-state type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 711 | Args.push_back(std::make_pair(RValue::get(StatePtr), |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 712 | getContext().getPointerType(StateTy))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 714 | // The second argument is a temporary array with space for NumItems |
| 715 | // pointers. We'll actually be loading elements from the array |
| 716 | // pointer written into the control state; this buffer is so that |
| 717 | // collections that *aren't* backed by arrays can still queue up |
| 718 | // batches of elements. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 719 | Args.push_back(std::make_pair(RValue::get(ItemsPtr), |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 720 | getContext().getPointerType(ItemsTy))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 721 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 722 | // The third argument is the capacity of that temporary array. |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 723 | const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 724 | llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 725 | Args.push_back(std::make_pair(RValue::get(Count), |
Daniel Dunbar | 41cf9de | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 726 | getContext().UnsignedLongTy)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 727 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 728 | // Start the enumeration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 729 | RValue CountRV = |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 730 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 731 | getContext().UnsignedLongTy, |
| 732 | FastEnumSel, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 733 | Collection, Args); |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 734 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 735 | // The initial number of objects that were returned in the buffer. |
| 736 | llvm::Value *initialBufferLimit = CountRV.getScalarVal(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 738 | llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty"); |
| 739 | llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 740 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 741 | llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy); |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 742 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 743 | // If the limit pointer was zero to begin with, the collection is |
| 744 | // empty; skip all this. |
| 745 | Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"), |
| 746 | EmptyBB, LoopInitBB); |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 747 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 748 | // Otherwise, initialize the loop. |
| 749 | EmitBlock(LoopInitBB); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 750 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 751 | // Save the initial mutations value. This is the value at an |
| 752 | // address that was written into the state object by |
| 753 | // countByEnumeratingWithState:objects:count:. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 754 | llvm::Value *StateMutationsPtrPtr = |
Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 755 | Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 756 | llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, |
Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 757 | "mutationsptr"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 758 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 759 | llvm::Value *initialMutations = |
| 760 | Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 761 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 762 | // Start looping. This is the point we return to whenever we have a |
| 763 | // fresh, non-empty batch of objects. |
| 764 | llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody"); |
| 765 | EmitBlock(LoopBodyBB); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 767 | // The current index into the buffer. |
| 768 | llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, "forcoll.index"); |
| 769 | index->addIncoming(zero, LoopInitBB); |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 770 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 771 | // The current buffer size. |
| 772 | llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, "forcoll.count"); |
| 773 | count->addIncoming(initialBufferLimit, LoopInitBB); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 775 | // Check whether the mutations value has changed from where it was |
| 776 | // at start. StateMutationsPtr should actually be invariant between |
| 777 | // refreshes. |
Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 778 | StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 779 | llvm::Value *currentMutations |
| 780 | = Builder.CreateLoad(StateMutationsPtr, "statemutations"); |
Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 781 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 782 | llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated"); |
| 783 | llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcool.notmutated"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 784 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 785 | Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations), |
| 786 | WasNotMutatedBB, WasMutatedBB); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 787 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 788 | // If so, call the enumeration-mutation function. |
| 789 | EmitBlock(WasMutatedBB); |
Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 790 | llvm::Value *V = |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 791 | Builder.CreateBitCast(Collection, |
Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 792 | ConvertType(getContext().getObjCIdType()), |
| 793 | "tmp"); |
Daniel Dunbar | 84388bf | 2009-02-03 23:55:40 +0000 | [diff] [blame] | 794 | CallArgList Args2; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 795 | Args2.push_back(std::make_pair(RValue::get(V), |
Daniel Dunbar | 84388bf | 2009-02-03 23:55:40 +0000 | [diff] [blame] | 796 | getContext().getObjCIdType())); |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 797 | // FIXME: We shouldn't need to get the function info here, the runtime already |
| 798 | // should have computed it to build the function. |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 799 | EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 800 | FunctionType::ExtInfo()), |
Anders Carlsson | 61a401c | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 801 | EnumerationMutationFn, ReturnValueSlot(), Args2); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 802 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 803 | // Otherwise, or if the mutation function returns, just continue. |
| 804 | EmitBlock(WasNotMutatedBB); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 806 | // Initialize the element variable. |
| 807 | RunCleanupsScope elementVariableScope(*this); |
John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 808 | bool elementIsVariable; |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 809 | LValue elementLValue; |
| 810 | QualType elementType; |
| 811 | if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) { |
John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 812 | // Initialize the variable, in case it's a __block variable or something. |
| 813 | EmitAutoVarInit(variable); |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 814 | |
John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 815 | const VarDecl* D = cast<VarDecl>(SD->getSingleDecl()); |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 816 | DeclRefExpr tempDRE(const_cast<VarDecl*>(D), D->getType(), |
| 817 | VK_LValue, SourceLocation()); |
| 818 | elementLValue = EmitLValue(&tempDRE); |
| 819 | elementType = D->getType(); |
John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 820 | elementIsVariable = true; |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 821 | } else { |
| 822 | elementLValue = LValue(); // suppress warning |
| 823 | elementType = cast<Expr>(S.getElement())->getType(); |
John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 824 | elementIsVariable = false; |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 825 | } |
| 826 | const llvm::Type *convertedElementType = ConvertType(elementType); |
| 827 | |
| 828 | // Fetch the buffer out of the enumeration state. |
| 829 | // TODO: this pointer should actually be invariant between |
| 830 | // refreshes, which would help us do certain loop optimizations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 831 | llvm::Value *StateItemsPtr = |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 832 | Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr"); |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 833 | llvm::Value *EnumStateItems = |
| 834 | Builder.CreateLoad(StateItemsPtr, "stateitems"); |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 835 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 836 | // Fetch the value at the current index from the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 837 | llvm::Value *CurrentItemPtr = |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 838 | Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr"); |
| 839 | llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 840 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 841 | // Cast that value to the right type. |
| 842 | CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType, |
| 843 | "currentitem"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 844 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 845 | // Make sure we have an l-value. Yes, this gets evaluated every |
| 846 | // time through the loop. |
John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 847 | if (!elementIsVariable) |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 848 | elementLValue = EmitLValue(cast<Expr>(S.getElement())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 850 | EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue, elementType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 851 | |
John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 852 | // If we do have an element variable, this assignment is the end of |
| 853 | // its initialization. |
| 854 | if (elementIsVariable) |
| 855 | EmitAutoVarCleanups(variable); |
| 856 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 857 | // Perform the loop body, setting up break and continue labels. |
Anders Carlsson | 33747b6 | 2009-02-10 05:52:02 +0000 | [diff] [blame] | 858 | BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody)); |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 859 | { |
| 860 | RunCleanupsScope Scope(*this); |
| 861 | EmitStmt(S.getBody()); |
| 862 | } |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 863 | BreakContinueStack.pop_back(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 864 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 865 | // Destroy the element variable now. |
| 866 | elementVariableScope.ForceCleanup(); |
| 867 | |
| 868 | // Check whether there are more elements. |
John McCall | ad5d61e | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 869 | EmitBlock(AfterBody.getBlock()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 870 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 871 | llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch"); |
Fariborz Jahanian | 6e7ecc8 | 2009-01-06 18:56:31 +0000 | [diff] [blame] | 872 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 873 | // First we check in the local buffer. |
| 874 | llvm::Value *indexPlusOne |
| 875 | = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1)); |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 876 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 877 | // If we haven't overrun the buffer yet, we can continue. |
| 878 | Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count), |
| 879 | LoopBodyBB, FetchMoreBB); |
| 880 | |
| 881 | index->addIncoming(indexPlusOne, AfterBody.getBlock()); |
| 882 | count->addIncoming(count, AfterBody.getBlock()); |
| 883 | |
| 884 | // Otherwise, we have to fetch more elements. |
| 885 | EmitBlock(FetchMoreBB); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 886 | |
| 887 | CountRV = |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 888 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 889 | getContext().UnsignedLongTy, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 890 | FastEnumSel, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 891 | Collection, Args); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 892 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 893 | // If we got a zero count, we're done. |
| 894 | llvm::Value *refetchCount = CountRV.getScalarVal(); |
| 895 | |
| 896 | // (note that the message send might split FetchMoreBB) |
| 897 | index->addIncoming(zero, Builder.GetInsertBlock()); |
| 898 | count->addIncoming(refetchCount, Builder.GetInsertBlock()); |
| 899 | |
| 900 | Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero), |
| 901 | EmptyBB, LoopBodyBB); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 903 | // No more elements. |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 904 | EmitBlock(EmptyBB); |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 905 | |
John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 906 | if (!elementIsVariable) { |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 907 | // If the element was not a declaration, set it to be null. |
| 908 | |
John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 909 | llvm::Value *null = llvm::Constant::getNullValue(convertedElementType); |
| 910 | elementLValue = EmitLValue(cast<Expr>(S.getElement())); |
| 911 | EmitStoreThroughLValue(RValue::get(null), elementLValue, elementType); |
Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Devang Patel | d2d6665 | 2011-01-19 01:36:36 +0000 | [diff] [blame] | 914 | if (DI) { |
| 915 | DI->setLocation(S.getSourceRange().getEnd()); |
| 916 | DI->EmitRegionEnd(Builder); |
| 917 | } |
| 918 | |
John McCall | ad5d61e | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 919 | EmitBlock(LoopEnd.getBlock()); |
Anders Carlsson | 2e744e8 | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 922 | void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) { |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 923 | CGM.getObjCRuntime().EmitTryStmt(*this, S); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 926 | void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) { |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 927 | CGM.getObjCRuntime().EmitThrowStmt(*this, S); |
| 928 | } |
| 929 | |
Chris Lattner | e132e24 | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 930 | void CodeGenFunction::EmitObjCAtSynchronizedStmt( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 931 | const ObjCAtSynchronizedStmt &S) { |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 932 | CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S); |
Chris Lattner | e132e24 | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Ted Kremenek | 43e0633 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 935 | CGObjCRuntime::~CGObjCRuntime() {} |