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