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