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" |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame^] | 19 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 20 | |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
| 23 | |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 24 | /// Emits an instance of NSConstantString representing the object. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 25 | llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) { |
| 26 | std::string String(E->getString()->getStrData(), E->getString()->getByteLength()); |
| 27 | llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(String); |
Daniel Dunbar | ed7c618 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 28 | // FIXME: This bitcast should just be made an invariant on the Runtime. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 29 | return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | /// Emit a selector. |
| 33 | llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) { |
| 34 | // Untyped selector. |
| 35 | // Note that this implementation allows for non-constant strings to be passed |
| 36 | // as arguments to @selector(). Currently, the only thing preventing this |
| 37 | // behaviour is the type checking in the front end. |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 38 | return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector()); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Daniel Dunbar | ed7c618 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 41 | llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) { |
| 42 | // FIXME: This should pass the Decl not the name. |
| 43 | return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol()); |
| 44 | } |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 45 | |
| 46 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 47 | RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) { |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 48 | // Only the lookup mechanism and first two arguments of the method |
| 49 | // implementation vary between runtimes. We can get the receiver and |
| 50 | // arguments in generic code. |
| 51 | |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 52 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 53 | const Expr *ReceiverExpr = E->getReceiver(); |
| 54 | bool isSuperMessage = false; |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 55 | bool isClassMessage = false; |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 56 | // Find the receiver |
| 57 | llvm::Value *Receiver; |
| 58 | if (!ReceiverExpr) { |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 59 | const ObjCInterfaceDecl *OID = E->getClassInfo().first; |
| 60 | |
| 61 | // Very special case, super send in class method. The receiver is |
| 62 | // self (the class object) and the send uses super semantics. |
| 63 | if (!OID) { |
| 64 | assert(!strcmp(E->getClassName()->getName(), "super") && |
| 65 | "Unexpected missing class interface in message send."); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 66 | isSuperMessage = true; |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 67 | Receiver = LoadObjCSelf(); |
| 68 | } else { |
| 69 | Receiver = Runtime.GetClass(Builder, OID); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 70 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 71 | |
| 72 | isClassMessage = true; |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 73 | } else if (const PredefinedExpr *PDE = |
| 74 | dyn_cast<PredefinedExpr>(E->getReceiver())) { |
| 75 | assert(PDE->getIdentType() == PredefinedExpr::ObjCSuper); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 76 | isSuperMessage = true; |
| 77 | Receiver = LoadObjCSelf(); |
| 78 | } else { |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 79 | Receiver = EmitScalarExpr(E->getReceiver()); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 82 | CallArgList Args; |
| 83 | for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end(); |
| 84 | i != e; ++i) |
| 85 | EmitCallArg(*i, Args); |
| 86 | |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 87 | if (isSuperMessage) { |
Chris Lattner | 9384c76 | 2008-06-26 04:42:20 +0000 | [diff] [blame] | 88 | // super is only valid in an Objective-C method |
| 89 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
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(), |
| 93 | Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 94 | isClassMessage, |
| 95 | Args); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 96 | } |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 97 | return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(), |
| 98 | Receiver, isClassMessage, Args); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 101 | /// StartObjCMethod - Begin emission of an ObjCMethod. This generates |
| 102 | /// the LLVM function and sets the other context used by |
| 103 | /// CodeGenFunction. |
| 104 | |
| 105 | // FIXME: This should really be merged with GenerateCode. |
| 106 | void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 107 | CurFn = CGM.getObjCRuntime().GenerateMethod(OMD); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 108 | llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn); |
| 109 | |
| 110 | // Create a marker to make it easy to insert allocas into the entryblock |
| 111 | // later. Don't create this with the builder, because we don't want it |
| 112 | // folded. |
| 113 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
| 114 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt", |
| 115 | EntryBB); |
| 116 | |
| 117 | FnRetTy = OMD->getResultType(); |
| 118 | CurFuncDecl = OMD; |
| 119 | |
| 120 | Builder.SetInsertPoint(EntryBB); |
| 121 | |
| 122 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 123 | llvm::Function::arg_iterator AI = CurFn->arg_begin(); |
| 124 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 125 | // Name the struct return argument. |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 126 | if (hasAggregateLLVMType(OMD->getResultType())) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 127 | AI->setName("agg.result"); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 128 | ++AI; |
| 129 | } |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 130 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 131 | // Add implicit parameters to the decl map. |
| 132 | EmitParmDecl(*OMD->getSelfDecl(), AI); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 133 | ++AI; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 134 | |
| 135 | EmitParmDecl(*OMD->getCmdDecl(), AI); |
| 136 | ++AI; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 137 | |
| 138 | for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) { |
| 139 | assert(AI != CurFn->arg_end() && "Argument mismatch!"); |
| 140 | EmitParmDecl(*OMD->getParamDecl(i), AI); |
| 141 | } |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 142 | assert(AI == CurFn->arg_end() && "Argument mismatch"); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 143 | } |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 144 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 145 | /// Generate an Objective-C method. An Objective-C method is a C function with |
| 146 | /// its pointer, name, and types registered in the class struture. |
| 147 | void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { |
| 148 | StartObjCMethod(OMD); |
| 149 | EmitStmt(OMD->getBody()); |
| 150 | |
| 151 | const CompoundStmt *S = dyn_cast<CompoundStmt>(OMD->getBody()); |
| 152 | if (S) { |
| 153 | FinishFunction(S->getRBracLoc()); |
| 154 | } else { |
| 155 | FinishFunction(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // FIXME: I wasn't sure about the synthesis approach. If we end up |
| 160 | // generating an AST for the whole body we can just fall back to |
| 161 | // having a GenerateFunction which takes the body Stmt. |
| 162 | |
| 163 | /// GenerateObjCGetter - Generate an Objective-C property getter |
| 164 | /// function. The given Decl must be either an ObjCCategoryImplDecl |
| 165 | /// or an ObjCImplementationDecl. |
| 166 | void CodeGenFunction::GenerateObjCGetter(const ObjCPropertyImplDecl *PID) { |
| 167 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 168 | ObjCMethodDecl *OMD = PD->getGetterMethodDecl(); |
| 169 | assert(OMD && "Invalid call to generate getter (empty method)"); |
| 170 | // FIXME: This is rather murky, we create this here since they will |
| 171 | // not have been created by Sema for us. |
| 172 | OMD->createImplicitParams(getContext()); |
| 173 | StartObjCMethod(OMD); |
| 174 | |
| 175 | // FIXME: What about nonatomic? |
| 176 | SourceLocation Loc = PD->getLocation(); |
| 177 | ValueDecl *Self = OMD->getSelfDecl(); |
| 178 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
| 179 | DeclRefExpr Base(Self, Self->getType(), Loc); |
| 180 | ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, |
| 181 | true, true); |
| 182 | ReturnStmt Return(Loc, &IvarRef); |
| 183 | EmitStmt(&Return); |
| 184 | |
| 185 | FinishFunction(); |
| 186 | } |
| 187 | |
| 188 | /// GenerateObjCSetter - Generate an Objective-C property setter |
| 189 | /// function. The given Decl must be either an ObjCCategoryImplDecl |
| 190 | /// or an ObjCImplementationDecl. |
| 191 | void CodeGenFunction::GenerateObjCSetter(const ObjCPropertyImplDecl *PID) { |
| 192 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 193 | ObjCMethodDecl *OMD = PD->getSetterMethodDecl(); |
| 194 | assert(OMD && "Invalid call to generate setter (empty method)"); |
| 195 | // FIXME: This is rather murky, we create this here since they will |
| 196 | // not have been created by Sema for us. |
| 197 | OMD->createImplicitParams(getContext()); |
| 198 | StartObjCMethod(OMD); |
| 199 | |
| 200 | switch (PD->getSetterKind()) { |
| 201 | case ObjCPropertyDecl::Assign: break; |
| 202 | case ObjCPropertyDecl::Copy: |
| 203 | CGM.ErrorUnsupported(PID, "Obj-C setter with 'copy'"); |
| 204 | break; |
| 205 | case ObjCPropertyDecl::Retain: |
| 206 | CGM.ErrorUnsupported(PID, "Obj-C setter with 'retain'"); |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | // FIXME: What about nonatomic? |
| 211 | SourceLocation Loc = PD->getLocation(); |
| 212 | ValueDecl *Self = OMD->getSelfDecl(); |
| 213 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
| 214 | DeclRefExpr Base(Self, Self->getType(), Loc); |
| 215 | ParmVarDecl *ArgDecl = OMD->getParamDecl(0); |
| 216 | DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc); |
| 217 | ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, |
| 218 | true, true); |
| 219 | BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign, |
| 220 | Ivar->getType(), Loc); |
| 221 | EmitStmt(&Assign); |
| 222 | |
| 223 | FinishFunction(); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 226 | llvm::Value *CodeGenFunction::LoadObjCSelf(void) { |
| 227 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 228 | return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self"); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 231 | RValue CodeGenFunction::EmitObjCPropertyGet(const ObjCPropertyRefExpr *E) { |
| 232 | // Determine getter selector. |
| 233 | Selector S; |
| 234 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(E->getDecl())) { |
| 235 | S = MD->getSelector(); |
| 236 | } else { |
| 237 | S = cast<ObjCPropertyDecl>(E->getDecl())->getGetterName(); |
| 238 | } |
| 239 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 240 | return CGM.getObjCRuntime(). |
| 241 | GenerateMessageSend(*this, E->getType(), S, |
| 242 | EmitScalarExpr(E->getBase()), |
| 243 | false, CallArgList()); |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 246 | void CodeGenFunction::EmitObjCPropertySet(const ObjCPropertyRefExpr *E, |
| 247 | RValue Src) { |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 248 | Selector S; |
| 249 | if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(E->getDecl())) { |
| 250 | S = PD->getSetterName(); |
| 251 | } else { |
| 252 | // FIXME: How can we have a method decl here? |
| 253 | ErrorUnsupported(E, "Objective-C property setter call"); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | CallArgList Args; |
| 258 | EmitCallArg(Src, E->getType(), Args); |
| 259 | CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S, |
| 260 | EmitScalarExpr(E->getBase()), |
| 261 | false, Args); |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame^] | 264 | void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S) |
| 265 | { |
| 266 | ErrorUnsupported(&S, "for ... in statement"); |
| 267 | } |
| 268 | |
Ted Kremenek | 2979ec7 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 269 | CGObjCRuntime::~CGObjCRuntime() {} |