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