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) { |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +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. |
| 124 | if (CGM.getDebugInfo() && !OMD->hasAttr<NoDebugAttr>()) |
| 125 | DebugInfo = CGM.getDebugInfo(); |
| 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 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | Args.push_back(std::make_pair(OMD->getSelfDecl(), |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 133 | OMD->getSelfDecl()->getType())); |
| 134 | Args.push_back(std::make_pair(OMD->getCmdDecl(), |
| 135 | OMD->getCmdDecl()->getType())); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 137 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 138 | E = OMD->param_end(); PI != E; ++PI) |
| 139 | Args.push_back(std::make_pair(*PI, (*PI)->getType())); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 140 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 141 | CurGD = OMD; |
| 142 | |
Devang Patel | a92d613 | 2010-02-15 18:08:38 +0000 | [diff] [blame] | 143 | StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocStart()); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 144 | } |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 145 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 146 | /// 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] | 147 | /// its pointer, name, and types registered in the class struture. |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 148 | void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 149 | StartObjCMethod(OMD, OMD->getClassInterface()); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 150 | EmitStmt(OMD->getBody()); |
| 151 | FinishFunction(OMD->getBodyRBrace()); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 154 | // FIXME: I wasn't sure about the synthesis approach. If we end up generating an |
| 155 | // AST for the whole body we can just fall back to having a GenerateFunction |
| 156 | // which takes the body Stmt. |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 157 | |
| 158 | /// GenerateObjCGetter - Generate an Objective-C property getter |
Steve Naroff | 489034c | 2009-01-10 22:55:25 +0000 | [diff] [blame] | 159 | /// function. The given Decl must be an ObjCImplementationDecl. @synthesize |
| 160 | /// is illegal within a category. |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 161 | void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP, |
| 162 | const ObjCPropertyImplDecl *PID) { |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 163 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 164 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 165 | bool IsAtomic = |
| 166 | !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 167 | ObjCMethodDecl *OMD = PD->getGetterMethodDecl(); |
| 168 | assert(OMD && "Invalid call to generate getter (empty method)"); |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 169 | StartObjCMethod(OMD, IMP->getClassInterface()); |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 170 | |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 171 | // Determine if we should use an objc_getProperty call for |
Fariborz Jahanian | 447d7ae | 2008-12-08 23:56:17 +0000 | [diff] [blame] | 172 | // this. Non-atomic properties are directly evaluated. |
| 173 | // atomic 'copy' and 'retain' properties are also directly |
| 174 | // evaluated in gc-only mode. |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 175 | if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly && |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 176 | IsAtomic && |
Fariborz Jahanian | 447d7ae | 2008-12-08 23:56:17 +0000 | [diff] [blame] | 177 | (PD->getSetterKind() == ObjCPropertyDecl::Copy || |
| 178 | PD->getSetterKind() == ObjCPropertyDecl::Retain)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | llvm::Value *GetPropertyFn = |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 180 | CGM.getObjCRuntime().GetPropertyGetFunction(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 182 | if (!GetPropertyFn) { |
| 183 | CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy"); |
| 184 | FinishFunction(); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true). |
| 189 | // FIXME: Can't this be simpler? This might even be worse than the |
| 190 | // corresponding gcc code. |
| 191 | CodeGenTypes &Types = CGM.getTypes(); |
| 192 | ValueDecl *Cmd = OMD->getCmdDecl(); |
| 193 | llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd"); |
| 194 | QualType IdTy = getContext().getObjCIdType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | llvm::Value *SelfAsId = |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 196 | Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 197 | llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar); |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 198 | llvm::Value *True = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 199 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1); |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 200 | CallArgList Args; |
| 201 | Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy)); |
| 202 | Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType())); |
| 203 | Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy)); |
| 204 | Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy)); |
Daniel Dunbar | e4be5a6 | 2009-02-03 23:43:59 +0000 | [diff] [blame] | 205 | // FIXME: We shouldn't need to get the function info here, the |
| 206 | // runtime already should have computed it to build the function. |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 207 | RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 208 | FunctionType::ExtInfo()), |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 209 | GetPropertyFn, ReturnValueSlot(), Args); |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 210 | // We need to fix the type here. Ivars with copy & retain are |
| 211 | // always objects so we don't need to worry about complex or |
| 212 | // aggregates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 213 | RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(), |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 214 | Types.ConvertType(PD->getType()))); |
| 215 | EmitReturnOfRValue(RV, PD->getType()); |
| 216 | } else { |
Fariborz Jahanian | 1b23fe6 | 2010-03-25 21:56:43 +0000 | [diff] [blame] | 217 | if (Ivar->getType()->isAnyComplexType()) { |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 218 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), |
| 219 | Ivar, 0); |
Fariborz Jahanian | 1b23fe6 | 2010-03-25 21:56:43 +0000 | [diff] [blame] | 220 | ComplexPairTy Pair = LoadComplexFromAddr(LV.getAddress(), |
| 221 | LV.isVolatileQualified()); |
| 222 | StoreComplexToAddr(Pair, ReturnValue, LV.isVolatileQualified()); |
| 223 | } |
| 224 | else if (hasAggregateLLVMType(Ivar->getType())) { |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 225 | bool IsStrong = false; |
| 226 | if ((IsAtomic || (IsStrong = IvarTypeWithAggrGCObjects(Ivar->getType()))) |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 227 | && CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 228 | && CGM.getObjCRuntime().GetGetStructFunction()) { |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 229 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), |
| 230 | Ivar, 0); |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 231 | llvm::Value *GetCopyStructFn = |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 232 | CGM.getObjCRuntime().GetGetStructFunction(); |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 233 | CodeGenTypes &Types = CGM.getTypes(); |
| 234 | // objc_copyStruct (ReturnValue, &structIvar, |
| 235 | // sizeof (Type of Ivar), isAtomic, false); |
| 236 | CallArgList Args; |
| 237 | RValue RV = RValue::get(Builder.CreateBitCast(ReturnValue, |
| 238 | Types.ConvertType(getContext().VoidPtrTy))); |
| 239 | Args.push_back(std::make_pair(RV, getContext().VoidPtrTy)); |
| 240 | RV = RValue::get(Builder.CreateBitCast(LV.getAddress(), |
| 241 | Types.ConvertType(getContext().VoidPtrTy))); |
| 242 | Args.push_back(std::make_pair(RV, getContext().VoidPtrTy)); |
| 243 | // sizeof (Type of Ivar) |
| 244 | uint64_t Size = getContext().getTypeSize(Ivar->getType()) / 8; |
| 245 | llvm::Value *SizeVal = |
| 246 | llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), Size); |
| 247 | Args.push_back(std::make_pair(RValue::get(SizeVal), |
| 248 | getContext().LongTy)); |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 249 | llvm::Value *isAtomic = |
Fariborz Jahanian | 08adf32 | 2010-04-13 18:43:37 +0000 | [diff] [blame] | 250 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), |
| 251 | IsAtomic ? 1 : 0); |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 252 | Args.push_back(std::make_pair(RValue::get(isAtomic), |
| 253 | getContext().BoolTy)); |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 254 | llvm::Value *hasStrong = |
| 255 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), |
| 256 | IsStrong ? 1 : 0); |
| 257 | Args.push_back(std::make_pair(RValue::get(hasStrong), |
| 258 | getContext().BoolTy)); |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 259 | EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args, |
| 260 | FunctionType::ExtInfo()), |
| 261 | GetCopyStructFn, ReturnValueSlot(), Args); |
| 262 | } |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 263 | else { |
| 264 | if (PID->getGetterCXXConstructor()) { |
| 265 | ReturnStmt *Stmt = |
| 266 | new (getContext()) ReturnStmt(SourceLocation(), |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 267 | PID->getGetterCXXConstructor(), |
| 268 | 0); |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 269 | EmitReturnStmt(*Stmt); |
| 270 | } |
| 271 | else { |
| 272 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), |
| 273 | Ivar, 0); |
| 274 | EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType()); |
| 275 | } |
| 276 | } |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 277 | } else { |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 278 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), |
| 279 | Ivar, 0); |
Fariborz Jahanian | ed1d29d | 2009-03-03 18:49:40 +0000 | [diff] [blame] | 280 | CodeGenTypes &Types = CGM.getTypes(); |
| 281 | RValue RV = EmitLoadOfLValue(LV, Ivar->getType()); |
| 282 | RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | Types.ConvertType(PD->getType()))); |
Fariborz Jahanian | ed1d29d | 2009-03-03 18:49:40 +0000 | [diff] [blame] | 284 | EmitReturnOfRValue(RV, PD->getType()); |
| 285 | } |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 286 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 287 | |
| 288 | FinishFunction(); |
| 289 | } |
| 290 | |
| 291 | /// GenerateObjCSetter - Generate an Objective-C property setter |
Steve Naroff | 489034c | 2009-01-10 22:55:25 +0000 | [diff] [blame] | 292 | /// function. The given Decl must be an ObjCImplementationDecl. @synthesize |
| 293 | /// is illegal within a category. |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 294 | void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP, |
| 295 | const ObjCPropertyImplDecl *PID) { |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 296 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 297 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 298 | ObjCMethodDecl *OMD = PD->getSetterMethodDecl(); |
| 299 | assert(OMD && "Invalid call to generate setter (empty method)"); |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 300 | StartObjCMethod(OMD, IMP->getClassInterface()); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 301 | |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 302 | bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | bool IsAtomic = |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 304 | !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic); |
| 305 | |
| 306 | // Determine if we should use an objc_setProperty call for |
| 307 | // this. Properties with 'copy' semantics always use it, as do |
| 308 | // non-atomic properties with 'release' semantics as long as we are |
| 309 | // not in gc-only mode. |
| 310 | if (IsCopy || |
| 311 | (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly && |
| 312 | PD->getSetterKind() == ObjCPropertyDecl::Retain)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | llvm::Value *SetPropertyFn = |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 314 | CGM.getObjCRuntime().GetPropertySetFunction(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 316 | if (!SetPropertyFn) { |
| 317 | CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy"); |
| 318 | FinishFunction(); |
| 319 | return; |
| 320 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
| 322 | // Emit objc_setProperty((id) self, _cmd, offset, arg, |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 323 | // <is-atomic>, <is-copy>). |
| 324 | // FIXME: Can't this be simpler? This might even be worse than the |
| 325 | // corresponding gcc code. |
| 326 | CodeGenTypes &Types = CGM.getTypes(); |
| 327 | ValueDecl *Cmd = OMD->getCmdDecl(); |
| 328 | llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd"); |
| 329 | QualType IdTy = getContext().getObjCIdType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | llvm::Value *SelfAsId = |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 331 | Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 332 | llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 333 | llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | llvm::Value *ArgAsId = |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 335 | Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"), |
| 336 | Types.ConvertType(IdTy)); |
| 337 | llvm::Value *True = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 338 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1); |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 339 | llvm::Value *False = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 340 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0); |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 341 | CallArgList Args; |
| 342 | Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy)); |
| 343 | Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType())); |
| 344 | Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy)); |
| 345 | Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False), |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 347 | getContext().BoolTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False), |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 349 | getContext().BoolTy)); |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 350 | // FIXME: We shouldn't need to get the function info here, the runtime |
| 351 | // already should have computed it to build the function. |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 352 | EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 353 | FunctionType::ExtInfo()), |
| 354 | SetPropertyFn, |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 355 | ReturnValueSlot(), Args); |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 356 | } else if (IsAtomic && hasAggregateLLVMType(Ivar->getType()) && |
| 357 | !Ivar->getType()->isAnyComplexType() && |
| 358 | IndirectObjCSetterArg(*CurFnInfo) |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 359 | && CGM.getObjCRuntime().GetSetStructFunction()) { |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 360 | // objc_copyStruct (&structIvar, &Arg, |
| 361 | // sizeof (struct something), true, false); |
| 362 | llvm::Value *GetCopyStructFn = |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 363 | CGM.getObjCRuntime().GetSetStructFunction(); |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 364 | CodeGenTypes &Types = CGM.getTypes(); |
| 365 | CallArgList Args; |
| 366 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0); |
| 367 | RValue RV = RValue::get(Builder.CreateBitCast(LV.getAddress(), |
| 368 | Types.ConvertType(getContext().VoidPtrTy))); |
| 369 | Args.push_back(std::make_pair(RV, getContext().VoidPtrTy)); |
| 370 | llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()]; |
| 371 | llvm::Value *ArgAsPtrTy = |
| 372 | Builder.CreateBitCast(Arg, |
| 373 | Types.ConvertType(getContext().VoidPtrTy)); |
| 374 | RV = RValue::get(ArgAsPtrTy); |
| 375 | Args.push_back(std::make_pair(RV, getContext().VoidPtrTy)); |
| 376 | // sizeof (Type of Ivar) |
| 377 | uint64_t Size = getContext().getTypeSize(Ivar->getType()) / 8; |
| 378 | llvm::Value *SizeVal = |
| 379 | llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), Size); |
| 380 | Args.push_back(std::make_pair(RValue::get(SizeVal), |
| 381 | getContext().LongTy)); |
| 382 | llvm::Value *True = |
| 383 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1); |
| 384 | Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy)); |
| 385 | llvm::Value *False = |
| 386 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0); |
| 387 | Args.push_back(std::make_pair(RValue::get(False), getContext().BoolTy)); |
| 388 | EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args, |
| 389 | FunctionType::ExtInfo()), |
| 390 | GetCopyStructFn, ReturnValueSlot(), Args); |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 391 | } else if (PID->getSetterCXXAssignment()) { |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 392 | EmitIgnoredExpr(PID->getSetterCXXAssignment()); |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 393 | } else { |
Daniel Dunbar | 45e8423 | 2009-10-27 19:21:30 +0000 | [diff] [blame] | 394 | // FIXME: Find a clean way to avoid AST node creation. |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 395 | SourceLocation Loc = PD->getLocation(); |
| 396 | ValueDecl *Self = OMD->getSelfDecl(); |
| 397 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 398 | DeclRefExpr Base(Self, Self->getType(), VK_RValue, Loc); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 399 | ParmVarDecl *ArgDecl = *OMD->param_begin(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 400 | DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), VK_LValue, Loc); |
Daniel Dunbar | 45e8423 | 2009-10-27 19:21:30 +0000 | [diff] [blame] | 401 | ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true); |
| 402 | |
| 403 | // The property type can differ from the ivar type in some situations with |
| 404 | // Objective-C pointer types, we can always bit cast the RHS in these cases. |
| 405 | if (getContext().getCanonicalType(Ivar->getType()) != |
| 406 | getContext().getCanonicalType(ArgDecl->getType())) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 407 | ImplicitCastExpr ArgCasted(ImplicitCastExpr::OnStack, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 408 | Ivar->getType(), CK_BitCast, &Arg, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 409 | VK_RValue); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 410 | BinaryOperator Assign(&IvarRef, &ArgCasted, BO_Assign, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 411 | Ivar->getType(), VK_RValue, OK_Ordinary, Loc); |
Daniel Dunbar | 45e8423 | 2009-10-27 19:21:30 +0000 | [diff] [blame] | 412 | EmitStmt(&Assign); |
| 413 | } else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 414 | BinaryOperator Assign(&IvarRef, &Arg, BO_Assign, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 415 | Ivar->getType(), VK_RValue, OK_Ordinary, Loc); |
Daniel Dunbar | 45e8423 | 2009-10-27 19:21:30 +0000 | [diff] [blame] | 416 | EmitStmt(&Assign); |
| 417 | } |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 418 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 419 | |
| 420 | FinishFunction(); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 423 | void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, |
| 424 | ObjCMethodDecl *MD, |
| 425 | bool ctor) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 426 | llvm::SmallVector<CXXCtorInitializer *, 8> IvarInitializers; |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 427 | MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface()); |
| 428 | StartObjCMethod(MD, IMP->getClassInterface()); |
| 429 | for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(), |
| 430 | E = IMP->init_end(); B != E; ++B) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 431 | CXXCtorInitializer *Member = (*B); |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 432 | IvarInitializers.push_back(Member); |
| 433 | } |
| 434 | if (ctor) { |
| 435 | for (unsigned I = 0, E = IvarInitializers.size(); I != E; ++I) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 436 | CXXCtorInitializer *IvarInit = IvarInitializers[I]; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 437 | FieldDecl *Field = IvarInit->getAnyMember(); |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 438 | ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); |
Fariborz Jahanian | 9b4d4fc | 2010-04-28 22:30:33 +0000 | [diff] [blame] | 439 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), |
| 440 | LoadObjCSelf(), Ivar, 0); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 441 | EmitAggExpr(IvarInit->getInit(), AggValueSlot::forLValue(LV, true)); |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 442 | } |
| 443 | // constructor returns 'self'. |
| 444 | CodeGenTypes &Types = CGM.getTypes(); |
| 445 | QualType IdTy(CGM.getContext().getObjCIdType()); |
| 446 | llvm::Value *SelfAsId = |
| 447 | Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); |
| 448 | EmitReturnOfRValue(RValue::get(SelfAsId), IdTy); |
Chandler Carruth | bc397cf | 2010-05-06 00:20:39 +0000 | [diff] [blame] | 449 | } else { |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 450 | // dtor |
| 451 | for (size_t i = IvarInitializers.size(); i > 0; --i) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 452 | FieldDecl *Field = IvarInitializers[i - 1]->getAnyMember(); |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 453 | QualType FieldType = Field->getType(); |
Fariborz Jahanian | 9b4d4fc | 2010-04-28 22:30:33 +0000 | [diff] [blame] | 454 | const ConstantArrayType *Array = |
| 455 | getContext().getAsConstantArrayType(FieldType); |
| 456 | if (Array) |
| 457 | FieldType = getContext().getBaseElementType(FieldType); |
| 458 | |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 459 | ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); |
| 460 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), |
| 461 | LoadObjCSelf(), Ivar, 0); |
| 462 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 463 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 464 | CXXDestructorDecl *Dtor = FieldClassDecl->getDestructor(); |
Chandler Carruth | bc397cf | 2010-05-06 00:20:39 +0000 | [diff] [blame] | 465 | if (!Dtor->isTrivial()) { |
Fariborz Jahanian | 8b688ed | 2010-05-04 19:29:32 +0000 | [diff] [blame] | 466 | if (Array) { |
| 467 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 468 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 469 | llvm::Value *BaseAddrPtr = |
| 470 | Builder.CreateBitCast(LV.getAddress(), BasePtr); |
| 471 | EmitCXXAggrDestructorCall(Dtor, |
| 472 | Array, BaseAddrPtr); |
Chandler Carruth | bc397cf | 2010-05-06 00:20:39 +0000 | [diff] [blame] | 473 | } else { |
Fariborz Jahanian | 8b688ed | 2010-05-04 19:29:32 +0000 | [diff] [blame] | 474 | EmitCXXDestructorCall(Dtor, |
| 475 | Dtor_Complete, /*ForVirtualBase=*/false, |
| 476 | LV.getAddress()); |
Chandler Carruth | bc397cf | 2010-05-06 00:20:39 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | } |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 480 | } |
| 481 | FinishFunction(); |
| 482 | } |
| 483 | |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 484 | bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) { |
| 485 | CGFunctionInfo::const_arg_iterator it = FI.arg_begin(); |
| 486 | it++; it++; |
| 487 | const ABIArgInfo &AI = it->info; |
| 488 | // FIXME. Is this sufficient check? |
| 489 | return (AI.getKind() == ABIArgInfo::Indirect); |
| 490 | } |
| 491 | |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 492 | bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) { |
| 493 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 494 | return false; |
| 495 | if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>()) |
| 496 | return FDTTy->getDecl()->hasObjectMember(); |
| 497 | return false; |
| 498 | } |
| 499 | |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 500 | llvm::Value *CodeGenFunction::LoadObjCSelf() { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 501 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 502 | return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self"); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 505 | QualType CodeGenFunction::TypeOfSelfObject() { |
| 506 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 507 | ImplicitParamDecl *selfDecl = OMD->getSelfDecl(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 508 | const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>( |
| 509 | getContext().getCanonicalType(selfDecl->getType())); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 510 | return PTy->getPointeeType(); |
| 511 | } |
| 512 | |
John McCall | e68b984 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 513 | LValue |
| 514 | CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) { |
| 515 | // This is a special l-value that just issues sends when we load or |
| 516 | // store through it. |
| 517 | |
| 518 | // For certain base kinds, we need to emit the base immediately. |
| 519 | llvm::Value *Base; |
| 520 | if (E->isSuperReceiver()) |
| 521 | Base = LoadObjCSelf(); |
| 522 | else if (E->isClassReceiver()) |
| 523 | Base = CGM.getObjCRuntime().GetClass(Builder, E->getClassReceiver()); |
| 524 | else |
| 525 | Base = EmitScalarExpr(E->getBase()); |
| 526 | return LValue::MakePropertyRef(E, Base); |
| 527 | } |
| 528 | |
| 529 | static RValue GenerateMessageSendSuper(CodeGenFunction &CGF, |
| 530 | ReturnValueSlot Return, |
| 531 | QualType ResultType, |
| 532 | Selector S, |
| 533 | llvm::Value *Receiver, |
| 534 | const CallArgList &CallArgs) { |
| 535 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CGF.CurFuncDecl); |
Fariborz Jahanian | f469557 | 2009-03-20 19:18:21 +0000 | [diff] [blame] | 536 | bool isClassMessage = OMD->isClassMethod(); |
| 537 | bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
John McCall | e68b984 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 538 | return CGF.CGM.getObjCRuntime() |
| 539 | .GenerateMessageSendSuper(CGF, Return, ResultType, |
| 540 | S, OMD->getClassInterface(), |
| 541 | isCategoryImpl, Receiver, |
| 542 | isClassMessage, CallArgs); |
Fariborz Jahanian | f469557 | 2009-03-20 19:18:21 +0000 | [diff] [blame] | 543 | } |
| 544 | |
John McCall | 119a1c6 | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 545 | RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV, |
| 546 | ReturnValueSlot Return) { |
| 547 | const ObjCPropertyRefExpr *E = LV.getPropertyRefExpr(); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 548 | QualType ResultType; |
| 549 | Selector S; |
| 550 | if (E->isExplicitProperty()) { |
| 551 | const ObjCPropertyDecl *Property = E->getExplicitProperty(); |
| 552 | S = Property->getGetterName(); |
| 553 | ResultType = E->getType(); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 554 | } else { |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 555 | const ObjCMethodDecl *Getter = E->getImplicitPropertyGetter(); |
| 556 | S = Getter->getSelector(); |
| 557 | ResultType = Getter->getResultType(); // with reference! |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 558 | } |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 559 | |
John McCall | 119a1c6 | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 560 | llvm::Value *Receiver = LV.getPropertyRefBaseAddr(); |
John McCall | e68b984 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 561 | |
| 562 | // Accesses to 'super' follow a different code path. |
| 563 | if (E->isSuperReceiver()) |
| 564 | return GenerateMessageSendSuper(*this, Return, ResultType, |
| 565 | S, Receiver, CallArgList()); |
| 566 | |
John McCall | 119a1c6 | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 567 | const ObjCInterfaceDecl *ReceiverClass |
| 568 | = (E->isClassReceiver() ? E->getClassReceiver() : 0); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 569 | return CGM.getObjCRuntime(). |
| 570 | GenerateMessageSend(*this, Return, ResultType, S, |
| 571 | Receiver, CallArgList(), ReceiverClass); |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 572 | } |
| 573 | |
John McCall | 119a1c6 | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 574 | void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src, |
| 575 | LValue Dst) { |
| 576 | const ObjCPropertyRefExpr *E = Dst.getPropertyRefExpr(); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 577 | Selector S = E->getSetterSelector(); |
| 578 | QualType ArgType; |
| 579 | if (E->isImplicitProperty()) { |
| 580 | const ObjCMethodDecl *Setter = E->getImplicitPropertySetter(); |
| 581 | ObjCMethodDecl::param_iterator P = Setter->param_begin(); |
| 582 | ArgType = (*P)->getType(); |
| 583 | } else { |
| 584 | ArgType = E->getType(); |
| 585 | } |
| 586 | |
John McCall | e68b984 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 587 | CallArgList Args; |
| 588 | Args.push_back(std::make_pair(Src, ArgType)); |
| 589 | |
| 590 | llvm::Value *Receiver = Dst.getPropertyRefBaseAddr(); |
| 591 | QualType ResultType = getContext().VoidTy; |
| 592 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 593 | if (E->isSuperReceiver()) { |
John McCall | e68b984 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 594 | GenerateMessageSendSuper(*this, ReturnValueSlot(), |
| 595 | ResultType, S, Receiver, Args); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 596 | return; |
| 597 | } |
| 598 | |
John McCall | 119a1c6 | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 599 | const ObjCInterfaceDecl *ReceiverClass |
| 600 | = (E->isClassReceiver() ? E->getClassReceiver() : 0); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 601 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 602 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
John McCall | e68b984 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 603 | ResultType, S, Receiver, Args, |
| 604 | ReceiverClass); |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 607 | void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | llvm::Constant *EnumerationMutationFn = |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 609 | CGM.getObjCRuntime().EnumerationMutationFunction(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 611 | if (!EnumerationMutationFn) { |
| 612 | CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime"); |
| 613 | return; |
| 614 | } |
| 615 | |
Devang Patel | bcbd03a | 2011-01-19 01:36:36 +0000 | [diff] [blame^] | 616 | CGDebugInfo *DI = getDebugInfo(); |
| 617 | if (DI) { |
| 618 | DI->setLocation(S.getSourceRange().getBegin()); |
| 619 | DI->EmitRegionStart(Builder); |
| 620 | } |
| 621 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 622 | JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end"); |
| 623 | JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 625 | // Fast enumeration state. |
| 626 | QualType StateTy = getContext().getObjCFastEnumerationStateType(); |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 627 | llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr"); |
Anders Carlsson | 1884eb0 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 628 | EmitNullInitialization(StatePtr, StateTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 629 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 630 | // Number of elements in the items array. |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 631 | static const unsigned NumItems = 16; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 633 | // Fetch the countByEnumeratingWithState:objects:count: selector. |
Benjamin Kramer | ad46886 | 2010-03-30 11:36:44 +0000 | [diff] [blame] | 634 | IdentifierInfo *II[] = { |
| 635 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 636 | &CGM.getContext().Idents.get("objects"), |
| 637 | &CGM.getContext().Idents.get("count") |
| 638 | }; |
| 639 | Selector FastEnumSel = |
| 640 | CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 641 | |
| 642 | QualType ItemsTy = |
| 643 | getContext().getConstantArrayType(getContext().getObjCIdType(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | llvm::APInt(32, NumItems), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 645 | ArrayType::Normal, 0); |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 646 | llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 647 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 648 | // Emit the collection pointer. |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 649 | llvm::Value *Collection = EmitScalarExpr(S.getCollection()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 650 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 651 | // Send it our message: |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 652 | CallArgList Args; |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 653 | |
| 654 | // The first argument is a temporary of the enumeration-state type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 655 | Args.push_back(std::make_pair(RValue::get(StatePtr), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 656 | getContext().getPointerType(StateTy))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 658 | // The second argument is a temporary array with space for NumItems |
| 659 | // pointers. We'll actually be loading elements from the array |
| 660 | // pointer written into the control state; this buffer is so that |
| 661 | // collections that *aren't* backed by arrays can still queue up |
| 662 | // batches of elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 663 | Args.push_back(std::make_pair(RValue::get(ItemsPtr), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 664 | getContext().getPointerType(ItemsTy))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 665 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 666 | // The third argument is the capacity of that temporary array. |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 667 | const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 668 | llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 669 | Args.push_back(std::make_pair(RValue::get(Count), |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 670 | getContext().UnsignedLongTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 672 | // Start the enumeration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | RValue CountRV = |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 674 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 675 | getContext().UnsignedLongTy, |
| 676 | FastEnumSel, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 677 | Collection, Args); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 678 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 679 | // The initial number of objects that were returned in the buffer. |
| 680 | llvm::Value *initialBufferLimit = CountRV.getScalarVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 681 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 682 | llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty"); |
| 683 | llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 684 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 685 | llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 686 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 687 | // If the limit pointer was zero to begin with, the collection is |
| 688 | // empty; skip all this. |
| 689 | Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"), |
| 690 | EmptyBB, LoopInitBB); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 691 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 692 | // Otherwise, initialize the loop. |
| 693 | EmitBlock(LoopInitBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 694 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 695 | // Save the initial mutations value. This is the value at an |
| 696 | // address that was written into the state object by |
| 697 | // countByEnumeratingWithState:objects:count:. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 698 | llvm::Value *StateMutationsPtrPtr = |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 699 | Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 700 | llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 701 | "mutationsptr"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 702 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 703 | llvm::Value *initialMutations = |
| 704 | Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 705 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 706 | // Start looping. This is the point we return to whenever we have a |
| 707 | // fresh, non-empty batch of objects. |
| 708 | llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody"); |
| 709 | EmitBlock(LoopBodyBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 710 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 711 | // The current index into the buffer. |
| 712 | llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, "forcoll.index"); |
| 713 | index->addIncoming(zero, LoopInitBB); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 714 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 715 | // The current buffer size. |
| 716 | llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, "forcoll.count"); |
| 717 | count->addIncoming(initialBufferLimit, LoopInitBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 718 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 719 | // Check whether the mutations value has changed from where it was |
| 720 | // at start. StateMutationsPtr should actually be invariant between |
| 721 | // refreshes. |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 722 | StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 723 | llvm::Value *currentMutations |
| 724 | = Builder.CreateLoad(StateMutationsPtr, "statemutations"); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 725 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 726 | llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated"); |
| 727 | llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcool.notmutated"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 728 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 729 | Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations), |
| 730 | WasNotMutatedBB, WasMutatedBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 731 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 732 | // If so, call the enumeration-mutation function. |
| 733 | EmitBlock(WasMutatedBB); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 734 | llvm::Value *V = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 735 | Builder.CreateBitCast(Collection, |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 736 | ConvertType(getContext().getObjCIdType()), |
| 737 | "tmp"); |
Daniel Dunbar | 2b2105e | 2009-02-03 23:55:40 +0000 | [diff] [blame] | 738 | CallArgList Args2; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | Args2.push_back(std::make_pair(RValue::get(V), |
Daniel Dunbar | 2b2105e | 2009-02-03 23:55:40 +0000 | [diff] [blame] | 740 | getContext().getObjCIdType())); |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 741 | // FIXME: We shouldn't need to get the function info here, the runtime already |
| 742 | // should have computed it to build the function. |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 743 | EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 744 | FunctionType::ExtInfo()), |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 745 | EnumerationMutationFn, ReturnValueSlot(), Args2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 747 | // Otherwise, or if the mutation function returns, just continue. |
| 748 | EmitBlock(WasNotMutatedBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 749 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 750 | // Initialize the element variable. |
| 751 | RunCleanupsScope elementVariableScope(*this); |
| 752 | bool elementIsDecl; |
| 753 | LValue elementLValue; |
| 754 | QualType elementType; |
| 755 | if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) { |
| 756 | EmitStmt(SD); |
| 757 | const VarDecl* D = cast<VarDecl>(SD->getSingleDecl()); |
| 758 | |
| 759 | DeclRefExpr tempDRE(const_cast<VarDecl*>(D), D->getType(), |
| 760 | VK_LValue, SourceLocation()); |
| 761 | elementLValue = EmitLValue(&tempDRE); |
| 762 | elementType = D->getType(); |
| 763 | elementIsDecl = true; |
| 764 | } else { |
| 765 | elementLValue = LValue(); // suppress warning |
| 766 | elementType = cast<Expr>(S.getElement())->getType(); |
| 767 | elementIsDecl = false; |
| 768 | } |
| 769 | const llvm::Type *convertedElementType = ConvertType(elementType); |
| 770 | |
| 771 | // Fetch the buffer out of the enumeration state. |
| 772 | // TODO: this pointer should actually be invariant between |
| 773 | // refreshes, which would help us do certain loop optimizations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | llvm::Value *StateItemsPtr = |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 775 | Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr"); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 776 | llvm::Value *EnumStateItems = |
| 777 | Builder.CreateLoad(StateItemsPtr, "stateitems"); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 778 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 779 | // Fetch the value at the current index from the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 780 | llvm::Value *CurrentItemPtr = |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 781 | Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr"); |
| 782 | llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 784 | // Cast that value to the right type. |
| 785 | CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType, |
| 786 | "currentitem"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 787 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 788 | // Make sure we have an l-value. Yes, this gets evaluated every |
| 789 | // time through the loop. |
| 790 | if (!elementIsDecl) |
| 791 | elementLValue = EmitLValue(cast<Expr>(S.getElement())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 793 | EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue, elementType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 794 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 795 | // Perform the loop body, setting up break and continue labels. |
Anders Carlsson | e4b6d34 | 2009-02-10 05:52:02 +0000 | [diff] [blame] | 796 | BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody)); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 797 | { |
| 798 | RunCleanupsScope Scope(*this); |
| 799 | EmitStmt(S.getBody()); |
| 800 | } |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 801 | BreakContinueStack.pop_back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 802 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 803 | // Destroy the element variable now. |
| 804 | elementVariableScope.ForceCleanup(); |
| 805 | |
| 806 | // Check whether there are more elements. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 807 | EmitBlock(AfterBody.getBlock()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 808 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 809 | llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch"); |
Fariborz Jahanian | f0906c4 | 2009-01-06 18:56:31 +0000 | [diff] [blame] | 810 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 811 | // First we check in the local buffer. |
| 812 | llvm::Value *indexPlusOne |
| 813 | = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1)); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 814 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 815 | // If we haven't overrun the buffer yet, we can continue. |
| 816 | Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count), |
| 817 | LoopBodyBB, FetchMoreBB); |
| 818 | |
| 819 | index->addIncoming(indexPlusOne, AfterBody.getBlock()); |
| 820 | count->addIncoming(count, AfterBody.getBlock()); |
| 821 | |
| 822 | // Otherwise, we have to fetch more elements. |
| 823 | EmitBlock(FetchMoreBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 824 | |
| 825 | CountRV = |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 826 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 827 | getContext().UnsignedLongTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 828 | FastEnumSel, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 829 | Collection, Args); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 830 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 831 | // If we got a zero count, we're done. |
| 832 | llvm::Value *refetchCount = CountRV.getScalarVal(); |
| 833 | |
| 834 | // (note that the message send might split FetchMoreBB) |
| 835 | index->addIncoming(zero, Builder.GetInsertBlock()); |
| 836 | count->addIncoming(refetchCount, Builder.GetInsertBlock()); |
| 837 | |
| 838 | Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero), |
| 839 | EmptyBB, LoopBodyBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 840 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 841 | // No more elements. |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 842 | EmitBlock(EmptyBB); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 843 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 844 | if (!elementIsDecl) { |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 845 | // If the element was not a declaration, set it to be null. |
| 846 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 847 | llvm::Value *null = llvm::Constant::getNullValue(convertedElementType); |
| 848 | elementLValue = EmitLValue(cast<Expr>(S.getElement())); |
| 849 | EmitStoreThroughLValue(RValue::get(null), elementLValue, elementType); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Devang Patel | bcbd03a | 2011-01-19 01:36:36 +0000 | [diff] [blame^] | 852 | if (DI) { |
| 853 | DI->setLocation(S.getSourceRange().getEnd()); |
| 854 | DI->EmitRegionEnd(Builder); |
| 855 | } |
| 856 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 857 | EmitBlock(LoopEnd.getBlock()); |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 860 | void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 861 | CGM.getObjCRuntime().EmitTryStmt(*this, S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 864 | void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) { |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 865 | CGM.getObjCRuntime().EmitThrowStmt(*this, S); |
| 866 | } |
| 867 | |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 868 | void CodeGenFunction::EmitObjCAtSynchronizedStmt( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 869 | const ObjCAtSynchronizedStmt &S) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 870 | CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S); |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Ted Kremenek | 2979ec7 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 873 | CGObjCRuntime::~CGObjCRuntime() {} |