CGObjC.cpp revision 7ded7f4983dc4a20561db7a8d02c6b2435030961
1//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This contains code to emit Objective-C code as LLVM code. 11// 12//===----------------------------------------------------------------------===// 13 14#include "CGObjCRuntime.h" 15#include "CodeGenFunction.h" 16#include "CodeGenModule.h" 17#include "clang/AST/DeclObjC.h" 18 19using namespace clang; 20using namespace CodeGen; 21 22/// Emits an instance of NSConstantString representing the object. 23llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) { 24 std::string String(E->getString()->getStrData(), E->getString()->getByteLength()); 25 llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(String); 26 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); 27} 28 29/// Emit a selector. 30llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) { 31 // Untyped selector. 32 // Note that this implementation allows for non-constant strings to be passed 33 // as arguments to @selector(). Currently, the only thing preventing this 34 // behaviour is the type checking in the front end. 35 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector()); 36} 37 38 39 40llvm::Value *CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) { 41 // Only the lookup mechanism and first two arguments of the method 42 // implementation vary between runtimes. We can get the receiver and 43 // arguments in generic code. 44 45 CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 46 const Expr *ReceiverExpr = E->getReceiver(); 47 bool isSuperMessage = false; 48 // Find the receiver 49 llvm::Value *Receiver; 50 if (!ReceiverExpr) { 51 const char * classname = E->getClassName()->getName(); 52 if (!strcmp(classname, "super")) { 53 classname = E->getMethodDecl()->getClassInterface()->getName(); 54 } 55 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(classname); 56 ClassName = Builder.CreateStructGEP(ClassName, 0); 57 Receiver = Runtime.LookupClass(Builder, ClassName); 58 } else if (const PredefinedExpr *PDE = 59 dyn_cast<PredefinedExpr>(E->getReceiver())) { 60 assert(PDE->getIdentType() == PredefinedExpr::ObjCSuper); 61 isSuperMessage = true; 62 Receiver = LoadObjCSelf(); 63 } else { 64 Receiver = EmitScalarExpr(E->getReceiver()); 65 } 66 67 // Process the arguments 68 unsigned ArgC = E->getNumArgs(); 69 llvm::SmallVector<llvm::Value*, 16> Args; 70 for (unsigned i = 0; i != ArgC; ++i) { 71 const Expr *ArgExpr = E->getArg(i); 72 QualType ArgTy = ArgExpr->getType(); 73 if (!hasAggregateLLVMType(ArgTy)) { 74 // Scalar argument is passed by-value. 75 Args.push_back(EmitScalarExpr(ArgExpr)); 76 } else if (ArgTy->isAnyComplexType()) { 77 // Make a temporary alloca to pass the argument. 78 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy)); 79 EmitComplexExprIntoAddr(ArgExpr, DestMem, false); 80 Args.push_back(DestMem); 81 } else { 82 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy)); 83 EmitAggExpr(ArgExpr, DestMem, false); 84 Args.push_back(DestMem); 85 } 86 } 87 88 if (isSuperMessage) { 89 // super is only valid in an Objective-C method 90 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 91 const char *SuperClass = 92 OMD->getClassInterface()->getSuperClass()->getName(); 93 return Runtime.GenerateMessageSendSuper(Builder, ConvertType(E->getType()), 94 SuperClass, 95 Receiver, E->getSelector(), 96 &Args[0], Args.size()); 97 } 98 return Runtime.GenerateMessageSend(Builder, ConvertType(E->getType()), 99 Receiver, E->getSelector(), 100 &Args[0], Args.size()); 101} 102 103/// Generate an Objective-C method. An Objective-C method is a C function with 104/// its pointer, name, and types registered in the class struture. 105void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { 106 CurFn = CGM.getObjCRuntime().GenerateMethod(OMD); 107 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn); 108 109 // Create a marker to make it easy to insert allocas into the entryblock 110 // later. Don't create this with the builder, because we don't want it 111 // folded. 112 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); 113 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt", 114 EntryBB); 115 116 FnRetTy = OMD->getResultType(); 117 CurFuncDecl = OMD; 118 119 Builder.SetInsertPoint(EntryBB); 120 121 // Emit allocs for param decls. Give the LLVM Argument nodes names. 122 llvm::Function::arg_iterator AI = CurFn->arg_begin(); 123 124 if (hasAggregateLLVMType(OMD->getResultType())) { 125 ++AI; 126 } 127 // Add implicit parameters to the decl map. 128 // TODO: Add something to AST to let the runtime specify the names and types 129 // of these. 130 131 llvm::Value *&SelfEntry = LocalDeclMap[OMD->getSelfDecl()]; 132 const llvm::Type *IPTy = AI->getType(); 133 llvm::Value *DeclPtr = new llvm::AllocaInst(IPTy, 0, AI->getName() + 134 ".addr", AllocaInsertPt); 135 // Store the initial value into the alloca. 136 Builder.CreateStore(AI, DeclPtr); 137 SelfEntry = DeclPtr; 138 ++AI; 139 llvm::Value *&CmdEntry = LocalDeclMap[OMD->getCmdDecl()]; 140 IPTy = AI->getType(); 141 DeclPtr = new llvm::AllocaInst(IPTy, 0, AI->getName() + 142 ".addr", AllocaInsertPt); 143 // Store the initial value into the alloca. 144 Builder.CreateStore(AI, DeclPtr); 145 CmdEntry = DeclPtr; 146 147 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) { 148 assert(AI != CurFn->arg_end() && "Argument mismatch!"); 149 EmitParmDecl(*OMD->getParamDecl(i), AI); 150 } 151 152 GenerateFunction(OMD->getBody()); 153} 154 155llvm::Value *CodeGenFunction::LoadObjCSelf(void) 156{ 157 if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurFuncDecl)) { 158 ValueDecl *Decl = OMD->getSelfDecl(); 159 llvm::Value *SelfPtr = LocalDeclMap[&(*(Decl))]; 160 return Builder.CreateLoad(SelfPtr, "self"); 161 } 162 return NULL; 163} 164 165CGObjCRuntime::~CGObjCRuntime() {} 166