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 | |
Ted Kremenek | 2979ec7 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 14 | #include "CGObjCRuntime.h" |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
| 16 | #include "CodeGenModule.h" |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 16f0049 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 19 | #include "clang/AST/StmtObjC.h" |
Daniel Dunbar | e66f4e3 | 2008-09-03 00:27:26 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 26 | /// Emits an instance of NSConstantString representing the object. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 27 | llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) |
Daniel Dunbar | 71fcec9 | 2008-11-25 21:53:21 +0000 | [diff] [blame] | 28 | { |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 29 | llvm::Constant *C = |
| 30 | CGM.getObjCRuntime().GenerateConstantString(E->getString()); |
Daniel Dunbar | ed7c618 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 31 | // FIXME: This bitcast should just be made an invariant on the Runtime. |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 32 | return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /// Emit a selector. |
| 36 | llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) { |
| 37 | // Untyped selector. |
| 38 | // Note that this implementation allows for non-constant strings to be passed |
| 39 | // as arguments to @selector(). Currently, the only thing preventing this |
| 40 | // behaviour is the type checking in the front end. |
Daniel Dunbar | 6d5a1c2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 41 | return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector()); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Daniel Dunbar | ed7c618 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 44 | llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) { |
| 45 | // FIXME: This should pass the Decl not the name. |
| 46 | return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol()); |
| 47 | } |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 48 | |
| 49 | |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 50 | RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E, |
| 51 | ReturnValueSlot Return) { |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 52 | // Only the lookup mechanism and first two arguments of the method |
| 53 | // implementation vary between runtimes. We can get the receiver and |
| 54 | // arguments in generic code. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 56 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 57 | bool isSuperMessage = false; |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 58 | bool isClassMessage = false; |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 59 | ObjCInterfaceDecl *OID = 0; |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 60 | // Find the receiver |
Daniel Dunbar | 0b647a6 | 2010-04-22 03:17:06 +0000 | [diff] [blame] | 61 | llvm::Value *Receiver = 0; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 62 | switch (E->getReceiverKind()) { |
| 63 | case ObjCMessageExpr::Instance: |
| 64 | Receiver = EmitScalarExpr(E->getInstanceReceiver()); |
| 65 | break; |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 66 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 67 | case ObjCMessageExpr::Class: { |
John McCall | 3031c63 | 2010-05-17 20:12:43 +0000 | [diff] [blame] | 68 | const ObjCObjectType *ObjTy |
| 69 | = E->getClassReceiver()->getAs<ObjCObjectType>(); |
| 70 | assert(ObjTy && "Invalid Objective-C class message send"); |
| 71 | OID = ObjTy->getInterface(); |
| 72 | assert(OID && "Invalid Objective-C class message send"); |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 73 | Receiver = Runtime.GetClass(Builder, OID); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 74 | isClassMessage = true; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 75 | break; |
| 76 | } |
| 77 | |
| 78 | case ObjCMessageExpr::SuperInstance: |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 79 | Receiver = LoadObjCSelf(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 80 | isSuperMessage = true; |
| 81 | break; |
| 82 | |
| 83 | case ObjCMessageExpr::SuperClass: |
| 84 | Receiver = LoadObjCSelf(); |
| 85 | isSuperMessage = true; |
| 86 | isClassMessage = true; |
| 87 | break; |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 90 | CallArgList Args; |
Anders Carlsson | 131038e | 2009-04-18 20:29:27 +0000 | [diff] [blame] | 91 | EmitCallArgs(Args, E->getMethodDecl(), E->arg_begin(), E->arg_end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | |
Anders Carlsson | 7e70fb2 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 93 | QualType ResultType = |
| 94 | E->getMethodDecl() ? E->getMethodDecl()->getResultType() : E->getType(); |
| 95 | |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 96 | if (isSuperMessage) { |
Chris Lattner | 9384c76 | 2008-06-26 04:42:20 +0000 | [diff] [blame] | 97 | // super is only valid in an Objective-C method |
| 98 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 99 | bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
Anders Carlsson | 7e70fb2 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 100 | return Runtime.GenerateMessageSendSuper(*this, Return, ResultType, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 101 | E->getSelector(), |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 102 | OMD->getClassInterface(), |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 103 | isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 104 | Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 105 | isClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 106 | Args, |
| 107 | E->getMethodDecl()); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 108 | } |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 109 | |
Anders Carlsson | 7e70fb2 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 110 | return Runtime.GenerateMessageSend(*this, Return, ResultType, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 111 | E->getSelector(), |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 112 | Receiver, Args, OID, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 113 | E->getMethodDecl()); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 116 | /// StartObjCMethod - Begin emission of an ObjCMethod. This generates |
| 117 | /// the LLVM function and sets the other context used by |
| 118 | /// CodeGenFunction. |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 119 | void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD, |
| 120 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 121 | FunctionArgList Args; |
Devang Patel | 4800ea6 | 2010-04-05 21:09:15 +0000 | [diff] [blame] | 122 | // Check if we should generate debug info for this method. |
| 123 | if (CGM.getDebugInfo() && !OMD->hasAttr<NoDebugAttr>()) |
| 124 | DebugInfo = CGM.getDebugInfo(); |
| 125 | |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 126 | llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 127 | |
Daniel Dunbar | 0e4f40e | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 128 | const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD); |
| 129 | CGM.SetInternalFunctionAttributes(OMD, Fn, FI); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 130 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | Args.push_back(std::make_pair(OMD->getSelfDecl(), |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 132 | OMD->getSelfDecl()->getType())); |
| 133 | Args.push_back(std::make_pair(OMD->getCmdDecl(), |
| 134 | OMD->getCmdDecl()->getType())); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 136 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 137 | E = OMD->param_end(); PI != E; ++PI) |
| 138 | Args.push_back(std::make_pair(*PI, (*PI)->getType())); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 139 | |
Devang Patel | a92d613 | 2010-02-15 18:08:38 +0000 | [diff] [blame] | 140 | StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocStart()); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 141 | } |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 142 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 143 | /// 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] | 144 | /// its pointer, name, and types registered in the class struture. |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 145 | void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 146 | StartObjCMethod(OMD, OMD->getClassInterface()); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 147 | EmitStmt(OMD->getBody()); |
| 148 | FinishFunction(OMD->getBodyRBrace()); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 151 | // FIXME: I wasn't sure about the synthesis approach. If we end up generating an |
| 152 | // AST for the whole body we can just fall back to having a GenerateFunction |
| 153 | // which takes the body Stmt. |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 154 | |
| 155 | /// GenerateObjCGetter - Generate an Objective-C property getter |
Steve Naroff | 489034c | 2009-01-10 22:55:25 +0000 | [diff] [blame] | 156 | /// function. The given Decl must be an ObjCImplementationDecl. @synthesize |
| 157 | /// is illegal within a category. |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 158 | void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP, |
| 159 | const ObjCPropertyImplDecl *PID) { |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 160 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 161 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 162 | bool IsAtomic = |
| 163 | !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 164 | ObjCMethodDecl *OMD = PD->getGetterMethodDecl(); |
| 165 | assert(OMD && "Invalid call to generate getter (empty method)"); |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 166 | StartObjCMethod(OMD, IMP->getClassInterface()); |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 167 | |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 168 | // Determine if we should use an objc_getProperty call for |
Fariborz Jahanian | 447d7ae | 2008-12-08 23:56:17 +0000 | [diff] [blame] | 169 | // this. Non-atomic properties are directly evaluated. |
| 170 | // atomic 'copy' and 'retain' properties are also directly |
| 171 | // evaluated in gc-only mode. |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 172 | if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly && |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 173 | IsAtomic && |
Fariborz Jahanian | 447d7ae | 2008-12-08 23:56:17 +0000 | [diff] [blame] | 174 | (PD->getSetterKind() == ObjCPropertyDecl::Copy || |
| 175 | PD->getSetterKind() == ObjCPropertyDecl::Retain)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | llvm::Value *GetPropertyFn = |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 177 | CGM.getObjCRuntime().GetPropertyGetFunction(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 179 | if (!GetPropertyFn) { |
| 180 | CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy"); |
| 181 | FinishFunction(); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true). |
| 186 | // FIXME: Can't this be simpler? This might even be worse than the |
| 187 | // corresponding gcc code. |
| 188 | CodeGenTypes &Types = CGM.getTypes(); |
| 189 | ValueDecl *Cmd = OMD->getCmdDecl(); |
| 190 | llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd"); |
| 191 | QualType IdTy = getContext().getObjCIdType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | llvm::Value *SelfAsId = |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 193 | Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 194 | llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar); |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 195 | llvm::Value *True = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 196 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1); |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 197 | CallArgList Args; |
| 198 | Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy)); |
| 199 | Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType())); |
| 200 | Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy)); |
| 201 | Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy)); |
Daniel Dunbar | e4be5a6 | 2009-02-03 23:43:59 +0000 | [diff] [blame] | 202 | // FIXME: We shouldn't need to get the function info here, the |
| 203 | // runtime already should have computed it to build the function. |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 204 | RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 205 | FunctionType::ExtInfo()), |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 206 | GetPropertyFn, ReturnValueSlot(), Args); |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 207 | // We need to fix the type here. Ivars with copy & retain are |
| 208 | // always objects so we don't need to worry about complex or |
| 209 | // aggregates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(), |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 211 | Types.ConvertType(PD->getType()))); |
| 212 | EmitReturnOfRValue(RV, PD->getType()); |
| 213 | } else { |
Fariborz Jahanian | 1b23fe6 | 2010-03-25 21:56:43 +0000 | [diff] [blame] | 214 | if (Ivar->getType()->isAnyComplexType()) { |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 215 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), |
| 216 | Ivar, 0); |
Fariborz Jahanian | 1b23fe6 | 2010-03-25 21:56:43 +0000 | [diff] [blame] | 217 | ComplexPairTy Pair = LoadComplexFromAddr(LV.getAddress(), |
| 218 | LV.isVolatileQualified()); |
| 219 | StoreComplexToAddr(Pair, ReturnValue, LV.isVolatileQualified()); |
| 220 | } |
| 221 | else if (hasAggregateLLVMType(Ivar->getType())) { |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 222 | bool IsStrong = false; |
| 223 | if ((IsAtomic || (IsStrong = IvarTypeWithAggrGCObjects(Ivar->getType()))) |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 224 | && CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect |
| 225 | && CGM.getObjCRuntime().GetCopyStructFunction()) { |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 226 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), |
| 227 | Ivar, 0); |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 228 | llvm::Value *GetCopyStructFn = |
| 229 | CGM.getObjCRuntime().GetCopyStructFunction(); |
| 230 | CodeGenTypes &Types = CGM.getTypes(); |
| 231 | // objc_copyStruct (ReturnValue, &structIvar, |
| 232 | // sizeof (Type of Ivar), isAtomic, false); |
| 233 | CallArgList Args; |
| 234 | RValue RV = RValue::get(Builder.CreateBitCast(ReturnValue, |
| 235 | Types.ConvertType(getContext().VoidPtrTy))); |
| 236 | Args.push_back(std::make_pair(RV, getContext().VoidPtrTy)); |
| 237 | RV = RValue::get(Builder.CreateBitCast(LV.getAddress(), |
| 238 | Types.ConvertType(getContext().VoidPtrTy))); |
| 239 | Args.push_back(std::make_pair(RV, getContext().VoidPtrTy)); |
| 240 | // sizeof (Type of Ivar) |
| 241 | uint64_t Size = getContext().getTypeSize(Ivar->getType()) / 8; |
| 242 | llvm::Value *SizeVal = |
| 243 | llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), Size); |
| 244 | Args.push_back(std::make_pair(RValue::get(SizeVal), |
| 245 | getContext().LongTy)); |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 246 | llvm::Value *isAtomic = |
Fariborz Jahanian | 08adf32 | 2010-04-13 18:43:37 +0000 | [diff] [blame] | 247 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), |
| 248 | IsAtomic ? 1 : 0); |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 249 | Args.push_back(std::make_pair(RValue::get(isAtomic), |
| 250 | getContext().BoolTy)); |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 251 | llvm::Value *hasStrong = |
| 252 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), |
| 253 | IsStrong ? 1 : 0); |
| 254 | Args.push_back(std::make_pair(RValue::get(hasStrong), |
| 255 | getContext().BoolTy)); |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 256 | EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args, |
| 257 | FunctionType::ExtInfo()), |
| 258 | GetCopyStructFn, ReturnValueSlot(), Args); |
| 259 | } |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 260 | else { |
| 261 | if (PID->getGetterCXXConstructor()) { |
| 262 | ReturnStmt *Stmt = |
| 263 | new (getContext()) ReturnStmt(SourceLocation(), |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 264 | PID->getGetterCXXConstructor(), |
| 265 | 0); |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 266 | EmitReturnStmt(*Stmt); |
| 267 | } |
| 268 | else { |
| 269 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), |
| 270 | Ivar, 0); |
| 271 | EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType()); |
| 272 | } |
| 273 | } |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 274 | } else { |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 275 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), |
| 276 | Ivar, 0); |
Fariborz Jahanian | ed1d29d | 2009-03-03 18:49:40 +0000 | [diff] [blame] | 277 | CodeGenTypes &Types = CGM.getTypes(); |
| 278 | RValue RV = EmitLoadOfLValue(LV, Ivar->getType()); |
| 279 | RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | Types.ConvertType(PD->getType()))); |
Fariborz Jahanian | ed1d29d | 2009-03-03 18:49:40 +0000 | [diff] [blame] | 281 | EmitReturnOfRValue(RV, PD->getType()); |
| 282 | } |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 283 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 284 | |
| 285 | FinishFunction(); |
| 286 | } |
| 287 | |
| 288 | /// GenerateObjCSetter - Generate an Objective-C property setter |
Steve Naroff | 489034c | 2009-01-10 22:55:25 +0000 | [diff] [blame] | 289 | /// function. The given Decl must be an ObjCImplementationDecl. @synthesize |
| 290 | /// is illegal within a category. |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 291 | void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP, |
| 292 | const ObjCPropertyImplDecl *PID) { |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 293 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 294 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 295 | ObjCMethodDecl *OMD = PD->getSetterMethodDecl(); |
| 296 | assert(OMD && "Invalid call to generate setter (empty method)"); |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 297 | StartObjCMethod(OMD, IMP->getClassInterface()); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 298 | |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 299 | bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | bool IsAtomic = |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 301 | !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic); |
| 302 | |
| 303 | // Determine if we should use an objc_setProperty call for |
| 304 | // this. Properties with 'copy' semantics always use it, as do |
| 305 | // non-atomic properties with 'release' semantics as long as we are |
| 306 | // not in gc-only mode. |
| 307 | if (IsCopy || |
| 308 | (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly && |
| 309 | PD->getSetterKind() == ObjCPropertyDecl::Retain)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 310 | llvm::Value *SetPropertyFn = |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 311 | CGM.getObjCRuntime().GetPropertySetFunction(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 313 | if (!SetPropertyFn) { |
| 314 | CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy"); |
| 315 | FinishFunction(); |
| 316 | return; |
| 317 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
| 319 | // Emit objc_setProperty((id) self, _cmd, offset, arg, |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 320 | // <is-atomic>, <is-copy>). |
| 321 | // FIXME: Can't this be simpler? This might even be worse than the |
| 322 | // corresponding gcc code. |
| 323 | CodeGenTypes &Types = CGM.getTypes(); |
| 324 | ValueDecl *Cmd = OMD->getCmdDecl(); |
| 325 | llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd"); |
| 326 | QualType IdTy = getContext().getObjCIdType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 327 | llvm::Value *SelfAsId = |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 328 | Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 329 | llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 330 | llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 331 | llvm::Value *ArgAsId = |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 332 | Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"), |
| 333 | Types.ConvertType(IdTy)); |
| 334 | llvm::Value *True = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 335 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1); |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 336 | llvm::Value *False = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 337 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0); |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 338 | CallArgList Args; |
| 339 | Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy)); |
| 340 | Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType())); |
| 341 | Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy)); |
| 342 | Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False), |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 344 | getContext().BoolTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False), |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 346 | getContext().BoolTy)); |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 347 | // FIXME: We shouldn't need to get the function info here, the runtime |
| 348 | // already should have computed it to build the function. |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 349 | EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 350 | FunctionType::ExtInfo()), |
| 351 | SetPropertyFn, |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 352 | ReturnValueSlot(), Args); |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 353 | } else if (IsAtomic && hasAggregateLLVMType(Ivar->getType()) && |
| 354 | !Ivar->getType()->isAnyComplexType() && |
| 355 | IndirectObjCSetterArg(*CurFnInfo) |
| 356 | && CGM.getObjCRuntime().GetCopyStructFunction()) { |
| 357 | // objc_copyStruct (&structIvar, &Arg, |
| 358 | // sizeof (struct something), true, false); |
| 359 | llvm::Value *GetCopyStructFn = |
| 360 | CGM.getObjCRuntime().GetCopyStructFunction(); |
| 361 | CodeGenTypes &Types = CGM.getTypes(); |
| 362 | CallArgList Args; |
| 363 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0); |
| 364 | RValue RV = RValue::get(Builder.CreateBitCast(LV.getAddress(), |
| 365 | Types.ConvertType(getContext().VoidPtrTy))); |
| 366 | Args.push_back(std::make_pair(RV, getContext().VoidPtrTy)); |
| 367 | llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()]; |
| 368 | llvm::Value *ArgAsPtrTy = |
| 369 | Builder.CreateBitCast(Arg, |
| 370 | Types.ConvertType(getContext().VoidPtrTy)); |
| 371 | RV = RValue::get(ArgAsPtrTy); |
| 372 | Args.push_back(std::make_pair(RV, getContext().VoidPtrTy)); |
| 373 | // sizeof (Type of Ivar) |
| 374 | uint64_t Size = getContext().getTypeSize(Ivar->getType()) / 8; |
| 375 | llvm::Value *SizeVal = |
| 376 | llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), Size); |
| 377 | Args.push_back(std::make_pair(RValue::get(SizeVal), |
| 378 | getContext().LongTy)); |
| 379 | llvm::Value *True = |
| 380 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1); |
| 381 | Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy)); |
| 382 | llvm::Value *False = |
| 383 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0); |
| 384 | Args.push_back(std::make_pair(RValue::get(False), getContext().BoolTy)); |
| 385 | EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args, |
| 386 | FunctionType::ExtInfo()), |
| 387 | GetCopyStructFn, ReturnValueSlot(), Args); |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 388 | } else if (PID->getSetterCXXAssignment()) { |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 389 | EmitAnyExpr(PID->getSetterCXXAssignment(), AggValueSlot::ignored(), true); |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 390 | |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 391 | } else { |
Daniel Dunbar | 45e8423 | 2009-10-27 19:21:30 +0000 | [diff] [blame] | 392 | // FIXME: Find a clean way to avoid AST node creation. |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 393 | SourceLocation Loc = PD->getLocation(); |
| 394 | ValueDecl *Self = OMD->getSelfDecl(); |
| 395 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
| 396 | DeclRefExpr Base(Self, Self->getType(), Loc); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 397 | ParmVarDecl *ArgDecl = *OMD->param_begin(); |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 398 | DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc); |
Daniel Dunbar | 45e8423 | 2009-10-27 19:21:30 +0000 | [diff] [blame] | 399 | ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true); |
| 400 | |
| 401 | // The property type can differ from the ivar type in some situations with |
| 402 | // Objective-C pointer types, we can always bit cast the RHS in these cases. |
| 403 | if (getContext().getCanonicalType(Ivar->getType()) != |
| 404 | getContext().getCanonicalType(ArgDecl->getType())) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 405 | ImplicitCastExpr ArgCasted(ImplicitCastExpr::OnStack, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 406 | Ivar->getType(), CK_BitCast, &Arg, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 407 | VK_RValue); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 408 | BinaryOperator Assign(&IvarRef, &ArgCasted, BO_Assign, |
Daniel Dunbar | 45e8423 | 2009-10-27 19:21:30 +0000 | [diff] [blame] | 409 | Ivar->getType(), Loc); |
| 410 | EmitStmt(&Assign); |
| 411 | } else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 412 | BinaryOperator Assign(&IvarRef, &Arg, BO_Assign, |
Daniel Dunbar | 45e8423 | 2009-10-27 19:21:30 +0000 | [diff] [blame] | 413 | Ivar->getType(), Loc); |
| 414 | EmitStmt(&Assign); |
| 415 | } |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 416 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 417 | |
| 418 | FinishFunction(); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 421 | void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, |
| 422 | ObjCMethodDecl *MD, |
| 423 | bool ctor) { |
| 424 | llvm::SmallVector<CXXBaseOrMemberInitializer *, 8> IvarInitializers; |
| 425 | MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface()); |
| 426 | StartObjCMethod(MD, IMP->getClassInterface()); |
| 427 | for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(), |
| 428 | E = IMP->init_end(); B != E; ++B) { |
| 429 | CXXBaseOrMemberInitializer *Member = (*B); |
| 430 | IvarInitializers.push_back(Member); |
| 431 | } |
| 432 | if (ctor) { |
| 433 | for (unsigned I = 0, E = IvarInitializers.size(); I != E; ++I) { |
| 434 | CXXBaseOrMemberInitializer *IvarInit = IvarInitializers[I]; |
| 435 | FieldDecl *Field = IvarInit->getMember(); |
| 436 | QualType FieldType = Field->getType(); |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 437 | ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); |
Fariborz Jahanian | 9b4d4fc | 2010-04-28 22:30:33 +0000 | [diff] [blame] | 438 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), |
| 439 | LoadObjCSelf(), Ivar, 0); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 440 | EmitAggExpr(IvarInit->getInit(), AggValueSlot::forLValue(LV, true)); |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 441 | } |
| 442 | // constructor returns 'self'. |
| 443 | CodeGenTypes &Types = CGM.getTypes(); |
| 444 | QualType IdTy(CGM.getContext().getObjCIdType()); |
| 445 | llvm::Value *SelfAsId = |
| 446 | Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); |
| 447 | EmitReturnOfRValue(RValue::get(SelfAsId), IdTy); |
Chandler Carruth | bc397cf | 2010-05-06 00:20:39 +0000 | [diff] [blame] | 448 | } else { |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 449 | // dtor |
| 450 | for (size_t i = IvarInitializers.size(); i > 0; --i) { |
| 451 | FieldDecl *Field = IvarInitializers[i - 1]->getMember(); |
| 452 | QualType FieldType = Field->getType(); |
Fariborz Jahanian | 9b4d4fc | 2010-04-28 22:30:33 +0000 | [diff] [blame] | 453 | const ConstantArrayType *Array = |
| 454 | getContext().getAsConstantArrayType(FieldType); |
| 455 | if (Array) |
| 456 | FieldType = getContext().getBaseElementType(FieldType); |
| 457 | |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 458 | ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); |
| 459 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), |
| 460 | LoadObjCSelf(), Ivar, 0); |
| 461 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 462 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 463 | CXXDestructorDecl *Dtor = FieldClassDecl->getDestructor(); |
Chandler Carruth | bc397cf | 2010-05-06 00:20:39 +0000 | [diff] [blame] | 464 | if (!Dtor->isTrivial()) { |
Fariborz Jahanian | 8b688ed | 2010-05-04 19:29:32 +0000 | [diff] [blame] | 465 | if (Array) { |
| 466 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 467 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 468 | llvm::Value *BaseAddrPtr = |
| 469 | Builder.CreateBitCast(LV.getAddress(), BasePtr); |
| 470 | EmitCXXAggrDestructorCall(Dtor, |
| 471 | Array, BaseAddrPtr); |
Chandler Carruth | bc397cf | 2010-05-06 00:20:39 +0000 | [diff] [blame] | 472 | } else { |
Fariborz Jahanian | 8b688ed | 2010-05-04 19:29:32 +0000 | [diff] [blame] | 473 | EmitCXXDestructorCall(Dtor, |
| 474 | Dtor_Complete, /*ForVirtualBase=*/false, |
| 475 | LV.getAddress()); |
Chandler Carruth | bc397cf | 2010-05-06 00:20:39 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | } |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 479 | } |
| 480 | FinishFunction(); |
| 481 | } |
| 482 | |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 483 | bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) { |
| 484 | CGFunctionInfo::const_arg_iterator it = FI.arg_begin(); |
| 485 | it++; it++; |
| 486 | const ABIArgInfo &AI = it->info; |
| 487 | // FIXME. Is this sufficient check? |
| 488 | return (AI.getKind() == ABIArgInfo::Indirect); |
| 489 | } |
| 490 | |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 491 | bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) { |
| 492 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 493 | return false; |
| 494 | if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>()) |
| 495 | return FDTTy->getDecl()->hasObjectMember(); |
| 496 | return false; |
| 497 | } |
| 498 | |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 499 | llvm::Value *CodeGenFunction::LoadObjCSelf() { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 500 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 501 | return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self"); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 504 | QualType CodeGenFunction::TypeOfSelfObject() { |
| 505 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 506 | ImplicitParamDecl *selfDecl = OMD->getSelfDecl(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 507 | const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>( |
| 508 | getContext().getCanonicalType(selfDecl->getType())); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 509 | return PTy->getPointeeType(); |
| 510 | } |
| 511 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | RValue CodeGenFunction::EmitObjCSuperPropertyGet(const Expr *Exp, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 513 | const Selector &S, |
| 514 | ReturnValueSlot Return) { |
Fariborz Jahanian | f469557 | 2009-03-20 19:18:21 +0000 | [diff] [blame] | 515 | llvm::Value *Receiver = LoadObjCSelf(); |
| 516 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 517 | bool isClassMessage = OMD->isClassMethod(); |
| 518 | bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 519 | return CGM.getObjCRuntime().GenerateMessageSendSuper(*this, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 520 | Return, |
Fariborz Jahanian | f469557 | 2009-03-20 19:18:21 +0000 | [diff] [blame] | 521 | Exp->getType(), |
| 522 | S, |
| 523 | OMD->getClassInterface(), |
| 524 | isCategoryImpl, |
| 525 | Receiver, |
| 526 | isClassMessage, |
| 527 | CallArgList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | |
Fariborz Jahanian | f469557 | 2009-03-20 19:18:21 +0000 | [diff] [blame] | 529 | } |
| 530 | |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 531 | RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp, |
| 532 | ReturnValueSlot Return) { |
Fariborz Jahanian | d2e1eb0 | 2009-09-01 17:02:21 +0000 | [diff] [blame] | 533 | Exp = Exp->IgnoreParens(); |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 534 | // FIXME: Split it into two separate routines. |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 535 | if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) { |
| 536 | Selector S = E->getProperty()->getGetterName(); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 537 | if (E->isSuperReceiver()) |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 538 | return EmitObjCSuperPropertyGet(E, S, Return); |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 539 | return CGM.getObjCRuntime(). |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 540 | GenerateMessageSend(*this, Return, Exp->getType(), S, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 541 | EmitScalarExpr(E->getBase()), |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 542 | CallArgList()); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 543 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | const ObjCImplicitSetterGetterRefExpr *KE = |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 545 | cast<ObjCImplicitSetterGetterRefExpr>(Exp); |
Daniel Dunbar | f479cea | 2009-01-16 01:50:29 +0000 | [diff] [blame] | 546 | Selector S = KE->getGetterMethod()->getSelector(); |
Fariborz Jahanian | 3523d4f | 2009-03-10 18:03:11 +0000 | [diff] [blame] | 547 | llvm::Value *Receiver; |
Fariborz Jahanian | d2ae5aa | 2009-08-18 21:37:33 +0000 | [diff] [blame] | 548 | if (KE->getInterfaceDecl()) { |
| 549 | const ObjCInterfaceDecl *OID = KE->getInterfaceDecl(); |
Fariborz Jahanian | 3523d4f | 2009-03-10 18:03:11 +0000 | [diff] [blame] | 550 | Receiver = CGM.getObjCRuntime().GetClass(Builder, OID); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 551 | } else if (KE->isSuperReceiver()) |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 552 | return EmitObjCSuperPropertyGet(KE, S, Return); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | else |
Fariborz Jahanian | 3523d4f | 2009-03-10 18:03:11 +0000 | [diff] [blame] | 554 | Receiver = EmitScalarExpr(KE->getBase()); |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 555 | return CGM.getObjCRuntime(). |
Fariborz Jahanian | 0339d72 | 2010-09-10 18:56:35 +0000 | [diff] [blame] | 556 | GenerateMessageSend(*this, Return, |
| 557 | KE->getGetterMethod()->getResultType(), S, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | Receiver, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 559 | CallArgList(), KE->getInterfaceDecl()); |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 560 | } |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Fariborz Jahanian | f469557 | 2009-03-20 19:18:21 +0000 | [diff] [blame] | 563 | void CodeGenFunction::EmitObjCSuperPropertySet(const Expr *Exp, |
| 564 | const Selector &S, |
| 565 | RValue Src) { |
| 566 | CallArgList Args; |
| 567 | llvm::Value *Receiver = LoadObjCSelf(); |
| 568 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 569 | bool isClassMessage = OMD->isClassMethod(); |
| 570 | bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
| 571 | Args.push_back(std::make_pair(Src, Exp->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | CGM.getObjCRuntime().GenerateMessageSendSuper(*this, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 573 | ReturnValueSlot(), |
Fariborz Jahanian | 3d4aeda | 2010-07-24 00:34:08 +0000 | [diff] [blame] | 574 | getContext().VoidTy, |
Fariborz Jahanian | f469557 | 2009-03-20 19:18:21 +0000 | [diff] [blame] | 575 | S, |
| 576 | OMD->getClassInterface(), |
| 577 | isCategoryImpl, |
| 578 | Receiver, |
| 579 | isClassMessage, |
| 580 | Args); |
| 581 | return; |
| 582 | } |
| 583 | |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 584 | void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp, |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 585 | RValue Src) { |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 586 | // FIXME: Split it into two separate routines. |
| 587 | if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) { |
| 588 | Selector S = E->getProperty()->getSetterName(); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 589 | if (E->isSuperReceiver()) { |
Fariborz Jahanian | f469557 | 2009-03-20 19:18:21 +0000 | [diff] [blame] | 590 | EmitObjCSuperPropertySet(E, S, Src); |
| 591 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 592 | } |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 593 | CallArgList Args; |
| 594 | Args.push_back(std::make_pair(Src, E->getType())); |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 595 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
| 596 | getContext().VoidTy, S, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 597 | EmitScalarExpr(E->getBase()), |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 598 | Args); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 599 | } else if (const ObjCImplicitSetterGetterRefExpr *E = |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 600 | dyn_cast<ObjCImplicitSetterGetterRefExpr>(Exp)) { |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 601 | const ObjCMethodDecl *SetterMD = E->getSetterMethod(); |
| 602 | Selector S = SetterMD->getSelector(); |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 603 | CallArgList Args; |
Fariborz Jahanian | 3523d4f | 2009-03-10 18:03:11 +0000 | [diff] [blame] | 604 | llvm::Value *Receiver; |
Fariborz Jahanian | d2ae5aa | 2009-08-18 21:37:33 +0000 | [diff] [blame] | 605 | if (E->getInterfaceDecl()) { |
| 606 | const ObjCInterfaceDecl *OID = E->getInterfaceDecl(); |
Fariborz Jahanian | 3523d4f | 2009-03-10 18:03:11 +0000 | [diff] [blame] | 607 | Receiver = CGM.getObjCRuntime().GetClass(Builder, OID); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 608 | } else if (E->isSuperReceiver()) { |
Fariborz Jahanian | f469557 | 2009-03-20 19:18:21 +0000 | [diff] [blame] | 609 | EmitObjCSuperPropertySet(E, S, Src); |
Fariborz Jahanian | 8e5d232 | 2009-03-20 17:22:23 +0000 | [diff] [blame] | 610 | return; |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 611 | } else |
Fariborz Jahanian | 3523d4f | 2009-03-10 18:03:11 +0000 | [diff] [blame] | 612 | Receiver = EmitScalarExpr(E->getBase()); |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 613 | ObjCMethodDecl::param_iterator P = SetterMD->param_begin(); |
| 614 | Args.push_back(std::make_pair(Src, (*P)->getType())); |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 615 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
| 616 | getContext().VoidTy, S, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 617 | Receiver, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 618 | Args, E->getInterfaceDecl()); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 619 | } else |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 620 | assert (0 && "bad expression node in EmitObjCPropertySet"); |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 623 | void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | llvm::Constant *EnumerationMutationFn = |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 625 | CGM.getObjCRuntime().EnumerationMutationFunction(); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 626 | llvm::Value *DeclAddress; |
| 627 | QualType ElementTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 628 | |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 629 | if (!EnumerationMutationFn) { |
| 630 | CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime"); |
| 631 | return; |
| 632 | } |
| 633 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 634 | if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) { |
| 635 | EmitStmt(SD); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 636 | assert(HaveInsertPoint() && "DeclStmt destroyed insert point!"); |
Chris Lattner | 7e24e82 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 637 | const Decl* D = SD->getSingleDecl(); |
Ted Kremenek | 39741ce | 2008-10-06 20:59:48 +0000 | [diff] [blame] | 638 | ElementTy = cast<ValueDecl>(D)->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | DeclAddress = LocalDeclMap[D]; |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 640 | } else { |
| 641 | ElementTy = cast<Expr>(S.getElement())->getType(); |
| 642 | DeclAddress = 0; |
| 643 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 645 | // Fast enumeration state. |
| 646 | QualType StateTy = getContext().getObjCFastEnumerationStateType(); |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 647 | llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr"); |
Anders Carlsson | 1884eb0 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 648 | EmitNullInitialization(StatePtr, StateTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 649 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 650 | // Number of elements in the items array. |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 651 | static const unsigned NumItems = 16; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 652 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 653 | // Get selector |
Benjamin Kramer | ad46886 | 2010-03-30 11:36:44 +0000 | [diff] [blame] | 654 | IdentifierInfo *II[] = { |
| 655 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 656 | &CGM.getContext().Idents.get("objects"), |
| 657 | &CGM.getContext().Idents.get("count") |
| 658 | }; |
| 659 | Selector FastEnumSel = |
| 660 | CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 661 | |
| 662 | QualType ItemsTy = |
| 663 | getContext().getConstantArrayType(getContext().getObjCIdType(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | llvm::APInt(32, NumItems), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 665 | ArrayType::Normal, 0); |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 666 | llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 667 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 668 | llvm::Value *Collection = EmitScalarExpr(S.getCollection()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 669 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 670 | CallArgList Args; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | Args.push_back(std::make_pair(RValue::get(StatePtr), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 672 | getContext().getPointerType(StateTy))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | |
| 674 | Args.push_back(std::make_pair(RValue::get(ItemsPtr), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 675 | getContext().getPointerType(ItemsTy))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 677 | const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 678 | llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 679 | Args.push_back(std::make_pair(RValue::get(Count), |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 680 | getContext().UnsignedLongTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 681 | |
| 682 | RValue CountRV = |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 683 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 684 | getContext().UnsignedLongTy, |
| 685 | FastEnumSel, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 686 | Collection, Args); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 687 | |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 688 | llvm::Value *LimitPtr = CreateMemTemp(getContext().UnsignedLongTy, |
| 689 | "limit.ptr"); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 690 | Builder.CreateStore(CountRV.getScalarVal(), LimitPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 691 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 692 | llvm::BasicBlock *NoElements = createBasicBlock("noelements"); |
| 693 | llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 694 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 695 | llvm::Value *Limit = Builder.CreateLoad(LimitPtr); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 696 | llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 697 | |
| 698 | llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero"); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 699 | Builder.CreateCondBr(IsZero, NoElements, SetStartMutations); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 700 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 701 | EmitBlock(SetStartMutations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 702 | |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 703 | llvm::Value *StartMutationsPtr = CreateMemTemp(getContext().UnsignedLongTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 704 | |
| 705 | llvm::Value *StateMutationsPtrPtr = |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 706 | Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 707 | llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 708 | "mutationsptr"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 709 | |
| 710 | llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr, |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 711 | "mutations"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 713 | Builder.CreateStore(StateMutations, StartMutationsPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 715 | llvm::BasicBlock *LoopStart = createBasicBlock("loopstart"); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 716 | EmitBlock(LoopStart); |
| 717 | |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 718 | llvm::Value *CounterPtr = CreateMemTemp(getContext().UnsignedLongTy, |
| 719 | "counter.ptr"); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 720 | Builder.CreateStore(Zero, CounterPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 721 | |
| 722 | llvm::BasicBlock *LoopBody = createBasicBlock("loopbody"); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 723 | EmitBlock(LoopBody); |
| 724 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 725 | StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); |
| 726 | StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations"); |
| 727 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 728 | llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr, |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 729 | "mutations"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations, |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 731 | StartMutations, |
| 732 | "tobool"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 733 | |
| 734 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 735 | llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated"); |
| 736 | llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 738 | Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 740 | EmitBlock(WasMutated); |
| 741 | llvm::Value *V = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 742 | Builder.CreateBitCast(Collection, |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 743 | ConvertType(getContext().getObjCIdType()), |
| 744 | "tmp"); |
Daniel Dunbar | 2b2105e | 2009-02-03 23:55:40 +0000 | [diff] [blame] | 745 | CallArgList Args2; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | Args2.push_back(std::make_pair(RValue::get(V), |
Daniel Dunbar | 2b2105e | 2009-02-03 23:55:40 +0000 | [diff] [blame] | 747 | getContext().getObjCIdType())); |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 748 | // FIXME: We shouldn't need to get the function info here, the runtime already |
| 749 | // should have computed it to build the function. |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 750 | EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 751 | FunctionType::ExtInfo()), |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 752 | EnumerationMutationFn, ReturnValueSlot(), Args2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 754 | EmitBlock(WasNotMutated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | |
| 756 | llvm::Value *StateItemsPtr = |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 757 | Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr"); |
| 758 | |
| 759 | llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter"); |
| 760 | |
| 761 | llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr, |
| 762 | "stateitems"); |
| 763 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 764 | llvm::Value *CurrentItemPtr = |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 765 | Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 767 | llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 768 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 769 | // Cast the item to the right type. |
| 770 | CurrentItem = Builder.CreateBitCast(CurrentItem, |
| 771 | ConvertType(ElementTy), "tmp"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 772 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 773 | if (!DeclAddress) { |
| 774 | LValue LV = EmitLValue(cast<Expr>(S.getElement())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 775 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 776 | // Set the value to null. |
| 777 | Builder.CreateStore(CurrentItem, LV.getAddress()); |
| 778 | } else |
| 779 | Builder.CreateStore(CurrentItem, DeclAddress); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 780 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 781 | // Increment the counter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 782 | Counter = Builder.CreateAdd(Counter, |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 783 | llvm::ConstantInt::get(UnsignedLongLTy, 1)); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 784 | Builder.CreateStore(Counter, CounterPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 786 | JumpDest LoopEnd = getJumpDestInCurrentScope("loopend"); |
| 787 | JumpDest AfterBody = getJumpDestInCurrentScope("afterbody"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 788 | |
Anders Carlsson | e4b6d34 | 2009-02-10 05:52:02 +0000 | [diff] [blame] | 789 | BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody)); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 790 | |
| 791 | EmitStmt(S.getBody()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 793 | BreakContinueStack.pop_back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 794 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 795 | EmitBlock(AfterBody.getBlock()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 796 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 797 | llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore"); |
Fariborz Jahanian | f0906c4 | 2009-01-06 18:56:31 +0000 | [diff] [blame] | 798 | |
| 799 | Counter = Builder.CreateLoad(CounterPtr); |
| 800 | Limit = Builder.CreateLoad(LimitPtr); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 801 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless"); |
Daniel Dunbar | fe2b2c0 | 2008-09-04 21:54:37 +0000 | [diff] [blame] | 802 | Builder.CreateCondBr(IsLess, LoopBody, FetchMore); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 803 | |
| 804 | // Fetch more elements. |
| 805 | EmitBlock(FetchMore); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 806 | |
| 807 | CountRV = |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 808 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 809 | getContext().UnsignedLongTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | FastEnumSel, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 811 | Collection, Args); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 812 | Builder.CreateStore(CountRV.getScalarVal(), LimitPtr); |
| 813 | Limit = Builder.CreateLoad(LimitPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 814 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 815 | IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero"); |
| 816 | Builder.CreateCondBr(IsZero, NoElements, LoopStart); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 817 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 818 | // No more elements. |
| 819 | EmitBlock(NoElements); |
| 820 | |
| 821 | if (!DeclAddress) { |
| 822 | // If the element was not a declaration, set it to be null. |
| 823 | |
| 824 | LValue LV = EmitLValue(cast<Expr>(S.getElement())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 826 | // Set the value to null. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 827 | Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 828 | LV.getAddress()); |
| 829 | } |
| 830 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 831 | EmitBlock(LoopEnd.getBlock()); |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 835 | CGM.getObjCRuntime().EmitTryStmt(*this, S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 838 | void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) { |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 839 | CGM.getObjCRuntime().EmitThrowStmt(*this, S); |
| 840 | } |
| 841 | |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 842 | void CodeGenFunction::EmitObjCAtSynchronizedStmt( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | const ObjCAtSynchronizedStmt &S) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 844 | CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S); |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Ted Kremenek | 2979ec7 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 847 | CGObjCRuntime::~CGObjCRuntime() {} |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 848 | |
| 849 | |