CGObjCGNU.cpp revision b3716efb0966620f205c939b671b579ad331ca0d
1//===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===// 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 provides Objective-C code generation targetting the GNU runtime. The 11// class in this file generates structures used by the GNU Objective-C runtime 12// library. These structures are defined in objc/objc.h and objc/objc-api.h in 13// the GNU runtime distribution. 14// 15//===----------------------------------------------------------------------===// 16 17#include "CGObjCRuntime.h" 18#include "CodeGenModule.h" 19#include "CodeGenFunction.h" 20#include "clang/AST/ASTContext.h" 21#include "clang/AST/Decl.h" 22#include "clang/AST/DeclObjC.h" 23#include "llvm/Module.h" 24#include "llvm/ADT/SmallVector.h" 25#include "llvm/ADT/StringMap.h" 26#include "llvm/Support/Compiler.h" 27#include "llvm/Target/TargetData.h" 28#include <map> 29 30 31using namespace clang; 32using namespace CodeGen; 33using llvm::dyn_cast; 34 35// The version of the runtime that this class targets. Must match the version 36// in the runtime. 37static const int RuntimeVersion = 8; 38static const int ProtocolVersion = 2; 39 40namespace { 41class CGObjCGNU : public CodeGen::CGObjCRuntime { 42private: 43 CodeGen::CodeGenModule &CGM; 44 llvm::Module &TheModule; 45 const llvm::PointerType *SelectorTy; 46 const llvm::PointerType *PtrToInt8Ty; 47 const llvm::FunctionType *IMPTy; 48 const llvm::PointerType *IdTy; 49 const llvm::IntegerType *IntTy; 50 const llvm::PointerType *PtrTy; 51 const llvm::IntegerType *LongTy; 52 const llvm::PointerType *PtrToIntTy; 53 std::vector<llvm::Constant*> Classes; 54 std::vector<llvm::Constant*> Categories; 55 std::vector<llvm::Constant*> ConstantStrings; 56 llvm::Function *LoadFunction; 57 llvm::StringMap<llvm::Constant*> ExistingProtocols; 58 typedef std::pair<std::string, std::string> TypedSelector; 59 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors; 60 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors; 61 // Some zeros used for GEPs in lots of places. 62 llvm::Constant *Zeros[2]; 63 llvm::Constant *NULLPtr; 64private: 65 llvm::Constant *GenerateIvarList( 66 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames, 67 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes, 68 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets); 69 llvm::Constant *GenerateMethodList(const std::string &ClassName, 70 const std::string &CategoryName, 71 const llvm::SmallVectorImpl<Selector> &MethodSels, 72 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes, 73 bool isClassMethodList); 74 llvm::Constant *GenerateProtocolList( 75 const llvm::SmallVectorImpl<std::string> &Protocols); 76 llvm::Constant *GenerateClassStructure( 77 llvm::Constant *MetaClass, 78 llvm::Constant *SuperClass, 79 unsigned info, 80 const char *Name, 81 llvm::Constant *Version, 82 llvm::Constant *InstanceSize, 83 llvm::Constant *IVars, 84 llvm::Constant *Methods, 85 llvm::Constant *Protocols); 86 llvm::Constant *GenerateProtocolMethodList( 87 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames, 88 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes); 89 llvm::Constant *MakeConstantString(const std::string &Str, const std::string 90 &Name=""); 91 llvm::Constant *MakeGlobal(const llvm::StructType *Ty, 92 std::vector<llvm::Constant*> &V, const std::string &Name=""); 93 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty, 94 std::vector<llvm::Constant*> &V, const std::string &Name=""); 95public: 96 CGObjCGNU(CodeGen::CodeGenModule &cgm); 97 virtual llvm::Constant *GenerateConstantString(const std::string &String); 98 virtual CodeGen::RValue 99 GenerateMessageSend(CodeGen::CodeGenFunction &CGF, 100 QualType ResultType, 101 Selector Sel, 102 llvm::Value *Receiver, 103 bool IsClassMessage, 104 const CallArgList &CallArgs); 105 virtual CodeGen::RValue 106 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, 107 QualType ResultType, 108 Selector Sel, 109 const ObjCInterfaceDecl *Class, 110 llvm::Value *Receiver, 111 bool IsClassMessage, 112 const CallArgList &CallArgs); 113 virtual llvm::Value *GetClass(CGBuilderTy &Builder, 114 const ObjCInterfaceDecl *OID); 115 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); 116 117 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, 118 const ObjCContainerDecl *CD); 119 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); 120 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); 121 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, 122 const ObjCProtocolDecl *PD); 123 virtual void GenerateProtocol(const ObjCProtocolDecl *PD); 124 virtual llvm::Function *ModuleInitFunction(); 125 virtual llvm::Function *GetPropertyGetFunction(); 126 virtual llvm::Function *GetPropertySetFunction(); 127 virtual llvm::Function *EnumerationMutationFunction(); 128 129 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, 130 const Stmt &S); 131 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, 132 const ObjCAtThrowStmt &S); 133 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, 134 llvm::Value *AddrWeakObj); 135 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, 136 llvm::Value *src, llvm::Value *dst); 137 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, 138 llvm::Value *src, llvm::Value *dest); 139 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, 140 llvm::Value *src, llvm::Value *dest); 141 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, 142 llvm::Value *src, llvm::Value *dest); 143 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, 144 QualType ObjectTy, 145 llvm::Value *BaseValue, 146 const ObjCIvarDecl *Ivar, 147 const FieldDecl *Field, 148 unsigned CVRQualifiers); 149}; 150} // end anonymous namespace 151 152 153 154static std::string SymbolNameForClass(const std::string &ClassName) { 155 return ".objc_class_" + ClassName; 156} 157 158static std::string SymbolNameForMethod(const std::string &ClassName, const 159 std::string &CategoryName, const std::string &MethodName, bool isClassMethod) 160{ 161 return "._objc_method_" + ClassName +"("+CategoryName+")"+ 162 (isClassMethod ? "+" : "-") + MethodName; 163} 164 165CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm) 166 : CGM(cgm), TheModule(CGM.getModule()) { 167 IntTy = cast<llvm::IntegerType>( 168 CGM.getTypes().ConvertType(CGM.getContext().IntTy)); 169 LongTy = cast<llvm::IntegerType>( 170 CGM.getTypes().ConvertType(CGM.getContext().LongTy)); 171 172 Zeros[0] = llvm::ConstantInt::get(LongTy, 0); 173 Zeros[1] = Zeros[0]; 174 NULLPtr = llvm::ConstantPointerNull::get( 175 llvm::PointerType::getUnqual(llvm::Type::Int8Ty)); 176 // C string type. Used in lots of places. 177 PtrToInt8Ty = 178 llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 179 // Get the selector Type. 180 SelectorTy = cast<llvm::PointerType>( 181 CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType())); 182 183 PtrToIntTy = llvm::PointerType::getUnqual(IntTy); 184 PtrTy = PtrToInt8Ty; 185 186 // Object type 187 IdTy = cast<llvm::PointerType>( 188 CGM.getTypes().ConvertType(CGM.getContext().getObjCIdType())); 189 190 // IMP type 191 std::vector<const llvm::Type*> IMPArgs; 192 IMPArgs.push_back(IdTy); 193 IMPArgs.push_back(SelectorTy); 194 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true); 195} 196// This has to perform the lookup every time, since posing and related 197// techniques can modify the name -> class mapping. 198llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder, 199 const ObjCInterfaceDecl *OID) { 200 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString()); 201 ClassName = Builder.CreateStructGEP(ClassName, 0); 202 203 llvm::Constant *ClassLookupFn = 204 TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty, 205 NULL); 206 return Builder.CreateCall(ClassLookupFn, ClassName); 207} 208 209/// GetSelector - Return the pointer to the unique'd string for this selector. 210llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) { 211 // FIXME: uniquing on the string is wasteful, unique on Sel instead! 212 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()]; 213 if (US == 0) 214 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy), 215 llvm::GlobalValue::InternalLinkage, 216 ".objc_untyped_selector_alias", 217 NULL, &TheModule); 218 219 return Builder.CreateLoad(US); 220 221} 222 223llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str, 224 const std::string &Name) { 225 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str); 226 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true, 227 llvm::GlobalValue::InternalLinkage, 228 ConstStr, Name, &TheModule); 229 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2); 230} 231llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty, 232 std::vector<llvm::Constant*> &V, const std::string &Name) { 233 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V); 234 return new llvm::GlobalVariable(Ty, false, 235 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule); 236} 237llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty, 238 std::vector<llvm::Constant*> &V, const std::string &Name) { 239 llvm::Constant *C = llvm::ConstantArray::get(Ty, V); 240 return new llvm::GlobalVariable(Ty, false, 241 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule); 242} 243 244/// Generate an NSConstantString object. 245//TODO: In case there are any crazy people still using the GNU runtime without 246//an OpenStep implementation, this should let them select their own class for 247//constant strings. 248llvm::Constant *CGObjCGNU::GenerateConstantString(const std::string &Str) { 249 std::vector<llvm::Constant*> Ivars; 250 Ivars.push_back(NULLPtr); 251 Ivars.push_back(MakeConstantString(Str)); 252 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size())); 253 llvm::Constant *ObjCStr = MakeGlobal( 254 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL), 255 Ivars, ".objc_str"); 256 ConstantStrings.push_back( 257 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty)); 258 return ObjCStr; 259} 260 261///Generates a message send where the super is the receiver. This is a message 262///send to self with special delivery semantics indicating which class's method 263///should be called. 264CodeGen::RValue 265CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, 266 QualType ResultType, 267 Selector Sel, 268 const ObjCInterfaceDecl *Class, 269 llvm::Value *Receiver, 270 bool IsClassMessage, 271 const CallArgList &CallArgs) { 272 llvm::Value *cmd = GetSelector(CGF.Builder, Sel); 273 274 CallArgList ActualArgs; 275 276 ActualArgs.push_back( 277 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)), 278 CGF.getContext().getObjCIdType())); 279 ActualArgs.push_back(std::make_pair(RValue::get(cmd), 280 CGF.getContext().getObjCSelType())); 281 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); 282 283 CodeGenTypes &Types = CGM.getTypes(); 284 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); 285 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false); 286 287 288 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass(); 289 // TODO: This should be cached, not looked up every time. 290 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass); 291 292 293 // Construct the structure used to look up the IMP 294 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(), 295 IdTy, NULL); 296 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy); 297 298 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0)); 299 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1)); 300 301 // Get the IMP 302 llvm::Constant *lookupFunction = 303 TheModule.getOrInsertFunction("objc_msg_lookup_super", 304 llvm::PointerType::getUnqual(impType), 305 llvm::PointerType::getUnqual(ObjCSuperTy), 306 SelectorTy, NULL); 307 llvm::Value *lookupArgs[] = {ObjCSuper, cmd}; 308 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs, 309 lookupArgs+2); 310 311 return CGF.EmitCall(FnInfo, imp, ActualArgs); 312} 313 314/// Generate code for a message send expression. 315CodeGen::RValue 316CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, 317 QualType ResultType, 318 Selector Sel, 319 llvm::Value *Receiver, 320 bool IsClassMessage, 321 const CallArgList &CallArgs) { 322 llvm::Value *cmd = GetSelector(CGF.Builder, Sel); 323 CallArgList ActualArgs; 324 325 ActualArgs.push_back( 326 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)), 327 CGF.getContext().getObjCIdType())); 328 ActualArgs.push_back(std::make_pair(RValue::get(cmd), 329 CGF.getContext().getObjCSelType())); 330 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); 331 332 CodeGenTypes &Types = CGM.getTypes(); 333 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); 334 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false); 335 336 llvm::Constant *lookupFunction = 337 TheModule.getOrInsertFunction("objc_msg_lookup", 338 llvm::PointerType::getUnqual(impType), 339 Receiver->getType(), SelectorTy, NULL); 340 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd); 341 342 return CGF.EmitCall(FnInfo, imp, ActualArgs); 343} 344 345/// Generates a MethodList. Used in construction of a objc_class and 346/// objc_category structures. 347llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName, 348 const std::string &CategoryName, 349 const llvm::SmallVectorImpl<Selector> &MethodSels, 350 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes, 351 bool isClassMethodList) { 352 // Get the method structure type. 353 llvm::StructType *ObjCMethodTy = llvm::StructType::get( 354 PtrToInt8Ty, // Really a selector, but the runtime creates it us. 355 PtrToInt8Ty, // Method types 356 llvm::PointerType::getUnqual(IMPTy), //Method pointer 357 NULL); 358 std::vector<llvm::Constant*> Methods; 359 std::vector<llvm::Constant*> Elements; 360 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) { 361 Elements.clear(); 362 llvm::Constant *C = 363 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString()); 364 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2)); 365 Elements.push_back( 366 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2)); 367 llvm::Constant *Method = 368 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName, 369 MethodSels[i].getAsString(), 370 isClassMethodList)); 371 Method = llvm::ConstantExpr::getBitCast(Method, 372 llvm::PointerType::getUnqual(IMPTy)); 373 Elements.push_back(Method); 374 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements)); 375 } 376 377 // Array of method structures 378 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy, 379 MethodSels.size()); 380 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy, 381 Methods); 382 383 // Structure containing list pointer, array and array count 384 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields; 385 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get(); 386 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy); 387 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy, 388 IntTy, 389 ObjCMethodArrayTy, 390 NULL); 391 // Refine next pointer type to concrete type 392 llvm::cast<llvm::OpaqueType>( 393 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy); 394 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get()); 395 396 Methods.clear(); 397 Methods.push_back(llvm::ConstantPointerNull::get( 398 llvm::PointerType::getUnqual(ObjCMethodListTy))); 399 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 400 MethodTypes.size())); 401 Methods.push_back(MethodArray); 402 403 // Create an instance of the structure 404 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list"); 405} 406 407/// Generates an IvarList. Used in construction of a objc_class. 408llvm::Constant *CGObjCGNU::GenerateIvarList( 409 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames, 410 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes, 411 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) { 412 // Get the method structure type. 413 llvm::StructType *ObjCIvarTy = llvm::StructType::get( 414 PtrToInt8Ty, 415 PtrToInt8Ty, 416 IntTy, 417 NULL); 418 std::vector<llvm::Constant*> Ivars; 419 std::vector<llvm::Constant*> Elements; 420 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) { 421 Elements.clear(); 422 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i], 423 Zeros, 2)); 424 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i], 425 Zeros, 2)); 426 Elements.push_back(IvarOffsets[i]); 427 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements)); 428 } 429 430 // Array of method structures 431 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy, 432 IvarNames.size()); 433 434 435 Elements.clear(); 436 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size())); 437 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars)); 438 // Structure containing array and array count 439 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy, 440 ObjCIvarArrayTy, 441 NULL); 442 443 // Create an instance of the structure 444 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list"); 445} 446 447/// Generate a class structure 448llvm::Constant *CGObjCGNU::GenerateClassStructure( 449 llvm::Constant *MetaClass, 450 llvm::Constant *SuperClass, 451 unsigned info, 452 const char *Name, 453 llvm::Constant *Version, 454 llvm::Constant *InstanceSize, 455 llvm::Constant *IVars, 456 llvm::Constant *Methods, 457 llvm::Constant *Protocols) { 458 // Set up the class structure 459 // Note: Several of these are char*s when they should be ids. This is 460 // because the runtime performs this translation on load. 461 llvm::StructType *ClassTy = llvm::StructType::get( 462 PtrToInt8Ty, // class_pointer 463 PtrToInt8Ty, // super_class 464 PtrToInt8Ty, // name 465 LongTy, // version 466 LongTy, // info 467 LongTy, // instance_size 468 IVars->getType(), // ivars 469 Methods->getType(), // methods 470 // These are all filled in by the runtime, so we pretend 471 PtrTy, // dtable 472 PtrTy, // subclass_list 473 PtrTy, // sibling_class 474 PtrTy, // protocols 475 PtrTy, // gc_object_type 476 NULL); 477 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0); 478 llvm::Constant *NullP = 479 llvm::ConstantPointerNull::get(PtrTy); 480 // Fill in the structure 481 std::vector<llvm::Constant*> Elements; 482 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty)); 483 Elements.push_back(SuperClass); 484 Elements.push_back(MakeConstantString(Name, ".class_name")); 485 Elements.push_back(Zero); 486 Elements.push_back(llvm::ConstantInt::get(LongTy, info)); 487 Elements.push_back(InstanceSize); 488 Elements.push_back(IVars); 489 Elements.push_back(Methods); 490 Elements.push_back(NullP); 491 Elements.push_back(NullP); 492 Elements.push_back(NullP); 493 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy)); 494 Elements.push_back(NullP); 495 // Create an instance of the structure 496 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name)); 497} 498 499llvm::Constant *CGObjCGNU::GenerateProtocolMethodList( 500 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames, 501 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) { 502 // Get the method structure type. 503 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get( 504 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us. 505 PtrToInt8Ty, 506 NULL); 507 std::vector<llvm::Constant*> Methods; 508 std::vector<llvm::Constant*> Elements; 509 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) { 510 Elements.clear(); 511 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i], 512 Zeros, 2)); 513 Elements.push_back( 514 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2)); 515 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements)); 516 } 517 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy, 518 MethodNames.size()); 519 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods); 520 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get( 521 IntTy, ObjCMethodArrayTy, NULL); 522 Methods.clear(); 523 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size())); 524 Methods.push_back(Array); 525 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list"); 526} 527// Create the protocol list structure used in classes, categories and so on 528llvm::Constant *CGObjCGNU::GenerateProtocolList( 529 const llvm::SmallVectorImpl<std::string> &Protocols) { 530 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty, 531 Protocols.size()); 532 llvm::StructType *ProtocolListTy = llvm::StructType::get( 533 PtrTy, //Should be a recurisve pointer, but it's always NULL here. 534 LongTy,//FIXME: Should be size_t 535 ProtocolArrayTy, 536 NULL); 537 std::vector<llvm::Constant*> Elements; 538 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end(); 539 iter != endIter ; iter++) { 540 llvm::Constant *Ptr = 541 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty); 542 Elements.push_back(Ptr); 543 } 544 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy, 545 Elements); 546 Elements.clear(); 547 Elements.push_back(NULLPtr); 548 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size())); 549 Elements.push_back(ProtocolArray); 550 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list"); 551} 552 553llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder, 554 const ObjCProtocolDecl *PD) { 555 return ExistingProtocols[PD->getNameAsString()]; 556} 557 558void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) { 559 ASTContext &Context = CGM.getContext(); 560 std::string ProtocolName = PD->getNameAsString(); 561 llvm::SmallVector<std::string, 16> Protocols; 562 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(), 563 E = PD->protocol_end(); PI != E; ++PI) 564 Protocols.push_back((*PI)->getNameAsString()); 565 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames; 566 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; 567 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(), 568 E = PD->instmeth_end(); iter != E; iter++) { 569 std::string TypeStr; 570 Context.getObjCEncodingForMethodDecl(*iter, TypeStr); 571 InstanceMethodNames.push_back( 572 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString())); 573 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); 574 } 575 // Collect information about class methods: 576 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames; 577 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; 578 for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(), 579 endIter = PD->classmeth_end() ; iter != endIter ; iter++) { 580 std::string TypeStr; 581 Context.getObjCEncodingForMethodDecl((*iter),TypeStr); 582 ClassMethodNames.push_back( 583 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString())); 584 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); 585 } 586 587 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols); 588 llvm::Constant *InstanceMethodList = 589 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes); 590 llvm::Constant *ClassMethodList = 591 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes); 592 // Protocols are objects containing lists of the methods implemented and 593 // protocols adopted. 594 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy, 595 PtrToInt8Ty, 596 ProtocolList->getType(), 597 InstanceMethodList->getType(), 598 ClassMethodList->getType(), 599 NULL); 600 std::vector<llvm::Constant*> Elements; 601 // The isa pointer must be set to a magic number so the runtime knows it's 602 // the correct layout. 603 Elements.push_back(llvm::ConstantExpr::getIntToPtr( 604 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy)); 605 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name")); 606 Elements.push_back(ProtocolList); 607 Elements.push_back(InstanceMethodList); 608 Elements.push_back(ClassMethodList); 609 ExistingProtocols[ProtocolName] = 610 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements, 611 ".objc_protocol"), IdTy); 612} 613 614void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) { 615 std::string ClassName = OCD->getClassInterface()->getNameAsString(); 616 std::string CategoryName = OCD->getNameAsString(); 617 // Collect information about instance methods 618 llvm::SmallVector<Selector, 16> InstanceMethodSels; 619 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; 620 for (ObjCCategoryImplDecl::instmeth_iterator iter = OCD->instmeth_begin(), 621 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) { 622 InstanceMethodSels.push_back((*iter)->getSelector()); 623 std::string TypeStr; 624 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr); 625 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); 626 } 627 628 // Collect information about class methods 629 llvm::SmallVector<Selector, 16> ClassMethodSels; 630 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; 631 for (ObjCCategoryImplDecl::classmeth_iterator iter = OCD->classmeth_begin(), 632 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) { 633 ClassMethodSels.push_back((*iter)->getSelector()); 634 std::string TypeStr; 635 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr); 636 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); 637 } 638 639 // Collect the names of referenced protocols 640 llvm::SmallVector<std::string, 16> Protocols; 641 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface(); 642 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols(); 643 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(), 644 E = Protos.end(); I != E; ++I) 645 Protocols.push_back((*I)->getNameAsString()); 646 647 std::vector<llvm::Constant*> Elements; 648 Elements.push_back(MakeConstantString(CategoryName)); 649 Elements.push_back(MakeConstantString(ClassName)); 650 // Instance method list 651 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( 652 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes, 653 false), PtrTy)); 654 // Class method list 655 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( 656 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true), 657 PtrTy)); 658 // Protocol list 659 Elements.push_back(llvm::ConstantExpr::getBitCast( 660 GenerateProtocolList(Protocols), PtrTy)); 661 Categories.push_back(llvm::ConstantExpr::getBitCast( 662 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy, 663 PtrTy, PtrTy, NULL), Elements), PtrTy)); 664} 665 666void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) { 667 ASTContext &Context = CGM.getContext(); 668 669 // Get the superclass name. 670 const ObjCInterfaceDecl * SuperClassDecl = 671 OID->getClassInterface()->getSuperClass(); 672 std::string SuperClassName; 673 if (SuperClassDecl) 674 SuperClassName = SuperClassDecl->getNameAsString(); 675 676 // Get the class name 677 ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OID->getClassInterface(); 678 std::string ClassName = ClassDecl->getNameAsString(); 679 680 // Get the size of instances. For runtimes that support late-bound instances 681 // this should probably be something different (size just of instance 682 // varaibles in this class, not superclasses?). 683 int instanceSize = 0; 684 const llvm::Type *ObjTy = 0; 685 if (!LateBoundIVars()) { 686 ObjTy = CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl)); 687 instanceSize = CGM.getTargetData().getTypePaddedSize(ObjTy); 688 } else { 689 // This is required by newer ObjC runtimes. 690 assert(0 && "Late-bound instance variables not yet supported"); 691 } 692 693 // Collect information about instance variables. 694 llvm::SmallVector<llvm::Constant*, 16> IvarNames; 695 llvm::SmallVector<llvm::Constant*, 16> IvarTypes; 696 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets; 697 const llvm::StructLayout *Layout = 698 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy)); 699 ObjTy = llvm::PointerType::getUnqual(ObjTy); 700 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(), 701 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) { 702 // Store the name 703 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter) 704 ->getNameAsString())); 705 // Get the type encoding for this ivar 706 std::string TypeStr; 707 Context.getObjCEncodingForType((*iter)->getType(), TypeStr); 708 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); 709 // Get the offset 710 FieldDecl *Field = ClassDecl->lookupFieldDeclForIvar(Context, (*iter)); 711 int offset = 712 (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); 713 IvarOffsets.push_back( 714 llvm::ConstantInt::get(llvm::Type::Int32Ty, offset)); 715 } 716 717 // Collect information about instance methods 718 llvm::SmallVector<Selector, 16> InstanceMethodSels; 719 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; 720 for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(), 721 endIter = OID->instmeth_end() ; iter != endIter ; iter++) { 722 InstanceMethodSels.push_back((*iter)->getSelector()); 723 std::string TypeStr; 724 Context.getObjCEncodingForMethodDecl((*iter),TypeStr); 725 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); 726 } 727 728 // Collect information about class methods 729 llvm::SmallVector<Selector, 16> ClassMethodSels; 730 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; 731 for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(), 732 endIter = OID->classmeth_end() ; iter != endIter ; iter++) { 733 ClassMethodSels.push_back((*iter)->getSelector()); 734 std::string TypeStr; 735 Context.getObjCEncodingForMethodDecl((*iter),TypeStr); 736 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); 737 } 738 // Collect the names of referenced protocols 739 llvm::SmallVector<std::string, 16> Protocols; 740 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols(); 741 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(), 742 E = Protos.end(); I != E; ++I) 743 Protocols.push_back((*I)->getNameAsString()); 744 745 746 747 // Get the superclass pointer. 748 llvm::Constant *SuperClass; 749 if (!SuperClassName.empty()) { 750 SuperClass = MakeConstantString(SuperClassName, ".super_class_name"); 751 } else { 752 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty); 753 } 754 // Empty vector used to construct empty method lists 755 llvm::SmallVector<llvm::Constant*, 1> empty; 756 // Generate the method and instance variable lists 757 llvm::Constant *MethodList = GenerateMethodList(ClassName, "", 758 InstanceMethodSels, InstanceMethodTypes, false); 759 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "", 760 ClassMethodSels, ClassMethodTypes, true); 761 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes, 762 IvarOffsets); 763 //Generate metaclass for class methods 764 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr, 765 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList( 766 empty, empty, empty), ClassMethodList, NULLPtr); 767 // Generate the class structure 768 llvm::Constant *ClassStruct = 769 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L, 770 ClassName.c_str(), 0, 771 llvm::ConstantInt::get(LongTy, instanceSize), IvarList, 772 MethodList, GenerateProtocolList(Protocols)); 773 // Add class structure to list to be added to the symtab later 774 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty); 775 Classes.push_back(ClassStruct); 776} 777 778llvm::Function *CGObjCGNU::ModuleInitFunction() { 779 // Only emit an ObjC load function if no Objective-C stuff has been called 780 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() && 781 ExistingProtocols.empty() && TypedSelectors.empty() && 782 UntypedSelectors.empty()) 783 return NULL; 784 785 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>( 786 SelectorTy->getElementType()); 787 const llvm::Type *SelStructPtrTy = SelectorTy; 788 bool isSelOpaque = false; 789 if (SelStructTy == 0) { 790 SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL); 791 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy); 792 isSelOpaque = true; 793 } 794 795 // Name the ObjC types to make the IR a bit easier to read 796 TheModule.addTypeName(".objc_selector", SelStructPtrTy); 797 TheModule.addTypeName(".objc_id", IdTy); 798 TheModule.addTypeName(".objc_imp", IMPTy); 799 800 std::vector<llvm::Constant*> Elements; 801 // Generate statics list: 802 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty, 803 ConstantStrings.size() + 1); 804 ConstantStrings.push_back(NULLPtr); 805 Elements.push_back(MakeConstantString("NSConstantString", 806 ".objc_static_class_name")); 807 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings)); 808 llvm::StructType *StaticsListTy = 809 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL); 810 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy); 811 llvm::Constant *Statics = 812 MakeGlobal(StaticsListTy, Elements, ".objc_statics"); 813 llvm::ArrayType *StaticsListArrayTy = 814 llvm::ArrayType::get(StaticsListPtrTy, 2); 815 Elements.clear(); 816 Elements.push_back(Statics); 817 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy)); 818 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr"); 819 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy); 820 // Array of classes, categories, and constant objects 821 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty, 822 Classes.size() + Categories.size() + 2); 823 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy, 824 llvm::Type::Int16Ty, 825 llvm::Type::Int16Ty, 826 ClassListTy, NULL); 827 828 Elements.clear(); 829 // Pointer to an array of selectors used in this module. 830 std::vector<llvm::Constant*> Selectors; 831 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator 832 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end(); 833 iter != iterEnd ; ++iter) { 834 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name")); 835 Elements.push_back(MakeConstantString(iter->first.second, 836 ".objc_sel_types")); 837 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements)); 838 Elements.clear(); 839 } 840 for (llvm::StringMap<llvm::GlobalAlias*>::iterator 841 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end(); 842 iter != iterEnd; ++iter) { 843 Elements.push_back( 844 MakeConstantString(iter->getKeyData(), ".objc_sel_name")); 845 Elements.push_back(NULLPtr); 846 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements)); 847 Elements.clear(); 848 } 849 Elements.push_back(NULLPtr); 850 Elements.push_back(NULLPtr); 851 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements)); 852 Elements.clear(); 853 // Number of static selectors 854 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() )); 855 llvm::Constant *SelectorList = MakeGlobal( 856 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors, 857 ".objc_selector_list"); 858 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, 859 SelStructPtrTy)); 860 861 // Now that all of the static selectors exist, create pointers to them. 862 int index = 0; 863 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator 864 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end(); 865 iter != iterEnd; ++iter) { 866 llvm::Constant *Idxs[] = {Zeros[0], 867 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]}; 868 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, 869 true, llvm::GlobalValue::InternalLinkage, 870 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2), 871 ".objc_sel_ptr", &TheModule); 872 // If selectors are defined as an opaque type, cast the pointer to this 873 // type. 874 if (isSelOpaque) { 875 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, 876 llvm::PointerType::getUnqual(SelectorTy)); 877 } 878 (*iter).second->setAliasee(SelPtr); 879 } 880 for (llvm::StringMap<llvm::GlobalAlias*>::iterator 881 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end(); 882 iter != iterEnd; iter++) { 883 llvm::Constant *Idxs[] = {Zeros[0], 884 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]}; 885 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, true, 886 llvm::GlobalValue::InternalLinkage, 887 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2), 888 ".objc_sel_ptr", &TheModule); 889 // If selectors are defined as an opaque type, cast the pointer to this 890 // type. 891 if (isSelOpaque) { 892 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, 893 llvm::PointerType::getUnqual(SelectorTy)); 894 } 895 (*iter).second->setAliasee(SelPtr); 896 } 897 // Number of classes defined. 898 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty, 899 Classes.size())); 900 // Number of categories defined 901 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty, 902 Categories.size())); 903 // Create an array of classes, then categories, then static object instances 904 Classes.insert(Classes.end(), Categories.begin(), Categories.end()); 905 // NULL-terminated list of static object instances (mainly constant strings) 906 Classes.push_back(Statics); 907 Classes.push_back(NULLPtr); 908 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes); 909 Elements.push_back(ClassList); 910 // Construct the symbol table 911 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements); 912 913 // The symbol table is contained in a module which has some version-checking 914 // constants 915 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy, 916 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL); 917 Elements.clear(); 918 // Runtime version used for compatibility checking. 919 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion)); 920 //FIXME: Should be sizeof(ModuleTy) 921 Elements.push_back(llvm::ConstantInt::get(LongTy, 16)); 922 //FIXME: Should be the path to the file where this module was declared 923 Elements.push_back(NULLPtr); 924 Elements.push_back(SymTab); 925 llvm::Value *Module = MakeGlobal(ModuleTy, Elements); 926 927 // Create the load function calling the runtime entry point with the module 928 // structure 929 std::vector<const llvm::Type*> VoidArgs; 930 llvm::Function * LoadFunction = llvm::Function::Create( 931 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false), 932 llvm::GlobalValue::InternalLinkage, ".objc_load_function", 933 &TheModule); 934 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction); 935 CGBuilderTy Builder; 936 Builder.SetInsertPoint(EntryBB); 937 llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class", 938 llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL); 939 Builder.CreateCall(Register, Module); 940 Builder.CreateRetVoid(); 941 return LoadFunction; 942} 943 944llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD, 945 const ObjCContainerDecl *CD) { 946 const ObjCCategoryImplDecl *OCD = 947 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext()); 948 std::string CategoryName = OCD ? OCD->getNameAsString() : ""; 949 std::string ClassName = OMD->getClassInterface()->getNameAsString(); 950 std::string MethodName = OMD->getSelector().getAsString(); 951 bool isClassMethod = !OMD->isInstanceMethod(); 952 953 CodeGenTypes &Types = CGM.getTypes(); 954 const llvm::FunctionType *MethodTy = 955 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); 956 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName, 957 MethodName, isClassMethod); 958 959 llvm::Function *Method = llvm::Function::Create(MethodTy, 960 llvm::GlobalValue::InternalLinkage, 961 FunctionName, 962 &TheModule); 963 return Method; 964} 965 966llvm::Function *CGObjCGNU::GetPropertyGetFunction() { 967 return 0; 968} 969 970llvm::Function *CGObjCGNU::GetPropertySetFunction() { 971 return 0; 972} 973 974llvm::Function *CGObjCGNU::EnumerationMutationFunction() { 975return 976 (llvm::Function*)TheModule.getOrInsertFunction( 977 "objc_enumerationMutation", llvm::Type::VoidTy, IdTy, NULL); 978} 979 980void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, 981 const Stmt &S) { 982 CGF.ErrorUnsupported(&S, "@try/@synchronized statement"); 983} 984 985void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, 986 const ObjCAtThrowStmt &S) { 987 CGF.ErrorUnsupported(&S, "@throw statement"); 988} 989 990llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, 991 llvm::Value *AddrWeakObj) 992{ 993 return 0; 994} 995 996void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, 997 llvm::Value *src, llvm::Value *dst) 998{ 999 return; 1000} 1001 1002void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, 1003 llvm::Value *src, llvm::Value *dst) 1004{ 1005 return; 1006} 1007 1008void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, 1009 llvm::Value *src, llvm::Value *dst) 1010{ 1011 return; 1012} 1013 1014void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, 1015 llvm::Value *src, llvm::Value *dst) 1016{ 1017 return; 1018} 1019 1020LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, 1021 QualType ObjectTy, 1022 llvm::Value *BaseValue, 1023 const ObjCIvarDecl *Ivar, 1024 const FieldDecl *Field, 1025 unsigned CVRQualifiers) { 1026 if (Ivar->isBitField()) 1027 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field), 1028 CVRQualifiers); 1029 // TODO: Add a special case for isa (index 0) 1030 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field); 1031 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp"); 1032 LValue LV = LValue::MakeAddr(V, 1033 Ivar->getType().getCVRQualifiers()|CVRQualifiers); 1034 LValue::SetObjCIvar(LV, true); 1035 return LV; 1036} 1037 1038CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){ 1039 return new CGObjCGNU(CGM); 1040} 1041