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