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